1 |
root |
1.1 |
#ifndef DYNBUF_H__ |
2 |
|
|
#define DYNBUF_H__ |
3 |
|
|
|
4 |
|
|
#include <cstring> |
5 |
|
|
#include <cassert> |
6 |
|
|
|
7 |
root |
1.8 |
#include "util.h" |
8 |
root |
1.4 |
#include "shstr.h" |
9 |
|
|
|
10 |
root |
1.1 |
// this is a "buffer" that can grow fast |
11 |
|
|
// and is still somewhat space-efficient. |
12 |
|
|
// unlike obstacks or other data structures, |
13 |
|
|
// it never moves data around. basically, |
14 |
|
|
// this is a fast strstream without the overhead. |
15 |
|
|
|
16 |
root |
1.4 |
struct dynbuf |
17 |
root |
1.1 |
{ |
18 |
root |
1.4 |
protected: |
19 |
root |
1.1 |
struct chunk |
20 |
|
|
{ |
21 |
|
|
chunk *next; |
22 |
root |
1.5 |
int alloc; |
23 |
root |
1.1 |
int size; |
24 |
|
|
char data[0]; |
25 |
|
|
}; |
26 |
|
|
|
27 |
root |
1.6 |
char *ptr, *end; |
28 |
root |
1.1 |
int ext; |
29 |
|
|
int _size; |
30 |
|
|
|
31 |
|
|
chunk *first, *last; |
32 |
|
|
|
33 |
|
|
void _reserve (int size); |
34 |
root |
1.7 |
void _clear (); |
35 |
root |
1.1 |
void clear (); |
36 |
|
|
void finish (); |
37 |
|
|
|
38 |
|
|
public: |
39 |
|
|
|
40 |
root |
1.2 |
dynbuf (int initial = 4096, int extend = 16384); |
41 |
root |
1.1 |
~dynbuf (); |
42 |
|
|
|
43 |
root |
1.6 |
int size () const { return _size + (ptr - last->data); } |
44 |
|
|
bool empty () const { return !size (); } |
45 |
root |
1.1 |
|
46 |
|
|
void linearise (void *data); |
47 |
root |
1.6 |
char *linearise (); // does not 0-terminate(!) |
48 |
|
|
|
49 |
|
|
int room () const { return end - ptr; } |
50 |
root |
1.1 |
|
51 |
|
|
char *force (int size) |
52 |
|
|
{ |
53 |
root |
1.8 |
if (expect_false (ptr + size >= end)) |
54 |
root |
1.1 |
_reserve (size); |
55 |
|
|
|
56 |
|
|
return ptr; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
char *alloc (int size) |
60 |
|
|
{ |
61 |
|
|
char *res = force (size); |
62 |
root |
1.9 |
ptr += size; |
63 |
root |
1.1 |
return res; |
64 |
|
|
} |
65 |
|
|
|
66 |
root |
1.6 |
void fadd (char c) { *ptr++ = c; } |
67 |
root |
1.2 |
void fadd (unsigned char c) { fadd (char (c)); } |
68 |
root |
1.1 |
|
69 |
|
|
void add (const void *p, int len) |
70 |
|
|
{ |
71 |
|
|
memcpy (alloc (len), p, len); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
void add (char c) |
75 |
|
|
{ |
76 |
root |
1.3 |
alloc (1)[0] = c; |
77 |
root |
1.1 |
} |
78 |
|
|
|
79 |
|
|
void add (const char *s) |
80 |
|
|
{ |
81 |
|
|
add (s, strlen (s)); |
82 |
|
|
} |
83 |
|
|
|
84 |
root |
1.4 |
void add (const shstr &s) |
85 |
|
|
{ |
86 |
|
|
add (s.s, s.length ()); |
87 |
|
|
} |
88 |
root |
1.1 |
|
89 |
|
|
//TODO |
90 |
|
|
//void add_destructive (dynbuf &buf); |
91 |
|
|
|
92 |
|
|
dynbuf &operator << (char c) { add (c); return *this; } |
93 |
|
|
dynbuf &operator << (unsigned char c) { return *this << char (c); } |
94 |
|
|
dynbuf &operator << (const char *s) { add (s); return *this; } |
95 |
root |
1.6 |
dynbuf &operator << (const shstr &s) { add (s); return *this; } |
96 |
root |
1.4 |
dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; } |
97 |
|
|
|
98 |
|
|
operator std::string (); |
99 |
|
|
}; |
100 |
|
|
|
101 |
|
|
struct dynbuf_text : dynbuf |
102 |
|
|
{ |
103 |
|
|
dynbuf_text (int initial = 4096, int extend = 16384) |
104 |
|
|
: dynbuf (initial, extend) |
105 |
|
|
{ } |
106 |
|
|
|
107 |
|
|
using dynbuf::add; |
108 |
|
|
|
109 |
|
|
static const int max_sint32_size = 11; |
110 |
|
|
static const int max_sint64_size = 20; |
111 |
|
|
|
112 |
|
|
void add (sint32 i); |
113 |
|
|
void add (sint64 i); |
114 |
|
|
|
115 |
|
|
void printf (const char *format, ...); |
116 |
root |
1.1 |
}; |
117 |
|
|
|
118 |
|
|
#endif |
119 |
root |
1.6 |
|