From 7fbc9044fe8eeac675d193585f85143661b90f68 Mon Sep 17 00:00:00 2001 From: Przemyslaw Czerpak Date: Sat, 21 May 2011 10:02:49 +0000 Subject: [PATCH] 2011-05-21 12:02 UTC+0200 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/src/rtl/valtoexp.prg ! fixed typo in recent modifications causing RTE in HB_VALTOEXP() * harbour/contrib/hbwin/win_prn2.c ! allocate dynamic memory for print buffer in WIN_PRINTFILERAW() 32KB variable on process execution stack is potential source of random GPFs in programs using deeper recursive calls and killer for MT mode when smaller thread stack is allocated. ! fixed potential data lost in WIN_PRINTFILERAW() when not all data is transfered in single WritePrinter() call ! interrupt printing in WIN_PRINTFILERAW() if WritePrinter() returns error * harbour/include/harbour.hbx * harbour/src/rtl/hbrandom.c ! restored my old fix - it was correct and the results over RAND_MAX range expected + added new PRG function: HB_RANDOMMAX() -> --- harbour/ChangeLog | 21 ++++++++++++++++++++ harbour/contrib/hbwin/win_prn2.c | 33 +++++++++++++++++++++++++++----- harbour/include/harbour.hbx | 1 + harbour/src/rtl/hbrandom.c | 24 ++++++++++------------- harbour/src/rtl/valtoexp.prg | 2 +- 5 files changed, 61 insertions(+), 20 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index af17e2af3c..5088ab5a1b 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,27 @@ The license applies to all entries newer than 2009-04-28. */ +2011-05-21 12:02 UTC+0200 Przemyslaw Czerpak (druzus/at/priv.onet.pl) + * harbour/src/rtl/valtoexp.prg + ! fixed typo in recent modifications causing RTE in HB_VALTOEXP() + + * harbour/contrib/hbwin/win_prn2.c + ! allocate dynamic memory for print buffer in WIN_PRINTFILERAW() + 32KB variable on process execution stack is potential source of + random GPFs in programs using deeper recursive calls and killer + for MT mode when smaller thread stack is allocated. + ! fixed potential data lost in WIN_PRINTFILERAW() when not all data + is transfered in single WritePrinter() call + ! interrupt printing in WIN_PRINTFILERAW() if WritePrinter() returns + error + + * harbour/include/harbour.hbx + * harbour/src/rtl/hbrandom.c + ! restored my old fix - it was correct and the results over RAND_MAX + range expected + + added new PRG function: + HB_RANDOMMAX() -> + 2011-05-20 17:27 UTC-0800 Pritpal Bedi (bedipritpal@hotmail.com) * contrib/hbide/idethemes.prg + Added: "NOT" as Harbour keyword. diff --git a/harbour/contrib/hbwin/win_prn2.c b/harbour/contrib/hbwin/win_prn2.c index fdd8a06fb9..633c8ccf5b 100644 --- a/harbour/contrib/hbwin/win_prn2.c +++ b/harbour/contrib/hbwin/win_prn2.c @@ -370,6 +370,8 @@ HB_FUNC( WIN_PRINTERPORTTONAME ) #endif } +#define HB_PRINT_BUFFER_SIZE ( 32 * 1024 ) + HB_FUNC( WIN_PRINTFILERAW ) { int iResult = -1; @@ -399,12 +401,15 @@ HB_FUNC( WIN_PRINTFILERAW ) if( fhnd != FS_ERROR ) { - HB_BYTE pbyBuffer[ 32 * 1024 ]; - DWORD dwWritten = 0; + HB_BYTE * pbyBuffer = ( HB_BYTE * ) hb_xgrab( HB_PRINT_BUFFER_SIZE ); HB_SIZE nRead; - while( ( nRead = hb_fsReadLarge( fhnd, pbyBuffer, sizeof( pbyBuffer ) ) ) > 0 ) + iResult = 1; + + while( ( nRead = hb_fsReadLarge( fhnd, pbyBuffer, HB_PRINT_BUFFER_SIZE ) ) > 0 ) { + DWORD nWritten = 0; + #if 0 /* TOFIX: This check seems wrong for any input files larger than our read buffer, in such case it @@ -414,11 +419,29 @@ HB_FUNC( WIN_PRINTFILERAW ) nRead--; /* Skip the EOF() character */ #endif - WritePrinter( hPrinter, pbyBuffer, ( DWORD ) nRead, &dwWritten ); + while( nWritten < nRead ) + { + DWORD dwWritten = 0; + if( !WritePrinter( hPrinter, &pbyBuffer[ nWritten ], + ( DWORD ) ( nRead - nWritten ), + &dwWritten ) ) + { + iResult = -7; + break; + } + else if( nWritten == 0 ) + { + iResult = -8; + break; + } + nWritten += dwWritten; + } + if( nWritten < nRead ) + break; } - iResult = 1; hb_fsClose( fhnd ); + hb_xfree( pbyBuffer ); } else iResult = -6; diff --git a/harbour/include/harbour.hbx b/harbour/include/harbour.hbx index a0b4cd48de..0509652ffa 100644 --- a/harbour/include/harbour.hbx +++ b/harbour/include/harbour.hbx @@ -695,6 +695,7 @@ DYNAMIC HB_RAND32 DYNAMIC HB_RANDOM DYNAMIC HB_RANDOMINT DYNAMIC HB_RANDOMSEED +DYNAMIC HB_RANDOMMAX DYNAMIC HB_RASCAN DYNAMIC HB_RAT DYNAMIC HB_RDDGETTEMPALIAS diff --git a/harbour/src/rtl/hbrandom.c b/harbour/src/rtl/hbrandom.c index 656856eeac..4094a8b0ba 100644 --- a/harbour/src/rtl/hbrandom.c +++ b/harbour/src/rtl/hbrandom.c @@ -124,8 +124,17 @@ HB_FUNC( HB_RANDOMSEED ) s_fInit = HB_TRUE; } +HB_FUNC( HB_RANDOMMAX ) +{ +#if RAND_MAX > HB_VMLONG_MAX + hb_retnd( RAND_MAX ); +#else + hb_retnint( RAND_MAX ); +#endif +} + /* Returns a double value between 0 and 1 */ -double hb_random_num() +double hb_random_num( void ) { double d1, d2; @@ -136,20 +145,7 @@ double hb_random_num() } d1 = ( double ) rand(); -#ifdef __HB_THIS_FIX_BREAKS_MINGW32_64__ d2 = ( double ) RAND_MAX + 1.0; -#else - d2 = ( double ) RAND_MAX; -#if defined( __BORLANDC__ ) - /* It seems that on Windows platform there some weirdness about EPSILON value so - that a float division using an epsilon smaller than 1e-10 may be rounded. - Must dig if it's a borland lib bug or a windows problem. - */ - d2 += 0.001; -#else - d2 += DBL_EPSILON; -#endif -#endif return d1 / d2; } diff --git a/harbour/src/rtl/valtoexp.prg b/harbour/src/rtl/valtoexp.prg index 9a96610686..a4ea6c7315 100644 --- a/harbour/src/rtl/valtoexp.prg +++ b/harbour/src/rtl/valtoexp.prg @@ -59,7 +59,7 @@ FUNCTION hb_VALTOEXP( xVal ) SWITCH v CASE "C" CASE "M" ; RETURN hb_StrToExp( xVal ) - CASE "N" ; hb_ntos( xVal ) + CASE "N" ; RETURN hb_ntos( xVal ) CASE "D" ; RETURN iif( Empty( xVal ), "0d00000000", "0d" + DToS( xVal ) ) CASE "T" ; RETURN 't"' + hb_TSToStr( xVal, .T. ) + '"' CASE "L" ; RETURN iif( xVal, ".T.", ".F." )