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

📄 basevalue.cpp

📁 C++连接一写常用数据库的接口
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * BaseValue defines a generic value storage and conversion class
 * Copyright (C) 2003 Johnathan Ingram, jingram@rogueware.org
 *
 * This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
 *
 * Note: The class makes a deep copy of the values
 *
 */

#include "baseValue.h"

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

#include "hexDigest.h"

//------------------------------------------------------------------------------
// Private Methods
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// _strtoll
//------------------------------------------------------------------------------
DBLONG
_strtoll(
      const char *nptr, 
      int base)
{
   /* Conversion Range: -9223372036854775808 to 9223372036854775807 */
   DBLONG res = 0, e;
   int i,j;

   if (!base) 
      base = 10;

   // Only convert the first valid chars.
   char *end = (char*)((int)nptr + strlen(nptr)-1);
   for (i=0; i<strlen(nptr); i++)
      if (!isdigit(nptr[i]) && nptr[i] != '+' && nptr[i] != '-')
      {
         end = (char*)((int)nptr + i -1);
         break;	
      }

   // Convert the string back to front raising base to the power..
   for (i=end-nptr, j=0; i>=0; i--, j++)
   {
      if (!isdigit(nptr[i])) 
         break;

      e = 1;
      for (int k=1; k<=j; k++)
         e *= base;
      res += (nptr[i] - 48) * e;
   }

   // Check if we have a - sign & negate.
   if (nptr[0] == '-')
      res *= -1;

   return res;
}  // _strtoll


//Private function to convert a string to a unsigned long long integer
DBULONG 
_strtoull(
      const char *nptr, 
      int base)
{
   /* Conversion Range: 0 to 18446744073709551615 */
   DBULONG res = 0, e;
   int i,j;

   if (!base) 
      base = 10;

   //Only convert the first valid chars.
   char *end = (char*)((int)nptr + strlen(nptr)-1);
   for (i=0; i<strlen(nptr); i++)
      if (!isdigit(nptr[i]) && nptr[i] != '+')
      {
         end = (char*)((int)nptr + i -1);
         break;	
      }
            
   //Convert the string back to front raising base to the power..
   for (i=end-nptr, j=0; i>=0; i--, j++)
   {
      if (!isdigit(nptr[i])) 
         break;

      e = 1;
      for (int k=1; k<=j; k++)
         e *= base;
      res += (nptr[i] - 48) * e;
   }

   return res;
}  // _strtoull          


//------------------------------------------------------------------------------
// BaseValue::_releasePtr
//------------------------------------------------------------------------------
void
BaseValue::_releasePtr()
{
   if (valuePtr.ptr)
   {
      switch (type)
      {
         case BP_STRING:
            free((void*)valuePtr.charPtr);
            break;         

         case BP_LONG:
            delete valuePtr.longPtr;
            break;

         case BP_ULONG:
            delete valuePtr.unsignedLongPtr;
            break;

         case BP_DOUBLE:
            delete valuePtr.doublePtr;
            break;

         case BP_DATETIME:
         case BP_DATE:
         case BP_TIME:
            delete valuePtr.JDatePtr;
            break;

         case BP_BOOLEAN:
            delete valuePtr.boolPtr;
            break;

         case BP_BINARY:
            free(valuePtr.ptr);
            break;
         
         // Don't free BP_NULL as a deep copy is not made
      }

      valuePtr.ptr = NULL;
      valueSize = 0;
      type = BP_UNKNOWN;
   }

   // Free any internal buffers
   if (_strBuffer)
   {
      free(_strBuffer);
      _strBuffer = NULL;
   }
}  // BaseValue::_releasePtr          


//------------------------------------------------------------------------------
// Public Methods
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// BaseValue::BaseValue
//------------------------------------------------------------------------------
BaseValue::BaseValue(
      const string& fieldName)   
   :
      _strBuffer(NULL)
{
   valuePtr.ptr = NULL;
   valueSize = 0;
   type = BP_UNKNOWN;
   
   // Convert the parameter name to all lowercase
   BaseValue::bindName = fieldName.c_str();
   for (int i=0; i<BaseValue::bindName.length(); i++)
      BaseValue::bindName[i] = tolower(bindName[i]);
}  // BaseValue::BaseValue          


//------------------------------------------------------------------------------
// BaseValue::~BaseValue
//------------------------------------------------------------------------------
BaseValue::~BaseValue()
{   
   // Release the bind parameter and free any deep copies made
   _releasePtr();
}  // BaseValue::~BaseValue


//------------------------------------------------------------------------------
// BaseValue::name
//------------------------------------------------------------------------------
string
BaseValue::name()
{
   return bindName;
}  // BaseValue::name


//------------------------------------------------------------------------------
// BaseValue::setNULL
//------------------------------------------------------------------------------
void 
BaseValue::setNULL()
{
   _releasePtr();

   type = BP_NULL;
   valuePtr.ptr = NULL;

   valueSize = 0;
}  // BaseValue::setNULL


//------------------------------------------------------------------------------
// BaseValue::setString
//------------------------------------------------------------------------------
void 
BaseValue::setString(
   const char* bindVar)
{
   _releasePtr();

   type = BP_STRING;
   valuePtr.charPtr = strdup(bindVar);
   
   valueSize = strlen(valuePtr.charPtr);
}  // BaseValue::setString


//------------------------------------------------------------------------------
// BaseValue::setString
//------------------------------------------------------------------------------
void 
BaseValue::setString(
   const string &bindVar)
{
   _releasePtr();

   type = BP_STRING;
   valuePtr.charPtr = strdup(bindVar.c_str());
   
   valueSize = strlen(valuePtr.charPtr);
}  // BaseValue::setString


//------------------------------------------------------------------------------
// BaseValue::setLong
//------------------------------------------------------------------------------
void 
BaseValue::setLong(
   DBLONG bindVar) 
{
   _releasePtr();

   type = BP_LONG;
   valuePtr.longPtr = new DBLONG;

   *(valuePtr.longPtr) = bindVar;
   valueSize = sizeof(DBLONG);
}  // BaseValue::setLong


//------------------------------------------------------------------------------
// BaseValue::setUnsignedLong
//------------------------------------------------------------------------------
void 
BaseValue::setUnsignedLong(
   DBULONG bindVar)
{
   _releasePtr();

   type = BP_ULONG;
   valuePtr.unsignedLongPtr = new DBULONG;

   *(valuePtr.unsignedLongPtr) = bindVar;
   valueSize = sizeof(DBULONG);
}  // BaseValue::setUnsignedLong


//------------------------------------------------------------------------------
// BaseValue::setFloat
//------------------------------------------------------------------------------
void 
BaseValue::setFloat(
   double bindVar)
{
   _releasePtr();

   type = BP_DOUBLE;
   valuePtr.doublePtr = new double;

   *(valuePtr.doublePtr) = bindVar;
   valueSize = sizeof(double);
}  // BaseValue::setFloat


//------------------------------------------------------------------------------
// BaseValue::setDateTime
//------------------------------------------------------------------------------
void 
BaseValue::setDateTime(
   JDate &bindVar) 
{
   _releasePtr();

   type = BP_DATETIME;
   valuePtr.JDatePtr = new JDate();

   valuePtr.JDatePtr->setDate(bindVar);
   valueSize = sizeof(JDate);
}  // BaseValue::setDateTime


//------------------------------------------------------------------------------
// BaseValue::setDate
//------------------------------------------------------------------------------
void 
BaseValue::setDate(
   JDate &bindVar) 
{
   _releasePtr();

   type = BP_DATE;
   valuePtr.JDatePtr = new JDate();

   valuePtr.JDatePtr->setDate(bindVar);
   valueSize = sizeof(JDate);
}  // BaseValue::setDate


//------------------------------------------------------------------------------
// BaseValue::setTime
//------------------------------------------------------------------------------
void 
BaseValue::setTime(
   JDate &bindVar) 
{
   _releasePtr();

   type = BP_TIME;
   valuePtr.JDatePtr = new JDate();

   valuePtr.JDatePtr->setDate(bindVar);
   valueSize = sizeof(JDate);
}  // BaseValue::setTime


//------------------------------------------------------------------------------
// BaseValue::setBoolean
//------------------------------------------------------------------------------
void 
BaseValue::setBoolean(
   bool bindVar)
{
   _releasePtr();

   type = BP_BOOLEAN;
   valuePtr.boolPtr = new bool;

   *(valuePtr.boolPtr) = bindVar;
   valueSize = sizeof(bool);
}  // BaseValue::setBoolean


//------------------------------------------------------------------------------
// BaseValue::setBinary
//------------------------------------------------------------------------------
void 
BaseValue::setBinary(
   void *bindVar, 
   unsigned long bindVarSize)
{
   _releasePtr();

   type = BP_BINARY;
   valuePtr.ptr = malloc(bindVarSize);
   
   memcpy(valuePtr.ptr, bindVar, bindVarSize);
   valueSize = bindVarSize;
}  // BaseValue::setBinary


//------------------------------------------------------------------------------
// BaseValue::getSize
//------------------------------------------------------------------------------
DBULONG
BaseValue::getSize()
{
   return valueSize;
}  // BaseValue::getSize


//------------------------------------------------------------------------------
// BaseValue::isNULL
//------------------------------------------------------------------------------
bool
BaseValue::isNULL()
{
   if (type == BP_NULL)
      return true;
   else
      return false;
}  // BaseValue::isNULL


//------------------------------------------------------------------------------
// BaseValue::asString
//------------------------------------------------------------------------------
const char*
BaseValue::asString()
{
   string res;

   // Check if the field is NULL
   if (!valuePtr.ptr || type == BP_NULL)
      return "NULL";

⌨️ 快捷键说明

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