// Copyright (c) 1993-2005 "Компания БЭСТ". Все права сохраняются.
//
// Функции которых нет в RDD
// и не только
//
#pragma BEGINDUMP
#ifdef __PLATFORM__Linux
#else
#include <windows.h>
#include <wincon.h>
#include <winnt.h>
#include <winioctl.h>
#include <iphlpapi.h>
#endif

#include "hbapi.h"
#include "hbapifs.h"
#include "hbapigt.h"
#include "hbapifs.h"
#include "hbapiitm.h"

#include "hbapierr.h"

#include "hbrddwrk.h"
#include "hbapirdd.h"
#include "hbrddcdx.h"

#include "hbvm.h"

#include "hbstack.h"

#define HB_ITEM_NEW(hb)  HB_ITEM hb = HB_ITEM_NIL

static char *hb_strTrim( char * szStr)
{
   char * szStrTrim ;
   ULONG ulLen =  hb_strRTrimLen( (char*) szStr, strlen( (char*) szStr ), FALSE );
   szStr = (BYTE*) hb_strLTrim( (char*) szStr, &ulLen );
   szStrTrim = (char * ) hb_xgrab( ulLen + 1 );
   memcpy( (char*) szStrTrim, (char*) szStr, ulLen );
   szStrTrim[ulLen] = '\0';
   return szStrTrim;
}

HB_FUNC( UPPERTRIM )
{
PHB_ITEM pName = hb_param( 1, HB_IT_STRING  );
if ( pName )
  {
      char *szName = hb_strUpperCopy( pName->item.asString.value, pName->item.asString.length );
      szName = hb_strTrim( szName );
      hb_retclen( szName, strlen( szName ) );
      hb_xfree(szName);
 }
 else
  {
   hb_retc("");
  }
}

HB_FUNC( LOWERTRIM )
{
PHB_ITEM pName = hb_param( 1, HB_IT_STRING  );
if ( pName )
  {
      char *szName = hb_strLowerCopy( pName->item.asString.value, pName->item.asString.length );
      szName = hb_strTrim( szName );
      hb_retclen( szName, strlen( szName ) );
      hb_xfree(szName);
 }
 else
  {
   hb_retc("");
  }
}

HB_FUNC( DBRELATIONLIST )
{
AREAP pArea = hb_rddGetCurrentWorkAreaPointer();
PHB_ITEM pArray = hb_itemArrayNew( 0 );
   if ( pArea && pArea->lpdbRelations )
   {
      AREAP childArea;
      USHORT uiPrevArea = hb_rddGetCurrentWorkAreaNumber();
      HB_ITEM SubArray;
      LPDBRELINFO lpdbRelation = pArea->lpdbRelations;

      while ( lpdbRelation )
         {
            SubArray.type = HB_IT_NIL;
            hb_arrayNew( &SubArray, 3 );
            hb_arraySet(&SubArray, 2,lpdbRelation->abKey );
            hb_arraySet(&SubArray, 3,lpdbRelation->itmCobExpr );
            hb_rddSelectWorkAreaNumber( lpdbRelation->lpaChild->uiArea );
            childArea = hb_rddGetCurrentWorkAreaPointer();
            hb_arraySetForward(&SubArray, 1, hb_itemPutC( NULL,( ( PHB_DYNS ) childArea->atomAlias )->pSymbol->szName  )); //            hb_itemPutC ( hb_arrayGetItemPtr( &SubArray, 3 ), ( ( PHB_DYNS ) childArea->atomAlias )->pSymbol->szName );
            hb_arrayAddForward( pArray , &SubArray );
            lpdbRelation = lpdbRelation->lpdbriNext;
         }
      hb_rddSelectWorkAreaNumber( uiPrevArea );
   }
hb_itemRelease(hb_itemReturn( pArray ));
}

HB_FUNC( FIELDEXIST )
{
AREAP pArea = hb_rddGetCurrentWorkAreaPointer();
USHORT uiIndex = ISCHAR( 1 ) ? hb_rddFieldIndex( pArea, hb_parc( 1 ) ) : 0;
hb_retl( uiIndex > 0 );
}
// ORDList(  like type,bag name )
HB_FUNC( ORDLIST )
{
AREAP pArea = hb_rddGetCurrentWorkAreaPointer();
PHB_ITEM pArray =hb_itemArrayNew( 0 );
   if ( pArea )
   {
    PHB_ITEM pType = hb_param( 1, HB_IT_ANY );
    DBORDERINFO pOrderInfo,pOrderInfoT;
    ULONG ulOrd, ulCount ;
    pOrderInfo.atomBagName = hb_param( 2, HB_IT_STRING );
    pOrderInfoT.atomBagName = hb_param( 2, HB_IT_STRING );

    pOrderInfo.itmOrder = NULL;
    pOrderInfo.itmResult = hb_itemPutNI( NULL, 0 );
    SELF_ORDINFO( pArea, DBOI_ORDERCOUNT, &pOrderInfo );
    ulOrd = hb_itemGetNL( pOrderInfo.itmResult );
    hb_itemRelease( pOrderInfo.itmResult );

    for( ulCount = 1; ulCount <= ulOrd; ulCount++ )
    {
     pOrderInfo.itmOrder = hb_itemPutNI( NULL, ulCount );
     pOrderInfo.itmResult = hb_itemPutC( NULL, "" );
     SELF_ORDINFO( pArea, DBOI_NAME, &pOrderInfo );


     pOrderInfoT.itmOrder = hb_itemPutNI( NULL, ulCount );
     pOrderInfoT.itmResult = hb_itemPutC( NULL, "" );
     SELF_ORDINFO( pArea, DBOI_KEYTYPE, &pOrderInfoT );

     if ( pType )
       {
        if ( pType->type == HB_IT_NIL || hb_itemTypeStr( pType )[0] == pOrderInfoT.itmResult->item.asString.value[0] )
          {
           hb_arrayAddForward( pArray, pOrderInfo.itmResult );
          }
       }
        else
       {
        hb_arrayAddForward( pArray, pOrderInfo.itmResult );
       }
     hb_itemRelease( pOrderInfo.itmResult );
     hb_itemRelease( pOrderInfo.itmOrder );

     hb_itemRelease( pOrderInfoT.itmResult );
     hb_itemRelease( pOrderInfoT.itmOrder );
    }
   }
   hb_itemRelease( hb_itemReturn( pArray ) );
}

HB_FUNC( ORDTYPE )
{
AREAP pArea = hb_rddGetCurrentWorkAreaPointer();

   if( pArea )
   {
      DBORDERINFO pOrderInfo;
      pOrderInfo.itmOrder = hb_param( 1, HB_IT_STRING | HB_IT_NUMERIC) ;
      pOrderInfo.atomBagName = hb_param( 2, HB_IT_STRING );
      pOrderInfo.itmResult = hb_itemPutC( NULL, "" );
      SELF_ORDINFO( pArea, DBOI_KEYTYPE, &pOrderInfo );
      hb_itemRelease( hb_itemReturn( pOrderInfo.itmResult ) );
    }
     else
      hb_retc("U");
}
HB_FUNC( SCATTER___ )
{
HB_THREAD_STUB
AREAP pArea = hb_rddGetCurrentWorkAreaPointer();
PHB_ITEM pArray  = hb_param( 1, HB_IT_ARRAY );
     if( ! pArray )
       pArray = hb_itemArrayNew( 0 );
        else
       hb_arraySize( pArray, 0 );


   if( pArea )
   {
         USHORT uiCount = pArea->uiFieldCount - (hb_rddFieldIndex( pArea,"CRC") ? (hb_rddFieldIndex( pArea,"CRYPT_CODE") ? 4 : 3 ) : 0);
         USHORT uiField;

         for( uiField = 1; uiField <= uiCount; uiField++ )
         {
          HB_ITEM_NEW( Item );
          SELF_GETVALUE( pArea, uiField, &Item );
          hb_arrayAddForward( pArray, &Item );
         }
   }
     hb_itemForwardValue( hb_stackReturnItem(), pArray );
}

HB_FUNC( SCATTERS___ )
{
HB_THREAD_STUB
AREAP pArea = hb_rddGetCurrentWorkAreaPointer();
PHB_ITEM pArray  = hb_param( 1, HB_IT_ARRAY );
     if( ! pArray )
       pArray = hb_itemArrayNew( 0 );
        else
       hb_arraySize( pArray, 0 );


   if( pArea )
   {
         USHORT uiField;
         USHORT uiCount = pArea->uiFieldCount - (hb_rddFieldIndex( pArea,"CRC") ? (hb_rddFieldIndex( pArea,"CRYPT_CODE") ? 4 : 3 ) : 0);
         for( uiField = 1; uiField <= uiCount; uiField++ )
         {
          char * szName = ( char * ) hb_xgrab( pArea->uiMaxFieldNameLength + 1 );
          HB_ITEM_NEW( Item );
          HB_ITEM_NEW( SubArray);
          hb_arrayNew( &SubArray, 2 );
          SELF_GETVALUE( pArea, uiField, &Item );
          SELF_FIELDNAME( pArea, uiField, szName );
          hb_arraySetForward(&SubArray, 1,hb_itemPutC( NULL, szName ));
          hb_arraySetForward(&SubArray, 2,&Item);
          hb_xfree(szName);
          hb_arrayAddForward( pArray, &SubArray );
         }
   }
     hb_itemForwardValue( hb_stackReturnItem(), pArray );
}

HB_FUNC( GATHER__ )
{
HB_THREAD_STUB
AREAP pArea = hb_rddGetCurrentWorkAreaPointer();
PHB_ITEM pItem  = hb_param( 1, HB_IT_ARRAY );
   if( pArea && pItem )
   {
      DBLOCKINFO dbLockInfo;
      USHORT uiField;
      USHORT uiArrayLen = ( USHORT ) hb_arrayLen( pItem );

         if( uiArrayLen > pArea->uiFieldCount )
             uiArrayLen = pArea->uiFieldCount;

         dbLockInfo.itmRecID = NULL;
         dbLockInfo.uiMethod = DBLM_EXCLUSIVE;
         if ( SELF_LOCK( pArea, &dbLockInfo ) == SUCCESS )
          {
            for( uiField = 1; uiField <= uiArrayLen; uiField++ )
            {
             SELF_PUTVALUE( pArea, uiField, hb_arrayGetItemPtr( pItem, uiField ) );
            }
            SELF_UNLOCK( pArea, 0 );
          }
   }
}

#pragma ENDDUMP
