Ejemplo n.º 1
0
Archivo: wafunc.c Proyecto: CsBela/core
HB_ERRCODE hb_dbTransStruct( AREAP lpaSource, AREAP lpaDest,
                             LPDBTRANSINFO lpdbTransInfo,
                             PHB_ITEM * pStruct, PHB_ITEM pFields )
{
   HB_USHORT uiFields, uiSize, uiCount, uiPosSrc, uiPosDst, uiSizeSrc, uiSizeDst;
   HB_ERRCODE errCode;
   const char * szField;
   HB_BOOL fAll;

   errCode = SELF_FIELDCOUNT( lpaSource, &uiSizeSrc );
   if( errCode != HB_SUCCESS )
      return errCode;

   if( lpaDest )
   {
      errCode = SELF_FIELDCOUNT( lpaDest, &uiSizeDst );
      if( errCode != HB_SUCCESS )
         return errCode;
      uiSize = HB_MIN( uiSizeDst, uiSizeSrc );
   }
   else
   {
      uiSize = uiSizeDst = uiSizeSrc;
   }

   if( ! uiSize )
      return HB_FAILURE;
   if( hb_itemType( pFields ) & HB_IT_ARRAY )
   {
      uiFields = ( HB_USHORT ) hb_arrayLen( pFields );
      if( uiFields )
         uiSize = uiFields;
   }
   else
      uiFields = 0;

   fAll = ( uiSizeDst == uiSizeSrc );

   lpdbTransInfo->lpaSource    = lpaSource;
   lpdbTransInfo->lpaDest      = lpaDest;
   lpdbTransInfo->lpTransItems = ( LPDBTRANSITEM )
                                    hb_xgrab( uiSize * sizeof( DBTRANSITEM ) );

   if( ! lpaDest )
   {
      *pStruct = hb_itemNew( NULL );
      hb_arrayNew( *pStruct, 0 );
   }

   if( uiFields == 0 )
   {
      if( lpaDest )
      {
         PHB_ITEM pItem = hb_itemNew( NULL );
         uiSize = 0;
         for( uiCount = 1; uiCount <= uiSizeSrc; ++uiCount )
         {
            if( SELF_FIELDINFO( lpaSource, uiCount, DBS_NAME, pItem ) != HB_SUCCESS )
            {
               uiSize = 0;
               break;
            }
            szField = hb_itemGetCPtr( pItem );
            uiPosDst = hb_rddFieldExpIndex( lpaDest, szField );
            if( uiPosDst != uiCount )
               fAll = HB_FALSE;
            if( uiPosDst )
            {
               HB_USHORT ui;

               /* check for replicated field names in source area */
               for( ui = 0; ui < uiSize; ++ui )
               {
                  if( lpdbTransInfo->lpTransItems[ ui ].uiDest == uiPosDst )
                     break;
               }
               if( ui == uiSize )
               {
                  lpdbTransInfo->lpTransItems[ uiSize ].uiSource = uiCount;
                  lpdbTransInfo->lpTransItems[ uiSize++ ].uiDest = uiPosDst;
               }
            }
         }
         hb_itemRelease( pItem );
      }
      else
      {
         hb_tblStructure( lpaSource, *pStruct, 0 );
         uiSize = ( HB_USHORT ) hb_arrayLen( *pStruct );
         for( uiCount = 0; uiCount < uiSize; ++uiCount )
         {
            lpdbTransInfo->lpTransItems[ uiCount ].uiSource =
            lpdbTransInfo->lpTransItems[ uiCount ].uiDest = uiCount + 1;
         }
      }
   }
   else
   {
      uiSize = 0;
      for( uiCount = 1; uiCount <= uiFields; ++uiCount )
      {
         szField = hb_dbTransFieldPos( pFields, uiCount );
         if( szField )
         {
            uiPosSrc = hb_rddFieldExpIndex( lpaSource, szField );
            if( ! uiPosSrc )
               continue;
            if( lpaDest )
               uiPosDst = hb_rddFieldExpIndex( lpaDest, szField );
            else
               uiPosDst = uiSize + 1;
            if( uiPosDst )
            {
               if( uiPosSrc != uiPosDst )
                  fAll = HB_FALSE;
               lpdbTransInfo->lpTransItems[ uiSize ].uiSource = uiPosSrc;
               lpdbTransInfo->lpTransItems[ uiSize++ ].uiDest = uiPosDst;
               if( ! lpaDest )
               {
                  hb_arraySize( *pStruct, uiSize );
                  hb_fldStructure( lpaSource, uiPosSrc, 0,
                                   hb_arrayGetItemPtr( *pStruct, uiSize ) );
               }
            }
         }
      }
   }

   if( uiSize != uiSizeSrc )
      fAll = HB_FALSE;

   if( fAll && lpaDest )
   {
      PHB_ITEM pSrcItm = hb_itemNew( NULL ),
               pDstItm = hb_itemNew( NULL );
      /*
       * if fAll is HB_TRUE here then it means that all fields are included
       * and they are on the same positions in both tables, so now check
       * if their types and sizes are also equal
       */
      for( uiCount = 1; uiCount <= uiSize; ++uiCount )
      {
         if( SELF_FIELDINFO( lpaSource, uiCount, DBS_TYPE, pSrcItm ) != HB_SUCCESS ||
             SELF_FIELDINFO( lpaDest,   uiCount, DBS_TYPE, pDstItm ) != HB_SUCCESS )
         {
            uiSize = 0;
            break;
         }
         if( hb_stricmp( hb_itemGetCPtr( pSrcItm ),
                         hb_itemGetCPtr( pDstItm ) ) != 0 )
         {
            fAll = HB_FALSE;
            break;
         }
         if( SELF_FIELDINFO( lpaSource, uiCount, DBS_LEN, pSrcItm ) != HB_SUCCESS ||
             SELF_FIELDINFO( lpaDest,   uiCount, DBS_LEN, pDstItm ) != HB_SUCCESS )
         {
            uiSize = 0;
            break;
         }
         if( hb_itemGetNL( pSrcItm ) != hb_itemGetNL( pDstItm ) )
         {
            fAll = HB_FALSE;
            break;
         }
         if( SELF_FIELDINFO( lpaSource, uiCount, DBS_DEC, pSrcItm ) != HB_SUCCESS ||
             SELF_FIELDINFO( lpaDest,   uiCount, DBS_DEC, pDstItm ) != HB_SUCCESS )
         {
            uiSize = 0;
            break;
         }
         if( hb_itemGetNL( pSrcItm ) != hb_itemGetNL( pDstItm ) )
         {
            fAll = HB_FALSE;
            break;
         }
#ifdef DBS_FLAG
         if( SELF_FIELDINFO( lpaSource, uiCount, DBS_FLAG, pSrcItm ) != HB_SUCCESS ||
             SELF_FIELDINFO( lpaDest,   uiCount, DBS_FLAG, pDstItm ) != HB_SUCCESS )
         {
            uiSize = 0;
            break;
         }
#endif
         if( hb_itemGetNL( pSrcItm ) != hb_itemGetNL( pDstItm ) )
         {
            fAll = HB_FALSE;
            break;
         }
      }
      hb_itemRelease( pSrcItm );
      hb_itemRelease( pDstItm );
   }

   lpdbTransInfo->uiFlags = fAll ? DBTF_MATCH : 0;
   lpdbTransInfo->uiItemCount = uiSize;

   return uiSize ? HB_SUCCESS : HB_FAILURE;
}
Ejemplo n.º 2
0
Archivo: eval.c Proyecto: CsBela/core
HB_BOOL hb_execFromArray( PHB_ITEM pParam )
{
   PHB_SYMB pExecSym = NULL;
   PHB_ITEM pArray = NULL;
   PHB_ITEM pSelf = NULL;
   HB_ULONG ulParamOffset = 0;
   int iPCount = 0;

   if( pParam && HB_IS_ARRAY( pParam ) && ! HB_IS_OBJECT( pParam ) )
   {
      pArray = pParam;
      pParam = hb_arrayGetItemPtr( pArray, 1 );
      if( HB_IS_OBJECT( pParam ) )
      {
         pSelf = pParam;
         pParam = hb_arrayGetItemPtr( pArray, 2 );
         ulParamOffset = 2;
      }
      else
         ulParamOffset = 1;
   }

   if( pParam )
   {
      if( HB_IS_SYMBOL( pParam ) )
         pExecSym = hb_itemGetSymbol( pParam );
      else if( HB_IS_STRING( pParam ) )
         pExecSym = hb_dynsymGet( hb_itemGetCPtr( pParam ) )->pSymbol;
      else if( HB_IS_BLOCK( pParam ) && ! pSelf )
      {
         pSelf = pParam;
         pExecSym = &hb_symEval;
      }

      if( pExecSym )
      {
         hb_vmPushSymbol( pExecSym );
         if( pSelf )
            hb_vmPush( pSelf );
         else
            hb_vmPushNil();

         if( pArray )
         {
            pParam = hb_arrayGetItemPtr( pArray, ++ulParamOffset );
            while( pParam && iPCount < 255 )
            {
               hb_vmPush( pParam );
               ++iPCount;
               pParam = hb_arrayGetItemPtr( pArray, ++ulParamOffset );
            }
         }

         if( pSelf )
            hb_vmSend( ( HB_USHORT ) iPCount );
         else
            hb_vmProc( ( HB_USHORT ) iPCount );

         return HB_TRUE;
      }
   }

   hb_errRT_BASE_SubstR( EG_ARG, 1099, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );

   return HB_FALSE;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
/*
 * Open a data store in the WorkArea.
 */
static HB_ERRCODE hb_sdfOpen( SDFAREAP pArea, LPDBOPENINFO pOpenInfo )
{
   PHB_ITEM pError = NULL;
   PHB_FNAME pFileName;
   HB_ERRCODE errCode;
   HB_USHORT uiFlags;
   HB_BOOL fRetry;
   char szFileName[ HB_PATH_MAX ];
   char szAlias[ HB_RDD_MAX_ALIAS_LEN + 1 ];

   HB_TRACE( HB_TR_DEBUG, ( "hb_sdfOpen(%p,%p)", pArea, pOpenInfo ) );

   pArea->fShared = HB_TRUE;     /* pOpenInfo->fShared; */
   pArea->fReadonly = HB_TRUE;   /* pOpenInfo->fReadonly; */

   if( pOpenInfo->cdpId )
   {
      pArea->area.cdPage = hb_cdpFindExt( pOpenInfo->cdpId );
      if( ! pArea->area.cdPage )
         pArea->area.cdPage = hb_vmCDP();
   }
   else
      pArea->area.cdPage = hb_vmCDP();

   uiFlags = ( pArea->fReadonly ? FO_READ : FO_READWRITE ) |
             ( pArea->fShared ? FO_DENYNONE : FO_EXCLUSIVE );

   pFileName = hb_fsFNameSplit( pOpenInfo->abName );
   /* Add default file name extension if necessary */
   if( hb_setGetDefExtension() && ! pFileName->szExtension )
   {
      PHB_ITEM pFileExt = hb_itemPutC( NULL, NULL );
      SELF_INFO( &pArea->area, DBI_TABLEEXT, pFileExt );
      pFileName->szExtension = hb_itemGetCPtr( pFileExt );
      hb_fsFNameMerge( szFileName, pFileName );
      hb_itemRelease( pFileExt );
   }
   else
   {
      hb_strncpy( szFileName, pOpenInfo->abName, sizeof( szFileName ) - 1 );
   }

   /* Create default alias if necessary */
   if( ! pOpenInfo->atomAlias && pFileName->szName )
   {
      const char * szName = strrchr( pFileName->szName, ':' );
      if( szName == NULL )
         szName = pFileName->szName;
      else
         ++szName;
      hb_strncpyUpperTrim( szAlias, szName, sizeof( szAlias ) - 1 );
      pOpenInfo->atomAlias = szAlias;
   }
   hb_xfree( pFileName );

   /* Try open */
   do
   {
      pArea->pFile = hb_fileExtOpen( szFileName, NULL, uiFlags |
                                     FXO_DEFAULTS | FXO_SHARELOCK |
                                     FXO_COPYNAME, NULL, pError );
      if( ! pArea->pFile )
      {
         if( ! pError )
         {
            pError = hb_errNew();
            hb_errPutGenCode( pError, EG_OPEN );
            hb_errPutSubCode( pError, EDBF_OPEN_DBF );
            hb_errPutOsCode( pError, hb_fsError() );
            hb_errPutDescription( pError, hb_langDGetErrorDesc( EG_OPEN ) );
            hb_errPutFileName( pError, szFileName );
            hb_errPutFlags( pError, EF_CANRETRY | EF_CANDEFAULT );
         }
         fRetry = ( SELF_ERROR( &pArea->area, pError ) == E_RETRY );
      }
      else
         fRetry = HB_FALSE;
   }
   while( fRetry );

   if( pError )
      hb_itemRelease( pError );

   if( ! pArea->pFile )
      return HB_FAILURE;

   errCode = SUPER_OPEN( &pArea->area, pOpenInfo );
   if( errCode != HB_SUCCESS )
   {
      SELF_CLOSE( &pArea->area );
      return HB_FAILURE;
   }

   hb_sdfInitArea( pArea, szFileName );

   /* Position cursor at the first record */
   return SELF_GOTOP( &pArea->area );
}
Ejemplo n.º 5
0
/*
 * Assign a value to a field.
 */
static HB_ERRCODE hb_sdfPutValue( SDFAREAP pArea, HB_USHORT uiIndex, PHB_ITEM pItem )
{
   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 )
   {
      char szBuffer[ 256 ];

      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;
}
Ejemplo n.º 6
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.º 7
0
/*
 * Create a data store in the specified WorkArea.
 */
static HB_ERRCODE hb_sdfCreate( SDFAREAP pArea, LPDBOPENINFO pCreateInfo )
{
   PHB_ITEM pError = NULL;
   HB_ERRCODE errCode;
   HB_BOOL fRetry;
   PHB_FNAME pFileName;
   char szFileName[ HB_PATH_MAX ];

   HB_TRACE( HB_TR_DEBUG, ( "hb_sdfCreate(%p,%p)", pArea, pCreateInfo ) );

   pArea->fShared = HB_FALSE;    /* pCreateInfo->fShared; */
   pArea->fReadonly = HB_FALSE;  /* pCreateInfo->fReadonly */

   if( pCreateInfo->cdpId )
   {
      pArea->area.cdPage = hb_cdpFindExt( pCreateInfo->cdpId );
      if( ! pArea->area.cdPage )
         pArea->area.cdPage = hb_vmCDP();
   }
   else
      pArea->area.cdPage = hb_vmCDP();

   pFileName = hb_fsFNameSplit( pCreateInfo->abName );
   if( hb_setGetDefExtension() && ! pFileName->szExtension )
   {
      PHB_ITEM pItem = hb_itemPutC( NULL, NULL );
      SELF_INFO( &pArea->area, DBI_TABLEEXT, pItem );
      pFileName->szExtension = hb_itemGetCPtr( pItem );
      hb_fsFNameMerge( szFileName, pFileName );
      hb_itemRelease( pItem );
   }
   else
   {
      hb_strncpy( szFileName, pCreateInfo->abName, sizeof( szFileName ) - 1 );
   }
   hb_xfree( pFileName );

   /* Try create */
   do
   {
      pArea->pFile = hb_fileExtOpen( szFileName, NULL,
                                     FO_READWRITE | FO_EXCLUSIVE | FXO_TRUNCATE |
                                     FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME,
                                     NULL, pError );
      if( ! pArea->pFile )
      {
         if( ! pError )
         {
            pError = hb_errNew();
            hb_errPutGenCode( pError, EG_CREATE );
            hb_errPutSubCode( pError, EDBF_CREATE_DBF );
            hb_errPutOsCode( pError, hb_fsError() );
            hb_errPutDescription( pError, hb_langDGetErrorDesc( EG_CREATE ) );
            hb_errPutFileName( pError, szFileName );
            hb_errPutFlags( pError, EF_CANRETRY | EF_CANDEFAULT );
         }
         fRetry = ( SELF_ERROR( &pArea->area, pError ) == E_RETRY );
      }
      else
         fRetry = HB_FALSE;
   }
   while( fRetry );

   if( pError )
      hb_itemRelease( pError );

   if( ! pArea->pFile )
      return HB_FAILURE;

   errCode = SUPER_CREATE( &pArea->area, pCreateInfo );
   if( errCode != HB_SUCCESS )
   {
      SELF_CLOSE( &pArea->area );
      return errCode;
   }

   hb_sdfInitArea( pArea, szFileName );
   pArea->ulRecNo = 1;
   pArea->area.fEof = HB_TRUE;
   pArea->fPositioned = HB_FALSE;
   hb_sdfClearRecordBuffer( pArea );

   return HB_SUCCESS;
}
Ejemplo n.º 8
0
static HB_UINT SCItm( char * cBuffer, HB_UINT ulMaxBuf, const char * cParFrm, int iCOut, int IsIndW,
                      int iIndWidth, int IsIndP, int iIndPrec,
                      PHB_ITEM pItmPar )
{
   HB_UINT s;

   if( IsIndW && IsIndP )
   {
      switch( iCOut )
      {
         case 'p':
            s = hb_snprintf( cBuffer, ulMaxBuf, cParFrm, iIndWidth, iIndPrec, hb_itemGetPtr( pItmPar ) );
            break;
         case 's': case 'S':
            s = hb_snprintf( cBuffer, ulMaxBuf, cParFrm, iIndWidth, iIndPrec, hb_itemGetCPtr( pItmPar ) );
            break;
         case 'e': case 'E': case 'f': case 'g': case 'G': case 'a': case 'A':
            s = hb_snprintf( cBuffer, ulMaxBuf, cParFrm, iIndWidth, iIndPrec, hb_itemGetND( pItmPar ) );
            break;
         /* case 'c': case 'C': case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': */
         default:
            s = hb_snprintf( cBuffer, ulMaxBuf, cParFrm, iIndWidth, iIndPrec,
                             HB_IS_LONG( pItmPar ) ? hb_itemGetNL( pItmPar ) : hb_itemGetNI( pItmPar ) );
      }
   }
   else if( IsIndW || IsIndP )
   {
      int iInd = ( IsIndW ? iIndWidth : iIndPrec );

      switch( iCOut )
      {
         case 'p':
            s = hb_snprintf( cBuffer, ulMaxBuf, cParFrm, iInd, hb_itemGetPtr( pItmPar ) );
            break;
         case 's': case 'S':
            s = hb_snprintf( cBuffer, ulMaxBuf, cParFrm, iInd, hb_itemGetCPtr( pItmPar ) );
            break;
         case 'e': case 'E': case 'f': case 'g': case 'G': case 'a': case 'A':
            s = hb_snprintf( cBuffer, ulMaxBuf, cParFrm, iInd, hb_itemGetND( pItmPar ) );
            break;
         /* case 'c': case 'C': case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': */
         default:
            s = hb_snprintf( cBuffer, ulMaxBuf, cParFrm, iInd,
                             HB_IS_LONG( pItmPar ) ? hb_itemGetNL( pItmPar ) : hb_itemGetNI( pItmPar ) );
      }
   }
   else
   {
      switch( iCOut )
      {
         case 'p':
            s = hb_snprintf( cBuffer, ulMaxBuf, cParFrm, hb_itemGetPtr( pItmPar ) );
            break;
         case 's': case 'S':
            s = hb_snprintf( cBuffer, ulMaxBuf, cParFrm, hb_itemGetCPtr( pItmPar ) );
            break;
         case 'e': case 'E': case 'f': case 'g': case 'G': case 'a': case 'A':
            s = hb_snprintf( cBuffer, ulMaxBuf, cParFrm, hb_itemGetND( pItmPar ) );
            break;
         /* case 'c': case 'C': case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': */
         default:
            s = hb_snprintf( cBuffer, ulMaxBuf, cParFrm,
                             HB_IS_LONG( pItmPar ) ? hb_itemGetNL( pItmPar ) : hb_itemGetNI( pItmPar ) );
      }
   }
   return s;
}
Ejemplo n.º 9
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 * ) LoadLibrary( HB_ITEMGETSTR( pLibName, &hFileName, NULL ) );

            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() ) );
         }
#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;
}
Ejemplo n.º 10
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.º 11
0
Archivo: adsx.c Proyecto: SBCamus/core
static PMIXTAG mixTagCreate( const char * szTagName, PHB_ITEM pKeyExpr, PHB_ITEM pKeyItem,
                             PHB_ITEM pForItem, PHB_ITEM pWhileItem, HB_BYTE bType,
                             HB_USHORT uiLen, ADSXAREAP pArea )
{
   PMIXTAG             pTag;
   PMIXKEY             pKey;
   LPDBORDERCONDINFO   pOrdCondInfo = pArea->adsarea.area.lpdbOrdCondInfo;
   ADSHANDLE           hOrder;
   HB_ULONG            ulRec, ulStartRec, ulNextCount = 0;
   HB_LONG             lStep = 0;
   PHB_ITEM            pItem, pEvalItem = NULL;


   pTag = ( PMIXTAG ) hb_xgrab( sizeof( MIXTAG ) );
   memset( pTag, 0, sizeof( MIXTAG ) );

   pTag->szName = ( char * ) hb_xgrab( MIX_MAXTAGNAMELEN + 1 );
   hb_strncpyUpperTrim( pTag->szName, szTagName, MIX_MAXTAGNAMELEN );

   pTag->szKeyExpr = ( char * ) hb_xgrab( hb_itemGetCLen( pKeyExpr ) + 1 );
   hb_strncpyTrim( pTag->szKeyExpr, hb_itemGetCPtr( pKeyExpr ), hb_itemGetCLen( pKeyExpr ) );

   /* TODO: for expresion */
   pTag->szForExpr = NULL;

   pTag->pKeyItem = pKeyItem;
   pTag->pForItem = pForItem;
   pTag->bType    = bType;
   pTag->uiLen    = uiLen;

   /* Use national support */
   if( bType == 'C' && pArea->adsarea.area.cdPage &&
       ! HB_CDP_ISBINSORT( pArea->adsarea.area.cdPage ) )
      pTag->pCodepage = pArea->adsarea.area.cdPage;

   pTag->pKeys    = ( PMIXKEY * ) hb_xgrab( sizeof( PMIXKEY ) * MIX_KEYPOOLFIRST );
   pTag->ulRecMax = MIX_KEYPOOLFIRST;

   ulStartRec = 0;


   if( pOrdCondInfo )
   {
      pEvalItem = pOrdCondInfo->itmCobEval;
      lStep = pOrdCondInfo->lStep;
   }

   if( ! pOrdCondInfo || pOrdCondInfo->fAll )
   {
      pArea->adsarea.hOrdCurrent = 0;
   }
   else
   {
      if( pOrdCondInfo->itmRecID )
         ulStartRec = hb_itemGetNL( pOrdCondInfo->itmRecID );

      if( ulStartRec )
      {
         ulNextCount = 1;
      }
      else if( pOrdCondInfo->fRest || pOrdCondInfo->lNextCount > 0 )
      {
         if( pOrdCondInfo->itmStartRecID )
            ulStartRec = hb_itemGetNL( pOrdCondInfo->itmStartRecID );

         if( ! ulStartRec )
            ulStartRec = pArea->adsarea.ulRecNo;

         if( pArea->adsarea.area.lpdbOrdCondInfo->lNextCount > 0 )
            ulNextCount = pOrdCondInfo->lNextCount;
      }
      else if( ! pOrdCondInfo->fUseCurrent )
      {
         pArea->adsarea.hOrdCurrent = 0;
      }
   }

   hOrder = pArea->adsarea.hOrdCurrent ? pArea->adsarea.hOrdCurrent :
                                         pArea->adsarea.hTable;

   if( ulStartRec )
      AdsGotoRecord( pArea->adsarea.hTable, ulStartRec );
   else
      AdsGotoTop( pArea->adsarea.hTable );
   hb_adsUpdateAreaFlags( pArea );

   while( ! pArea->adsarea.area.fEof )
   {
      SELF_RECNO( ( AREAP ) pArea, &ulRec );
      SELF_GOTO( ( AREAP ) pArea, ulRec );

      if( pEvalItem )
      {
         if( lStep >= pOrdCondInfo->lStep )
         {
            lStep = 0;
            if( ! mixEvalCond( pEvalItem, NULL ) )
               break;
         }
         ++lStep;
      }

      if( pWhileItem && ! mixEvalCond( pWhileItem, NULL ) )
      {
         break;
      }

      if( pForItem == NULL || mixEvalCond( pForItem, NULL ) )
      {
         pItem = hb_vmEvalBlockOrMacro( pKeyItem );

         pKey = mixKeyNew( pItem, ulRec, bType, uiLen );

         if( pTag->ulRecCount == pTag->ulRecMax )
         {
            pTag->pKeys = ( PMIXKEY * ) hb_xrealloc( pTag->pKeys, sizeof( PMIXKEY ) * ( pTag->ulRecMax + MIX_KEYPOOLRESIZE ) );
            pTag->ulRecMax += MIX_KEYPOOLRESIZE;
         }

         pTag->pKeys[ pTag->ulRecCount ] = pKey;
         pTag->ulRecCount++;
      }

      if( ulNextCount )
      {
         ulNextCount--;
         if( ! ulNextCount )
            break;
      }

      AdsSkip( hOrder, 1 );
      hb_adsUpdateAreaFlags( pArea );
   }

   /* QuickSort */
   if( pTag->ulRecCount >= 2 )
      mixQSort( pTag->pKeys, 0, pTag->ulRecCount - 1, uiLen, pTag->pCodepage );

   return pTag;
}
Ejemplo n.º 12
0
Archivo: adsx.c Proyecto: SBCamus/core
static HB_ERRCODE adsxOrderCreate( ADSXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo )
{
   PMIXTAG    pTagNew, pTag;
   PHB_ITEM   pKeyItem, pForItem = NULL, pWhileItem = NULL, pResult;
   HB_ULONG   ulRecNo;
   HB_USHORT  uiLen;
   HB_BYTE    bType;
   UNSIGNED16 bValidExpr;
   HB_BOOL    bUseADS;

   /* Test key expression */
   bValidExpr = 0;
   AdsIsExprValid( pArea->adsarea.hTable, ( UNSIGNED8 * ) hb_itemGetCPtr( pOrderInfo->abExpr ), &bValidExpr );
   bUseADS = bValidExpr;

   if( bUseADS && pArea->adsarea.area.lpdbOrdCondInfo )
   {
      /* Test FOR expression */
      if( pArea->adsarea.area.lpdbOrdCondInfo->abFor )
      {
         bValidExpr = 0;
         AdsIsExprValid( pArea->adsarea.hTable, ( UNSIGNED8 * ) pArea->adsarea.area.lpdbOrdCondInfo->abFor, &bValidExpr );
         bUseADS = bValidExpr;
      }
      else if( pArea->adsarea.area.lpdbOrdCondInfo->itmCobFor )
      {
         bUseADS = HB_FALSE;
      }

      /* Test WHILE expression */
      if( bUseADS )
      {
         if( pArea->adsarea.area.lpdbOrdCondInfo->abWhile )
         {
            bValidExpr = 0;
            AdsIsExprValid( pArea->adsarea.hTable, ( UNSIGNED8 * ) pArea->adsarea.area.lpdbOrdCondInfo->abWhile, &bValidExpr );
            bUseADS = ( bValidExpr != 0 );
         }
         else if( pArea->adsarea.area.lpdbOrdCondInfo->itmCobWhile )
            bUseADS = HB_FALSE;
      }
   }

   if( bUseADS )
   {
      return SUPER_ORDCREATE( ( AREAP ) pArea, pOrderInfo );
   }

   /* Obtain key codeblock */
   if( pOrderInfo->itmCobExpr )
   {
      pKeyItem = hb_itemNew( pOrderInfo->itmCobExpr );
   }
   else
   {
      if( SELF_COMPILE( ( AREAP ) pArea, hb_itemGetCPtr( pOrderInfo->abExpr ) ) == HB_FAILURE )
         return HB_FAILURE;
      pKeyItem = pArea->adsarea.area.valResult;
      pArea->adsarea.area.valResult = NULL;
   }

   /* Test key codeblock on EOF */
   ulRecNo = pArea->adsarea.ulRecNo;
   SELF_GOTO( ( AREAP ) pArea, 0 );
   if( SELF_EVALBLOCK( ( AREAP ) pArea, pKeyItem ) == HB_FAILURE )
   {
      hb_vmDestroyBlockOrMacro( pKeyItem );
      SELF_GOTO( ( AREAP ) pArea, ulRecNo );
      return HB_FAILURE;
   }

   pResult = pArea->adsarea.area.valResult;
   pArea->adsarea.area.valResult = NULL;

   switch( hb_itemType( pResult ) )
   {
      case HB_IT_STRING:
      case HB_IT_STRING | HB_IT_MEMO:
         bType = 'C';
         uiLen = ( HB_USHORT ) hb_itemGetCLen( pResult );
         if( uiLen > MIX_MAXKEYLEN )
            uiLen = MIX_MAXKEYLEN;
         break;

      case HB_IT_INTEGER:
      case HB_IT_LONG:
      case HB_IT_DOUBLE:
         bType = 'N';
         uiLen = 8;
         break;

      case HB_IT_DATE:
         bType = 'D';
         uiLen = 8;
         break;

      case HB_IT_LOGICAL:
         bType = 'L';
         uiLen = 1;
         break;

      default:
         bType = 'U';
         uiLen = 0;
   }
   hb_itemRelease( pResult );

   if( bType == 'U' || uiLen == 0 )
   {
      hb_vmDestroyBlockOrMacro( pKeyItem );
      SELF_GOTO( ( AREAP ) pArea, ulRecNo );
      hb_mixErrorRT( pArea, bType == 'U' ? EG_DATATYPE : EG_DATAWIDTH, 1026, NULL, 0, 0 );
      return HB_FAILURE;
   }

   if( pArea->adsarea.area.lpdbOrdCondInfo )
   {
      /* Obtain FOR codeblock */
      if( pArea->adsarea.area.lpdbOrdCondInfo->itmCobFor )
      {
         pForItem = hb_itemNew( pArea->adsarea.area.lpdbOrdCondInfo->itmCobFor );
      }
      else if( pArea->adsarea.area.lpdbOrdCondInfo->abFor )
      {
         if( SELF_COMPILE( ( AREAP ) pArea, pArea->adsarea.area.lpdbOrdCondInfo->abFor ) == HB_FAILURE )
         {
            hb_vmDestroyBlockOrMacro( pKeyItem );
            SELF_GOTO( ( AREAP ) pArea, ulRecNo );
            return HB_FAILURE;
         }
         pForItem = pArea->adsarea.area.valResult;
         pArea->adsarea.area.valResult = NULL;
      }

      /* Obtain WHILE codeblock */
      if( pArea->adsarea.area.lpdbOrdCondInfo->itmCobWhile )
      {
         pWhileItem = hb_itemNew( pArea->adsarea.area.lpdbOrdCondInfo->itmCobWhile );
      }
      else if( pArea->adsarea.area.lpdbOrdCondInfo->abWhile )
      {
         if( SELF_COMPILE( ( AREAP ) pArea, pArea->adsarea.area.lpdbOrdCondInfo->abWhile ) == HB_FAILURE )
         {
            hb_vmDestroyBlockOrMacro( pKeyItem );
            if( pForItem )
               hb_vmDestroyBlockOrMacro( pForItem );
            SELF_GOTO( ( AREAP ) pArea, ulRecNo );
            return HB_FAILURE;
         }
         pWhileItem = pArea->adsarea.area.valResult;
         pArea->adsarea.area.valResult = NULL;
      }
   }

   /* Test FOR codeblock on EOF */
   if( pForItem )
   {
      if( SELF_EVALBLOCK( ( AREAP ) pArea, pForItem ) == HB_FAILURE )
      {
         hb_vmDestroyBlockOrMacro( pKeyItem );
         hb_vmDestroyBlockOrMacro( pForItem );
         if( pWhileItem )
            hb_vmDestroyBlockOrMacro( pWhileItem );
         SELF_GOTO( ( AREAP ) pArea, ulRecNo );
         return HB_FAILURE;
      }
      if( hb_itemType( pArea->adsarea.area.valResult ) != HB_IT_LOGICAL )
      {
         hb_itemRelease( pArea->adsarea.area.valResult );
         pArea->adsarea.area.valResult = 0;
         hb_vmDestroyBlockOrMacro( pKeyItem );
         hb_vmDestroyBlockOrMacro( pForItem );
         if( pWhileItem )
            hb_vmDestroyBlockOrMacro( pWhileItem );
         SELF_GOTO( ( AREAP ) pArea, ulRecNo );
         hb_mixErrorRT( pArea, EG_DATATYPE, EDBF_INVALIDFOR, NULL, 0, 0 );
         return HB_FAILURE;
      }
      hb_itemRelease( pArea->adsarea.area.valResult );
      pArea->adsarea.area.valResult = NULL;
   }

   /* TODO: WHILE condition is not tested, like in DBFCDX. Why? Compatibility with Clipper? */

   SELF_GOTO( ( AREAP ) pArea, ulRecNo );

   pTagNew = mixTagCreate( pOrderInfo->atomBagName, pOrderInfo->abExpr, pKeyItem,
                           pForItem, pWhileItem, bType, uiLen, pArea );

   if( pWhileItem )
      hb_vmDestroyBlockOrMacro( pWhileItem );

   /* Append the tag to the end of list */
   if( pArea->pTagList )
   {
      pTag = pArea->pTagList;
      while( pTag->pNext )
         pTag = pTag->pNext;
      pTag->pNext = pTagNew;
   }
   else
   {
      pArea->pTagList = pTagNew;
   }

   pArea->pTagCurrent = pTagNew;
   pArea->adsarea.hOrdCurrent = 0;

   return HB_SUCCESS;
}
Ejemplo n.º 13
0
Archivo: core.c Proyecto: UIKit0/core
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", iNameLen ) );
#endif

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

      PHB_ITEM pName;

      int iDataType;
      int iSize;
      int iDec;

      pName = S_HB_ITEMPUTSTR( NULL, sqlite3_column_name( st, uiIndex ) );
      pFieldInfo.atomName = hb_itemGetCPtr( pName );

      iDataType = sqlite3_column_type( st, uiIndex );

      iSize = sqlite3_column_bytes( st, uiIndex );
      iDec  = 0;

      pFieldInfo.uiLen = ( HB_USHORT ) iSize;
      pFieldInfo.uiDec = ( HB_USHORT ) iDec;

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

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

         case SQLITE_FLOAT:
         case SQLITE_INTEGER:
            pFieldInfo.uiType = HB_FT_LONG;
            break;

         case SQLITE_BLOB:
            pFieldInfo.uiType = HB_FT_BLOB;
            break;

         case SQLITE_NULL:
            pFieldInfo.uiType = HB_FT_ANY;
            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 = ( 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;

            case HB_FT_ANY:
               pItem  = hb_itemNew( NULL );
               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_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->pStmt = st;
   return HB_SUCCESS;
}
Ejemplo n.º 14
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_retni( iSent > 0 ? iSent : iLen );
   }
}
Ejemplo n.º 15
0
Archivo: mzip.c Proyecto: CsBela/core
static int hb_unzipExtractCurrentFile( unzFile hUnzip, const char * szFileName, const char * szPassword )
{
   char szNameRaw[ HB_PATH_MAX * 3 ];
   char *        szName;
   HB_SIZE       nPos, nLen;
   char          cSep, * pString;
   unz_file_info ufi;
   int        iResult;
   HB_FHANDLE hFile;

   iResult = unzGetCurrentFileInfo( hUnzip, &ufi, szNameRaw, sizeof( szNameRaw ) - 1,
                                    NULL, 0, NULL, 0 );
   if( iResult != UNZ_OK )
      return iResult;

   iResult = unzOpenCurrentFilePassword( hUnzip, szPassword );

   if( iResult != UNZ_OK )
      return iResult;

   if( szFileName )
      szName = hb_strdup( szFileName );
   else
   {
      HB_BOOL fUnicode = ( ufi.flag & _ZIP_FLAG_UNICODE ) != 0;

      if( fUnicode )
      {
         PHB_ITEM pTemp = hb_itemPutStrUTF8( NULL, szNameRaw );

         szName = hb_strdup( hb_itemGetCPtr( pTemp ) );

         hb_itemRelease( pTemp );
      }
      else
         szName = hb_strdup( szNameRaw );
   }

   nLen = strlen( szName );

   /* Test shows that files in subfolders can be stored to zip file without
      explicitly adding folder. So, let's create a required path */

   nPos = 1;
   while( nPos < nLen )
   {
      cSep = szName[ nPos ];

      /* allow both path separators, ignore terminating path separator */
      if( ( cSep == '\\' || cSep == '/' ) && nPos < nLen - 1 )
      {
         szName[ nPos ] = '\0';
         hb_fsMkDir( szName );
         szName[ nPos ] = cSep;
      }
      nPos++;
   }

   if( ufi.external_fa & 0x40000000 ) /* DIRECTORY */
   {
      hb_fsMkDir( szName );
      iResult = UNZ_OK;
   }
   else
   {
      hFile = hb_fsCreate( szName, FC_NORMAL );

      if( hFile != FS_ERROR )
      {
         pString = ( char * ) hb_xgrab( HB_Z_IOBUF_SIZE );

         while( ( iResult = unzReadCurrentFile( hUnzip, pString, HB_Z_IOBUF_SIZE ) ) > 0 )
            hb_fsWriteLarge( hFile, pString, ( HB_SIZE ) iResult );

         hb_xfree( pString );

#if defined( HB_OS_WIN )
         {
            FILETIME   ftutc, ft;
            SYSTEMTIME st;

            st.wSecond       = ( WORD ) ufi.tmu_date.tm_sec;
            st.wMinute       = ( WORD ) ufi.tmu_date.tm_min;
            st.wHour         = ( WORD ) ufi.tmu_date.tm_hour;
            st.wDay          = ( WORD ) ufi.tmu_date.tm_mday;
            st.wMonth        = ( WORD ) ufi.tmu_date.tm_mon + 1;
            st.wYear         = ( WORD ) ufi.tmu_date.tm_year;
            st.wMilliseconds = 0;

            if( SystemTimeToFileTime( &st, &ft ) &&
                LocalFileTimeToFileTime( &ft, &ftutc ) )
            {
               SetFileTime( ( HANDLE ) hb_fsGetOsHandle( hFile ), &ftutc, &ftutc, &ftutc );
            }
         }
#endif

         hb_fsClose( hFile );
      }
      else
         iResult = -200 - hb_fsError();
   }
   unzCloseCurrentFile( hUnzip );

#if defined( HB_OS_WIN )
   {
      LPTSTR  lpFileNameFree;
      LPCTSTR lpFileName = HB_FSNAMECONV( szName, &lpFileNameFree );

      SetFileAttributes( ( LPCTSTR ) lpFileName, ufi.external_fa & 0xFF );

      if( lpFileNameFree )
         hb_xfree( lpFileNameFree );
   }
#elif defined( HB_OS_UNIX ) || defined( __DJGPP__ )
   {
      struct utimbuf utim;
      struct tm      st;
      time_t         tim;

      char *       pszFree;
      const char * szNameOS = hb_fsNameConv( szName, &pszFree );

#  if defined( __DJGPP__ )
      _chmod( szNameOS, 1, ufi.external_fa & 0xFF );
#  else
      HB_FATTR ulAttr = ufi.external_fa;

      if( ( ulAttr & 0xFFFF0000 ) == 0 )
         ulAttr = hb_translateExtAttr( szName, ulAttr );

      chmod( szNameOS,
             ( ( ulAttr & 0x00010000 ) ? S_IXOTH : 0 ) |
             ( ( ulAttr & 0x00020000 ) ? S_IWOTH : 0 ) |
             ( ( ulAttr & 0x00040000 ) ? S_IROTH : 0 ) |
             ( ( ulAttr & 0x00080000 ) ? S_IXGRP : 0 ) |
             ( ( ulAttr & 0x00100000 ) ? S_IWGRP : 0 ) |
             ( ( ulAttr & 0x00200000 ) ? S_IRGRP : 0 ) |
             ( ( ulAttr & 0x00400000 ) ? S_IXUSR : 0 ) |
             ( ( ulAttr & 0x00800000 ) ? S_IWUSR : 0 ) |
             ( ( ulAttr & 0x01000000 ) ? S_IRUSR : 0 ) );
#  endif
      memset( &st, 0, sizeof( st ) );

      st.tm_sec  = ufi.tmu_date.tm_sec;
      st.tm_min  = ufi.tmu_date.tm_min;
      st.tm_hour = ufi.tmu_date.tm_hour;
      st.tm_mday = ufi.tmu_date.tm_mday;
      st.tm_mon  = ufi.tmu_date.tm_mon;
      st.tm_year = ufi.tmu_date.tm_year - 1900;

      tim = mktime( &st );
#  if defined( HB_HAS_LOCALTIME_R )
      gmtime_r( &tim, &st );
#  else
      st = *gmtime( &tim );
#  endif
      utim.actime = utim.modtime = mktime( &st );

      utime( szNameOS, &utim );

      if( pszFree )
         hb_xfree( pszFree );
   }
#elif defined( HB_OS_DOS )
   {
#  if defined( __RSX32__ ) || defined( __GNUC__ )
      char * pszFree;

      _chmod( hb_fsNameConv( szName, &pszFree ), 1, ufi.external_fa & 0xFF );

      if( pszFree )
         hb_xfree( pszFree );
#  else
      hb_fsSetAttr( szName, ufi.external_fa & 0xFF );
#  endif
   }

#elif defined( HB_OS_OS2 )
   {
      FILESTATUS3 fs3;
      APIRET      ulrc;
      HB_FATTR    ulAttr = FILE_NORMAL;
      int         iAttr  = ufi.external_fa & 0xFF;

      char *       pszFree;
      const char * szNameOS = hb_fsNameConv( szName, &pszFree );

      if( iAttr & HB_FA_READONLY )
         ulAttr |= FILE_READONLY;
      if( iAttr & HB_FA_HIDDEN )
         ulAttr |= FILE_HIDDEN;
      if( iAttr & HB_FA_SYSTEM )
         ulAttr |= FILE_SYSTEM;
      if( iAttr & HB_FA_ARCHIVE )
         ulAttr |= FILE_ARCHIVED;

      ulrc = DosQueryPathInfo( ( PCSZ ) szNameOS, FIL_STANDARD, &fs3, sizeof( fs3 ) );

      if( ulrc == NO_ERROR )
      {
         FDATE fdate;
         FTIME ftime;

         fdate.year    = ufi.tmu_date.tm_year - 1980;
         fdate.month   = ufi.tmu_date.tm_mon;
         fdate.day     = ufi.tmu_date.tm_mday;
         ftime.hours   = ufi.tmu_date.tm_hour;
         ftime.minutes = ufi.tmu_date.tm_min;
         ftime.twosecs = ufi.tmu_date.tm_sec / 2;

         fs3.attrFile = ulAttr;

         fs3.fdateCreation = fs3.fdateLastAccess = fs3.fdateLastWrite = fdate;
         fs3.ftimeCreation = fs3.ftimeLastAccess = fs3.ftimeLastWrite = ftime;
         ulrc = DosSetPathInfo( ( PCSZ ) szNameOS, FIL_STANDARD,
                                &fs3, sizeof( fs3 ), DSPI_WRTTHRU );
      }

      if( pszFree )
         hb_xfree( pszFree );
   }
#else
   {
      hb_fsSetAttr( szName, ufi.external_fa );
   }
#endif

   hb_xfree( szName );

   return iResult;
}
Ejemplo n.º 16
0
static void hb_compGenArgList( int iFirst, int iLast,
                               int * pArgC, const char *** pArgV,
                               PHB_ITEM * pIncItem,
                               PHB_PP_OPEN_FUNC * pOpenFunc,
                               PHB_PP_MSG_FUNC * pMsgFunc )
{
   PHB_ITEM pParam;
   HB_SIZE ul, nLen;
   int argc = 1, i;
   const char ** argv;

   if( pMsgFunc )
   {
      *pMsgFunc = NULL;
      if( HB_ISLOG( iFirst ) )
      {
         if( hb_parl( iFirst ) )
            *pMsgFunc = s_pp_msg;
         ++iFirst;
      }
   }

   if( pIncItem && pOpenFunc )
   {
      *pOpenFunc = NULL;
      *pIncItem = hb_param( iFirst, HB_IT_HASH );
      if( *pIncItem )
      {
         ++iFirst;
         *pOpenFunc = s_pp_openFile;
      }
   }

   for( i = iFirst; i <= iLast; ++i )
   {
      pParam = hb_param( i, HB_IT_ARRAY | HB_IT_STRING );
      if( pParam )
      {
         if( HB_IS_ARRAY( pParam ) )
         {
            ul = hb_arrayLen( pParam );
            if( ul )
            {
               do
               {
                  if( hb_arrayGetType( pParam, ul ) & HB_IT_STRING )
                     ++argc;
               }
               while( --ul );
            }
         }
         else if( HB_IS_STRING( pParam ) )
            ++argc;
      }
   }

   argv = ( const char ** ) hb_xgrab( sizeof( char * ) * ( argc + 1 ) );
   argc = 0;
   for( i = iFirst; i <= iLast; ++i )
   {
      pParam = hb_param( i, HB_IT_ARRAY | HB_IT_STRING );
      if( pParam )
      {
         if( HB_IS_ARRAY( pParam ) )
         {
            nLen = hb_arrayLen( pParam );
            for( ul = 1; ul <= nLen; ++ul )
            {
               if( hb_arrayGetType( pParam, ul ) & HB_IT_STRING )
                  argv[ argc++ ] = hb_arrayGetCPtr( pParam, ul );
            }
         }
         else if( HB_IS_STRING( pParam ) )
            argv[ argc++ ] = hb_itemGetCPtr( pParam );
      }
   }
   argv[ argc ] = NULL;

   *pArgC = argc;
   *pArgV = argv;
}