Example #1
0
/* 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;
         }
      }
   }
}
Example #2
0
static HB_BOOL hb_strMatchWildRaw( const char * szString, const char * szPattern,
                                   HB_BOOL fExact, HB_BOOL fCase, HB_BOOL fFile )
{
   HB_BOOL fMatch = HB_TRUE, fAny = HB_FALSE;
   HB_SIZE pnBufPosP[ HB_MAX_WILDPATTERN ], pnBufPosV[ HB_MAX_WILDPATTERN ],
           nBufSize = HB_MAX_WILDPATTERN;
   HB_SIZE * nAnyPosP = pnBufPosP, * nAnyPosV = pnBufPosV,
           nSize, nLen, nAny, nPosP, nPosV;

   nPosP = nPosV = nAny = 0;
   nLen = strlen( szString );
   nSize = strlen( szPattern );
   while( nPosP < nSize || ( fExact && ! fAny && nPosV < nLen ) )
   {
      if( nPosP < nSize && szPattern[ nPosP ] == '*' )
      {
         fAny = HB_TRUE;
         nPosP++;
      }
      else if( nPosV < nLen && nPosP < nSize &&
               ( szPattern[ nPosP ] == '?' ||
                 ( ! fCase ? szPattern[ nPosP ] == szString[ nPosV ] :
                   ( hb_charUpper( szPattern[ nPosP ] ) ==
                     hb_charUpper( szString[ nPosV ] ) ) ) ) )
      {
         if( fAny )
         {
            if( nAny >= nBufSize )
            {
               if( ( nBufSize <<= 1 ) == ( HB_MAX_WILDPATTERN << 1 ) )
               {
                  nAnyPosP = ( HB_SIZE * ) hb_xgrab( nBufSize * sizeof( HB_SIZE ) );
                  nAnyPosV = ( HB_SIZE * ) hb_xgrab( nBufSize * sizeof( HB_SIZE ) );
                  memcpy( nAnyPosP, pnBufPosP, HB_MAX_WILDPATTERN * sizeof( HB_SIZE ) );
                  memcpy( nAnyPosV, pnBufPosV, HB_MAX_WILDPATTERN * sizeof( HB_SIZE ) );
               }
               else
               {
                  nAnyPosP = ( HB_SIZE * ) hb_xrealloc( nAnyPosP, nBufSize * sizeof( HB_SIZE ) );
                  nAnyPosV = ( HB_SIZE * ) hb_xrealloc( nAnyPosV, nBufSize * sizeof( HB_SIZE ) );
               }
            }
            nAnyPosP[ nAny ] = nPosP;
            nAnyPosV[ nAny ] = nPosV;
            nAny++;
            fAny = HB_FALSE;
         }
         nPosV++;
         nPosP++;
      }
      else if( fFile && nPosV == nLen && nPosP < nSize &&
               szPattern[ nPosP ] == '.' &&
               ( nPosP + 1 == nSize || 
                 ( nPosP + 2 == nSize && szPattern[ nPosP + 1 ] == '*' ) ) )
      {
         break;
      }
      else if( fAny && nPosV < nLen )
      {
         nPosV++;
      }
      else if( nAny > 0 )
      {
         nAny--;
         nPosP = nAnyPosP[ nAny ];
         nPosV = nAnyPosV[ nAny ] + 1;
         fAny = HB_TRUE;
      }
      else
      {
         fMatch = HB_FALSE;
         break;
      }
   }
   if( nBufSize > HB_MAX_WILDPATTERN )
   {
      hb_xfree( nAnyPosP );
      hb_xfree( nAnyPosV );
   }
   return fMatch;
}
Example #3
0
BOOL hb_strMatchCaseWildExact( const char *szString, const char *szPattern )
{
   BOOL fMatch = TRUE, fAny = FALSE;
   ULONG pulBufPosP[ HB_MAX_WILDPATTERN ], pulBufPosV[ HB_MAX_WILDPATTERN ],
         ulBufSize = HB_MAX_WILDPATTERN;
   ULONG * ulAnyPosP = pulBufPosP, * ulAnyPosV = pulBufPosV,
         ulSize, ulLen, ulAny, i, j;

   i = j = ulAny = 0;
   ulLen = strlen( szString );
   ulSize = strlen( szPattern );
   while ( i < ulSize || ( j < ulLen && !fAny ) )
   {
      if( i < ulSize && szPattern[i] == '*' )
      {
         fAny = TRUE;
         i++;
      }
      else if( j < ulLen && i < ulSize &&
               ( szPattern[i] == '?' ||
                 hb_charUpper( szPattern[i] ) == hb_charUpper( szString[j] ) ) )
      {
         if( fAny )
         {
            if( ulAny >= ulBufSize )
            {
               if( ( ulBufSize <<= 1 ) == ( HB_MAX_WILDPATTERN << 1 ) )
               {
                  ulAnyPosP = ( ULONG * ) hb_xgrab( ulBufSize * sizeof( ULONG ) );
                  ulAnyPosV = ( ULONG * ) hb_xgrab( ulBufSize * sizeof( ULONG ) );
                  memcpy( ulAnyPosP, pulBufPosP, HB_MAX_WILDPATTERN * sizeof( ULONG ) );
                  memcpy( ulAnyPosV, pulBufPosV, HB_MAX_WILDPATTERN * sizeof( ULONG ) );
               }
               else
               {
                  ulAnyPosP = ( ULONG * ) hb_xrealloc( ulAnyPosP, ulBufSize * sizeof( ULONG ) );
                  ulAnyPosV = ( ULONG * ) hb_xrealloc( ulAnyPosV, ulBufSize * sizeof( ULONG ) );
               }
            }
            ulAnyPosP[ulAny] = i;
            ulAnyPosV[ulAny] = j;
            ulAny++;
            fAny = FALSE;
         }
         j++;
         i++;
      }
      else if( fAny && j < ulLen )
      {
         j++;
      }
      else if( ulAny > 0 )
      {
         ulAny--;
         i = ulAnyPosP[ulAny];
         j = ulAnyPosV[ulAny] + 1;
         fAny = TRUE;
      }
      else
      {
         fMatch = FALSE;
         break;
      }
   }
   if( ulBufSize > HB_MAX_WILDPATTERN )
   {
      hb_xfree( ulAnyPosP );
      hb_xfree( ulAnyPosV );
   }
   return fMatch;
}