static void ChkInverted( long * lStart, long * lEnd, unsigned long ulValue, BOOL bInvert )
{
if( bInvert )
{
*lStart = ulValue;
}
else
{
*lEnd = ulValue;
}
}
unsigned int InternalSeek( MYSQL_RES* mresult, int iData, unsigned int uiField, BOOL bSoft, char * szSearch )
{
MYSQL_ROW row;
unsigned long * pulFieldLengths;
int iRet = -1;
unsigned int uiLen = strlen( szSearch );
char * szTempSearch = (char *) hb_xgrab( sizeof( char * ) * uiLen );
char * szSource;
hb_strncpy( szTempSearch, szSearch, uiLen );
mysql_data_seek( mresult, iData );
row = mysql_fetch_row( mresult );
pulFieldLengths = mysql_fetch_lengths( mresult ) ;
if( pulFieldLengths[ uiField ] != 0 )
{
if( bSoft )
{
szSource = (char *) hb_xgrab( sizeof( char * ) * uiLen );
hb_strncpy( szSource, row[ uiField ], uiLen );
hb_strLower( szSource, uiLen );
hb_strLower( szTempSearch, uiLen );
}
else
{
szSource = (char *) hb_xgrab( sizeof( char * ) * pulFieldLengths[ uiField ] );
hb_strncpy( szSource, row[ uiField ], pulFieldLengths[ uiField ] );
}
if( IS_NUM( mresult->fields[ uiField ].type ) )
{
iRet = _fltcmp( atof( szTempSearch ), atof( szSource ) );
}
else
{
setlocale( LC_COLLATE, szLang );
iRet = strcoll( (const char *) szSource, (const char *) szTempSearch );
}
hb_xfree( szSource );
}
hb_xfree( szTempSearch );
return (unsigned int) iRet;
}
HB_FUNC( MYSEEK2 )
{
MYSQL_RES * mresult = (MYSQL_RES *) hb_MYSQL_RES_par( 1 );
unsigned int uiField = (unsigned int) hb_parni( 2 ) - 1;
char * szSearch = (char *) hb_parc( 3 );
BOOL bSoft = hb_parl( 6 );
BOOL bInverted = hb_pcount() > 6 ? hb_parl( 7 ) : FALSE;
unsigned int uiRet, uiRet2;
long lOk = -1;
long lStart, lStart2, lMid, lEnd;
long lLastFound;
ChkInverted( &lEnd,
&lStart,
( HB_ISNUM( 4 ) ? (unsigned long) hb_parnl( 4 ) - 1 : 0 ),
bInverted );
if( mresult )
{
if( !HB_ISNUM( 5 ) )
{
ChkInverted( &lStart, &lEnd, mysql_num_rows( mresult ), bInverted );
}
else
{
ChkInverted( &lStart, &lEnd, (unsigned long) hb_parnl( 5 ), bInverted );
}
uiRet = InternalSeek( mresult, 0, uiField, bSoft, szSearch );
if( uiRet == 0 )
{
lOk = 0;
}
lMid = ( lEnd + lStart ) / 2;
while( ( bInverted ? lStart > lMid : lStart < lMid ) && lOk < 0 )
{
uiRet = InternalSeek( mresult, lMid, uiField, bSoft, szSearch );
if( uiRet == (unsigned int) ( bInverted ? 1 : -1 ) )
{
ChkInverted( &lEnd, &lStart, (unsigned long) lMid, bInverted );
}
else if( uiRet == (unsigned int) ( bInverted ? -1 : 1 ) )
{
ChkInverted( &lStart, &lEnd, (unsigned long) lMid, bInverted );
}
else
{
lLastFound = lMid;
lStart2 = lLastFound - 1;
while( lLastFound > lStart2 )
{
uiRet2 = InternalSeek( mresult, lStart2, uiField, bSoft, szSearch );
if( uiRet2 != 0 )
{
break;
}
else
{
lLastFound = lStart2;
}
lStart2 = lLastFound - 1;
if( lStart2 < 0 )
{
break;
}
}
lOk = lLastFound;
break;
}
lMid = ( lEnd + lStart ) / 2;
}
}
lOk = lOk >= 0 ? lOk + 1 : 0;
hb_retnl( (unsigned long) lOk );
}