home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / database / db12 / stredt.c < prev   
Encoding:
C/C++ Source or Header  |  1987-07-14  |  1.8 KB  |  102 lines

  1. #include <stdio.h>
  2.  
  3. /*
  4.  
  5.         memedt / stredt  edit buffer/string based on flag
  6.  
  7.         flag values:    1    -   trim parity bit
  8.                 2    -   discard all spaces and tabs
  9.                 4    -   discard excess characters
  10.                 8    -   discard leading spaces & tabs
  11.                       16    -   reduce spaces and tabs to 1
  12.                     32    -   convert to upper case
  13.                                64    -   *** not used ***
  14.                   128    -   discard trailing spaces & tabs
  15.                   256    -   don't alter char in quotes
  16.  
  17.  
  18. */
  19.  
  20. int memedt(s, n, f)
  21. char *s;
  22. int   n,f;
  23. {
  24.     char *p, *q;
  25.     int  c, prev_c, i, q_flag, dq_flag;
  26.     
  27.     q_flag = dq_flag =0;
  28.  
  29.     q = s;
  30.     i = n;
  31.     if (f & 8)  /* strip leading spaces & tabs */
  32.     {    while (i && (*q == ' ' || *q == '\t')) 
  33.         {    q++;
  34.             i--;
  35.         }
  36.     }        
  37.     if (f & 128)  /* strip trailing spaces and tabs */
  38.     {    p = s + n - 1;
  39.         while (i && (*p == ' ' || *p == '\t'))
  40.         {    p--;
  41.             i--;
  42.         }
  43.     }
  44.     p = s;
  45.     while (i)
  46.     {    c = *q++;
  47.         i--;
  48.  
  49.         if (p > s)
  50.             prev_c = *(p-1);
  51.         else
  52.             prev_c = NULL;
  53.  
  54.         if (f & 256)
  55.         {    if (c=='\'' && !dq_flag) 
  56.             {    q_flag = ~ q_flag;
  57.                 *p++ = c;
  58.                 continue;
  59.             }
  60.             if (c=='"' && !q_flag) 
  61.             {    dq_flag = ~ dq_flag;
  62.                 *p++ = c;
  63.                 continue;
  64.             }
  65.             if (q_flag || dq_flag) 
  66.             {    *p++ = c;
  67.                 continue;
  68.             }
  69.         }
  70.  
  71.         if (f & 1) c = c & 127;
  72.  
  73.         if (f & 2)
  74.             if (c == ' ' || c=='\t') continue;
  75.  
  76.         if (f & 4)
  77.             if (c==0 || c==10 || c==12 || c==13 || c==27 || c==127)
  78.                 continue;
  79.  
  80.         if (f & 16) 
  81.             if (c == ' ' || c == '\t')
  82.                 if (prev_c == ' ' || prev_c == '\t')
  83.                     continue;
  84.  
  85.         if (f & 32) c = toupper(c);
  86.  
  87.         *p++ = c;
  88.     }
  89.     return(p-s);
  90. }
  91.             
  92. int stredt(s,f)     /* edit string */
  93. char *s;
  94. int  f;
  95. {
  96.     int i;
  97.  
  98.         i = memedt(s,strlen(s),f);
  99.     s[i] = NULL;
  100.     return(i);
  101. }
  102.