home *** CD-ROM | disk | FTP | other *** search
- /*
- * strwd.c
- * contains: strwd(),strwdlf(),strwdrt()
- *
- * Copyright (C)1987-1990 Greenleaf Software Inc. All Rights Reserved.
- */
-
- #include <stdio.h>
- #include <string.h>
- #include "gfuncts.h"
-
- int GF_CONV strwc(),GF_CONV strmove(),GF_CONV strwd();
- int GF_CONV strwdlf(),GF_CONV strwdrt();
-
- /*
- * int
- * strwd(str,start,num)
- *
- * ARGUMENT
- * (char *) str - pointer to string
- * (int) start - first word to delete (leftmost==0)
- * (int) num - number of words to delete
- *
- * DESCRIPTION
- * Zero or more words are deleted and the string is shortened. Words
- * to be deleted start with start in str. "num" words are deleted unless
- * the count of words is less than that in which case the string is set
- * to a null string.
- *
- * RETURNS
- * Number of characters deleted.
- */
- int GF_CONV strwd(ps,start,num)
- char *ps;
- int start, num;
- {
- int curword,words,chs,inword,stop,delon, delrt,last;
- char *pt;
-
- words=strwc(ps);
- if(start<=0)
- start=1;
- if(start>words||num<=0)
- return 0;
- if ((start+num)>(words+1)) {
- chs=strlen(ps);
- *ps='\0';
- return chs;
- } else {
- stop=start+num;
- if (start+num-1==words)
- delrt=1;
- else
- delrt=0;
- pt=ps;
- last=inword=chs=curword=delon=0;
- while (1) {
- if(!*ps) {
- last=1;
- inword=0;
- }
- if (gisspace(*ps)) {
- if(delrt&&curword==(start-1)) {
- *ps='\0';
- return chs;
- }
- if(inword)
- inword=0;
- else
- goto point1;
- } else if(!inword) {
- inword=1;
- ++curword;
- if(curword==start) {
- delon=1;
- pt=ps;
- }
- else if(curword==stop) {
- strmove(pt,ps);
- return chs;
- }
- }
- if(last)
- return(chs);
- point1:
- if (delon)
- ++chs;
- ++ps;
- }
- }
- }
-
- /*
- * int
- * strwdlf(str,num)
- *
- * ARGUMENT
- * (char *) str - pointer to string
- * (int) num - number of words to delete
- *
- * DESCRIPTION
- * Zero or more words are deleted starting at the left side of string (str)
- * "num" words are deleted unless the count of words is less than that in
- * which case the string is set to a null string.
- *
- * RETURNS
- * Number of characters deleted.
- */
- int GF_CONV strwdlf(ps,num)
- char *ps;
- int num;
- {
- return strwd(ps,1,num);
- }
-
- /*
- * int
- * strwdrt(str,num)
- *
- * ARGUMENT
- * (char *) str - pointer to string
- * (int) num - number of words to delete
- *
- * DESCRIPTION
- * Zero or more words are deleted starting at the right side of string (str)
- * "num" words are deleted unless the count of words is less than that in
- * which case the string is set to a null string.
- *
- * RETURNS
- * Number of characters deleted.
- */
- int GF_CONV strwdrt(ps,num)
- char *ps;
- int num;
- {
- int start;
-
- start=strwc(ps)-num+1;
- return strwd(ps,start,num);
- }
-