… | |
… | |
22 | |
22 | |
23 | #include "psiconv.h" |
23 | #include "psiconv.h" |
24 | #include "general.h" |
24 | #include "general.h" |
25 | #include <psiconv/list.h> |
25 | #include <psiconv/list.h> |
26 | #include <psiconv/unicode.h> |
26 | #include <psiconv/unicode.h> |
|
|
27 | #include <psiconv/error.h> |
|
|
28 | #include <stdlib.h> |
|
|
29 | #include <string.h> |
27 | |
30 | |
28 | /* Output a UCS2 character in one of the supported encodings. */ |
31 | /* Output a UCS2 character in one of the supported encodings. */ |
29 | int output_char(psiconv_config config, psiconv_list list, |
32 | void output_char(psiconv_config config, psiconv_list list, |
30 | psiconv_ucs2 character, encoding enc) |
33 | psiconv_ucs2 character, encoding enc) |
31 | { |
34 | { |
32 | psiconv_u8 temp; |
35 | psiconv_u8 temp; |
33 | int res; |
36 | int res; |
|
|
37 | #define TEMPSTR_LEN 80 |
|
|
38 | char tempstr[TEMPSTR_LEN]; |
34 | |
39 | |
35 | if (enc == ENCODING_UCS2) { |
40 | if (enc == ENCODING_UCS2) { |
36 | temp = character >> 8; |
41 | temp = character >> 8; |
37 | if ((res = psiconv_list_add(list,&temp))) |
42 | if ((res = psiconv_list_add(list,&temp))) { |
38 | return res; |
43 | fputs("Out of memory error\n",stderr); |
|
|
44 | exit(1); |
|
|
45 | } |
39 | temp = character & 0xff; |
46 | temp = character & 0xff; |
40 | if ((res = psiconv_list_add(list,&temp))) |
47 | if ((res = psiconv_list_add(list,&temp))) { |
41 | return res; |
48 | fputs("Out of memory error\n",stderr); |
|
|
49 | exit(1); |
|
|
50 | } |
42 | } else if (enc == ENCODING_UTF8) { |
51 | } else if (enc == ENCODING_UTF8) { |
43 | if (character < 0x80) { |
52 | if (character < 0x80) { |
44 | temp = character; |
53 | temp = character; |
45 | if ((res = psiconv_list_add(list,&temp))) |
54 | if ((res = psiconv_list_add(list,&temp))) { |
46 | return res; |
55 | fputs("Out of memory error\n",stderr); |
|
|
56 | exit(1); |
|
|
57 | } |
47 | } else if (character < 0x800) { |
58 | } else if (character < 0x800) { |
48 | temp = 0xc0 + (character >> 6); |
59 | temp = 0xc0 + (character >> 6); |
49 | if ((res = psiconv_list_add(list,&temp))) |
60 | if ((res = psiconv_list_add(list,&temp))) { |
50 | return res; |
61 | fputs("Out of memory error\n",stderr); |
|
|
62 | exit(1); |
|
|
63 | } |
51 | temp = 0x80 + (character & 0x3f); |
64 | temp = 0x80 + (character & 0x3f); |
52 | if ((res = psiconv_list_add(list,&temp))) |
65 | if ((res = psiconv_list_add(list,&temp))) { |
53 | return res; |
66 | fputs("Out of memory error\n",stderr); |
|
|
67 | exit(1); |
|
|
68 | } |
54 | } else { |
69 | } else { |
55 | temp = 0xe0 + (character >> 12); |
70 | temp = 0xe0 + (character >> 12); |
56 | if ((res = psiconv_list_add(list,&temp))) |
71 | if ((res = psiconv_list_add(list,&temp))) { |
57 | return res; |
72 | fputs("Out of memory error\n",stderr); |
|
|
73 | exit(1); |
|
|
74 | } |
58 | temp = 0x80 + ((character >> 6) & 0x3f); |
75 | temp = 0x80 + ((character >> 6) & 0x3f); |
59 | if ((res = psiconv_list_add(list,&temp))) |
76 | if ((res = psiconv_list_add(list,&temp))) { |
60 | return res; |
77 | fputs("Out of memory error\n",stderr); |
|
|
78 | exit(1); |
|
|
79 | } |
61 | temp = 0x80 + (character & 0x3f); |
80 | temp = 0x80 + (character & 0x3f); |
62 | if ((res = psiconv_list_add(list,&temp))) |
81 | if ((res = psiconv_list_add(list,&temp))) { |
63 | return res; |
82 | fputs("Out of memory error\n",stderr); |
|
|
83 | exit(1); |
|
|
84 | } |
64 | } |
85 | } |
65 | } else if (enc == ENCODING_ASCII) { |
86 | } else if (enc == ENCODING_ASCII) { |
66 | if (character == 0xa0) |
87 | if (character == 0xa0) |
67 | temp = ' '; |
88 | temp = ' '; |
68 | else if (character >= 0x80) |
89 | else if (character >= 0x80) |
69 | temp = '?'; |
90 | temp = '?'; |
70 | else |
91 | else |
71 | temp = character; |
92 | temp = character; |
72 | if ((res = psiconv_list_add(list,&temp))) |
93 | if ((res = psiconv_list_add(list,&temp))) { |
73 | return res; |
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 | } |
74 | } else if (enc == ENCODING_PSION) { |
108 | } else if (enc == ENCODING_PSION) { |
75 | temp = psiconv_unicode_to_char(config,character); |
109 | temp = psiconv_unicode_to_char(config,character); |
76 | if ((res = psiconv_list_add(list,&temp))) |
110 | if ((res = psiconv_list_add(list,&temp))) { |
77 | return res; |
111 | fputs("Out of memory error\n",stderr); |
|
|
112 | exit(1); |
|
|
113 | } |
78 | } |
114 | } |
79 | return 0; |
|
|
80 | } |
115 | } |
81 | |
116 | |
82 | int output_string(psiconv_config config, psiconv_list list, |
117 | void output_string(psiconv_config config, psiconv_list list, |
83 | psiconv_ucs2 *string, encoding enc) |
118 | psiconv_ucs2 *string, encoding enc) |
84 | { |
119 | { |
85 | int i = 0; |
120 | int i = 0; |
86 | int res; |
|
|
87 | |
121 | |
88 | while (string[i]) { |
122 | while (string[i]) { |
89 | if ((res = output_char(config,list,string[i],enc))) |
123 | output_char(config,list,string[i],enc); |
90 | return res; |
|
|
91 | i++; |
124 | i++; |
92 | } |
125 | } |
93 | return 0; |
|
|
94 | } |
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 | } |