-
-
Notifications
You must be signed in to change notification settings - Fork 333
/
worksheet.c
11746 lines (9623 loc) · 349 KB
/
worksheet.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
/*****************************************************************************
* worksheet - A library for creating Excel XLSX worksheet files.
*
* Used in conjunction with the libxlsxwriter library.
*
* SPDX-License-Identifier: BSD-2-Clause
* Copyright 2014-2024, John McNamara, jmcnamara@cpan.org.
*
*/
#ifdef USE_FMEMOPEN
#define _POSIX_C_SOURCE 200809L
#endif
#include "xlsxwriter/xmlwriter.h"
#include "xlsxwriter/worksheet.h"
#include "xlsxwriter/format.h"
#include "xlsxwriter/utility.h"
#ifdef USE_OPENSSL_MD5
#include <openssl/md5.h>
#else
#ifndef USE_NO_MD5
#include "xlsxwriter/third_party/md5.h"
#endif
#endif
#define LXW_STR_MAX 32767
#define LXW_BUFFER_SIZE 4096
#define LXW_PRINT_ACROSS 1
#define LXW_VALIDATION_MAX_TITLE_LENGTH 32
#define LXW_VALIDATION_MAX_STRING_LENGTH 255
#define LXW_THIS_ROW "[#This Row],"
/*
* Forward declarations.
*/
STATIC void _worksheet_write_rows(lxw_worksheet *self);
STATIC int _row_cmp(lxw_row *row1, lxw_row *row2);
STATIC int _cell_cmp(lxw_cell *cell1, lxw_cell *cell2);
STATIC int _drawing_rel_id_cmp(lxw_drawing_rel_id *tuple1,
lxw_drawing_rel_id *tuple2);
STATIC int _cond_format_hash_cmp(lxw_cond_format_hash_element *elem_1,
lxw_cond_format_hash_element *elem_2);
#ifndef __clang_analyzer__
LXW_RB_GENERATE_ROW(lxw_table_rows, lxw_row, tree_pointers, _row_cmp);
LXW_RB_GENERATE_CELL(lxw_table_cells, lxw_cell, tree_pointers, _cell_cmp);
LXW_RB_GENERATE_DRAWING_REL_IDS(lxw_drawing_rel_ids, lxw_drawing_rel_id,
tree_pointers, _drawing_rel_id_cmp);
LXW_RB_GENERATE_VML_DRAWING_REL_IDS(lxw_vml_drawing_rel_ids,
lxw_drawing_rel_id, tree_pointers,
_drawing_rel_id_cmp);
LXW_RB_GENERATE_COND_FORMAT_HASH(lxw_cond_format_hash,
lxw_cond_format_hash_element, tree_pointers,
_cond_format_hash_cmp);
#endif
/*****************************************************************************
*
* Private functions.
*
****************************************************************************/
/*
* Find but don't create a row object for a given row number.
*/
lxw_row *
lxw_worksheet_find_row(lxw_worksheet *self, lxw_row_t row_num)
{
lxw_row tmp_row;
tmp_row.row_num = row_num;
return RB_FIND(lxw_table_rows, self->table, &tmp_row);
}
/*
* Find but don't create a cell object for a given row object and col number.
*/
lxw_cell *
lxw_worksheet_find_cell_in_row(lxw_row *row, lxw_col_t col_num)
{
lxw_cell tmp_cell;
if (!row)
return NULL;
tmp_cell.col_num = col_num;
return RB_FIND(lxw_table_cells, row->cells, &tmp_cell);
}
/*
* Create a new worksheet object.
*/
lxw_worksheet *
lxw_worksheet_new(lxw_worksheet_init_data *init_data)
{
lxw_worksheet *worksheet = calloc(1, sizeof(lxw_worksheet));
GOTO_LABEL_ON_MEM_ERROR(worksheet, mem_error);
worksheet->table = calloc(1, sizeof(struct lxw_table_rows));
GOTO_LABEL_ON_MEM_ERROR(worksheet->table, mem_error);
RB_INIT(worksheet->table);
worksheet->hyperlinks = calloc(1, sizeof(struct lxw_table_rows));
GOTO_LABEL_ON_MEM_ERROR(worksheet->hyperlinks, mem_error);
RB_INIT(worksheet->hyperlinks);
worksheet->comments = calloc(1, sizeof(struct lxw_table_rows));
GOTO_LABEL_ON_MEM_ERROR(worksheet->comments, mem_error);
RB_INIT(worksheet->comments);
/* Initialize the cached rows. */
worksheet->table->cached_row_num = LXW_ROW_MAX + 1;
worksheet->hyperlinks->cached_row_num = LXW_ROW_MAX + 1;
worksheet->comments->cached_row_num = LXW_ROW_MAX + 1;
if (init_data && init_data->optimize) {
worksheet->array = calloc(LXW_COL_MAX, sizeof(struct lxw_cell *));
GOTO_LABEL_ON_MEM_ERROR(worksheet->array, mem_error);
}
worksheet->col_options =
calloc(LXW_COL_META_MAX, sizeof(lxw_col_options *));
worksheet->col_options_max = LXW_COL_META_MAX;
GOTO_LABEL_ON_MEM_ERROR(worksheet->col_options, mem_error);
worksheet->col_formats = calloc(LXW_COL_META_MAX, sizeof(lxw_format *));
worksheet->col_formats_max = LXW_COL_META_MAX;
GOTO_LABEL_ON_MEM_ERROR(worksheet->col_formats, mem_error);
worksheet->optimize_row = calloc(1, sizeof(struct lxw_row));
GOTO_LABEL_ON_MEM_ERROR(worksheet->optimize_row, mem_error);
worksheet->optimize_row->height = LXW_DEF_ROW_HEIGHT;
worksheet->merged_ranges = calloc(1, sizeof(struct lxw_merged_ranges));
GOTO_LABEL_ON_MEM_ERROR(worksheet->merged_ranges, mem_error);
STAILQ_INIT(worksheet->merged_ranges);
worksheet->image_props = calloc(1, sizeof(struct lxw_image_props));
GOTO_LABEL_ON_MEM_ERROR(worksheet->image_props, mem_error);
STAILQ_INIT(worksheet->image_props);
worksheet->embedded_image_props =
calloc(1, sizeof(struct lxw_embedded_image_props));
GOTO_LABEL_ON_MEM_ERROR(worksheet->embedded_image_props, mem_error);
STAILQ_INIT(worksheet->embedded_image_props);
worksheet->chart_data = calloc(1, sizeof(struct lxw_chart_props));
GOTO_LABEL_ON_MEM_ERROR(worksheet->chart_data, mem_error);
STAILQ_INIT(worksheet->chart_data);
worksheet->comment_objs = calloc(1, sizeof(struct lxw_comment_objs));
GOTO_LABEL_ON_MEM_ERROR(worksheet->comment_objs, mem_error);
STAILQ_INIT(worksheet->comment_objs);
worksheet->header_image_objs = calloc(1, sizeof(struct lxw_comment_objs));
GOTO_LABEL_ON_MEM_ERROR(worksheet->header_image_objs, mem_error);
STAILQ_INIT(worksheet->header_image_objs);
worksheet->button_objs = calloc(1, sizeof(struct lxw_comment_objs));
GOTO_LABEL_ON_MEM_ERROR(worksheet->button_objs, mem_error);
STAILQ_INIT(worksheet->button_objs);
worksheet->selections = calloc(1, sizeof(struct lxw_selections));
GOTO_LABEL_ON_MEM_ERROR(worksheet->selections, mem_error);
STAILQ_INIT(worksheet->selections);
worksheet->data_validations =
calloc(1, sizeof(struct lxw_data_validations));
GOTO_LABEL_ON_MEM_ERROR(worksheet->data_validations, mem_error);
STAILQ_INIT(worksheet->data_validations);
worksheet->table_objs = calloc(1, sizeof(struct lxw_table_objs));
GOTO_LABEL_ON_MEM_ERROR(worksheet->table_objs, mem_error);
STAILQ_INIT(worksheet->table_objs);
worksheet->external_hyperlinks = calloc(1, sizeof(struct lxw_rel_tuples));
GOTO_LABEL_ON_MEM_ERROR(worksheet->external_hyperlinks, mem_error);
STAILQ_INIT(worksheet->external_hyperlinks);
worksheet->external_drawing_links =
calloc(1, sizeof(struct lxw_rel_tuples));
GOTO_LABEL_ON_MEM_ERROR(worksheet->external_drawing_links, mem_error);
STAILQ_INIT(worksheet->external_drawing_links);
worksheet->drawing_links = calloc(1, sizeof(struct lxw_rel_tuples));
GOTO_LABEL_ON_MEM_ERROR(worksheet->drawing_links, mem_error);
STAILQ_INIT(worksheet->drawing_links);
worksheet->vml_drawing_links = calloc(1, sizeof(struct lxw_rel_tuples));
GOTO_LABEL_ON_MEM_ERROR(worksheet->vml_drawing_links, mem_error);
STAILQ_INIT(worksheet->vml_drawing_links);
worksheet->external_table_links =
calloc(1, sizeof(struct lxw_rel_tuples));
GOTO_LABEL_ON_MEM_ERROR(worksheet->external_table_links, mem_error);
STAILQ_INIT(worksheet->external_table_links);
if (init_data && init_data->optimize) {
FILE *tmpfile;
worksheet->optimize_buffer = NULL;
worksheet->optimize_buffer_size = 0;
tmpfile = lxw_get_filehandle(&worksheet->optimize_buffer,
&worksheet->optimize_buffer_size,
init_data->tmpdir);
if (!tmpfile) {
LXW_ERROR("Error creating tmpfile() for worksheet in "
"'constant_memory' mode.");
goto mem_error;
}
worksheet->optimize_tmpfile = tmpfile;
GOTO_LABEL_ON_MEM_ERROR(worksheet->optimize_tmpfile, mem_error);
worksheet->file = worksheet->optimize_tmpfile;
}
worksheet->drawing_rel_ids =
calloc(1, sizeof(struct lxw_drawing_rel_ids));
GOTO_LABEL_ON_MEM_ERROR(worksheet->drawing_rel_ids, mem_error);
RB_INIT(worksheet->drawing_rel_ids);
worksheet->vml_drawing_rel_ids =
calloc(1, sizeof(struct lxw_vml_drawing_rel_ids));
GOTO_LABEL_ON_MEM_ERROR(worksheet->vml_drawing_rel_ids, mem_error);
RB_INIT(worksheet->vml_drawing_rel_ids);
worksheet->conditional_formats =
calloc(1, sizeof(struct lxw_cond_format_hash));
GOTO_LABEL_ON_MEM_ERROR(worksheet->conditional_formats, mem_error);
RB_INIT(worksheet->conditional_formats);
/* Initialize the worksheet dimensions. */
worksheet->dim_rowmax = 0;
worksheet->dim_colmax = 0;
worksheet->dim_rowmin = LXW_ROW_MAX;
worksheet->dim_colmin = LXW_COL_MAX;
worksheet->default_row_height = LXW_DEF_ROW_HEIGHT;
worksheet->default_row_pixels = 20;
worksheet->default_col_pixels = 64;
/* Initialize the page setup properties. */
worksheet->fit_height = 0;
worksheet->fit_width = 0;
worksheet->page_start = 0;
worksheet->print_scale = 100;
worksheet->fit_page = 0;
worksheet->orientation = LXW_TRUE;
worksheet->page_order = 0;
worksheet->page_setup_changed = LXW_FALSE;
worksheet->page_view = LXW_FALSE;
worksheet->paper_size = 0;
worksheet->vertical_dpi = 0;
worksheet->horizontal_dpi = 0;
worksheet->margin_left = 0.7;
worksheet->margin_right = 0.7;
worksheet->margin_top = 0.75;
worksheet->margin_bottom = 0.75;
worksheet->margin_header = 0.3;
worksheet->margin_footer = 0.3;
worksheet->print_gridlines = 0;
worksheet->screen_gridlines = 1;
worksheet->print_options_changed = 0;
worksheet->zoom = 100;
worksheet->zoom_scale_normal = LXW_TRUE;
worksheet->show_zeros = LXW_TRUE;
worksheet->outline_on = LXW_TRUE;
worksheet->outline_style = LXW_TRUE;
worksheet->outline_below = LXW_TRUE;
worksheet->outline_right = LXW_FALSE;
worksheet->tab_color = LXW_COLOR_UNSET;
worksheet->max_url_length = 2079;
worksheet->comment_display_default = LXW_COMMENT_DISPLAY_HIDDEN;
worksheet->header_footer_objs[0] = &worksheet->header_left_object_props;
worksheet->header_footer_objs[1] = &worksheet->header_center_object_props;
worksheet->header_footer_objs[2] = &worksheet->header_right_object_props;
worksheet->header_footer_objs[3] = &worksheet->footer_left_object_props;
worksheet->header_footer_objs[4] = &worksheet->footer_center_object_props;
worksheet->header_footer_objs[5] = &worksheet->footer_right_object_props;
if (init_data) {
worksheet->name = init_data->name;
worksheet->quoted_name = init_data->quoted_name;
worksheet->tmpdir = init_data->tmpdir;
worksheet->index = init_data->index;
worksheet->hidden = init_data->hidden;
worksheet->sst = init_data->sst;
worksheet->optimize = init_data->optimize;
worksheet->active_sheet = init_data->active_sheet;
worksheet->first_sheet = init_data->first_sheet;
worksheet->default_url_format = init_data->default_url_format;
worksheet->max_url_length = init_data->max_url_length;
}
return worksheet;
mem_error:
lxw_worksheet_free(worksheet);
return NULL;
}
/*
* Free vml object.
*/
STATIC void
_free_vml_object(lxw_vml_obj *vml_obj)
{
if (!vml_obj)
return;
free(vml_obj->author);
free(vml_obj->font_name);
free(vml_obj->text);
free(vml_obj->image_position);
free(vml_obj->name);
free(vml_obj->macro);
free(vml_obj);
}
/*
* Free autofilter rule object.
*/
STATIC void
_free_filter_rule(lxw_filter_rule_obj *rule_obj)
{
uint16_t i;
if (!rule_obj)
return;
free(rule_obj->value1_string);
free(rule_obj->value2_string);
if (rule_obj->list) {
for (i = 0; i < rule_obj->num_list_filters; i++)
free(rule_obj->list[i]);
free(rule_obj->list);
}
free(rule_obj);
}
/*
* Free autofilter rules.
*/
STATIC void
_free_filter_rules(lxw_worksheet *worksheet)
{
uint16_t i;
if (!worksheet->filter_rules)
return;
for (i = 0; i < worksheet->num_filter_rules; i++)
_free_filter_rule(worksheet->filter_rules[i]);
free(worksheet->filter_rules);
}
/*
* Free a worksheet cell.
*/
STATIC void
_free_cell(lxw_cell *cell)
{
if (!cell)
return;
if (cell->type != NUMBER_CELL && cell->type != STRING_CELL
&& cell->type != BLANK_CELL && cell->type != BOOLEAN_CELL
&& cell->type != ERROR_CELL) {
free((void *) cell->u.string);
}
free(cell->user_data1);
free(cell->user_data2);
_free_vml_object(cell->comment);
free(cell);
}
/*
* Free a worksheet row.
*/
STATIC void
_free_row(lxw_row *row)
{
lxw_cell *cell;
lxw_cell *next_cell;
if (!row)
return;
for (cell = RB_MIN(lxw_table_cells, row->cells); cell; cell = next_cell) {
next_cell = RB_NEXT(lxw_table_cells, row->cells, cell);
RB_REMOVE(lxw_table_cells, row->cells, cell);
_free_cell(cell);
}
free(row->cells);
free(row);
}
/*
* Free a worksheet image_options.
*/
STATIC void
_free_object_properties(lxw_object_properties *object_property)
{
if (!object_property)
return;
free(object_property->filename);
free(object_property->description);
free(object_property->extension);
free(object_property->url);
free(object_property->tip);
free(object_property->image_buffer);
free(object_property->md5);
free(object_property->image_position);
free(object_property);
object_property = NULL;
}
/*
* Free a worksheet data_validation.
*/
STATIC void
_free_data_validation(lxw_data_val_obj *data_validation)
{
if (!data_validation)
return;
free(data_validation->value_formula);
free(data_validation->maximum_formula);
free(data_validation->input_title);
free(data_validation->input_message);
free(data_validation->error_title);
free(data_validation->error_message);
free(data_validation->minimum_formula);
free(data_validation);
}
/*
* Free a worksheet conditional format obj.
*/
STATIC void
_free_cond_format(lxw_cond_format_obj *cond_format)
{
if (!cond_format)
return;
free(cond_format->min_value_string);
free(cond_format->mid_value_string);
free(cond_format->max_value_string);
free(cond_format->type_string);
free(cond_format->guid);
free(cond_format);
}
/*
* Free a relationship structure.
*/
STATIC void
_free_relationship(lxw_rel_tuple *relationship)
{
if (!relationship)
return;
free(relationship->type);
free(relationship->target);
free(relationship->target_mode);
free(relationship);
}
/*
* Free a worksheet table column object.
*/
STATIC void
_free_worksheet_table_column(lxw_table_column *column)
{
if (!column)
return;
free((void *) column->header);
free((void *) column->formula);
free((void *) column->total_string);
free(column);
}
/*
* Free a worksheet table object.
*/
STATIC void
_free_worksheet_table(lxw_table_obj *table)
{
uint16_t i;
if (!table)
return;
for (i = 0; i < table->num_cols; i++)
_free_worksheet_table_column(table->columns[i]);
free(table->name);
free(table->total_string);
free(table->columns);
free(table);
}
/*
* Free a worksheet object.
*/
void
lxw_worksheet_free(lxw_worksheet *worksheet)
{
lxw_row *row;
lxw_row *next_row;
lxw_col_t col;
lxw_merged_range *merged_range;
lxw_object_properties *object_props;
lxw_vml_obj *vml_obj;
lxw_selection *selection;
lxw_data_val_obj *data_validation;
lxw_rel_tuple *relationship;
lxw_cond_format_obj *cond_format;
lxw_table_obj *table_obj;
struct lxw_drawing_rel_id *drawing_rel_id;
struct lxw_drawing_rel_id *next_drawing_rel_id;
struct lxw_cond_format_hash_element *cond_format_elem;
struct lxw_cond_format_hash_element *next_cond_format_elem;
if (!worksheet)
return;
if (worksheet->col_options) {
for (col = 0; col < worksheet->col_options_max; col++) {
if (worksheet->col_options[col])
free(worksheet->col_options[col]);
}
}
free(worksheet->col_options);
free(worksheet->col_sizes);
free(worksheet->col_formats);
if (worksheet->table) {
for (row = RB_MIN(lxw_table_rows, worksheet->table); row;
row = next_row) {
next_row = RB_NEXT(lxw_table_rows, worksheet->table, row);
RB_REMOVE(lxw_table_rows, worksheet->table, row);
_free_row(row);
}
free(worksheet->table);
}
if (worksheet->hyperlinks) {
for (row = RB_MIN(lxw_table_rows, worksheet->hyperlinks); row;
row = next_row) {
next_row = RB_NEXT(lxw_table_rows, worksheet->hyperlinks, row);
RB_REMOVE(lxw_table_rows, worksheet->hyperlinks, row);
_free_row(row);
}
free(worksheet->hyperlinks);
}
if (worksheet->comments) {
for (row = RB_MIN(lxw_table_rows, worksheet->comments); row;
row = next_row) {
next_row = RB_NEXT(lxw_table_rows, worksheet->comments, row);
RB_REMOVE(lxw_table_rows, worksheet->comments, row);
_free_row(row);
}
free(worksheet->comments);
}
if (worksheet->merged_ranges) {
while (!STAILQ_EMPTY(worksheet->merged_ranges)) {
merged_range = STAILQ_FIRST(worksheet->merged_ranges);
STAILQ_REMOVE_HEAD(worksheet->merged_ranges, list_pointers);
free(merged_range);
}
free(worksheet->merged_ranges);
}
if (worksheet->image_props) {
while (!STAILQ_EMPTY(worksheet->image_props)) {
object_props = STAILQ_FIRST(worksheet->image_props);
STAILQ_REMOVE_HEAD(worksheet->image_props, list_pointers);
_free_object_properties(object_props);
}
free(worksheet->image_props);
}
if (worksheet->embedded_image_props) {
while (!STAILQ_EMPTY(worksheet->embedded_image_props)) {
object_props = STAILQ_FIRST(worksheet->embedded_image_props);
STAILQ_REMOVE_HEAD(worksheet->embedded_image_props,
list_pointers);
_free_object_properties(object_props);
}
free(worksheet->embedded_image_props);
}
if (worksheet->chart_data) {
while (!STAILQ_EMPTY(worksheet->chart_data)) {
object_props = STAILQ_FIRST(worksheet->chart_data);
STAILQ_REMOVE_HEAD(worksheet->chart_data, list_pointers);
_free_object_properties(object_props);
}
free(worksheet->chart_data);
}
/* Just free the list. The list objects are freed from the RB tree. */
free(worksheet->comment_objs);
if (worksheet->header_image_objs) {
while (!STAILQ_EMPTY(worksheet->header_image_objs)) {
vml_obj = STAILQ_FIRST(worksheet->header_image_objs);
STAILQ_REMOVE_HEAD(worksheet->header_image_objs, list_pointers);
_free_vml_object(vml_obj);
}
free(worksheet->header_image_objs);
}
if (worksheet->button_objs) {
while (!STAILQ_EMPTY(worksheet->button_objs)) {
vml_obj = STAILQ_FIRST(worksheet->button_objs);
STAILQ_REMOVE_HEAD(worksheet->button_objs, list_pointers);
_free_vml_object(vml_obj);
}
free(worksheet->button_objs);
}
if (worksheet->selections) {
while (!STAILQ_EMPTY(worksheet->selections)) {
selection = STAILQ_FIRST(worksheet->selections);
STAILQ_REMOVE_HEAD(worksheet->selections, list_pointers);
free(selection);
}
free(worksheet->selections);
}
if (worksheet->table_objs) {
while (!STAILQ_EMPTY(worksheet->table_objs)) {
table_obj = STAILQ_FIRST(worksheet->table_objs);
STAILQ_REMOVE_HEAD(worksheet->table_objs, list_pointers);
_free_worksheet_table(table_obj);
}
free(worksheet->table_objs);
}
if (worksheet->data_validations) {
while (!STAILQ_EMPTY(worksheet->data_validations)) {
data_validation = STAILQ_FIRST(worksheet->data_validations);
STAILQ_REMOVE_HEAD(worksheet->data_validations, list_pointers);
_free_data_validation(data_validation);
}
free(worksheet->data_validations);
}
while (!STAILQ_EMPTY(worksheet->external_hyperlinks)) {
relationship = STAILQ_FIRST(worksheet->external_hyperlinks);
STAILQ_REMOVE_HEAD(worksheet->external_hyperlinks, list_pointers);
_free_relationship(relationship);
}
free(worksheet->external_hyperlinks);
while (!STAILQ_EMPTY(worksheet->external_drawing_links)) {
relationship = STAILQ_FIRST(worksheet->external_drawing_links);
STAILQ_REMOVE_HEAD(worksheet->external_drawing_links, list_pointers);
_free_relationship(relationship);
}
free(worksheet->external_drawing_links);
while (!STAILQ_EMPTY(worksheet->drawing_links)) {
relationship = STAILQ_FIRST(worksheet->drawing_links);
STAILQ_REMOVE_HEAD(worksheet->drawing_links, list_pointers);
_free_relationship(relationship);
}
free(worksheet->drawing_links);
while (!STAILQ_EMPTY(worksheet->vml_drawing_links)) {
relationship = STAILQ_FIRST(worksheet->vml_drawing_links);
STAILQ_REMOVE_HEAD(worksheet->vml_drawing_links, list_pointers);
_free_relationship(relationship);
}
free(worksheet->vml_drawing_links);
while (!STAILQ_EMPTY(worksheet->external_table_links)) {
relationship = STAILQ_FIRST(worksheet->external_table_links);
STAILQ_REMOVE_HEAD(worksheet->external_table_links, list_pointers);
_free_relationship(relationship);
}
free(worksheet->external_table_links);
if (worksheet->drawing_rel_ids) {
for (drawing_rel_id =
RB_MIN(lxw_drawing_rel_ids, worksheet->drawing_rel_ids);
drawing_rel_id; drawing_rel_id = next_drawing_rel_id) {
next_drawing_rel_id =
RB_NEXT(lxw_drawing_rel_ids, worksheet->drawing_rel_id,
drawing_rel_id);
RB_REMOVE(lxw_drawing_rel_ids, worksheet->drawing_rel_ids,
drawing_rel_id);
free(drawing_rel_id->target);
free(drawing_rel_id);
}
free(worksheet->drawing_rel_ids);
}
if (worksheet->vml_drawing_rel_ids) {
for (drawing_rel_id =
RB_MIN(lxw_vml_drawing_rel_ids, worksheet->vml_drawing_rel_ids);
drawing_rel_id; drawing_rel_id = next_drawing_rel_id) {
next_drawing_rel_id =
RB_NEXT(lxw_vml_drawing_rel_ids, worksheet->drawing_rel_id,
drawing_rel_id);
RB_REMOVE(lxw_vml_drawing_rel_ids, worksheet->vml_drawing_rel_ids,
drawing_rel_id);
free(drawing_rel_id->target);
free(drawing_rel_id);
}
free(worksheet->vml_drawing_rel_ids);
}
if (worksheet->conditional_formats) {
for (cond_format_elem =
RB_MIN(lxw_cond_format_hash, worksheet->conditional_formats);
cond_format_elem; cond_format_elem = next_cond_format_elem) {
next_cond_format_elem = RB_NEXT(lxw_cond_format_hash,
worksheet->conditional_formats,
cond_format_elem);
RB_REMOVE(lxw_cond_format_hash,
worksheet->conditional_formats, cond_format_elem);
while (!STAILQ_EMPTY(cond_format_elem->cond_formats)) {
cond_format = STAILQ_FIRST(cond_format_elem->cond_formats);
STAILQ_REMOVE_HEAD(cond_format_elem->cond_formats,
list_pointers);
_free_cond_format(cond_format);
}
free(cond_format_elem->cond_formats);
free(cond_format_elem);
}
free(worksheet->conditional_formats);
}
_free_relationship(worksheet->external_vml_comment_link);
_free_relationship(worksheet->external_comment_link);
_free_relationship(worksheet->external_vml_header_link);
_free_relationship(worksheet->external_background_link);
_free_filter_rules(worksheet);
if (worksheet->array) {
for (col = 0; col < LXW_COL_MAX; col++) {
_free_cell(worksheet->array[col]);
}
free(worksheet->array);
}
if (worksheet->optimize_row)
free(worksheet->optimize_row);
if (worksheet->drawing)
lxw_drawing_free(worksheet->drawing);
free(worksheet->hbreaks);
free(worksheet->vbreaks);
free((void *) worksheet->name);
free((void *) worksheet->quoted_name);
free(worksheet->vba_codename);
free(worksheet->vml_data_id_str);
free(worksheet->vml_header_id_str);
free(worksheet->comment_author);
free(worksheet->ignore_number_stored_as_text);
free(worksheet->ignore_eval_error);
free(worksheet->ignore_formula_differs);
free(worksheet->ignore_formula_range);
free(worksheet->ignore_formula_unlocked);
free(worksheet->ignore_empty_cell_reference);
free(worksheet->ignore_list_data_validation);
free(worksheet->ignore_calculated_column);
free(worksheet->ignore_two_digit_text_year);
free(worksheet->header);
free(worksheet->footer);
free(worksheet);
worksheet = NULL;
}
/*
* Create a new worksheet row object.
*/
STATIC lxw_row *
_new_row(lxw_row_t row_num)
{
lxw_row *row = calloc(1, sizeof(lxw_row));
if (row) {
row->row_num = row_num;
row->cells = calloc(1, sizeof(struct lxw_table_cells));
row->height = LXW_DEF_ROW_HEIGHT;
if (row->cells)
RB_INIT(row->cells);
else
LXW_MEM_ERROR();
}
else {
LXW_MEM_ERROR();
}
return row;
}
/*
* Create a new worksheet number cell object.
*/
STATIC lxw_cell *
_new_number_cell(lxw_row_t row_num,
lxw_col_t col_num, double value, lxw_format *format)
{
lxw_cell *cell = calloc(1, sizeof(lxw_cell));
RETURN_ON_MEM_ERROR(cell, cell);
cell->row_num = row_num;
cell->col_num = col_num;
cell->type = NUMBER_CELL;
cell->format = format;
cell->u.number = value;
return cell;
}
/*
* Create a new worksheet string cell object.
*/
STATIC lxw_cell *
_new_string_cell(lxw_row_t row_num,
lxw_col_t col_num, int32_t string_id, char *sst_string,
lxw_format *format)
{
lxw_cell *cell = calloc(1, sizeof(lxw_cell));
RETURN_ON_MEM_ERROR(cell, cell);
cell->row_num = row_num;
cell->col_num = col_num;
cell->type = STRING_CELL;
cell->format = format;
cell->u.string_id = string_id;
cell->sst_string = sst_string;
return cell;
}
/*
* Create a new worksheet inline_string cell object.
*/
STATIC lxw_cell *
_new_inline_string_cell(lxw_row_t row_num,
lxw_col_t col_num, char *string, lxw_format *format)
{
lxw_cell *cell = calloc(1, sizeof(lxw_cell));
RETURN_ON_MEM_ERROR(cell, cell);
cell->row_num = row_num;
cell->col_num = col_num;
cell->type = INLINE_STRING_CELL;
cell->format = format;
cell->u.string = string;
return cell;
}
/*
* Create a new worksheet inline_string cell object for rich strings.
*/
STATIC lxw_cell *
_new_inline_rich_string_cell(lxw_row_t row_num,
lxw_col_t col_num, const char *string,
lxw_format *format)
{
lxw_cell *cell = calloc(1, sizeof(lxw_cell));
RETURN_ON_MEM_ERROR(cell, cell);
cell->row_num = row_num;
cell->col_num = col_num;
cell->type = INLINE_RICH_STRING_CELL;
cell->format = format;
cell->u.string = string;
return cell;
}
/*
* Create a new worksheet formula cell object.
*/
STATIC lxw_cell *
_new_formula_cell(lxw_row_t row_num,
lxw_col_t col_num, char *formula, lxw_format *format)
{
lxw_cell *cell = calloc(1, sizeof(lxw_cell));
RETURN_ON_MEM_ERROR(cell, cell);
cell->row_num = row_num;
cell->col_num = col_num;
cell->type = FORMULA_CELL;
cell->format = format;
cell->u.string = formula;
return cell;
}
/*
* Create a new worksheet array formula cell object.
*/
STATIC lxw_cell *
_new_array_formula_cell(lxw_row_t row_num, lxw_col_t col_num, char *formula,
char *range, lxw_format *format, uint8_t is_dynamic)
{
lxw_cell *cell = calloc(1, sizeof(lxw_cell));
RETURN_ON_MEM_ERROR(cell, cell);
cell->row_num = row_num;
cell->col_num = col_num;
cell->format = format;
cell->u.string = formula;
cell->user_data1 = range;
if (is_dynamic)
cell->type = DYNAMIC_ARRAY_FORMULA_CELL;
else
cell->type = ARRAY_FORMULA_CELL;
return cell;
}
/*
* Create a new worksheet blank cell object.
*/
STATIC lxw_cell *
_new_blank_cell(lxw_row_t row_num, lxw_col_t col_num, lxw_format *format)
{
lxw_cell *cell = calloc(1, sizeof(lxw_cell));
RETURN_ON_MEM_ERROR(cell, cell);
cell->row_num = row_num;
cell->col_num = col_num;
cell->type = BLANK_CELL;
cell->format = format;
return cell;
}
/*
* Create a new worksheet boolean cell object.
*/
STATIC lxw_cell *
_new_boolean_cell(lxw_row_t row_num, lxw_col_t col_num, int value,
lxw_format *format)
{
lxw_cell *cell = calloc(1, sizeof(lxw_cell));
RETURN_ON_MEM_ERROR(cell, cell);