aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--booki.c37
1 files changed, 14 insertions, 23 deletions
diff --git a/booki.c b/booki.c
index d3670ff..79563fe 100644
--- a/booki.c
+++ b/booki.c
@@ -93,6 +93,9 @@ long parse_int(char* current_pos, char** new_pos) {
else return 0;
}
+#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) {
// TODO handle failure
@@ -100,18 +103,14 @@ struct es parse_string(char* current_pos, char** new_pos) {
char* value;
// leading spaces
- while ((c = *current_pos) != '"') {
- current_pos++;
- }
+ SEEK_UNTIL(current_pos, '"');
// go past the quote and set the position of the start of value
current_pos++;
value = current_pos;
// until the next quote
- while ((c = *current_pos) != '"') {
- current_pos++;
- }
+ SEEK_UNTIL(current_pos, '"');
struct es output;
output.len = current_pos - value;
@@ -136,9 +135,7 @@ void parse_book(char* current_pos, struct Book* book) {
while (c != '\n') {
// we start at the beginning of a line
attr = current_pos;
- while ((c = *current_pos) != '=') {
- current_pos++;
- }
+ SEEK_UNTIL(current_pos, '=');
// go past the equals sign
current_pos++;
@@ -179,14 +176,12 @@ void parse_book(char* current_pos, struct Book* book) {
} else if (ATTR_MATCH(attr, "on")) {
// this is always a list
// look until we hit the opening brace
- while ((c = *current_pos) != '[')
- current_pos++;
+ SEEK_UNTIL(current_pos, '[');
current_pos++; // go past opening brace
// loop past opening spaces
- while ((c = *current_pos) == ' ')
- current_pos++;
+ SEEK_WHILE(current_pos, ' ');
// so long as we keep seeing quotes, keep reading them
int on_count = 0;
@@ -199,19 +194,16 @@ void parse_book(char* current_pos, struct Book* book) {
current_pos = new_pos;
// go past any commas or opening spaces
- while ((c = *current_pos) == ' ')
- current_pos++;
- if (c != ',')
+ SEEK_WHILE(current_pos, ' ');
+ if (*current_pos != ',')
break;
- while ((c = *current_pos) != '"')
- current_pos++;
+
+ SEEK_UNTIL(current_pos, '"');
}
}
// go to (and then past) the newline
- while ((c = *current_pos) != '\n') {
- current_pos++;
- }
+ SEEK_UNTIL(current_pos, '\n');
PARSE_LINE += 1;
c = *(++current_pos);
}
@@ -234,8 +226,7 @@ char* next_book(char* current_pos) {
// current_pos is at the beginning of [[books]]
// pass it, and any spaces/newlines, then return
current_pos += 9; // [[books]]
- while (*current_pos != '\n')
- current_pos++;
+ SEEK_UNTIL(current_pos, '\n');
// current_pos is '\n', go past then return
PARSE_LINE += 1;