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