muhstik- irc flooding solution |
git clone git://git.acid.vegas/muhstik.git |
Log | Files | Refs | Archive | README |
lists.c (8134B)
1 /* Muhstik, Copyright (C) 2001-2002, Louis Bavoil <mulder@gmx.fr> */ 2 /* 2009-2011, Leon Kaiser <literalka@gnaa.eu> */ 3 /* */ 4 /* This program is free software; you can redistribute it and/or */ 5 /* modify it under the terms of the GNU Library General Public License */ 6 /* as published by the Free Software Foundation; either version 2 */ 7 /* of the License, or (at your option) any later version. */ 8 /* */ 9 /* This program is distributed in the hope that it will be useful, */ 10 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 11 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 12 /* GNU Library General Public License for more details. */ 13 14 /* {{{ Header includes */ 15 #include <stdio.h> 16 #include <stdlib.h> 17 18 #include "../include/lists.h" 19 #include "../include/string.h" 20 /* }}} */ 21 /* {{{ File functions */ 22 /* {{{ int occur_file(char *s, char *list) */ 23 int occur_file(char *s, char *list) 24 { 25 char buffer[BIGBUF]; 26 register int i = 0; 27 FILE *f; 28 29 if ((f = fopen(list, "r"))) 30 { 31 while (fgets(buffer, sizeof(buffer), f)) 32 { 33 if (!StrCompare(buffer, s)) 34 { 35 ++i; 36 } 37 fclose(f); 38 } 39 } 40 41 return i; 42 } 43 /* }}} */ 44 /* {{{ void add_file(char *s, char *list) */ 45 void add_file(char *s, char *list) 46 { 47 FILE *f; 48 49 if (!(f = fopen(list, "a+"))) 50 { 51 return; 52 } 53 fputs(s, f); 54 fclose(f); 55 } 56 /* }}} */ 57 /* {{{ int file_length(FILE *f) */ 58 int file_length(FILE *f) 59 { 60 register int i = 0; 61 int c = 0; 62 63 while ((c=fgetc(f)) != EOF) 64 { 65 if (c == '\n') 66 { 67 ++i; 68 } 69 } 70 rewind(f); 71 return i; 72 } 73 /* }}} */ 74 /* {{{ int get_a_line(char *s, int z, FILE *f) */ 75 int get_a_line(char *s, int z, FILE *f) 76 { 77 do 78 { 79 if (!fgets(s, z, f)) 80 { 81 return 1; 82 } 83 } 84 while (s[0]=='#' || s[0]=='[' || s[0]==10); 85 return 0; 86 } 87 /* }}} */ 88 /* }}} */ 89 /* {{{ Table functions */ 90 /* {{{ int add_table(char *toadd, char **list, int max) */ 91 int add_table(char *toadd, char **list, int max) 92 { 93 register int i; 94 for (i = 0; i < max; ++i, ++list) 95 { 96 if (*list == NULL) 97 { 98 *list = StrDuplicate(toadd); 99 return i; 100 } 101 } 102 return -1; 103 } 104 /* }}} */ 105 /* {{{ int remove_table(char *torm, char **list, int max) */ 106 int remove_table(char *torm, char **list, int max) 107 { 108 register int i; 109 for (i = 0; i < max; ++i, ++list) 110 { 111 if (*list && !StrCompare(*list, torm)) 112 { 113 free(*list); 114 *list = NULL; 115 return 0; 116 } 117 } 118 return 1; 119 } 120 /* }}} */ 121 /* {{{ void update_table(char *from, char *to, char **list, int max) */ 122 void update_table(char *from, char *to, char **list, int max) 123 { 124 register int i; 125 for (i = 0; i < max; ++i, ++list) 126 { 127 if (*list && !StrCompare(from, *list)) 128 { 129 free(*list); 130 *list = StrDuplicate(to); 131 return; 132 } 133 } 134 } 135 /* }}} */ 136 /* {{{ void update_pattern_table(char *from, char *to, char **list, int max) */ 137 void update_pattern_table(char *from, char *to, char **list, int max) 138 { 139 register int i; 140 char buffer[MEDBUF+1]; 141 142 for (i = 0; i < max; ++i, ++list) 143 { 144 if ((*list) && (sscanf(*list, "%"MEDBUF_TXT"[^!]", buffer) == 1) && (!StrCompare(from, buffer))) 145 { 146 snprintf(buffer, sizeof(buffer), "%s%s", to, strchr(*list, '!')); 147 free(*list); 148 *list = StrDuplicate(buffer); 149 return; 150 } 151 } 152 } 153 /* }}} */ 154 /* {{{ int occur_table(char *s, char **list, int max) */ 155 int occur_table(char *s, char **list, int max) 156 { 157 register int i; 158 int k = 0; 159 for (i = 0; i < max; ++i, ++list) 160 { 161 if (*list && !StrCompare(*list, s)) 162 { 163 ++k; 164 } 165 } 166 return k; 167 } 168 /* }}} */ 169 /* {{{ void fill_table(char **list, int max, FILE *f) */ 170 void fill_table(char **list, int max, FILE *f) 171 { 172 register int i; 173 char parm[MEDBUF]; 174 for (i = 0; i < max && fgets(parm, sizeof(parm), f); ++i) 175 { 176 sscanf(parm, "%[^\r\n]", parm); 177 list[i] = StrDuplicate(parm); 178 } 179 } 180 /* }}} */ 181 /* {{{ void clear_table(char **list, int max) */ 182 void clear_table(char **list, int max) 183 { 184 register int i; 185 for (i = 0; i < max; ++i, ++list) 186 { 187 if (*list) 188 { 189 free(*list); 190 *list = NULL; 191 } 192 } 193 } 194 /* }}} */ 195 /* {{{ int match_table(char *s, char **list, int max) */ 196 int match_table(char *s, char **list, int max) 197 { 198 register int i; 199 char parm[MEDBUF]; 200 201 for (i = 0; i < max; ++i, ++list) 202 { 203 if (*list && !StrParam(parm, sizeof(parm), *list, 0)) 204 { 205 if (match_pattern(parm, s)) 206 { 207 return i; 208 } 209 } 210 } 211 212 return -1; 213 } 214 /* }}} */ 215 /* }}} */ 216 /* {{{ Queue functions */ 217 /* {{{ int add_gen_queue(void *d, cmp_fun *cmp, queue *list) */ 218 int add_gen_queue(void *d, cmp_fun *cmp, queue *list) 219 { 220 queue s; 221 queue p1 = NULL; 222 queue p2 = NULL; 223 224 s = xmalloc(sizeof(struct st_list)); 225 s->data = d; 226 s->next = NULL; 227 228 if (*list == NULL) 229 { 230 *list = s; 231 return 0; 232 } 233 234 for (p2 = *list; p2; p2 = p2->next) 235 { 236 if (cmp && !cmp(p2->data, d)) 237 { 238 free(s->data); 239 free(s); 240 return 1; 241 } 242 p1 = p2; 243 } 244 p1->next = s; 245 return 0; 246 } 247 /* }}} */ 248 /* {{{ inline int add_queue(char *s, queue *list) */ 249 inline int add_queue(char *s, queue *list) 250 { 251 return add_gen_queue(StrDuplicate(s), NULL, list); 252 } 253 /* }}} */ 254 /* {{{ inline int uniq_add_queue(char *s, queue *list) */ 255 inline int uniq_add_queue(char *s, queue *list) 256 { 257 return add_gen_queue(StrDuplicate(s), StrCompare, list); 258 } 259 /* }}} */ 260 /* {{{ int remove_queue(char *d, queue *list) */ 261 int remove_queue(char *d, queue *list) 262 { 263 queue p1 = NULL; 264 queue p2 = NULL; 265 266 if (*list == NULL) 267 { 268 return 1; 269 } 270 271 if (!StrCompare((*list)->data, d)) 272 { 273 free_cell(list); 274 return 0; 275 } 276 277 for (p2 = *list; p2; p2 = p2->next) 278 { 279 if (!StrCompare(p2->data, d)) 280 { 281 p1->next = p2->next; 282 free(p2->data); 283 free(p2); 284 return 0; 285 } 286 p1 = p2; 287 } 288 return 1; 289 } 290 /* }}} */ 291 /* {{{ void update_queue(char *old, char *new, queue *list) */ 292 void update_queue(char *old, char *new, queue *list) 293 { 294 queue p; 295 296 for (p = *list; p; p = p->next) 297 { 298 if (!StrCompare(p->data, old)) 299 { 300 free(p->data); 301 p->data = StrDuplicate(new); 302 } 303 } 304 } 305 /* }}} */ 306 /* {{{ int occur_queue(char *s, queue *list) */ 307 int occur_queue(char *s, queue *list) 308 { 309 queue p; 310 int k = 0; 311 312 for (p = *list; p; p = p->next) 313 { 314 if (!StrCompare(p->data, s)) 315 { 316 ++k; 317 } 318 } 319 320 return k; 321 } 322 /* }}} */ 323 /* }}} */ 324 /* {{{ Cell functions */ 325 /* {{{ void free_cell(queue *list) */ 326 void free_cell(queue *list) 327 { 328 queue tmp; 329 330 if (*list == NULL) 331 { 332 return; 333 } 334 335 tmp = (*list)->next; 336 free((*list)->data); 337 free(*list); 338 *list = tmp; 339 } 340 /* }}} */ 341 /* {{{ void rotate_cell(queue *list) */ 342 void rotate_cell(queue *list) 343 { 344 queue cell; 345 void *data; 346 347 cell = (*list)->next; 348 data = (*list)->data; 349 free(*list); 350 *list = cell; 351 add_gen_queue(data, NULL, list); 352 } 353 /* }}} */ 354 /* }}} */ 355 /* {{{ void clear_queue(queue *list) */ 356 void clear_queue(queue *list) 357 { 358 while (*list) 359 { 360 free_cell(list); 361 } 362 } 363 /* }}} */