diff options
| author | Ben Winston | 2024-06-18 11:40:58 -0400 |
|---|---|---|
| committer | Ben Winston | 2024-06-18 11:40:58 -0400 |
| commit | 1a49fc0f83dbd6b7e4bbbf5af344baafd39e1999 (patch) | |
| tree | 91e36bca5c93aa747d1a6fb00beb6e5c0c492481 /booki.c | |
| parent | 18f520b8f3bdcf56e473efdb1ae793e97c24b7e4 (diff) | |
refactor: parse with a loop through fields instead of ifs
Diffstat (limited to 'booki.c')
| -rw-r--r-- | booki.c | 68 |
1 files changed, 27 insertions, 41 deletions
@@ -686,47 +686,33 @@ void parse_book(char* current_pos, BOOK* book) { // go past the equals sign current_pos++; - // attr should be the name of the attribute, with (possibly) trailing spaces - if (ATTR_MATCH(attr, "title")) { - ES title = parse_string(current_pos, &new_pos); - book->title = title; - current_pos = new_pos; - } else if (ATTR_MATCH(attr, "author")) { - ES author = parse_strings(current_pos, &new_pos); - book->author = author; - current_pos = new_pos; - } else if (ATTR_MATCH(attr, "language")) { - ES language = parse_strings(current_pos, &new_pos); - book->language = language; - current_pos = new_pos; - } else if (ATTR_MATCH(attr, "isbn")) { - ES isbn = parse_string(current_pos, &new_pos); - book->isbn = isbn; - current_pos = new_pos; - } else if (ATTR_MATCH(attr, "translator")) { - ES translator = parse_strings(current_pos, &new_pos); - book->translator = translator; - current_pos = new_pos; - } else if (ATTR_MATCH(attr, "pages")) { - int pages = parse_int(current_pos, &new_pos); - book->pages = pages; - current_pos = new_pos; - } else if (ATTR_MATCH(attr, "published")) { - int published = parse_int(current_pos, &new_pos); - book->published = published; - current_pos = new_pos; - } else if (ATTR_MATCH(attr, "id")) { - int id = parse_int(current_pos, &new_pos); - book->id = id; - current_pos = new_pos; - } else if (ATTR_MATCH(attr, "on")) { - ES on = parse_strings(current_pos, &new_pos); - book->on = on; - current_pos = new_pos; - } else { - // anything we didn't match should be captured as-is - SEEK_UNTIL(current_pos, '\n'); - add_to_other(book, attr, current_pos - attr); + + // loop through fields to find a match + DataField datafield; + ES* string_field; + int* number_field; + for (int i = 0; i < BOOK_FIELDS_COUNT; i++) { + datafield = BOOK_FIELDS[i]; + // if we haven't found it yet, keep looping + if (!ATTR_MATCH(attr, datafield.name)) + continue; + + if (datafield.type == booki_string) { + // set 'string_field' to be the pointer to the correct field in the struct + string_field = get_string_field(book, datafield.name); + // set the value of the struct's field to be the parsed string + *string_field = parse_string(current_pos, &new_pos); + current_pos = new_pos; + break; + } else if (datafield.type == booki_number) { + number_field = get_number_field(book, datafield.name); + *number_field = parse_int(current_pos, &new_pos); + current_pos = new_pos; + } else { + // anything we didn't match should be captured as-is + SEEK_UNTIL(current_pos, '\n'); + add_to_other(book, attr, current_pos - attr); + } } // go to (and then past) the newline |
