Ejemplo n.º 1
0
static HB_BOOL hb_errGetNumCode( int * piValue, const char * szOperation )
{
   PHB_ITEM pItem = hb_param( 1, HB_IT_NUMERIC );

   if( pItem )
      *piValue = hb_itemGetNI( pItem );
   else
   {
      pItem = hb_errRT_BASE_Subst( EG_ARG, 0, NULL, szOperation,
                                   HB_ERR_ARGS_BASEPARAMS );
      if( ! pItem )
      {
         *piValue = 0;
         return HB_FALSE;
      }

      if( ! HB_IS_NUMERIC( pItem ) )
         hb_errInternal( HB_EI_ERRRECFAILURE, NULL, NULL, NULL );

      *piValue = hb_itemGetNI( pItem );
      hb_itemRelease( pItem );
   }

   return HB_TRUE;
}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
0
static HB_BOOL s_fileConfigure( PHB_FILE pFile, int iIndex, PHB_ITEM pValue )
{
   switch( iIndex )
   {
      case HB_VF_TIMEOUT:
      {
         HB_MAXINT timeout = pFile->timeout;

         if( HB_IS_NUMERIC( pValue ) )
            pFile->timeout = hb_itemGetNInt( pValue );
         hb_itemPutNInt( pValue, timeout );
         return HB_TRUE;
      }
      case HB_VF_SHUTDOWN:
      {
         HB_SOCKET sd = hb_sockexGetHandle( pFile->sock );

         if( HB_IS_NUMERIC( pValue ) && sd != HB_NO_SOCKET )
         {
            switch( hb_itemGetNI( pValue ) )
            {
               case FO_READ:
                  hb_socketShutdown( sd, HB_SOCKET_SHUT_RD );
                  break;
               case FO_WRITE:
                  hb_socketShutdown( sd, HB_SOCKET_SHUT_WR );
                  break;
               case FO_READWRITE:
                  hb_socketShutdown( sd, HB_SOCKET_SHUT_RDWR );
                  break;
            }
         }
         hb_itemClear( pValue );
         return HB_TRUE;
      }
      case HB_VF_RDHANDLE:
      case HB_VF_WRHANDLE:
         hb_itemPutNInt( pValue, ( HB_NHANDLE ) hb_sockexGetHandle( pFile->sock ) );
         return HB_TRUE;
   }

   return HB_FALSE;
}
Ejemplo n.º 4
0
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 );
   }
}
Ejemplo n.º 5
0
/*
 * Position the cursor to a specific, physical identity.
 */
static HB_ERRCODE hb_sdfGoToId( SDFAREAP pArea, PHB_ITEM pItem )
{
   HB_TRACE( HB_TR_DEBUG, ( "hb_sdfGoToId(%p, %p)", pArea, pItem ) );

#ifndef HB_CLP_STRICT
   if( HB_IS_NUMERIC( pItem ) )
      return SELF_GOTO( &pArea->area, hb_itemGetNL( pItem ) );
#endif
   /* generate RTE */
   return SUPER_GOTOID( &pArea->area, pItem );
}
Ejemplo n.º 6
0
void hb_socekxParamsGetStd( PHB_ITEM pParams,
                            const void ** pKeydata, int * pKeylen,
                            const void ** pIV, int * pIVlen,
                            int * pLevel, int * pStrategy )
{
   if( pParams && HB_IS_HASH( pParams ) )
   {
      PHB_ITEM pItem;

      if( pKeydata && pKeylen &&
          ( pItem = hb_hashGetCItemPtr( pParams, "key" ) ) != NULL &&
          HB_IS_STRING( pItem ) )
      {
         *pKeydata = hb_itemGetCPtr( pItem );
         *pKeylen  = ( int ) hb_itemGetCLen( pItem );
      }
      else if( pKeydata && pKeylen &&
               ( pItem = hb_hashGetCItemPtr( pParams, "pass" ) ) != NULL &&
               HB_IS_STRING( pItem ) )
      {
         *pKeydata = hb_itemGetCPtr( pItem );
         *pKeylen  = ( int ) hb_itemGetCLen( pItem );
      }
      if( pIV && pIVlen &&
          ( pItem = hb_hashGetCItemPtr( pParams, "iv" ) ) != NULL &&
          HB_IS_STRING( pItem ) )
      {
         *pIV    = hb_itemGetCPtr( pItem );
         *pIVlen = ( int ) hb_itemGetCLen( pItem );
      }
      if( pLevel &&
          ( pItem = hb_hashGetCItemPtr( pParams, "zlib" ) ) != NULL &&
          HB_IS_NUMERIC( pItem ) )
         *pLevel = hb_itemGetNI( pItem );
      if( pStrategy &&
          ( pItem = hb_hashGetCItemPtr( pParams, "zs" ) ) != NULL &&
          HB_IS_NUMERIC( pItem ) )
         *pStrategy = hb_itemGetNI( pItem );
   }
}
Ejemplo n.º 7
0
static HB_BOOL s_hashWithNumKeys( PHB_ITEM pHash )
{
   HB_SIZE nLen = hb_hashLen( pHash ), n;

   for( n = 1; n <= nLen; ++n )
   {
      PHB_ITEM pKey = hb_hashGetKeyAt( pHash, n );
      if( ! pKey || ! HB_IS_NUMERIC( pKey ) )
         return HB_FALSE;
   }

   return HB_TRUE;
}
Ejemplo n.º 8
0
/* For C Calls 2003.05.08 */
WORD _sx_select( PHB_ITEM vParam )
{
   WORD iSelected = SX_DUMMY_NUMBER;

   if( HB_IS_NUMERIC( vParam ) )
      iSelected = sx_Select( ( WORD ) HB_GETNI( vParam ) );
   else if( HB_IS_STRING( vParam ) )
      iSelected = sx_Select( sx_WorkArea( ( PBYTE ) HB_GETC( vParam ) ) );

   if( iSelected == SX_DUMMY_NUMBER )
      hb_errRT_DBCMD( EG_ARG, EDBCMD_NOALIAS, NULL, "_sx_select" );

   return iSelected;
}
Ejemplo n.º 9
0
static HB_ERRCODE sqlbaseGoToId( SQLBASEAREAP pArea, PHB_ITEM pItem )
{
   PHB_ITEM pError;

   if( HB_IS_NUMERIC( pItem ) )
      return SELF_GOTO( ( AREAP ) pArea, hb_itemGetNL( pItem ) );
   else
   {
      pError = hb_errNew();
      hb_errPutGenCode( pError, EG_DATATYPE );
      hb_errPutDescription( pError, hb_langDGetErrorDesc( EG_DATATYPE ) );
      hb_errPutSubCode( pError, EDBF_DATATYPE );
      SELF_ERROR( ( AREAP ) pArea, pError );
      hb_itemRelease( pError );
      return HB_FAILURE;
   }
}
Ejemplo n.º 10
0
/*
 * Retorna o valor LONG de uma variavel numérica do xHarbour, criada via .PRG
 * 01/07/2008 - 08:40:47
 */
LONG wxMemvarGetNL( char * szVarName )
{
   HB_THREAD_STUB   
   HB_DYNS_PTR pDynVar;
   LONG result = 0;

   HB_TRACE(HB_TR_DEBUG, ("hb_memvarGetStrValuePtr(%s, %p)", szVarName, pulLen));

   #ifdef HB_THREAD_SUPPORT
      pDynVar = s_memvarThFindName( szVarName, &HB_VM_STACK );
   #else
      pDynVar = hb_dynsymFindName( szVarName );
   #endif

   if( pDynVar )
   {
      /* there is dynamic symbol with the requested name - check if it is
       * a memvar variable
       */
      if( pDynVar->hMemvar )
      {
         /* variable contains some data
          */
         HB_ITEM_PTR pItem = hb_memvarGetValueByHandle( pDynVar->hMemvar );

         if( HB_IS_BYREF( pItem ) )
         {
            pItem = hb_itemUnRef( pItem );   /* it is a PARAMETER variable */
         }

         if( HB_IS_NUMERIC( pItem ) )
         {
            result = hb_itemGetNL( pItem );
         }
      }
   }
   return result;
}
Ejemplo n.º 11
0
Archivo: adsx.c Proyecto: SBCamus/core
static HB_ERRCODE adsxOrderInfo( ADSXAREAP pArea, HB_USHORT uiIndex, LPDBORDERINFO pOrderInfo )
{
   PMIXTAG pTag = pArea->pTagCurrent;

   /* resolve any pending relations */
   if( pArea->adsarea.lpdbPendingRel )
      SELF_FORCEREL( ( AREAP ) pArea );

   /* all others need an index handle */
   if( uiIndex != DBOI_ORDERCOUNT )
   {
      if( pOrderInfo->itmOrder )
      {
         if( HB_IS_STRING( pOrderInfo->itmOrder ) )
         {
            pTag = pArea->pTagList;
            while( pTag )
            {
               if( ! hb_stricmp( hb_itemGetCPtr( pOrderInfo->itmOrder ), pTag->szName ) )
                  break;

               pTag = pTag->pNext;
            }
         }
         else if( HB_IS_NUMERIC( pOrderInfo->itmOrder ) )
         {
            UNSIGNED16 usOrder = 0, usSearch = ( UNSIGNED16 ) hb_itemGetNI( pOrderInfo->itmOrder );

            AdsGetNumIndexes( pArea->adsarea.hTable, &usOrder );

            pTag = usSearch <= usOrder ? NULL : pArea->pTagList;
            while( pTag )
            {
               if( ++usOrder == usSearch )
                  break;

               pTag = pTag->pNext;
            }
         }
      }

      if( ! pTag )
         return SUPER_ORDINFO( ( AREAP ) pArea, uiIndex, pOrderInfo );
   }

   switch( uiIndex )
   {
      case DBOI_CONDITION:
         hb_itemPutC( pOrderInfo->itmResult, pTag->szForExpr );
         break;

      case DBOI_EXPRESSION:
         hb_itemPutC( pOrderInfo->itmResult, pTag->szKeyExpr );
         break;

      case DBOI_ISCOND:
         hb_itemPutL( pOrderInfo->itmResult, pTag->pForItem != NULL );
         break;

      case DBOI_ISDESC:
         hb_itemPutL( pOrderInfo->itmResult, HB_FALSE );
         break;

      case DBOI_UNIQUE:
         hb_itemPutL( pOrderInfo->itmResult, HB_FALSE );
         break;

      case DBOI_KEYTYPE:
         hb_itemPutCL( pOrderInfo->itmResult, ( char * ) &pTag->bType, 1 );
         break;

      case DBOI_KEYSIZE:
         hb_itemPutNI( pOrderInfo->itmResult, pTag->uiLen );
         break;

      case DBOI_KEYVAL:
      {
         PHB_ITEM pItem;
         PHB_CODEPAGE pCodepage = hb_cdpSelect( pArea->adsarea.area.cdPage );

         pItem = hb_vmEvalBlockOrMacro( pTag->pKeyItem );
         hb_cdpSelect( pCodepage );
         hb_itemMove( pOrderInfo->itmResult, pItem );
         break;
      }
      case DBOI_KEYCOUNT:
      case DBOI_KEYCOUNTRAW:            /* ignore filter but RESPECT SCOPE */
         hb_itemPutNL( pOrderInfo->itmResult, pTag->ulRecCount );
         break;

      case DBOI_POSITION:
      case DBOI_RECNO:
      case DBOI_KEYNORAW:
         if( uiIndex == DBOI_POSITION && pOrderInfo->itmNewVal && HB_IS_NUMERIC( pOrderInfo->itmNewVal ) )
         {
            HB_ULONG ulPos;

            ulPos = hb_itemGetNL( pOrderInfo->itmNewVal );

            if( ulPos > 0 && ulPos <= pTag->ulRecCount )
               SELF_GOTO( ( AREAP ) pArea, pTag->pKeys[ ulPos - 1 ]->rec );

            pOrderInfo->itmResult = hb_itemPutL( pOrderInfo->itmResult, ! pArea->adsarea.area.fEof );
         }
         else
         {
            PMIXKEY  pKey;
            HB_ULONG ulKeyPos;

            if( ! pArea->adsarea.fPositioned )
               SELF_GOTO( ( AREAP ) pArea, pArea->adsarea.ulRecNo );
            else
               pArea->adsarea.area.fEof = HB_FALSE;

            pKey = mixKeyEval( pTag, pArea );

            hb_itemPutNL( pOrderInfo->itmResult,
                          mixFindKey( pTag, pKey, &ulKeyPos ) ? ( ulKeyPos + 1 ) : 0 );
            mixKeyFree( pKey );
         }
         break;

      case DBOI_RELKEYPOS:
         if( pOrderInfo->itmNewVal && HB_IS_NUMERIC( pOrderInfo->itmNewVal ) )
         {
            HB_ULONG ulPos;

            ulPos = ( HB_ULONG ) ( hb_itemGetND( pOrderInfo->itmNewVal ) * ( double ) pTag->ulRecCount );

            if( ulPos > 0 && ulPos <= pTag->ulRecCount )
               SELF_GOTO( ( AREAP ) pArea, pTag->pKeys[ ulPos - 1 ]->rec );

            pOrderInfo->itmResult = hb_itemPutL( pOrderInfo->itmResult, ! pArea->adsarea.area.fEof );
         }
         else
         {
            PMIXKEY  pKey;
            HB_ULONG ulKeyPos;

            if( ! pArea->adsarea.fPositioned )
               SELF_GOTO( ( AREAP ) pArea, pArea->adsarea.ulRecNo );
            else
               pArea->adsarea.area.fEof = HB_FALSE;

            pKey = mixKeyEval( pTag, pArea );

            if( ! mixFindKey( pTag, pKey, &ulKeyPos + 1 ) )
               ulKeyPos = 0;

            mixKeyFree( pKey );

            pOrderInfo->itmResult = hb_itemPutND( pOrderInfo->itmResult,
                                                  ( double ) ulKeyPos / ( double ) pTag->ulRecCount );
         }
         break;

      case DBOI_NAME:
         hb_itemPutC( pOrderInfo->itmResult, pTag->szName );
         break;

      case DBOI_BAGNAME:
         hb_itemPutC( pOrderInfo->itmResult, NULL );
         break;

      case DBOI_FULLPATH:
         hb_itemPutC( pOrderInfo->itmResult, NULL );
         break;

      case DBOI_BAGEXT:
         hb_itemPutC( pOrderInfo->itmResult, "mix" );
         break;

      case DBOI_ORDERCOUNT:
      {
         UNSIGNED16 usOrder = 0;

         AdsGetNumIndexes( pArea->adsarea.hTable, &usOrder );
         pTag = pArea->pTagList;
         while( pTag )
         {
            pTag = pTag->pNext;
            usOrder++;
         }
         hb_itemPutNI( pOrderInfo->itmResult, ( int ) usOrder );
         break;
      }

      case DBOI_NUMBER:
      {
         PMIXTAG    pTag2;
         UNSIGNED16 usOrder = 0;

         AdsGetNumIndexes( pArea->adsarea.hTable, &usOrder );
         pTag2 = pArea->pTagList;
         usOrder++;
         while( pTag2 && pTag != pTag2 )
         {
            pTag2 = pTag2->pNext;
            usOrder++;
         }
         hb_itemPutNI( pOrderInfo->itmResult, ( int ) usOrder );
         break;
      }

      case DBOI_CUSTOM:
         hb_itemPutL( pOrderInfo->itmResult, HB_FALSE );
         break;

      case DBOI_OPTLEVEL:
         hb_itemPutNI( pOrderInfo->itmResult, DBOI_OPTIMIZED_NONE );
         break;

      case DBOI_KEYADD:
         hb_itemPutL( pOrderInfo->itmResult, HB_FALSE );
         break;

      case DBOI_KEYDELETE:
         hb_itemPutL( pOrderInfo->itmResult, HB_FALSE );
         break;

      case DBOI_AUTOOPEN:
         hb_itemPutL( pOrderInfo->itmResult, HB_FALSE );
         break;

      default:
         return SUPER_ORDINFO( ( AREAP ) pArea, uiIndex, pOrderInfo );
   }
   return HB_SUCCESS;
}
Ejemplo n.º 12
0
Archivo: math.c Proyecto: emazv72/core
/* Harbour default math error handling routine */
static int hb_matherr( HB_MATH_EXCEPTION * pexc )
{
   int mode = hb_mathGetErrMode();
   int iRet = 1;

   HB_TRACE( HB_TR_DEBUG, ( "hb_matherr(%p)", ( void * ) pexc ) );

   if( pexc == NULL || pexc->handled != 0 )
   {
      /* error already handled by other handlers ! */
      return 1;
   }

   if( mode == HB_MATH_ERRMODE_USER || mode == HB_MATH_ERRMODE_USERDEFAULT ||
       mode == HB_MATH_ERRMODE_USERCDEFAULT )
   {
      PHB_ITEM pArg1, pArg2, pError;
      PHB_ITEM pMatherrResult;

      /* create an error object */
      /* NOTE: In case of HB_MATH_ERRMODE_USER[C]DEFAULT, I am setting both
               EF_CANSUBSTITUTE and EF_CANDEFAULT to .T. here.
               This is forbidden according to the original Cl*pper docs, but
               I think this reflects the situation best here:
               The error handler can either substitute the erroneous value
               (by returning a numeric value) or choose the default error
               handling (by returning .F., as usual) [martin vogel] */
      pError = hb_errRT_New_Subst( ES_ERROR, "MATH", EG_NUMERR, pexc->type,
                                   pexc->error, pexc->funcname, 0, EF_CANSUBSTITUTE |
                                   ( mode == HB_MATH_ERRMODE_USER ? 0 : EF_CANDEFAULT ) );

      /* Assign the new array to the object data item. */
      /* NOTE: Unfortunately, we cannot decide whether one or two parameters
               have been used when the math function has been called, so we
               always take two */
      pArg1 = hb_itemPutND( NULL, pexc->arg1 );
      pArg2 = hb_itemPutND( NULL, pexc->arg2 );
      hb_errPutArgs( pError, 2, pArg1, pArg2 );
      hb_itemRelease( pArg1 );
      hb_itemRelease( pArg2 );

      /* launch error codeblock */
      pMatherrResult = hb_errLaunchSubst( pError );
      hb_errRelease( pError );

      if( pMatherrResult )
      {
         if( HB_IS_NUMERIC( pMatherrResult ) )
         {
            pexc->retval = hb_itemGetND( pMatherrResult );
            hb_itemGetNLen( pMatherrResult, &pexc->retvalwidth, &pexc->retvaldec );
            pexc->handled = 1;
         }
         hb_itemRelease( pMatherrResult );
      }
   }

   /* math exception not handled by Harbour error routine above ? */
   if( pexc->handled == 0 )
   {
      switch( mode )
      {
         case HB_MATH_ERRMODE_USER:
            /* user failed to handle the math exception, so quit the app
               [yes, that's the meaning of this mode !!] */
            iRet = 0;
            hb_vmRequestQuit();
            break;

         case HB_MATH_ERRMODE_DEFAULT:
         case HB_MATH_ERRMODE_USERDEFAULT:
            /* return 1 to suppress C RTL error msgs, but leave error
               handling to the calling Harbour routine */
            break;

         case HB_MATH_ERRMODE_CDEFAULT:
         case HB_MATH_ERRMODE_USERCDEFAULT:
            /* use the correction value supplied in pexc->retval */
            pexc->handled = 1;
            break;
      }
   }

   return iRet;  /* error handling successful */
}
Ejemplo n.º 13
0
// 26/07/2008 - 11:32:47
static
void printf_add_conv( PPrintInfo pInfo, PHB_ITEM pItem )
{
    char flag = 0;
    int width = 0;
    int precision = 0;
    char specifier = '\0';
    char m = '\0';
    char c;
    char mask[20];
    const char *t1;
    const char *t2;

    char * cValue;
    double dValue;
    ULONG  Length;

    /*
     * Procura por uma string de conversao com o formato:
     *        0       1        2         3      4
     *     %[flags][width][.precision][length]specifier
     *
     */
    t1 = pInfo->Mask;
    mask[0] = '\0';

    pInfo->Mask ++;
    pInfo->msk_len--;

    /*
     * Se nao tem nenhum item de argumento, e o proximo caracter de escape nao for
     * o "%", entao temos que abortar a rotina... indicando erro de argumento!
     */
    if (pInfo->msk_len &&
            // pInfo->Mask[0] != '%' &&
            !pItem)
    {
        pInfo->error =  FAILURE;
        return;
    }

    /* processamos a string como um todo */
    while (pInfo->error == NONE   &&
            pInfo->msk_len         &&
            pInfo->Mask[0] )
    {
        c = pInfo->Mask[0];

        // setou o flag?
        if (strchr( "-+ #0", c ))
        {
            flag = c;
            c = 'A';
        }

        // setar o width?
        if (strchr( "1234567890", c ))
        {
            char *t = (char *) hb_xgrab(15);
            char *s = t;

            do
            {
                *t = c;
                t++;
                pInfo->Mask ++;
                pInfo->msk_len--;
                c = pInfo->Mask[0];

            } while (pInfo->msk_len && strchr( "1234567890", c ));

            *t = '\0';
            width = atoi( s );
            hb_xfree(s);
        }

        // setar precision?
        if (c == '.')
        {
            char *t = (char *) hb_xgrab(15);
            char *s = t;

            pInfo->Mask ++;
            pInfo->msk_len--;
            c = pInfo->Mask[0];

            while (pInfo->msk_len && strchr( "1234567890", c ))
            {
                *t = c;
                t++;
                pInfo->Mask ++;
                pInfo->msk_len--;
                c = pInfo->Mask[0];
            }

            *t = '\0';
            precision = atoi( s );
            hb_xfree(s);

            c = pInfo->Mask[0];
        }

        if (c == 'l' && strchr( "idouxX", pInfo->Mask[1] ))
            m = c;
        if (c == 'h' && strchr( "idouxX", pInfo->Mask[1] ))
            m = c;
        if (c == 'L' && strchr( "eEfgG", pInfo->Mask[1] ))
            m = c;

        // Chegou no tipo?
        if (strchr( "cdieEfgGosuxXpn%", c ))
        {
            specifier = c;
            pInfo->Mask ++;
            pInfo->msk_len--;

            t2 = pInfo->Mask;

            switch( specifier )
            {
            case '%':
            {
                printf_add_str( pInfo, "%", 1L );
                break;
            }
            case 'c':
                //  O argumento é tratado como um inteiro, e mostrado como o caractere ASCII correspondente.
            {
                char s[2];
                memset( s, 0, 2 );

                if (HB_IS_STRING( pItem ))
                {
                    const char *t = hb_itemGetCPtr( pItem );
                    s[0] = ((t) ? t[0] : 0);
                } else if( HB_IS_NUMERIC( pItem ) )
                {
                    s[0] = (char) hb_itemGetNI( pItem );
                }

                printf_add_str( pInfo, s, 1L );
                break;
            }
            case 's':
            {
                Length = t2-t1;
                width  = max( hb_itemGetCLen( pItem ), (ULONG) width );
                cValue = (char *) hb_xgrab( width + 1 );

                memmove( mask, t1, Length );
                mask[Length] = '\0';

                cValue[0] = '\0';
                Length = sprintf( cValue, mask, hb_itemGetCPtr( pItem ) );
                printf_add_str( pInfo, cValue, Length );
                hb_xfree( cValue );
                break;
            }
            case 'p':
            {
                char s[9];
                s[0] = '\0';
                sprintf( s, "%p", hb_itemGetPtr( pItem ) );
                printf_add_str( pInfo, s, 8);
                break;
            }

            /*
             * Separamos aqui as operações sobre inteiros
             */
            case 'i':
            case 'd':
            // O argumento é tratado como um INTEIRO, e mostrado como um número decimal com sinal.
            case 'o':
            // O argumento é tratado com um INTEIRO, e mostrado como un número octal.
            case 'u':
            // O argumento é tratado com um INTEIRO, e mostrado como um número decimal sem sinal.
            case 'x':
            // O argumento é tratado como um INTEIRO, e mostrado como um número hexadecimal (com as letras minúsculas).
            case 'X':
                // O argumento é tratado como um INTEIRO, e mostrado como um número hexadecimal (com as letras maiúsculas).
            {
                Length = t2-t1;
                width  = ((width) ? width : 32 );
                cValue = (char *) hb_xgrab( width + 1 );

                memmove( mask, t1, Length );
                mask[Length] = '\0';
                cValue[0] = '\0';

                if (m == '\0')
                    Length = sprintf( cValue, mask, (int) hb_itemGetNI( pItem ));
                if (m == 'l')
                    Length = sprintf( cValue, mask, (LONG) hb_itemGetNL( pItem )); // como LONG ???
                if (m == 'h')
                    Length = sprintf( cValue, mask, (LONG) hb_itemGetNL( pItem )); // como LONG ???

                printf_add_str( pInfo, cValue, Length );
                hb_xfree( cValue );
                break;
            }

            /*
             * Separamos aqui as operações com numeros flutuantes
             */
            case 'e' :
            // O argumento é tratado como notação científica (e.g. 1.2e+2).
            case 'E' :
            // O argumento é tratado como notação científica (e.g. 1.2e+2).
            case 'f' :
            // O argumento é tratado como um float, e mostrado como um número de ponto flutuante.
            case 'F' :
                // O argumento é tratado como um float, e mostrado como um número de ponto flutuante.
            {
                Length = t2-t1;
                width  = ((width) ? width : 64 );
                dValue = (double) hb_itemGetND( pItem );     // converting double --> float ???
                cValue = (char *) hb_xgrab( width + 1 );

                memmove( mask, t1, Length );
                mask[Length] = '\0';

                sprintf( cValue, mask, dValue );
                printf_add_str( pInfo, cValue, strlen( cValue ) );
                hb_xfree( cValue );
                break;
            }

            default:
                break;
            }
            break;
        } else {
            pInfo->Mask ++;
            pInfo->msk_len--;
        }
    }
}
Ejemplo n.º 14
0
Archivo: math.c Proyecto: emazv72/core
static int hb_matherrblock( HB_MATH_EXCEPTION * pexc )
{
   PHB_MATHERRDATA pMathErr = hb_mathErrData();
   int retval;

   /* call codeblock for both case: handled and unhandled exceptions */

   if( pMathErr->block )
   {
      PHB_ITEM pArray, pRet;
      PHB_ITEM pType, pFuncname, pError, pArg1, pArg2, pRetval, pHandled;
      const char * funcname = pexc->funcname;

      if( funcname == HB_ERR_FUNCNAME )
      {
         PHB_SYMB pSym = hb_itemGetSymbol( hb_stackBaseItem() );
         if( pSym )
            funcname = pSym->szName;
      }

      pType = hb_itemPutNI( NULL, pexc->type );
      pFuncname = hb_itemPutC( NULL, funcname );
      pError = hb_itemPutC( NULL, pexc->error );
      pArg1 = hb_itemPutND( NULL, pexc->arg1 );
      pArg2 = hb_itemPutND( NULL, pexc->arg2 );
      pRetval = hb_itemPutNDLen( NULL, pexc->retval, pexc->retvalwidth, pexc->retvaldec );
      pHandled = hb_itemPutL( NULL, pexc->handled );

      pArray = hb_itemArrayNew( 2 );
      hb_itemArrayPut( pArray, 1, pRetval );
      hb_itemArrayPut( pArray, 2, pHandled );

      /* launch error codeblock that can
         a) change the members of the array = {dRetval, lHandled} to set the
            return value of the math C RTL routine and the <exception handled
            flag> and it
         b) can return an integer value to set the return value of matherr().
         NOTE that these values are only used if lHandled was .F. and is set
         to .T. within the codeblock */
      pRet = hb_itemDo( pMathErr->block, 6, pType, pFuncname, pError, pArg1, pArg2, pArray );

      hb_itemRelease( pType );
      hb_itemRelease( pFuncname );
      hb_itemRelease( pError );
      hb_itemRelease( pArg1 );
      hb_itemRelease( pArg2 );
      hb_itemRelease( pRetval );
      hb_itemRelease( pHandled );

      if( pexc->handled )
      {
         /* math exception has already been handled, so codeblock call above
            was only informative */
         retval = 1;
      }
      else
      {
         /* exception handled by codeblock ? */
         pHandled = hb_itemArrayGet( pArray, 2 );
         if( pHandled )
         {
            pexc->handled = hb_itemGetL( pHandled );
            hb_itemRelease( pHandled );
         }

         if( pexc->handled )
         {
            /* YES ! */
            /* extract retval for math routine and matherr() */
            pRetval = hb_itemArrayGet( pArray, 1 );
            if( pRetval )
            {
               pexc->retval = hb_itemGetND( pRetval );
               hb_itemGetNLen( pRetval, &pexc->retvalwidth, &pexc->retvaldec );
               hb_itemRelease( pRetval );
            }
            if( pRet && HB_IS_NUMERIC( pRet ) )
            {
               retval = hb_itemGetNI( pRet );  /* block may also return 0 to force C math lib warnings */
               hb_itemRelease( pRet );
            }
            else
            {
               retval = 1;  /* default return value to suppress C math lib warnings */
            }
         }
         else
         {
            /* NO ! */
            retval = 1;
         }
      }
      hb_itemRelease( pArray );
   }
   else
   {
      retval = 1;  /* default return value to suppress C math lib warnings */
   }

   if( pMathErr->prevHandler )
   {
      if( pexc->handled )
      {
         /* the error is handled, so simply inform the previous handler */
         ( *pMathErr->prevHandler )( pexc );
      }
      else
      {
         /* else go on error handling within previous handler */
         retval = ( *pMathErr->prevHandler )( pexc );
      }
   }
   return retval;
}
Ejemplo n.º 15
0
static void s_xhb_bitOper( int iOper )
{
   PHB_ITEM pItem1 = hb_param( 1, HB_IT_ANY ),
            pItem2 = hb_param( 2, HB_IT_ANY );
   HB_SIZE nLen1 = hb_itemGetCLen( pItem1 ),
           nLen2 = hb_itemGetCLen( pItem2 );

   if( pItem1 && pItem2 )
   {
      if( HB_IS_NUMERIC( pItem1 ) && ( HB_IS_NUMERIC( pItem2 ) || nLen2 == 1 ) )
      {
         HB_MAXINT nVal1 = hb_itemGetNInt( pItem1 ),
                   nVal2 = nLen2 == 1 ? ( HB_BYTE ) hb_itemGetCPtr( pItem1 )[ 0 ] :
                                        hb_itemGetNInt( pItem2 );
         switch( iOper )
         {
            case XHB_AND:
               nVal1 &= nVal2;
               break;
            case XHB_OR:
               nVal1 |= nVal2;
               break;
            default: /* XHB_XOR */
               nVal1 ^= nVal2;
               break;
         }
         hb_retnint( nVal1 );
         return;
      }

      if( HB_IS_STRING( pItem1 ) && HB_IS_STRING( pItem2 ) )
      {
         if( ( nLen1 | nLen2 ) != 0 )
         {
            const char * pStr1 = hb_itemGetCPtr( pItem1 ),
                       * pStr2 = hb_itemGetCPtr( pItem2 );
            char * pRet = ( char * ) hb_xmemdup( pStr1, nLen1 + 1 );
            HB_SIZE n1, n2;

            switch( iOper )
            {
               case XHB_AND:
                  for( n1 = n2 = 0; n1 < nLen1; n1++ )
                  {
                     pRet[ n1 ] &= pStr2[ n2 ];
                     if( ++n2 == nLen2 )
                        n2 = 0;
                  }
                  break;
               case XHB_OR:
                  for( n1 = n2 = 0; n1 < nLen1; n1++ )
                  {
                     pRet[ n1 ] |= pStr2[ n2 ];
                     if( ++n2 == nLen2 )
                        n2 = 0;
                  }
                  break;
               default: /* XHB_XOR */
                  for( n1 = n2 = 0; n1 < nLen1; n1++ )
                  {
                     pRet[ n1 ] ^= pStr2[ n2 ];
                     if( ++n2 == nLen2 )
                        n2 = 0;
                  }
                  break;
            }
            hb_retclen_buffer( pRet, nLen1 );
         }
         else
            hb_itemReturn( pItem1 );
         return;
      }

      if( HB_IS_STRING( pItem1 ) && ( HB_IS_NUMERIC( pItem2 ) || nLen2 == 1 ) )
      {
         if( nLen1 )
         {
            const char * pStr = hb_itemGetCPtr( pItem1 );
            char * pRet = ( char * ) hb_xmemdup( pStr, nLen1 + 1 );
            char cVal = nLen2 == 1 ? hb_itemGetCPtr( pItem2 )[ 0 ] :
                                     ( char ) hb_itemGetNI( pItem2 );

            nLen2 = nLen1;
            switch( iOper )
            {
               case XHB_AND:
                  while( nLen2-- )
                     pRet[ nLen2 ] &= cVal;
                  break;
               case XHB_OR:
                  while( nLen2-- )
                     pRet[ nLen2 ] |= cVal;
                  break;
               default: /* XHB_XOR */
                  while( nLen2-- )
                     pRet[ nLen2 ] ^= cVal;
                  break;
            }
            hb_retclen_buffer( pRet, nLen1 );
         }
         else
            hb_itemReturn( pItem1 );
         return;
      }

      if( ( HB_IS_NUMERIC( pItem1 ) || nLen1 == 1 ) && HB_IS_STRING( pItem2 ) )
      {
         const char * pStr = hb_itemGetCPtr( pItem2 );
         int iVal = nLen1 == 1 ? hb_itemGetCPtr( pItem1 )[ 0 ] :
                                 hb_itemGetNI( pItem1 );

         switch( iOper )
         {
            case XHB_AND:
               while( nLen2 )
                  iVal &= ( HB_UCHAR ) pStr[ --nLen2 ];
               break;
            case XHB_OR:
               while( nLen2 )
                  iVal |= ( HB_UCHAR ) pStr[ --nLen2 ];
               break;
            default: /* XHB_XOR */
               while( nLen2 )
                  iVal ^= ( HB_UCHAR ) pStr[ --nLen2 ];
               break;
         }
         hb_retni( iVal );
         return;
      }
   }

   hb_errRT_BASE_SubstR( EG_ARG, 1088, NULL,
                         iOper == XHB_AND ? "&" : ( iOper == XHB_OR ? "|" : "^^" ),
                         2, pItem1, pItem2 );
}
Ejemplo n.º 16
0
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 );
   }
}
Ejemplo n.º 17
0
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;
}
Ejemplo n.º 18
0
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 );
   }
}