2018-05-25 20:07:45 -05:00
|
|
|
// 21 may 2018
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
class Error {
|
|
|
|
public:
|
2018-05-26 15:56:53 -05:00
|
|
|
virtual ~Error(void) = default;
|
2018-05-25 20:07:45 -05:00
|
|
|
|
|
|
|
virtual const char *String(void) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern Error *NewEOF(void);
|
|
|
|
extern Error *NewErrShortWrite(void);
|
|
|
|
extern bool IsEOF(Error *e);
|
|
|
|
|
|
|
|
class ReadCloser {
|
|
|
|
public:
|
2018-05-26 15:56:53 -05:00
|
|
|
virtual ~ReadCloser(void) = default;
|
2018-05-25 20:07:45 -05:00
|
|
|
|
|
|
|
virtual Error *Read(void *buf, size_t n, size_t *actual) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class WriteCloser {
|
|
|
|
public:
|
2018-05-26 15:56:53 -05:00
|
|
|
virtual ~WriteCloser(void) = default;
|
2018-05-25 20:07:45 -05:00
|
|
|
|
|
|
|
virtual Error *Write(void *buf, size_t n) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern Error *OpenRead(const char *filename, ReadCloser **r);
|
2018-05-26 16:42:11 -05:00
|
|
|
extern Error *CreateWrite(const char *filename, WriteCloser **w);
|
|
|
|
extern Error *WriteVector(WriteCloser *w, std::vector<char> *v);
|
2018-05-25 20:07:45 -05:00
|
|
|
|
|
|
|
class Scanner {
|
|
|
|
ReadCloser *r;
|
|
|
|
char *buf;
|
|
|
|
const char *p;
|
|
|
|
size_t n;
|
|
|
|
std::vector<char> *line;
|
|
|
|
Error *err;
|
|
|
|
public:
|
|
|
|
Scanner(ReadCloser *r);
|
|
|
|
~Scanner(void);
|
|
|
|
|
|
|
|
bool Scan(void);
|
|
|
|
const char *Bytes(void) const;
|
|
|
|
size_t Len(void) const;
|
|
|
|
Error *Err(void) const;
|
|
|
|
};
|
2018-05-26 16:42:11 -05:00
|
|
|
|
2018-05-26 19:09:16 -05:00
|
|
|
extern void AppendString(std::vector<char> *v, const char *str);
|
|
|
|
|
2018-05-26 16:42:11 -05:00
|
|
|
class Slice {
|
|
|
|
const char *p;
|
|
|
|
size_t n;
|
|
|
|
public:
|
|
|
|
Slice(const char *p, size_t n);
|
|
|
|
|
|
|
|
const char *Data(void) const;
|
|
|
|
size_t Len(void) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern std::vector<Slice *> *TokenizeWhitespace(const char *buf, size_t n);
|
|
|
|
extern void FreeTokenized(std::vector<Slice *> *v);
|
|
|
|
extern void AppendSlice(std::vector<char> *v, Slice *s);
|