home *** CD-ROM | disk | FTP | other *** search
- /*
- * strpl.c
- * contains: strpli(),strplsz(),strplrm(),strpla(),strpld(),strplb()
- *
- * Copyright (C)1987-1990 Greenleaf Software Inc. All Rights Reserved.
- *
- * Modifications:
- *
- * YCC January 30,1990.
- * Modified the strpld function to correctly delete the ptr.
- *
- *
- */
-
- #include <stdio.h>
- #include <string.h>
- #include "gfuncts.h"
-
- int GF_CONV strpli(),GF_CONV strplsz(),GF_CONV strplrm();
- int GF_CONV strpla(),GF_CONV strpld(),GF_CONV strplb();
-
- /*
- * int
- * strpli(ptr,num)
- *
- * ARGUMENT
- * (char *[]) ptr - pointer array
- * (int) num - number of pointers in array to be initialized
- *
- * DESCRIPTION
- * Initialize string pointer list
- *
- * RETURNS
- * num+1 to indicate number of words used.
- */
- int GF_CONV strpli(ptr,num)
- char *ptr[];
- int num;
- {
- int i;
-
- for(i=0;i<=(num-1);i++)
- ptr[i]=0;
- #ifdef _LDATA
- ptr[num]=(char *)((long)-1);
- #else
- ptr[num]=((char *)-1);
- #endif
- return(num+1);
- }
-
- /*
- * int
- * strplsz(ptr)
- *
- * ARGUMENT
- * (char *[]) ptr - pointer array
- *
- * DESCRIPTION
- * This function determines the size of the list.
- *
- * RETURNS
- * number of words not including terminator
- */
- int GF_CONV strplsz(ptr)
- char *ptr[];
- {
- int i=0;
-
- #ifdef _LDATA
- while(ptr[i]!=(char *) ((long)-1))
- #else
- while(ptr[i]!=(char *)(-1))
- #endif
- ++i;
- return i;
- }
-
- /*
- * int
- * strplrm(ptr)
- *
- * ARGUMENT
- * (char *[]) ptr - pointer array
- *
- * DESCRIPTION
- * returns number of remaining zero value pointers
- *
- * RETURNS
- * number of remaining zero value pointers
- */
- int GF_CONV strplrm(ptr)
- char *ptr[];
- {
- int i,imax,iret;
-
- i=iret=0;
- imax=strplsz(ptr);
- for(i=0;i<=(imax-1);i++)
- if(!ptr[i])
- ++iret;
- return iret;
- }
-
- /*
- * int
- * strpla(ptr,new)
- *
- * ARGUMENT
- * (char *[]) ptr - pointer array
- * (char *) new - pointer to be added
- *
- * DESCRIPTION
- * Add a pointer to first zero-value position in the list without altering
- * any pointers already in the list.
- *
- * RETURNS
- * If the list is full on entry, return 0, else return position in the list
- * at which the new pointer was placed. Position 0 is first in the array.
- */
- int GF_CONV strpla(ptr,new)
- char *ptr[],*new;
- {
- int i=0,total,availabl;
-
- total=strplsz(ptr);
- availabl=strplrm(ptr);
- if(!total||!availabl)
- return 0;
- for (i=0;i<=total;i++) {
- if (ptr[i]==0) {
- ptr[i]=new;
- return i;
- }
- }
- return 0;
- }
-
- /*
- * int
- * strpld(ptr,del)
- *
- * ARGUMENT
- * (char *[]) ptr - pointer array
- * (char *) del - pointer to be deleted
- *
- * DESCRIPTION
- * Delete pointer from list if it exists in the list.
- *
- * RETURNS
- * The number of positions remaining in the list after the deletion, or -1
- * if the pointer is not found.
- */
- int GF_CONV strpld(ptr,del)
- char *ptr[],*del;
- {
- int i,total,avail;
-
- total=strplsz(ptr);
- avail=strplrm(ptr);
- for (i=0;i<=(total-1);i++) {
- if ( strcmp(ptr[i],del) == 0 ) {
- ptr[i] = 0;
- return (avail+1);
- }
- }
- return (-1);
- }
-
- /*
- * int
- * strplb(ptr,sa,inum)
- *
- * ARGUMENT
- * (char *[]) str - pointer array
- * (char *) sa - pointer to string
- * (int) inum - see below
- *
- * DESCRIPTION
- * Build Pointer List - Scan array of strings, transfer pointer
- * to each string into list while there is space.
- *
- * This is a destructive process in that any strings in the list
- * on entry are lost. The list is sized, then initialized, and
- * then pointers are added as required until either (1) no more
- * space is available in the list, or (2) the next string in the
- * array is a null string.
- *
- * The "inum" argument is used as an argument to "strpli" to initialize
- * the pointer array ONLY if the array is currently void.
- *
- * NOTE that this does not mean the pointer array does not need to be
- * initialized. It must be explicitly initialized by "strpli" prior
- * to calling any other pointer list function including "strplb".
- *
- * RETURNS
- * Returns actual number of pointers in list as built.
- */
- int GF_CONV strplb(ptr,sa,inum)
- char *ptr[],*sa;
- int inum;
- {
- int total,num;
- char *p,*peek;
-
- total=strplsz(ptr);
- if (total==0)
- total=inum;
- num = strpli(ptr,total);
- peek=p=sa;
- while(1) {
- num=strpla(ptr,peek);
- if(num==total)
- return num;
- while(*p++)
- ;
- peek=p;
- if (!*peek)
- return num;
- }
- }