source: cpp/gdk/sstring.h @ 88

Last change on this file since 88 was 81, checked in by Maciej Komosinski, 12 years ago

improved parsing of properties (e.g. in f0 genotypes)

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
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
12class SBuf
13{
14char *txt;
15int used;       ///< data size
16int size;       ///< buffer size, not including \0, special case: 0==buffer not allocated
17int refcount;   ///< buffer is used by 'refcount' objects.
18void initEmpty();
19void ensureSize(int wantsize, int memhint);
20void copyFrom(const char* ch, int chlen=-1, int memhint=-1);
21void freeBuf();
22void append(const char* ch, int chlen=-1);
23static SBuf &empty();
24SBuf(int initsize, int memhint=-1);
25friend class SString;
26SBuf(const SBuf& b) {}
27public:
28SBuf();
29~SBuf();
30};
31
32/// (not so) simple text string class
33
34class SString
35{
36private:
37SBuf *buf;      ///< buffer
38int appending;  ///< append mode, changes can occur after character # 'appending'
39int memhint;    ///< sstring will allocate bigger chunks of memory
40
41void initEmpty();
42int guessMemSize(int request);
43void copyFrom(SString &from); ///< copy from SString, reference if possible
44void detach(); ///< detach from shared buffer, if any
45void detachEmpty(int ensuresize=0); ///< detach and make empty
46void detachCopy(int ensuresize=0); ///< detach and make private copy
47
48public:
49SString(); ///< make an empty string
50SString(const char*t,int t_len=-1); ///< make a string from char*
51SString(int x); ///< string with initial buffer size
52SString(const SString& from); ///< duplicate string
53~SString();
54
55void copyFrom(const char* ch, int chlen=-1); ///< copy string, length of -1 == unknown
56
57void* operator new(size_t s, void* mem) {return mem;}
58#ifdef _MSC_VER
59void operator delete(void* mem, void* t) {}
60#endif
61void* operator new(size_t s) {return malloc(sizeof(SString));}
62void operator delete(void* mem) {free(mem);}
63
64int len() const {return buf->used;} ///< get string length
65void 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>
78char *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>
87char *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>
96void 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>
106void 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).
110void memoryHint(int howbig);
111
112/// find a character in SString.
113/// return index if the character was found or -1 otherwise.
114int indexOf(int character,int start=0) const;
115
116/// find a substring.
117/// return index if the substring was found or -1 otherwise.
118int indexOf(const char *substring,int start=0) const;
119
120/// find a substring.
121/// return index if the substring was found or -1 otherwise.
122int indexOf(const SString & substring,int start=0) const;
123
124operator const char*() const {return buf->txt;} ///< get SString's readonly buffer
125//operator char*() {detachCopy(len()); return buf->txt;} ///< get SString's writable buffer
126void operator=(const char*t); ///< assign from const char*
127//void operator=(int x) {free(txt);nowy(x);} ///< clear string and make new empty one
128void operator=(const SString &s);
129
130void append(const char *txt,int count);
131SString operator+(const SString &s) const;
132void operator+=(int x); ///< append x spaces after current string
133void operator+=(const char*); ///< append char* contents
134void operator+=(const SString&); ///< append other SString
135
136int equals(const SString &s) const; ///< TRUE if equal
137int operator==(const SString &s) const {return equals(s);} ///< TRUE if equal
138const char* operator()(int p) const {return buf->txt+p;} ///< pointer to p'th character in SString
139char operator[](int i) const {return buf->txt[i];} ///< get char like in array
140
141/// return a substring of the current string
142SString 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.
149int getNextToken(int& pos,SString &token,char separator) const;
150
151void operator+=(char ch) {directAppend(1)[0]=ch;endAppend(1);} ///< append single character
152
153int startsWith(const char *pattern) const;
154char charAt(int pos) const {return buf->txt[pos];}
155
156static const SString& valueOf(int);
157static const SString& valueOf(double);
158
159static SString &empty();
160};
161
162#endif
Note: See TracBrowser for help on using the repository browser.