aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Winston2025-03-23 23:03:07 -0400
committerBen Winston2025-03-23 23:03:07 -0400
commit39b6385d9ce0bb7469334fd5c2ed76ea198c5196 (patch)
tree2267e9624936d8929080255ea58fbb88e488392e
parentfcc9deb379f8e288dc22b86968cbf93c8e655bb1 (diff)
update format to start/end with %, take in optional command line arg
-rw-r--r--booki.c50
1 files changed, 17 insertions, 33 deletions
diff --git a/booki.c b/booki.c
index 6a5315e..02d1105 100644
--- a/booki.c
+++ b/booki.c
@@ -10,6 +10,7 @@
#define EDIT_FILE "/tmp/booki-edit.toml"
#define FIXED_FILE "/tmp/booki-fixed.toml"
+#define DEFAULT_BOOK_FORMAT "%title% by %author%"
#define MAX_SEARCH_OPTS 5
@@ -461,8 +462,8 @@ int* get_number_field(BOOK* book, char* name) {
int format_book(BOOK book, char* format, char* buf) {
- // example: %title by %author
- // field is defined following %, ending with a space
+ // example: %title% by %author%
+ // field is defined between % characters, no spaces
int pos = 0;
char c;
@@ -473,12 +474,8 @@ int format_book(BOOK book, char* format, char* buf) {
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);
+ while ((c = *format)) {
+ if (parsing && c == '%') {
parsing = false;
// need to find what the field is
for (int i = 0; i < BOOK_FIELDS_COUNT; i++) {
@@ -491,14 +488,12 @@ int format_book(BOOK book, char* format, char* buf) {
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);
}
}
@@ -506,24 +501,14 @@ int format_book(BOOK book, char* format, char* buf) {
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++;
@@ -532,17 +517,9 @@ int format_book(BOOK book, char* format, char* buf) {
}
-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]";
+void print_book(BOOK book, bool all_fields, char *format) {
char buf[255];
- int len = format_book(book, fmt, buf);
+ int len = format_book(book, format, buf);
printf("%.*s\n", len, buf);
if (all_fields) {
@@ -910,6 +887,7 @@ bool match_int(char* pattern, int candidate) {
static struct option search_options[] = {
{"show", no_argument, 0, 's'},
{"edit", no_argument, 0, 'e'},
+ {"format", required_argument, 0, 'f'},
{0, 0, 0, 0} // marks the end of the array
};
@@ -917,6 +895,7 @@ struct search_opt {
int show;
int edit;
int count;
+ char* format;
char* opts[MAX_SEARCH_OPTS];
char* args[MAX_SEARCH_OPTS];
};
@@ -928,6 +907,7 @@ struct search_opt parse_search_options(int argc, char* argv[]) {
int count = 0;
int show = false;
int edit = false;
+ char *format = DEFAULT_BOOK_FORMAT;
// opt options
int opt;
@@ -943,6 +923,9 @@ struct search_opt parse_search_options(int argc, char* argv[]) {
case 'e':
edit = true;
break;
+ case 'f':
+ format = optarg;
+ break;
case '?':
// detect if we didn't get an operand
if (!argv[optind] || argv[optind][0] == '-') {
@@ -978,6 +961,7 @@ struct search_opt parse_search_options(int argc, char* argv[]) {
opt_out.count = count;
opt_out.show = show;
opt_out.edit = edit;
+ opt_out.format = format;
return opt_out;
}
@@ -1043,7 +1027,7 @@ void search(int argc, char* argv[], char* booki_file) {
if (search_opts.edit)
write_book(book, edit_file);
else
- print_book(book, search_opts.show);
+ print_book(book, search_opts.show, search_opts.format);
book_count++;
} else if (search_opts.edit) {
write_book(book, fixed_file);
@@ -1069,7 +1053,7 @@ void search(int argc, char* argv[], char* booki_file) {
while ((cur_data = next_book(cur_data)) != NULL) {
init_book(&book);
parse_book(cur_data, &book);
- print_book(book, true);
+ print_book(book, true, search_opts.format);
write_book(book, fixed_file);
free_book(book);
}
@@ -1123,7 +1107,7 @@ void add(char *booki_file) {
while ((cur_data = next_book(cur_data)) != NULL) {
init_book(&book);
parse_book(cur_data, &book);
- print_book(book, true);
+ print_book(book, true, DEFAULT_BOOK_FORMAT);
write_book(book, output);
free_book(book);
}