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, *end; |
23 | int room; |
|
|
24 | int ext; |
27 | int ext; |
25 | int _size; |
28 | int _size; |
26 | |
29 | |
27 | chunk *first, *last; |
30 | chunk *first, *last; |
28 | |
31 | |
… | |
… | |
30 | void clear (); |
33 | void clear (); |
31 | void finish (); |
34 | void finish (); |
32 | |
35 | |
33 | public: |
36 | public: |
34 | |
37 | |
35 | dynbuf (int initial = 128, int extend = 128); |
38 | dynbuf (int initial = 4096, int extend = 16384); |
36 | ~dynbuf (); |
39 | ~dynbuf (); |
37 | |
40 | |
38 | int size () { return _size + (ptr - last->data); } |
41 | int size () const { return _size + (ptr - last->data); } |
|
|
42 | bool empty () const { return !size (); } |
39 | |
43 | |
40 | void linearise (void *data); |
44 | void linearise (void *data); |
41 | char *linearise (); |
45 | char *linearise (); // does not 0-terminate(!) |
|
|
46 | |
|
|
47 | int room () const { return end - ptr; } |
42 | |
48 | |
43 | char *force (int size) |
49 | char *force (int size) |
44 | { |
50 | { |
45 | if (room < size) |
51 | if (ptr + size >= end) |
46 | _reserve (size); |
52 | _reserve (size); |
47 | |
53 | |
48 | return ptr; |
54 | return ptr; |
49 | } |
55 | } |
50 | |
56 | |
51 | char *alloc (int size) |
57 | char *alloc (int size) |
52 | { |
58 | { |
53 | char *res = force (size); |
59 | char *res = force (size); |
54 | |
|
|
55 | room -= size; |
|
|
56 | ptr += size; |
60 | ptr += size; |
57 | |
|
|
58 | return res; |
61 | return res; |
59 | } |
62 | } |
60 | |
63 | |
61 | void fadd (char c) { --room; *ptr++ = c; } |
64 | void fadd (char c) { *ptr++ = c; } |
62 | void fadd (unsigned char c) { fadd (char (c));; } |
65 | void fadd (unsigned char c) { fadd (char (c)); } |
63 | |
66 | |
64 | void add (const void *p, int len) |
67 | void add (const void *p, int len) |
65 | { |
68 | { |
66 | memcpy (alloc (len), p, len); |
69 | memcpy (alloc (len), p, len); |
67 | } |
70 | } |
68 | |
71 | |
69 | void add (char c) |
72 | void add (char c) |
70 | { |
73 | { |
71 | if (room < 1) |
74 | alloc (1)[0] = c; |
72 | _reserve (1); |
|
|
73 | |
|
|
74 | room--; |
|
|
75 | *ptr++ = c; |
|
|
76 | } |
75 | } |
77 | |
76 | |
78 | void add (const char *s) |
77 | void add (const char *s) |
79 | { |
78 | { |
80 | add (s, strlen (s)); |
79 | add (s, strlen (s)); |
81 | } |
80 | } |
82 | |
81 | |
83 | static const int max_sint32_size = 11; |
82 | void add (const shstr &s) |
84 | static const int max_sint64_size = 20; |
83 | { |
85 | |
84 | add (s.s, s.length ()); |
86 | void add (sint32 i); |
85 | } |
87 | void add (sint64 i); |
|
|
88 | |
86 | |
89 | //TODO |
87 | //TODO |
90 | //void add_destructive (dynbuf &buf); |
88 | //void add_destructive (dynbuf &buf); |
91 | |
89 | |
92 | dynbuf &operator << (char c) { add (c); return *this; } |
90 | dynbuf &operator << (char c) { add (c); return *this; } |
93 | dynbuf &operator << (unsigned char c) { return *this << char (c); } |
91 | dynbuf &operator << (unsigned char c) { return *this << char (c); } |
94 | dynbuf &operator << (const char *s) { add (s); return *this; } |
92 | dynbuf &operator << (const char *s) { add (s); return *this; } |
|
|
93 | dynbuf &operator << (const shstr &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 |
|
|
117 | |