string.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <iostream>
14 #include <cstring>
15 #include <string>
16 #include "../../include/ecl/devices/string.hpp"
17 
18 /*****************************************************************************
19 ** Namespaces
20 *****************************************************************************/
21 
22 namespace ecl {
23 
24 /*****************************************************************************
25 ** Implementation [String]
26 *****************************************************************************/
27 String::String(const char* str)
28 {
29  buffer_length = strlen(str) + 1;
30  buffer = new char[buffer_length]; // Need +1 so we can attach \0 to produce c_str()
34 }
36  delete [] buffer;
37 }
38 const char* String::c_str() {
39  *buffer_cur_write = '\0'; // Null terminate
40  return buffer;
41 }
42 std::string String::str() {
43  std::string s;
44  s.assign(buffer,size());
45  return s;
46 }
47 
48 void String::grow(int no_bytes) {
49  char* new_buffer;
50  unsigned long cur_pos = buffer_cur_read - buffer;
51  unsigned long cur_end = buffer_cur_write - buffer;
52  new_buffer = new char[size()+no_bytes+1];
53  memcpy(new_buffer,buffer,size());
54  delete [] buffer;
55  buffer = new_buffer;
56  buffer_cur_read = buffer+cur_pos;
57  buffer_cur_write = buffer+cur_end;
58 }
59 
60 /*****************************************************************************
61 ** Implementation [String][Source Interface]
62 *****************************************************************************/
63 long String::read(char &c) {
64  if ( remaining() != 0 ) {
65  c = *buffer_cur_read;
67  return 1;
68  } else {
69  return 0;
70  }
71 }
78 long String::read(char* s, unsigned long n)
79 {
80  unsigned long rem = remaining();
81 
82  if ( rem > n ) {
83  memcpy(s,buffer_cur_read,n);
84  buffer_cur_read += n;
85  return n;
86  } else if ( rem != 0 ) {
87  memcpy(s,buffer_cur_read,rem);
88  buffer_cur_read += rem;
89  return rem;
90  } else { // rem = 0;
91  return 0;
92  }
93 }
99 unsigned long String::remaining()
100 {
102 };
103 
108 void String::clear()
109 {
110  delete [] buffer;
111  buffer = new char[1];
115 };
116 
117 /*****************************************************************************
118 ** Implementation [String][Sink Interface]
119 *****************************************************************************/
125 long String::write(char c)
126 {
127  // Remember that the last position in the buffer is for the char string terminator
128  if ( buffer_cur_write-buffer == static_cast<long>(buffer_length) -1) {
129  grow();
130  }
131  *buffer_cur_write = c;
133  return 1;
134 }
141 long String::write(const char* s, unsigned long n)
142 {
143  // Remember that the last position in the buffer is for the char string terminator
144  if ( buffer_cur_write-buffer > static_cast<long>(buffer_length) - 1 - static_cast<long>(n) ) {
145  grow(n+256);
146  }
147  memcpy(buffer_cur_write,s,n);
148  buffer_cur_write += n;
149  return n;
150 }
151 
152 
153 /*****************************************************************************
154 ** Implementation [String][Seekable Interface]
155 *****************************************************************************/
160 unsigned long String::size()
161 {
162  return buffer_cur_write - buffer;
163 }
164 
165 
166 } // namespace ecl
ecl::String::remaining
unsigned long remaining()
Specifies the number of characters remaining to be read..
Definition: string.cpp:103
ecl::String::buffer_length
unsigned long buffer_length
Definition: string.hpp:208
ecl::String::write
long write(char c)
Write a character to the buffer.
Definition: string.cpp:129
ecl::String::clear
void clear()
Clears the device's internal buffers.
Definition: string.cpp:112
ecl::String::c_str
const char * c_str()
Character string representation of the device's contents.
Definition: string.cpp:42
ecl::String::buffer_cur_write
char * buffer_cur_write
Definition: string.hpp:213
ecl::String::size
unsigned long size()
Number of characters stored in the buffer.
Definition: string.cpp:164
ecl::String::String
String(const char *str="")
Initialises the string device.
Definition: string.cpp:31
ecl::String::buffer_cur_read
char * buffer_cur_read
Definition: string.hpp:214
ecl::String::read
long read(char &c)
Read a character from the string device.
Definition: string.cpp:67
ecl::String::grow
void grow(int no_bytes=256)
Grow the buffer by the specified amount.
Definition: string.cpp:52
ecl::String::str
std::string str()
String representation of the device's contents.
Definition: string.cpp:46
ecl::String::~String
virtual ~String()
Cleans up memory allocations.
Definition: string.cpp:39
ecl::String::buffer
char * buffer
Definition: string.hpp:212
ecl
Embedded control libraries.


ecl_devices
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:45