intl/uconv/tests/nsconv.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 // Utility that converts file encoded in one charset codepage to
michael@0 7 // another encoding
michael@0 8
michael@0 9 #include "nscore.h"
michael@0 10 #include "nsString.h"
michael@0 11 #include "nsIServiceManager.h"
michael@0 12 #include "nsICharsetConverterManager.h"
michael@0 13 #include "nsIUnicodeEncoder.h"
michael@0 14 #include "nsIUnicodeDecoder.h"
michael@0 15
michael@0 16 static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
michael@0 17
michael@0 18 #include <stdio.h>
michael@0 19 #include <string.h>
michael@0 20 #include <stdlib.h>
michael@0 21 void usage()
michael@0 22 {
michael@0 23 printf(
michael@0 24 "nsconv -f fromcode -t tocode infile outfile\n"
michael@0 25 "nsconv -f fromcode -t tocode infile > outfile\n"
michael@0 26 "nsconv -f fromcode -t tocode < infile > outfile\n"
michael@0 27 );
michael@0 28 }
michael@0 29
michael@0 30 #define INBUFSIZE (1024*16)
michael@0 31 #define MEDBUFSIZE (1024*16*2)
michael@0 32 #define OUTBUFSIZE (1024*16*8)
michael@0 33 char inbuffer[INBUFSIZE];
michael@0 34 char outbuffer[OUTBUFSIZE];
michael@0 35 char16_t medbuffer[MEDBUFSIZE];
michael@0 36
michael@0 37 int main(int argc, const char** argv)
michael@0 38 {
michael@0 39 nsIUnicodeEncoder* encoder = nullptr;
michael@0 40 nsIUnicodeDecoder* decoder = nullptr;
michael@0 41 FILE* fin = 0;
michael@0 42 FILE* fout = 0;
michael@0 43 FILE* infile = 0;
michael@0 44 FILE* outfile = 0;
michael@0 45 nsresult res= NS_OK;
michael@0 46
michael@0 47 NS_InitXPCOM2(nullptr, nullptr, nullptr);
michael@0 48
michael@0 49 // get ccMain;
michael@0 50 nsCOMPtr<nsICharsetConverterManager> ccMain =
michael@0 51 do_GetService(kCharsetConverterManagerCID, &res);
michael@0 52 if(NS_FAILED(res))
michael@0 53 {
michael@0 54 fprintf(stderr, "Cannot get Character Converter Manager %x\n", res);
michael@0 55 return -1;
michael@0 56 }
michael@0 57
michael@0 58 int i;
michael@0 59 if(argc > 4)
michael@0 60 {
michael@0 61 for(i =0; i < argc; i++)
michael@0 62 {
michael@0 63 if(strcmp(argv[i], "-f") == 0)
michael@0 64 {
michael@0 65 // User has specified the charset to convert from
michael@0 66 nsAutoCString str;
michael@0 67
michael@0 68 // First check if a charset alias was given,
michael@0 69 // and convert to the canonical name
michael@0 70 res = ccMain->GetCharsetAlias(argv[i+1], str);
michael@0 71 if (NS_FAILED(res))
michael@0 72 {
michael@0 73 fprintf(stderr, "Cannot get charset alias for %s %x\n",
michael@0 74 argv[i+1], res);
michael@0 75 goto error_exit;
michael@0 76 }
michael@0 77
michael@0 78 // Finally create the decoder
michael@0 79 res = ccMain->GetUnicodeDecoder(str.get(), &decoder);
michael@0 80 if(NS_FAILED(res)) {
michael@0 81 fprintf(stderr, "Cannot get Unicode decoder %s %x\n",
michael@0 82 argv[i+1],res);
michael@0 83 goto error_exit;
michael@0 84 }
michael@0 85
michael@0 86 }
michael@0 87
michael@0 88 if(strcmp(argv[i], "-t") == 0)
michael@0 89 {
michael@0 90 // User has specified which charset to convert to
michael@0 91 nsAutoCString str;
michael@0 92
michael@0 93 // First check if a charset alias was given,
michael@0 94 // and convert to the canonical name
michael@0 95 res = ccMain->GetCharsetAlias(argv[i+1], str);
michael@0 96 if (NS_FAILED(res))
michael@0 97 {
michael@0 98 fprintf(stderr, "Cannot get charset alias for %s %x\n",
michael@0 99 argv[i+1], res);
michael@0 100 goto error_exit;
michael@0 101 }
michael@0 102
michael@0 103 // Finally create the encoder
michael@0 104 res = ccMain->GetUnicodeEncoderRaw(str.get(), &encoder);
michael@0 105 if(NS_FAILED(res)) {
michael@0 106 fprintf(stderr, "Cannot get Unicode encoder %s %x\n",
michael@0 107 argv[i+1],res);
michael@0 108 goto error_exit;
michael@0 109 }
michael@0 110 }
michael@0 111 }
michael@0 112
michael@0 113 if (argc > 5)
michael@0 114 {
michael@0 115 // The user has specified an input file
michael@0 116 // if we have more than four arguments
michael@0 117 fin = infile = fopen(argv[5], "rb");
michael@0 118 if(!infile)
michael@0 119 {
michael@0 120 usage();
michael@0 121 fprintf(stderr,"cannot open input file %s\n", argv[5]);
michael@0 122 goto error_exit;
michael@0 123 }
michael@0 124
michael@0 125 if (argc > 6)
michael@0 126 {
michael@0 127 // The user has specified an output file
michael@0 128 // if we have more than four arguments
michael@0 129 fout = outfile = fopen(argv[6], "ab");
michael@0 130 if(!outfile)
michael@0 131 {
michael@0 132 usage();
michael@0 133 fprintf(stderr,"cannot open output file %s\n", argv[6]);
michael@0 134 goto error_exit;
michael@0 135 }
michael@0 136 }
michael@0 137 else
michael@0 138 fout = stdout;
michael@0 139 }
michael@0 140 else
michael@0 141 {
michael@0 142 // No inputfiles are given. Read and write
michael@0 143 // to/from standard in and standard out
michael@0 144 fin = stdin;
michael@0 145 fout = stdout;
michael@0 146 }
michael@0 147
michael@0 148 int32_t insize,medsize,outsize;
michael@0 149 while((insize=fread(inbuffer, 1,INBUFSIZE, fin)) > 0)
michael@0 150 {
michael@0 151 medsize=MEDBUFSIZE;
michael@0 152
michael@0 153 res = decoder->Convert(inbuffer,&insize, medbuffer, &medsize);
michael@0 154 if(NS_FAILED(res)) {
michael@0 155 fprintf(stderr, "failed in decoder->Convert %x\n",res);
michael@0 156 goto error_exit;
michael@0 157 }
michael@0 158 outsize = OUTBUFSIZE;
michael@0 159 res = encoder->Convert(medbuffer, &medsize, outbuffer,&outsize);
michael@0 160 if(NS_FAILED(res)) {
michael@0 161 fprintf(stderr, "failed in encoder->Convert %x\n",res);
michael@0 162 goto error_exit;
michael@0 163 }
michael@0 164 fwrite(outbuffer, 1, outsize, fout);
michael@0 165
michael@0 166 }
michael@0 167
michael@0 168 // Clean up
michael@0 169 if (infile != 0)
michael@0 170 fclose(infile);
michael@0 171 if (outfile != 0)
michael@0 172 fclose(outfile);
michael@0 173 fprintf(stderr, "Done!\n");
michael@0 174 NS_IF_RELEASE(encoder);
michael@0 175 NS_IF_RELEASE(decoder);
michael@0 176 return 0;
michael@0 177 }
michael@0 178 usage();
michael@0 179 error_exit:
michael@0 180 // Clean up after
michael@0 181 if (infile != 0)
michael@0 182 fclose(infile);
michael@0 183 if (outfile != 0)
michael@0 184 fclose(outfile);
michael@0 185 NS_IF_RELEASE(encoder);
michael@0 186 NS_IF_RELEASE(decoder);
michael@0 187 return -1;
michael@0 188 }

mercurial