]> jfr.im git - irc/quakenet/newserv.git/blob - helpmod/helpmod_entries.c
Initial Import
[irc/quakenet/newserv.git] / helpmod / helpmod_entries.c
1 #include "helpmod_entries.h"
2 #include "helpmod_alias.h"
3 #include "../core/error.c"
4
5 struct helpmod_parsed_line_struct helpmod_parsed_line;
6
7 int helpmod_valid_selection(helpmod_entry state, int selection)
8 {
9 if (selection == -1)
10 return ((int)state->parent);
11 return (selection >= 0 && selection < state->option_count);
12 }
13
14 helpmod_entry helpmod_make_selection(helpmod_entry state, int selection)
15 {
16 if (selection == -1)
17 return state->parent;
18 return state->options[selection];
19 }
20
21 void helpmod_init_entry(helpmod_entry * entry)
22 {
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;
29 }
30
31 void helpmod_clear_entries(helpmod_entry state)
32 {
33 int i;
34 if (state == NULL)
35 return;
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)
41 free(state->text);
42 for (i=0;i<state->option_count;i++)
43 {
44 helpmod_clear_entries(state->options[i]);
45 free(state->options[i]);
46 }
47 if (state->option_count)
48 free(state->options);
49 }
50
51 void helpmod_parse_line(FILE* input)
52 {
53 int i, slen;
54 char str[512] = "";
55 /**str = '\0'; */
56 fgets(str,511,input);
57 helpmod_parsed_line.line_type = HM_LINE_NORMAL;
58 if (*str >= '0' && *str <= '0')
59 {
60 sscanf(str, "%2d", &helpmod_parsed_line.depth);
61 if (str[2] == '*')
62 {
63 helpmod_parsed_line.line_type = HM_LINE_TEXT;
64 strcpy(helpmod_parsed_line.text, str + 3);
65 }
66 else if (str[2] == '$')
67 {
68 helpmod_parsed_line.line_type = HM_LINE_ALIAS;
69 strcpy(helpmod_parsed_line.text, str + 3);
70 }
71 else
72 strcpy(helpmod_parsed_line.text, str + 2);
73 return;
74 }
75 for(i=0,slen=strlen(str);i<slen;i++)
76 if (str[i] == '*')
77 {
78 helpmod_parsed_line.line_type = HM_LINE_TEXT;
79 i++;
80 break;
81 }
82 else if (str[i] == '$')
83 {
84 helpmod_parsed_line.line_type = HM_LINE_ALIAS;
85 i++;
86 break;
87 }
88 else if (str[i] != ' ')
89 break;
90
91 helpmod_parsed_line.depth = i;
92 strcpy(helpmod_parsed_line.text, str + i);
93 }
94
95 void helpmod_clear_all_entries(void)
96 {
97 helpmod_clear_entries(helpmod_base);
98 free(helpmod_base);
99 helpmod_base = NULL;
100 }
101
102 void helpmod_load_entries(char* setting_file)
103 {
104 FILE *main_input;
105 FILE *tmp_input;
106 char buffer[512];
107 int i;
108 if (NULL == setting_file)
109 setting_file = "helpmod/default";
110 if (NULL == (main_input = fopen(setting_file, "rt")))
111 {
112 Error("helpmod", ERR_WARNING, "Default database not found, service will be unavailable until a valid database is loaded\n");
113 return;
114 }
115 while (!feof(main_input))
116 {
117 fgets(buffer,511,main_input);
118 if (feof(main_input))
119 return;
120 if (*buffer == '$')
121 {
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')
127 {
128 (buffer+1)[i] = 0x00;
129 break;
130 }
131 tmp_input = fopen(buffer+1, "r");
132 if (tmp_input == NULL)
133 {
134 Error("helpmod", ERR_ERROR, "File %s specified in %s not found\n",buffer+1, setting_file);
135 return;
136 }
137 helpmod_parse_line(tmp_input);
138 helpmod_load_entry(&helpmod_base->options[helpmod_base->option_count-1], tmp_input, 0, helpmod_base);
139 fclose(tmp_input);
140 }
141 else
142 {
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));
145 }
146 }
147 fclose(main_input);
148 }
149
150 void helpmod_load_entry(helpmod_entry* base, FILE* input, int depth, helpmod_entry parent)
151 {
152 if (feof(input))
153 {
154 *base = NULL;
155 helpmod_parsed_line.depth = 0;
156 return;
157 }
158
159 if (helpmod_parsed_line.depth < depth)
160 {
161 *base = NULL;
162 return;
163 }
164
165 helpmod_init_entry(base);
166 (*base)->parent = parent;
167 (*base)->description = getsstring(helpmod_parsed_line.text, strlen(helpmod_parsed_line.text));
168 depth++;
169
170 helpmod_parse_line(input);
171 while (helpmod_parsed_line.line_type == HM_LINE_ALIAS)
172 {
173 helpmod_add_alias(helpmod_parsed_line.text, *base); /* create an alias */
174 helpmod_parse_line(input);
175 }
176 while (helpmod_parsed_line.line_type == HM_LINE_TEXT)
177 {
178 if (feof(input))
179 return;
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);
183 }
184 while (helpmod_parsed_line.depth == depth)
185 {
186
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);
189 }
190 return;
191 }
192
193 long helpmod_entry_count(helpmod_entry base)
194 {
195 int i;
196 long counter = 0;
197 if (base == NULL)
198 return 0;
199 if (base->options == NULL)
200 return 0;
201 for (i=0;i<base->option_count;i++)
202 counter+=helpmod_entry_count(base->options[i]);
203 return i+counter;
204 }