src/stringutil.c File Reference

#include "copyright.h"
#include "config.h"
#include "mudconf.h"
#include "externs.h"
#include "alloc.h"
#include "ansi.h"

Include dependency graph for stringutil.c:

Go to the source code of this file.

Functions

char * translate_string (const char *str, int type)
char * upcasestr (char *s)
char * skip_space (const char *s)
char * seek_char (const char *s, char c)
char * munge_space (char *string)
char * trim_spaces (char *string)
char * grabto (char **str, char targ)
int string_compare (const char *s1, const char *s2)
int string_prefix (const char *string, const char *prefix)
const char * string_match (const char *src, const char *sub)
char * replace_string (const char *old, const char *new, const char *string)
char * replace_string_inplace (const char *old, const char *new, char *string)
int count_chars (const char *str, const char c)
int prefix_match (const char *s1, const char *s2)
int minmatch (char *str, char *target, int min)
char * strsave (const char *s)
int safe_copy_str (char *src, char *buff, char **bufp, int max)
int safe_copy_chr (char src, char *buff, char **bufp, int max)
int matches_exit_from_list (char *str, char *pattern)


Function Documentation

int count_chars ( const char *  str,
const char  c 
)

Counts occurances of C in STR. - mnp 7 feb 91

Definition at line 452 of file stringutil.c.

00453 {
00454         int out = 0;
00455         const char *p = str;
00456 
00457         if(p)
00458                 while (*p != '\0')
00459                         if(*p++ == c)
00460                                 out++;
00461         return out;
00462 }

char* grabto ( char **  str,
char  targ 
)

Return portion of a string up to the indicated character. Also returns a modified pointer to the string ready for another call.

Definition at line 291 of file stringutil.c.

Referenced by decrypt_logindata().

00292 {
00293         char *savec, *cp;
00294 
00295         if(!str || !*str || !**str)
00296                 return NULL;
00297 
00298         savec = cp = *str;
00299         while (*cp && *cp != targ)
00300                 cp++;
00301         if(*cp)
00302                 *cp++ = '\0';
00303         *str = cp;
00304         return savec;
00305 }

int matches_exit_from_list ( char *  str,
char *  pattern 
)

Definition at line 540 of file stringutil.c.

References EXIT_DELIMITER, and ToLower.

Referenced by match_exit_internal(), and process_command().

00541 {
00542         char *s;
00543 
00544         while (*pattern) {
00545                 for(s = str;                    /*
00546                                                                  * check out this one 
00547                                                                  */
00548                         (*s && (ToLower(*s) == ToLower(*pattern)) && *pattern &&
00549                          (*pattern != EXIT_DELIMITER)); s++, pattern++);
00550 
00551                 /*
00552                  * Did we match it all? 
00553                  */
00554 
00555                 if(*s == '\0') {
00556 
00557                         /*
00558                          * Make sure nothing afterwards 
00559                          */
00560 
00561                         while (*pattern && isspace(*pattern))
00562                                 pattern++;
00563 
00564                         /*
00565                          * Did we get it? 
00566                          */
00567 
00568                         if(!*pattern || (*pattern == EXIT_DELIMITER))
00569                                 return 1;
00570                 }
00571                 /*
00572                  * We didn't get it, find next string to test 
00573                  */
00574 
00575                 while (*pattern && *pattern++ != EXIT_DELIMITER);
00576                 while (isspace(*pattern))
00577                         pattern++;
00578         }
00579         return 0;
00580 }

int minmatch ( char *  str,
char *  target,
int  min 
)

Definition at line 481 of file stringutil.c.

References ToLower.

Referenced by cf_ntab_access(), find_nametab_ent(), and search_nametab().

00482 {
00483         while (*str && *target && (ToLower(*str) == ToLower(*target))) {
00484                 str++;
00485                 target++;
00486                 min--;
00487         }
00488         if(*str)
00489                 return 0;
00490         if(!*target)
00491                 return 1;
00492         return ((min <= 0) ? 1 : 0);
00493 }

char* munge_space ( char *  string  ) 

Compress multiple spaces to one space, also remove leading and trailing spaces.

Definition at line 227 of file stringutil.c.

References alloc_lbuf.

Referenced by create_obj().

00228 {
00229         char *buffer, *p, *q;
00230 
00231         buffer = alloc_lbuf("munge_space");
00232         p = string;
00233         q = buffer;
00234         while (p && *p && isspace(*p))
00235                 p++;                                    /*
00236                                                                  * remove inital spaces 
00237                                                                  */
00238         while (p && *p) {
00239                 while (*p && !isspace(*p))
00240                         *q++ = *p++;
00241                 while (*p && isspace(*++p));
00242                 if(*p)
00243                         *q++ = ' ';
00244         }
00245         *q = '\0';                                      /*
00246                                                                  * remove terminal spaces and terminate * * * 
00247                                                                  * 
00248                                                                  * * string 
00249                                                                  */
00250         return (buffer);
00251 }

int prefix_match ( const char *  s1,
const char *  s2 
)

returns the number of identical characters in the two strings

Definition at line 467 of file stringutil.c.

References ToLower.

00468 {
00469         int count = 0;
00470 
00471         while (*s1 && *s2 && (ToLower(*s1) == ToLower(*s2)))
00472                 s1++, s2++, count++;
00473         /*
00474          * If the whole string matched, count the null.  (Yes really.) 
00475          */
00476         if(!*s1 && !*s2)
00477                 count++;
00478         return count;
00479 }

char* replace_string ( const char *  old,
const char *  new,
const char *  string 
)

Returns an lbuf containing string STRING with all occurances of OLD replaced by NEW. OLD and NEW may be different lengths.

Definition at line 392 of file stringutil.c.

References alloc_lbuf, safe_chr, and safe_str.

Referenced by add_folder_name(), bind_and_queue(), do_edit_msg(), do_setnewtitle(), edit_string(), edit_string_ansi(), fun_iter(), fun_list(), fun_parse(), replace_string_inplace(), and search_perform().

00393 {
00394         char *result, *r, *s;
00395         int olen;
00396 
00397         if(string == NULL)
00398                 return NULL;
00399         s = (char *) string;
00400         olen = strlen(old);
00401         r = result = alloc_lbuf("replace_string");
00402         while (*s) {
00403 
00404                 /*
00405                  * Copy up to the next occurrence of the first char of OLD 
00406                  */
00407 
00408                 while (*s && *s != *old) {
00409                         safe_chr(*s, result, &r);
00410                         s++;
00411                 }
00412 
00413                 /*
00414                  * If we are really at an OLD, append NEW to the result and * 
00415                  * 
00416                  * *  * *  * * bump the input string past the occurrence of
00417                  * OLD. *  * * * Otherwise, copy the char and try again. 
00418                  */
00419 
00420                 if(*s) {
00421                         if(!strncmp(old, s, olen)) {
00422                                 safe_str((char *) new, result, &r);
00423                                 s += olen;
00424                         } else {
00425                                 safe_chr(*s, result, &r);
00426                                 s++;
00427                         }
00428                 }
00429         }
00430         *r = '\0';
00431         return result;
00432 }

char* replace_string_inplace ( const char *  old,
const char *  new,
char *  string 
)

Returns string STRING with all occurances * of OLD replaced by NEW. OLD and NEW may be different lengths. Modifies string, so: Note - STRING must already be allocated large enough to handle the new size. (mitch 1 feb 91)

Definition at line 439 of file stringutil.c.

References free_lbuf, replace_string(), and StringCopy.

00440 {
00441         char *s;
00442 
00443         s = replace_string(old, new, string);
00444         StringCopy(string, s);
00445         free_lbuf(s);
00446         return string;
00447 }

int safe_copy_chr ( char  src,
char *  buff,
char **  bufp,
int  max 
)

Copy buffers, watching for overflows.

Definition at line 524 of file stringutil.c.

00525 {
00526         char *tp;
00527         int retval;
00528 
00529         tp = *bufp;
00530         retval = 0;
00531         if((tp - buff) < max) {
00532                 *tp++ = src;
00533         } else {
00534                 retval = 1;
00535         }
00536         *bufp = tp;
00537         return retval;
00538 }

int safe_copy_str ( char *  src,
char *  buff,
char **  bufp,
int  max 
)

Copy buffers, watching for overflows.

Definition at line 508 of file stringutil.c.

Referenced by do_doing().

00509 {
00510         char *tp;
00511 
00512         tp = *bufp;
00513         if(src == NULL)
00514                 return 0;
00515         while (*src && ((tp - buff) < max))
00516                 *tp++ = *src++;
00517         *bufp = tp;
00518         return strlen(src);
00519 }

char* seek_char ( const char *  s,
char  c 
)

returns a pointer to the next character in s matching c, or a pointer to the at the end of s. Yes, this is a lot like index, but not exactly.

Definition at line 213 of file stringutil.c.

Referenced by parse_msglist().

00214 {
00215         char *cp;
00216 
00217         cp = (char *) s;
00218         while (cp && *cp && (*cp != c))
00219                 cp++;
00220         return (cp);
00221 }

char* skip_space ( const char *  s  ) 

Definition at line 199 of file stringutil.c.

00200 {
00201         char *cp;
00202 
00203         cp = (char *) s;
00204         while (cp && *cp && isspace(*cp))
00205                 cp++;
00206         return (cp);
00207 }

int string_compare ( const char *  s1,
const char *  s2 
)

Definition at line 307 of file stringutil.c.

References mudconf, confdata::space_compress, and ToLower.

Referenced by badname_remove(), cf_func_access(), do_chown(), do_convtime(), do_last(), do_move(), do_name(), do_teleport(), eval_boolexp(), fun_stats(), lookup_player(), match_here(), match_home(), match_list(), match_me(), ok_name(), and process_command().

00308 {
00309         if(!mudconf.space_compress) {
00310                 while (*s1 && *s2 && ToLower(*s1) == ToLower(*s2))
00311                         s1++, s2++;
00312 
00313                 return (ToLower(*s1) - ToLower(*s2));
00314         } else {
00315                 while (isspace(*s1))
00316                         s1++;
00317                 while (isspace(*s2))
00318                         s2++;
00319                 while (*s1 && *s2 && ((ToLower(*s1) == ToLower(*s2)) ||
00320                                                           (isspace(*s1) && isspace(*s2)))) {
00321                         if(isspace(*s1) && isspace(*s2)) {      /*
00322                                                                                                  * skip all  
00323                                                                                                  * other
00324                                                                                                  * spaces 
00325                                                                                                  */
00326                                 while (isspace(*s1))
00327                                         s1++;
00328                                 while (isspace(*s2))
00329                                         s2++;
00330                         } else {
00331                                 s1++;
00332                                 s2++;
00333                         }
00334                 }
00335                 if((*s1) && (*s2))
00336                         return (1);
00337                 if(isspace(*s1)) {
00338                         while (isspace(*s1))
00339                                 s1++;
00340                         return (*s1);
00341                 }
00342                 if(isspace(*s2)) {
00343                         while (isspace(*s2))
00344                                 s2++;
00345                         return (*s2);
00346                 }
00347                 if((*s1) || (*s2))
00348                         return (1);
00349                 return (0);
00350         }
00351 }

const char* string_match ( const char *  src,
const char *  sub 
)

Accepts only nonempty matches starting at the beginning of a word

Definition at line 370 of file stringutil.c.

References string_prefix().

Referenced by add_folder_name(), do_find(), get_folder_name(), and match_list().

00371 {
00372         if((*sub != '\0') && (src)) {
00373                 while (*src) {
00374                         if(string_prefix(src, sub))
00375                                 return src;
00376                         /*
00377                          * else scan to beginning of next word 
00378                          */
00379                         while (*src && isalnum(*src))
00380                                 src++;
00381                         while (*src && !isalnum(*src))
00382                                 src++;
00383                 }
00384         }
00385         return 0;
00386 }

int string_prefix ( const char *  string,
const char *  prefix 
)

Definition at line 353 of file stringutil.c.

References ToLower.

Referenced by check_connect(), do_mail_debug(), dump_users(), find_connected_name(), search_perform(), search_setup(), and string_match().

00354 {
00355         int count = 0;
00356 
00357         while (*string && *prefix && ToLower(*string) == ToLower(*prefix))
00358                 string++, prefix++, count++;
00359         if(*prefix == '\0')                     /*
00360                                                                  * Matched all of prefix 
00361                                                                  */
00362                 return (count);
00363         else
00364                 return (0);
00365 }

char* strsave ( const char *  s  ) 

Definition at line 495 of file stringutil.c.

References StringCopy, and XMALLOC.

Referenced by cf_cmd_alias(), dup_bool(), getboolexp1(), init_cmdtab(), and test_atr().

00496 {
00497         char *p;
00498         p = (char *) XMALLOC(sizeof(char) * (strlen(s) + 1), "strsave");
00499 
00500         if(p)
00501                 StringCopy(p, s);
00502         return p;
00503 }

char* translate_string ( const char *  str,
int  type 
)

Convert raw character sequences into MUX substitutions (type = 1) or strips them (type = 0).

Definition at line 23 of file stringutil.c.

References c, ESC_CHAR, LBUF_SIZE, safe_chr, safe_str, and StringCopy.

Referenced by fun_translate().

00024 {
00025         char old[LBUF_SIZE];
00026         static char new[LBUF_SIZE];
00027         char *j, *c, *bp;
00028         int i;
00029 
00030         bp = new;
00031         StringCopy(old, str);
00032 
00033         for(j = old; *j != '\0'; j++) {
00034                 switch (*j) {
00035                 case ESC_CHAR:
00036                         c = strchr(j, 'm');
00037                         if(c) {
00038                                 if(!type) {
00039                                         j = c;
00040                                         break;
00041                                 }
00042 
00043                                 *c = '\0';
00044                                 i = atoi(j + 2);
00045                                 switch (i) {
00046                                 case 0:
00047                                         safe_str("%cn", new, &bp);
00048                                         break;
00049                                 case 1:
00050                                         safe_str("%ch", new, &bp);
00051                                         break;
00052                                 case 5:
00053                                         safe_str("%cf", new, &bp);
00054                                         break;
00055                                 case 7:
00056                                         safe_str("%ci", new, &bp);
00057                                         break;
00058                                 case 30:
00059                                         safe_str("%cx", new, &bp);
00060                                         break;
00061                                 case 31:
00062                                         safe_str("%cr", new, &bp);
00063                                         break;
00064                                 case 32:
00065                                         safe_str("%cg", new, &bp);
00066                                         break;
00067                                 case 33:
00068                                         safe_str("%cy", new, &bp);
00069                                         break;
00070                                 case 34:
00071                                         safe_str("%cb", new, &bp);
00072                                         break;
00073                                 case 35:
00074                                         safe_str("%cm", new, &bp);
00075                                         break;
00076                                 case 36:
00077                                         safe_str("%cc", new, &bp);
00078                                         break;
00079                                 case 37:
00080                                         safe_str("%cw", new, &bp);
00081                                         break;
00082                                 case 40:
00083                                         safe_str("%cX", new, &bp);
00084                                         break;
00085                                 case 41:
00086                                         safe_str("%cR", new, &bp);
00087                                         break;
00088                                 case 42:
00089                                         safe_str("%cG", new, &bp);
00090                                         break;
00091                                 case 43:
00092                                         safe_str("%cY", new, &bp);
00093                                         break;
00094                                 case 44:
00095                                         safe_str("%cB", new, &bp);
00096                                         break;
00097                                 case 45:
00098                                         safe_str("%cM", new, &bp);
00099                                         break;
00100                                 case 46:
00101                                         safe_str("%cC", new, &bp);
00102                                         break;
00103                                 case 47:
00104                                         safe_str("%cW", new, &bp);
00105                                         break;
00106                                 }
00107                                 j = c;
00108                         } else {
00109                                 safe_chr(*j, new, &bp);
00110                         }
00111                         break;
00112                 case ' ':
00113                         if((*(j + 1) == ' ') && type)
00114                                 safe_str("%b", new, &bp);
00115                         else
00116                                 safe_chr(' ', new, &bp);
00117                         break;
00118                 case '\\':
00119                         if(type)
00120                                 safe_str("\\", new, &bp);
00121                         else
00122                                 safe_chr('\\', new, &bp);
00123                         break;
00124                 case '%':
00125                         if(type)
00126                                 safe_str("%%", new, &bp);
00127                         else
00128                                 safe_chr('%', new, &bp);
00129                         break;
00130                 case '[':
00131                         if(type)
00132                                 safe_str("%[", new, &bp);
00133                         else
00134                                 safe_chr('[', new, &bp);
00135                         break;
00136                 case ']':
00137                         if(type)
00138                                 safe_str("%]", new, &bp);
00139                         else
00140                                 safe_chr(']', new, &bp);
00141                         break;
00142                 case '{':
00143                         if(type)
00144                                 safe_str("%{", new, &bp);
00145                         else
00146                                 safe_chr('{', new, &bp);
00147                         break;
00148                 case '}':
00149                         if(type)
00150                                 safe_str("%}", new, &bp);
00151                         else
00152                                 safe_chr('}', new, &bp);
00153                         break;
00154                 case '(':
00155                         if(type)
00156                                 safe_str("%(", new, &bp);
00157                         else
00158                                 safe_chr('(', new, &bp);
00159                         break;
00160                 case ')':
00161                         if(type)
00162                                 safe_str("%)", new, &bp);
00163                         else
00164                                 safe_chr(')', new, &bp);
00165                         break;
00166                 case '\r':
00167                         break;
00168                 case '\n':
00169                         if(type)
00170                                 safe_str("%r", new, &bp);
00171                         else
00172                                 safe_chr(' ', new, &bp);
00173                         break;
00174                 default:
00175                         safe_chr(*j, new, &bp);
00176                 }
00177         }
00178         *bp = '\0';
00179         return new;
00180 }

char* trim_spaces ( char *  string  ) 

Remove leading and trailing spaces.

Definition at line 256 of file stringutil.c.

References alloc_lbuf.

Referenced by create_player(), do_alias(), and do_name().

00257 {
00258         char *buffer, *p, *q;
00259 
00260         buffer = alloc_lbuf("trim_spaces");
00261         p = string;
00262         q = buffer;
00263         while (p && *p && isspace(*p))  /*
00264                                                                          * remove inital spaces 
00265                                                                          */
00266                 p++;
00267         while (p && *p) {
00268                 while (*p && !isspace(*p))      /*
00269                                                                          * copy nonspace chars 
00270                                                                          */
00271                         *q++ = *p++;
00272                 while (*p && isspace(*p))       /*
00273                                                                          * compress spaces 
00274                                                                          */
00275                         p++;
00276                 if(*p)
00277                         *q++ = ' ';                     /*
00278                                                                  * leave one space 
00279                                                                  */
00280         }
00281         *q = '\0';                                      /*
00282                                                                  * terminate string 
00283                                                                  */
00284         return (buffer);
00285 }

char* upcasestr ( char *  s  ) 

Definition at line 186 of file stringutil.c.

References ToUpper.

00187 {
00188         char *p;
00189 
00190         for(p = s; p && *p; p++)
00191                 *p = ToUpper(*p);
00192         return s;
00193 }


Generated on Mon May 28 04:25:47 2007 for BattletechMUX by  doxygen 1.4.7