| 1 |
pippijn |
1.5 |
/** |
| 2 |
|
|
* string.h: Various string structures and functions. |
| 3 |
|
|
* |
| 4 |
pippijn |
1.1 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
| 5 |
pippijn |
1.5 |
* Rights to this code are as documented in doc/pod/gplicense.pod. |
| 6 |
pippijn |
1.1 |
* |
| 7 |
pippijn |
1.5 |
* $Id: string.h,v 1.4 2007-09-05 11:23:13 pippijn Exp $ |
| 8 |
pippijn |
1.1 |
*/ |
| 9 |
|
|
|
| 10 |
pippijn |
1.2 |
#ifndef ERMYTH_STRING_H |
| 11 |
|
|
#define ERMYTH_STRING_H |
| 12 |
pippijn |
1.1 |
|
| 13 |
pippijn |
1.4 |
#include <cstddef> |
| 14 |
|
|
#include <cstring> |
| 15 |
pippijn |
1.1 |
|
| 16 |
pippijn |
1.2 |
/* static string */ |
| 17 |
|
|
struct ststr |
| 18 |
pippijn |
1.1 |
{ |
| 19 |
pippijn |
1.2 |
ststr (char const * const initstr) |
| 20 |
|
|
: m_rep (initstr) |
| 21 |
pippijn |
1.1 |
{ |
| 22 |
|
|
} |
| 23 |
|
|
|
| 24 |
pippijn |
1.2 |
ststr (ststr &initstr) |
| 25 |
|
|
: m_rep (initstr) |
| 26 |
|
|
{ |
| 27 |
|
|
} |
| 28 |
|
|
|
| 29 |
|
|
char const *c_str () const |
| 30 |
|
|
{ |
| 31 |
|
|
return m_rep; |
| 32 |
|
|
} |
| 33 |
|
|
|
| 34 |
|
|
operator char const * () const |
| 35 |
|
|
{ |
| 36 |
|
|
return c_str (); |
| 37 |
|
|
} |
| 38 |
|
|
|
| 39 |
pippijn |
1.4 |
private: |
| 40 |
|
|
char const * const m_rep; |
| 41 |
|
|
}; |
| 42 |
|
|
|
| 43 |
|
|
// dynamic string |
| 44 |
|
|
struct dynstr |
| 45 |
|
|
{ |
| 46 |
|
|
dynstr (size_t initlength = 32) |
| 47 |
|
|
: m_rep (new char[initlength]), pos (0), length (initlength) |
| 48 |
pippijn |
1.2 |
{ |
| 49 |
pippijn |
1.4 |
} |
| 50 |
|
|
|
| 51 |
|
|
~dynstr () |
| 52 |
|
|
{ |
| 53 |
|
|
delete[] m_rep; |
| 54 |
|
|
} |
| 55 |
|
|
|
| 56 |
|
|
void clear () |
| 57 |
|
|
{ |
| 58 |
|
|
m_rep[0] = '\0'; |
| 59 |
|
|
pos = 0; |
| 60 |
|
|
} |
| 61 |
|
|
|
| 62 |
|
|
char const *c_str () const |
| 63 |
|
|
{ |
| 64 |
|
|
return m_rep; |
| 65 |
|
|
} |
| 66 |
|
|
|
| 67 |
|
|
operator char const * () const |
| 68 |
|
|
{ |
| 69 |
|
|
return c_str (); |
| 70 |
|
|
} |
| 71 |
|
|
|
| 72 |
|
|
size_t size () |
| 73 |
|
|
{ |
| 74 |
|
|
return length; |
| 75 |
|
|
} |
| 76 |
|
|
|
| 77 |
|
|
dynstr &operator = (char const *rhs) |
| 78 |
|
|
{ |
| 79 |
|
|
clear (); |
| 80 |
|
|
add (rhs, strlen (rhs)); |
| 81 |
|
|
return *this; |
| 82 |
|
|
} |
| 83 |
|
|
|
| 84 |
|
|
int add (char const c); |
| 85 |
|
|
int add (char const * const src, size_t size); |
| 86 |
|
|
int add (char const * const src) |
| 87 |
|
|
{ |
| 88 |
|
|
return add (src, strlen (src)); |
| 89 |
pippijn |
1.2 |
} |
| 90 |
|
|
|
| 91 |
|
|
private: |
| 92 |
pippijn |
1.4 |
char *m_rep; |
| 93 |
|
|
size_t pos; |
| 94 |
|
|
size_t length; |
| 95 |
pippijn |
1.2 |
}; |
| 96 |
|
|
|
| 97 |
pippijn |
1.1 |
#endif |