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