Back to index

lightning-sunbird  0.9+nobinonly
convperf.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include <windows.h>
00038 #include <winnls.h>
00039 
00040 #include "nscore.h"
00041 #include "nsString.h"
00042 #include "nsIServiceManager.h"
00043 #include "nsICharsetConverterManager.h"
00044 #include "nsIUnicodeEncoder.h"
00045 #include "nsIUnicodeDecoder.h"
00046 static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
00047 static NS_DEFINE_IID(kICharsetConverterManagerIID, NS_ICHARSETCONVERTERMANAGER_IID);
00048 
00049 #include <stdio.h>
00050 #include <string.h>
00051 #include <stdlib.h>
00052 void usage()
00053 {
00054   printf(
00055 "convperf -f fromcode -t tocode [file]\n"
00056   );
00057 }
00058 int fromcodeind = 0;
00059 int tocodeind = 0;
00060 FILE* infile = 0;
00061 #define INBUFSIZE (1024*16)
00062 #define MEDBUFSIZE (1024*16*2)
00063 #define OUTBUFSIZE (1024*16*8)
00064 char inbuffer[INBUFSIZE];
00065 char outbuffer[OUTBUFSIZE];
00066 PRUnichar  medbuffer[MEDBUFSIZE];
00067 nsIUnicodeEncoder* encoder = nsnull;
00068 nsIUnicodeDecoder* decoder = nsnull;
00069 UINT incp = 932;
00070 UINT outcp = 932;
00071 
00072 void memcpyDecode(const char* src, PRInt32 srclen, char* dest)
00073 {
00074    ::memcpy(dest, src, srclen);
00075 }
00076 void memcpyEncode(const char* src, PRInt32 srclen, char* dest)
00077 {
00078    ::memcpy(dest, src, srclen);
00079 }
00080 
00081 void WideDecode(const char* src, 
00082               PRInt32 srclen, PRUnichar *dest, PRInt32 *destLen)
00083 {
00084    const char* end = src+srclen ;
00085    while(src < end)
00086      *dest++ = (PRUnichar) *src++;
00087    *destLen = srclen;
00088 }
00089 void NarrowEncode(const PRUnichar *src, 
00090               PRInt32 srclen, char* dest, PRInt32* destLen)
00091 {
00092    const PRUnichar* end = src+srclen ;
00093    while(src < end)
00094      *dest++ = (char) *src++;
00095    *destLen = srclen;
00096 }
00097 void msDecode(UINT cp, const char* src, 
00098               PRInt32 srclen, PRUnichar *dest, PRInt32 *destLen)
00099 {
00100    *destLen = ::MultiByteToWideChar(cp, 0,src, srclen, (LPWSTR)dest, *destLen);
00101    if(*destLen <= 0)
00102       fprintf(stderr, "problem in ::MultiByteToWideChar\n");
00103 }
00104 void msEncode(UINT cp, const PRUnichar *src, 
00105               PRInt32 srcLen, char* dest, PRInt32* destLen)
00106 {
00107    *destLen = ::WideCharToMultiByte(cp, 0, src, srcLen, (LPSTR)dest, *destLen, 
00108                 (LPCSTR)" ", FALSE);
00109    if(*destLen <= 0)
00110       fprintf(stderr, "problem in ::WideCharToMultiByte\n");
00111 }
00112      
00113 int main(int argc, const char** argv)
00114 {
00115    nsresult res;
00116    nsCOMPtr<nsICharsetConverterManager> ccMain =
00117       do_GetService(kCharsetConverterManagerCID, &res);
00118    if(NS_FAILED(res))
00119    {
00120         fprintf(stderr, "Cannot get Character Converter Manager %x\n", res);
00121    }
00122    int i;
00123    if(argc > 5)
00124    {
00125      for(i =0; i < argc; i++)
00126      {
00127        if(strcmp(argv[i], "-f") == 0)
00128        {
00129              tocodeind = i+1;
00130              res = ccMain->GetUnicodeDecoder(argv[tocodeind], &decoder);
00131              if(NS_FAILED(res)) {
00132               fprintf(stderr, "Cannot get Unicode decoder %s %x\n", 
00133                      argv[tocodeind],res);
00134               return -1;
00135             }
00136 
00137        }
00138        if(strcmp(argv[i], "-t") == 0)
00139        {
00140              fromcodeind = i+1;
00141              res = ccMain->GetUnicodeEncoderRaw(argv[fromcodeind], &encoder);
00142              if(NS_FAILED(res)) {
00143               fprintf(stderr, "Cannot get Unicode encoder %s %x\n", 
00144                      argv[fromcodeind],res);
00145               return -1;
00146             }
00147        }
00148      }
00149      if(argc == 6)
00150      {
00151         infile = fopen(argv[5], "rb");
00152         if(NULL == infile) 
00153         {  
00154            usage();
00155            fprintf(stderr,"cannot open file %s\n", argv[5]);
00156            return -1; 
00157         }
00158      }
00159      else
00160      {
00161         infile = stdin;
00162      }
00163     
00164      PRInt32 insize,medsize,outsize;
00165      while((insize=fread(inbuffer, 1,INBUFSIZE,infile)) > 0)
00166      {
00167         medsize=MEDBUFSIZE;
00168         
00169        res = decoder->Convert(inbuffer,&insize, medbuffer, &medsize);
00170         if(NS_FAILED(res)) {
00171             fprintf(stderr, "failed in decoder->Convert %x\n",res);
00172            return -1;
00173        }
00174         outsize = OUTBUFSIZE;
00175        res = encoder->Convert(medbuffer, &medsize, outbuffer,&outsize);
00176         if(NS_FAILED(res)) {
00177             fprintf(stderr, "failed in encoder->Convert %x\n",res);
00178            return -1;
00179        }
00180         fwrite(outbuffer, 1, outsize, stdout);
00181 
00182         memcpyDecode(inbuffer, insize, outbuffer);
00183 
00184         memcpyEncode(inbuffer, insize, outbuffer);
00185 
00186         medsize = MEDBUFSIZE;
00187         msDecode(incp, inbuffer, insize, medbuffer, &medsize);
00188 
00189         outsize = OUTBUFSIZE;
00190         msEncode(outcp, medbuffer, medsize, outbuffer, &outsize);
00191 
00192         medsize = MEDBUFSIZE;
00193         WideDecode( inbuffer, insize, medbuffer, &medsize);
00194 
00195         outsize = OUTBUFSIZE;
00196         NarrowEncode( medbuffer, medsize, outbuffer, &outsize);
00197      }
00198      
00199      fclose(infile);
00200      fclose(stdout);
00201      fprintf(stderr, "Done!\n");
00202      return 0;
00203    }
00204    usage();
00205    return -1;
00206 }