ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/dynbuf.h
Revision: 1.7
Committed: Thu May 3 09:26:45 2007 UTC (17 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.6: +1 -0 lines
Log Message:
only allow one range weapon to be applied at any one time, some dynbuf fixes

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 root 1.5 int alloc;
22 root 1.1 int size;
23     char data[0];
24     };
25    
26 root 1.6 char *ptr, *end;
27 root 1.1 int ext;
28     int _size;
29    
30     chunk *first, *last;
31    
32     void _reserve (int size);
33 root 1.7 void _clear ();
34 root 1.1 void clear ();
35     void finish ();
36    
37     public:
38    
39 root 1.2 dynbuf (int initial = 4096, int extend = 16384);
40 root 1.1 ~dynbuf ();
41    
42 root 1.6 int size () const { return _size + (ptr - last->data); }
43     bool empty () const { return !size (); }
44 root 1.1
45     void linearise (void *data);
46 root 1.6 char *linearise (); // does not 0-terminate(!)
47    
48     int room () const { return end - ptr; }
49 root 1.1
50     char *force (int size)
51     {
52 root 1.6 if (ptr + size >= end)
53 root 1.1 _reserve (size);
54    
55     return ptr;
56     }
57    
58     char *alloc (int size)
59     {
60     char *res = force (size);
61 root 1.6 ptr += size;
62 root 1.1 return res;
63     }
64    
65 root 1.6 void fadd (char c) { *ptr++ = c; }
66 root 1.2 void fadd (unsigned char c) { fadd (char (c)); }
67 root 1.1
68     void add (const void *p, int len)
69     {
70     memcpy (alloc (len), p, len);
71     }
72    
73     void add (char c)
74     {
75 root 1.3 alloc (1)[0] = c;
76 root 1.1 }
77    
78     void add (const char *s)
79     {
80     add (s, strlen (s));
81     }
82    
83 root 1.4 void add (const shstr &s)
84     {
85     add (s.s, s.length ());
86     }
87 root 1.1
88     //TODO
89     //void add_destructive (dynbuf &buf);
90    
91     dynbuf &operator << (char c) { add (c); return *this; }
92     dynbuf &operator << (unsigned char c) { return *this << char (c); }
93     dynbuf &operator << (const char *s) { add (s); return *this; }
94 root 1.6 dynbuf &operator << (const shstr &s) { add (s); return *this; }
95 root 1.4 dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; }
96    
97     operator std::string ();
98     };
99    
100     struct dynbuf_text : dynbuf
101     {
102     dynbuf_text (int initial = 4096, int extend = 16384)
103     : dynbuf (initial, extend)
104     { }
105    
106     using dynbuf::add;
107    
108     static const int max_sint32_size = 11;
109     static const int max_sint64_size = 20;
110    
111     void add (sint32 i);
112     void add (sint64 i);
113    
114     void printf (const char *format, ...);
115 root 1.1 };
116    
117     #endif
118 root 1.6