SOL9 2.0 Class: BString

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

Source code

/*
 * BString.h 
 * Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


#pragma once

// 2010/04/02 Added some operator+() methods.

#include <sol/StringConverter.h>

namespace SOL {

class BString :public Object {

private:
    BSTR bstring;

public:
    BString()
    :bstring(NULL)
    {
    }

public:
    BString(const char* cstring)
    :bstring(NULL)
    {
        if (cstring) {
            StringConverter converter;
            this->bstring = converter.toBSTR(cstring);
        } else {
            ;//Igonre
        }
    }

public:
    BString(const BString& string)
    :bstring(NULL)
    {
        const wchar_t* wstring = (const wchar_t*)string;
        if (wstring) {
            this->bstring = SysAllocString(wstring);
        } else {
            ;//Ignore
        }
    }

public:
    BString(const wchar_t* wstring)
    :bstring(NULL)
    {
        if (wstring) {
            this->bstring = SysAllocString(wstring);
        } else {
            ;//Ignore
        }
    }


public:
    BString(const BSTR string)
    :bstring(NULL)
    {
        if (string) {
            this->bstring = SysAllocString(string);
        } else {
            ;//Ignore
        }
    }

public:
    ~BString()
    {
        clear();
    }

public:
    operator const wchar_t*() const{
        return (const wchar_t*)this->bstring;
    }

public:
    BString& operator=(const char* cstring)
    { 
        if (cstring) {
            StringConverter converter;
            this->bstring = converter.toBSTR(cstring);
        } else {
            this->bstring = NULL;
        }
        return *this;
    }

public:
    BString& operator=(const wchar_t* wstring)
    { 
        if (wstring) {
            this->bstring = SysAllocString(wstring);
        } else {
            this->bstring = NULL;
        }        
        return *this;
    }


public:
    operator wchar_t*() const
    {
        return (wchar_t*)this->bstring;
    }

public:
    operator const BSTR() const
    {
        return this->bstring;
    }

public:
    operator BSTR()
    {
        return this->bstring;
    }

public:
    bool operator==(const wchar_t* string)
    {
        bool rc = false;
        if (this->bstring == NULL && string ==NULL) {
            rc = true;
        }

        if (this->bstring != NULL && string != NULL) {
            if(strcmp(this->bstring, string) == 0) {
                rc = true;
            }
        }
        return rc;
    }


public:
    bool operator==(BString& string)
    {
        bool rc = false;
        const wchar_t* str = (const wchar_t*)string;
        if (this->bstring == NULL && str ==NULL) {
            rc = true;
        }

        if (this->bstring != NULL && str != NULL) {
            if(strcmp(this->bstring, str) == 0) {
                rc = true;
            }
        }
        return rc;
    }


public:
    void clear()
    {
        if (bstring) {
            SysFreeString(bstring);
            bstring = NULL;
        }
    }

public:
    void shallowCopy(BSTR bstrAllocated)
    {
        this->bstring = bstrAllocated;
    }

public:
    void print()
    {
        printf("%S\n", bstring);
    }

//2010/04/02
public:
    BString& operator+(const char* cstring)
    {
        BSTR string = NULL;
        if (cstring) {
            StringConverter converter;
            string = converter.toBSTR(cstring);
        }
        append(string);
        SysFreeString(string);

        return *this;
    }
public:
    BString& operator+(const wchar_t* wstring)
    {
        BSTR string = NULL;
        if (wstring) {
            string = SysAllocStringLen(wstring, wcslen(wstring));
        }
        append(string);
        SysFreeString(string);

        return *this;
    }


public:
    BString& operator+(BSTR string)
    {
        append(string);
        return *this;
    }


public:
    BSTR append(BSTR string)
    {
        if (string ==NULL) {
            return this->bstring;
        }
        
        int olen = 0;
        if (this->bstring) {
            olen = SysStringLen(this->bstring);
        }
        int len  = SysStringLen(string);

        BSTR constring = SysAllocStringLen(NULL, olen + len);
        
        wmemcpy(constring, this->bstring, olen);
        wmemcpy(constring + olen, string, len);

        if (this->bstring) {
            SysFreeString(this->bstring);
        }

        this->bstring = constring;
        return this->bstring;
    }

public:
    int getLength() const
    {
        long len = 0;
        if (this->bstring) {
            len = SysStringLen(this->bstring);
        }
        return len;
    }
};

}


Last modified: 2 Apr 2010

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