Implement grid resizing.

This commit is contained in:
@@@No user configured@@@ 2016-05-16 10:17:00 +02:00
parent 9b76c4f9f7
commit 95a7a3ee1a

View file

@ -8,6 +8,7 @@
#include <stdlib.h> /* malloc...*/ #include <stdlib.h> /* malloc...*/
#include <string.h> /* strtok...*/ #include <string.h> /* strtok...*/
#include <ctype.h> #include <ctype.h>
#include <errno.h>
/** /**
@ -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 /* Resize CSV table
* - grow columns: on each row, add missing columns cells * - grow columns: on each row, add missing columns cells
* - grow rows: add now rows, with all columns count * - grow rows: add now rows, with all columns count
* - reduce columns: remove columns from right * - reduce columns: remove columns from right
* - reduce lines: remove columns from the end * - reduce lines: remove columns from the end
*/ */
int csv_resize(CSV * csv, unsigned int new_cols, unsigned int new_rows) { int csv_resize(CSV * old_csv, unsigned int new_cols, unsigned int new_rows) {
char ** new_table;
unsigned int cur_col, unsigned int cur_col,
cur_row, cur_row,
idx,
new_idx,
max_cols, max_cols,
max_rows; max_rows;
CSV * new_csv;
char * content;
bool in_old, in_new; bool in_old, in_new;
/* add missing column cells */ /* Build a new (fake) csv */
new_table = malloc(sizeof(char *) * new_cols * new_rows); new_csv = csv_create();
new_csv->rows = new_rows;
new_csv->cols = new_cols;
max_cols = (new_cols > csv->cols)? new_cols : csv->cols; new_csv->table = malloc(sizeof(char *) * new_cols * new_rows);
max_rows = (new_rows > csv->rows)? new_rows : csv->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_col<max_cols; cur_col++) { for (cur_col=0; cur_col<max_cols; cur_col++) {
for (cur_row=0; cur_row<max_rows; cur_row++) { for (cur_row=0; cur_row<max_rows; cur_row++) {
idx = cur_col + (cur_row * csv->cols); in_old = (cur_col < old_csv->cols) && (cur_row < old_csv->rows);
new_idx = cur_col + (cur_row * new_cols); in_new = (cur_col < new_csv->cols) && (cur_row < new_csv->rows);
in_old = (cur_col < csv->cols) && (cur_row < csv->rows);
in_new = (cur_col < new_cols) && (cur_row < new_rows);
if (in_old && in_new) { if (in_old && in_new) {
/* re-link data */ /* 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) { } else if (in_old) {
/* destroy data */ /* destroy data */
free(csv->table[idx]); content = csv_get(old_csv, cur_col, cur_row);
free(content);
} else { } else {
/* set to NULL */ /* skip */
new_table[new_idx] = NULL; /* csv_set(new_csv, cur_col, cur_row, NULL); */
} }
} }
} }
/* on rows */ /* on rows */
free(csv->table); free(old_csv->table);
csv->table = new_table; old_csv->rows = new_rows;
old_csv->cols = new_cols;
old_csv->table = new_csv->table;
return 0; return 0;
error:
printf("Unable to resize CSV table: error %d - %s\n", errno, strerror(errno));
return -1;
} }
/* , char delim='\t' */ /* , char delim='\t' */
@ -155,10 +182,6 @@ int csv_save(CSV * csv, char * filename) {
return 0; return 0;
} }
int csv_set(CSV * csv, unsigned int col, unsigned int row, char * value) {
return 0;
}
int main(int argc, char ** argv) { int main(int argc, char ** argv) {
CSV * csv; CSV * csv;