[vConnect/trunk/stand2.0] add: TextWriter
@@ -0,0 +1,48 @@ | ||
1 | +#ifndef TEST_TextWriterTest | |
2 | +#define TEST_TextWriterTestTest | |
3 | +#include <cppunit/extensions/HelperMacros.h> | |
4 | +#include "../TextWriter.h" | |
5 | + | |
6 | +using namespace vconnect; | |
7 | +using namespace std; | |
8 | + | |
9 | +class TextWriterTest : public CppUnit::TestFixture | |
10 | +{ | |
11 | +public: | |
12 | + void testTextWriter() | |
13 | + { | |
14 | + string outputPath = "textWriterTest.txt"; | |
15 | + char crlf[3] = { 0x0D, 0x0A }; | |
16 | + string newLine = crlf; | |
17 | + TextWriter writer( outputPath, "Shift_JIS", newLine ); | |
18 | + writer.writeLine( "吾輩は猫である。名前はまだ無い。" ); | |
19 | + writer.writeLine( "どこで生れたかとんと見当がつかぬ。" ); | |
20 | + writer.close(); | |
21 | + | |
22 | + string expected, actual; | |
23 | + expected = this->getContents( "expected/TextWriter/shift_jis_crlf.txt" ); | |
24 | + actual = this->getContents( outputPath ); | |
25 | + CPPUNIT_ASSERT_EQUAL( expected, actual ); | |
26 | + } | |
27 | + | |
28 | + /** | |
29 | + * 指定されたパスに置かれたファイルを文字列に読み込む | |
30 | + * @param path 読み込むファイルのパス | |
31 | + * @return 読み込んだデータ | |
32 | + */ | |
33 | + string getContents( string path ) | |
34 | + { | |
35 | + ifstream stream( path.c_str() ); | |
36 | + string result; | |
37 | + stream >> result; | |
38 | + stream.close(); | |
39 | + return result; | |
40 | + } | |
41 | + | |
42 | + CPPUNIT_TEST_SUITE( TextWriterTest ); | |
43 | + CPPUNIT_TEST( testTextWriter ); | |
44 | + CPPUNIT_TEST_SUITE_END(); | |
45 | +}; | |
46 | + | |
47 | +CPPUNIT_TEST_SUITE_REGISTRATION( TextWriterTest ); | |
48 | +#endif |
@@ -6,8 +6,9 @@ | ||
6 | 6 | #include <cppunit/BriefTestProgressListener.h> |
7 | 7 | #include <cppunit/extensions/TestFactoryRegistry.h> |
8 | 8 | |
9 | +#include "EncodingConverterTest.h" | |
9 | 10 | #include "TextReaderTest.h" |
10 | -#include "EncodingConverterTest.h" | |
11 | +#include "TextWriterTest.h" | |
11 | 12 | |
12 | 13 | int main( int argc, char* argv[] ) |
13 | 14 | { |
@@ -0,0 +1,92 @@ | ||
1 | +/* | |
2 | + * TextWriter.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 __TextWriter_h__ | |
15 | +#define __TextWriter_h__ | |
16 | + | |
17 | +#include "EncodingConverter.h" | |
18 | + | |
19 | +using namespace std; | |
20 | + | |
21 | +namespace vconnect | |
22 | +{ | |
23 | + | |
24 | + /** | |
25 | + * テキストファイルへの書き込みを行うためのクラス | |
26 | + */ | |
27 | + class TextWriter | |
28 | + { | |
29 | + private: | |
30 | + EncodingConverter *converter; | |
31 | + FILE *fileHandle; | |
32 | + string newLine; | |
33 | + | |
34 | + public: | |
35 | + /** | |
36 | + * テキストエンコーディング、ファイルパス、改行文字を指定してファイルを開く | |
37 | + * @param path ファイルパス | |
38 | + * @param encoding 書きこむテキストのエンコーディング | |
39 | + * @param newLine 改行文字列 | |
40 | + */ | |
41 | + TextWriter( string path, string encoding, string newLine ) | |
42 | + { | |
43 | + this->fileHandle = fopen( path.c_str(), "wb" ); | |
44 | + this->converter = new EncodingConverter( EncodingConverter::getInternalEncoding(), encoding ); | |
45 | + this->newLine = newLine; | |
46 | + } | |
47 | + | |
48 | + /** | |
49 | + * デストラクタ | |
50 | + */ | |
51 | + ~TextWriter() | |
52 | + { | |
53 | + this->close(); | |
54 | + } | |
55 | + | |
56 | + /** | |
57 | + * バッファをフラッシュしファイルを閉じる | |
58 | + */ | |
59 | + void close() | |
60 | + { | |
61 | + if( this->fileHandle ){ | |
62 | + fflush( this->fileHandle ); | |
63 | + fclose( this->fileHandle ); | |
64 | + } | |
65 | + if( this->converter ){ | |
66 | + delete this->converter; | |
67 | + } | |
68 | + this->fileHandle = NULL; | |
69 | + this->converter = NULL; | |
70 | + } | |
71 | + | |
72 | + /** | |
73 | + * 新しい行を書き込む | |
74 | + * @param line 書きこむ行データ | |
75 | + */ | |
76 | + void writeLine( string line ) | |
77 | + { | |
78 | + string converted; | |
79 | + converted = this->converter->convert( line ); | |
80 | + converted += this->newLine; | |
81 | + fprintf( this->fileHandle, "%s", converted.c_str() ); | |
82 | + } | |
83 | + | |
84 | + private: | |
85 | + TextWriter() | |
86 | + { | |
87 | + }; | |
88 | + }; | |
89 | + | |
90 | +} | |
91 | + | |
92 | +#endif |