mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-28 05:00:20 +00:00
[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:
parent
dd633119bb
commit
856848466a
1 changed files with 25 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue