--- psiconv/trunk/program/psiconv/general.c 2004/01/09 22:20:03 185 +++ psiconv/trunk/program/psiconv/general.c 2004/02/22 22:24:39 217 @@ -1,6 +1,6 @@ /* general.c - Part of psiconv, a PSION 5 file formats converter - Copyright (c) 2003 Frodo Looijaard + Copyright (c) 2003-2004 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 @@ -24,43 +24,66 @@ #include "general.h" #include #include +#include +#include +#include /* Output a UCS2 character in one of the supported encodings. */ -int output_char(psiconv_config config, psiconv_list list, +void output_char(psiconv_config config, psiconv_list list, psiconv_ucs2 character, encoding enc) { psiconv_u8 temp; - int res; + psiconv_u8 *byteptr; + int res,i; + psiconv_buffer buf; +#define TEMPSTR_LEN 80 + char tempstr[TEMPSTR_LEN]; if (enc == ENCODING_UCS2) { temp = character >> 8; - if ((res = psiconv_list_add(list,&temp))) - return res; + if ((res = psiconv_list_add(list,&temp))) { + fputs("Out of memory error\n",stderr); + exit(1); + } temp = character & 0xff; - if ((res = psiconv_list_add(list,&temp))) - return res; + if ((res = psiconv_list_add(list,&temp))) { + fputs("Out of memory error\n",stderr); + exit(1); + } } else if (enc == ENCODING_UTF8) { if (character < 0x80) { temp = character; - if ((res = psiconv_list_add(list,&temp))) - return res; + if ((res = psiconv_list_add(list,&temp))) { + fputs("Out of memory error\n",stderr); + exit(1); + } } else if (character < 0x800) { temp = 0xc0 + (character >> 6); - if ((res = psiconv_list_add(list,&temp))) - return res; + if ((res = psiconv_list_add(list,&temp))) { + fputs("Out of memory error\n",stderr); + exit(1); + } temp = 0x80 + (character & 0x3f); - if ((res = psiconv_list_add(list,&temp))) - return res; + if ((res = psiconv_list_add(list,&temp))) { + fputs("Out of memory error\n",stderr); + exit(1); + } } else { temp = 0xe0 + (character >> 12); - if ((res = psiconv_list_add(list,&temp))) - return res; + if ((res = psiconv_list_add(list,&temp))) { + fputs("Out of memory error\n",stderr); + exit(1); + } temp = 0x80 + ((character >> 6) & 0x3f); - if ((res = psiconv_list_add(list,&temp))) - return res; + if ((res = psiconv_list_add(list,&temp))) { + fputs("Out of memory error\n",stderr); + exit(1); + } temp = 0x80 + (character & 0x3f); - if ((res = psiconv_list_add(list,&temp))) - return res; + if ((res = psiconv_list_add(list,&temp))) { + fputs("Out of memory error\n",stderr); + exit(1); + } } } else if (enc == ENCODING_ASCII) { if (character == 0xa0) @@ -69,26 +92,70 @@ temp = '?'; else temp = character; - if ((res = psiconv_list_add(list,&temp))) - return res; + if ((res = psiconv_list_add(list,&temp))) { + fputs("Out of memory error\n",stderr); + exit(1); + } + } else if (enc == ENCODING_ASCII_HTML) { + if (character >= 0x80) { + snprintf(tempstr,TEMPSTR_LEN,"&#x%x;",character); + output_simple_chars(config,list,tempstr,enc); + } else { + temp = character; + if ((res = psiconv_list_add(list,&temp))) { + fputs("Out of memory error\n",stderr); + exit(1); + } + } } else if (enc == ENCODING_PSION) { - temp = psiconv_unicode_to_char(config,character); - if ((res = psiconv_list_add(list,&temp))) - return res; + if (!(buf = psiconv_buffer_new())) { + fputs("Out of memory error\n",stderr); + exit(1); + } + psiconv_unicode_write_char(config,buf,0,character); + for (i = 0; i < psiconv_buffer_length(buf); i++) { + if (!(byteptr = psiconv_buffer_get(buf,i))) { + fputs("Internal memory corruption\n",stderr); + exit(1); + } + if ((res = psiconv_list_add(list,byteptr))) { + fputs("Out of memory error\n",stderr); + exit(1); + } + } + psiconv_buffer_free(buf); } - return 0; } -int output_string(psiconv_config config, psiconv_list list, +void output_string(psiconv_config config, psiconv_list list, psiconv_ucs2 *string, encoding enc) { int i = 0; - int res; while (string[i]) { - if ((res = output_char(config,list,string[i],enc))) - return res; + output_char(config,list,string[i],enc); i++; } - return 0; +} + +void output_simple_chars(psiconv_config config, psiconv_list list, + char *string, encoding enc) +{ + psiconv_ucs2 *ucs_string; + int i; + + if (!(ucs_string = malloc(sizeof(*ucs_string) * (strlen(string) + 1)))) { + fputs("Out of memory error",stderr); + exit(1); + } + for (i = 0; i < strlen(string); i++) { + if ((string[i] != '\n') && ((string[i] < 0x20) || (string[i] > 0x7e))) { + fprintf(stderr,"output_simple_chars unknown char: %02x",string[i]); + exit(1); + } + ucs_string[i] = string[i]; + } + ucs_string[i] = string[i]; + output_string(config,list,ucs_string,enc); + free(ucs_string); }