… | |
… | |
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; |
34 | |
37 | |
35 | if (enc == ENCODING_UCS2) { |
38 | if (enc == ENCODING_UCS2) { |
36 | temp = character >> 8; |
39 | temp = character >> 8; |
37 | if ((res = psiconv_list_add(list,&temp))) |
40 | if ((res = psiconv_list_add(list,&temp))) { |
38 | return res; |
41 | fputs("Out of memory error\n",stderr); |
|
|
42 | exit(1); |
|
|
43 | } |
39 | temp = character & 0xff; |
44 | temp = character & 0xff; |
40 | if ((res = psiconv_list_add(list,&temp))) |
45 | if ((res = psiconv_list_add(list,&temp))) { |
41 | return res; |
46 | fputs("Out of memory error\n",stderr); |
|
|
47 | exit(1); |
|
|
48 | } |
42 | } else if (enc == ENCODING_UTF8) { |
49 | } else if (enc == ENCODING_UTF8) { |
43 | if (character < 0x80) { |
50 | if (character < 0x80) { |
44 | temp = character; |
51 | temp = character; |
45 | if ((res = psiconv_list_add(list,&temp))) |
52 | if ((res = psiconv_list_add(list,&temp))) { |
46 | return res; |
53 | fputs("Out of memory error\n",stderr); |
|
|
54 | exit(1); |
|
|
55 | } |
47 | } else if (character < 0x800) { |
56 | } else if (character < 0x800) { |
48 | temp = 0xc0 + (character >> 6); |
57 | temp = 0xc0 + (character >> 6); |
49 | if ((res = psiconv_list_add(list,&temp))) |
58 | if ((res = psiconv_list_add(list,&temp))) { |
50 | return res; |
59 | fputs("Out of memory error\n",stderr); |
|
|
60 | exit(1); |
|
|
61 | } |
51 | temp = 0x80 + (character & 0x3f); |
62 | temp = 0x80 + (character & 0x3f); |
52 | if ((res = psiconv_list_add(list,&temp))) |
63 | if ((res = psiconv_list_add(list,&temp))) { |
53 | return res; |
64 | fputs("Out of memory error\n",stderr); |
|
|
65 | exit(1); |
|
|
66 | } |
54 | } else { |
67 | } else { |
55 | temp = 0xe0 + (character >> 12); |
68 | temp = 0xe0 + (character >> 12); |
56 | if ((res = psiconv_list_add(list,&temp))) |
69 | if ((res = psiconv_list_add(list,&temp))) { |
57 | return res; |
70 | fputs("Out of memory error\n",stderr); |
|
|
71 | exit(1); |
|
|
72 | } |
58 | temp = 0x80 + ((character >> 6) & 0x3f); |
73 | temp = 0x80 + ((character >> 6) & 0x3f); |
59 | if ((res = psiconv_list_add(list,&temp))) |
74 | if ((res = psiconv_list_add(list,&temp))) { |
60 | return res; |
75 | fputs("Out of memory error\n",stderr); |
|
|
76 | exit(1); |
|
|
77 | } |
61 | temp = 0x80 + (character & 0x3f); |
78 | temp = 0x80 + (character & 0x3f); |
62 | if ((res = psiconv_list_add(list,&temp))) |
79 | if ((res = psiconv_list_add(list,&temp))) { |
63 | return res; |
80 | fputs("Out of memory error\n",stderr); |
|
|
81 | exit(1); |
|
|
82 | } |
64 | } |
83 | } |
65 | } else if (enc == ENCODING_ASCII) { |
84 | } else if (enc == ENCODING_ASCII) { |
66 | if (character == 0xa0) |
85 | if (character == 0xa0) |
67 | temp = ' '; |
86 | temp = ' '; |
68 | else if (character >= 0x80) |
87 | else if (character >= 0x80) |
69 | temp = '?'; |
88 | temp = '?'; |
70 | else |
89 | else |
71 | temp = character; |
90 | temp = character; |
72 | if ((res = psiconv_list_add(list,&temp))) |
91 | if ((res = psiconv_list_add(list,&temp))) { |
73 | return res; |
92 | fputs("Out of memory error\n",stderr); |
|
|
93 | exit(1); |
|
|
94 | } |
74 | } else if (enc == ENCODING_PSION) { |
95 | } else if (enc == ENCODING_PSION) { |
75 | temp = psiconv_unicode_to_char(config,character); |
96 | temp = psiconv_unicode_to_char(config,character); |
76 | if ((res = psiconv_list_add(list,&temp))) |
97 | if ((res = psiconv_list_add(list,&temp))) { |
77 | return res; |
98 | fputs("Out of memory error\n",stderr); |
|
|
99 | exit(1); |
|
|
100 | } |
78 | } |
101 | } |
79 | return 0; |
|
|
80 | } |
102 | } |
81 | |
103 | |
82 | int output_string(psiconv_config config, psiconv_list list, |
104 | void output_string(psiconv_config config, psiconv_list list, |
83 | psiconv_ucs2 *string, encoding enc) |
105 | psiconv_ucs2 *string, encoding enc) |
84 | { |
106 | { |
85 | int i = 0; |
107 | int i = 0; |
86 | int res; |
|
|
87 | |
108 | |
88 | while (string[i]) { |
109 | while (string[i]) { |
89 | if ((res = output_char(config,list,string[i],enc))) |
110 | output_char(config,list,string[i],enc); |
90 | return res; |
|
|
91 | i++; |
111 | i++; |
92 | } |
112 | } |
93 | return 0; |
|
|
94 | } |
113 | } |
|
|
114 | |
|
|
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 | } |