source: cpp/frams/util/sstring.h @ 132

Last change on this file since 132 was 121, checked in by sz, 11 years ago

updated file headers and makefiles

  • Property svn:eol-style set to native
File size: 7.1 KB
RevLine 
[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
28class ExtValue;  //this include would result in recurrent inclusion: #include "extvalue.h"
29class ExtObject;
30
31class SBuf
32{
33char *txt;
34int used;       ///< data size
35int size;       ///< buffer size, not including \0, special case: 0==buffer not allocated
36int refcount;   ///< buffer is used by 'refcount' objects.
37void initEmpty();
38void ensureSize(int wantsize);
39void copyFrom(const char* ch, int chlen=-1);
40void freeBuf();
41void append(const char* ch, int chlen=-1);
42static SBuf &empty();
43SBuf(int initsize);
44friend class SString;
45SBuf(const SBuf& b) {}
46public:
47SBuf();
48~SBuf();
49};
50
51/// (not so) simple text string class
52
53class SString
54{
55private:
56SBuf *buf;      ///< buffer
57int appending;  ///< append mode, changes can occur after character # 'appending'
58//int memhint;
59
60void initEmpty();
61int guessMemSize(int request);
62void copyFrom(SString &from); ///< copy from SString, reference if possible
63void detach(); ///< detach from shared buffer, if any
64void detachEmpty(int ensuresize=0); ///< detach and make empty
65void detachCopy(int ensuresize=0); ///< detach and make private copy
66
67public:
68SString(); ///< make an empty string
69SString(const char*t,int t_len=-1); ///< make a string from char*
70SString(int x); ///< string with initial buffer size
71SString(const SString& from); ///< duplicate string
72~SString();
73
74void copyFrom(const char* ch, int chlen=-1); ///< copy string, length of -1 == unknown
75
76void* operator new(size_t s, void* mem) {return mem;}
77#ifdef _MSC_VER
78void operator delete(void* mem, void* t) {}
79#endif
80void* operator new(size_t s) {return malloc(sizeof(SString));}
81void operator delete(void* mem) {free(mem);}
82
83int len() const {return buf->used;} ///< get string length
84void 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>
97char *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>
106char *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>
115void 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>
125void 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).
129void memoryHint(int howbig);
130int 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.
134int indexOf(int character,int start=0) const;
135
136/// find a substring.
137/// return index if the substring was found or -1 otherwise.
138int indexOf(const char *substring,int start=0) const;
139
140/// find a substring.
141/// return index if the substring was found or -1 otherwise.
142int indexOf(const SString & substring,int start=0) const;
143
144operator const char*() const {return buf->txt;} ///< get SString's readonly buffer
145//operator char*() {detachCopy(len()); return buf->txt;} ///< get SString's writable buffer
146void operator=(const char*t); ///< assign from const char*
147//void operator=(int x) {free(txt);nowy(x);} ///< clear string and make new empty one
148void operator=(const SString &s);
149
150void append(const char *txt,int count);
151SString operator+(const SString &s) const;
152void operator+=(int x); ///< append x spaces after current string
153void operator+=(const char*); ///< append char* contents
154void operator+=(const SString&); ///< append other SString
155
156int equals(const SString &s) const; ///< TRUE if equal
157int operator==(const SString &s) const {return equals(s);} ///< TRUE if equal
158int operator!=(const SString &s) const {return !equals(s);}
159const char* operator()(int p) const {return buf->txt+p;} ///< pointer to p'th character in SString
160char operator[](int i) const {return buf->txt[i];} ///< get char like in array
161
162/// return a substring of the current string
163SString 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.
170int getNextToken(int& pos,SString &token,char separator) const;
171
172void operator+=(char ch) {directAppend(1)[0]=ch;endAppend(1);} ///< append single character
173
174int startsWith(const char *pattern) const;
175char charAt(int pos) const {return buf->txt[pos];}
176
177static SString valueOf(int);
178static SString valueOf(long);
179static SString valueOf(double);
180static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu
181static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu
182static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu
183static SString sprintf(const char* format, ...);
184
185static SString &empty();
186};
187
188#endif //#ifdef SSTRING_SIMPLE
189
190#endif
Note: See TracBrowser for help on using the repository browser.