/*******************************************************************************
*
* 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