/** * If the first none space character is a quotation mark, returns the string * between two quotation marks, else returns the content before the first comma. * Updates *p_cur. */ static char *nextTok(char **p_cur) { char *ret = NULL; skipWhiteSpace(p_cur); if (*p_cur == NULL) { ret = NULL; } else if (**p_cur == '"') { enum State {END, NORMAL, ESCAPE} state = NORMAL; (*p_cur)++; ret = *p_cur; while (state != END) { switch (state) { case NORMAL: switch (**p_cur) { case '\\': state = ESCAPE; break; case '"': state = END; break; case '\0': /* * Error case, parsing string is not quoted by ending * double quote, e.g. "bla bla, this function expects input * string to be NULL terminated, so that the loop can exit. */ ret = NULL; goto exit; default: /* Stays in normal case. */ break; } break; case ESCAPE: state = NORMAL; break; default: /* This should never happen. */ break; } if (state == END) { **p_cur = '\0'; } (*p_cur)++; } skipNextComma(p_cur); } else { ret = strsep(p_cur, ","); } exit: return ret; }
static char * nextTok(char **p_cur) { char *ret = NULL; skipWhiteSpace(p_cur); if (*p_cur == NULL) { ret = NULL; } else if (**p_cur == '"') { (*p_cur)++; ret = strsep(p_cur, "\""); skipNextComma(p_cur); } else { ret = strsep(p_cur, ","); } return ret; }