muhstik- irc flooding solution |
git clone git://git.acid.vegas/muhstik.git |
Log | Files | Refs | Archive | README |
string.c (4607B)
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/clone.h" 19 #include "../include/string.h" 20 #include "../include/print.h" 21 /* }}} */ 22 /* {{{ External variables */ 23 extern config_t conf; 24 /* }}} */ 25 /* {{{ xmalloc() */ 26 void *xmalloc(size_t size) 27 { 28 void *ret; 29 if (!(ret = malloc(size))) 30 { 31 print_error("malloc"); 32 exit(EXIT_FAILURE); 33 } 34 return ret; 35 } 36 /* }}} */ 37 /* {{{ Str*() */ 38 char *StrDuplicate(char *src) 39 { 40 char *dest; 41 42 if (!src) 43 { 44 return NULL; 45 } 46 47 if (!(dest = strdup(src))) 48 { 49 print_error("malloc"); 50 exit(EXIT_FAILURE); 51 } 52 return dest; 53 } 54 55 inline int StrCompare(char *s1, char *s2) 56 { 57 return (s1 && s2) ? strcasecmp(s1,s2) : 1; 58 } 59 60 inline int StrCmpPrefix(char *s1, char *s2) 61 { 62 return (s1 && s2) ? strncasecmp(s1,s2,strlen(s2)) : 1; 63 } 64 65 inline void StrFirstToken(char *s) 66 { 67 sscanf(s, "%s", s); 68 } 69 70 inline void StrCopy(char *s1, char *s2, size_t n) 71 { 72 strncpy(s1, s2, n); 73 s1[n-1] = 0; 74 } 75 76 inline void StrCat(char *s1, char *s2, size_t n) 77 { 78 if (strlen(s1) + strlen(s2) + 1 <= n) 79 { 80 strcat(s1, s2); 81 } 82 } 83 84 #ifdef NO_STRSEP 85 86 char *strsep(char **stringp, const char *delim) 87 { 88 char *res; 89 90 if (!stringp || !*stringp || !**stringp) 91 { 92 return NULL; 93 } 94 95 res = *stringp; 96 while (**stringp && !strchr(delim, **stringp)) 97 { 98 (*stringp)++; 99 } 100 101 if (**stringp) 102 { 103 **stringp = '\0'; 104 (*stringp)++; 105 } 106 107 return res; 108 } 109 110 #endif 111 112 int StrParam(char *parm, size_t size, char *s, int i) 113 { 114 register unsigned int k = 0; /* ``unsigned'' is not needed, stops a warning when compiled with -Wsign-compare */ 115 116 memset(parm, 0, size); 117 118 while (i >= 0 && k < size) 119 { 120 if (*s == 0 || *s == '\n' || *s == '\r') 121 { 122 break; 123 } 124 125 if (*s == ' ') 126 { 127 --i; 128 } 129 else if (i == 0) 130 { 131 parm[k++] = *s; 132 } 133 ++s; 134 } 135 136 return (i > 0 || parm[0] == 0) ? 1 : 0; 137 } 138 /* }}} */ 139 /* {{{ Booleans */ 140 int is_in(char *w, char *s) 141 { 142 char *parm; 143 144 while ((parm = strsep(&s, DELIM))) 145 { 146 if (!StrCompare(parm, w)) 147 { 148 return 1; 149 } 150 } 151 152 return 0; 153 } 154 155 int is_nick(char *s) 156 { 157 return ((NULL == strchr(s, ',')) && (strlen(s) <= (unsigned) conf.nick_length)); /* ``unsigned'' is not needed, stops a warning when compiled with -Wsign-compare */ 158 } 159 160 int is_pattern(char *s) 161 { 162 while ((*s != '\0') && (*s != '!') && (*s != '*')) 163 { 164 ++s; 165 } 166 return (*s == '*') || (*s == '!'); 167 } 168 /* }}} */ 169 /* {{{ match_pattern() */ 170 int match_pattern(char *pattern, char *string) 171 { 172 if (!string || !pattern) 173 { 174 return 0; 175 } 176 177 while (1) 178 { 179 if (!*string && !*pattern) 180 { 181 return 1; 182 } 183 184 if (!*string || !*pattern) 185 { 186 return 0; 187 } 188 189 if ((*string == *pattern) || (*string && *pattern == '?')) 190 { 191 ++string; 192 ++pattern; 193 } 194 else if (*pattern == '*') 195 { 196 if (*string == *(pattern + 1)) 197 { 198 ++pattern; 199 } 200 else if (*(pattern + 1) == *(string + 1)) 201 { 202 ++string; 203 ++pattern; 204 } 205 else 206 { 207 ++string; 208 } 209 } 210 else 211 { 212 return 0; 213 } 214 } 215 } 216 /* }}} */