home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / ascpos.c < prev    next >
C/C++ Source or Header  |  1993-10-14  |  2KB  |  78 lines

  1. /*
  2.  * GT CLIPPER STANDARD HEADER
  3.  *
  4.  * File......: ascpos.c
  5.  * Author....: Andy M Leighton
  6.  * BBS.......: The Dark Knight Returns
  7.  * Net/Node..: 050/069
  8.  * User Name.: Andy Leighton
  9.  * Date......: 23/05/93
  10.  * Revision..: 1.00
  11.  *
  12.  * This is an original work by Andy Leighton and is placed in the
  13.  * public domain.
  14.  *
  15.  * Modification history:
  16.  * ---------------------
  17.  *
  18.  * $Log$
  19.  *
  20.  */
  21.  
  22. /*
  23.  *  $DOC$
  24.  *  $FUNCNAME$
  25.  *      GT_ASCPOS()
  26.  *  $CATEGORY$
  27.  *      String
  28.  *  $ONELINER$
  29.  *      Return the ascii value of a specified character in a string
  30.  *  $SYNTAX$
  31.  *      GT_Ascpos(<cStr>, <nPos>) --> nAscVal
  32.  *  $ARGUMENTS$
  33.  *      <cStr>  - The string
  34.  *      <nPos>  - The position in <cStr>
  35.  *  $RETURNS$
  36.  *      nAscVal - The ascii value of substr(<cStr>, <nPos>, 1)
  37.  *  $DESCRIPTION$
  38.  *      Return the ascii value of a specified character in a string
  39.  *      Equivalent (but much faster) to
  40.  *          asc(substr(cStr, nPos, 1)
  41.  *
  42.  *      NOTE:
  43.  *         invalid parameters will return -1
  44.  *         nPos > len(cStr)   will return -2
  45.  *
  46.  *      This last behaviour is different to the Funcky function of the
  47.  *      same name.  I changed the behaviour because some of the strings
  48.  *      I process contain embedded NULs.
  49.  *  $EXAMPLES$
  50.  *       ? gt_ascpos("the cat sat on the mat", 3)
  51.  *                                   // prints e
  52.  *  $END$
  53.  */
  54.  
  55. #include "extend.h"
  56.  
  57. CLIPPER
  58. gt_ascpos()
  59. {
  60.   char *s;
  61.   int  p;
  62.  
  63.   if (ISCHAR(1) && ISNUM(2)) {
  64.     s = _parc(1);
  65.     p = _parni(2);
  66.     p--;                            // decrement p to adjust for c strings
  67.                                     // starting at position 0
  68.  
  69.     if (p > _parclen(1))            // oh oh p > length of passed string
  70.       _retni(-2);                   // error -2
  71.     else
  72.       _retni((int) s[p]);           // return ascii code of appropriate
  73.                                     // character in string
  74.   } else {
  75.     _retni(-1);                     // parameter mismatch - error -1
  76.   }
  77. }
  78.