string splitting function

This commit is contained in:
Ron Pedde 2006-03-20 22:27:56 +00:00
parent 5020ed6884
commit 2fbb744e6f

View File

@ -122,13 +122,13 @@ CONF_ELEMENTS *_conf_get_keyinfo(char *section, char *key) {
pcurrent = &conf_elements[0]; pcurrent = &conf_elements[0];
while(pcurrent->section && pcurrent->term) { while(pcurrent->section && pcurrent->term) {
if((strcasecmp(section,pcurrent->section) != 0) || if((strcasecmp(section,pcurrent->section) != 0) ||
(strcasecmp(key,pcurrent->term) != 0)) { (strcasecmp(key,pcurrent->term) != 0)) {
pcurrent++; pcurrent++;
} else { } else {
found = 1; found = 1;
break; break;
} }
} }
return found ? pcurrent : NULL; return found ? pcurrent : NULL;
@ -415,22 +415,22 @@ int conf_read(char *file) {
} }
/* see what kind this is, and act accordingly */ /* see what kind this is, and act accordingly */
pce = _conf_get_keyinfo(section_name,term); pce = _conf_get_keyinfo(section_name,term);
key_type = CONF_T_STRING; key_type = CONF_T_STRING;
if(pce) if(pce)
key_type = pce->type; key_type = pce->type;
switch(key_type) { switch(key_type) {
case CONF_T_MULTICOMMA: case CONF_T_MULTICOMMA:
break; break;
case CONF_T_INT: case CONF_T_INT:
case CONF_T_STRING: case CONF_T_STRING:
case CONF_T_EXISTPATH: case CONF_T_EXISTPATH:
default: default:
ll_add_string(pllcurrent,term,value); ll_add_string(pllcurrent,term,value);
break; break;
} }
if(comment) { if(comment) {
@ -842,4 +842,52 @@ int conf_isset(char *section, char *key) {
return retval; return retval;
} }
/**
* split a string on delimiter boundaries, filling
* a string-pointer array.
*
* The user must free both the first element in the array,
* and the array itself.
*
* @param s string to split
* @param delimiters boundaries to split on
* @param argvp an argv array to be filled
* @returns number of tokens
*/
int _conf_split(char *s, char *delimiters, char ***argvp) {
int i;
int numtokens;
const char *snew;
char *t;
char *tokptr;
if ((s == NULL) || (delimiters == NULL) || (argvp == NULL))
return -1;
*argvp = NULL;
snew = s + strspn(s, delimiters);
if ((t = malloc(strlen(snew) + 1)) == NULL)
return -1;
strcpy(t, snew);
numtokens = 0;
tokptr = NULL;
while(strtok_r(t,delimiters,&tokptr) != NULL)
numtokens++;
if ((*argvp = malloc((numtokens + 1)*sizeof(char *))) == NULL) {
free(t);
return -1;
}
if (numtokens == 0)
free(t);
else {
strcpy(t, snew);
tokptr = NULL;
for (i = 0; i < numtokens; i++)
*((*argvp) + i) = strtok_r(t, delimiters, &tokptr);
}
*((*argvp) + numtokens) = NULL;
return numtokens;
}