aboutsummaryrefslogtreecommitdiff
path: root/booki.c
diff options
context:
space:
mode:
authorBen Winston2024-05-28 20:20:48 -0400
committerBen Winston2024-05-28 20:20:48 -0400
commitf46b627c10605694375323394fc5697bd895198f (patch)
tree25db77cbfdaf14cd63cfa6dee767ac4517dbc641 /booki.c
parentf4822b352ab3d6dc57f21ae241b4f594ef106dd4 (diff)
WIP initial commit of linked list of strings
Diffstat (limited to 'booki.c')
-rw-r--r--booki.c53
1 files changed, 49 insertions, 4 deletions
diff --git a/booki.c b/booki.c
index 4a32065..680571f 100644
--- a/booki.c
+++ b/booki.c
@@ -16,9 +16,10 @@
struct es {
int len;
char* ptr;
+ struct es* next;
};
-struct es default_es = { 0, NULL };
+struct es default_es = { 0, NULL, NULL };
struct Book {
int id;
@@ -42,7 +43,7 @@ void init_book(struct Book* book) {
book->author = default_es;
book->pages = 0;
book->isbn = default_es;
- struct es language = { 7, "English" }; // default language
+ struct es language = { 7, "English", NULL }; // default language
book->language = language;
book->translator = default_es;
for (int i = 0; i < 5; i++)
@@ -111,6 +112,7 @@ struct es parse_string(char* current_pos, char** new_pos) {
struct es output;
output.len = current_pos - value;
output.ptr = value;
+ output.next = NULL;
// go past the quote
current_pos++;
@@ -121,6 +123,40 @@ struct es parse_string(char* current_pos, char** new_pos) {
return output;
}
+struct es parse_strings(char* current_pos, char** new_pos) {
+ // loop until we know what we have
+ SEEK_WHILE(current_pos, ' ');
+ if (*current_pos == '"') {
+ return parse_string(current_pos, new_pos);
+ } else if (*current_pos == '[') {
+ // get the first one
+ char* my_new_pos;
+ struct es head = parse_string(current_pos, &my_new_pos);
+ current_pos = my_new_pos;
+
+ struct es* prev = &head;
+ while (*current_pos == ',') {
+ prev->next = (struct es*)malloc(sizeof(struct es));
+ struct es str = parse_string(current_pos, &my_new_pos);
+ prev->next->ptr = str.ptr;
+ prev->next->len = str.len;
+ prev->next->next = NULL;
+ prev = prev->next;
+ current_pos = my_new_pos;
+ }
+
+ SEEK_UNTIL(current_pos, ']');
+ current_pos++;
+ *new_pos = current_pos;
+ return head;
+ } else {
+ printf("error at %x\n", current_pos);
+ SEEK_UNTIL(current_pos, '\n');
+ *new_pos = current_pos;
+ return default_es;
+ }
+}
+
#define ATTR_MATCH(cand, attr) (strncmp(cand, attr, strlen(attr)) == 0)
void parse_book(char* current_pos, struct Book* book) {
@@ -142,7 +178,7 @@ void parse_book(char* current_pos, struct Book* book) {
book->title = title;
current_pos = new_pos;
} else if (ATTR_MATCH(attr, "author")) {
- struct es author = parse_string(current_pos, &new_pos);
+ struct es author = parse_strings(current_pos, &new_pos);
book->author = author;
current_pos = new_pos;
} else if (ATTR_MATCH(attr, "language")) {
@@ -353,7 +389,16 @@ struct search_opt parse_search_options(int argc, char* argv[]) {
}
void print_book(struct Book book, bool all_fields) {
- printf("%.*s by %.*s\n", book.title.len, book.title.ptr, book.author.len, book.author.ptr);
+ if (book.author.next == NULL) {
+ printf("%.*s by %.*s\n", book.title.len, book.title.ptr, book.author.len, book.author.ptr);
+ } else {
+ char str[100];
+ int size = sprintf(str, "%.*s", book.author.len, book.author.ptr);
+ struct es* next = &(book.author);
+ while ((next = next->next) != NULL)
+ size += sprintf(str + size, ", %.*s", next->len, next->ptr);
+ printf("%.*s by %.*s\n", book.title.len, book.title.ptr, size, str);
+ }
if (all_fields) {
char* esfmt = " - %s: %.*s\n";
char* intfmt = " - %s: %d\n";