Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
StringBuffer.h
1 /*
2  * StringBuffer class interface
3  * Copyright (c) 2016, university of Toulouse.
4  *
5  * elm/string/StringBuffer.h -- interface for StringBuffer class.
6  */
7 #ifndef ELM_STRING_STRING_BUFFER_H
8 #define ELM_STRING_STRING_BUFFER_H
9 
10 #include <elm/string/CString.h>
11 #include <elm/string/String.h>
12 #include <elm/io/BlockOutStream.h>
13 #include <elm/io/Output.h>
14 
15 namespace elm {
16 
17 // StringBuffer class
18 class StringBuffer: public io::Output {
19 public:
20 
21  inline StringBuffer(int capacity = 64, int increment = 32)
22  : io::Output(_stream), _stream(capacity, increment)
23  { init(); }
24 
25  inline String toString()
26  { int len = length(); _stream.write('\0');
27  return String((String::buffer_t *)_stream.detach(), sizeof(short), len); }
28  inline CString toCString()
29  { _stream.write('\0'); return _stream.block() + sizeof(short); }
30 
31  inline String copyString()
32  { return String( _stream.block() + sizeof(short), _stream.size() - sizeof(short)); }
33  inline int length(void) const { return _stream.size() - sizeof(short); }
34  inline void reset(void) { _stream.clear(); init(); }
35  inline io::OutStream& stream(void) { return _stream; }
36 
37 private:
38  inline void init(void) { String::buffer_t str = { 0 }; _stream.write((char *)&str, sizeof(short)); }
39  io::BlockOutStream _stream;
40 };
41 
42 } // elm
43 
44 #endif // ELM_STRING_STRING_BUFFER_H
elm::io::BlockOutStream::size
int size(void) const
Definition: BlockOutStream.h:22
elm::io::BlockOutStream::write
virtual int write(const char *buffer, int size)
Definition: io_BlockOutStream.cpp:85
elm::StringBuffer
Definition: StringBuffer.h:18
elm::io::BlockOutStream::detach
char * detach(void)
Definition: BlockOutStream.h:23
elm::StringBuffer::reset
void reset(void)
Definition: StringBuffer.h:34
elm::StringBuffer::copyString
String copyString()
Definition: StringBuffer.h:31
elm::StringBuffer::stream
io::OutStream & stream(void)
Definition: StringBuffer.h:35
elm::CString
Definition: CString.h:17
elm
Definition: adapter.h:26
elm::StringBuffer::length
int length(void) const
Definition: StringBuffer.h:33
elm::StringBuffer::toCString
CString toCString()
Definition: StringBuffer.h:28
elm::io::OutStream
Definition: OutStream.h:30
elm::StringBuffer::toString
String toString()
Definition: StringBuffer.h:25
elm::io::BlockOutStream::clear
void clear(void)
Definition: BlockOutStream.h:24
elm::String
Definition: String.h:30
elm::io::BlockOutStream::block
const char * block(void) const
Definition: BlockOutStream.h:21
elm::StringBuffer::StringBuffer
StringBuffer(int capacity=64, int increment=32)
Definition: StringBuffer.h:21
elm::str
string str(const char *s)
Definition: String.h:150
elm::io::Output
Definition: Output.h:179