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

Comparing deliantra/server/server/dynbuf.C (file contents):
Revision 1.5 by root, Sun Sep 10 15:59:57 2006 UTC vs.
Revision 1.8 by root, Mon Apr 23 18:21:54 2007 UTC

1#define __STDC_CONSTANT_MACROS
2#include <stdint.h>
3
4#ifndef UINT64_C
5# error missing c99 support, define UINT64_C yourself
6# define UINT64_C(c) c ## LL
7#endif
8
9#include "global.h" 1#include "global.h"
10 2
11#include <cstdio> 3#include <cstdio>
12 4
13dynbuf::dynbuf (int initial, int extend) 5dynbuf::dynbuf (int initial, int extend)
14{ 6{
15 _size = 0; 7 _size = 0;
16 ext = extend; 8 ext = extend;
9
17 first = last = (chunk *) new char[sizeof (chunk) + initial]; 10 first = last = (chunk *)salloc<char> (sizeof (chunk) + initial);
18 11 first->alloc = sizeof (chunk) + initial;
19 first->next = 0; 12 first->next = 0;
13
20 room = initial; 14 room = initial;
21 ptr = first->data; 15 ptr = first->data;
22} 16}
23 17
24dynbuf::~dynbuf () 18dynbuf::~dynbuf ()
31{ 25{
32 while (first) 26 while (first)
33 { 27 {
34 chunk *next = first->next; 28 chunk *next = first->next;
35 29
36 delete[](char *) first; 30 sfree<char> ((char *)first, first->alloc);
37 first = next; 31 first = next;
38 } 32 }
39} 33}
40 34
41void 35void
55 ext += ext >> 1; 49 ext += ext >> 1;
56 ext = (ext + 15) & ~15; 50 ext = (ext + 15) & ~15;
57 } 51 }
58 while (ext < size); 52 while (ext < size);
59 53
60 chunk *add = (chunk *) new char[sizeof (chunk) + ext]; 54 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + ext);
61 55 add->alloc = sizeof (chunk) + ext;
62 add->next = 0; 56 add->next = 0;
63 57
64 last->next = add; 58 last->next = add;
65 last = add; 59 last = add;
66 60
87{ 81{
88 if (first->next) 82 if (first->next)
89 { 83 {
90 finish (); 84 finish ();
91 85
92 chunk *add = (chunk *) new char[sizeof (chunk) + _size]; 86 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size);
93 87 add->alloc = sizeof (chunk) + _size;
94 add->next = 0; 88 add->next = 0;
89
95 linearise ((void *) add->data); 90 linearise ((void *)add->data);
96 clear (); 91 clear ();
97 92
98 first = last = add; 93 first = last = add;
99 ptr = last->data + _size; 94 ptr = last->data + _size;
100 _size = 0; 95 _size = 0;
102 } 97 }
103 98
104 return first->data; 99 return first->data;
105} 100}
106 101
102dynbuf::operator std::string ()
103{
104 // could optimise
105 return std::string (linearise (), size ());
106}
107
107void 108void
109dynbuf_text::printf (const char *format, ...)
110{
111 int len;
112
113 {
114 force (128);
115
116 va_list ap;
117 va_start (ap, format);
118 len = vsnprintf (ptr, room, format, ap);
119 va_end (ap);
120
121 assert (len >= 0); // shield against broken vsnprintf's
122
123 // was enough room available
124 if (len < room)
125 {
126 alloc (len);
127 return;
128 }
129 }
130
131 // longer, try harder
132 va_list ap;
133 va_start (ap, format);
134 vsnprintf (force (len + 1), len + 1, format, ap);
135 va_end (ap);
136
137 alloc (len);
138}
139
140void
108dynbuf::add (sint32 i) 141dynbuf_text::add (sint32 i)
109{ 142{
110 char buf[max_sint32_size]; 143 char buf[max_sint32_size];
111 char *p = buf + sizeof (buf); 144 char *p = buf + sizeof (buf);
112 char neg; 145 char neg;
113 146
138 171
139 add ((void *) p, buf + sizeof (buf) - p); 172 add ((void *) p, buf + sizeof (buf) - p);
140} 173}
141 174
142void 175void
143dynbuf::add (sint64 i) 176dynbuf_text::add (sint64 i)
144{ 177{
145 if (i > -10000000 && i < 10000000) 178 if (i > -10000000 && i < 10000000)
146 { 179 {
147 add (sint32 (i)); 180 add (sint32 (i));
148 return; 181 return;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines