[asterisk-commits] murf: branch murf/config_comments r40875 - /team/murf/config_comments/main/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Aug 22 19:34:23 MST 2006


Author: murf
Date: Tue Aug 22 21:34:23 2006
New Revision: 40875

URL: http://svn.digium.com/view/asterisk?rev=40875&view=rev
Log:
added some code to the config_text_file_save routine to output the
precomments, as it seems to already have code for the same line comments.
Did this for both ast_variables and ast_categories.

One thing to remember is that the comment collector gets the comment
start char(s) also, so there's no need to put a ';' in front of the
comment when you print it out. Also gets the line spacings.

Minimally tested. More testing to come.



Modified:
    team/murf/config_comments/main/config.c

Modified: team/murf/config_comments/main/config.c
URL: http://svn.digium.com/view/asterisk/team/murf/config_comments/main/config.c?rev=40875&r1=40874&r2=40875&view=diff
==============================================================================
--- team/murf/config_comments/main/config.c (original)
+++ team/murf/config_comments/main/config.c Tue Aug 22 21:34:23 2006
@@ -70,68 +70,84 @@
 static int  lline_buffer_size;
 
 
+struct ast_comment {
+	struct ast_comment *next;
+	char cmt[0];
+};
+
 #define CB_INCR 250
 
-#define CB_INIT()								\
-	{											\
-		if( !comment_buffer ) {					\
-			comment_buffer = (char*)malloc(CB_INCR);	\
-			comment_buffer_size = CB_INCR;		\
-			lline_buffer = (char*)malloc(CB_INCR);	\
-			lline_buffer_size = CB_INCR;		\
-		}  else {                               \
-			comment_buffer[0] = 0;              \
-			lline_buffer[0] = 0;              \
-        }                                       \
-	}
-
-#define CB_ADD(str)								\
-	{							\
-		char *x = (str);											\
-		int rem = comment_buffer_size - strlen(comment_buffer) - 1;	\
-		int siz = strlen(x);										\
-		if( rem < siz+1 )										\
-		{															\
-			comment_buffer = (char*)realloc(comment_buffer,comment_buffer_size+CB_INCR+siz+1); \
-			comment_buffer_size += CB_INCR+siz+1;						\
-		}																\
-		strcat(comment_buffer,x);													\
-	}
-
-#define CB_ADD_LEN(str, len)		\
-	{							\
-		char *x = (str);											\
-		int cbl = strlen(comment_buffer)+1;							\
-		int rem = comment_buffer_size - cbl;	\
-		if( rem < len+1 )										\
-		{															\
-			comment_buffer = (char*)realloc(comment_buffer,comment_buffer_size+CB_INCR+len+1); \
-			comment_buffer_size += CB_INCR+len+1;						\
-		}																\
-		strncat(comment_buffer,x,len);													\
-		comment_buffer[cbl+len-1] = 0;													\
-	}
-
-#define LLB_ADD(str)								\
-	{							\
-		char *x = (str);											\
-		int rem = lline_buffer_size - strlen(lline_buffer) - 1;	\
-		int siz = strlen(x);										\
-		if( rem < siz+1 )										\
-		{															\
-			lline_buffer = (char*)realloc(lline_buffer,lline_buffer_size+CB_INCR+siz+1); \
-			lline_buffer_size += CB_INCR+siz+1;						\
-		}																\
-		strcat(lline_buffer,x);													\
-	}
-
-#define CB_RESET()  comment_buffer[0] = 0; lline_buffer[0] = 0
-
-#define CB_DESTROY if (comment_buffer) { free(comment_buffer);free(lline_buffer);  \
-                                             comment_buffer=0; lline_buffer=0; comment_buffer_size=0; lline_buffer_size=0;}
-
-#define ALLOC_COMMENT(buffer) (struct ast_comment *)calloc(1,sizeof(struct ast_comment)+strlen(buffer)+1)
-#define COPY_COMMENT(ptr,buffer) strcpy((ptr)->cmt,buffer)
+static void CB_INIT(void)
+{
+	if( !comment_buffer ) {
+		comment_buffer = (char*)malloc(CB_INCR);
+		comment_buffer[0] = 0;
+		comment_buffer_size = CB_INCR;
+		lline_buffer = (char*)malloc(CB_INCR);
+		lline_buffer[0] = 0;
+		lline_buffer_size = CB_INCR;
+		
+	}  else {
+		comment_buffer[0] = 0;
+		lline_buffer[0] = 0;
+	}
+}
+
+static void  CB_ADD(char *str)
+{
+	char *x = (str);
+	int rem = comment_buffer_size - strlen(comment_buffer) - 1;
+	int siz = strlen(x);
+	if( rem < siz+1 )
+	{
+		comment_buffer = (char*)realloc(comment_buffer,comment_buffer_size+CB_INCR+siz+1);
+		comment_buffer_size += CB_INCR+siz+1;
+	}
+	strcat(comment_buffer,x);
+}
+
+static void  CB_ADD_LEN(char *str, int len)
+{
+	char *x = (str);
+	int cbl = strlen(comment_buffer)+1;
+	int rem = comment_buffer_size - cbl;
+	if( rem < len+1 )
+	{
+		comment_buffer = (char*)realloc(comment_buffer,comment_buffer_size+CB_INCR+len+1);
+		comment_buffer_size += CB_INCR+len+1;
+	}
+	strncat(comment_buffer,x,len);
+	comment_buffer[cbl+len-1] = 0;
+}
+
+static void  LLB_ADD(char *str)
+{
+	char *x = (str);
+	int rem = lline_buffer_size - strlen(lline_buffer) - 1;
+	int siz = strlen(x);
+	if( rem < siz+1 )
+	{
+		lline_buffer = (char*)realloc(lline_buffer,lline_buffer_size+CB_INCR+siz+1);
+		lline_buffer_size += CB_INCR+siz+1;
+	}
+	strcat(lline_buffer,x);
+}
+
+static void CB_RESET(void )  
+{ 
+	comment_buffer[0] = 0; 
+	lline_buffer[0] = 0;
+}
+		
+
+
+static struct ast_comment *ALLOC_COMMENT(const char *buffer)
+{ 
+	struct ast_comment *x = (struct ast_comment *)calloc(1,sizeof(struct ast_comment)+strlen(buffer)+1);
+	strcpy(x->cmt, buffer);
+	return x;
+}
+
 
 static struct ast_config_map {
 	struct ast_config_map *next;
@@ -146,11 +162,6 @@
 static struct ast_config_engine *config_engine_list;
 
 #define MAX_INCLUDE_LEVEL 10
-
-struct ast_comment {
-	struct ast_comment *next;
-	char cmt[0];
-};
 
 struct ast_category {
 	char name[80];
@@ -600,11 +611,9 @@
 		/* add comments */
 		if (withcomments && comment_buffer && comment_buffer[0] ) {
 			newcat->precomments = ALLOC_COMMENT(comment_buffer);
-			COPY_COMMENT(newcat->precomments,comment_buffer);
 		}
 		if (withcomments && lline_buffer && lline_buffer[0] ) {
 			newcat->sameline = ALLOC_COMMENT(lline_buffer);
-			COPY_COMMENT(newcat->sameline,lline_buffer);
 		}
 		if( withcomments )
 			CB_RESET();
@@ -734,11 +743,9 @@
 				/* add comments */
 				if (withcomments && comment_buffer && comment_buffer[0] ) {
 					v->precomments = ALLOC_COMMENT(comment_buffer);
-					COPY_COMMENT(v->precomments,comment_buffer);
 				}
 				if (withcomments && lline_buffer && lline_buffer[0] ) {
 					v->sameline = ALLOC_COMMENT(lline_buffer);
-					COPY_COMMENT(v->sameline,lline_buffer);
 				}
 				if( withcomments )
 					CB_RESET();
@@ -940,6 +947,7 @@
 	time_t t;
 	struct ast_variable *var;
 	struct ast_category *cat;
+	struct ast_comment *cmt;
 	int blanklines = 0;
 
 	if (configfile[0] == '/') {
@@ -965,11 +973,27 @@
 		cat = cfg->root;
 		while(cat) {
 			/* Dump section with any appropriate comment */
-			fprintf(f, "\n[%s]\n", cat->name);
+			for (cmt = cat->precomments; cmt; cmt=cmt->next)
+			{
+				fprintf(f,"\n%s", cmt->cmt);
+			}
+			if (!cat->precomments)
+				fprintf(f,"\n");
+			fprintf(f, "[%s]", cat->name);
+			for(cmt = cat->sameline; cmt; cmt=cmt->next)
+			{
+				fprintf(f,"%s", cmt->cmt);
+			}
+			if (!cat->sameline)
+				fprintf(f,"\n");
 			var = cat->root;
 			while(var) {
+				for (cmt = var->precomments; cmt; cmt=cmt->next)
+				{
+					fprintf(f,"\n%s", cmt->cmt);
+				}
 				if (var->sameline) 
-					fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
+					fprintf(f, "%s %s %s  %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
 				else	
 					fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
 				if (var->blanklines) {



More information about the asterisk-commits mailing list