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: longp@cslab.felk.cvut.cz
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
--------------------------------------------------------------------------------*/
35
int vnFileStreamConvert(int inCharset, int outCharset, FILE * inf, FILE *outf);
37
DllExport int genConvert(VnCharset & incs, VnCharset & outcs, ByteInStream & input, ByteOutStream & output)
40
int bytesRead, bytesWritten;
46
while (!input.eos()) {
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);
61
return (ret? 0 : VNCONV_OUT_OF_MEMORY);
64
//----------------------------------------------
66
// inCharset: charset of input
67
// outCharset: charset of output
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)
80
DllExport int VnConvert(int inCharset, int outCharset, UKBYTE *input, UKBYTE *output,
81
int * pInLen, int * pMaxOutLen)
87
maxOutLen = *pMaxOutLen;
89
if (inLen != -1 && inLen < 0) // invalid inLen
92
VnCharset *pInCharset = VnCharsetLibObj.getVnCharset(inCharset);
93
VnCharset *pOutCharset = VnCharsetLibObj.getVnCharset(outCharset);
95
if (!pInCharset || !pOutCharset)
96
return VNCONV_INVALID_CHARSET;
98
StringBIStream is(input, inLen, pInCharset->elementSize());
99
StringBOStream os(output, maxOutLen);
101
ret = genConvert(*pInCharset, *pOutCharset, is, os);
102
*pMaxOutLen = os.getOutBytes();
107
//---------------------------------------
109
// inFile: input file name. NULL if STDIN is used
110
// outFile: output file name, NULL if STDOUT is used
113
// errCode: if failed
114
//---------------------------------------
115
DllExport int VnFileConvert(int inCharset, int outCharset, const char *inFile, const char *outFile)
122
if (inFile == NULL) {
125
_setmode( _fileno(stdin), _O_BINARY);
129
inf = fopen(inFile, "rb");
131
ret = VNCONV_ERR_INPUT_FILE;
139
// setup temporary output file (because real output file may be the same as input file
141
strcpy(outDir, outFile);
144
char *p = strrchr(outDir, '\\');
146
char *p = strrchr(outDir, '/');
154
strcpy(tmpName, outDir);
155
strcat(tmpName, "XXXXXX");
157
if (mkstemp(tmpName) == -1) {
159
ret = VNCONV_ERR_OUTPUT_FILE;
162
outf = fopen(tmpName, "wb");
166
ret = VNCONV_ERR_OUTPUT_FILE;
172
ret = vnFileStreamConvert(inCharset, outCharset, inf, outf);
175
if (outf != stdout) {
178
// delete output file if exisits
183
sprintf(cmd, "mv %s %s", tmpName, outFile);
184
cmd[0] = system(cmd);
186
if (rename(tmpName, outFile) != 0) {
188
ret = VNCONV_ERR_OUTPUT_FILE;
200
_setmode( _fileno(stdin), _O_BINARY);
206
//------------------------------------------------
209
// errCode: if failed
210
//---------------------------------------
211
int vnFileStreamConvert(int inCharset, int outCharset, FILE * inf, FILE *outf)
213
VnCharset *pInCharset = VnCharsetLibObj.getVnCharset(inCharset);
214
VnCharset *pOutCharset = VnCharsetLibObj.getVnCharset(outCharset);
216
if (!pInCharset || !pOutCharset)
217
return VNCONV_INVALID_CHARSET;
219
if (outCharset == CONV_CHARSET_UNICODE) {
220
UKWORD sign = 0xFEFF;
221
fwrite(&sign, sizeof(UKWORD), 1, outf);
230
return genConvert(*pInCharset, *pOutCharset, is, os);
233
const char *ErrTable[VNCONV_LAST_ERROR] =
237
"Error opening input file",
238
"Error opening output file",
239
"Error writing to output stream",
243
DllExport const char * VnConvErrMsg(int errCode)
245
if (errCode < 0 || errCode >= VNCONV_LAST_ERROR)
246
errCode = VNCONV_UNKNOWN_ERROR;
247
return ErrTable[errCode];