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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|
/*
* PROPRIETARY INFORMATION. This software is proprietary to POWDER
* Development, and is not to be reproduced, transmitted, or disclosed
* in any way without written permission.
*
* Produced by: Jeff Lait
*
* POWDER Development
*
* NAME: buf.cpp ( POWDER Library, C++ )
*
* COMMENTS:
*/
#include "assert.h"
#include "buf.h"
#include <string.h>
#include <stdio.h>
#include <ctype.h>
int glbBufferCount = 0;
int
buf_numbufs()
{
return glbBufferCount;
}
class BUF_int
{
public:
BUF_int();
// Takes ownership of the given text, will delete[] it.
BUF_int(char *text, int len);
// Does not take ownership of the text
BUF_int(const char *text);
void incref();
void decref();
int refcnt() { return myRefCount; }
// Read methods works for shared objects
const char *buffer() const { return myData; }
int datalen() { return myDataLen; }
int strlen() { return ::strlen(myData); }
// Write methods require you have a uniqued buffer.
void resize(int newlen);
char *data() { return myData; }
private:
~BUF_int();
private:
int myRefCount;
char *myData;
// Allocated bytes for data. -1 means we don't own it.
int myDataLen;
};
BUF_int::BUF_int()
{
myData = (char *) "";
myDataLen = -1;
myRefCount = 0;
glbBufferCount++;
}
BUF_int::BUF_int(char *text, int len)
{
myData = text;
myDataLen = len;
myRefCount = 0;
glbBufferCount++;
}
BUF_int::BUF_int(const char *text)
{
myData = (char *) text;
myDataLen = -1;
myRefCount = 0;
glbBufferCount++;
}
BUF_int::~BUF_int()
{
UT_ASSERT(myRefCount <= 0);
if (myDataLen >= 0)
delete [] myData;
glbBufferCount--;
}
void
BUF_int::incref()
{
myRefCount++;
}
void
BUF_int::decref()
{
myRefCount--;
if (myRefCount <= 0)
delete this;
}
void
BUF_int::resize(int newlen)
{
UT_ASSERT(myRefCount <= 1);
char *text;
int slen;
// This is an inplace resize so we can never reduce!
slen = strlen() + 1;
if (slen > newlen)
newlen = slen;
// If this is a downsize, ignore it.
if (newlen < myDataLen)
return;
text = new char[newlen];
::strcpy(text, myData);
delete [] myData;
myData = text;
myDataLen = newlen;
}
///
/// BUF methods
///
BUF::BUF()
{
myBuffer = 0;
}
BUF::BUF(int len)
{
myBuffer = 0;
allocate(len);
}
BUF::~BUF()
{
if (myBuffer)
myBuffer->decref();
}
BUF::BUF(const BUF &buf)
{
myBuffer = 0;
*this = buf;
}
BUF &
BUF::operator=(const BUF &buf)
{
if (buf.myBuffer)
buf.myBuffer->incref();
if (myBuffer)
myBuffer->decref();
myBuffer = buf.myBuffer;
return *this;
}
const char *
BUF::buffer() const
{
if (!myBuffer)
return 0;
return myBuffer->buffer();
}
void
BUF::steal(char *text)
{
if (myBuffer)
myBuffer->decref();
if (!text)
myBuffer = 0;
else
{
myBuffer = new BUF_int(text, ::strlen(text)+1);
myBuffer->incref();
}
}
void
BUF::reference(const char *text)
{
if (myBuffer)
myBuffer->decref();
if (!text)
myBuffer = 0;
else
{
myBuffer = new BUF_int(text);
myBuffer->incref();
}
}
void
BUF::strcpy(const char *src)
{
if (myBuffer)
myBuffer->decref();
myBuffer = 0;
if (src)
{
char *text = new char [::strlen(src)+1];
::strcpy(text, src);
steal(text);
}
}
int
BUF::strlen() const
{
if (!myBuffer)
return 0;
return myBuffer->strlen();
}
int
BUF::strcmp(const char *cmp) const
{
if (!myBuffer)
{
if (!cmp)
return 0;
return -1;
}
return ::strcmp(buffer(), cmp);
}
void
BUF::strcat(const char *src)
{
// Trivial strcat.
if (!src)
return;
if (!myBuffer)
{
// Equivalent to strcpy
strcpy(src);
return;
}
// Find our total length
int srclen, mylen;
uniquify();
mylen = strlen();
srclen = ::strlen(src);
mylen += srclen + 1;
myBuffer->resize(mylen);
// Now safe...
::strcat(myBuffer->data(), src);
}
void
BUF::append(char c)
{
char s[2];
s[0] = c;
s[1] = '\0';
strcat(s);
}
void
BUF::clear()
{
uniquify();
evildata()[0] = '\0';
}
bool
BUF::isstring() const
{
if (!myBuffer)
return false;
if (buffer()[0])
return true;
return false;
}
char
BUF::lastchar(int nthlast) const
{
if (!myBuffer)
return 0;
int len;
len = strlen();
len -= nthlast + 1;
if (len < 0)
return 0;
return buffer()[len];
}
void
BUF::allocate(int len)
{
char *text = new char[len];
// We always want to be null terminated!
*text = 0;
if (myBuffer)
myBuffer->decref();
myBuffer = new BUF_int(text, len);
myBuffer->incref();
}
int
OURvsnprintf(char *str, size_t size, const char *format, va_list ap)
{
int result;
#ifdef WIN32
result = _vsnprintf(str, size, format, ap);
#else
va_list ap_copy;
// Apparently va_list can't be reused on modern compilers. Same
// compilers require support for va_copy which older compilers
// lack. *sigh*
va_copy(ap_copy, ap);
result = vsnprintf(str, size, format, ap_copy);
va_end(ap_copy);
#endif
return result;
}
int
BUF::vsprintf(const char *fmt, va_list ap)
{
int result;
int cursize;
// We wipe ourself out, so is safe to allocate.
cursize = 100;
while (1)
{
// Reallocate with the new (larger?) size.
allocate(cursize);
// Marker so we can tell if we overflowed vs having a bad
// format tag
myBuffer->data()[cursize-1] = '\0';
result = OURvsnprintf(myBuffer->data(), cursize, fmt, ap);
if (result < 0)
{
// Check if the null is still there, signifying something
// went wrong with formatting.
if (myBuffer->data()[cursize-1] == '\0')
{
// Treat this as the final buffer.
result = strlen();
break;
}
}
// We really must have a final null, thus this paranoia...
if (result < 0 || result > cursize-1)
{
cursize *= 2;
}
else
{
// Success!
break;
}
}
return result;
}
int
BUF::sprintf(const char *fmt, ...)
{
va_list marker;
int result;
va_start(marker, fmt);
result = vsprintf(fmt, marker);
va_end(marker);
return result;
}
int
BUF::appendSprintf(const char *fmt, ...)
{
BUF newtext;
va_list marker;
int result;
va_start(marker, fmt);
result = newtext.vsprintf(fmt, marker);
va_end(marker);
strcat(newtext);
return result;
}
char *
BUF::strdup() const
{
if (!myBuffer)
return ::strdup("");
return ::strdup(buffer());
}
char *
BUF::evildata()
{
UT_ASSERT(myBuffer);
UT_ASSERT(myBuffer->refcnt() <= 1);
return (char *) buffer();
}
void
BUF::uniquify()
{
if (!myBuffer)
{
// We want a writeable buffer
allocate(50);
}
else
{
// If ref count is 1 or 0, we are only reference, so good.
// UNLESS the current dude is a read only buffer.
if (myBuffer->refcnt() <= 1 && myBuffer->datalen() >= 0)
{
// Already writeable.
return;
}
// Copy the current buffer.
int len;
char *text;
if (myBuffer->datalen() >= 0)
len = myBuffer->datalen();
else
len = myBuffer->strlen() + 1;
text = new char[len];
::strcpy(text, myBuffer->buffer());
// Throw away this buffer and make a new one.
myBuffer->decref();
myBuffer = new BUF_int(text, len);
myBuffer->incref();
}
}
void
BUF::makeFileSafe()
{
uniquify();
// Strip out evil characters
unsigned char *c;
for (c = (unsigned char *)evildata(); *c; c++)
{
if (!isalnum(*c) && *c != '.')
*c = '_';
}
}
|