convperf.cpp 4.5 KB

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