From 95a7a3ee1ad181d4674aeaebcebe39b3dcd8447c Mon Sep 17 00:00:00 2001 From: "@@@No user configured@@@" <@@@No user configured@@@> Date: Mon, 16 May 2016 10:17:00 +0200 Subject: [PATCH] Implement grid resizing. --- src/csv-data-manipulation.c | 69 ++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/src/csv-data-manipulation.c b/src/csv-data-manipulation.c index 822b3a8..e4bfc94 100644 --- a/src/csv-data-manipulation.c +++ b/src/csv-data-manipulation.c @@ -8,6 +8,7 @@ #include /* malloc...*/ #include /* strtok...*/ #include +#include /** @@ -65,52 +66,78 @@ int csv_destroy(CSV * csv) { } +/** + * Get value in CSV table at COL, ROW + */ +char * csv_get(CSV * csv, unsigned int col, unsigned int row) { + return NULL; +} + + +/** + * Set value in CSV table at COL, ROW + */ +int csv_set(CSV * csv, unsigned int col, unsigned int row, char * value) { + return 0; +} + /* Resize CSV table * - grow columns: on each row, add missing columns cells * - grow rows: add now rows, with all columns count * - reduce columns: remove columns from right * - reduce lines: remove columns from the end */ -int csv_resize(CSV * csv, unsigned int new_cols, unsigned int new_rows) { - char ** new_table; +int csv_resize(CSV * old_csv, unsigned int new_cols, unsigned int new_rows) { unsigned int cur_col, cur_row, - idx, - new_idx, max_cols, max_rows; + CSV * new_csv; + char * content; bool in_old, in_new; - /* add missing column cells */ - new_table = malloc(sizeof(char *) * new_cols * new_rows); + /* Build a new (fake) csv */ + new_csv = csv_create(); + new_csv->rows = new_rows; + new_csv->cols = new_cols; - max_cols = (new_cols > csv->cols)? new_cols : csv->cols; - max_rows = (new_rows > csv->rows)? new_rows : csv->rows; + new_csv->table = malloc(sizeof(char *) * new_cols * new_rows); + memset(new_csv->table, 0, sizeof(char *) * new_cols * new_rows); + + if (new_csv->table == NULL) { goto error; } + + max_cols = (new_cols > old_csv->cols)? new_cols : old_csv->cols; + max_rows = (new_rows > old_csv->rows)? new_rows : old_csv->rows; for (cur_col=0; cur_colcols); - new_idx = cur_col + (cur_row * new_cols); - - in_old = (cur_col < csv->cols) && (cur_row < csv->rows); - in_new = (cur_col < new_cols) && (cur_row < new_rows); + in_old = (cur_col < old_csv->cols) && (cur_row < old_csv->rows); + in_new = (cur_col < new_csv->cols) && (cur_row < new_csv->rows); if (in_old && in_new) { /* re-link data */ - new_table[new_idx] = csv->table[idx]; + content = csv_get(old_csv, cur_col, cur_row); + csv_set(new_csv, cur_col, cur_row, content); } else if (in_old) { /* destroy data */ - free(csv->table[idx]); + content = csv_get(old_csv, cur_col, cur_row); + free(content); } else { - /* set to NULL */ - new_table[new_idx] = NULL; + /* skip */ + /* csv_set(new_csv, cur_col, cur_row, NULL); */ } } } /* on rows */ - free(csv->table); - csv->table = new_table; + free(old_csv->table); + old_csv->rows = new_rows; + old_csv->cols = new_cols; + old_csv->table = new_csv->table; return 0; + +error: + printf("Unable to resize CSV table: error %d - %s\n", errno, strerror(errno)); + return -1; } /* , char delim='\t' */ @@ -155,10 +182,6 @@ int csv_save(CSV * csv, char * filename) { return 0; } -int csv_set(CSV * csv, unsigned int col, unsigned int row, char * value) { - return 0; -} - int main(int argc, char ** argv) { CSV * csv;