ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/dynbuf.h
(Generate patch)

Comparing deliantra/server/include/dynbuf.h (file contents):
Revision 1.1 by root, Thu Aug 31 17:54:14 2006 UTC vs.
Revision 1.4 by root, Mon Apr 23 18:09:57 2007 UTC

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
13class dynbuf 15struct dynbuf
14{ 16{
17protected:
15 struct chunk 18 struct chunk
16 { 19 {
17 chunk *next; 20 chunk *next;
18 int size; 21 int size;
19 char data[0]; 22 char data[0];
30 void clear (); 33 void clear ();
31 void finish (); 34 void finish ();
32 35
33public: 36public:
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 () { return _size + (ptr - last->data); }
39 42
40 void linearise (void *data); 43 void linearise (void *data);
57 60
58 return res; 61 return res;
59 } 62 }
60 63
61 void fadd (char c) { --room; *ptr++ = c; } 64 void fadd (char c) { --room; *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 std::string &s) { add (s.data(), s.size ()); return *this; }
94
95 operator std::string ();
96};
97
98struct 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, ...);
95}; 113};
96 114
97#endif 115#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines