diff options
author | sinanmohd <pcmsinan@gmail.com> | 2022-12-17 07:57:30 +0530 |
---|---|---|
committer | sinanmohd <pcmsinan@gmail.com> | 2022-12-17 07:57:30 +0530 |
commit | b81fb4214a8055588048b0fb392a0b2b9157a286 (patch) | |
tree | 28568c12fe6137a5ac465ac383700b2d6ba3547e /5.11.c | |
parent | b73253fb617886664d6e2f67a6e45c9448f5d7cb (diff) |
5.11: rewrite, fixed broken entab
Diffstat (limited to '5.11.c')
-rw-r--r-- | 5.11.c | 226 |
1 files changed, 111 insertions, 115 deletions
@@ -1,132 +1,128 @@ #include <stdio.h> +#include <stdlib.h> #include <ctype.h> #define DEFTAB 8 -#define MAXSTOPS 65 -#define ON 1 +#define TRUE 1 +#define FALSE 0 +#define MAXSTOPS 100 int stops[MAXSTOPS]; -int stop_position, detab_bool, entab_bool; -void entab(void); -void detab(void); +void detab(int *stops, int limit); +void entab(int *stops, int limit); -int -main(int argc, char **argv) +int main(int argc, char **argv) { - int flag; - while (--argc > 0 && **++argv == '-') - while((flag = *++*argv)) - switch (flag) { - case 'e' : - entab_bool = ON; - break; - case 'd' : - detab_bool = ON; - break; - case 's' : - if(*++*argv != '=') - argc = -1; - else - (*argv)++; - - if (**argv == '\0') - argc = -1; - - while (isdigit(**argv)) { - if (stop_position > MAXSTOPS) - argc = -1; - - stops[stop_position] = stops[stop_position] * 10 + (**argv) - '0'; - (*argv)++; - if (**argv == ',') { - (*argv)++; - if(isdigit(**argv) && stop_position < MAXSTOPS) - stop_position++; - } - } - stop_position++; - - (*argv)--; - break; - default : - argc = -1; - printf("Err: illigal option %c\n", flag); - break; - } - - if (argc || !entab_bool ^ detab_bool) - printf("Usage: tab (-e || -d) -s=s1,s2,s3,...,s%d\n", MAXSTOPS-1); - else if(entab_bool) - entab(); - else if (detab_bool) - detab(); - - return 0; -} - -void -entab(void) -{ - char input; - int space, stop_now; - - space = 0; - stop_now = 0; - stops[stop_position] = DEFTAB; - - while((input = getchar()) != EOF) - switch(input) { - case ' ' : - space++; - break; - default : - while (space/stops[stop_now]) { - space -= stops[stop_now]; - - putchar('\t'); - - if (stop_now < stop_position) - stop_now++; + int entab_bool = FALSE; + int detab_bool = FALSE; + int stop_position = 0; + + while (--argc > 0 && **++argv == '-') { + while (*++*argv) { + switch (**argv) { + case 'e' : + entab_bool = TRUE; + break; + case 'd' : + detab_bool = TRUE; + break; + case 's' : + if (*++*argv != '=') + break; + + while (*++*argv && stop_position < MAXSTOPS) { + stops[stop_position++] = atoi(*argv); + printf("loooop -- %d\n", stop_position); + + while (isdigit(**argv)) + ++*argv; + + if (**argv == '\0') + --*argv; + } + break; + } + } } - while (space) { - putchar(' '); - space--; + stops[stop_position] = DEFTAB; + + if (entab_bool ^ detab_bool) { + if (entab_bool) + entab(stops, stop_position); + else if (detab_bool) + detab(stops, stop_position); + } else { + printf("usage: tab -[ed] -s=s1,s2,s3,...,s3\n"); } - putchar(input); - break; - } + return 0; } -void -detab(void) +void detab(int *stops, int limit) { - char input; - int count, space, stop_now; - - count = 0; - stop_now = 0; - stops[stop_position] = DEFTAB; - - while ((input = getchar()) != EOF) - switch (input) { - case '\n' : - count = 0; - putchar('\n'); - break; - case '\t' : - space = stops[stop_now] - count%stops[stop_now]; - count += space; - while (space--) - putchar(' '); - if (stop_now < stop_position) - stop_now++; - break; - default : - count++; - putchar(input); - break; - } + char input; + int space, count = 0, stop_position = 0; + + while((input = getchar()) != EOF) { + switch (input) { + case '\t' : + for (space = count%stops[stop_position]; space < stops[stop_position]; space++) + putchar(' '); + count = 0; + + if (stop_position < limit) + stop_position++; + + break; + case '\n' : + count = 0; + break; + default : + putchar(input); + count++; + break; + + } + } +} + +void entab(int *stops, int limit) +{ + char input; + int spaces = 0, stop_position = 0, count = 0; + + while((input = getchar()) != EOF) { + switch (input) { + case ' ' : + spaces++; + break; + case '\n' : + count = 0; + putchar('\n'); + break; + default : + // add spaces misssed by the first tab + if (spaces >= stops[stop_position]) + spaces += count%stops[stop_position]; + + while (spaces/stops[stop_position]) { + spaces = spaces - stops[stop_position]; + + putchar('\t'); + + if (stop_position < limit) + stop_position++; + } + + while (spaces--) + putchar(' '); + + spaces = 0; + count++; + putchar(input); + break; + } + } } |