[vConnect/trunk/stand2.0] modified: mb_text; 不要なメソッドを削除
@@ -57,7 +57,7 @@ | ||
57 | 57 | // (1-1) download http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.tar.gz |
58 | 58 | // and http://www2d.biglobe.ne.jp/~msyk/software/libiconv/libiconv-1.13-ja-1.patch.gz |
59 | 59 | // (1-2) unzip |
60 | -// (1-3) apply patch: | |
60 | +// (1-3) apply patch: | |
61 | 61 | // patch -p1 -N < ../libiconv-1.13-ja-1.patch |
62 | 62 | // (1-4) build |
63 | 63 | // ./configure --enable-static --disable-shared |
@@ -87,16 +87,6 @@ | ||
87 | 87 | // 無効な記述子 |
88 | 88 | #define MB_INVALID ((MB_CODEPAGE_DESCRIPTER)-1) |
89 | 89 | |
90 | -// コードページ指定込みのファイルを表す型. | |
91 | -typedef struct{ | |
92 | - // wcharに変換するための記述子 | |
93 | - MB_CODEPAGE_DESCRIPTER descripter_for_wchar; | |
94 | - // charに変換するための記述子 | |
95 | - MB_CODEPAGE_DESCRIPTER descripter_for_char; | |
96 | - FILE *file; | |
97 | - int unit_len; | |
98 | -} MB_FILE; | |
99 | - | |
100 | 90 | /** |
101 | 91 | * 指定したコードページを取り扱いできるかどうかを調べます. |
102 | 92 | */ |
@@ -103,56 +93,16 @@ | ||
103 | 93 | bool mb_is_valid_codepage( unsigned int codepage ); |
104 | 94 | |
105 | 95 | /** |
106 | - * コードページの名称から、読込み時の読込単位長さ(通常はバイト)を調べます | |
107 | - */ | |
108 | -int mb_unitlen_from_charset_name( const char *name ); | |
109 | - | |
110 | -/** | |
111 | 96 | * ASCII文字列のアルファベットを全て小文字に変換します |
112 | 97 | */ |
113 | 98 | void mb_tolower( string& s ); |
114 | 99 | |
115 | 100 | /** |
116 | - * コードページの名称から、コードページ番号を取得します | |
117 | - */ | |
118 | -unsigned int mb_codepage_from_charset_name( const char *name ); | |
119 | - | |
120 | -/** | |
121 | 101 | * コードページ番号から、コードページの名称を取得します |
122 | 102 | */ |
123 | 103 | const char *mb_charset_name_from_codepage( unsigned int codepage ); |
124 | 104 | |
125 | 105 | /** |
126 | - * 指定したコードページを使って、読込み専用のテキストファイルを開く | |
127 | - */ | |
128 | -MB_FILE *mb_fopen( const char *file_name, const char *codepage ); | |
129 | - | |
130 | -/** | |
131 | - * 指定したコードページを使って、読込み専用のテキストファイルを開く | |
132 | - */ | |
133 | -MB_FILE *mb_fopen( string file_name, const char *codepage ); | |
134 | - | |
135 | -/** | |
136 | - * 指定したコードページを使って、読込み専用のテキストファイルを開く | |
137 | - */ | |
138 | -MB_FILE *mb_fopen( wstring file_name, const char *codepage ); | |
139 | - | |
140 | -/** | |
141 | - * ファイルを閉じる | |
142 | - */ | |
143 | -int mb_fclose( MB_FILE *fp ); | |
144 | - | |
145 | -/** | |
146 | - * 内部関数 | |
147 | - * マルチバイト文字列が、指定したコードの制御文字を表すかどうかを調べます | |
148 | - * @param buf 調査対象のマルチバイト文字列のバッファ | |
149 | - * @param len バッファbufの長さ | |
150 | - * @param check_char チェックする制御文字コード | |
151 | - * @return バッファの文字列が指定した制御文字を表すならtrue、そうでなければfalse | |
152 | - */ | |
153 | -bool mb_code_check( char *buf, int len, char check_char ); | |
154 | - | |
155 | -/** | |
156 | 106 | * 内部関数. |
157 | 107 | * バイト列を指定したコードページ記述子を用いて変換します. |
158 | 108 | */ |
@@ -159,27 +109,6 @@ | ||
159 | 109 | void mb_code_conv( char *buf, int buflen, MB_CODEPAGE_DESCRIPTER descripter ); |
160 | 110 | |
161 | 111 | /** |
162 | - * ファイルから1行読み込む. | |
163 | - * mb_fopenでファイルを開いたときに指定したコードページは無視される. | |
164 | - * 改行はCR,CRLF,LFの3種類に対応. | |
165 | - */ | |
166 | -bool mb_fgets( string& line, MB_FILE *fp ); | |
167 | - | |
168 | -/** | |
169 | - * ファイルから1行読み込む. | |
170 | - * mb_fopenでファイルを開いたときに指定したコードページとみなして読み込む. | |
171 | - * 改行はCR,CRLF,LFの3種類に対応. | |
172 | - */ | |
173 | -bool mb_fgets( wstring& line, MB_FILE *fp ); | |
174 | - | |
175 | -#ifdef _DEBUG | |
176 | -/** | |
177 | - * いろんなエンコーディングのテキストファイルを読み込むテストを実行します. | |
178 | - */ | |
179 | -int mb_test(); | |
180 | -#endif | |
181 | - | |
182 | -/** | |
183 | 112 | * ワイド文字列をマルチバイト文字列に変換します. |
184 | 113 | */ |
185 | 114 | void mb_conv( const wstring &src, string &dest ); |
@@ -68,37 +68,6 @@ | ||
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
71 | - * コードページの名称から、読込み時の読込単位(バイト)を調べます | |
72 | - */ | |
73 | -int mb_unitlen_from_charset_name( const char *name ){ | |
74 | - string sname = name; | |
75 | - mb_tolower( sname ); | |
76 | - if( sname.compare( "shift_jis" ) == 0 ){ | |
77 | - return 1; | |
78 | - }else if( sname.compare( "euc-jp" ) == 0 ){ | |
79 | - return 1; | |
80 | - }else if( sname.compare( "iso-2022-jp" ) == 0 ){ | |
81 | - return 1; | |
82 | - }else if( sname.compare( "utf-8" ) == 0 ){ | |
83 | - return 1; | |
84 | - }else if( sname.compare( "utf-16le" ) == 0 ){ | |
85 | - return 2; | |
86 | - }else if( sname.compare( "utf-16be" ) == 0 ){ | |
87 | - return 2; | |
88 | - }else if( sname.compare( "utf-16" ) == 0 ){ | |
89 | - return 2; | |
90 | - }else if( sname.compare( "utf-32le" ) == 0 ){ | |
91 | - return 4; | |
92 | - }else if( sname.compare( "utf-32be" ) == 0 ){ | |
93 | - return 4; | |
94 | - }else if( sname.compare( "utf-32" ) == 0 ){ | |
95 | - return 4; | |
96 | - }else{ | |
97 | - return 1; | |
98 | - } | |
99 | -} | |
100 | - | |
101 | -/** | |
102 | 71 | * ASCII文字列のアルファベットを全て小文字に変換します |
103 | 72 | */ |
104 | 73 | void mb_tolower( string& s ){ |
@@ -110,37 +79,6 @@ | ||
110 | 79 | } |
111 | 80 | |
112 | 81 | /** |
113 | - * コードページの名称から、コードページ番号を取得します | |
114 | - */ | |
115 | -unsigned int mb_codepage_from_charset_name( const char *name ){ | |
116 | - string sname = name; | |
117 | - mb_tolower( sname ); | |
118 | - if( sname.compare( "shift_jis") == 0 ){ | |
119 | - return 932; | |
120 | - }else if( sname.compare( "euc-jp" ) == 0 ){ | |
121 | - return 51932; | |
122 | - }else if( sname.compare( "iso-2022-jp" ) == 0 ){ | |
123 | - return 50220; | |
124 | - }else if( sname.compare( "utf-8" ) == 0 || sname.compare( "utf8" ) == 0 ){ | |
125 | - return 1208; | |
126 | - }else if( sname.compare( "utf-16le" ) == 0 || sname.compare( "utf16le" ) == 0 ){ | |
127 | - return 1202; | |
128 | - }else if( sname.compare( "utf-16be" ) == 0 || sname.compare( "utf16be" ) == 0 ){ | |
129 | - return 1200; | |
130 | - }else if( sname.compare( "utf-16" ) == 0 || sname.compare( "utf16" ) == 0 ){ | |
131 | - return 1204; | |
132 | - }else if( sname.compare( "utf-32le" ) == 0 || sname.compare( "utf32le" ) == 0 ){ | |
133 | - return 1234; | |
134 | - }else if( sname.compare( "utf-32be" ) == 0 || sname.compare( "utf32be" ) == 0 ){ | |
135 | - return 1232; | |
136 | - }else if( sname.compare( "utf-32" ) == 0 || sname.compare( "utf32" ) == 0 ){ | |
137 | - return 1236; | |
138 | - }else{ | |
139 | - return 0; | |
140 | - } | |
141 | -} | |
142 | - | |
143 | -/** | |
144 | 82 | * コードページ番号から、コードページの名称を取得します |
145 | 83 | */ |
146 | 84 | const char *mb_charset_name_from_codepage( unsigned int codepage ){ |
@@ -258,7 +196,7 @@ | ||
258 | 196 | cout << "::mb_init; mb_charset_char=" << mb_charset_char << endl; |
259 | 197 | cout << "::mb_init; mb_charset_wchar=" << mb_charset_wchar << endl; |
260 | 198 | #endif |
261 | - | |
199 | + | |
262 | 200 | #ifdef MB_USE_ICONV |
263 | 201 | mb_descripter_for_wchar2char = iconv_open( mb_charset_char.c_str(), mb_charset_wchar.c_str() ); |
264 | 202 | mb_descripter_for_char2wchar = iconv_open( mb_charset_wchar.c_str(), mb_charset_char.c_str() ); |
@@ -272,151 +210,6 @@ | ||
272 | 210 | } |
273 | 211 | |
274 | 212 | /** |
275 | - * 指定したコードページを使って、読込み専用のテキストファイルを開く。 | |
276 | - * MB_FILEから1行ずつ読み込むのは多重定義されたmb_fgetsを使うが、 | |
277 | - * mb_fgets( wstring &line, MB_FILE *fp )のほうは指定したコードページで、 | |
278 | - * mb_fgets( string &line, MB_FILE *fp )のほうはコードページを無視して読み込む。 | |
279 | - * | |
280 | - * @param file_name ファイルの名前 | |
281 | - * @param codepage コードページ | |
282 | - * @returns ファイル記述子 | |
283 | - */ | |
284 | -MB_FILE *mb_fopen( const char *file_name, const char *codepage ){ | |
285 | - mb_init(); | |
286 | - MB_FILE *fp = (MB_FILE *)malloc( sizeof( MB_FILE ) ); | |
287 | -#ifdef __GNUC__ | |
288 | - fp->file = fopen( file_name, "rb" ); | |
289 | -#else | |
290 | - fopen_s( &(fp->file), file_name, "rb" ); | |
291 | -#endif | |
292 | - if( !(fp->file) ){ | |
293 | -#ifdef _DEBUG | |
294 | - cout << "::mb_fopen; fopen failed; file_name=" << file_name << endl; | |
295 | -#endif | |
296 | - free( fp ); | |
297 | - return NULL; | |
298 | - } | |
299 | - fp->unit_len = mb_unitlen_from_charset_name( codepage ); | |
300 | - if( fp->unit_len < 1 ){ | |
301 | - fp->unit_len = 1; | |
302 | - } | |
303 | -#ifdef _DEBUG | |
304 | - cout << "::mb_fopen; fp->unit_len=" << fp->unit_len << endl; | |
305 | -#endif | |
306 | -#ifdef MB_USE_ICONV | |
307 | - fp->descripter_for_wchar = iconv_open( mb_charset_wchar.c_str(), codepage ); | |
308 | - fp->descripter_for_char = iconv_open( mb_charset_char.c_str(), codepage ); | |
309 | -#else // MB_USE_ICONV | |
310 | - fp->descripter_for_wchar = MB_INVALID; | |
311 | - if( strcmp( codepage, "UTF-8" ) == 0 || | |
312 | - strcmp( codepage, "UTF8" ) == 0 ){ | |
313 | - fp->descripter_for_wchar = CP_UTF8; | |
314 | - }else if( strcmp( codepage, "UTF-7" ) == 0 || | |
315 | - strcmp( codepage, "UTF7" ) == 0 ){ | |
316 | - fp->descripter_for_wchar = CP_UTF7; | |
317 | - }else if( strcmp( codepage, "SJIS" ) == 0 || | |
318 | - strcmp( codepage, "Shift_JIS" ) == 0 ){ | |
319 | - fp->descripter_for_wchar = 932; | |
320 | - }else if( strcmp( codepage, "JIS" ) == 0 || | |
321 | - strcmp( codepage, "ISO-2022-JP" ) == 0 ){ | |
322 | - fp->descripter_for_wchar = 50220; | |
323 | - }else if( strcmp( codepage, "UTF-16" ) == 0 || | |
324 | - strcmp( codepage, "UTF16" ) == 0 ){ | |
325 | - fp->descripter_for_wchar = 1200; | |
326 | - }else{ | |
327 | - fp->descripter_for_wchar = CP_ACP; | |
328 | - } | |
329 | - if( !IsValidCodePage( fp->descripter_for_wchar ) ){ | |
330 | - fp->descripter_for_wchar = MB_INVALID; | |
331 | - } | |
332 | - fp->descripter_for_char = fp->descripter_for_wchar; | |
333 | -#endif // MB_USE_ICONV | |
334 | - | |
335 | - // BOMがある場合は読み飛ばす | |
336 | - unsigned int codepage_number = mb_codepage_from_charset_name( codepage ); | |
337 | - if( codepage_number == 1208 || codepage_number == 1209 ){ | |
338 | - // UTF-8 | |
339 | - int c1 = fgetc( fp->file ); | |
340 | - int c2 = fgetc( fp->file ); | |
341 | - int c3 = fgetc( fp->file ); | |
342 | - if( c1 == 0xEF && c2 == 0xBB && c3 == 0xBF ){ | |
343 | - // BOM | |
344 | - }else{ | |
345 | - // BOMではないようなので戻る | |
346 | - fseek( fp->file, -3, SEEK_CUR ); | |
347 | - } | |
348 | - }else if( 1200 <= codepage_number && codepage_number <= 1205 ){ | |
349 | - // UTF-16 | |
350 | - int c1 = fgetc( fp->file ); | |
351 | - int c2 = fgetc( fp->file ); | |
352 | - if( (c1 == 0xFF && c2 == 0xFE) || | |
353 | - (c1 == 0xFE && c2 == 0xFF) ){ | |
354 | - // BOM | |
355 | - }else{ | |
356 | - // BOMではないようなので戻る | |
357 | - fseek( fp->file, -2, SEEK_CUR ); | |
358 | - } | |
359 | - }else if( 1232 <= codepage_number && codepage_number <= 1237 ){ | |
360 | - // UTF-32 | |
361 | - int c1 = fgetc( fp->file ); | |
362 | - int c2 = fgetc( fp->file ); | |
363 | - int c3 = fgetc( fp->file ); | |
364 | - int c4 = fgetc( fp->file ); | |
365 | - if( (c1 == 0x00 && c2 == 0x00 && c3 == 0xFE && c4 == 0xFF) || | |
366 | - (c1 == 0xFF && c2 == 0xFE && c3 == 0x00 && c4 == 0x00) ){ | |
367 | - // BOM | |
368 | - }else{ | |
369 | - // BOMではないようなので戻る | |
370 | - fseek( fp->file, -4, SEEK_CUR ); | |
371 | - } | |
372 | - } | |
373 | - return fp; | |
374 | -} | |
375 | - | |
376 | -MB_FILE *mb_fopen( string file_name, const char *codepage ){ | |
377 | - return mb_fopen( file_name.c_str(), codepage ); | |
378 | -} | |
379 | - | |
380 | -MB_FILE *mb_fopen( wstring file_name, const char *codepage ){ | |
381 | - mb_init(); | |
382 | - string s; | |
383 | - mb_conv( file_name, s ); | |
384 | -#ifdef _DEBUG | |
385 | - cout << "::mb_fopen(wstring,const char*); s=" << s << endl; | |
386 | -#endif | |
387 | - return mb_fopen( s.c_str(), codepage ); | |
388 | -} | |
389 | - | |
390 | -/** | |
391 | - * コードページ記述子を閉じる | |
392 | - */ | |
393 | -int mb_fclose( MB_FILE *fp ){ | |
394 | - if( !fp ){ | |
395 | - return 0; | |
396 | - } | |
397 | - | |
398 | - // コードページ記述子を閉じる | |
399 | -#ifdef MB_USE_ICONV | |
400 | - if( fp->descripter_for_wchar != MB_INVALID ){ | |
401 | - iconv_close( fp->descripter_for_wchar ); | |
402 | - } | |
403 | - if( fp->descripter_for_char != MB_INVALID ){ | |
404 | - iconv_close( fp->descripter_for_char ); | |
405 | - } | |
406 | -#else | |
407 | - // do nothing | |
408 | -#endif | |
409 | - | |
410 | - // ファイルを閉じる | |
411 | - int ret = 0; | |
412 | - if( fp->file ){ | |
413 | - ret = fclose( fp->file ); | |
414 | - } | |
415 | - free( fp ); | |
416 | - return ret; | |
417 | -} | |
418 | - | |
419 | -/** | |
420 | 213 | * バッファを指定したコードページに変換します |
421 | 214 | */ |
422 | 215 | void mb_code_conv( char *buf, int buffer_bytes, MB_CODEPAGE_DESCRIPTER descripter ){ |
@@ -455,285 +248,6 @@ | ||
455 | 248 | #endif |
456 | 249 | } |
457 | 250 | |
458 | -/** | |
459 | - * マルチバイト文字列が、指定したコードの制御文字を表すかどうかを調べます | |
460 | - * @param buf 調査対象のマルチバイト文字列のバッファ | |
461 | - * @param len バッファbufの長さ | |
462 | - * @param check_char チェックする制御文字コード | |
463 | - * @return バッファの文字列が指定した制御文字を表すならtrue、そうでなければfalse | |
464 | - */ | |
465 | -bool mb_code_check( char *buf, int len, char check_char ){ | |
466 | - if( len < 1 ){ | |
467 | - return false; | |
468 | - } | |
469 | - int i; | |
470 | - | |
471 | - // LEを仮定 | |
472 | - if( buf[0] == check_char ){ | |
473 | - for( i = 1; i < len; i++ ){ | |
474 | - if( buf[i] != 0x00 ){ | |
475 | - return false; | |
476 | - } | |
477 | - } | |
478 | - return true; | |
479 | - } | |
480 | - | |
481 | - // BEを仮定 | |
482 | - if( buf[len - 1] == check_char ){ | |
483 | - for( i = 0; i < len - 1; i++ ){ | |
484 | - if( buf[i] != 0x00 ){ | |
485 | - return false; | |
486 | - } | |
487 | - } | |
488 | - return true; | |
489 | - } | |
490 | - | |
491 | - return false; | |
492 | -} | |
493 | - | |
494 | -/** | |
495 | - * マルチバイト文字列が、復帰制御文字(キャリッジリターン, CR)を表すかどうかを調べます | |
496 | - * @param buf 調査対象のマルチバイト文字列のバッファ | |
497 | - * @param len バッファbufの長さ | |
498 | - * @param check_char チェックする制御文字コード | |
499 | - * @return バッファの文字列がCRを表すならtrue、そうでなければfalse | |
500 | - */ | |
501 | -bool mb_is_cr( char *buf, int len ){ | |
502 | - return mb_code_check( buf, len, 0x0D ); | |
503 | -} | |
504 | - | |
505 | -/** | |
506 | - * マルチバイト文字列が、改行制御文字(ラインフィード, LF)を表すかどうかを調べます | |
507 | - * @param buf 調査対象のマルチバイト文字列のバッファ | |
508 | - * @param len バッファbufの長さ | |
509 | - * @param check_char チェックする制御文字コード | |
510 | - * @return バッファの文字列がLFを表すならtrue、そうでなければfalse | |
511 | - */ | |
512 | -bool mb_is_lf( char *buf, int len ){ | |
513 | - return mb_code_check( buf, len, 0x0A ); | |
514 | -} | |
515 | - | |
516 | -/** | |
517 | - * ファイルから指定されたバイト数を読み込みます。 | |
518 | - * 読込の途中でEOFになった場合true、それ以外はfalseを返します | |
519 | - */ | |
520 | -int mb_fread( char *buf, int len, FILE *fp ){ | |
521 | - int ret = 0; | |
522 | -#ifdef _DEBUG | |
523 | - //cout << "::mb_fread; len=" << len << ";\""; | |
524 | -#endif | |
525 | - memset( buf, 0, len * sizeof( char ) ); | |
526 | - for( int i = 0; i < len; i++ ){ | |
527 | - int c = fgetc( fp ); | |
528 | - if( c == EOF ){ | |
529 | - return ret; | |
530 | - } | |
531 | - buf[i] = (char)c; | |
532 | - ret++; | |
533 | -#ifdef _DEBUG | |
534 | - //cout << "c=" << c << ";"; | |
535 | - //char pch[2]; | |
536 | - //pch[0] = (char)c; | |
537 | - //pch[1] = 0; | |
538 | - //string s( pch ); | |
539 | - //cout << s; | |
540 | -#endif | |
541 | - } | |
542 | -#ifdef _DEBUG | |
543 | - //cout << "\"" << endl; | |
544 | -#endif | |
545 | - return ret; | |
546 | -} | |
547 | - | |
548 | -bool mb_fgets_core( char *buf, int buflen, MB_FILE *file ){ | |
549 | - FILE *fp = file->file; | |
550 | - int unit_buflen = file->unit_len; | |
551 | - int unit_bufbytes = sizeof( char ) * unit_buflen; | |
552 | - char *unit_buf = (char *)malloc( unit_bufbytes ); | |
553 | - int i; | |
554 | - int bufbytes = sizeof( char ) * buflen; | |
555 | - memset( buf, 0, bufbytes ); | |
556 | - int offset = -unit_buflen; | |
557 | - for( i = 0; i < buflen - 1; i++ ){ | |
558 | - // このループ中でbuf[offset]からbuf[offset+buflen]までを埋めます | |
559 | - offset += unit_buflen; | |
560 | - int j; | |
561 | - | |
562 | - // 1文字分読み込む | |
563 | - int len = mb_fread( unit_buf, unit_buflen, fp ); | |
564 | - | |
565 | - if( len != unit_buflen ){ | |
566 | - // EOFまで読んだ場合 | |
567 | - for( j = 0; j < unit_buflen; j++ ){ | |
568 | - buf[j + offset] = '\0'; | |
569 | - } | |
570 | - if( i == 0 ){ | |
571 | - // 最初の文字でEOFの場合 | |
572 | - //free( buf ); | |
573 | - free( unit_buf ); | |
574 | - return false; | |
575 | - }else{ | |
576 | - // それ以外は単にbreakするだけ | |
577 | - break; | |
578 | - } | |
579 | - }else if( mb_is_cr( unit_buf, unit_buflen ) ){ | |
580 | - // 読んだのがCRだった場合 | |
581 | - // 次の文字がLFかどうかを調べる | |
582 | - len = mb_fread( unit_buf, unit_buflen, fp ); | |
583 | - if( len == unit_buflen ){ | |
584 | - if( mb_is_lf( unit_buf, unit_buflen ) ){ | |
585 | - // LFのようだ | |
586 | - }else{ | |
587 | - // LFでないので、ファイルポインタを戻す | |
588 | - fseek( fp, -unit_bufbytes, SEEK_CUR ); | |
589 | - } | |
590 | - } | |
591 | - break; | |
592 | - }else if( mb_is_lf( unit_buf, unit_buflen ) ){ | |
593 | - // 読んだのがLFだった場合 | |
594 | - // 次の文字がCRかどうかを調べる | |
595 | - len = mb_fread( unit_buf, unit_buflen, fp ); | |
596 | - if( len == unit_buflen ){ | |
597 | - if( mb_is_cr( unit_buf, unit_buflen ) ){ | |
598 | - // CRのようだ | |
599 | - // LF-CRという改行方法があるかどうかは知らないけれどサポートしとこう | |
600 | - }else{ | |
601 | - // CRでないので、ファイルポインタを戻す | |
602 | - fseek( fp, -unit_bufbytes, SEEK_CUR ); | |
603 | - } | |
604 | - } | |
605 | - break; | |
606 | - }else{ | |
607 | - // 通常の処理 | |
608 | - for( j = 0; j < unit_buflen; j++ ){ | |
609 | - buf[offset + j] = unit_buf[j]; | |
610 | - } | |
611 | - } | |
612 | - } | |
613 | - | |
614 | - free( unit_buf ); | |
615 | - | |
616 | - return true; | |
617 | -} | |
618 | - | |
619 | -/** | |
620 | - * ファイルから1行読み込む. | |
621 | - * ファイルはdescipterで指定したコードページとみなして読み込む. | |
622 | - * 改行はCR,CRLF,LFの3種類に対応. | |
623 | - */ | |
624 | -bool mb_fgets( wstring& line, MB_FILE *file ){ | |
625 | - int unit_buflen = file->unit_len; | |
626 | - int unit_bufbytes = sizeof( char ) * unit_buflen; | |
627 | - int buflen = LINEBUFF_LEN; | |
628 | - int bufbytes = unit_bufbytes * buflen; | |
629 | - char *buf = (char *)malloc( bufbytes ); | |
630 | - line.clear(); | |
631 | - | |
632 | - bool ret = mb_fgets_core( buf, buflen, file ); | |
633 | - if( !ret ){ | |
634 | - free( buf ); | |
635 | - return ret; | |
636 | - } | |
637 | - | |
638 | - // コードページの読み替え | |
639 | - if( file->descripter_for_wchar != MB_INVALID ){ | |
640 | - mb_code_conv( buf, bufbytes, file->descripter_for_wchar ); | |
641 | - ret = true; | |
642 | - } | |
643 | - | |
644 | - wstring ws = (wchar_t *)buf; | |
645 | - line += ws; | |
646 | - free( buf ); | |
647 | - | |
648 | - return ret; | |
649 | -} | |
650 | - | |
651 | -/** | |
652 | - * ファイルから1行読み込む。 | |
653 | - */ | |
654 | -bool mb_fgets( string& line, MB_FILE *file ){ | |
655 | - int unit_buflen = file->unit_len; | |
656 | - int unit_bufbytes = sizeof( char ) * unit_buflen; | |
657 | - int buflen = LINEBUFF_LEN; | |
658 | - int bufbytes = unit_bufbytes * buflen; | |
659 | - char *buf = (char *)malloc( bufbytes ); | |
660 | - line.clear(); | |
661 | - | |
662 | - bool ret = mb_fgets_core( buf, buflen, file ); | |
663 | - if( !ret ){ | |
664 | - free( buf ); | |
665 | - return ret; | |
666 | - } | |
667 | - | |
668 | - // コードページの読み替え | |
669 | - if( file->descripter_for_char != MB_INVALID ){ | |
670 | - mb_code_conv( buf, bufbytes, file->descripter_for_char ); | |
671 | - ret = true; | |
672 | - } | |
673 | - | |
674 | - string s = (char *)buf; | |
675 | - line += s; | |
676 | - free( buf ); | |
677 | - | |
678 | - return ret; | |
679 | -} | |
680 | - | |
681 | -#ifdef _DEBUG | |
682 | -/** | |
683 | - * いろんなエンコーディングのテキストファイルを読み込むテストを実行します. | |
684 | - */ | |
685 | -#include <iostream> | |
686 | -#if defined( WIN32 ) | |
687 | - #include <windows.h> | |
688 | -#endif | |
689 | -using namespace std; | |
690 | -int mb_test(){ | |
691 | - //do nothing | |
692 | - return 0; | |
693 | - | |
694 | - const char *cp = "UTF-16LE"; // ここを,in.txtのエンコーディングに変える(コンパイル時に手動で) | |
695 | - MB_FILE *fp = mb_fopen( "in.txt", cp ); | |
696 | - if( !fp ){ | |
697 | - printf( "error; io-error; in.txt\n" ); | |
698 | - return 0; | |
699 | - } | |
700 | - FILE *fout_ws = fopen( "out_ws.txt", "wb" ); | |
701 | - fprintf( fout_ws, "%c%c", 0xff, 0xfe ); | |
702 | - wstring buf_ws = L""; | |
703 | - while( mb_fgets( buf_ws, fp ) ){ | |
704 | - int i; | |
705 | - // UNICODE => | |
706 | - for( i = 0; i < buf_ws.length(); i++ ){ | |
707 | - wchar_t c = buf_ws[i]; | |
708 | - int hi = 0xff & (c >> 8); | |
709 | - int lo = 0xff & c; | |
710 | - fputc( lo, fout_ws ); | |
711 | - fputc( hi, fout_ws ); | |
712 | - } | |
713 | - fprintf( fout_ws, "%c%c%c%c", 0x0d, 0x00, 0x0a, 0x00 ); | |
714 | - } | |
715 | - mb_fclose( fp ); | |
716 | - fclose( fout_ws ); | |
717 | - | |
718 | - FILE *fout_mbs = fopen( "out_mbs.txt", "wb" ); | |
719 | - fp = mb_fopen( "in.txt", cp ); | |
720 | - string buf_mbs = ""; | |
721 | - while( mb_fgets( buf_mbs, fp ) ){ | |
722 | - fprintf( fout_mbs, "%s\n", buf_mbs.c_str() ); | |
723 | - } | |
724 | - mb_fclose( fp ); | |
725 | - fclose( fout_mbs ); | |
726 | - int d; | |
727 | - printf( "type eny key to continue..." ); | |
728 | -#ifdef __GNUC__ | |
729 | - scanf( "%d", &d ); | |
730 | -#else | |
731 | - scanf_s( "%d", &d ); | |
732 | -#endif | |
733 | - return 0; | |
734 | -} | |
735 | -#endif | |
736 | - | |
737 | 251 | // ワイド文字列からマルチバイト文字列 |
738 | 252 | // ロケール依存、mainの最初でsetlocale( LC_CTYPE, "" );すること |
739 | 253 | void mb_conv( const wstring &src, string &dest ){ |