ack/lang/basic/lib/read.c

189 lines
2.9 KiB
C
Raw Normal View History

#include "bc_string.h"
#include "bc_io.h"
1984-11-29 14:22:02 +00:00
#include <ctype.h>
1994-06-24 11:31:16 +00:00
/* $Id$ */
1984-11-29 14:22:02 +00:00
2016-12-12 20:15:25 +00:00
void _readln(void)
1984-11-29 14:22:02 +00:00
{
register int c;
while ((c = fgetc(_chanrd)) != EOF && c != '\n')
1984-11-29 14:22:02 +00:00
;
}
2016-12-12 20:15:25 +00:00
void readskip(void)
1984-11-29 14:22:02 +00:00
{
register int c;
1984-11-29 14:22:02 +00:00
#ifdef DEBUG
printf("readskip\n");
#endif
while ((c = fgetc(_chanrd)) != EOF && c != ',' && c != '\n')
1984-11-29 14:22:02 +00:00
;
}
2016-12-12 20:15:25 +00:00
void _readint(int* addr)
1984-11-29 14:22:02 +00:00
{
int i;
char buf[1024];
1984-11-29 14:22:02 +00:00
#ifdef DEBUG
printf("read int from %d\n", _chann);
1984-11-29 14:22:02 +00:00
#endif
_asschn();
if (fscanf(_chanrd, "%d", &i) != 1)
1984-11-29 14:22:02 +00:00
{
if (ferror(_chanrd))
error(29);
if (feof(_chanrd))
error(2);
if (_chann == -1)
1984-11-29 14:22:02 +00:00
{
_asschn(); /* may be closed by now */
fgets(buf, 1024, _chanrd);
1984-11-29 14:22:02 +00:00
printf("?Redo ");
_readint(addr);
return;
}
error(40);
}
else
{
readskip();
*addr = i;
}
1984-11-29 14:22:02 +00:00
}
2016-12-12 20:15:25 +00:00
void _readflt(double* addr)
1984-11-29 14:22:02 +00:00
{
double f;
char buf[1024];
#ifdef DEBUG
printf("read flt from %d\n", _chann);
1984-11-29 14:22:02 +00:00
#endif
_asschn();
if (fscanf(_chanrd, "%lf", &f) != 1)
1984-11-29 14:22:02 +00:00
{
if (ferror(_chanrd))
error(29);
if (feof(_chanrd))
error(2);
if (_chann == -1)
1984-11-29 14:22:02 +00:00
{
fgets(buf, 1024, _chanrd);
1984-11-29 14:22:02 +00:00
printf("?Redo ");
_readflt(addr);
return;
}
error(40);
}
else
{
readskip();
*addr = f;
}
1984-11-29 14:22:02 +00:00
}
2016-12-12 20:15:25 +00:00
void _readstr(String** s)
1984-11-29 14:22:02 +00:00
{
char buffer[1024];
register int kar;
char* c;
1984-11-29 14:22:02 +00:00
#ifdef DEBUG
printf("read str from %d\n", _chann);
1984-11-29 14:22:02 +00:00
#endif
_asschn();
c = buffer;
kar = fgetc(_chanrd);
while (isspace(kar) && kar != EOF)
kar = fgetc(_chanrd);
*c = kar;
if (kar == '"')
1984-11-29 14:22:02 +00:00
{
/* read quoted string */
1984-11-29 14:22:02 +00:00
#ifdef DEBUG
printf("qouted string\n");
#endif
while ((kar = fgetc(_chanrd)) != EOF && kar != '"')
*c++ = kar;
ungetc(kar, _chanrd);
*c = 0;
}
else if (isalpha(*c))
1984-11-29 14:22:02 +00:00
{
/* read normal string */
c++;
#ifdef DEBUG
printf("non-qouted string\n");
#endif
while ((kar = fgetc(_chanrd)) != ',' && kar != EOF && !isspace(kar) && kar != '\n')
*c++ = kar;
ungetc(kar, _chanrd);
*c = 0;
}
else
{
if (ferror(_chanrd))
error(29);
if (feof(_chanrd))
error(2);
if (_chann == -1)
1984-11-29 14:22:02 +00:00
{
fgets(buffer, 1024, _chanrd);
1984-11-29 14:22:02 +00:00
printf("?Redo ");
_rdline(s);
return;
}
error(40);
}
#ifdef DEBUG
printf("string read: %s\n", buffer);
1984-11-29 14:22:02 +00:00
#endif
readskip();
/* save value read */
_decstr(*s);
*s = (String*)_newstr(buffer);
1984-11-29 14:22:02 +00:00
}
extern int _seektab[];
1984-11-29 14:22:02 +00:00
2016-12-12 20:15:25 +00:00
void _restore(int line)
1984-11-29 14:22:02 +00:00
{
int nr;
char buffer[1024];
#ifdef DEBUG
printf("seek to %d", line);
1984-11-29 14:22:02 +00:00
#endif
fseek(_chanrd, 0l, 0);
if (line)
1984-11-29 14:22:02 +00:00
{
/* search number of lines to skip */
for (nr = 0; _seektab[nr] && _seektab[nr] < line; nr += 2)
1984-11-29 14:22:02 +00:00
#ifdef DEBUG
printf("test %d %d\n", _seektab[nr], _seektab[nr + 1]);
1984-11-29 14:22:02 +00:00
#endif
;
nr /= 2;
#ifdef DEBUG
printf(" %d lines to skip\n", nr);
1984-11-29 14:22:02 +00:00
#endif
while (nr-- > 0)
fgets(buffer, 1024, _chanrd);
1984-11-29 14:22:02 +00:00
}
}
2016-12-12 20:15:25 +00:00
void _rdline(String** s)
1984-11-29 14:22:02 +00:00
{
char buffer[1024];
if (fgets(buffer, 1024, _chanrd) == 0)
1984-11-29 14:22:02 +00:00
{
if (_chann == -1)
1984-11-29 14:22:02 +00:00
{
printf("?Redo ");
_rdline(s);
return;
}
error(40);
}
_decstr(*s);
*s = (String*)_newstr(buffer);
1984-11-29 14:22:02 +00:00
}