-
Notifications
You must be signed in to change notification settings - Fork 0
/
sps.c
1456 lines (1277 loc) · 33.7 KB
/
sps.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/****************************/
/* */
/* Projekt c.2 */
/* Praca s datovymi typmi */
/* Patrik Sehnoutek */
/* ( xsehno01 ) */
/* 6.12.2020 */
/* */
/****************************/
#define MAX 1000
typedef struct
{
char *cellContent;
int length;
int cap;
bool containsDel;
}cell_t;
typedef struct
{
cell_t *columns;
int colCount;
}row_t;
typedef struct
{
row_t *rows;
int rowCount;
int maxCol;
}table_t;
typedef struct
{
char *delimiters;
int n;
int cap;
}delim_t;
typedef struct
{
int row;
int col;
}cellPos_t;
typedef struct
{
char *name;
char *parameters;
}command_t;
typedef struct
{
int length;
command_t *array;
} sequence_t;
typedef struct
{
cellPos_t *cells;
} selection_t;
/**
* cellPos struct constructor
*/
void cell_pos_ctor(cellPos_t *cPos)
{
cPos->row = 0;
cPos->col = 0;
}
/**
* Command struct constructor
*/
void commandCtor(command_t *command)
{
command->name = NULL;
command->parameters = NULL;
}
/**
* Selection struct constructor
*/
void selectionCtor(selection_t *selected)
{
selected->cells = NULL;
}
/**
* Set values to selection struct
*/
void selectionSet(selection_t *selected, int row, int col)
{
if (selected->cells == NULL)
selected->cells = malloc(sizeof(cellPos_t) * 2);
selected->cells[0].row = row;
selected->cells[0].col = col;
selected->cells[1].row = row;
selected->cells[1].col = col;
}
/**
* Delim struct constructor.
*/
void delimCtor(delim_t *del)
{
del->delimiters = NULL;
del->n = 0;
del->cap = 0;
}
/**
* Delim struct inicialization.
*/
int delimInit(delim_t *del)
{
delimCtor(del);
del->delimiters = malloc(sizeof(char) * 2);
if (del->delimiters == NULL)
return -1;
del->delimiters[0] = ' ';
del->n = 1;
del->cap = 2;
return 0;
}
/**
* Delim struct destructor.
*/
void delimDtor(delim_t *del)
{
free(del->delimiters);
del->n = 0;
del->cap = 0;
}
/**
* Check for duplicate delimiters.
*/
bool isDuplicate(delim_t *del, char c)
{
for (int i = 0; i < del->n; i++)
{
if (c == del->delimiters[i])
return true;
}
return false;
}
/**
* Add delimiter to delim struct.
*/
int addDelim(delim_t *del, char c)
{
del->n++;
if (del->n >= del->cap)
{
del->cap = (del->cap)*2;
char *tmp = realloc(del->delimiters, del->cap*sizeof(char));
if (tmp == NULL)
return -1;
del->delimiters = tmp;
}
del->delimiters[del->n-1] = c;
return 0;
}
/**
* Check if delim is correct.
*/
int checkDelim(delim_t *del, char *s1, char *s2)
{
if ((strcmp(s1, "-d")) != 0)
return 0;
if (strchr(s2, '\\') != NULL || strchr(s2, '\"'))
{
fprintf(stderr, "Invalid delim. Delim contains special characters.\n");
return -1;
}
del->delimiters[0] = s2[0];
int len = strlen(s2);
for (int i = 1; i < len; i++)
{
bool duplicate = isDuplicate(del, s2[i]);
if (!duplicate)
if (addDelim(del, s2[i]) == -1)
{
fprintf(stderr, "Realloc error.\n");
return -1;
}
}
return 0;
}
/**
* Check if input file exists
*/
int checkInputFile(char *fileName)
{
FILE *file;
file = fopen(fileName, "r");
if (file == NULL)
return -1;
if (fclose(file) == EOF)
return -1;
return 0;
}
/**
* Check if the given char is delimiter.
* return: true/false
*/
bool isDelim(delim_t *del, char c)
{
for (int i = 0; i < del->n; i++)
{
if (c == del->delimiters[i])
return true;
}
return false;
}
/**
* Cell constructor.
*/
void cellCtor(cell_t *cell)
{
cell->cellContent = NULL;
cell->length = 0;
cell->cap = 0;
cell->containsDel = false;
}
/**
* Cell destructor.
*/
void cellDtor(cell_t *cell)
{
free(cell->cellContent);
cellCtor(cell);
}
/**
* Realloc memory for a cell.
*/
int cellRealloc(cell_t *cell, int n)
{
char *tmp = realloc(cell->cellContent, n * sizeof(char));
if (tmp == NULL) return -1;
cell->cellContent = tmp;
cell->cap = n;
return 0;
}
/**
* Set a given string to the cells.
*/
int cellCset(cell_t *cell, char *string, delim_t *del)
{
if (cell->cellContent != NULL)
cellDtor(cell);
if (cellRealloc(cell, 2) == -1) return -1;
int len = strlen(string);
for (int i = 0; i < len; i++)
{
if (isDelim(del, string[i]))
cell->containsDel = true;
if (i == cell->cap-1)
cellRealloc(cell, cell->cap * 2);
cell->cellContent[i] = string[i];
cell->length++;
if(i + 1 == len) cell->cellContent[i+1] = '\0';
}
return 0;
}
/**
* Table inicialization.
*/
int tableInit(table_t *table)
{
table->maxCol = -1;
table->rows = malloc(sizeof(row_t));
if (table->rows == NULL)
return -1;
else
table->rowCount = 1;
table->rows[0].columns = malloc(sizeof(cell_t));
if (table->rows[0].columns == NULL)
return -1;
else
table->rows[0].colCount = 1;
cellCtor(&table->rows[0].columns[0]);
return 0;
}
/**
* Add columns to a row.
*/
int addColumns(table_t *table, int row, int n, int from)
{
if (from == 0)
table->rows[row-1].columns = NULL;
if (table->rows[row-1].colCount < n)
{
cell_t *cell = realloc(table->rows[row-1].columns, n * sizeof(cell_t));
if (cell == NULL) return -1;
table->rows[row-1].columns = cell;
table->rows[row-1].colCount = n;
// Cell constructor
for (int i = from; i < n; i++)
cellCtor(&table->rows[row-1].columns[i]);
}
return 0;
}
/**
* Add an empty row to the table.
*/
int addRow(table_t *table, int row, int n)
{
if (table->rowCount < row)
{
row_t *r = realloc(table->rows, row * sizeof(row_t));
if (r == NULL) return -1;
table->rows = r;
(table->rowCount)++;
table->rows[row-1].colCount = 0;
}
if(addColumns(table, row, n, table->rows[row-1].colCount) == -1) return -1;
return 0;
}
/**
* Realloc memory for the table.
*/
int tableRealloc(table_t *table, int n, int m)
{
for (int i = 1; i <= m; i++)
if(addRow(table, i, n) == -1) return -1;
return 0;
}
/**
* Free memory.
*/
int tableFree(table_t *table)
{
if (table->rows == NULL) return 0;
for (int i = 0; i < table->rowCount; i++)
{
for (int j = 0; j < table->rows[i].colCount; j++)
{
free(table->rows[i].columns[j].cellContent);
}
free(table->rows[i].columns);
}
free(table->rows);
return 0;
}
/**
* Load table from a file to table_t structure.
*/
int loadTable(table_t *table, char *fileName, delim_t *del)
{
FILE *file;
if ((file = fopen(fileName, "r")) == NULL)
{
fprintf(stderr, "%s %s\n", "Cannot open the file: ", fileName);
return -1;
}
int row = 1;
int column = 1;
int pos = 0;
int c = getc(file);
bool escape = false;
bool oneTable = false;
while(c != EOF)
{
// Detect special character
if (c == '\\')
escape = true;
else if (!escape && c == '\"')
oneTable = !oneTable;
// Char is delimiter
else if (isDelim(del, c) && !escape && !oneTable)
{
if (addColumns(table, row, column+1, column) == -1) return -1;
column++;
pos = 0;
}
// The end of the row
else if (c == '\n')
{
if(table->maxCol < column) table->maxCol = column;
pos = 0;
row++;
column = 1;
// Initialize a new row
if(addRow(table, row, column) == -1) return -1;
if (escape) escape = !escape;
}
// Inserting char
else
{
// Realloc memory if there left no memory to insert char
if (pos >= table->rows[row-1].columns[column-1].cap-1)
{
int new_cap = (table->rows[row-1].columns[column-1].cap) * 2;
if (new_cap == 0) new_cap = 2;
cellRealloc(&(table->rows[row-1].columns[column-1]), new_cap);
}
// Insert char to cell
table->rows[row-1].columns[column-1].cellContent[pos] = c;
table->rows[row-1].columns[column-1].length++;
pos++;
if (isDelim(del, c) || c == '\"')
table->rows[row-1].columns[column-1].containsDel = true;
if (escape) escape = !escape;
}
c = getc(file);
if (c == EOF)
{
free(table->rows[row-1].columns);
(table->rowCount)--;
}
}
if (fclose(file) == EOF)
return -1;
if (oneTable)
return -1;
return 0;
}
/**
* Print changed table to the file.
*/
int printTable(table_t *table, delim_t *del, char *fileName)
{
FILE *file;
if ((file = fopen(fileName, "w")) == NULL)
{
fprintf(stderr, "%s %s\n", "Cannot open the file: ", fileName);
return -1;
}
for (int i = 0; i < table->rowCount; i++)
{
for (int j = 0; j < table->rows[i].colCount; j++)
{
int len = table->rows[i].columns[j].length;
// print quotation mark before cell, which contains delimiter
if (table->rows[i].columns[j].containsDel)
fprintf(file, "%c", '\"');
for (int k = 0; k < len; k++)
{
char c = table->rows[i].columns[j].cellContent[k];
// Escape special characters
if (c == '\\' || c == '\"')
fprintf(file, "%c", '\\');
fprintf(file, "%c", c);
}
// print quotation mark after cell, which contains delimiter
if (table->rows[i].columns[j].containsDel)
fprintf(file, "%c", '\"');
if (j != table->rows[i].colCount-1)
fprintf(file, "%c", del->delimiters[0]);
}
fprintf(file, "%s", "\n");
}
if (fclose(file) == EOF)
return -1;
return 0;
}
/**
* Sequence constructor.
*/
void sequenceCtor(sequence_t *cmds)
{
cmds->array = NULL;
cmds->length = 0;
}
/**
* Realloc memory for a sequence of commands.
*/
int sequenceRealloc(sequence_t *cmds, int n)
{
command_t *tmp;
if (cmds->array == NULL)
tmp = malloc(sizeof(command_t));
else
tmp = realloc(cmds->array, n * sizeof(command_t));
if (tmp == NULL) return -1;
cmds->array = tmp;
for (int i = cmds->length; i < n; i++)
commandCtor(&cmds->array[i]);
cmds->length = n;
return 0;
}
/**
* Free memory.
*/
int sequenceFree(sequence_t *cmds)
{
for (int i = 0; i < cmds->length; i++)
{
if (cmds->array[i].name != NULL)
free(cmds->array[i].name);
if (cmds->array[i].parameters != NULL)
free(cmds->array[i].parameters);
}free(cmds->array);
return 0;
}
/**
* Realloc memory for a command.
*/
int commandRealloc(command_t *cmd, int choice, int n)
{
char *tmp;
if (choice == 0)
{
if (cmd->name == NULL)
tmp = malloc(n * sizeof(char));
else
tmp = realloc(cmd->name, n * sizeof(char));
if (tmp == NULL) return -1;
cmd->name = tmp;
}
else if(choice == 1)
{
if (cmd->parameters == NULL)
tmp = malloc(n * sizeof(char));
else
tmp = realloc(cmd->parameters, n * sizeof(char));
if (tmp == NULL) return -1;
cmd->parameters = tmp;
}
return 0;
}
/**
* Change the given string according to the rules.
*/
int editString(char *string)
{
char tmp[MAX+1];
memset(tmp, '\0', MAX+1);
bool escape = false;
bool oneTable = false;
int pos = 0;
int len = strlen(string);
for (int i = 0; i < len; i++)
{
if(string[i] == '\\')
escape = true;
else if (!escape && string[i] == '\"')
oneTable = !oneTable;
else
{
tmp[pos] = string[i];
escape = false;
pos++;
}
}
memset(string, '\0', MAX+1);
strcpy(string, tmp);
if (oneTable) return -1;
return 0;
}
/**
* Convert string containing commands to the sequence of commands.
*/
int convertCommands(char *argv, sequence_t *commands)
{
sequenceCtor(commands);
if (sequenceRealloc(commands, 1) == -1) return -1;
int pos = 0;
int characterCount = 0;
int commandsCount = 1;
char tmp[MAX+1];
memset(tmp, '\0', MAX+1);
int len = strlen(argv);
for (int i = 0; i <= len; i++)
{
if (argv[i] == ' ' && tmp[0] != '[')
{
commandRealloc(&commands->array[commandsCount-1], 0, pos+1);
strcpy(commands->array[commandsCount-1].name, tmp);
memset(tmp, '\0', MAX+1);
pos = 0;
}
else if(argv[i] == ';'|| i == len)
{
if (commands->array[commandsCount-1].name == NULL)
{
commandRealloc(&commands->array[commandsCount-1], 0, 10);
if (tmp[0] == '[')
strcpy(commands->array[commandsCount-1].name, "selection");
else
strcpy(commands->array[commandsCount-1].name, "editing");
}
commandRealloc(&commands->array[commandsCount-1], 1, pos+1);
// change string according to the rules
if (editString(tmp) == -1) return -1;
strcpy(commands->array[commandsCount-1].parameters, tmp);
memset(tmp, '\0', MAX+1);
pos = 0;
if (i != len)
{
commandsCount++;
sequenceRealloc(commands, commandsCount);
}
}
else
{
tmp[pos] = argv[i];
pos++;
}
characterCount++;
if (characterCount == MAX)
{
fprintf(stderr, "Exceeded characters limit in a command/-s. Character limit is %d.\n", MAX);
return -1;
}
if (commandsCount > 1000)
{
fprintf(stderr, "Exceeded number of commands. Limit of commands is %d.\n", MAX);
return -1;
}
}
return 0;
}
/**
* Find the given string in the selected cells. If string is found, change
* the selection.
*/
void findStr(table_t *table, selection_t *selected, command_t *cmd)
{
char tmp[MAX+1];
memset(tmp, '\0', MAX+1);
strncpy(tmp, cmd->parameters+6, strlen(cmd->parameters)-7);
for (int i = selected->cells[0].row; i <= selected->cells[1].row; i++)
{
for (int j = selected->cells[0].col; j <= selected->cells[1].col; j++)
{
if (table->rows[i-1].columns[j-1].cellContent != NULL)
if (strstr(table->rows[i-1].columns[j-1].cellContent, tmp))
{
selectionSet(selected, i, j);
return;
}
}
}
}
/**
* Copy positions of cells.
*/
void changeCells(selection_t *dst, selection_t *from)
{
if (from->cells[0].row != 0)
{
dst->cells[0].row = from->cells[0].row;
dst->cells[0].col = from->cells[0].col;
dst->cells[1].row = from->cells[1].row;
dst->cells[1].col = from->cells[1].col;
}
}
/**
* Find the biggest number in the selected cells. If number is found, change
* the selection.
*/
void findMax(table_t *table, selection_t *selected)
{
double max;
char *endptr;
bool found = false;
selection_t newSelected;
selectionCtor(&newSelected);
for (int i = selected->cells[0].row; i <= selected->cells[1].row; i++)
{
for (int j = selected->cells[0].col; j <= selected->cells[1].col; j++)
{
if (table->rows[i-1].columns[j-1].cellContent == NULL)
continue;
double tmp = strtod(table->rows[i-1].columns[j-1].cellContent, &endptr);
if (*endptr != '\0')
continue;
if (!found)
{
max = tmp;
found = true;
selectionSet(&newSelected, i, j);
}
else if (max < tmp)
{
max = tmp;
selectionSet(&newSelected, i, j);
}
}
}
if (found) changeCells(selected, &newSelected);
free(newSelected.cells);
}
/**
* Find the smallest number in the selected cells. If number is found, change
* the selection.
*/
void findMin(table_t *table, selection_t *selected)
{
double min;
char *endptr;
bool found = false;
selection_t newSelected;
selectionCtor(&newSelected);
for (int i = selected->cells[0].row; i <= selected->cells[1].row; i++)
{
for (int j = selected->cells[0].col; j <= selected->cells[1].col; j++)
{
if (table->rows[i-1].columns[j-1].cellContent == NULL)
continue;
double tmp = strtod(table->rows[i-1].columns[j-1].cellContent, &endptr);
if (*endptr != '\0')
continue;
if (!found)
{
min = tmp;
found = true;
selectionSet(&newSelected, i, j);
}
else if (min > tmp)
{
min = tmp;
selectionSet(&newSelected, i, j);
}
}
}
if (found) changeCells(selected, &newSelected);
free(newSelected.cells);
}
/**
* Change string to the position selected cells.
*/
int changeToCellPos(table_t *table, selection_t *selected, command_t *cmd)
{
char tmp[MAX+1];
int pos = 0;
int commasCount = 0;
bool containsSpecial = false;
memset(tmp, '\0', MAX+1);
selectionSet(selected, 0, 0);
int len = strlen(cmd->parameters);
for (int i = 1; i < len; i++)
{
if (cmd->parameters[i] == ',' || i == len-1)
{
commasCount++;
int number = atoi(tmp);
if (tmp[0] == '-' && pos > 1) return -1;
if (tmp[0] == '_' || tmp[0] == '-')
{
containsSpecial = true;
if (commasCount == 1)
{
number = 1;
selected->cells[1].row = table->rowCount;
}
else if (commasCount == 2)
{
number = 1;
selected->cells[1].col = table->maxCol;
}
else if (commasCount == 3) number = table->rowCount;
else number = table->maxCol;
}
if (number < 1)
return -1;
if (commasCount == 1) selected->cells[0].row = number;
else if (commasCount == 2) selected->cells[0].col = number;
else if (commasCount == 3) selected->cells[1].row = number;
else if (commasCount == 4) selected->cells[1].col = number;
pos = 0;
memset(tmp, '\0', MAX+1);
}
else
{
tmp[pos] = cmd->parameters[i];
pos++;
}
}
if (commasCount == 2)
{
if (!containsSpecial)
selectionSet(selected, selected->cells[0].row, selected->cells[0].col);
if (selected->cells[1].row == 0)
selected->cells[1].row = selected->cells[0].row;
if (selected->cells[1].col == 0)
selected->cells[1].col = selected->cells[0].col;
}
if (selected->cells[0].row > selected->cells[1].row ||
selected->cells[0].col > selected->cells[1].col)
return -1;
return 0;
}
/**
* Change selection commands.
*/
int changeSelection(table_t *table, selection_t *selected, selection_t *temperory, command_t *cmd)
{
if (strcmp(cmd->parameters, "[max]") == 0) findMax(table, selected);
else if (strcmp(cmd->parameters, "[min]") == 0) findMin(table, selected);
else if (strcmp(cmd->parameters, "[set]") == 0)
changeCells(temperory, selected);
else if (strcmp(cmd->parameters, "[_]") == 0)
changeCells(selected, temperory);
else if (strstr(cmd->parameters, "[find") != 0)
findStr(table, selected, cmd);
else
if(changeToCellPos(table, selected, cmd) == -1)
{
fprintf(stderr, "Invalid given positions.\n");
return -1;
}
if(selected->cells[1].row > table->rowCount || selected->cells[1].col > table->maxCol) {
tableRealloc(table, selected->cells[1].col, selected->cells[1].row);
table->maxCol = selected->cells[1].col;
tableRealloc(table, table->maxCol, table->rowCount);
}
return 0;
}
/**
* Insert column before(side == 0) or after(side == 1) selected cells.
*/
int insertColumns(table_t *table, selection_t *selected, delim_t *del, int side)
{
if (tableRealloc(table, table->maxCol+1, table->rowCount) == -1) return -1;
table->maxCol++;
int pos;
if (side == 0)
pos = selected->cells[0].col;
else if (side == 1)
pos = selected->cells[1].col+1;
for (int i = table->maxCol-1; i >= pos; i--)
{
for (int j = 0; j < table->rowCount; j++)
{
if (table->rows[j].columns[i-1].cellContent != NULL)
cellCset(&table->rows[j].columns[i], table->rows[j].columns[i-1].cellContent, del);
cellDtor(&table->rows[j].columns[i-1]);
}
}
return 0;
}
/**
* Delele all selected columns.
*/
int deleteColumns(table_t *table, selection_t *selected, delim_t *del)
{
int count = selected->cells[1].col - selected->cells[0].col + 1;
for (int i = selected->cells[0].col; i <= table->maxCol; i++)
{
for (int j = 0; j < table->rowCount; j++)
{
if (i == selected->cells[0].col) table->rows[j].colCount -= count;
if (i <= selected->cells[1].col)
cellDtor(&table->rows[j].columns[i-1]);
if (i+count <= table->maxCol)
{
if (table->rows[j].columns[i-1+count].cellContent != NULL)
{
cellCset(&table->rows[j].columns[i-1], table->rows[j].columns[i-1+count].cellContent, del);
cellDtor(&table->rows[j].columns[i-1+count]);
}
}
if (table->rows[j].colCount == 0)
{
free(table->rows[j].columns);
table->rowCount--;
}
}
}
if(table->rowCount == 0)
free(table->rows);
else
table->rows = realloc(table->rows, (table->rowCount) * sizeof(row_t));
// Change selection if whole table is selected
if (selected->cells[0].col == table->maxCol) selected->cells[0].col -= count;
if (selected->cells[1].col == table->maxCol) selected->cells[1].col -= count;
table->maxCol -= count;
// if selection is beyond table dimensions
if (selected->cells[1].col > table->maxCol)
{
tableRealloc(table, selected->cells[1].col, table->rowCount);
table->maxCol = selected->cells[1].col;
}
return 0;
}
/**
* Insert row above(side == 0) or below(side == 1) selected cells.
*/
int insertRows(table_t *table, selection_t *selected, delim_t *del, int side)
{
if (tableRealloc(table, table->maxCol, table->rowCount+1) == -1) return -1;