• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/DcString.h

Go to the documentation of this file.
00001 /*
00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net)
00003 This file is part of Beesnest.
00004 
00005 Beesnest is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 Beesnest is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with Beesnest; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 /* DcString.h: interface for the CdcString class. */
00021 
00022 #ifndef __CDC_STRING
00023 #define __CDC_STRING
00024 
00025 #include "Interfaces/BufferHolder.h"
00026 #include <string>
00027 using namespace std;
00028 
00029 /**
00030  * This class is a wrapper of the STL string class. I wrote it because I started
00031  * the project using MFC, and then decided not to use it. This class take some
00032  * of the interface of CString and implements it with an STL string. In addition
00033  * if I will ever decide to make this CdcString working with Reference Counting
00034  * method, it will be much easier.
00035  *
00036  * <p>
00037  * @author Erez Bibi
00038  * @version 1.0
00039  */
00040 class CdcString : public string
00041 {
00042 public:
00043 
00044     /** Constructors. */
00045     CdcString ()
00046         :string () {}
00047     CdcString (char ch)
00048         :string (1, ch) {}
00049     CdcString (const char* str)
00050         :string (str) {}
00051     CdcString (char* str)
00052         :string (str) {}
00053     CdcString (const char* str, int len)
00054         :string (str, len) {}
00055     CdcString (char* str, int len)
00056         :string (str, len) {}
00057     CdcString (const string& str)
00058         :string (str) {}
00059     CdcString (const CdcString& str)
00060         :string (str.GetBuffer ()) {}
00061     CdcString (BufferHolder bh)
00062     { if (!bh.empty ()) *this = string (bh.pointer, bh.size); }
00063 
00064     /** Return the length of this string, */
00065     int GetLength () const
00066     { return length (); }
00067 
00068     /** Return True if the string is empty. */
00069     bool IsEmpty () const
00070     { return empty (); }
00071 
00072     /** Compare two strings. */
00073     int Compare (const CdcString& str) const
00074     { return compare (str); }
00075 
00076     /** Compare two strings but ignore the case. */
00077     int CompareNoCase (const CdcString& str) const;
00078 
00079     /** Compare the end of a string to 'str'. */
00080     int CompareEnd (const CdcString& str) const;
00081 
00082     /** Extract a sub string. if <b>count</b> is missing, it will extract the
00083      * sub string from <b>first</b> until the end.
00084      */
00085     CdcString Mid (int first, int count = CdcString::npos) const
00086     { return CdcString (substr (first, count)); }
00087 
00088     /** Extract the left part of a string. */
00089     CdcString Left (int count) const
00090     { return CdcString (substr (0, count)); }
00091 
00092     /** Extract the right part of a string. */
00093     CdcString Right (int count) const
00094     { return CdcString (substr (GetLength () - count)); }
00095 
00096     /** Make a string upper case. */
00097     void MakeUpper ();
00098 
00099     /** Make a string lower case. */
00100     void MakeLower ();
00101 
00102     /** Replace chars in a string. */
00103 //  int Replace (char chOld, char chNew);
00104 
00105 
00106     /** Replace sub strings in a string. */
00107     void Replace (const CdcString& strOld, const CdcString& strNew);
00108 
00109     /** Trim white spaces in the left of a string. */
00110     void TrimLeft ();
00111 
00112     /** Trim white spaces in the right of a string. */
00113     void TrimRight ();
00114 
00115     /** Trim from left and right. This is not in MFC string. */
00116     CdcString& Trim ()
00117     { TrimLeft (); TrimRight (); return *this;}
00118 
00119 
00120     /** Find sub string in a string. */
00121     int Find (const CdcString& str, int start = 0) const
00122     { return find (str, start); }
00123     int Find (const char* str, int start = 0) const
00124     { return find (str, start); }
00125     int Find (char c, int start = 0) const
00126     { return find (c, start); }
00127 
00128     int ReverseFind (const CdcString& str, int start = -1) const
00129     { start = start == -1 ? length () -1 : start;
00130       return rfind (str, start); }
00131     int ReverseFind (const char* str, int start = -1) const
00132     { start = start == -1 ? length () -1 : start;
00133       return rfind (str, start); }
00134     int ReverseFind (char c, int start = -1) const
00135     { start = start == -1 ? length () -1 : start;
00136       return rfind (c, start); }
00137 
00138     /** Find one char from a set in a string. */
00139     int FindOneOf (const CdcString& charSet, int start = 0) const
00140     { return find_first_of (charSet, start); }
00141 
00142     /** Return pointer to the inner buffer of a string. */
00143     const char* GetBuffer () const
00144     { return c_str (); }
00145 
00146 
00147     /** Format a string like printf. */
00148 //  void Format (char* strFormat, ... );
00149 
00150 
00151     /** Extract a sub string from the beginning that includes only characters
00152      * from charSet.
00153      */
00154     CdcString SpanIncluding (const CdcString& charSet) const;
00155 
00156     operator const char*() const
00157     { return this->GetBuffer (); }
00158 
00159     char operator [] (int place) const
00160     { return at (place); }
00161 
00162     char& operator [] (int place)
00163     { return at (place); }
00164 
00165     static CdcString IntToString (int num, const char* format = "%d");
00166 };
00167 
00168 #endif /* __CDC_STRING */
00169 

Generated on Mon Oct 11 2010 16:23:24 for Beesnest by  doxygen 1.7.2