source: cpp/frams/util/sstring.h @ 666

Last change on this file since 666 was 395, checked in by Maciej Komosinski, 10 years ago

Fixed int/bool warnings

  • Property svn:eol-style set to native
File size: 7.3 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#ifndef _SSTRING_H_
6#define _SSTRING_H_
7
8#define SSTRING_SIMPLE
9
10#ifdef SSTRING_SIMPLE
11
12// simple sstring implementation using direct character arrays
13// - duplicate = copy all characters
14// - no mutex needed
15
16#include "sstring-simple.h"
17
18#else
19///////////////////////////////////////////////////////////////////////////
20// old sstring implementation using SBuf references
21// - duplicate = copy buffer pointer
22// - mutex required to be thread safe
23
24#include <stdint.h>
25#include <string.h>
26#include <stdlib.h>
27#include <stdio.h>
28
29class ExtValue;  //this include would result in recurrent inclusion: #include "extvalue.h"
30class ExtObject;
31
32class SBuf
33{
34char *txt;
35int used;       ///< data size
36int size;       ///< buffer size, not including \0, special case: 0==buffer not allocated
37int refcount;   ///< buffer is used by 'refcount' objects.
38void initEmpty();
39void ensureSize(int wantsize);
40void copyFrom(const char* ch, int chlen=-1);
41void freeBuf();
42void append(const char* ch, int chlen=-1);
43static SBuf &empty();
44SBuf(int initsize);
45friend class SString;
46SBuf(const SBuf& b) {}
47public:
48SBuf();
49~SBuf();
50uint32_t hash() const; // 32-bit FNV-1 hash -> http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash
51};
52
53/// (not so) simple text string class
54
55class SString
56{
57private:
58SBuf *buf;      ///< buffer
59int appending;  ///< append mode, changes can occur after character # 'appending'
60//int memhint;
61
62void initEmpty();
63int guessMemSize(int request);
64void copyFrom(SString &from); ///< copy from SString, reference if possible
65void detach(); ///< detach from shared buffer, if any
66void detachEmpty(int ensuresize=0); ///< detach and make empty
67void detachCopy(int ensuresize=0); ///< detach and make private copy
68
69public:
70SString(); ///< make an empty string
71SString(const char*t,int t_len=-1); ///< make a string from char*
72SString(int x); ///< string with initial buffer size
73SString(const SString& from); ///< duplicate string
74SString(SString&& from); ///< move
75~SString();
76
77void copyFrom(const char* ch, int chlen=-1); ///< copy string, length of -1 == unknown
78
79void* operator new(size_t s, void* mem) {return mem;}
80#ifdef _MSC_VER
81void operator delete(void* mem, void* t) {}
82#endif
83void* operator new(size_t s) {return malloc(sizeof(SString));}
84void operator delete(void* mem) {free(mem);}
85
86int len() const {return buf->used;} ///< get string length
87void shrink(); ///< free unnecessary buffer
88
89/// after this call, you can modify sstring directly.
90/// returned value is the pointer to the internal buffer.
91/// <B>ensuresize</B> is minimal value of bytes you need,
92/// the buffer will be resized as needed.
93/// all "direct" operations have to leave the buffer with trailing '\0'
94/// at the end. endWrite() will search for this value in order to determine
95/// new string length.
96/// <P>Sample:<CODE>
97/// SString t;
98/// sprintf(t.directWrite(50),"a=%d,b=%f",a,b);
99/// t.endWrite();</CODE>
100char *directWrite(int ensuresize=-1);
101//char *directWrite();
102/// like directWrite, but it returns the pointer to the first char after current string
103/// for easy appending. <B>maxappend</B> is minimum of character in buffer
104/// that can be appended after this call.
105/// <P>Sample:<CODE>
106/// SString t;
107/// sprintf(t.directAppend(10),"c=%d",c);
108/// t.endAppend();</CODE>
109char *directAppend(int maxappend=0);
110/// update string length, after directWrite.
111/// you don't have to to call endWrite after directWrite if the string's length doesn't change.
112/// optional <B>newlength</B> parameter gives a chance to further optimize
113/// this operation if you know exact length of resulting string.
114/// <P>Sample:<CODE>
115/// SString t("samplestring");
116/// strncpy(t.directWrite(50),src,bytecount);
117/// t.endWrite(bytecount);</CODE>
118void endWrite(int newlength=-1);
119/// update string length, after directAppend.
120/// you will usually need to call endAppend (or endWrite) after directAppend,
121/// because the purpose of directAppend is to change string's length.
122/// optional <B>appendlength</B> parameter gives a chance to further optimize
123/// this operation if you know exact length of the appended string.
124/// <P>Sample:<CODE>
125/// SString t("samplestring");
126/// strncpy(t.directAppend(50),src,bytecount);
127/// t.endAppend(bytecount);</CODE>
128void endAppend(int appendlength=-1);
129/// argument is the amount of memory, that will be probably used
130/// by this string instance. string can use this value
131/// to optimize memory allocation (bigger chunks will be allocated).
132void memoryHint(int howbig);
133int directMaxLen() {return buf->size;} ///< when called after directWrite: max number of characters allowed (can be more than requested)
134
135/// find a character in SString.
136/// return index if the character was found or -1 otherwise.
137int indexOf(int character,int start=0) const;
138
139/// find a substring.
140/// return index if the substring was found or -1 otherwise.
141int indexOf(const char *substring,int start=0) const;
142
143/// find a substring.
144/// return index if the substring was found or -1 otherwise.
145int indexOf(const SString & substring,int start=0) const;
146
147const char* c_str() const {return buf->txt;} ///< get SString's readonly buffer
148//operator char*() {detachCopy(len()); return buf->txt;} ///< get SString's writable buffer
149void operator=(const char*t); ///< assign from const char*
150//void operator=(int x) {free(txt);nowy(x);} ///< clear string and make new empty one
151void operator=(const SString &s);
152
153void append(const char *txt,int count);
154SString operator+(const SString &s) const;
155void operator+=(int x); ///< append x spaces after current string
156void operator+=(const char*); ///< append char* contents
157void operator+=(const SString&); ///< append other SString
158
159bool equals(const SString &s) const; ///< TRUE if equal
160bool operator==(const SString &s) const {return equals(s);} ///< TRUE if equal
161bool operator!=(const SString &s) const {return !equals(s);}
162const char* operator()(int p) const {return buf->txt+p;} ///< pointer to p'th character in SString
163char operator[](int i) const {return buf->txt[i];} ///< get char like in array
164
165/// return a substring of the current string
166SString substr(int begin, int length=1<<30) const;
167
168/// simple tokenization:
169/// starting at <B>pos</B>, get next substring delimited by <B>separator</B> character
170/// and put it in output parameter <B>token</B>.
171/// <B>pos</B> is moved accordingly.
172/// returns <B>false</B> if no more tokens are available or <B>true</B> otherwise.
173bool getNextToken(int& pos,SString &token,char separator) const;
174
175void operator+=(char ch) {directAppend(1)[0]=ch;endAppend(1);} ///< append single character
176
177bool startsWith(const char *pattern) const;
178char charAt(int pos) const {return buf->txt[pos];}
179uint32_t hash() const {return buf->hash();}
180
181static SString valueOf(int);
182static SString valueOf(long);
183static SString valueOf(double);
184static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu
185static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu
186static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu
187static SString sprintf(const char* format, ...);
188
189static SString &empty();
190};
191
192#endif //#ifdef SSTRING_SIMPLE
193
194#endif
Note: See TracBrowser for help on using the repository browser.