home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource3
/
126_01
/
martz_ei.c
< prev
next >
Wrap
Text File
|
1985-03-11
|
13KB
|
423 lines
/*********************************************************************\
** .---------------------------------------------------------------. **
** | | **
** | | **
** | Copyright (c) 1981, 1982, 1983 by Eric Martz. | **
** | | **
** | | **
** | Permission is hereby granted to use this source | **
** | code only for non-profit purposes. Publication of | **
** | all or any part of this source code, as well as | **
** | use for business purposes is forbidden without | **
** | written permission of the author and copyright | **
** | holder: | **
** | | **
** | Eric Martz | **
** | POWER TOOLS | **
** | 48 Hunter's Hill Circle | **
** | Amherst MA 01002 USA | **
** | | **
** | | **
** `---------------------------------------------------------------' **
\*********************************************************************/
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* MARTZLIB.C IS A LIBRARY OF FUNCTIONS OF GENERAL USE.
MARTZ-EI.C CONTAINS:
equal(s1,s2)
fbrkout(width, buf, tnull, force_nl, fpout)
findwords(s,p)
firstone(buf,tofind)
char *fnnlgetl(s,fp,max)
char *fnnlgets(s,fp)
freq(buf,item)
char *ftgets(buf,fp,maxcount)
getint(string,ptr)
getwrds(message,wordbuf,pp)
goodfile(message,name)
icata(n,stout)
instr(i,s,t)
*/
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
equal(s1, s2)
/* Copyright (c) 1983 by Eric Martz, Amherst MA */
/* RETURNS YES IF THE STRINGS IN s1 AND s2 ARE EQUAL, ELSE NO */
char *s1, *s2;
{
if (strcmp(s1, s2) EQ 0) return(YES);
else return(NO);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
fbrkout(width, buf, tnull, force_nl, fpout, displayall, indent)
/* Copyright (c) 1983 by Eric Martz, Amherst MA */
/* PUTS INTO fpout CONTENTS OF buf, BROKEN AT SPACES, TABS, OR
NEWLINES INTO LINES OF LENGTH <= width.
---> WARNING!!! FBRKOUT() MODIFIES THE CONTENTS OF buf!!! <---
BREAK IS FORCED AT THE STRING force_nl (WHICH IS DELETED FROM
OUTPUT). IF YOU DO NOT WISH TO FORCE BREAKS, CALL WITH 0.
IF displayall IS YES, UNPRINTABLE CHARACTERS ARE EXPANDED TO
PRINTABLE FORM.
IF indent IS YES, THE 2ND-NTH LINES PUT OUT BY FBRKOUT ARE
INDENTED 5 SPACES.
IF TNULL (POINTER TO TERMINAL NULL OF buf) IS NOT AVAILABLE, CALL
WITH ZERO. TNULL IS NOT NECESSARY BUT IMPROVES EFFICIENCY(?). */
char *buf, *tnull, *force_nl;
int width, displayall, indent;
FILE *fpout;
{
char *brk, out[4];
int len, force, firstline, lesswidth;
if (force_nl EQ 0) force_nl = "";
force = ERROR;
if (tnull EQ 0) tnull = buf + strlen(buf);
if ((tnull - buf) EQ 0) { /* GUARANTEE NEWLINE FOR EMPTY BUFFER */
fputs("\n", fpout);
return(0);
}
firstline = YES;
while ((len = (tnull - buf)) > 0) {
/* INDENT IF NEEDED AND SET lesswidth */
if (indent AND !firstline) {
lesswidth = width - 5;
fputs(" ", fpout); /* 5 spaces */
}
else lesswidth = width;
if (len > lesswidth) { /* IF THE LINE IS TOO LONG */
/* WE'LL ASSUME THERE IS A SPACE BEFORE lesswidth! */
/* FIND A BREAKPOINT AT A SPACE */
brk = buf + rindex(buf, " ", lesswidth);
}
else brk = tnull;
/* LOOK FOR force_nl */
if (force_nl[0]) force = instr(0, buf, force_nl);
/* SET BREAK AT WHICHEVER OCCURS FIRST */
if (force NE ERROR AND (brk > buf + force)) brk = buf + force;
/* BREAK THE BUFFER */
*brk = NULL;
/* WRITE OUT THE LINE SEGMENT */
while (*buf) {
if (displayall) {
dispexp(*buf, out);
fputs(out, fpout);
}
else putc(*buf, fpout);
buf++;
}
fputs("\n", fpout); /* putc() does not expand \n */
/* SET THE POINTER PAST END OF STRING */
buf = brk + strlen(force_nl);
if(!*force_nl) buf++;
firstline = NO;
}
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
findwords(s,p)
/* Copyright (c) 1983 by Eric Martz, Amherst MA */
/* FINDS THE WHITESPACE-DELIMITED WORDS IN STRING "s" (WHERE WHITESPACE
INCLUDES SPACES, TABS AND NEWLINES). RETURNS COUNT OF WORDS FOUND. EACH WORD
IS POINTED TO BY A MEMBER OF THE ARRAY "p". THE STRING POINTED TO BY "s" IS
COMPRESSED INTO WORDS DELIMITED ONLY BY NULLS. THE FIRST WORD IS POINTED TO BY
p[1], NOT p[0]. (p[0] IS WASTED.) */
char *s, **p;
{
int wcnt, atend;
char *beginw, *endw, *nextat;
blankout(s,"\t\n");
atend = FALSE;
wcnt = 0;
beginw = nextat = s;
while(1){
for(;*beginw; beginw++)
if(*beginw != ' ') break;
if (!*beginw) return(wcnt);
for(endw=beginw; *endw; endw++)
if (*endw == ' ') break;
if (!*endw) atend = TRUE;
*endw = '\0';
wcnt += 1;
p[wcnt] = nextat;
if (nextat != beginw) strcpy(nextat,beginw);
if (atend) return(wcnt);
nextat += (endw + 1) - beginw;
beginw = endw + 1;
}
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
char *firstone(buf, tofind)
/* Copyright (c) 1983 by Eric Martz, Amherst MA */
/* RETURNS POINTER [NOT AN INDEX!] TO FIRST OCCURRENCE IN buf OF
A CHARACTER INCLUDED IN THE STRING tofind, OR NULL IF NOT FOUND.
*/
char *buf, *tofind;
{
char *p, *f;
for (p=buf; *p; p++) {
f = tofind;
while (*f)
if (*p == *(f++))
return (p);
}
return(0);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
char *fnnlgetl(s,fp,max)
/* Copyright (c) 1983 by Eric Martz, Amherst MA */
/* FNNLGET = File-No-NewLine-GET-Longstring
SAME AS FTGETS BUT STRING LEFT IN S IS STRIPPED OF THE TERMINAL NEWLINE
AND BUFFER LENGTH IS AN ARGUMENT FOR LONG INPUT LINES */
char *s;
FILE *fp;
{
char *tnull;
if (!(tnull = ftgets(s,fp,max))) return(0);
if (*(--tnull) EQ '\n') *tnull = NULL;
return (tnull);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
char *fnnlgets(s,fp)
/* Copyright (c) 1983 by Eric Martz, Amherst MA */
/* FNNLGETS = File-No-NewLine-GET-String
SAME AS FTGETS BUT STRING LEFT IN S IS STRIPPED OF THE TERMINAL NEWLINE */
char *s;
FILE *fp;
{
char *tnull;
if (!(tnull = ftgets(s,fp,MAXLINE))) return(0);
if (*(--tnull) EQ '\n') *tnull = NULL;
return (tnull);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
freq(buf, item)
/* Copyright (c) 1983 by Eric Martz, Amherst MA */
/* RETURNS THE FREQUENCY OF item IN buf. FREQ IS FOR
NONOVERLAPPING OCCURRENCES. THUS IF buf CONTAINS "...." AND item
CONTAINS "..", FREQ IS 2 NOT 3. */
char *buf, *item;
{
int i, count, start, itemlen;
count = start = 0;
itemlen = strlen(item);
while ((i=instr(start,buf,item)) NE ERROR) {
count++;
start = i + itemlen;
}
return(count);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
char *ftgets(buf,fp,maxcount)
/* Copyright (c) 1983 by Eric Martz, Amherst MA */
/* This is BDSC fgets (from stdlib2.c) with the following modifications:
Returns pointer to terminal null in buf;
The buffer length restriction (formerly MAXLINE) has been added as
an argument.
ftgets() retains inclusion of newline in buf (like fgets).
*/
char *buf;
FILE *fp;
int maxcount;
{
int c;
char *cptr;
cptr = buf;
if ( (c = rawgetc(fp)) == CPMEOF || c == EOF) return NULL;
do {
if ((*cptr++ = c) == '\n') {
if (cptr