CLI: Rewrite ast_el_strtoarr to use vector's internally.

This rewrites ast_el_strtoarr to use vector's internally, but still
return the original NULL terminated array of strings.

Change-Id: Ibfe776cbe14f750effa9ca360930acaccc02e957
certified/13.21
Corey Farrell 8 years ago
parent e07d94111d
commit e723331f4f

@ -3130,64 +3130,47 @@ static char *cli_prompt(EditLine *editline)
return ast_str_buffer(prompt); return ast_str_buffer(prompt);
} }
static void destroy_match_list(char **match_list, int matches)
{
if (match_list) {
int idx;
for (idx = 0; idx < matches; ++idx) {
ast_free(match_list[idx]);
}
ast_free(match_list);
}
}
static char **ast_el_strtoarr(char *buf) static char **ast_el_strtoarr(char *buf)
{ {
char *retstr; char *retstr;
char **match_list = NULL; char **match_list;
char **new_list; struct ast_vector_string *vec = ast_calloc(1, sizeof(*vec));
size_t match_list_len = 1;
int matches = 0; if (!vec) {
return NULL;
}
while ((retstr = strsep(&buf, " "))) { while ((retstr = strsep(&buf, " "))) {
if (!strcmp(retstr, AST_CLI_COMPLETE_EOF)) { if (!strcmp(retstr, AST_CLI_COMPLETE_EOF)) {
break; break;
} }
if (matches + 1 >= match_list_len) {
match_list_len <<= 1;
new_list = ast_realloc(match_list, match_list_len * sizeof(char *));
if (!new_list) {
destroy_match_list(match_list, matches);
return NULL;
}
match_list = new_list;
}
retstr = ast_strdup(retstr); retstr = ast_strdup(retstr);
if (!retstr) { if (!retstr || AST_VECTOR_APPEND(vec, retstr)) {
destroy_match_list(match_list, matches); ast_free(retstr);
return NULL; goto vector_cleanup;
} }
match_list[matches++] = retstr;
} }
if (!match_list) { if (!AST_VECTOR_SIZE(vec)) {
return NULL; goto vector_cleanup;
} }
if (matches >= match_list_len) { if (AST_VECTOR_APPEND(vec, NULL)) {
new_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(char *)); /* We failed to NULL terminate the elements */
if (!new_list) { goto vector_cleanup;
destroy_match_list(match_list, matches);
return NULL;
}
match_list = new_list;
} }
match_list[matches] = NULL; match_list = AST_VECTOR_STEAL_ELEMENTS(vec);
AST_VECTOR_PTR_FREE(vec);
return match_list; return match_list;
vector_cleanup:
AST_VECTOR_CALLBACK_VOID(vec, ast_free);
AST_VECTOR_PTR_FREE(vec);
return NULL;
} }
static int ast_el_sort_compare(const void *i1, const void *i2) static int ast_el_sort_compare(const void *i1, const void *i2)

Loading…
Cancel
Save