TextInputStream.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * TextInputStream.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 __TextInputStream_h__
  15. #define __TextInputStream_h__
  16. #include <stdio.h>
  17. #include "EncodingConverter.h"
  18. #include "InputStream.h"
  19. namespace vconnect
  20. {
  21. /**
  22. * テキストファイルを読み込むためのクラス
  23. * @todo 1 行が BUFFER_SIZE を超えるテキストを読む場合動作がデタラメ
  24. */
  25. class TextInputStream : public InputStream
  26. {
  27. private:
  28. /**
  29. * 1行の最大文字数
  30. */
  31. static const int BUFFER_SIZE = 1024;
  32. /**
  33. * テキストエンコーディングのコンバータ
  34. */
  35. EncodingConverter *converter;
  36. /**
  37. * ファイルハンドル
  38. */
  39. FILE *fileHandle;
  40. /**
  41. * 指定されたエンコーディングの読み込み単位バイト数
  42. * たとえば UTF8 なら 1 バイト、UTF16 なら 2 バイト単位で読み込む
  43. */
  44. int bytesPerWord;
  45. /**
  46. * bytesPerWord バイト分のバッファー
  47. */
  48. char *unitBuffer;
  49. /**
  50. * ファイルからデータを読み込むときに使うバッファー
  51. */
  52. char *buffer;
  53. public:
  54. /**
  55. * テキストエンコーディングを指定して、テキストファイルを開く
  56. * @param path 開くファイルのパス
  57. * @param encoding テキストエンコーディング
  58. */
  59. TextInputStream( string path, string encoding )
  60. {
  61. this->converter = new EncodingConverter( encoding, EncodingConverter::getInternalEncoding() );
  62. this->fileHandle = fopen( path.c_str(), "rb" );
  63. this->bytesPerWord = EncodingConverter::getBytesPerWord( encoding );
  64. this->unitBuffer = new char[this->bytesPerWord];
  65. int bufferBytes = this->bytesPerWord * BUFFER_SIZE;
  66. this->buffer = new char[bufferBytes];
  67. }
  68. /**
  69. * デストラクタ
  70. */
  71. ~TextInputStream()
  72. {
  73. this->close();
  74. }
  75. /**
  76. * テキストファイルから 1 行読み込む
  77. * @return 行データ
  78. */
  79. string readLine()
  80. {
  81. FILE *fp = this->fileHandle;
  82. int unit_buflen = this->bytesPerWord;
  83. int unit_bufbytes = sizeof( char ) * unit_buflen;
  84. int i;
  85. int bufbytes = this->bytesPerWord * BUFFER_SIZE;
  86. string result = "";
  87. bool isEndOfLine = false;
  88. while( false == isEndOfLine ){
  89. memset( this->buffer, 0, bufbytes );
  90. int offset = -unit_buflen;
  91. for( i = 0; i < bufbytes - 1; i++ ){
  92. // このループ中でbuf[offset]からbuf[offset+buflen]までを埋めます
  93. offset += unit_buflen;
  94. int j;
  95. // 1文字分読み込む
  96. int len = fillUnitBuffer( this->unitBuffer, unit_buflen, fp );
  97. if( len != unit_buflen ){
  98. // EOFまで読んだ場合
  99. for( j = 0; j < unit_buflen; j++ ){
  100. this->buffer[j + offset] = '\0';
  101. }
  102. isEndOfLine = true;
  103. break;
  104. }else if( isCR( this->unitBuffer, unit_buflen ) ){
  105. // 読んだのがCRだった場合
  106. // 次の文字がLFかどうかを調べる
  107. len = fillUnitBuffer( this->unitBuffer, unit_buflen, fp );
  108. if( len == unit_buflen ){
  109. if( isLF( this->unitBuffer, unit_buflen ) ){
  110. // LFのようだ
  111. }else{
  112. // LFでないので、ファイルポインタを戻す
  113. fseek( fp, -unit_bufbytes, SEEK_CUR );
  114. }
  115. }
  116. isEndOfLine = true;
  117. break;
  118. }else if( isLF( this->unitBuffer, unit_buflen ) ){
  119. // 読んだのがLFだった場合
  120. // 次の文字がCRかどうかを調べる
  121. len = fillUnitBuffer( this->unitBuffer, unit_buflen, fp );
  122. if( len == unit_buflen ){
  123. if( isCR( this->unitBuffer, unit_buflen ) ){
  124. // CRのようだ
  125. // LF-CRという改行方法があるかどうかは知らないけれどサポートしとこう
  126. }else{
  127. // CRでないので、ファイルポインタを戻す
  128. fseek( fp, -unit_bufbytes, SEEK_CUR );
  129. }
  130. }
  131. isEndOfLine = true;
  132. break;
  133. }else{
  134. // 通常の処理
  135. for( j = 0; j < unit_buflen; j++ ){
  136. this->buffer[offset + j] = this->unitBuffer[j];
  137. }
  138. }
  139. }
  140. string source = this->buffer;
  141. result += this->converter->convert( source );
  142. }
  143. return result;
  144. }
  145. /**
  146. * ストリームに対してさらに読み込めるかどうか
  147. * @return 読み込める状態であれば true を返す
  148. */
  149. bool ready()
  150. {
  151. if( this->fileHandle ){
  152. return feof( this->fileHandle ) ? false : true;
  153. }else{
  154. return false;
  155. }
  156. }
  157. /**
  158. * ファイルを閉じる
  159. */
  160. void close()
  161. {
  162. if( this->fileHandle ){
  163. fclose( this->fileHandle );
  164. }
  165. if( this->converter ){
  166. delete this->converter;
  167. }
  168. if( this->unitBuffer ){
  169. delete [] this->unitBuffer;
  170. }
  171. if( this->buffer ){
  172. delete [] this->buffer;
  173. }
  174. this->fileHandle = NULL;
  175. this->converter = NULL;
  176. this->unitBuffer = NULL;
  177. this->buffer = NULL;
  178. }
  179. private:
  180. TextInputStream()
  181. {
  182. }
  183. /**
  184. * マルチバイトの一文字分のデータをファイルから読み込む
  185. * @param buffer 読み込んだデータの格納先
  186. * @param length buffer のバイト数
  187. * @param fileHandle 読み込み対象のファイル
  188. * @return 読み込んだデータのバイト数
  189. */
  190. static int fillUnitBuffer( char *buffer, int length, FILE *fileHandle )
  191. {
  192. int ret = 0;
  193. memset( buffer, 0, length * sizeof( char ) );
  194. for( int i = 0; i < length; i++ ){
  195. int c = fgetc( fileHandle );
  196. if( c == EOF ){
  197. return ret;
  198. }
  199. buffer[i] = (char)c;
  200. ret++;
  201. }
  202. return ret;
  203. }
  204. /**
  205. * 指定したマルチバイトの一文字が、指定した制御文字を表すかどうかを調べる
  206. * @param buffer マルチバイトの一文字
  207. * @param length buffer のバイト数
  208. * @param expected 期待値
  209. * @return 一致したら ture を、そうでなければ false を返す
  210. */
  211. static bool isExpectedCode( char *buffer, int length, char expected )
  212. {
  213. if( length < 1 ){
  214. return false;
  215. }
  216. int i;
  217. // LEを仮定
  218. if( buffer[0] == expected ){
  219. for( i = 1; i < length; i++ ){
  220. if( buffer[i] != 0x00 ){
  221. return false;
  222. }
  223. }
  224. return true;
  225. }
  226. // BEを仮定
  227. if( buffer[length - 1] == expected ){
  228. for( i = 0; i < length - 1; i++ ){
  229. if( buffer[i] != 0x00 ){
  230. return false;
  231. }
  232. }
  233. return true;
  234. }
  235. return false;
  236. }
  237. /**
  238. * マルチバイト文字列が、改行制御文字(キャリッジリターン, CR)を表すかどうかを調べます
  239. * @param buf 調査対象のマルチバイト文字列のバッファ
  240. * @param len バッファbufの長さ
  241. * @param check_char チェックする制御文字コード
  242. * @return バッファの文字列がLFを表すならtrue、そうでなければfalse
  243. */
  244. static bool isCR( char *buffer, int length )
  245. {
  246. return isExpectedCode( buffer, length, 0x0D );
  247. }
  248. /**
  249. * マルチバイト文字列が、改行制御文字(ラインフィード, LF)を表すかどうかを調べます
  250. * @param buf 調査対象のマルチバイト文字列のバッファ
  251. * @param len バッファbufの長さ
  252. * @param check_char チェックする制御文字コード
  253. * @return バッファの文字列がLFを表すならtrue、そうでなければfalse
  254. */
  255. static bool isLF( char *buffer, int length )
  256. {
  257. return isExpectedCode( buffer, length, 0x0A );
  258. }
  259. };
  260. }
  261. #endif