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

  1. /*
  2.  * strentab.c
  3.  * contains: strentab()
  4.  *
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include "gfuncts.h"
  9.  
  10. /*
  11.  *  char *
  12.  * strentab(destination,source,tabs,def,opt,maxlength)
  13.  *
  14.  * ARGUMENT
  15.  *  (char *)    destination    -    points to destination string
  16.  *  (char *)    source        -    points to source string
  17.  *  (char *)    tabs        -    tab stops string
  18.  *  (int)    def        -    default tab stop intervals
  19.  *                    (only used when tabs==NULL)
  20.  *  (int)    opt        -    options
  21.  *                    0=Expand single spaces
  22.  *                    1=Copy Single Spaces
  23.  *  (int)    maxlength    -    maximum length of output string
  24.  *
  25.  * DESCRIPTION
  26.  *  The source string is copied to the destination string except that
  27.  *  spaces are converted to tab characters (\t).
  28.  *
  29.  * SIDE EFFECTS
  30.  *  (none)
  31.  *
  32.  * RETURNS
  33.  *  pointer to destination string or NULL if source or destination are NULL
  34.  *  or tabs is NULL and default is 0.
  35.  *
  36.  * AUTHOR
  37.  *  ""   21-APR-1987  13:45:50.22
  38.  *   Copyright (C)1987-1990 Greenleaf Software Inc. All Rights Reserved.
  39.  *
  40.  * MODIFICATIONS
  41.  *
  42.  */
  43. char* GF_CONV strentab(destination,source,tabs,def,opt,maxlength)
  44. char *destination,*source,*tabs;
  45. int def,opt,maxlength;
  46. {
  47.     char *orgdest,*s,*t;
  48.     int column,tabcount;
  49.     register int spacecount;
  50.     
  51.     if(!destination||!source||(!tabs&&!def)||opt<0||opt>1)
  52.         return((char *)0);
  53.     orgdest=destination;
  54.     for(spacecount=column=0;*source&&maxlength;--maxlength,spacecount=0) {
  55.         if(*source&&*source==' ')
  56.             for(s=source;*s&&*s==' ';++s,++spacecount)
  57.                 ;
  58.         if(spacecount>1||(spacecount==1&&opt!=YES)) {
  59.             if(tabs) {
  60.                 for(t=tabs,tabcount=1;*t&&(*t!='t'&&*t!='T'&&
  61.                     *t!='x'&&*t!='X');++t,++tabcount)
  62.                         ;
  63.                 if(tabcount>1&&*t&&spacecount>=tabcount) {
  64.                     tabs+=tabcount;
  65.                     source+=tabcount;
  66.                     *destination++='\t';
  67.                 } else
  68.                     for(;maxlength&&spacecount;
  69.                          --maxlength,--spacecount,++tabs)
  70.                             *destination++=*source++;
  71.             } else {
  72.                 tabcount=column;
  73.                 while(++tabcount%def)
  74.                     ;
  75.                 tabcount-=column;
  76.                 if(tabcount&&spacecount>=tabcount) {
  77.                     source+=tabcount;
  78.                     column+=tabcount;
  79.                     *destination++='\t';
  80.                 } else
  81.                     for(;maxlength&&spacecount;
  82.                         --maxlength,--spacecount,++column)
  83.                             *destination++=*source++;
  84.             }
  85.         } else {
  86.             *destination++=*source++;
  87.             tabs+=(tabs)?1:0;
  88.             ++column;
  89.         }
  90.     }
  91.     *destination='\0';
  92.     return(orgdest);
  93. }
  94.