ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/include/common/string.h
Revision: 1.6
Committed: Sat Sep 22 14:27:26 2007 UTC (19 years ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +1 -1 lines
State: FILE REMOVED
Log Message:
split up ermyth into ermyth-modules, libermyth (currently just ermyth-util) and ermyth-core

File Contents

# Content
1 /**
2 * string.h: Various string structures and functions.
3 *
4 * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
5 * Rights to this code are as documented in doc/pod/gplicense.pod.
6 *
7 * $Id: string.h,v 1.5 2007-09-16 18:54:42 pippijn Exp $
8 */
9
10 #ifndef ERMYTH_STRING_H
11 #define ERMYTH_STRING_H
12
13 #include <cstddef>
14 #include <cstring>
15
16 /* static string */
17 struct ststr
18 {
19 ststr (char const * const initstr)
20 : m_rep (initstr)
21 {
22 }
23
24 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 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 {
49 }
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 }
90
91 private:
92 char *m_rep;
93 size_t pos;
94 size_t length;
95 };
96
97 #endif