Fixed the negation of body conditions.
authorFrancois Fleuret <francois@fleuret.org>
Tue, 5 Feb 2013 08:06:02 +0000 (09:06 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Tue, 5 Feb 2013 08:06:02 +0000 (09:06 +0100)
mymail.c

index 6d57fed..b1734f2 100644 (file)
--- a/mymail.c
+++ b/mymail.c
@@ -270,6 +270,58 @@ int mbox_line_match_search(struct search_condition *condition,
   }
 }
 
+void update_body_hits(char *mail_filename, int position_in_mail,
+                      int nb_search_conditions, struct search_condition *search_conditions,
+                      int nb_body_conditions,
+                      int *hits) {
+  FILE *mail_file;
+  int header, n;
+  int last_mbox_line_was_empty;
+  char raw_mbox_line[BUFFER_SIZE];
+  int nb_body_hits;
+
+  nb_body_hits = 0;
+
+  header = 1;
+  mail_file = fopen(mail_filename, "r");
+
+  if(!mail_file) {
+    fprintf(stderr,
+            "mymail: Cannot open mbox '%s' for body scan.\n",
+            mail_filename);
+    exit(EXIT_FAILURE);
+  }
+
+  fseek(mail_file, position_in_mail, SEEK_SET);
+
+  if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
+    while(nb_body_hits < nb_body_conditions) {
+      last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
+
+      if(last_mbox_line_was_empty) { header = 0; }
+
+      if(!header) {
+        for(n = 0; n < nb_search_conditions; n++) {
+          if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
+            hits[n] =
+              (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
+            if(hits[n]) {
+              nb_body_hits++;
+            }
+          }
+        }
+      }
+
+      if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
+         (last_mbox_line_was_empty &&
+          is_a_leading_from_line(raw_mbox_line)))
+        break;
+    }
+  }
+
+  fclose(mail_file);
+}
+
 void search_in_db(const char *db_filename,
                   int nb_search_conditions,
                   struct search_condition *search_conditions,
@@ -349,50 +401,20 @@ void search_in_db(const char *db_filename,
           /* all conditions but the body ones are fine, check the body
              ones */
 
-          nb_fulfilled_body_conditions = 0;
-
           if(nb_body_conditions > 0) {
-            FILE *mail_file;
-            int header;
-
-            header = 1;
-            mail_file = fopen(current_mail_filename, "r");
-
-            if(!mail_file) {
-              fprintf(stderr,
-                      "mymail: Cannot open mbox '%s' for body scan.\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)) {
-              while(nb_fulfilled_body_conditions < nb_body_conditions) {
-                last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
-
-                if(last_mbox_line_was_empty) { header = 0; }
+            update_body_hits(current_mail_filename, current_position_in_mail,
+                             nb_search_conditions, search_conditions,
+                             nb_body_conditions,
+                             hits);
+          }
 
-                if(!header) {
-                  for(n = 0; n < nb_search_conditions; n++) {
-                    if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
-                      hits[n] =
-                        (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
-                      if(hits[n]) {
-                        nb_fulfilled_body_conditions++;
-                      }
-                    }
-                  }
-                }
+          nb_fulfilled_body_conditions = 0;
 
-                if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
-                   (last_mbox_line_was_empty &&
-                    is_a_leading_from_line(raw_mbox_line)))
-                  break;
-              }
+          for(n = 0; n < nb_search_conditions; n++) {
+            if(search_conditions[n].field_id == ID_BODY &&
+               xor(hits[n], search_conditions[n].negation)) {
+              nb_fulfilled_body_conditions++;
             }
-
-            fclose(mail_file);
           }
 
           if(nb_body_conditions == nb_fulfilled_body_conditions) {