From 62478c56577b66130ac4eeb2cb6ce9368387b6b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Czerpak?= Date: Fri, 3 Oct 2014 16:03:55 +0200 Subject: [PATCH] 2014-10-03 16:03 UTC+0200 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) * src/rdd/wafunc.c % use dynamic symbol pointer to find field index * src/rdd/workarea.c * set default maximum field size to HB_SYMBOL_NAME_LEN characters (by default in Harbour builds HB_SYMBOL_NAME_LEN is 63) Some RDDs may set longer names but it will be respected only by FieldName() function and in all other cases only HB_SYMBOL_NAME_LEN characters is significant for Harbour so such decision should be well thought by RDD authors because it may confuse users. Warning: all 3-rd party RDDs which need to force shorter names should update uiMaxFieldNameLength in their NEW() method just after SUPER_NEW() call, i.e. like in core DBF RDD. ! respect uiMaxFieldNameLength when new fields are added * src/rdd/dbf1.c * set maximum field size to 10 characters * contrib/rddsql/sqlbase.c - removed code used to set uiMaxFieldNameLength to HB_SYMBOL_NAME_LEN. Now it's default field length value for each RDD unless it does not change it. * contrib/rddads/ads1.c * minor modification in adsFieldName() * contrib/hbwin/wapi_winbase.c + added support for FORMAT_MESSAGE_ALLOCATE_BUFFER and 6-th parameter to wapi_FormatMessage() function. The 6-th parameter has higher priority then size of string passed in the 5-th parameter. ! fixed potential memory leak --- ChangeLog.txt | 33 +++++++++++++++++++++++++ contrib/hbwin/wapi_winbase.c | 48 ++++++++++++++++++++++++++---------- contrib/rddads/ads1.c | 16 ++++++------ contrib/rddsql/sqlbase.c | 15 +---------- src/rdd/dbf1.c | 3 +++ src/rdd/wafunc.c | 25 +++++++++---------- src/rdd/workarea.c | 8 +++--- 7 files changed, 97 insertions(+), 51 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index fa402d48fd..87517bfe52 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -10,6 +10,39 @@ * Change, ! Fix, % Optimization, + Addition, - Removal, ; Comment */ +2014-10-03 16:03 UTC+0200 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) + * src/rdd/wafunc.c + % use dynamic symbol pointer to find field index + + * src/rdd/workarea.c + * set default maximum field size to HB_SYMBOL_NAME_LEN characters + (by default in Harbour builds HB_SYMBOL_NAME_LEN is 63) + Some RDDs may set longer names but it will be respected only by + FieldName() function and in all other cases only HB_SYMBOL_NAME_LEN + characters is significant for Harbour so such decision should be + well thought by RDD authors because it may confuse users. + Warning: all 3-rd party RDDs which need to force shorter names + should update uiMaxFieldNameLength in their NEW() method + just after SUPER_NEW() call, i.e. like in core DBF RDD. + ! respect uiMaxFieldNameLength when new fields are added + + * src/rdd/dbf1.c + * set maximum field size to 10 characters + + * contrib/rddsql/sqlbase.c + - removed code used to set uiMaxFieldNameLength to HB_SYMBOL_NAME_LEN. + Now it's default field length value for each RDD unless it does not + change it. + + * contrib/rddads/ads1.c + * minor modification in adsFieldName() + + * contrib/hbwin/wapi_winbase.c + + added support for FORMAT_MESSAGE_ALLOCATE_BUFFER and 6-th + parameter to wapi_FormatMessage() function. The 6-th parameter + has higher priority then size of string passed in the 5-th parameter. + ! fixed potential memory leak + 2014-10-02 22:55 UTC+0200 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) * contrib/hbwin/win_prn2.c + save LastError value in win_PrintDataRaw() and win_PrintFileRaw() diff --git a/contrib/hbwin/wapi_winbase.c b/contrib/hbwin/wapi_winbase.c index 5468c52884..3aedfda6d9 100644 --- a/contrib/hbwin/wapi_winbase.c +++ b/contrib/hbwin/wapi_winbase.c @@ -277,23 +277,39 @@ HB_FUNC( WAPI_OUTPUTDEBUGSTRING ) HB_FUNC( WAPI_FORMATMESSAGE ) { void * hSource = NULL; - void * hBuffer; - - HB_SIZE nBufferLen; - - LPTSTR lpBuffer; + LPTSTR lpAllocBuff = NULL; + LPTSTR lpBuffer = NULL; + HB_SIZE nSize = 0; DWORD dwRetVal; + DWORD dwFlags; - ( void ) HB_PARSTR( 5, &hBuffer, &nBufferLen ); + dwFlags = ( DWORD ) hb_parnldef( 1, FORMAT_MESSAGE_FROM_SYSTEM ); - lpBuffer = nBufferLen > 0 ? ( LPTSTR ) hb_xgrab( nBufferLen * sizeof( TCHAR ) ) : NULL; + if( HB_ISBYREF( 5 ) ) + { + nSize = hb_parns( 6 ); + if( ( dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER ) == 0 ) + { + if( nSize == 0 && ! HB_ISNUM( 6 ) ) + nSize = hb_parclen( 5 ); + if( nSize > 0 ) + lpBuffer = ( LPTSTR ) hb_xgrab( nSize * sizeof( TCHAR ) ); + else + dwFlags |= FORMAT_MESSAGE_ALLOCATE_BUFFER; + } + } + else + dwFlags = ~FORMAT_MESSAGE_ALLOCATE_BUFFER; - dwRetVal = FormatMessage( ( DWORD ) hb_parnldef( 1, FORMAT_MESSAGE_FROM_SYSTEM ) /* dwFlags */, + if( dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER ) + lpBuffer = ( LPTSTR ) &lpAllocBuff; + + dwRetVal = FormatMessage( dwFlags, HB_ISCHAR( 2 ) ? ( LPCVOID ) HB_PARSTR( 2, &hSource, NULL ) : hb_parptr( 2 ), HB_ISNUM( 3 ) ? ( DWORD ) hb_parnl( 3 ) : hbwapi_GetLastError() /* dwMessageId */, ( DWORD ) hb_parnldef( 4, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ) ) /* dwLanguageId */, lpBuffer, - ( DWORD ) nBufferLen, + ( DWORD ) nSize, NULL /* TODO: Add support for this parameter. */ ); hbwapi_SetLastError( GetLastError() ); @@ -301,14 +317,20 @@ HB_FUNC( WAPI_FORMATMESSAGE ) if( lpBuffer ) { + if( dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER ) + lpBuffer = lpAllocBuff; + else + lpBuffer[ nSize - 1 ] = '\0'; + HB_STORSTR( dwRetVal ? lpBuffer : NULL, 5 ); - hb_xfree( lpBuffer ); + + if( lpAllocBuff ) + LocalFree( lpAllocBuff ); + else if( lpBuffer ) + hb_xfree( lpBuffer ); } - else - hb_storc( NULL, 5 ); hb_strfree( hSource ); - hb_strfree( hBuffer ); } HB_FUNC( WAPI_MULDIV ) diff --git a/contrib/rddads/ads1.c b/contrib/rddads/ads1.c index ad42e1a180..db02cd28c5 100644 --- a/contrib/rddads/ads1.c +++ b/contrib/rddads/ads1.c @@ -2087,16 +2087,18 @@ static HB_ERRCODE adsFieldInfo( ADSAREAP pArea, HB_USHORT uiIndex, HB_USHORT uiT static HB_ERRCODE adsFieldName( ADSAREAP pArea, HB_USHORT uiIndex, void * szName ) { - UNSIGNED16 u16Len = pArea->area.uiMaxFieldNameLength + 1; - HB_TRACE( HB_TR_DEBUG, ( "adsFieldName(%p, %hu, %p)", pArea, uiIndex, szName ) ); - if( uiIndex > pArea->area.uiFieldCount ) + if( uiIndex <= pArea->area.uiFieldCount ) + { + UNSIGNED16 u16Len = pArea->area.uiMaxFieldNameLength + 1; + + AdsGetFieldName( pArea->hTable, uiIndex, ( UNSIGNED8 * ) szName, &u16Len ); + + return HB_SUCCESS; + } + else return HB_FAILURE; - - AdsGetFieldName( pArea->hTable, uiIndex, ( UNSIGNED8 * ) szName, &u16Len ); - - return HB_SUCCESS; } static HB_ERRCODE adsFlush( ADSAREAP pArea ) diff --git a/contrib/rddsql/sqlbase.c b/contrib/rddsql/sqlbase.c index a760424a13..8e57829a43 100644 --- a/contrib/rddsql/sqlbase.c +++ b/contrib/rddsql/sqlbase.c @@ -781,19 +781,6 @@ static HB_ERRCODE sqlbaseInfo( SQLBASEAREAP pArea, HB_USHORT uiIndex, PHB_ITEM p } -static HB_ERRCODE sqlbaseNewArea( SQLBASEAREAP pArea ) -{ - HB_ERRCODE errCode; - - HB_TRACE( HB_TR_DEBUG, ( "sqlbaseNewArea(%p)", pArea ) ); - - errCode = SUPER_NEW( ( AREAP ) pArea ); - pArea->area.uiMaxFieldNameLength = HB_SYMBOL_NAME_LEN; - - return errCode; -} - - static HB_ERRCODE sqlbaseOpen( SQLBASEAREAP pArea, LPDBOPENINFO pOpenInfo ) { HB_ERRCODE errCode; @@ -1146,7 +1133,7 @@ static RDDFUNCS sqlbaseTable = ( DBENTRYP_V ) sqlbaseClose, ( DBENTRYP_VO ) sqlbaseCreate, ( DBENTRYP_SI ) sqlbaseInfo, - ( DBENTRYP_V ) sqlbaseNewArea, + ( DBENTRYP_V ) NULL, /* sqlbaseNewArea */ ( DBENTRYP_VO ) sqlbaseOpen, ( DBENTRYP_V ) NULL, /* sqlbaseRelease */ ( DBENTRYP_SP ) sqlbaseStructSize, diff --git a/src/rdd/dbf1.c b/src/rdd/dbf1.c index c8c15c1108..6e298640d7 100644 --- a/src/rdd/dbf1.c +++ b/src/rdd/dbf1.c @@ -3929,6 +3929,9 @@ static HB_ERRCODE hb_dbfNewArea( DBFAREAP pArea ) if( SUPER_NEW( ( AREAP ) pArea ) == HB_FAILURE ) return HB_FAILURE; + /* set maximum fieldname length to 10 characters */ + pArea->area.uiMaxFieldNameLength = 10; + pArea->pDataFile = pArea->pMemoFile = pArea->pMemoTmpFile = NULL; pArea->fDataFlush = pArea->fMemoFlush = HB_FALSE; /* Index dirty read flag initialized to global RDD setting */ diff --git a/src/rdd/wafunc.c b/src/rdd/wafunc.c index 1b4aa62f31..d70b9aa324 100644 --- a/src/rdd/wafunc.c +++ b/src/rdd/wafunc.c @@ -205,28 +205,27 @@ void * hb_rddAllocWorkAreaAlias( const char * szAlias, int iArea ) */ HB_USHORT hb_rddFieldIndex( AREAP pArea, const char * szName ) { - HB_USHORT uiCount = 0; - LPFIELD pField; - HB_TRACE( HB_TR_DEBUG, ( "hb_rddFieldIndex(%p, %s)", pArea, szName ) ); while( HB_ISSPACE( *szName ) ) - { ++szName; - } if( *szName ) { - char szSym[ HB_SYMBOL_NAME_LEN + 1 ]; - hb_strncpyUpperTrim( szSym, szName, sizeof( szSym ) - 1 ); + PHB_DYNS pDynSym = hb_dynsymFindName( szName ); - pField = pArea->lpFields; - while( pField ) + if( pDynSym ) { - ++uiCount; - if( strcmp( szSym, hb_dynsymName( ( PHB_DYNS ) pField->sym ) ) == 0 ) - return uiCount; - pField = pField->lpfNext; + LPFIELD pField = pArea->lpFields; + HB_USHORT uiCount = 0; + + while( pField ) + { + ++uiCount; + if( pDynSym == ( PHB_DYNS ) pField->sym ) + return uiCount; + pField = pField->lpfNext; + } } } return 0; diff --git a/src/rdd/workarea.c b/src/rdd/workarea.c index 1e996eb7ab..5b6bf454ab 100644 --- a/src/rdd/workarea.c +++ b/src/rdd/workarea.c @@ -243,10 +243,9 @@ static HB_ERRCODE hb_waAddField( AREAP pArea, LPDBFIELDINFO pFieldInfo ) /* Validate the name of field */ szPtr = pFieldInfo->atomName; while( HB_ISSPACE( *szPtr ) ) - { ++szPtr; - } - hb_strncpyUpperTrim( szFieldName, szPtr, sizeof( szFieldName ) - 1 ); + hb_strncpyUpperTrim( szFieldName, szPtr, + HB_MIN( HB_SYMBOL_NAME_LEN, pArea->uiMaxFieldNameLength ) ); if( szFieldName[ 0 ] == 0 ) return HB_FAILURE; @@ -261,6 +260,7 @@ static HB_ERRCODE hb_waAddField( AREAP pArea, LPDBFIELDINFO pFieldInfo ) pField->uiFlags = pFieldInfo->uiFlags; pField->uiArea = pArea->uiArea; pArea->uiFieldCount++; + return HB_SUCCESS; } @@ -850,7 +850,7 @@ static HB_ERRCODE hb_waNewArea( AREAP pArea ) pArea->valResult = hb_itemNew( NULL ); pArea->lpdbRelations = NULL; pArea->uiParents = 0; - pArea->uiMaxFieldNameLength = 10; + pArea->uiMaxFieldNameLength = HB_SYMBOL_NAME_LEN; return HB_SUCCESS; }