Example #1
0
static void s_sslSocketNew( HB_BOOL fServer )
{
   HB_SOCKET sd = hb_socketParam( 1 );

   if( sd != HB_NO_SOCKET )
   {
      PHB_SOCKEX pSock = NULL;
      SSL * ssl = hb_SSL_par( 2 );

      if( ssl )
         pSock = hb_sockexNewSSL( sd, ssl, fServer, hb_parnintdef( 3, - 1 ), hb_param( 2, HB_IT_ANY ) );
      else if( HB_ISHASH( 2 ) )
         pSock = hb_sockexNew( sd, s_sockFilter.pszName, hb_param( 2, HB_IT_ANY ) );
      else
         hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );

      if( pSock )
      {
         PHB_ITEM pSockItm = hb_param( 1, HB_IT_POINTER );

         if( HB_ISBYREF( 1 ) && hb_sockexItemReplace( pSockItm, pSock ) )
            hb_itemReturn( pSockItm );
         else
         {
            hb_socketItemClear( pSockItm );
            hb_sockexItemPut( hb_param( -1, HB_IT_ANY ), pSock );
         }
      }
   }
}
Example #2
0
static void hb_trace_message( char * buffer, HB_SIZE nSize, int iParam, int iCount )
{
   int iFirst = iParam;

   buffer[ 0 ] = '\0';

   while( iParam <= iCount && nSize > 1 )
   {
      char * pszString;
      HB_SIZE nLen;
      HB_BOOL fFree;

      if( iParam > iFirst )
      {
         *buffer++ = ' ';
         --nSize;
      }
      pszString = hb_itemString( hb_param( iParam, HB_IT_ANY ), &nLen, &fFree );
      hb_strncpy( buffer, pszString, nSize );
      nLen = strlen( buffer );
      nSize -= nLen;
      buffer += nLen;
      if( fFree )
         hb_xfree( pszString );
      iParam++;
   }
}
Example #3
0
static void hb_fileReturn( PHB_FILE pFile )
{
   if( pFile )
      hb_fileItemPut( hb_param( -1, HB_IT_ANY ), pFile );
   else
      hb_ret();
}
Example #4
0
POINT * hbwapi_par_POINT( POINT * p, int iParam, HB_BOOL bMandatory )
{
   PHB_ITEM pStru = hb_param( iParam, HB_IT_ANY );

   memset( p, 0, sizeof( POINT ) );

   if( pStru && HB_IS_HASH( pStru ) )
   {
      p->x = ( LONG ) hb_itemGetNL( hb_hashGetCItemPtr( pStru, "x" ) );
      p->y = ( LONG ) hb_itemGetNL( hb_hashGetCItemPtr( pStru, "y" ) );

      return p;
   }
   else if( pStru && HB_IS_ARRAY( pStru ) && hb_arrayLen( pStru ) >= 2 )
   {
      p->x = ( LONG ) hb_arrayGetNL( pStru, 1 );
      p->y = ( LONG ) hb_arrayGetNL( pStru, 2 );

      return p;
   }
   else if( bMandatory )
      return p;

   return NULL;
}
Example #5
0
static void hb_PEM_read_bio( PEM_READ_BIO * func )
{
   BIO * bio;

   if( hb_BIO_is( 1 ) )
      bio = hb_BIO_par( 1 );
   else if( HB_ISCHAR( 1 ) )
      bio = BIO_new_file( hb_parc( 1 ), "r" );
   else if( HB_ISNUM( 1 ) )
      bio = BIO_new_fd( hb_parni( 1 ), BIO_NOCLOSE );
   else
      bio = NULL;

   if( bio )
   {
      PHB_ITEM pPassCallback = hb_param( 2, HB_IT_EVALITEM );

      if( pPassCallback )
      {
         hb_retptr( ( *func )( bio, NULL, hb_ssl_pem_password_cb, pPassCallback ) );
      }
      else
      {
         /* NOTE: Dropping 'const' qualifier. [vszakats] */
         hb_retptr( ( *func )( bio, NULL, NULL, ( void * ) hb_parc( 2 ) ) );
      }

      if( ! hb_BIO_is( 1 ) )
         BIO_free( bio );
   }
   else
      hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}
Example #6
0
DOCINFO * hbwapi_par_DOCINFO( DOCINFO * p, int iParam, HB_BOOL bMandatory, void *** ph )
{
   PHB_ITEM pStru = hb_param( iParam, HB_IT_ANY );
   void ** h = ( void ** ) hb_xgrabz( 3 * sizeof( void * ) );

   *ph = h;

   memset( p, 0, sizeof( DOCINFO ) );

   p->cbSize = sizeof( DOCINFO );

   if( pStru && HB_IS_HASH( pStru ) )
   {
      p->lpszDocName  = HB_ITEMGETSTR( hb_hashGetCItemPtr( pStru, "lpszDocName"  ), &h[ 0 ], NULL );
      p->lpszOutput   = HB_ITEMGETSTR( hb_hashGetCItemPtr( pStru, "lpszOutput"   ), &h[ 1 ], NULL );
      p->lpszDatatype = HB_ITEMGETSTR( hb_hashGetCItemPtr( pStru, "lpszDatatype" ), &h[ 2 ], NULL );
      p->fwType       = ( DWORD ) hb_itemGetNL( hb_hashGetCItemPtr( pStru, "fwType" ) );

      return p;
   }
   else if( bMandatory )
      return p;

   hb_xfree( h );
   *ph = NULL;

   return NULL;
}
Example #7
0
RECT * hbwapi_par_RECT( RECT * p, int iParam, HB_BOOL bMandatory )
{
   PHB_ITEM pStru = hb_param( iParam, HB_IT_ANY );

   memset( p, 0, sizeof( RECT ) );

   if( pStru && HB_IS_HASH( pStru ) )
   {
      p->left   = ( LONG ) hb_itemGetNL( hb_hashGetCItemPtr( pStru, "left"   ) );
      p->top    = ( LONG ) hb_itemGetNL( hb_hashGetCItemPtr( pStru, "top"    ) );
      p->right  = ( LONG ) hb_itemGetNL( hb_hashGetCItemPtr( pStru, "right"  ) );
      p->bottom = ( LONG ) hb_itemGetNL( hb_hashGetCItemPtr( pStru, "bottom" ) );

      return p;
   }
   else if( pStru && HB_IS_ARRAY( pStru ) && hb_arrayLen( pStru ) >= 4 )
   {
      p->left   = ( LONG ) hb_arrayGetNL( pStru, 1 );
      p->top    = ( LONG ) hb_arrayGetNL( pStru, 2 );
      p->right  = ( LONG ) hb_arrayGetNL( pStru, 3 );
      p->bottom = ( LONG ) hb_arrayGetNL( pStru, 4 );

      return p;
   }
   else if( bMandatory )
      return p;

   return NULL;
}
Example #8
0
static const char * save_cb( mxml_node_t * node, int where )
{
   HB_CBS_VAR * pCbs = ( HB_CBS_VAR * ) hb_stackTestTSD( &s_cbs_var );

   if( node != NULL && pCbs != NULL )
   {
      PHB_ITEM pCallback = pCbs->save_cb;

      if( pCbs->hText )
      {
         hb_strfree( pCbs->hText );
         pCbs->hText = NULL;
      }

      if( pCallback && hb_vmRequestReenter() )
      {
         const char * pszResult;

         hb_vmPushEvalSym();
         hb_vmPush( pCallback );
         mxml_node_push( node, 0 );
         hb_vmPushInteger( where );
         hb_vmSend( 2 );

         pszResult = hb_itemGetStrUTF8( hb_param( -1, HB_IT_ANY ), &pCbs->hText, NULL );

         hb_vmRequestRestore();

         return pszResult;
      }
   }
   return NULL;
}
Example #9
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;
}
Example #10
0
static void s_GetFileName( HB_BOOL fSave )
{
   void * hInitDir, * hTitle, * hDefExt;
   LPTSTR lpstrFilter;
   OPENFILENAME ofn;

   memset( &ofn, 0, sizeof( ofn ) );
#if defined( OPENFILENAME_SIZE_VERSION_400 )
   ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
#else
   ofn.lStructSize = sizeof( ofn );
#endif
   ofn.hwndOwner = GetActiveWindow();
   ofn.hInstance = GetModuleHandle( NULL );

   ofn.nFilterIndex     = hbwapi_par_DWORD( 6 );
   ofn.lpstrFilter      = lpstrFilter = s_dialogPairs( 5, &ofn.nFilterIndex );

   ofn.nMaxFile         = hbwapi_par_DWORD( 7 );
   if( ofn.nMaxFile < 0x400 )
      ofn.nMaxFile = ofn.nMaxFile == 0 ? 0x10000 : 0x400;
   ofn.lpstrFile        = ( LPTSTR )
                          memset( hb_xgrab( ofn.nMaxFile * sizeof( TCHAR ) ),
                                  0, ofn.nMaxFile * sizeof( TCHAR ) );

   ofn.lpstrInitialDir  = HB_PARSTR( 3, &hInitDir, NULL );
   ofn.lpstrTitle       = HB_PARSTR( 2, &hTitle, NULL );
   ofn.Flags            = HB_ISNUM( 1 ) ? hbwapi_par_DWORD( 1 ) :
                          ( OFN_EXPLORER | OFN_ALLOWMULTISELECT |
                            OFN_HIDEREADONLY | OFN_NOCHANGEDIR );
   ofn.lpstrDefExt      = HB_PARSTR( 4, &hDefExt, NULL );
   if( ofn.lpstrDefExt && ofn.lpstrDefExt[ 0 ] == '.' )
      ++ofn.lpstrDefExt;

   HB_ITEMCOPYSTR( hb_param( 8, HB_IT_ANY ), ofn.lpstrFile, ofn.nMaxFile );

   if( fSave ? GetSaveFileName( &ofn ) : GetOpenFileName( &ofn ) )
   {
      HB_SIZE nLen;
      for( nLen = 0; nLen < ofn.nMaxFile; ++nLen )
      {
         if( ofn.lpstrFile[ nLen ] == 0 &&
             ( nLen + 1 == ofn.nMaxFile || ofn.lpstrFile[ nLen + 1 ] == 0 ) )
            break;
      }
      hb_stornint( ofn.Flags, 1 );
      hb_stornint( ofn.nFilterIndex, 6 );
      HB_RETSTRLEN( ofn.lpstrFile, nLen );
   }
   else
      hb_retc_null();

   hb_xfree( ofn.lpstrFile );
   if( lpstrFilter )
      hb_xfree( lpstrFilter );

   hb_strfree( hInitDir );
   hb_strfree( hTitle );
   hb_strfree( hDefExt );
}
Example #11
0
static HB_BOOL hb_errGetNumCode( int * piValue, const char * szOperation )
{
   PHB_ITEM pItem = hb_param( 1, HB_IT_NUMERIC );

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

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

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

   return HB_TRUE;
}
Example #12
0
static void hb_inetConnectInternal( HB_BOOL fResolve )
{
   const char * szHost = hb_parc( 1 );
   char * szAddr = NULL;
   PHB_SOCKET_STRUCT socket = HB_PARSOCKET( 3 );
   PHB_ITEM pSocket = NULL;
   int iPort = hb_parni( 2 );

   if( szHost == NULL || iPort == 0 || ( socket == NULL && ! HB_ISNIL( 3 ) ) )
      hb_inetErrRT();
   else
   {
      if( ! socket )
         HB_SOCKET_INIT( socket, pSocket );
      else if( socket->sd != HB_NO_SOCKET )
         hb_inetCloseSocket( socket, HB_FALSE );

      if( fResolve )
         szHost = szAddr = hb_socketResolveAddr( szHost, HB_SOCKET_AF_INET );

      if( fResolve && ! szAddr )
      {
         hb_inetGetError( socket );
         if( socket->iError == 0 )
            socket->iError = HB_SOCKET_ERR_WRONGADDR;
      }
      else
      {
         /* Creates comm socket */
         socket->sd = hb_socketOpen( HB_SOCKET_PF_INET, HB_SOCKET_PT_STREAM, 0 );
         if( socket->sd == HB_NO_SOCKET )
            hb_inetGetError( socket );
         else
         {
            if( socket->remote )
               hb_xfree( socket->remote );
            if( hb_socketInetAddr( &socket->remote, &socket->remotelen,
                                   szHost, iPort ) )
            {
               hb_socketSetKeepAlive( socket->sd, HB_TRUE );
               if( hb_socketConnect( socket->sd, socket->remote, socket->remotelen,
                                     socket->iTimeout ) != 0 )
                  hb_inetGetError( socket );
               else
                  socket->iError = HB_INET_ERR_OK;
            }
            else
               hb_inetGetError( socket );
         }
         if( szAddr )
            hb_xfree( szAddr );
      }
      if( pSocket )
         hb_itemReturnRelease( pSocket );
      else
         hb_itemReturn( hb_param( 3, HB_IT_ANY ) );
   }
}
Example #13
0
File: pem.c Project: diegopego/core
static void hb_PEM_read_bio( PEM_READ_BIO * func, HB_PEM_TYPES type )
{
   BIO * bio;

   if( hb_BIO_is( 1 ) )
      bio = hb_BIO_par( 1 );
   else if( HB_ISCHAR( 1 ) )
      bio = BIO_new_file( hb_parc( 1 ), "r" );
   else if( HB_ISNUM( 1 ) )
      bio = BIO_new_fd( hb_parni( 1 ), BIO_NOCLOSE );
   else
      bio = NULL;

   if( bio )
   {
      PHB_ITEM pPassCallback = hb_param( 2, HB_IT_EVALITEM );
      pem_password_cb * cb;
      void * cargo, * result;

      if( pPassCallback )
      {
         cb = hb_ssl_pem_password_cb;
         cargo = pPassCallback;
      }
      else
      {
         cb = NULL;
         cargo = ( void * ) hb_parc( 2 ); /* NOTE: Dropping 'const' qualifier. [vszakats] */
      }

      result = ( *func )( bio, NULL, cb, cargo );

      if( result )
      {
         switch( type )
         {
            case hb_PEM_X509:
               hb_X509_ret( ( X509 * ) result, HB_TRUE );
               break;
            case hb_PEM_EVP_PKEY:
               hb_EVP_PKEY_ret( ( EVP_PKEY * ) result );
               break;
            case hb_PEM_ANY:
               hb_retptr( NULL );
               break;
         }
      }
      else
         hb_retptr( NULL );

      if( ! hb_BIO_is( 1 ) )
         BIO_free( bio );
   }
   else
      hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}
Example #14
0
static HB_BOOL socketaddrParam( int iParam, void ** pAddr, unsigned int * puiLen )
{
   PHB_ITEM pItem = hb_param( iParam, HB_IT_ARRAY );

   if( pItem && hb_socketAddrFromItem( pAddr, puiLen, pItem ) )
      return HB_TRUE;

   hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
   return HB_FALSE;
}
Example #15
0
static LPTSTR s_StringList( int iParam )
{
   PHB_ITEM pItem = hb_param( iParam, HB_IT_ARRAY | HB_IT_STRING ), pArrItem;
   LPTSTR lpStr = NULL;

   if( pItem )
   {
      HB_SIZE nLen, nSize, nTotal, n, n1;

      if( HB_IS_ARRAY( pItem ) )
      {
         nSize = hb_arrayLen( pItem );
         for( n = nLen = 0; n < nSize; ++n )
         {
            pArrItem = hb_arrayGetItemPtr( pItem, n + 1 );
            if( HB_IS_STRING( pArrItem ) )
            {
               n1 = HB_ITEMCOPYSTR( pArrItem, NULL, 0 );
               if( n1 )
                  nLen += n1 + 1;
            }
         }
         if( nLen )
         {
            nTotal = nLen + 1;
            lpStr = ( LPTSTR ) hb_xgrab( nTotal * sizeof( TCHAR ) );
            for( n = nLen = 0; n < nSize; ++n )
            {
               pArrItem = hb_arrayGetItemPtr( pItem, n + 1 );
               if( HB_IS_STRING( pArrItem ) )
               {
                  n1 = HB_ITEMCOPYSTR( pArrItem,
                                       lpStr + nLen, nTotal - nLen );
                  if( n1 )
                     nLen += n1 + 1;
               }
            }
            lpStr[ nLen ] = 0;
         }
      }
      else
      {
         nLen = HB_ITEMCOPYSTR( pItem, NULL, 0 );
         if( nLen )
         {
            lpStr = ( LPTSTR ) hb_xgrab( ( nLen + 1 ) * sizeof( TCHAR ) );
            HB_ITEMCOPYSTR( pItem, lpStr, nLen );
            lpStr[ nLen ] = 0;
         }
      }
   }

   return lpStr;
}
Example #16
0
static void hb_PEM_read_bio( PEM_READ_BIO * func, HB_BOOL fX509 )
{
   BIO * bio = NULL;
   HB_BYTE * pBuffer = NULL;
   HB_SIZE nSize = 0;

   if( hb_BIO_is( 1 ) )
      bio = hb_BIO_par( 1 );
   else if( HB_ISCHAR( 1 ) )
   {
      pBuffer = hb_fileLoad( hb_parc( 1 ), 0, &nSize );
      if( pBuffer )
         bio = BIO_new_mem_buf( ( char * ) pBuffer, ( int ) nSize );
   }
   else if( HB_ISNUM( 1 ) )
      bio = BIO_new_fd( hb_parni( 1 ), BIO_NOCLOSE );

   if( bio )
   {
      PHB_ITEM pPassCallback = hb_param( 2, HB_IT_EVALITEM );
      pem_password_cb * cb;
      void * cargo, * result;

      if( pPassCallback )
      {
         cb = hb_ssl_pem_password_cb;
         cargo = pPassCallback;
      }
      else
      {
         cb = NULL;
         cargo = HB_UNCONST( hb_parc( 2 ) );  /* NOTE: Discarding 'const' qualifier, OpenSSL will memcpy() it */
      }

      result = ( *func )( bio, NULL, cb, cargo );

      if( fX509 && result )
         hb_X509_ret( ( X509 * ) result, HB_TRUE );
      else
         hb_retptr( result );

      if( ! hb_BIO_is( 1 ) )
         BIO_free( bio );

      if( pBuffer )
      {
         OPENSSL_cleanse( pBuffer, ( size_t ) nSize );
         hb_xfree( pBuffer );
      }
   }
   else
      hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}
Example #17
0
static HB_BOOL hb_sxOrdParam( LPDBORDERINFO pInfo )
{
   memset( pInfo, 0, sizeof( DBORDERINFO ) );

   if( HB_ISCHAR( 1 ) )
   {
      pInfo->itmOrder = hb_param( 1, HB_IT_STRING );
      pInfo->atomBagName = hb_param( 2, HB_IT_STRING );
   }
   else if( HB_ISNUM( 1 ) )
   {
      pInfo->itmOrder = hb_param( 1, HB_IT_NUMERIC );
      if( ! HB_ISNIL( 2 ) ) /* hb_pcount() > 2 */
      {
         pInfo->atomBagName = hb_param( 2, HB_IT_NUMERIC );
         if( hb_parni( 2 ) <= 0 )
            return HB_FALSE;
      }
   }
   return HB_TRUE;
}
Example #18
0
void hbwapi_stor_POINT( POINT * p, int iParam )
{
   PHB_ITEM pStru = hb_param( iParam, HB_IT_ANY );

   if( pStru && HB_IS_HASH( pStru ) )
   {
      s_hb_hashSetCItemNL( pStru, "x", p->x );
      s_hb_hashSetCItemNL( pStru, "y", p->y );
   }
   else if( pStru && HB_IS_ARRAY( pStru ) && hb_arrayLen( pStru ) >= 2 )
   {
      hb_arraySetNL( pStru, 1, p->x );
      hb_arraySetNL( pStru, 2, p->y );
   }
}
Example #19
0
void hbwapi_stor_RECT( RECT * p, int iParam )
{
   PHB_ITEM pStru = hb_param( iParam, HB_IT_ANY );

   if( pStru && HB_IS_HASH( pStru ) )
   {
      s_hb_hashSetCItemNL( pStru, "left"  , p->left   );
      s_hb_hashSetCItemNL( pStru, "top"   , p->top    );
      s_hb_hashSetCItemNL( pStru, "right" , p->right  );
      s_hb_hashSetCItemNL( pStru, "bottom", p->bottom );
   }
   else if( pStru && HB_IS_ARRAY( pStru ) && hb_arrayLen( pStru ) >= 4 )
   {
      hb_arraySetNL( pStru, 1, p->left   );
      hb_arraySetNL( pStru, 2, p->top    );
      hb_arraySetNL( pStru, 3, p->right  );
      hb_arraySetNL( pStru, 4, p->bottom );
   }
}
Example #20
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 );
}
Example #21
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;
}
/*
    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 );
    }
}
Example #23
0
static HB_BOOL hb_sxSemName( char * szFileName )
{
   const char * szName = hb_parc( 1 );
   HB_BOOL fResult = HB_FALSE;

   if( szName && szName[ 0 ] )
   {
      hb_cdpnDup2Lower( hb_vmCDP(), szName, strlen( szName ),
                        szFileName, HB_PATH_MAX );
      szFileName[ HB_PATH_MAX - 1 ] = '\0';
      fResult = HB_TRUE;
   }
   else
   {
      AREAP pArea = ( AREAP ) hb_rddGetCurrentWorkAreaPointer();

      if( pArea )
      {
         DBORDERINFO pOrderInfo;

         memset( &pOrderInfo, 0, sizeof( pOrderInfo ) );
         pOrderInfo.itmOrder = hb_param( 1, HB_IT_NUMERIC );
         if( pOrderInfo.itmOrder && hb_itemGetNI( pOrderInfo.itmOrder ) == 0 )
            pOrderInfo.itmOrder = NULL;
         pOrderInfo.itmResult = hb_itemPutC( NULL, NULL );
         SELF_ORDINFO( pArea, DBOI_NAME, &pOrderInfo );
         szName = hb_itemGetCPtr( pOrderInfo.itmResult );
         if( szName && szName[ 0 ] )
         {
            hb_cdpnDup2Lower( hb_vmCDP(), szName, strlen( szName ),
                              szFileName, HB_PATH_MAX );
            szFileName[ HB_PATH_MAX - 1 ] = '\0';
            fResult = HB_TRUE;
         }
         hb_itemRelease( pOrderInfo.itmResult );
      }
   }

   return fResult;
}
Example #24
0
static void hb_PEM_read_bio( PEM_READ_BIO * func )
{
   BIO * bio = NULL;
   HB_BYTE * pBuffer = NULL;
   HB_SIZE nSize = 0;

   if( hb_BIO_is( 1 ) )
      bio = hb_BIO_par( 1 );
   else if( HB_ISCHAR( 1 ) )
   {
      pBuffer = hb_fileLoad( hb_parc( 1 ), 0, &nSize );
      if( pBuffer )
         bio = BIO_new_mem_buf( ( char * ) pBuffer, ( int ) nSize );
   }
   else if( HB_ISNUM( 1 ) )
      bio = BIO_new_fd( hb_parni( 1 ), BIO_NOCLOSE );

   if( bio )
   {
      PHB_ITEM pPassCallback = hb_param( 2, HB_IT_EVALITEM );

      if( pPassCallback )
         hb_retptr( ( *func )( bio, NULL, hb_ssl_pem_password_cb, pPassCallback ) );
      else
         hb_retptr( ( *func )( bio, NULL, NULL, ( void * ) hb_parc( 2 ) ) );  /* NOTE: Dropping 'const' qualifier. [vszakats] */

      if( ! hb_BIO_is( 1 ) )
         BIO_free( bio );

      if( pBuffer )
      {
         OPENSSL_cleanse( pBuffer, ( size_t ) nSize );
         hb_xfree( pBuffer );
      }
   }
   else
      hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}
Example #25
0
static void s_inetRecvPattern( const char * const * patterns, int * patternsizes,
                               int iPatternsCount, int iParam )
{
   PHB_SOCKET_STRUCT socket = HB_PARSOCKET( 1 );
   PHB_ITEM pResult         = hb_param( iParam, HB_IT_BYREF );
   PHB_ITEM pMaxSize        = hb_param( iParam + 1, HB_IT_NUMERIC );
   PHB_ITEM pBufferSize     = hb_param( iParam + 2, HB_IT_NUMERIC );

   char cChar = '\0';
   char * buffer;
   int iPaternFound = 0;
   int iTimeElapsed = 0;
   int iPos = 0;
   int iLen;
   int iAllocated, iBufferSize, iMax;
   int i;

   if( socket == NULL )
   {
      hb_inetErrRT();
      return;
   }
   else if( ! hb_inetIsOpen( socket ) )
   {
      if( pResult )
         hb_itemPutNI( pResult, -1 );
      hb_retc_null();
      return;
   }


   iBufferSize = pBufferSize ? hb_itemGetNI( pBufferSize ) : 80;
   iMax = pMaxSize ? hb_itemGetNI( pMaxSize ) : 0;

   socket->iError = HB_INET_ERR_OK;

   buffer = ( char * ) hb_xgrab( iBufferSize );
   iAllocated = iBufferSize;

   do
   {
      if( iPos == iAllocated - 1 )
      {
         iAllocated += iBufferSize;
         buffer = ( char * ) hb_xrealloc( buffer, iAllocated );
      }

      iLen = s_inetRecv( socket, &cChar, 1, HB_TRUE, socket->iTimeout );
      if( iLen == -1 && s_inetIsTimeout( socket ) )
      {
         iLen = -2;     /* this signals timeout */
         if( socket->pPeriodicBlock )
         {
            HB_BOOL fResult;

            iTimeElapsed += socket->iTimeout;
            hb_execFromArray( socket->pPeriodicBlock );
            fResult = hb_parl( -1 ) && hb_vmRequestQuery() == 0;

            if( fResult &&
                ( socket->iTimeLimit == -1 || iTimeElapsed < socket->iTimeLimit ) )
               iLen = 1;
         }
      }
      else if( iLen > 0 )
      {
         buffer[ iPos++ ] = cChar;
         for( i = 0; i < iPatternsCount; ++i )
         {
            if( patternsizes[ i ] <= iPos &&
                cChar == patterns[ i ][ patternsizes[ i ] - 1 ] )
            {
               if( memcmp( buffer + iPos - patternsizes[ i ],
                           patterns[ i ], patternsizes[ i ] ) == 0 )
               {
                  iPaternFound = i + 1;
                  break;
               }
            }
         }
      }
   }
   while( iLen > 0 && iPaternFound == 0 && ( iMax == 0 || iPos < iMax ) );

   if( iPaternFound )
   {
      socket->iCount = iPos;
      if( pResult )
         hb_itemPutNI( pResult, iPos );
      hb_retclen_buffer( buffer, iPos - patternsizes[ iPaternFound - 1 ] );
   }
   else
   {
      if( iLen == 0 )
         socket->iError = HB_INET_ERR_CLOSEDCONN;
      else if( iLen < 0 )
         hb_inetGetError( socket );
      else
      {
         socket->iError = HB_INET_ERR_BUFFOVERRUN;
         iLen = -1;
      }
      if( pResult )
         hb_itemPutNI( pResult, iLen );
      hb_xfree( buffer );
      hb_retc_null();
   }
}
Example #26
0
static void s_inetRecvInternal( int iMode )
{
   PHB_SOCKET_STRUCT socket = HB_PARSOCKET( 1 );
   PHB_ITEM pBuffer = hb_param( 2, HB_IT_STRING );

   if( socket == NULL || pBuffer == NULL || ! HB_ISBYREF( 2 ) )
      hb_inetErrRT();
   else if( ! hb_inetIsOpen( socket ) )
      hb_retni( -1 );
   else
   {
      int iLen, iMaxLen, iReceived, iTimeElapsed;
      char * buffer;
      HB_SIZE nLen;

      if( hb_itemGetWriteCL( pBuffer, &buffer, &nLen ) )
         iLen = ( int ) nLen;
      else
      {
         iLen = 0;
         buffer = NULL;
      }

      if( HB_ISNUM( 3 ) )
      {
         iMaxLen = hb_parni( 3 );
         if( iMaxLen < 0 )
            iMaxLen = 0;
         else if( iLen < iMaxLen )
            iMaxLen = iLen;
      }
      else
         iMaxLen = iLen;

      iReceived = iTimeElapsed = 0;
      socket->iError = HB_INET_ERR_OK;
      do
      {
         iLen = s_inetRecv( socket, buffer + iReceived, iMaxLen - iReceived,
                            HB_FALSE, socket->iTimeout );
         if( iLen >= 0 )
         {
            iReceived += iLen;
            if( iMode == 0 ) /* Called from hb_inetRecv()? */
               break;
         }
         else if( iLen == -1 && s_inetIsTimeout( socket ) )
         {
            /* if we have a pPeriodicBlock, timeLimit is our REAL timeout */
            if( socket->pPeriodicBlock )
            {
               /* timed out; let's see if we have to run a cb routine */
               iTimeElapsed += socket->iTimeout;
               hb_execFromArray( socket->pPeriodicBlock );
               /* do we continue? */
               if( hb_parl( -1 ) && hb_vmRequestQuery() == 0 &&
                   ( socket->iTimeLimit == -1 || iTimeElapsed < socket->iTimeLimit ) )
                  iLen = 1;   /* Declare success to continue loop */
            }
         }
      }
      while( iReceived < iMaxLen && iLen > 0 );

      socket->iCount = iReceived;

      if( iLen == 0 )
         socket->iError = HB_INET_ERR_CLOSEDCONN;
      else if( iLen < 0 )
         hb_inetGetError( socket );

      hb_retni( iReceived > 0 ? iReceived : iLen );
   }
}
Example #27
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 );
   }
}
Example #28
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;
}
Example #29
0
/*
 * Implementação de printf() para Harbour/xHarbour.
 *
 * TODO: Creio que uma ótima melhoria seria somar o tamanho de todos os argumentos
 * e criar um buffer já pre-determinado com um tamanho +/- necessário para evitar
 * tantas realocações de memoria como ocorre nestes casos.
 * 26/07/2008 - 08:17:42
 */
static
char *wx_printf( ULONG *Length )
{
    PPrintInfo pInfo;
    char c;
    char *result;
    ULONG request_len;

    *Length = 0L;

    // o primeiro argumento tem que ser uma string!
    if ( !ISCHAR(1) )
        return NULL;

    /* Iniciamos a memoria */
    pInfo = (PPrintInfo) hb_xgrab( sizeof(TPrintInfo) );
    memset( pInfo, 0, sizeof(TPrintInfo) );

    pInfo->Mask   = hb_parcx(1);
    pInfo->msk_len= hb_parclen(1);

    pInfo->Result = (char *) hb_xgrab( pInfo->msk_len+1 );
    pInfo->Buffer = pInfo->Result;
    pInfo->cap_len= pInfo->msk_len;

    pInfo->argp ++;

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

        switch (c)
        {
        case '%':
        {
            /*
             * 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->Mask[1] == '%')
            {
                printf_add_str( pInfo, "%", 1L );
                pInfo->Mask    += 2;
                pInfo->msk_len -= 2;
                continue;
            }

            pInfo->argp ++;
            printf_add_conv( pInfo, hb_param( pInfo->argp, HB_IT_ANY ));
            continue;
        }

        default:
        {
            // Precisa realocar memoria?
            request_len = 1L;

            if (pInfo->out_len+request_len > pInfo->cap_len)
            {
                pInfo->cap_len += request_len;
                pInfo->Result   = (char *) hb_xrealloc( pInfo->Result, pInfo->cap_len+1 );
                pInfo->Buffer   = pInfo->Result + (pInfo->out_len);
                pInfo->out_len += request_len;
            } else {
                pInfo->out_len += request_len;
            }

            /*
             * É um caracter simples ou é uma sequencia de escape a ser add?
             * Links relacionados:
             *    http://docs.sun.com/app/docs/doc/816-0220/6m6nkorp8?a=view
             *    http://docs.sun.com/app/docs/doc/816-0213/6m6ne387j?a=view
             */
            if (c == '\\')
            {
                pInfo->Mask ++;
                pInfo->msk_len--;

                if (!pInfo->msk_len)
                    continue;

                c = pInfo->Mask[0];

                switch(c)
                {
                case '\\':
                    c = '\\';
                    break;
                case '\0':
                    c = '\0';
                    break;
                case 'a':
                    c = '\a';
                    break;
                case 'b':
                    c = '\b';
                    break;
                case 'f':
                    c = '\f';
                    break;
                case 'n':
                    c = '\n';
                    break;
                case 'r':
                    c = '\r';
                    break;
                case 't':
                    c = '\t';
                    break;
                case 'v':
                    c = '\v';
                    break;
                }
            }

            pInfo->Buffer[0] = c;
            pInfo->Buffer ++;

            pInfo->Mask ++;
            pInfo->msk_len--;
            break;
        }
        }
    }
    pInfo->Result[pInfo->out_len] = '\0';

    result  = pInfo->Result;
    *Length = pInfo->out_len;

    /* Liberamos a memoria alocada */
    hb_xfree( pInfo );
    return result;
}
Example #30
0
//------------------------------------------------------------------------------
// IEventHandler's Invoke()
// self is where the action happens
// self function receives events (by their ID number) and distributes the processing
// or them or ignores them
static ULONG STDMETHODCALLTYPE Invoke( IEventHandler *self, DISPID dispid, REFIID riid,
   LCID lcid, WORD wFlags, DISPPARAMS *params, VARIANT *result, EXCEPINFO *pexcepinfo,
   UINT *puArgErr )
{
   PHB_ITEM pItem;
   int iArg, i;
   PHB_ITEM pItemArray[32]; // max 32 parameters?
   PHB_ITEM *pItems;
   ULONG ulRefMask = 0;
   ULONG ulPos;
   PHB_ITEM Key;

   Key = hb_itemNew( NULL );

   // We implement only a "default" interface
   if ( !IsEqualIID( riid, &IID_NULL ) )
      return( DISP_E_UNKNOWNINTERFACE );

   HB_SYMBOL_UNUSED(lcid);
   HB_SYMBOL_UNUSED(wFlags);
   HB_SYMBOL_UNUSED(result);
   HB_SYMBOL_UNUSED(pexcepinfo);
   HB_SYMBOL_UNUSED(puArgErr);

   // delegate work to somewhere else in PRG
   //***************************************

#ifdef __USEHASHEVENTS

   if ( hb_hashScan( ((MyRealIEventHandler* ) self)->pEvents, hb_itemPutNL( Key, dispid ),
      &ulPos ) )
   {
      PHB_ITEM pArray = hb_hashGetValueAt( ((MyRealIEventHandler* ) self)->pEvents, ulPos );

#else

   ulPos = hb_arrayScan( ((MyRealIEventHandler* ) self)->pEvents, hb_itemPutNL( Key, dispid ),
      NULL, NULL, 0
   #ifdef __XHARBOUR__
      , 0
   #endif
      );

   if ( ulPos )
   {
      PHB_ITEM pArray = hb_arrayGetItemPtr( ((MyRealIEventHandler* ) self)->pEventsExec, ulPos );

#endif

      PHB_ITEM pExec  = hb_arrayGetItemPtr( pArray, 01 );

      if ( pExec )
      {
         hb_vmRequestReenter();

         switch ( hb_itemType( pExec ) )
         {
            case HB_IT_BLOCK:
            {
               hb_vmPushSymbol( &hb_symEval );
               hb_vmPush( pExec );
               break;
            }
            case HB_IT_STRING:
            {
               PHB_ITEM pObject = hb_arrayGetItemPtr( pArray, 2 );
               hb_vmPushSymbol( hb_dynsymSymbol( hb_dynsymFindName( hb_itemGetCPtr( pExec ) ) ) );

               if ( HB_IS_OBJECT( pObject ) )
                  hb_vmPush( pObject );
               else
                  hb_vmPushNil();
               break;
            }
            case HB_IT_POINTER:
            {
               hb_vmPushSymbol( hb_dynsymSymbol( ( (PHB_SYMB) pExec ) -> pDynSym ) );
               hb_vmPushNil();
               break;
            }
         }

         iArg = params->cArgs;
         for( i = 1; i<= iArg; i++ )
         {
            pItem = hb_itemNew(NULL);
            hb_oleVariantToItem( pItem, &(params->rgvarg[iArg-i]) );
            pItemArray[i-1] = pItem;
            // set bit i
            ulRefMask |= ( 1L << (i-1) );
         }

         if( iArg )
         {
            pItems = pItemArray;
            hb_itemPushList( ulRefMask, iArg, &pItems );
         }

         // execute
         hb_vmDo( (USHORT) iArg );

         // En caso de que los parametros sean pasados por referencia
         for( i=iArg; i > 0; i-- )
         {
            if( ( (&(params->rgvarg[iArg-i]))->n1.n2.vt & VT_BYREF ) == VT_BYREF )
            {
               switch( (&(params->rgvarg[iArg-i]))->n1.n2.vt )
               {
                  //case VT_UI1|VT_BYREF:
                  //   *((&(params->rgvarg[iArg-i]))->n1.n2.n3.pbVal) = va_arg(argList,unsigned char*);  //pItemArray[i-1]
                  //   break;
                  case VT_I2|VT_BYREF:
                     *((&(params->rgvarg[iArg-i]))->n1.n2.n3.piVal)    = (short)          hb_itemGetNI(pItemArray[i-1]);
                     break;
                  case VT_I4|VT_BYREF:
                     *((&(params->rgvarg[iArg-i]))->n1.n2.n3.plVal)    = (long)           hb_itemGetNL(pItemArray[i-1]);
                     break;
                  case VT_R4|VT_BYREF:
                     *((&(params->rgvarg[iArg-i]))->n1.n2.n3.pfltVal)  = (float)          hb_itemGetND(pItemArray[i-1]);
                     break;
                  case VT_R8|VT_BYREF:
                     *((&(params->rgvarg[iArg-i]))->n1.n2.n3.pdblVal)  = (double)         hb_itemGetND(pItemArray[i-1]);
                     break;
                  case VT_BOOL|VT_BYREF:
                     *((&(params->rgvarg[iArg-i]))->n1.n2.n3.pboolVal) =  (VARIANT_BOOL)  ( hb_itemGetL( pItemArray[i-1] ) ? 0xFFFF : 0 );
                     break;
                  //case VT_ERROR|VT_BYREF:
                  //   *((&(params->rgvarg[iArg-i]))->n1.n2.n3.pscode) = va_arg(argList, SCODE*);
                  //   break;
                  case VT_DATE|VT_BYREF:
                     *((&(params->rgvarg[iArg-i]))->n1.n2.n3.pdate)    = (DATE) (double) (hb_itemGetDL(pItemArray[i-1])-2415019 );
                     break;
                  //case VT_CY|VT_BYREF:
                  //   *((&(params->rgvarg[iArg-i]))->n1.n2.n3.pcyVal) = va_arg(argList, CY*);
                  //   break;
                  //case VT_BSTR|VT_BYREF:
                  //   *((&(params->rgvarg[iArg-i]))->n1.n2.n3.pbstrVal = va_arg(argList, BSTR*);
                  //   break;
                  //case VT_UNKNOWN|VT_BYREF:
                  //   pArg->ppunkVal = va_arg(argList, LPUNKNOWN*);
                  //   break;
                  //case VT_DISPATCH|VT_BYREF:
                  //   pArg->ppdispVal = va_arg(argList, LPDISPATCH*);
                  //   break;
               } // EOF switch( (&(params->rgvarg[iArg-i]))->n1.n2.vt )
            } // EOF if( (&(params->rgvarg[iArg-i]))->n1.n2.vt & VT_BYREF == VT_BYREF )
         } // EOF for( i=iArg; i > 0; i-- )

         hb_vmRequestRestore();
      } // EOF if ( pExec )
   }  // EOF If Scan

   hb_itemRelease( Key );

   return S_OK;
}  // EOF invoke

//------------------------------------------------------------------------------
// Here's IEventHandler's VTable. It never changes so we can declare it static
static const IEventHandlerVtbl IEventHandler_Vtbl = {
   QueryInterface,
   AddRef,
   Release,
   GetTypeInfoCount,
   GetTypeInfo,
   GetIDsOfNames,
   Invoke
};

//------------------------------------------------------------------------------
// constructor
// params:
// device_interface        - refers to the interface type of the COM object (whose event we are trying to receive).
// device_event_interface  - indicates the interface type of the outgoing interface supported by the COM object.
//                           This will be the interface that must be implemented by the Sink object.
//                           is essentially derived from IDispatch, our Sink object (self IEventHandler)
//                           is also derived from IDispatch.

typedef IEventHandler device_interface;

// Hash  // SetupConnectionPoint( oOle:hObj, @hSink, hEvents )             -> nError
// Array // SetupConnectionPoint( oOle:hObj, @hSink, aEvents, aExecEvent ) -> nError

HB_FUNC( SETUPCONNECTIONPOINT )
{
   IConnectionPointContainer*  pIConnectionPointContainerTemp = NULL;
   IUnknown*                   pIUnknown = NULL;
   IConnectionPoint*           m_pIConnectionPoint;
   IEnumConnectionPoints*      m_pIEnumConnectionPoints;
   HRESULT                     hr; //,r;
   IID                         rriid;
   register IEventHandler *    selfobj;
   DWORD                       dwCookie = 0;

   device_interface*           pdevice_interface = (device_interface*) HB_PARNL( 1 );
   MyRealIEventHandler*        pThis;

   // Allocate our IEventHandler object (actually a MyRealIEventHandler)
   // intentional misrepresentation of size

   selfobj = ( IEventHandler *) GlobalAlloc( GMEM_FIXED, sizeof( MyRealIEventHandler ) );

   if ( ! selfobj )
   {
      hr = E_OUTOFMEMORY;
   }
   else
   {
      // Store IEventHandler's VTable in the object
      selfobj->lpVtbl = (IEventHandlerVtbl *) &IEventHandler_Vtbl;

      // Increment the reference count so we can call Release() below and
      // it will deallocate only if there is an error with QueryInterface()
      ((MyRealIEventHandler *) selfobj)->count = 0;

      //((MyRealIEventHandler *) selfobj)->device_event_interface_iid = &riid;
      ((MyRealIEventHandler *) selfobj)->device_event_interface_iid = IID_IDispatch;

      // Query self object itself for its IUnknown pointer which will be used
      // later to connect to the Connection Point of the device_interface object.
      hr = selfobj->lpVtbl->QueryInterface( selfobj, &IID_IUnknown, (void**) (void *) &pIUnknown );
      if ( hr == S_OK && pIUnknown )
      {
         // Query the pdevice_interface for its connection point.
         hr = pdevice_interface->lpVtbl->QueryInterface( pdevice_interface,
            &IID_IConnectionPointContainer, (void**) (void *) &pIConnectionPointContainerTemp );

         if ( hr == S_OK && pIConnectionPointContainerTemp )
         {
            // start uncomment
            hr = pIConnectionPointContainerTemp->lpVtbl->EnumConnectionPoints( pIConnectionPointContainerTemp, &m_pIEnumConnectionPoints );

            if ( hr == S_OK && m_pIEnumConnectionPoints )
            {
               do
               {
                  hr = m_pIEnumConnectionPoints->lpVtbl->Next( m_pIEnumConnectionPoints, 1, &m_pIConnectionPoint , NULL);
                  if( hr == S_OK )
                  {
                     if ( m_pIConnectionPoint->lpVtbl->GetConnectionInterface( m_pIConnectionPoint, &rriid ) == S_OK )
                     {
                        break;
                     }
                  }

               } while( hr == S_OK );
               m_pIEnumConnectionPoints->lpVtbl->Release(m_pIEnumConnectionPoints);
            }
            // end uncomment

            //hr = pIConnectionPointContainerTemp ->lpVtbl->FindConnectionPoint(pIConnectionPointContainerTemp ,  &IID_IDispatch, &m_pIConnectionPoint);
            pIConnectionPointContainerTemp->lpVtbl->Release( pIConnectionPointContainerTemp );
            pIConnectionPointContainerTemp = NULL;
         }

         if ( hr == S_OK && m_pIConnectionPoint )
         {
            //OutputDebugString("getting iid");
            //Returns the IID of the outgoing interface managed by self connection point.
            //hr = m_pIConnectionPoint->lpVtbl->GetConnectionInterface(m_pIConnectionPoint, &rriid );
            //OutputDebugString("called");

            if( hr == S_OK )
            {
               ((MyRealIEventHandler *) selfobj)->device_event_interface_iid = rriid;
            }
            else
               OutputDebugString("error getting iid");

            //OutputDebugString("calling advise");
            hr = m_pIConnectionPoint->lpVtbl->Advise( m_pIConnectionPoint, pIUnknown, &dwCookie );
            ((MyRealIEventHandler *) selfobj)->pIConnectionPoint = m_pIConnectionPoint;
            ((MyRealIEventHandler *) selfobj)->dwEventCookie = dwCookie;
         }

         pIUnknown->lpVtbl->Release(pIUnknown);
         pIUnknown = NULL;
      }
   }

   if( selfobj )
   {
      pThis = (MyRealIEventHandler *) selfobj;

#ifndef __USEHASHEVENTS
      pThis->pEventsExec = hb_itemNew( hb_param( 4, HB_IT_ANY ) );
#endif

      pThis->pEvents = hb_itemNew( hb_param( 3, HB_IT_ANY ) );
      HB_STORNL2( (LONG_PTR) pThis, 2 );
   }

   HWNDret( hr );
}

//------------------------------------------------------------------------------
HB_FUNC( SHUTDOWNCONNECTIONPOINT )
{
   MyRealIEventHandler *self = ( MyRealIEventHandler * ) HB_PARNL( 1 );
   if( self->pIConnectionPoint )
   {
      self->pIConnectionPoint->lpVtbl->Unadvise( self->pIConnectionPoint, self->dwEventCookie );
      self->dwEventCookie = 0;
      self->pIConnectionPoint->lpVtbl->Release( self->pIConnectionPoint );
      self->pIConnectionPoint = NULL;
   }
}

//------------------------------------------------------------------------------
HB_FUNC( RELEASEDISPATCH )
{
   IDispatch * pObj;
   pObj = ( IDispatch * ) HWNDparam( 1 );
   pObj->lpVtbl->Release( pObj );
}