cleanups and such
This commit is contained in:
parent
7aae14e9b8
commit
99fd65baf0
4 changed files with 46 additions and 15 deletions
|
@ -3,6 +3,7 @@
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
GList *lst = cl_parse("foo.conf");
|
GList *lst = cl_parse("foo.conf");
|
||||||
cl_print_tree(lst,0);
|
cl_tree_print(lst,0);
|
||||||
|
cl_tree_free(lst);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
49
obcl/obcl.c
49
obcl/obcl.c
|
@ -1,22 +1,49 @@
|
||||||
#include "obcl.h"
|
#include "obcl.h"
|
||||||
|
|
||||||
void free_cl_tree(GList *tree)
|
void cl_tree_free(GList *tree)
|
||||||
{
|
{
|
||||||
|
CLNode *tmp;
|
||||||
|
|
||||||
|
if (!tree) return;
|
||||||
|
|
||||||
|
for (; tree; tree = tree->next) {
|
||||||
|
tmp = (CLNode*)tree->data;
|
||||||
|
switch(tmp->type) {
|
||||||
|
case CL_ID:
|
||||||
|
case CL_STR:
|
||||||
|
g_free(tmp->u.str);
|
||||||
|
break;
|
||||||
|
case CL_LIST:
|
||||||
|
case CL_BLOCK:
|
||||||
|
case CL_LISTBLOCK:
|
||||||
|
g_free(tmp->u.lb.id);
|
||||||
|
cl_tree_free(tmp->u.lb.list);
|
||||||
|
cl_tree_free(tmp->u.lb.block);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
g_free(tmp);
|
||||||
|
}
|
||||||
|
g_list_free(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
GList *cl_parse(gchar *file)
|
GList *cl_parse(gchar *file)
|
||||||
{
|
{
|
||||||
FILE *fh = fopen(file, "r");
|
FILE *fh = fopen(file, "r");
|
||||||
if (fh)
|
GList *ret = NULL;
|
||||||
return cl_parse_fh(fh);
|
|
||||||
else {
|
if (fh) {
|
||||||
printf("can't open file %s\n", file);
|
ret = cl_parse_fh(fh);
|
||||||
return 0;
|
fclose(fh);
|
||||||
|
} else {
|
||||||
|
perror(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cl_print_tree(GList *tree, int depth)
|
void cl_tree_print(GList *tree, int depth)
|
||||||
{
|
{
|
||||||
CLNode *tmp;
|
CLNode *tmp;
|
||||||
int tmpd = depth;
|
int tmpd = depth;
|
||||||
|
@ -40,17 +67,17 @@ void cl_print_tree(GList *tree, int depth)
|
||||||
break;
|
break;
|
||||||
case CL_LIST:
|
case CL_LIST:
|
||||||
printf("--LIST-- %s\n", tmp->u.lb.id);
|
printf("--LIST-- %s\n", tmp->u.lb.id);
|
||||||
cl_print_tree(tmp->u.lb.list, depth+2);
|
cl_tree_print(tmp->u.lb.list, depth+2);
|
||||||
break;
|
break;
|
||||||
case CL_BLOCK:
|
case CL_BLOCK:
|
||||||
printf("--BLOCK-- %s\n", tmp->u.lb.id);
|
printf("--BLOCK-- %s\n", tmp->u.lb.id);
|
||||||
cl_print_tree(tmp->u.lb.block, depth+2);
|
cl_tree_print(tmp->u.lb.block, depth+2);
|
||||||
break;
|
break;
|
||||||
case CL_LISTBLOCK:
|
case CL_LISTBLOCK:
|
||||||
printf("--LISTBLOCK-- %s\n", tmp->u.lb.id);
|
printf("--LISTBLOCK-- %s\n", tmp->u.lb.id);
|
||||||
cl_print_tree(tmp->u.lb.list, depth+2);
|
cl_tree_print(tmp->u.lb.list, depth+2);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
cl_print_tree(tmp->u.lb.block, depth+2);
|
cl_tree_print(tmp->u.lb.block, depth+2);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,11 +28,12 @@ typedef struct CLNode {
|
||||||
|
|
||||||
} CLNode;
|
} CLNode;
|
||||||
|
|
||||||
void free_cl_tree(GList *tree);
|
|
||||||
GList *cl_parse(gchar *file);
|
GList *cl_parse(gchar *file);
|
||||||
GList *cl_parse_fh(FILE *file);
|
GList *cl_parse_fh(FILE *file);
|
||||||
void cl_print_tree(GList *tree, int depth);
|
|
||||||
|
|
||||||
GList *parse_file(FILE *fh);
|
void cl_tree_free(GList *tree);
|
||||||
|
void cl_tree_print(GList *tree, int depth);
|
||||||
|
|
||||||
|
void cl_tree_process(GList *tree);
|
||||||
|
|
||||||
#endif /* __obcl_h */
|
#endif /* __obcl_h */
|
||||||
|
|
|
@ -51,6 +51,7 @@ stmt: TOK_ID list ';'
|
||||||
CLNode *s = g_new(CLNode,1);
|
CLNode *s = g_new(CLNode,1);
|
||||||
s->type = CL_LIST;
|
s->type = CL_LIST;
|
||||||
s->u.lb.list = $2;
|
s->u.lb.list = $2;
|
||||||
|
s->u.lb.block = NULL;
|
||||||
s->u.lb.id = $1;
|
s->u.lb.id = $1;
|
||||||
$$ = s;
|
$$ = s;
|
||||||
}
|
}
|
||||||
|
@ -68,6 +69,7 @@ stmt: TOK_ID list ';'
|
||||||
CLNode *s = g_new(CLNode,1);
|
CLNode *s = g_new(CLNode,1);
|
||||||
s->type = CL_BLOCK;
|
s->type = CL_BLOCK;
|
||||||
s->u.lb.block = $2;
|
s->u.lb.block = $2;
|
||||||
|
s->u.lb.list = NULL;
|
||||||
s->u.lb.id = $1;
|
s->u.lb.id = $1;
|
||||||
$$ = s;
|
$$ = s;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue