source: cpp/frams/util/sstring.cpp @ 347

Last change on this file since 347 was 347, checked in by Maciej Komosinski, 9 years ago

Using memmove instead of memcpy because overlapping source/destination can happen in SString copy/append

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