aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--booki.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/booki.c b/booki.c
index 6af0e2c..afca7ed 100644
--- a/booki.c
+++ b/booki.c
@@ -28,6 +28,7 @@ struct Book {
struct es isbn;
struct es language;
struct es translator;
+ struct es on[5];
int published;
// maybe?
@@ -44,6 +45,8 @@ struct Book* init_book(struct Book* book) {
struct es language = { 7, "English" }; // default language
book->language = language;
book->translator = default_es;
+ for (int i = 0; i < 5; i++)
+ book->on[i] = default_es;
book->published = 0;
//book_count++;
@@ -174,6 +177,36 @@ void parse_book(char* current_pos, struct Book* book) {
int id = parse_int(current_pos, &new_pos);
book->id = id;
current_pos = new_pos;
+ } else if (ATTR_MATCH(attr, "on")) {
+ // this is always a list
+ // look until we hit the opening brace
+ while ((c = *current_pos) != '[')
+ current_pos++;
+
+ current_pos++; // go past opening brace
+
+ // loop past opening spaces
+ while ((c = *current_pos) == ' ')
+ current_pos++;
+
+ // so long as we keep seeing quotes, keep reading them
+ int on_count = 0;
+ struct es value;
+ while ((c = *current_pos) == '"') {
+ // read the string
+ value = parse_string(current_pos, &new_pos);
+ book->on[on_count] = value;
+ on_count++;
+ current_pos = new_pos;
+
+ // go past any commas or opening spaces
+ while ((c = *current_pos) == ' ')
+ current_pos++;
+ if (c != ',')
+ break;
+ while ((c = *current_pos) != '"')
+ current_pos++;
+ }
}
// go to (and then past) the newline
@@ -333,6 +366,19 @@ void print_book(struct Book book, bool all_fields) {
printf(intfmt, "pages", book.pages);
if (book.published)
printf(intfmt, "published", book.published);
+
+ int on_count = 0;
+ struct es tmp = book.on[on_count];
+ while (tmp.len != 0) {
+ if (on_count == 0)
+ printf(" - %s: [ %.*s", "on", tmp.len, tmp.ptr);
+ else
+ printf(", %.*s", tmp.len, tmp.ptr);
+ on_count++;
+ tmp = book.on[on_count];
+ }
+ if (on_count != 0)
+ printf(" ]\n");
}
}
@@ -372,6 +418,19 @@ char** search(int argc, char* argv[]) {
} else if (ATTR_MATCH(field, "language")) {
if (!regex_match(search_opts.args[i], book->language))
break;
+ } else if (ATTR_MATCH(field, "on")) {
+ int cnt = 0;
+ struct es tmp = book->on[cnt];
+ bool match = false;
+ while (tmp.len != 0) {
+ if (regex_match(search_opts.args[i], tmp))
+ match = true;
+ cnt++;
+ tmp = book->on[cnt];
+ }
+ if (!match)
+ break;
+
} else {
printf("unsupported field: %s\n", field);
break;