home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0020 - 0029 / ibm0020-0029 / ibm0028.tar / ibm0028 / GRLF-C-2.ZIP / GFUNC / STRWI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-30  |  2.6 KB  |  124 lines

  1. /*
  2.  * strwi.c
  3.  * contains: strwi(),strwilf(),strwirt()
  4.  *
  5.  *   Copyright (C)1987-1990 Greenleaf Software Inc. All Rights Reserved.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "gfuncts.h"
  11.  
  12. int GF_CONV strwc(),GF_CONV strwi(),GF_CONV strwilf(),GF_CONV strwirt();
  13.  
  14. /*
  15.  *  int
  16.  * strwi(ps,pi,pos)
  17.  *
  18.  * ARGUMENT
  19.  *  (char *)    ps    -    points to string to operate on
  20.  *  (char *)    pi    -    string to insert
  21.  *  (int)    pos    -    word position at which to insert
  22.  *
  23.  * DESCRIPTION
  24.  *  General word insert, the string pi is inserted into the indicated string
  25.  *  ps. pi is assummed to be one or more words but can be any string.
  26.  *  The insertion begins at the word position "pos".  Hence if pos=1 the
  27.  *  existing string is pushed to the right in its entirety.
  28.  *
  29.  * RETURNS
  30.  *  Number of characters inserted.  0 if no insertion.
  31.  */
  32. int GF_CONV strwi(ps,pi,pos)
  33. char *ps,*pi;
  34. int pos;
  35. {
  36.     int words,curword,inword,inslen;
  37.     char *pt;
  38.  
  39.     words=strwc(ps);
  40.     inslen= strlen(pi);
  41.     if (!inslen||!words||pos==0||pos>(words+1)) 
  42.         return 0;
  43.     inword=curword=0;
  44.     if(pos==(words+1)) {
  45.         pt=ps;
  46.         strcat(ps," ");
  47.         strcat(pt,pi);
  48.         return inslen;
  49.     } else while (1) {
  50.         if(!*ps) 
  51.             return 0;
  52.         if(!gisspace(*ps)) {
  53.             if(!inword) {
  54.                 inword=1;
  55.                 ++curword;
  56.                 if(curword!=pos)
  57.                     goto point1;
  58.                 else {
  59.                     pt=ps;
  60.                     pt+=inslen;
  61.                     pt++;
  62.                     strmove(pt,ps);
  63.                     while(*pi) 
  64.                         *ps++=*pi++;
  65.                     *ps=' ';
  66.                     return inslen;
  67.                 }
  68.             }
  69.         } else if(inword) 
  70.             inword=0;
  71. point1:
  72.         ++ps;
  73.     }
  74. }
  75.  
  76. /*
  77.  *  int
  78.  * strwilf(ps,pi)
  79.  *
  80.  * ARGUMENT
  81.  *  (char *)    ps    -    points to string to operate on
  82.  *  (char *)    pi    -    string to insert
  83.  *
  84.  * DESCRIPTION
  85.  *  String Word insert left, the string pi is inserted into the indicated 
  86.  *  string starting at the left side.  ps. pi is assummed to be one or more 
  87.  *  words but can be any string.
  88.  *
  89.  * RETURNS
  90.  *  Number of characters inserted.  0 if no insertion.
  91.  */
  92. int GF_CONV strwilf(ps,ins)
  93. char *ps,*ins;
  94. {
  95.     return strwi(ps,ins,1);
  96. }
  97.  
  98. /*  String Word Insert Right
  99.  */
  100. /*
  101.  *  int
  102.  * strwirt(ps,pi)
  103.  *
  104.  * ARGUMENT
  105.  *  (char *)    ps    -    points to string to operate on
  106.  *  (char *)    pi    -    string to insert
  107.  *
  108.  * DESCRIPTION
  109.  *  String Word insert right, the string pi is inserted into the indicated 
  110.  *  string starting at the right side.  ps. pi is assummed to be one or more 
  111.  *  words but can be any string.
  112.  *
  113.  * RETURNS
  114.  *  Number of characters inserted.  0 if no insertion.
  115.  */
  116. int GF_CONV strwirt(ps,ins)
  117. char *ps,*ins;
  118. {
  119.     int words;
  120.     
  121.     words=strwc(ps);
  122.     return strwi(ps,ins,(words+1));
  123. }
  124.