mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 03:25:20 +00:00
list: Add helper to convert strings separated by ', ' to array
tools/virsh.c: New helper function vshStringToArray. tools/virsh.h: Declare vshStringToArray. tools/virsh-domain.c: use the helper in cmdUndefine.
This commit is contained in:
parent
cf45862817
commit
f7eac4f722
@ -2443,23 +2443,8 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
|
||||
|
||||
/* tokenize the string from user and save it's parts into an array */
|
||||
if (volumes) {
|
||||
/* count the delimiters */
|
||||
volume_tok = volumes;
|
||||
nvolume_tokens = 1; /* we need at least one member */
|
||||
while (*volume_tok) {
|
||||
if (*(volume_tok++) == ',')
|
||||
nvolume_tokens++;
|
||||
}
|
||||
|
||||
volume_tokens = vshCalloc(ctl, nvolume_tokens, sizeof(char *));
|
||||
|
||||
/* tokenize the input string */
|
||||
nvolume_tokens = 0;
|
||||
volume_tok = volumes;
|
||||
do {
|
||||
volume_tokens[nvolume_tokens] = strsep(&volume_tok, ",");
|
||||
nvolume_tokens++;
|
||||
} while (volume_tok);
|
||||
if ((nvolume_tokens = vshStringToArray(volumes, &volume_tokens)) < 0)
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if ((nvolumes = virXPathNodeSet("./devices/disk", ctxt,
|
||||
|
@ -166,6 +166,50 @@ vshPrettyCapacity(unsigned long long val, const char **unit)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Convert the strings separated by ',' into array. The caller
|
||||
* must free the returned array after use.
|
||||
*
|
||||
* Returns the length of the filled array on success, or -1
|
||||
* on error.
|
||||
*/
|
||||
int
|
||||
vshStringToArray(char *str,
|
||||
char ***array)
|
||||
{
|
||||
char *str_tok = NULL;
|
||||
unsigned int nstr_tokens = 0;
|
||||
char **arr = NULL;
|
||||
|
||||
/* tokenize the string from user and save it's parts into an array */
|
||||
if (str) {
|
||||
nstr_tokens = 1;
|
||||
|
||||
/* count the delimiters */
|
||||
str_tok = str;
|
||||
while (*str_tok) {
|
||||
if (*str_tok == ',')
|
||||
nstr_tokens++;
|
||||
str_tok++;
|
||||
}
|
||||
|
||||
if (VIR_ALLOC_N(arr, nstr_tokens) < 0) {
|
||||
virReportOOMError();
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* tokenize the input string */
|
||||
nstr_tokens = 0;
|
||||
str_tok = str;
|
||||
do {
|
||||
arr[nstr_tokens] = strsep(&str_tok, ",");
|
||||
nstr_tokens++;
|
||||
} while (str_tok);
|
||||
}
|
||||
|
||||
*array = arr;
|
||||
return nstr_tokens;
|
||||
}
|
||||
|
||||
virErrorPtr last_error;
|
||||
|
||||
|
@ -328,6 +328,7 @@ int vshAskReedit(vshControl *ctl, const char *msg);
|
||||
int vshStreamSink(virStreamPtr st, const char *bytes, size_t nbytes,
|
||||
void *opaque);
|
||||
double vshPrettyCapacity(unsigned long long val, const char **unit);
|
||||
int vshStringToArray(char *str, char ***array);
|
||||
|
||||
/* Typedefs, function prototypes for job progress reporting.
|
||||
* There are used by some long lingering commands like
|
||||
|
Loading…
Reference in New Issue
Block a user