static PHB_SOCKEX s_sockexNew( HB_SOCKET sd, PHB_ITEM pParams ) { PHB_SOCKEX pSock; HB_BOOL fServer = HB_FALSE; HB_MAXINT timeout = -1; SSL * ssl = NULL; if( pParams && HB_IS_HASH( pParams ) ) { PHB_ITEM pItem; if( ssl == NULL && ( pItem = hb_hashGetCItemPtr( pParams, "ssl" ) ) != NULL ) ssl = hb_SSL_itemGet( pItem ); if( ssl == NULL && ( pItem = hb_hashGetCItemPtr( pParams, "ctx" ) ) != NULL ) ssl = hb_SSL_itemGet( pItem ); if( ssl == NULL && ( pItem = hb_hashGetCItemPtr( pParams, "key" ) ) != NULL ) ssl = hb_SSL_itemGet( pItem ); if( ( pItem = hb_hashGetCItemPtr( pParams, "timeout" ) ) != NULL && HB_IS_NUMERIC( pItem ) ) timeout = hb_itemGetNInt( pItem ); if( ( pItem = hb_hashGetCItemPtr( pParams, "server" ) ) != NULL && HB_IS_LOGICAL( pItem ) ) fServer = hb_itemGetL( pItem ); else if( ( pItem = hb_hashGetCItemPtr( pParams, "client" ) ) != NULL && HB_IS_LOGICAL( pItem ) ) fServer = ! hb_itemGetL( pItem ); } pSock = hb_sockexNewSSL( sd, ssl, fServer, timeout ); if( pSock ) hb_socekxParamsInit( pSock, pParams ); return pSock; }
static char * hb_itemStringCon( PHB_ITEM pItem, HB_SIZE * pnLen, HB_BOOL * pfFreeReq ) { /* logical values in device output (not console, stdout or stderr) are shown as single letter */ if( HB_IS_LOGICAL( pItem ) ) { *pnLen = 1; *pfFreeReq = HB_FALSE; return ( char * ) ( hb_itemGetL( pItem ) ? "T" : "F" ); } return hb_itemString( pItem, pnLen, pfFreeReq ); }
static HB_ERRCODE hb_rddEvalWABlock( AREAP pArea, void * pBlock ) { PHB_ITEM pItem; hb_rddSelectWorkAreaNumber( pArea->uiArea ); pItem = hb_vmEvalBlockOrMacro( ( PHB_ITEM ) pBlock ); if( hb_vmRequestQuery() != 0 || ( HB_IS_LOGICAL( pItem ) && ! hb_itemGetL( pItem ) ) ) return HB_FAILURE; else return HB_SUCCESS; }
static HB_ERRCODE sqlbasePutValue( SQLBASEAREAP pArea, HB_USHORT uiIndex, PHB_ITEM pItem ) { LPFIELD pField; HB_ERRCODE errCode; if( uiIndex == 0 || uiIndex > pArea->area.uiFieldCount ) return HB_FAILURE; if( ! pArea->fPositioned ) return HB_SUCCESS; if( ! pArea->fRecordChanged && SELF_GOHOT( ( AREAP ) pArea ) == HB_FAILURE ) return HB_FAILURE; errCode = HB_SUCCESS; pField = pArea->area.lpFields + ( uiIndex - 1 ); if( ( ( HB_IS_MEMO( pItem ) || HB_IS_STRING( pItem ) ) && ( pField->uiType == HB_FT_STRING || pField->uiType == HB_FT_MEMO ) ) || ( HB_IS_DATE( pItem ) && pField->uiType == HB_FT_DATE ) || ( HB_IS_TIMESTAMP( pItem ) && pField->uiType == HB_FT_TIMESTAMP ) || ( HB_IS_NUMBER( pItem ) && ( pField->uiType == HB_FT_INTEGER || pField->uiType == HB_FT_LONG || pField->uiType == HB_FT_FLOAT || pField->uiType == HB_FT_DOUBLE ) ) || ( HB_IS_LOGICAL( pItem ) && pField->uiType == HB_FT_LOGICAL ) || HB_IS_NIL( pItem ) ) { hb_arraySet( ( PHB_ITEM ) pArea->pRecord, uiIndex, pItem ); } else { PHB_ITEM pError; pError = hb_errNew(); hb_errPutGenCode( pError, EG_DATATYPE ); hb_errPutDescription( pError, hb_langDGetErrorDesc( EG_DATATYPE ) ); hb_errPutOperation( pError, hb_dynsymName( ( PHB_DYNS ) pField->sym ) ); hb_errPutSubCode( pError, errCode ); hb_errPutFlags( pError, EF_CANDEFAULT ); errCode = SELF_ERROR( ( AREAP ) pArea, pError ); hb_itemRelease( pError ); return errCode == E_DEFAULT ? HB_SUCCESS : HB_FAILURE; } return HB_SUCCESS; }
void hb_socekxParamsInit( PHB_SOCKEX pSock, PHB_ITEM pParams ) { if( pParams && HB_IS_HASH( pParams ) ) { PHB_ITEM pItem; if( ( pItem = hb_hashGetCItemPtr( pParams, "readahead" ) ) != NULL && HB_IS_NUMERIC( pItem ) ) { if( pSock->buffer == NULL ) pSock->readahead = hb_itemGetNL( pItem ); } if( ( pItem = hb_hashGetCItemPtr( pParams, "flush" ) ) != NULL && HB_IS_NUMERIC( pItem ) ) pSock->iAutoFlush = hb_itemGetNI( pItem ); if( ( pItem = hb_hashGetCItemPtr( pParams, "redir" ) ) != NULL && HB_IS_LOGICAL( pItem ) ) pSock->fRedirAll = hb_itemGetL( pItem ); } }
HB_USHORT hb_errLaunch( PHB_ITEM pError ) { HB_USHORT uiAction = E_DEFAULT; /* Needed to avoid GCC -O2 warning */ HB_TRACE( HB_TR_DEBUG, ( "hb_errLaunch(%p)", pError ) ); if( pError ) { PHB_ERRDATA pErrData = ( PHB_ERRDATA ) hb_stackGetTSD( &s_errData ); HB_USHORT uiFlags = hb_errGetFlags( pError ); PHB_ITEM pResult; /* Check if we have a valid error handler */ if( ! pErrData->errorBlock || ! HB_IS_EVALITEM( pErrData->errorBlock ) ) hb_errInternal( HB_EI_ERRNOBLOCK, NULL, NULL, NULL ); /* Check if the error launcher was called too many times recursively */ if( pErrData->iLaunchCount == HB_ERROR_LAUNCH_MAX ) hb_errInternal( HB_EI_ERRTOOMANY, NULL, NULL, NULL ); /* Launch the error handler: "lResult := Eval( ErrorBlock(), oError )" */ pErrData->iLaunchCount++; /* set DosError() to last OS error code */ pErrData->uiErrorDOS = ( int ) hb_errGetOsCode( pError ); /* Add one try to the counter. */ if( uiFlags & EF_CANRETRY ) hb_errPutTries( pError, ( HB_USHORT ) ( hb_errGetTries( pError ) + 1 ) ); if( pErrData->errorHandler ) { /* there is a low-level error handler defined - use it instead * of normal Harbour level one */ pErrData->errorHandler->Error = pError; pErrData->errorHandler->ErrorBlock = pErrData->errorBlock; pResult = ( pErrData->errorHandler->Func )( pErrData->errorHandler ); pErrData->errorHandler->Error = NULL; } else pResult = hb_itemDo( pErrData->errorBlock, 1, pError ); pErrData->iLaunchCount--; /* Check results */ if( hb_vmRequestQuery() != 0 ) { if( pResult ) hb_itemRelease( pResult ); uiAction = E_BREAK; } else if( pResult ) { HB_BOOL bFailure = HB_FALSE; /* If the error block didn't return a logical value, */ /* or the canSubstitute flag has been set, consider it as a failure */ if( ! HB_IS_LOGICAL( pResult ) || ( uiFlags & EF_CANSUBSTITUTE ) ) bFailure = HB_TRUE; else { uiAction = hb_itemGetL( pResult ) ? E_RETRY : E_DEFAULT; if( ( uiAction == E_DEFAULT && !( uiFlags & EF_CANDEFAULT ) ) || ( uiAction == E_RETRY && !( uiFlags & EF_CANRETRY ) ) ) bFailure = HB_TRUE; } hb_itemRelease( pResult ); if( bFailure ) hb_errInternal( HB_EI_ERRRECFAILURE, NULL, NULL, NULL ); } else hb_errInternal( HB_EI_ERRRECFAILURE, NULL, NULL, NULL ); } else uiAction = E_RETRY; /* Clipper does this, undocumented */ return uiAction; }
/* * Assign a value to a field. */ static HB_ERRCODE hb_delimPutValue( DELIMAREAP pArea, USHORT uiIndex, PHB_ITEM pItem ) { char szBuffer[ 256 ]; HB_ERRCODE uiError; LPFIELD pField; USHORT uiSize; HB_TRACE(HB_TR_DEBUG, ("hb_delimPutValue(%p,%hu,%p)", pArea, uiIndex, pItem)); if( !pArea->fPositioned ) return HB_SUCCESS; if( !pArea->fRecordChanged ) return HB_FAILURE; uiError = HB_SUCCESS; --uiIndex; pField = pArea->lpFields + uiIndex; if( pField->uiType != HB_FT_MEMO && pField->uiType != HB_FT_NONE ) { if( HB_IS_MEMO( pItem ) || HB_IS_STRING( pItem ) ) { if( pField->uiType == HB_FT_STRING ) { uiSize = ( USHORT ) hb_itemGetCLen( pItem ); if( uiSize > pField->uiLen ) uiSize = pField->uiLen; memcpy( pArea->pRecord + pArea->pFieldOffset[ uiIndex ], hb_itemGetCPtr( pItem ), uiSize ); #ifndef HB_CDP_SUPPORT_OFF hb_cdpnTranslate( (char *) pArea->pRecord + pArea->pFieldOffset[ uiIndex ], hb_cdppage(), pArea->cdPage, uiSize ); #endif memset( pArea->pRecord + pArea->pFieldOffset[ uiIndex ] + uiSize, ' ', pField->uiLen - uiSize ); } else uiError = EDBF_DATATYPE; } else if( HB_IS_DATE( pItem ) ) { if( pField->uiType == HB_FT_DATE ) { hb_itemGetDS( pItem, szBuffer ); memcpy( pArea->pRecord + pArea->pFieldOffset[ uiIndex ], szBuffer, 8 ); } else uiError = EDBF_DATATYPE; } else if( HB_IS_NUMBER( pItem ) ) { if( pField->uiType == HB_FT_LONG ) { if( hb_itemStrBuf( szBuffer, pItem, pField->uiLen, pField->uiDec ) ) { memcpy( pArea->pRecord + pArea->pFieldOffset[ uiIndex ], szBuffer, pField->uiLen ); } else { uiError = EDBF_DATAWIDTH; memset( pArea->pRecord + pArea->pFieldOffset[ uiIndex ], '*', pField->uiLen ); } } else uiError = EDBF_DATATYPE; } else if( HB_IS_LOGICAL( pItem ) ) { if( pField->uiType == HB_FT_LOGICAL ) pArea->pRecord[ pArea->pFieldOffset[ uiIndex ] ] = hb_itemGetL( pItem ) ? 'T' : 'F'; else uiError = EDBF_DATATYPE; } else uiError = EDBF_DATATYPE; } if( uiError != HB_SUCCESS ) { PHB_ITEM pError= hb_errNew(); USHORT uiGenCode = uiError == EDBF_DATAWIDTH ? EG_DATAWIDTH : EDBF_DATATYPE; hb_errPutGenCode( pError, uiGenCode ); hb_errPutDescription( pError, hb_langDGetErrorDesc( uiGenCode ) ); hb_errPutOperation( pError, hb_dynsymName( ( PHB_DYNS ) pField->sym ) ); hb_errPutSubCode( pError, uiError ); hb_errPutFlags( pError, EF_CANDEFAULT ); uiError = SELF_ERROR( ( AREAP ) pArea, pError ); hb_itemRelease( pError ); return uiError == E_DEFAULT ? HB_SUCCESS : HB_FAILURE; } return HB_SUCCESS; }
/* * Assign a value to a field. */ static HB_ERRCODE hb_sdfPutValue( SDFAREAP pArea, HB_USHORT uiIndex, PHB_ITEM pItem ) { char szBuffer[ 256 ]; HB_ERRCODE errCode; LPFIELD pField; HB_SIZE nSize; HB_TRACE( HB_TR_DEBUG, ( "hb_sdfPutValue(%p,%hu,%p)", pArea, uiIndex, pItem ) ); if( ! pArea->fPositioned ) return HB_SUCCESS; if( ! pArea->fRecordChanged ) return HB_FAILURE; if( --uiIndex >= pArea->area.uiFieldCount ) return HB_FAILURE; errCode = HB_SUCCESS; pField = pArea->area.lpFields + uiIndex; if( pField->uiType != HB_FT_MEMO && pField->uiType != HB_FT_NONE ) { if( HB_IS_MEMO( pItem ) || HB_IS_STRING( pItem ) ) { if( pField->uiType == HB_FT_STRING ) { if( ( pField->uiFlags & HB_FF_BINARY ) == 0 ) { nSize = pField->uiLen; hb_cdpnDup2( hb_itemGetCPtr( pItem ), hb_itemGetCLen( pItem ), ( char * ) pArea->pRecord + pArea->pFieldOffset[ uiIndex ], &nSize, hb_vmCDP(), pArea->area.cdPage ); } else { nSize = hb_itemGetCLen( pItem ); if( nSize > ( HB_SIZE ) pField->uiLen ) nSize = pField->uiLen; memcpy( pArea->pRecord + pArea->pFieldOffset[ uiIndex ], hb_itemGetCPtr( pItem ), nSize ); } if( nSize < ( HB_SIZE ) pField->uiLen ) memset( pArea->pRecord + pArea->pFieldOffset[ uiIndex ] + nSize, ' ', pField->uiLen - nSize ); } else errCode = EDBF_DATATYPE; } else if( HB_IS_DATETIME( pItem ) ) { if( pField->uiType == HB_FT_DATE ) { hb_itemGetDS( pItem, szBuffer ); memcpy( pArea->pRecord + pArea->pFieldOffset[ uiIndex ], szBuffer, 8 ); } else if( pField->uiType == HB_FT_TIMESTAMP && ( pField->uiLen == 12 || pField->uiLen == 23 ) ) { long lDate, lTime; hb_itemGetTDT( pItem, &lDate, &lTime ); if( pField->uiLen == 12 ) hb_timeStr( szBuffer, lTime ); else hb_timeStampStr( szBuffer, lDate, lTime ); memcpy( pArea->pRecord + pArea->pFieldOffset[ uiIndex ], szBuffer, pField->uiLen ); } else errCode = EDBF_DATATYPE; } else if( HB_IS_NUMBER( pItem ) ) { if( pField->uiType == HB_FT_LONG ) { if( hb_itemStrBuf( szBuffer, pItem, pField->uiLen, pField->uiDec ) ) { memcpy( pArea->pRecord + pArea->pFieldOffset[ uiIndex ], szBuffer, pField->uiLen ); } else { errCode = EDBF_DATAWIDTH; memset( pArea->pRecord + pArea->pFieldOffset[ uiIndex ], '*', pField->uiLen ); } } else errCode = EDBF_DATATYPE; } else if( HB_IS_LOGICAL( pItem ) ) { if( pField->uiType == HB_FT_LOGICAL ) pArea->pRecord[ pArea->pFieldOffset[ uiIndex ] ] = hb_itemGetL( pItem ) ? 'T' : 'F'; else errCode = EDBF_DATATYPE; } else errCode = EDBF_DATATYPE; } if( errCode != HB_SUCCESS ) { PHB_ITEM pError = hb_errNew(); HB_ERRCODE errGenCode = errCode == EDBF_DATAWIDTH ? EG_DATAWIDTH : EDBF_DATATYPE; hb_errPutGenCode( pError, errGenCode ); hb_errPutDescription( pError, hb_langDGetErrorDesc( errGenCode ) ); hb_errPutOperation( pError, hb_dynsymName( ( PHB_DYNS ) pField->sym ) ); hb_errPutSubCode( pError, errCode ); hb_errPutFlags( pError, EF_CANDEFAULT ); errCode = SELF_ERROR( &pArea->area, pError ); hb_itemRelease( pError ); return errCode == E_DEFAULT ? HB_SUCCESS : HB_FAILURE; } return HB_SUCCESS; }
static PHB_SOCKEX s_sockexNext( PHB_SOCKEX pSock, PHB_ITEM pParams ) { PHB_SOCKEX pSockNew = NULL; if( pSock ) { HB_BOOL fDecompressIn = HB_TRUE, fCompressOut = HB_TRUE; int level = HB_ZLIB_COMPRESSION_DEFAULT, strategy = HB_ZLIB_STRATEGY_DEFAULT, windowBitsIn = MAX_WBITS, windowBitsOut = MAX_WBITS; if( pParams && HB_IS_HASH( pParams ) ) { PHB_ITEM pItem; if( ( pItem = hb_hashGetCItemPtr( pParams, "zlib" ) ) != NULL && HB_IS_NUMERIC( pItem ) ) level = hb_itemGetNI( pItem ); if( ( pItem = hb_hashGetCItemPtr( pParams, "zs" ) ) != NULL && HB_IS_NUMERIC( pItem ) ) strategy = hb_itemGetNI( pItem ); if( ( pItem = hb_hashGetCItemPtr( pParams, "gzin" ) ) != NULL && HB_IS_LOGICAL( pItem ) ) { fDecompressIn = hb_itemGetL( pItem ); if( fDecompressIn ) windowBitsIn += 16; } if( ( pItem = hb_hashGetCItemPtr( pParams, "zin" ) ) != NULL && HB_IS_LOGICAL( pItem ) ) { if( windowBitsIn == MAX_WBITS ) fDecompressIn = hb_itemGetL( pItem ); else if( hb_itemGetL( pItem ) ) windowBitsIn += 16; } if( ( pItem = hb_hashGetCItemPtr( pParams, "gzout" ) ) != NULL && HB_IS_LOGICAL( pItem ) ) { fCompressOut = hb_itemGetL( pItem ); if( fCompressOut ) windowBitsOut += 16; } if( ( pItem = hb_hashGetCItemPtr( pParams, "zout" ) ) != NULL && HB_IS_LOGICAL( pItem ) && windowBitsOut == MAX_WBITS ) fCompressOut = hb_itemGetL( pItem ); } if( level != HB_ZLIB_COMPRESSION_DISABLE && ( fDecompressIn || fCompressOut ) ) { PHB_SOCKEX_Z pZ = ( PHB_SOCKEX_Z ) hb_xgrabz( sizeof( HB_SOCKEX_Z ) ); pSockNew = ( PHB_SOCKEX ) hb_xgrabz( sizeof( HB_SOCKEX ) ); pSockNew->sd = HB_NO_SOCKET; pSockNew->fRedirAll = HB_TRUE; pSockNew->pFilter = &s_sockFilter; pSockNew->cargo = ( void * ) pZ; pZ->z_read.zalloc = s_zsock_zalloc; pZ->z_read.zfree = s_zsock_zfree; pZ->z_read.opaque = Z_NULL; pZ->z_write.zalloc = s_zsock_zalloc; pZ->z_write.zfree = s_zsock_zfree; pZ->z_write.opaque = Z_NULL; pZ->z_read.next_in = NULL; pZ->z_read.avail_in = 0; if( level != Z_DEFAULT_COMPRESSION && !( level >= Z_NO_COMPRESSION && level <= Z_BEST_COMPRESSION ) ) level = Z_DEFAULT_COMPRESSION; if( strategy != Z_FILTERED && #if defined( Z_RLE ) strategy != Z_RLE && #endif #if defined( Z_FIXED ) strategy != Z_FIXED && #endif strategy != Z_HUFFMAN_ONLY ) strategy = Z_DEFAULT_STRATEGY; if( fDecompressIn && level != HB_ZLIB_COMPRESSION_DISABLE ) { /* MAX_WBITS=15, decompression - support for formats: * -15: raw, 15: ZLIB, 31: GZIP, 47: ZLIB+GZIP */ if( inflateInit2( &pZ->z_read, windowBitsIn ) == Z_OK ) { pZ->fDecompressIn = HB_TRUE; pZ->rdbuf = ( HB_BYTE * ) hb_xgrab( HB_ZSOCK_RDBUFSIZE ); } else level = HB_ZLIB_COMPRESSION_DISABLE; } if( fCompressOut && level != HB_ZLIB_COMPRESSION_DISABLE ) { /* MAX_WBITS=15, compression format: * -15: raw, 15: ZLIB (+6 bytes), 31: GZIP(+18 bytes) */ if( deflateInit2( &pZ->z_write, level, Z_DEFLATED, windowBitsOut, HB_ZSOCK_MEM_LEVEL, strategy ) == Z_OK ) { pZ->fCompressOut = HB_TRUE; pZ->wrbuf = ( HB_BYTE * ) hb_xgrab( HB_ZSOCK_WRBUFSIZE ); pZ->z_write.next_out = ( Bytef * ) pZ->wrbuf; pZ->z_write.avail_out = HB_ZSOCK_WRBUFSIZE; } else level = HB_ZLIB_COMPRESSION_DISABLE; } if( level != HB_ZLIB_COMPRESSION_DISABLE ) { pSockNew->sd = pSock->sd; pSockNew->fShutDown = pSock->fShutDown; pSockNew->iAutoFlush = pSock->iAutoFlush; pZ->sock = pSock; hb_socekxParamsInit( pSockNew, pParams ); } else { s_sockexClose( pSockNew, HB_FALSE ); pSockNew = NULL; } } } return pSockNew; }
static void _hb_jsonEncode( PHB_ITEM pValue, PHB_JSON_ENCODE_CTX pCtx, HB_SIZE nLevel, HB_BOOL fEOL ) { /* Protection against recursive structures */ if( ( HB_IS_ARRAY( pValue ) || HB_IS_HASH( pValue ) ) && hb_itemSize( pValue ) > 0 ) { void * id = HB_IS_HASH( pValue ) ? hb_hashId( pValue ) : hb_arrayId( pValue ); HB_SIZE nIndex; for( nIndex = 0; nIndex < nLevel; nIndex++ ) { if( pCtx->pId[ nIndex ] == id ) { if( ! fEOL && pCtx->fHuman ) _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); _hb_jsonCtxAdd( pCtx, "null", 4 ); return; } } if( nLevel >= pCtx->nAllocId ) { pCtx->nAllocId += 8; pCtx->pId = ( void ** ) hb_xrealloc( pCtx->pId, sizeof( void * ) * pCtx->nAllocId ); } pCtx->pId[ nLevel ] = id; } if( fEOL ) { --pCtx->pHead; _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); } if( HB_IS_STRING( pValue ) ) { const char * szString = hb_itemGetCPtr( pValue ); HB_SIZE nPos, nPos2, nLen = hb_itemGetCLen( pValue ); _hb_jsonCtxAdd( pCtx, "\"", 1 ); nPos = 0; while( nPos < nLen ) { nPos2 = nPos; while( *( ( const unsigned char * ) szString + nPos2 ) >= ' ' && szString[ nPos2 ] != '\\' && szString[ nPos2 ] != '\"' ) nPos2++; if( nPos2 > nPos ) { _hb_jsonCtxAdd( pCtx, szString + nPos, nPos2 - nPos ); nPos = nPos2; continue; } switch( szString[ nPos ] ) { case '\\': _hb_jsonCtxAdd( pCtx, "\\\\", 2 ); break; case '\"': _hb_jsonCtxAdd( pCtx, "\\\"", 2 ); break; case '\b': _hb_jsonCtxAdd( pCtx, "\\b", 2 ); break; case '\f': _hb_jsonCtxAdd( pCtx, "\\f", 2 ); break; case '\n': _hb_jsonCtxAdd( pCtx, "\\n", 2 ); break; case '\r': _hb_jsonCtxAdd( pCtx, "\\r", 2 ); break; case '\t': _hb_jsonCtxAdd( pCtx, "\\t", 2 ); break; default: { char buf[ 8 ]; hb_snprintf( buf, sizeof( buf ), "\\u00%02X", ( unsigned char ) szString[ nPos ] ); _hb_jsonCtxAdd( pCtx, buf, 6 ); break; } } nPos++; } _hb_jsonCtxAdd( pCtx, "\"", 1 ); } else if( HB_IS_NUMINT( pValue ) ) { char buf[ 32 ]; hb_snprintf( buf, sizeof( buf ), "%" PFHL "d", hb_itemGetNInt( pValue ) ); _hb_jsonCtxAdd( pCtx, buf, strlen( buf ) ); } else if( HB_IS_NUMERIC( pValue ) ) { char buf[ 64 ]; int iDec; double dblValue = hb_itemGetNDDec( pValue, &iDec ); hb_snprintf( buf, sizeof( buf ), "%.*f", iDec, dblValue ); _hb_jsonCtxAdd( pCtx, buf, strlen( buf ) ); } else if( HB_IS_NIL( pValue ) ) { _hb_jsonCtxAdd( pCtx, "null", 4 ); } else if( HB_IS_LOGICAL( pValue ) ) { if( hb_itemGetL( pValue ) ) _hb_jsonCtxAdd( pCtx, "true", 4 ); else _hb_jsonCtxAdd( pCtx, "false", 5 ); } else if( HB_IS_DATE( pValue ) ) { char szBuffer[ 10 ]; hb_itemGetDS( pValue, szBuffer + 1 ); szBuffer[ 0 ] = '\"'; szBuffer[ 9 ] = '\"'; _hb_jsonCtxAdd( pCtx, szBuffer, 10 ); } else if( HB_IS_TIMESTAMP( pValue ) ) { char szBuffer[ 19 ]; hb_itemGetTS( pValue, szBuffer + 1 ); szBuffer[ 0 ] = '\"'; szBuffer[ 18 ] = '\"'; _hb_jsonCtxAdd( pCtx, szBuffer, 19 ); } else if( HB_IS_ARRAY( pValue ) ) { HB_SIZE nLen = hb_itemSize( pValue ); if( nLen ) { HB_SIZE nIndex; if( pCtx->fHuman ) _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); _hb_jsonCtxAdd( pCtx, "[", 1 ); for( nIndex = 1; nIndex <= nLen; nIndex++ ) { PHB_ITEM pItem = hb_arrayGetItemPtr( pValue, nIndex ); if( nIndex > 1 ) _hb_jsonCtxAdd( pCtx, ",", 1 ); if( pCtx->fHuman ) _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); if( pCtx->fHuman && ! ( ( HB_IS_ARRAY( pItem ) || HB_IS_HASH( pItem ) ) && hb_itemSize( pItem ) > 0 ) ) _hb_jsonCtxAddIndent( pCtx, ( nLevel + 1 ) * INDENT_SIZE ); _hb_jsonEncode( pItem, pCtx, nLevel + 1, HB_FALSE ); } if( pCtx->fHuman ) { _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); } _hb_jsonCtxAdd( pCtx, "]", 1 ); } else _hb_jsonCtxAdd( pCtx, "[]", 2 ); } else if( HB_IS_HASH( pValue ) ) { HB_SIZE nLen = hb_hashLen( pValue ); if( nLen ) { HB_SIZE nIndex; if( pCtx->fHuman ) _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); _hb_jsonCtxAdd( pCtx, "{", 1 ); for( nIndex = 1; nIndex <= nLen; nIndex++ ) { PHB_ITEM pKey = hb_hashGetKeyAt( pValue, nIndex ); if( HB_IS_STRING( pKey ) ) { PHB_ITEM pItem = hb_hashGetValueAt( pValue, nIndex ); HB_BOOL fEOL = HB_FALSE; if( nIndex > 1 ) _hb_jsonCtxAdd( pCtx, ",", 1 ); if( pCtx->fHuman ) { _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); _hb_jsonCtxAddIndent( pCtx, ( nLevel + 1 ) * INDENT_SIZE ); } _hb_jsonEncode( pKey, pCtx, nLevel + 1, HB_FALSE ); if( pCtx->fHuman ) { _hb_jsonCtxAdd( pCtx, ": ", 2 ); fEOL = ( HB_IS_ARRAY( pItem ) || HB_IS_HASH( pItem ) ) && hb_itemSize( pItem ) > 0; } else _hb_jsonCtxAdd( pCtx, ":", 1 ); _hb_jsonEncode( pItem, pCtx, nLevel + 1, fEOL ); } } if( pCtx->fHuman ) { _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); } _hb_jsonCtxAdd( pCtx, "}", 1 ); } else _hb_jsonCtxAdd( pCtx, "{}", 2 ); } else { /* All unsupported types are replacd by null */ _hb_jsonCtxAdd( pCtx, "null", 4 ); } }
static void _hb_jsonEncode( PHB_ITEM pValue, PHB_JSON_ENCODE_CTX pCtx, HB_SIZE nLevel, HB_BOOL fEOL, PHB_CODEPAGE cdp ) { /* Protection against recursive structures */ if( ( HB_IS_ARRAY( pValue ) || HB_IS_HASH( pValue ) ) && hb_itemSize( pValue ) > 0 ) { void * id = HB_IS_HASH( pValue ) ? hb_hashId( pValue ) : hb_arrayId( pValue ); HB_SIZE nIndex; for( nIndex = 0; nIndex < nLevel; nIndex++ ) { if( pCtx->pId[ nIndex ] == id ) { if( ! fEOL && pCtx->fHuman ) _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); _hb_jsonCtxAdd( pCtx, "null", 4 ); return; } } if( nLevel >= pCtx->nAllocId ) { pCtx->nAllocId += 8; pCtx->pId = ( void ** ) hb_xrealloc( pCtx->pId, sizeof( void * ) * pCtx->nAllocId ); } pCtx->pId[ nLevel ] = id; } if( fEOL ) { --pCtx->pHead; _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); } if( HB_IS_STRING( pValue ) ) { HB_SIZE nPos, nLen = hb_itemGetCLen( pValue ); const char * szString = hb_itemGetCPtr( pValue ); char buf[ 8 ]; _hb_jsonCtxAdd( pCtx, "\"", 1 ); if( cdp ) { HB_WCHAR wc; nPos = 0; while( HB_CDPCHAR_GET( cdp, szString, nLen, &nPos, &wc ) ) { if( wc >= ' ' && wc < 0x7F && wc != '\\' && wc != '\"' ) { buf[ 0 ] = ( char ) wc; _hb_jsonCtxAdd( pCtx, buf, 1 ); continue; } switch( wc ) { case '\\': _hb_jsonCtxAdd( pCtx, "\\\\", 2 ); break; case '\"': _hb_jsonCtxAdd( pCtx, "\\\"", 2 ); break; case '\b': _hb_jsonCtxAdd( pCtx, "\\b", 2 ); break; case '\f': _hb_jsonCtxAdd( pCtx, "\\f", 2 ); break; case '\n': _hb_jsonCtxAdd( pCtx, "\\n", 2 ); break; case '\r': _hb_jsonCtxAdd( pCtx, "\\r", 2 ); break; case '\t': _hb_jsonCtxAdd( pCtx, "\\t", 2 ); break; default: hb_snprintf( buf, sizeof( buf ), "\\u%04X", wc ); _hb_jsonCtxAdd( pCtx, buf, 6 ); break; } } } else { nPos = 0; while( nPos < nLen ) { unsigned char uch = szString[ nPos ]; HB_SIZE nPos2 = nPos; while( uch >= ' ' && uch != '\\' && uch != '\"' ) uch = szString[ ++nPos2 ]; if( nPos2 > nPos ) { _hb_jsonCtxAdd( pCtx, szString + nPos, nPos2 - nPos ); if( nPos2 >= nLen ) break; nPos = nPos2; } switch( uch ) { case '\\': _hb_jsonCtxAdd( pCtx, "\\\\", 2 ); break; case '\"': _hb_jsonCtxAdd( pCtx, "\\\"", 2 ); break; case '\b': _hb_jsonCtxAdd( pCtx, "\\b", 2 ); break; case '\f': _hb_jsonCtxAdd( pCtx, "\\f", 2 ); break; case '\n': _hb_jsonCtxAdd( pCtx, "\\n", 2 ); break; case '\r': _hb_jsonCtxAdd( pCtx, "\\r", 2 ); break; case '\t': _hb_jsonCtxAdd( pCtx, "\\t", 2 ); break; default: hb_snprintf( buf, sizeof( buf ), "\\u00%02X", uch ); _hb_jsonCtxAdd( pCtx, buf, 6 ); break; } nPos++; } } _hb_jsonCtxAdd( pCtx, "\"", 1 ); } else if( HB_IS_NUMINT( pValue ) ) { char buf[ 24 ]; HB_MAXINT nVal = hb_itemGetNInt( pValue ); HB_BOOL fNeg = nVal < 0; int i = 0; if( fNeg ) nVal = -nVal; do buf[ sizeof( buf ) - ++i ] = ( nVal % 10 ) + '0'; while( ( nVal /= 10 ) != 0 ); if( fNeg ) buf[ sizeof( buf ) - ++i ] = '-'; _hb_jsonCtxAdd( pCtx, &buf[ sizeof( buf ) - i ], i ); } else if( HB_IS_NUMERIC( pValue ) ) { char buf[ 64 ]; int iDec; double dblValue = hb_itemGetNDDec( pValue, &iDec ); hb_snprintf( buf, sizeof( buf ), "%.*f", iDec, dblValue ); _hb_jsonCtxAdd( pCtx, buf, strlen( buf ) ); } else if( HB_IS_NIL( pValue ) ) { _hb_jsonCtxAdd( pCtx, "null", 4 ); } else if( HB_IS_LOGICAL( pValue ) ) { if( hb_itemGetL( pValue ) ) _hb_jsonCtxAdd( pCtx, "true", 4 ); else _hb_jsonCtxAdd( pCtx, "false", 5 ); } else if( HB_IS_DATE( pValue ) ) { char szBuffer[ 10 ]; hb_itemGetDS( pValue, szBuffer + 1 ); szBuffer[ 0 ] = '\"'; szBuffer[ 9 ] = '\"'; _hb_jsonCtxAdd( pCtx, szBuffer, 10 ); } else if( HB_IS_TIMESTAMP( pValue ) ) { char szBuffer[ 19 ]; hb_itemGetTS( pValue, szBuffer + 1 ); szBuffer[ 0 ] = '\"'; szBuffer[ 18 ] = '\"'; _hb_jsonCtxAdd( pCtx, szBuffer, 19 ); } else if( HB_IS_ARRAY( pValue ) ) { HB_SIZE nLen = hb_itemSize( pValue ); if( nLen ) { HB_SIZE nIndex; if( pCtx->fHuman ) _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); _hb_jsonCtxAdd( pCtx, "[", 1 ); for( nIndex = 1; nIndex <= nLen; nIndex++ ) { PHB_ITEM pItem = hb_arrayGetItemPtr( pValue, nIndex ); if( nIndex > 1 ) _hb_jsonCtxAdd( pCtx, ",", 1 ); if( pCtx->fHuman ) _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); if( pCtx->fHuman && ! ( ( HB_IS_ARRAY( pItem ) || HB_IS_HASH( pItem ) ) && hb_itemSize( pItem ) > 0 ) ) _hb_jsonCtxAddIndent( pCtx, ( nLevel + 1 ) * INDENT_SIZE ); _hb_jsonEncode( pItem, pCtx, nLevel + 1, HB_FALSE, cdp ); } if( pCtx->fHuman ) { _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); } _hb_jsonCtxAdd( pCtx, "]", 1 ); } else _hb_jsonCtxAdd( pCtx, "[]", 2 ); } else if( HB_IS_HASH( pValue ) ) { HB_SIZE nLen = hb_hashLen( pValue ); if( nLen ) { HB_SIZE nIndex; if( pCtx->fHuman ) _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); _hb_jsonCtxAdd( pCtx, "{", 1 ); for( nIndex = 1; nIndex <= nLen; nIndex++ ) { PHB_ITEM pKey = hb_hashGetKeyAt( pValue, nIndex ); if( HB_IS_STRING( pKey ) ) { PHB_ITEM pItem = hb_hashGetValueAt( pValue, nIndex ); if( nIndex > 1 ) _hb_jsonCtxAdd( pCtx, ",", 1 ); if( pCtx->fHuman ) { _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); _hb_jsonCtxAddIndent( pCtx, ( nLevel + 1 ) * INDENT_SIZE ); } _hb_jsonEncode( pKey, pCtx, nLevel + 1, HB_FALSE, cdp ); if( pCtx->fHuman ) { _hb_jsonCtxAdd( pCtx, ": ", 2 ); fEOL = ( HB_IS_ARRAY( pItem ) || HB_IS_HASH( pItem ) ) && hb_itemSize( pItem ) > 0; } else { _hb_jsonCtxAdd( pCtx, ":", 1 ); fEOL = HB_FALSE; } _hb_jsonEncode( pItem, pCtx, nLevel + 1, fEOL, cdp ); } } if( pCtx->fHuman ) { _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); } _hb_jsonCtxAdd( pCtx, "}", 1 ); } else _hb_jsonCtxAdd( pCtx, "{}", 2 ); } else { /* All unsupported types are replacd by null */ _hb_jsonCtxAdd( pCtx, "null", 4 ); } }