2001-09-26 04:40:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-08-04 01:05:59 +04:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winnls.h>
|
|
|
|
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
void usage()
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"convperf -f fromcode -t tocode [file]\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
int fromcodeind = 0;
|
|
|
|
int tocodeind = 0;
|
|
|
|
FILE* infile = 0;
|
|
|
|
#define INBUFSIZE (1024*16)
|
|
|
|
#define MEDBUFSIZE (1024*16*2)
|
|
|
|
#define OUTBUFSIZE (1024*16*8)
|
|
|
|
char inbuffer[INBUFSIZE];
|
|
|
|
char outbuffer[OUTBUFSIZE];
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t medbuffer[MEDBUFSIZE];
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIUnicodeEncoder* encoder = nullptr;
|
|
|
|
nsIUnicodeDecoder* decoder = nullptr;
|
1999-08-04 01:05:59 +04:00
|
|
|
UINT incp = 932;
|
|
|
|
UINT outcp = 932;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void memcpyDecode(const char* src, int32_t srclen, char* dest)
|
1999-08-04 01:05:59 +04:00
|
|
|
{
|
|
|
|
::memcpy(dest, src, srclen);
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
void memcpyEncode(const char* src, int32_t srclen, char* dest)
|
1999-08-04 01:05:59 +04:00
|
|
|
{
|
|
|
|
::memcpy(dest, src, srclen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WideDecode(const char* src,
|
2014-01-04 19:02:17 +04:00
|
|
|
int32_t srclen, char16_t *dest, int32_t *destLen)
|
1999-08-04 01:05:59 +04:00
|
|
|
{
|
|
|
|
const char* end = src+srclen ;
|
|
|
|
while(src < end)
|
2014-01-04 19:02:17 +04:00
|
|
|
*dest++ = (char16_t) *src++;
|
1999-08-04 01:05:59 +04:00
|
|
|
*destLen = srclen;
|
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
void NarrowEncode(const char16_t *src,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t srclen, char* dest, int32_t* destLen)
|
1999-08-04 01:05:59 +04:00
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* end = src+srclen ;
|
1999-08-04 01:05:59 +04:00
|
|
|
while(src < end)
|
|
|
|
*dest++ = (char) *src++;
|
|
|
|
*destLen = srclen;
|
|
|
|
}
|
|
|
|
void msDecode(UINT cp, const char* src,
|
2014-01-04 19:02:17 +04:00
|
|
|
int32_t srclen, char16_t *dest, int32_t *destLen)
|
1999-08-04 01:05:59 +04:00
|
|
|
{
|
|
|
|
*destLen = ::MultiByteToWideChar(cp, 0,src, srclen, (LPWSTR)dest, *destLen);
|
|
|
|
if(*destLen <= 0)
|
|
|
|
fprintf(stderr, "problem in ::MultiByteToWideChar\n");
|
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
void msEncode(UINT cp, const char16_t *src,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t srcLen, char* dest, int32_t* destLen)
|
1999-08-04 01:05:59 +04:00
|
|
|
{
|
|
|
|
*destLen = ::WideCharToMultiByte(cp, 0, src, srcLen, (LPSTR)dest, *destLen,
|
|
|
|
(LPCSTR)" ", FALSE);
|
|
|
|
if(*destLen <= 0)
|
|
|
|
fprintf(stderr, "problem in ::WideCharToMultiByte\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char** argv)
|
|
|
|
{
|
2004-11-08 02:59:35 +03:00
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> ccMain =
|
2007-09-05 17:04:54 +04:00
|
|
|
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
|
1999-08-04 01:05:59 +04:00
|
|
|
if(NS_FAILED(res))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Cannot get Character Converter Manager %x\n", res);
|
|
|
|
}
|
|
|
|
int i;
|
|
|
|
if(argc > 5)
|
|
|
|
{
|
|
|
|
for(i =0; i < argc; i++)
|
|
|
|
{
|
1999-08-09 18:54:13 +04:00
|
|
|
if(strcmp(argv[i], "-f") == 0)
|
1999-08-04 01:05:59 +04:00
|
|
|
{
|
|
|
|
tocodeind = i+1;
|
2003-06-11 22:16:03 +04:00
|
|
|
res = ccMain->GetUnicodeDecoder(argv[tocodeind], &decoder);
|
1999-08-04 01:05:59 +04:00
|
|
|
if(NS_FAILED(res)) {
|
|
|
|
fprintf(stderr, "Cannot get Unicode decoder %s %x\n",
|
|
|
|
argv[tocodeind],res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
1999-08-09 18:54:13 +04:00
|
|
|
if(strcmp(argv[i], "-t") == 0)
|
1999-08-04 01:05:59 +04:00
|
|
|
{
|
|
|
|
fromcodeind = i+1;
|
2003-06-11 22:16:03 +04:00
|
|
|
res = ccMain->GetUnicodeEncoderRaw(argv[fromcodeind], &encoder);
|
1999-08-04 01:05:59 +04:00
|
|
|
if(NS_FAILED(res)) {
|
|
|
|
fprintf(stderr, "Cannot get Unicode encoder %s %x\n",
|
|
|
|
argv[fromcodeind],res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(argc == 6)
|
|
|
|
{
|
|
|
|
infile = fopen(argv[5], "rb");
|
2012-08-24 11:49:00 +04:00
|
|
|
if (!infile)
|
1999-08-04 01:05:59 +04:00
|
|
|
{
|
|
|
|
usage();
|
|
|
|
fprintf(stderr,"cannot open file %s\n", argv[5]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
infile = stdin;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t insize,medsize,outsize;
|
1999-08-04 01:05:59 +04:00
|
|
|
while((insize=fread(inbuffer, 1,INBUFSIZE,infile)) > 0)
|
|
|
|
{
|
|
|
|
medsize=MEDBUFSIZE;
|
|
|
|
|
1999-09-02 02:50:50 +04:00
|
|
|
res = decoder->Convert(inbuffer,&insize, medbuffer, &medsize);
|
1999-08-04 01:05:59 +04:00
|
|
|
if(NS_FAILED(res)) {
|
|
|
|
fprintf(stderr, "failed in decoder->Convert %x\n",res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
outsize = OUTBUFSIZE;
|
|
|
|
res = encoder->Convert(medbuffer, &medsize, outbuffer,&outsize);
|
|
|
|
if(NS_FAILED(res)) {
|
|
|
|
fprintf(stderr, "failed in encoder->Convert %x\n",res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fwrite(outbuffer, 1, outsize, stdout);
|
|
|
|
|
|
|
|
memcpyDecode(inbuffer, insize, outbuffer);
|
|
|
|
|
|
|
|
memcpyEncode(inbuffer, insize, outbuffer);
|
|
|
|
|
|
|
|
medsize = MEDBUFSIZE;
|
|
|
|
msDecode(incp, inbuffer, insize, medbuffer, &medsize);
|
|
|
|
|
|
|
|
outsize = OUTBUFSIZE;
|
|
|
|
msEncode(outcp, medbuffer, medsize, outbuffer, &outsize);
|
|
|
|
|
|
|
|
medsize = MEDBUFSIZE;
|
|
|
|
WideDecode( inbuffer, insize, medbuffer, &medsize);
|
|
|
|
|
|
|
|
outsize = OUTBUFSIZE;
|
|
|
|
NarrowEncode( medbuffer, medsize, outbuffer, &outsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(infile);
|
|
|
|
fclose(stdout);
|
|
|
|
fprintf(stderr, "Done!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
usage();
|
|
|
|
return -1;
|
|
|
|
}
|