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