]> jfr.im git - irc/quakenet/newserv.git/blob - helpmod2/hterm.c
Initial Import
[irc/quakenet/newserv.git] / helpmod2 / hterm.c
1 #include <string.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include "../lib/sstring.h"
6
7 #include "hterm.h"
8
9 #include "hgen.h"
10
11 hterm *hterm_add(hterm** ptr, const char *name, const char *desc)
12 {
13 hterm *htrm;
14
15 if (ptr == NULL)
16 ptr = &hterms;
17
18 if (name == NULL || desc == NULL || hterm_get(*ptr, name))
19 return NULL;
20
21 htrm = (hterm*)malloc(sizeof(htrm));
22 htrm->name = getsstring(name, strlen(name));
23 htrm->description = getsstring(desc, strlen(desc));
24
25 htrm->next = *ptr;
26 *ptr = htrm;
27
28 return htrm;
29 }
30
31 hterm *hterm_get(hterm *source, const char *str)
32 {
33 hterm *ptr;
34 if (source == NULL)
35 ptr = hterms;
36 else
37 ptr = source;
38 for (;ptr;ptr = ptr->next)
39 if (!ci_strcmp(ptr->name->content, str))
40 return ptr;
41 if (source != NULL)
42 return hterm_get(NULL, str);
43 else
44 return NULL;
45 }
46
47 hterm *hterm_find(hterm *source, const char *str)
48 {
49 hterm *ptr;
50 char buffer[512];
51
52 if (source == NULL)
53 source = hterms;
54
55 sprintf(buffer, "*%s*", str);
56
57 for (ptr = source;ptr;ptr = ptr->next)
58 if (strregexp(ptr->name->content, buffer))
59 return ptr;
60 for (ptr = source;ptr;ptr = ptr->next)
61 if (strregexp(ptr->description->content, buffer))
62 return ptr;
63 if (source == NULL)
64 return hterm_find(NULL, str);
65 else
66 return NULL;
67 }
68
69 hterm *hterm_get_and_find(hterm *source, const char *str)
70 {
71 /*hterm *ptr = hterm_get(source, str);
72 if (ptr == NULL)
73 ptr = hterm_find(source, str);
74 return ptr;*/
75 /* search order: get source, get NULL, find source, find NULL */
76 hterm *ptr;
77 ptr = hterm_get(source, str);
78 if (ptr != NULL)
79 return ptr;
80 ptr = hterm_get(NULL, str);
81 if (ptr != NULL)
82 return ptr;
83 ptr = hterm_find(source, str);
84 if (ptr != NULL)
85 return ptr;
86 ptr = hterm_find(NULL, str);
87 return ptr;
88 }
89
90 hterm *hterm_del(hterm** start, hterm *htrm)
91 {
92 hterm** ptr = start;
93
94 if (start == NULL)
95 ptr = &hterms;
96
97 for (;*ptr;ptr = &(*ptr)->next)
98 if (*ptr == htrm)
99 {
100 hterm *tmp = (*ptr)->next;
101 freesstring((*ptr)->name);
102 freesstring((*ptr)->description);
103 free(*ptr);
104 *ptr = tmp;
105
106 return NULL;
107 }
108
109 if (start != NULL)
110 return hterm_del(NULL, htrm);
111 else
112 return htrm;
113 }
114
115 void hterm_del_all(hterm **source)
116 {
117 if (source == NULL)
118 source = &hterms;
119
120 while (*source)
121 hterm_del(source, *source);
122 }
123
124 int hterm_count(hterm* ptr)
125 {
126 int i = 0;
127 for (;ptr;ptr = ptr->next)
128 i++;
129 return i;
130 }