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:
committed by
Carlos R. Mafra
parent
19f0998cd1
commit
42cccb2313
@@ -876,8 +876,7 @@ WMenuParser WMenuParserCreate(const char *file_name, void *file);
|
|||||||
|
|
||||||
const char *WMenuParserGetFilename(WMenuParser parser);
|
const char *WMenuParserGetFilename(WMenuParser parser);
|
||||||
|
|
||||||
char *getLine(WMenuParser parser);
|
Bool WMenuParserGetLine(WMenuParser parser, char **title, char **command, char **parameter, char **shortcut);
|
||||||
void separateline(char *line, char **title, char **command, char **parameter, char **shortcut);
|
|
||||||
|
|
||||||
void WMenuParserDelete(WMenuParser parser);
|
void WMenuParserDelete(WMenuParser parser);
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@
|
|||||||
#include "menuparser.h"
|
#include "menuparser.h"
|
||||||
|
|
||||||
static WMenuParser menu_parser_create_new(const char *file_name, void *file);
|
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 *****/
|
/***** Constructor and Destructor for the Menu Parser object *****/
|
||||||
@@ -62,18 +63,28 @@ const char *WMenuParserGetFilename(WMenuParser parser)
|
|||||||
return parser->file_name;
|
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;
|
char *line = NULL, *result = NULL;
|
||||||
size_t len;
|
size_t len;
|
||||||
int done;
|
int done;
|
||||||
|
|
||||||
|
*title = NULL;
|
||||||
|
*command = NULL;
|
||||||
|
*parameter = NULL;
|
||||||
|
*shortcut = NULL;
|
||||||
|
|
||||||
|
cur_parser = top_parser;
|
||||||
|
|
||||||
again:
|
again:
|
||||||
done = 0;
|
done = 0;
|
||||||
while (!done && fgets(linebuf, sizeof(linebuf), parser->file_handle) != NULL) {
|
while (!done && fgets(cur_parser->line_buffer, sizeof(cur_parser->line_buffer), cur_parser->file_handle) != NULL) {
|
||||||
line = wtrimspace(linebuf);
|
line = wtrimspace(cur_parser->line_buffer);
|
||||||
len = strlen(line);
|
len = strlen(line);
|
||||||
|
cur_parser->line_number++;
|
||||||
|
|
||||||
/* allow line wrapping */
|
/* allow line wrapping */
|
||||||
if (len > 0 && line[len - 1] == '\\') {
|
if (len > 0 && line[len - 1] == '\\') {
|
||||||
@@ -91,7 +102,7 @@ again:
|
|||||||
wfree(line);
|
wfree(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!done || ferror(parser->file_handle)) {
|
if (!done || ferror(cur_parser->file_handle)) {
|
||||||
wfree(result);
|
wfree(result);
|
||||||
result = NULL;
|
result = NULL;
|
||||||
} else if (result != NULL && (result[0] == 0 || result[0] == '#' ||
|
} else if (result != NULL && (result[0] == 0 || result[0] == '#' ||
|
||||||
@@ -101,24 +112,24 @@ again:
|
|||||||
goto again;
|
goto again;
|
||||||
} else if (result != NULL && strlen(result) >= MAXLINE) {
|
} else if (result != NULL && strlen(result) >= MAXLINE) {
|
||||||
wwarning(_("%s:maximal line size exceeded in menu config: %s"),
|
wwarning(_("%s:maximal line size exceeded in menu config: %s"),
|
||||||
parser->file_name, line);
|
cur_parser->file_name, line);
|
||||||
wfree(result);
|
wfree(result);
|
||||||
result = NULL;
|
result = NULL;
|
||||||
goto again;
|
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;
|
char *suffix, *next = line;
|
||||||
|
|
||||||
*title = NULL;
|
|
||||||
*command = NULL;
|
|
||||||
*parameter = NULL;
|
|
||||||
*shortcut = NULL;
|
|
||||||
|
|
||||||
/* get the title */
|
/* get the title */
|
||||||
*title = wtokennext(line, &next);
|
*title = wtokennext(line, &next);
|
||||||
if (next == NULL)
|
if (next == NULL)
|
||||||
|
|||||||
@@ -893,16 +893,13 @@ static void freeline(char *title, char *command, char *parameter, char *shortcut
|
|||||||
|
|
||||||
static WMenu *parseCascade(WScreen * scr, WMenu * menu, WMenuParser parser)
|
static WMenu *parseCascade(WScreen * scr, WMenu * menu, WMenuParser parser)
|
||||||
{
|
{
|
||||||
char *line;
|
|
||||||
char *command, *params, *shortcut, *title;
|
char *command, *params, *shortcut, *title;
|
||||||
|
|
||||||
while ((line = getLine(parser)) != NULL) {
|
while (WMenuParserGetLine(parser, &title, &command, ¶ms, &shortcut)) {
|
||||||
separateline(line, &title, &command, ¶ms, &shortcut);
|
|
||||||
|
|
||||||
if (command == NULL || !command[0]) {
|
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);
|
freeline(title, command, params, shortcut);
|
||||||
wfree(line);
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -921,14 +918,12 @@ static WMenu *parseCascade(WScreen * scr, WMenu * menu, WMenuParser parser)
|
|||||||
} else if (strcasecmp(command, "END") == 0) {
|
} else if (strcasecmp(command, "END") == 0) {
|
||||||
/* end of menu */
|
/* end of menu */
|
||||||
freeline(title, command, params, shortcut);
|
freeline(title, command, params, shortcut);
|
||||||
wfree(line);
|
|
||||||
return menu;
|
return menu;
|
||||||
} else {
|
} else {
|
||||||
/* normal items */
|
/* normal items */
|
||||||
addMenuEntry(menu, M_(title), shortcut, command, params, WMenuParserGetFilename(parser));
|
addMenuEntry(menu, M_(title), shortcut, command, params, WMenuParserGetFilename(parser));
|
||||||
}
|
}
|
||||||
freeline(title, command, params, shortcut);
|
freeline(title, command, params, shortcut);
|
||||||
wfree(line);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wwarning(_("%s:syntax error in menu file:END declaration missing"), WMenuParserGetFilename(parser));
|
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;
|
WMenu *menu = NULL;
|
||||||
FILE *file = NULL;
|
FILE *file = NULL;
|
||||||
WMenuParser parser;
|
WMenuParser parser;
|
||||||
char *line;
|
|
||||||
char *command, *params, *shortcut, *title;
|
char *command, *params, *shortcut, *title;
|
||||||
char cmd[MAXLINE];
|
char cmd[MAXLINE];
|
||||||
#ifdef USECPP
|
#ifdef USECPP
|
||||||
@@ -977,13 +971,11 @@ static WMenu *readMenuFile(WScreen * scr, char *file_name)
|
|||||||
}
|
}
|
||||||
parser = WMenuParserCreate(file_name, file);
|
parser = WMenuParserCreate(file_name, file);
|
||||||
|
|
||||||
while ((line = getLine(parser)) != NULL) {
|
while (WMenuParserGetLine(parser, &title, &command, ¶ms, &shortcut)) {
|
||||||
separateline(line, &title, &command, ¶ms, &shortcut);
|
|
||||||
|
|
||||||
if (command == NULL || !command[0]) {
|
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);
|
freeline(title, command, params, shortcut);
|
||||||
wfree(line);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (strcasecmp(command, "MENU") == 0) {
|
if (strcasecmp(command, "MENU") == 0) {
|
||||||
@@ -994,16 +986,13 @@ static WMenu *readMenuFile(WScreen * scr, char *file_name)
|
|||||||
menu = NULL;
|
menu = NULL;
|
||||||
}
|
}
|
||||||
freeline(title, command, params, shortcut);
|
freeline(title, command, params, shortcut);
|
||||||
wfree(line);
|
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
wwarning(_("%s:invalid menu file. MENU command is missing"), file_name);
|
wwarning(_("%s:invalid menu file. MENU command is missing"), file_name);
|
||||||
freeline(title, command, params, shortcut);
|
freeline(title, command, params, shortcut);
|
||||||
wfree(line);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
freeline(title, command, params, shortcut);
|
freeline(title, command, params, shortcut);
|
||||||
wfree(line);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WMenuParserDelete(parser);
|
WMenuParserDelete(parser);
|
||||||
@@ -1030,7 +1019,6 @@ static WMenu *readMenuPipe(WScreen * scr, char **file_name)
|
|||||||
FILE *file = NULL;
|
FILE *file = NULL;
|
||||||
WMenuParser parser;
|
WMenuParser parser;
|
||||||
char *command, *params, *shortcut, *title;
|
char *command, *params, *shortcut, *title;
|
||||||
char *line;
|
|
||||||
char *filename;
|
char *filename;
|
||||||
char flat_file[MAXLINE];
|
char flat_file[MAXLINE];
|
||||||
char cmd[MAXLINE];
|
char cmd[MAXLINE];
|
||||||
@@ -1074,13 +1062,11 @@ static WMenu *readMenuPipe(WScreen * scr, char **file_name)
|
|||||||
}
|
}
|
||||||
parser = WMenuParserCreate(flat_file, file);
|
parser = WMenuParserCreate(flat_file, file);
|
||||||
|
|
||||||
while ((line = getLine(parser)) != NULL) {
|
while (WMenuParserGetLine(parser, &title, &command, ¶ms, &shortcut)) {
|
||||||
separateline(line, &title, &command, ¶ms, &shortcut);
|
|
||||||
|
|
||||||
if (command == NULL || !command[0]) {
|
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);
|
freeline(title, command, params, shortcut);
|
||||||
wfree(line);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (strcasecmp(command, "MENU") == 0) {
|
if (strcasecmp(command, "MENU") == 0) {
|
||||||
@@ -1091,17 +1077,14 @@ static WMenu *readMenuPipe(WScreen * scr, char **file_name)
|
|||||||
menu = NULL;
|
menu = NULL;
|
||||||
}
|
}
|
||||||
freeline(title, command, params, shortcut);
|
freeline(title, command, params, shortcut);
|
||||||
wfree(line);
|
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
wwarning(_("%s:no title given for the root menu"), filename);
|
wwarning(_("%s:no title given for the root menu"), filename);
|
||||||
freeline(title, command, params, shortcut);
|
freeline(title, command, params, shortcut);
|
||||||
wfree(line);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
freeline(title, command, params, shortcut);
|
freeline(title, command, params, shortcut);
|
||||||
wfree(line);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WMenuParserDelete(parser);
|
WMenuParserDelete(parser);
|
||||||
|
|||||||
Reference in New Issue
Block a user