--- psiconv/trunk/lib/psiconv/parse_common.c 2004/02/09 23:12:58 208 +++ psiconv/trunk/lib/psiconv/parse_common.c 2004/03/22 20:08:09 244 @@ -277,145 +277,107 @@ 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); - } - para->text[j] = 0; - - if ((res = psiconv_list_add(*result,para))) - goto ERROR4; + 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 = psiconv_unicode_from_list(line))) + 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) @@ -609,7 +571,7 @@ psiconv_debug(config,lev+4,off+len,"Type: %02x",temp); for (j = 0; j < psiconv_list_length(anon_styles); j++) { if (!(anon_ptr = psiconv_list_get(anon_styles,j))) { - psiconv_error(config,lev+4,off+len,"Massive memory curruption"); + psiconv_error(config,lev+4,off+len,"Data structure corruption"); goto ERROR4; } if (temp == anon_ptr->nr) @@ -715,7 +677,7 @@ total = 0; for (i = 0; i < psiconv_list_length(result); i++) { if (!(para = psiconv_list_get(result,i))) { - psiconv_error(config,lev+3,off+len,"Massive memory corruption"); + psiconv_error(config,lev+3,off+len,"Data structure corruption"); goto ERROR4; } line_length = -1; @@ -820,7 +782,7 @@ for (i = 0 ; i < psiconv_list_length(anon_styles); i ++) { if (!(anon_ptr = psiconv_list_get(anon_styles,i))) { - psiconv_error(config,lev+4,off+len,"Massive memory corruption"); + psiconv_error(config,lev+4,off+len,"Data structure corruption"); goto ERROR2; } psiconv_free_character_layout(anon_ptr->character); @@ -862,7 +824,7 @@ ERROR3: for (i = 0; i < psiconv_list_length(anon_styles); i++) { if (!(anon_ptr = psiconv_list_get(anon_styles,i))) { - psiconv_error(config,lev+1,off,"Massive memory corruption"); + psiconv_error(config,lev+1,off,"Data structure corruption"); break; } psiconv_free_paragraph_layout(anon_ptr->paragraph); @@ -913,8 +875,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)))) @@ -1265,7 +1227,6 @@ (*result)->icon_name = psiconv_read_string(config,buf,lev+2,off+len,&leng,&res); if (res) goto ERROR2; - psiconv_debug(config,lev+2,off+len,"Icon name: %s",(*result)->icon_name); len += leng; psiconv_progress(config,lev+2,off+len,"Going to read the icon width");