]> git.cworth.org Git - mnemon/commitdiff
Macro-ify some repeated code from parsing
authorCarl Worth <cworth@cworth.org>
Mon, 14 May 2007 21:03:00 +0000 (14:03 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 14 May 2007 21:03:00 +0000 (14:03 -0700)
mnemon.c

index 9bdef93f3c4994a87730ab5b324667e2bb13f8c3..63ea457f49a18deb3cfb4d034aba9b1a209cfc59 100644 (file)
--- a/mnemon.c
+++ b/mnemon.c
@@ -439,19 +439,24 @@ mnemon_load_category (mnemon_t            *mnemon,
 
     category = mnemon_get_category (mnemon, name);
 
+#define READ_LINE do {                                 \
+    bytes_read = getline (&line, &line_size, file);    \
+    if (bytes_read == -1)                              \
+       goto END_OF_FILE;                               \
+    line_count++;                                      \
+    chomp (line);                                      \
+} while (0)
+
     while (1) {
        int score;
        char *challenge, *response;
 
-       /* Read bin number (ignoring blank separator lines) */
-       do {
-           bytes_read = getline (&line, &line_size, file);
-           if (bytes_read == -1)
-               goto END_OF_FILE;
-           line_count++;
-           chomp (line);
-       } while (*line == '\0');
+       /* Ignore blank lines */
+       READ_LINE;
+       if (*line == '\0')
+           continue;
 
+       /* Read bin number */
        score = strtol (line, &end, 10);
        if (*end != '\0') {
            fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
@@ -460,19 +465,11 @@ mnemon_load_category (mnemon_t            *mnemon,
        }
 
        /* Read challenge */
-       bytes_read = getline (&line, &line_size, file);
-       if (bytes_read == -1)
-           break;
-       line_count++;
-       chomp (line);
+       READ_LINE;
        challenge = strdup (line);
 
        /* Read response */
-       bytes_read = getline (&line, &line_size, file);
-       if (bytes_read == -1)
-           break;
-       line_count++;
-       chomp (line);
+       READ_LINE;
        response = line;
 
        category_add_item (category, score, challenge, response);