home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
progm
/
cpgms.zip
/
TELEFIX.C
< prev
next >
Wrap
C/C++ Source or Header
|
1985-11-17
|
6KB
|
271 lines
/*
This program read a file containing names as follows:
<last name><,><first name><space><mi><space><(><nickname><)>
and reformats it to:
<first name><space><mi><space><last name>.
nicknames are dropped and Jr, Sr, etc are kept at the end. */
#include "stdio.h"
#define EOF 0x1a
#define ERROR 0
#define CPMEOF 0X1A
#define NULL 0
#define EOS 0
#define DeSmet 1
struct {
char *suffix; /* suffix in text (w/ first char in caps) */
char *newsuf; /* replacement suffix */
} stbl[] = {{"JR","JR"},
{"SR","SR"},
{"III","III"},
{"II","II"},
{"IV","IV"},
{0x00,0x00}};
int sufindx; /* index to prior table */
char *documentation[] = {
"telefix cleans up a list of names.",
" telefix [flags] [file] [<file] [>file]",
"",
"Flags are single characters preceeded by '-':",
" - none are yet defined",
"",
"Input is from the given file or stdin if no file is named.",
"Output is to stdout. To print add >PRN: to your command.",
0 };
int debug = 0;
main(argc, argv)
char *argv[];
{
register char *p;
register int c, i;
int nfile;
char tolower();
FILE fdta;
if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) {
help(documentation);
return;
}
nfile = argc-1;
for (i=1; i < argc; ++i) {
p = argv[i];
if (*p == '-') {
++p;
while (c = *p++) {
switch(tolower(c)) {
case '?':
help(documentation);
break;
case 'D':
case 'd':
debug++;
break;
default:
usage("Unknown flag");
}
}
argv[i] = 0;
--nfile;
}
}
if (nfile == 0)
fdta = stdin;
else
for (i=1; i < argc; ++i)
if (argv[i] != 0) {
if((fdta = fopen(argv[i],"r")) == NULL)
cant(argv[i]);
break;
}
telefix(fdta, argv[i]);
if (fdta != stdin)
fclose(fdta);
}
/*******************************************************/
usage(s)
char *s;
{
puts("?LIST-E-");
puts(s);
puts("\n");
puts("Usage: telefix [-flags] [file] [<file] [>file]. \n");
puts(" telefix ? for help\n");
exit(1);
}
/*******************************************************/
telefix(fdin,filenm)
FILE fdin;
char *filenm;
{
char *sp, *cp, *wp, *xp; /* starting ptr, current ptr, and word ptr */
int parensw, commasw, wordsw, abbrv, lblanksw;
char c, lname[30], line[80];
while (sp = cp = wp = fgets(line, sizeof(line), fdin)) {
#if DeSmet
while (*wp)
if (*wp++ == '\n')
wp[-1] = ' ';
#endif
commasw = parensw = wordsw = abbrv = lblanksw = 0;
wp = cp;
if (debug)
puts(sp); /* debug */
while (c = *cp) {
switch (c) {
case '(': /* start of nickname */
parensw = 1;
*cp = ' ';
break;
case ')': /* end of nickname */
parensw = 0;
*cp = ' ';
break;
case ',': /* last name delimited. copy it */
commasw = 1;
strncpy(lname, wp, cp - wp);
lname[cp - wp] = EOS; /* end of string */
strcpy(sp,cp+1); /* delete last name */
wp = cp = sp-1; /* and reset pointer */
if (debug)
printf(" lname = %s, fmi = %s\n",
lname, sp);
break;
case ' ': /* leading blank???? */
if (!lblanksw)
wp++; /* then fall through into default */
default:
if (!commasw) /* do nothing to last name */
break;
lblanksw = 1;
if (parensw) { /* blank out nicknames */
*cp = ' ';
break;
}
if (isalpha(c)) {
if (!wordsw) {
wordsw = 1;
wp = cp;
}
}
else {
if (wordsw == 1) {
abbrv = srchtbl(wp - 1, cp - 1); /* Jr, etc */
if (abbrv) {
for (xp=wp-1; xp < cp; xp++)
*xp = ' ';
if (debug)
printf(" abbrv = %s\n",
stbl[sufindx].newsuf);
}
}
wordsw = 0;
}
break;
}
cp++;
}
/* output name (first, mi, last, suffix) */
if (debug)
printf(" output fmi = %s\n", sp);
cp = sp;
while (*cp) { /* here comes first and mi */
if (!(*cp == ' ' && cp[-1] == ' '))
putc(*cp, stdout);
cp++;
}
if (cp[-1] != ' ')
putc(' ', stdout); /* make a nice space */
wp = &lname;
while (*wp) { /* here comes the last name */
putc(*wp, stdout);
wp++;
}
if (abbrv) {
putc(',', stdout); /* make a nice comma */
putc(' ', stdout); /* make a nice space */
wp = stbl[sufindx].newsuf;
while (*wp) { /* Jr, Sr, etc, if any */
putc(*wp, stdout);
wp++;
}
}
putc('\n', stdout); /* end of record */
}
}
/*******************************************************/
int srchtbl(start, end)
char *start, *end;
{
char *sp, arg[30];
makstr(arg,start,end,sizeof(arg)); /* turn into a real string */
/* search the table */
if (debug)
printf(" -->arg = %s\n", arg);
sufindx = 0;
while (sp = stbl[sufindx++].suffix) /* till end of table */
if (!strcmp(sp, arg)) /* compare to tbl */
break;
sufindx--;
if (debug)
printf(" -->sufindx = %d\n", sufindx);
if (!stbl[sufindx].newsuf)
return 0;
else
return 1;
}
/*******************************************************/
int makstr(buffer, start, end, length)
char *buffer, *start, *end;
int length;
{
char *sp;
int i, j, len;
sp = start;
if ((len = end - start) > length - 1)
len = length - 1;
for (j=i=0; i <= length; j=i++)
if (j < len)
buffer[j] = *sp++;
else {
buffer[j] = EOS;
break;
}
}