Index: common/shared.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/common/shared.c,v retrieving revision 1.66 diff -u -r1.66 shared.c --- common/shared.c 2001/09/15 21:25:11 1.66 +++ common/shared.c 2001/09/22 21:23:15 @@ -410,6 +410,64 @@ } /*************************************************************************** + PERL-like split. Takes a string of characters to split on, a buffer to + split, and an array of pointers to return. Can set a max size on the + number of chunks into which you can split the buffer. + + NOTE: This WILL change the original buffer, so if you want an original + copy of it, make it before you pass it to this function. + + Return the number of sub-elements found. +***************************************************************************/ +int split(char *toks, char *buf, char **args, int max_args) +{ + int i; + int nfound; + int len; + char *my_toks; + + if((buf[0] == '#') || (buf[0] == '\n')) /* Comment or blank line */ + return 0; + + /* Remove all leading whitespace and toks. + * Make sure it doesn't segfault if we were passed a NULL toks. */ + i = 0; + len = strlen(buf); + my_toks = toks ? toks : ""; + while((i < len) && (isspace(buf[i]) || strchr(my_toks, buf[i]))) + i++; + + /* Are we at the end? */ + if(i == len) + return 0; + + /* Split this puppy up. */ + nfound = 0; + args[nfound++] = buf+i; + while(nfound < max_args) { + /* Get through this word */ + while((i < len) && !isspace(buf[i]) && !strchr(my_toks, buf[i])) + i++; + + if(i == len) /* Are we done? */ + return nfound; + + /* Get rid of any whitespace or characters in my_toks */ + while((i < len) && (isspace(buf[i]) || strchr(my_toks, buf[i]))) + buf[i++] = '\0'; + + /* Are we done, or are we at a new word? */ + if(i == len) + return nfound; + else + args[nfound++] = buf+i; + } + + return nfound; +} + + +/*************************************************************************** Change spaces in s into newlines, so as to keep lines length len or shorter. That is, modifies s. Returns number of lines in modified s. Index: common/shared.h =================================================================== RCS file: /home/freeciv/CVS/freeciv/common/shared.h,v retrieving revision 1.87 diff -u -r1.87 shared.h --- common/shared.h 2001/09/15 21:25:12 1.87 +++ common/shared.h 2001/09/22 21:23:15 @@ -77,6 +77,7 @@ void remove_trailing_spaces(char *s); void remove_leading_trailing_spaces(char *s); void remove_trailing_char(char *s, char trailing); +int split(char *delims, char *buf, char **args, int max_args); int wordwrap_string(char *s, int len); int check_strlen(const char *str, size_t len, const char *errmsg);