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