600
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
#include "UTF16String.h"
|
|
18 |
#include <iostream>
|
|
19 |
#include <fstream>
|
|
20 |
#include <string>
|
|
21 |
using namespace std ;
|
|
22 |
|
|
23 |
#ifdef __LINUX__
|
|
24 |
#define stricmp strcasecmp
|
|
25 |
#endif
|
|
26 |
|
|
27 |
void PrintHelp(){
|
|
28 |
cout << "Syntax: TestUTF16Str <-[mbtou|utomb] > -i inputfilename -o outputfilename "<<endl;
|
|
29 |
cout << " mbtou is by default."<<endl;
|
|
30 |
}
|
|
31 |
int main(int argc, char* argv[]){
|
|
32 |
const char* input = 0 ;
|
|
33 |
const char* output = 0 ;
|
|
34 |
if(argc < 5){
|
|
35 |
PrintHelp();
|
|
36 |
return 1;
|
|
37 |
}
|
|
38 |
bool mbtou = true ;
|
|
39 |
int i = 1;
|
|
40 |
while(i < argc){
|
|
41 |
if('-' == *argv[i] || '/' == *argv[i]){
|
|
42 |
if(!stricmp(&(argv[i][1]),"utomb"))
|
|
43 |
mbtou = false ;
|
|
44 |
else if((argv[i][1] | 0x20) == 'i'){
|
|
45 |
i++ ;
|
|
46 |
if(i < argc)
|
|
47 |
input = argv[i];
|
|
48 |
}
|
|
49 |
else if((argv[i][1] | 0x20) == 'o'){
|
|
50 |
i++ ;
|
|
51 |
if(i < argc)
|
|
52 |
output = argv[i];
|
|
53 |
}
|
|
54 |
else if(stricmp(&(argv[i][1]),"mbtou")){
|
|
55 |
cerr << "Unrecognized option "<< argv[i] << endl ;
|
|
56 |
}
|
|
57 |
}
|
|
58 |
else {
|
|
59 |
cerr << "Unrecognized option "<< argv[i] << endl ;
|
|
60 |
}
|
|
61 |
i++ ;
|
|
62 |
}
|
|
63 |
if(!input || !output){
|
|
64 |
PrintHelp();
|
|
65 |
return 2;
|
|
66 |
}
|
|
67 |
fstream ifs(input, ios_base::in + ios_base::binary);
|
|
68 |
if(!ifs.is_open()){
|
|
69 |
cerr << "Cannot open \""<< input << "\" for reading."<<endl ;
|
|
70 |
return 3;
|
|
71 |
}
|
|
72 |
fstream ofs(output, ios_base::out + ios_base::binary + ios_base::trunc);
|
|
73 |
if(!ofs.is_open()){
|
|
74 |
cerr << "Cannot open \""<< output << "\" for writing."<<endl ;
|
|
75 |
ifs.close();
|
|
76 |
return 4;
|
|
77 |
}
|
|
78 |
ifs.seekg(0,ios_base::end);
|
|
79 |
size_t length = ifs.tellg();
|
|
80 |
ifs.seekg(0,ios_base::beg);
|
|
81 |
char* buffer = new char[length + 2];
|
|
82 |
ifs.read(buffer,length);
|
|
83 |
buffer[length] = 0 ;
|
|
84 |
buffer[length + 1] = 0 ;
|
|
85 |
ifs.close();
|
|
86 |
static unsigned char const utf16FileHdr[2] = {0xFF,0xFE};
|
|
87 |
static unsigned char const utf8FileHdr[3] = {0xEF,0xBB,0xBF};
|
|
88 |
if(mbtou){
|
|
89 |
char* mbstr = buffer ;
|
|
90 |
if(length > 3){
|
|
91 |
if(memcmp(buffer,utf8FileHdr,sizeof(utf8FileHdr)) == 0){
|
|
92 |
mbstr += 3;
|
|
93 |
length -= 3 ;
|
|
94 |
}
|
|
95 |
}
|
|
96 |
UTF16String theStr(mbstr , length);
|
|
97 |
if(length > 0 && theStr.IsEmpty()){
|
|
98 |
cerr << "Convert Error[From UTF8 To UTF16]."<<endl ;
|
|
99 |
}
|
|
100 |
else{
|
|
101 |
length = theStr.length() << 1;
|
|
102 |
ofs.write(reinterpret_cast<const char*>(utf16FileHdr),sizeof(utf16FileHdr));
|
|
103 |
ofs.write(reinterpret_cast<const char*>(theStr.c_str()),length);
|
|
104 |
cout << "Done."<<endl ;
|
|
105 |
}
|
|
106 |
}
|
|
107 |
else{
|
|
108 |
TUint16* unistr = reinterpret_cast<TUint16*>(buffer);
|
|
109 |
length >>= 1;
|
|
110 |
if(*unistr == 0xFEFF){
|
|
111 |
unistr ++ ;
|
|
112 |
length -- ;
|
|
113 |
}
|
|
114 |
UTF16String theStr(unistr , length);
|
|
115 |
string mbstr ;
|
|
116 |
if(!theStr.ToUTF8(mbstr)){
|
|
117 |
cerr << "Convert Error[From UTF16 To UTF8]."<<endl ;
|
|
118 |
}else{
|
|
119 |
//ofs.write(reinterpret_cast<const char*>(utf8FileHdr),sizeof(utf8FileHdr));
|
|
120 |
ofs.write(mbstr.c_str(),mbstr.length());
|
|
121 |
cout << "Done."<<endl ;
|
|
122 |
}
|
|
123 |
}
|
|
124 |
ofs.close();
|
|
125 |
delete []buffer ;
|
|
126 |
return 0;
|
|
127 |
}
|