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 / STRPL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-30  |  4.4 KB  |  222 lines

  1. /*
  2.  * strpl.c
  3.  * contains: strpli(),strplsz(),strplrm(),strpla(),strpld(),strplb()
  4.  *
  5.  *   Copyright (C)1987-1990 Greenleaf Software Inc. All Rights Reserved.
  6.  *
  7.  * Modifications:
  8.  *
  9.  * YCC January 30,1990.
  10.  * Modified the strpld function to correctly delete the ptr.
  11.  *
  12.  *
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "gfuncts.h"
  18.  
  19. int GF_CONV strpli(),GF_CONV strplsz(),GF_CONV strplrm();
  20. int GF_CONV strpla(),GF_CONV strpld(),GF_CONV strplb();
  21.  
  22. /*
  23.  *  int
  24.  * strpli(ptr,num)
  25.  *
  26.  * ARGUMENT
  27.  *  (char *[])    ptr    -    pointer array
  28.  *  (int)    num    -    number of pointers in array to be initialized
  29.  *
  30.  * DESCRIPTION
  31.  *  Initialize string pointer list
  32.  *
  33.  * RETURNS
  34.  *  num+1 to indicate number of words used.
  35.  */
  36. int GF_CONV strpli(ptr,num)
  37. char *ptr[];
  38. int num;
  39. {
  40.     int i;
  41.  
  42.     for(i=0;i<=(num-1);i++) 
  43.         ptr[i]=0;
  44. #ifdef    _LDATA
  45.            ptr[num]=(char *)((long)-1);
  46. #else
  47.            ptr[num]=((char *)-1);
  48. #endif
  49.     return(num+1);
  50. }
  51.  
  52. /*
  53.  *  int
  54.  * strplsz(ptr)
  55.  *
  56.  * ARGUMENT
  57.  *  (char *[])    ptr    -    pointer array
  58.  *
  59.  * DESCRIPTION
  60.  *  This function determines the size of the list.
  61.  *
  62.  * RETURNS
  63.  *  number of words not including terminator
  64.  */
  65. int GF_CONV strplsz(ptr)
  66. char *ptr[];
  67. {
  68.     int i=0;
  69.  
  70. #ifdef    _LDATA
  71.     while(ptr[i]!=(char *) ((long)-1)) 
  72. #else
  73.     while(ptr[i]!=(char *)(-1)) 
  74. #endif
  75.         ++i;
  76.     return i;
  77. }
  78.  
  79. /*
  80.  *  int
  81.  * strplrm(ptr)
  82.  *
  83.  * ARGUMENT
  84.  *  (char *[])    ptr    -    pointer array
  85.  *
  86.  * DESCRIPTION
  87.  *  returns number of remaining zero value pointers
  88.  *
  89.  * RETURNS
  90.  *  number of remaining zero value pointers
  91.  */
  92. int GF_CONV strplrm(ptr)
  93. char *ptr[];
  94. {
  95.     int i,imax,iret;
  96.  
  97.     i=iret=0;
  98.     imax=strplsz(ptr);
  99.     for(i=0;i<=(imax-1);i++) 
  100.         if(!ptr[i]) 
  101.             ++iret;
  102.     return iret;
  103. }
  104.  
  105. /*
  106.  *  int
  107.  * strpla(ptr,new)
  108.  *
  109.  * ARGUMENT
  110.  *  (char *[])    ptr    -    pointer array
  111.  *  (char *)    new    -    pointer to be added
  112.  *
  113.  * DESCRIPTION
  114.  *  Add a pointer to first zero-value position in the list without altering
  115.  *  any pointers already in the list.
  116.  *
  117.  * RETURNS
  118.  *  If the list is full on entry, return 0, else return position in the list
  119.  *  at which the new pointer was placed.  Position 0 is first in the array.
  120.  */
  121. int GF_CONV strpla(ptr,new)
  122. char *ptr[],*new;
  123. {
  124.     int i=0,total,availabl;
  125.  
  126.     total=strplsz(ptr);
  127.     availabl=strplrm(ptr);
  128.     if(!total||!availabl) 
  129.         return 0;
  130.     for (i=0;i<=total;i++) {
  131.         if (ptr[i]==0) {
  132.             ptr[i]=new;
  133.             return i;
  134.         }
  135.     }
  136.     return 0;
  137. }
  138.  
  139. /*
  140.  *  int
  141.  * strpld(ptr,del)
  142.  *
  143.  * ARGUMENT
  144.  *  (char *[])    ptr    -    pointer array
  145.  *  (char *)    del    -    pointer to be deleted
  146.  *
  147.  * DESCRIPTION
  148.  *  Delete pointer from list if it exists in the list.
  149.  *
  150.  * RETURNS
  151.  *  The number of positions remaining in the list after the deletion, or -1
  152.  *  if the pointer is not found.
  153.  */
  154. int GF_CONV strpld(ptr,del)
  155. char *ptr[],*del;
  156. {
  157.     int i,total,avail;
  158.  
  159.     total=strplsz(ptr);
  160.     avail=strplrm(ptr);
  161.     for (i=0;i<=(total-1);i++) {
  162.    if ( strcmp(ptr[i],del) == 0 ) {                        
  163.             ptr[i] = 0;
  164.             return (avail+1);
  165.         }
  166.     }                                                                
  167.     return (-1);
  168. }
  169.  
  170. /*
  171.  *  int
  172.  * strplb(ptr,sa,inum)
  173.  *
  174.  * ARGUMENT
  175.  *  (char *[])    str    -    pointer array
  176.  *  (char *)    sa    -    pointer to string
  177.  *  (int)    inum    -    see below
  178.  *
  179.  * DESCRIPTION
  180.  *   Build Pointer List - Scan array of strings, transfer pointer
  181.  *   to each string into list while there is space.
  182.  *
  183.  *   This is a destructive process in that any strings in the list
  184.  *   on entry are lost.  The list is sized, then initialized, and
  185.  *   then pointers are added as required until either (1) no more
  186.  *   space is available in the list, or (2) the next string in the
  187.  *   array is a null string.
  188.  *
  189.  *   The "inum" argument is used as an argument to "strpli" to initialize
  190.  *   the pointer array ONLY if the array is currently void.
  191.  *
  192.  *   NOTE that this does not mean the pointer array does not need to be
  193.  *   initialized.  It must be explicitly initialized by "strpli" prior
  194.  *   to calling any other pointer list function including "strplb".
  195.  *
  196.  * RETURNS
  197.  *  Returns actual number of pointers in list as built.
  198.  */
  199. int GF_CONV strplb(ptr,sa,inum)
  200. char *ptr[],*sa;
  201. int inum;
  202. {
  203.     int total,num;
  204.     char *p,*peek;
  205.  
  206.     total=strplsz(ptr);
  207.     if (total==0) 
  208.         total=inum;
  209.     num = strpli(ptr,total);
  210.     peek=p=sa;
  211.     while(1) {
  212.         num=strpla(ptr,peek);
  213.         if(num==total) 
  214.             return num;
  215.         while(*p++) 
  216.             ;
  217.         peek=p;
  218.         if (!*peek) 
  219.             return num;
  220.     }
  221. }
  222.