diff options
author | Salvador Cuñat <salvador.cunat@gmail.com> | 2018-09-05 21:20:29 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-09-06 12:23:51 -0700 |
commit | 856848466af607267d67ee3d44b71beaf20c5bf0 (patch) | |
tree | 1afc6fd2dd0196ba6a6097984e292fe6dc7d50a5 /smtk-import | |
parent | dd633119bb42f0c38c522db2e4bd4eb27e95f8fe (diff) | |
download | subsurface-856848466af607267d67ee3d44b71beaf20c5bf0.tar.gz |
[smtk-import] Add a function to get the size of an array
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 <salvador.cunat@gmail.com>
Diffstat (limited to 'smtk-import')
-rw-r--r-- | smtk-import/smartrak.c | 25 |
1 files changed, 25 insertions, 0 deletions
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 |