Bug 454376 add -lCrun -lCstd for Solaris OS_LIBS, r=bsmedberg
[wine-gecko.git] / intl / uconv / tests / nsconv.cpp
bloba423892648d5adf519c4005597fa979e473a30a7
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
13 * License.
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.
22 * Contributor(s):
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
40 // another encoding
42 #include "nscore.h"
43 #include "nsString.h"
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);
53 #include <stdio.h>
54 #include <string.h>
55 #include <stdlib.h>
56 void usage()
58 printf(
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;
76 FILE* fin = 0;
77 FILE* fout = 0;
78 FILE* infile = 0;
79 FILE* outfile = 0;
80 nsresult res= NS_OK;
82 NS_InitXPCOM2(nsnull, nsnull, nsnull);
84 // get ccMain;
85 nsCOMPtr<nsICharsetConverterManager> ccMain =
86 do_GetService(kCharsetConverterManagerCID, &res);
87 if(NS_FAILED(res))
89 fprintf(stderr, "Cannot get Character Converter Manager %x\n", res);
90 return -1;
93 // Get the charset alias manager
94 nsCOMPtr<nsICharsetAlias> aliasmgr =
95 do_GetService(NS_CHARSETALIAS_CONTRACTID, &res);
96 if (NS_FAILED(res))
98 fprintf(stderr, "Cannot get Charset Alias Manager %x\n", res);
99 return -1;
102 int i;
103 if(argc > 4)
105 for(i =0; i < argc; i++)
107 if(strcmp(argv[i], "-f") == 0)
109 // User has specified the charset to convert from
110 nsCAutoString str;
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);
115 if (NS_FAILED(res))
117 fprintf(stderr, "Cannot get charset alias for %s %x\n",
118 argv[i+1], res);
119 goto error_exit;
122 // Finally create the decoder
123 res = ccMain->GetUnicodeDecoder(str.get(), &decoder);
124 if(NS_FAILED(res)) {
125 fprintf(stderr, "Cannot get Unicode decoder %s %x\n",
126 argv[i+1],res);
127 goto error_exit;
132 if(strcmp(argv[i], "-t") == 0)
134 // User has specified which charset to convert to
135 nsCAutoString str;
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);
140 if (NS_FAILED(res))
142 fprintf(stderr, "Cannot get charset alias for %s %x\n",
143 argv[i+1], res);
144 goto error_exit;
147 // Finally create the encoder
148 res = ccMain->GetUnicodeEncoderRaw(str.get(), &encoder);
149 if(NS_FAILED(res)) {
150 fprintf(stderr, "Cannot get Unicode encoder %s %x\n",
151 argv[i+1],res);
152 goto error_exit;
157 if (argc > 5)
159 // The user has specified an input file
160 // if we have more than four arguments
161 fin = infile = fopen(argv[5], "rb");
162 if(NULL == infile)
164 usage();
165 fprintf(stderr,"cannot open input file %s\n", argv[5]);
166 goto error_exit;
169 if (argc > 6)
171 // The user has specified an output file
172 // if we have more than four arguments
173 fout = outfile = fopen(argv[6], "ab");
174 if(NULL == outfile)
176 usage();
177 fprintf(stderr,"cannot open output file %s\n", argv[6]);
178 goto error_exit;
181 else
182 fout = stdout;
184 else
186 // No inputfiles are given. Read and write
187 // to/from standard in and standard out
188 fin = stdin;
189 fout = stdout;
192 PRInt32 insize,medsize,outsize;
193 while((insize=fread(inbuffer, 1,INBUFSIZE, fin)) > 0)
195 medsize=MEDBUFSIZE;
197 res = decoder->Convert(inbuffer,&insize, medbuffer, &medsize);
198 if(NS_FAILED(res)) {
199 fprintf(stderr, "failed in decoder->Convert %x\n",res);
200 goto error_exit;
202 outsize = OUTBUFSIZE;
203 res = encoder->Convert(medbuffer, &medsize, outbuffer,&outsize);
204 if(NS_FAILED(res)) {
205 fprintf(stderr, "failed in encoder->Convert %x\n",res);
206 goto error_exit;
208 fwrite(outbuffer, 1, outsize, fout);
212 // Clean up
213 if (infile != 0)
214 fclose(infile);
215 if (outfile != 0)
216 fclose(outfile);
217 fprintf(stderr, "Done!\n");
218 NS_IF_RELEASE(encoder);
219 NS_IF_RELEASE(decoder);
220 return 0;
222 usage();
223 error_exit:
224 // Clean up after
225 if (infile != 0)
226 fclose(infile);
227 if (outfile != 0)
228 fclose(outfile);
229 NS_IF_RELEASE(encoder);
230 NS_IF_RELEASE(decoder);
231 return -1;