ARB
FileContent.cxx
Go to the documentation of this file.
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 
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 
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 // --------------------------------------------------------------------------------
#define arb_assert(cond)
Definition: arb_assert.h:245
group_matcher all()
Definition: test_unit.h:1000
GB_ERROR GB_IO_error(const char *action, const char *filename)
Definition: arb_msg.cxx:293
STL namespace.
char * ARB_stringdup(const std::string &str)
Definition: arb_stdstring.h:24
long GB_size_of_file(const char *path)
Definition: arb_file.cxx:28
#define TEST_PUBLISH(testfunction)
Definition: test_unit.h:1485
GB_ERROR save()
Definition: FileContent.cxx:35
static void error(const char *msg)
Definition: mkptypes.cxx:96
fputc('\n', stderr)
#define that(thing)
Definition: test_unit.h:1032
#define is_equal_to(val)
Definition: test_unit.h:1014
fputs(TRACE_PREFIX, stderr)
static int line
Definition: arb_a2ps.c:296
bool fulfilled() const OVERRIDE
Definition: test_unit.h:562
#define TEST_EXPECT_EQUAL(expr, want)
Definition: test_unit.h:1283