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