Back to index

lightning-sunbird  0.9+nobinonly
nsconv.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  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 // Utility that converts file encoded in one charset codepage to
00040 // another encoding
00041 
00042 #include "nscore.h"
00043 #include "nsString.h"
00044 #include "nsIServiceManager.h"
00045 #include "nsICharsetConverterManager.h"
00046 #include "nsIUnicodeEncoder.h"
00047 #include "nsIUnicodeDecoder.h"
00048 
00049 #include "nsICharsetAlias.h"
00050 
00051 static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
00052 
00053 #include <stdio.h>
00054 #include <string.h>
00055 #include <stdlib.h>
00056 void usage()
00057 {
00058   printf(
00059     "nsconv -f fromcode -t tocode infile outfile\n"
00060     "nsconv -f fromcode -t tocode infile > outfile\n"
00061     "nsconv -f fromcode -t tocode < infile > outfile\n"
00062     );
00063 }
00064 
00065 #define INBUFSIZE (1024*16)
00066 #define MEDBUFSIZE (1024*16*2)
00067 #define OUTBUFSIZE (1024*16*8)
00068 char inbuffer[INBUFSIZE];
00069 char outbuffer[OUTBUFSIZE];
00070 PRUnichar  medbuffer[MEDBUFSIZE];
00071 
00072 int main(int argc, const char** argv)
00073 {
00074   nsIUnicodeEncoder* encoder = nsnull;
00075   nsIUnicodeDecoder* decoder = nsnull;
00076   FILE* fin = 0;
00077   FILE* fout = 0;
00078   FILE* infile = 0;
00079   FILE* outfile = 0;
00080   nsresult res= NS_OK;
00081 
00082   NS_InitXPCOM2(nsnull, nsnull, nsnull);
00083 
00084   // get ccMain;
00085   nsCOMPtr<nsICharsetConverterManager> ccMain =
00086       do_GetService(kCharsetConverterManagerCID, &res);
00087   if(NS_FAILED(res))
00088   {
00089     fprintf(stderr, "Cannot get Character Converter Manager %x\n", res);
00090     return -1;
00091   }
00092 
00093   // Get the charset alias manager
00094   nsCOMPtr<nsICharsetAlias> aliasmgr =
00095       do_GetService(NS_CHARSETALIAS_CONTRACTID, &res);
00096   if (NS_FAILED(res))
00097   {
00098     fprintf(stderr, "Cannot get Charset Alias Manager %x\n", res);
00099     return -1;
00100   }
00101 
00102   int i;
00103   if(argc > 4)
00104   {
00105     for(i =0; i < argc; i++)
00106     {
00107       if(strcmp(argv[i], "-f") == 0)
00108       {
00109         // User has specified the charset to convert from
00110         nsCAutoString str;
00111 
00112         // First check if a charset alias was given, 
00113         // and convert to the canonical name
00114         res = aliasmgr->GetPreferred(nsDependentCString(argv[i+1]), str);
00115         if (NS_FAILED(res))
00116         {
00117           fprintf(stderr, "Cannot get charset alias for %s %x\n",
00118                   argv[i+1], res);
00119           goto error_exit;
00120         }
00121 
00122         // Finally create the decoder
00123         res = ccMain->GetUnicodeDecoder(str.get(), &decoder);
00124         if(NS_FAILED(res)) {
00125           fprintf(stderr, "Cannot get Unicode decoder %s %x\n", 
00126                   argv[i+1],res);
00127           goto error_exit;
00128         }
00129 
00130       }
00131 
00132       if(strcmp(argv[i], "-t") == 0)
00133       {
00134         // User has specified which charset to convert to
00135         nsCAutoString str;
00136 
00137         // First check if a charset alias was given, 
00138         // and convert to the canonical name
00139         res = aliasmgr->GetPreferred(nsDependentCString(argv[i+1]), str);
00140         if (NS_FAILED(res))
00141         {
00142           fprintf(stderr, "Cannot get charset alias for %s %x\n",
00143                   argv[i+1], res);
00144           goto error_exit;
00145         }
00146 
00147         // Finally create the encoder 
00148         res = ccMain->GetUnicodeEncoderRaw(str.get(), &encoder);
00149         if(NS_FAILED(res)) {
00150           fprintf(stderr, "Cannot get Unicode encoder %s %x\n", 
00151                   argv[i+1],res);
00152           goto error_exit;
00153         }
00154       }
00155     }
00156 
00157     if (argc > 5)
00158     {
00159       // The user has specified an input file 
00160       // if we have more than four arguments
00161       fin = infile = fopen(argv[5], "rb");
00162       if(NULL == infile) 
00163       {  
00164         usage();
00165         fprintf(stderr,"cannot open input file %s\n", argv[5]);
00166         goto error_exit; 
00167       }
00168 
00169       if (argc > 6)
00170       {
00171         // The user has specified an output file
00172         // if we have more than four arguments
00173         fout = outfile = fopen(argv[6], "ab");
00174         if(NULL == outfile) 
00175         {  
00176           usage();
00177           fprintf(stderr,"cannot open output file %s\n", argv[6]);
00178           goto error_exit; 
00179         }
00180       }
00181       else
00182         fout = stdout;
00183     }
00184     else
00185     {
00186       // No inputfiles are given. Read and write
00187       // to/from standard in and standard out
00188       fin = stdin;
00189       fout = stdout;
00190     }
00191     
00192     PRInt32 insize,medsize,outsize;
00193     while((insize=fread(inbuffer, 1,INBUFSIZE, fin)) > 0)
00194     {
00195       medsize=MEDBUFSIZE;
00196         
00197       res = decoder->Convert(inbuffer,&insize, medbuffer, &medsize);
00198       if(NS_FAILED(res)) {
00199         fprintf(stderr, "failed in decoder->Convert %x\n",res);
00200         goto error_exit;
00201       }
00202       outsize = OUTBUFSIZE;
00203       res = encoder->Convert(medbuffer, &medsize, outbuffer,&outsize);
00204       if(NS_FAILED(res)) {
00205         fprintf(stderr, "failed in encoder->Convert %x\n",res);
00206         goto error_exit;
00207       }
00208       fwrite(outbuffer, 1, outsize, fout);
00209 
00210     }
00211      
00212     // Clean up 
00213     if (infile != 0)
00214       fclose(infile);
00215     if (outfile != 0)
00216       fclose(outfile);
00217     fprintf(stderr, "Done!\n");
00218     NS_IF_RELEASE(encoder);
00219     NS_IF_RELEASE(decoder);
00220     return 0;
00221   }
00222   usage();
00223   error_exit:
00224   // Clean up after
00225   if (infile != 0)
00226     fclose(infile);
00227   if (outfile != 0)
00228     fclose(outfile);
00229   NS_IF_RELEASE(encoder);
00230   NS_IF_RELEASE(decoder);
00231   return -1;
00232 }