Category : Word Processors
Archive   : TOKREP.ZIP
Filename : TOKREP.C

 
Output of file : TOKREP.C contained in archive : TOKREP.ZIP

/*
*
* TOKREP.C - tokenized replace strings in files
*
* Usage:
*
* TOKREP infile [-d destfile][-1][-s strfile][-l][-u][-o oldstr -n newstr]
*
* Copyright 1983, David Brower.
*
* 1.0 - 6/16/84 Dave Brower from replacer 1.7
*
*/



/*
* DATA FILE FORMAT:
* -----------------
*
* The string file is constructed of pairs of lines;
* The first line contains the "old" string and the second contains
* the replacement text. The trailing newlines don't count.
*
*
* Example:
*
* oldword1
* newword1
* oldword2
* newword2
*/

#include
#ifdef LATTICE
#include "a:ctype.h"
#endif

#include "DATE.H"

#define MYNAME "TokRep"
#define VERSION "1.0"

#define MAXREP 400 /* Max number of strings to replace */
#define MAXLINE 135
#define VOID int

#define FOPEN(file,mode,fp) ( (fp=fopen(file,mode))>0 )

#ifdef C86
#define AINIT()
#define ALLOC(size) malloc(size)
char *malloc();
#endif

#ifdef LATTICE
#define AINIT() allmem()
#define ALLOC(size) getmem(size)
char *getmem();
#endif


/* global variables */

int onechange, /* flags one change per input line option */
repc; /* number of strings in oldv/newv */

FILE *ofp; /* output file pointer */

char infile[MAXLINE], /* input file name */
destfile[MAXLINE], /* output file name */
strfile[MAXLINE], /* string file */
*oldv[MAXREP], /* oldstrings */
*newv[MAXREP]; /* new strings */


VOID
main(argc,argv)
int argc;
char **argv;
{
FILE *ifp; /* input file descriptor */

int nummsgs, /* number of msg bodies done */
changes, /* count of changes made */
cnt, /* line counter to avoid boredom */
intok, /* in a token flag */
len, /* length of token */
nochange, /* token not found */
c, /* current input char */
i; /* loop counter */

char * tok, /* pointer inside token */
token[MAXLINE]; /* another buffer */

/* initialize */
fprintf(stderr, "%s version %s of %s\n", MYNAME, VERSION, DATE);
fprintf(stderr, "Copyright (c) 1984, David Brower\n");

scanargs(argc,argv); /* handle arguments */
AINIT();


/* read in the strings */

if(*strfile) {
if( !FOPEN( strfile,"r",ifp)) punt( strfile );
fprintf(stderr, "Reading replacement strings from '%s'\n",strfile);
for( ; repc {
if( !(repc%10) ) fprintf(stderr,"%4d strings\r",repc);
oldv[repc] = ALLOC( len=strlen(token) );
token[len-1]='\0'; /* kill the \n */
strcpy( oldv[repc], token );
if(!fgets( token, MAXLINE, ifp ) ) {
fprintf(stderr,"\n%s: Unexpected EOF -- Have:\n",
MYNAME);
fprintf(stderr,"'%s',\nlooking for new string.\n",
oldv[repc] );
exit();
}
newv[repc] = ALLOC( len=strlen(token) );
token[len-1]='\0'; /* kill the \n */
strcpy( newv[repc], token );
} /* end of for */

fprintf(stderr,"%4d strings\n",repc);
fclose( ifp );
if( repc >= MAXREP ) {
fprintf(stderr, "%s: Too many lines in '%s'! (%d max)\n",
MYNAME, strfile,MAXREP);
exit(-1);
}
} /* end of if(*strfile) */

/* process the input file & replace all strings */

if( !FOPEN( infile,"r",ifp) ) punt( infile );
fprintf(stderr, "Replacing strings in '%s'\n", infile);
intok=cnt=changes = 0;
tok = token;
while( (c=fgetc(ifp)) != EOF )
{
if( c=='\n' ) { /* check for line count */
cnt++;
if( ofp != stdout ) /* don't dirty up output if to stdout */
fprintf(stderr,"%4d lines, %d changes\r", cnt, changes);
}
if( isalnum(c) || (c == '_') ) /* accumulate tokens */
{
intok = 1;
*tok++ = c;
continue;
}
else if( !intok ) /* just pass non token char */
{
fputc(c,ofp);
continue;
}
else { /* check token */
intok = 0;

/* At this point, we're in a token, but have
* just read in a non-token char. Look up the
* token and replace it if needed, then write
* out the term char.
*/

*tok = '\0';
tok=token;
nochange = 1;
for( i = 0; i if( !strcmp(token,oldv[i]) )
{
changes++;
fprintf(ofp,"%s",newv[i]);
nochange = 0;
}
}
if( nochange ) {
fprintf(ofp,"%s",token); /* not found */
}
fputc(c,ofp);
}

} /* while !EOF */


fclose(ifp);
fclose(ofp);
fprintf(stderr,"%4d lines, %d changes\n", cnt, changes);
exit(0);
} /* end of main() */




VOID
scanargs(argc, argv) /* Process the arguments */
int argc;
char **argv;
{
int upcasep, lowcasep;

*infile = *destfile = *strfile = NULL;
*oldv = *newv = "";
repc = 0;

if( argc == 1 ) usage(); /* any args at all? */

*infile = NULL; /* no input file or title */
while( --argc ) {
if( **++argv != '-' ) /* infile name */
{
if(!*infile)
strcpy( infile, *argv );
else {
fprintf(stderr, "\07%s: Already have input file '%s'\n",
MYNAME, infile);
badarg( *argv );
}
}

else

switch( tolower( argv[0][1] ) )
{
case 'd': /* -d destfile */
if(!*destfile) {
--argc;
strcpy( destfile, *++argv );
}
else {
fprintf(stderr,
"\07%s: Already have destination file '%s'\n",
MYNAME, destfile);
badarg( *argv );
}
break;

case 'n':
if(!**newv) {
--argc;
*newv=ALLOC(strlen(*++argv)+1);
strcpy( *newv, *argv );
}
else {
fprintf(stderr, "\07%s: Already have newstring '%s'\n",
MYNAME, *newv);
badarg( *argv );
}
break;

case 'o':
if(!**oldv) {
--argc;
*oldv=ALLOC(strlen(*++argv)+1);
strcpy( *oldv, *argv );
}
else {
fprintf(stderr, "\07%s: Already have oldstring '%s'\n",
MYNAME, *oldv);
badarg( *argv );
}
break;

case 'r': /* for "replacements", because I forget to use 's' */
case 's':
if(!*strfile) {
--argc;
strcpy( strfile, *++argv );
}
else {
fprintf(stderr, "\07%s: Already have string file '%s'\n",
MYNAME, strfile);
badarg( *argv );
}
break;

default:
badarg( *argv ); /* choke */

} /* end of the switch */

} /* end of while() */



/* make sure he specified an input file */

if( !*infile ) {
fprintf(stderr, "\07%s: Missing infile\n",MYNAME);
usage();
}

if(!*destfile ) ofp = stdout;
else {
if(!FOPEN(destfile,"w",ofp)) punt( destfile );
fprintf(stderr,"Sending output to \"%s\" \n", destfile);
}

if( **oldv && **newv ) {
++repc;
fprintf(stderr, "Replacing '%s' with '%s'\n", *oldv, *newv);
}
else
if( !*strfile ) {
fprintf(stderr, "\07%s: I need some strings! [-nos]\n",
MYNAME);
usage();
}
} /* end of scanargs() */



VOID
usage()
{

fprintf(stderr,

"\07\nUsage: %s: infile [-d destfile][-s strfile][-o oldstr -n newstr]",
MYNAME );
fprintf(stderr,
"\nWhere:\n");
fprintf(stderr,
"\tinfile\tIs the source file to do replacements on\n");
fprintf(stderr,
"\t-d destfile\tIs the output file, defauting to the console\n");
fprintf(stderr,
"\t-s strfile\tIs a file containing replacement pairs\n");
fprintf(stderr,
"\t-o oldstr\tIs an old string to replace\n");
fprintf(stderr,
"\t-n newstr\tIs the replacement string for -o\n" );
exit(-1);
}



VOID
badarg(s)
char *s;
{
fprintf(stderr, "%s: bad argument '%s'\n", MYNAME, s );
usage();
}




VOID
punt(s)
char *s;
{
fprintf(stderr, "\n\07Can't open '%s'\n", s);
exit(-1);
}


/**************** end of TOKREP.C ****************/