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