Back to index

radiance  4R0+20100331
neat.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: neat.c,v 1.4 2003/11/14 17:31:24 schorsch Exp $";
00003 #endif
00004 /*
00005  *  neat.c - program to tidy up columns.
00006  *
00007  *     10/24/86
00008  */
00009 
00010 #include  <stdlib.h>
00011 #include  <stdio.h>
00012 
00013 #include  <ctype.h>
00014 
00015 
00016 char  *format = "8.8";                    /* default format */
00017 
00018 
00019 int
00020 main(argc, argv)
00021 int  argc;
00022 char  *argv[];
00023 {
00024        int  left, anchor, right;
00025        char  buf[512];
00026        register char  *cp, *word;
00027        register int  i;
00028 
00029        if (argc == 2)
00030               format = argv[1];
00031        else if (argc > 2)
00032               goto userror;
00033        left = 0;
00034        for (cp = format; isdigit(*cp); cp++)
00035               left = left*10 + *cp - '0';
00036        right = 0;
00037        if ( (anchor = *cp) )
00038               for (cp++; isdigit(*cp); cp++)
00039                      right = right*10 + *cp - '0';
00040        if (*cp)
00041               goto userror;
00042        
00043        while ((cp = fgets(buf, sizeof(buf), stdin)) != NULL)
00044               for ( ; ; ) {
00045                      while (isspace(*cp))
00046                             cp++;
00047                      if (!*cp) {
00048                             putchar('\n');
00049                             break;
00050                      }
00051                      word = cp;
00052                      while (*cp && *cp != anchor && !isspace(*cp))
00053                             cp++;
00054                      i = left-(cp-word);
00055                      while (i-- > 0)
00056                             putchar(' ');
00057                      while (word < cp)
00058                             putchar(*word++);
00059                      i = right+1;
00060                      if (*cp == anchor)
00061                             do {
00062                                    putchar(*cp++);
00063                                    i--;
00064                             } while (*cp && !isspace(*cp));
00065                      while (i-- > 0)
00066                             putchar(' ');
00067               }
00068        exit(0);
00069 userror:
00070        fputs("Usage: ", stderr);
00071        fputs(argv[0], stderr);
00072        fputs(" [format]\n", stderr);
00073        exit(1);
00074 }