Cosmetics.
authorFrancois Fleuret <francois@fleuret.org>
Fri, 8 Feb 2013 17:05:27 +0000 (18:05 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Fri, 8 Feb 2013 17:05:27 +0000 (18:05 +0100)
mymail.c

index fe2d9a0..54febff 100644 (file)
--- a/mymail.c
+++ b/mymail.c
@@ -162,11 +162,6 @@ char *parse_token(char *token_buffer, size_t token_buffer_size,
   return string;
 }
 
-void remove_eof(char *c) {
-  while(*c && *c != '\n' && *c != '\r') c++;
-  *c = '\0';
-}
-
 /********************************************************************/
 
 /* malloc with error checking.  */
@@ -350,6 +345,39 @@ void update_body_hits(char *mail_filename, int position_in_mail,
   fclose(mail_file);
 }
 
+void write_mail(const char *mail_filename, unsigned long int position_in_mail,
+                FILE *output_file) {
+  char raw_mbox_line[BUFFER_SIZE];
+  int last_mbox_line_was_empty;
+  FILE *mail_file;
+
+  mail_file = fopen(mail_filename, "r");
+
+  if(!mail_file) {
+    fprintf(stderr,
+            "mymail: Cannot open mbox '%s' for mail extraction.\n",
+            mail_filename);
+    exit(EXIT_FAILURE);
+  }
+
+  fseek(mail_file, position_in_mail, SEEK_SET);
+
+  if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
+    last_mbox_line_was_empty = 0;
+    fprintf(output_file, "%s", raw_mbox_line);
+    while(1) {
+      if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
+         (is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line))
+         )
+        break;
+      last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
+      fprintf(output_file, "%s", raw_mbox_line);
+    }
+  }
+
+  fclose(mail_file);
+}
+
 void search_in_db(const char *db_filename,
                   int nb_search_conditions,
                   struct search_condition *search_conditions,
@@ -357,13 +385,11 @@ void search_in_db(const char *db_filename,
 
   int hits[MAX_NB_SEARCH_CONDITIONS];
   char raw_db_line[BUFFER_SIZE];
-  char raw_mbox_line[BUFFER_SIZE];
   char current_mail_filename[PATH_MAX + 1];
   unsigned long int current_position_in_mail;
   char mbox_name[TOKEN_BUFFER_SIZE], *mbox_value;
   int mbox_id;
   int already_written, m, n;
-  int last_mbox_line_was_empty;
   int nb_body_conditions, nb_fulfilled_body_conditions;
   FILE *db_file;
 
@@ -382,6 +408,8 @@ void search_in_db(const char *db_filename,
     exit(EXIT_FAILURE);
   }
 
+  /* First, check the db file leading line integrity */
+
   if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
     if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
       fprintf(stderr,
@@ -396,6 +424,8 @@ void search_in_db(const char *db_filename,
     exit(EXIT_FAILURE);
   }
 
+  /* Then parse the said db file */
+
   current_position_in_mail = 0;
   already_written = 0;
 
@@ -426,8 +456,7 @@ void search_in_db(const char *db_filename,
 
         if(n == nb_search_conditions) {
 
-          /* all conditions but the body ones are fine, check the body
-             ones */
+          /* Now check the body ones */
 
           if(nb_body_conditions > 0) {
             update_body_hits(current_mail_filename, current_position_in_mail,
@@ -446,33 +475,7 @@ void search_in_db(const char *db_filename,
           }
 
           if(nb_body_conditions == nb_fulfilled_body_conditions) {
-            FILE *mail_file;
-
-            mail_file = fopen(current_mail_filename, "r");
-
-            if(!mail_file) {
-              fprintf(stderr,
-                      "mymail: Cannot open mbox '%s' for mail extraction.\n",
-                      current_mail_filename);
-              exit(EXIT_FAILURE);
-            }
-
-            fseek(mail_file, current_position_in_mail, SEEK_SET);
-
-            if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
-              last_mbox_line_was_empty = 0;
-              fprintf(output_file, "%s", raw_mbox_line);
-              while(1) {
-                if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
-                   (is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line))
-                   )
-                  break;
-                last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
-                fprintf(output_file, "%s", raw_mbox_line);
-              }
-            }
-
-            fclose(mail_file);
+            write_mail(current_mail_filename, current_position_in_mail, output_file);
           }
         }
       }
@@ -480,9 +483,8 @@ void search_in_db(const char *db_filename,
       for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
 
       mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
-      mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, ' ', mbox_value);
+      mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, '\n', mbox_value);
       current_position_in_mail = atol(position_in_file_string);
-      remove_eof(current_mail_filename);
       already_written = 0;
     }