void test_splitnamevalue(const std::string& istr, const std::string& exp_pn, const std::string& exp_pv) { std::string pn, pv; splitnamevalue(istr, pn, pv); if (pn != exp_pn || pv != exp_pv) { INFO_PRINT << "splitnamevalue( \"" << istr << "\" ) fails!\n"; } }
PairStruct * text_to_name_value_pairs (const char * text) { /* Process a multi-line and/or ;-separated text and create a list of name=value pairs from each line which has a name = value pattern. Whitespaces are removed. "X = 1 Y = 2" is not valid because of missing ';', but "X=1; Y=5; Z=apple" is valid */ char *name, *value; char *item, *delim; int len; char line[256]; PairStruct *res = NULL, *last = NULL, *pair; if (!text) return res; item = (char *)text; while (item) { delim = strchr (item, ';'); if (delim) len = (int) (delim-item); else len = strlen (item); strncpy (line, item, len); line[len] = '\0'; //printf (" --Line=[%s]\n", line); splitnamevalue(line, len, &name, &value); if (name) { pair = (PairStruct *) malloc (sizeof(PairStruct)); pair->name = name; pair->value = value; pair->next = NULL; if (last) { last->next = pair; last = pair; } else { res = pair; last = pair; } } if (delim && delim+1 != 0) item = delim+1; else item = NULL; } return res; }