diff options
Diffstat (limited to 'booki.c')
| -rw-r--r-- | booki.c | 84 |
1 files changed, 84 insertions, 0 deletions
@@ -459,11 +459,95 @@ int* get_number_field(BOOK* book, char* name) { return 0; } + +int format_book(BOOK book, char* format, char* buf) { + // example: %title by %author + // field is defined following %, ending with a space + int pos = 0; + + char c; + char field_buf[25]; + int field_pos = 0; + bool parsing = false; + + DataField datafield; + ES string_field; + int number_field; + while (true) { + c = *format; + + // are we done parsing? + if (parsing && (c == ' ' || c == '\0')) { + //printf("'%c': done parsing\n", c); + parsing = false; + // need to find what the field is + for (int i = 0; i < BOOK_FIELDS_COUNT; i++) { + datafield = BOOK_FIELDS[i]; + if (!ATTR_MATCH(field_buf, datafield.name)) + continue; + if (datafield.type == booki_string) { + string_field = *(get_string_field(&book, datafield.name)); + // if we don't have anything in this field, don't print it + if (!string_field.ptr) + break; + pos = pos + concat_es_print(&string_field, buf + pos); + //printf(esfmt, datafield.name, size, str); + // number fields + } else if (datafield.type == booki_number) { + number_field = *(get_number_field(&book, datafield.name)); + if (!number_field) + continue; + pos = pos + sprintf(buf + pos, "%d", number_field); + //printf(intfmt, datafield.name, number_field); + } + } + + // reset field buffer + for (int i = 0; i < field_pos; i++) + field_buf[i] = '\0'; + field_pos = 0; + + // capture the space, if it was there + if (c == ' ') { + buf[pos++] = c; + } else { + break; + } + // are we still parsing? + } else if (parsing) { + //printf("'%c': still parsing\n", c); + field_buf[field_pos++] = c; + // are we ready to parse? + } else if (!parsing && c == '%') { + //printf("'%c': start parsing\n", c); + parsing = true; + // are we not ready to parse? + } else if (!parsing) { + //printf("'%c': not parsing\n", c); + buf[pos++] = c; + } + format++; + } + return pos; +} + + void print_book(BOOK book, bool all_fields) { + /* char str[100]; int size = concat_es_print(&(book.author), str); printf("%.*s by %.*s\n", book.title.len, book.title.ptr, size, str); + */ + + // use a default format + char *fmt = "%title by %author [%language]"; + char buf[255]; + int len = format_book(book, fmt, buf); + printf("%.*s\n", len, buf); + if (all_fields) { + int size; + char str[100]; char* esfmt = " - %s: %.*s\n"; char* intfmt = " - %s: %d\n"; |
