File: gstring.h

package info (click to toggle)
sdcc 4.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 100,120 kB
  • sloc: ansic: 935,524; cpp: 75,055; makefile: 57,615; sh: 30,106; asm: 14,243; perl: 12,136; yacc: 7,297; lisp: 1,672; python: 815; lex: 781; awk: 498; sed: 89
file content (312 lines) | stat: -rw-r--r-- 7,568 bytes parent folder | download | duplicates (2)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
 * Copyright 1999, 2000 by PC Drew
 *
 * All rights reserved.
 *
 * This file is a part of the gstring class - a string class for
 * C++ programs.
 *
 * The gstring class, including all files distributed with it,
 * is free software; you can redistribute it and/or use it and/or modify it
 * under the terms of the Python License (http://www.python.org/doc/Copyright.html)
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#ifndef _GSTRING_H
#define _GSTRING_H

//#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>

class gstring
{
public:
  // Constructors.
  gstring(void);
  //gstring(char);
  gstring(char*);
  gstring(int);
  gstring(double);

  // Copy Constructor.
  gstring(const gstring&);

  // Destructor.
  virtual ~gstring(void);

  // ASCII to Integer.
  inline int a2i(void)
  {
    assert(str != NULL);
    return (atoi(str));
  };

  // ASCII to Double.
  inline double a2d(void)
  {
    assert(str != NULL);
    return (atof(str));
  };

  // String length.
  inline int length(void)
  {
    assert(str != NULL);
    return (strlen(str));
  };

  // Repeat the string _x times.
  gstring& repeatme(int);
  inline gstring repeat(int _x)
  {
    return (gstring(str).repeatme(_x));
  };

  // Return the number of times _token appears in the string.
  int ntokens(char*);
  /*inline int ntokens(char _token)
  {
    return (ntokens((char*)_token));
    };*/
  inline int ntokens(gstring _token)
  { 
    return (ntokens((char*)_token));
  };

  // Return the number of times _token appears in the string, +1.
  // This is, theoretically, the number of fields seperated by _token.
  /*inline int nfields(char _token)
  {
    return (ntokens((char*)_token) + 1);
    };*/
  inline int nfields(char* _token)
  {
    return (ntokens(_token) + 1);
  };
  inline int nfields(gstring _token)
  {
    return (ntokens((char*)_token) + 1);
  };

  // Chop of the _xth character off the string.
  gstring& chop(int = 1);

  // Return a substring.
  gstring at(int, int);
  gstring first(int = 1);
  gstring last(int = 1);

  // Convert to upper case.
  gstring& upcaseme(void);
  inline gstring upcase(void)
  {
    return (gstring(str).upcaseme());
  };

  // Convert to lower case.
  gstring& downcaseme(void);
  inline gstring downcase(void)
  {
    return (gstring(str).downcaseme());
  };

  // Explode the string, delimted by a token, into an array of gstrings.
  gstring* explode(char*);
  /*inline gstring* explode(char _token)
  {
    return (explode((char*)_token));
    };*/
  inline gstring* explode(gstring _token)
  {
    return (explode((char*)_token));
  };

  // Append.
  gstring& append(char*);
  /*inline gstring& append(char _s)
  {
    return (append((char*)_s));
    };*/
  inline gstring& append(const gstring& _gstr)
  {
    return (append((char*)_gstr));
  };

  // Prepend.
  gstring& prepend(char*);
  /*inline gstring& prepend(char _s)
  {
    return (prepend((char*)_s));
    };*/
  inline gstring& prepend(gstring _gstr)
  {
    return (prepend((char*)_gstr));
  };

  // Addition assignment operators.
  /*inline gstring& operator +=(char _s)
  {
    return (append((char*)_s));
    };*/
  inline gstring& operator +=(char* _str)
  {
    return (append(_str));
  };
  inline gstring& operator +=(const gstring& _gstr)
  {
    return (append((char*)_gstr));
  };

  // Assignment operators.
  //gstring& operator =(char);
  gstring& operator =(char *);
  gstring& operator =(double);
  gstring& operator =(const gstring&);
  gstring& operator =(int);

  // Use for casting.  You can do both (char*)gstring and
  // (const char*)gstring.
  * operator char(void) { return str; };
  * operator char(void) const { return str; };

private:
  // Use for creating and destroying current gstrings.
  int create(char* _str);
  int destroy(void);

  // The string itself.
  char* str;
};

// Useful for converting one array type to another.
char** tochararray(gstring*, int);
gstring* togstringarray(char**, int);

// Implode an array of gstrings or char*s into a gstring.
gstring implode(char**, char*, int);
/*inline gstring implode(char** _strarr, char _token, int _num)
{
  return (implode(_strarr, (char*)_token, _num));
};*/
inline gstring implode(char** _strarr, gstring _token, int _num)
{
  return (implode(_strarr, (char*)_token, _num));
};
gstring implode(gstring*, char*, int);
/*inline gstring implode(gstring* _gstrarr, char _token, int _num)
{
  return (implode(_gstrarr, (char*)_token, _num));
};*/
inline gstring implode(gstring* _gstrarr, gstring _token, int _num)
{
  return (implode(_gstrarr, (char*)_token, _num));
};

// Addition operators.
inline gstring operator +(const gstring& _gstr, char* _str)
{
  gstring gstr(_gstr); return (gstr.append(_str));
};
inline gstring operator +(char* _str, const gstring& _gstr)
{
  gstring gstr(_str); return (gstr.append(_gstr));
};
inline gstring operator +(const gstring& _gstr1, const gstring& _gstr2)
{
  gstring gstr(_gstr1); return (gstr.append(_gstr2));
};

// Boolean equality operators.
/*inline bool operator ==(const gstring& _gstr, char _s)
{
  return (strcmp((char*)_gstr, (char*)_s) == 0);
};*/
inline bool operator ==(const gstring& _gstr, char* _str)
{
  return (strcmp((char*)_gstr, _str) == 0);
};
inline bool operator ==(const gstring& _gstr1, gstring _gstr2)
{
  return (strcmp((char*)_gstr1, (char*)_gstr2) == 0);
};

// Boolean inequality operators.
/*inline bool operator !=(const gstring& _gstr, char _s)
{
  return (strcmp((char*)_gstr, (char*)_s) != 0);
};*/
inline bool operator !=(const gstring& _gstr, char* _str)
{
  return (strcmp((char*)_gstr, _str) != 0);
};
inline bool operator !=(const gstring& _gstr1, const gstring& _gstr2)
{
  return (strcmp((char*)_gstr1, (char*)_gstr2) != 0);
};

// Boolean comparison operators.
/*inline bool operator <=(const gstring& _gstr, char _s)
{
  return (strcmp((char*)_gstr, (char*)_s) <= 0);
};*/
inline bool operator <=(const gstring& _gstr, char* _str)
{
  return (strcmp((char*)_gstr, _str) <= 0);
};
inline bool operator <=(const gstring& _gstr1, const gstring& _gstr2)
{
  return (strcmp((char*)_gstr1, (char*)_gstr2) <= 0);
};
/*inline bool operator >=(const gstring& _gstr, char _s)
{
  return (strcmp((char*)_gstr, (char*)_s) >= 0);
};*/
inline bool operator >=(const gstring& _gstr, char* _str)
{
  return (strcmp((char*)_gstr, _str) >= 0);
};
inline bool operator >=(const gstring& _gstr1, const gstring& _gstr2)
{
  return (strcmp((char*)_gstr1, (char*)_gstr2) >= 0);
};
/*inline bool operator <(const gstring& _gstr, char _s)
{
  return (strcmp((char*)_gstr, (char*)_s) < 0);
};*/
inline bool operator <(const gstring& _gstr, char* _str)
{
  return (strcmp((char*)_gstr, _str) < 0);
};
inline bool operator <(const gstring& _gstr1, const gstring& _gstr2)
{
  return (strcmp((char*)_gstr1, (char*)_gstr2) < 0);
};
/*inline bool operator >(const gstring& _gstr, char _s)
{
  return (strcmp((char*)_gstr, (char*)_s) > 0);
};*/
inline bool operator >(const gstring& _gstr, char* _str)
{
  return (strcmp((char*)_gstr, _str) > 0);
};
inline bool operator >(const gstring& _gstr1, const gstring& _gstr2)
{
  return (strcmp((char*)_gstr1, (char*)_gstr2) > 0);
};

// Stream operators (i.e. cin >> gstring or cout << gstring).
/*inline ostream& operator <<(ostream& _out, const gstring _gstr)
{
  return (_out << (char*)_gstr);
};
istream& operator >>(istream&, gstring&);*/

#endif