Skip to content

Commit

Permalink
oapi-cli: support --Filters.VmIds.X alongside --Filters.VmIds[]
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Gatto <matthias.gatto@outscale.com>
  • Loading branch information
outscale-mgo committed Jul 25, 2024
1 parent f01c86f commit 1a73036
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
34 changes: 29 additions & 5 deletions cognac_gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,39 @@ EOF
elif [ 'array integer' == "$type" -o 'array string' == "$type" -o 'array double' == "$type" ]; then


convertor=""
local convertor=""
local null_val='""'
if [ 'array integer' == "$type" ]; then
convertor=atoi
null_val=0
elif [ 'array double' == "$type" ]; then
convertor=atof
null_val=0.0
fi
cat <<EOF
${indent_plus} TRY(!aa, "$a argument missing\n");
${indent_plus} s->${snake_a}_str = aa;
${indent_plus} if (aret == '.') {
${indent_plus} int pos;
${indent_plus} char *endptr;
${indent_plus} int last = 0;
${indent_plus} char *dot_pos = strchr(str, '.');
${indent_plus} TRY(!(dot_pos++), "$a argument missing\n");
${indent_plus} pos = strtoul(dot_pos, &endptr, 0);
${indent_plus} TRY(endptr == dot_pos, "$a require an index\n");
${indent_plus} if (s->${snake_a}) {
${indent_plus} for (; s->${snake_a}[last]; ++last);
${indent_plus} }
${indent_plus} if (pos < last) {
${indent_plus} s->${snake_a}[pos] = ${convertor}(aa);
${indent_plus} } else {
${indent_plus} for (int i = last; i < pos; ++i)
${indent_plus} SET_NEXT(s->${snake_a}, $null_val, pa);
${indent_plus} SET_NEXT(s->${snake_a}, ${convertor}(aa), pa);
${indent_plus} }
${indent_plus} } else {
${indent_plus} TRY(!aa, "$a argument missing\n");
${indent_plus} s->${snake_a}_str = aa;
${indent_plus} }
$indent_base } else if (!(aret = strcmp(str, "$a[]")) || aret == '=') {
${indent_plus} TRY(!aa, "$a[] argument missing\n");
${indent_plus} SET_NEXT(s->${snake_a}, ${convertor}(aa), pa);
Expand Down Expand Up @@ -287,7 +311,7 @@ EOF
t=$(get_type2 "$s" "$a")
snake_n=$(to_snakecase <<< $a)

echo " if ((aret = argcmp(str, \"$a\")) == 0 || aret == '=') {"
echo " if ((aret = strcmp(str, \"$a\")) == 0 || aret == '=' || aret == '.') {"
cli_c_type_parser "$a" "$t" " "
done
cat <<EOF
Expand Down Expand Up @@ -361,7 +385,7 @@ EOF
snake_a=$(to_snakecase <<< $a)

cat <<EOF
if ((aret = argcmp(next_a, "$a")) == 0 || aret == '=' ) {
if ((aret = strcmp(next_a, "$a")) == 0 || aret == '=' || aret == '.') {
char *eq_ptr = strchr(next_a, '=');
if (eq_ptr) {
TRY((!*eq_ptr), "$a argument missing\n");
Expand Down
14 changes: 11 additions & 3 deletions main-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,29 @@ struct ptr_array {
} \
} while (0)

/*
* use to be sizeof v, but if I pass "" to SET_NEXT, sizeof v is 1, and I expect
* char *,
* because the array can contain int and double too, I'll gowith allocated the
* biggerst size for all
*/
#define OBJ_SIZE 8

#define SET_NEXT(a,v,pa) do { \
int cnt; \
if (!a) { \
a = calloc(64, sizeof(v)); \
a = calloc(64, OBJ_SIZE); \
if (!a) break; \
if (ptr_array_append(pa, a) < 0) \
break; \
} \
for (cnt = 0; a[cnt]; ++cnt); \
if (cnt && (cnt % 63) == 0) { \
int idx = ptr_array_get_idx(pa, a); \
pa->ptrs[idx] = realloc(a, (cnt + 1 + 64) * sizeof(v)); \
pa->ptrs[idx] = realloc(a, (cnt + 1 + 64) * OBJ_SIZE); \
if (!pa->ptrs[idx]) { free(a); break; } \
a = pa->ptrs[idx]; \
memset(a + cnt + 1, 0, 64 * sizeof(v)); \
memset(a + cnt + 1, 0, 64 * OBJ_SIZE); \
} \
a[cnt] = v; \
} while (0)
Expand Down

0 comments on commit 1a73036

Please sign in to comment.