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