⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ocibin.cpp

📁 一个通用的oracle OCI开发程序包
💻 CPP
字号:
#ifndef _COCIBinary
#include "ocibin.h"
#endif

#include <assert.h>

COCIBinary::COCIBinary() : m_Buffer(0)
{
	resize(0);
}

COCIBinary::COCIBinary(char* cp, ub4 size) : m_Buffer(0)
{
	resize(size);	// Allocate buffer of correct size
	memcpy(m_Buffer, cp, size);	// then fill with values 
}

COCIBinary::COCIBinary(COCIBinary& binary) : m_Buffer(0)
{
	resize(binary.length());
	memcpy(m_Buffer, binary.m_Buffer, binary.length());
}

COCIBinary::~COCIBinary()
{
	delete [] m_Buffer;
}

ub1* COCIBinary::begin()
{
	return m_Buffer;
}

ub1* COCIBinary::end()
{
	return m_Buffer + length();
}

void COCIBinary::insert(ub4 position, COCIBinary& newBinary)
{
	ub4 len = length();
	ub4 nbLen = newBinary.length();

	ub4 newLen = len + nbLen;

	resize(newLen);

	ub4 i = 0;
	for(i = len; i > position; i--)
		m_Buffer[i + nbLen] = m_Buffer[i];

	for(i = 0; i < nbLen; i++)
		m_Buffer[position + i] = newBinary[i];

}

int COCIBinary::length()
{
	return m_BufferLength;
}

void COCIBinary::remove(ub4 start, ub4 len)
{
	assert(len < m_BufferLength);
	assert(start < m_BufferLength);

	memset(m_Buffer + start, '.', len);
}


void COCIBinary::replace(ub4 start, ub4 len, COCIBinary& newBinary)
{
	remove(start, len);
	insert(start, newBinary);
}

void COCIBinary::resize(ub4 newlength)
{
	if(m_Buffer == 0)
	{
		m_BufferLength = 0;
	}

	// Do we need to shrink the buffer?
	if(newlength < m_BufferLength)
	{
		m_Buffer = (ub1*)realloc(m_Buffer, newlength);
	}
	else
	{
		ub1* newBuffer = new ub1[newlength];
		assert(newBuffer != 0);
		memset(newBuffer, '\0', newlength);
		// Copy existing bytes...
    if(m_Buffer)
    {
		  memcpy(newBuffer, m_Buffer, m_BufferLength);
    }
		if(m_Buffer != 0)
		{
			delete [] m_Buffer;
		}
		m_Buffer = newBuffer;
		m_BufferLength = newlength;
	}

}


std::string COCIBinary::raw_to_hex()
{
	std::string hex;
  for(ub4 i = 0; i < m_BufferLength; i++)
  {

    int hi = m_Buffer[i] / 16;
    int lo = m_Buffer[i] % 16;
    char h,l;
    if(hi < 10)
    {
      h = hi + '0';
    }
    else
    {
      h = hi - 10 + 'a';
    }

    if(lo < 10)
    {
      l = lo + '0';
    }
    else
    {
      l = lo - 10 + 'a';
    }
    hex += h;
    hex += l;
  }
	return hex;
}

void COCIBinary::hex_to_raw(std::string& hexstr)
{
  ub4 len = hexstr.length();
  resize(len / 2);

  assert(len % 2 == 0);

  ub4 buf_count = 0;

  for(ub4 i = 0; i < len; i+=2)
  {
    char hi = hexstr[(int)i];
    char lo = hexstr[(int)i+1];

    assert((hi >= '0' && hi <= '9') || (hi >= 'a' && hi <= 'f') || (hi >= 'A' && hi <= 'F'));
    assert((lo >= '0' && lo <= '9') || (lo >= 'a' && lo <= 'f') || (lo >= 'A' && lo <= 'F'));

    int byte = 0;
    if(hi >= '0' && hi <= '9')
    {
      byte = (hi - '0') * 16;
    }
    else if(hi >= 'a' && hi <= 'f')
    {
      byte = ((hi - 'a') + 10) * 16;
    }
    else
    {
      byte = ((hi - 'A') + 10) * 16;
    }

    if(lo >= '0' && lo <= '9')
    {
      byte = byte + (lo - '0');
    }
    else if(lo >= 'a' && lo <= 'f')
    {
      byte = byte + ((lo - 'a') + 10);
    }
    else
    {
      byte = byte + ((lo - 'A') + 10);
    }

    m_Buffer[buf_count++] = byte;
  }

}

ub1& COCIBinary::operator[] (ub4 index)
{
	assert(index <= m_BufferLength);
	return m_Buffer[index];
}

void COCIBinary::operator = (const COCIBinary& binary)
{
	resize(((COCIBinary&)binary).length());
	memcpy(m_Buffer, binary.m_Buffer, ((COCIBinary&)binary).length());
}

COCIBinary& COCIBinary::operator = (char * str)
{
  COCIBinary bin(str,strlen(str));
  *this = bin;
  return *this;
}

void COCIBinary::operator += (COCIBinary& right)
{
	insert(length(), right);
}

COCIBinary::operator char* () const
{
  return (char*)m_Buffer;
}

COCIBinary operator + (COCIBinary& left, COCIBinary& right)
{
	COCIBinary clone(left);	// Copy left argument
	clone += right;
	return clone;
}


int operator == (const COCIBinary& left, const COCIBinary& right)
{
	return memcmp(left.m_Buffer, right.m_Buffer, left.m_BufferLength) == 0;
}

int operator != (const COCIBinary& left, const COCIBinary& right)
{
	return !(left == right);
}

int operator < (const COCIBinary& left, const COCIBinary& right)
{
	return memcmp(left.m_Buffer, right.m_Buffer, left.m_BufferLength) < 0;
}

int operator <= (const COCIBinary& left, const COCIBinary& right)
{
	return (left < right || left == right);
}

int operator > (const COCIBinary& left, const COCIBinary& right)
{
	return memcmp(left.m_Buffer, right.m_Buffer, left.m_BufferLength) > 0;
}

int operator >= (const COCIBinary& left, const COCIBinary& right)
{
	return (left > right || left == right);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -