/** * extract the value of Content-Type header * - src: pointer to C-T content * - len: length of src * - ctype: parsed C-T * - flag: what to parse - bit mask of CT_TYPE, CT_CHARSET, CT_MSGR * * #return: 0 OK ; -1 error */ int m_extract_content_type(char* src, int len, t_content_type* ctype, int flag) { char *p, *end; int f = 0, pos; if( !src || len <=0 ) goto error; p = src; end = p + len; while((p < end) && f != flag) { EAT_SPACES(p, end); if((flag & CT_TYPE) && !(f & CT_TYPE)) { NEXT_SEP(p, pos, end); if(p[pos] == ';') { ctype->type.s = p; ctype->type.len = pos; SKIP_CHARS(p, pos+1, end); f |= CT_TYPE; continue; } } if((flag & CT_CHARSET) && !(f & CT_CHARSET)) { } if((flag & CT_MSGR) && !(f & CT_MSGR)) { } } return 0; error: return -1; }
bool Config::load(const char* fname) { E_ASSERT(fname != NULL); clear(); FILE *f = fopen(fname, "r"); if (!f) { errcode = CONF_ERR_FILE; return false; } // set default return values errcode = CONF_SUCCESS; bool status = true; // we must have at least one section bool sect_found = false; // use fixed sizes for sections and keys char section[ESECT_MAX]; char keybuf[EKEY_MAX]; // line and value can grow int buflen = ELINE_SIZE_START; char* buf = new char[buflen]; // use the same size as for line int valbuflen = buflen; char* valbuf = new char[valbuflen]; char *bufp; ConfigSection* tsect = NULL; while(config_getline(&buf, &buflen, f) != -1) { ++linenum; bufp = buf; EAT_SPACES(bufp); // comment or empty line if (*bufp == COMMENT || *bufp == '\0') continue; // we found an section if (*bufp == SECT_OPEN) { sect_found = true; bufp++; if (!scan_section(bufp, section, sizeof(section))) { errcode = CONF_ERR_BAD; status = false; break; } else { // first check if section exists, or create if not tsect = find_section(section); if (!tsect) { ++sectnum; tsect = new ConfigSection(section); section_list.push_back(tsect); } } } // data part else { // file without sections if (!sect_found) { errcode = CONF_ERR_SECTION; status = false; break; } /* * check if size of valbuf is less than buflen; * in that case make it size as buflen (better would be to use * buflen - EKEY_MAX - '=' - <spaces>, but that would complicate thing, * also more size does not hurts :P) */ if(valbuflen < buflen) { valbuflen = buflen; delete [] valbuf; valbuf = new char[valbuflen]; } if (!scan_keyvalues(bufp, keybuf, valbuf, buflen, EKEY_MAX, valbuflen)) { errcode = CONF_ERR_BAD; status = false; break; } E_ASSERT(tsect != NULL && "Entry without a section ?!"); tsect->add_entry(keybuf, valbuf); } } fclose(f); delete [] buf; delete [] valbuf; return status; }
/** * gnm_complex_from_string: * @dst: return location * @src: string to parse * @imunit: (out): return location of imaginary unit. * * Returns: zero on success, -1 otherwise. * * This function differs from Excel's parsing in at least the following * ways: * (1) We allow spaces before the imaginary unit used with an impled "1". * Therefore we allow "+ i". * (2) We do not allow a thousands separator as in "1,000i". */ int gnm_complex_from_string (gnm_complex *dst, char const *src, char *imunit) { gnm_float x, y; char *end; int sign; EAT_SPACES (src); HANDLE_SIGN (src, sign); /* Case: "i", "+i", "-i", ... */ if (*src == 'i' || *src == 'j') { x = 1; } else { x = gnm_strto (src, &end); if (src == end || errno == ERANGE) return -1; src = end; EAT_SPACES (src); } if (sign < 0) x = 0 - x; /* Case: "42", "+42", "-42", ... */ if (*src == 0) { *dst = GNM_CREAL (x); *imunit = 'i'; return 0; } /* Case: "42i", "+42i", "-42i", "-i", "i", ... */ if (*src == 'i' || *src == 'j') { *imunit = *src++; EAT_SPACES (src); if (*src == 0) { *dst = GNM_CMAKE (0, x); return 0; } else return -1; } HANDLE_SIGN (src, sign); if (!sign) return -1; if (*src == 'i' || *src == 'j') { y = 1; } else { y = gnm_strto (src, &end); if (src == end || errno == ERANGE) return -1; src = end; EAT_SPACES (src); } if (sign < 0) y = 0 - y; /* Case: "42+12i", "+42-12i", "-42-12i", "-42+i", "+42-i", ... */ if (*src == 'i' || *src == 'j') { *imunit = *src++; EAT_SPACES (src); if (*src == 0) { *dst = GNM_CMAKE (x, y); return 0; } } return -1; }