Files
harbour-core/harbour/contrib/hbct/atrepl.c
Viktor Szakats 81c9b0506c 2010-06-04 15:21 UTC+0200 Viktor Szakats (harbour.01 syenar.hu)
* contrib/*
    * Deleted 'www.' from harbour-project.org website name.
      (www.harbour-project.org -> harbour-project.org)
2010-06-04 13:32:23 +00:00

327 lines
12 KiB
C

/*
* $Id$
*/
/*
* Harbour Project source code:
* ATREPL() CT3 string function
*
* Copyright 2001 IntTec GmbH, Neunlindenstr 32, 79106 Freiburg, Germany
* Author: Martin Vogel <vogel@inttec.de>
*
* www - http://harbour-project.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
*
* As a special exception, the Harbour Project gives permission for
* additional uses of the text contained in its release of Harbour.
*
* The exception is that, if you link the Harbour libraries with other
* files to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the Harbour library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the Harbour
* Project under the name Harbour. If you copy code from other
* Harbour Project or Free Software Foundation releases into a copy of
* Harbour, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for Harbour, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice.
*
*/
#include "ct.h"
/* $DOC$
* $FUNCNAME$
* ATREPL()
* $CATEGORY$
* CT3 string functions
* $ONELINER$
* Search and replace sequences in a string
* $SYNTAX$
* ATREPL (<cStringToMatch>, <cString>, <cReplacement>, [<nCounter>],
* [<lMode>], [<nIgnore>]) --> cString
* $ARGUMENTS$
* <cStringToMatch> is the substring searched for in <cString>
* <cString> is the processed string
* <cReplacement> is the replacement for sequences found
* [<nCounter>] specifies the number of replacements
* Default: last occurence
* [<lMode>] if set to .T., only the <nCounter>th sequence
* of <cStringToMatch> will be replaced, else
* all sequences will be replaced.
* Default: .F.
* [<nIgnore>]) specifies how many characters in <cString> from
* the beginning should be ignored by the function
* Default: 0
* $RETURNS$
* <cString>
* $DESCRIPTION$
* The ATREPL() function searches and replaces sequences in a string.
* First, the function ignores the first <nIgnore> characters of <cString>.
* Then, if <lMode> is set to .T., it searches for the <nCounter>th
* occurence of <cStringToMatch> in <cString>. If successful, the
* sequence will be replaced with <cReplacement>.
* If <lMode> is set to .F., the same search is performed, but EVERY
* occurence of <cStringToMatch> till the <nCounter>th (inclusive) will
* be replaced with <cReplacement>. Note that, in this case,
* the replacements are performed even if the <nCounter>th occurence
* does not exist.
* By using the CSETATMUPA() switch you can decide whether the
* function restarts searching after a found sequence of after
* the first character of that sequence.
* The function allows the use of wildcards in <cStringToMatch>
* and looks for the settings of SETATLIKE().
* $EXAMPLES$
* ? ATREPL("ABC", "ABCDABCDABC", "xx") --> "xxDxxDxx"
* ? ATREPL("ABC", "ABCDABC", "ZYXW") --> "ZYXWDZYXW"
* ? ATREPL("ABC", "ABCDABCDABC", "xx", 2) --> "xxDxxDABC"
* ? ATREPL("ABC", "ABCDABCDABC", "xx", 2, .T.) --> "ABCDxxDABC"
* $TESTS$
* ATREPL("ABC", "ABCDABCDABC", "xx") == "xxDxxDxx"
* ATREPL("ABC", "ABCDABC", "ZYXW") == "ZYXWDZYXW"
* ATREPL("ABC", "ABCDABCDABC", "xx", 2) == "xxDxxDABC"
* ATREPL("ABC", "ABCDABCDABC", "xx", 2, .T.) == "ABCDxxDABC"
* $STATUS$
* Ready
* $COMPLIANCE$
* ATREPL() is compatible with CT3's ATREPL().
* Note the new, 6th parameter !
* $PLATFORMS$
* All
* $FILES$
* Source is atrepl.c, library is ct3.
* $SEEALSO$
* CSETATMUPA() SETATLIKE()
* $END$
*/
HB_FUNC( ATREPL )
{
if( HB_ISCHAR( 1 ) && HB_ISCHAR( 2 ) )
{
const char *pcStringToMatch = hb_parc( 1 );
HB_SIZE nStrToMatchLen = hb_parclen( 1 );
const char *pcString = hb_parc( 2 );
HB_SIZE nStrLen = hb_parclen( 2 );
int iMultiPass = ct_getatmupa();
int iAtLike = ct_getatlike();
char cAtLike = ct_getatlikechar();
HB_SIZE nIgnore, nMatchStrLen = 0;
HB_SIZE ulCounter;
char *pc;
const char *pcReplacement;
HB_SIZE nReplaceLen;
int iReplaceMode;
char *pcRetStr;
HB_SIZE nRetStrLen;
/* eventually ignore some characters */
nIgnore = ( HB_SIZE ) hb_parnl( 6 );
if( nIgnore >= nStrLen )
{
int iArgErrorMode = ct_getargerrormode();
if( iArgErrorMode != CT_ARGERR_IGNORE )
{
ct_error( ( HB_USHORT ) iArgErrorMode, EG_ARG, CT_ERROR_ATREPL, NULL,
HB_ERR_FUNCNAME, 0, EF_CANDEFAULT, HB_ERR_ARGS_BASEPARAMS );
}
hb_retclen( pcString, nStrLen );
return;
}
/* replacement */
pcReplacement = hb_parc( 3 );
nReplaceLen = pcReplacement ? hb_parclen( 3 ) : 0;
/* replace mode */
iReplaceMode = hb_parl( 5 );
/* n-th match or last match ? */
ulCounter = hb_parnl( 4 );
/* little trick: */
if( iReplaceMode == 0 && ulCounter == 0 )
ulCounter = ULONG_MAX;
if( ulCounter != 0 )
{
/* depending on iReplaceMode: replace all occurences including the nth one
or only the nth occurence
NOTE: if iReplaceMode = false and the nth occurence does not exist,
all occurences are replaced */
char *pcRetSubStr;
HB_SIZE sRetSubStrLen;
HB_SIZE ulMatchCounter = 0;
nRetStrLen = nStrLen;
pcRetStr = ( char * ) hb_xgrab( nRetStrLen + 1 );
hb_xmemcpy( pcRetStr, pcString, nRetStrLen );
pcRetSubStr = pcRetStr + nIgnore;
sRetSubStrLen = nRetStrLen - nIgnore;
while( ulMatchCounter < ulCounter )
{
switch ( iAtLike )
{
case CT_SETATLIKE_EXACT:
pc = ( char * ) ct_at_exact_forward( pcRetSubStr, sRetSubStrLen, pcStringToMatch,
nStrToMatchLen, &nMatchStrLen );
break;
case CT_SETATLIKE_WILDCARD:
pc = ( char * ) ct_at_wildcard_forward( pcRetSubStr, sRetSubStrLen, pcStringToMatch,
nStrToMatchLen, cAtLike, &nMatchStrLen );
break;
default:
pc = NULL;
}
if( pc == NULL )
{
hb_retclen_buffer( pcRetStr, nRetStrLen );
return;
}
ulMatchCounter++;
/* replace match ? */
if( ( iReplaceMode == 0 ) || ( ulMatchCounter == ulCounter ) )
{
if( nMatchStrLen < nReplaceLen )
{
/* pcRetStr grows, so realloc memory */
/* save pc pointer */
HB_SIZE sPCPos = pc - pcRetStr;
pcRetStr = ( char * ) hb_xrealloc( pcRetStr,
nRetStrLen + ( nReplaceLen - nMatchStrLen ) + 1 );
pc = pcRetStr + sPCPos;
}
if( nReplaceLen != nMatchStrLen )
memmove( pc + nReplaceLen, pc + nMatchStrLen,
nRetStrLen - ( ( pc + nMatchStrLen ) - pcRetStr ) );
if( nReplaceLen > 0 )
hb_xmemcpy( pc, pcReplacement, nReplaceLen );
if( iMultiPass )
pcRetSubStr = pc + 1;
else
pcRetSubStr = pc + nReplaceLen;
nRetStrLen += nReplaceLen - nMatchStrLen;
}
else
{
if( iMultiPass )
pcRetSubStr = pc + 1;
else
pcRetSubStr = pc + nMatchStrLen;
}
sRetSubStrLen = nRetStrLen - ( pcRetSubStr - pcRetStr );
}
}
else /* ( ulCounter != 0 ) */
{
/* find and replace last match */
nRetStrLen = nStrLen;
pcRetStr = ( char * ) hb_xgrab( nRetStrLen + 1 );
hb_xmemcpy( pcRetStr, pcString, nRetStrLen );
/* we have to find the last match and replace it */
switch ( iAtLike )
{
case CT_SETATLIKE_EXACT:
pc = ( char * ) ct_at_exact_backward( pcRetStr + nIgnore, nRetStrLen - nIgnore,
pcStringToMatch, nStrToMatchLen, &nMatchStrLen );
break;
case CT_SETATLIKE_WILDCARD:
pc = ( char * ) ct_at_wildcard_backward( pcRetStr + nIgnore, nRetStrLen - nIgnore,
pcStringToMatch, nStrToMatchLen,
cAtLike, &nMatchStrLen );
break;
default:
pc = NULL;
}
if( pc == NULL )
{
hb_retclen_buffer( pcRetStr, nRetStrLen );
return;
}
/* replace match */
if( nMatchStrLen < nReplaceLen )
{
/* pcRetStr grows, so realloc memory */
/* save pc pointer */
HB_SIZE sPCPos = pc - pcRetStr;
pcRetStr = ( char * ) hb_xrealloc( pcRetStr,
nRetStrLen + ( nReplaceLen - nMatchStrLen ) + 1 );
pc = pcRetStr + sPCPos;
}
if( nReplaceLen != nMatchStrLen )
memmove( pc + nReplaceLen, pc + nMatchStrLen,
nRetStrLen - ( ( pc + nMatchStrLen ) - pcRetStr ) );
if( nReplaceLen > 0 )
hb_xmemcpy( pc, pcReplacement, nReplaceLen );
nRetStrLen += ( nReplaceLen - nMatchStrLen );
}
hb_retclen_buffer( pcRetStr, nRetStrLen );
}
else /* ( HB_ISCHAR( 1 ) && HB_ISCHAR( 2 ) ) */
{
PHB_ITEM pSubst = NULL;
int iArgErrorMode = ct_getargerrormode();
if( iArgErrorMode != CT_ARGERR_IGNORE )
{
pSubst = ct_error_subst( ( HB_USHORT ) iArgErrorMode, EG_ARG,
CT_ERROR_ATREPL, NULL, HB_ERR_FUNCNAME, 0,
EF_CANSUBSTITUTE, HB_ERR_ARGS_BASEPARAMS );
}
if( pSubst != NULL )
hb_itemReturnRelease( pSubst );
else
hb_retclen( hb_parc( 2 ), hb_parclen( 2 ) );
}
}