source: cpp/gdk/sstring.h @ 95

Last change on this file since 95 was 92, checked in by Maciej Komosinski, 11 years ago

SString::valueOf(value) can handle more argument types

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