Debian Codegraph @

unikey/convert.cpp

raw
1 // -*- coding:unix; mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 /*------------------------------------------------------------------------------
3 VnConv: Vietnamese Encoding Converter Library
4 UniKey Project: http://unikey.sourceforge.net
5 Copyleft (C) 1998-2002 Pham Kim Long
6 Contact: [email protected]
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 --------------------------------------------------------------------------------*/
22
23 #include "charset.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #if defined(_WIN32)
29 #include <io.h>
30 #include <fcntl.h>
31 #endif
32
33 #include "vnconv.h"
34
35 int vnFileStreamConvert(int inCharset, int outCharset, FILE * inf, FILE *outf);
36
37 DllExport int genConvert(VnCharset & incs, VnCharset & outcs, ByteInStream & input, ByteOutStream & output)
38 {
39 StdVnChar stdChar;
40 int bytesRead, bytesWritten;
41
42 incs.startInput();
43 outcs.startOutput();
44
45 int ret = 1;
46 while (!input.eos()) {
47 stdChar = 0;
48 if (incs.nextInput(input, stdChar, bytesRead)) {
49 if (stdChar != INVALID_STD_CHAR) {
50 if (VnCharsetLibObj.m_options.toLower)
51 stdChar = StdVnToLower(stdChar);
52 else if (VnCharsetLibObj.m_options.toUpper)
53 stdChar = StdVnToUpper(stdChar);
54 if (VnCharsetLibObj.m_options.removeTone)
55 stdChar = StdVnGetRoot(stdChar);
56 ret = outcs.putChar(output, stdChar, bytesWritten);
57 }
58 }
59 else break;
60 }
61 return (ret? 0 : VNCONV_OUT_OF_MEMORY);
62 }
63
64 //----------------------------------------------
65 // Arguments:
66 // inCharset: charset of input
67 // outCharset: charset of output
68 // input: input data
69 // output: output data
70 // inLen: [in] size of input. if inLen = -1, input data is null-terminated.
71 // [out] if input inLen != -1, output iLen is the numbers of byte left in input.
72 // maxOutLen: [in] size of output.
73 // [out] number of bytes output, if enough memory
74 // number of bytes needed for output, if not enough memory
75 // Returns: 0 if successful
76 // error code: if failed
77 //----------------------------------------------
78 //int VnConvert(int inCharset, int outCharset, UKBYTE *input, UKBYTE *output, int & inLen, int & maxOutLen)
79
80 DllExport int VnConvert(int inCharset, int outCharset, UKBYTE *input, UKBYTE *output,
81 int * pInLen, int * pMaxOutLen)
82 {
83 int inLen, maxOutLen;
84 int ret = -1;
85
86 inLen = *pInLen;
87 maxOutLen = *pMaxOutLen;
88
89 if (inLen != -1 && inLen < 0) // invalid inLen
90 return ret;
91
92 VnCharset *pInCharset = VnCharsetLibObj.getVnCharset(inCharset);
93 VnCharset *pOutCharset = VnCharsetLibObj.getVnCharset(outCharset);
94
95 if (!pInCharset || !pOutCharset)
96 return VNCONV_INVALID_CHARSET;
97
98 StringBIStream is(input, inLen, pInCharset->elementSize());
99 StringBOStream os(output, maxOutLen);
100
101 ret = genConvert(*pInCharset, *pOutCharset, is, os);
102 *pMaxOutLen = os.getOutBytes();
103 *pInLen = is.left();
104 return ret;
105 }
106
107 //---------------------------------------
108 // Arguments:
109 // inFile: input file name. NULL if STDIN is used
110 // outFile: output file name, NULL if STDOUT is used
111 // Returns:
112 // 0: successful
113 // errCode: if failed
114 //---------------------------------------
115 DllExport int VnFileConvert(int inCharset, int outCharset, const char *inFile, const char *outFile)
116 {
117 FILE *inf = NULL;
118 FILE *outf = NULL;
119 int ret = 0;
120 char tmpName[32];
121
122 if (inFile == NULL) {
123 inf = stdin;
124 #if defined(_WIN32)
125 _setmode( _fileno(stdin), _O_BINARY);
126 #endif
127 }
128 else {
129 inf = fopen(inFile, "rb");
130 if (inf == NULL) {
131 ret = VNCONV_ERR_INPUT_FILE;
132 goto end;
133 }
134 }
135
136 if (outFile == NULL)
137 outf = stdout;
138 else {
139 // setup temporary output file (because real output file may be the same as input file
140 char outDir[256];
141 strcpy(outDir, outFile);
142
143 #if defined(_WIN32)
144 char *p = strrchr(outDir, '\\');
145 #else
146 char *p = strrchr(outDir, '/');
147 #endif
148
149 if (p == NULL)
150 outDir[0] = 0;
151 else
152 *p = 0;
153
154 strcpy(tmpName, outDir);
155 strcat(tmpName, "XXXXXX");
156
157 if (mkstemp(tmpName) == -1) {
158 fclose(inf);
159 ret = VNCONV_ERR_OUTPUT_FILE;
160 goto end;
161 }
162 outf = fopen(tmpName, "wb");
163
164 if (outf == NULL) {
165 fclose(inf);
166 ret = VNCONV_ERR_OUTPUT_FILE;
167 goto end;
168 }
169 }
170
171
172 ret = vnFileStreamConvert(inCharset, outCharset, inf, outf);
173 if (inf != stdin)
174 fclose(inf);
175 if (outf != stdout) {
176 fclose(outf);
177
178 // delete output file if exisits
179 if (ret == 0) {
180 remove(outFile);
181 #if !defined(_WIN32)
182 char cmd[256];
183 sprintf(cmd, "mv %s %s", tmpName, outFile);
184 cmd[0] = system(cmd);
185 #else
186 if (rename(tmpName, outFile) != 0) {
187 remove(tmpName);
188 ret = VNCONV_ERR_OUTPUT_FILE;
189 goto end;
190 }
191 #endif
192 }
193 else
194 remove(tmpName);
195 }
196
197 end:
198 #if defined(_WIN32)
199 if (inf == stdin) {
200 _setmode( _fileno(stdin), _O_BINARY);
201 }
202 #endif
203 return ret;
204 }
205
206 //------------------------------------------------
207 // Returns:
208 // 0: successful
209 // errCode: if failed
210 //---------------------------------------
211 int vnFileStreamConvert(int inCharset, int outCharset, FILE * inf, FILE *outf)
212 {
213 VnCharset *pInCharset = VnCharsetLibObj.getVnCharset(inCharset);
214 VnCharset *pOutCharset = VnCharsetLibObj.getVnCharset(outCharset);
215
216 if (!pInCharset || !pOutCharset)
217 return VNCONV_INVALID_CHARSET;
218
219 if (outCharset == CONV_CHARSET_UNICODE) {
220 UKWORD sign = 0xFEFF;
221 fwrite(&sign, sizeof(UKWORD), 1, outf);
222 }
223
224 FileBIStream is;
225 FileBOStream os;
226
227 is.attach(inf);
228 os.attach(outf);
229
230 return genConvert(*pInCharset, *pOutCharset, is, os);
231 }
232
233 const char *ErrTable[VNCONV_LAST_ERROR] =
234 {"No error",
235 "Unknown error",
236 "Invalid charset",
237 "Error opening input file",
238 "Error opening output file",
239 "Error writing to output stream",
240 "Not enough memory",
241 };
242
243 DllExport const char * VnConvErrMsg(int errCode)
244 {
245 if (errCode < 0 || errCode >= VNCONV_LAST_ERROR)
246 errCode = VNCONV_UNKNOWN_ERROR;
247 return ErrTable[errCode];
248 }
249