ARB
reader.cxx
Go to the documentation of this file.
1 #include "reader.h"
2 #include <unistd.h>
3 
4 Reader::Reader(const char *inf)
5  : fp(fopen(inf, "rt")),
6  file(NULp),
7  curr(NULp),
8  failure(false)
9 {
10  reset();
11  try {
12  if (!fp) {
13  throw_errorf(1, "can't read input file '%s' (Reason: %s)", inf, strerror(errno));
14  }
15  file = new BufferedFileReader(inf, fp);
16  file->showFilenameInLineError(true);
17  read();
18  }
19  catch (Convaln_exception& exc) {
20  failure = true;
21  curr = NULp;
22  throw;
23  }
24 }
25 
27  // if kicked by exception, decorate error-msg with current reader position
29  exc->replace_msg(file->lineError(exc->get_msg()).c_str());
30  }
31  else {
32  ca_assert(!curr); // reader did NOT read till EOF, why ?
33  }
34  delete file;
35 }
36 
37 void Reader::read() {
38  if (!curr) {
39  failure = true; // read _beyond_ EOF
40  }
41  else {
42  ca_assert(!failure); // attempt to read after failure
43 
44  string next_line;
45  if (!file->getLine(next_line)) {
46  curr = NULp;
47  }
48  else {
49  size_t len = next_line.length();
50 
51  if (len >= (LINESIZE-1)) {
52  char lbuf[200];
53  memcpy(lbuf, next_line.c_str(), 200-4);
54  strcpy(lbuf+200-4, "...");
55  throw_errorf(148, "Line too long: '%s'", lbuf);
56  }
57  memcpy(linebuf, next_line.c_str(), len);
58  linebuf[len] = '\n';
59  linebuf[len + 1] = 0;
60 
61  curr = linebuf;
62  }
63  }
64 }
65 
66 
67 // --------------------------------------------------------------------------------
68 
69 FileWriter::FileWriter(const char *outname)
70  : ofp(NULp),
71  filename(NULp),
72  written(0)
74  , checked_written(false)
75 #endif
76 {
77  ofp = fopen(outname, "wt");
78  if (!ofp) {
79  throw_errorf(2, "can't write output file '%s' (Reason: %s)", outname, strerror(errno));
80  }
81  filename = ARB_strdup(outname);
82 }
84  bool fine = is_fine();
85 
86 #if defined(ENFORCE_CHECKED_WRITTEN)
87  ca_assert(implicated(fine, checked_written)); // you have to call expect_written() on FileWriter before destruction
88 #endif
89 
90  if (ofp) fclose(ofp);
91  if (!fine) unlink(filename);
92  free(filename);
93 
94  log_processed(written);
95 }
96 
98 #if defined(ENFORCE_CHECKED_WRITTEN)
99  ca_assert(!checked_written); // checking twice is nonsense
100  checked_written = true;
101 #endif
102  if (is_fine() && !written) {
103  throw_errorf(42, "No sequence has been written");
104  }
105 }
106 
108  throw_errorf(41, "Write error: %s(=%i) while writing %s",
109  strerror(errno), errno, name());
110 }
111 
112 int FileWriter::outf(const char *format, ...) {
113  va_list parg;
114  va_start(parg, format);
115  int printed = vfprintf(ofp, format, parg);
116  va_end(parg);
117  if (printed<0) throw_write_error();
118  return printed;
119 }
120 
121 int Writer::outf(const char *format, ...) {
122  va_list parg;
123  va_start(parg, format);
124  char buffer[LINESIZE];
125  int printed = vsprintf(buffer, format, parg);
126  ca_assert(printed <= LINESIZE);
127  va_end(parg);
128 
129  out(buffer);
130  return printed;
131 }
132 
133 
Reader(const char *inf)
Definition: reader.cxx:4
FileWriter(const char *outf)
Definition: reader.cxx:69
AliDataPtr format(AliDataPtr data, const size_t wanted_len, GB_ERROR &error)
Definition: insdel.cxx:615
#define implicated(hypothesis, conclusion)
Definition: arb_assert.h:289
void throw_errorf(int error_num, const char *error_messagef,...) __ATTR__FORMAT(2) __ATTR__NORETURN
Definition: util.cxx:41
bool getLine(string &line)
#define ca_assert(cond)
Definition: global.h:33
void expect_written()
Definition: reader.cxx:97
char * ARB_strdup(const char *str)
Definition: arb_string.h:27
char buffer[MESSAGE_BUFFERSIZE]
Definition: seq_search.cxx:34
void showFilenameInLineError(bool show)
string lineError(const string &msg) const
#define false
Definition: ureadseq.h:13
virtual void out(char ch)=0
virtual int outf(const char *format,...) __ATTR__FORMAT_MEMBER(1)
Definition: reader.cxx:121
GB_write_int const char GB_write_autoconv_string WRITE_SKELETON(write_pointer, GBDATA *,"%p", GB_write_pointer) char *AW_awa if)(!gb_var) return strdup("")
Definition: AW_awar.cxx:163
vfprintf(stderr, format, parg)
virtual void throw_write_error() const __ATTR__NORETURN
Definition: reader.cxx:107
va_end(argPtr)
static const Convaln_exception * exception_thrown()
Definition: global.h:71
virtual ~Reader()
Definition: reader.cxx:26
va_start(argPtr, format)
virtual const char * name() const =0
#define NULp
Definition: cxxforward.h:116
#define ENFORCE_CHECKED_WRITTEN
Definition: reader.h:92
void log_processed(int seqCount)
Definition: fconv.cxx:50
int outf(const char *format,...) OVERRIDE __ATTR__FORMAT_MEMBER(1)
Definition: reader.cxx:112
~FileWriter() OVERRIDE
Definition: reader.cxx:83
#define LINESIZE
Definition: defs.h:16