📄 io.c
字号:
/* ************************************************** */
/* file io.c: contains most input/output functions */
/* */
/* Copyright (c) 1990-96 by Donald R. Tveter */
/* */
/* ************************************************** */
#ifdef INTEGER
#include "ibp.h"
#else
#include "rbp.h"
#endif
#if defined(UNIX) && defined(HOTKEYS)
#ifdef BSD
struct termios raw; /* characteristics for hotkeys */
struct termios noraw; /* original characteristics */
#elif defined NEXT
struct sgttyb raw; /* characteristics for hotkeys */
struct sgttyb noraw; /* original characteristics */
#else /* the System V solution */
struct termio raw; /* characteristics for hotkeys */
struct termio noraw; /* original characteristics */
#endif
char onechar[1]; /* the buffer for the hotkey character */
#endif
extern UNIT *locateunit();
extern WTTYPE userscaleup();
extern char buffer[], outstr[], *inputfile, sysstr[];
extern char copyflag,deriv,*datafile,echo,informat;
extern char outformat, probtype, update, *wtfile, wtformat, zeroderiv;
extern char offby1, ringbell, up_to_date_stats, summary, biasset;
extern char wtlimithit, runningflag, seednote;
extern char saveonminimum, wtfilename[], trfiles[];
extern char recurinit;
extern int bufferend, bufferptr, filestackptr, format[];
extern int filetimes[], wtfilecount, timestoread, maxiter;
extern FILE *data, *filestack[];
extern LAYER *last, *start;
extern int extraconnect,ioconnects,lastsave,pagesize,pccnet,prevnpats;
extern int readerror, readingpattern, totaliter;
extern int printrate, saverate;
extern int lineno, stmsize;
extern long iotime;
extern short nlayers;
extern WTTYPE unknown, toler, toosmall, stdthresh;
extern WTTYPE initrange, kickrange, kicksize;
extern WTTYPE alpha, eta, eta2;
extern WTTYPE decay, dbdeta, kappa, etamax, theta1;
extern WTTYPE qpeta, qpnoise, mu;
extern WTTYPE classoff, classon;
extern WTTYPE hbiasact, obiasact;
extern char qpslope;
extern FILE *copy;
extern REAL qpdecayh, qpdecayo;
extern REAL toloverall, minimumsofar;
extern DATA s[2][2];
extern WTTYPE Dh, Do;
extern char ah, ao;
extern int wtsinuse, wttotal;
extern char incrementseed;
extern unsigned seed;
extern SEEDNODE *seedstart;
#if defined(UNIX) && defined(HOTKEYS)
#ifdef BSD
void initraw()
{
int i;
tcgetattr(0,&noraw);
raw.c_iflag = noraw.c_iflag;
raw.c_oflag = noraw.c_oflag;
raw.c_cflag = noraw.c_cflag;
raw.c_lflag = noraw.c_lflag & ~ ECHO;
raw.c_lflag = raw.c_lflag & ~ ICANON;
for (i=0;i<NCCS;i++) raw.c_cc[i] = noraw.c_cc[i];
raw.c_cc[VMIN] = 1;
raw.c_cc[VTIME] = 0;
raw.c_ispeed = noraw.c_ispeed;
raw.c_ospeed = noraw.c_ospeed;
}
void setraw()
{
tcsetattr(0,TCSANOW,&raw);
}
void setnoraw()
{
tcsetattr(0,TCSANOW,&noraw);
}
#elif defined NEXT
void initraw()
{
ioctl(0,TIOCGETD,&noraw);
raw.sg_ispeed = noraw.sg_ispeed;
raw.sg_ospeed = noraw.sg_ospeed;
raw.sg_erase = noraw.sg_erase;
raw.sg_kill = noraw.sg_kill;
raw.sg_flags = noraw.sg_flags & 0000040; /* turn on raw mode */
}
void setraw()
{
ioctl(0,TIOCSETD,&raw);
}
void setnoraw()
{
ioctl(0,TIOCSETD,&noraw);
}
#else /* System V code */
void initraw()
{
int i;
ioctl(0,TCGETA,&noraw);
raw.c_iflag = noraw.c_iflag;
raw.c_oflag = noraw.c_oflag;
raw.c_cflag = noraw.c_cflag;
raw.c_lflag = noraw.c_lflag & ~ 0x02; /* hot key bit */
raw.c_lflag = raw.c_lflag & ~ 010; /* echo bit */
raw.c_line = noraw.c_line;
for (i=0;i<=7;i++) raw.c_cc[i] = noraw.c_cc[i];
raw.c_cc[4] = 1;
}
void setraw()
{
ioctl(0,TCSETA,&raw);
}
void setnoraw()
{
ioctl(0,TCSETA,&noraw);
}
#endif /* end of the System V code */
int getch()
{
int ch;
setraw();
do ch = read(0,onechar,1); while (ch != 1);
setnoraw();
return(onechar[0]);
}
#endif /* end of the if defined(UNIX) ... code */
#ifdef INTEGER
short scale(x) /* returns x as a scaled 16-bit value */
REAL x;
{
short s;
if (x > 31.999 || x < -32.0)
{
sprintf(outstr,"magnitude of %f is too large for the integer",x);
pg(stdout,outstr);
pg(stdout," representation\n");
readerror = 1;
if (x > 31.999) return(MAXSHORT); else return(MINSHORT);
};
if (x > 0.0) s = x * 1024 + 0.5;
else s = x * 1024 - 0.5;
if (x != 0.0 && s == 0)
{
sprintf(outstr,"warning: magnitude of %f is too small for",x);
pg(stdout,outstr);
pg(stdout," the integer representation\n");
return(0);
};
return(s);
}
REAL unscale(x) /* returns the REAL value of short x */
short x;
{return((REAL) x / 1024.0);}
REAL unscaleint(x) /* returns the REAL value of INT32 x */
INT32 x;
{ return((REAL) x / 1024.0); }
#endif
int pushfile(filename,timestoread)
char *filename;
int timestoread;
{
FILE *file;
bufferptr = 0;
bufferend = 0;
buffer[0] = '\n';
file = fopen(filename,"r");
if (file == NULL)
{
sprintf(outstr,"cannot open: %s\n",filename); pg(stdout,outstr);
return(0);
};
filestackptr = filestackptr + 1;
if (filestackptr > MAXFILES)
{
pg(stdout,"can't stack up any more files\n");
filestackptr = filestackptr - 1;
fclose(file);
return(0);
};
filestack[filestackptr] = file;
filetimes[filestackptr] = timestoread;
data = file;
return(1);
}
void popfile()
{
bufferptr = 0;
bufferend = 0;
buffer[0] = '\n';
if (s[TOL][TRAIN].wrong == 0) /* break the "loop" */
filetimes[filestackptr] = 1;
filetimes[filestackptr] = filetimes[filestackptr] - 1;
if (filetimes[filestackptr] > 0) rewind(data);
else if (filestackptr > 0)
{
fclose(data);
filestackptr = filestackptr - 1;
}
else pg(stdout,"\nunexpected EOF: to quit the program, type q\n");
data = filestack[filestackptr];
}
int readch() /* returns the next character in the input buffer */
{
int i, ch2;
if (bufferptr > bufferend) /* then read next line into buffer */
{
#ifdef HOTKEYS
if (data == stdin)
{
ch2 = getch();
if (ch2 <= 26 || (ch2 >= 63 && ch2 <= 90)) /* HOTKEY characters */
{
if (ch2 == 2) buffer[0] = 'b';
else if (ch2 == 9) buffer[0] = 'i';
else if (ch2 == 18) buffer[0] = 'r';
else buffer[0] = ch2;
buffer[1] = '\n';
buffer[2] = '\0';
i = 1;
printf("%s",buffer);
goto entryfromhotkeys;
}
else /* all other characters */
{
putchar(ch2);
i = 0;
buffer[i] = ch2;
#ifdef UNIX
while (ch2 != '\n' && i < (BUFFSIZE-1))
#else
while (ch2 != 13 && i < (BUFFSIZE-1))
#endif
{
ch2 = getch();
if (ch2 == 8 && i >= 0)
{
i = i - 1;
putchar(8);
}
else if (ch2 == 8) {/* do nothing */}
#ifdef UNIX
else if (ch2 != '\n')
#else
else if (ch2 != 13)
#endif
{
putchar(ch2);
i = i + 1;
buffer[i] = ch2;
};
};
putchar('\n');
i = i + 1;
goto entryfromhotkeys;
};
}
else
#endif
ch2 = getc(data);
if (ch2 == EOF) return(ch2);
i = 0;
while(ch2 != '\n' && i < (BUFFSIZE-1))
{
#ifdef UNIX
if (ch2 == 13) ch2 = ' '; /* turn a ctrl-M into a blank */
#endif
if (ch2 == '\t') ch2 = ' ';
buffer[i] = ch2;
i = i + 1;
ch2 = getc(data);
};
entryfromhotkeys:
if (i == (BUFFSIZE-1)) pg(stdout,"line too long\n");
buffer[i] = '\n';
buffer[i+1] = '\0';
bufferend = i;
bufferptr = 0;
if (echo == '+') for(i = 0; i <= bufferend; i++) putchar(buffer[i]);
if (copy && ((data == stdin) || (echo == '+')))
for (i=0;i<=bufferend;i++) putc(buffer[i],copy);
}
ch2 = buffer[bufferptr];
bufferptr = bufferptr + 1;
return(ch2);
}
void texterror()
{
pg(stdout,"unexpected text: ");
pg(stdout,&buffer[bufferptr-1]);
bufferptr = bufferend;
}
char *readstr()
{
short i,start,end;
char *addr, *addr2;
i = bufferptr;
while (buffer[i] == ' ') i = i + 1;
start = i;
while (buffer[i] != ' ' && buffer[i] != '\n') i = i + 1;
end = i-1;
addr = (char *) malloc((int) end-start+2);
addr2 = addr;
for (i=start;i<=end;i++) *addr++ = buffer[i];
bufferptr = end + 1;
*addr = '\0';
return(addr2);
}
int scanfordigit()
{
int sign, ch2;
sign = 1;
restart:
do ch2 = readch(); while (ch2 == ' ' || ch2 == '\n');
if ((ch2 >= '0' && ch2 <= '9') || ch2 == '.')
{
bufferptr = bufferptr - 1;
return(sign);
};
switch (ch2) {
case '}': readerror = 3;
return(0);
case EOF: readerror = 2;
return(0);
case '*': while (ch2 != '\n') ch2 = readch();
goto restart;
case '-': sign = -sign;
goto restart;
case 'h':
case 'x':
case 'X':
case 'H':
case 'd':
case 's': bufferptr = bufferptr - 1;
return(0);
default: readerror = 1;
return(0);
};
}
int readint(min,max,command)
int min, max;
char command;
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -