1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/uconv/tests/nsconv.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,188 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +// Utility that converts file encoded in one charset codepage to 1.10 +// another encoding 1.11 + 1.12 +#include "nscore.h" 1.13 +#include "nsString.h" 1.14 +#include "nsIServiceManager.h" 1.15 +#include "nsICharsetConverterManager.h" 1.16 +#include "nsIUnicodeEncoder.h" 1.17 +#include "nsIUnicodeDecoder.h" 1.18 + 1.19 +static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); 1.20 + 1.21 +#include <stdio.h> 1.22 +#include <string.h> 1.23 +#include <stdlib.h> 1.24 +void usage() 1.25 +{ 1.26 + printf( 1.27 + "nsconv -f fromcode -t tocode infile outfile\n" 1.28 + "nsconv -f fromcode -t tocode infile > outfile\n" 1.29 + "nsconv -f fromcode -t tocode < infile > outfile\n" 1.30 + ); 1.31 +} 1.32 + 1.33 +#define INBUFSIZE (1024*16) 1.34 +#define MEDBUFSIZE (1024*16*2) 1.35 +#define OUTBUFSIZE (1024*16*8) 1.36 +char inbuffer[INBUFSIZE]; 1.37 +char outbuffer[OUTBUFSIZE]; 1.38 +char16_t medbuffer[MEDBUFSIZE]; 1.39 + 1.40 +int main(int argc, const char** argv) 1.41 +{ 1.42 + nsIUnicodeEncoder* encoder = nullptr; 1.43 + nsIUnicodeDecoder* decoder = nullptr; 1.44 + FILE* fin = 0; 1.45 + FILE* fout = 0; 1.46 + FILE* infile = 0; 1.47 + FILE* outfile = 0; 1.48 + nsresult res= NS_OK; 1.49 + 1.50 + NS_InitXPCOM2(nullptr, nullptr, nullptr); 1.51 + 1.52 + // get ccMain; 1.53 + nsCOMPtr<nsICharsetConverterManager> ccMain = 1.54 + do_GetService(kCharsetConverterManagerCID, &res); 1.55 + if(NS_FAILED(res)) 1.56 + { 1.57 + fprintf(stderr, "Cannot get Character Converter Manager %x\n", res); 1.58 + return -1; 1.59 + } 1.60 + 1.61 + int i; 1.62 + if(argc > 4) 1.63 + { 1.64 + for(i =0; i < argc; i++) 1.65 + { 1.66 + if(strcmp(argv[i], "-f") == 0) 1.67 + { 1.68 + // User has specified the charset to convert from 1.69 + nsAutoCString str; 1.70 + 1.71 + // First check if a charset alias was given, 1.72 + // and convert to the canonical name 1.73 + res = ccMain->GetCharsetAlias(argv[i+1], str); 1.74 + if (NS_FAILED(res)) 1.75 + { 1.76 + fprintf(stderr, "Cannot get charset alias for %s %x\n", 1.77 + argv[i+1], res); 1.78 + goto error_exit; 1.79 + } 1.80 + 1.81 + // Finally create the decoder 1.82 + res = ccMain->GetUnicodeDecoder(str.get(), &decoder); 1.83 + if(NS_FAILED(res)) { 1.84 + fprintf(stderr, "Cannot get Unicode decoder %s %x\n", 1.85 + argv[i+1],res); 1.86 + goto error_exit; 1.87 + } 1.88 + 1.89 + } 1.90 + 1.91 + if(strcmp(argv[i], "-t") == 0) 1.92 + { 1.93 + // User has specified which charset to convert to 1.94 + nsAutoCString str; 1.95 + 1.96 + // First check if a charset alias was given, 1.97 + // and convert to the canonical name 1.98 + res = ccMain->GetCharsetAlias(argv[i+1], str); 1.99 + if (NS_FAILED(res)) 1.100 + { 1.101 + fprintf(stderr, "Cannot get charset alias for %s %x\n", 1.102 + argv[i+1], res); 1.103 + goto error_exit; 1.104 + } 1.105 + 1.106 + // Finally create the encoder 1.107 + res = ccMain->GetUnicodeEncoderRaw(str.get(), &encoder); 1.108 + if(NS_FAILED(res)) { 1.109 + fprintf(stderr, "Cannot get Unicode encoder %s %x\n", 1.110 + argv[i+1],res); 1.111 + goto error_exit; 1.112 + } 1.113 + } 1.114 + } 1.115 + 1.116 + if (argc > 5) 1.117 + { 1.118 + // The user has specified an input file 1.119 + // if we have more than four arguments 1.120 + fin = infile = fopen(argv[5], "rb"); 1.121 + if(!infile) 1.122 + { 1.123 + usage(); 1.124 + fprintf(stderr,"cannot open input file %s\n", argv[5]); 1.125 + goto error_exit; 1.126 + } 1.127 + 1.128 + if (argc > 6) 1.129 + { 1.130 + // The user has specified an output file 1.131 + // if we have more than four arguments 1.132 + fout = outfile = fopen(argv[6], "ab"); 1.133 + if(!outfile) 1.134 + { 1.135 + usage(); 1.136 + fprintf(stderr,"cannot open output file %s\n", argv[6]); 1.137 + goto error_exit; 1.138 + } 1.139 + } 1.140 + else 1.141 + fout = stdout; 1.142 + } 1.143 + else 1.144 + { 1.145 + // No inputfiles are given. Read and write 1.146 + // to/from standard in and standard out 1.147 + fin = stdin; 1.148 + fout = stdout; 1.149 + } 1.150 + 1.151 + int32_t insize,medsize,outsize; 1.152 + while((insize=fread(inbuffer, 1,INBUFSIZE, fin)) > 0) 1.153 + { 1.154 + medsize=MEDBUFSIZE; 1.155 + 1.156 + res = decoder->Convert(inbuffer,&insize, medbuffer, &medsize); 1.157 + if(NS_FAILED(res)) { 1.158 + fprintf(stderr, "failed in decoder->Convert %x\n",res); 1.159 + goto error_exit; 1.160 + } 1.161 + outsize = OUTBUFSIZE; 1.162 + res = encoder->Convert(medbuffer, &medsize, outbuffer,&outsize); 1.163 + if(NS_FAILED(res)) { 1.164 + fprintf(stderr, "failed in encoder->Convert %x\n",res); 1.165 + goto error_exit; 1.166 + } 1.167 + fwrite(outbuffer, 1, outsize, fout); 1.168 + 1.169 + } 1.170 + 1.171 + // Clean up 1.172 + if (infile != 0) 1.173 + fclose(infile); 1.174 + if (outfile != 0) 1.175 + fclose(outfile); 1.176 + fprintf(stderr, "Done!\n"); 1.177 + NS_IF_RELEASE(encoder); 1.178 + NS_IF_RELEASE(decoder); 1.179 + return 0; 1.180 + } 1.181 + usage(); 1.182 + error_exit: 1.183 + // Clean up after 1.184 + if (infile != 0) 1.185 + fclose(infile); 1.186 + if (outfile != 0) 1.187 + fclose(outfile); 1.188 + NS_IF_RELEASE(encoder); 1.189 + NS_IF_RELEASE(decoder); 1.190 + return -1; 1.191 +}