home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / cpgms.zip / TELEFIX.C < prev    next >
C/C++ Source or Header  |  1985-11-17  |  6KB  |  271 lines

  1. /*
  2.       This program read a file containing names as follows:
  3.         <last name><,><first name><space><mi><space><(><nickname><)>
  4.       and reformats it to:
  5.         <first name><space><mi><space><last name>.
  6.       nicknames are dropped and Jr, Sr, etc are kept at the end. */
  7.  
  8. #include "stdio.h"
  9. #define EOF 0x1a
  10. #define ERROR 0
  11. #define CPMEOF 0X1A
  12. #define NULL 0
  13. #define EOS  0
  14. #define DeSmet 1
  15.  
  16. struct {
  17.     char *suffix; /* suffix in text (w/ first char in caps) */
  18.     char *newsuf; /* replacement suffix */
  19.   } stbl[]  = {{"JR","JR"},
  20.            {"SR","SR"},
  21.            {"III","III"},
  22.            {"II","II"},
  23.            {"IV","IV"},
  24.            {0x00,0x00}};
  25. int sufindx;            /* index to prior table */
  26.  
  27.  
  28.  
  29. char    *documentation[] = {
  30. "telefix cleans up a list of names.",
  31. "   telefix [flags] [file] [<file] [>file]",
  32. "",
  33. "Flags are single characters preceeded by '-':",
  34. "   -       none are yet defined",
  35. "",
  36. "Input is from the given file or stdin if no file is named.",
  37. "Output is to stdout.  To print add >PRN: to your command.",
  38. 0 };
  39.  
  40. int debug = 0;
  41.  
  42. main(argc, argv)
  43. char *argv[];
  44. {
  45.    register char   *p;
  46.    register int    c, i;
  47.     int nfile;
  48.     char tolower();
  49.    FILE  fdta;
  50.  
  51.    if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) {
  52.       help(documentation);
  53.       return;
  54.       }
  55.    nfile = argc-1;
  56.    for (i=1; i < argc; ++i) {
  57.       p = argv[i];
  58.       if (*p == '-') {
  59.      ++p;
  60.      while (c = *p++) {
  61.         switch(tolower(c)) {
  62.  
  63.         case '?':
  64.            help(documentation);
  65.            break;
  66.  
  67.         case 'D':
  68.         case 'd':
  69.            debug++;
  70.            break;
  71.  
  72.         default:
  73.            usage("Unknown flag");
  74.         }
  75.      }
  76.      argv[i] = 0;
  77.      --nfile;
  78.       }
  79.    }
  80.     if (nfile == 0)
  81.         fdta = stdin;
  82.     else
  83.         for (i=1; i < argc; ++i)
  84.             if (argv[i] != 0) {
  85.             if((fdta = fopen(argv[i],"r")) == NULL)
  86.                 cant(argv[i]);
  87.             break;
  88.             }
  89.  
  90.     telefix(fdta, argv[i]);
  91.  
  92.     if (fdta != stdin)
  93.           fclose(fdta);
  94. }
  95.  
  96.  
  97. /*******************************************************/
  98.  
  99. usage(s)
  100. char    *s;
  101. {
  102.    puts("?LIST-E-");
  103.    puts(s);
  104.    puts("\n");
  105.    puts("Usage: telefix [-flags] [file] [<file] [>file].  \n");
  106.    puts(" telefix ? for help\n");
  107.    exit(1);
  108. }
  109.  
  110.  
  111. /*******************************************************/
  112.  
  113. telefix(fdin,filenm)
  114. FILE fdin;
  115. char *filenm;
  116. {
  117.     char *sp, *cp, *wp, *xp;    /* starting ptr, current ptr, and word ptr */
  118.     int parensw, commasw, wordsw, abbrv, lblanksw;
  119.     char c, lname[30], line[80];
  120.  
  121.     while (sp = cp = wp = fgets(line, sizeof(line), fdin)) {
  122. #if DeSmet
  123.         while (*wp)
  124.             if (*wp++ == '\n')
  125.                 wp[-1] = ' ';
  126. #endif
  127.         commasw = parensw = wordsw = abbrv = lblanksw = 0;
  128.         wp = cp;
  129.  
  130.         if (debug)
  131.             puts(sp);    /* debug */
  132.  
  133.         while (c = *cp) {
  134.             switch (c) {
  135.  
  136.             case '(':   /* start of nickname */
  137.                 parensw = 1;
  138.                 *cp = ' ';
  139.                 break;
  140.  
  141.             case ')':   /* end of nickname */
  142.                 parensw = 0;
  143.                 *cp = ' ';
  144.                 break;
  145.  
  146.             case ',':   /* last name delimited. copy it */
  147.                 commasw = 1;
  148.                 strncpy(lname, wp, cp - wp);
  149.                 lname[cp - wp] = EOS;  /* end of string */
  150.                 strcpy(sp,cp+1);   /* delete last name */
  151.                 wp = cp = sp-1;          /* and reset pointer */
  152.                 if (debug)
  153.                     printf("  lname = %s, fmi = %s\n",
  154.                          lname, sp);
  155.                 break;
  156.  
  157.             case ' ':   /* leading blank???? */
  158.                 if (!lblanksw)
  159.                     wp++;       /* then fall through into default */
  160.  
  161.             default:
  162.                 if (!commasw)    /* do nothing to last name */
  163.                     break;
  164.                 lblanksw = 1;
  165.                 if (parensw) { /* blank out nicknames  */
  166.                     *cp = ' ';
  167.                     break;
  168.                 }
  169.                 if (isalpha(c)) {
  170.                     if (!wordsw) {
  171.                         wordsw = 1;
  172.                         wp = cp;
  173.                     }
  174.                 }
  175.                 else {
  176.                     if (wordsw == 1) {
  177.                         abbrv = srchtbl(wp - 1, cp - 1); /* Jr, etc */
  178.                         if (abbrv) {
  179.                             for (xp=wp-1; xp < cp; xp++)
  180.                                 *xp = ' ';
  181.                             if (debug)
  182.                                 printf("  abbrv = %s\n",
  183.                                    stbl[sufindx].newsuf);
  184.                         }
  185.                       }
  186.                     wordsw = 0;
  187.                 }
  188.                 break;
  189.             }
  190.             cp++;
  191.         }
  192.     /* output name (first, mi, last, suffix)  */
  193.         if (debug)
  194.             printf(" output fmi = %s\n", sp);
  195.         cp = sp;
  196.         while (*cp) {    /* here comes first and mi  */
  197.             if (!(*cp == ' ' && cp[-1] == ' '))
  198.                 putc(*cp, stdout);
  199.             cp++;
  200.         }
  201.         if (cp[-1] != ' ')
  202.             putc(' ', stdout);  /* make a nice space  */
  203.         wp = &lname;
  204.         while (*wp) {    /* here comes the last name */
  205.             putc(*wp, stdout);
  206.             wp++;
  207.         }
  208.         if (abbrv) {
  209.             putc(',', stdout);  /* make a nice comma  */
  210.             putc(' ', stdout);  /* make a nice space  */
  211.             wp = stbl[sufindx].newsuf;
  212.             while (*wp) {  /* Jr, Sr, etc, if any  */
  213.                 putc(*wp, stdout);
  214.                 wp++;
  215.             }
  216.         }
  217.         putc('\n', stdout);     /* end of record */
  218.     }
  219. }
  220.  
  221.  
  222. /*******************************************************/
  223.  
  224. int srchtbl(start, end)
  225. char *start, *end;
  226. {
  227.     char *sp, arg[30];
  228.  
  229.     makstr(arg,start,end,sizeof(arg));    /* turn into a real string */
  230.  
  231.     /*   search the table    */
  232.  
  233.     if (debug)
  234.         printf("  -->arg = %s\n", arg);
  235.     sufindx = 0;
  236.     while (sp = stbl[sufindx++].suffix)     /* till end of table */
  237.         if (!strcmp(sp, arg)) /* compare to tbl */
  238.             break;
  239.     sufindx--;
  240.  
  241.     if (debug)
  242.         printf("  -->sufindx = %d\n", sufindx);
  243.     if (!stbl[sufindx].newsuf)
  244.         return 0;
  245.     else
  246.         return 1;
  247. }
  248.  
  249.  
  250. /*******************************************************/
  251.  
  252. int makstr(buffer, start, end, length)
  253. char *buffer, *start, *end;
  254. int length;
  255. {
  256.     char *sp;
  257.     int i, j, len;
  258.  
  259.     sp = start;
  260.     if ((len = end - start) > length - 1)
  261.         len = length - 1;
  262.  
  263.     for (j=i=0; i <= length; j=i++)
  264.         if (j < len)
  265.             buffer[j] = *sp++;
  266.         else {
  267.             buffer[j] = EOS;
  268.             break;
  269.         }
  270. }
  271.