1 #include "helpmod_entries.h"
2 #include "helpmod_alias.h"
3 #include "../core/error.h"
6 struct helpmod_parsed_line_struct helpmod_parsed_line
;
8 int helpmod_valid_selection(helpmod_entry state
, int selection
)
11 return ((int)((long)state
->parent
));
12 return (selection
>= 0 && selection
< state
->option_count
);
15 helpmod_entry
helpmod_make_selection(helpmod_entry state
, int selection
)
19 return state
->options
[selection
];
22 void helpmod_init_entry(helpmod_entry
* entry
)
24 *entry
= (helpmod_entry
)malloc(sizeof(**entry
));
25 (*entry
)->option_count
= (*entry
)->text_lines
= 0;
26 (*entry
)->text
= NULL
;
27 (*entry
)->options
= NULL
;
28 (*entry
)->parent
= NULL
;
29 (*entry
)->description
= NULL
;
32 void helpmod_clear_entries(helpmod_entry state
)
37 if (state
->description
!= NULL
)
38 freesstring(state
->description
);
39 for (i
=0;i
<state
->text_lines
;i
++)
40 freesstring(state
->text
[i
]);
41 if (state
->text_lines
)
43 for (i
=0;i
<state
->option_count
;i
++)
45 helpmod_clear_entries(state
->options
[i
]);
46 free(state
->options
[i
]);
48 if (state
->option_count
)
52 void helpmod_parse_line(FILE* input
)
57 helpmod_parsed_line
.line_type
= HM_LINE_NORMAL
;
58 if (*str
>= '0' && *str
<= '0')
60 sscanf(str
, "%2d", &helpmod_parsed_line
.depth
);
63 helpmod_parsed_line
.line_type
= HM_LINE_TEXT
;
64 strcpy(helpmod_parsed_line
.text
, str
+ 3);
66 else if (str
[2] == '$')
68 helpmod_parsed_line
.line_type
= HM_LINE_ALIAS
;
69 strcpy(helpmod_parsed_line
.text
, str
+ 3);
72 strcpy(helpmod_parsed_line
.text
, str
+ 2);
75 for(i
=0,slen
=strlen(str
);i
<slen
;i
++)
78 helpmod_parsed_line
.line_type
= HM_LINE_TEXT
;
82 else if (str
[i
] == '$')
84 helpmod_parsed_line
.line_type
= HM_LINE_ALIAS
;
88 else if (str
[i
] != ' ')
91 helpmod_parsed_line
.depth
= i
;
92 strcpy(helpmod_parsed_line
.text
, str
+ i
);
95 void helpmod_clear_all_entries(void)
97 helpmod_clear_entries(helpmod_base
);
102 void helpmod_load_entries(char* setting_file
)
108 if (NULL
== setting_file
)
109 setting_file
= HELPMOD_HELP_DEFAULT_DB
;
110 if (NULL
== (main_input
= fopen(setting_file
, "rt")))
112 Error("helpmod", ERR_WARNING
, "Default database not found, service will be unavailable until a valid database is loaded");
115 while (!feof(main_input
))
117 fgets(buffer
,511,main_input
);
118 if (feof(main_input
))
122 /* remove the \n, it's not wanted */
123 for (i
=0;i
<strlen(buffer
+1);i
++)
124 if ((buffer
+1)[i
] == '\n')
126 (buffer
+1)[i
] = '\0';
129 tmp_input
= fopen(buffer
+1, "r");
130 if (tmp_input
== NULL
)
132 Error("helpmod", ERR_ERROR
, "File %s specified in %s not found",buffer
+1, setting_file
);
135 helpmod_base
->options
= (helpmod_entry
*)realloc(helpmod_base
->options
, sizeof(helpmod_entry
) * ++helpmod_base
->option_count
);
136 helpmod_base
->options
[helpmod_base
->option_count
-1] = NULL
;
138 helpmod_parse_line(tmp_input
);
139 helpmod_load_entry(&helpmod_base
->options
[helpmod_base
->option_count
-1], tmp_input
, 0, helpmod_base
);
144 helpmod_base
->text
= (sstring
**)realloc(helpmod_base
->text
, sizeof(sstring
*) * ++helpmod_base
->text_lines
);
145 helpmod_base
->text
[helpmod_base
->text_lines
-1] = getsstring(buffer
, strlen(buffer
));
151 void helpmod_load_entry(helpmod_entry
* base
, FILE* input
, int depth
, helpmod_entry parent
)
156 helpmod_parsed_line
.depth
= 0;
160 if (helpmod_parsed_line
.depth
< depth
)
166 helpmod_init_entry(base
);
167 (*base
)->parent
= parent
;
168 (*base
)->description
= getsstring(helpmod_parsed_line
.text
, strlen(helpmod_parsed_line
.text
));
171 helpmod_parse_line(input
);
172 while (helpmod_parsed_line
.line_type
== HM_LINE_ALIAS
)
174 helpmod_add_alias(helpmod_parsed_line
.text
, *base
); /* create an alias */
175 helpmod_parse_line(input
);
177 while (helpmod_parsed_line
.line_type
== HM_LINE_TEXT
)
181 (*base
)->text
= (sstring
**)realloc((*base
)->text
, sizeof(sstring
*) * ++(*base
)->text_lines
);
182 (*base
)->text
[(*base
)->text_lines
- 1] = getsstring(helpmod_parsed_line
.text
, strlen(helpmod_parsed_line
.text
));
183 helpmod_parse_line(input
);
185 while (helpmod_parsed_line
.depth
== depth
)
188 (*base
)->options
= (helpmod_entry
*)realloc((*base
)->options
, sizeof(helpmod_entry
) * ++(*base
)->option_count
);
189 helpmod_load_entry(&(*base
)->options
[(*base
)->option_count
-1],input
,depth
, *base
);
194 long helpmod_entry_count(helpmod_entry base
)
200 if (base
->options
== NULL
)
202 for (i
=0;i
<base
->option_count
;i
++)
203 counter
+=helpmod_entry_count(base
->options
[i
]);