Fix typo in 9b54bd30006c008b4a951331b273613d5bac3abf
[pm.git] / intl / uconv / tests / nsconv.cpp
blob116f6fc1f316f731f6a9dc165c857bf5cdb983ad
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
7 // another encoding
9 #include "nscore.h"
10 #include "nsString.h"
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);
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 void usage()
23 printf(
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;
41 FILE* fin = 0;
42 FILE* fout = 0;
43 FILE* infile = 0;
44 FILE* outfile = 0;
45 nsresult res= NS_OK;
47 NS_InitXPCOM2(nullptr, nullptr, nullptr);
49 // get ccMain;
50 nsCOMPtr<nsICharsetConverterManager> ccMain =
51 do_GetService(kCharsetConverterManagerCID, &res);
52 if(NS_FAILED(res))
54 fprintf(stderr, "Cannot get Character Converter Manager %x\n", res);
55 return -1;
58 int i;
59 if(argc > 4)
61 for(i =0; i < argc; i++)
63 if(strcmp(argv[i], "-f") == 0)
65 // User has specified the charset to convert from
66 nsAutoCString str;
68 // First check if a charset alias was given,
69 // and convert to the canonical name
70 res = ccMain->GetCharsetAlias(argv[i+1], str);
71 if (NS_FAILED(res))
73 fprintf(stderr, "Cannot get charset alias for %s %x\n",
74 argv[i+1], res);
75 goto error_exit;
78 // Finally create the decoder
79 res = ccMain->GetUnicodeDecoder(str.get(), &decoder);
80 if(NS_FAILED(res)) {
81 fprintf(stderr, "Cannot get Unicode decoder %s %x\n",
82 argv[i+1],res);
83 goto error_exit;
88 if(strcmp(argv[i], "-t") == 0)
90 // User has specified which charset to convert to
91 nsAutoCString str;
93 // First check if a charset alias was given,
94 // and convert to the canonical name
95 res = ccMain->GetCharsetAlias(argv[i+1], str);
96 if (NS_FAILED(res))
98 fprintf(stderr, "Cannot get charset alias for %s %x\n",
99 argv[i+1], res);
100 goto error_exit;
103 // Finally create the encoder
104 res = ccMain->GetUnicodeEncoderRaw(str.get(), &encoder);
105 if(NS_FAILED(res)) {
106 fprintf(stderr, "Cannot get Unicode encoder %s %x\n",
107 argv[i+1],res);
108 goto error_exit;
113 if (argc > 5)
115 // The user has specified an input file
116 // if we have more than four arguments
117 fin = infile = fopen(argv[5], "rb");
118 if(!infile)
120 usage();
121 fprintf(stderr,"cannot open input file %s\n", argv[5]);
122 goto error_exit;
125 if (argc > 6)
127 // The user has specified an output file
128 // if we have more than four arguments
129 fout = outfile = fopen(argv[6], "ab");
130 if(!outfile)
132 usage();
133 fprintf(stderr,"cannot open output file %s\n", argv[6]);
134 goto error_exit;
137 else
138 fout = stdout;
140 else
142 // No inputfiles are given. Read and write
143 // to/from standard in and standard out
144 fin = stdin;
145 fout = stdout;
148 int32_t insize,medsize,outsize;
149 while((insize=fread(inbuffer, 1,INBUFSIZE, fin)) > 0)
151 medsize=MEDBUFSIZE;
153 res = decoder->Convert(inbuffer,&insize, medbuffer, &medsize);
154 if(NS_FAILED(res)) {
155 fprintf(stderr, "failed in decoder->Convert %x\n",res);
156 goto error_exit;
158 outsize = OUTBUFSIZE;
159 res = encoder->Convert(medbuffer, &medsize, outbuffer,&outsize);
160 if(NS_FAILED(res)) {
161 fprintf(stderr, "failed in encoder->Convert %x\n",res);
162 goto error_exit;
164 fwrite(outbuffer, 1, outsize, fout);
168 // Clean up
169 if (infile != 0)
170 fclose(infile);
171 if (outfile != 0)
172 fclose(outfile);
173 fprintf(stderr, "Done!\n");
174 NS_IF_RELEASE(encoder);
175 NS_IF_RELEASE(decoder);
176 return 0;
178 usage();
179 error_exit:
180 // Clean up after
181 if (infile != 0)
182 fclose(infile);
183 if (outfile != 0)
184 fclose(outfile);
185 NS_IF_RELEASE(encoder);
186 NS_IF_RELEASE(decoder);
187 return -1;