Beispiel #1
0
static HB_BOOL hb_memowrit( HB_BOOL bHandleEOF )
{
   const char * pszFileName = hb_parc( 1 );
   PHB_ITEM pString   = hb_param( 2, HB_IT_STRING );
   HB_BOOL bRetVal    = HB_FALSE;

   if( pszFileName && pString )
   {
      PHB_FILE pFile = hb_fileExtOpen( pszFileName, NULL,
                                       FO_READWRITE | FO_EXCLUSIVE | FO_PRIVATE |
                                       FXO_TRUNCATE | FXO_SHARELOCK | FXO_NOSEEKPOS,
                                       NULL, NULL );

      if( pFile != NULL )
      {
         HB_SIZE nSize = hb_itemGetCLen( pString );

         bRetVal = hb_fileWriteAt( pFile, hb_itemGetCPtr( pString ), nSize, 0 ) == nSize;

         /* NOTE: CA-Cl*pper will add the EOF even if the write failed. [vszakats] */
         /* NOTE: CA-Cl*pper will not return .F. when the EOF could not be written. [vszakats] */
         if( bHandleEOF && bRetVal )  /* if true, then write EOF */
         {
            char cEOF = HB_CHAR_EOF;
            hb_fileWriteAt( pFile, &cEOF, sizeof( char ), nSize );
         }

         hb_fileClose( pFile );
      }
   }

   return bRetVal;
}
Beispiel #2
0
static PHB_I18N_TRANS hb_i18n_deserialize( PHB_ITEM pItem )
{
   PHB_I18N_TRANS pI18N = NULL;

   if( pItem && HB_IS_STRING( pItem ) )
   {
      HB_SIZE nLen = hb_itemGetCLen( pItem );
      const char * pBuffer = hb_itemGetCPtr( pItem );

      if( nLen > HB_I18N_HEADER_SIZE && hb_i18n_headercheck( pBuffer, nLen ) )
      {
         PHB_ITEM pTable;

         pBuffer += HB_I18N_HEADER_SIZE;
         nLen -= HB_I18N_HEADER_SIZE;
         pTable = hb_itemDeserialize( &pBuffer, &nLen );
         if( pTable )
         {
            pI18N = hb_i18n_initialize( pTable );
            if( ! pI18N )
               hb_itemRelease( pTable );
         }
      }
   }

   return pI18N;
}
Beispiel #3
0
static HB_SIZE hb_cdpItemLen( PHB_CODEPAGE cdp, PHB_ITEM pItem )
{
   HB_SIZE nLen = hb_itemGetCLen( pItem );

   return nLen && HB_CDP_ISCHARIDX( cdp ) ?
          hb_cdpTextLen( cdp, hb_itemGetCPtr( pItem ), nLen ) : nLen;
}
Beispiel #4
0
static HB_SIZE s_fileReadAt( PHB_FILE pFile, void * buffer,
                             HB_SIZE nSize, HB_FOFFSET nOffset )
{
   PHB_IOUSR pIO = ( PHB_IOUSR ) pFile->pFuncs;
   HB_SIZE nResult;
   int iOffset;

   iOffset = ( int ) ( hb_stackTopOffset() - hb_stackBaseOffset() );
   memset( buffer, 0, nSize );
   hb_vmPushString( ( const char * ) buffer, nSize );

   s_pushMethod( pIO, IOUSR_READAT );
   hb_vmPush( pFile->pFileItm );
   hb_xvmPushLocalByRef( ( HB_SHORT ) iOffset );
   hb_vmPushSize( nSize );
   hb_vmPushNumInt( ( HB_MAXINT ) nOffset );
   hb_vmDo( 4 );

   nResult = hb_parns( -1 );
   if( nResult > 0 )
   {
      nSize = hb_itemGetCLen( hb_stackItemFromBase( iOffset ) );
      if( nResult > nSize )
         nResult = nSize;
      memcpy( buffer, hb_itemGetCPtr( hb_stackItemFromBase( iOffset ) ), nSize );
   }
   hb_stackPop();

   return nResult;
}
Beispiel #5
0
PHB_REGEX hb_regexGet( PHB_ITEM pRegExItm, int iFlags )
{
   PHB_REGEX pRegEx = NULL;
   HB_BOOL fArgError = HB_TRUE;

   if( pRegExItm )
   {
      if( HB_IS_POINTER( pRegExItm ) )
      {
         pRegEx = ( PHB_REGEX ) hb_itemGetPtrGC( pRegExItm, &s_gcRegexFuncs );
         if( pRegEx )
            fArgError = HB_FALSE;
      }
      else if( HB_IS_STRING( pRegExItm ) )
      {
         HB_SIZE nLen = hb_itemGetCLen( pRegExItm );
         const char * szRegEx = hb_itemGetCPtr( pRegExItm );
         if( nLen > 0 )
         {
            fArgError = HB_FALSE;
            pRegEx = hb_regexCompile( szRegEx, nLen, iFlags );
         }
      }
   }

   if( fArgError )
      hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, HB_ERR_FUNCNAME, 1, pRegExItm );
   else if( ! pRegEx ) /* hb_regexCompile() failed */
      hb_errRT_BASE_SubstR( EG_ARG, 3015, NULL, HB_ERR_FUNCNAME, 1, pRegExItm );

   return pRegEx;
}
Beispiel #6
0
static void hb_i18n_transitm( PHB_ITEM pText, PHB_CODEPAGE cdpIn, PHB_CODEPAGE cdpOut )
{
   HB_SIZE nLen = hb_itemGetCLen( pText );

   if( nLen > 0 )
   {
      char * szValue = hb_cdpnDup( hb_itemGetCPtr( pText ), &nLen,
                                   cdpIn, cdpOut );
      hb_itemPutCLPtr( pText, szValue, nLen );
   }
}
/*
    wxSocketBase_ReadBase
    Teo. Mexico 2009
*/
void wxSocketBase_ReadBase( HB_BYTE type )
{
    PHB_ITEM pSelf = hb_stackSelfItem();
    wx_SocketBase* socketBase = (wx_SocketBase*) xho_itemListGet_XHO( pSelf );

    PHB_ITEM pBuffer = hb_param( 1, HB_IT_STRING );

    if( pBuffer == NULL || !HB_ISBYREF( 1 ) )
    {
        hb_errRT_BASE_SubstR( EG_ARG, WXH_ERRBASE + 10, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
        return;
    }

    if( socketBase )
    {
        pBuffer = hb_itemUnShareString( pBuffer );

        wxUint32 nbytes = HB_ISNIL( 2 ) ?  hb_itemGetCLen( pBuffer ) : hb_parnl( 2 );

        if( nbytes > 0 )
        {
            if( nbytes > hb_itemGetCLen( pBuffer ) )
                hb_itemReSizeString( pBuffer, nbytes );

            if( type == '0' )
                socketBase->Read( (char *) hb_itemGetCPtr( pBuffer ), nbytes );
            else
                socketBase->ReadMsg( (char *) hb_itemGetCPtr( pBuffer ), nbytes );

            wxUint32 uiLastCount = socketBase->LastCount();

            if( uiLastCount < hb_itemGetCLen( pBuffer ) )
//         hb_itemGetCLen( pBuffer ) = uiLastCount;
                hb_itemReSizeString( pBuffer, uiLastCount );

        }else
            hb_errRT_BASE_SubstR( EG_ARG, WXH_ERRBASE + 10, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );

        hb_itemReturn( pSelf );
    }
}
Beispiel #8
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 );
   }
}
Beispiel #9
0
static HB_ERRCODE odbcExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem )
{
   SDDCONN *  pSDDConn = ( SDDCONN * ) pConnection->pSDDConn;
   void *     hStatement;
   SQLHSTMT   hStmt;
   SQLLEN     iCount;
   char *     szError;
   HB_ERRCODE errCode;

#if ODBCVER >= 0x0300
   if( ! SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_STMT, pSDDConn->hConn, &hStmt ) ) )
#else
   if( ! SQL_SUCCEEDED( SQLAllocStmt( pSDDConn->hConn, &hStmt ) ) )
#endif
   {
      szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, SQL_NULL_HSTMT, &errCode );
      hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTALLOC, szError, hb_itemGetCPtr( pItem ), errCode );
      hb_xfree( szError );
      return HB_FAILURE;
   }

   if( SQL_SUCCEEDED( SQLExecDirect( hStmt,
                                     ( SQLTCHAR * ) O_HB_ITEMGETSTR( pItem, &hStatement, NULL ),
                                     ( SQLINTEGER ) hb_itemGetCLen( pItem ) ) ) )
   {
      hb_strfree( hStatement );

      if( SQL_SUCCEEDED( SQLRowCount( hStmt, &iCount ) ) )
      {
         /* TODO: new id */
         hb_rddsqlSetError( 0, NULL, hb_itemGetCPtr( pItem ), NULL, ( unsigned long ) iCount );
#if ODBCVER >= 0x0300
         SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
#else
         SQLFreeStmt( hStmt, SQL_DROP );
#endif
         return HB_SUCCESS;
      }
   }
   else
      hb_strfree( hStatement );

   szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode );
   hb_rddsqlSetError( errCode, szError, hb_itemGetCPtr( pItem ), NULL, errCode );
   hb_xfree( szError );
#if ODBCVER >= 0x0300
   SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
#else
   SQLFreeStmt( hStmt, SQL_DROP );
#endif
   return HB_FAILURE;
}
Beispiel #10
0
/* returns the numeric value of a character string representation of a number  */
static void hb_val( HB_BOOL fExt )
{
   PHB_ITEM pText = hb_param( 1, HB_IT_STRING );

   if( pText )
   {
      const char * szText = hb_itemGetCPtr( pText );
      int iWidth, iDec, iLen = ( int ) hb_itemGetCLen( pText );
      HB_BOOL fDbl;
      HB_MAXINT lValue;
      double dValue;

      fDbl = hb_valStrnToNum( szText, iLen, &lValue, &dValue, &iDec, &iWidth );

      if( fExt )
      {
         if( HB_ISNUM( 2 ) )
            iLen = hb_parni( 2 );

         if( fDbl && iDec > 0 )
            iLen -= iDec + 1;

         if( iLen > iWidth )
            iWidth = iLen;
         else if( iLen > 0 )
         {
            while( iWidth > iLen && *szText == ' ' )
            {
               iWidth--;
               szText++;
            }
         }
      }

      if( ! fDbl )
         hb_retnintlen( lValue, iWidth );
      else
         hb_retndlen( dValue, iWidth, iDec );
   }
   else
      hb_errRT_BASE_SubstR( EG_ARG, 1098, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}
Beispiel #11
0
static PMIXKEY mixKeyNew( PHB_ITEM pItem, HB_ULONG ulRecNo, HB_BYTE bType, HB_USHORT uiLen )
{
   PMIXKEY pKey;
   double  dbl;
   HB_BYTE buf[ 8 ];

   pKey = ( PMIXKEY ) hb_xgrab( sizeof( HB_ULONG ) + uiLen );
   pKey->rec = ulRecNo;

   switch( bType )
   {
      case 'C':
      {
         HB_SIZE ul = hb_itemGetCLen( pItem );
         if( ul > ( HB_SIZE ) uiLen )
            ul = uiLen;
         memcpy( pKey->val, hb_itemGetCPtr( pItem ), ul );
         if( ul < ( HB_SIZE ) uiLen )
            memset( pKey->val + ul, ' ', ( HB_SIZE ) uiLen - ul );
         break;
      }
      case 'N':
         dbl = hb_itemGetND( pItem );
         HB_DBL2ORD( &dbl, buf );
         memcpy( pKey->val, buf, 8 );
         break;

      case 'D':
         dbl = ( double ) hb_itemGetDL( pItem );
         HB_DBL2ORD( &dbl, buf );
         memcpy( pKey->val, buf, 8 );
         break;

      case 'L':
         pKey->val[ 0 ] = ( HB_BYTE ) ( hb_itemGetL( pItem ) ? 'T' : 'F' );
         break;

      default:
         memset( pKey->val, ' ', uiLen );
   }
   return pKey;
}
Beispiel #12
0
static HB_BOOL hb_memowrit( HB_BOOL bHandleEOF )
{
   const char * pszFileName = hb_parc( 1 );
   PHB_ITEM pString   = hb_param( 2, HB_IT_STRING );
   HB_BOOL bRetVal    = HB_FALSE;

   if( pszFileName && pString )
   {
      PHB_FILE pFile = hb_fileExtOpen( pszFileName, NULL,
                                       FO_READWRITE | FO_EXCLUSIVE | FO_PRIVATE |
                                       FXO_TRUNCATE | FXO_SHARELOCK,
                                       NULL, NULL );

      if( pFile != NULL )
      {
         HB_SIZE nSize = hb_itemGetCLen( pString );
         const char * pData = hb_itemGetCPtr( pString );

         while( nSize > 0 )
         {
            HB_SIZE nWritten = hb_fileWrite( pFile, pData, nSize, 0 );
            if( nWritten == 0 || nWritten == ( HB_SIZE ) FS_ERROR )
               break;
            nSize -= nWritten;
            pData += nWritten;
         }
         bRetVal = nSize == 0;

         /* NOTE: CA-Cl*pper will add the EOF even if the write failed. [vszakats] */
         /* NOTE: CA-Cl*pper will not return .F. when the EOF could not be written. [vszakats] */
         if( bHandleEOF && bRetVal )  /* if true, then write EOF */
         {
            char cEOF = HB_CHAR_EOF;
            hb_fileWrite( pFile, &cEOF, sizeof( char ), -1 );
         }

         hb_fileClose( pFile );
      }
   }

   return bRetVal;
}
Beispiel #13
0
static HB_ERRCODE mysqlExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem )
{
   MYSQL *     pMySql = ( ( SDDCONN * ) pConnection->pSDDConn )->pMySql;
   MYSQL_RES * pResult;
   HB_ULONG    ulAffectedRows;
   PHB_ITEM    pNewID = NULL;

   if( mysql_real_query( pMySql, hb_itemGetCPtr( pItem ), ( unsigned long ) hb_itemGetCLen( pItem ) ) )
   {
      hb_rddsqlSetError( mysql_errno( pMySql ), mysql_error( pMySql ), hb_itemGetCPtr( pItem ), NULL, 0 );
      return HB_FAILURE;
   }

   pResult = mysql_store_result( pMySql );
   if( pResult )
   {
      ulAffectedRows = ( HB_ULONG ) mysql_num_rows( pResult );
      mysql_free_result( pResult );
      hb_rddsqlSetError( 0, NULL, hb_itemGetCPtr( pItem ), NULL, ulAffectedRows );
   }
   else
   {
      if( mysql_field_count( pMySql ) == 0 )
      {
         ulAffectedRows = ( HB_ULONG ) mysql_affected_rows( pMySql );
         if( mysql_insert_id( pMySql ) != 0 )
         {
            pNewID = hb_itemPutNInt( NULL, mysql_insert_id( pMySql ) );
         }
         hb_rddsqlSetError( 0, NULL, hb_itemGetCPtr( pItem ), pNewID, ulAffectedRows );
         if( pNewID )
            hb_itemRelease( pNewID );
      }
      else /* error */
      {
         hb_rddsqlSetError( mysql_errno( pMySql ), mysql_error( pMySql ), hb_itemGetCPtr( pItem ), NULL, 0 );
         return HB_FAILURE;
      }
   }
   return HB_SUCCESS;
}
Beispiel #14
0
static int s_pp_openFile( void * cargo, char * szFileName,
                          HB_BOOL fBefore, HB_BOOL fSysFile, HB_BOOL fBinary,
                          HB_PATHNAMES * pIncludePaths,
                          HB_BOOL * pfNested, FILE ** file_ptr,
                          const char ** pBufPtr, HB_SIZE * pnLen, HB_BOOL * pfFree )
{
   HB_SYMBOL_UNUSED( fSysFile );
   HB_SYMBOL_UNUSED( fBinary );
   HB_SYMBOL_UNUSED( pIncludePaths );
   HB_SYMBOL_UNUSED( pfNested );
   HB_SYMBOL_UNUSED( file_ptr );

   if( ! fBefore )
   {
      HB_COMP_DECL = ( PHB_COMP ) cargo;
      PHB_ITEM pIncItem = ( PHB_ITEM ) HB_COMP_PARAM->cargo;

      if( pIncItem )
      {
         if( HB_IS_HASH( pIncItem ) )
         {
            PHB_ITEM pFileItem = hb_hashGetCItemPtr( pIncItem, szFileName );

            if( pFileItem )
            {
               HB_SIZE nLen = hb_itemGetCLen( pFileItem );
               if( nLen )
               {
                  *pBufPtr = hb_itemGetCPtr( pFileItem );
                  *pnLen   = nLen;
                  *pfFree  = HB_FALSE;
                  return HB_PP_OPEN_OK;
               }
            }
         }
      }
   }

   return HB_PP_OPEN_FILE;
}
Beispiel #15
0
static void STAItm( PHB_ITEM pItmPar )
{
   HB_UINT      i, ulItmPar = ( HB_UINT ) hb_itemGetCLen( pItmPar );
   const char * cItmPar = hb_itemGetCPtr( pItmPar ), * c;
   char *       cRes;

   for( i = 3, c = cItmPar; *c; c++ )
   {
      if( *c == '\'' )
         i++;                 /* Count ' Tokens */
   }
   cRes = ( char * ) hb_xgrab( ulItmPar + i * sizeof( char ) );
   i    = 0; c = cItmPar; cRes[ i++ ] = '\'';
   while( *c )
   {
      if( *c == '\'' )
         cRes[ i++ ] = '\'';
      cRes[ i++ ] = *c++;
   }
   cRes[ i++ ] = '\''; /* cRes[i] = '\0'; */
   hb_itemPutCLPtr( pItmPar, cRes, i );
}
Beispiel #16
0
static PHB_ITEM hb_i18n_pluralexp_compile( PHB_ITEM pExp )
{
   HB_SIZE nLen = hb_itemGetCLen( pExp );
   PHB_ITEM pBlock = NULL;

   if( nLen > 0 )
   {
      char * szMacro = ( char * ) hb_xgrab( nLen + 6 );
      const char * szType;
      PHB_ITEM pMacro;

      szMacro[ 0 ] = '{';
      szMacro[ 1 ] = '|';
      szMacro[ 2 ] = 'n';
      szMacro[ 3 ] = '|';
      memcpy( &szMacro[ 4 ], hb_itemGetCPtr( pExp ), nLen );
      szMacro[ 4 + nLen ] = '}';
      szMacro[ 5 + nLen ] = '\0';
      pMacro = hb_itemPutCLPtr( NULL, szMacro, nLen );
      szType = hb_macroGetType( pMacro );
      if( *szType == 'B' )
      {
         hb_vmPush( pMacro );
         hb_macroGetValue( hb_stackItemFromTop( -1 ), 0, 0 );
         if( hb_vmRequestQuery() == 0 )
         {
            pExp = hb_stackItemFromTop( -1 );
            if( HB_IS_BLOCK( pExp ) )
               pBlock = hb_itemNew( pExp );
            hb_stackPop();
         }
      }
      hb_itemRelease( pMacro );
   }

   return pBlock;
}
Beispiel #17
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 );
   }
}
Beispiel #18
0
static void s_inetSendInternal( HB_BOOL lAll )
{
   PHB_SOCKET_STRUCT socket = HB_PARSOCKET( 1 );
   PHB_ITEM pBuffer = hb_param( 2, HB_IT_STRING );
   const char * buffer;
   int iLen, iSent, iSend;
   long lLastSnd = 1;

   if( socket == NULL || pBuffer == NULL )
      hb_inetErrRT();
   else if( ! hb_inetIsOpen( socket ) )
      hb_retni( -1 );
   else
   {
      buffer = hb_itemGetCPtr( pBuffer );
      iSend = ( int ) hb_itemGetCLen( pBuffer );
      if( HB_ISNUM( 3 ) )
      {
         iLen = hb_parni( 3 );
         if( iLen < iSend )
            iSend = iLen;
      }

      socket->iError = HB_INET_ERR_OK;

      iSent = iLen = 0;
      while( iSent < iSend )
      {
         if( socket->sendFunc )
         {
            iLen = socket->sendFunc( socket->stream, socket->sd,
                                     buffer + iSent, iSend - iSent,
                                     socket->iTimeout, &lLastSnd );
            if( lLastSnd <= 0 && iLen > 0 )
            {
               iSent += iLen;
               iLen = ( int ) lLastSnd;
            }
         }
         else
            iLen = hb_socketSend( socket->sd, buffer + iSent, iSend - iSent,
                                  0, socket->iTimeout );
         if( iLen > 0 )
         {
            iSent += iLen;
            if( ! lAll )
               break;
         }
         else
         {
            hb_inetGetError( socket );
            break;
         }
      }
      socket->iCount = iSent;

      if( socket->flushFunc && ( lLastSnd > 0 || ( lLastSnd == -1 &&
             socket->iTimeout >= 0 && socket->iTimeout < 10000 &&
             s_inetIsTimeout( socket ) ) ) )
      {
         /* TODO: safe information about unflushed data and try to call
                  flush before entering receive wait sate */
         socket->flushFunc( socket->stream, socket->sd, socket->iTimeout < 0 ?
                            socket->iTimeout : HB_MAX( socket->iTimeout, 10000 ),
                            HB_FALSE );
      }

      hb_retni( iSent > 0 ? iSent : iLen );
   }
}
Beispiel #19
0
PHB_ITEM hb_libLoad( PHB_ITEM pLibName, PHB_ITEM pArgs )
{
   void * hDynLib = NULL;

   if( hb_itemGetCLen( pLibName ) > 0 )
   {
      int argc = pArgs ? ( int ) hb_arrayLen( pArgs ) : 0, i;
      const char ** argv = NULL;

      if( argc > 0 )
      {
         argv = ( const char ** ) hb_xgrab( sizeof( char * ) * argc );
         for( i = 0; i < argc; ++i )
            argv[ i ] = hb_arrayGetCPtr( pArgs, i + 1 );
      }

      if( hb_vmLockModuleSymbols() )
      {
         /* use stack address as first level marker */
         hb_vmBeginSymbolGroup( ( void * ) hb_stackId(), HB_TRUE );
#if defined( HB_OS_WIN )
         {
            void * hFileName;

            hDynLib = ( void * ) LoadLibraryEx( HB_ITEMGETSTR( pLibName, &hFileName, NULL ), NULL, LOAD_WITH_ALTERED_SEARCH_PATH );

            hb_strfree( hFileName );
         }
#elif defined( HB_OS_OS2 )
         {
            HB_UCHAR LoadError[ 256 ] = "";  /* Area for load failure information */
            HMODULE hDynModule;
            if( DosLoadModule( ( PSZ ) LoadError, sizeof( LoadError ),
                               ( PCSZ ) hb_itemGetCPtr( pLibName ), &hDynModule ) == NO_ERROR )
               hDynLib = ( void * ) hDynModule;
         }
#elif defined( HB_HAS_DLFCN )
         hDynLib = ( void * ) dlopen( hb_itemGetCPtr( pLibName ), RTLD_LAZY | RTLD_GLOBAL );

         if( ! hDynLib )
         {
            HB_TRACE( HB_TR_DEBUG, ( "hb_libLoad(): dlopen(): %s", dlerror() ) );
         }
#elif defined( HB_CAUSEWAY_DLL )
         hDynLib = LoadLibrary( hb_itemGetCPtr( pLibName ) );
#else
         {
            int iTODO;
         }
#endif
         /* set real marker */
         hb_vmInitSymbolGroup( hDynLib, argc, argv );

         hb_vmUnlockModuleSymbols();
      }

      if( argv )
         hb_xfree( ( void * ) argv );
   }

   if( hDynLib )
   {
      void ** pLibPtr = ( void ** ) hb_gcAllocate( sizeof( void * ), &s_gcDynlibFuncs );
      *pLibPtr = hDynLib;
      return hb_itemPutPtrGC( NULL, pLibPtr );
   }

   return NULL;
}
Beispiel #20
0
static HB_BOOL hb_regex( int iRequest )
{
   HB_REGMATCH aMatches[ HB_REGMATCH_SIZE( REGEX_MAX_GROUPS ) ];
   PHB_ITEM pRetArray, pMatch, pString;
   int i, iMatches, iMaxMatch;
   HB_BOOL fResult = HB_FALSE;
   PHB_REGEX pRegEx;
   const char * pszString;
   HB_SIZE nLen;

   pString = hb_param( 2, HB_IT_STRING );
   if( ! pString )
   {
      hb_errRT_BASE_SubstR( EG_ARG, 3014, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
      return HB_FALSE;
   }
   pRegEx = hb_regexGet( hb_param( 1, HB_IT_ANY ),
                         ( ! hb_parldef( 3, 1 ) ? HBREG_ICASE : 0 ) |
                         ( hb_parl( 4 ) ? HBREG_NEWLINE : 0 ) );
   if( ! pRegEx )
      return HB_FALSE;

   pszString = hb_itemGetCPtr( pString );
   nLen      = hb_itemGetCLen( pString );
   iMaxMatch = iRequest == 0 || iRequest == 4 || iRequest == 5 ?
               REGEX_MAX_GROUPS : 1;
   iMatches = hb_regexec( pRegEx, pszString, nLen, iMaxMatch, aMatches );
   if( iMatches > 0 )
   {
      switch( iRequest )
      {
         case 0:
            pRetArray = hb_itemArrayNew( iMatches );
            for( i = 0; i < iMatches; i++ )
            {
               if( HB_REGMATCH_EO( aMatches, i ) > -1 )
                  hb_arraySetCL( pRetArray, i + 1,
                                 pszString + HB_REGMATCH_SO( aMatches, i ),
                                 HB_REGMATCH_EO( aMatches, i ) -
                                 HB_REGMATCH_SO( aMatches, i ) );
               else
                  hb_arraySetCL( pRetArray, i + 1, NULL, 0 );
            }
            hb_itemReturnRelease( pRetArray );
            fResult = HB_TRUE;
            break;

         case 1: /* LIKE */
            fResult = HB_REGMATCH_SO( aMatches, 0 ) == 0 &&
                      ( HB_SIZE ) HB_REGMATCH_EO( aMatches, 0 ) == nLen;
            break;

         case 2: /* MATCH ( HAS ) */
            fResult = HB_TRUE;
            break;

         case 3: /* SPLIT */
            iMaxMatch = hb_parni( 5 );
            pRetArray = hb_itemArrayNew( 0 );
            pMatch = hb_itemNew( NULL );
            iMatches = 0;
            do
            {
               hb_itemPutCL( pMatch, pszString, HB_REGMATCH_SO( aMatches, 0 ) );
               hb_arrayAddForward( pRetArray, pMatch );
               nLen -= HB_REGMATCH_EO( aMatches, 0 );
               pszString += HB_REGMATCH_EO( aMatches, 0 );
               iMatches++;
            }
            while( HB_REGMATCH_EO( aMatches, 0 ) > 0 && nLen &&
                   ( iMaxMatch == 0 || iMatches < iMaxMatch ) &&
                   hb_regexec( pRegEx, pszString, nLen, 1, aMatches ) > 0 );

            /* last match must be done also in case that pszString is empty;
               this would mean an empty split field at the end of the string */
            /* if( nLen ) */
            {
               hb_itemPutCL( pMatch, pszString, nLen );
               hb_arrayAddForward( pRetArray, pMatch );
            }
            hb_itemRelease( pMatch );

            hb_itemReturnRelease( pRetArray );
            fResult = HB_TRUE;
            break;

         case 4: /* results AND positions */
            pRetArray = hb_itemArrayNew( iMatches );

            for( i = 0; i < iMatches; i++ )
            {
               int iSO = HB_REGMATCH_SO( aMatches, i ),
                   iEO = HB_REGMATCH_EO( aMatches, i );
               pMatch = hb_arrayGetItemPtr( pRetArray, i + 1 );
               hb_arrayNew( pMatch, 3 );
               if( iEO != -1 )
               {
                  /* matched string */
                  hb_arraySetCL( pMatch, 1, pszString + iSO, iEO - iSO );
                  /* begin of match */
                  hb_arraySetNS( pMatch, 2, iSO + 1 );
                  /* End of match */
                  hb_arraySetNS( pMatch, 3, iEO );
               }
               else
               {
                  hb_arraySetCL( pMatch, 1, NULL, 0 );
                  hb_arraySetNS( pMatch, 2, 0 );
                  hb_arraySetNS( pMatch, 3, 0 );
               }
            }
            hb_itemReturnRelease( pRetArray );
            fResult = HB_TRUE;
            break;

         case 5: /* _ALL_ results AND positions */
         {
            PHB_ITEM pAtxArray;
            int      iMax       = hb_parni( 5 );   /* max nuber of matches I want, 0 = unlimited */
            int      iGetMatch  = hb_parni( 6 );   /* Gets if want only one single match or a sub-match */
            HB_BOOL  fOnlyMatch = hb_parldef( 7, 1 ); /* if HB_TRUE returns only matches and sub-matches, not positions */
            HB_SIZE  nOffset    = 0;
            int      iCount     = 0;
            int      iSO, iEO;

            /* Set new array */
            pRetArray = hb_itemArrayNew( 0 );
            do
            {
               /* If I want all matches */
               if( iGetMatch == 0 || /* Check boundaries */
                   ( iGetMatch < 0 || iGetMatch > iMatches ) )
               {
                  pAtxArray = hb_itemArrayNew( iMatches );
                  for( i = 0; i < iMatches; i++ )
                  {
                     iSO = HB_REGMATCH_SO( aMatches, i );
                     iEO = HB_REGMATCH_EO( aMatches, i );
                     pMatch = hb_arrayGetItemPtr( pAtxArray, i + 1 );
                     if( ! fOnlyMatch )
                     {
                        hb_arrayNew( pMatch, 3 );
                        if( iEO != -1 )
                        {
                           /* matched string */
                           hb_arraySetCL( pMatch, 1, pszString + iSO, iEO - iSO );
                           /* begin of match */
                           hb_arraySetNS( pMatch, 2, nOffset + iSO + 1 );
                           /* End of match */
                           hb_arraySetNS( pMatch, 3, nOffset + iEO );
                        }
                        else
                        {
                           hb_arraySetCL( pMatch, 1, NULL, 0 );
                           hb_arraySetNS( pMatch, 2, 0 );
                           hb_arraySetNS( pMatch, 3, 0 );
                        }
                     }
                     else
                     {
                        if( iEO != -1 )
                           /* matched string */
                           hb_itemPutCL( pMatch, pszString + iSO, iEO - iSO );
                        else
                           hb_itemPutC( pMatch, NULL );
                     }
                  }
                  hb_arrayAddForward( pRetArray, pAtxArray );
                  hb_itemRelease( pAtxArray );
               }
               else /* Here I get only single matches */
               {
                  i = iGetMatch - 1;
                  iSO = HB_REGMATCH_SO( aMatches, i );
                  iEO = HB_REGMATCH_EO( aMatches, i );
                  pMatch = hb_itemNew( NULL );
                  if( ! fOnlyMatch )
                  {
                     hb_arrayNew( pMatch, 3 );
                     if( iEO != -1 )
                     {
                        /* matched string */
                        hb_arraySetCL( pMatch, 1, pszString + iSO, iEO - iSO );
                        /* begin of match */
                        hb_arraySetNS( pMatch, 2, nOffset + iSO + 1 );
                        /* End of match */
                        hb_arraySetNS( pMatch, 3, nOffset + iEO );
                     }
                     else
                     {
                        hb_arraySetCL( pMatch, 1, NULL, 0 );
                        hb_arraySetNS( pMatch, 2, 0 );
                        hb_arraySetNS( pMatch, 3, 0 );
                     }
                  }
                  else
                  {
                     if( iEO != -1 )
                        /* matched string */
                        hb_itemPutCL( pMatch, pszString + iSO, iEO - iSO );
                     else
                        hb_itemPutC( pMatch, NULL );
                  }
                  hb_arrayAddForward( pRetArray, pMatch );
                  hb_itemRelease( pMatch );
               }

               iEO = HB_REGMATCH_EO( aMatches, 0 );
               if( iEO == -1 )
                  break;
               nLen -= iEO;
               pszString += iEO;
               nOffset += iEO;
               iCount++;
            }
            while( iEO && nLen && ( iMax == 0 || iCount < iMax ) &&
                   ( iMatches = hb_regexec( pRegEx, pszString, nLen, iMaxMatch, aMatches ) ) > 0 );
            hb_itemReturnRelease( pRetArray );
            fResult = HB_TRUE;
            break;
         }
      }
   }
   else if( iRequest == 3 )
   {
      pRetArray = hb_itemArrayNew( 1 );
      hb_arraySet( pRetArray, 1, pString );
      hb_itemReturnRelease( pRetArray );
      fResult = HB_TRUE;
   }

   hb_regexFree( pRegEx );
   return fResult;
}
Beispiel #21
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--;
        }
    }
}
Beispiel #22
0
static HB_ERRCODE odbcOpen( SQLBASEAREAP pArea )
{
   SDDCONN *   pSDDConn = ( SDDCONN * ) pArea->pConnection->pSDDConn;
   SDDDATA *   pSDDData;
   void *      hQuery;
   SQLHSTMT    hStmt;
   SQLSMALLINT iNameLen;
   PHB_ITEM    pItemEof, pItem;
   HB_BOOL     bError;
   HB_USHORT   uiFields, uiIndex;
   HB_ERRCODE  errCode;
   char *      szError;

   pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) );
   pSDDData        = ( SDDDATA * ) pArea->pSDDData;

#if ODBCVER >= 0x0300
   if( ! SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_STMT, pSDDConn->hConn, &hStmt ) ) )
#else
   if( ! SQL_SUCCEEDED( SQLAllocStmt( pSDDConn->hConn, &hStmt ) ) )
#endif
   {
      szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, SQL_NULL_HSTMT, &errCode );
      hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTALLOC, szError, pArea->szQuery, errCode );
      hb_xfree( szError );
      return HB_FAILURE;
   }

   pItem = hb_itemPutC( NULL, pArea->szQuery );

   if( ! SQL_SUCCEEDED( SQLExecDirect( hStmt,
                                       ( SQLTCHAR * ) O_HB_ITEMGETSTR( pItem, &hQuery, NULL ),
                                       ( SQLINTEGER ) hb_itemGetCLen( pItem ) ) ) )
   {
      hb_strfree( hQuery );
      hb_itemRelease( pItem );
      szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode );
#if ODBCVER >= 0x0300
      SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
#else
      SQLFreeStmt( hStmt, SQL_DROP );
#endif
      hb_errRT_ODBCDD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode );
      hb_xfree( szError );
      return HB_FAILURE;
   }
   else
   {
      hb_strfree( hQuery );
      hb_itemRelease( pItem );
   }

   if( ! SQL_SUCCEEDED( SQLNumResultCols( hStmt, &iNameLen ) ) )
   {
      szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode );
#if ODBCVER >= 0x0300
      SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
#else
      SQLFreeStmt( hStmt, SQL_DROP );
#endif
      hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTDESCR + 1000, szError, pArea->szQuery, errCode );
      hb_xfree( szError );
      return HB_FAILURE;
   }

   uiFields = ( HB_USHORT ) iNameLen;
   SELF_SETFIELDEXTENT( ( AREAP ) pArea, uiFields );

   pItemEof = hb_itemArrayNew( uiFields );

#if 0
   HB_TRACE( HB_TR_ALWAYS, ( "fieldcount=%d", iNameLen ) );
#endif

   errCode = 0;
   bError  = HB_FALSE;
   for( uiIndex = 0; uiIndex < uiFields; uiIndex++ )
   {
      DBFIELDINFO pFieldInfo;

      PHB_ITEM pName;

      SQLTCHAR    cName[ 256 ];
      SQLULEN     uiSize;
      SQLSMALLINT iDataType, iDec, iNull;

      if( ! SQL_SUCCEEDED( SQLDescribeCol( hStmt, ( SQLSMALLINT ) uiIndex + 1, ( SQLTCHAR * ) cName, HB_SIZEOFARRAY( cName ), &iNameLen, &iDataType, &uiSize, &iDec, &iNull ) ) )
      {
         hb_itemRelease( pItemEof );
         szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, NULL );
#if ODBCVER >= 0x0300
         SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
#else
         SQLFreeStmt( hStmt, SQL_DROP );
#endif
         hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTDESCR + 1001, szError, pArea->szQuery, 0 );
         hb_xfree( szError );
         return HB_FAILURE;
      }

      pName = O_HB_ITEMPUTSTRLEN( NULL, ( O_HB_CHAR * ) cName, iNameLen );
      pFieldInfo.atomName = hb_itemGetCPtr( pName );

      /*
         We do mapping of many SQL types to one Harbour field type here, so, we need store
         real SQL type in uiTypeExtended. SQL types are signed, so, HB_USHORT type casting
         is a little hacky. We need to remember use this casting also in expressions like
         this:
            if( pField->uiTypeExtended == ( HB_USHORT ) SQL_BIGINT )
         or introduce our own unsigned SQL types.
         [Mindaugas]
       */
      pFieldInfo.uiTypeExtended = ( HB_USHORT ) iDataType;
      pFieldInfo.uiLen = ( HB_USHORT ) uiSize;
      pFieldInfo.uiDec = iDec;

#if 0
      HB_TRACE( HB_TR_ALWAYS, ( "field: name=%s type=%d len=%d dec=%d null=%d", pFieldInfo.atomName, iDataType, uiSize, iDec, iNull ) );
#endif

      switch( iDataType )
      {
         case SQL_CHAR:
         case SQL_VARCHAR:
         case SQL_LONGVARCHAR:
         case SQL_WCHAR:
         case SQL_WVARCHAR:
         case SQL_WLONGVARCHAR:
            pFieldInfo.uiType = HB_FT_STRING;
            break;

         case SQL_VARBINARY:
         case SQL_LONGVARBINARY:
            pFieldInfo.uiType  = HB_FT_STRING;
            pFieldInfo.uiFlags = HB_FF_BINARY;
            break;

         case SQL_TINYINT:
         case SQL_SMALLINT:
         case SQL_INTEGER:
         case SQL_BIGINT:
            pFieldInfo.uiType = HB_FT_INTEGER;
            break;

         case SQL_DECIMAL:
         case SQL_NUMERIC:
            pFieldInfo.uiType = HB_FT_LONG;
            break;

         case SQL_REAL:
         case SQL_FLOAT:
         case SQL_DOUBLE:
            pFieldInfo.uiType = HB_FT_DOUBLE;
            break;

         case SQL_BIT:
            pFieldInfo.uiType = HB_FT_LOGICAL;
            break;

         case SQL_DATE:
#if ODBCVER >= 0x0300
         case SQL_TYPE_DATE:
#endif
            pFieldInfo.uiType = HB_FT_DATE;
            break;

         case SQL_TIME:
#if ODBCVER >= 0x0300
         case SQL_TYPE_TIME:
#endif
            pFieldInfo.uiType = HB_FT_DATE;
            break;

         /*  SQL_DATETIME = SQL_DATE = 9 */
         case SQL_TIMESTAMP:
#if ODBCVER >= 0x0300
         case SQL_TYPE_TIMESTAMP:
#endif
            pFieldInfo.uiType = HB_FT_TIMESTAMP;
            break;

         default:
#if 0
            HB_TRACE( HB_TR_ALWAYS, ( "new sql type=%d", iDataType ) );
#endif
            bError  = HB_TRUE;
            errCode = ( HB_ERRCODE ) iDataType;
            pFieldInfo.uiType = 0;
            pFieldInfo.uiType = HB_FT_STRING;
            break;
      }

      if( ! bError )
      {
         switch( pFieldInfo.uiType )
         {
            case HB_FT_STRING:
            {
               char * pStr;

               pStr = ( char * ) hb_xgrab( ( HB_SIZE ) pFieldInfo.uiLen + 1 );
               memset( pStr, ' ', pFieldInfo.uiLen );
               pStr[ pFieldInfo.uiLen ] = '\0';

               pItem = hb_itemPutCL( NULL, pStr, pFieldInfo.uiLen );
               hb_xfree( pStr );
               break;
            }

            case HB_FT_MEMO:
               pItem = hb_itemPutC( NULL, NULL );
               break;

            case HB_FT_INTEGER:
               pItem = hb_itemPutNI( NULL, 0 );
               break;

            case HB_FT_LONG:
               if( pFieldInfo.uiDec == 0 )
                  pItem = hb_itemPutNLLen( NULL, 0, pFieldInfo.uiLen );
               else
                  pItem = hb_itemPutNDLen( NULL, 0.0, pFieldInfo.uiLen, pFieldInfo.uiDec );
               break;

            case HB_FT_DOUBLE:
               pItem = hb_itemPutNDLen( NULL, 0.0, pFieldInfo.uiLen, pFieldInfo.uiDec );
               break;

            case HB_FT_LOGICAL:
               pItem = hb_itemPutL( NULL, HB_FALSE );
               break;

            case HB_FT_DATE:
               pItem = hb_itemPutDL( NULL, 0 );
               break;

            case HB_FT_TIME:
               pItem = hb_itemPutTDT( NULL, 0, 0 );
               break;

            case HB_FT_TIMESTAMP:
               pItem = hb_itemPutTDT( NULL, 0, 0 );
               break;

            default:
               pItem  = hb_itemNew( NULL );
               bError = HB_TRUE;
               break;
         }

         hb_arraySetForward( pItemEof, uiIndex + 1, pItem );
         hb_itemRelease( pItem );

         if( ! bError )
            bError = ( SELF_ADDFIELD( ( AREAP ) pArea, &pFieldInfo ) == HB_FAILURE );
      }

      hb_itemRelease( pName );

      if( bError )
         break;
   }

   if( bError )
   {
      hb_itemRelease( pItemEof );
#if ODBCVER >= 0x0300
      SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
#else
      SQLFreeStmt( hStmt, SQL_DROP );
#endif
      hb_errRT_ODBCDD( EG_CORRUPTION, ESQLDD_INVALIDFIELD, "Invalid field type", pArea->szQuery, errCode );
      return HB_FAILURE;
   }

   pArea->ulRecCount = 0;
   pArea->ulRecMax   = SQLDD_ROWSET_INIT;

   pArea->pRow = ( void ** ) hb_xgrab( SQLDD_ROWSET_INIT * sizeof( void * ) );
   memset( pArea->pRow, 0, SQLDD_ROWSET_INIT * sizeof( void * ) );
   pArea->pRowFlags = ( HB_BYTE * ) hb_xgrab( SQLDD_ROWSET_INIT * sizeof( HB_BYTE ) );
   memset( pArea->pRowFlags, 0, SQLDD_ROWSET_INIT * sizeof( HB_BYTE ) );

   pArea->pRow[ 0 ]      = pItemEof;
   pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED;

   pSDDData->hStmt = hStmt;
   return HB_SUCCESS;
}
Beispiel #23
0
/*
 * 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;
}
Beispiel #24
0
/* Export field value into the buffer in SQL format */
static HB_BOOL hb_exportBufSqlVar( PHB_FILEBUF pFileBuf, PHB_ITEM pValue,
                                   const char * szDelim, const char * szEsc )
{
   switch( hb_itemType( pValue ) )
   {
      case HB_IT_STRING:
      {
         HB_SIZE nLen = hb_itemGetCLen( pValue );
         HB_SIZE nCnt = 0;
         const char *szVal = hb_itemGetCPtr( pValue );

         hb_addStrToFBuffer( pFileBuf, szDelim );
         while( nLen && HB_ISSPACE( szVal[ nLen - 1 ] ) )
            nLen--;

         while( *szVal && nCnt++ < nLen )
         {
            if( *szVal == *szDelim || *szVal == *szEsc )
               hb_addToFBuffer( pFileBuf, *szEsc );
            if( ( HB_UCHAR ) *szVal >= 32 )
               hb_addToFBuffer( pFileBuf, *szVal );
            else
            {
               /* printf( "%d %c", *szVal, *szVal ); */
            }
            szVal++;
         }
         hb_addStrToFBuffer( pFileBuf, szDelim );
         break;
      }

      case HB_IT_DATE:
      {
         char szDate[ 9 ];

         hb_addStrToFBuffer( pFileBuf, szDelim );
         hb_itemGetDS( pValue, szDate );
         if( szDate[ 0 ] == ' ' )
         {
            hb_addStrToFBuffer( pFileBuf, "0100-01-01" );
         }
         else
         {
            hb_addStrnToFBuffer( pFileBuf, &szDate[0], 4 );
            hb_addToFBuffer( pFileBuf, '-' );
            hb_addStrnToFBuffer( pFileBuf, &szDate[4], 2 );
            hb_addToFBuffer( pFileBuf, '-' );
            hb_addStrnToFBuffer( pFileBuf, &szDate[6], 2 );
         }
         hb_addStrToFBuffer( pFileBuf, szDelim );
         break;
      }

      case HB_IT_TIMESTAMP:
      {
         long lDate, lTime;
         char szDateTime[ 24 ];

         hb_itemGetTDT( pValue, &lDate, &lTime );
         hb_timeStampStr( szDateTime, lDate, lTime );
         hb_addStrToFBuffer( pFileBuf, szDelim );
         hb_addStrToFBuffer( pFileBuf, szDateTime );
         hb_addStrToFBuffer( pFileBuf, szDelim );
         break;
      }

      case HB_IT_LOGICAL:
         hb_addStrToFBuffer( pFileBuf, szDelim );
         hb_addToFBuffer( pFileBuf, hb_itemGetL( pValue ) ? 'Y' : 'N' );
         hb_addStrToFBuffer( pFileBuf, szDelim );
         break;

      case HB_IT_INTEGER:
      case HB_IT_LONG:
      case HB_IT_DOUBLE:
      {
         char szResult[ HB_MAX_DOUBLE_LENGTH ];
         int iSize, iWidth, iDec;

         hb_itemGetNLen( pValue, &iWidth, &iDec );
         iSize = ( iDec > 0 ? iWidth + 1 + iDec : iWidth );
         if( hb_itemStrBuf( szResult, pValue, iSize, iDec ) )
         {
            int iPos = 0;
            while( iSize && HB_ISSPACE( szResult[ iPos ] ) )
            {
               iPos++;
               iSize--;
            }
            hb_addStrnToFBuffer( pFileBuf, &szResult[ iPos ], iSize );
         }
         else
            hb_addToFBuffer( pFileBuf, '0' );
         break;
      }
      /* an "M" field or the other, might be a "V" in SixDriver */
      default:
         /* We do not want MEMO contents */
         return HB_FALSE;
   }
   return HB_TRUE;
}
Beispiel #25
0
BOOL sql_item2str( sql_item_arg *pItemArg, SQLSYS_INFO *pSysInfo )
{
   ULONG lSize  = 0L;         /* For Temporary values */
   char *szText = NULL;       /* Temporary buffer */
   BOOL bFree   = TRUE;
   
   if (!pItemArg)
   {
      HB_TRACE(HB_TR_DEBUG,("  Invalid Argument -> %p", pItemArg ));
      return FALSE;
   }
      
   if (!pSysInfo)
   {
      pSysInfo = (SQLSYS_INFO *) SQLSYS_GETINFO( 0 );
      
      // Valid driver? It has been correctly loaded into memory?
      if (!SR_ISVALID_DRIVER(pSysInfo))
      {
         HB_TRACE(HB_TR_DEBUG,("  Invalid driver !!" ));
         return FALSE;
      }
   }   
   
   HB_TRACE(HB_TR_DEBUG,("  ARGUMENT  => '%s'", hb_itemTypeStr( pItemArg->pItem ) ));
   
   switch( hb_itemType( pItemArg->pItem ) )
   {
      case HB_IT_MEMO:
      case HB_IT_STRING:
      {         
         HB_TRACE(HB_TR_DEBUG,("  As Text   => '%s'", hb_itemGetCPtr( pItemArg->pItem ) ));
         
         lSize  = hb_itemGetCLen( pItemArg->pItem );
         szText = (*pSysInfo->EscapeString) ( hb_itemGetCPtr( pItemArg->pItem ), &lSize, 0 );
         
         /*
         char *szTemp, *szBuff;
         szTemp = (*pSysInfo->EscapeString) ( hb_itemGetCPtr( pItemArg->pItem ), &lSize, 0 );
         
         if (szTemp)
         {
            szText = (char *) hb_xgrab( lSize + 3L );   // chr(0) + 2 separators
            szBuff = szText;
            
           *szText = pSysInfo->FieldDelim; szText ++;
            memcpy( szText, szTemp, lSize );
            szText += lSize;
           *szText = pSysInfo->FieldDelim; szText ++;
           *szText = '\0';
           
           hb_xfree( szTemp );
         }
         /***/
         break;
      }
      case HB_IT_LOGICAL:
         HB_TRACE(HB_TR_DEBUG,("  As BOOL   => %d", (int) hb_itemGetL( pItemArg->pItem ) ));
         szText = (hb_itemGetL( pItemArg->pItem ) ? pSysInfo->BoolTrue : pSysInfo->BoolFalse); 
         lSize  = strlen( szText );
         bFree  = FALSE;
         break;
         
      case HB_IT_DATE:
      {               
         int iYear, iMonth, iDay;
         hb_dateDecode( hb_itemGetDL( pItemArg->pItem ), &iYear, &iMonth, &iDay );
         HB_TRACE(HB_TR_DEBUG,("  As DATE   => %04d/%02d/%02d", iYear, iMonth, iDay ));
         
         if (pSysInfo->DateFormat == ESCAPE_FORMAT_DATE_ISO)
         {               
            szText = SQLSYS_DATETOISO( (char *) hb_xgrab(13), iYear, iMonth, iDay, TRUE );
            lSize  = 12L;
         }
         break;
      }   
      case HB_IT_INTEGER:
      case HB_IT_DOUBLE:
      case HB_IT_LONG:
      {
         szText = hb_itemStr( pItemArg->pItem, NULL, NULL );
         HB_TRACE(HB_TR_DEBUG,("  As Number => %s", szText ));
   
         if( szText )
         {
            ULONG nToSkip = 0L;
   
            while( szText[ nToSkip] == ' ' )
               ++nToSkip;
   
            /* Rever isto aqui de perto */
            if( nToSkip )
               memmove( szText, szText + nToSkip, strlen( szText + nToSkip ) + 1 );
            
            lSize = strlen( szText );
            HB_TRACE(HB_TR_DEBUG,("  As Text => '%s' -- %lu", szText, lSize ));
         }
         break;
      }
         
      default:                    
      {
         szText = (char *) hb_xgrab(5);
        *szText = '\0';
         strcat( szText, "NULL" );
         lSize  = strlen( szText );
         break;
      }
   }
      
   if (!szText)
   {
      HB_TRACE(HB_TR_DEBUG,("  Impossivel converter este valor:" ));
      HB_TRACE_ARGS( pItemArg->pItem ); 
      return FALSE;
   }
      
   pItemArg->szResult = szText;
   pItemArg->ulSize   = lSize; 
   pItemArg->bDestroy = bFree;      
   return TRUE;
}
Beispiel #26
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 );
}
Beispiel #27
0
static HB_ERRCODE sqlite3Open( SQLBASEAREAP pArea )
{
   sqlite3 *      pDb = ( ( SDDCONN * ) pArea->pConnection->pSDDConn )->pDb;
   sqlite3_stmt * st  = NULL;
   SDDDATA *      pSDDData;
   const char *   pszQuery;
   HB_SIZE        nQueryLen;
   void *         hQuery;
   HB_USHORT      uiFields, uiIndex;
   PHB_ITEM       pItemEof, pItem;
   HB_ERRCODE     errCode;
   char *         szError;
   HB_BOOL        bError;

   pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) );
   pSDDData        = ( SDDDATA * ) pArea->pSDDData;

   pItem    = hb_itemPutC( NULL, pArea->szQuery );
   pszQuery = S_HB_ITEMGETSTR( pItem, &hQuery, &nQueryLen );

   if( sqlite3_prepare_v2( pDb, pszQuery, ( int ) nQueryLen, &st, NULL ) != SQLITE_OK )
   {
      hb_strfree( hQuery );
      hb_itemRelease( pItem );
      szError = sqlite3GetError( pDb, &errCode );
      hb_errRT_SQLT3DD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode );
      sqlite3_finalize( st );
      hb_xfree( szError );
      return HB_FAILURE;
   }
   else
   {
      hb_strfree( hQuery );
      hb_itemRelease( pItem );
   }

   if( sqlite3_step( st ) != SQLITE_ROW )
   {
      szError = sqlite3GetError( pDb, &errCode );
      hb_errRT_SQLT3DD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode );
      sqlite3_finalize( st );
      hb_xfree( szError );
      return HB_FAILURE;
   }

   uiFields = ( HB_USHORT ) sqlite3_column_count( st );
   SELF_SETFIELDEXTENT( ( AREAP ) pArea, uiFields );

   pItemEof = hb_itemArrayNew( uiFields );

#if 0
   HB_TRACE( HB_TR_ALWAYS, ( "fieldcount=%d", uiFields ) );
#endif

   errCode = 0;
   bError  = HB_FALSE;
   for( uiIndex = 0; uiIndex < uiFields; ++uiIndex )
   {
      DBFIELDINFO pFieldInfo;

      int iDataType = sqlite3_column_type( st, uiIndex );
      PHB_ITEM pName = S_HB_ITEMPUTSTR( NULL, sqlite3_column_name( st, uiIndex ) );
      HB_USHORT uiNameLen = ( HB_USHORT ) hb_itemGetCLen( pName );

      if( ( ( AREAP ) pArea )->uiMaxFieldNameLength < uiNameLen )
         ( ( AREAP ) pArea )->uiMaxFieldNameLength = uiNameLen;

      pFieldInfo.atomName = hb_itemGetCPtr( pName );

#if 0
      HB_TRACE( HB_TR_ALWAYS, ( "field: name=%s type=%d len=%d", pFieldInfo.atomName, iDataType, sqlite3_column_bytes( st, uiIndex ) ) );
#endif

      /* There are no field length limits stored in the SQLite3 database,
         so we're resorting to setting some arbitrary default values to
         make apps relying on these (f.e. Browse()/GET) to behave somewhat
         better. For better results, update apps to untie UI metrics from
         any database field/value widths. [vszakats] */

      pFieldInfo.uiLen = 10;
      pFieldInfo.uiDec = 0;

      switch( iDataType )
      {
         case SQLITE3_TEXT:
            pFieldInfo.uiType = HB_FT_STRING;
            break;

         case SQLITE_FLOAT:
            pFieldInfo.uiType = HB_FT_LONG;
            pFieldInfo.uiDec = ( HB_USHORT ) hb_setGetDecimals();
            pFieldInfo.uiLen += pFieldInfo.uiDec + 1;
            break;

         case SQLITE_INTEGER:
            pFieldInfo.uiType = HB_FT_LONG;
            break;

         case SQLITE_BLOB:
         case SQLITE_NULL:
            pFieldInfo.uiType = HB_FT_BLOB;
            break;

         default:
#if 0
            HB_TRACE( HB_TR_ALWAYS, ( "new sql type=%d", iDataType ) );
#endif
            bError  = HB_TRUE;
            errCode = ( HB_ERRCODE ) iDataType;
            pFieldInfo.uiType = 0;
            break;
      }

      if( ! bError )
      {
         switch( pFieldInfo.uiType )
         {
            case HB_FT_STRING:
            {
               char * pStr = ( char * ) hb_xgrab( ( HB_SIZE ) pFieldInfo.uiLen + 1 );
               memset( pStr, ' ', pFieldInfo.uiLen );
               pStr[ pFieldInfo.uiLen ] = '\0';

               pItem = hb_itemPutCLPtr( NULL, pStr, pFieldInfo.uiLen );
               break;
            }
            case HB_FT_BLOB:
               pItem = hb_itemPutC( NULL, NULL );
               break;

            case HB_FT_LONG:
               if( pFieldInfo.uiDec == 0 )
                  pItem = hb_itemPutNLLen( NULL, 0, pFieldInfo.uiLen );
               else
                  pItem = hb_itemPutNDLen( NULL, 0.0, pFieldInfo.uiLen, pFieldInfo.uiDec );
               break;

            default:
               pItem  = hb_itemNew( NULL );
               bError = HB_TRUE;
         }

         hb_arraySetForward( pItemEof, uiIndex + 1, pItem );
         hb_itemRelease( pItem );

         if( ! bError )
            bError = ( SELF_ADDFIELD( ( AREAP ) pArea, &pFieldInfo ) == HB_FAILURE );
      }

      hb_itemRelease( pName );

      if( bError )
         break;
   }

   if( bError )
   {
      hb_itemRelease( pItemEof );
      sqlite3_finalize( st );
      hb_errRT_SQLT3DD( EG_CORRUPTION, ESQLDD_INVALIDFIELD, "Invalid field type", pArea->szQuery, errCode );
      return HB_FAILURE;
   }

   pArea->ulRecCount = 0;
   pArea->ulRecMax   = SQLDD_ROWSET_INIT;

   pArea->pRow = ( void ** ) hb_xgrabz( SQLDD_ROWSET_INIT * sizeof( void * ) );
   pArea->pRowFlags = ( HB_BYTE * ) hb_xgrabz( SQLDD_ROWSET_INIT * sizeof( HB_BYTE ) );

   pArea->pRow[ 0 ]      = pItemEof;
   pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED;

   pSDDData->pStmt = st;
   return HB_SUCCESS;
}
Beispiel #28
0
/* Export field values to text file */
static HB_BOOL hb_ExportVar( HB_FHANDLE handle, PHB_ITEM pValue, const char * cDelim, PHB_CODEPAGE cdp )
{
   switch( hb_itemType( pValue ) )
   {
      /* a "C" field */
      case HB_IT_STRING:
      {
         char * szStrEsc;
         char * szString;

         szStrEsc = hb_strescape( hb_itemGetCPtr( pValue ),
                                  hb_itemGetCLen( pValue ), cDelim );
         if( cdp )
            hb_cdpnDupLen( szStrEsc, strlen( szStrEsc ), hb_vmCDP(), cdp );

         szString = hb_xstrcpy( NULL, cDelim, szStrEsc, cDelim, NULL );

         /* FWrite( handle, szString ) */
         hb_fsWriteLarge( handle, szString, strlen( szString ) );

         /* Orphaned, get rif off it */
         hb_xfree( szStrEsc );
         hb_xfree( szString );
         break;
      }
      /* a "D" field */
      case HB_IT_DATE:
      {
         char * szDate = ( char * ) hb_xgrab( 9 );

         hb_itemGetDS( pValue, szDate );
         hb_fsWriteLarge( handle, szDate, strlen( szDate ) );
         hb_xfree( szDate );
         break;
      }
      /* an "L" field */
      case HB_IT_LOGICAL:
         hb_fsWriteLarge( handle, ( hb_itemGetL( pValue ) ? "T" : "F" ), 1 );
         break;
      /* an "N" field */
      case HB_IT_INTEGER:
      case HB_IT_LONG:
      case HB_IT_DOUBLE:
      {
         char * szResult = hb_itemStr( pValue, NULL, NULL );

         if( szResult )
         {
            HB_SIZE      nLen      = strlen( szResult );
            const char * szTrimmed = hb_strLTrim( szResult, &nLen );

            hb_fsWriteLarge( handle, szTrimmed, strlen( szTrimmed ) );
            hb_xfree( szResult );
         }
         break;
      }
      /* an "M" field or the other, might be a "V" in SixDriver */
      default:
         /* We do not want MEMO contents */
         return HB_FALSE;
   }
   return HB_TRUE;
}
Beispiel #29
0
/* Export field value into the buffer in PG accepted CSV format */
static HB_BOOL exportBufSqlVar( pgCopyContext * context, PHB_ITEM pValue, const char * szQuote, const char * szEsc )
{
   switch( hb_itemType( pValue ) )
   {
      case HB_IT_STRING:
      case HB_IT_MEMO:
      {
         HB_SIZE      nLen  = hb_itemGetCLen( pValue );
         HB_SIZE      nCnt  = 0;
         const char * szVal = hb_itemGetCPtr( pValue );

         if( ! addStrToContext( context, szQuote ) )
            return HB_FALSE;

         if( context->str_trim )
         {
            while( nLen && HB_ISSPACE( szVal[ nLen - 1 ] ) )
               nLen--;
         }

         while( *szVal && nCnt++ < nLen )
         {
            /* if( *szVal == *szDelim || *szVal == *szEsc || *szVal == *szQuote )
               we don't need to escape delim in CSV mode,
               only the quote and the escape itself */

            if( *szVal == *szQuote || *szVal == *szEsc )
               if( ! addToContext( context, *szEsc ) )
                  return HB_FALSE;
            if( ( HB_UCHAR ) *szVal >= 32 )
               if( ! addToContext( context, *szVal ) )
                  return HB_FALSE;
            szVal++;
         }
         if( ! addStrToContext( context, szQuote ) )
            return HB_FALSE;
         break;
      }

      case HB_IT_DATE:
      {
         char szDate[ 9 ];

         if( ! addStrToContext( context, szQuote ) )
            return HB_FALSE;
         hb_itemGetDS( pValue, szDate );
         if( szDate[ 0 ] == ' ' )
         {
            if( ! addStrToContext( context, "0100-01-01" ) )
               return HB_FALSE;
         }
         else
         {
            if( ! addStrnToContext( context, &szDate[ 0 ], 4 ) ||
                ! addToContext( context, '-' ) ||
                ! addStrnToContext( context, &szDate[ 4 ], 2 ) ||
                ! addToContext( context, '-' ) ||
                ! addStrnToContext( context, &szDate[ 6 ], 2 ) )
               return HB_FALSE;
         }
         if( ! addStrToContext( context, szQuote ) )
            return HB_FALSE;
         break;
      }

      case HB_IT_TIMESTAMP:
      {
         long lDate, lTime;
         char szDateTime[ 24 ];

         hb_itemGetTDT( pValue, &lDate, &lTime );
         hb_timeStampStr( szDateTime, lDate, lTime );
         if( ! addStrToContext( context, szQuote ) ||
             ! addStrToContext( context, szDateTime ) ||
             ! addStrToContext( context, szQuote ) )
            return HB_FALSE;
         break;
      }

      case HB_IT_LOGICAL:
#if 0
         if( ! addStrToContext( context, szQuote ) || ! addToContext( context, hb_itemGetL( pValue ) ? 'Y' : 'N' ) || ! addStrToContext( context, szQuote ) )
#endif
         if( ! addToContext( context, hb_itemGetL( pValue ) ? 'Y' : 'N' ) )
            return HB_FALSE;
         break;

      case HB_IT_INTEGER:
      case HB_IT_LONG:
      case HB_IT_DOUBLE:
      {
         char szResult[ HB_MAX_DOUBLE_LENGTH ];
         int  iSize, iWidth, iDec;

         hb_itemGetNLen( pValue, &iWidth, &iDec );
         iSize = ( iDec > 0 ? iWidth + 1 + iDec : iWidth );
         if( hb_itemStrBuf( szResult, pValue, iSize, iDec ) )
         {
            int iPos = 0;
            while( iSize && HB_ISSPACE( szResult[ iPos ] ) )
            {
               iPos++;
               iSize--;
            }
            if( ! addStrnToContext( context, &szResult[ iPos ], iSize ) )
               return HB_FALSE;
         }
         else
         if( ! addToContext( context, '0' ) )
            return HB_FALSE;
         break;
      }
      /* an "M" field or the other, might be a "V" in SixDriver */
      default:
         return HB_FALSE;
   }

   return HB_TRUE;
}
Beispiel #30
0
/*
 * 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;
}