]> jfr.im git - irc/quakenet/newserv.git/blob - helpmod2/htopic.c
Merge pull request #1 from meeb/meeb
[irc/quakenet/newserv.git] / helpmod2 / htopic.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "helpmod.h"
6 #include "htopic.h"
7
8 htopic *htopic_add(htopic** htop, const char* entry, int pos)
9 {
10 htopic *tmp;
11
12 for (;*htop && pos;htop = &(*htop)->next, pos--);
13
14 tmp = *htop;
15
16 *htop = malloc(sizeof(htopic));
17 (*htop)->entry = getsstring(entry, strlen(entry));
18 (*htop)->next = tmp;
19
20 return *htop;
21 }
22
23 htopic *htopic_del(htopic** htop, htopic* target)
24 {
25 htopic *tmp;
26 for (;*htop;htop = &(*htop)->next)
27 if (*htop == target)
28 break;
29 if (!*htop)
30 return target;
31 tmp = (*htop)->next;
32 freesstring((*htop)->entry);
33 free(*htop);
34 *htop = tmp;
35
36
37 return NULL;
38 }
39
40 void htopic_del_all(htopic** htop)
41 {
42 while (*htop)
43 htopic_del(htop, *htop);
44 }
45
46 htopic *htopic_get(htopic *htop, int pos)
47 {
48 for (;htop && pos;htop = htop->next,pos--);
49 return htop;
50 }
51
52 const char *htopic_construct(htopic *htop)
53 {
54 static char buffer[512] = "";
55
56 *buffer = '\0';
57
58 for (;htop;htop = htop->next)
59 {
60 strcat(buffer, htop->entry->content);
61 if (htop->next)
62 strcat(buffer, " | ");
63 }
64 return buffer;
65 }
66
67 int htopic_count(htopic *htop)
68 {
69 int count = 0;
70 for (;htop;htop = htop->next)
71 count++;
72 return count;
73 }
74
75 int htopic_len(htopic *htop)
76 {
77 int len = 0;
78
79 if (htopic_count(htop))
80 len+=(htopic_count(htop) - 1) * 3;
81 for (;htop;htop = htop->next)
82 len+=strlen(htop->entry->content);
83
84 return len;
85 }
86