nstring.h

Go to the documentation of this file.
00001 // LICENSE: (Please see the file COPYING for details)
00002 //
00003 // NUS - Nemesis Utilities System: A C++ application development framework 
00004 // Copyright (C) 2006, 2007 Otavio Rodolfo Piske
00005 //
00006 //  This file is part of NUS
00007 //
00008 //  This library is free software; you can redistribute it and/or
00009 //  modify it under the terms of the GNU Lesser General Public
00010 //  License as published by the Free Software Foundation version 2.1
00011 //  of the License.
00012 //
00013 //  This library is distributed in the hope that it will be useful,
00014 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 //  Lesser General Public License for more details.
00017 //
00018 //  You should have received a copy of the GNU Lesser General Public
00019 //  License along with this library; if not, write to the Free Software
00020 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021 //
00022 #ifndef NSTRING_H
00023 #define NSTRING_H
00024 
00030 #include "ndefs.h"
00031 #include "nlist.h"
00032 
00033 #include <cstdio>
00034 #include <cstdarg>
00035 #include <cstring>
00036 #include <cstdlib>
00037 
00038 // Ok, I don't like this voodoo. But given the scenario I don't think there
00039 // would be a better way to do this magic.
00040 #undef n_vasprintf
00041 #if defined(__WIN32) && defined (_MSC_VER)
00042 #define n_vasprintf(fmt, ap, ret) \
00043 { \
00044       int len = _vscprintf(fmt, ap); \
00045       ret = static_cast<char *>(malloc(len * sizeof(char))); \
00046       vsprintf_s(ret, len, fmt, ap); \
00047 }
00048 #elif defined(__WIN32) && defined (__GNUC__) 
00049 #define n_vasprintf(fmt, ap, ret) \
00050 { \
00051       int max_size = 2048;\
00052       ret = static_cast<char *>(malloc(max_size));\
00053       vsnprintf(ret, max_size, fmt, ap); \
00054 }
00055 #else
00056 #define n_vasprintf(fmt, ap, ret) vasprintf(&ret, fmt, ap);
00057 #endif
00058 
00063 class DllDeclSpec NString {
00064       public:
00068             NString(void);
00069       
00074             NString(const char *str);
00075             
00080             NString(const NString &str);
00081             
00085             ~NString(void);
00086             
00092             static NString number(int num);
00093             
00099             static NString number(nint64 num);
00100             
00106             NString &set(const char *str);
00107             
00113             NString &set(const NString &str);
00114             
00120             const char *toChar() const;
00121 
00122             
00127             unsigned int length() const;
00128             
00133             bool isNull() const;
00134             
00140             NString &append(const char *str, nuint32 bytes);
00141             
00147             NString &append(const char *str);
00148             
00154             NString &append(const NString &str);
00155             
00161             NString &append(char c);
00162             
00170             NString &insert(nuint32 pos, nuint32 size, const NString str);
00171             
00178             NString &insert(nuint32 pos, const NString str);
00179 
00188             bool compare(nuint32 pos, nuint32 size, const NString &str) const;
00189             
00190             
00198             bool compare(nuint32 pos, const NString &str) const;
00199             
00209             bool compare(nuint32 pos, nuint32 size, const NString &str, 
00210                         nuint32 str_pos) const;
00211             
00220             NString substr(nuint32 pos, nuint32 size) const;
00221 
00222 
00230             nint32 find(const NString &str, nuint32 pos, nuint32 ocur) const;
00231 
00238             nint32 find(const NString &str, nuint32 pos) const;
00239 
00240 
00246             nint32 find(const NString &str) const;
00247 
00248 
00256             NString &replace_first(const NString &src, const NString &dest);
00257 
00265             NString &replace(const NString &src, const NString &dest);
00266 
00275             NString &replace(const NString &src, const NString &dest, 
00276                   nuint32 ocur);
00277 
00278 
00285             NList<NString> split(const NString &substr);
00286 
00293             NString operator+(const char *str);
00299             NString operator+(const NString &str);
00300             
00306             NString operator+(char c);
00307             
00313             NString &operator+=(const char *str);
00314             
00320             NString &operator+=(const NString &str);
00321             
00327             NString &operator+=(char c);
00328             
00334             char operator[](unsigned int index) const;
00335             
00341             bool operator==(const NString &str) const;
00342             
00348             bool operator!=(const NString &str) const;
00349             
00356             bool operator>(const NString &str) const;
00357             
00364             bool operator<(const NString &str) const;
00365             
00373             bool operator>=(const NString &str) const;
00374             
00382             bool operator<=(const NString &str) const;
00383             
00384             
00390             NString &operator=(const NString &str);
00391 
00392             
00398             NString &arg(const char *fmt, ...);
00399 
00400             static const nint32 NOT_FOUND; 
00402       private:
00403             char *m_string;
00404             nuint32 m_used;
00405             nuint32 m_allocated;
00406             static const nuint32 ALLOC_SIZE;
00407             
00408             bool needsMemory(nuint32 bytes);
00409             nuint32 calculateBlocks(nuint32 bytes);
00410             void memcopy(char *dest);
00411             nuint32 freeBytes(void) const;
00412             void realloc(nuint32 bytes);
00413       
00414 };
00415 
00423 inline NString NSTR(const char *str) { 
00424       return NString(str);    
00425 }
00426 
00427 #endif // NSTRING_H
00428 
00429 

Generated on Wed Mar 5 23:10:36 2008 for NemesisUtilitiesSystem by  doxygen 1.5.4