home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
-
- /*
-
- memedt / stredt edit buffer/string based on flag
-
- flag values: 1 - trim parity bit
- 2 - discard all spaces and tabs
- 4 - discard excess characters
- 8 - discard leading spaces & tabs
- 16 - reduce spaces and tabs to 1
- 32 - convert to upper case
- 64 - *** not used ***
- 128 - discard trailing spaces & tabs
- 256 - don't alter char in quotes
-
-
- */
-
- int memedt(s, n, f)
- char *s;
- int n,f;
- {
- char *p, *q;
- int c, prev_c, i, q_flag, dq_flag;
-
- q_flag = dq_flag =0;
-
- q = s;
- i = n;
- if (f & 8) /* strip leading spaces & tabs */
- { while (i && (*q == ' ' || *q == '\t'))
- { q++;
- i--;
- }
- }
- if (f & 128) /* strip trailing spaces and tabs */
- { p = s + n - 1;
- while (i && (*p == ' ' || *p == '\t'))
- { p--;
- i--;
- }
- }
- p = s;
- while (i)
- { c = *q++;
- i--;
-
- if (p > s)
- prev_c = *(p-1);
- else
- prev_c = NULL;
-
- if (f & 256)
- { if (c=='\'' && !dq_flag)
- { q_flag = ~ q_flag;
- *p++ = c;
- continue;
- }
- if (c=='"' && !q_flag)
- { dq_flag = ~ dq_flag;
- *p++ = c;
- continue;
- }
- if (q_flag || dq_flag)
- { *p++ = c;
- continue;
- }
- }
-
- if (f & 1) c = c & 127;
-
- if (f & 2)
- if (c == ' ' || c=='\t') continue;
-
- if (f & 4)
- if (c==0 || c==10 || c==12 || c==13 || c==27 || c==127)
- continue;
-
- if (f & 16)
- if (c == ' ' || c == '\t')
- if (prev_c == ' ' || prev_c == '\t')
- continue;
-
- if (f & 32) c = toupper(c);
-
- *p++ = c;
- }
- return(p-s);
- }
-
- int stredt(s,f) /* edit string */
- char *s;
- int f;
- {
- int i;
-
- i = memedt(s,strlen(s),f);
- s[i] = NULL;
- return(i);
- }