Next Previous Contents

13. Appendix A example_mychar.cpp

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
//*****************************************************************

// And uncommnet the TEST_MYCHAR variable below -

#ifdef TEST_MYCHAR

#include <stdlib.h>  // for putenv
#include "mychar.h"

/////////////////////////////////////////////////
// A example program to demo usage of mychar 
/////////////////////////////////////////////////

int main(int argc, char **argv)
{
        char p_name[1024];
        sprintf(p_name, "PROGRAM_NAME=%s", argv[0]);
        putenv(p_name);
        print_total_memsize(); // in the beginning
        mychar aa, bb;

        //bb.str_cpy("  bbSTRing  ");
        bb = "   bbSTRing   ";

        // Testing the + operator
        // aa + " rhs "; // You will not get any output here !!!
        // You must directly use in fprintf as in below line -
        fprintf(stdout, "\n0) aa.val is :%sEOF\n", (aa + " my rhs " ).val);

        // Testing the = operator
        aa = " lhs " ;
        fprintf(stdout, "0-1) With operator= aa.val is :%sEOF\n", aa.val);

        // Testing the + operator
        // " lhs " + aa; // You will not get any output here !!!
        // You must directly use in fprintf as in below line -
        fprintf(stdout, "\n0) With lsh operator+, aa.val is :%sEOF\n", (" my lhs " + aa ).val);

        //aa.str_cpy(bb.val);
        aa = bb;
        aa.to_upper();
        fprintf(stdout, "1) aa.val is :%sEOF\n", aa.val);

        aa = bb;
        aa.to_lower();
        fprintf(stdout, "2) aa.val is :%sEOF\n", aa.val);

        aa = bb;
        aa.ltrim();
        fprintf(stdout, "3) aa.val is :%sEOF\n", aa.val);

        aa = bb;
        aa.rtrim();
        fprintf(stdout, "4) aa.val is :%sEOF\n", aa.val);

        aa = bb;
        aa.trim();
        fprintf(stdout, "5) aa.val is :%sEOF\n", aa.val);

        aa = bb;
        aa = aa + " testing newlines \n\n\n\n";
        aa.chop();
        fprintf(stdout, "5-1) aa.val is :%sEOF\n", aa.val);

        aa = bb;
        aa = aa + " rhs ";
        fprintf(stdout, "6) aa.val is :%sEOF\n", aa.val);

        aa = bb;
        aa = " lhs " + aa;
        fprintf(stdout, "7) aa.val is :%sEOF\n", aa.val);

        // Sample addition of numbers
        //aa = (mychar) 9989 + "kkk" + 33 ;
        aa = 9999;
        fprintf(stdout, "7-1) aa.val is :%sEOF\n", aa.val);

        aa = bb;
        aa = " lhs " + aa + " rhs " + " 9989 " + " 33 ";
        fprintf(stdout, "8) aa.val is :%sEOF\n", aa.val);

        aa = " AA value ";
        aa = bb + "alkja " + " 99djd " ;
        fprintf(stdout, "9) aa.val is :%sEOF\n", aa.val);

        aa = " AA value ";
        aa = (mychar) "alkja " + " 99djd " ;
        fprintf(stdout, "10) aa.val is :%sEOF\n", aa.val);

        aa = " AA value ";
        aa += (mychar) " al dev test kkk... " + " al2 slkj" + " al3333 ";
        fprintf(stdout, "11) aa.val is :%sEOF\n", aa.val);

        aa = " AA value ";
        aa = aa + " add aa " + aa + aa + aa + " 1111 " + " 2222 " + aa + aa + aa + " 3333 ";
        fprintf(stdout, "12) aa.val is :%sEOF\n", aa.val);

        aa = "12345678";
        aa.reverse();
        fprintf(stdout, "13) aa.val is :%sEOF\n", aa.val);

        aa = " AA value ";
        aa = aa + " add aa " + aa + 1111 +" "+ 2222 + " " + 3.344 + aa;
        fprintf(stdout, "14) aa.val is :%sEOF\n", aa.val);

        aa.roundd(123456.0123456789012345, 13);
        fprintf(stdout, "15) double aa.val is :%sEOF\n", aa.val);

        aa.roundf(123456.0123456789, 13);
        fprintf(stdout, "16) float aa.val is :%sEOF\n", aa.val);

        // Test equal to operators
        aa = " AA value ";
        mychar cc(" AA value ");
        if (aa == cc)
                fprintf(stdout, "\naa=%s and cc=%s are equal!!\n", aa.val, cc.val);
        else
                fprintf(stdout, "\naa=%s and cc=%s are NOT equal!!\n", aa.val, cc.val);
        cc = "CC";
        if (aa == cc)
                fprintf(stdout, "\naa=%s and cc=%s are equal!!\n", aa.val, cc.val);
        else
                fprintf(stdout, "\naa=%s and cc=%s are NOT equal!!\n", aa.val, cc.val);
        if (aa == " AA value ")
                fprintf(stdout, "\naa=%s and string are equal!!\n", aa.val);
        else
                fprintf(stdout, "\naa=%s and string are NOT equal!!\n", aa.val);
        if (aa == " AA valuexxx ")
                fprintf(stdout, "\naa=%s and string are equal!!\n", aa.val);
        else
                fprintf(stdout, "\naa=%s and string are NOT equal!!\n", aa.val);

        // You can use aa.val like a 'char *' variable in programs !!
        fprintf(stdout, "\n ");
        for (unsigned long tmpii = 0; tmpii < aa.length(); tmpii++)
        {
                fprintf(stdout, "aa.val[%ld]=%c ", tmpii, aa.val[tmpii]);
        }
        fprintf(stdout, "\n");

        // Using pointers on 'char *' val ...
        fprintf(stdout, "\n ");
        for (; *aa.val != 0; aa.val++)
        {
                fprintf(stdout, "aa.val=%c ", *aa.val);
        }
        fprintf(stdout, "\n");

        print_total_memsize(); // in the end
        exit(0);
}
#endif // TEST_MYCHAR


Next Previous Contents