1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 // Utility that converts file encoded in one charset codepage to
44 #include "nsIServiceManager.h"
45 #include "nsICharsetConverterManager.h"
46 #include "nsIUnicodeEncoder.h"
47 #include "nsIUnicodeDecoder.h"
49 #include "nsICharsetAlias.h"
51 static NS_DEFINE_CID(kCharsetConverterManagerCID
, NS_ICHARSETCONVERTERMANAGER_CID
);
59 "nsconv -f fromcode -t tocode infile outfile\n"
60 "nsconv -f fromcode -t tocode infile > outfile\n"
61 "nsconv -f fromcode -t tocode < infile > outfile\n"
65 #define INBUFSIZE (1024*16)
66 #define MEDBUFSIZE (1024*16*2)
67 #define OUTBUFSIZE (1024*16*8)
68 char inbuffer
[INBUFSIZE
];
69 char outbuffer
[OUTBUFSIZE
];
70 PRUnichar medbuffer
[MEDBUFSIZE
];
72 int main(int argc
, const char** argv
)
74 nsIUnicodeEncoder
* encoder
= nsnull
;
75 nsIUnicodeDecoder
* decoder
= nsnull
;
82 NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
85 nsCOMPtr
<nsICharsetConverterManager
> ccMain
=
86 do_GetService(kCharsetConverterManagerCID
, &res
);
89 fprintf(stderr
, "Cannot get Character Converter Manager %x\n", res
);
93 // Get the charset alias manager
94 nsCOMPtr
<nsICharsetAlias
> aliasmgr
=
95 do_GetService(NS_CHARSETALIAS_CONTRACTID
, &res
);
98 fprintf(stderr
, "Cannot get Charset Alias Manager %x\n", res
);
105 for(i
=0; i
< argc
; i
++)
107 if(strcmp(argv
[i
], "-f") == 0)
109 // User has specified the charset to convert from
112 // First check if a charset alias was given,
113 // and convert to the canonical name
114 res
= aliasmgr
->GetPreferred(nsDependentCString(argv
[i
+1]), str
);
117 fprintf(stderr
, "Cannot get charset alias for %s %x\n",
122 // Finally create the decoder
123 res
= ccMain
->GetUnicodeDecoder(str
.get(), &decoder
);
125 fprintf(stderr
, "Cannot get Unicode decoder %s %x\n",
132 if(strcmp(argv
[i
], "-t") == 0)
134 // User has specified which charset to convert to
137 // First check if a charset alias was given,
138 // and convert to the canonical name
139 res
= aliasmgr
->GetPreferred(nsDependentCString(argv
[i
+1]), str
);
142 fprintf(stderr
, "Cannot get charset alias for %s %x\n",
147 // Finally create the encoder
148 res
= ccMain
->GetUnicodeEncoderRaw(str
.get(), &encoder
);
150 fprintf(stderr
, "Cannot get Unicode encoder %s %x\n",
159 // The user has specified an input file
160 // if we have more than four arguments
161 fin
= infile
= fopen(argv
[5], "rb");
165 fprintf(stderr
,"cannot open input file %s\n", argv
[5]);
171 // The user has specified an output file
172 // if we have more than four arguments
173 fout
= outfile
= fopen(argv
[6], "ab");
177 fprintf(stderr
,"cannot open output file %s\n", argv
[6]);
186 // No inputfiles are given. Read and write
187 // to/from standard in and standard out
192 PRInt32 insize
,medsize
,outsize
;
193 while((insize
=fread(inbuffer
, 1,INBUFSIZE
, fin
)) > 0)
197 res
= decoder
->Convert(inbuffer
,&insize
, medbuffer
, &medsize
);
199 fprintf(stderr
, "failed in decoder->Convert %x\n",res
);
202 outsize
= OUTBUFSIZE
;
203 res
= encoder
->Convert(medbuffer
, &medsize
, outbuffer
,&outsize
);
205 fprintf(stderr
, "failed in encoder->Convert %x\n",res
);
208 fwrite(outbuffer
, 1, outsize
, fout
);
217 fprintf(stderr
, "Done!\n");
218 NS_IF_RELEASE(encoder
);
219 NS_IF_RELEASE(decoder
);
229 NS_IF_RELEASE(encoder
);
230 NS_IF_RELEASE(decoder
);