ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/dynbuf.h
Revision: 1.4
Committed: Mon Apr 23 18:09:57 2007 UTC (17 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.3: +28 -6 lines
Log Message:
- add format utility function.
- split dynbuf into dynbuf and dynbuf_text.
- use dynbuf_text for examine strings instead of
  outputting each line seperately. tried to use stringstreams
  but they add insane overheads (as does std::string, but less so).

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