libStatGen Software  1
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         if(buffer != NULL)
00062             delete [] buffer;
00063     }
00064 
00065     String & Clear()
00066     {
00067         len = buffer[0] = 0;
00068         return *this;
00069     }
00070 
00071     String & Copy(const String & s);
00072     String & Copy(const String & s, int start, int count);
00073     String & Copy(const char * s);
00074 
00075     bool     IsEmpty() const
00076     {
00077         return len == 0;
00078     }
00079     String & ToUpper();
00080     String & ToLower();
00081     String   AsUpper();
00082     String   AsLower();
00083     String   Capitalize();
00084     String & Reverse();
00085 
00086     String & LeftClip(int clipAmount);
00087     String & RightClip(int clipAmount);
00088 
00089     String & operator = (char ch);
00090     String   operator + (char ch) const;
00091     String & operator += (char ch);
00092 
00093     String & operator = (const String & rhs);
00094     String   operator + (const String & rhs) const;
00095     String & operator += (const String & rhs);
00096 
00097     String & operator = (const char * rhs);
00098     String   operator + (const char * rhs) const;
00099     String & operator += (const char * rhs);
00100 
00101     String & operator = (int rhs);
00102     String   operator + (int rhs) const;
00103     String & operator += (int rhs);
00104 
00105     String & operator = (double rhs);
00106     String   operator + (double rhs) const;
00107     String & operator += (double rhs);
00108 
00109     void appendFullFloat(float rhs);
00110 
00111     String & operator = (unsigned int rhs);
00112     String   operator + (unsigned int rhs) const;
00113     String & operator += (unsigned int rhs);
00114     String   operator *(unsigned int rhs) const;
00115     String & operator *= (unsigned int rhs);
00116 
00117     int Compare(const String & rhs) const;
00118     int FastCompare(const String & rhs) const;
00119     int SlowCompare(const String & rhs) const;
00120 
00121     int Compare(const char * rhs) const;
00122     int FastCompare(const char * rhs) const;
00123     int SlowCompare(const char * rhs) const;
00124 
00125     int CompareToStem(const String & stem) const;
00126     int FastCompareToStem(const String & stem) const;
00127     int SlowCompareToStem(const String & stem) const;
00128 
00129     int CompareToStem(const char * stem) const;
00130     int FastCompareToStem(const char * stem) const;
00131     int SlowCompareToStem(const char * stem) const;
00132 
00133     int MatchesBeginningOf(const String & stem) const;
00134     int FastMatchesBeginningOf(const String & stem) const;
00135     int SlowMatchesBeginningOf(const String & stem) const;
00136 
00137     int MatchesBeginningOf(const char * stem) const;
00138     int FastMatchesBeginningOf(const char * stem) const;
00139     int SlowMatchesBeginningOf(const char * stem) const;
00140 
00141     int operator == (const String & rhs) const
00142     {
00143         return Compare(rhs) == 0;
00144     }
00145     int operator != (const String & rhs) const
00146     {
00147         return Compare(rhs) != 0;
00148     }
00149     int operator < (const String & rhs) const
00150     {
00151         return Compare(rhs)  < 0;
00152     }
00153     int operator > (const String & rhs) const
00154     {
00155         return Compare(rhs)  > 0;
00156     }
00157     int operator >= (const String & rhs) const
00158     {
00159         return Compare(rhs) >= 0;
00160     }
00161     int operator <= (const String & rhs) const
00162     {
00163         return Compare(rhs) <= 0;
00164     }
00165 
00166     int operator == (const char * rhs) const
00167     {
00168         return Compare(rhs) == 0;
00169     }
00170     int operator != (const char * rhs) const
00171     {
00172         return Compare(rhs) != 0;
00173     }
00174     int operator < (const char * rhs) const
00175     {
00176         return Compare(rhs)  < 0;
00177     }
00178     int operator > (const char * rhs) const
00179     {
00180         return Compare(rhs)  > 0;
00181     }
00182     int operator <= (const char * rhs) const
00183     {
00184         return Compare(rhs) <= 0;
00185     }
00186     int operator >= (const char * rhs) const
00187     {
00188         return Compare(rhs) >= 0;
00189     }
00190 
00191     operator const char *() const
00192     {
00193         return buffer;
00194     }
00195     const char *c_str() const
00196     {
00197         return (const char *) buffer;
00198     }
00199     operator char *()
00200     {
00201         return buffer;
00202     }
00203 
00204     operator int () const
00205     {
00206         return atoi(buffer);
00207     }
00208     operator double() const
00209     {
00210         return atof(buffer);
00211     }
00212     operator long double() const;
00213 
00214     char operator [](int i)  const
00215     {
00216         return buffer[i];
00217     }
00218     char & operator [](int i)
00219     {
00220         return buffer[i];
00221     }
00222 
00223     char & Last()
00224     {
00225         return buffer[len - 1];
00226     }
00227     char & First()
00228     {
00229         return buffer[0];
00230     }
00231 
00232     void Grow(int newSize);
00233     void Swap(String & s);
00234 
00235     char * LockBuffer(int size = -1);
00236     String & UnlockBuffer();
00237 
00238     String & Read();
00239     // Return the status.  A negative number indicates an error/EOF.
00240     int ReadLine();
00241     void     WriteLine();
00242     void     Write();
00243 
00244     String & Read(FILE * f);
00245     // Return the status.  A negative number indicates an error/EOF.
00246     int ReadLine(FILE * f);
00247     void     WriteLine(FILE * f);
00248     void     Write(FILE * f);
00249 
00250     String & Read(IFILE & f);
00251 
00252     // Read a line using getc
00253     // Return the status.  A negative number indicates an error/EOF.
00254     int ReadLine(IFILE & f);
00255 
00256     String Left(int count) const;
00257     String Right(int count) const;
00258     String Mid(int start, int end) const;
00259     String SubStr(int start, int count) const;
00260     String SubStr(int start) const;
00261 
00262     int FindChar(char ch, int start = 0) const;
00263     int FastFindChar(char ch, int start = 0) const;
00264     int SlowFindChar(char ch, int start = 0) const;
00265 
00266     int FindLastChar(char ch) const;
00267     int FastFindLastChar(char ch) const;
00268     int SlowFindLastChar(char ch) const;
00269 
00270     // Since there is no longer implicit conversion
00271     // from char to String, declare this method that
00272     // takes a character rather than a String reference.
00273     int Find(char ch, int start = 0) const
00274     {
00275         return(FindChar(ch, start));
00276     }
00277     int Find(const String & str, int start = 0) const;
00278     int FastFind(const String & str, int start = 0) const;
00279     int SlowFind(const String & str, int start = 0) const;
00280 
00281     String & Filter(const String & s);
00282     String & Filter(const char * s);
00283 
00284     String & ExcludeCharacters(const String & s);
00285     String & ExcludeCharacters(const char * s);
00286 
00287     int Length() const
00288     {
00289         return len;
00290     }
00291     int BufferSize() const
00292     {
00293         return size;
00294     }
00295 
00296     int SetLength(int newlen);
00297     int Dimension(int newlen)
00298     {
00299         return SetLength(newlen);
00300     }
00301 
00302     String & Add(const String & s)
00303     {
00304         return *this += s;
00305     }
00306     String & Add(char ch)
00307     {
00308         return *this += ch;
00309     }
00310 
00311     String   RightToLeft();
00312     String & Invert();
00313     String & Invert(const String & s);
00314 
00315     String & Trim();
00316     String & Trim(char character);
00317     vector<String> *Split(char splitChar);
00318 
00319     long   AsInteger() const;
00320     bool   AsInteger(long& intValue) const;
00321     bool   AsInteger(int& intValue) const;
00322     double AsDouble() const
00323     {
00324         return (double) *this;
00325     }
00326     long double AsLongDouble() const
00327     {
00328         return (long double) *this;
00329     }
00330 
00331     int    printf(const char * format, ...);
00332     int    vprintf(const char * format, va_list arglist);
00333 
00334     int    catprintf(const char * format, ...);
00335     int    vcatprintf(const char * format, va_list arglist);
00336 
00337     // Replacement vsnprintf and snprint functions for
00338     // problematic architectures...
00339 
00340     static int  my_snprintf(char * buffer, int bufsize, const char * format, ...);
00341     static int  my_vsnprintf(char * buffer, int bufsize, const char * format, va_list args);
00342     static void my_vsnprintf_close_file();
00343     static void check_vsnprintf();
00344 
00345     // Check string contents
00346     bool   IsNumber();
00347 
00348     // Explicit conversions
00349     const unsigned char * uchar() const
00350     {
00351         return (unsigned char *) buffer;
00352     }
00353     const signed char * schar() const
00354     {
00355         return (signed char *) buffer;
00356     }
00357 
00358     static FILE * my_vsnprintf_file;
00359 
00360     // Utility functions
00361     void Fill(char ch, int length = -1);
00362 
00363 private:
00364 
00365     static int vsnprintfChecked;
00366 };
00367 
00368 inline int Compare(const String & s1, const String & s2)
00369 {
00370     return s1.Compare(s2);
00371 }
00372 
00373 inline int Compare(const String & s1, const char * s2)
00374 {
00375     return s1.Compare(s2);
00376 }
00377 
00378 inline int Compare(const char * s1, const String & s2)
00379 {
00380     return -s2.Compare(s1);
00381 }
00382 
00383 inline int FastCompare(const String & s1, const String & s2)
00384 {
00385     return s1.FastCompare(s2);
00386 }
00387 
00388 inline int FastCompare(const String & s1, const char * s2)
00389 {
00390     return s1.FastCompare(s2);
00391 }
00392 
00393 inline int FastCompare(const char * s1, const String & s2)
00394 {
00395     return -s2.FastCompare(s1);
00396 }
00397 
00398 inline int SlowCompare(const String & s1, const String & s2)
00399 {
00400     return s1.SlowCompare(s2);
00401 }
00402 
00403 inline int SlowCompare(const String & s1, const char * s2)
00404 {
00405     return s1.SlowCompare(s2);
00406 }
00407 
00408 inline int SlowCompare(const char * s1, const String & s2)
00409 {
00410     return -s2.SlowCompare(s1);
00411 }
00412 
00413 String operator + (char lhs, const String & rhs);
00414 String operator + (const char * lhs, const String & rhs);
00415 String operator + (int lhs, const String & rhs);
00416 String operator + (unsigned int lhs, const String & rhs);
00417 
00418 std::ostream& operator << (std::ostream& os, const String& s);
00419 
00420 /// Write to a file using streaming.
00421 /// \param stream file to write to - IFILE is a pointer to an InputFile object
00422 /// \param str string containing what should be written to the file.
00423 inline InputFile& operator << (InputFile& stream, const String& str)
00424 {
00425     unsigned int numExpected = str.Length();
00426     unsigned int numWritten = 
00427         stream.ifwrite(str.c_str(), numExpected);
00428     if(numExpected != numWritten)
00429     {
00430         std::cerr << "Failed to stream to IFILE, expected " 
00431                   << numExpected << " but only wrote "
00432                   << numWritten << std::endl;
00433     }
00434     return(stream);
00435 }
00436 
00437 
00438 #endif
00439 
00440 
00441 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends