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     String(int startsize = 0)
00052     {
00053         NewString(startsize);
00054     }
00055     String(const char * s);
00056     String(const String & s);
00057     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 
00197     operator int () const
00198     {
00199         return atoi(buffer);
00200     }
00201     operator double() const
00202     {
00203         return atof(buffer);
00204     }
00205     operator long double() const;
00206 
00207     char operator [](int i)  const
00208     {
00209         return buffer[i];
00210     }
00211     char & operator [](int i)
00212     {
00213         return buffer[i];
00214     }
00215 
00216     char & Last()
00217     {
00218         return buffer[len - 1];
00219     }
00220     char & First()
00221     {
00222         return buffer[0];
00223     }
00224 
00225     void Grow(int newSize);
00226     void Swap(String & s);
00227 
00228     char * LockBuffer(int size = -1);
00229     String & UnlockBuffer();
00230 
00231     String & Read();
00232     // Return the status.  A negative number indicates an error/EOF.
00233     int ReadLine();
00234     void     WriteLine();
00235     void     Write();
00236 
00237     String & Read(FILE * f);
00238     // Return the status.  A negative number indicates an error/EOF.
00239     int ReadLine(FILE * f);
00240     void     WriteLine(FILE * f);
00241     void     Write(FILE * f);
00242 
00243 #ifdef __ZLIB_AVAILABLE__
00244     String & Read(IFILE & f);
00245 
00246     // Read a line using getc
00247     // Return the status.  A negative number indicates an error/EOF.
00248     int ReadLine(IFILE & f);
00249 
00250 #endif
00251 
00252 
00253     String Left(int count) const;
00254     String Right(int count) const;
00255     String Mid(int start, int end) const;
00256     String SubStr(int start, int count) const;
00257     String SubStr(int start) const;
00258 
00259     int FindChar(char ch, int start = 0) const;
00260     int FastFindChar(char ch, int start = 0) const;
00261     int SlowFindChar(char ch, int start = 0) const;
00262 
00263     int FindLastChar(char ch) const;
00264     int FastFindLastChar(char ch) const;
00265     int SlowFindLastChar(char ch) const;
00266 
00267     int Find(const String & str, int start = 0) const;
00268     int FastFind(const String & str, int start = 0) const;
00269     int SlowFind(const String & str, int start = 0) const;
00270 
00271     String & Filter(const String & s);
00272     String & Filter(const char * s);
00273 
00274     String & ExcludeCharacters(const String & s);
00275     String & ExcludeCharacters(const char * s);
00276 
00277     int Length() const
00278     {
00279         return len;
00280     }
00281     int BufferSize() const
00282     {
00283         return size;
00284     }
00285 
00286     int SetLength(int newlen);
00287     int Dimension(int newlen)
00288     {
00289         return SetLength(newlen);
00290     }
00291 
00292     String & Add(const String & s)
00293     {
00294         return *this += s;
00295     }
00296     String & Add(char ch)
00297     {
00298         return *this += ch;
00299     }
00300 
00301     String   RightToLeft();
00302     String & Invert();
00303     String & Invert(const String & s);
00304 
00305     String & Trim();
00306     String & Trim(char character);
00307     vector<String> *Split(char splitChar);
00308 
00309     long   AsInteger() const;
00310     bool   AsInteger(long& intValue) const;
00311     double AsDouble() const
00312     {
00313         return (double) *this;
00314     }
00315     long double AsLongDouble() const
00316     {
00317         return (long double) *this;
00318     }
00319 
00320     int    printf(const char * format, ...);
00321     int    vprintf(const char * format, va_list arglist);
00322 
00323     int    catprintf(const char * format, ...);
00324     int    vcatprintf(const char * format, va_list arglist);
00325 
00326     // Replacement vsnprintf and snprint functions for
00327     // problematic architectures...
00328 
00329     static int  my_snprintf(char * buffer, int bufsize, const char * format, ...);
00330     static int  my_vsnprintf(char * buffer, int bufsize, const char * format, va_list args);
00331     static void my_vsnprintf_close_file();
00332     static void check_vsnprintf();
00333 
00334     // Check string contents
00335     bool   IsNumber();
00336 
00337     // Explicit conversions
00338     const unsigned char * uchar() const
00339     {
00340         return (unsigned char *) buffer;
00341     }
00342     const signed char * schar() const
00343     {
00344         return (signed char *) buffer;
00345     }
00346 
00347     static FILE * my_vsnprintf_file;
00348 
00349     // Utility functions
00350     void Fill(char ch, int length = -1);
00351 
00352 private:
00353 
00354     static int vsnprintfChecked;
00355 };
00356 
00357 inline int Compare(const String & s1, const String & s2)
00358 {
00359     return s1.Compare(s2);
00360 }
00361 
00362 inline int Compare(const String & s1, const char * s2)
00363 {
00364     return s1.Compare(s2);
00365 }
00366 
00367 inline int Compare(const char * s1, const String & s2)
00368 {
00369     return -s2.Compare(s1);
00370 }
00371 
00372 inline int FastCompare(const String & s1, const String & s2)
00373 {
00374     return s1.FastCompare(s2);
00375 }
00376 
00377 inline int FastCompare(const String & s1, const char * s2)
00378 {
00379     return s1.FastCompare(s2);
00380 }
00381 
00382 inline int FastCompare(const char * s1, const String & s2)
00383 {
00384     return -s2.FastCompare(s1);
00385 }
00386 
00387 inline int SlowCompare(const String & s1, const String & s2)
00388 {
00389     return s1.SlowCompare(s2);
00390 }
00391 
00392 inline int SlowCompare(const String & s1, const char * s2)
00393 {
00394     return s1.SlowCompare(s2);
00395 }
00396 
00397 inline int SlowCompare(const char * s1, const String & s2)
00398 {
00399     return -s2.SlowCompare(s1);
00400 }
00401 
00402 String operator + (char lhs, const String & rhs);
00403 String operator + (const char * lhs, const String & rhs);
00404 String operator + (int lhs, const String & rhs);
00405 String operator + (unsigned int lhs, const String & rhs);
00406 
00407 std::ostream& operator << (std::ostream& os, const String& s);
00408 
00409 #endif
00410 
00411 
00412 
Generated on Wed Nov 17 15:38:29 2010 for StatGen Software by  doxygen 1.6.3