#include #include #include #include "misc.h" /* Copy one word from a string. A word is a sequence of non white space. Return a pointer on the first blank character after the word. If there were no word, dest[0] == '\0' */ char *str_copyword(char *dest, const char *str) { if (str != NULL){ str = str_skip(str); while (*str > ' ') *dest++ = *str++; } *dest = '\0'; return (char*) str; } char *str_copyword(char *dest, const char *str, int size) { if (str != NULL){ str = str_skip(str); size--; while (*str > ' ' && size > 0){ *dest++ = *str++; size--; } } *dest = '\0'; return (char*) str; } /* Check if a string start with a given word followed by a space or '\0'; Return != 0 if this is true. */ int str_isword (const char *str, const char *word) { int len = strlen (word); int ret = strncmp(str,word,len)==0; if (ret){ char car = str[len]; ret = (isspace(car) || car == '\0'); } return ret; } /* Like strncpy but make sure the '\0' is there size if the sizeof() of the destination. One byte is kept to put the '\0'; */ void strcpy_cut (char *dst, const char *src, int size) { if (size >= 0){ if (size > 0){ size--; strncpy (dst,src,size); } dst[size] = '\0'; } } /* Move over one word */ char *str_skipword(const char *str) { str = str_skip(str); while (*str > ' ') str++; str = str_skip(str); return (char*) str; } /* Copy one word from a string. A word is a sequence of non white space. Return a pointer on the first blank character after the word. If there were no word, dest[0] == '\0' */ char *str_copyword(SSTRING &dest, const char *str) { str = str_skip(str); char tmp[1000]; char *pt = tmp; while (*str > ' ') *pt++ = *str++; *pt = '\0'; dest.setfrom (tmp); return (char*) str; } /* Free all entry of the string table. */ void tbstr_free (char *tb[], int nb) { for (int i=0; i= ' ') line = str_skip(line); if (*line == '\0'){ break; }else{ char word[100]; char *pt = word; while (*line != '\0' && *line != delim && (pt-word) < 99) *pt++ = *line++; *pt = '\0'; // Forget the rest of a word which is too long (this must be // crap anyway and not expected by the caller) while (*line != '\0' && *line != delim) line++; if (word[0] != '\0' || *line == delim) words.add (new SSTRING(word)); if (*line == delim) line++; } } return words.getnb()-start; } /* Decompose words in a line, including double quoted sequence. Words (and quoted sequences) are separated with with white chars. Return the number of field written in words */ int str_splitlineq ( const char *line, // Line to split SSTRINGS &words) // Will contain the separated words { int start = words.getnb(); while (1){ line = str_skip(line); if (*line == '\0') break; char tmp[1000]; if (*line == '"'){ line = str_copyquote (tmp,line,sizeof(tmp)-1); }else{ line = str_copyword (tmp,line,sizeof(tmp)-1); } words.add (new SSTRING(tmp)); } return words.getnb()-start; } /* Split a multi-line string in a table Return the number of lines found. */ int str_cnv2lines(const char *pt, SSTRINGS &tb) { int ret = 0; while (*pt != '\0'){ const char *start = pt; while (*pt != '\n' && *pt != '\0') pt++; SSTRING *n = new SSTRING; int len = (int)(pt-start); if (len > 0) n->setfrom (start,len); tb.add (n); ret++; if (*pt == '\n') pt++; } return ret; }