/* helper function */ static void do_charevod( int iSwitch ) { if( HB_ISCHAR( 1 ) ) { const char * pcString = hb_parc( 1 ); HB_SIZE sLen = hb_parclen( 1 ); char * pcResult; HB_SIZE sPos, sResultPos; if( sLen == 0 ) { int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) ct_error( ( HB_USHORT ) iArgErrorMode, EG_ARG, iSwitch == DO_CHAREVOD_CHAREVEN ? CT_ERROR_CHAREVEN : CT_ERROR_CHARODD, NULL, HB_ERR_FUNCNAME, 0, EF_CANDEFAULT, HB_ERR_ARGS_BASEPARAMS ); hb_retc_null(); return; } pcResult = ( char * ) hb_xgrab( ( sLen + 1 ) / 2 ); if( iSwitch == DO_CHAREVOD_CHAREVEN ) sPos = 1; else sPos = 0; sResultPos = 0; for( ; sPos < sLen; sPos += 2 ) pcResult[ sResultPos++ ] = pcString[ sPos ]; if( sResultPos == 0 ) hb_retc_null(); else hb_retclen( pcResult, sResultPos ); hb_xfree( pcResult ); } else { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, iSwitch == DO_CHAREVOD_CHAREVEN ? CT_ERROR_CHAREVEN : CT_ERROR_CHARODD, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else hb_retc_null(); } }
static void hb_hrbDo( PHRB_BODY pHrbBody, int iPCount, PHB_ITEM * pParams ) { PHB_ITEM pRetVal = NULL; int i; hb_hrbInit( pHrbBody, iPCount, pParams ); /* May not have a startup symbol, if first symbol was an INIT Symbol (was executed already). */ if( pHrbBody->lSymStart >= 0 && hb_vmRequestQuery() == 0 ) { hb_vmPushSymbol( &pHrbBody->pSymRead[ pHrbBody->lSymStart ] ); hb_vmPushNil(); for( i = 0; i < iPCount; i++ ) hb_vmPush( pParams[ i ] ); hb_vmProc( ( HB_USHORT ) iPCount ); pRetVal = hb_itemNew( NULL ); hb_itemMove( pRetVal, hb_stackReturnItem() ); } if( pRetVal ) hb_itemReturnRelease( pRetVal ); }
void hbgi_hb_itemReturnRelease(PHB_ITEM pItem) { if (pItem != hb_stackReturnItem()) { hb_itemReturnRelease(pItem); } }
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 ) ); } }
void hb_errRT_BASE_SubstR( HB_ERRCODE errGenCode, HB_ERRCODE errSubCode, const char * szDescription, const char * szOperation, HB_ULONG ulArgCount, ... ) { PHB_ITEM pError; PHB_ITEM pArray; va_list va; HB_ULONG ulArgPos; pError = hb_errRT_New_Subst( ES_ERROR, HB_ERR_SS_BASE, errGenCode, errSubCode, szDescription, szOperation, 0, EF_NONE ); /* Build the array from the passed arguments. */ switch( ulArgCount ) { case 0: pArray = NULL; break; case HB_ERR_ARGS_BASEPARAMS: pArray = hb_pcount() ? hb_arrayBaseParams() : NULL; break; case HB_ERR_ARGS_SELFPARAMS: pArray = hb_arraySelfParams(); break; default: pArray = hb_itemArrayNew( ulArgCount ); va_start( va, ulArgCount ); for( ulArgPos = 1; ulArgPos <= ulArgCount; ulArgPos++ ) { PHB_ITEM pArg = va_arg( va, PHB_ITEM ); if( pArg ) hb_itemArrayPut( pArray, ulArgPos, pArg ); } va_end( va ); } if( pArray ) { /* Assign the new array to the object data item. */ hb_errPutArgsArray( pError, pArray ); /* Release the Array. */ hb_itemRelease( pArray ); } /* Ok, launch... */ hb_itemReturnRelease( hb_errLaunchSubst( pError ) ); hb_errRelease( pError ); }
/* helper function */ static void do_ascpos( int iSwitch ) { if( HB_ISCHAR( 1 ) ) { HB_SIZE sStrSize = hb_parclen( 1 ); const HB_BYTE * pcString = ( const HB_BYTE * ) hb_parc( 1 ); HB_SIZE sPos = hb_parnsdef( 2, sStrSize ); if( sPos == 0 || sPos > sStrSize ) hb_retni( 0 ); else { if( iSwitch == DO_ASCPOS_VALPOS ) { if( HB_ISDIGIT( ( HB_UCHAR ) pcString[ sPos - 1 ] ) ) hb_retni( pcString[ sPos - 1 ] - '0' ); else hb_retni( 0 ); } else hb_retni( pcString[ sPos - 1 ] ); } } else { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, iSwitch == DO_ASCPOS_VALPOS ? CT_ERROR_VALPOS : CT_ERROR_ASCPOS, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else hb_retni( 0 ); } }
/* helper function for the charswap and wordswap functions */ static void do_charswap( int iSwitch ) { /* suppress return value ? */ int iNoRet = ct_getref() && HB_ISBYREF( 1 ); /* param check */ if( HB_ISCHAR( 1 ) ) { const char * pcString = hb_parc( 1 ); HB_SIZE sStrLen = hb_parclen( 1 ); char * pcRet; HB_SIZE sRetIndex = 0; int iShift, iMod; const char * pcSub; if( sStrLen == 0 ) { if( iNoRet ) hb_ret(); else hb_retc_null(); return; } if( iSwitch == DO_CHARSWAP_WORDSWAP ) { iShift = 4; if( hb_parl( 2 ) ) iSwitch = DO_CHARSWAP_WORDSWAP_CHARSWAP; } else iShift = 2; pcRet = ( char * ) hb_xgrab( sStrLen ); for( pcSub = pcString; pcSub < pcString + sStrLen + 1 - iShift; pcSub += iShift ) { switch( iSwitch ) { case DO_CHARSWAP_WORDSWAP: pcRet[ sRetIndex++ ] = pcSub[ 2 ]; pcRet[ sRetIndex++ ] = pcSub[ 3 ]; pcRet[ sRetIndex++ ] = pcSub[ 0 ]; pcRet[ sRetIndex++ ] = pcSub[ 1 ]; break; case DO_CHARSWAP_WORDSWAP_CHARSWAP: pcRet[ sRetIndex++ ] = pcSub[ 3 ]; pcRet[ sRetIndex++ ] = pcSub[ 2 ]; /* no 'break' here !! */ case DO_CHARSWAP_CHARSWAP: pcRet[ sRetIndex++ ] = pcSub[ 1 ]; pcRet[ sRetIndex++ ] = pcSub[ 0 ]; } } /* copy rest of string */ if( iSwitch == DO_CHARSWAP_WORDSWAP || iSwitch == DO_CHARSWAP_WORDSWAP_CHARSWAP ) iMod = sStrLen % 4; else iMod = sStrLen % 2; for( pcSub = pcString + sStrLen - iMod; pcSub < pcString + sStrLen; pcSub++ ) pcRet[ sRetIndex++ ] = *pcSub; /* return string */ hb_storclen( pcRet, sRetIndex, 1 ); if( iNoRet ) hb_retl( HB_FALSE ); else hb_retclen( pcRet, sRetIndex ); hb_xfree( pcRet ); } else { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) { if( iSwitch == DO_CHARSWAP_CHARSWAP ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, CT_ERROR_CHARSWAP, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); else pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, CT_ERROR_WORDSWAP, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); } if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else if( iNoRet ) hb_retl( HB_FALSE ); else hb_retc_null(); } }
/* helper function for the Pos*() functions */ static void do_pos1( int iSwitch ) { if( HB_ISCHAR( 1 ) && /* all functions need string as 1st param */ ( iSwitch != DO_POS1_POSRANGE || /* that's the only condition for all functions _except_ PosRange() */ ( iSwitch == DO_POS1_POSRANGE && /* In addition, PosRange() needs .. */ HB_ISCHAR( 2 ) && /* .. string as 2nd .. */ HB_ISCHAR( 3 ) ) ) ) /* .. and 3rd param */ { const unsigned char * pcString, * puc; HB_SIZE sStrLen; unsigned char ucChar1 = ' ', ucChar2 = ' '; int iMode; HB_SIZE sIgnore; int iParamShift = 0; if( iSwitch == DO_POS1_POSRANGE ) { if( hb_parclen( 1 ) == 0 ) { hb_retns( 0 ); return; } else ucChar1 = *( hb_parc( 1 ) ); if( hb_parclen( 2 ) == 0 ) { hb_retns( 0 ); return; } else ucChar2 = *( hb_parc( 2 ) ); iParamShift += 2; } pcString = ( const unsigned char * ) hb_parc( iParamShift + 1 ); sStrLen = hb_parclen( iParamShift + 1 ); iMode = hb_parl( iParamShift + 2 ); sIgnore = hb_parnsdef( iParamShift + 3, 0 ); for( puc = pcString + sIgnore; puc < pcString + sStrLen; puc++ ) { int iDoRet = 0; switch( iSwitch ) { case DO_POS1_POSALPHA: iDoRet = hb_charIsAlpha( ( HB_UCHAR ) *puc ); break; case DO_POS1_POSLOWER: iDoRet = hb_charIsLower( ( HB_UCHAR ) *puc ); break; case DO_POS1_POSRANGE: iDoRet = ( ucChar1 <= *puc && ucChar2 >= *puc ); break; case DO_POS1_POSUPPER: iDoRet = hb_charIsUpper( ( HB_UCHAR ) *puc ); break; } if( ( iMode && ! iDoRet ) || ( ! iMode && iDoRet ) ) { hb_retns( puc - pcString + 1 ); return; } } hb_retns( 0 ); } else { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) { HB_ERRCODE iError = 0; switch( iSwitch ) { case DO_POS1_POSALPHA: iError = CT_ERROR_POSALPHA; break; case DO_POS1_POSLOWER: iError = CT_ERROR_POSLOWER; break; case DO_POS1_POSRANGE: iError = CT_ERROR_POSRANGE; break; case DO_POS1_POSUPPER: iError = CT_ERROR_POSUPPER; break; } pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, iError, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); } if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else hb_retns( 0 ); } }
/* helper function for the list function */ void ct_charlist( int iMode ) { const char * pcString = hb_parc( 1 ); HB_SIZE sStrLen = hb_parclen( 1 ); HB_SIZE sCnt; if( iMode == CT_CHARLIST_CHARHIST ) { HB_SIZE asCharCnt[ UCHAR_MAX ]; PHB_ITEM pArray = hb_itemArrayNew( HB_SIZEOFARRAY( asCharCnt ) ); for( sCnt = 0; sCnt < HB_SIZEOFARRAY( asCharCnt ); ++sCnt ) asCharCnt[ sCnt ] = 0; for( sCnt = 0; sCnt < sStrLen; ++sCnt ) asCharCnt[ ( HB_UCHAR ) pcString[ sCnt ] ]++; for( sCnt = 0; sCnt < HB_SIZEOFARRAY( asCharCnt ); ++sCnt ) hb_arraySetNS( pArray, sCnt + 1, asCharCnt[ sCnt ] ); hb_itemReturnRelease( pArray ); } else { char acCharCnt[ UCHAR_MAX ]; HB_SIZE sRetStrLen = 0; if( iMode == CT_CHARLIST_CHARLIST ) { char acMark[ UCHAR_MAX ]; memset( acMark, 0, sizeof( acMark ) ); for( sCnt = 0; sCnt < sStrLen; ++sCnt ) { HB_UCHAR uc = ( HB_UCHAR ) pcString[ sCnt ]; if( acMark[ uc ] == 0 ) { acCharCnt[ sRetStrLen++ ] = uc; acMark[ uc ] = 1; } } } else if( iMode == CT_CHARLIST_CHARSLIST || iMode == CT_CHARLIST_CHARNOLIST ) { char cScan = iMode == CT_CHARLIST_CHARSLIST ? 1 : 0; for( sCnt = 0; sCnt < sStrLen; ++sCnt ) acCharCnt[ ( HB_UCHAR ) pcString[ sCnt ] ] = 1; for( sCnt = 0; sCnt < HB_SIZEOFARRAY( acCharCnt ); ++sCnt ) { if( acCharCnt[ sCnt ] == cScan ) acCharCnt[ sRetStrLen++ ] = ( HB_UCHAR ) sCnt; } } hb_retclen( acCharCnt, sRetStrLen ); } }
/* helper function for the justxxx() functions */ static void do_justify( int iSwitch ) { int iNoRet; iNoRet = ct_getref() && ISBYREF( 1 ); if( ISCHAR( 1 ) ) { const char * pcString = hb_parc( 1 ); HB_SIZE sStrLen = hb_parclen( 1 ); char cJustChar; const char * pc; char * pcRet, * pcw; HB_SIZE sJustOffset; if( sStrLen == 0 ) { if( iNoRet ) hb_ret(); else hb_retc_null(); return; } if( hb_parclen( 2 ) > 0 ) cJustChar = *( hb_parc( 2 ) ); else if( ISNUM( 2 ) ) cJustChar = ( char ) ( hb_parnl( 2 ) % 256 ); else cJustChar = 0x20; pcRet = ( char * ) hb_xgrab( sStrLen + 1 ); switch( iSwitch ) { case DO_JUSTIFY_JUSTLEFT: pc = pcString; sJustOffset = 0; while( ( *pc == cJustChar ) && ( pc < pcString + sStrLen ) ) { sJustOffset++; pc++; } hb_xmemcpy( pcRet, pcString + sJustOffset, (size_t) ( sStrLen - sJustOffset ) ); for( pcw = pcRet + sStrLen - sJustOffset; pcw < pcRet + sStrLen; pcw++ ) { *pcw = cJustChar; } break; case DO_JUSTIFY_JUSTRIGHT: pc = pcString + sStrLen - 1; sJustOffset = 0; while( ( *pc == cJustChar ) && ( pc >= pcString ) ) { sJustOffset++; pc--; } for( pcw = pcRet; pcw < pcRet + sJustOffset; pcw++ ) { *pcw = cJustChar; } hb_xmemcpy( pcRet + sJustOffset, pcString, (size_t) ( sStrLen - sJustOffset ) ); break; } if( ISBYREF( 1 ) ) hb_storclen( pcRet, sStrLen, 1 ); if( iNoRet ) { hb_ret(); hb_xfree( pcRet ); } else hb_retclen_buffer( pcRet, sStrLen ); } else /* ISCHAR( 1 ) */ { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) { pSubst = ct_error_subst( ( USHORT ) iArgErrorMode, EG_ARG, iSwitch == DO_JUSTIFY_JUSTLEFT ? CT_ERROR_JUSTLEFT : CT_ERROR_JUSTRIGHT, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); } if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else if( iNoRet ) hb_ret(); else hb_retc_null(); } }
/* helper function for the remxxx functions */ static void do_remove( int iSwitch ) { /* param check */ if( HB_ISCHAR( 1 ) ) { const char * pcString = hb_parc( 1 ); HB_SIZE sStrLen = hb_parclen( 1 ); const char * pcRet; const char * pc; HB_SIZE sRetLen; char cSearch; if( hb_parclen( 2 ) > 0 ) cSearch = *( hb_parc( 2 ) ); else if( HB_ISNUM( 2 ) ) cSearch = ( char ) ( hb_parnl( 2 ) % 256 ); else cSearch = 0x20; sRetLen = sStrLen; pcRet = pcString; if( iSwitch != DO_REMOVE_REMRIGHT ) { while( ( *pcRet == cSearch ) && ( pcRet < pcString + sStrLen ) ) { pcRet++; sRetLen--; } } if( iSwitch != DO_REMOVE_REMLEFT ) { pc = pcString + sStrLen - 1; while( ( *pc == cSearch ) && ( pc >= pcRet ) ) { pc--; sRetLen--; } } if( sRetLen == 0 ) hb_retc_null(); else hb_retclen( pcRet, sRetLen ); } else { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, sulErrorSubcodes[ iSwitch ], NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else hb_retc_null(); } }
/* helper function for the Count*() functions */ static void do_count( int iSwitch ) { /* param check */ if( HB_ISCHAR( 1 ) ) { const char * pcString = hb_parc( 1 ); HB_SIZE sStrLen = hb_parclen( 1 ); HB_SIZE sRetVal; const char * pc; char cSearch; if( hb_parclen( 2 ) > 0 ) cSearch = *( hb_parc( 2 ) ); else if( HB_ISNUM( 2 ) ) cSearch = ( char ) ( hb_parnl( 2 ) % 256 ); else cSearch = 0x20; sRetVal = 0; switch( iSwitch ) { case DO_COUNT_COUNTLEFT: pc = pcString; while( *pc == cSearch && pc < ( pcString + sStrLen ) ) { sRetVal++; pc++; } break; case DO_COUNT_COUNTRIGHT: pc = pcString + sStrLen - 1; while( *pc == cSearch && pc >= pcString ) { sRetVal++; pc--; } break; } hb_retns( sRetVal ); } else { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, iSwitch == DO_COUNT_COUNTLEFT ? CT_ERROR_COUNTLEFT : CT_ERROR_COUNTRIGHT, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else hb_retns( 0 ); } }
/* helper function */ static void do_atnum( int iSwitch ) { if( HB_ISCHAR( 1 ) && HB_ISCHAR( 2 ) ) { const char * pcStringToMatch = hb_parc( 1 ); HB_SIZE sStrToMatchLen = hb_parclen( 1 ); const char * pcString = hb_parc( 2 ); HB_SIZE sStrLen = hb_parclen( 2 ); int iMultiPass = ct_getatmupa(); int iAtLike = ct_getatlike(); char cAtLike = ct_getatlikechar(); HB_SIZE sIgnore = hb_parnsdef( 4, 0 ); /* eventually ignore some characters */ HB_SIZE sMatchStrLen = 0; HB_SIZE nCounter; const char * pc = NULL; if( sIgnore >= sStrLen ) { switch( iSwitch ) { case DO_ATNUM_AFTERATNUM: { /* AFTERATNUM */ int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) ct_error( ( HB_USHORT ) iArgErrorMode, EG_ARG, CT_ERROR_AFTERATNUM, NULL, HB_ERR_FUNCNAME, 0, EF_CANDEFAULT, HB_ERR_ARGS_BASEPARAMS ); hb_retc_null(); break; } case DO_ATNUM_BEFORATNUM: { /* BEFORATNUM */ int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) ct_error( ( HB_USHORT ) iArgErrorMode, EG_ARG, CT_ERROR_BEFORATNUM, NULL, HB_ERR_FUNCNAME, 0, EF_CANDEFAULT, HB_ERR_ARGS_BASEPARAMS ); hb_retc_null(); break; } case DO_ATNUM_ATNUM: { /* ATNUM */ int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) ct_error( ( HB_USHORT ) iArgErrorMode, EG_ARG, CT_ERROR_ATNUM, NULL, HB_ERR_FUNCNAME, 0, EF_CANDEFAULT, HB_ERR_ARGS_BASEPARAMS ); hb_retns( 0 ); break; } } return; } else { pcString += sIgnore; sStrLen -= sIgnore; } /* nth match or last match ? */ if( HB_ISNUM( 3 ) && ( nCounter = hb_parns( 3 ) ) != 0 ) { /* find the <nCounter>th match */ const char * pcSubStr; HB_SIZE sSubStrLen; HB_SIZE nMatchCounter = 0; pcSubStr = pcString; sSubStrLen = sStrLen; while( nMatchCounter < nCounter ) { switch( iAtLike ) { case CT_SETATLIKE_EXACT: pc = ct_at_exact_forward( pcSubStr, sSubStrLen, pcStringToMatch, sStrToMatchLen, &sMatchStrLen ); break; case CT_SETATLIKE_WILDCARD: pc = ct_at_wildcard_forward( pcSubStr, sSubStrLen, pcStringToMatch, sStrToMatchLen, cAtLike, &sMatchStrLen ); break; default: pc = NULL; } if( pc == NULL ) { /* no match found; if this happens at this point, there are no <nCounter> matches, so return an empty string */ switch( iSwitch ) { case DO_ATNUM_AFTERATNUM: case DO_ATNUM_BEFORATNUM: /* AFTERATNUM */ /* BEFORATNUM */ hb_retc_null(); break; case DO_ATNUM_ATNUM: /* ATNUM */ hb_retns( 0 ); break; } return; } nMatchCounter++; if( iMultiPass ) pcSubStr = pc + 1; else pcSubStr = pc + sMatchStrLen; sSubStrLen = sStrLen - ( pcSubStr - pcString ); } } else { /* we have to find the last match and return the string after that last match */ switch( iAtLike ) { case CT_SETATLIKE_EXACT: pc = ct_at_exact_backward( pcString, sStrLen, pcStringToMatch, sStrToMatchLen, &sMatchStrLen ); break; case CT_SETATLIKE_WILDCARD: pc = ct_at_wildcard_backward( pcString, sStrLen, pcStringToMatch, sStrToMatchLen, cAtLike, &sMatchStrLen ); break; default: pc = NULL; } if( pc == NULL ) { /* no matches found */ switch( iSwitch ) { case DO_ATNUM_AFTERATNUM: case DO_ATNUM_BEFORATNUM: /* AFTERATNUM */ /* BEFORATNUM */ hb_retc_null(); break; case DO_ATNUM_ATNUM: /* ATNUM */ hb_retns( 0 ); break; } return; } } switch( iSwitch ) { case DO_ATNUM_AFTERATNUM: /* AFTERATNUM */ if( pc + sMatchStrLen >= pcString + sStrLen ) hb_retc_null(); else hb_retclen( pc + sMatchStrLen, sStrLen - ( pc + sMatchStrLen - pcString ) ); break; case DO_ATNUM_BEFORATNUM: /* BEFORATNUM */ hb_retclen( pcString - sIgnore, pc - ( pcString - sIgnore ) ); break; case DO_ATNUM_ATNUM: /* ATNUM */ #if defined( __POCC__ ) && ( __POCC__ >= 500 ) && defined( HB_OS_WIN_64 ) /* NOTE: Workaround for Pelles C 5.00.13 AMD64 mode internal error: 'fatal error: Internal error: reduce_tree()' [vszakats]. */ hb_retns( pc - pcString + sIgnore + 1 ); #else hb_retns( pc - ( pcString - sIgnore ) + 1 ); #endif break; } } else { switch( iSwitch ) { case DO_ATNUM_AFTERATNUM: case DO_ATNUM_BEFORATNUM: { /* AFTERATNUM */ PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, iSwitch == DO_ATNUM_AFTERATNUM ? CT_ERROR_AFTERATNUM : CT_ERROR_BEFORATNUM, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else hb_retc_null(); break; } case DO_ATNUM_ATNUM: { /* ATNUM */ PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, CT_ERROR_ATNUM, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else hb_retns( 0 ); break; } } } }
/* helper function for the token function group I */ static void do_token1( int iSwitch ) { PCT_TOKEN ct_token = ( PCT_TOKEN ) hb_stackGetTSD( &s_ct_token ); int iParamCheck = 0; int iNoRef = ct_getref() && HB_ISBYREF( 1 ); switch( iSwitch ) { case DO_TOKEN1_TOKEN: ct_token->iPreSeparator = ct_token->iPostSeparator = -1; /* no "break" here !! */ case DO_TOKEN1_ATTOKEN: case DO_TOKEN1_NUMTOKEN: case DO_TOKEN1_TOKENLOWER: case DO_TOKEN1_TOKENUPPER: iParamCheck = HB_ISCHAR( 1 ); break; } if( iParamCheck ) { const char * pcString = hb_parc( 1 ); HB_SIZE sStrLen = hb_parclen( 1 ); const char * pcSeparatorStr; HB_SIZE sSeparatorStrLen; HB_SIZE nTokenCounter; HB_SIZE nSkip; const char * pcSubStr; char * pcRet = NULL; HB_SIZE sSubStrLen; HB_SIZE sRetStrLen = 0; HB_SIZE nToken = 0; HB_SIZE nSkipCnt; const char * pc; /* separator string */ sSeparatorStrLen = hb_parclen( 2 ); if( sSeparatorStrLen != 0 ) pcSeparatorStr = hb_parc( 2 ); else { pcSeparatorStr = sc_pcSeparatorStr; sSeparatorStrLen = sc_sSeparatorStrLen; } if( iSwitch == DO_TOKEN1_NUMTOKEN ) { /* token counter */ nTokenCounter = HB_SIZE_MAX; /* skip width */ nSkip = hb_parns( 3 ); } else { /* token counter */ nTokenCounter = hb_parns( 3 ); /* skip width */ nSkip = hb_parns( 4 ); /* HB_EXTENSION for AtToken()/TokenLower()/TokenUpper() */ } if( nTokenCounter == 0 ) nTokenCounter = HB_SIZE_MAX; if( nSkip == 0 ) nSkip = HB_SIZE_MAX; /* prepare return value for TokenUpper()/TokenLower() */ if( iSwitch == DO_TOKEN1_TOKENLOWER || iSwitch == DO_TOKEN1_TOKENUPPER ) { if( sStrLen == 0 ) { if( iNoRef ) hb_retl( HB_FALSE ); else hb_retc_null(); return; } sRetStrLen = sStrLen; pcRet = ( char * ) hb_xgrab( sRetStrLen + 1 ); hb_xmemcpy( pcRet, pcString, sRetStrLen ); } /* find the <nTokenCounter>th token */ pcSubStr = pcString; sSubStrLen = sStrLen; /* scan start condition */ pc = pcSubStr - 1; while( nToken < nTokenCounter ) { HB_SIZE sMatchedPos = sSeparatorStrLen; /* Skip the left nSkip successive separators */ nSkipCnt = 0; do { sSubStrLen -= ( pc - pcSubStr ) + 1; pcSubStr = pc + 1; pc = ct_at_charset_forward( pcSubStr, sSubStrLen, pcSeparatorStr, sSeparatorStrLen, &sMatchedPos ); if( iSwitch == DO_TOKEN1_TOKEN ) { ct_token->iPreSeparator = ct_token->iPostSeparator; if( sMatchedPos < sSeparatorStrLen ) ct_token->iPostSeparator = pcSeparatorStr[ sMatchedPos ]; else ct_token->iPostSeparator = -1; } nSkipCnt++; } while( nSkipCnt < nSkip && pc == pcSubStr ); if( sSubStrLen == 0 ) { /* string ends with tokenizer (null string after tokenizer at end of string is not a token) */ switch( iSwitch ) { case DO_TOKEN1_TOKEN: { char cRet; hb_retc_null(); if( HB_ISBYREF( 5 ) ) /* HB_EXTENSION */ { cRet = ( char ) ct_token->iPreSeparator; hb_storclen( &cRet, ( ct_token->iPreSeparator != -1 ? 1 : 0 ), 5 ); } if( HB_ISBYREF( 6 ) ) /* HB_EXTENSION */ { cRet = ( char ) ct_token->iPostSeparator; hb_storclen( &cRet, ( ct_token->iPostSeparator != -1 ? 1 : 0 ), 6 ); } break; } case DO_TOKEN1_NUMTOKEN: hb_retns( nToken ); break; case DO_TOKEN1_ATTOKEN: hb_retns( 0 ); break; case DO_TOKEN1_TOKENLOWER: case DO_TOKEN1_TOKENUPPER: hb_storclen( pcRet, sRetStrLen, 1 ); if( iNoRef ) { hb_xfree( pcRet ); hb_retl( HB_FALSE ); } else hb_retclen_buffer( pcRet, sRetStrLen ); break; } return; } switch( iSwitch ) { case DO_TOKEN1_TOKEN: case DO_TOKEN1_NUMTOKEN: case DO_TOKEN1_ATTOKEN: break; case DO_TOKEN1_TOKENLOWER: if( pcSubStr != pc ) /* letters can be tokenizers, too, but they should not be lowercase'd */ *( pcRet + ( pcSubStr - pcString ) ) = ( char ) hb_charLower( ( HB_UCHAR ) *pcSubStr ); break; case DO_TOKEN1_TOKENUPPER: if( pcSubStr != pc ) /* letters can be tokenizers, too, but they should not be uppercase'd */ *( pcRet + ( pcSubStr - pcString ) ) = ( char ) hb_charUpper( ( HB_UCHAR ) *pcSubStr ); break; default: break; } nToken++; if( pc == NULL ) { /* little trick for return values */ pc = pcSubStr + sSubStrLen; /* we must leave the while loop even if we have not yet found the <nTokenCounter>th token */ break; } /* should we find the last token, but string ends with tokenizer, i.e. pc points to the last character at the moment ? -> break here ! */ if( nTokenCounter == HB_SIZE_MAX ) { if( nSkip == HB_SIZE_MAX ) { const char * t; HB_BOOL bLast = HB_TRUE; for( t = pc + 1; t < pcString + sStrLen; t++ ) { if( ! memchr( pcSeparatorStr, *t, sSeparatorStrLen ) ) { bLast = HB_FALSE; break; } } if( bLast ) break; } else if( pc + 1 == pcString + sStrLen ) break; } } switch( iSwitch ) { case DO_TOKEN1_TOKEN: { char cRet; if( nTokenCounter == HB_SIZE_MAX || nToken == nTokenCounter ) hb_retclen( pcSubStr, pc - pcSubStr ); else hb_retc_null(); if( HB_ISBYREF( 5 ) ) /* HB_EXTENSION */ { cRet = ( char ) ct_token->iPreSeparator; hb_storclen( &cRet, ( ct_token->iPreSeparator != -1 ? 1 : 0 ), 5 ); } if( HB_ISBYREF( 6 ) ) /* HB_EXTENSION */ { cRet = ( char ) ct_token->iPostSeparator; hb_storclen( &cRet, ( ct_token->iPostSeparator != -1 ? 1 : 0 ), 6 ); } break; } case DO_TOKEN1_NUMTOKEN: hb_retns( nToken ); break; case DO_TOKEN1_ATTOKEN: if( nTokenCounter == HB_SIZE_MAX || nToken == nTokenCounter ) hb_retns( pcSubStr - pcString + 1 ); else hb_retns( 0 ); break; case DO_TOKEN1_TOKENLOWER: case DO_TOKEN1_TOKENUPPER: hb_storclen( pcRet, sRetStrLen, 1 ); if( iNoRef ) { hb_xfree( pcRet ); hb_retl( HB_FALSE ); } else hb_retclen_buffer( pcRet, sRetStrLen ); break; } } else { switch( iSwitch ) { case DO_TOKEN1_TOKEN: { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); char cRet; if( HB_ISBYREF( 5 ) ) /* HB_EXTENSION */ { cRet = ( char ) ct_token->iPreSeparator; hb_storclen( &cRet, ( ct_token->iPreSeparator != -1 ? 1 : 0 ), 5 ); } if( HB_ISBYREF( 6 ) ) /* HB_EXTENSION */ { cRet = ( char ) ct_token->iPostSeparator; hb_storclen( &cRet, ( ct_token->iPostSeparator != -1 ? 1 : 0 ), 6 ); } if( iArgErrorMode != CT_ARGERR_IGNORE ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, CT_ERROR_TOKEN, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else if( ! iNoRef ) hb_retc_null(); else hb_retl( HB_FALSE ); break; } case DO_TOKEN1_TOKENLOWER: case DO_TOKEN1_TOKENUPPER: { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, iSwitch == DO_TOKEN1_TOKENLOWER ? CT_ERROR_TOKENLOWER : CT_ERROR_TOKENUPPER, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else if( ! iNoRef ) hb_retc_null(); else hb_retl( HB_FALSE ); break; } case DO_TOKEN1_NUMTOKEN: case DO_TOKEN1_ATTOKEN: { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, iSwitch == DO_TOKEN1_NUMTOKEN ? CT_ERROR_NUMTOKEN : CT_ERROR_ATTOKEN, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else hb_retns( 0 ); break; } } } }
/* helper function for the replxxx functions */ static void do_replace( int iSwitch ) { /* suppressing return value ? */ int iNoRet = ct_getref() && HB_ISBYREF( 1 ); /* param check */ if( HB_ISCHAR( 1 ) && ( hb_parclen( 2 ) > 0 || HB_ISNUM( 2 ) ) ) { const char * pcString = hb_parc( 1 ); HB_SIZE sStrLen = hb_parclen( 1 ); char * pcRet, * pc; char cSearch, cReplace; if( sStrLen == 0 ) { if( iNoRet ) hb_ret(); else hb_retc_null(); return; } if( HB_ISNUM( 2 ) ) cReplace = ( char ) ( hb_parnl( 2 ) % 256 ); else cReplace = *( ( const char * ) hb_parc( 2 ) ); if( hb_parclen( 3 ) > 0 ) cSearch = *( ( const char * ) hb_parc( 3 ) ); else if( HB_ISNUM( 3 ) ) cSearch = ( char ) ( hb_parnl( 3 ) % 256 ); else cSearch = 0x20; pcRet = ( char * ) hb_xgrab( sStrLen + 1 ); hb_xmemcpy( pcRet, pcString, sStrLen ); if( iSwitch != DO_REPLACE_REPLRIGHT ) { pc = pcRet; while( *pc == cSearch && pc < pcRet + sStrLen ) { *pc = cReplace; pc++; } } if( iSwitch != DO_REPLACE_REPLLEFT ) { pc = pcRet + sStrLen - 1; while( *pc == cSearch && pc >= pcRet ) { *pc = cReplace; pc--; } } hb_storclen( pcRet, sStrLen, 1 ); if( iNoRet ) { hb_xfree( pcRet ); hb_ret(); } else hb_retclen_buffer( pcRet, sStrLen ); } else { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, sulErrorSubcodes[ iSwitch ], NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else if( iNoRet ) hb_ret(); else hb_retc_null(); } }
/* helper function for the *one functions */ static void do_charonly( int iSwitch ) { /* param check */ if( HB_ISCHAR( 1 ) && HB_ISCHAR( 2 ) ) { const char * pcString = hb_parc( 2 ); HB_SIZE sStrLen = hb_parclen( 2 ); const char * pcOnlySet = hb_parc( 1 ); HB_SIZE sOnlySetLen = hb_parclen( 1 ); char * pcRet; HB_SIZE sRetStrLen = 0; int iShift, iBool; const char * pcSub, * pc; /* check for zero-length strings */ switch( iSwitch ) { case DO_CHARONLY_CHARONLY: case DO_CHARONLY_WORDONLY: if( sStrLen == 0 || sOnlySetLen == 0 ) { hb_retc_null(); return; } break; case DO_CHARONLY_CHARREM: case DO_CHARONLY_WORDREM: if( sStrLen == 0 ) { hb_retc_null(); return; } if( sOnlySetLen == 0 ) { hb_retclen( pcString, sStrLen ); return; } break; } if( iSwitch == DO_CHARONLY_WORDONLY || iSwitch == DO_CHARONLY_WORDREM ) iShift = 2; else iShift = 1; pcRet = ( char * ) hb_xgrab( sStrLen ); for( pcSub = pcString; pcSub < pcString + sStrLen + 1 - iShift; pcSub += iShift ) { pc = ct_at_exact_forward( pcOnlySet, sOnlySetLen, pcSub, iShift, NULL ); iBool = ( ( pc != NULL ) && ( ( ( pc - pcOnlySet ) % iShift ) == 0 ) ); if( iBool ? ( iSwitch == DO_CHARONLY_CHARONLY || iSwitch == DO_CHARONLY_WORDONLY ) : ( iSwitch == DO_CHARONLY_CHARREM || iSwitch == DO_CHARONLY_WORDREM ) ) { for( pc = pcSub; pc < pcSub + iShift; pc++ ) pcRet[ sRetStrLen++ ] = *pc; } } /* copy last character if string len is odd */ if( iShift == 2 && sStrLen % 2 == 1 ) pcRet[ sRetStrLen++ ] = pcString[ sStrLen - 1 ]; hb_retclen( pcRet, sRetStrLen ); hb_xfree( pcRet ); } else { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(), iError = 0; if( iArgErrorMode != CT_ARGERR_IGNORE ) { switch( iSwitch ) { case DO_CHARONLY_CHARONLY: iError = CT_ERROR_CHARONLY; break; case DO_CHARONLY_WORDONLY: iError = CT_ERROR_WORDONLY; break; case DO_CHARONLY_CHARREM: iError = CT_ERROR_CHARREM; break; case DO_CHARONLY_WORDREM: iError = CT_ERROR_WORDREM; break; } pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, iError, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); } if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else hb_retc_null(); } }
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; }
/* helper function for the *one functions */ static void do_charone( int iSwitch ) { const char * pcString; HB_SIZE sStrLen; const char * pcDeleteSet; HB_SIZE sDeleteSetLen; /* param check */ if( HB_ISCHAR( 1 ) ) { if( HB_ISCHAR( 2 ) ) { pcString = hb_parc( 2 ); sStrLen = hb_parclen( 2 ); pcDeleteSet = hb_parc( 1 ); sDeleteSetLen = hb_parclen( 1 ); } else { pcString = hb_parc( 1 ); sStrLen = hb_parclen( 1 ); pcDeleteSet = NULL; sDeleteSetLen = 0; } switch( iSwitch ) { case DO_CHARONE_CHARONE: if( sStrLen > 1 ) { const char * pcSub; char * pcRet; HB_SIZE sRetStrLen = 0; char cCurrent = *pcString; pcRet = ( char * ) hb_xgrab( sStrLen ); /* copy first char */ pcRet[ sRetStrLen++ ] = cCurrent; for( pcSub = pcString + 1; pcSub < pcString + sStrLen; pcSub++ ) { if( *pcSub != cCurrent ) { cCurrent = *pcSub; pcRet[ sRetStrLen++ ] = cCurrent; } else if( pcDeleteSet != NULL && ! ct_at_exact_forward( pcDeleteSet, sDeleteSetLen, pcSub, 1, NULL ) ) { pcRet[ sRetStrLen++ ] = cCurrent; } } hb_retclen( pcRet, sRetStrLen ); hb_xfree( pcRet ); } else { /* algorithm does nothing to 1-char-strings */ hb_retclen( pcString, sStrLen ); } break; case DO_CHARONE_WORDONE: if( sStrLen > 3 && ( pcDeleteSet == NULL || sDeleteSetLen >= 2 ) ) { const char * pcSub; char * pcRet; HB_SIZE sRetStrLen = 0; char cCurrent1 = pcString[ 0 ]; char cCurrent2 = pcString[ 1 ]; pcRet = ( char * ) hb_xgrab( sStrLen ); /* copy first double char */ pcRet[ sRetStrLen++ ] = cCurrent1; pcRet[ sRetStrLen++ ] = cCurrent2; for( pcSub = pcString + 2; pcSub < pcString + sStrLen - 1; pcSub += 2 ) { if( ! ( pcSub[ 0 ] == cCurrent1 && pcSub[ 1 ] == cCurrent2 ) ) { cCurrent1 = pcSub[ 0 ]; cCurrent2 = pcSub[ 1 ]; pcRet[ sRetStrLen++ ] = cCurrent1; pcRet[ sRetStrLen++ ] = cCurrent2; } else if( pcDeleteSet != NULL ) { const char * pc = NULL; const char * pStart = pcDeleteSet; HB_SIZE sLen = sDeleteSetLen; while( sLen >= 2 && ( pc = ct_at_exact_forward( pStart, sLen, pcSub, 2, NULL ) ) != 0 && ( pc - pcDeleteSet ) % 2 == 1 ) { pStart = pc + 1; sLen = sDeleteSetLen - ( pStart - pcDeleteSet ); } if( pc == NULL ) { pcRet[ sRetStrLen++ ] = cCurrent1; pcRet[ sRetStrLen++ ] = cCurrent2; } } } /* copy last character if string len is odd */ if( sStrLen & 1 ) pcRet[ sRetStrLen++ ] = pcString[ sStrLen - 1 ]; hb_retclen( pcRet, sRetStrLen ); hb_xfree( pcRet ); } else { /* algorithm does nothing to 3-char-strings */ hb_retclen( pcString, sStrLen ); } break; } } else { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, iSwitch == DO_CHARONE_CHARONE ? CT_ERROR_CHARONE : CT_ERROR_WORDONE, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else hb_retc_null(); } }
/* helper function for the pad functions */ static void do_pad( int iSwitch ) { if( HB_ISCHAR( 1 ) && HB_ISNUM( 2 ) ) { const char * pcString = hb_parc( 1 ); HB_SIZE sStrLen = hb_parclen( 1 ); char * pcRet, * pc; HB_ISIZ nRetLen; HB_SIZE sRetLen; char cFill; nRetLen = hb_parns( 2 ); if( nRetLen <= 0 ) { int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) ct_error( ( HB_USHORT ) iArgErrorMode, EG_ARG, iSwitch == DO_PAD_PADLEFT ? CT_ERROR_PADLEFT : CT_ERROR_PADRIGHT, NULL, HB_ERR_FUNCNAME, 0, EF_CANDEFAULT, HB_ERR_ARGS_BASEPARAMS ); hb_retc_null(); return; } sRetLen = ( HB_SIZE ) nRetLen; if( hb_parclen( 3 ) > 0 ) cFill = *( hb_parc( 3 ) ); else if( HB_ISNUM( 3 ) ) cFill = ( char ) ( hb_parnl( 3 ) % 256 ); else cFill = 0x20; pcRet = ( char * ) hb_xgrab( sRetLen + 1 ); if( iSwitch == DO_PAD_PADLEFT ) { if( sRetLen > sStrLen ) { /* fill with cFill */ for( pc = pcRet; pc < pcRet + ( sRetLen - sStrLen ); pc++ ) *pc = cFill; hb_xmemcpy( pcRet + ( sRetLen - sStrLen ), pcString, sStrLen ); } else hb_xmemcpy( pcRet, pcString + ( sStrLen - sRetLen ), sRetLen ); } else { hb_xmemcpy( pcRet, pcString, ( sRetLen < sStrLen ? sRetLen : sStrLen ) ); if( sRetLen > sStrLen ) { /* fill with cFill */ for( pc = pcRet + sStrLen; pc < pcRet + sRetLen; pc++ ) *pc = cFill; } } hb_retclen_buffer( pcRet, sRetLen ); } else { PHB_ITEM pSubst = NULL; int iArgErrorMode = ct_getargerrormode(); if( iArgErrorMode != CT_ARGERR_IGNORE ) pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG, iSwitch == DO_PAD_PADLEFT ? CT_ERROR_PADLEFT : CT_ERROR_PADRIGHT, NULL, HB_ERR_FUNCNAME, 0, EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS ); if( pSubst != NULL ) hb_itemReturnRelease( pSubst ); else hb_retc_null(); } }