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