[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>
This commit is contained in:
Salvador Cuñat 2018-09-05 21:20:29 +02:00 committed by Dirk Hohndel
parent dd633119bb
commit 856848466a

View file

@ -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