1 | // ============================================================= // |
---|
2 | // // |
---|
3 | // File : FileContent.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Coded by Ralf Westram (coder@reallysoft.de) in April 2012 // |
---|
7 | // Institute of Microbiology (Technical University Munich) // |
---|
8 | // http://www.arb-home.de/ // |
---|
9 | // // |
---|
10 | // ============================================================= // |
---|
11 | |
---|
12 | #include "FileContent.h" |
---|
13 | #include "BufferedFileReader.h" |
---|
14 | #include "arb_msg.h" |
---|
15 | #include "arb_file.h" |
---|
16 | #include "arb_stdstring.h" |
---|
17 | |
---|
18 | using namespace std; |
---|
19 | |
---|
20 | void FileContent::init() { |
---|
21 | FILE *fp = fopen(path, "rb"); // b! |
---|
22 | if (!fp) { |
---|
23 | error = GB_IO_error("loading", path); |
---|
24 | } |
---|
25 | else { |
---|
26 | BufferedFileReader buf(path, fp); |
---|
27 | |
---|
28 | string line; |
---|
29 | while (buf.getLine(line)) { |
---|
30 | Lines.put(ARB_stringdup(line)); |
---|
31 | } |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | GB_ERROR FileContent::save() { |
---|
36 | arb_assert(!has_error()); |
---|
37 | |
---|
38 | FILE *out = fopen(path, "wt"); |
---|
39 | bool failed = !out; |
---|
40 | |
---|
41 | if (out) { |
---|
42 | for (size_t i = 0; i<Lines.size(); ++i) { |
---|
43 | fputs(Lines[i], out); |
---|
44 | fputc('\n', out); |
---|
45 | } |
---|
46 | failed = fclose(out) != 0; |
---|
47 | } |
---|
48 | |
---|
49 | if (failed) error = GB_IO_error("saving", path); |
---|
50 | return error; |
---|
51 | } |
---|
52 | |
---|
53 | // -------------------------------------------------------------------------------- |
---|
54 | |
---|
55 | #ifdef UNIT_TESTS |
---|
56 | #ifndef TEST_UNIT_H |
---|
57 | #include <test_unit.h> |
---|
58 | #endif |
---|
59 | |
---|
60 | static arb_test::match_expectation arrays_equal(const StrArray& expected, const StrArray& got) { |
---|
61 | using namespace arb_test; |
---|
62 | match_expectation same_size = that(expected.size()).is_equal_to(got.size()); |
---|
63 | if (same_size.fulfilled()) { |
---|
64 | for (size_t i = 0; i<expected.size(); ++i) { |
---|
65 | match_expectation eq = that(expected[i]).is_equal_to(got[i]); |
---|
66 | if (!eq.fulfilled()) { |
---|
67 | return all().of(same_size, eq); |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | return same_size; |
---|
72 | } |
---|
73 | |
---|
74 | #define TEST_EXPECT_READS_SAME(fc,name) do { \ |
---|
75 | FileContent oc(name); \ |
---|
76 | TEST_EXPECTATION(arrays_equal(fc.lines(), oc.lines())); \ |
---|
77 | } while(0) |
---|
78 | |
---|
79 | void TEST_linefeed_conversion() { |
---|
80 | const char *funix = "general/text.input"; // LF |
---|
81 | const char *fdos = "general/dos.input"; // CR LF |
---|
82 | const char *fmac = "general/mac.input"; // CR |
---|
83 | const char *fbroken = "general/broken_LF.input"; // LF CR |
---|
84 | |
---|
85 | const int LINES = 6; |
---|
86 | |
---|
87 | FileContent cunix(funix); |
---|
88 | TEST_EXPECT_EQUAL(cunix.lines().size(), LINES); |
---|
89 | |
---|
90 | TEST_EXPECT_EQUAL(GB_size_of_file(fmac), GB_size_of_file(funix)); |
---|
91 | TEST_EXPECT_EQUAL(GB_size_of_file(fdos), GB_size_of_file(funix)+LINES); |
---|
92 | TEST_EXPECT_EQUAL(GB_size_of_file(fbroken), GB_size_of_file(fdos)); |
---|
93 | |
---|
94 | TEST_EXPECT_READS_SAME(cunix,fdos); |
---|
95 | TEST_EXPECT_READS_SAME(cunix,fmac); |
---|
96 | TEST_EXPECT_READS_SAME(cunix,fbroken); |
---|
97 | } |
---|
98 | TEST_PUBLISH(TEST_linefeed_conversion); |
---|
99 | |
---|
100 | #endif // UNIT_TESTS |
---|
101 | |
---|
102 | // -------------------------------------------------------------------------------- |
---|