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