Skip to content

Commit

Permalink
ofBuffer: set and operator= from string + append method
Browse files Browse the repository at this point in the history
  • Loading branch information
arturoc committed Aug 8, 2012
1 parent 3790de2 commit 3c31bbf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
33 changes: 27 additions & 6 deletions libs/openFrameworks/utils/ofFileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ ofBuffer::ofBuffer(){
}

//--------------------------------------------------
ofBuffer::ofBuffer(const char * buffer, int size){
ofBuffer::ofBuffer(const char * buffer, unsigned int size){
set(buffer, size);
}

//--------------------------------------------------
ofBuffer::ofBuffer(const string & text){
set(text);
}

//--------------------------------------------------
ofBuffer::ofBuffer(istream & stream){
set(stream);
Expand Down Expand Up @@ -81,11 +86,21 @@ bool ofBuffer::writeTo(ostream & stream) const {
}

//--------------------------------------------------
void ofBuffer::set(const char * _buffer, int _size){
clear();
buffer.resize(_size + 1);
memcpy(getBinaryBuffer(), _buffer, _size);
buffer[_size] = 0;
void ofBuffer::set(const char * _buffer, unsigned int _size){
buffer.assign(_buffer,_buffer+_size);
buffer.resize(buffer.size()+1);
buffer.back() = 0;
}

//--------------------------------------------------
void ofBuffer::set(const string & text){
set(text.c_str(),text.size());
}

//--------------------------------------------------
void ofBuffer::append(const char * _buffer, unsigned int _size){
buffer.insert(buffer.end()-1,_buffer,_buffer+_size);
buffer.back() = 0;
}

//--------------------------------------------------
Expand Down Expand Up @@ -124,10 +139,16 @@ string ofBuffer::getText() const {
return &buffer[0];
}

//--------------------------------------------------
ofBuffer::operator string() const {
return getText();
}

//--------------------------------------------------
ofBuffer & ofBuffer::operator=(const string & text){
set(text);
}

//--------------------------------------------------
long ofBuffer::size() const {
if(buffer.empty()){
Expand Down
8 changes: 6 additions & 2 deletions libs/openFrameworks/utils/ofFileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ class ofBuffer{

public:
ofBuffer();
ofBuffer(const char * buffer, int size);
ofBuffer(const char * buffer, unsigned int size);
ofBuffer(const string & text);
ofBuffer(istream & stream);
ofBuffer(const ofBuffer & buffer_);

~ofBuffer();

void set(const char * _buffer, int _size);
void set(const char * _buffer, unsigned int _size);
void set(const string & text);
bool set(istream & stream);
void append(const char * _buffer, unsigned int _size);

bool writeTo(ostream & stream) const;

Expand All @@ -31,6 +34,7 @@ class ofBuffer{

string getText() const;
operator string() const; // cast to string, to use a buffer as a string
ofBuffer & operator=(const string & text);

long size() const;
string getNextLine();
Expand Down

0 comments on commit 3c31bbf

Please sign in to comment.