[121] | 1 | // This file is a part of the Framsticks GDK. |
---|
| 2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
[109] | 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
| 4 | |
---|
| 5 | #include "sstring.h" |
---|
| 6 | |
---|
| 7 | #ifdef SSTRING_SIMPLE |
---|
| 8 | |
---|
| 9 | // simple sstring implementation using direct character arrays |
---|
| 10 | // - duplicate = copy all characters |
---|
| 11 | // - no mutex needed |
---|
| 12 | |
---|
| 13 | #include "sstring-simple.cpp" |
---|
| 14 | |
---|
| 15 | #else |
---|
| 16 | /////////////////////////////////////////////////////////////////////////// |
---|
| 17 | // old sstring implementation using SBuf references |
---|
| 18 | // - duplicate = copy buffer pointer |
---|
| 19 | // - mutex required to be thread safe |
---|
| 20 | |
---|
| 21 | #include <common/nonstd_stl.h> |
---|
| 22 | #include "extvalue.h" |
---|
| 23 | #include <assert.h> |
---|
| 24 | |
---|
| 25 | #ifdef MULTITHREADED |
---|
| 26 | #include <pthread.h> |
---|
| 27 | static pthread_mutex_t sstring_ref_lock=PTHREAD_MUTEX_INITIALIZER; |
---|
| 28 | #define REF_LOCK pthread_mutex_lock(&sstring_ref_lock); |
---|
| 29 | #define REF_UNLOCK pthread_mutex_unlock(&sstring_ref_lock) |
---|
| 30 | #else |
---|
| 31 | #define REF_LOCK |
---|
| 32 | #define REF_UNLOCK |
---|
| 33 | #endif |
---|
| 34 | |
---|
| 35 | static int guessMemSize(int request) |
---|
| 36 | { |
---|
| 37 | return request+min(request/2,10000)+8; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | SBuf::SBuf() |
---|
| 41 | { |
---|
[128] | 42 | txt=(char*)""; |
---|
[109] | 43 | size=used=0; |
---|
| 44 | refcount=1; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | SBuf::SBuf(int initsize) |
---|
| 48 | { |
---|
| 49 | size=guessMemSize(initsize); |
---|
| 50 | if (size>0) { txt=(char*)malloc(size+1); txt[0]=0; } |
---|
[128] | 51 | else txt=(char*)""; |
---|
[109] | 52 | used=0; |
---|
| 53 | refcount=1; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | SBuf::~SBuf() |
---|
| 57 | { |
---|
| 58 | freeBuf(); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | void SBuf::initEmpty() |
---|
| 62 | { |
---|
[128] | 63 | txt=(char*)""; |
---|
[109] | 64 | used=size=0; |
---|
| 65 | refcount=1; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | void SBuf::freeBuf() |
---|
| 69 | { |
---|
| 70 | if (!size) return; |
---|
| 71 | free(txt); used=0; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | void SBuf::copyFrom(const char *ch,int chlen) |
---|
| 75 | { |
---|
| 76 | if (chlen==-1) chlen=strlen(ch); |
---|
| 77 | if (chlen>0) |
---|
| 78 | { |
---|
| 79 | if (chlen<size) |
---|
| 80 | { |
---|
| 81 | memcpy(txt,ch,chlen); |
---|
| 82 | } |
---|
| 83 | else |
---|
| 84 | { |
---|
| 85 | size=guessMemSize(chlen); |
---|
| 86 | char *newtxt=(char*)malloc(size+1); |
---|
| 87 | memcpy(newtxt,ch,chlen); |
---|
| 88 | free(txt); |
---|
| 89 | txt=newtxt; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | txt[chlen]=0; |
---|
| 93 | used=chlen; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | void SBuf::append(const char *ch,int chlen) |
---|
| 97 | { // doesn't check anything! |
---|
| 98 | memcpy(txt+used,ch,chlen); |
---|
| 99 | used+=chlen; |
---|
| 100 | txt[used]=0; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void SBuf::ensureSize(int needed) |
---|
| 104 | { |
---|
| 105 | if (size>=needed) return; |
---|
| 106 | needed=guessMemSize(needed); |
---|
| 107 | txt=(char*)realloc(txt,needed+1); |
---|
| 108 | size=needed; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | ///////////////////////////////////////////// |
---|
| 112 | |
---|
| 113 | SString::SString() |
---|
| 114 | { |
---|
| 115 | initEmpty(); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | SString::~SString() |
---|
| 119 | { |
---|
| 120 | REF_LOCK; |
---|
| 121 | detach(); |
---|
| 122 | REF_UNLOCK; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | SString::SString(int x) |
---|
| 126 | { |
---|
| 127 | buf=new SBuf(x); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | SString::SString(const char *t,int t_len) |
---|
| 131 | { |
---|
| 132 | initEmpty(); |
---|
| 133 | if (!t) return; |
---|
| 134 | copyFrom(t,t_len); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | SString::SString(const SString &from) |
---|
| 138 | { |
---|
| 139 | if (from.buf==&SBuf::empty()) |
---|
| 140 | buf=&SBuf::empty(); |
---|
| 141 | else |
---|
| 142 | { |
---|
| 143 | REF_LOCK; |
---|
| 144 | buf=from.buf; |
---|
| 145 | if (buf->size) |
---|
| 146 | buf->refcount++; |
---|
| 147 | REF_UNLOCK; |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | void SString::initEmpty() |
---|
| 152 | { |
---|
| 153 | buf=&SBuf::empty(); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | void SString::memoryHint(int howbig) |
---|
| 157 | { |
---|
| 158 | detachCopy(howbig); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | void SString::detachEmpty(int ensuresize) |
---|
| 162 | { |
---|
| 163 | if (buf==&SBuf::empty()) { buf=new SBuf(ensuresize); return; } |
---|
| 164 | if (buf->refcount<2) buf->ensureSize(ensuresize); |
---|
| 165 | else |
---|
| 166 | { |
---|
| 167 | buf->refcount--; |
---|
| 168 | buf=new SBuf(ensuresize); |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | void SString::detach() |
---|
| 173 | { |
---|
| 174 | if (buf==&SBuf::empty()) return; |
---|
| 175 | if (!--buf->refcount) delete buf; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | void SString::detachCopy(int ensuresize) |
---|
| 179 | { |
---|
| 180 | if (buf==&SBuf::empty()) { buf=new SBuf(ensuresize); return; } |
---|
| 181 | if (buf->refcount<2) |
---|
| 182 | { |
---|
| 183 | buf->ensureSize(ensuresize); |
---|
| 184 | return; |
---|
| 185 | } |
---|
| 186 | buf->refcount--; |
---|
| 187 | SBuf *newbuf=new SBuf(ensuresize); |
---|
| 188 | newbuf->copyFrom(buf->txt,min(ensuresize,buf->used)); |
---|
| 189 | buf=newbuf; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | char *SString::directWrite(int ensuresize) |
---|
| 193 | { |
---|
| 194 | if (ensuresize<0) ensuresize=len(); |
---|
| 195 | REF_LOCK; |
---|
| 196 | detachCopy(ensuresize); |
---|
| 197 | REF_UNLOCK; |
---|
| 198 | appending=buf->used; |
---|
| 199 | return buf->txt; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | /* |
---|
| 203 | char *SString::directWrite() |
---|
| 204 | { |
---|
| 205 | return directWrite(buf->used); |
---|
| 206 | } |
---|
| 207 | */ |
---|
| 208 | char *SString::directAppend(int maxappend) |
---|
| 209 | { |
---|
| 210 | REF_LOCK; |
---|
| 211 | detachCopy(buf->used+maxappend); |
---|
| 212 | REF_UNLOCK; |
---|
| 213 | appending=buf->used; |
---|
| 214 | return buf->txt+appending; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | void SString::endWrite(int newlength) |
---|
| 218 | { |
---|
| 219 | if (newlength<0) newlength=strlen(buf->txt); |
---|
| 220 | else buf->txt[newlength]=0; |
---|
| 221 | buf->used=newlength; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | void SString::endAppend(int newappend) |
---|
| 225 | { |
---|
| 226 | if (newappend<0) newappend=strlen(buf->txt+appending); |
---|
| 227 | else buf->txt[appending+newappend]=0; |
---|
| 228 | buf->used=appending+newappend; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | ////////////// append ///////////////// |
---|
| 232 | |
---|
| 233 | void SString::operator+=(const char *s) |
---|
| 234 | { |
---|
| 235 | if (!s) return; |
---|
| 236 | int x=strlen(s); |
---|
| 237 | if (!x) return; |
---|
| 238 | append(s,x); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | void SString::append(const char *txt,int count) |
---|
| 242 | { |
---|
| 243 | if (!count) return; |
---|
| 244 | REF_LOCK; |
---|
| 245 | detachCopy(buf->used+count); |
---|
| 246 | REF_UNLOCK; |
---|
| 247 | buf->append(txt,count); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | void SString::operator+=(const SString&s) |
---|
| 251 | { |
---|
| 252 | append((const char*)s,s.len()); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | SString SString::operator+(const SString& s) const |
---|
| 256 | { |
---|
| 257 | SString ret(*this); |
---|
| 258 | ret+=s; |
---|
| 259 | return ret; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | ///////////////////////////// |
---|
| 263 | |
---|
| 264 | void SString::copyFrom(const char *ch,int chlen) |
---|
| 265 | { |
---|
| 266 | if (!ch) chlen=0; |
---|
| 267 | else if (chlen<0) chlen=strlen(ch); |
---|
| 268 | REF_LOCK; |
---|
| 269 | detachEmpty(chlen); |
---|
| 270 | REF_UNLOCK; |
---|
| 271 | memcpy(buf->txt,ch,chlen); |
---|
| 272 | buf->txt[chlen]=0; buf->used=chlen; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | void SString::operator=(const char *ch) |
---|
| 276 | { |
---|
| 277 | copyFrom(ch); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | void SString::operator=(const SString&s) |
---|
| 281 | { |
---|
| 282 | if (s.buf==buf) return; |
---|
| 283 | REF_LOCK; |
---|
| 284 | detach(); |
---|
| 285 | buf=s.buf; |
---|
| 286 | if (buf->size) buf->refcount++; |
---|
| 287 | REF_UNLOCK; |
---|
| 288 | } |
---|
| 289 | /////////////////////////////////////// |
---|
| 290 | |
---|
| 291 | SString SString::substr(int begin, int length) const |
---|
| 292 | { |
---|
| 293 | if (begin<0) { length+=begin; begin=0; } |
---|
| 294 | if (length>=(len()-begin)) length=len()-begin; |
---|
| 295 | if (length<=0) return SString(); |
---|
| 296 | if (length==len()) return *this; |
---|
| 297 | return SString((*this)(begin),length); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | /////////////////////////////////////// |
---|
| 301 | |
---|
| 302 | int SString::equals(const SString& s) const |
---|
| 303 | { |
---|
| 304 | if (s.buf==buf) return 1; |
---|
| 305 | return !strcmp(buf->txt,s.buf->txt); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | /////////////////////////////////////// |
---|
| 309 | |
---|
| 310 | int SString::indexOf(int character,int start) const |
---|
| 311 | { |
---|
| 312 | const char *found=strchr(buf->txt+start,character); |
---|
| 313 | return found?found-buf->txt:-1; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | int SString::indexOf(const char *substring,int start) const |
---|
| 317 | { |
---|
| 318 | char *found=strstr(buf->txt+start,substring); |
---|
| 319 | return found?found-buf->txt:-1; |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | int SString::indexOf(const SString & substring,int start) const |
---|
| 323 | { |
---|
| 324 | char *found=strstr(buf->txt+start,((const char*)substring)); |
---|
| 325 | return found?found-buf->txt:-1; |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | int SString::getNextToken (int& pos,SString &token,char separator) const |
---|
| 329 | { |
---|
| 330 | if (pos>=len()) {token=0;return 0;} |
---|
| 331 | int p1=pos,p2; |
---|
| 332 | const char *t1=buf->txt+pos; |
---|
| 333 | const char *t2=strchr(t1,separator); |
---|
| 334 | if (t2) pos=(p2=(t2-buf->txt))+1; else p2=pos=len(); |
---|
| 335 | strncpy(token.directWrite(p2-p1),t1,p2-p1); |
---|
| 336 | token.endWrite(p2-p1); |
---|
| 337 | return 1; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | int SString::startsWith(const char *pattern) const |
---|
| 341 | { |
---|
| 342 | const char *t=(const char*)(*this); |
---|
| 343 | for (;*pattern;pattern++,t++) |
---|
| 344 | if (*t != *pattern) return 0; |
---|
| 345 | return 1; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | SString SString::valueOf(int i) |
---|
| 349 | { |
---|
| 350 | return SString::sprintf("%d",i); |
---|
| 351 | } |
---|
| 352 | SString SString::valueOf(long i) |
---|
| 353 | { |
---|
| 354 | return SString::sprintf("%d",i); |
---|
| 355 | } |
---|
| 356 | SString SString::valueOf(double d) |
---|
| 357 | { |
---|
| 358 | SString tmp=SString::sprintf("%.15g",d); |
---|
| 359 | if ((!strchr(tmp,'.'))&&(!strchr(tmp,'e'))) tmp+=".0"; |
---|
| 360 | return tmp; |
---|
| 361 | } |
---|
| 362 | SString SString::valueOf(const SString& s) |
---|
| 363 | { |
---|
| 364 | return s; |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | #ifdef LINUX |
---|
| 368 | #define VSNPRINTF_RETURNS_REQUIRED_SIZE |
---|
| 369 | #endif |
---|
| 370 | #if defined _WIN32 && !defined __BORLANDC__ |
---|
| 371 | #define USE_VSCPRINTF |
---|
| 372 | #endif |
---|
| 373 | |
---|
| 374 | |
---|
| 375 | #if 0 //testing _vscprintf |
---|
| 376 | #define USE_VSCPRINTF |
---|
| 377 | int _vscprintf(const char *format,va_list argptr) |
---|
| 378 | { |
---|
| 379 | return vsnprintf("",0,format,argptr); |
---|
| 380 | } |
---|
| 381 | #endif |
---|
| 382 | |
---|
| 383 | SString SString::sprintf(const char* format, ...) |
---|
| 384 | { |
---|
| 385 | int n, size = 30; |
---|
| 386 | va_list ap; |
---|
| 387 | |
---|
| 388 | SString ret; |
---|
| 389 | |
---|
| 390 | #ifdef USE_VSCPRINTF |
---|
| 391 | va_start(ap, format); |
---|
| 392 | size=_vscprintf(format, ap); |
---|
| 393 | va_end(ap); |
---|
| 394 | #endif |
---|
| 395 | |
---|
| 396 | while (1) |
---|
| 397 | { |
---|
| 398 | char* p=ret.directWrite(size); |
---|
| 399 | assert(p!=NULL); |
---|
| 400 | size=ret.directMaxLen()+1; |
---|
| 401 | /* Try to print in the allocated space. */ |
---|
| 402 | va_start(ap, format); |
---|
| 403 | n = vsnprintf(p, size, format, ap); |
---|
| 404 | va_end(ap); |
---|
| 405 | /* If that worked, return the string. */ |
---|
| 406 | if (n > -1 && n < size) |
---|
| 407 | { |
---|
| 408 | ret.endWrite(n); |
---|
| 409 | return ret; |
---|
| 410 | } |
---|
| 411 | /* Else try again with more space. */ |
---|
| 412 | #ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE |
---|
| 413 | if (n > -1) /* glibc 2.1 */ |
---|
| 414 | size = n; /* precisely what is needed */ |
---|
| 415 | else /* glibc 2.0 */ |
---|
| 416 | #endif |
---|
| 417 | size *= 2; /* twice the old size */ |
---|
| 418 | } |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | SString &SString::empty() |
---|
| 422 | { |
---|
| 423 | static SString empty; |
---|
| 424 | return empty; |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | SBuf &SBuf::empty() |
---|
| 428 | { |
---|
| 429 | static SBuf empty; |
---|
| 430 | return empty; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | #endif //#ifdef SSTRING_SIMPLE |
---|