1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 // Utility that converts file encoded in one charset codepage to
11 #include "nsIServiceManager.h"
12 #include "nsICharsetConverterManager.h"
13 #include "nsIUnicodeEncoder.h"
14 #include "nsIUnicodeDecoder.h"
16 static NS_DEFINE_CID(kCharsetConverterManagerCID
, NS_ICHARSETCONVERTERMANAGER_CID
);
24 "nsconv -f fromcode -t tocode infile outfile\n"
25 "nsconv -f fromcode -t tocode infile > outfile\n"
26 "nsconv -f fromcode -t tocode < infile > outfile\n"
30 #define INBUFSIZE (1024*16)
31 #define MEDBUFSIZE (1024*16*2)
32 #define OUTBUFSIZE (1024*16*8)
33 char inbuffer
[INBUFSIZE
];
34 char outbuffer
[OUTBUFSIZE
];
35 char16_t medbuffer
[MEDBUFSIZE
];
37 int main(int argc
, const char** argv
)
39 nsIUnicodeEncoder
* encoder
= nullptr;
40 nsIUnicodeDecoder
* decoder
= nullptr;
47 NS_InitXPCOM2(nullptr, nullptr, nullptr);
50 nsCOMPtr
<nsICharsetConverterManager
> ccMain
=
51 do_GetService(kCharsetConverterManagerCID
, &res
);
54 fprintf(stderr
, "Cannot get Character Converter Manager %x\n", res
);
61 for(i
=0; i
< argc
; i
++)
63 if(strcmp(argv
[i
], "-f") == 0)
65 // User has specified the charset to convert from
68 // First check if a charset alias was given,
69 // and convert to the canonical name
70 res
= ccMain
->GetCharsetAlias(argv
[i
+1], str
);
73 fprintf(stderr
, "Cannot get charset alias for %s %x\n",
78 // Finally create the decoder
79 res
= ccMain
->GetUnicodeDecoder(str
.get(), &decoder
);
81 fprintf(stderr
, "Cannot get Unicode decoder %s %x\n",
88 if(strcmp(argv
[i
], "-t") == 0)
90 // User has specified which charset to convert to
93 // First check if a charset alias was given,
94 // and convert to the canonical name
95 res
= ccMain
->GetCharsetAlias(argv
[i
+1], str
);
98 fprintf(stderr
, "Cannot get charset alias for %s %x\n",
103 // Finally create the encoder
104 res
= ccMain
->GetUnicodeEncoderRaw(str
.get(), &encoder
);
106 fprintf(stderr
, "Cannot get Unicode encoder %s %x\n",
115 // The user has specified an input file
116 // if we have more than four arguments
117 fin
= infile
= fopen(argv
[5], "rb");
121 fprintf(stderr
,"cannot open input file %s\n", argv
[5]);
127 // The user has specified an output file
128 // if we have more than four arguments
129 fout
= outfile
= fopen(argv
[6], "ab");
133 fprintf(stderr
,"cannot open output file %s\n", argv
[6]);
142 // No inputfiles are given. Read and write
143 // to/from standard in and standard out
148 int32_t insize
,medsize
,outsize
;
149 while((insize
=fread(inbuffer
, 1,INBUFSIZE
, fin
)) > 0)
153 res
= decoder
->Convert(inbuffer
,&insize
, medbuffer
, &medsize
);
155 fprintf(stderr
, "failed in decoder->Convert %x\n",res
);
158 outsize
= OUTBUFSIZE
;
159 res
= encoder
->Convert(medbuffer
, &medsize
, outbuffer
,&outsize
);
161 fprintf(stderr
, "failed in encoder->Convert %x\n",res
);
164 fwrite(outbuffer
, 1, outsize
, fout
);
173 fprintf(stderr
, "Done!\n");
174 NS_IF_RELEASE(encoder
);
175 NS_IF_RELEASE(decoder
);
185 NS_IF_RELEASE(encoder
);
186 NS_IF_RELEASE(decoder
);