aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Visualization/vis_milk2/gstring.h
blob: c713464535e00d531888c012d218bc9f93aa7691 (plain) (blame)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifndef __GSTRING_SMART_STRING_CLASS__
#define __GSTRING_SMART_STRING_CLASS__ 1

// This is just a streamlined version of std::string.
// It cleans itself up automatically, it can copy itself,
//   test for equality, etc. 
// and you can use .c_str() to get the string const, so it's
//   sytactically the same (usage-wise) as std::string.
// Written by Ryan Geiss.

#include <windows.h>

class GString 
{
public:
    GString() 
    { 
        m_data = new wchar_t[1]; 
        m_data[0] = 0;
        m_size = 1; 
    }

	GString(const wchar_t* src)
	{
        m_data = NULL; 
        m_size = 0; 
        operator=(src);
    }

	GString(const GString &src_string)
	{
        m_data = NULL; 
        m_size = 0; 
        operator=(src_string);
    }

    ~GString() 
    {
        delete m_data;  // note: delete is safe on NULL ptrs
        m_data = NULL;
        m_size = 0;
    }

    inline GString& operator=(const wchar_t* src)
	{
        if (src != m_data)  // don't do anything on "x = x.c_str();"
        {
            delete m_data;  // note: delete is safe on NULL ptrs
            if (src) 
            {
                m_size = wcslen(src)+1;
                m_data = new wchar_t[m_size];
                memcpy(m_data, src, m_size*2);
            }
            else
            {
                m_size = 1;
                m_data = new wchar_t[1];
                m_data[0] = 0;
            }
        }
        return *this;
    }

    inline GString& operator=(const GString &src_string)
	{
        if (&src_string != this)  // don't do anything on "x = x;"
        {
            if (src_string.GetSize() != m_size)  //optimization
            {
                delete m_data;
                m_size = src_string.GetSize();
                m_data = new wchar_t[m_size];
            }
            memcpy(m_data, src_string.c_str(), m_size*2);
        }
        return *this;
	}

    inline wchar_t operator[](int index) const
    {
        return m_data[index];
    }

    inline bool operator==(const wchar_t* comp)
	{
        return (wcscmp(m_data,comp) == 0);
	}

    inline bool operator==(const GString &comp)
	{
        if (m_size != comp.m_size)  // shortcut
            return false;
        return (wcscmp(m_data,comp.c_str()) == 0);     //return operator==(comp.m_data);
	}

	inline const wchar_t* c_str() const
	{
        return m_data;
	}

    // This is actually unsafe, but we need it for convenience, unless we really
    // feel like copying all data twice.  BE WARNED!  When this class reallocates
    // memory, all old references to _data are invalid!
    //operator const char*() const { return _data; }
    inline int GetSize() const  //in bytes - including terminating NULL char.
    {
        return m_size;
    }

    inline int GetLength() const  
    {
        return (m_size >= 1) ? m_size-1 : 0;
    }

private:
    wchar_t* m_data;
    int   m_size;
};

class GStringA
{
public:
    GStringA() 
    { 
        m_data = new char[1]; 
        m_data[0] = 0;
        m_size = 1;
    }

	GStringA(const char* src)
	{
        m_data = NULL;
        m_size = 0;
        operator=(src);
    }

	GStringA(const GStringA &src_string)
	{
        m_data = NULL;
        m_size = 0;
        operator=(src_string);
    }

    ~GStringA()
    {
        delete m_data;  // note: delete is safe on NULL ptrs
        m_data = NULL;
        m_size = 0;
    }

    inline GStringA& operator=(const char* src)
	{
        if (src != m_data)  // don't do anything on "x = x.c_str();"
        {
            delete m_data;  // note: delete is safe on NULL ptrs
            if (src)
            {
                m_size = strlen(src)+1;
                m_data = new char[m_size];
                memcpy(m_data, src, m_size);
            }
            else
            {
                m_size = 1;
                m_data = new char[1];
                m_data[0] = 0;
            }
        }
        return *this;
    }

    inline GStringA& operator=(const GStringA &src_string)
	{
        if (&src_string != this)  // don't do anything on "x = x;"
        {
            if (src_string.GetSize() != m_size)  //optimization
            {
                delete m_data;
                m_size = src_string.GetSize();
                m_data = new char[m_size];
            }
            memcpy(m_data, src_string.c_str(), m_size);
        }
        return *this;
	}

    inline char operator[](int index) const
    {
        return m_data[index];
    }

    inline bool operator==(const char* comp)
	{
        return (strcmp(m_data,comp) == 0);
	}

    inline bool operator==(const GStringA &comp)
	{
        if (m_size != comp.m_size)  // shortcut
            return false;
        return (strcmp(m_data,comp.c_str()) == 0);     //return operator==(comp.m_data);
	}

	inline const char* c_str() const
	{
        return m_data;
	}

    // This is actually unsafe, but we need it for convenience, unless we really
    // feel like copying all data twice.  BE WARNED!  When this class reallocates
    // memory, all old references to _data are invalid!
    //operator const char*() const { return _data; }
    inline int GetSize() const  //in bytes - including terminating NULL char.
    {
        return m_size;
    }

    inline int GetLength() const  
    {
        return (m_size >= 1) ? m_size-1 : 0;
    }

private:
    char* m_data;
    int   m_size;
};

#endif