ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/dynbuf.h
Revision: 1.10
Committed: Sun May 27 23:49:49 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.9: +14 -6 lines
Log Message:
more ยต-opts

File Contents

# User Rev Content
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 root 1.10 char *falloc (int size)
60 root 1.1 {
61 root 1.10 char *res = ptr;
62 root 1.9 ptr += size;
63 root 1.1 return res;
64     }
65    
66 root 1.10 char *alloc (int size)
67     {
68     force (size);
69     return falloc (size);
70     }
71    
72 root 1.6 void fadd (char c) { *ptr++ = c; }
73 root 1.2 void fadd (unsigned char c) { fadd (char (c)); }
74 root 1.10 void fadd (const void *p, int len)
75     {
76     memcpy (falloc (len), p, len);
77     }
78 root 1.1
79     void add (const void *p, int len)
80     {
81 root 1.10 force (len);
82     fadd (p, len);
83 root 1.1 }
84    
85     void add (char c)
86     {
87 root 1.3 alloc (1)[0] = c;
88 root 1.1 }
89    
90     void add (const char *s)
91     {
92     add (s, strlen (s));
93     }
94    
95 root 1.4 void add (const shstr &s)
96     {
97     add (s.s, s.length ());
98     }
99 root 1.1
100     //TODO
101     //void add_destructive (dynbuf &buf);
102    
103     dynbuf &operator << (char c) { add (c); return *this; }
104     dynbuf &operator << (unsigned char c) { return *this << char (c); }
105     dynbuf &operator << (const char *s) { add (s); return *this; }
106 root 1.6 dynbuf &operator << (const shstr &s) { add (s); return *this; }
107 root 1.4 dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; }
108    
109     operator std::string ();
110     };
111    
112     struct dynbuf_text : dynbuf
113     {
114     dynbuf_text (int initial = 4096, int extend = 16384)
115     : dynbuf (initial, extend)
116     { }
117    
118     using dynbuf::add;
119    
120     void add (sint32 i);
121     void add (sint64 i);
122    
123     void printf (const char *format, ...);
124 root 1.1 };
125    
126     #endif
127 root 1.6