| 1 |
// -*- related-file-name: "../../liblcdf/slurper.cc" -*- |
| 2 |
#ifndef LCDF_SLURPER_HH |
| 3 |
#define LCDF_SLURPER_HH |
| 4 |
#include <lcdf/landmark.hh> |
| 5 |
#include <lcdf/filename.hh> |
| 6 |
#include <stdio.h> |
| 7 |
|
| 8 |
class Slurper { public: |
| 9 |
|
| 10 |
Slurper(const Filename &, FILE * = 0); |
| 11 |
~Slurper(); |
| 12 |
|
| 13 |
bool ok() const { return _f != 0; } |
| 14 |
|
| 15 |
Landmark landmark() const { return Landmark(_filename.name(), _lineno); } |
| 16 |
operator Landmark() const { return landmark(); } |
| 17 |
unsigned lineno() const { return _lineno; } |
| 18 |
|
| 19 |
const Filename &filename() const { return _filename; } |
| 20 |
char *peek_line(); |
| 21 |
char *next_line(); |
| 22 |
char *append_next_line(); |
| 23 |
void save_line() { _saved_line = true; } |
| 24 |
|
| 25 |
char *cur_line() const { return (char *)_line; } |
| 26 |
unsigned cur_line_length() const { return _line_len; } |
| 27 |
void shorten_line(unsigned); |
| 28 |
|
| 29 |
private: |
| 30 |
|
| 31 |
FILE *_f; |
| 32 |
Filename _filename; |
| 33 |
unsigned _lineno; |
| 34 |
bool _own_f; |
| 35 |
|
| 36 |
unsigned char *_data; |
| 37 |
unsigned _cap; |
| 38 |
unsigned _pos; |
| 39 |
unsigned _len; |
| 40 |
|
| 41 |
unsigned char *_line; |
| 42 |
unsigned _line_len; |
| 43 |
|
| 44 |
bool _saved_line; |
| 45 |
bool _at_eof; |
| 46 |
|
| 47 |
void grow_buffer(); |
| 48 |
inline int more_data(); |
| 49 |
char *get_line_at(unsigned); |
| 50 |
|
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
inline void |
| 55 |
Slurper::shorten_line(unsigned pos) |
| 56 |
{ |
| 57 |
if (pos < _line_len) { |
| 58 |
_line_len = pos; |
| 59 |
_line[_line_len] = 0; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
#endif |