intl/uconv/tests/convperf.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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 #include <windows.h>
michael@0 6 #include <winnls.h>
michael@0 7
michael@0 8 #include "nscore.h"
michael@0 9 #include "nsString.h"
michael@0 10 #include "nsIServiceManager.h"
michael@0 11 #include "nsICharsetConverterManager.h"
michael@0 12 #include "nsIUnicodeEncoder.h"
michael@0 13 #include "nsIUnicodeDecoder.h"
michael@0 14
michael@0 15 #include <stdio.h>
michael@0 16 #include <string.h>
michael@0 17 #include <stdlib.h>
michael@0 18 void usage()
michael@0 19 {
michael@0 20 printf(
michael@0 21 "convperf -f fromcode -t tocode [file]\n"
michael@0 22 );
michael@0 23 }
michael@0 24 int fromcodeind = 0;
michael@0 25 int tocodeind = 0;
michael@0 26 FILE* infile = 0;
michael@0 27 #define INBUFSIZE (1024*16)
michael@0 28 #define MEDBUFSIZE (1024*16*2)
michael@0 29 #define OUTBUFSIZE (1024*16*8)
michael@0 30 char inbuffer[INBUFSIZE];
michael@0 31 char outbuffer[OUTBUFSIZE];
michael@0 32 char16_t medbuffer[MEDBUFSIZE];
michael@0 33 nsIUnicodeEncoder* encoder = nullptr;
michael@0 34 nsIUnicodeDecoder* decoder = nullptr;
michael@0 35 UINT incp = 932;
michael@0 36 UINT outcp = 932;
michael@0 37
michael@0 38 void memcpyDecode(const char* src, int32_t srclen, char* dest)
michael@0 39 {
michael@0 40 ::memcpy(dest, src, srclen);
michael@0 41 }
michael@0 42 void memcpyEncode(const char* src, int32_t srclen, char* dest)
michael@0 43 {
michael@0 44 ::memcpy(dest, src, srclen);
michael@0 45 }
michael@0 46
michael@0 47 void WideDecode(const char* src,
michael@0 48 int32_t srclen, char16_t *dest, int32_t *destLen)
michael@0 49 {
michael@0 50 const char* end = src+srclen ;
michael@0 51 while(src < end)
michael@0 52 *dest++ = (char16_t) *src++;
michael@0 53 *destLen = srclen;
michael@0 54 }
michael@0 55 void NarrowEncode(const char16_t *src,
michael@0 56 int32_t srclen, char* dest, int32_t* destLen)
michael@0 57 {
michael@0 58 const char16_t* end = src+srclen ;
michael@0 59 while(src < end)
michael@0 60 *dest++ = (char) *src++;
michael@0 61 *destLen = srclen;
michael@0 62 }
michael@0 63 void msDecode(UINT cp, const char* src,
michael@0 64 int32_t srclen, char16_t *dest, int32_t *destLen)
michael@0 65 {
michael@0 66 *destLen = ::MultiByteToWideChar(cp, 0,src, srclen, (LPWSTR)dest, *destLen);
michael@0 67 if(*destLen <= 0)
michael@0 68 fprintf(stderr, "problem in ::MultiByteToWideChar\n");
michael@0 69 }
michael@0 70 void msEncode(UINT cp, const char16_t *src,
michael@0 71 int32_t srcLen, char* dest, int32_t* destLen)
michael@0 72 {
michael@0 73 *destLen = ::WideCharToMultiByte(cp, 0, src, srcLen, (LPSTR)dest, *destLen,
michael@0 74 (LPCSTR)" ", FALSE);
michael@0 75 if(*destLen <= 0)
michael@0 76 fprintf(stderr, "problem in ::WideCharToMultiByte\n");
michael@0 77 }
michael@0 78
michael@0 79 int main(int argc, const char** argv)
michael@0 80 {
michael@0 81 nsresult res;
michael@0 82 nsCOMPtr<nsICharsetConverterManager> ccMain =
michael@0 83 do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
michael@0 84 if(NS_FAILED(res))
michael@0 85 {
michael@0 86 fprintf(stderr, "Cannot get Character Converter Manager %x\n", res);
michael@0 87 }
michael@0 88 int i;
michael@0 89 if(argc > 5)
michael@0 90 {
michael@0 91 for(i =0; i < argc; i++)
michael@0 92 {
michael@0 93 if(strcmp(argv[i], "-f") == 0)
michael@0 94 {
michael@0 95 tocodeind = i+1;
michael@0 96 res = ccMain->GetUnicodeDecoder(argv[tocodeind], &decoder);
michael@0 97 if(NS_FAILED(res)) {
michael@0 98 fprintf(stderr, "Cannot get Unicode decoder %s %x\n",
michael@0 99 argv[tocodeind],res);
michael@0 100 return -1;
michael@0 101 }
michael@0 102
michael@0 103 }
michael@0 104 if(strcmp(argv[i], "-t") == 0)
michael@0 105 {
michael@0 106 fromcodeind = i+1;
michael@0 107 res = ccMain->GetUnicodeEncoderRaw(argv[fromcodeind], &encoder);
michael@0 108 if(NS_FAILED(res)) {
michael@0 109 fprintf(stderr, "Cannot get Unicode encoder %s %x\n",
michael@0 110 argv[fromcodeind],res);
michael@0 111 return -1;
michael@0 112 }
michael@0 113 }
michael@0 114 }
michael@0 115 if(argc == 6)
michael@0 116 {
michael@0 117 infile = fopen(argv[5], "rb");
michael@0 118 if (!infile)
michael@0 119 {
michael@0 120 usage();
michael@0 121 fprintf(stderr,"cannot open file %s\n", argv[5]);
michael@0 122 return -1;
michael@0 123 }
michael@0 124 }
michael@0 125 else
michael@0 126 {
michael@0 127 infile = stdin;
michael@0 128 }
michael@0 129
michael@0 130 int32_t insize,medsize,outsize;
michael@0 131 while((insize=fread(inbuffer, 1,INBUFSIZE,infile)) > 0)
michael@0 132 {
michael@0 133 medsize=MEDBUFSIZE;
michael@0 134
michael@0 135 res = decoder->Convert(inbuffer,&insize, medbuffer, &medsize);
michael@0 136 if(NS_FAILED(res)) {
michael@0 137 fprintf(stderr, "failed in decoder->Convert %x\n",res);
michael@0 138 return -1;
michael@0 139 }
michael@0 140 outsize = OUTBUFSIZE;
michael@0 141 res = encoder->Convert(medbuffer, &medsize, outbuffer,&outsize);
michael@0 142 if(NS_FAILED(res)) {
michael@0 143 fprintf(stderr, "failed in encoder->Convert %x\n",res);
michael@0 144 return -1;
michael@0 145 }
michael@0 146 fwrite(outbuffer, 1, outsize, stdout);
michael@0 147
michael@0 148 memcpyDecode(inbuffer, insize, outbuffer);
michael@0 149
michael@0 150 memcpyEncode(inbuffer, insize, outbuffer);
michael@0 151
michael@0 152 medsize = MEDBUFSIZE;
michael@0 153 msDecode(incp, inbuffer, insize, medbuffer, &medsize);
michael@0 154
michael@0 155 outsize = OUTBUFSIZE;
michael@0 156 msEncode(outcp, medbuffer, medsize, outbuffer, &outsize);
michael@0 157
michael@0 158 medsize = MEDBUFSIZE;
michael@0 159 WideDecode( inbuffer, insize, medbuffer, &medsize);
michael@0 160
michael@0 161 outsize = OUTBUFSIZE;
michael@0 162 NarrowEncode( medbuffer, medsize, outbuffer, &outsize);
michael@0 163 }
michael@0 164
michael@0 165 fclose(infile);
michael@0 166 fclose(stdout);
michael@0 167 fprintf(stderr, "Done!\n");
michael@0 168 return 0;
michael@0 169 }
michael@0 170 usage();
michael@0 171 return -1;
michael@0 172 }

mercurial