📄 basevalue.cpp
字号:
char *bufPtr = NULL;
char buf[256];
// Convert
switch (type)
{
case BP_STRING:
res = valuePtr.charPtr;
break;
case BP_LONG:
snprintf(buf, 255, DBLONG_SPRINTF, *(valuePtr.longPtr));
res = buf;
break;
case BP_ULONG:
snprintf(buf, 255, DBULONG_SPRINTF, *(valuePtr.unsignedLongPtr));
res = buf;
break;
case BP_DOUBLE:
// Do the conversion as integer coversions
double intVal, fracVal;
fracVal = modf(*(valuePtr.doublePtr), &intVal);
// First convert the integer part
snprintf(buf, 255, DBULONG_SPRINTF, (DBULONG)intVal);
res = buf;
res += ".";
// Now convert the fraction.
// Note that a fraction of 84562.9932849 may be stored as 84562.9932849000033456
double tmp;
tmp = modf(fracVal, &intVal);
while (0.0 < tmp)
{
fracVal *= 10;
tmp = modf(fracVal, &intVal);
}
snprintf(buf, 255, DBULONG_SPRINTF, (DBULONG)fracVal);
res += buf;
break;
case BP_DATETIME:
res = valuePtr.JDatePtr->asString("%Y-%m-%d %H:%M:%S").c_str();
break;
case BP_DATE:
res = valuePtr.JDatePtr->asString("%Y-%m-%d").c_str();
break;
case BP_TIME:
res = valuePtr.JDatePtr->asString("%H:%M:%S").c_str();
break;
case BP_BOOLEAN:
if (*(valuePtr.boolPtr))
res = "True";
else
res = "False";
break;
case BP_BINARY:
// Return the result as a hex encoded string.
char *hexRes = (char*)malloc((2*valueSize)+1);
HexDigest::encode((unsigned char*)valuePtr.ptr, valueSize, hexRes);
res = hexRes;
free(hexRes);
break;
}
// Make a deep copy of the generated result
if (_strBuffer)
{
free(_strBuffer);
_strBuffer = NULL;
}
_strBuffer = strdup(res.c_str());
return (const char*)_strBuffer;
} // BaseValue::asString
//------------------------------------------------------------------------------
// BaseValue::asLong
//------------------------------------------------------------------------------
DBLONG
BaseValue::asLong()
{
if (!valuePtr.ptr || type == BP_NULL)
return 0;
DBLONG res = 0;
// Convert
switch (type)
{
case BP_STRING:
res = _strtoll(valuePtr.charPtr, 10);
break;
case BP_LONG:
res = *(valuePtr.longPtr);
break;
case BP_ULONG:
res = (DBLONG)*(valuePtr.unsignedLongPtr);
break;
case BP_DOUBLE:
res = (DBLONG)*(valuePtr.doublePtr);
break;
case BP_DATETIME:
case BP_DATE:
case BP_TIME:
// Try and return the unix time stamp otherwise 0
try
{
res = valuePtr.JDatePtr->asUnixTimeStamp();
}
catch(...)
{
// Date is out of range for a unix timestamp
res = 0;
}
break;
case BP_BOOLEAN:
if (*(valuePtr.boolPtr))
res = 1;
else
res = 0;
break;
case BP_BINARY:
// Always return zero as this is undefined :/
res = 0;
break;
}
return res;
} // BaseValue::asLong
//------------------------------------------------------------------------------
// BaseValue::asUnsignedLong
//------------------------------------------------------------------------------
DBULONG
BaseValue::asUnsignedLong()
{
if (!valuePtr.ptr || type == BP_NULL)
return 0;
DBULONG res = 0;
// Convert
switch (type)
{
case BP_STRING:
res = _strtoull(valuePtr.charPtr, 10);
break;
case BP_LONG:
res = (DBULONG)*(valuePtr.longPtr);
break;
case BP_ULONG:
res = *(valuePtr.unsignedLongPtr);
break;
case BP_DOUBLE:
res = (DBULONG)*(valuePtr.doublePtr);
break;
case BP_DATETIME:
case BP_DATE:
case BP_TIME:
// Try and return the unix time stamp otherwise 0
try
{
res = valuePtr.JDatePtr->asUnixTimeStamp();
}
catch(...)
{
// Date is out of range for a unix timestamp
res = 0;
}
break;
case BP_BOOLEAN:
if (*(valuePtr.boolPtr))
res = 1;
else
res = 0;
break;
case BP_BINARY:
// Always return zero as this is undefined :/
res = 0;
break;
}
return res;
} // BaseValue::asUnsignedLong
//------------------------------------------------------------------------------
// BaseValue::asFloat
//------------------------------------------------------------------------------
double
BaseValue::asFloat()
{
if (!valuePtr.ptr || type == BP_NULL)
return 0.0;
double res = 0.0;
// Convert
switch (type)
{
case BP_STRING:
res = strtod(valuePtr.charPtr, NULL);
break;
case BP_LONG:
res = (double)*(valuePtr.longPtr);
break;
case BP_ULONG:
res = (double)*(valuePtr.unsignedLongPtr);
break;
case BP_DOUBLE:
res = *(valuePtr.doublePtr);
break;
case BP_DATETIME:
case BP_DATE:
case BP_TIME:
// Return the number as the Julian Day Number.
// See the JDate class for more details
res = valuePtr.JDatePtr->asJulianDayNumber();
break;
case BP_BOOLEAN:
if (*(valuePtr.boolPtr))
res = 1.0;
else
res = 0.0;
break;
case BP_BINARY:
// Always return zero as this is undefined :/
res = 0.0;
break;
}
return res;
} // BaseValue::asFloat
//------------------------------------------------------------------------------
// BaseValue::asDateTime
//------------------------------------------------------------------------------
JDate
BaseValue::asDateTime()
{
if (!valuePtr.ptr || type == BP_NULL)
return JDate(0.0);
JDate res(0.0); // Set to Julian Day Epoch
const char *fmt[8] = {JDate::SET_FMT_DATETIME_ISO, JDate::SET_FMT_DATETIME_ISO_TZ,
JDate::SET_FMT_DATE_ISO, JDate::SET_FMT_TIME_ISO,
JDate::SET_FMT_TIME_ISO_TZ, JDate::SET_FMT_SERIAL_ISO,
JDate::SET_FMT_SERIAL_ISO_TZ, "yyyy"};
int i;
// Convert
switch (type)
{
case BP_STRING:
// Try and convert the date using all possible formats starting with the most common
// Can be in one of the following formats
// - yyyy-mm-dd hh:nn:ss
// - yyyy-mm-dd hh:nn:sszzz
// - yyyy-mm-dd
// - hh:nn:ss
// - hh:nn:sszzz
// - yyyymmddhhnnss
// - yyyymmddhhnnsszzz
// - yyyy
for(i=0; i<8; i++)
{
try
{
res.setDate(valuePtr.charPtr, fmt[i]);
break; // We have managed to convert
}
catch(...)
{ } // Date not in the correct format yet.
}
break;
case BP_LONG:
try
{
res.setDate((time_t)*valuePtr.longPtr);
}
catch(...)
{ }
break;
case BP_ULONG:
try
{
res.setDate((time_t)*(valuePtr.unsignedLongPtr));
}
catch(...)
{ }
break;
case BP_DOUBLE:
try
{
res.setDate(*valuePtr.doublePtr); // Sets in Julian Day Number
}
catch(...)
{ }
break;
case BP_DATETIME:
case BP_DATE:
case BP_TIME:
try
{
res.setDate(*valuePtr.JDatePtr);
}
catch(...)
{ }
break;
case BP_BOOLEAN:
// Always return epoch as this is undefined :/
res.setDate(0.0);
break;
case BP_BINARY:
// Always return epoch as this is undefined :/
res.setDate(0.0);
break;
}
return res;
} // BaseValue::asDateTime
//------------------------------------------------------------------------------
// BaseValue::asBoolean
//------------------------------------------------------------------------------
bool
BaseValue::asBoolean()
{
if (!valuePtr.ptr || type == BP_NULL)
return false;
bool res = false;
DBLONG check = 0;
// Convert
switch (type)
{
case BP_STRING:
// Check if the text of the string has a boolean representation
if (strcasecmp("true", valuePtr.charPtr) == 0)
res = true;
if (strcasecmp("t", valuePtr.charPtr) == 0)
res = true;
// Finally check if an integer value is present in the string
if (!res)
{
check = _strtoll(valuePtr.charPtr, 10);
res = check > 0;
}
break;
case BP_LONG:
res = *(valuePtr.longPtr) > 0L;
break;
case BP_ULONG:
res = *(valuePtr.unsignedLongPtr) > 0UL;
break;
case BP_DOUBLE:
res = *(valuePtr.doublePtr) > 0.0;
break;
case BP_DATETIME:
case BP_DATE:
case BP_TIME:
res = false;
break;
case BP_BOOLEAN:
res = *(valuePtr.boolPtr);
break;
case BP_BINARY:
// Always return false as this is undefined :/
res = false;
break;
}
return res;
} // BaseValue::asBoolean
//------------------------------------------------------------------------------
// BaseValue::asBinary
//------------------------------------------------------------------------------
void*
BaseValue::asBinary()
{
return valuePtr.ptr; // So what did you expect, fancy mmx assembler optimized code ? :)
} // BaseValue::asBinary
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -