EncodingConverter.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * EncodingConverter.h
  3. * Copyright © 2012 kbinani
  4. *
  5. * This file is part of vConnect-STAND.
  6. *
  7. * vConnect-STAND is free software; you can redistribute it and/or
  8. * modify it under the terms of the GPL License.
  9. *
  10. * vConnect-STAND is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14. #ifndef __EncodingConverter_h__
  15. #define __EncodingConverter_h__
  16. #include <errno.h>
  17. #include <iconv.h>
  18. #include <string>
  19. #include <stdio.h>
  20. #include <string.h>
  21. using namespace std;
  22. namespace vconnect
  23. {
  24. /**
  25. * マルチバイト文字列のテキストエンコーディングを変換するためのコンバータ
  26. */
  27. class EncodingConverter
  28. {
  29. private:
  30. /**
  31. * システム内部のマルチバイト文字列が使用しているテキストエンコーディング名
  32. */
  33. string internalEncoding;
  34. /**
  35. * libiconv のコンバータ
  36. */
  37. iconv_t converter;
  38. public:
  39. /**
  40. * 変換元と変換先のエンコーディングを指定し、コンバータを初期化する
  41. * @param from 変換前のエンコーディング名
  42. * @param to 変換後のエンコーディング名
  43. */
  44. EncodingConverter( string from, string to )
  45. {
  46. this->converter = iconv_open( to.c_str(), from.c_str() );
  47. if( false == isValidConverter( this->converter ) ){
  48. this->converter = NULL;
  49. }
  50. this->internalEncoding = getInternalEncoding();
  51. }
  52. ~EncodingConverter()
  53. {
  54. if( this->converter && isValidConverter( this->converter ) ){
  55. iconv_close( this->converter );
  56. }
  57. this->converter = NULL;
  58. }
  59. /**
  60. * 文字列のテキストエンコーディングを変換する
  61. * @param source 変換する文字列
  62. * @return エンコーディングを変換した文字列
  63. */
  64. string convert( string source )
  65. {
  66. if( NULL == this->converter ){
  67. return source;
  68. }
  69. string result;
  70. char const* input = source.c_str();
  71. size_t remainingInputBytes = source.size();
  72. char *buffer = new char[remainingInputBytes + 1];
  73. memset(buffer, 0, sizeof(char) * (remainingInputBytes + 1));
  74. char *output = buffer;
  75. size_t remainingOutputBytes = remainingInputBytes;
  76. size_t outputBytes = remainingInputBytes;
  77. while( remainingInputBytes > 0 ){
  78. char const* originalInput = input;
  79. size_t n = iconv( this->converter, (char **)&input, &remainingInputBytes, &output, &remainingOutputBytes );
  80. int error = errno;
  81. if( (n != (size_t) - 1 && remainingInputBytes == 0) || (error == EINVAL) ){
  82. remainingInputBytes = 0;
  83. result.append( buffer, 0, outputBytes - remainingOutputBytes );
  84. }else{
  85. switch( error ){
  86. case E2BIG:{
  87. result.append( buffer, 0, outputBytes - remainingOutputBytes );
  88. output = buffer;
  89. remainingOutputBytes = outputBytes;
  90. break;
  91. }
  92. case EILSEQ:{
  93. result.append( buffer, 0, outputBytes - remainingOutputBytes );
  94. result.append( input, 0, 1 );
  95. input++;
  96. remainingInputBytes--;
  97. output = buffer;
  98. remainingOutputBytes = outputBytes;
  99. break;
  100. }
  101. default:{
  102. result.append( originalInput );
  103. remainingInputBytes = 0;
  104. break;
  105. }
  106. }
  107. }
  108. }
  109. output = buffer;
  110. remainingOutputBytes = outputBytes;
  111. if( iconv( this->converter , NULL, NULL, &output, &remainingOutputBytes ) != (size_t) - 1 ){
  112. result.append( buffer, 0, outputBytes - remainingOutputBytes );
  113. }
  114. delete [] buffer;
  115. return result;
  116. }
  117. /**
  118. * char の内部のエンコーディングを調べる
  119. * @return
  120. */
  121. static string getInternalEncoding()
  122. {
  123. string result = "";
  124. char *localeNameRaw = setlocale( LC_CTYPE, "" );
  125. if( NULL != localeNameRaw ){
  126. // localeName = "ja_JP.UTF-8" (MacOSX Lion)
  127. // localeName = "ja_JP.UTF-8" (openSUSE, g++)
  128. // localeName = "ja_JP.UTF-8" (CentOS, g++)
  129. // localeName = "Japanese_Japan.932" (Windows XP, g++)
  130. // localeName = "Japanese_Japan.932" (Windows XP, VC++2008)
  131. string localeName = localeNameRaw;
  132. result = getCodeset( localeName );
  133. if( false == isValidEncoding( result ) && 0 != atoi( result.c_str() ) ){
  134. // result が全部数字だったら、"CP" という文字列を付けてリトライする
  135. result = "CP" + result;
  136. if( false == isValidEncoding( result ) ){
  137. result = "";
  138. }
  139. }
  140. }
  141. return result;
  142. }
  143. /**
  144. * コードページの名称から、読込み時の読込単位(バイト)を調べます
  145. * @return テキストファイルからの読み込み単位
  146. */
  147. static int getBytesPerWord( string encoding )
  148. {
  149. encoding = toLower( encoding );
  150. if( encoding.compare( "utf-16le" ) == 0 ){
  151. return 2;
  152. }else if( encoding.compare( "utf-16be" ) == 0 ){
  153. return 2;
  154. }else if( encoding.compare( "utf-16" ) == 0 ){
  155. return 2;
  156. }else if( encoding.compare( "utf-32le" ) == 0 ){
  157. return 4;
  158. }else if( encoding.compare( "utf-32be" ) == 0 ){
  159. return 4;
  160. }else if( encoding.compare( "utf-32" ) == 0 ){
  161. return 4;
  162. }else{
  163. return 1;
  164. }
  165. }
  166. /**
  167. * 有効なエンコーディングかどうかを取得する
  168. * @param codeset エンコーディング名
  169. * @return 有効なエンコーディングであれば true を、そうでなければ false を返す
  170. */
  171. static bool isValidEncoding( string codeset )
  172. {
  173. // まずUTF-8が有効かどうか
  174. iconv_t cnv = iconv_open( "UTF-8", "UTF-8" );
  175. if( false == isValidConverter( cnv ) ){
  176. return false;
  177. }
  178. iconv_close( cnv );
  179. iconv_t cnv2 = iconv_open( "UTF-8", codeset.c_str() );
  180. if( false == isValidConverter( cnv2 ) ){
  181. return false;
  182. }
  183. iconv_close( cnv2 );
  184. iconv_t cnv3 = iconv_open( codeset.c_str(), "UTF-8" );
  185. if( false == isValidConverter( cnv3 ) ){
  186. return false;
  187. }
  188. iconv_close( cnv3 );
  189. return true;
  190. }
  191. protected:
  192. EncodingConverter()
  193. {
  194. this->converter = NULL;
  195. }
  196. /**
  197. * setlocale( LC_CTYPE, "" ) の戻り値から、コードセット名を取得する
  198. * @param locale setlocale 関数の戻り値
  199. * @return コードセット名。取得できない場合は空文字
  200. * @ref http://linuxjm.sourceforge.jp/html/LDP_man-pages/man3/setlocale.3.html
  201. */
  202. static string getCodeset( string locale )
  203. {
  204. string::size_type indexCollon = locale.find( "." );
  205. string::size_type indexAtmark = locale.find( "@" );
  206. if( string::npos == indexCollon ){
  207. return "";
  208. }
  209. if( string::npos == indexAtmark ){
  210. return locale.substr( indexCollon + 1 );
  211. }else{
  212. return locale.substr( indexCollon + 1, indexAtmark - indexCollon - 1 );
  213. }
  214. }
  215. private:
  216. /**
  217. * 小文字に変換する
  218. * @param text 変換元の文字列
  219. * @return 小文字に変換後の文字列
  220. */
  221. static string toLower( string text )
  222. {
  223. string::size_type length = text.length();
  224. string::size_type i;
  225. for( i = 0; i < length; i++ ){
  226. text[i] = tolower( text[i] );
  227. }
  228. return text;
  229. }
  230. /**
  231. * 有効なコンバータかどうかを調べる
  232. * @param converter 調べる対象のコンバータ
  233. * @return コンバータが有効であれば true を、そうでなければ false を返す
  234. */
  235. static bool isValidConverter( iconv_t converter )
  236. {
  237. iconv_t invalid = (iconv_t) - 1;
  238. return (converter == invalid) ? false : true;
  239. }
  240. };
  241. }
  242. #endif