1 |
frodo |
185 |
/* |
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 |
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 |
|
|
int res; |
37 |
|
|
|
38 |
|
|
if (enc == ENCODING_UCS2) { |
39 |
|
|
temp = character >> 8; |
40 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
41 |
|
|
fputs("Out of memory error\n",stderr); |
42 |
|
|
exit(1); |
43 |
|
|
} |
44 |
frodo |
185 |
temp = character & 0xff; |
45 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
46 |
|
|
fputs("Out of memory error\n",stderr); |
47 |
|
|
exit(1); |
48 |
|
|
} |
49 |
frodo |
185 |
} else if (enc == ENCODING_UTF8) { |
50 |
|
|
if (character < 0x80) { |
51 |
|
|
temp = character; |
52 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
53 |
|
|
fputs("Out of memory error\n",stderr); |
54 |
|
|
exit(1); |
55 |
|
|
} |
56 |
frodo |
185 |
} else if (character < 0x800) { |
57 |
|
|
temp = 0xc0 + (character >> 6); |
58 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
59 |
|
|
fputs("Out of memory error\n",stderr); |
60 |
|
|
exit(1); |
61 |
|
|
} |
62 |
frodo |
185 |
temp = 0x80 + (character & 0x3f); |
63 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
64 |
|
|
fputs("Out of memory error\n",stderr); |
65 |
|
|
exit(1); |
66 |
|
|
} |
67 |
frodo |
185 |
} else { |
68 |
|
|
temp = 0xe0 + (character >> 12); |
69 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
70 |
|
|
fputs("Out of memory error\n",stderr); |
71 |
|
|
exit(1); |
72 |
|
|
} |
73 |
frodo |
185 |
temp = 0x80 + ((character >> 6) & 0x3f); |
74 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
75 |
|
|
fputs("Out of memory error\n",stderr); |
76 |
|
|
exit(1); |
77 |
|
|
} |
78 |
frodo |
185 |
temp = 0x80 + (character & 0x3f); |
79 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
80 |
|
|
fputs("Out of memory error\n",stderr); |
81 |
|
|
exit(1); |
82 |
|
|
} |
83 |
frodo |
185 |
} |
84 |
|
|
} else if (enc == ENCODING_ASCII) { |
85 |
|
|
if (character == 0xa0) |
86 |
|
|
temp = ' '; |
87 |
|
|
else if (character >= 0x80) |
88 |
|
|
temp = '?'; |
89 |
|
|
else |
90 |
|
|
temp = character; |
91 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
92 |
|
|
fputs("Out of memory error\n",stderr); |
93 |
|
|
exit(1); |
94 |
|
|
} |
95 |
frodo |
185 |
} else if (enc == ENCODING_PSION) { |
96 |
|
|
temp = psiconv_unicode_to_char(config,character); |
97 |
frodo |
189 |
if ((res = psiconv_list_add(list,&temp))) { |
98 |
|
|
fputs("Out of memory error\n",stderr); |
99 |
|
|
exit(1); |
100 |
|
|
} |
101 |
frodo |
185 |
} |
102 |
|
|
} |
103 |
|
|
|
104 |
frodo |
189 |
void output_string(psiconv_config config, psiconv_list list, |
105 |
frodo |
185 |
psiconv_ucs2 *string, encoding enc) |
106 |
|
|
{ |
107 |
|
|
int i = 0; |
108 |
|
|
|
109 |
|
|
while (string[i]) { |
110 |
frodo |
189 |
output_char(config,list,string[i],enc); |
111 |
frodo |
185 |
i++; |
112 |
|
|
} |
113 |
|
|
} |
114 |
frodo |
189 |
|
115 |
|
|
void output_simple_chars(psiconv_config config, psiconv_list list, |
116 |
|
|
char *string, encoding enc) |
117 |
|
|
{ |
118 |
|
|
psiconv_ucs2 *ucs_string; |
119 |
|
|
int i; |
120 |
|
|
|
121 |
|
|
if (!(ucs_string = malloc(sizeof(*ucs_string) * (strlen(string) + 1)))) { |
122 |
|
|
fputs("Out of memory error",stderr); |
123 |
|
|
exit(1); |
124 |
|
|
} |
125 |
|
|
for (i = 0; i < strlen(string); i++) { |
126 |
|
|
if ((string[i] != '\n') && ((string[i] < 0x20) || (string[i] > 0x7e))) { |
127 |
|
|
fprintf(stderr,"output_simple_chars unknown char: %02x",string[i]); |
128 |
|
|
exit(1); |
129 |
|
|
} |
130 |
|
|
ucs_string[i] = string[i]; |
131 |
|
|
} |
132 |
|
|
ucs_string[i] = string[i]; |
133 |
|
|
output_string(config,list,ucs_string,enc); |
134 |
|
|
free(ucs_string); |
135 |
|
|
} |