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 "shstr.h" |
6 | |
8 | |
7 | // this is a "buffer" that can grow fast |
9 | // this is a "buffer" that can grow fast |
8 | // and is still somewhat space-efficient. |
10 | // and is still somewhat space-efficient. |
9 | // unlike obstacks or other data structures, |
11 | // unlike obstacks or other data structures, |
10 | // it never moves data around. basically, |
12 | // it never moves data around. basically, |
11 | // this is a fast strstream without the overhead. |
13 | // this is a fast strstream without the overhead. |
12 | |
14 | |
13 | class dynbuf |
15 | struct dynbuf |
14 | { |
16 | { |
|
|
17 | protected: |
15 | struct chunk |
18 | struct chunk |
16 | { |
19 | { |
17 | chunk *next; |
20 | chunk *next; |
|
|
21 | int alloc; |
18 | int size; |
22 | int size; |
19 | char data[0]; |
23 | char data[0]; |
20 | }; |
24 | }; |
21 | |
25 | |
22 | char *ptr; |
26 | char *ptr; |
… | |
… | |
30 | void clear (); |
34 | void clear (); |
31 | void finish (); |
35 | void finish (); |
32 | |
36 | |
33 | public: |
37 | public: |
34 | |
38 | |
35 | dynbuf (int initial = 128, int extend = 128); |
39 | dynbuf (int initial = 4096, int extend = 16384); |
36 | ~dynbuf (); |
40 | ~dynbuf (); |
37 | |
41 | |
38 | int size () { return _size + (ptr - last->data); } |
42 | int size () { return _size + (ptr - last->data); } |
39 | |
43 | |
40 | void linearise (void *data); |
44 | void linearise (void *data); |
… | |
… | |
57 | |
61 | |
58 | return res; |
62 | return res; |
59 | } |
63 | } |
60 | |
64 | |
61 | void fadd (char c) { --room; *ptr++ = c; } |
65 | void fadd (char c) { --room; *ptr++ = c; } |
62 | void fadd (unsigned char c) { fadd (char (c));; } |
66 | void fadd (unsigned char c) { fadd (char (c)); } |
63 | |
67 | |
64 | void add (const void *p, int len) |
68 | void add (const void *p, int len) |
65 | { |
69 | { |
66 | memcpy (alloc (len), p, len); |
70 | memcpy (alloc (len), p, len); |
67 | } |
71 | } |
68 | |
72 | |
69 | void add (char c) |
73 | void add (char c) |
70 | { |
74 | { |
71 | if (room < 1) |
75 | alloc (1)[0] = c; |
72 | _reserve (1); |
|
|
73 | |
|
|
74 | room--; |
|
|
75 | *ptr++ = c; |
|
|
76 | } |
76 | } |
77 | |
77 | |
78 | void add (const char *s) |
78 | void add (const char *s) |
79 | { |
79 | { |
80 | add (s, strlen (s)); |
80 | add (s, strlen (s)); |
81 | } |
81 | } |
82 | |
82 | |
83 | static const int max_sint32_size = 11; |
83 | void add (const shstr &s) |
84 | static const int max_sint64_size = 20; |
84 | { |
85 | |
85 | add (s.s, s.length ()); |
86 | void add (sint32 i); |
86 | } |
87 | void add (sint64 i); |
|
|
88 | |
87 | |
89 | //TODO |
88 | //TODO |
90 | //void add_destructive (dynbuf &buf); |
89 | //void add_destructive (dynbuf &buf); |
91 | |
90 | |
92 | dynbuf &operator << (char c) { add (c); return *this; } |
91 | dynbuf &operator << (char c) { add (c); return *this; } |
93 | dynbuf &operator << (unsigned char c) { return *this << char (c); } |
92 | dynbuf &operator << (unsigned char c) { return *this << char (c); } |
94 | dynbuf &operator << (const char *s) { add (s); return *this; } |
93 | dynbuf &operator << (const char *s) { add (s); return *this; } |
|
|
94 | dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; } |
|
|
95 | |
|
|
96 | operator std::string (); |
|
|
97 | }; |
|
|
98 | |
|
|
99 | struct dynbuf_text : dynbuf |
|
|
100 | { |
|
|
101 | dynbuf_text (int initial = 4096, int extend = 16384) |
|
|
102 | : dynbuf (initial, extend) |
|
|
103 | { } |
|
|
104 | |
|
|
105 | using dynbuf::add; |
|
|
106 | |
|
|
107 | static const int max_sint32_size = 11; |
|
|
108 | static const int max_sint64_size = 20; |
|
|
109 | |
|
|
110 | void add (sint32 i); |
|
|
111 | void add (sint64 i); |
|
|
112 | |
|
|
113 | void printf (const char *format, ...); |
95 | }; |
114 | }; |
96 | |
115 | |
97 | #endif |
116 | #endif |