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