1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "cbuffer_t.h"
#include "simstring.h"
#include "../simtypes.h"
cbuffer_t::cbuffer_t() :
capacity(256),
size(0),
buf(new char[capacity])
{
buf[0] = '\0';
}
cbuffer_t::~cbuffer_t()
{
free();
}
void cbuffer_t::clear()
{
buf[0] = '\0';
size = 0;
}
cbuffer_t::cbuffer_t (const cbuffer_t& cbx)
{
copy( cbx );
}
cbuffer_t& cbuffer_t::operator= (const cbuffer_t& cbx)
{
if ( this != &cbx )
{
free();
copy( cbx );
}
return *this;
}
void cbuffer_t::copy (const cbuffer_t& cbx)
{
capacity = cbx.capacity;
size = cbx.size;
buf = new char[capacity];
memcpy( buf, cbx.buf, size + 1 );
}
void cbuffer_t::free ()
{
delete [] buf;
}
void cbuffer_t::append(const char * text)
{
size_t const n = strlen(text);
extend(n);
memcpy(buf + size, text, n + 1);
size += n;
}
void cbuffer_t::append (const char* text, size_t maxchars)
{
size_t const n = min( strlen( text ), maxchars );
extend( n );
memcpy( buf + size, text, n );
size += n;
buf[size] = '\0'; // Ensure buffer is null terminated
}
void cbuffer_t::append(double n,int decimals)
{
char tmp[32];
number_to_string( tmp, n, decimals );
append(tmp);
}
const char* cbuffer_t::get_str () const
{
return buf;
}
/* this is a vsnprintf which can always process positional parameters
* like "%2$i: %1$s"
* WARNING: posix specification as well as this function always assumes that
* ALL parameter are either positional or not!!!
*
* When numbered argument specifications are used, specifying the Nth argument requires that all the
* leading arguments, from the first to the (N-1)th, are specified in the format string.
*
* ATTENTION: no support for positional precision (which are not used in simutrans anyway!
*/
static int my_vsnprintf(char *buf, size_t n, const char* fmt, va_list ap )
{
#if defined _MSC_FULL_VER && _MSC_FULL_VER >= 140050727 && !defined __WXWINCE__
// this MSC function can handle positional parameters since 2008
return _vsprintf_p(buf, n, fmt, ap);
#else
#if !defined(HAVE_UNIX98_PRINTF)
// this function cannot handle positional parameters
if( const char *c=strstr( fmt, "%1$" ) ) {
// but they are requested here ...
// our routine can only handle max. 9 parameters
char pos[6];
static char format_string[256];
char *cfmt = format_string;
static char buffer[16000]; // the longest possible buffer ...
int count = 0;
for( ; c && count<9; count++ ) {
sprintf( pos, "%%%i$", count+1 );
c = strstr( fmt, pos );
if( c ) {
// extend format string, using 1 as marke between strings
if( count ) {
*cfmt++ = '\01';
}
*cfmt++ = '%';
// now find the end
c += 3;
int len = strspn( c, "+-0123456789 #.hlI" )+1;
while( len-->0 ) {
*cfmt++ = *c++;
}
}
}
*cfmt = 0;
// now printf into buffer
int result = vsnprintf( buffer, 16000, format_string, ap );
if( result<0 || result>=16000 ) {
*buf = 0;
return 0;
}
// check the length
result += strlen(fmt);
if( (size_t)result > n ) {
// increase the size please ...
return result;
}
// we have enough size: copy everything together
*buf = 0;
char *cbuf = buf;
cfmt = const_cast<char *>(fmt); // cast is save, as the string is not modified
while( *cfmt!=0 ) {
while( *cfmt!='%' && *cfmt ) {
*cbuf++ = *cfmt++;
}
if( *cfmt==0 ) {
break;
}
// get the nth argument
char *carg = buffer;
int current = cfmt[1]-'1';
for( int j=0; j<current; j++ ) {
while( *carg && *carg!='\01' ) {
carg ++;
}
assert( *carg );
carg ++;
}
while( *carg && *carg!='\01' ) {
*cbuf++ = *carg++;
}
// jump rest
cfmt += 3;
cfmt += strspn( cfmt, "+-0123456789 #.hlI" )+1;
}
*cbuf = 0;
return cbuf-buf;
}
// no positional parameters: use standard vsnprintf
#endif
// normal posix system can handle positional parameters
return vsnprintf(buf, n, fmt, ap);
#endif
}
void cbuffer_t::printf(const char* fmt, ...)
{
for (;;) {
size_t const n = capacity - size;
size_t inc;
va_list ap;
va_start(ap, fmt);
int const count = my_vsnprintf(buf + size, n, fmt, ap );
va_end(ap);
if (count < 0) {
#ifdef _WIN32
inc = capacity;
#else
// error
buf[size] = '\0';
break;
#endif
} else if ((size_t)count < n) {
size += count;
break;
} else {
// Make room for the string.
inc = (size_t)count;
}
extend(inc);
}
}
void cbuffer_t::extend(unsigned int min_free_space)
{
if (min_free_space >= capacity - size) {
unsigned int by_amount = min_free_space + 1 - (capacity - size);
if (by_amount < capacity) {
// At least double the size of the buffer.
by_amount = capacity;
}
unsigned int new_capacity = capacity + by_amount;
char *new_buf = new char [new_capacity];
memcpy( new_buf, buf, capacity );
delete [] buf;
buf = new_buf;
capacity = new_capacity;
}
}
|