SOL9 2.0 Class: StringBuffer

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * StringBuffer.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
// 2000/11/11
#pragma once

#include <sol\Object.h>
#include <sol\String.h>
#include <sol\InvalidArgumentException.h>

namespace SOL {

class StringBuffer :public Object{
private:
    TCHAR*    buffer;
    int        size;
    int        increment;
    int        position;

    //2008/07/06
public:

    StringBuffer(int s =SIZE_1KB, int inc = SIZE_128B)
        :size(s), increment(inc), buffer(NULL),
        position(0)
    { 
        if (size<=0 || increment<=0) {
            throw InvalidArgumentException("StringBuffer::StringBuffer,1,InvalidArgument");    
        }

        this->buffer = new TCHAR[size+1];
        memset(this->buffer, (TCHAR)0, size+1);
    }

public:
    ~StringBuffer()
    {
        if (this->buffer) {
            delete [] this->buffer;
            this->buffer = NULL;
        }
    }

public:
    void clear()
    {
        this->position = 0;
        if (this->buffer) {
            //delete [] this->buffer;
        
            //this->size = SIZE_1KB; // 2008/07/05
            //this->buffer = new TCHAR[size+1];
            memset(this->buffer, (TCHAR)0, size+1);
        }
    }

public:
    bool append(TCHAR ch)
    {    
        if (position >=size) {
            size += increment;
            TCHAR* temp = new TCHAR[size+1];
            if (temp == NULL) {
                return false;
            }
            memset(temp, (TCHAR)0, size+1);
            memcpy(temp, buffer, position);
            delete [] buffer;
            buffer = temp;
        }

        *(buffer+position) = ch;
        position++;
        *(buffer+position) = Zero;

        return true;
    }

public:
    bool append(const TCHAR* string)
    {
        bool rc = true;
        if (string) {
            int len = strlen(string);
            for (int i = 0; i<len; i++) {
                rc = append(*string++);
            }
        }
        return rc;
    }


//<added date="2000/11/10">
public:
    bool append(int num)
    {
        TCHAR integer[80];
        _stprintf_s(integer, SizeOf(integer), _T("%d"), num);
        return append(integer);
    }

public:
    bool append(float num)
    {
        TCHAR integer[80];
        _stprintf_s(integer, SizeOf(integer), _T("%f"), num);
        return append(integer);
    }

public:
    bool append(String& string)
    {
        return append((const TCHAR*)string);
    }

public:
    String* newString() 
    {
        //buffer is NULL terminated
        return new String(this->buffer);    
    }
//<added>

public:
    const TCHAR* find(const TCHAR* string)
    {
        const TCHAR* ptr = NULL;
        if (buffer && string) {
            ptr = strstr(buffer, string);
        }
        return string;
    }

public:
    const TCHAR* getBuffer() const { 
        return this->buffer; 
    }

public:
    operator const TCHAR*() const{
        return this->buffer;
    }

public:
    String*        getString() const { 
        return new String(this->buffer); 
    }

public:
    int            getSize() const  { 
        return this->size; 
    }

public:
    int            getContentSize() const { 
        return this->position; 
    }

};

}



Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.