home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Garbo
/
Garbo.cdr
/
pc
/
source
/
remind23.lzh
/
remind.3
/
files.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-02-24
|
8KB
|
310 lines
#include <stdio.h>
#ifndef UNIX
#include <stdlib.h>
#endif
#include <string.h>
#ifndef NO_MALLOC_H
#include <malloc.h>
#endif
#ifndef UNIX
#include <dos.h>
#endif
#include <fcntl.h>
#ifdef UNIX
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#endif
#include "defines.h"
#include "globals.h"
#include "protos.h"
#ifdef __STDC__
static int PopFile(void);
#else
static int PopFile();
#endif
/***************************************************************/
/* */
/* FILES.C */
/* */
/* All the routines for opening initial file, getting */
/* and settting initial file's date, closing files, */
/* handling INCLUDE commands, etc. */
/* */
/***************************************************************/
/* Define the structure for saving info about a file */
typedef struct {
long offset;
int curline;
char *name;
} FileSave;
#define MAXINCLUDE 10
/* Set up array of MAXINCLUDE file save areas */
static FileSave stack[MAXINCLUDE];
static int SP;
static FILE *fp;
/***************************************************************/
/* */
/* OpenFile */
/* */
/* Open the named file, initialize stack, get file date. */
/* If there's a problem, print an error msg and die. */
/* */
/***************************************************************/
#ifdef __STDC__
void OpenFile(char *s)
#else
void OpenFile(s)
char *s;
#endif
{
unsigned date, time;
#ifndef UNIX
unsigned handle;
#endif
int d, m, y;
#ifndef UNIX
/* Get the file's modification date */
if(_dos_open(s, O_RDONLY, &handle)) {
fprintf(stderr, "remind: Can't open %s.\n", s);
exit(1);
#else
struct stat t;
struct tm *t1;
/* Get the file's access date */
if (stat(s, &t)) {
fprintf(stderr, "remind: Can't find file %s.\n", s);
exit(1);
#endif
}
#ifndef UNIX
_dos_getftime(handle, &date, &time);
d = date & 0x1F;
m = (date >> 5) & 0xF;
y = (date >> 9) + 1980;
#else
t1 = localtime(&(t.st_atime));
#endif
#ifndef UNIX
if (y < BASE) LastRun = 0; else LastRun = Julian(d, m-1, y);
_dos_close(handle);
#else
y = t1->tm_year + 1900;
m = t1->tm_mon;
d = t1->tm_mday;
if (y < BASE) LastRun = 0; else LastRun = Julian(d, m, y);
#endif
fp = fopen(s, "r");
if (fp == NULL) {
fprintf(stderr, "remind: Can't open %s.\n", s);
exit(1);
}
CurLine = 0;
strcpy(FileName, s);
SP = 0;
return;
}
/***************************************************************/
/* */
/* DoInclude */
/* */
/* Push the state of the current file and open a new file. */
/* */
/***************************************************************/
#ifdef __STDC__
void DoInclude(char **s)
#else
void DoInclude(s)
char **s;
#endif
{
Token tok;
tok = ParseToken(s);
/* First, check if there's room on the stack */
if (SP == MAXINCLUDE) {
Eprint("Too many levels of INCLUDE\n");
return;
}
/* Save current data */
#ifndef UNIX
stack[SP].offset = ftell(fp) - 1L;
#else
stack[SP].offset = ftell(fp);
#endif
stack[SP].curline = CurLine;
stack[SP].name = (char *) malloc(strlen(FileName)+1);
if (stack[SP].name == NULL) {
Eprint("Out of memory for INCLUDE\n");
return;
}
strcpy(stack[SP].name, FileName);
SP++;
/* Close the current file */
fclose(fp);
/* Open the new file */
fp = fopen(tok.str, "r");
if (fp == NULL) {
Eprint("Can't open %s for INCLUDE\n", tok.str);
PopFile();
return;
}
if (Debug || Purge) {
Eprint("INCLUDING file %s\n", tok.str);
}
/* Set the global variables */
CurLine = 0;
strcpy(FileName, tok.str);
return;
}
/***************************************************************/
/* */
/* PopFile */
/* */
/* Pop to the previous file, if there is one. Return 0 for */
/* OK, non-zero for no more files. If we can't pop back */
/* to a file, print an error message and die. */
/* */
/***************************************************************/
#ifdef __STDC__
static int PopFile(void)
#else
static int PopFile()
#endif
{
#ifndef UNIX
unsigned handle, date, time;
struct dostime_t t;
#endif
if (fp) fclose(fp);
#ifndef UNIX
if (!SP) {
if (!Debug && !Purge && (JulianToday == RealToday)) {
if (_dos_open(FileName, O_RDONLY, &handle)) {
fprintf(stderr, "Could not reset date of %s\n", FileName);
return 1;
}
_dos_gettime(&t);
date = CurDay;
date |= (CurMon + 1) << 5;
date |= (CurYear - 1980) << 9;
time = t.second / 2;
time |= t.minute << 5;
time |= t.hour << 11;
_dos_setftime(handle, date, time);
}
return 1;
}
#else
if (!SP) return -1;
#endif
SP--;
fp = fopen(stack[SP].name, "r");
if (fp == NULL) {
Eprint("Argh! Can't return to %s from INCLUDE file %s\n", stack[SP].name, FileName);
exit(1);
}
#ifndef UNIX
if (fseek(fp, stack[SP].offset, SEEK_SET)) {
#else
if (fseek(fp, stack[SP].offset, 0)) {
#endif
Eprint("Argh! Can't fseek %s after returning from INCLUDE file %s\n", stack[SP].name, FileName);
exit(1);
}
if (Debug || Purge) {
Eprint("Returning to file %s\n", stack[SP].name);
}
CurLine = stack[SP].curline;
strcpy(FileName, stack[SP].name);
free(stack[SP].name);
return 0;
}
/***************************************************************/
/* */
/* ReadLine */
/* */
/* Reads a line from the file. If EOF, pops to previous file */
/* if there was one. Returns 0 if more input, non-zero */
/* if no more input. Updates CurLine. */
/* */
/***************************************************************/
int ReadLine()
{
int done = 0;
int len;
Fresh = 1;
while (!done) {
CurLine++;
if (fgets(Line, 512, fp) == NULL) {
if (ferror(fp)) Eprint("Error reading %s\n", FileName);
if (PopFile()) return 1;
} else {
len = strlen(Line);
/* Remove the newline */
if (*Line && (*(Line + len-1)=='\n')) {
*(Line + strlen(Line)-1) = 0;
len--;
}
done = 1;
while(*Line && (*(Line + len-1) == '\\') && len<512) {
*(Line + len-1) = '\n';
if (fgets(Line+len, 512-len,fp) == NULL) {
*(Line + len) = 0;
break;
}
CurLine++;
len = strlen(Line);
/* Remove the newline */
if (*Line && (*(Line + len-1)=='\n')) {
*(Line + strlen(Line)-1) = 0;
len--;
}
}
}
}
return 0;
}
/***************************************************************/
/* */
/* TopLevel - Returns 1 if current file is top level, 0 */
/* if it is INCLUDEd. */
/* */
/***************************************************************/
#ifdef __STDC__
int TopLevel(void) { return (SP == 0); }
#else
int TopLevel()
{
return (SP == 0);
}
#endif