Set the atom to the supplied data type and value.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | hzAtom::SetValue | (hdbBasetype,hzString&,) |
Declared in file: hzDatabase.h
Defined in file : hzAtom.cpp
Function Logic:
Function body:
hzEcode hzAtom::SetValue (hdbBasetype eType)hzString& S,
{
// Set the atom to the supplied data type and value.
//
// Arguments: 1) eType The datatype
// 2) s The string that either is or contains the value
//
// Returns: E_TYPE If the anticipated data type is not specified or conflicts with current type
// E_BADVALUE If the supplied string does not represent a valid value for the anticipated data type.
// E_OK If the operation was successful.
_hzfunc("hzAtom::SetValue(hzString)") ;
const char* j ; // For processing string data
uint64_t x ; // Storage for integer data
hzXDate xd ; // Storage for hzXDate data
hzSDate sd ; // Storage for hzSDate data
hzTime ti ; // Storage for hzTime data
hzIpaddr ipa ; // Storage for hzIpaddr data
bool bMinus = false ; // Negation indicator
hzEcode rc = E_BADVALUE ; // Return code
// Clear atom first
Clear() ;
// If no value, just return
if (!S)
return E_OK ;
m_eType = eType ;
/*
** ** HadronZoo string-like types
** */
if (m_eType == BASETYPE_DIGEST)
{
hzMD5 Md5 ; // Pointer to digest value
Md5 = S ;
if (!Md5.IsNull())
{
m_Data.m_pVoid = new uchar[sizeof(hzMD5)] ;
// memcpy(m_Data.m_pVoid, Md5.Value(), sizeof(hzMD5)) ;
memcpy(m_Data.m_pVoid, &Md5, sizeof(hzMD5)) ;
m_eStatus = ATOM_SET ;
return E_OK ;
}
m_eType = BASETYPE_UNDEF ;
m_eStatus = ATOM_ERROR ;
return E_BADVALUE ;
}
if (m_eType == BASETYPE_STRING)
{
hzString tmp ;
tmp = S ; m_Data.m_uInt32 = tmp._int_addr() ; tmp._int_clr() ;
m_eType = eType ;
m_eStatus = ATOM_SET ;
// m_bCast = 1 ;
return E_OK ;
}
if (m_eType == BASETYPE_DOMAIN)
{
hzDomain tmp ;
tmp = S ;
if (tmp)
{
m_Data.m_uInt32 = tmp._int_addr() ;
tmp._inc_copy() ;
m_eType = eType ;
m_eStatus = ATOM_SET ;
// m_bCast = 1 ;
return E_OK ;
}
m_eType = BASETYPE_UNDEF ;
m_eStatus = ATOM_ERROR ;
return E_BADVALUE ;
}
if (m_eType == BASETYPE_EMADDR)
{
/*
** hzEmaddr* pEma ; // Cast
**
** pEma = (hzEmaddr*) &m_Data ;
** *pEma = S ;
** if (*pEma)
** {
** m_eType = eType ;
** m_eStatus = ATOM_SET ;
** return E_OK ;
** }
** */
hzEmaddr tmp ;
tmp = S ;
if (tmp)
{
m_Data.m_uInt32 = tmp._int_addr() ;
tmp._inc_copy() ;
m_eType = eType ;
m_eStatus = ATOM_SET ;
// m_bCast = 1 ;
return E_OK ;
}
m_eType = BASETYPE_UNDEF ;
m_eStatus = ATOM_ERROR ;
return E_BADVALUE ;
}
if (m_eType == BASETYPE_URL)
{
hzUrl tmp ; // Temp URL
tmp = S ;
if (tmp)
{
m_Data.m_uInt32 = tmp._int_addr() ;
// tmp._int_clr() ;
tmp._inc_copy() ;
m_eType = eType ;
m_eStatus = ATOM_SET ;
// m_bCast = 1 ;
return E_OK ;
}
m_eType = BASETYPE_UNDEF ;
m_eStatus = ATOM_ERROR ;
return E_BADVALUE ;
}
/*
** ** HadronZoo types without smart pointers
** */
if (m_eType == BASETYPE_IPADDR)
{
ipa = *S ;
if (ipa)
{ m_eStatus = ATOM_SET ; m_Data.m_uInt32 = (uint32_t) ipa ; return E_OK ; }
return E_BADVALUE ;
}
if (m_eType == BASETYPE_XDATE)
{
rc = xd.SetDateTime(S) ;
if (rc == E_OK && xd.AsVal())
{ m_eStatus = ATOM_SET ; m_Data.m_uInt64 = xd.AsVal() ; return E_OK ; }
m_eStatus = ATOM_ERROR ;
return E_BADVALUE ;
}
if (m_eType == BASETYPE_SDATE)
{
rc = sd.SetDate(S) ;
if (rc == E_OK)
{ m_eStatus = ATOM_SET ; m_Data.m_uInt32 = sd.NoDays() ; return E_OK ; }
m_eStatus = ATOM_ERROR ;
return E_BADVALUE ;
}
if (m_eType == BASETYPE_TIME)
{
rc = ti.SetTime(S) ;
if (rc == E_OK)
{ m_eStatus = ATOM_SET ; m_Data.m_uInt32 = ti.NoSecs() ; return E_OK ; }
m_eStatus = ATOM_ERROR ;
return E_BADVALUE ;
}
/*
** ** Numeric types (no smart pointers)
** */
if (m_eType == BASETYPE_DOUBLE)
{
if (IsDouble(m_Data.m_Double, *S))
{ m_eStatus = ATOM_SET ; return E_OK ; }
m_eStatus = ATOM_ERROR ;
return E_BADVALUE ;
}
if (m_eType == BASETYPE_BOOL)
{
if (S == "TRUE" || S == "true" || S == "yes" || S == "y" || S == "1") { m_Data.m_Bool = true ; m_eStatus = ATOM_SET ; return E_OK ; }
if (S == "FALSE" || S == "false" || S == "no" || S == "n" || S == "0") { m_Data.m_Bool = false ; m_eStatus = ATOM_SET ; return E_OK ; }
m_eStatus = ATOM_ERROR ;
return E_BADVALUE ;
}
// Must be number or number equiv
j = *S ;
if (*j == CHAR_MINUS)
{ j++ ; bMinus = true ; }
for (x = 0; IsDigit(*j) ; j++)
{ x *= 10;x += (*j - ''0'');}
if (*j)
return E_BADVALUE ;
switch (m_eType)
{
case BASETYPE_BYTE: if (x > 0x7f)
return E_BADVALUE ;
m_Data.m_sByte = x & 0x7f;
if (bMinus)
m_Data.m_sByte *= -1;
break ;
case BASETYPE_INT16: if (x > 0x7fff)
return E_BADVALUE ;
m_Data.m_sInt16 = x & 0x7fff;
if (bMinus)
m_Data.m_sInt16 *= -1;
break ;
case BASETYPE_INT32: if (x > 0x7fffffff)
return E_BADVALUE ;
m_Data.m_sInt32 = x & 0x7fffffff;
if (bMinus)
m_Data.m_sInt32 *= -1;
break ;
case BASETYPE_INT64: m_Data.m_sInt64 = x ;
if (bMinus)
m_Data.m_sInt64 *= -1;
break ;
case BASETYPE_UBYTE: if (x & 0xffffffffffffff00)
return E_BADVALUE ;
m_Data.m_uByte = (x & 0xff);
break ;
// case BASETYPE_ENUM2:
case BASETYPE_UINT16: if (x & 0xffffffffffff0000)
return E_BADVALUE ;
m_Data.m_uInt16 = (x & 0xffff);
break ;
case BASETYPE_UINT32: if (x & 0xffffffff00000000)
return E_BADVALUE ;
m_Data.m_uInt32 = (x & 0xffffffff);
break ;
// case BASETYPE_UUID:
case BASETYPE_UINT64: m_Data.m_uInt64 = x ;
break ;
}
m_eStatus = ATOM_SET ;
return E_OK ;
}