--- psiconv/trunk/lib/psiconv/parse_common.c 2004/02/21 13:24:04 216 +++ psiconv/trunk/lib/psiconv/parse_common.c 2004/02/22 22:24:39 217 @@ -277,145 +277,116 @@ psiconv_u32 text_len; psiconv_paragraph para; - psiconv_u8 temp; + psiconv_ucs2 temp; + psiconv_list line; + psiconv_ucs2 *nextcharptr; int nr; - int i,j,start,leng; + int i,j,leng; char *str_copy; psiconv_progress(config,lev+1,off,"Going to parse the text section"); - psiconv_progress(config,lev+2,off,"Reading the text length"); if(!(*result = psiconv_list_new(sizeof(*para)))) goto ERROR1; if (!(para = malloc(sizeof(*para)))) goto ERROR2; + psiconv_progress(config,lev+2,off,"Reading the text length"); text_len = psiconv_read_X(config,buf,lev+2,off,&leng,&res); if (res) goto ERROR3; psiconv_debug(config,lev+2,off,"Length: %08x",text_len); len += leng; - psiconv_progress(config,lev+2,off+len,"Going to read all paragraph text"); + if (!(line = psiconv_list_new(sizeof(psiconv_ucs2)))) + goto ERROR3; + + i = 0; nr = 0; - start = 0; - for (i = 0; i < text_len; i++) { - temp = psiconv_read_u8(config,buf,lev+2,off+len+i,&res); + while (i < text_len) { + temp = psiconv_unicode_read_char(config,buf,lev+2,off+len+i,&leng,&res); if (res) - goto ERROR3; - if (temp == 0x06) { - if (!(para->text = malloc((sizeof((*(para->text))) * (i - start + 1))))) - goto ERROR3; - for (j = 0; j < i - start; j++) { - temp = psiconv_read_u8(config,buf,lev+1,off + len + start + j,&res); - if (res) - goto ERROR4; - para->text[j] = psiconv_unicode_from_char(config,temp); + goto ERROR4; + if (i + leng > text_len) { + psiconv_error(config,lev+2,off+len+i,"Malformed text section"); + res = PSICONV_E_PARSE; + goto ERROR4; + } + if ((temp == 0x06) || (i + leng == text_len)) { + if (!(para->text = malloc(sizeof(*(para->text)) * + (psiconv_list_length(line) + 1)))) + goto ERROR4; + for (j = 0; j < psiconv_list_length(line); j++) { + if (!(nextcharptr = psiconv_list_get(line,j))) { + psiconv_error(config,lev+2,off+i+len,"Internal data corruption"); + goto ERROR5; + } + para->text[j] = *nextcharptr; } para->text[j] = 0; - - if ((res = psiconv_list_add(*result,para))) - goto ERROR4; if (!(str_copy = psiconv_make_printable(config,para->text))) - goto ERROR3; + goto ERROR5; psiconv_debug(config,lev+2,off+i+len,"Line %d: %d characters",nr, strlen(str_copy) +1); psiconv_debug(config,lev+2,off+i+len,"Line %d: `%s'",nr,str_copy); free(str_copy); + i += leng; + + if (!(para->in_lines = psiconv_list_new(sizeof( + struct psiconv_in_line_layout_s)))) + goto ERROR5; + if (!(para->replacements = psiconv_list_new(sizeof( + struct psiconv_replacement_s)))) + goto ERROR6; + if (!(para->base_character = psiconv_basic_character_layout())) + goto ERROR7; + if (!(para->base_paragraph = psiconv_basic_paragraph_layout())) + goto ERROR8; + para->base_style = 0; - start = i + 1; + if ((res = psiconv_list_add(*result,para))) + goto ERROR9; + psiconv_progress(config,lev+2,off+len+i,"Starting a new line"); + psiconv_list_empty(line); nr ++; + } else { + if ((res = psiconv_list_add(line,&temp))) + goto ERROR4; + i += leng; } } - - if (start != text_len) { - psiconv_warn(config,lev+2,off+start+len, - "Last line does not end on EOL (%d characters left)", len - start); - if (!(para->text = malloc(text_len - start + 1))) - goto ERROR3; - for (j = 0; j < text_len - start; j++) { - temp = psiconv_read_u8(config,buf,lev+2,off + start + j + len, &res); - if (res) - goto ERROR4; - para->text[j] = psiconv_unicode_from_char(config,temp); - } - para->text[text_len - start] = 0; - if ((res = psiconv_list_add(*result,para))) - goto ERROR4; - if (!(str_copy = psiconv_make_printable(config,para->text))) - goto ERROR3; - psiconv_debug(config,lev+2,off+start+len,"Last line: %d characters",nr, - strlen(str_copy)+1); - psiconv_debug(config,lev+2,off+start+len,"Last line: `%s'",str_copy); - free(str_copy); - } - - free(para); - /* Initialize the remaining parts of each paragraph */ - for (i = 0; i < psiconv_list_length(*result); i ++) { - if (!(para = psiconv_list_get(*result,i))) { - psiconv_error(config,lev+2,off+len,"Massive memory corruption"); - goto ERROR2_0; - } - if (!(para->in_lines = psiconv_list_new(sizeof( - struct psiconv_in_line_layout_s)))) - goto ERROR2_0; - if (!(para->replacements = psiconv_list_new(sizeof( - struct psiconv_replacement_s)))) - goto ERROR2_1; - if (!(para->base_character = psiconv_basic_character_layout())) - goto ERROR2_2; - if (!(para->base_paragraph = psiconv_basic_paragraph_layout())) - goto ERROR2_3; - para->base_style = 0; - } - + psiconv_list_free(line); + free(para); len += text_len; if (length) *length = len; - psiconv_progress(config,lev+1,off+len-1,"End of text section (total length: %08x", - len); + psiconv_progress(config,lev+1,off+len-1, + "End of text section (total length: %08x", len); return res; -ERROR2_3: +ERROR9: + psiconv_free_paragraph_layout(para->base_paragraph); +ERROR8: psiconv_free_character_layout(para->base_character); -ERROR2_2: +ERROR7: psiconv_list_free(para->replacements); -ERROR2_1: +ERROR6: psiconv_list_free(para->in_lines); -ERROR2_0: - for (j = 0; j < i; j++) { - if (!(para = psiconv_list_get(*result,j))) { - psiconv_error(config,lev+1,off,"Massive memory corruption..."); - break; - } - psiconv_list_free(para->in_lines); - psiconv_list_free(para->replacements); - psiconv_free_character_layout(para->base_character); - psiconv_free_paragraph_layout(para->base_paragraph); - } - goto ERROR2; - -ERROR4: +ERROR5: free(para->text); +ERROR4: + psiconv_list_free(line); ERROR3: free(para); ERROR2: - for (i = 0; i < psiconv_list_length(*result);i++) { - if (!(para = psiconv_list_get(*result,i))) { - psiconv_error(config,lev+1,off,"Massive memory corruption..."); - break; - } - free(para->text); - } - psiconv_list_free(*result); + psiconv_free_text_and_layout(*result); ERROR1: psiconv_error(config,lev+1,off,"Reading of Text Section failed"); if (length) @@ -913,8 +884,8 @@ psiconv_clone_paragraph_layout(base_para))) goto ERROR4; styles_section->normal->hotkey = 0; - if (( res = psiconv_unicode_from_chars(config,(psiconv_u8 *) "", - &styles_section->normal->name))) + + if (!(styles_section->normal->name = psiconv_unicode_empty_string())) goto ERROR5; if (!(styles_section->styles = psiconv_list_new(sizeof( struct psiconv_word_style_s))))