string.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 #include <iostream>
00014 #include <cstring>
00015 #include <string>
00016 #include "../../include/ecl/devices/string.hpp"
00017 
00018 /*****************************************************************************
00019 ** Namespaces
00020 *****************************************************************************/
00021 
00022 namespace ecl {
00023 
00024 /*****************************************************************************
00025 ** Implementation [String]
00026 *****************************************************************************/
00027 String::String(const char* str)
00028 {
00029     buffer_length = strlen(str) + 1;
00030     buffer = new char[buffer_length]; // Need +1 so we can attach \0 to produce c_str()
00031     memcpy(buffer,str,buffer_length-1);
00032     buffer_cur_write = buffer + buffer_length - 1;
00033     buffer_cur_read = buffer;
00034 }
00035 String::~String() {
00036     delete [] buffer;
00037 }
00038 const char* String::c_str() {
00039     *buffer_cur_write = '\0'; // Null terminate
00040     return buffer;
00041 }
00042 std::string String::str() {
00043         std::string s;
00044     s.assign(buffer,size());
00045     return s;
00046 }
00047 
00048 void String::grow(int no_bytes) {
00049     char* new_buffer;
00050     unsigned long cur_pos = buffer_cur_read - buffer;
00051     unsigned long cur_end = buffer_cur_write - buffer;
00052     new_buffer = new char[size()+no_bytes+1];
00053     memcpy(new_buffer,buffer,size());
00054     delete [] buffer;
00055     buffer = new_buffer;
00056     buffer_cur_read = buffer+cur_pos;
00057     buffer_cur_write = buffer+cur_end;
00058 }
00059 
00060 /*****************************************************************************
00061 ** Implementation [String][Source Interface]
00062 *****************************************************************************/
00063 long String::read(char &c) {
00064     if ( remaining() != 0 ) {
00065         c = *buffer_cur_read;
00066         ++buffer_cur_read;
00067         return 1;
00068     } else {
00069         return 0;
00070     }
00071 }
00078 long String::read(char* s, unsigned long n)
00079 {
00080     unsigned long rem = remaining();
00081 
00082     if ( rem > n ) {
00083         memcpy(s,buffer_cur_read,n);
00084         buffer_cur_read += n;
00085         return n;
00086     } else if ( rem != 0 ) {
00087         memcpy(s,buffer_cur_read,rem);
00088         buffer_cur_read += rem;
00089         return rem;
00090     } else { // rem = 0;
00091         return 0;
00092     }
00093 }
00099 unsigned long String::remaining()
00100 {
00101     return buffer_cur_write - buffer_cur_read;
00102 };
00103 
00108 void String::clear()
00109 {
00110     delete [] buffer;
00111     buffer = new char[1];
00112     buffer_length = 0;
00113     buffer_cur_write = buffer;
00114     buffer_cur_read = buffer;
00115 };
00116 
00117 /*****************************************************************************
00118 ** Implementation [String][Sink Interface]
00119 *****************************************************************************/
00125 long String::write(char c)
00126 {
00127     // Remember that the last position in the buffer is for the char string terminator
00128     if ( buffer_cur_write-buffer == static_cast<long>(buffer_length) -1) {
00129         grow();
00130     }
00131     *buffer_cur_write = c;
00132     ++buffer_cur_write;
00133     return 1;
00134 }
00141 long String::write(const char* s, unsigned long n)
00142 {
00143     // Remember that the last position in the buffer is for the char string terminator
00144     if ( buffer_cur_write-buffer > static_cast<long>(buffer_length) - 1 - static_cast<long>(n) ) {
00145         grow(n+256);
00146     }
00147     memcpy(buffer_cur_write,s,n);
00148     buffer_cur_write += n;
00149     return n;
00150 }
00151 
00152 
00153 /*****************************************************************************
00154 ** Implementation [String][Seekable Interface]
00155 *****************************************************************************/
00160 unsigned long String::size()
00161 {
00162     return buffer_cur_write - buffer;
00163 }
00164 
00165 
00166 } // namespace ecl


ecl_devices
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:18:03