#include <ClipApi.h>

//----------------------------------------------------------------------------//

LPSTR StrToken( LPSTR szText, WORD wOcurrence, BYTE bSeparator, LPWORD pwLen )
{
   WORD wStart = 0, wEnd = 0, wCounter = 0;

   if( ! bSeparator )
     bSeparator = ' ';

   do {
      wStart = wEnd;

      while( szText[ wStart ] && szText[ wStart ] == bSeparator )
         wStart++;

      if( szText[ wStart ] )
      {
         wEnd = wStart + 1;

         while( szText[ wEnd ] && szText[ wEnd ] != bSeparator )
            wEnd++;
      }
      else
         wEnd = wStart;

   } while( wCounter++ < wOcurrence - 1 && szText[ wEnd ] );

   * pwLen = wEnd - wStart;

   return szText + wStart;
}

//----------------------------------------------------------------------------//

CLIPPER STRTOKEN()  // ( cText, nOcurrence, cSepChar ) --> cToken
{
   WORD wLen;
   LPSTR szToken = StrToken( _parc( 1 ), _parni( 2 ),
                             IF( ISCHAR( 3 ) && ( PCOUNT() > 2 ),
                                 * _parc( 3 ), 0 ), &wLen );

   _retclen( szToken, wLen );
}

//----------------------------------------------------------------------------//
