Next Previous Contents

14. Appendix B mychar.h

You can download all programs as a single tar.gz file from Download mychar . To get this file, in the web-browser, save this file as 'Text' type.


//*****************************************************************
// Copyright policy is GNU/GPL but additional restriction is 
// that you include author's name and email on all copies
// Author : Al Dev Email: alavoor@yahoo.com
//*****************************************************************

// To prevent memory leaks - a char class to manage character variables
// Always prefer to use mychar or string class 
// instead of char[] or char *
//

#ifndef __MYCHAR_H_
#define __MYCHAR_H_

//#include <iostream> // do not use iostream as program becomes bulky..
//#include <stdlib.h> // for free() amd malloc()
#include <string.h> // for strcpy()
#include <ctype.h> // for isspace()
#include <stdio.h> // for sprintf()
#include <list.h> // for sprintf()
#include <math.h> // for modf(), rint()

#include "my_malloc.h"
#include "debug.h" // debug_(name, value)  debug2_(name, value, LOG_YES)

const short INITIAL_SIZE =      50;
const short NUMBER_LENGTH = 70;

// a small class with a VERY MINIMUM of functions and variables...
// This class to be kept small... 
class mychar
{
        public:
                mychar();
                mychar(char bb[]);  // needed by operator+
                mychar(int bb);  // needed by operator+
                mychar(unsigned long bb);  // needed by operator+
                mychar(float bb);  // needed by operator+
                mychar(double bb);  // needed by operator+
                mychar(const mychar & rhs);  // Copy Constructor needed by operator+
                ~mychar();

                char *val;

                unsigned long length() { return strlen(val); }

                void ltrim();
                void rtrim();
                void trim();
                void chop();

                void to_upper();
                void to_lower();

                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); 

                long pos(char substr[], unsigned long start);

                void explode(char *seperator);
                void implode(char *glue);
                void join(char *glue);
                void repeat(char *input, unsigned int multiplier);
                void reverse();
                void replace(char *needle, char *str);
                void str_tr(char *from, char *to);
                void center(int length, char padchar = ' ');
                void space(int number = 0, char padchar = ' ');
                void xrange(char start, char end);
                void compress(char *list);
                void delstr(int start, int length);
                void insert(char *newstr, int start = 0, int length = 0, char padchar = ' ');
                void left(int length = 0, char padchar = ' ');
                void right(int length = 0, char padchar = ' ');
                void overlay(char *newstr, int start = 0, int length = 0, char padchar = ' ');
                mychar substr(int start, int length = 0);

                mychar at(char *regx); // matches first match of regx
                mychar before(char *regx); // returns string before regx
                mychar after(char *regx); // returns string after regx

                bool isnull();
                void clear();

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

                mychar& operator+= (const mychar & rhs); // using reference will be faster
                mychar& operator= (const mychar & rhs); // using reference will be faster
                bool operator== (const mychar & rhs); // using reference will be faster
                bool operator== (const char *rhs);
                bool operator!= (const mychar & rhs);
                bool operator!= (const char *rhs); 

                static  list<mychar>                 explodeH;  // list head

        private:
                //static mychar *global_mychar; // for use in add operator
                //inline void free_glob(mychar **aa);
                void str_cpy(char bb[]);
                void str_cpy(int bb); // itoa
                void str_cpy(unsigned long bb);
                void str_cpy(float bb); // itof

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

                bool equalto(const mychar & rhs, bool type = false);
                bool equalto(const char *rhs, bool type = false);
};
// Global variables are defined in mychar.cpp

#endif // __MYCHAR_H_


Next Previous Contents