lib/eggdrop/string.c File Reference

#include <eggdrop/eggdrop.h>

Go to the source code of this file.

Functions

int egg_get_word (const char *text, const char **next, char **word)
int egg_get_arg (const char *text, const char **next, char **arg)
int egg_get_words (const char *text, const char **next, char **word,...)
int egg_get_args (const char *text, const char **next, char **arg,...)
int egg_get_word_array (const char *text, const char **next, char **word, int nwords)
int egg_get_arg_array (const char *text, const char **next, char **args, int nargs)
int egg_free_word_array (char **words, int nwords)
int egg_free_arg_array (char **args, int nargs)
void egg_append_static_str (char **dest, int *remaining, const char *src)
void egg_append_str (char **dest, int *cur, int *max, const char *src)
int str_ends_with (const char *text, const char *str)
int str_starts_with (const char *text, const char *str)
void str_tolower (char *str)
void shuffleArray (char *array[], size_t n)

Variables

static const char rcsid [] = "$Id: string.c,v 1.5 2007-04-14 15:21:12 sven Exp $"


Function Documentation

void egg_append_static_str ( char **  dest,
int *  remaining,
const char *  src 
)

Definition at line 205 of file string.c.

Referenced by queue_entry_to_text().

00206 {
00207   int len;
00208 
00209   if (!src || *remaining <= 0) return;
00210 
00211   len = strlen(src);
00212   if (len > *remaining) len = *remaining;
00213 
00214   memmove(*dest, src, len);
00215   *remaining -= len;
00216   *dest += len;
00217 }

void egg_append_str ( char **  dest,
int *  cur,
int *  max,
const char *  src 
)

Definition at line 220 of file string.c.

Referenced by convert_ansi_code_to_mirc(), convert_mirc_color_to_ansi(), and telnet_filter_write().

00221 {
00222   int len;
00223 
00224   if (!src) return;
00225 
00226   len = strlen(src);
00227   if (*cur + len + 10 > *max) {
00228     *max += len + 128;
00229     *dest = realloc(*dest, *max+1);
00230   }
00231   memmove(*dest + *cur, src, len);
00232   *cur += len;
00233 }

int egg_free_arg_array ( char **  args,
int  nargs 
)

Definition at line 199 of file string.c.

References egg_free_word_array().

00200 {
00201   return egg_free_word_array(args, nargs);
00202 }

int egg_free_word_array ( char **  words,
int  nwords 
)

Definition at line 188 of file string.c.

References NULL.

Referenced by egg_free_arg_array(), got_actchan(), got_away(), got_botbroadcast(), got_botmsg(), got_chat(), got_join(), got_link(), got_motd(), got_nickchange(), got_nlinked(), got_part(), got_privmsg(), got_unlink(), got_versions(), and got_who().

00189 {
00190   int i;
00191 
00192   for (i = 0; i < nwords; i++) {
00193     if (words[i]) free(words[i]);
00194     words[i] = NULL;
00195   }
00196   return(0);
00197 }

int egg_get_arg ( const char *  text,
const char **  next,
char **  arg 
)

Definition at line 51 of file string.c.

References NULL.

Referenced by egg_get_arg_array(), egg_get_args(), parse_chanset(), party_act(), party_chaninfo(), party_chanmembermode(), party_chanset(), party_link(), party_msg(), party_pls_chan(), party_set(), party_unlink(), party_whisper(), and party_whois().

00052 {
00053   int len = 0, max = 64, inquote = 0, insingle = 0, n, done;
00054 
00055   if (!arg || !text) {
00056     if (next) *next = NULL;
00057     if (arg) *arg = NULL;
00058     return(-1);
00059   }
00060   while (isspace(*text)) text++;
00061   if (!*text) {
00062     if (next) *next = NULL;
00063     *arg = NULL;
00064     return(-1);
00065   }
00066   *arg = malloc(max+10);
00067 
00068   done = 0;
00069   while (!done) {
00070     n = strcspn(text, "\"\\ \t'");
00071     if (n) {
00072       /* Guarantee there is always some unused space left. */
00073       if (n + len + 10 > max) {
00074         max = 2*max + n + 10;
00075         *arg = realloc(*arg, max+1);
00076       }
00077       memcpy(*arg + len, text, n);
00078       len += n;
00079     }
00080     text += n;
00081     switch (*text) {
00082       case '"':
00083         if (insingle) (*arg)[len++] = '"';
00084         else if (inquote) inquote = 0;
00085         else inquote = 1;
00086         text++;
00087         break;
00088       case '\'':
00089         if (inquote) (*arg)[len++] = '\'';
00090         else if (insingle) insingle = 0;
00091         else insingle = 1;
00092         text++;
00093         break;
00094       case ' ':
00095       case '\t':
00096         /* If we're *not* in a quoted string, this is
00097          * the end. Otherwise, just copy it. */
00098         if (!insingle && !inquote) done = 1;
00099         else (*arg)[len++] = *text;
00100         text++;
00101         break;
00102       case '\\':
00103         text++;
00104         if (insingle) (*arg)[len++] = '\\';
00105         else if (!inquote) (*arg)[len++] = *text++;
00106         else if (*text == '\\' || *text == '"') (*arg)[len++] = *text++;
00107         else (*arg)[len++] = '\\';
00108         break;
00109       default:
00110         done = 1;
00111         break;
00112     }
00113   }
00114   while (*text && isspace(*text)) text++;
00115   if (next) *next = text;
00116   (*arg)[len] = 0;
00117   return(0);
00118 }

int egg_get_arg_array ( const char *  text,
const char **  next,
char **  args,
int  nargs 
)

Definition at line 176 of file string.c.

References egg_get_arg(), and NULL.

00177 {
00178   int i;
00179 
00180   for (i = 0; i < nargs; i++) {
00181     if (egg_get_arg(text, &text, args+i)) break;
00182   }
00183   while (i < nargs) args[i++] = NULL;
00184   if (next) *next = text;
00185   return(i);
00186 }

int egg_get_args ( const char *  text,
const char **  next,
char **  arg,
  ... 
)

Definition at line 144 of file string.c.

References egg_get_arg(), and NULL.

Referenced by party_chattr(), party_chhandle(), party_chpass(), party_match(), party_matchattr(), party_matchwild(), party_minus_host(), and party_plus_host().

00145 {
00146   va_list args;
00147   int nargs = 0;
00148 
00149   va_start(args, arg);
00150   while (arg) {
00151     if (egg_get_arg(text, &text, arg)) break;
00152     nargs++;
00153     arg = va_arg(args, char **);
00154   }
00155   while (arg) {
00156     *arg = NULL;
00157     arg = va_arg(args, char **);
00158   }
00159   va_end(args);
00160   if (next) *next = text;
00161   return(nargs);
00162 }

int egg_get_word ( const char *  text,
const char **  next,
char **  word 
)

Definition at line 26 of file string.c.

References NULL.

Referenced by egg_get_word_array(), egg_get_words(), got_bcast(), got_unlinked(), and oldbotnet_on_read().

00027 {
00028   int len;
00029   const char *ptr;
00030 
00031   if (!word || !text) {
00032     if (next) *next = NULL;
00033     if (word) *word = NULL;
00034     return(-1);
00035   }
00036   while (isspace(*text)) text++;
00037   ptr = text;
00038   while (*ptr && !isspace(*ptr)) ptr++;
00039   if (next) *next = ptr;
00040   len = ptr - text;
00041   if (!len) {
00042     *word = NULL;
00043     return(-1);
00044   }
00045   *word = malloc(len + 1);
00046   memcpy(*word, text, len);
00047   (*word)[len] = 0;
00048   return(0);
00049 }

int egg_get_word_array ( const char *  text,
const char **  next,
char **  word,
int  nwords 
)

Definition at line 164 of file string.c.

References egg_get_word(), and NULL.

Referenced by got_actchan(), got_away(), got_botbroadcast(), got_botmsg(), got_chat(), got_join(), got_link(), got_motd(), got_nickchange(), got_nlinked(), got_part(), got_privmsg(), got_unlink(), got_versions(), and got_who().

00165 {
00166   int i;
00167 
00168   for (i = 0; i < nwords; i++) {
00169     if (egg_get_word(text, &text, word+i)) break;
00170   }
00171   while (i < nwords) word[i++] = NULL;
00172   if (next) *next = text;
00173   return(i);
00174 }

int egg_get_words ( const char *  text,
const char **  next,
char **  word,
  ... 
)

Definition at line 124 of file string.c.

References egg_get_word(), and NULL.

Referenced by party_plus_bot(), and party_plus_obot().

00125 {
00126   va_list args;
00127   int nwords = 0;
00128 
00129   va_start(args, word);
00130   while (word) {
00131     if (egg_get_word(text, &text, word)) break;
00132     nwords++;
00133     word = va_arg(args, char **);
00134   }
00135   while (word) {
00136     *word = NULL;
00137     word = va_arg(args, char **);
00138   }
00139   va_end(args);
00140   if (next) *next = text;
00141   return(nwords);
00142 }

void shuffleArray ( char *  array[],
size_t  n 
)

Definition at line 273 of file string.c.

References NULL.

Referenced by egg_dns_lookup(), and egg_dns_reverse().

00274 {
00275   size_t j = 0, i = 0;
00276   char *temp = NULL;
00277 
00278   for (i = 0; i < n; i++) {
00279     j = i + random() / (RAND_MAX / (n - i) + 1);
00280     temp = array[j];
00281     array[j] = array[i];
00282     array[i] = temp;
00283   }
00284 }

int str_ends_with ( const char *  text,
const char *  str 
)

Definition at line 236 of file string.c.

References egg_return_val_if_fail, and NULL.

00237 {
00238   int len1, len2;
00239 
00240   egg_return_val_if_fail (text != NULL, 0);
00241   egg_return_val_if_fail (str != NULL, 0);
00242 
00243   len1 = strlen (text);
00244   len2 = strlen (str);
00245 
00246   if (len2 > len1)
00247     return 0;
00248 
00249   return (strcmp (text + (len1 - len2), str) == 0);
00250 }

int str_starts_with ( const char *  text,
const char *  str 
)

Definition at line 253 of file string.c.

References egg_return_val_if_fail, and NULL.

00254 {
00255   egg_return_val_if_fail (text != NULL, 0);
00256   egg_return_val_if_fail (str != NULL, 0);
00257 
00258   while (*text && *str && *text++ == *str++)
00259     ;
00260 
00261   return (*str);
00262 }

void str_tolower ( char *  str  ) 

Definition at line 264 of file string.c.

Referenced by cache_lookup(), LookupWord(), uhost_cache_addref(), and uhost_cache_swap().

00265 {
00266   while (*str) {
00267     *str = tolower(*str);
00268     str++;
00269   }
00270 }


Variable Documentation

const char rcsid[] = "$Id: string.c,v 1.5 2007-04-14 15:21:12 sven Exp $" [static]

Definition at line 21 of file string.c.


Generated on Sun Nov 30 18:43:35 2008 for eggdrop1.9 by  doxygen 1.5.6