Parse a config.h-style file. A dictionary containing name/value pairs is returned. If an optional dictionary is passed in as the second argument, it is used instead of a new dictionary.
(fp, vars=None)
| 551 | |
| 552 | |
| 553 | def parse_config_h(fp, vars=None): |
| 554 | """Parse a config.h-style file. |
| 555 | |
| 556 | A dictionary containing name/value pairs is returned. If an |
| 557 | optional dictionary is passed in as the second argument, it is |
| 558 | used instead of a new dictionary. |
| 559 | """ |
| 560 | if vars is None: |
| 561 | vars = {} |
| 562 | import re |
| 563 | define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n") |
| 564 | undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n") |
| 565 | |
| 566 | while True: |
| 567 | line = fp.readline() |
| 568 | if not line: |
| 569 | break |
| 570 | m = define_rx.match(line) |
| 571 | if m: |
| 572 | n, v = m.group(1, 2) |
| 573 | try: |
| 574 | if n in _ALWAYS_STR: |
| 575 | raise ValueError |
| 576 | v = int(v) |
| 577 | except ValueError: |
| 578 | pass |
| 579 | vars[n] = v |
| 580 | else: |
| 581 | m = undef_rx.match(line) |
| 582 | if m: |
| 583 | vars[m.group(1)] = 0 |
| 584 | return vars |
| 585 | |
| 586 | |
| 587 | def get_config_h_filename(): |
no test coverage detected