2001-09-26 04:40:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-08-04 01:05:59 +04:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1999-08-04 01:05:59 +04:00
|
|
|
*
|
2001-09-26 04:40:45 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-08-04 01:05:59 +04:00
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-26 04:40:45 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-26 04:40:45 +04:00
|
|
|
* Contributor(s):
|
2000-02-03 01:24:56 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2001-09-26 04:40:45 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-26 04:40:45 +04:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-26 04:40:45 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-26 04:40:45 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-08-04 01:05:59 +04:00
|
|
|
|
1999-11-02 10:01:44 +03:00
|
|
|
// Utility that converts file encoded in one charset codepage to
|
|
|
|
// another encoding
|
|
|
|
|
1999-08-04 01:05:59 +04:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
1999-11-02 10:01:44 +03:00
|
|
|
|
|
|
|
#include "nsICharsetAlias.h"
|
|
|
|
|
1999-08-04 01:05:59 +04:00
|
|
|
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
void usage()
|
|
|
|
{
|
|
|
|
printf(
|
1999-11-02 10:01:44 +03:00
|
|
|
"nsconv -f fromcode -t tocode infile outfile\n"
|
|
|
|
"nsconv -f fromcode -t tocode infile > outfile\n"
|
|
|
|
"nsconv -f fromcode -t tocode < infile > outfile\n"
|
|
|
|
);
|
1999-08-04 01:05:59 +04:00
|
|
|
}
|
1999-11-02 10:01:44 +03:00
|
|
|
|
1999-08-04 01:05:59 +04:00
|
|
|
#define INBUFSIZE (1024*16)
|
|
|
|
#define MEDBUFSIZE (1024*16*2)
|
|
|
|
#define OUTBUFSIZE (1024*16*8)
|
|
|
|
char inbuffer[INBUFSIZE];
|
|
|
|
char outbuffer[OUTBUFSIZE];
|
|
|
|
PRUnichar medbuffer[MEDBUFSIZE];
|
|
|
|
|
|
|
|
int main(int argc, const char** argv)
|
|
|
|
{
|
1999-11-02 10:01:44 +03:00
|
|
|
nsIUnicodeEncoder* encoder = nsnull;
|
|
|
|
nsIUnicodeDecoder* decoder = nsnull;
|
|
|
|
FILE* fin = 0;
|
|
|
|
FILE* fout = 0;
|
|
|
|
FILE* infile = 0;
|
|
|
|
FILE* outfile = 0;
|
|
|
|
nsresult res= NS_OK;
|
|
|
|
|
2002-06-29 03:16:19 +04:00
|
|
|
NS_InitXPCOM2(nsnull, nsnull, nsnull);
|
|
|
|
|
1999-11-02 10:01:44 +03:00
|
|
|
// get ccMain;
|
2004-11-08 02:59:35 +03:00
|
|
|
nsCOMPtr<nsICharsetConverterManager> ccMain =
|
|
|
|
do_GetService(kCharsetConverterManagerCID, &res);
|
1999-11-02 10:01:44 +03:00
|
|
|
if(NS_FAILED(res))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Cannot get Character Converter Manager %x\n", res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the charset alias manager
|
2004-11-08 02:59:35 +03:00
|
|
|
nsCOMPtr<nsICharsetAlias> aliasmgr =
|
|
|
|
do_GetService(NS_CHARSETALIAS_CONTRACTID, &res);
|
1999-11-02 10:01:44 +03:00
|
|
|
if (NS_FAILED(res))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Cannot get Charset Alias Manager %x\n", res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i;
|
|
|
|
if(argc > 4)
|
|
|
|
{
|
|
|
|
for(i =0; i < argc; i++)
|
|
|
|
{
|
|
|
|
if(strcmp(argv[i], "-f") == 0)
|
|
|
|
{
|
|
|
|
// User has specified the charset to convert from
|
2003-06-11 22:16:03 +04:00
|
|
|
nsCAutoString str;
|
1999-11-02 10:01:44 +03:00
|
|
|
|
|
|
|
// First check if a charset alias was given,
|
|
|
|
// and convert to the canonical name
|
2003-06-11 22:16:03 +04:00
|
|
|
res = aliasmgr->GetPreferred(nsDependentCString(argv[i+1]), str);
|
1999-11-02 10:01:44 +03:00
|
|
|
if (NS_FAILED(res))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Cannot get charset alias for %s %x\n",
|
|
|
|
argv[i+1], res);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally create the decoder
|
2003-06-11 22:16:03 +04:00
|
|
|
res = ccMain->GetUnicodeDecoder(str.get(), &decoder);
|
1999-11-02 10:01:44 +03:00
|
|
|
if(NS_FAILED(res)) {
|
|
|
|
fprintf(stderr, "Cannot get Unicode decoder %s %x\n",
|
|
|
|
argv[i+1],res);
|
|
|
|
goto error_exit;
|
1999-08-04 01:05:59 +04:00
|
|
|
}
|
1999-11-02 10:01:44 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strcmp(argv[i], "-t") == 0)
|
|
|
|
{
|
|
|
|
// User has specified which charset to convert to
|
2003-06-11 22:16:03 +04:00
|
|
|
nsCAutoString str;
|
1999-11-02 10:01:44 +03:00
|
|
|
|
|
|
|
// First check if a charset alias was given,
|
|
|
|
// and convert to the canonical name
|
2003-06-11 22:16:03 +04:00
|
|
|
res = aliasmgr->GetPreferred(nsDependentCString(argv[i+1]), str);
|
1999-11-02 10:01:44 +03:00
|
|
|
if (NS_FAILED(res))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Cannot get charset alias for %s %x\n",
|
|
|
|
argv[i+1], res);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally create the encoder
|
2003-06-11 22:16:03 +04:00
|
|
|
res = ccMain->GetUnicodeEncoderRaw(str.get(), &encoder);
|
1999-11-02 10:01:44 +03:00
|
|
|
if(NS_FAILED(res)) {
|
|
|
|
fprintf(stderr, "Cannot get Unicode encoder %s %x\n",
|
|
|
|
argv[i+1],res);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 5)
|
|
|
|
{
|
|
|
|
// The user has specified an input file
|
|
|
|
// if we have more than four arguments
|
|
|
|
fin = infile = fopen(argv[5], "rb");
|
|
|
|
if(NULL == infile)
|
|
|
|
{
|
|
|
|
usage();
|
|
|
|
fprintf(stderr,"cannot open input file %s\n", argv[5]);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 6)
|
|
|
|
{
|
|
|
|
// The user has specified an output file
|
|
|
|
// if we have more than four arguments
|
|
|
|
fout = outfile = fopen(argv[6], "ab");
|
1999-09-10 23:02:25 +04:00
|
|
|
if(NULL == outfile)
|
|
|
|
{
|
1999-11-02 10:01:44 +03:00
|
|
|
usage();
|
|
|
|
fprintf(stderr,"cannot open output file %s\n", argv[6]);
|
|
|
|
goto error_exit;
|
1999-09-10 23:02:25 +04:00
|
|
|
}
|
1999-11-02 10:01:44 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
fout = stdout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No inputfiles are given. Read and write
|
|
|
|
// to/from standard in and standard out
|
|
|
|
fin = stdin;
|
|
|
|
fout = stdout;
|
|
|
|
}
|
1999-08-04 01:05:59 +04:00
|
|
|
|
1999-11-02 10:01:44 +03:00
|
|
|
PRInt32 insize,medsize,outsize;
|
|
|
|
while((insize=fread(inbuffer, 1,INBUFSIZE, fin)) > 0)
|
|
|
|
{
|
|
|
|
medsize=MEDBUFSIZE;
|
1999-08-04 01:05:59 +04:00
|
|
|
|
1999-11-02 10:01:44 +03:00
|
|
|
res = decoder->Convert(inbuffer,&insize, medbuffer, &medsize);
|
|
|
|
if(NS_FAILED(res)) {
|
|
|
|
fprintf(stderr, "failed in decoder->Convert %x\n",res);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
outsize = OUTBUFSIZE;
|
|
|
|
res = encoder->Convert(medbuffer, &medsize, outbuffer,&outsize);
|
|
|
|
if(NS_FAILED(res)) {
|
|
|
|
fprintf(stderr, "failed in encoder->Convert %x\n",res);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
fwrite(outbuffer, 1, outsize, fout);
|
1999-08-04 01:05:59 +04:00
|
|
|
|
1999-11-02 10:01:44 +03:00
|
|
|
}
|
1999-08-04 01:05:59 +04:00
|
|
|
|
1999-11-02 10:01:44 +03:00
|
|
|
// Clean up
|
|
|
|
if (infile != 0)
|
|
|
|
fclose(infile);
|
|
|
|
if (outfile != 0)
|
|
|
|
fclose(outfile);
|
|
|
|
fprintf(stderr, "Done!\n");
|
|
|
|
NS_IF_RELEASE(encoder);
|
|
|
|
NS_IF_RELEASE(decoder);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
usage();
|
|
|
|
error_exit:
|
|
|
|
// Clean up after
|
|
|
|
if (infile != 0)
|
|
|
|
fclose(infile);
|
|
|
|
if (outfile != 0)
|
|
|
|
fclose(outfile);
|
|
|
|
NS_IF_RELEASE(encoder);
|
|
|
|
NS_IF_RELEASE(decoder);
|
|
|
|
return -1;
|
1999-08-04 01:05:59 +04:00
|
|
|
}
|