--- psiconv/trunk/lib/psiconv/data.c 1999/10/11 16:15:14 12 +++ psiconv/trunk/lib/psiconv/data.c 2000/12/25 14:34:17 78 @@ -1,6 +1,6 @@ /* data.c - Part of psiconv, a PSION 5 file formats converter - Copyright (c) 1999 Frodo Looijaard + Copyright (c) 1999, 2000 Frodo Looijaard This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,11 +18,10 @@ */ #include "config.h" +#include "compat.h" #include #include #include "data.h" -#include "list.h" -#include "general.h" static psiconv_color clone_color(psiconv_color color); static psiconv_font clone_font(psiconv_font font); @@ -33,22 +32,31 @@ static void psiconv_free_in_line_layout_aux(void * layout); static void psiconv_free_paragraph_aux(void * paragraph); static void psiconv_free_paint_data_section_aux(void * section); +static void psiconv_free_clipart_section_aux(void * section); +/* Note: these defaults seem to be hard-coded somewhere outside the + files themself. */ psiconv_character_layout psiconv_basic_character_layout(void) { - struct psiconv_color black = + /* Make the structures static, to oblige IRIX */ + static struct psiconv_color_s black = { 0x00, /* red */ 0x00, /* green */ 0x00, /* blue */ }; - struct psiconv_color white = + static struct psiconv_color_s white = { 0xff, /* red */ 0xff, /* green */ 0xff, /* blue */ }; - struct psiconv_character_layout cl = + static struct psiconv_font_s font = + { + "Times New Roman", /* name */ + 3 /* screenfont */ + }; + struct psiconv_character_layout_s cl = { &black, /* color */ &white, /* back_color */ @@ -57,52 +65,41 @@ psiconv_bool_false, /* bold */ psiconv_normalscript, /* super_sub */ psiconv_bool_false, /* underline */ - psiconv_bool_false, /* strike_out */ - NULL, /* font */ + psiconv_bool_false, /* strikethrough */ + &font, /* font */ }; - cl.color = malloc(sizeof(*cl.color)); - cl.color->red = 0x00; - cl.color->green = 0x00; - cl.color->blue = 0x00; - cl.back_color = malloc(sizeof(*cl.color)); - cl.back_color->red = 0xff; - cl.back_color->green = 0xff; - cl.back_color->blue = 0xff; - cl.font = malloc(sizeof(*cl.font)); - cl.font->name = strdup("Times New Roman"); - cl.font->screenfont = 3; return psiconv_clone_character_layout(&cl); } +/* Note: these defaults seem to be hard-coded somewhere outside the + files themself. */ psiconv_paragraph_layout psiconv_basic_paragraph_layout(void) { - char base_font[] = "Times New Roman"; - - struct psiconv_font font = + static struct psiconv_font_s font = { - base_font, /* name */ + "Times New Roman", /* name */ 2 /* screenfont */ }; - struct psiconv_color black = + static struct psiconv_color_s black = { 0x00, /* red */ 0x00, /* green */ 0x00, /* blue */ }; - struct psiconv_color white = + static struct psiconv_color_s white = { 0xff, /* red */ 0xff, /* green */ 0xff, /* blue */ }; - struct psiconv_border no_border = + static struct psiconv_border_s no_border = { psiconv_border_none, /* kind */ 1, /* thickness */ &black /* color */ }; - struct psiconv_bullet bullet = + static struct psiconv_bullet_s bullet = { psiconv_bool_false, /* on */ 10.0, /* font_size */ @@ -111,12 +108,12 @@ &black, /* color */ &font, /* font */ }; - struct psiconv_all_tabs tabs = + static struct psiconv_all_tabs_s tabs = { 0.64, /* normal */ NULL /* kind */ }; - struct psiconv_paragraph_layout pl = + struct psiconv_paragraph_layout_s pl = { &white, /* back_color */ 0.0, /* indent_left */ @@ -124,12 +121,12 @@ 0.0, /* indent_first */ psiconv_justify_left, /* justify_hor */ psiconv_justify_middle,/* justify_ver */ - 0.0, /* interline */ - psiconv_bool_false, /* interline_exact */ - 0.0, /* top_space */ - 0.0, /* bottom_space */ - psiconv_bool_false, /* on_one_page */ - psiconv_bool_false, /* together_with */ + 10.0, /* linespacing */ + psiconv_bool_false, /* linespacing_exact */ + 0.0, /* space_above */ + 0.0, /* space_below */ + psiconv_bool_false, /* keep_together */ + psiconv_bool_false, /* keep_with_next */ psiconv_bool_false, /* on_next_page */ psiconv_bool_false, /* no_widow_protection */ 0.0, /* left_margin */ @@ -142,7 +139,8 @@ }; psiconv_paragraph_layout res; - pl.tabs->extras = psiconv_list_new(sizeof(struct psiconv_tab)); + if (!(pl.tabs->extras = psiconv_list_new(sizeof(struct psiconv_tab_s)))) + return NULL; res = psiconv_clone_paragraph_layout(&pl); psiconv_list_free(pl.tabs->extras); return res; @@ -151,7 +149,8 @@ psiconv_color clone_color(psiconv_color color) { psiconv_color result; - result = malloc(sizeof(*result)); + if (!(result = malloc(sizeof(*result)))) + return NULL; *result = *color; return result; } @@ -159,38 +158,65 @@ psiconv_font clone_font(psiconv_font font) { psiconv_font result; - result = malloc(sizeof(*result)); + if(!(result = malloc(sizeof(*result)))) + goto ERROR1; *result = *font; - result->name = strdup(result->name); + if (!(result->name = strdup(result->name))) + goto ERROR2; return result; +ERROR2: + free(result); +ERROR1: + return NULL; } psiconv_border clone_border(psiconv_border border) { psiconv_border result; - result = malloc(sizeof(*result)); + if (!(result = malloc(sizeof(*result)))) + goto ERROR1; *result = *border; - result->color = clone_color(result->color); + if(!(result->color = clone_color(result->color))) + goto ERROR2; return result; +ERROR2: + free(result); +ERROR1: + return NULL; } psiconv_bullet clone_bullet(psiconv_bullet bullet) { psiconv_bullet result; - result = malloc(sizeof(*result)); + if (!(result = malloc(sizeof(*result)))) + goto ERROR1; *result = *bullet; - result->font = clone_font(result->font); - result->color = clone_color(result->color); + if (!(result->font = clone_font(result->font))) + goto ERROR2; + if (!(result->color = clone_color(result->color))) + goto ERROR3; return result; +ERROR3: + psiconv_free_font(result->font); +ERROR2: + free(result); +ERROR1: + return NULL; } psiconv_all_tabs clone_all_tabs(psiconv_all_tabs all_tabs) { psiconv_all_tabs result; - result = malloc(sizeof(*result)); + if (!(result = malloc(sizeof(*result)))) + goto ERROR1; *result = *all_tabs; - result->extras = psiconv_list_clone(result->extras); + if (!(result->extras = psiconv_list_clone(result->extras))) + goto ERROR2; return result; +ERROR2: + free(result); +ERROR1: + return NULL; } psiconv_character_layout psiconv_clone_character_layout @@ -198,12 +224,24 @@ { psiconv_character_layout result; - result = malloc(sizeof(*result)); + if (!(result = malloc(sizeof(*result)))) + goto ERROR1; *result = *ls; - result->color = clone_color(result->color); - result->back_color = clone_color(result->back_color); - result->font = clone_font(result->font); + if (!(result->color = clone_color(result->color))) + goto ERROR2; + if (!(result->back_color = clone_color(result->back_color))) + goto ERROR3; + if (!(result->font = clone_font(result->font))) + goto ERROR4; return result; +ERROR4: + psiconv_free_color(result->back_color); +ERROR3: + psiconv_free_color(result->color); +ERROR2: + free(result); +ERROR1: + return NULL; } psiconv_paragraph_layout psiconv_clone_paragraph_layout @@ -211,16 +249,40 @@ { psiconv_paragraph_layout result; - result = malloc(sizeof(*result)); + if (!(result = malloc(sizeof(*result)))) + goto ERROR1; *result = *ls; - result->back_color = clone_color(result->back_color); - result->bullet = clone_bullet(result->bullet); - result->left_border = clone_border(result->left_border); - result->right_border = clone_border(result->right_border); - result->top_border = clone_border(result->top_border); - result->bottom_border = clone_border(result->bottom_border); - result->tabs = clone_all_tabs(result->tabs); + if (!(result->back_color = clone_color(result->back_color))) + goto ERROR2; + if (!(result->bullet = clone_bullet(result->bullet))) + goto ERROR3; + if (!(result->left_border = clone_border(result->left_border))) + goto ERROR4; + if (!(result->right_border = clone_border(result->right_border))) + goto ERROR5; + if (!(result->top_border = clone_border(result->top_border))) + goto ERROR6; + if (!(result->bottom_border = clone_border(result->bottom_border))) + goto ERROR7; + if (!(result->tabs = clone_all_tabs(result->tabs))) + goto ERROR8; return result; +ERROR8: + psiconv_free_border(result->bottom_border); +ERROR7: + psiconv_free_border(result->top_border); +ERROR6: + psiconv_free_border(result->right_border); +ERROR5: + psiconv_free_border(result->left_border); +ERROR4: + psiconv_free_bullet(result->bullet); +ERROR3: + psiconv_free_color(result->back_color); +ERROR2: + free(result); +ERROR1: + return NULL; } psiconv_word_style psiconv_get_style (psiconv_word_styles_section ss, int nr) @@ -489,6 +551,12 @@ psiconv_list_free_el(section,&psiconv_free_paint_data_section_aux); } +void psiconv_free_jumptable_section (psiconv_jumptable_section section) +{ + if (section) + psiconv_list_free(section); +} + void psiconv_free_mbm_f(psiconv_mbm_f file) { if (file) { @@ -497,11 +565,49 @@ } } -void psiconv_free_mbm_jumptable_section (psiconv_mbm_jumptable_section section) +void psiconv_free_sketch_section(psiconv_sketch_section sec) +{ + if (sec) { + psiconv_free_paint_data_section(sec->picture); + free(sec); + } +} + +void psiconv_free_sketch_f(psiconv_sketch_f file) +{ + if (file) { + psiconv_free_sketch_section(file->sketch_sec); + free(file); + } +} + +void psiconv_free_clipart_section_aux(void *section) { if (section) - psiconv_list_free(section); -} + free(((psiconv_clipart_section ) section)->picture); +} + +void psiconv_free_clipart_section(psiconv_clipart_section section) +{ + if (section) { + psiconv_free_clipart_section_aux(section); + free(section); + } +} + +void psiconv_free_cliparts(psiconv_cliparts section) +{ + if (section) + psiconv_list_free_el(section,&psiconv_free_clipart_section_aux); +} + +void psiconv_free_clipart_f(psiconv_clipart_f file) +{ + if (file) { + psiconv_free_cliparts(file->sections); + free(file); + } +} void psiconv_free_file(psiconv_file file) { @@ -512,6 +618,145 @@ psiconv_free_texted_f((psiconv_texted_f) file->file); else if (file->type == psiconv_mbm_file) psiconv_free_mbm_f((psiconv_mbm_f) file->file); + else if (file->type == psiconv_sketch_file) + psiconv_free_sketch_f((psiconv_sketch_f) file->file); + else if (file->type == psiconv_clipart_file) + psiconv_free_clipart_f((psiconv_clipart_f) file->file); free(file); } } + +int psiconv_compare_color(const psiconv_color value1, + const psiconv_color value2) +{ + if (!value1 || !value2) + return 1; + if ((value1->red == value2->red) && + (value1->green == value2->green) && + (value1->blue == value2->blue)) + return 0; + else + return 1; +} + +int psiconv_compare_font(const psiconv_font value1, + const psiconv_font value2) +{ + if (!value1 || !value2 || !value1->name || !value2->name) + return 1; + if ((value1->screenfont == value2->screenfont) && + !strcmp(value1->name,value2->name)) + return 0; + else + return 1; +} + +int psiconv_compare_border(const psiconv_border value1, + const psiconv_border value2) +{ + if (!value1 || !value2) + return 1; + if ((value1->kind == value2->kind) && + (value1->thickness == value2->thickness) && + !psiconv_compare_color(value1->color,value2->color)) + return 0; + else + return 1; +} + +int psiconv_compare_bullet(const psiconv_bullet value1, + const psiconv_bullet value2) +{ + if (!value1 || !value2) + return 1; + if ((value1->on == value2->on) && + (value1->font_size == value2->font_size) && + (value1->character == value2->character) && + (value1->indent == value2->indent) && + !psiconv_compare_color(value1->color,value2->color) && + !psiconv_compare_font(value1->font,value2->font)) + return 0; + else + return 1; +} + +int psiconv_compare_tab(const psiconv_tab value1, const psiconv_tab value2) +{ + if (!value1 || !value2) + return 1; + if ((value1->location == value2->location) && + (value1->kind == value2->kind)) + return 0; + else + return 1; +} + +int psiconv_compare_all_tabs(const psiconv_all_tabs value1, + const psiconv_all_tabs value2) +{ + int i; + + if (!value1 || !value2 || !value1->extras || !value2->extras) + return 1; + + if ((value1->normal != value2->normal) || + psiconv_list_length(value1->extras) != + psiconv_list_length(value2->extras)) + return 1; + for (i = 0; i < psiconv_list_length(value1->extras); i++) + if (psiconv_compare_tab(psiconv_list_get(value1->extras,i), + psiconv_list_get(value2->extras,i))) + + return 1; + return 0; +} + +int psiconv_compare_paragraph_layout(const psiconv_paragraph_layout value1, + const psiconv_paragraph_layout value2) +{ + if (!value1 || !value2) + return 1; + if ((value1->indent_left == value2->indent_left) && + (value1->indent_right == value2->indent_right) && + (value1->indent_first == value2->indent_first) && + (value1->justify_hor == value2->justify_hor) && + (value1->justify_ver == value2->justify_ver) && + (value1->linespacing == value2->linespacing) && + (value1->space_above == value2->space_above) && + (value1->space_below == value2->space_below) && + (value1->keep_together == value2->keep_together) && + (value1->keep_with_next == value2->keep_with_next) && + (value1->on_next_page == value2->on_next_page) && + (value1->no_widow_protection == value2->no_widow_protection) && + (value1->border_distance == value2->border_distance) && + !psiconv_compare_color(value1->back_color,value2->back_color) && + !psiconv_compare_bullet(value1->bullet,value2->bullet) && + !psiconv_compare_border(value1->left_border,value2->left_border) && + !psiconv_compare_border(value1->right_border,value2->right_border) && + !psiconv_compare_border(value1->top_border,value2->top_border) && + !psiconv_compare_border(value1->bottom_border,value2->bottom_border) && + !psiconv_compare_all_tabs(value1->tabs,value2->tabs)) + return 0; + else + return 1; +} + + +int psiconv_compare_character_layout(const psiconv_character_layout value1, + const psiconv_character_layout value2) +{ + if (!value1 || !value2) + return 1; + if ((value1->font_size == value2->font_size) && + (value1->italic == value2->italic) && + (value1->bold == value2->bold) && + (value1->super_sub == value2->super_sub) && + (value1->underline == value2->underline) && + (value1->strikethrough == value2->strikethrough) && + !psiconv_compare_color(value1->color,value2->color) && + !psiconv_compare_color(value1->back_color,value2->back_color) && + !psiconv_compare_font(value1->font,value2->font)) + return 0; + else + return 1; +}