From 856848466af607267d67ee3d44b71beaf20c5bf0 Mon Sep 17 00:00:00 2001 From: Salvador Cuñat Date: Wed, 5 Sep 2018 21:20:29 +0200 Subject: [smtk-import] Add a function to get the size of an array MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We can allocate fixed size arrays for smartrak tables as its size can be known in advance. Simply reading table->num_rows is too dangereous as smartrak tables have "holes" commonly. This is, they can look like: Idx | Txt 1 | blablabla 2 | blebleble 4 | blobloblo table->num_rows would give us 3, but we need to allocate 4 to get an array like: |0|blablabla |1|blebleble |2| |3|blobloblo as the idea is to use the table index to reference the array data. Signed-off-by: Salvador Cuñat --- smtk-import/smartrak.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'smtk-import') diff --git a/smtk-import/smartrak.c b/smtk-import/smartrak.c index ed4717287..6d3cf0e6e 100644 --- a/smtk-import/smartrak.c +++ b/smtk-import/smartrak.c @@ -583,6 +583,31 @@ static void smtk_list_free(struct types_list *head) } } +/* Return the number of rows in a given table */ +static int get_rows_num(MdbHandle *mdb, char *table_name) +{ + MdbTableDef *table; + MdbColumn *col[MDB_MAX_COLS]; + char *bound_values[MDB_MAX_COLS]; + int n = 0, i = 0; + + table = smtk_open_table(mdb, table_name, col, bound_values); + if (!table) + return n; + + /* We can get an sparse array (less rows in the table than + * index). Ensure we allocate as many strings as greater + * index, at least */ + while (mdb_fetch_row(table)) { + i = atoi(col[0]->bind_ptr); + if (i > n) + n = i; + } + smtk_free(bound_values, table->num_cols); + mdb_free_tabledef(table); + return n; +} + /* * Build a list from a given table_name (Type, Gear, etc) * Managed tables formats: Just consider Idx and Text -- cgit v1.2.3-70-g09d2