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

  1. /*
  2.  * GT CLIPPER STANDARD HEADER
  3.  *
  4.  * File......: strexpan.c
  5.  * Author....: Andy M Leighton
  6.  * BBS.......: The Dark Knight Returns
  7.  * Net/Node..: 050/069
  8.  * User Name.: Andy Leighton
  9.  * Date......: 24/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_STREXPAND()
  26.  *  $CATEGORY$
  27.  *      String
  28.  *  $ONELINER$
  29.  *      Insert fillers between characters in a passed string
  30.  *  $SYNTAX$
  31.  *      GT_StrExpand(<cStr>, [<nNum>], [<cChar>]) --> cRet
  32.  *  $ARGUMENTS$
  33.  *      <cStr1>  - A character string to insert chars into
  34.  *      <nNum>   - The number of fill characters to insert (default 1)
  35.  *      <cChar>  - The fill chararacter (default space)
  36.  *  $RETURNS$
  37.  *      cRet     - The input string with fill characters inserted between
  38.  *                 every character in the original.
  39.  *  $DESCRIPTION$
  40.  *      Inserts fill characters into a string.
  41.  *
  42.  *      NOTE:
  43.  *         invalid parameters will return ""
  44.  *  $EXAMPLES$
  45.  *
  46.  *      ? gt_strexpand("abc")                    // prints "a b c"
  47.  *      ? gt_strexpand("abc", 2)                 // prints "a  b  c"
  48.  *      ? gt_strexpand("abc", 2, '■')            // prints "a■■b■■c"
  49.  *
  50.  *  $END$
  51.  */
  52.  
  53.  
  54. #include "extend.h"
  55.  
  56.  
  57.  
  58. CLIPPER
  59. gt_strexpand()
  60. {
  61.   char *in, *out;
  62.   int  nIns = 1;
  63.   char *insert = " ";
  64.   int  len;
  65.   int  i, j, p;
  66.  
  67.   if (ISCHAR(1) && (ISNUM(2) || PCOUNT < 2) && (ISCHAR(3) || PCOUNT < 3)) {
  68.     in  = _parc(1);
  69.     len = _parclen(1);
  70.  
  71.     if (ISNUM(2))
  72.       nIns = _parni(2);
  73.  
  74.     if (ISCHAR(3))
  75.       insert = _parc(3);
  76.  
  77.     out = _xgrab(len * (nIns + 1));    // alloc us some memory
  78.  
  79.     for (i = 0, p = 0; i < len; i++) { // loop thru input
  80.       out[p++] = in[i];                // insert a character from input
  81.  
  82.       if (i < (len - 1)) {             // do not insert fill chars on last
  83.                                        // char of input
  84.          for (j = 1; j <= nIns; j++)   // insert the fill characters
  85.             out[p++] = insert[0];
  86.       }
  87.     }
  88.     out[p] = '\0';                     // Add terminating NUL
  89.  
  90.     _retc(out);
  91.     _xfree(out);                       // free alloc'ed mem
  92.   } else {
  93.     _retc((char *) NULL);              // parameter mismatch - error NullStr
  94.   }
  95. }
  96.  
  97.