/*************************************************************************** * Copyright (C) 2008 by Peter Eschright * * petereschright@gmail.com * * * * This source code is presented for informational purposes only. * * Please contact the author for permision for use. * **************************************************************************/ // ///////////////////////////////////////////////////////////// // // File name: my_string.h // File description: Header file for simple string class // // ///////////////////////////////////////////////////////////// #ifndef MY_STRING_H #define MY_STRING_H #include <iostream> #include <cstdlib> #include <ctype.h> #include <cstring> using namespace std; // ///////////////////////////////////////////////////////////// // // my_string class // // A simple to use string class with usefull features // // ///////////////////////////////////////////////////////////// class my_string{ public: // /////////////////////// // constructors // /////////////////////// my_string(); my_string(const char *text_str); my_string(const my_string &str); // destructor ~my_string(); // clear string and release memory void clear(); // ////////////////////////////////////////////// // // Operators // // ////////////////////////////////////////////// // /////////////////////////////// // assignment // /////////////////////////////// my_string & operator=(const my_string &cpystring); my_string & operator=(const char *ascii_text); // //////////////////////////////// // Input and Output // //////////////////////////////// // output string to stream friend ostream &operator<<(ostream &, const my_string &); // read line of input like below friend istream &operator>>(istream &, my_string &); // read up to 255 char in a line // return length of input int getline(istream &, char delim = '\n'); // //////////////////////////////// // character access // //////////////////////////////// char &operator[](int i); // //////////////////////////////////////// // logical and comparison operators // //////////////////////////////////////// bool operator==(const my_string &str) const; bool operator<(const my_string &str) const; bool operator>(const my_string &str) const; bool operator!=(const my_string &str) const; bool operator<=(const my_string &str) const; bool operator>=(const my_string &str) const; bool operator==(const char *str) const; bool operator!=(const char *str) const; bool operator<(const char *str) const; bool operator<=(const char *str) const; bool operator>(const char *str) const; bool operator>=(const char *str) const; friend bool operator==(const char *str1, const my_string &str2); friend bool operator!=(const char *str1, const my_string &str2); friend bool operator<(const char *str1, const my_string &str2); friend bool operator<=(const char *str1, const my_string &str2); friend bool operator>(const char *str1, const my_string &str2); friend bool operator>=(const char *str1, const my_string &str2); // /////////////////////////////// // concatenation // /////////////////////////////// // in place my_string &operator+=(const my_string &cpystring); my_string &operator+=(const char *str); // general my_string operator+(const my_string &cpystring); my_string operator+(const char *str); // /////////////////////////////////////////// // reformating and truncation // /////////////////////////////////////////// // remove preceding and trailing whitespace bool trim(); // remove preceding whitespace bool ltrim(); // remove trailing whitespace bool rtrim(); // remove trailing newline if found bool chomp(); // truncate to specified position (pos) // if position is after end of string then // string is unchanged // return length after truncation int truncate(int pos); private: char *strdata; int mem_size; int length; // returned if [] index out of range char bad_char; }; // //////////////////////////////// // Input and Output // //////////////////////////////// ostream &operator<<(ostream &, const my_string &); istream &operator>>(istream &, my_string &); // //////////////////////////////////////// // logical and comparison operators // //////////////////////////////////////// bool operator==(const char *str1, const my_string &str2); bool operator!=(const char *str1, const my_string &str2); bool operator<(const char *str1, const my_string &str2); bool operator<=(const char *str1, const my_string &str2); bool operator>(const char *str1, const my_string &str2); bool operator>=(const char *str1, const my_string &str2); #endif