StringBasics.h

00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef __BASICSTRING_H__
00019 #define __BASICSTRING_H__
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <stdarg.h>
00024 #include <vector>
00025 #include <iostream>
00026 
00027 using std::vector;
00028 
00029 #define READBUF     128
00030 #define READBUFSTR  "128"
00031 
00032 #ifdef __PREFIX_STRING__
00033 #define String      BasicString
00034 #endif
00035 
00036 #include "InputFile.h"
00037 
00038 class String
00039 {
00040 private:
00041     void NewString(int startsize);
00042 
00043 protected:
00044     char * buffer;
00045     int  len, size;
00046 
00047 public:
00048     static int  alloc;
00049     static bool caseSensitive;
00050 
00051     explicit String(int startsize = 0)
00052     {
00053         NewString(startsize);
00054     }
00055     String(const char * s);
00056     String(const String & s);
00057     explicit String(char ch, int count = 1);
00058 
00059     ~String()
00060     {
00061         delete [] buffer;
00062     }
00063 
00064     String & Clear()
00065     {
00066         len = buffer[0] = 0;
00067         return *this;
00068     }
00069 
00070     String & Copy(const String & s);
00071     String & Copy(const String & s, int start, int count);
00072     String & Copy(const char * s);
00073 
00074     bool     IsEmpty() const
00075     {
00076         return len == 0;
00077     }
00078     String & ToUpper();
00079     String & ToLower();
00080     String   AsUpper();
00081     String   AsLower();
00082     String   Capitalize();
00083     String & Reverse();
00084 
00085     String & LeftClip(int clipAmount);
00086     String & RightClip(int clipAmount);
00087 
00088     String & operator = (char ch);
00089     String   operator + (char ch) const;
00090     String & operator += (char ch);
00091 
00092     String & operator = (const String & rhs);
00093     String   operator + (const String & rhs) const;
00094     String & operator += (const String & rhs);
00095 
00096     String & operator = (const char * rhs);
00097     String   operator + (const char * rhs) const;
00098     String & operator += (const char * rhs);
00099 
00100     String & operator = (int rhs);
00101     String   operator + (int rhs) const;
00102     String & operator += (int rhs);
00103 
00104     String & operator = (double rhs);
00105     String   operator + (double rhs) const;
00106     String & operator += (double rhs);
00107 
00108     String & operator = (unsigned int rhs);
00109     String   operator + (unsigned int rhs) const;
00110     String & operator += (unsigned int rhs);
00111     String   operator *(unsigned int rhs) const;
00112     String & operator *= (unsigned int rhs);
00113 
00114     int Compare(const String & rhs) const;
00115     int FastCompare(const String & rhs) const;
00116     int SlowCompare(const String & rhs) const;
00117 
00118     int Compare(const char * rhs) const;
00119     int FastCompare(const char * rhs) const;
00120     int SlowCompare(const char * rhs) const;
00121 
00122     int CompareToStem(const String & stem) const;
00123     int FastCompareToStem(const String & stem) const;
00124     int SlowCompareToStem(const String & stem) const;
00125 
00126     int CompareToStem(const char * stem) const;
00127     int FastCompareToStem(const char * stem) const;
00128     int SlowCompareToStem(const char * stem) const;
00129 
00130     int MatchesBeginningOf(const String & stem) const;
00131     int FastMatchesBeginningOf(const String & stem) const;
00132     int SlowMatchesBeginningOf(const String & stem) const;
00133 
00134     int MatchesBeginningOf(const char * stem) const;
00135     int FastMatchesBeginningOf(const char * stem) const;
00136     int SlowMatchesBeginningOf(const char * stem) const;
00137 
00138     int operator == (const String & rhs) const
00139     {
00140         return Compare(rhs) == 0;
00141     }
00142     int operator != (const String & rhs) const
00143     {
00144         return Compare(rhs) != 0;
00145     }
00146     int operator < (const String & rhs) const
00147     {
00148         return Compare(rhs)  < 0;
00149     }
00150     int operator > (const String & rhs) const
00151     {
00152         return Compare(rhs)  > 0;
00153     }
00154     int operator >= (const String & rhs) const
00155     {
00156         return Compare(rhs) >= 0;
00157     }
00158     int operator <= (const String & rhs) const
00159     {
00160         return Compare(rhs) <= 0;
00161     }
00162 
00163     int operator == (const char * rhs) const
00164     {
00165         return Compare(rhs) == 0;
00166     }
00167     int operator != (const char * rhs) const
00168     {
00169         return Compare(rhs) != 0;
00170     }
00171     int operator < (const char * rhs) const
00172     {
00173         return Compare(rhs)  < 0;
00174     }
00175     int operator > (const char * rhs) const
00176     {
00177         return Compare(rhs)  > 0;
00178     }
00179     int operator <= (const char * rhs) const
00180     {
00181         return Compare(rhs) <= 0;
00182     }
00183     int operator >= (const char * rhs) const
00184     {
00185         return Compare(rhs) >= 0;
00186     }
00187 
00188     operator const char *() const
00189     {
00190         return buffer;
00191     }
00192     const char *c_str() const
00193     {
00194         return (const char *) buffer;
00195     }
00196     operator char *()
00197     {
00198         return buffer;
00199     }
00200 
00201     operator int () const
00202     {
00203         return atoi(buffer);
00204     }
00205     operator double() const
00206     {
00207         return atof(buffer);
00208     }
00209     operator long double() const;
00210 
00211     char operator [](int i)  const
00212     {
00213         return buffer[i];
00214     }
00215     char & operator [](int i)
00216     {
00217         return buffer[i];
00218     }
00219 
00220     char & Last()
00221     {
00222         return buffer[len - 1];
00223     }
00224     char & First()
00225     {
00226         return buffer[0];
00227     }
00228 
00229     void Grow(int newSize);
00230     void Swap(String & s);
00231 
00232     char * LockBuffer(int size = -1);
00233     String & UnlockBuffer();
00234 
00235     String & Read();
00236     // Return the status.  A negative number indicates an error/EOF.
00237     int ReadLine();
00238     void     WriteLine();
00239     void     Write();
00240 
00241     String & Read(FILE * f);
00242     // Return the status.  A negative number indicates an error/EOF.
00243     int ReadLine(FILE * f);
00244     void     WriteLine(FILE * f);
00245     void     Write(FILE * f);
00246 
00247 #ifdef __ZLIB_AVAILABLE__
00248     String & Read(IFILE & f);
00249 
00250     // Read a line using getc
00251     // Return the status.  A negative number indicates an error/EOF.
00252     int ReadLine(IFILE & f);
00253 
00254 #endif
00255 
00256 
00257     String Left(int count) const;
00258     String Right(int count) const;
00259     String Mid(int start, int end) const;
00260     String SubStr(int start, int count) const;
00261     String SubStr(int start) const;
00262 
00263     int FindChar(char ch, int start = 0) const;
00264     int FastFindChar(char ch, int start = 0) const;
00265     int SlowFindChar(char ch, int start = 0) const;
00266 
00267     int FindLastChar(char ch) const;
00268     int FastFindLastChar(char ch) const;
00269     int SlowFindLastChar(char ch) const;
00270 
00271     int Find(const String & str, int start = 0) const;
00272     int FastFind(const String & str, int start = 0) const;
00273     int SlowFind(const String & str, int start = 0) const;
00274 
00275     String & Filter(const String & s);
00276     String & Filter(const char * s);
00277 
00278     String & ExcludeCharacters(const String & s);
00279     String & ExcludeCharacters(const char * s);
00280 
00281     int Length() const
00282     {
00283         return len;
00284     }
00285     int BufferSize() const
00286     {
00287         return size;
00288     }
00289 
00290     int SetLength(int newlen);
00291     int Dimension(int newlen)
00292     {
00293         return SetLength(newlen);
00294     }
00295 
00296     String & Add(const String & s)
00297     {
00298         return *this += s;
00299     }
00300     String & Add(char ch)
00301     {
00302         return *this += ch;
00303     }
00304 
00305     String   RightToLeft();
00306     String & Invert();
00307     String & Invert(const String & s);
00308 
00309     String & Trim();
00310     String & Trim(char character);
00311     vector<String> *Split(char splitChar);
00312 
00313     long   AsInteger() const;
00314     bool   AsInteger(long& intValue) const;
00315     bool   AsInteger(int& intValue) const;
00316     double AsDouble() const
00317     {
00318         return (double) *this;
00319     }
00320     long double AsLongDouble() const
00321     {
00322         return (long double) *this;
00323     }
00324 
00325     int    printf(const char * format, ...);
00326     int    vprintf(const char * format, va_list arglist);
00327 
00328     int    catprintf(const char * format, ...);
00329     int    vcatprintf(const char * format, va_list arglist);
00330 
00331     // Replacement vsnprintf and snprint functions for
00332     // problematic architectures...
00333 
00334     static int  my_snprintf(char * buffer, int bufsize, const char * format, ...);
00335     static int  my_vsnprintf(char * buffer, int bufsize, const char * format, va_list args);
00336     static void my_vsnprintf_close_file();
00337     static void check_vsnprintf();
00338 
00339     // Check string contents
00340     bool   IsNumber();
00341 
00342     // Explicit conversions
00343     const unsigned char * uchar() const
00344     {
00345         return (unsigned char *) buffer;
00346     }
00347     const signed char * schar() const
00348     {
00349         return (signed char *) buffer;
00350     }
00351 
00352     static FILE * my_vsnprintf_file;
00353 
00354     // Utility functions
00355     void Fill(char ch, int length = -1);
00356 
00357 private:
00358 
00359     static int vsnprintfChecked;
00360 };
00361 
00362 inline int Compare(const String & s1, const String & s2)
00363 {
00364     return s1.Compare(s2);
00365 }
00366 
00367 inline int Compare(const String & s1, const char * s2)
00368 {
00369     return s1.Compare(s2);
00370 }
00371 
00372 inline int Compare(const char * s1, const String & s2)
00373 {
00374     return -s2.Compare(s1);
00375 }
00376 
00377 inline int FastCompare(const String & s1, const String & s2)
00378 {
00379     return s1.FastCompare(s2);
00380 }
00381 
00382 inline int FastCompare(const String & s1, const char * s2)
00383 {
00384     return s1.FastCompare(s2);
00385 }
00386 
00387 inline int FastCompare(const char * s1, const String & s2)
00388 {
00389     return -s2.FastCompare(s1);
00390 }
00391 
00392 inline int SlowCompare(const String & s1, const String & s2)
00393 {
00394     return s1.SlowCompare(s2);
00395 }
00396 
00397 inline int SlowCompare(const String & s1, const char * s2)
00398 {
00399     return s1.SlowCompare(s2);
00400 }
00401 
00402 inline int SlowCompare(const char * s1, const String & s2)
00403 {
00404     return -s2.SlowCompare(s1);
00405 }
00406 
00407 String operator + (char lhs, const String & rhs);
00408 String operator + (const char * lhs, const String & rhs);
00409 String operator + (int lhs, const String & rhs);
00410 String operator + (unsigned int lhs, const String & rhs);
00411 
00412 std::ostream& operator << (std::ostream& os, const String& s);
00413 
00414 #endif
00415 
00416 
00417 
Generated on Tue Aug 23 18:19:05 2011 for libStatGen Software by  doxygen 1.6.3