#include #include #include #include "misc.h" using namespace std; /* 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-tmp)<999) *pt++ = *str++; *pt = '\0'; dest.setfrom (tmp); 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(string &dest, const char *str) { str = str_skip(str); const char *start = str; while (*str > ' ') str++; dest = string(start,str-start); 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{ const char *start = line; while (*line != '\0' && *line != delim && *line != delim2) line++; if (line > start || *line == delim || *line == delim2){ SSTRING *word = new SSTRING; word->setfrom (start,line-start); words.add (word); } if (*line == delim || *line == delim2) line++; } } return words.getnb()-start; } int str_splitline ( const char *line, // Line to split char delim, // Field delimiter vector &words) // Will contain the separated words { SSTRINGS tmp; int ret = str_splitline (line,delim,tmp); for (int i=0; ic_str()); return ret; } int str_splitline ( const SSTRING &line, // Line to split char delim, // Field delimiter SSTRINGS &words) // Will contain the separated words { return str_splitline (line.get(),delim,words); } int str_splitline ( const string &line, // Line to split char delim, // Field delimiter vector &words) // Will contain the separated words { return str_splitline (line.c_str(),delim,words); } /* Decompose words in a line, including double quoted sequence. Words (and quoted sequences) are separated with white chars. Return the number of field written in words */ int str_splitlineq ( const char *line, // Line to split const char delim, const char quote1, const char quote2, vector &words) // Will contain the separated words { unsigned start = words.size(); while (1){ line = str_skip(line); if (*line == '\0') break; char tmp[10000]; if (*line == quote1 || *line == quote2){ line = str_copyquote (tmp,line,sizeof(tmp)-1); }else if (delim == ' '){ line = str_copyword (tmp,line,sizeof(tmp)-1); }else{ const char *pt = line; while (*pt != '\0' && *pt != delim) pt++; size_t len = pt-line; if (len > sizeof(tmp)-1) len = sizeof(tmp)-1; strncpy (tmp,line,len); tmp[len] = '\0'; line = pt; } words.push_back (tmp); if (delim != ' ' && *line == delim) line++; } return words.size()-start; } int str_splitlineq ( const char *line, // Line to split const char delim, const char quote1, const char quote2, SSTRINGS &words) // Will contain the separated words { vector tmp; int n = str_splitlineq (line,delim,quote1,quote2,tmp); for (int i=0; i &words) // Will contain the separated words { return str_splitlineq (line,' ','"','\'',words); } int str_splitlineq ( const char *line, // Line to split SSTRINGS &words) // Will contain the separated words { return str_splitlineq (line,' ','"','\'',words); } /* 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; } int string_cmp(const PARAM_STRING s1, const PARAM_STRING s2) { return strcmp(s1.ptr,s2.ptr); } int string_ncmp(const PARAM_STRING s1, const PARAM_STRING s2, size_t len) { return strncmp(s1.ptr,s2.ptr,len); } int string_casecmp(const PARAM_STRING s1, const PARAM_STRING s2) { return strcasecmp(s1.ptr,s2.ptr); } int string_ncasecmp(const PARAM_STRING s1, const PARAM_STRING s2, size_t len) { return strncasecmp(s1.ptr,s2.ptr,len); }