1 |
frodo |
185 |
/* |
2 |
|
|
general.c - Part of psiconv, a PSION 5 file formats converter |
3 |
frodo |
196 |
Copyright (c) 2003-2004 Frodo Looijaard <frodol@dds.nl> |
4 |
frodo |
185 |
|
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 |
|
|
|
23 |
|
|
#include "psiconv.h" |
24 |
|
|
#include "general.h" |
25 |
|
|
#include <psiconv/list.h> |
26 |
|
|
#include <psiconv/unicode.h> |
27 |
frodo |
189 |
#include <psiconv/error.h> |
28 |
|
|
#include <stdlib.h> |
29 |
|
|
#include <string.h> |
30 |
frodo |
185 |
|
31 |
|
|
/* Output a UCS2 character in one of the supported encodings. */ |
32 |
frodo |
189 |
void output_char(psiconv_config config, psiconv_list list, |
33 |
frodo |
185 |
psiconv_ucs2 character, encoding enc) |
34 |
|
|
{ |
35 |
|
|
psiconv_u8 temp; |
36 |
frodo |
217 |
psiconv_u8 *byteptr; |
37 |
|
|
int res,i; |
38 |
|
|
psiconv_buffer buf; |
39 |
frodo |
192 |
#define TEMPSTR_LEN 80 |
40 |
|
|
char tempstr[TEMPSTR_LEN]; |
41 |
frodo |
185 |
|
42 |
|
|
if (enc == ENCODING_UCS2) { |
43 |
|
|
temp = character >> 8; |
44 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
45 |
|
|
fputs("Out of memory error\n",stderr); |
46 |
|
|
exit(1); |
47 |
|
|
} |
48 |
frodo |
185 |
temp = character & 0xff; |
49 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
50 |
|
|
fputs("Out of memory error\n",stderr); |
51 |
|
|
exit(1); |
52 |
|
|
} |
53 |
frodo |
185 |
} else if (enc == ENCODING_UTF8) { |
54 |
|
|
if (character < 0x80) { |
55 |
|
|
temp = character; |
56 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
57 |
|
|
fputs("Out of memory error\n",stderr); |
58 |
|
|
exit(1); |
59 |
|
|
} |
60 |
frodo |
185 |
} else if (character < 0x800) { |
61 |
|
|
temp = 0xc0 + (character >> 6); |
62 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
63 |
|
|
fputs("Out of memory error\n",stderr); |
64 |
|
|
exit(1); |
65 |
|
|
} |
66 |
frodo |
185 |
temp = 0x80 + (character & 0x3f); |
67 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
68 |
|
|
fputs("Out of memory error\n",stderr); |
69 |
|
|
exit(1); |
70 |
|
|
} |
71 |
frodo |
185 |
} else { |
72 |
|
|
temp = 0xe0 + (character >> 12); |
73 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
74 |
|
|
fputs("Out of memory error\n",stderr); |
75 |
|
|
exit(1); |
76 |
|
|
} |
77 |
frodo |
185 |
temp = 0x80 + ((character >> 6) & 0x3f); |
78 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
79 |
|
|
fputs("Out of memory error\n",stderr); |
80 |
|
|
exit(1); |
81 |
|
|
} |
82 |
frodo |
185 |
temp = 0x80 + (character & 0x3f); |
83 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
84 |
|
|
fputs("Out of memory error\n",stderr); |
85 |
|
|
exit(1); |
86 |
|
|
} |
87 |
frodo |
185 |
} |
88 |
|
|
} else if (enc == ENCODING_ASCII) { |
89 |
|
|
if (character == 0xa0) |
90 |
|
|
temp = ' '; |
91 |
|
|
else if (character >= 0x80) |
92 |
|
|
temp = '?'; |
93 |
|
|
else |
94 |
|
|
temp = character; |
95 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
96 |
|
|
fputs("Out of memory error\n",stderr); |
97 |
|
|
exit(1); |
98 |
|
|
} |
99 |
frodo |
192 |
} else if (enc == ENCODING_ASCII_HTML) { |
100 |
|
|
if (character >= 0x80) { |
101 |
|
|
snprintf(tempstr,TEMPSTR_LEN,"&#x%x;",character); |
102 |
|
|
output_simple_chars(config,list,tempstr,enc); |
103 |
|
|
} else { |
104 |
|
|
temp = character; |
105 |
|
|
if ((res = psiconv_list_add(list,&temp))) { |
106 |
|
|
fputs("Out of memory error\n",stderr); |
107 |
|
|
exit(1); |
108 |
|
|
} |
109 |
|
|
} |
110 |
frodo |
185 |
} else if (enc == ENCODING_PSION) { |
111 |
frodo |
217 |
if (!(buf = psiconv_buffer_new())) { |
112 |
|
|
fputs("Out of memory error\n",stderr); |
113 |
|
|
exit(1); |
114 |
|
|
} |
115 |
|
|
psiconv_unicode_write_char(config,buf,0,character); |
116 |
|
|
for (i = 0; i < psiconv_buffer_length(buf); i++) { |
117 |
|
|
if (!(byteptr = psiconv_buffer_get(buf,i))) { |
118 |
|
|
fputs("Internal memory corruption\n",stderr); |
119 |
|
|
exit(1); |
120 |
|
|
} |
121 |
|
|
if ((res = psiconv_list_add(list,byteptr))) { |
122 |
frodo |
189 |
fputs("Out of memory error\n",stderr); |
123 |
|
|
exit(1); |
124 |
|
|
} |
125 |
frodo |
217 |
} |
126 |
|
|
psiconv_buffer_free(buf); |
127 |
frodo |
185 |
} |
128 |
|
|
} |
129 |
|
|
|
130 |
frodo |
189 |
void output_string(psiconv_config config, psiconv_list list, |
131 |
frodo |
185 |
psiconv_ucs2 *string, encoding enc) |
132 |
|
|
{ |
133 |
|
|
int i = 0; |
134 |
|
|
|
135 |
|
|
while (string[i]) { |
136 |
frodo |
189 |
output_char(config,list,string[i],enc); |
137 |
frodo |
185 |
i++; |
138 |
|
|
} |
139 |
|
|
} |
140 |
frodo |
189 |
|
141 |
|
|
void output_simple_chars(psiconv_config config, psiconv_list list, |
142 |
|
|
char *string, encoding enc) |
143 |
|
|
{ |
144 |
|
|
psiconv_ucs2 *ucs_string; |
145 |
|
|
int i; |
146 |
|
|
|
147 |
|
|
if (!(ucs_string = malloc(sizeof(*ucs_string) * (strlen(string) + 1)))) { |
148 |
|
|
fputs("Out of memory error",stderr); |
149 |
|
|
exit(1); |
150 |
|
|
} |
151 |
|
|
for (i = 0; i < strlen(string); i++) { |
152 |
|
|
if ((string[i] != '\n') && ((string[i] < 0x20) || (string[i] > 0x7e))) { |
153 |
|
|
fprintf(stderr,"output_simple_chars unknown char: %02x",string[i]); |
154 |
|
|
exit(1); |
155 |
|
|
} |
156 |
|
|
ucs_string[i] = string[i]; |
157 |
|
|
} |
158 |
|
|
ucs_string[i] = string[i]; |
159 |
|
|
output_string(config,list,ucs_string,enc); |
160 |
|
|
free(ucs_string); |
161 |
|
|
} |