Implement grid resizing.
This commit is contained in:
parent
9b76c4f9f7
commit
95a7a3ee1a
1 changed files with 46 additions and 23 deletions
|
@ -8,6 +8,7 @@
|
|||
#include <stdlib.h> /* malloc...*/
|
||||
#include <string.h> /* strtok...*/
|
||||
#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
|
||||
* - 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_col<max_cols; cur_col++) {
|
||||
for (cur_row=0; cur_row<max_rows; cur_row++) {
|
||||
idx = cur_col + (cur_row * csv->cols);
|
||||
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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue