home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / CCHAR.HPP < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  165 lines

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. //
  4. // Classed char
  5. //
  6. // puts array of characters on the heap using new without having
  7. // to delete.
  8.  
  9. /*
  10.  * I hereby give this source code to the public domain.
  11.  * You may use this file for any purpose whatsoever without royalty.
  12.  * However, you also accept that I bear no responsibility for the
  13.  * fitness for any particular use, expressed or implied, or that this
  14.  * will even work.
  15.  *
  16.  * If you make any changes to this file, please let me know so that
  17.  * I may incorporate those changes into the originals.
  18.  *
  19.  * Reachable at:
  20.  *   mcbride@ee.ualberta.ca
  21.  *   mcbride@tower.bohica.net
  22.  *   darin.mcbride@tower.alive.ampr.ab.ca
  23.  *   Sysop of 1:342/708 (fido)
  24.  *           40:6494/2004 (ibm)
  25.  *           77:2/0 (fire)
  26.  *          100:1403/0 (rpg)
  27.  */
  28.  
  29. #ifndef __CHAR_ON_HEAP_HPP
  30. #define __CHAR_ON_HEAP_HPP
  31.  
  32. #include <string.h>
  33.  
  34. #define ASSERT(x) Assert(x) // use to point to your assert function type
  35. // use '#define ASSERT(x)' to remove assertions (DANGEROUS!)
  36.  
  37. // if your compiler requires a specific ostream& operator<< for this
  38. // class, uncomment the following line, or define it in the makefile/project
  39. // #define REQUIRES_OSTREAM_INSERTER_OPERATOR
  40.  
  41. #ifdef REQUIRES_OSTREAM_INSERTER_OPERATOR
  42. #include <iostream.h>
  43. #endif
  44.  
  45. class cchar
  46. {
  47. #ifdef REQUIRES_OSTREAM_INSERTER_OPERATOR
  48.     friend ostream& operator << (ostream& os, cchar& cc);
  49. #endif
  50.    private:
  51.        char* pchar; // the pointer to the character(s)
  52.  
  53.        // following routines are provided to easily change the
  54.        // method for allocation.  For example, to change from
  55.        // C++'s new/delete to farmalloc/farfree, just change these
  56.        // functions
  57.        void Allocate(size_t n)
  58.        {
  59.            ASSERT(pchar == NULL);
  60.            pchar = new char[n];
  61.        }
  62.        void DeAllocate()
  63.        {
  64.            ASSERT(pchar != NULL);
  65.            delete[] pchar;
  66.            pchar = NULL;
  67.        }
  68.  
  69.    public:
  70.        cchar(int n = 0) : pchar(NULL)
  71.        {
  72.            if (n > 0)
  73.                Allocate(n); // grab enough memory
  74.        }
  75.        cchar(const char* psz) : pchar(NULL)
  76.        {
  77.            if (psz != NULL) {
  78.                Allocate(strlen(psz) + 1);
  79.                strcpy(pchar, psz);
  80.            }
  81.        }
  82.        cchar(const cchar& cch) : pchar(NULL)
  83.        {
  84.            if (cch.pchar != NULL) {
  85.                Allocate(strlen(cch.pchar) + 1);
  86.                strcpy(pchar, cch.pchar);
  87.            }
  88.        }
  89.        cchar& operator=(const char* psz)
  90.        {
  91.            if (psz != pchar) { // ensure we aren't copying over ourselves
  92.                if (pchar != NULL)
  93.                    DeAllocate();
  94.                if (psz != NULL) {
  95.                    Allocate(strlen(psz) + 1);
  96.                    strcpy (pchar, psz);
  97.                } else {
  98.                    pchar = NULL;
  99.                }
  100.            }
  101.            return *this;
  102.        }
  103.        cchar& operator=(const cchar& cch)
  104.        {
  105.            // use the above operator= to do the rest - code reuse?
  106.            return *this = cch.pchar;
  107.        }
  108.        ~cchar()
  109.        {
  110.            if (pchar != NULL)
  111.                DeAllocate();
  112.        }
  113.        // for those cases that you really need to test validity...
  114.        // and the standard 'char_ptr == NULL' isn't working.  <sigh>
  115.        const int IsValid() const
  116.        {
  117.            return pchar != NULL;
  118.        }
  119.        operator char*() const
  120.        {
  121.            return pchar;
  122.        }
  123.        operator const char*() const
  124.        {
  125.            return pchar;
  126.        }
  127.        const char* operator()() const
  128.        {
  129.            return pchar;
  130.        }
  131.        char* operator()()
  132.        {
  133.            return pchar;
  134.        }
  135.        char operator()(int n) const
  136.        {
  137.            ASSERT(pchar != NULL);
  138.            return pchar[n];
  139.        }
  140.        char& operator()(int n)
  141.        {
  142.            ASSERT(pchar != NULL);
  143.            return pchar[n];
  144.        }
  145.        char& operator[](int n)
  146.        {
  147.            ASSERT(pchar != NULL);
  148.            return pchar[n];
  149.        }
  150.        char operator[](int n) const
  151.        {
  152.            ASSERT(pchar != NULL);
  153.            return pchar[n];
  154.        }
  155. };
  156.  
  157. #ifdef REQUIRES_OSTREAM_INSERTER_OPERATOR
  158. inline ostream& operator << (ostream& os, cchar& cc)
  159. {
  160.    return os << cc.pchar;
  161. }
  162. #endif
  163.  
  164. #endif
  165.