From bd4750043f3a3e2bb8a4b04e50e3ec684f8dcbfb Mon Sep 17 00:00:00 2001 From: Przemyslaw Czerpak Date: Wed, 2 Dec 2009 02:12:14 +0000 Subject: [PATCH] 2009-12-02 03:12 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/doc/en-EN/hb_apifs.txt * harbour/include/hbapifs.h * harbour/src/rtl/filesys.c * changed: BOOL hb_fsSetDevMode( HB_FHANDLE hFileHandle, USHORT uiDevMode ) to: int hb_fsSetDevMode( HB_FHANDLE hFileHandle, int iDevMode ) Now it returns previous mode for hFileHandle or -1 on error. if iDevMode == 0 then it does not change device mode. * harbour/src/rtl/philes.c * modified HB_FSETDEVMODE() function to work with new hb_fsSetDevMode() Current syntax is: HB_FSETDEVMODE( [, ] ) -> It may return FD_BINARY, FD_TEXT or FS_ERROR. * harbour/src/rtl/philes53.c * modified FSETDEVMOD() function to work with new hb_fsSetDevMode() It's similar to HB_FSETDEVMODE() but returns FD_BINARY instead of FS_ERROR. Such behavior is more closer to CL53. * harbour/src/rtl/console.c * set also STDIN into binary mode --- harbour/ChangeLog | 25 ++++++++++++ harbour/doc/en-EN/hb_apifs.txt | 6 +-- harbour/include/hbapifs.h | 2 +- harbour/src/rtl/console.c | 2 + harbour/src/rtl/filesys.c | 72 ++++++++++++++++++---------------- harbour/src/rtl/philes.c | 15 +++---- harbour/src/rtl/philes53.c | 16 ++++++-- 7 files changed, 90 insertions(+), 48 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index b55a5a6f31..f10daffb33 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -17,6 +17,31 @@ past entries belonging to author(s): Viktor Szakats. */ +2009-12-02 03:12 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) + * harbour/doc/en-EN/hb_apifs.txt + * harbour/include/hbapifs.h + * harbour/src/rtl/filesys.c + * changed: + BOOL hb_fsSetDevMode( HB_FHANDLE hFileHandle, USHORT uiDevMode ) + to: + int hb_fsSetDevMode( HB_FHANDLE hFileHandle, int iDevMode ) + Now it returns previous mode for hFileHandle or -1 on error. + if iDevMode == 0 then it does not change device mode. + + * harbour/src/rtl/philes.c + * modified HB_FSETDEVMODE() function to work with new hb_fsSetDevMode() + Current syntax is: + HB_FSETDEVMODE( [, ] ) -> + It may return FD_BINARY, FD_TEXT or FS_ERROR. + + * harbour/src/rtl/philes53.c + * modified FSETDEVMOD() function to work with new hb_fsSetDevMode() + It's similar to HB_FSETDEVMODE() but returns FD_BINARY instead of + FS_ERROR. Such behavior is more closer to CL53. + + * harbour/src/rtl/console.c + * set also STDIN into binary mode + 2009-12-01 18:09 UTC-0800 Pritpal Bedi (pritpal@vouchcac.com) * contrib/hbide/hbide.prg * contrib/hbxbp/xbpstatusbar.prg diff --git a/harbour/doc/en-EN/hb_apifs.txt b/harbour/doc/en-EN/hb_apifs.txt index 75b591dfeb..6151f2c366 100644 --- a/harbour/doc/en-EN/hb_apifs.txt +++ b/harbour/doc/en-EN/hb_apifs.txt @@ -1069,13 +1069,13 @@ * C Prototype * * #include - * hb_fsSetDevMode( HB_FHANDLE hFileHandle, USHORT uiDevMode ) --> BOOL + * hb_fsSetDevMode( HB_FHANDLE hFileHandle, int iDevMode ) --> int * $ARGUMENTS$ * * - * + * * $RETURNS$ - * TRUE when successful, FALSE when not. + * previous mode or -1 on error * $DESCRIPTION$ * * $EXAMPLES$ diff --git a/harbour/include/hbapifs.h b/harbour/include/hbapifs.h index fc5c802a7e..8f63801720 100644 --- a/harbour/include/hbapifs.h +++ b/harbour/include/hbapifs.h @@ -192,7 +192,7 @@ extern HB_EXPORT BOOL hb_fsRename ( const char * pszOldName, const ch extern HB_EXPORT ULONG hb_fsSeek ( HB_FHANDLE hFileHandle, LONG lOffset, USHORT uiMode ); /* reposition an open file */ extern HB_EXPORT HB_FOFFSET hb_fsSeekLarge ( HB_FHANDLE hFileHandle, HB_FOFFSET llOffset, USHORT uiFlags ); /* reposition an open file using 64bit API */ extern HB_EXPORT ULONG hb_fsTell ( HB_FHANDLE hFileHandle ); /* retrieve the current position of a file */ -extern HB_EXPORT BOOL hb_fsSetDevMode ( HB_FHANDLE hFileHandle, USHORT uiDevMode ); /* change the device mode of a file (text/binary) */ +extern HB_EXPORT int hb_fsSetDevMode ( HB_FHANDLE hFileHandle, int iDevMode ); /* change the device mode of a file (text/binary) */ extern HB_EXPORT BOOL hb_fsGetFileTime ( const char * pszFileName, long * plJulian, long * plMillisec ); extern HB_EXPORT BOOL hb_fsSetFileTime ( const char * pszFileName, long lJulian, long lMillisec ); extern HB_EXPORT BOOL hb_fsGetAttr ( const char * pszFileName, ULONG * pulAttr ); diff --git a/harbour/src/rtl/console.c b/harbour/src/rtl/console.c index 0165955240..654223ff9a 100644 --- a/harbour/src/rtl/console.c +++ b/harbour/src/rtl/console.c @@ -154,6 +154,7 @@ void hb_conInit( void ) * Some compilers open stdout and stderr in text mode, but * Harbour needs them to be open in binary mode. */ + hb_fsSetDevMode( s_hFilenoStdin, FD_BINARY ); hb_fsSetDevMode( s_hFilenoStdout, FD_BINARY ); hb_fsSetDevMode( s_hFilenoStderr, FD_BINARY ); @@ -183,6 +184,7 @@ void hb_conRelease( void ) hb_gtExit(); + hb_fsSetDevMode( s_hFilenoStdin, FD_TEXT ); hb_fsSetDevMode( s_hFilenoStdout, FD_TEXT ); hb_fsSetDevMode( s_hFilenoStderr, FD_TEXT ); } diff --git a/harbour/src/rtl/filesys.c b/harbour/src/rtl/filesys.c index 8e115f19d5..aaf2965a83 100644 --- a/harbour/src/rtl/filesys.c +++ b/harbour/src/rtl/filesys.c @@ -842,53 +842,66 @@ void hb_fsClose( HB_FHANDLE hFileHandle ) #endif } -BOOL hb_fsSetDevMode( HB_FHANDLE hFileHandle, USHORT uiDevMode ) + +#define FD_TEST 0 + +int hb_fsSetDevMode( HB_FHANDLE hFileHandle, int iDevMode ) { - HB_TRACE(HB_TR_DEBUG, ("hb_fsSetDevMode(%p, %hu)", ( void * ) ( HB_PTRDIFF ) hFileHandle, uiDevMode)); + HB_TRACE(HB_TR_DEBUG, ("hb_fsSetDevMode(%p, %d)", ( void * ) ( HB_PTRDIFF ) hFileHandle, iDevMode)); /* TODO: HB_IO_WIN support */ #if defined( __BORLANDC__ ) || defined( __IBMCPP__ ) || defined( __DJGPP__ ) || \ defined( __CYGWIN__ ) || defined( __WATCOMC__ ) || defined( HB_OS_OS2 ) { - int iRet = 0; + int iRet = -1; #if defined( HB_IO_WIN ) - if( hFileHandle != ( HB_FHANDLE ) 0 && - hFileHandle != ( HB_FHANDLE ) 1 && - hFileHandle != ( HB_FHANDLE ) 2 ) - iRet = -1; - else + if( hFileHandle == ( HB_FHANDLE ) 0 || + hFileHandle == ( HB_FHANDLE ) 1 || + hFileHandle == ( HB_FHANDLE ) 2 ) #endif - switch( uiDevMode ) + switch( iDevMode ) { + case FD_TEST: + iRet = setmode( ( int ) hFileHandle, O_BINARY ); + if( iRet != -1 ) + setmode( ( int ) hFileHandle, iRet ); + break; + case FD_BINARY: - iRet = setmode( ( HB_NHANDLE ) hFileHandle, O_BINARY ); + iRet = setmode( ( int ) hFileHandle, O_BINARY ); break; case FD_TEXT: - iRet = setmode( ( HB_NHANDLE ) hFileHandle, O_TEXT ); + iRet = setmode( ( int ) hFileHandle, O_TEXT ); break; } + if( iRet != -1 ) + iRet = ( iRet & O_TEXT ) == O_TEXT ? FD_TEXT : FD_BINARY; hb_fsSetIOError( iRet != -1, 0 ); - return iRet != -1; + return iRet; } #elif ( defined( _MSC_VER ) || defined( __MINGW32__ ) || defined( __DMC__ ) ) && \ !defined( HB_OS_WIN_CE ) { - int iRet = 0; + int iRet = -1; #if defined( HB_IO_WIN ) - if( hFileHandle != ( HB_FHANDLE ) 0 && - hFileHandle != ( HB_FHANDLE ) 1 && - hFileHandle != ( HB_FHANDLE ) 2 ) - iRet = -1; - else + if( hFileHandle == ( HB_FHANDLE ) 0 || + hFileHandle == ( HB_FHANDLE ) 1 || + hFileHandle == ( HB_FHANDLE ) 2 ) #endif - switch( uiDevMode ) + switch( iDevMode ) { + case FD_TEST: + iRet = _setmode( ( int ) hFileHandle, _O_BINARY ); + if( iRet != -1 ) + _setmode( ( int ) hFileHandle, iRet ); + break; + case FD_BINARY: iRet = _setmode( ( int ) hFileHandle, _O_BINARY ); break; @@ -898,27 +911,18 @@ BOOL hb_fsSetDevMode( HB_FHANDLE hFileHandle, USHORT uiDevMode ) break; } + if( iRet != -1 ) + iRet = ( iRet & _O_TEXT ) == _O_TEXT ? FD_TEXT : FD_BINARY; hb_fsSetIOError( iRet != -1, 0 ); - return iRet != -1; + return iRet; } -#elif defined( HB_OS_UNIX ) || defined( HB_OS_WIN_CE ) +#else HB_SYMBOL_UNUSED( hFileHandle ); - if( uiDevMode == FD_TEXT ) - { - hb_fsSetError( ( HB_ERRCODE ) FS_ERROR ); - return FALSE; - } - - hb_fsSetError( 0 ); - return TRUE; - -#else - - hb_fsSetError( ( HB_ERRCODE ) FS_ERROR ); - return FALSE; + hb_fsSetError( ( HB_ERRCODE ) ( iDevMode == FD_TEXT ? FS_ERROR : 0 ) ); + return FD_BINARY; #endif } diff --git a/harbour/src/rtl/philes.c b/harbour/src/rtl/philes.c index ff97b9998b..09a14ea824 100644 --- a/harbour/src/rtl/philes.c +++ b/harbour/src/rtl/philes.c @@ -419,16 +419,17 @@ HB_FUNC( HB_FGETDATETIME ) HB_FUNC( HB_FSETDEVMODE ) { - /* C53 checks only number of parameters: hb_pcount() == 2 */ - if( HB_ISNUM( 1 ) && HB_ISNUM( 2 ) ) + int iRet = -1; + + if( HB_ISNUM( 1 ) ) { - hb_fsSetDevMode( hb_numToHandle( hb_parnint( 1 ) ), ( USHORT ) hb_parni( 2 ) ); + iRet = hb_fsSetDevMode( hb_numToHandle( hb_parnint( 1 ) ), hb_parni( 2 ) ); hb_fsSetFError( hb_fsError() ); } - /* NOTE: INCOMPATIBILITY! C53 will return the device flags - before applying the new setting, Harbour will - always return 0. [vszakats] */ - hb_retni( 0 ); + else + hb_fsSetFError( 6 ); /* ERROR_INVALID_HANDLE */ + + hb_retni( iRet ); } HB_FUNC( HB_OSERROR ) diff --git a/harbour/src/rtl/philes53.c b/harbour/src/rtl/philes53.c index f10fd27ad7..e4ddb159ac 100644 --- a/harbour/src/rtl/philes53.c +++ b/harbour/src/rtl/philes53.c @@ -57,11 +57,21 @@ /* NOTE: Clipper 5.3 undocumented */ -HB_FUNC_EXTERN( HB_FSETDEVMODE ); - HB_FUNC( FSETDEVMOD ) { - HB_FUNC_EXEC( HB_FSETDEVMODE ); + int iRet = FD_BINARY; + + if( HB_ISNUM( 1 ) ) + { + iRet = hb_fsSetDevMode( hb_numToHandle( hb_parnint( 1 ) ), hb_parni( 2 ) ); + if( iRet != FD_TEXT ) + iRet = FD_BINARY; + hb_fsSetFError( hb_fsError() ); + } + else + hb_fsSetFError( 6 ); /* ERROR_INVALID_HANDLE */ + + hb_retni( iRet ); } #endif