1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-20 04:48:06 +01:00

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 '\')
This commit is contained in:
Christophe CURIS
2012-07-08 01:24:03 +02:00
committed by Carlos R. Mafra
parent 19f0998cd1
commit 42cccb2313
3 changed files with 31 additions and 38 deletions

View File

@@ -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);

View File

@@ -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)

View File

@@ -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);