1 /*
2     data.c - Part of psiconv, a PSION 5 file formats converter
3     Copyright (c) 1999-2014  Frodo Looijaard <frodo@frodo.looijaard.name>
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 
20 #include "config.h"
21 #include "compat.h"
22 #include <stdlib.h>
23 #include <string.h>
24 #include "data.h"
25 #include "list.h"
26 #include "unicode.h"
27 #include "error.h"
28 
29 #ifdef DMALLOC
30 #include <dmalloc.h>
31 #endif
32 
33 static psiconv_color clone_color(psiconv_color color);
34 static psiconv_font clone_font(psiconv_font font);
35 static psiconv_border clone_border(psiconv_border border);
36 static psiconv_bullet clone_bullet(psiconv_bullet bullet);
37 static psiconv_all_tabs clone_all_tabs(psiconv_all_tabs all_tabs);
38 static void psiconv_free_style_aux(void *style);
39 static void psiconv_free_in_line_layout_aux(void * layout);
40 static void psiconv_free_paragraph_aux(void * paragraph);
41 static void psiconv_free_paint_data_section_aux(void * section);
42 static void psiconv_free_clipart_section_aux(void * section);
43 static void psiconv_free_formula_aux(void *data);
44 static void psiconv_free_sheet_worksheet_aux (void *data);
45 static void psiconv_free_sheet_variable_aux(void * variable);
46 static void psiconv_free_sheet_cell_aux(void *cell);
47 static void psiconv_free_sheet_line_aux(void *line);
48 static void psiconv_free_sheet_worksheet_aux (void *data);
49 
50 static psiconv_word_styles_section psiconv_empty_word_styles_section(void);
51 static psiconv_text_and_layout psiconv_empty_text_and_layout(void);
52 static psiconv_texted_section psiconv_empty_texted_section(void);
53 static psiconv_page_header psiconv_empty_page_header(void);
54 static psiconv_page_layout_section psiconv_empty_page_layout_section(void);
55 static psiconv_word_status_section psiconv_empty_word_status_section(void);
56 static psiconv_word_f psiconv_empty_word_f(void);
57 static psiconv_sheet_status_section psiconv_empty_sheet_status_section(void);
58 static psiconv_formula_list psiconv_empty_formula_list(void);
59 static psiconv_sheet_workbook_section
60                                   psiconv_empty_sheet_workbook_section(void);
61 static psiconv_sheet_f psiconv_empty_sheet_f(void);
62 static psiconv_texted_f psiconv_empty_texted_f(void);
63 static psiconv_paint_data_section psiconv_empty_paint_data_section(void);
64 static psiconv_pictures psiconv_empty_pictures(void);
65 static psiconv_mbm_f psiconv_empty_mbm_f(void);
66 static psiconv_sketch_section psiconv_empty_sketch_section(void);
67 static psiconv_sketch_f psiconv_empty_sketch_f(void);
68 static psiconv_clipart_f psiconv_empty_clipart_f(void);
69 static psiconv_cliparts psiconv_empty_cliparts(void);
70 
71 
72 /* Note: these defaults seem to be hard-coded somewhere outside the
73    files themself. */
psiconv_basic_character_layout(void)74 psiconv_character_layout psiconv_basic_character_layout(void)
75 {
76   /* Make the structures static, to oblige IRIX */
77   static struct psiconv_color_s black =
78   {
79     0x00,                  /* red */
80     0x00,                  /* green */
81     0x00,                  /* blue */
82   };
83   static struct psiconv_color_s white =
84   {
85     0xff,                  /* red */
86     0xff,                  /* green */
87     0xff,                  /* blue */
88   };
89   static psiconv_ucs2 font_times[16] = { 'T','i','m','e','s',' ',
90                                          'N','e','w',' ',
91 					 'R','o','m','a','n',0 };
92   static struct psiconv_font_s font =
93   {
94     font_times,            /* name */
95     3                      /* screenfont */
96   };
97   struct psiconv_character_layout_s cl =
98   {
99     &black,                /* color               */
100     &white,                /* back_color          */
101     10.0,                  /* font_size           */
102     psiconv_bool_false,    /* italic              */
103     psiconv_bool_false,    /* bold                */
104     psiconv_normalscript,  /* super_sub           */
105     psiconv_bool_false,    /* underline           */
106     psiconv_bool_false,    /* strikethrough       */
107     &font,                  /* font                */
108   };
109 
110   return psiconv_clone_character_layout(&cl);
111 }
112 
113 /* Note: these defaults seem to be hard-coded somewhere outside the
114    files themself. */
psiconv_basic_paragraph_layout(void)115 psiconv_paragraph_layout psiconv_basic_paragraph_layout(void)
116 {
117   static psiconv_ucs2 font_times[16] = { 'T','i','m','e','s',' ',
118                                          'N','e','w',' ',
119 					 'R','o','m','a','n',0 };
120 
121   static struct psiconv_font_s font =
122   {
123     font_times,           /* name */
124     2                      /* screenfont */
125   };
126   static struct psiconv_color_s black =
127   {
128     0x00,                  /* red */
129     0x00,                  /* green */
130     0x00,                  /* blue */
131   };
132   static struct psiconv_color_s white =
133   {
134     0xff,                  /* red */
135     0xff,                  /* green */
136     0xff,                  /* blue */
137   };
138   static struct psiconv_border_s no_border =
139   {
140     psiconv_border_none,   /* kind                */
141     1,                     /* thickness           */
142     &black                 /* color               */
143   };
144   static struct psiconv_bullet_s bullet =
145   {
146     psiconv_bool_false,    /* on                  */
147     10.0,                  /* font_size           */
148     0x201d,                /* character           */
149     psiconv_bool_true,     /* indent              */
150     &black,                /* color               */
151     &font,                 /* font                */
152   };
153   static struct psiconv_all_tabs_s tabs =
154   {
155     0.64,                  /* normal              */
156     NULL                   /* kind                */
157   };
158   struct psiconv_paragraph_layout_s pl =
159   {
160     &white,                /* back_color          */
161     0.0,                   /* indent_left         */
162     0.0,                   /* indent_right        */
163     0.0,                   /* indent_first        */
164     psiconv_justify_left,  /* justify_hor         */
165     psiconv_justify_middle,/* justify_ver         */
166     10.0,                  /* linespacing         */
167     psiconv_bool_false,    /* linespacing_exact   */
168     0.0,                   /* space_above         */
169     0.0,                   /* space_below         */
170     psiconv_bool_false,    /* keep_together       */
171     psiconv_bool_false,    /* keep_with_next      */
172     psiconv_bool_false,    /* on_next_page        */
173     psiconv_bool_false,    /* no_widow_protection */
174     psiconv_bool_false,    /* wrap_to_fit_cell    */
175     0.0,                   /* left_margin         */
176     &bullet,               /* bullet              */
177     &no_border,            /* left_border         */
178     &no_border,            /* right_border        */
179     &no_border,            /* top_border          */
180     &no_border,            /* bottom_border       */
181     &tabs,                 /* tabs                */
182   };
183   psiconv_paragraph_layout res;
184 
185   if (!(pl.tabs->extras = psiconv_list_new(sizeof(struct psiconv_tab_s))))
186     return NULL;
187   res = psiconv_clone_paragraph_layout(&pl);
188   psiconv_list_free(pl.tabs->extras);
189   return res;
190 }
191 
clone_color(psiconv_color color)192 psiconv_color clone_color(psiconv_color color)
193 {
194   psiconv_color result;
195   if (!(result = malloc(sizeof(*result))))
196     return NULL;
197   *result = *color;
198   return result;
199 }
200 
clone_font(psiconv_font font)201 psiconv_font clone_font(psiconv_font font)
202 {
203   psiconv_font result;
204   if(!(result = malloc(sizeof(*result))))
205     goto ERROR1;
206   *result = *font;
207   if (!(result->name = psiconv_unicode_strdup(result->name)))
208     goto ERROR2;
209   return result;
210 ERROR2:
211   free(result);
212 ERROR1:
213   return NULL;
214 }
215 
clone_border(psiconv_border border)216 psiconv_border clone_border(psiconv_border border)
217 {
218   psiconv_border result;
219   if (!(result = malloc(sizeof(*result))))
220     goto ERROR1;
221   *result = *border;
222   if(!(result->color = clone_color(result->color)))
223     goto ERROR2;
224   return result;
225 ERROR2:
226   free(result);
227 ERROR1:
228   return NULL;
229 }
230 
clone_bullet(psiconv_bullet bullet)231 psiconv_bullet clone_bullet(psiconv_bullet bullet)
232 {
233   psiconv_bullet result;
234   if (!(result = malloc(sizeof(*result))))
235     goto ERROR1;
236   *result = *bullet;
237   if (!(result->font = clone_font(result->font)))
238     goto ERROR2;
239   if (!(result->color = clone_color(result->color)))
240     goto ERROR3;
241   return result;
242 ERROR3:
243   psiconv_free_font(result->font);
244 ERROR2:
245   free(result);
246 ERROR1:
247   return NULL;
248 }
249 
clone_all_tabs(psiconv_all_tabs all_tabs)250 psiconv_all_tabs clone_all_tabs(psiconv_all_tabs all_tabs)
251 {
252   psiconv_all_tabs result;
253   if (!(result = malloc(sizeof(*result))))
254     goto ERROR1;
255   *result = *all_tabs;
256   if (!(result->extras = psiconv_list_clone(result->extras)))
257     goto ERROR2;
258   return result;
259 ERROR2:
260   free(result);
261 ERROR1:
262   return NULL;
263 }
264 
psiconv_clone_character_layout(psiconv_character_layout ls)265 psiconv_character_layout psiconv_clone_character_layout
266                                            (psiconv_character_layout ls)
267 {
268   psiconv_character_layout result;
269 
270   if (!(result = malloc(sizeof(*result))))
271     goto ERROR1;
272   *result = *ls;
273   if (!(result->color = clone_color(result->color)))
274     goto ERROR2;
275   if (!(result->back_color = clone_color(result->back_color)))
276     goto ERROR3;
277   if (!(result->font = clone_font(result->font)))
278     goto ERROR4;
279   return result;
280 ERROR4:
281   psiconv_free_color(result->back_color);
282 ERROR3:
283   psiconv_free_color(result->color);
284 ERROR2:
285   free(result);
286 ERROR1:
287   return NULL;
288 }
289 
psiconv_clone_paragraph_layout(psiconv_paragraph_layout ls)290 psiconv_paragraph_layout psiconv_clone_paragraph_layout
291                                           (psiconv_paragraph_layout ls)
292 {
293   psiconv_paragraph_layout result;
294 
295   if (!(result = malloc(sizeof(*result))))
296     goto ERROR1;
297   *result = *ls;
298   if (!(result->back_color = clone_color(result->back_color)))
299     goto ERROR2;
300   if (!(result->bullet = clone_bullet(result->bullet)))
301     goto ERROR3;
302   if (!(result->left_border = clone_border(result->left_border)))
303     goto ERROR4;
304   if (!(result->right_border = clone_border(result->right_border)))
305     goto ERROR5;
306   if (!(result->top_border = clone_border(result->top_border)))
307     goto ERROR6;
308   if (!(result->bottom_border = clone_border(result->bottom_border)))
309     goto ERROR7;
310   if (!(result->tabs = clone_all_tabs(result->tabs)))
311     goto ERROR8;
312   return result;
313 ERROR8:
314   psiconv_free_border(result->bottom_border);
315 ERROR7:
316   psiconv_free_border(result->top_border);
317 ERROR6:
318   psiconv_free_border(result->right_border);
319 ERROR5:
320   psiconv_free_border(result->left_border);
321 ERROR4:
322   psiconv_free_bullet(result->bullet);
323 ERROR3:
324   psiconv_free_color(result->back_color);
325 ERROR2:
326   free(result);
327 ERROR1:
328   return NULL;
329 }
330 
psiconv_get_style(psiconv_word_styles_section ss,int nr)331 psiconv_word_style psiconv_get_style (psiconv_word_styles_section ss, int nr)
332 {
333   if (nr == 0)
334     return ss->normal;
335   else
336     return psiconv_list_get(ss->styles,0xff - nr);
337 }
338 
psiconv_find_style(const psiconv_word_styles_section ss,const psiconv_ucs2 * name,int * nr)339 int psiconv_find_style(const psiconv_word_styles_section ss,
340                        const psiconv_ucs2 *name,
341                        int *nr)
342 {
343   const psiconv_ucs2 value_normal[] = { 'N','o','r','m','a','l',0 };
344   psiconv_word_style style;
345   int i;
346 
347   if (!nr)
348     return PSICONV_E_OTHER;
349   if(!psiconv_unicode_strcmp(value_normal,name)) {
350     *nr = 0;
351     return 0;
352   }
353   for (i = 0; i < psiconv_list_length(ss->styles);i++) {
354     if (!(style = psiconv_list_get(ss->styles,i)))
355       return PSICONV_E_NOMEM;
356     if (!psiconv_unicode_strcmp(style->name,name)) {
357       *nr = 0xff - i;
358       return 0;
359     }
360   }
361   *nr = 0;
362   return PSICONV_E_OTHER;
363 }
364 
365 
psiconv_get_formula(psiconv_formula_list ss,int nr)366 psiconv_formula psiconv_get_formula (psiconv_formula_list ss, int nr)
367 {
368   return psiconv_list_get(ss,psiconv_list_length(ss)-nr-1);
369 }
370 
371 /* TODO: What if a cell is both in a default row and a default column?!? */
psiconv_get_default_layout(psiconv_sheet_line_list row_defaults,psiconv_sheet_line_list col_defaults,psiconv_sheet_cell_layout cell_default,int row,int col)372 psiconv_sheet_cell_layout psiconv_get_default_layout
373                                        (psiconv_sheet_line_list row_defaults,
374                                         psiconv_sheet_line_list col_defaults,
375                                         psiconv_sheet_cell_layout cell_default,
376                                         int row,int col)
377 {
378   int i;
379   psiconv_sheet_line line;
380   for (i = 0;i < psiconv_list_length(row_defaults);i++) {
381     line = psiconv_list_get(row_defaults,i);
382     if (line->position == row)
383       return line->layout;
384   }
385   for (i = 0;i < psiconv_list_length(col_defaults);i++) {
386     line = psiconv_list_get(col_defaults,i);
387     if (line->position == col)
388       return line->layout;
389   }
390   return cell_default;
391 }
392 
393 
psiconv_free_color(psiconv_color color)394 void psiconv_free_color (psiconv_color color)
395 {
396   if (color)
397     free(color);
398 }
399 
psiconv_free_border(psiconv_border border)400 void psiconv_free_border(psiconv_border border)
401 {
402   if (border) {
403     psiconv_free_color(border->color);
404     free(border);
405   }
406 }
407 
psiconv_free_font(psiconv_font font)408 void psiconv_free_font(psiconv_font font)
409 {
410   if (font) {
411     if (font->name)
412       free(font->name);
413     free(font);
414   }
415 }
416 
psiconv_free_bullet(psiconv_bullet bullet)417 void psiconv_free_bullet(psiconv_bullet bullet)
418 {
419   if (bullet) {
420     psiconv_free_color(bullet->color);
421     psiconv_free_font(bullet->font);
422     free(bullet);
423   }
424 }
425 
psiconv_free_character_layout(psiconv_character_layout layout)426 void psiconv_free_character_layout(psiconv_character_layout layout)
427 {
428   if (layout) {
429     psiconv_free_color(layout->color);
430     psiconv_free_color(layout->back_color);
431     psiconv_free_font(layout->font);
432     free(layout);
433   }
434 }
435 
psiconv_free_tab(psiconv_tab tab)436 void psiconv_free_tab(psiconv_tab tab)
437 {
438   if (tab)
439     free(tab);
440 }
441 
psiconv_free_tabs(psiconv_all_tabs tabs)442 void psiconv_free_tabs(psiconv_all_tabs tabs)
443 {
444   if (tabs) {
445     psiconv_list_free(tabs->extras);
446     free(tabs);
447   }
448 }
449 
psiconv_free_paragraph_layout(psiconv_paragraph_layout layout)450 void psiconv_free_paragraph_layout(psiconv_paragraph_layout layout)
451 {
452   if (layout) {
453     psiconv_free_color(layout->back_color);
454     psiconv_free_bullet(layout->bullet);
455     psiconv_free_border(layout->left_border);
456     psiconv_free_border(layout->right_border);
457     psiconv_free_border(layout->top_border);
458     psiconv_free_border(layout->bottom_border);
459     psiconv_free_tabs(layout->tabs);
460     free(layout);
461   }
462 }
463 
psiconv_free_style_aux(void * style)464 void psiconv_free_style_aux(void *style)
465 {
466   if(((psiconv_word_style) style)->name)
467     free(((psiconv_word_style) style)->name);
468   psiconv_free_character_layout(((psiconv_word_style) style)->character);
469   psiconv_free_paragraph_layout(((psiconv_word_style) style)->paragraph);
470 }
471 
psiconv_free_word_style(psiconv_word_style style)472 void psiconv_free_word_style(psiconv_word_style style)
473 {
474   if (style) {
475     psiconv_free_style_aux(style);
476     free(style);
477   }
478 }
479 
psiconv_free_word_style_list(psiconv_word_style_list style_list)480 void psiconv_free_word_style_list(psiconv_word_style_list style_list)
481 {
482   if (style_list)
483     psiconv_list_free_el(style_list,psiconv_free_style_aux);
484 }
485 
psiconv_free_word_styles_section(psiconv_word_styles_section styles)486 void psiconv_free_word_styles_section(psiconv_word_styles_section styles)
487 {
488   if (styles) {
489     psiconv_free_word_style(styles->normal);
490     psiconv_free_word_style_list(styles->styles);
491     free(styles);
492   }
493 }
494 
psiconv_free_header_section(psiconv_header_section header)495 void psiconv_free_header_section(psiconv_header_section header)
496 {
497   if (header)
498     free(header);
499 }
500 
psiconv_free_section_table_entry(psiconv_section_table_entry entry)501 void psiconv_free_section_table_entry(psiconv_section_table_entry entry)
502 {
503   if (entry)
504     free(entry);
505 }
506 
psiconv_free_section_table_section(psiconv_section_table_section section)507 void psiconv_free_section_table_section(psiconv_section_table_section section)
508 {
509   if (section)
510     psiconv_list_free(section);
511 }
512 
psiconv_free_application_id_section(psiconv_application_id_section section)513 void psiconv_free_application_id_section(psiconv_application_id_section section)
514 {
515   if (section) {
516     if (section->name)
517       free(section->name);
518     free(section);
519   }
520 }
521 
psiconv_free_object_icon_section(psiconv_object_icon_section section)522 void psiconv_free_object_icon_section(psiconv_object_icon_section section)
523 {
524   if (section) {
525     if (section->icon_name)
526       free(section->icon_name);
527     free(section);
528   }
529 }
530 
psiconv_free_object_display_section(psiconv_object_display_section section)531 void psiconv_free_object_display_section(psiconv_object_display_section section)
532 {
533   if (section)
534     free(section);
535 }
536 
psiconv_free_embedded_object_section(psiconv_embedded_object_section object)537 void psiconv_free_embedded_object_section
538                                      (psiconv_embedded_object_section object)
539 {
540   if (object) {
541     psiconv_free_object_icon_section(object->icon);
542     psiconv_free_object_display_section(object->display);
543     psiconv_free_file(object->object);
544     free(object);
545   }
546 }
547 
psiconv_free_in_line_layout_aux(void * layout)548 void psiconv_free_in_line_layout_aux(void * layout)
549 {
550   psiconv_free_character_layout(((psiconv_in_line_layout) layout)->layout);
551   psiconv_free_embedded_object_section
552                                 (((psiconv_in_line_layout) layout)->object);
553 }
554 
psiconv_free_in_line_layout(psiconv_in_line_layout layout)555 void psiconv_free_in_line_layout(psiconv_in_line_layout layout)
556 {
557   if (layout) {
558     psiconv_free_in_line_layout_aux(layout);
559     free(layout);
560   }
561 }
562 
psiconv_free_in_line_layouts(psiconv_in_line_layouts layouts)563 void psiconv_free_in_line_layouts(psiconv_in_line_layouts layouts)
564 {
565   if (layouts)
566     psiconv_list_free_el(layouts,&psiconv_free_in_line_layout_aux);
567 }
568 
psiconv_free_replacement(psiconv_replacement replacement)569 void psiconv_free_replacement(psiconv_replacement replacement)
570 {
571   if (replacement)
572     free(replacement);
573 }
574 
psiconv_free_replacements(psiconv_replacements replacements)575 void psiconv_free_replacements(psiconv_replacements replacements)
576 {
577   if (replacements)
578     psiconv_list_free(replacements);
579 }
580 
psiconv_free_paragraph_aux(void * paragraph)581 void psiconv_free_paragraph_aux(void * paragraph)
582 {
583   if(((psiconv_paragraph) paragraph)->text)
584     free(((psiconv_paragraph) paragraph)->text);
585   psiconv_free_character_layout(((psiconv_paragraph) paragraph)
586                                                        ->base_character);
587   psiconv_free_paragraph_layout(((psiconv_paragraph) paragraph)
588                                                        ->base_paragraph);
589   psiconv_free_in_line_layouts(((psiconv_paragraph) paragraph)
590                                                        ->in_lines);
591   psiconv_free_replacements(((psiconv_paragraph) paragraph)
592                                                        ->replacements);
593 }
594 
psiconv_free_paragraph(psiconv_paragraph paragraph)595 void psiconv_free_paragraph(psiconv_paragraph paragraph)
596 {
597   if (paragraph) {
598     psiconv_free_paragraph_aux(paragraph);
599     free(paragraph);
600   }
601 }
602 
psiconv_free_text_and_layout(psiconv_text_and_layout text)603 void psiconv_free_text_and_layout(psiconv_text_and_layout text)
604 {
605   if (text)
606     psiconv_list_free_el(text,&psiconv_free_paragraph_aux);
607 }
608 
psiconv_free_texted_section(psiconv_texted_section section)609 void psiconv_free_texted_section(psiconv_texted_section section)
610 {
611   if (section) {
612     psiconv_free_text_and_layout(section->paragraphs);
613     free(section);
614   }
615 }
616 
psiconv_free_page_header(psiconv_page_header header)617 void psiconv_free_page_header(psiconv_page_header header)
618 {
619   if (header) {
620     psiconv_free_character_layout(header->base_character_layout);
621     psiconv_free_paragraph_layout(header->base_paragraph_layout);
622     psiconv_free_texted_section(header->text);
623     free(header);
624   }
625 }
626 
psiconv_free_page_layout_section(psiconv_page_layout_section section)627 void psiconv_free_page_layout_section(psiconv_page_layout_section section)
628 {
629   if (section) {
630     psiconv_free_page_header(section->header);
631     psiconv_free_page_header(section->footer);
632     free(section);
633   }
634 }
635 
psiconv_free_word_status_section(psiconv_word_status_section section)636 void psiconv_free_word_status_section(psiconv_word_status_section section)
637 {
638   if (section)
639     free(section);
640 }
641 
psiconv_free_word_f(psiconv_word_f file)642 void psiconv_free_word_f(psiconv_word_f file)
643 {
644   if (file) {
645     psiconv_free_page_layout_section(file->page_sec);
646     psiconv_free_text_and_layout(file->paragraphs);
647     psiconv_free_word_status_section(file->status_sec);
648     psiconv_free_word_styles_section(file->styles_sec);
649     free(file);
650   }
651 }
652 
psiconv_free_sheet_status_section(psiconv_sheet_status_section section)653 void psiconv_free_sheet_status_section(psiconv_sheet_status_section section)
654 {
655   if (section)
656     free(section);
657 }
658 
psiconv_free_sheet_numberformat(psiconv_sheet_numberformat numberformat)659 void psiconv_free_sheet_numberformat(psiconv_sheet_numberformat numberformat)
660 {
661   if (numberformat)
662     free(numberformat);
663 }
664 
psiconv_free_sheet_cell_layout(psiconv_sheet_cell_layout layout)665 void psiconv_free_sheet_cell_layout(psiconv_sheet_cell_layout layout)
666 {
667   psiconv_free_paragraph_layout(layout->paragraph);
668   psiconv_free_character_layout(layout->character);
669   psiconv_free_sheet_numberformat(layout->numberformat);
670 }
671 
psiconv_free_sheet_cell_aux(void * cell)672 void psiconv_free_sheet_cell_aux(void *cell)
673 {
674   psiconv_sheet_cell data = cell;
675 
676   psiconv_free_sheet_cell_layout(data->layout);
677 
678   if ((data->type == psiconv_cell_string) && (data->data.dat_string))
679     free(data->data.dat_string);
680 }
681 
psiconv_free_sheet_cell(psiconv_sheet_cell cell)682 void psiconv_free_sheet_cell(psiconv_sheet_cell cell)
683 {
684   if (cell) {
685     psiconv_free_sheet_cell_aux(cell);
686     free(cell);
687   }
688 }
689 
psiconv_free_sheet_cell_list(psiconv_sheet_cell_list list)690 void psiconv_free_sheet_cell_list(psiconv_sheet_cell_list list)
691 {
692   if (list)
693     psiconv_list_free_el(list,psiconv_free_sheet_cell_aux);
694 }
695 
psiconv_free_sheet_line_aux(void * line)696 void psiconv_free_sheet_line_aux(void *line)
697 {
698   psiconv_sheet_line data = line;
699 
700   psiconv_free_sheet_cell_layout(data->layout);
701 }
702 
psiconv_free_sheet_line(psiconv_sheet_line line)703 void psiconv_free_sheet_line(psiconv_sheet_line line)
704 {
705   if (line) {
706     psiconv_free_sheet_line_aux(line);
707     free(line);
708   }
709 }
710 
711 
psiconv_free_sheet_line_list(psiconv_sheet_line_list list)712 void psiconv_free_sheet_line_list(psiconv_sheet_line_list list)
713 {
714   if (list)
715     psiconv_list_free_el(list,psiconv_free_sheet_line_aux);
716 }
717 
psiconv_free_sheet_grid_break_list(psiconv_sheet_grid_break_list list)718 void psiconv_free_sheet_grid_break_list(psiconv_sheet_grid_break_list list)
719 {
720   if (list)
721     psiconv_list_free(list);
722 }
723 
psiconv_free_sheet_grid_size(psiconv_sheet_grid_size s)724 void psiconv_free_sheet_grid_size(psiconv_sheet_grid_size s)
725 {
726   if (s)
727     free(s);
728 }
729 
psiconv_free_sheet_grid_size_list(psiconv_sheet_grid_size_list list)730 void psiconv_free_sheet_grid_size_list(psiconv_sheet_grid_size_list list)
731 {
732   if (list)
733     psiconv_list_free(list);
734 }
735 
psiconv_free_sheet_grid_section(psiconv_sheet_grid_section sec)736 void psiconv_free_sheet_grid_section(psiconv_sheet_grid_section sec)
737 {
738   if (sec) {
739     psiconv_free_sheet_grid_size_list(sec->row_heights);
740     psiconv_free_sheet_grid_size_list(sec->column_heights);
741     psiconv_free_sheet_grid_break_list(sec->row_page_breaks);
742     psiconv_free_sheet_grid_break_list(sec->column_page_breaks);
743     free(sec);
744   }
745 }
746 
psiconv_free_sheet_worksheet_aux(void * data)747 void psiconv_free_sheet_worksheet_aux (void *data)
748 {
749   psiconv_sheet_worksheet section = data;
750   psiconv_free_sheet_cell_layout(section->default_layout);
751   psiconv_free_sheet_cell_list(section->cells);
752   psiconv_free_sheet_line_list(section->row_default_layouts);
753   psiconv_free_sheet_line_list(section->col_default_layouts);
754   psiconv_free_sheet_grid_section(section->grid);
755 }
756 
psiconv_free_sheet_worksheet(psiconv_sheet_worksheet sheet)757 void psiconv_free_sheet_worksheet(psiconv_sheet_worksheet sheet)
758 {
759   if (sheet) {
760     psiconv_free_sheet_worksheet_aux(sheet);
761     free(sheet);
762   }
763 }
764 
psiconv_free_sheet_worksheet_list(psiconv_sheet_worksheet_list list)765 void psiconv_free_sheet_worksheet_list(psiconv_sheet_worksheet_list list)
766 {
767   if (list)
768     psiconv_list_free_el(list,psiconv_free_sheet_worksheet_aux);
769 }
770 
psiconv_free_formula_aux(void * data)771 void psiconv_free_formula_aux(void *data)
772 {
773   psiconv_formula formula;
774   formula = data;
775   if (formula->type == psiconv_formula_dat_string)
776     free(formula->data.dat_string);
777   else if ((formula->type != psiconv_formula_dat_int) &&
778            (formula->type != psiconv_formula_dat_var) &&
779            (formula->type != psiconv_formula_dat_float) &&
780            (formula->type != psiconv_formula_dat_cellref) &&
781            (formula->type != psiconv_formula_dat_cellblock) &&
782            (formula->type != psiconv_formula_dat_vcellblock) &&
783            (formula->type != psiconv_formula_mark_opsep) &&
784            (formula->type != psiconv_formula_mark_opend) &&
785            (formula->type != psiconv_formula_mark_eof)  &&
786            (formula->type != psiconv_formula_unknown))
787     psiconv_free_formula_list(formula->data.fun_operands);
788 }
789 
psiconv_free_formula(psiconv_formula formula)790 void psiconv_free_formula(psiconv_formula formula)
791 {
792   if (formula) {
793     psiconv_free_formula_aux(formula);
794     free(formula);
795   }
796 }
797 
psiconv_free_formula_list(psiconv_formula_list list)798 void psiconv_free_formula_list(psiconv_formula_list list)
799 {
800   if (list)
801     psiconv_list_free_el(list,psiconv_free_formula_aux);
802 }
803 
psiconv_free_sheet_name_section(psiconv_sheet_name_section section)804 void psiconv_free_sheet_name_section(psiconv_sheet_name_section section)
805 {
806   if (section) {
807     if(section->name)
808       free(section->name);
809     free(section);
810   }
811 }
812 
psiconv_free_sheet_info_section(psiconv_sheet_info_section section)813 void psiconv_free_sheet_info_section(psiconv_sheet_info_section section)
814 {
815   if (section) {
816     free(section);
817   }
818 }
819 
psiconv_free_sheet_variable_aux(void * variable)820 void psiconv_free_sheet_variable_aux(void * variable)
821 {
822   psiconv_sheet_variable var = variable;
823   if (var->name)
824     free(var->name);
825   if (var->type == psiconv_var_string)
826     free(var->data.dat_string);
827 }
828 
psiconv_free_sheet_variable(psiconv_sheet_variable var)829 void psiconv_free_sheet_variable(psiconv_sheet_variable var)
830 {
831   if (var) {
832     psiconv_free_sheet_variable_aux(var);
833     free(var);
834   }
835 }
836 
psiconv_free_sheet_variable_list(psiconv_sheet_variable_list list)837 void psiconv_free_sheet_variable_list(psiconv_sheet_variable_list list)
838 {
839   if (list)
840     psiconv_list_free_el(list,psiconv_free_sheet_variable_aux);
841 }
842 
psiconv_free_sheet_workbook_section(psiconv_sheet_workbook_section section)843 void psiconv_free_sheet_workbook_section(psiconv_sheet_workbook_section section)
844 {
845   if (section) {
846     psiconv_free_formula_list(section->formulas);
847     psiconv_free_sheet_worksheet_list(section->worksheets);
848     psiconv_free_sheet_name_section(section->name);
849     psiconv_free_sheet_info_section(section->info);
850     psiconv_free_sheet_variable_list(section->variables);
851     free(section);
852   }
853 }
854 
psiconv_free_sheet_f(psiconv_sheet_f file)855 void psiconv_free_sheet_f(psiconv_sheet_f file)
856 {
857   if (file) {
858     psiconv_free_page_layout_section(file->page_sec);
859     psiconv_free_sheet_status_section(file->status_sec);
860     psiconv_free_sheet_workbook_section(file->workbook_sec);
861     free(file);
862   }
863 }
864 
psiconv_free_texted_f(psiconv_texted_f file)865 void psiconv_free_texted_f(psiconv_texted_f file)
866 {
867   if (file) {
868     psiconv_free_page_layout_section(file->page_sec);
869     psiconv_free_texted_section(file->texted_sec);
870     free(file);
871   }
872 }
873 
psiconv_free_paint_data_section_aux(void * section)874 void psiconv_free_paint_data_section_aux(void * section)
875 {
876   if (((psiconv_paint_data_section) section)->red)
877     free(((psiconv_paint_data_section)section) -> red);
878   if (((psiconv_paint_data_section) section)->green)
879     free(((psiconv_paint_data_section)section) -> green);
880   if (((psiconv_paint_data_section) section)->blue)
881     free(((psiconv_paint_data_section)section) -> blue);
882 }
883 
psiconv_free_paint_data_section(psiconv_paint_data_section section)884 void psiconv_free_paint_data_section(psiconv_paint_data_section section)
885 {
886   if (section) {
887     psiconv_free_paint_data_section_aux(section);
888     free(section);
889   }
890 }
891 
psiconv_free_pictures(psiconv_pictures section)892 void psiconv_free_pictures(psiconv_pictures section)
893 {
894   if (section)
895     psiconv_list_free_el(section,&psiconv_free_paint_data_section_aux);
896 }
897 
psiconv_free_jumptable_section(psiconv_jumptable_section section)898 void psiconv_free_jumptable_section (psiconv_jumptable_section section)
899 {
900   if (section)
901     psiconv_list_free(section);
902 }
903 
psiconv_free_mbm_f(psiconv_mbm_f file)904 void psiconv_free_mbm_f(psiconv_mbm_f file)
905 {
906   if (file) {
907     psiconv_free_pictures(file->sections);
908     free(file);
909   }
910 }
911 
psiconv_free_sketch_section(psiconv_sketch_section sec)912 void psiconv_free_sketch_section(psiconv_sketch_section sec)
913 {
914   if (sec) {
915     psiconv_free_paint_data_section(sec->picture);
916     free(sec);
917   }
918 }
919 
psiconv_free_sketch_f(psiconv_sketch_f file)920 void psiconv_free_sketch_f(psiconv_sketch_f file)
921 {
922   if (file) {
923     psiconv_free_sketch_section(file->sketch_sec);
924     free(file);
925   }
926 }
927 
psiconv_free_clipart_section_aux(void * section)928 void psiconv_free_clipart_section_aux(void *section)
929 {
930   if (section)
931     psiconv_free_paint_data_section(((psiconv_clipart_section) section)->picture);
932 }
933 
psiconv_free_clipart_section(psiconv_clipart_section section)934 void psiconv_free_clipart_section(psiconv_clipart_section section)
935 {
936   if (section) {
937     psiconv_free_clipart_section_aux(section);
938     free(section);
939   }
940 }
941 
psiconv_free_cliparts(psiconv_cliparts section)942 void psiconv_free_cliparts(psiconv_cliparts section)
943 {
944   if (section)
945     psiconv_list_free_el(section,&psiconv_free_clipart_section_aux);
946 }
947 
psiconv_free_clipart_f(psiconv_clipart_f file)948 void psiconv_free_clipart_f(psiconv_clipart_f file)
949 {
950   if (file) {
951     psiconv_free_cliparts(file->sections);
952     free(file);
953   }
954 }
955 
psiconv_free_file(psiconv_file file)956 void psiconv_free_file(psiconv_file file)
957 {
958   if (file) {
959     if (file->type == psiconv_word_file)
960       psiconv_free_word_f((psiconv_word_f) file->file);
961     else if (file->type == psiconv_texted_file)
962       psiconv_free_texted_f((psiconv_texted_f) file->file);
963     else if (file->type == psiconv_mbm_file)
964       psiconv_free_mbm_f((psiconv_mbm_f) file->file);
965     else if (file->type == psiconv_sketch_file)
966       psiconv_free_sketch_f((psiconv_sketch_f) file->file);
967     else if (file->type == psiconv_clipart_file)
968       psiconv_free_clipart_f((psiconv_clipart_f) file->file);
969     else if (file->type == psiconv_sheet_file)
970       psiconv_free_sheet_f((psiconv_sheet_f) file->file);
971     free(file);
972   }
973 }
974 
psiconv_compare_color(const psiconv_color value1,const psiconv_color value2)975 int psiconv_compare_color(const psiconv_color value1,
976                           const psiconv_color value2)
977 {
978   if (!value1 || !value2)
979     return 1;
980   if ((value1->red == value2->red) &&
981       (value1->green == value2->green) &&
982       (value1->blue == value2->blue))
983     return 0;
984   else
985     return 1;
986 }
987 
psiconv_compare_font(const psiconv_font value1,const psiconv_font value2)988 int psiconv_compare_font(const psiconv_font value1,
989                          const psiconv_font value2)
990 {
991   if (!value1 || !value2 || !value1->name || !value2->name)
992     return 1;
993   if ((value1->screenfont == value2->screenfont) &&
994       !psiconv_unicode_strcmp(value1->name,value2->name))
995     return 0;
996   else
997     return 1;
998 }
999 
psiconv_compare_border(const psiconv_border value1,const psiconv_border value2)1000 int psiconv_compare_border(const psiconv_border value1,
1001                            const psiconv_border value2)
1002 {
1003   if (!value1 || !value2)
1004     return 1;
1005   if ((value1->kind == value2->kind) &&
1006       (value1->thickness == value2->thickness) &&
1007       !psiconv_compare_color(value1->color,value2->color))
1008     return 0;
1009   else
1010     return 1;
1011 }
1012 
psiconv_compare_bullet(const psiconv_bullet value1,const psiconv_bullet value2)1013 int psiconv_compare_bullet(const psiconv_bullet value1,
1014                            const psiconv_bullet value2)
1015 {
1016   if (!value1 || !value2)
1017     return 1;
1018   if ((value1->on == value2->on) &&
1019       (value1->font_size == value2->font_size) &&
1020       (value1->character == value2->character) &&
1021       (value1->indent == value2->indent) &&
1022       !psiconv_compare_color(value1->color,value2->color) &&
1023       !psiconv_compare_font(value1->font,value2->font))
1024     return 0;
1025   else
1026     return 1;
1027 }
1028 
psiconv_compare_tab(const psiconv_tab value1,const psiconv_tab value2)1029 int psiconv_compare_tab(const psiconv_tab value1, const psiconv_tab value2)
1030 {
1031   if (!value1 || !value2)
1032     return 1;
1033   if ((value1->location == value2->location) &&
1034       (value1->kind == value2->kind))
1035     return 0;
1036   else
1037     return 1;
1038 }
1039 
psiconv_compare_all_tabs(const psiconv_all_tabs value1,const psiconv_all_tabs value2)1040 int psiconv_compare_all_tabs(const psiconv_all_tabs value1,
1041                              const psiconv_all_tabs value2)
1042 {
1043   int i;
1044 
1045   if (!value1 || !value2 || !value1->extras || !value2->extras)
1046     return 1;
1047 
1048   if ((value1->normal != value2->normal) ||
1049       psiconv_list_length(value1->extras) !=
1050                                         psiconv_list_length(value2->extras))
1051     return 1;
1052   for (i = 0; i < psiconv_list_length(value1->extras); i++)
1053     if (psiconv_compare_tab(psiconv_list_get(value1->extras,i),
1054                             psiconv_list_get(value2->extras,i)))
1055 
1056       return 1;
1057   return 0;
1058 }
1059 
psiconv_compare_paragraph_layout(const psiconv_paragraph_layout value1,const psiconv_paragraph_layout value2)1060 int psiconv_compare_paragraph_layout(const psiconv_paragraph_layout value1,
1061                                      const psiconv_paragraph_layout value2)
1062 {
1063   if (!value1 || !value2)
1064     return 1;
1065   if ((value1->indent_left == value2->indent_left) &&
1066       (value1->indent_right == value2->indent_right) &&
1067       (value1->indent_first == value2->indent_first) &&
1068       (value1->justify_hor == value2->justify_hor) &&
1069       (value1->justify_ver == value2->justify_ver) &&
1070       (value1->linespacing == value2->linespacing) &&
1071       (value1->space_above == value2->space_above) &&
1072       (value1->space_below == value2->space_below) &&
1073       (value1->keep_together == value2->keep_together) &&
1074       (value1->keep_with_next == value2->keep_with_next) &&
1075       (value1->on_next_page == value2->on_next_page) &&
1076       (value1->no_widow_protection == value2->no_widow_protection) &&
1077       (value1->border_distance == value2->border_distance) &&
1078       !psiconv_compare_color(value1->back_color,value2->back_color) &&
1079       !psiconv_compare_bullet(value1->bullet,value2->bullet) &&
1080       !psiconv_compare_border(value1->left_border,value2->left_border) &&
1081       !psiconv_compare_border(value1->right_border,value2->right_border) &&
1082       !psiconv_compare_border(value1->top_border,value2->top_border) &&
1083       !psiconv_compare_border(value1->bottom_border,value2->bottom_border) &&
1084       !psiconv_compare_all_tabs(value1->tabs,value2->tabs))
1085     return 0;
1086   else
1087     return 1;
1088 }
1089 
1090 
psiconv_compare_character_layout(const psiconv_character_layout value1,const psiconv_character_layout value2)1091 int psiconv_compare_character_layout(const psiconv_character_layout value1,
1092                                      const psiconv_character_layout value2)
1093 {
1094   if (!value1 || !value2)
1095     return 1;
1096   if ((value1->font_size == value2->font_size) &&
1097       (value1->italic == value2->italic) &&
1098       (value1->bold == value2->bold) &&
1099       (value1->super_sub == value2->super_sub) &&
1100       (value1->underline == value2->underline) &&
1101       (value1->strikethrough == value2->strikethrough) &&
1102       !psiconv_compare_color(value1->color,value2->color) &&
1103       !psiconv_compare_color(value1->back_color,value2->back_color) &&
1104       !psiconv_compare_font(value1->font,value2->font))
1105     return 0;
1106   else
1107     return 1;
1108 }
1109 
1110 
1111 
psiconv_empty_word_styles_section(void)1112 psiconv_word_styles_section psiconv_empty_word_styles_section(void)
1113 {
1114   psiconv_word_styles_section result;
1115   if (!(result = malloc(sizeof(*result))))
1116     goto ERROR1;
1117   if (!(result->styles = psiconv_list_new(sizeof(struct psiconv_word_style_s))))
1118     goto ERROR2;
1119   if (!(result->normal = malloc(sizeof(struct psiconv_word_style_s))))
1120     goto ERROR3;
1121   if (!(result->normal->character = psiconv_basic_character_layout()))
1122     goto ERROR4;
1123   if (!(result->normal->paragraph = psiconv_basic_paragraph_layout()))
1124     goto ERROR5;
1125   result->normal->hotkey = 'N';
1126   result->normal->name = NULL;
1127   result->normal->built_in = psiconv_bool_true;
1128   result->normal->outline_level = 0;
1129   return result;
1130 ERROR5:
1131   psiconv_free_character_layout(result->normal->character);
1132 ERROR4:
1133   free(result->normal);
1134 ERROR3:
1135   psiconv_list_free(result->styles);
1136 ERROR2:
1137   free(result);
1138 ERROR1:
1139   return NULL;
1140 }
1141 
psiconv_empty_text_and_layout(void)1142 psiconv_text_and_layout psiconv_empty_text_and_layout(void)
1143 {
1144   return psiconv_list_new(sizeof(struct psiconv_paragraph_s));
1145 }
1146 
psiconv_empty_texted_section(void)1147 psiconv_texted_section psiconv_empty_texted_section(void)
1148 {
1149   psiconv_texted_section result;
1150   if (!(result = malloc(sizeof(*result))))
1151     goto ERROR1;
1152   if (!(result->paragraphs = psiconv_empty_text_and_layout()))
1153     goto ERROR2;
1154   return result;
1155 ERROR2:
1156   free(result);
1157 ERROR1:
1158   return NULL;
1159 }
1160 
psiconv_empty_page_header(void)1161 psiconv_page_header psiconv_empty_page_header(void)
1162 {
1163   psiconv_page_header result;
1164   if (!(result = malloc(sizeof(*result))))
1165     goto ERROR1;
1166   result->on_first_page = psiconv_bool_true;
1167   if (!(result->base_paragraph_layout = psiconv_basic_paragraph_layout()))
1168     goto ERROR2;
1169   if (!(result->base_character_layout = psiconv_basic_character_layout()))
1170     goto ERROR3;
1171   if (!(result->text = psiconv_empty_texted_section()))
1172     goto ERROR4;
1173   return result;
1174 ERROR4:
1175   psiconv_free_character_layout(result->base_character_layout);
1176 ERROR3:
1177   psiconv_free_paragraph_layout(result->base_paragraph_layout);
1178 ERROR2:
1179   free(result);
1180 ERROR1:
1181   return NULL;
1182 }
1183 
psiconv_empty_page_layout_section(void)1184 psiconv_page_layout_section psiconv_empty_page_layout_section(void)
1185 {
1186   psiconv_page_layout_section result;
1187   if (!(result = malloc(sizeof(*result))))
1188     goto ERROR1;
1189   result->first_page_nr = 1;
1190   result->header_dist = result->footer_dist = 1.27;
1191   result->left_margin = result->right_margin = 3.175;
1192   result->top_margin = result->bottom_margin = 2.54;
1193   result->page_width = 21.0;
1194   result->page_height = 29.7;
1195   result->landscape = psiconv_bool_false;
1196   if (!(result->header = psiconv_empty_page_header()))
1197     goto ERROR2;
1198   if (!(result->footer = psiconv_empty_page_header()))
1199     goto ERROR3;
1200   return result;
1201 ERROR3:
1202   psiconv_free_page_header(result->header);
1203 ERROR2:
1204   free(result);
1205 ERROR1:
1206   return NULL;
1207 }
1208 
psiconv_empty_word_status_section(void)1209 psiconv_word_status_section psiconv_empty_word_status_section(void)
1210 {
1211   psiconv_word_status_section result;
1212   if (!(result = malloc(sizeof(*result))))
1213     return NULL;
1214   result->show_tabs = result->show_spaces = result->show_paragraph_ends =
1215         result->show_hard_minus = result->show_hard_space =
1216         result->fit_lines_to_screen = psiconv_bool_false;
1217   result->show_full_pictures = result->show_full_graphs =
1218         result->show_top_toolbar = result->show_side_toolbar =
1219         psiconv_bool_true;
1220   result->cursor_position = 0;
1221   result->display_size = 1000;
1222   return result;
1223 }
1224 
psiconv_empty_word_f(void)1225 psiconv_word_f psiconv_empty_word_f(void)
1226 {
1227   psiconv_word_f result;
1228   if (!(result = malloc(sizeof(*result))))
1229     goto ERROR1;
1230   if (!(result->page_sec = psiconv_empty_page_layout_section()))
1231     goto ERROR2;
1232   if (!(result->paragraphs = psiconv_empty_text_and_layout()))
1233     goto ERROR3;
1234   if (!(result->status_sec = psiconv_empty_word_status_section()))
1235     goto ERROR4;
1236   if (!(result->styles_sec = psiconv_empty_word_styles_section()))
1237     goto ERROR5;
1238   return result;
1239 ERROR5:
1240   psiconv_free_word_status_section(result->status_sec);
1241 ERROR4:
1242   psiconv_free_text_and_layout(result->paragraphs);
1243 ERROR3:
1244   psiconv_free_page_layout_section(result->page_sec);
1245 ERROR2:
1246   free(result);
1247 ERROR1:
1248   return NULL;
1249 }
1250 
psiconv_empty_sheet_status_section(void)1251 psiconv_sheet_status_section psiconv_empty_sheet_status_section(void)
1252 {
1253   psiconv_sheet_status_section result;
1254   if (!(result = malloc(sizeof(*result))))
1255     return NULL;
1256   result->show_horizontal_scrollbar = result->show_vertical_scrollbar =
1257         psiconv_triple_auto;
1258   result->show_graph = psiconv_bool_false;
1259   result->show_top_sheet_toolbar = result->show_side_sheet_toolbar =
1260         result->show_top_graph_toolbar = result->show_side_graph_toolbar =
1261         psiconv_bool_true;
1262   result->cursor_row = result->cursor_column = 0;
1263   result->sheet_display_size = result->graph_display_size = 1000;
1264   return result;
1265 }
1266 
psiconv_empty_formula_list(void)1267 psiconv_formula_list psiconv_empty_formula_list(void)
1268 {
1269   return psiconv_list_new(sizeof(struct psiconv_formula_s));
1270 }
1271 
psiconv_empty_sheet_workbook_section(void)1272 psiconv_sheet_workbook_section psiconv_empty_sheet_workbook_section(void)
1273 {
1274   psiconv_sheet_workbook_section result;
1275   if (!(result = malloc(sizeof(*result))))
1276     goto ERROR1;
1277   if (!(result->formulas = psiconv_empty_formula_list()))
1278     goto ERROR2;
1279   return result;
1280 ERROR2:
1281   free(result);
1282 ERROR1:
1283   return NULL;
1284 }
1285 
1286 
psiconv_empty_sheet_f(void)1287 psiconv_sheet_f psiconv_empty_sheet_f(void)
1288 {
1289   psiconv_sheet_f result;
1290   if (!(result = malloc(sizeof(*result))))
1291     goto ERROR1;
1292   if (!(result->page_sec = psiconv_empty_page_layout_section()))
1293     goto ERROR2;
1294   if (!(result->status_sec = psiconv_empty_sheet_status_section()))
1295     goto ERROR3;
1296   if (!(result->workbook_sec = psiconv_empty_sheet_workbook_section()))
1297     goto ERROR4;
1298   return result;
1299 ERROR4:
1300   psiconv_free_sheet_status_section(result->status_sec);
1301 ERROR3:
1302   psiconv_free_page_layout_section(result->page_sec);
1303 ERROR2:
1304   free(result);
1305 ERROR1:
1306   return NULL;
1307 }
1308 
psiconv_empty_texted_f(void)1309 psiconv_texted_f psiconv_empty_texted_f(void)
1310 {
1311   psiconv_texted_f result;
1312   if (!(result = malloc(sizeof(*result))))
1313     goto ERROR1;
1314   if (!(result->page_sec = psiconv_empty_page_layout_section()))
1315     goto ERROR2;
1316   if (!(result->texted_sec = psiconv_empty_texted_section()))
1317     goto ERROR3;
1318   return result;
1319 ERROR3:
1320   psiconv_free_page_layout_section(result->page_sec);
1321 ERROR2:
1322   free(result);
1323 ERROR1:
1324   return NULL;
1325 }
1326 
psiconv_empty_paint_data_section(void)1327 psiconv_paint_data_section psiconv_empty_paint_data_section(void)
1328 {
1329   psiconv_paint_data_section result;
1330   if (!(result = malloc(sizeof(*result))))
1331     goto ERROR1;
1332   /* Is this correct? */
1333   result->xsize = result->ysize = result->pic_xsize = result->pic_ysize = 0;
1334   /* Probably forbidden... */
1335   if (!(result->red = malloc(0)))
1336     goto ERROR2;
1337   if (!(result->green = malloc(0)))
1338     goto ERROR3;
1339   if (!(result->blue = malloc(0)))
1340     goto ERROR4;
1341   return result;
1342 ERROR4:
1343   free(result->green);
1344 ERROR3:
1345   free(result->red);
1346 ERROR2:
1347   free(result);
1348 ERROR1:
1349   return NULL;
1350 }
1351 
1352 
psiconv_empty_pictures(void)1353 psiconv_pictures psiconv_empty_pictures(void)
1354 {
1355   psiconv_pictures result;
1356   psiconv_paint_data_section pds;
1357   if (!(result = psiconv_list_new(sizeof(struct psiconv_paint_data_section_s))))
1358     goto ERROR1;
1359   if (!(pds = psiconv_empty_paint_data_section()))
1360     goto ERROR2;
1361   if (psiconv_list_add(result,pds))
1362     goto ERROR3;
1363   free(pds);
1364   return result;
1365 ERROR3:
1366   psiconv_free_paint_data_section(pds);
1367 ERROR2:
1368   psiconv_list_free(result);
1369 ERROR1:
1370   return NULL;
1371 }
1372 
psiconv_empty_mbm_f(void)1373 psiconv_mbm_f psiconv_empty_mbm_f(void)
1374 {
1375   psiconv_mbm_f result;
1376   if (!(result = malloc(sizeof(*result))))
1377     goto ERROR1;
1378   if (!(result->sections = psiconv_empty_pictures()))
1379     goto ERROR2;
1380   return result;
1381 ERROR2:
1382   free(result);
1383 ERROR1:
1384   return NULL;
1385 }
1386 
psiconv_empty_sketch_section(void)1387 psiconv_sketch_section psiconv_empty_sketch_section(void)
1388 {
1389   psiconv_sketch_section result;
1390   if (!(result = malloc(sizeof(*result))))
1391     goto ERROR1;
1392   result->displayed_xsize = 320;
1393   result->displayed_ysize = 200;
1394   result->picture_data_x_offset = result->picture_data_y_offset =
1395           result->form_xsize = result->form_ysize =
1396           result->displayed_size_x_offset = result->displayed_size_y_offset = 0;
1397   result->magnification_x = result->magnification_y = 1.0;
1398   result->cut_left = result->cut_right = result->cut_top =
1399           result->cut_bottom = 0.0;
1400   if (!(result->picture = psiconv_empty_paint_data_section()))
1401     goto ERROR2;
1402   return result;
1403 ERROR2:
1404   free(result);
1405 ERROR1:
1406   return NULL;
1407 }
1408 
psiconv_empty_sketch_f(void)1409 psiconv_sketch_f psiconv_empty_sketch_f(void)
1410 {
1411   psiconv_sketch_f result;
1412   if (!(result = malloc(sizeof(*result))))
1413     goto ERROR1;
1414   if (!(result->sketch_sec = psiconv_empty_sketch_section()))
1415     goto ERROR2;
1416   return result;
1417 ERROR2:
1418   free(result);
1419 ERROR1:
1420   return NULL;
1421 }
1422 
psiconv_empty_cliparts(void)1423 psiconv_cliparts psiconv_empty_cliparts(void)
1424 {
1425   /* Is this correct? */
1426   return psiconv_list_new(sizeof(struct psiconv_clipart_section_s));
1427 }
1428 
psiconv_empty_clipart_f(void)1429 psiconv_clipart_f psiconv_empty_clipart_f(void)
1430 {
1431   psiconv_clipart_f result;
1432   if (!(result = malloc(sizeof(*result))))
1433     goto ERROR1;
1434   if (!(result->sections = psiconv_empty_cliparts()))
1435     goto ERROR2;
1436   return result;
1437 ERROR2:
1438   free(result);
1439 ERROR1:
1440   return NULL;
1441 }
1442 
psiconv_empty_file(psiconv_file_type_t type)1443 psiconv_file psiconv_empty_file(psiconv_file_type_t type)
1444 {
1445   psiconv_file result;
1446   if (!(result = malloc(sizeof(*result))))
1447     return NULL;
1448   result->type = type;
1449   if (type == psiconv_word_file) {
1450     if (!(result->file =  psiconv_empty_word_f()))
1451       goto ERROR;
1452   } else if (type == psiconv_sheet_file) {
1453     if (!(result->file =  psiconv_empty_sheet_f()))
1454       goto ERROR;
1455   } else if (type == psiconv_texted_file) {
1456     if (!(result->file =  psiconv_empty_texted_f()))
1457       goto ERROR;
1458   } else if (type == psiconv_mbm_file) {
1459     if (!(result->file =  psiconv_empty_mbm_f()))
1460       goto ERROR;
1461   } else if (type == psiconv_sketch_file) {
1462     if (!(result->file =  psiconv_empty_sketch_f()))
1463       goto ERROR;
1464   } else if (type == psiconv_clipart_file) {
1465     if (!(result->file =  psiconv_empty_clipart_f()))
1466       goto ERROR;
1467   } else
1468     goto ERROR;
1469   return result;
1470 ERROR:
1471   free(result);
1472   return NULL;
1473 }
1474