]> www.ginac.de Git - cln.git/blob - src/base/string/cl_spushstring.h
0abfa513bd57c73deac831ab43d19c90e56cb07b
[cln.git] / src / base / string / cl_spushstring.h
1 // Extendable strings.
2
3 #ifndef _CL_SPUSHSTRING_H
4 #define _CL_SPUSHSTRING_H
5
6 #include "cln/object.h"
7 #include "cln/malloc.h"
8 #include "cl_sstring.h"
9 #include "cln/abort.h"
10
11 namespace cln {
12
13 class cl_spushstring {
14 protected:
15         char * buffer;
16         uintL alloc; // allocated size of buffer
17         uintL index; // index into buffer, 0 <= index <= alloc
18 public:
19 // Constructor. When constructed, the string is empty.
20         cl_spushstring ();
21 // Destructor.
22         ~cl_spushstring ();
23 // Forget the contents.
24         void reset ();
25 // Add a character at the end.
26         void push (char);
27 // Adds several characters at the end at once.
28         void append (const char * ptr, uintL len);
29 // Get the contents as a string. Free it using free_hook() when done.
30         char* contents ();
31 // Look at the contents.
32         uintL length () const;
33         char operator[] (uintL i) const;
34 };
35 inline cl_spushstring::cl_spushstring ()
36 {
37         alloc = 20; // Must be > 0.
38         buffer = (char *) malloc_hook(alloc);
39         index = 0;
40 }
41 inline cl_spushstring::~cl_spushstring ()
42 {
43         free_hook(buffer);
44 }
45 inline void cl_spushstring::reset ()
46 {
47         index = 0;
48 }
49 inline char* cl_spushstring::contents ()
50 {
51         return cl_sstring(buffer,index);
52 }
53 inline uintL cl_spushstring::length () const
54 {
55         return index;
56 }
57 inline char cl_spushstring::operator[] (uintL i) const
58 {
59         if (!(i < index)) cl_abort();
60         return buffer[i];
61 }
62
63 }  // namespace cln
64
65 #endif /* _CL_SPUSHSTRING_H */