SOL9 2.0 Class: Bytes

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

Source code

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


// SOL9 2009/02/17
// Renamed in the following way:
// ByteArray -> Bytes

// Array ->Bytes -> ByteArray(will be deprecated)

#pragma once

#include <sol\Array.h>
#include <sol\InvalidArgumentException.h>
#include <sol\OutOfRangeException.h>

/**
 * Bytes class
 */
namespace SOL {

class Bytes :public Array {
private:
    unsigned char*     bytes;

public:
    /**
     * Constructor
     */
    Bytes()
        :Array(0), bytes(NULL)
    {

    }

public:
    /**
     * Constructor
     */
    Bytes(uint size)
        :Array(size), bytes(NULL)
    {
        if (size <=0) {
            throw InvalidArgumentException("Bytes#Bytes: InvalidArgument");
        }

        bytes    = new unsigned char[size];
        memset(bytes, 0, size);
    }

public:
    /**
     * Constructor
     */
    Bytes(unsigned char* data, uint size)
        :Array(size), bytes(NULL)
    {
        if (data == NULL || size <=0) {
            throw InvalidArgumentException("Bytes#Bytes: InvalidArgument");
        }
        bytes    = new unsigned char[size];
        memcpy(bytes, data, size); 
    }

public:
    /**
     * Destructor
     */
    ~Bytes() 
    {
        clear();
    }

public:
    void    clear() {
        if(bytes) {
            delete [] bytes;
        }
        setSize(0);
        bytes = NULL;
    }

public:
    void    set(const unsigned char* data, uint len) {
        if (data !=NULL && len>0) {
            if (this->bytes != data) {
                delete [] bytes;
                bytes =NULL;
            }
            bytes = new unsigned char[len];
            memcpy(bytes, data, len);
            setSize(len);
        } else {
            throw InvalidArgumentException("Bytes#copy: InvalidArgument");
        }
    }

public:
    void    shallowCopy(unsigned char* data, uint len) {
        if (data !=NULL && len>0) {
            if (this->bytes != data) {
                delete [] bytes;
                bytes =NULL;
            }
            this->bytes = data;
            setSize(len);
        } else {
            throw InvalidArgumentException("Bytes#copy: InvalidArgument");
        }
    }


public:
    void    copy(uint pos, const unsigned char* data, uint len) {
        uint size = getSize();
        if (pos >=0 && data !=NULL && (pos + len)<=size) {
            unsigned char* ptr = (bytes + pos);
            memcpy(ptr, data, len);
        } else {
            throw InvalidArgumentException("Bytes#copy: InvalidArgument");
        }
    }

public:
    BOOL    expand(uint len) {
        uint size = getSize();
        BOOL rc = FALSE;
        if (len<0) {
            throw InvalidArgumentException("Bytes#expand: InvalidArgument");
        }
        unsigned char* temp = new unsigned char[size+len];

        if(temp) {
            memset(temp, 0, size+len);
            memcpy(temp, bytes, size);
        } else {
            return rc;
        }

        size += len;
        setSize(size);

        delete [] bytes;
        bytes = temp;

        rc = TRUE;
        
        return rc;
    }

public:
    unsigned char*    getContents() {
            return bytes;
    }

public:
    unsigned char    getNth(uint pos) {
        char val = NULL;
        
        if(pos>=0 && pos < getSize()) {
            val = bytes[pos];
        } else {
            throw OutOfRangeException("Bytes#getNth: Parameter is out of range");
        }
        return val;
    }

public:
    void    setNth(uint pos, unsigned char ch) {
        if(pos>=0 && pos < getSize()) {
            bytes[pos] = ch;
        } else {
            throw OutOfRangeException("Bytes#setNth: Parameter is out of range");
        }
    }

public:
    char* toStringWithSpace()
    {
        size_t len = getSize();
        size_t slen = len * 3 +1;
        char* string = new char[len*3 +1];
        memset(string, 0, slen);
        char* ptr = string; 
        for (size_t i = 0; i<len; i++) {
            //if (i >0 && (i %16) == 0) {
            //    printf("\n");
            //}
            
            sprintf(ptr, "%02X ", this->bytes[i]); 
            ptr +=3;
        }
        return string;
    }

public:
    void dump()
    {
        size_t len = getSize();
        printf("Size = %d(Bytes)\n", len);
        for (size_t i = 0; i<len; i++) {
            if (i >0 && (i %16) == 0) {
                printf("\n");
            }
            printf("%02X ", this->bytes[i]); 
        }
        printf("\n");
    }
public:
    unsigned char* getData() {
        return this->bytes;
    }

public:
    //2009/03/17
    const unsigned char* getBytes() {
        return this->bytes;
    }

};

}



Last modified: 19 Dec 2009

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