Skip to content

Commit

Permalink
util/table: new rz_table_add_vrowf to take va_list and add a new row
Browse files Browse the repository at this point in the history
For cases when we need to directly use arguments of a variadic function
to add a new row to `RzTable`, the new `rz_table_add_vrowf` will take a
`va_list` and a format string to do the same.
  • Loading branch information
brightprogrammer committed Sep 18, 2024
1 parent 1d0f7f4 commit 5bfcbbf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions librz/include/rz_util/rz_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ RZ_API void rz_table_set_columnsf(RzTable *t, const char *fmt, ...);
RZ_API void rz_table_set_vcolumnsf(RzTable *t, const char *fmt, va_list ap);
RZ_API RzTableRow *rz_table_row_new(RzPVector /*<char *>*/ *items);
RZ_API void rz_table_add_row(RZ_NONNULL RzTable *t, const char *name, ...);
RZ_API void rz_table_add_vrowf(RZ_NONNULL RzTable *t, const char *fmt, va_list ap);
RZ_API void rz_table_add_rowf(RzTable *t, const char *fmt, ...);
RZ_API void rz_table_add_row_columnsf(RzTable *t, const char *fmt, ...);
RZ_API void rz_table_add_row_vec(RZ_NONNULL RzTable *t, RZ_NONNULL RzPVector /*<char *>*/ *items);
Expand Down
42 changes: 37 additions & 5 deletions librz/util/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,23 @@ RZ_API void rz_table_set_columnsf(RzTable *t, const char *fmt, ...) {
va_end(ap);
}

/**
* \brief Given a row in \c RzTable, append a new column with given format
* by pulling out value of that type form given va_list
*
* Possible values for format are :
* - s, z : String (const char*)
* - b : Boolean (bool)
* - i, d : Integer (int, int32_t)
* - n : Number (unsigned long long int, uint64_t)
* - u : Size (B, KB, MB, GB, etc...)
* - f : Double
* - x, X : Hex value (x means all smal, X means all CAPS)
*
* \param row \p RzPVector to append the new column into
* \param fmt Character representing the type of value to be stored in last column of given row.
* \param ap Variadic argument list. Will be used to pull out value based on given fmt.
*/
#define add_column_to_rowf(row, fmt, ap) \
do { \
const char *arg = NULL; \
Expand Down Expand Up @@ -318,21 +335,36 @@ RZ_API void rz_table_add_row_columnsf(RzTable *t, const char *fmt, ...) {
}

/**
* Add a new row with the specified columns values.
* \breif Add a new row with given format and variadic list of values.
*
* \param t Table to add new row into
* \param fmt Format string to define column ordering and type of given row.
* \param ap Variadic argument list to pull values for each column in row from.
*
* \sa rz_table_append_column_to_vrowf
*/
RZ_API void rz_table_add_rowf(RzTable *t, const char *fmt, ...) {
RZ_API void rz_table_add_vrowf(RZ_NONNULL RzTable *t, const char *fmt, va_list ap) {
rz_return_if_fail(t && fmt);

va_list ap;
va_start(ap, fmt);
RzPVector *vec = rz_pvector_new(free);
for (const char *f = fmt; *f; f++) {
add_column_to_rowf(vec, *f, ap);
}
va_end(ap);
rz_table_add_row_vec(t, vec);
}

/**
* Add a new row with the specified columns values.
*/
RZ_API void rz_table_add_rowf(RzTable *t, const char *fmt, ...) {
rz_return_if_fail(t && fmt);

va_list ap;
va_start(ap, fmt);
rz_table_add_vrowf(t, fmt, ap);
va_end(ap);
}

RZ_API void rz_table_add_row(RZ_NONNULL RzTable *t, const char *name, ...) {
rz_return_if_fail(t);
va_list ap;
Expand Down

0 comments on commit 5bfcbbf

Please sign in to comment.