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

Last change on this file since 277 was 247, checked in by Maciej Komosinski, 10 years ago

Sources support both 32-bit and 64-bit, and more compilers

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