-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser.h
66 lines (56 loc) · 979 Bytes
/
parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* CPP Seializer - Fast and simple data serialization for C++.
*
* https://swapped.ch/cpp-serializer
*
* The code is distributed under terms of the BSD license.
* Copyright (c) 2019 Alex Pankratov. All rights reserved.
*/
#ifndef _LIBP_PARSER_H_
#define _LIBP_PARSER_H_
#include <stdint.h>
#include "buffer.h"
/*
* 'parser' is a pair of pointers that frame yet unparsed data
* and a boolean flag that is cleared if parsing runs into trouble.
*/
struct parser
{
parser()
{
ptr = end = NULL;
ok = false;
}
void init(const buffer & buf)
{
init(buf.data(), buf.size());
}
void init(const uint8_t * buf, size_t len)
{
ptr = buf;
end = buf + len;
ok = true;
}
//
bool stop()
{
ok = false;
return false;
}
//
bool has(size_t bytes)
{
if (ok && ptr + bytes > end)
ok = false;
return ok;
}
bool eof() const
{
return ok && (ptr == end);
}
public:
const uint8_t * ptr;
const uint8_t * end;
bool ok;
};
#endif