[Libpst-devel] PATCH: readpst.c code reorganisation

Arne Ahrend aahrend at web.de
Sun Feb 19 19:33:56 UTC 2006


This is a series of patches succesively taking chunks of code out of 
the huge main() function in readpst.c and into separate functions.

No functionality change, but improvement of readability.

Arne

First patch applied against readpst.c from libpst-0.5.1

--- readpst.c-00	2004-11-17 15:48:03.000000000 +0100
+++ readpst.c	2006-02-19 19:21:39.000000000 +0100
@@ -86,6 +86,7 @@ int32_t chr_count(char *str, char x);
 char *rfc2425_datetime_format(FILETIME *ft);
 char *rfc2445_datetime_format(FILETIME *ft);
 char *skip_header_prologue(char *headers);
+void write_separate_attachment(char f_name[], pst_item_attach* current_attach, int attach_num, pst_file* pst);
 // }}}1
 // Global Variables {{{1
 char *prog_name; 
@@ -143,7 +144,6 @@ int main(int argc, char** argv) {
   int overwrite = 0;
   int base64_body = 0;
   //  int encrypt = 0;
-  FILE *fp;
   char *enc; // base64 encoded attachment
   char *boundary = NULL, *b1, *b2; // the boundary marker between multipart sections
   char *temp = NULL; //temporary char pointer
@@ -832,35 +832,7 @@ int main(int argc, char** argv) {
 	    DEBUG_MAIN(("main: Data of attachment is NULL!. Size is supposed to be %i\n", item->current_attach->size));
 	  }
 	  if (mode == MODE_SEPERATE) {
-	    f->name = check_filename(f->name);
-	    if (item->current_attach->filename2 == NULL) {
-	      temp = xmalloc(strlen(f->name)+15);
-	      sprintf(temp, "%s-attach%i", f->name, attach_num);
-	    } else {
-	      temp = xmalloc(strlen(f->name)+strlen(item->current_attach->filename2)+15);
-	      fp = NULL; x=0;
-	      do {
-		if (fp != NULL) fclose(fp);
-		if (x == 0)
-		  sprintf(temp, "%s-%s", f->name, item->current_attach->filename2);
-		else
-		  sprintf(temp, "%s-%s-%i", f->name, item->current_attach->filename2, x);
-	      } while ((fp = fopen(temp, "r"))!=NULL && ++x < 99999999);
-	      if (x > 99999999) {
-	        DIE(("error finding attachment name. exhausted possibilities to %s\n", temp));
-	      }
-	    }
-	    DEBUG_MAIN(("main: Saving attachment to %s\n", temp));
-	    if ((fp = fopen(temp, "w")) == NULL) {
-	      WARN(("main: Cannot open attachment save file \"%s\"\n", temp));
-	    } else {
-	      if (item->current_attach->data != NULL)
-		fwrite(item->current_attach->data, 1, item->current_attach->size, fp);
-	      else {
-		pst_attach_to_file(&pstfile, item->current_attach, fp);
-	      }
-	      fclose(fp);
-	    }
+	    write_separate_attachment(f->name, item->current_attach, attach_num, &pstfile);
 	  } else {
 	    DEBUG_MAIN(("main: Attachment Size is %i\n", item->current_attach->size));
 	    DEBUG_MAIN(("main: Attachment Pointer is %p\n", item->current_attach->data));
@@ -1503,3 +1475,39 @@ char *skip_header_prologue(char *headers
 
 // vim:sw=4 ts=4:
 // vim600: set foldlevel=0 foldmethod=marker:
+
+void write_separate_attachment(char f_name[], pst_item_attach* current_attach, int attach_num, pst_file* pst)
+{
+	FILE *fp = NULL;
+	char *temp = NULL;
+	int x = 0;
+  
+	check_filename(f_name);
+	if (current_attach->filename2 == NULL) {
+		temp = xmalloc(strlen(f_name)+15);
+		sprintf(temp, "%s-attach%i", f_name, attach_num);
+	} else {
+		temp = xmalloc(strlen(f_name)+strlen(current_attach->filename2)+15);
+		do {
+			if (fp != NULL) fclose(fp);
+			if (x == 0)
+				sprintf(temp, "%s-%s", f_name, current_attach->filename2);
+			else
+				sprintf(temp, "%s-%s-%i", f_name, current_attach->filename2, x);
+		} while ((fp = fopen(temp, "r"))!=NULL && ++x < 99999999);
+		if (x > 99999999) {
+			DIE(("write_separate_attachment: error finding attachment name. exhausted possibilities to %s\n", temp));
+		}
+	}
+	DEBUG_MAIN(("write_separate_attachment: Saving attachment to %s\n", temp));
+	if ((fp = fopen(temp, "w")) == NULL) {
+		WARN(("write_separate_attachment: Cannot open attachment save file \"%s\"\n", temp));
+	} else {
+		if (current_attach->data != NULL)
+			fwrite(current_attach->data, 1, current_attach->size, fp);
+		else {
+			pst_attach_to_file(pst, current_attach, fp);
+		}
+		fclose(fp);
+	}
+}



More information about the Libpst-devel mailing list