[asterisk-commits] murf: branch murf/bug8684-trunk r81351 - /team/murf/bug8684-trunk/main/config.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Aug 29 11:51:33 CDT 2007


Author: murf
Date: Wed Aug 29 11:51:33 2007
New Revision: 81351

URL: http://svn.digium.com/view/asterisk?view=rev&rev=81351
Log:
Did some cleanup. Repeated code blocks replaced with func/call.

Modified:
    team/murf/bug8684-trunk/main/config.c

Modified: team/murf/bug8684-trunk/main/config.c
URL: http://svn.digium.com/view/asterisk/team/murf/bug8684-trunk/main/config.c?view=diff&rev=81351&r1=81350&r2=81351
==============================================================================
--- team/murf/bug8684-trunk/main/config.c (original)
+++ team/murf/bug8684-trunk/main/config.c Wed Aug 29 11:51:33 2007
@@ -1260,23 +1260,53 @@
    the position of the #include and #exec directives. So, blank lines tend to disappear from a read/rewrite operation,
    and a header gets added.
 
+   vars and category heads are output in the order they are stored in the config file. So, if the software
+   shuffles these at all, then the placement of #include directives might get a little mixed up, because the
+   file/lineno data probably won't get changed.
+
 */
+
+static void gen_header(FILE *f1, const char *configfile, const char *fn, const char *generator)
+{
+	char date[256]="";
+	time_t t;
+	time(&t);
+	ast_copy_string(date, ctime(&t), sizeof(date));
+
+	fprintf(f1, ";!\n");
+	fprintf(f1, ";! Automatically generated configuration file\n");
+	if (strcmp(configfile, fn))
+		fprintf(f1, ";! Filename: %s (%s)\n", configfile, fn);
+	else
+		fprintf(f1, ";! Filename: %s\n", configfile);
+	fprintf(f1, ";! Generator: %s\n", generator);
+	fprintf(f1, ";! Creation Date: %s", date);
+	fprintf(f1, ";!\n");
+}
+
+static void set_fn(char *fn, int fn_size, const char *file, const char *configfile)
+{
+	if (!file || file[0] == 0) {
+		if (configfile[0] == '/')
+			ast_copy_string(fn, configfile, fn_size);
+		else
+			snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
+	} else if (file[0] == '/') 
+		ast_copy_string(fn, file, fn_size);
+	else
+		snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, file);
+}
 
 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
 {
 	FILE *f;
 	char fn[256];
-	char date[256]="";
-	time_t t;
 	struct ast_variable *var;
 	struct ast_category *cat;
 	struct ast_comment *cmt;
 	struct ast_config_include *incl;
 	int blanklines = 0;
 
-	time(&t);
-	ast_copy_string(date, ctime(&t), sizeof(date));
-
 	/* reset all the output flags, in case this isn't our first time saving this data */
 
 	for (incl=cfg->includes; incl; incl = incl->next)
@@ -1289,23 +1319,11 @@
 	{
 		if (!incl->exec) { /* leave the execs alone -- we'll write out the #exec directives, but won't zero out the include files or exec files*/
 			FILE *f1;
-			if (incl->included_file[0] == '/') {
-				ast_copy_string(fn, incl->included_file, sizeof(fn));
-			} else {
-				snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, incl->included_file);
-			}
+
+			set_fn(fn, sizeof(fn), incl->included_file, configfile); /* normally, fn is just set to incl->included_file, prepended with config dir if relative */
 			f1 = fopen(fn,"w");
 			if (f1) {
-				fprintf(f1, ";!\n");
-				fprintf(f1, ";! Automatically generated configuration file\n");
-				if (strcmp(configfile, fn))
-					fprintf(f1, ";! Filename: %s (%s)\n", configfile, fn);
-				else
-					fprintf(f1, ";! Filename: %s\n", configfile);
-				fprintf(f1, ";! Generator: %s\n", generator);
-				fprintf(f1, ";! Creation Date: %s", date);
-				fprintf(f1, ";!\n");
-				
+				gen_header(f1, configfile, fn, generator);
 				fclose(f1); /* this should zero out the file */
 			} else {
 				ast_debug(1, "Unable to open for writing: %s\n", fn);
@@ -1314,26 +1332,14 @@
 		}
 	}
 
-	if (configfile[0] == '/') {
-		ast_copy_string(fn, configfile, sizeof(fn));
-	} else {
-		snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
-	}
+	set_fn(fn, sizeof(fn), 0, configfile); /* just set fn to absolute ver of configfile */
 #ifdef __CYGWIN__	
 	if ((f = fopen(fn, "w+"))) {
 #else
 	if ((f = fopen(fn, "w"))) {
 #endif	    
 		ast_verb(2, "Saving '%s': ", fn);
-		fprintf(f, ";!\n");
-		fprintf(f, ";! Automatically generated configuration file\n");
-		if (strcmp(configfile, fn))
-			fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
-		else
-			fprintf(f, ";! Filename: %s\n", configfile);
-		fprintf(f, ";! Generator: %s\n", generator);
-		fprintf(f, ";! Creation Date: %s", date);
-		fprintf(f, ";!\n");
+		gen_header(f, configfile, fn, generator);
 		cat = cfg->root;
 		fclose(f);
 		
@@ -1342,16 +1348,7 @@
 		   mobile, and open each file, print, and close it on an entry-by-entry basis */
 
 		while (cat) {
-			if (!cat->file || cat->file[0] == 0) {
-				if (configfile[0] == '/')
-					ast_copy_string(fn, configfile, sizeof(fn));
-				else
-					snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
-			} else if (cat->file[0] == '/') 
-				ast_copy_string(fn, cat->file, sizeof(fn));
-			else
-				snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, cat->file);
-
+			set_fn(fn, sizeof(fn), cat->file, configfile);
 			f = fopen(fn, "a");
 			if (!f)
 			{
@@ -1390,15 +1387,7 @@
 			
 			var = cat->root;
 			while (var) {
-				if (!var->file || var->file[0] == 0) {
-					if (configfile[0] == '/')
-						ast_copy_string(fn, configfile, sizeof(fn));
-					else
-						snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
-				} else if (var->file[0] == '/') 
-					ast_copy_string(fn, var->file, sizeof(fn));
-				else
-					snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, var->file);
+				set_fn(fn, sizeof(fn), var->file, configfile);
 				f = fopen(fn, "a");
 				if (!f)
 				{
@@ -1454,15 +1443,7 @@
 	for (incl=cfg->includes; incl; incl = incl->next) {
 		if (!incl->output) {
 			/* open the respective file */
-			if (!incl->include_location_file || incl->include_location_file[0] == 0) {
-				if (configfile[0] == '/')
-					ast_copy_string(fn, configfile, sizeof(fn));
-				else
-					snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
-			} else if (incl->include_location_file[0] == '/') 
-				ast_copy_string(fn, incl->include_location_file, sizeof(fn));
-			else
-				snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, incl->include_location_file);
+			set_fn(fn, sizeof(fn), incl->include_location_file, configfile);
 			f = fopen(fn, "a");
 			if (!f)
 			{




More information about the asterisk-commits mailing list