|
|
1.1 ! root 1: #include "stream.h" ! 2: ! 3: /* define some UNIX calls */ ! 4: extern int open (char *, int); ! 5: extern int close (int); ! 6: extern long lseek (int, long, int); ! 7: extern int read (int, char *, unsigned); ! 8: extern int write (int, char *, unsigned); ! 9: extern int creat (char *, int); ! 10: ! 11: /* ! 12: * Open a file with the given mode. ! 13: * Return: NULL if failure ! 14: * this if success ! 15: */ ! 16: filebuf* filebuf::open (char *name, open_mode om) ! 17: { ! 18: switch (om) { ! 19: case input: ! 20: fd = ::open(name, 0); ! 21: break; ! 22: case output: ! 23: fd = creat(name, 0664); ! 24: break; ! 25: case append: ! 26: fd = ::open(name, 1); ! 27: if (fd < 0) fd = creat(name, 0664); ! 28: if (fd >= 0) (void)lseek(fd, 0, 2); ! 29: break; ! 30: } ! 31: ! 32: if (fd < 0) return NULL; ! 33: ! 34: opened = 1; ! 35: return this; ! 36: } ! 37: ! 38: /* ! 39: * Empty an output buffer. ! 40: * Returns: EOF on error ! 41: * 0 on success ! 42: */ ! 43: int filebuf::overflow(int c) ! 44: { ! 45: if (!opened || allocate()==EOF) return EOF; ! 46: ! 47: if (fp != NULL) { // stdio compatibility ! 48: fflush(fp); ! 49: return 0; ! 50: } ! 51: else if (base == eptr) { // unbuffered IO ! 52: if (c != EOF) { ! 53: *pptr = c; ! 54: if (write(fd, pptr, 1) != 1) return EOF; ! 55: } ! 56: } ! 57: else { // buffered IO ! 58: if (pptr > base) ! 59: if (write(fd, base, pptr-base) != pptr-base) return EOF; ! 60: pptr = gptr = base; ! 61: if (c != EOF) *pptr++ = c; ! 62: } ! 63: return c & 0377; ! 64: } ! 65: ! 66: /* ! 67: * Fill an input buffer. ! 68: * Returns: EOF on error or end of input ! 69: * next character on success ! 70: */ ! 71: int filebuf::underflow() ! 72: { ! 73: int count; ! 74: extern int strlen(char *); ! 75: ! 76: if (!opened || allocate()==EOF) return EOF; ! 77: ! 78: if (fp!=NULL) { // stdio compatibility ! 79: if (fgets(base+1, eptr-base-1, fp) == NULL) return EOF; ! 80: count = strlen(base+1); ! 81: } ! 82: else { // normal stream io ! 83: if ((count=read(fd, base+1, eptr-base-1)) < 1) return EOF; ! 84: } ! 85: gptr = base+1; // leave room for putback ! 86: pptr = gptr+count; ! 87: return *gptr & 0377; ! 88: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.