[go: up one dir, main page]

Menu

[r35]: / trunk / lib / cstrings.h  Maximize  Restore  History

Download this file

165 lines (135 with data), 5.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************************
*
* libbasic Version 0.0.1
*
* libbasic is a simple framework of foundation classes written in C++.
*
* The full text of the GNU General Public License can be found in the doc
* directory.
*
* Copyright (C) 2002, 2003 Stefan Hoefer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* You can contact Stefan Hoefer via email at stefan@hoefer.ch
*
******************************************************************************/
#ifndef STRING_H
#define STRING_H
#include <stdlib.h>
#include <string.h>
#include "simpleobjectlist.h"
typedef const char * PCCHAR;
class CStringCore
{
friend class CStrings;
private:
unsigned int m_nRefCount;
char *m_cString;
size_t m_nLength;
size_t m_nInternalLength;
private:
inline size_t internalLength() { return m_nInternalLength; }
public:
char *string();
inline int refCount() { return m_nRefCount; }
inline void decreaseRefCount() { m_nRefCount--; }
inline void increaseRefCount() { m_nRefCount++; }
void allocMemory(size_t nLength);
inline void setLength(size_t nLength) { m_nLength = nLength; }
inline size_t length() const { return m_nLength; }
size_t utf8_length() const;
int check_utf8() const;
CStringCore();
~CStringCore();
};
class CCatString;
class CStrings
{
friend CCatString operator+(const char *cData1, const CStrings &strData2);
private:
CStringCore *m_pCore;
private:
void setLength(size_t nLength);
void fetchBuffer(size_t nLength);
void commonConstruct();
void unRef(bool bCopy = false);
void stringCopy(const char *cString, size_t nLength);
void concatenate(const char *cData1, unsigned int nLength1, const char *cData2, unsigned int nLength2);
protected:
CStrings(const char *cData1, const char *cData2);
void concatenate(const char *cData1, const char *cData2);
void concatenate(const CStrings &cData1, const char *cData2);
void concatenate(const char *cData1, const CStrings &cData2);
void concatenate(const CStrings &cData1, const CStrings &cData2);
public:
CStrings();
CStrings(const char *cString);
CStrings(const CStrings & S);
CStrings(const char *cString, size_t nLength);
virtual ~CStrings();
inline size_t length() const { return m_pCore != NULL ? m_pCore->length() : 0;}
inline size_t utf8_length() const { return m_pCore != NULL ? m_pCore->utf8_length() : 0;}
inline int check_utf8() const { return m_pCore != NULL ? m_pCore->check_utf8() : 0;}
void operator=(const char *cData);
operator PCCHAR() const;
void trimLeft(char c=' ', int nNumber = -1);
void trimRight(char c=' ', int nNumber = -1);
inline void trim(char c=' ', int nNumber = -1) { trimLeft(c, nNumber), trimRight(c, nNumber); }
void prepareSize(size_t nLength);
void operator=(const CStrings &string);
void empty();
void operator += (const char *cData);
void operator += (const CStrings &cData);
void operator += (char cData);
void operator = (char cData);
CCatString operator + (const char *cData) const;
CCatString operator + (const CStrings &strData) const;
void format(const char *cFormat, ...);
bool oneOf(const char *strString, ...) const;
bool operator==(const char *strData) const;
bool operator==(const CStrings& strData) const;
inline bool operator>(const CStrings& strData) const { return (strcmp((const char *)(*this), (const char *)strData) > 0); }
inline bool operator<(const CStrings& strData) const { return (strcmp((const char *)(*this), (const char *)strData) < 0); }
inline bool operator!=(const char *strData) const { return !operator==(strData); }
CStrings left(size_t nPos) const;
CStrings right(size_t nPos) const;
CStrings mid(size_t nPosStart, size_t nPosEnd=-1) const;
size_t find(char cData) const;
size_t findReverse(char cData) const;
char operator[](unsigned int nIndex) const;
void replace(const char *strOldString, const char *strNewString);
void set(const char *pBuffer, size_t nLength);
void chomp();
bool matchRegex(const char *strRegex, CSimpleObjectList<const CStrings> &map, bool bSubstrings=true) const;
bool matchRegex(const char *strRegex) const;
void setAt(int nIndex, char c);
CStrings substr(int nStart) const;
CStrings substr(int nStart, size_t nLength) const;
CStrings encodeBase64() const;
CStrings decodeBase64() const;
};
class CCatString : public CStrings
{
public:
CCatString();
CCatString &operator + (const char *cData);
CCatString &operator + (const CStrings &strData);
void operator=(const char *cData);
void operator += (const char *cData);
void operator += (const CStrings &cData);
};
bool operator==(const char *cData1, const CStrings &strData2);
#endif