From c915a751dda7d08ae1bca24b4a02fa4de41b1f9a Mon Sep 17 00:00:00 2001
From: Christophe CURIS <[email protected]>
Date: Sun, 8 Jul 2012 01:24:03 +0200
Subject: [PATCH 03/10] Remove dependency to CPP: merged 'getLine' and
 'separateline' into a single function call

From caller point of view, the two function have been merged into a
single function in the API. This will be needed by the advanced
parser that will have to not separate the concept of a 'line' and
the concept of 'content' (due to empty/comment lines, multi-line
comments, long lines split with '\')
---
 WINGs/WINGs/WUtil.h |    3 +--
 WINGs/menuparser.c  |   37 ++++++++++++++++++++++++-------------
 src/rootmenu.c      |   29 ++++++-----------------------
 3 files changed, 31 insertions(+), 38 deletions(-)

diff --git a/WINGs/WINGs/WUtil.h b/WINGs/WINGs/WUtil.h
index 802b9e4..c19a0af 100644
--- a/WINGs/WINGs/WUtil.h
+++ b/WINGs/WINGs/WUtil.h
@@ -876,8 +876,7 @@ WMenuParser WMenuParserCreate(const char *file_name, void *file);
 
 const char *WMenuParserGetFilename(WMenuParser parser);
 
-char *getLine(WMenuParser parser);
-void separateline(char *line, char **title, char **command, char **parameter, char **shortcut);
+Bool WMenuParserGetLine(WMenuParser parser, char **title, char **command, char **parameter, char **shortcut);
 
 void WMenuParserDelete(WMenuParser parser);
 
diff --git a/WINGs/menuparser.c b/WINGs/menuparser.c
index b3a3349..f4a77a2 100644
--- a/WINGs/menuparser.c
+++ b/WINGs/menuparser.c
@@ -28,6 +28,7 @@
 #include "menuparser.h"
 
 static WMenuParser menu_parser_create_new(const char *file_name, void *file);
+static void separateline(char *line, char **title, char **command, char **parameter, char **shortcut);
 
 
 /***** Constructor and Destructor for the Menu Parser object *****/
@@ -62,18 +63,28 @@ const char *WMenuParserGetFilename(WMenuParser parser)
 	return parser->file_name;
 }
 
-char *getLine(WMenuParser parser)
+
+/***** Read one line from file and split content *****/
+Bool WMenuParserGetLine(WMenuParser top_parser, char **title, char **command, char **parameter, char **shortcut)
 {
-	char linebuf[MAXLINE];
+	WMenuParser cur_parser;
 	char *line = NULL, *result = NULL;
 	size_t len;
 	int done;
 
+	*title = NULL;
+	*command = NULL;
+	*parameter = NULL;
+	*shortcut = NULL;
+
+	cur_parser = top_parser;
+
 again:
 	done = 0;
-	while (!done && fgets(linebuf, sizeof(linebuf), parser->file_handle) != NULL) {
-		line = wtrimspace(linebuf);
+	while (!done && fgets(cur_parser->line_buffer, sizeof(cur_parser->line_buffer), cur_parser->file_handle) != NULL) {
+		line = wtrimspace(cur_parser->line_buffer);
 		len = strlen(line);
+		cur_parser->line_number++;
 
 		/* allow line wrapping */
 		if (len > 0 && line[len - 1] == '\\') {
@@ -91,7 +102,7 @@ again:
 			wfree(line);
 		}
 	}
-	if (!done || ferror(parser->file_handle)) {
+	if (!done || ferror(cur_parser->file_handle)) {
 		wfree(result);
 		result = NULL;
 	} else if (result != NULL && (result[0] == 0 || result[0] == '#' ||
@@ -101,24 +112,24 @@ again:
 		goto again;
 	} else if (result != NULL && strlen(result) >= MAXLINE) {
 		wwarning(_("%s:maximal line size exceeded in menu config: %s"),
-			 parser->file_name, line);
+			 cur_parser->file_name, line);
 		wfree(result);
 		result = NULL;
 		goto again;
 	}
 
-	return result;
+	if (result == NULL)
+	  return False;
+
+	separateline(line, title, command, parameter, shortcut);
+	wfree(line);
+	return True;
 }
 
-void separateline(char *line, char **title, char **command, char **parameter, char **shortcut)
+static void separateline(char *line, char **title, char **command, char **parameter, char **shortcut)
 {
 	char *suffix, *next = line;
 
-	*title = NULL;
-	*command = NULL;
-	*parameter = NULL;
-	*shortcut = NULL;
-
 	/* get the title */
 	*title = wtokennext(line, &next);
 	if (next == NULL)
diff --git a/src/rootmenu.c b/src/rootmenu.c
index 1186892..609079e 100644
--- a/src/rootmenu.c
+++ b/src/rootmenu.c
@@ -893,16 +893,13 @@ static void freeline(char *title, char *command, char *parameter, char *shortcut
 
 static WMenu *parseCascade(WScreen * scr, WMenu * menu, WMenuParser parser)
 {
-	char *line;
 	char *command, *params, *shortcut, *title;
 
-	while ((line = getLine(parser)) != NULL) {
-		separateline(line, &title, &command, &params, &shortcut);
+	while (WMenuParserGetLine(parser, &title, &command, &params, &shortcut)) {
 
 		if (command == NULL || !command[0]) {
-			wwarning(_("%s:missing command in menu config: %s"), WMenuParserGetFilename(parser), line);
+			wwarning(_("%s:missing command in menu config"), WMenuParserGetFilename(parser));
 			freeline(title, command, params, shortcut);
-			wfree(line);
 			goto error;
 		}
 
@@ -921,14 +918,12 @@ static WMenu *parseCascade(WScreen * scr, WMenu * menu, WMenuParser parser)
 		} else if (strcasecmp(command, "END") == 0) {
 			/* end of menu */
 			freeline(title, command, params, shortcut);
-			wfree(line);
 			return menu;
 		} else {
 			/* normal items */
 			addMenuEntry(menu, M_(title), shortcut, command, params, WMenuParserGetFilename(parser));
 		}
 		freeline(title, command, params, shortcut);
-		wfree(line);
 	}
 
 	wwarning(_("%s:syntax error in menu file:END declaration missing"), WMenuParserGetFilename(parser));
@@ -942,7 +937,6 @@ static WMenu *readMenuFile(WScreen * scr, char *file_name)
 	WMenu *menu = NULL;
 	FILE *file = NULL;
 	WMenuParser parser;
-	char *line;
 	char *command, *params, *shortcut, *title;
 	char cmd[MAXLINE];
 #ifdef USECPP
@@ -977,13 +971,11 @@ static WMenu *readMenuFile(WScreen * scr, char *file_name)
 	}
 	parser = WMenuParserCreate(file_name, file);
 
-	while ((line = getLine(parser)) != NULL) {
-		separateline(line, &title, &command, &params, &shortcut);
+	while (WMenuParserGetLine(parser, &title, &command, &params, &shortcut)) {
 
 		if (command == NULL || !command[0]) {
-			wwarning(_("%s:missing command in menu config: %s"), file_name, line);
+			wwarning(_("%s:missing command in menu config"), file_name);
 			freeline(title, command, params, shortcut);
-			wfree(line);
 			break;
 		}
 		if (strcasecmp(command, "MENU") == 0) {
@@ -994,16 +986,13 @@ static WMenu *readMenuFile(WScreen * scr, char *file_name)
 				menu = NULL;
 			}
 			freeline(title, command, params, shortcut);
-			wfree(line);
 			break;
 		} else {
 			wwarning(_("%s:invalid menu file. MENU command is missing"), file_name);
 			freeline(title, command, params, shortcut);
-			wfree(line);
 			break;
 		}
 		freeline(title, command, params, shortcut);
-		wfree(line);
 	}
 
 	WMenuParserDelete(parser);
@@ -1030,7 +1019,6 @@ static WMenu *readMenuPipe(WScreen * scr, char **file_name)
 	FILE *file = NULL;
 	WMenuParser parser;
 	char *command, *params, *shortcut, *title;
-	char *line;
 	char *filename;
 	char flat_file[MAXLINE];
 	char cmd[MAXLINE];
@@ -1074,13 +1062,11 @@ static WMenu *readMenuPipe(WScreen * scr, char **file_name)
 	}
 	parser = WMenuParserCreate(flat_file, file);
 
-	while ((line = getLine(parser)) != NULL) {
-		separateline(line, &title, &command, &params, &shortcut);
+	while (WMenuParserGetLine(parser, &title, &command, &params, &shortcut)) {
 
 		if (command == NULL || !command[0]) {
-			wwarning(_("%s:missing command in menu config: %s"), filename, line);
+			wwarning(_("%s:missing command in menu config"), filename);
 			freeline(title, command, params, shortcut);
-			wfree(line);
 			break;
 		}
 		if (strcasecmp(command, "MENU") == 0) {
@@ -1091,17 +1077,14 @@ static WMenu *readMenuPipe(WScreen * scr, char **file_name)
 				menu = NULL;
 			}
 			freeline(title, command, params, shortcut);
-			wfree(line);
 			break;
 		} else {
 			wwarning(_("%s:no title given for the root menu"), filename);
 			freeline(title, command, params, shortcut);
-			wfree(line);
 			break;
 		}
 
 		freeline(title, command, params, shortcut);
-		wfree(line);
 	}
 
 	WMenuParserDelete(parser);
-- 
1.7.10.4

Reply via email to