aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Winston2024-06-07 18:36:37 -0400
committerBen Winston2024-06-07 18:36:37 -0400
commitae60a8385a97689a61680b49400d70d553cfb299 (patch)
treeffff08a6c4da79ddbd20a1992b423a805687e5a8
parent2995f4c401894dbedf9307695036ddb33a8b2acc (diff)
use typedefs
-rw-r--r--booki.c72
1 files changed, 37 insertions, 35 deletions
diff --git a/booki.c b/booki.c
index 606471b..5b884d9 100644
--- a/booki.c
+++ b/booki.c
@@ -319,38 +319,39 @@ bool copy(char* src, char* dest) {
}
/*** strings ***/
+typedef struct es ES;
struct es {
int len;
char* ptr;
- struct es* next;
+ ES* next;
};
-struct es default_es = { 0, NULL, NULL };
+ES default_es = { 0, NULL, NULL };
-int concat_es_print(struct es* str, char* buf) {
+int concat_es_print(ES* str, char* buf) {
int size = sprintf(buf, "%.*s", str->len, str->ptr);
- struct es* next = str;
+ ES* next = str;
while ((next = next->next) != NULL)
size += sprintf(buf + size, ", %.*s", next->len, next->ptr);
return size;
}
-int concat_es_toml(struct es* str, char* buf) {
+int concat_es_toml(ES* str, char* buf) {
if (str->next == NULL) {
return sprintf(buf, "\"%.*s\"\n", str->len, str->ptr);
}
else {
int size = sprintf(buf, "[ \"%.*s\"", str->len, str->ptr);
- struct es* next = str;
+ ES* next = str;
while ((next = next->next) != NULL)
size += sprintf(buf + size, ", \"%.*s\"", next->len, next->ptr);
return size + sprintf(buf + size, " ]\n");
}
}
-void free_es(struct es* str) {
+void free_es(ES* str) {
if (str == NULL) return;
- struct es* tmp;
+ ES* tmp;
while (str != NULL) {
tmp = str->next;
free(str);
@@ -359,32 +360,33 @@ void free_es(struct es* str) {
}
/*** books ***/
+typedef struct Book BOOK;
struct Book {
int id;
- struct es title;
- struct es author;
+ ES title;
+ ES author;
int pages;
- struct es isbn;
- struct es language;
- struct es translator;
- struct es on;
+ ES isbn;
+ ES language;
+ ES translator;
+ ES on;
int published;
};
-void init_book(struct Book* book) {
+void init_book(BOOK* book) {
book->id = 0;
book->title = default_es;
book->author = default_es;
book->pages = 0;
book->isbn = default_es;
- struct es language = { 7, "English", NULL }; // default language
+ ES language = { 7, "English", NULL }; // default language
book->language = language;
book->translator = default_es;
book->on = default_es;
book->published = 0;
}
-void print_book(struct Book book, bool all_fields) {
+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);
@@ -415,7 +417,7 @@ void print_book(struct Book book, bool all_fields) {
}
}
-void write_book(struct Book book, FILE *output) {
+void write_book(BOOK book, FILE *output) {
fwrite("[[books]]\n", 1, 10, output);
char str[100];
int size;
@@ -463,7 +465,7 @@ void write_book(struct Book book, FILE *output) {
fwrite("\n", 1, 1, output); // trailing newline between books
}
-void free_book(struct Book book) {
+void free_book(BOOK book) {
// any string can be a list of strings
free_es(book.author.next);
free_es(book.translator.next);
@@ -510,7 +512,7 @@ long parse_int(char* current_pos, char** new_pos) {
#define SEEK_UNTIL(ptr, ch) while (*ptr != ch) ptr++;
#define SEEK_WHILE(ptr, ch) while (*ptr == ch) ptr++;
-struct es parse_string(char* current_pos, char** new_pos) {
+ES parse_string(char* current_pos, char** new_pos) {
// TODO handle failure
char* value;
@@ -524,7 +526,7 @@ struct es parse_string(char* current_pos, char** new_pos) {
// until the next quote
SEEK_UNTIL(current_pos, '"');
- struct es output;
+ ES output;
output.len = current_pos - value;
output.ptr = value;
output.next = NULL;
@@ -538,7 +540,7 @@ struct es parse_string(char* current_pos, char** new_pos) {
return output;
}
-struct es parse_strings(char* current_pos, char** new_pos) {
+ES parse_strings(char* current_pos, char** new_pos) {
// loop until we know what we have
SEEK_WHILE(current_pos, ' ');
if (*current_pos == '"') {
@@ -546,13 +548,13 @@ struct es parse_strings(char* current_pos, char** new_pos) {
} else if (*current_pos == '[') {
// get the first one
char* my_new_pos;
- struct es head = parse_string(current_pos, &my_new_pos);
+ ES head = parse_string(current_pos, &my_new_pos);
current_pos = my_new_pos;
- struct es* prev = &head;
+ ES* prev = &head;
while (*current_pos == ',') {
prev->next = (struct es*)malloc(sizeof(struct es));
- struct es str = parse_string(current_pos, &my_new_pos);
+ ES str = parse_string(current_pos, &my_new_pos);
prev->next->ptr = str.ptr;
prev->next->len = str.len;
prev->next->next = NULL;
@@ -574,7 +576,7 @@ struct es parse_strings(char* current_pos, char** new_pos) {
#define ATTR_MATCH(cand, attr) (strncmp(cand, attr, strlen(attr)) == 0)
-void parse_book(char* current_pos, struct Book* book) {
+void parse_book(char* current_pos, BOOK* book) {
char* attr;
char c = *current_pos;
char* new_pos;
@@ -589,23 +591,23 @@ void parse_book(char* current_pos, struct Book* book) {
// attr should be the name of the attribute, with (possibly) trailing spaces
if (ATTR_MATCH(attr, "title")) {
- struct es title = parse_string(current_pos, &new_pos);
+ ES title = parse_string(current_pos, &new_pos);
book->title = title;
current_pos = new_pos;
} else if (ATTR_MATCH(attr, "author")) {
- struct es author = parse_strings(current_pos, &new_pos);
+ ES author = parse_strings(current_pos, &new_pos);
book->author = author;
current_pos = new_pos;
} else if (ATTR_MATCH(attr, "language")) {
- struct es language = parse_strings(current_pos, &new_pos);
+ ES language = parse_strings(current_pos, &new_pos);
book->language = language;
current_pos = new_pos;
} else if (ATTR_MATCH(attr, "isbn")) {
- struct es isbn = parse_string(current_pos, &new_pos);
+ ES isbn = parse_string(current_pos, &new_pos);
book->isbn = isbn;
current_pos = new_pos;
} else if (ATTR_MATCH(attr, "translator")) {
- struct es translator = parse_strings(current_pos, &new_pos);
+ ES translator = parse_strings(current_pos, &new_pos);
book->translator = translator;
current_pos = new_pos;
} else if (ATTR_MATCH(attr, "pages")) {
@@ -621,7 +623,7 @@ void parse_book(char* current_pos, struct Book* book) {
book->id = id;
current_pos = new_pos;
} else if (ATTR_MATCH(attr, "on")) {
- struct es on = parse_strings(current_pos, &new_pos);
+ ES on = parse_strings(current_pos, &new_pos);
book->on = on;
current_pos = new_pos;
}
@@ -651,7 +653,7 @@ char* next_book(char* current_pos) {
/*** search ***/
-bool match_string(const char* pattern, const struct es text) {
+bool match_string(const char* pattern, const ES text) {
// empty pattern matches everything
if (!*pattern) return true;
@@ -800,7 +802,7 @@ void search(int argc, char* argv[], char* booki_file) {
// book loop
int book_count = 0;
- struct Book book;
+ BOOK book;
char* cur_data = data;
while ((cur_data = next_book(cur_data)) != NULL) {
init_book(&book);
@@ -914,7 +916,7 @@ void add(char *booki_file) {
FILE* output = fopen(booki_file, "a");
char* data = load_file(EDIT_FILE);
char* cur_data = data;
- struct Book book;
+ BOOK book;
while ((cur_data = next_book(cur_data)) != NULL) {
init_book(&book);
parse_book(cur_data, &book);