File: convert.cpp

package info (click to toggle)
fcitx-unikey 0.2.7%2Bgit20220410-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 684 kB
  • sloc: cpp: 6,904; makefile: 8
file content (249 lines) | stat: -rw-r--r-- 6,503 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// -*- coding:unix; mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
/*------------------------------------------------------------------------------
VnConv: Vietnamese Encoding Converter Library
UniKey Project: http://unikey.sourceforge.net
Copyleft (C) 1998-2002 Pham Kim Long
Contact: longp@cslab.felk.cvut.cz

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
--------------------------------------------------------------------------------*/

#include "charset.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(_WIN32)
	#include <io.h>
	#include <fcntl.h>
#endif

#include "vnconv.h"

int vnFileStreamConvert(int inCharset, int outCharset, FILE * inf, FILE *outf);

DllExport int genConvert(VnCharset & incs, VnCharset & outcs, ByteInStream & input, ByteOutStream & output)
{
	StdVnChar stdChar;
	int bytesRead, bytesWritten;

	incs.startInput();
	outcs.startOutput();

	int ret = 1;
	while (!input.eos()) {
        stdChar = 0;
		if (incs.nextInput(input, stdChar, bytesRead)) {
			if (stdChar != INVALID_STD_CHAR) {
			  if (VnCharsetLibObj.m_options.toLower)
			    stdChar = StdVnToLower(stdChar);
			  else if (VnCharsetLibObj.m_options.toUpper)
			    stdChar = StdVnToUpper(stdChar);
			  if (VnCharsetLibObj.m_options.removeTone)
			    stdChar = StdVnGetRoot(stdChar);
			  ret = outcs.putChar(output, stdChar, bytesWritten);
			}
		}
		else break;
	}
	return (ret? 0 : VNCONV_OUT_OF_MEMORY);
}

//----------------------------------------------
// Arguments:
//       inCharset: charset of input
//       outCharset: charset of output
//       input: input data
//       output: output data
//       inLen: [in]  size of input. if inLen = -1, input data is null-terminated.
//              [out] if input inLen != -1, output iLen is the numbers of byte left in input.
//       maxOutLen: [in] size of output.
//                  [out] number of bytes output, if enough memory
//                        number of bytes needed for output, if not enough memory
// Returns:  0 if successful
//           error code: if failed
//----------------------------------------------
//int VnConvert(int inCharset, int outCharset, UKBYTE *input, UKBYTE *output, int & inLen, int & maxOutLen)

DllExport int VnConvert(int inCharset, int outCharset, UKBYTE *input, UKBYTE *output,
	      int * pInLen, int * pMaxOutLen)
{
	int inLen, maxOutLen;
	int ret = -1;

	inLen = *pInLen;
	maxOutLen = *pMaxOutLen;

	if (inLen != -1 && inLen < 0) // invalid inLen
		return ret;

	VnCharset *pInCharset = VnCharsetLibObj.getVnCharset(inCharset);
	VnCharset *pOutCharset = VnCharsetLibObj.getVnCharset(outCharset);

	if (!pInCharset || !pOutCharset)
		return VNCONV_INVALID_CHARSET;

	StringBIStream is(input, inLen, pInCharset->elementSize());
	StringBOStream os(output, maxOutLen);

	ret = genConvert(*pInCharset, *pOutCharset, is, os);
	*pMaxOutLen = os.getOutBytes();
	*pInLen = is.left();
	return ret;
}

//---------------------------------------
// Arguments:
//   inFile: input file name. NULL if STDIN is used
//   outFile: output file name, NULL if STDOUT is used
// Returns:
//     0: successful
//     errCode: if failed
//---------------------------------------
DllExport int VnFileConvert(int inCharset, int outCharset, const char *inFile, const char *outFile)
{
	FILE *inf = NULL;
	FILE *outf = NULL;
	int ret = 0;
	char tmpName[32];

	if (inFile == NULL) {
		inf = stdin;
#if defined(_WIN32)
		_setmode( _fileno(stdin), _O_BINARY);
#endif
	}
	else {
		inf = fopen(inFile, "rb");
		if (inf == NULL) {
			ret = VNCONV_ERR_INPUT_FILE;
			goto end;
		}
	}

	if (outFile == NULL)
		outf = stdout;
	else {
		// setup temporary output file (because real output file may be the same as input file
		char outDir[256];
		strcpy(outDir, outFile);

#if defined(_WIN32)
		char *p = strrchr(outDir, '\\');
#else
		char *p = strrchr(outDir, '/');
#endif

		if (p == NULL)
			outDir[0] = 0;
		else
			*p = 0;

		strcpy(tmpName, outDir);
        strcat(tmpName, "XXXXXX");

		if (mkstemp(tmpName) == -1) {
			fclose(inf);
			ret = VNCONV_ERR_OUTPUT_FILE;
			goto end;
		}
		outf = fopen(tmpName, "wb");

		if (outf == NULL) {
			fclose(inf);
			ret = VNCONV_ERR_OUTPUT_FILE;
			goto end;
		}
	}


	ret = vnFileStreamConvert(inCharset, outCharset, inf, outf);
	if (inf != stdin)
		fclose(inf);
	if (outf != stdout) {
		fclose(outf);

		// delete output file if exisits
		if (ret == 0) {
			remove(outFile);
#if !defined(_WIN32)
			char cmd[256];
			sprintf(cmd, "mv %s %s", tmpName, outFile);
			cmd[0] = system(cmd);
#else
			if (rename(tmpName, outFile) != 0) {
				remove(tmpName);
				ret = VNCONV_ERR_OUTPUT_FILE;
				goto end;
			}
#endif
		}
		else
			remove(tmpName);
	}

end:
#if defined(_WIN32)
	if (inf == stdin) {
		_setmode( _fileno(stdin), _O_BINARY);
	}
#endif
	return ret;
}

//------------------------------------------------
// Returns:
//     0: successful
//     errCode: if failed
//---------------------------------------
int vnFileStreamConvert(int inCharset, int outCharset, FILE * inf, FILE *outf)
{
	VnCharset *pInCharset = VnCharsetLibObj.getVnCharset(inCharset);
	VnCharset *pOutCharset = VnCharsetLibObj.getVnCharset(outCharset);

	if (!pInCharset || !pOutCharset)
		return VNCONV_INVALID_CHARSET;

	if (outCharset == CONV_CHARSET_UNICODE) {
		UKWORD sign = 0xFEFF;
		fwrite(&sign, sizeof(UKWORD), 1, outf);
	}

	FileBIStream is;
	FileBOStream os;

	is.attach(inf);
	os.attach(outf);

	return genConvert(*pInCharset, *pOutCharset, is, os);
}

const char *ErrTable[VNCONV_LAST_ERROR] =
{"No error",
 "Unknown error",
 "Invalid charset",
 "Error opening input file",
 "Error opening output file",
 "Error writing to output stream",
 "Not enough memory",
};

DllExport const char * VnConvErrMsg(int errCode)
{
	if (errCode < 0 || errCode >= VNCONV_LAST_ERROR)
		errCode = VNCONV_UNKNOWN_ERROR;
	return ErrTable[errCode];
}