… | |
… | |
3 | * Copyright © 2005-2006 Atheme Development Group |
3 | * Copyright © 2005-2006 Atheme Development Group |
4 | * Rights to this code are documented in doc/pod/license.pod. |
4 | * Rights to this code are documented in doc/pod/license.pod. |
5 | * |
5 | * |
6 | * This file contains a generic help system implementation. |
6 | * This file contains a generic help system implementation. |
7 | * |
7 | * |
8 | * $Id: help.C,v 1.2 2007/07/21 01:29:10 pippijn Exp $ |
8 | * $Id: help.C,v 1.3 2007/08/28 17:08:12 pippijn Exp $ |
9 | */ |
9 | */ |
10 | |
10 | |
11 | #include "atheme.h" |
11 | #include "atheme.h" |
12 | |
12 | |
13 | static char const rcsid[] = "$Id: help.C,v 1.2 2007/07/21 01:29:10 pippijn Exp $"; |
13 | static char const rcsid[] = "$Id: help.C,v 1.3 2007/08/28 17:08:12 pippijn Exp $"; |
14 | |
14 | |
15 | struct help_eq |
15 | struct help_eq |
16 | { |
16 | { |
17 | help_eq () { } |
17 | help_eq () |
18 | help_eq (char *cmd) : cmd (cmd) { } |
18 | : cmd (0) |
|
|
19 | { |
|
|
20 | } |
|
|
21 | |
|
|
22 | help_eq (char const * const command) |
|
|
23 | : cmd (command) |
|
|
24 | { |
|
|
25 | } |
|
|
26 | |
|
|
27 | /******/ |
19 | |
28 | |
20 | bool operator () (helpentry_t *h) |
29 | bool operator () (helpentry_t *h) |
21 | { |
30 | { |
22 | return !strcasecmp (h->name, cmd); |
31 | return !strcasecmp (h->name, cmd); |
23 | } |
32 | } |
24 | |
33 | |
25 | /******/ |
34 | /******/ |
26 | |
35 | |
27 | bool operator () (char *h1, helpentry_t *h2) |
36 | bool operator () (char const * const h1, helpentry_t *h2) |
28 | { |
37 | { |
29 | return !strcasecmp (h1, h2->name); |
38 | return !strcasecmp (h1, h2->name); |
30 | } |
39 | } |
31 | |
40 | |
32 | bool operator () (helpentry_t *h1, char *h2) |
41 | bool operator () (helpentry_t *h1, char const * const h2) |
33 | { |
42 | { |
34 | return !strcasecmp (h1->name, h2); |
43 | return !strcasecmp (h1->name, h2); |
35 | } |
44 | } |
36 | |
45 | |
37 | bool operator () (helpentry_t *h1, helpentry_t *h2) |
46 | bool operator () (helpentry_t *h1, helpentry_t *h2) |
38 | { |
47 | { |
39 | return !strcasecmp (h1->name, h2->name); |
48 | return !strcasecmp (h1->name, h2->name); |
40 | } |
49 | } |
41 | |
50 | |
42 | private: |
51 | private: |
43 | char *cmd; |
52 | char const * const cmd; |
44 | }; |
53 | }; |
45 | |
54 | |
46 | static helpentry_t * |
55 | static helpentry_t * |
47 | help_cmd_find (sourceinfo_t *si, char *cmd, helpvec *list) |
56 | help_cmd_find (sourceinfo_t *si, char const * const cmd, helpvec &list) |
48 | { |
57 | { |
49 | helpvec::iterator he = std::find_if (list->begin (), list->end (), help_eq (cmd)); |
58 | helpvec::iterator he = std::find_if (list.begin (), list.end (), help_eq (cmd)); |
50 | |
59 | |
51 | if (he != list->end ()) |
60 | if (he != list.end ()) |
52 | return *he; |
61 | return *he; |
53 | |
62 | |
54 | command_fail (si, fault_nosuch_target, _("No help available for \2%s\2."), cmd); |
63 | command_fail (si, fault_nosuch_target, _("No help available for \2%s\2."), cmd); |
55 | return NULL; |
64 | return NULL; |
56 | } |
65 | } |
57 | |
66 | |
58 | void |
67 | void |
59 | help_display (sourceinfo_t *si, char *command, helpvec *list) |
68 | help_display (sourceinfo_t *si, char const * const command, helpvec &list) |
60 | { |
69 | { |
61 | helpentry_t *c; |
70 | helpentry_t *c; |
62 | FILE *help_file; |
71 | FILE *help_file; |
63 | char buf[BUFSIZE]; |
72 | char buf[BUFSIZE]; |
64 | |
73 | |
… | |
… | |
113 | command_fail (si, fault_nosuch_target, _("No help available for \2%s\2."), command); |
122 | command_fail (si, fault_nosuch_target, _("No help available for \2%s\2."), command); |
114 | } |
123 | } |
115 | } |
124 | } |
116 | |
125 | |
117 | void |
126 | void |
118 | help_addentry (helpvec *list, char *topic, char *fname, void (*func) (sourceinfo_t *si)) |
127 | help_addentry (helpvec &list, char const * const topic, char const * const fname, void (*func) (sourceinfo_t *si)) |
119 | { |
128 | { |
120 | help_eq help_compare; |
129 | help_eq help_compare; |
121 | helpentry_t *he; |
130 | helpentry_t *he; |
122 | // helpvec::iterator heit; |
131 | // helpvec::iterator heit; |
123 | |
132 | |
124 | if (!list && !topic && !func && !fname) |
133 | if (!topic && !func && !fname) |
125 | { |
134 | { |
126 | slog (LG_DEBUG, "help_addentry(): invalid params"); |
135 | slog (LG_DEBUG, "help_addentry(): invalid params"); |
127 | return; |
136 | return; |
128 | } |
137 | } |
129 | |
138 | |
… | |
… | |
133 | slog (LG_DEBUG, "help_addentry(): invalid params"); |
142 | slog (LG_DEBUG, "help_addentry(): invalid params"); |
134 | return; |
143 | return; |
135 | } |
144 | } |
136 | |
145 | |
137 | #if 0 |
146 | #if 0 |
138 | heit = std::find_if (list->begin (), list->end (), help_eq (topic)); |
147 | heit = std::find_if (list.begin (), list.end (), help_eq (topic)); |
139 | if (heit != list->end ()) |
148 | if (heit != list.end ()) |
140 | { |
149 | { |
141 | slog (LG_DEBUG, "help_addentry(): trying to add an existing helpentry: %s", topic); |
150 | slog (LG_DEBUG, "help_addentry(): trying to add an existing helpentry: %s", topic); |
142 | return; |
151 | return; |
143 | } |
152 | } |
144 | #endif |
153 | #endif |
145 | |
154 | |
146 | he = new helpentry_t; |
155 | he = new helpentry_t (topic, fname, func); |
147 | |
156 | |
148 | he->name = sstrdup (topic); |
|
|
149 | |
|
|
150 | if (func != NULL) |
|
|
151 | he->func = func; |
|
|
152 | else if (fname != NULL) |
|
|
153 | he->file = sstrdup (fname); |
|
|
154 | |
|
|
155 | list->push_back (he); |
157 | list.push_back (he); |
156 | } |
158 | } |
157 | |
159 | |
158 | void |
160 | void |
159 | help_delentry (helpvec *list, char *name) |
161 | help_delentry (helpvec &list, char const * const name) |
160 | { |
162 | { |
161 | help_eq help_compare; |
163 | help_eq help_compare; |
162 | helpvec::iterator he = std::find_if (list->begin (), list->end (), help_eq (name)); |
164 | helpvec::iterator he = std::find_if (list.begin (), list.end (), help_eq (name)); |
163 | |
165 | |
164 | #if 0 |
166 | #if 0 |
165 | if (he == list->end ()) |
167 | if (he == list->end ()) |
166 | { |
168 | { |
167 | slog (LG_DEBUG, "help_delentry(): trying to delete a nonexistent help entry: %s", name); |
169 | slog (LG_DEBUG, "help_delentry(): trying to delete a nonexistent help entry: %s", name); |
168 | return; |
170 | return; |
169 | } |
171 | } |
170 | #endif |
172 | #endif |
171 | |
173 | |
172 | free ((*he)->name); |
|
|
173 | |
|
|
174 | if ((*he)->file != NULL) |
|
|
175 | free ((*he)->file); |
|
|
176 | |
|
|
177 | (*he)->func = NULL; |
|
|
178 | delete *he; |
174 | delete *he; |
179 | |
175 | |
180 | list->erase (he); |
176 | list.erase (he); |
181 | } |
177 | } |