imgtools/buildrom/src/charsettran.cpp
changeset 626 ac03b93ca9c4
equal deleted inserted replaced
625:a1925fb7753a 626:ac03b93ca9c4
       
     1 /*
       
     2 * Copyright (c) 1995-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 
       
    18 #include <iostream>
       
    19 #include <string>
       
    20 #include "uniconv.hpp"
       
    21 #include "h_utl.h"
       
    22 
       
    23 using namespace std;
       
    24 
       
    25 void UTF82Host();
       
    26 void Host2UTF8();
       
    27 void PrintUsage();
       
    28 const unsigned int maxlength = 512;
       
    29 const float version = 0.1;
       
    30 
       
    31 int main(int argc, char* argv[])
       
    32 {
       
    33 	if(argc != 2)
       
    34 	{
       
    35 		PrintUsage();
       
    36 		return 0;
       
    37 	}
       
    38 	if(strncmp(argv[1], "-to=", 4))
       
    39 	{
       
    40 		cout << "Error parameters!" << endl;
       
    41 		return 0;
       
    42 	}
       
    43 	char * p = argv[1]+4;
       
    44 	if(!strnicmp(p, "hostcharset", 11))
       
    45 	{
       
    46 		UTF82Host();
       
    47 	}
       
    48 	else if(!strnicmp(p, "utf8", 4) || !strnicmp(p, "utf-8", 5))
       
    49 	{
       
    50 		Host2UTF8();
       
    51 	}
       
    52 	else
       
    53 	{
       
    54 		PrintUsage();
       
    55 	}
       
    56 }
       
    57 void PrintUsage()
       
    58 {
       
    59 	cout << "Charset translation tool - version: " << version << endl;
       
    60 	cout << "Usage: charsettran -to=[utf8|hostcharset]" << endl;
       
    61 }
       
    62 void UTF82Host()
       
    63 {
       
    64 	string tmpline;
       
    65 	char* tmpBuf = new char[maxlength];
       
    66 	unsigned int strLen = maxlength;
       
    67 	while(getline(cin, tmpline))
       
    68 	{
       
    69 		if(UniConv::IsPureASCIITextStream(tmpline.c_str()))
       
    70 		{
       
    71 			cout << tmpline << endl;
       
    72 			continue;
       
    73 		}
       
    74 		unsigned int outLen = maxlength;
       
    75 		int ret = UniConv::UTF82DefaultCodePage(tmpline.c_str(), tmpline.length(), &tmpBuf, &outLen);
       
    76 		if(ret == -1)
       
    77 		{
       
    78 			cout << tmpline << endl;
       
    79 			continue;
       
    80 		}
       
    81 		if(outLen > strLen)
       
    82 		{
       
    83 			strLen = outLen;
       
    84 		}
       
    85 		cout << tmpBuf << endl;
       
    86 
       
    87 	}
       
    88 	delete[] tmpBuf;
       
    89 }
       
    90 void Host2UTF8()
       
    91 {
       
    92 	string tmpline;
       
    93 	char* tmpBuf = new char[maxlength];
       
    94 	unsigned int strLen = maxlength;
       
    95 	while(cin >> tmpline)
       
    96 	{
       
    97 		if(UniConv::IsPureASCIITextStream(tmpline.c_str()))
       
    98 		{
       
    99 			cout << tmpline << endl;
       
   100 			continue;
       
   101 		}
       
   102 		unsigned int outLen = maxlength;
       
   103 		int ret = UniConv::DefaultCodePage2UTF8(tmpline.c_str(), tmpline.length(), &tmpBuf, &outLen);
       
   104 		if(ret == -1)
       
   105 		{
       
   106 			cout << tmpline << endl;
       
   107 			continue;
       
   108 		}
       
   109 		if(outLen > strLen)
       
   110 		{
       
   111 			strLen = outLen;
       
   112 		}
       
   113 		cout << tmpBuf << endl;
       
   114 
       
   115 	}
       
   116 	delete[] tmpBuf;
       
   117 }