Next Previous Contents

5. String.h file

In C++ (or any object oriented language), you just read the "class data-structure" (i.e. interface) to begin using that object. You just need to understand the interface and not the implementation of the interface. In case of String class, you just need to read and understand the String class in String.h file. You do not need to read the entire implementation (String.cpp) in order to use String class. The object oriented classes are real time saver and they very neatly hide the implementation.

(In object oriented Java language there is the equivalent called 'Intefaces' , which hides the implementation details.)

Given below is String.h file and see also Appendix A String.h


//
// Author : Al Dev  Email: alavoor@yahoo.com
// Use string class or String class
//
// To prevent memory leaks - a char class to manage character variables
// Always prefer to use String or string class 
// instead of char[] or char *
//

#ifndef __STRING_H_
#define __STRING_H_

#include <iostream> // do not use iostream as program becomes bulky..
#include <stdio.h> // for FILE and sprintf()
#include <list.h> // for list

const short INITIAL_SIZE =      50;
const short NUMBER_LENGTH = 300;
const int MAX_ISTREAM_SIZE = 2048;

class StringBuffer;

// a small class with a VERY MINIMUM of functions and variables...
// This class to be kept small... 
class String
{
        public:
                String();
                String(char bb[]);  // needed by operator+
                String(char bb[], int start, int slength); // subset of chars
                String(int bb);  // needed by operator+
                String(unsigned long bb);  // needed by operator+
                String(long bb);  // needed by operator+
                String(float bb);  // needed by operator+
                String(double bb);  // needed by operator+
                String(const String & rhs);  // Copy Constructor needed by operator+
                String(StringBuffer sb);  // Java compatibility
                String(int bb, bool dummy);  // for StringBuffer class
                virtual ~String();  // Made virtual so that when base class is deleted
                                                        // then the derived class destructor is called.

                char *val() {return sval;} // Not safe to make sval public

                // Functions below imitate Java language's String object 
                unsigned long length() { return strlen(sval); }
                char charAt(int where);
                void getChars(int sourceStart, int sourceEnd, 
                                char target[], int targetStart);
                char* toCharArray();
                char* getBytes();

                bool equals(String str2); // See also == operator
                bool equals(char *str2); // See also == operator
                bool equalsIgnoreCase(String str2);

                bool regionMatches(int startIndex, String str2, 
                                int str2StartIndex, int numChars);
                bool regionMatches(bool ignoreCase, int startIndex, 
                                String str2, int str2StartIndex, int numChars);

                String toUpperCase();
                String toLowerCase();

                bool startsWith(String str2);
                bool startsWith(char *str2);

                bool endsWith(String str2);
                bool endsWith(char *str2);

                int compareTo(String str2);
                int compareTo(char *str2);
                int compareToIgnoreCase(String str2);
                int compareToIgnoreCase(char *str2);

                int indexOf(char ch, int startIndex = 0);
                int indexOf(char *str2, int startIndex = 0);
                int indexOf(String str2, int startIndex = 0);

                int lastIndexOf(char ch, int startIndex = 0);
                int lastIndexOf(char *str2, int startIndex = 0);
                int lastIndexOf(String str2, int startIndex = 0);

                String substring(int startIndex, int endIndex = 0);
                String replace(char original, char replacement);
                String replace(char *original, char *replacement);

                String trim(); // See also overloaded trim()

                String concat(String str2);  // See also operator +
                String concat(char *str2); // See also operator +

                String reverse(); // See also overloaded reverse()
                String deleteCharAt(int loc);
                String deleteStr(int startIndex, int endIndex); // Java's "delete()"

                String valueOf(char ch)
                        {char aa[2]; aa[0]=ch; aa[1]=0; return String(aa);}
                String valueOf(char chars[]){ return String(chars);}
                String valueOf(char chars[], int startIndex, int numChars);
                String valueOf(bool tf)
                        {if (tf) return String("true"); else return String("false");}
                String valueOf(int num){ return String(num);}
                String valueOf(long num){ return String(num);}
                String valueOf(float num) {return String(num);}
                String valueOf(double num) {return String(num);}

                // See also StringBuffer class in this file given below

                // ---- End of Java like String object functions -----

                //////////////////////////////////////////////////////
                //              List of additonal functions not in java
                //////////////////////////////////////////////////////
                String ltrim();
                void ltrim(bool dummy); // dummy to get different signature
                String rtrim();
                void rtrim(bool dummy); // dummy to get different signature

                void chopall(char ch='\n'); // removes trailing character 'ch'
                void chop(); // removes one trailing character

                void roundf(float input_val, short precision);
                void decompose_float(long *integral, long *fraction); 

                void roundd(double input_val, short precision);
                void decompose_double(long *integral, long *fraction); 

                void explode(char *seperator); // see also token() and overloaded explode()
                String *explode(int & strcount, char seperator = ' '); // see also token()
                void implode(char *glue);
                void join(char *glue);
                String repeat(char *input, unsigned int multiplier);
                String tr(char *from, char *to); // translate characters
                String center(int padlength, char padchar = ' ');
                String space(int number = 0, char padchar = ' ');
                String xrange(char start, char end);
                String compress(char *list = " ");
                String left(int slength = 0, char padchar = ' ');
                String right(int slength = 0, char padchar = ' ');
                String overlay(char *newstr, int start = 0, int slength = 0, char padchar = ' ');

                String at(char *regx); // matches first match of regx
                String before(char *regx); // returns string before regx
                String after(char *regx); // returns string after regx
                String mid(int startIndex = 0, int length = 0);

                bool isNull();  
                bool isInteger();
                bool isInteger(int pos);
                bool isNumeric();
                bool isNumeric(int pos);
                bool isEmpty();  // same as length() == 0
                bool isUpperCase();
                bool isUpperCase(int pos);
                bool isLowerCase();
                bool isLowerCase(int pos);
                bool isWhiteSpace();
                bool isWhiteSpace(int pos);
                bool isBlackSpace();
                bool isBlackSpace(int pos);
                bool isAlpha();
                bool isAlpha(int pos);
                bool isAlphaNumeric();
                bool isAlphaNumeric(int pos);
                bool isPunct();
                bool isPunct(int pos);
                bool isPrintable();
                bool isPrintable(int pos);
                bool isHexDigit();
                bool isHexDigit(int pos);
                bool isCntrl();
                bool isCntrl(int pos);
                bool isGraph();
                bool isGraph(int pos);

                void clear();
                int toInteger();
                long parseLong();

                double toDouble();
                String token(char seperator = ' '); // ref StringTokenizer, explode()
                String crypt(char *original, char *salt);
                String getline(FILE *infp = stdin); // see also putline()
                //String getline(fstream *infp = stdin); // see also putline()

                void putline(FILE *outfp = stdout); // see also getline()
                //void putline(fstream *outfp = stdout); // see also getline()

                void swap(String aa, String bb); // swap aa to bb
                String *sort(String aa[]);  // sorts array of strings
                String sort(int startIndex = 0, int length = 0);  // sorts characters inside a string
                int freq(char ch); // returns the number of distinct, nonoverlapping matches
                void Format(const char *fmt, ...);
                String replace (int startIndex, int endIndex, String str);

                void substring(int startIndex, int endIndex, bool dummy);
                void reverse(bool dummy); // dummy to get different signature
                String deleteCharAt(int loc, bool dummy);
                String deleteStr(int startIndex, int endIndex, bool dummy);
                void trim(bool dummy); // dummy to get different signature
                String insert(int index, String str2);
                String insert(int index, String str2, bool dummy);
                String insert(int index, char ch);
                String insert(int index, char ch, bool dummy);
                String insert(char *newstr, int start = 0, int length = 0, char padchar = ' ');

                String dump(); // Dump the string like 'od -c' (octal dump) does

                // required by java's StringBuffer
                void ensureCapacity(int capacity);
                void setLength(int len);
                void setCharAt(int where, char ch);

                // required by java's Integer class, Long, Double classes
                int parseInt(String ss) {return ss.toInteger();}
                int parseInt(char *ss)
                        {String tmpstr(ss); return tmpstr.toInteger();}
                long parseLong(String ss) {return ss.parseLong();}
                long parseLong(char *ss)
                        {String tmpstr(ss); return tmpstr.parseLong();}
                float floatValue() {return (float) toDouble(); }
                double doubleValue() {return toDouble(); }

                ///////////////////////////////////////////////
                //              List of duplicate function names
                ///////////////////////////////////////////////
                // char * c_str() // use val()
                // bool find();  // Use regionMatches()
                // bool search();  // Use regionMatches()
                // bool matches(); // Use regionMatches()
                // int rindex(String str2, int startIndex = 0); Use lastIndexOf()
                // String blanks(int slength);  // Use repeat()
                // String append(String str2); // Use concat() or + operator
                // String prepend(String str2);  // Use + operator. See also append()
                // String split(char seperator = ' ');  // Use token()
                bool contains(char *str2, int startIndex = 0); // use indexOf()
                // void empty(); Use is_empty()
                // void vacuum(); Use clear()
                // void erase(); Use clear()
                // void zero(); Use clear()
                // bool is_float(); Use is_numeric();
                // bool is_decimal(); Use is_numeric();
                // bool is_Digit(); Use is_numeric();
                // float float_value(); Use toDouble();
                // float tofloat(); Use toDouble();
                // double double_value(); Use toDouble();
                // double numeric_value(); Use toDouble();
                // int int_value(); Use toInteger()
                // int tonumber(); Use toInteger()
                // String get(); Use substring() or val() but prefer java's substring
                // String getFrom(); Use substring() or val() but prefer java's substring
                // String head(int len); Use substring(0, len)
                // String tail(int len); Use substring(length()-len, length())
                // String cut(); Use deleteCharAt() or deleteStr()
                // String cutFrom(); Use deleteCharAt() or deleteStr()
                // String paste(); Use insert()
                // String fill(); Use replace()
                // char firstChar(); // Use substring(0, 1);
                // char lastChar(); // Use substring(length()-1, length());
                // String findNext(); Use token()

                // begin();  iterator. Use operator [ii]
                // end();  iterator. Use operator [ii]
                // copy();  Use assignment =  operator, String aa = bb;
                // clone();  Use assignment =  operator, String aa = bb;

                // All Operators ...
                String operator+ (const String & rhs);
                friend String operator+ (const String & lhs, const String & rhs);

                String& operator+= (const String & rhs); // using reference will be faster
                String& operator= (const String & rhs); // using reference will be faster
                bool operator== (const String & rhs); // using reference will be faster
                bool operator== (const char *rhs);
                bool operator!= (const String & rhs);
                bool operator!= (const char *rhs); 
                char operator [] (unsigned long Index) const;
                char& operator [] (unsigned long Index);
                friend ostream &  operator<< (ostream & Out, const String & str2);
                friend istream &  operator>> (istream & In, String & str2);

                static  list<String>                 explodeH;  // list head

        protected:
                char *sval; // Not safe to make sval public
                inline void verifyIndex(unsigned long index) const;
                inline void verifyIndex(unsigned long index, char *aa) const;

                void _str_cat(char bb[]);
                void _str_cat(int bb);
                void _str_cat(unsigned long bb);
                void _str_cat(float bb);

        private:
                // Note: All the private variables and functions begin
                // with _ (underscore)
                
                //static String *_global_String; // for use in add operator
                //inline void _free_glob(String **aa);
                void _str_cpy(char bb[]);
                void _str_cpy(int bb); // itoa
                void _str_cpy(unsigned long bb);
                void _str_cpy(float bb); // itof

                bool _equalto(const String & rhs, bool type = false);
                bool _equalto(const char *rhs, bool type = false);
                String *_pString;  // temporary pointer for internal use..
                inline void _allocpString();
                inline void _reverse();
                inline void _deleteCharAt(int loc);
                inline void _deleteStr(int startIndex, int endIndex);
                inline void _trim();
                inline void _ltrim();
                inline void _rtrim();
                inline void _substring(int startIndex, int endIndex);
};

// Imitate Java's StringBuffer object
// This class is provided so that the Java code is
// portable to C++, requiring minimum code changes
// Note: While coding in C++ DO NOT use this class StringBuffer,
// this is provided only for compiling code written in Java
// which is cut/pasted inside C++ code.
class StringBuffer: public String
{
        public:
                StringBuffer();
                StringBuffer(int size);
                StringBuffer(String str);
                ~StringBuffer();

                int capacity() {return strlen(sval);}
                StringBuffer append(String str2)
                        { *this += str2; return *this;} // See also operator +
                StringBuffer append(char *str2)
                        { *this += str2; return *this;} // See also operator +
                StringBuffer append(int bb)
                        { *this += bb; return *this;} // See also operator +
                StringBuffer append(unsigned long bb) 
                        { *this += bb; return *this;} // See also operator +
                StringBuffer append(float bb) 
                        { *this += bb; return *this;} // See also operator +
                StringBuffer append(double bb) 
                        { *this += bb; return *this;} // See also operator +

                StringBuffer insert(int index, String str2)
                        { return String::insert(index, str2, true);}

                StringBuffer insert(int index, char ch)
                        { return String::insert(index, ch, true);}

                StringBuffer reverse()
                        { String::reverse(true); return *this;}

                // Java's "delete()". Cannot use name delete in C++
                StringBuffer deleteStr(int startIndex, int endIndex)
                        { String::deleteStr(startIndex, endIndex, true); return *this;}
                StringBuffer deleteCharAt(int loc)
                        { String::deleteCharAt(loc, true); return *this;}

                StringBuffer substring(int startIndex, int endIndex = 0)
                        { String::substring(startIndex, endIndex, true); return *this;}
};

static String Integer("0"); // java's Integer.parseInt(String);
static String Long("0"); // java's Long.parseLong(String);

// Imitate java's Float class and Float.floatValue()
// provided to compile java code in C++
class Float: public String
{
        public:
                Float(String str);
                Float valueOf(String str2) {return Float(str2);}
                float floatValue() {return (float) toDouble(); }
};

// Imitate java's Double class and Double.doubleValue()
// provided to compile java code in C++
class Double: public String
{
        public:
                Double(String str);
                Double valueOf(String str2) {return Double(str2);}
                double doubleValue() {return toDouble(); }
};

// Imitate java's StringTokenizer class
// provided to compile java code in C++ and vice-versa
class StringTokenizer: public String
{
        public:
                StringTokenizer(String str);
                StringTokenizer(String str, String delimiters);
                StringTokenizer(String str, String delimiters, bool dflag);
                ~StringTokenizer();

                int     countTokens();
                bool    hasMoreElements();
                bool    hasMoreTokens();
                String  nextElement(); // in java returns type 'Object'
                String  nextToken();
                String  nextToken(String delimiters);
        private:
                int             mvCurrentPosition; // current index on string
                int             mvTotalTokens;
                int             mvRemainingTokens;
                char *  mvpListOfDl; // list of delimiters
                char *  mvpWorkStr; // temp work string
                char *  mvpOrigStr; // original string passed
                bool    mvDlFlag;  // delimiter flag
                inline  void vPrepWorkStr(char *delimiters = NULL);
};

// Imitate java's StringReader class
// provided to compile java code in C++
class StringReader: public String
{
        public:
                StringReader(String str);
                void close() {}  // close the stream
                void mark(int readAheadLimit);
                bool markSupported() {return true;} // tell whether this stream supports the mark() operation, which it does
                int read();
                int read(char cbuf[], int offset, int length);
                bool ready() {return true;} // tell whether this stream is ready to read
                void reset();
                long skip(long ii);
        private:
                unsigned long   _curpos;
                unsigned long   _mark_pos;
};

// Imitate java's StringWriter class 
// provided to compile java code in C++
class StringWriter: public String
{
        public:
                StringWriter();
                StringWriter(int bufferSize);
                void close() {clear();}
                void flush() {clear();}
                StringBuffer getBuffer() {return (StringBuffer) *this;}
                String  toString() {return (String) *this;}
                void write(int);
                void write(String);
                void write(char *str1);
                void write(char str1[], int startIndex, int endIndex);
                void write(String str1, int startIndex, int endIndex);
};

// Global variables are defined in String.cpp

#endif // __STRING_H_


Next Previous Contents