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

Arne Ahrend aahrend at web.de
Sun Feb 19 19:39:38 UTC 2006


On Sun, 19 Feb 2006 20:33:56 +0100
Arne Ahrend <aahrend at web.de> wrote:

Fifth patch

> 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

--- readpst.c-04	2006-02-19 19:53:08.000000000 +0100
+++ readpst.c	2006-02-19 20:04:36.000000000 +0100
@@ -91,6 +91,7 @@ void write_inline_attachment(FILE* f_out
 void write_fabricated_header(FILE* f_output, pst_item_email* email, pst_item_attach* attach,
 			     char boundary[], int mode, char c_time[], time_t em_time);
 void write_normal_email(FILE* f_output, char f_name[], pst_item* item, int mode, pst_file* pst);
+void create_enter_dir(struct file_ll* f, char file_as[], int mode, int overwrite);
 // }}}1
 // Global Variables {{{1
 char *prog_name; 
@@ -262,54 +263,7 @@ int main(int argc, char** argv) {
   f->skip_count = 0;
   f->next = NULL;
   head = f;
-  if (mode == MODE_KMAIL)
-    f->name = mk_kmail_dir(item->file_as);
-  else if (mode == MODE_RECURSE)
-    f->name = mk_recurse_dir(item->file_as);
-  else if (mode == MODE_SEPERATE) {
-    // do similar stuff to recurse here.
-    mk_seperate_dir(item->file_as, overwrite);
-    f->name = (char*) xmalloc(10);
-    sprintf(f->name, "%09i", f->email_count);
-  } else {
-    f->name = (char*) malloc(strlen(item->file_as)+strlen(OUTPUT_TEMPLATE)+1);
-    sprintf(f->name, OUTPUT_TEMPLATE, item->file_as);
-  }
-
-  f->dname = (char*) malloc(strlen(item->file_as)+1);
-  strcpy(f->dname, item->file_as);
-
-  if (overwrite != 1 && mode != MODE_SEPERATE) {
-    // if overwrite is set to 1 we keep the existing name and don't modify anything
-    // we don't want to go changing the file name of the SEPERATE items
-    temp = (char*) malloc (strlen(f->name)+10); //enough room for 10 digits
-    sprintf(temp, "%s", f->name);
-    temp = check_filename(temp);
-    x = 0;
-    while ((f->output = fopen(temp, "r")) != NULL) {
-      DEBUG_MAIN(("main: need to increase filename cause one already exists with that name\n"));
-      DEBUG_MAIN(("main: - increasing it to %s%d\n", f->name, x));
-      x++;
-      sprintf(temp, "%s%08d", f->name, x);
-      DEBUG_MAIN(("main: - trying \"%s\"\n", temp));
-      if (x == 99999999) {
-	DIE(("main: Why can I not create a folder %s? I have tried %i extensions...\n", f->name, x));
-      }
-      fclose(f->output);
-    }
-    if (x > 0) { //then the f->name should change
-      free (f->name);
-      f->name = temp;
-    } else {
-      free (temp);
-    }
-  }
-  if (mode != MODE_SEPERATE) {
-    f->name = check_filename(f->name);
-    if ((f->output = fopen(f->name, "w")) == NULL) {
-      DIE(("main: Could not open file \"%s\" for write\n", f->name));
-    }
-  }
+  create_enter_dir(f, item->file_as, mode, overwrite);
   f->type = item->type;
 
   if ((d_ptr = pst_getTopOfFolders(&pstfile, item)) == NULL) {
@@ -370,56 +324,7 @@ int main(int argc, char** argv) {
 
 	temp = item->file_as;
 	temp = check_filename(temp);
-
-	if (mode == MODE_KMAIL)
-	  f->name = mk_kmail_dir(item->file_as); //create directory and form filename
-	else if (mode == MODE_RECURSE)
-	  f->name = mk_recurse_dir(item->file_as);
-	else if (mode == MODE_SEPERATE) {
-	  // do similar stuff to recurse here.
-	  mk_seperate_dir(item->file_as, overwrite);
-	  f->name = (char*) xmalloc(10);
-	  memset(f->name, 0, 10);
-	  //	    sprintf(f->name, "%09i", f->email_count);
-	} else {
-	  f->name = (char*) xmalloc(strlen(item->file_as)+strlen(OUTPUT_TEMPLATE+1));
-	  sprintf(f->name, OUTPUT_TEMPLATE, item->file_as);
-	}
-
-	f->dname = (char*) xmalloc(strlen(item->file_as)+1);
-	strcpy(f->dname, item->file_as);
-
-	if (overwrite != 1) {
-	  temp = (char*) xmalloc (strlen(f->name)+10); //enough room for 10 digits
-	  sprintf(temp, "%s", f->name);
-	  x = 0;
-	  temp = check_filename(temp);
-	  while ((f->output = fopen(temp, "r")) != NULL) {
-	    DEBUG_MAIN(("main: need to increase filename cause one already exists with that name\n"));
-	    DEBUG_MAIN(("main: - increasing it to %s%d\n", f->name, x));
-	    x++;
-	    sprintf(temp, "%s%08d", f->name, x);
-	    DEBUG_MAIN(("main: - trying \"%s\"\n", f->name));
-	    if (x == 99999999) {
-	      DIE(("main: Why can I not create a folder %s? I have tried %i extensions...\n", f->name, x));
-	    }
-	    fclose(f->output);
-	  }
-	  if (x > 0) { //then the f->name should change
-	    free (f->name);
-	    f->name = temp;
-	  } else {
-	    free(temp);
-	  }
-	}
-
-	DEBUG_MAIN(("main: f->name = %s\nitem->folder_name = %s\n", f->name, item->file_as));
-	if (mode != MODE_SEPERATE) {
-	  f->name = check_filename(f->name);
-	  if ((f->output = fopen(f->name, "w")) == NULL) {
-	    DIE(("main: Could not open file \"%s\" for write\n", f->name));
-	  }
-	}
+	create_enter_dir(f, item->file_as, mode, overwrite);
 	if (d_ptr->child != NULL) {
 	  d_ptr = d_ptr->child;
 	  skip_child = 1;
@@ -1522,3 +1427,59 @@ void write_normal_email(FILE* f_output, 
 	if (boundary)
 		free (boundary);
 }
+
+void create_enter_dir(struct file_ll* f, char file_as[], int mode, int overwrite)
+{
+	int x = 0;
+	if (mode == MODE_KMAIL)
+		f->name = mk_kmail_dir(file_as); //create directory and form filename
+	else if (mode == MODE_RECURSE)
+		f->name = mk_recurse_dir(file_as);
+	else if (mode == MODE_SEPERATE) {
+		// do similar stuff to recurse here.
+		mk_seperate_dir(file_as, overwrite);
+		f->name = (char*) xmalloc(10);
+		memset(f->name, 0, 10);
+		//	    sprintf(f->name, "%09i", f->email_count);
+	} else {
+		f->name = (char*) xmalloc(strlen(file_as)+strlen(OUTPUT_TEMPLATE+1));
+		sprintf(f->name, OUTPUT_TEMPLATE, file_as);
+	}
+
+	f->dname = (char*) xmalloc(strlen(file_as)+1);
+	strcpy(f->dname, file_as);
+
+	if (overwrite != 1) {
+		char* temp = (char*) malloc (strlen(f->name)+10); //enough room for 10 digits
+		if(temp == NULL)
+			DIE(("create_enter_dir: Cannot allocate memory for file name\n"));
+		sprintf(temp, "%s", f->name);
+		x = 0;
+		temp = check_filename(temp);
+		while ((f->output = fopen(temp, "r")) != NULL) {
+			DEBUG_MAIN(("create_enter_dir: need to increase filename cause one already exists with that name\n"));
+			DEBUG_MAIN(("create_enter_dir: - increasing it to %s%d\n", f->name, x));
+			x++;
+			sprintf(temp, "%s%08d", f->name, x);
+			DEBUG_MAIN(("create_enter_dir: - trying \"%s\"\n", f->name));
+			if (x == 99999999) {
+				DIE(("create_enter_dir: Why can I not create a folder %s? I have tried %i extensions...\n", f->name, x));
+			}
+			fclose(f->output);
+		}
+		if (x > 0) { //then the f->name should change
+			free (f->name);
+			f->name = temp;
+		} else {
+			free(temp);
+		}
+	}
+
+	DEBUG_MAIN(("create_enter_dir: f->name = %s\nitem->folder_name = %s\n", f->name, file_as));
+	if (mode != MODE_SEPERATE) {
+		f->name = check_filename(f->name);
+		if ((f->output = fopen(f->name, "w")) == NULL) {
+			DIE(("create_enter_dir: Could not open file \"%s\" for write\n", f->name));
+		}
+	}
+}



More information about the Libpst-devel mailing list