[asterisk-commits] murf: branch murf/config_comments r40838 - /team/murf/config_comments/config.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Aug 22 09:38:29 MST 2006


Author: murf
Date: Tue Aug 22 11:38:29 2006
New Revision: 40838

URL: http://svn.digium.com/view/asterisk?rev=40838&view=rev
Log:
Committing my first round of changes so I can update and 
test this beast.


Modified:
    team/murf/config_comments/config.c

Modified: team/murf/config_comments/config.c
URL: http://svn.digium.com/view/asterisk/team/murf/config_comments/config.c?rev=40838&r1=40837&r2=40838&view=diff
==============================================================================
--- team/murf/config_comments/config.c (original)
+++ team/murf/config_comments/config.c Tue Aug 22 11:38:29 2006
@@ -62,6 +62,77 @@
 
 static char *extconfig_conf = "extconfig.conf";
 
+/* Growable string buffer */
+static char *comment_buffer;   /* this will be a comment collector.*/
+static int   comment_buffer_size;  /* the amount of storage so far alloc'd for the comment_buffer */
+
+static char *lline_buffer;    /* A buffer for stuff behind the ; */
+static int  lline_buffer_size;
+
+
+#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 struct ast_config_map {
 	struct ast_config_map *next;
 	char *name;
@@ -84,6 +155,8 @@
 struct ast_category {
 	char name[80];
 	int ignored;			/* do not let user of the config see this category */
+	struct ast_comment *precomments;
+	struct ast_comment *sameline;
 	struct ast_variable *root;
 	struct ast_variable *last;
 	struct ast_category *next;
@@ -524,6 +597,18 @@
 		if (!(*cat = newcat = ast_category_new(catname))) {
 			return -1;
 		}
+		/* 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();
+		
  		/* If there are options or categories to inherit from, process them now */
  		if (c) {
  			if (!(cur = strchr(c, ')'))) {
@@ -646,13 +731,24 @@
 				/* Put and reset comments */
 				v->blanklines = 0;
 				ast_variable_append(*cat, v);
+				/* 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();
+				
 			} else {
 				return -1;
 			}
 		} else {
 			ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
 		}
-
 	}
 	return 0;
 }
@@ -677,6 +773,10 @@
 		snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
 	}
 
+	if (withcomments) {
+		CB_INIT();
+	}
+	
 #ifdef AST_INCLUDE_GLOB
 	{
 		int glob_ret;
@@ -726,11 +826,17 @@
 		while(!feof(f)) {
 			lineno++;
 			if (fgets(buf, sizeof(buf), f)) {
+				if ( withcomments ) {    
+					CB_ADD(lline_buffer);       /* add the current lline buffer to the comment buffer */
+					lline_buffer[0] = 0;        /* erase the lline buffer */
+				}
+				
 				new_buf = buf;
-				if (comment)
+				if (comment) 
 					process_buf = NULL;
 				else
 					process_buf = buf;
+				
 				while ((comment_p = strchr(new_buf, COMMENT_META))) {
 					if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
 						/* Yuck, gotta memmove */
@@ -758,6 +864,11 @@
 								/* Actually have to move what's left over the top, then continue */
 								char *oldptr;
 								oldptr = process_buf + strlen(process_buf);
+								if ( withcomments ) {
+									CB_ADD(";");
+									CB_ADD_LEN(oldptr+1,new_buf-oldptr-1);
+								}
+								
 								memmove(oldptr, new_buf, strlen(new_buf) + 1);
 								new_buf = oldptr;
 							} else
@@ -767,12 +878,20 @@
 						if (!comment) {
 							/* If ; is found, and we are not nested in a comment, 
 							   we immediately stop all comment processing */
+							if ( withcomments ) {
+								LLB_ADD(comment_p);
+							}
 							*comment_p = '\0'; 
 							new_buf = comment_p;
 						} else
 							new_buf = comment_p + 1;
 					}
 				}
+				if( comment && !process_buf )
+				{
+					CB_ADD(buf);  /* the whole line is a comment, store it */
+				}
+				
 				if (process_buf) {
 					char *buf = ast_strip(process_buf);
 					if (!ast_strlen_zero(buf)) {
@@ -797,6 +916,16 @@
 			}
 		}
 #endif
+	if (withcomments) {
+		if (comment_buffer) { 
+			free(comment_buffer);
+			free(lline_buffer);
+			comment_buffer=0; 
+			lline_buffer=0; 
+			comment_buffer_size=0; 
+			lline_buffer_size=0;
+		}
+	}
 	if (count == 0)
 		return NULL;
 



More information about the asterisk-commits mailing list