1 #include "helpmod_entries.h"
2 #include "helpmod_alias.h"
3 #include "../core/error.c"
5 struct helpmod_parsed_line_struct helpmod_parsed_line
;
7 int helpmod_valid_selection(helpmod_entry state
, int selection
)
10 return ((int)state
->parent
);
11 return (selection
>= 0 && selection
< state
->option_count
);
14 helpmod_entry
helpmod_make_selection(helpmod_entry state
, int selection
)
18 return state
->options
[selection
];
21 void helpmod_init_entry(helpmod_entry
* entry
)
23 *entry
= (helpmod_entry
)malloc(sizeof(**entry
));
24 (*entry
)->option_count
= (*entry
)->text_lines
= 0;
25 (*entry
)->text
= NULL
;
26 (*entry
)->options
= NULL
;
27 (*entry
)->parent
= NULL
;
28 (*entry
)->description
= NULL
;
31 void helpmod_clear_entries(helpmod_entry state
)
36 if (state
->description
!= NULL
)
37 freesstring(state
->description
);
38 for (i
=0;i
<state
->text_lines
;i
++)
39 freesstring(state
->text
[i
]);
40 if (state
->text_lines
)
42 for (i
=0;i
<state
->option_count
;i
++)
44 helpmod_clear_entries(state
->options
[i
]);
45 free(state
->options
[i
]);
47 if (state
->option_count
)
51 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/default";
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\n");
115 while (!feof(main_input
))
117 fgets(buffer
,511,main_input
);
118 if (feof(main_input
))
122 helpmod_base
->options
= (helpmod_entry
*)realloc(helpmod_base
->options
, sizeof(helpmod_entry
) * ++helpmod_base
->option_count
);
123 helpmod_base
->options
[helpmod_base
->option_count
-1] = NULL
;
124 /* remove the \n, it's not wanted */
125 for (i
=0;i
<strlen(buffer
+1);i
++)
126 if ((buffer
+1)[i
] == '\n')
128 (buffer
+1)[i
] = 0x00;
131 tmp_input
= fopen(buffer
+1, "r");
132 if (tmp_input
== NULL
)
134 Error("helpmod", ERR_ERROR
, "File %s specified in %s not found\n",buffer
+1, setting_file
);
137 helpmod_parse_line(tmp_input
);
138 helpmod_load_entry(&helpmod_base
->options
[helpmod_base
->option_count
-1], tmp_input
, 0, helpmod_base
);
143 helpmod_base
->text
= (sstring
**)realloc(helpmod_base
->text
, sizeof(sstring
*) * ++helpmod_base
->text_lines
);
144 helpmod_base
->text
[helpmod_base
->text_lines
-1] = getsstring(buffer
, strlen(buffer
));
150 void helpmod_load_entry(helpmod_entry
* base
, FILE* input
, int depth
, helpmod_entry parent
)
155 helpmod_parsed_line
.depth
= 0;
159 if (helpmod_parsed_line
.depth
< depth
)
165 helpmod_init_entry(base
);
166 (*base
)->parent
= parent
;
167 (*base
)->description
= getsstring(helpmod_parsed_line
.text
, strlen(helpmod_parsed_line
.text
));
170 helpmod_parse_line(input
);
171 while (helpmod_parsed_line
.line_type
== HM_LINE_ALIAS
)
173 helpmod_add_alias(helpmod_parsed_line
.text
, *base
); /* create an alias */
174 helpmod_parse_line(input
);
176 while (helpmod_parsed_line
.line_type
== HM_LINE_TEXT
)
180 (*base
)->text
= (sstring
**)realloc((*base
)->text
, sizeof(sstring
*) * ++(*base
)->text_lines
);
181 (*base
)->text
[(*base
)->text_lines
- 1] = getsstring(helpmod_parsed_line
.text
, strlen(helpmod_parsed_line
.text
));
182 helpmod_parse_line(input
);
184 while (helpmod_parsed_line
.depth
== depth
)
187 (*base
)->options
= (helpmod_entry
*)realloc((*base
)->options
, sizeof(helpmod_entry
) * ++(*base
)->option_count
);
188 helpmod_load_entry(&(*base
)->options
[(*base
)->option_count
-1],input
,depth
, *base
);
193 long helpmod_entry_count(helpmod_entry base
)
199 if (base
->options
== NULL
)
201 for (i
=0;i
<base
->option_count
;i
++)
202 counter
+=helpmod_entry_count(base
->options
[i
]);