--- deliantra/server/include/dynbuf.h 2006/08/31 17:54:14 1.1 +++ deliantra/server/include/dynbuf.h 2007/04/23 18:21:54 1.5 @@ -4,17 +4,21 @@ #include #include +#include "shstr.h" + // this is a "buffer" that can grow fast // and is still somewhat space-efficient. // unlike obstacks or other data structures, // it never moves data around. basically, // this is a fast strstream without the overhead. -class dynbuf +struct dynbuf { +protected: struct chunk { chunk *next; + int alloc; int size; char data[0]; }; @@ -32,7 +36,7 @@ public: - dynbuf (int initial = 128, int extend = 128); + dynbuf (int initial = 4096, int extend = 16384); ~dynbuf (); int size () { return _size + (ptr - last->data); } @@ -59,7 +63,7 @@ } void fadd (char c) { --room; *ptr++ = c; } - void fadd (unsigned char c) { fadd (char (c));; } + void fadd (unsigned char c) { fadd (char (c)); } void add (const void *p, int len) { @@ -68,11 +72,7 @@ void add (char c) { - if (room < 1) - _reserve (1); - - room--; - *ptr++ = c; + alloc (1)[0] = c; } void add (const char *s) @@ -80,11 +80,10 @@ add (s, strlen (s)); } - static const int max_sint32_size = 11; - static const int max_sint64_size = 20; - - void add (sint32 i); - void add (sint64 i); + void add (const shstr &s) + { + add (s.s, s.length ()); + } //TODO //void add_destructive (dynbuf &buf); @@ -92,6 +91,26 @@ dynbuf &operator << (char c) { add (c); return *this; } dynbuf &operator << (unsigned char c) { return *this << char (c); } dynbuf &operator << (const char *s) { add (s); return *this; } + dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; } + + operator std::string (); +}; + +struct dynbuf_text : dynbuf +{ + dynbuf_text (int initial = 4096, int extend = 16384) + : dynbuf (initial, extend) + { } + + using dynbuf::add; + + static const int max_sint32_size = 11; + static const int max_sint64_size = 20; + + void add (sint32 i); + void add (sint64 i); + + void printf (const char *format, ...); }; #endif