SMFList_T *smf_settings_group_get_list(SMFSettings_T *settings, char *group_name, char *key) { char *tmp = NULL; char *s = NULL; char **sl = NULL; char **p = NULL; SMFList_T *list = NULL; assert(settings); assert(group_name); assert(key); if (smf_list_new(&list,smf_internal_string_list_destroy)!=0) return NULL; asprintf(&tmp,"%s:%s",group_name,key); s = smf_dict_get(settings->groups,tmp); free(tmp); sl = smf_core_strsplit(s, ";", NULL); p = sl; while(*p != NULL) { tmp = smf_core_strstrip(*p); smf_list_append(list, tmp); p++; } free(sl); return list; }
END_TEST START_TEST(strsplit_no_split) { const char *src = "value1"; char **sl = NULL; int nelems = -99; fail_unless((sl = smf_core_strsplit(src, ";", &nelems)) != NULL); fail_unless(strcmp(sl[0], "value1") == 0); fail_unless(sl[1] == '\0'); fail_unless(nelems == 1); free(sl[0]); free(sl); }
END_TEST START_TEST(strsplit_no_nelems) { const char *src = "value1;value2;value3"; char **sl = NULL; fail_unless((sl = smf_core_strsplit(src, ";", NULL)) != NULL); fail_unless(strcmp(sl[0], "value1") == 0); fail_unless(strcmp(sl[1], "value2") == 0); fail_unless(strcmp(sl[2], "value3") == 0); fail_unless(sl[3] == '\0'); free(sl[0]); free(sl[1]); free(sl[2]); free(sl); }
int smf_core_expand_string(const char *format, const char *addr, char **buf) { int rep_made = 0; int nelems; char **parts = smf_core_strsplit(addr, "@", &nelems); char *out; size_t out_size; int offs; assert(format != NULL); assert(addr != NULL); assert(buf != NULL); out_size = strlen(format) + 1; if ((out = malloc(out_size)) == NULL) { return -1; } // Prepare the result with the format. // The '%<x>'-expressions are replaced later. strncpy(out, format, out_size); out[out_size - 1] = '\0'; offs = 0; while(out[offs] != '\0') { if(out[offs] == '%') { const char *insert = NULL; switch(out[offs + 1]) { case 's': insert = addr; break; case 'u': insert = parts[0]; break; case 'd': if (nelems < 2) { smf_core_strsplit_free(parts); free(out); return -1; } insert = parts[1]; break; default: smf_core_strsplit_free(parts); free(out); return(-2); break; /* never reached */ } // Replace the both option-characters with the "insert"-string if (insert != NULL) { // New size of out: size of insert-string but without the two option-characters const size_t insert_len = strlen(insert); out_size += (insert_len - 2); out = realloc(out, out_size); // First move everything behind the option-characters memmove(out + offs + insert_len, out + offs + 2, strlen(out + offs + 2) + 1); // Now insert the "insert"-string at the current position memcpy(out + offs, insert, insert_len); offs += insert_len - 1; } else { offs++; } rep_made++; } offs++; } *buf = out; smf_core_strsplit_free(parts); return(rep_made); }