home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / nfsrc21.zip / FINDITH.PRG < prev    next >
Text File  |  1991-08-16  |  3KB  |  108 lines

  1. /*
  2.  * File......: FindIth.Prg
  3.  * Author....: David Husnian
  4.  * Date......: $Date:   15 Aug 1991 23:03:36  $
  5.  * Revision..: $Revision:   1.2  $
  6.  * Log file..: $Logfile:   E:/nanfor/src/findith.prv  $
  7.  * 
  8.  * This is an original work by David Husnian and is placed in the
  9.  * public domain.
  10.  *
  11.  * Modification history:
  12.  * ---------------------
  13.  *
  14.  * $Log:   E:/nanfor/src/findith.prv  $
  15.  * 
  16.  *    Rev 1.2   15 Aug 1991 23:03:36   GLENN
  17.  * Forest Belt proofread/edited/cleaned up doc
  18.  * 
  19.  *    Rev 1.1   14 Jun 1991 19:51:52   GLENN
  20.  * Minor edit to file header
  21.  * 
  22.  *    Rev 1.0   01 Apr 1991 01:01:22   GLENN
  23.  * Nanforum Toolkit
  24.  *
  25.  */
  26.  
  27.  
  28. /*  $DOC$
  29.  *  $FUNCNAME$
  30.  *     FT_FINDITH()
  31.  *  $CATEGORY$
  32.  *     String
  33.  *  $ONELINER$
  34.  *     Find the "ith" occurrence of a substring within a string
  35.  *  $SYNTAX$
  36.  *     FT_FINDITH( <cCheckFor>, <cCheckIn>, <nWhichOccurrence> ;
  37.  *                 [, <lIgnoreCase> ] ) -> <nStringPosition>
  38.  *  $ARGUMENTS$
  39.  *     <cCheckFor> is the string to search for.
  40.  *
  41.  *     <cCheckIn> is the string to search.
  42.  *
  43.  *     <nWhichOccurrence> is the number of the occurrence to find.
  44.  *
  45.  *     <lIgnoreCase> is a logical indicating if the search is to be case
  46.  *        sensitive.  The default is no case sensitivity (.F.).
  47.  *  $RETURNS$
  48.  *     The position in the string cCheckIn of the ith occurrence of cCheckFor.
  49.  *  $DESCRIPTION$
  50.  *     This function finds the position in a string of the "ith" time another
  51.  *     string appears in it.
  52.  *  $EXAMPLES$
  53.  *     // Find the Position in cMemoString of
  54.  *     // the 10th Occurrence of "the", case
  55.  *     // insensitive
  56.  *
  57.  *     nNextPosition := FT_FINDITH("the", cMemoString, 10)
  58.  *  $SEEALSO$
  59.  *     FT_AT2()
  60.  *  $END$
  61.  */
  62.  
  63.  
  64. #define IS_NOT_LOGICAL(x)               (VALTYPE(x) != "L")
  65. #define MAKE_UPPER(cString)             (cString := UPPER(cString))
  66. #define NULL                            ""
  67.  
  68. #ifdef FT_TEST
  69.   FUNCTION MAIN( cCk, cStr, nOcc, xCase )
  70.      LOCAL nFind
  71.      if pcount() != 4
  72.         QOut( "usage: findith cCk cStr nOcc xCase")
  73.         quit
  74.      endif
  75.  
  76.      xCase := iif( xCase == "Y", .t., .f. )
  77.      nOcc  := val(nOcc)
  78.      QOut( iif( xCase, "Ignoring ", "Observing ") + "case:" )
  79.  
  80.      QOut( cStr )
  81.      nFind := FT_FINDITH( cCk, cStr, nOcc, xCase )
  82.      QOut( iif( nFind > 0, space( nFind - 1) + "^" , "Not found" ) )
  83.   RETURN nil
  84. #endif
  85.  
  86. FUNCTION FT_FINDITH(cCheckFor,cCheckIn,nWhichOccurrence,lIgnoreCase)
  87.  
  88.    LOCAL nIthOccurrence
  89.  
  90.                                         // Is Case Sensitivity Important??
  91.    IF IS_NOT_LOGICAL(lIgnoreCase) .OR. ;
  92.       lIgnoreCase
  93.                                         
  94.       MAKE_UPPER(cCheckFor)             // No, Force Everything to Uppercase
  95.       MAKE_UPPER(cCheckIn)
  96.  
  97.    ENDIF                                // IS_NOT_LOGICAL(lIgnoreCase) or
  98.                                         // lIgnoreCase
  99.  
  100.    RETURN (IF(nWhichOccurrence == 1, ;
  101.               AT(cCheckFor, cCheckIn), ;
  102.               IF((nIthOccurrence := AT(cCheckFor, ;
  103.                                       STRTRAN(cCheckIn, cCheckFor, ;
  104.                                               NULL, 1, ;
  105.                                               nWhichOccurrence-1))) == 0, ;
  106.                  0, ;
  107.                  nIthOccurrence + ((nWhichOccurrence - 1) * LEN(cCheckFor)))))
  108.