Skip to content

Commit 912c205

Browse files
ikegami-tigaw
authored andcommitted
util: add table_init_with_columns() function
This combined table_init() and table_add_columns() functions. Signed-off-by: Tokunori Ikegami <[email protected]>
1 parent 9aef347 commit 912c205

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

util/table.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,32 @@ void table_add_row(struct table *t, int row_id)
211211

212212
struct table *table_init(void)
213213
{
214-
struct table *t;
214+
struct table *t = malloc(sizeof(struct table));
215215

216-
t = malloc(sizeof(struct table));
217216
if (!t)
218217
return NULL;
219218

220219
memset(t, 0, sizeof(struct table));
220+
221221
return t;
222222
}
223223

224+
struct table *table_init_with_columns(struct table_column *c, int num_columns)
225+
{
226+
struct table *t = table_init();
227+
228+
if (!t)
229+
return NULL;
230+
231+
if (table_add_columns(t, c, num_columns)) {
232+
table_free(t);
233+
return NULL;
234+
}
235+
236+
return t;
237+
238+
}
239+
224240
static int table_add_column(struct table *t, struct table_column *c)
225241
{
226242
struct table_column *new_columns;

util/table.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,16 @@ void table_add_row(struct table *t, int row);
146146
void table_print(struct table *t);
147147
void table_free(struct table *t);
148148

149+
/**
150+
* table_init_with_columns() - Allocate a table instance with column definitions
151+
* @c: Column definitions
152+
* @num_columns:Number of columns
153+
*
154+
* This is a function combined table_init() and table_add_columns().
155+
*
156+
* Return: The table instance, or NULL if unsuccessful. If allocated, the caller
157+
* is responsible to free the table.
158+
*/
159+
struct table *table_init_with_columns(struct table_column *c, int num_columns);
160+
149161
#endif /* _TABLE_H_ */

0 commit comments

Comments
 (0)