|
|
1.1 ! root 1: /* This is part of libio/iostream, providing -*- C++ -*- input/output. ! 2: Copyright (C) 1993 Free Software Foundation ! 3: ! 4: This file is part of the GNU IO Library. This library is free ! 5: software; you can redistribute it and/or modify it under the ! 6: terms of the GNU General Public License as published by the ! 7: Free Software Foundation; either version 2, or (at your option) ! 8: any later version. ! 9: ! 10: This library is distributed in the hope that it will be useful, ! 11: but WITHOUT ANY WARRANTY; without even the implied warranty of ! 12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ! 13: GNU General Public License for more details. ! 14: ! 15: You should have received a copy of the GNU General Public License ! 16: along with GNU CC; see the file COPYING. If not, write to ! 17: the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. ! 18: ! 19: As a special exception, if you link this library with files ! 20: compiled with a GNU compiler to produce an executable, this does not cause ! 21: the resulting executable to be covered by the GNU General Public License. ! 22: This exception does not however invalidate any other reasons why ! 23: the executable file might be covered by the GNU General Public License. ! 24: ! 25: Written by Per Bothner ([email protected]). */ ! 26: ! 27: #include "iostreamP.h" ! 28: #include <sys/types.h> ! 29: #include <sys/stat.h> ! 30: #include <fcntl.h> ! 31: #include <errno.h> ! 32: #include "builtinbuf.h" ! 33: ! 34: #define CLOSED_FILEBUF_FLAGS \ ! 35: (_IO_IS_FILEBUF+_IO_NO_READS+_IO_NO_WRITES+_IO_TIED_PUT_GET) ! 36: ! 37: void filebuf::init() ! 38: { ! 39: _IO_file_init(this); ! 40: } ! 41: ! 42: filebuf::filebuf() : streambuf(CLOSED_FILEBUF_FLAGS) ! 43: { ! 44: _IO_file_init(this); ! 45: } ! 46: ! 47: /* This is like "new filebuf()", but it uses the _IO_file_jump jumptable, ! 48: for eficiency. */ ! 49: ! 50: filebuf* filebuf::__new() ! 51: { ! 52: filebuf *fb = new filebuf; ! 53: fb->_jumps = &_IO_file_jumps; ! 54: fb->_vtable() = NULL; ! 55: return fb; ! 56: } ! 57: ! 58: filebuf::filebuf(int fd) : streambuf(CLOSED_FILEBUF_FLAGS) ! 59: { ! 60: _IO_file_init(this); ! 61: _IO_file_attach(this, fd); ! 62: } ! 63: ! 64: filebuf::filebuf(int fd, char* p, int len) : streambuf(CLOSED_FILEBUF_FLAGS) ! 65: { ! 66: _IO_file_init(this); ! 67: _IO_file_attach(this, fd); ! 68: setbuf(p, len); ! 69: } ! 70: ! 71: filebuf::~filebuf() ! 72: { ! 73: if (!(xflags() & _IO_DELETE_DONT_CLOSE)) ! 74: close(); ! 75: ! 76: _un_link(); ! 77: } ! 78: ! 79: filebuf* filebuf::open(const char *filename, ios::openmode mode, int prot) ! 80: { ! 81: if (_IO_file_is_open (this)) ! 82: return NULL; ! 83: int posix_mode; ! 84: int read_write; ! 85: if (mode & ios::app) ! 86: mode |= ios::out; ! 87: if ((mode & (ios::in|ios::out)) == (ios::in|ios::out)) { ! 88: posix_mode = O_RDWR; ! 89: read_write = 0; ! 90: } ! 91: else if (mode & ios::out) ! 92: posix_mode = O_WRONLY, read_write = _IO_NO_READS; ! 93: else if (mode & (int)ios::in) ! 94: posix_mode = O_RDONLY, read_write = _IO_NO_WRITES; ! 95: else ! 96: posix_mode = 0, read_write = _IO_NO_READS+_IO_NO_WRITES; ! 97: if ((mode & (int)ios::trunc) || mode == (int)ios::out) ! 98: posix_mode |= O_TRUNC; ! 99: if (mode & ios::app) ! 100: posix_mode |= O_APPEND, read_write |= _IO_IS_APPENDING; ! 101: if (!(mode & (int)ios::nocreate) && mode != ios::in) ! 102: posix_mode |= O_CREAT; ! 103: if (mode & (int)ios::noreplace) ! 104: posix_mode |= O_EXCL; ! 105: int fd = ::open(filename, posix_mode, prot); ! 106: if (fd < 0) ! 107: return NULL; ! 108: _fileno = fd; ! 109: xsetflags(read_write, _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING); ! 110: if (mode & (ios::ate|ios::app)) { ! 111: if (sseekoff(0, ios::end) == EOF) ! 112: return NULL; ! 113: } ! 114: _IO_link_in(this); ! 115: return this; ! 116: } ! 117: ! 118: filebuf* filebuf::open(const char *filename, const char *mode) ! 119: { ! 120: return (filebuf*)_IO_file_fopen(this, filename, mode); ! 121: } ! 122: ! 123: filebuf* filebuf::attach(int fd) ! 124: { ! 125: return (filebuf*)_IO_file_attach(this, fd); ! 126: } ! 127: ! 128: streambuf* filebuf::setbuf(char* p, int len) ! 129: { ! 130: return (streambuf*)_IO_file_setbuf(this, p, len); ! 131: } ! 132: ! 133: int filebuf::doallocate() { return _IO_file_doallocate(this); } ! 134: ! 135: int filebuf::overflow(int c) ! 136: { ! 137: return _IO_file_overflow(this, c); ! 138: } ! 139: ! 140: int filebuf::underflow() ! 141: { ! 142: return _IO_file_underflow(this); ! 143: } ! 144: ! 145: int filebuf::do_write(const char *data, int to_do) ! 146: { ! 147: return _IO_do_write(this, data, to_do); ! 148: } ! 149: ! 150: int filebuf::sync() ! 151: { ! 152: return _IO_file_sync(this); ! 153: } ! 154: ! 155: streampos filebuf::seekoff(streamoff offset, _seek_dir dir, int mode) ! 156: { ! 157: return _IO_file_seekoff (this, offset, convert_to_seekflags(dir, mode)); ! 158: } ! 159: ! 160: filebuf* filebuf::close() ! 161: { ! 162: return (_IO_file_close_it(this) ? NULL : this); ! 163: } ! 164: ! 165: streamsize filebuf::sys_read(char* buf, streamsize size) ! 166: { ! 167: return _IO_file_read(this, buf, size); ! 168: } ! 169: ! 170: streampos filebuf::sys_seek(streamoff offset, _seek_dir dir) ! 171: { ! 172: return _IO_file_seek(this, offset, dir); ! 173: } ! 174: ! 175: streamsize filebuf::sys_write(const char *buf, streamsize n) ! 176: { ! 177: return _IO_file_write (this, buf, n); ! 178: } ! 179: ! 180: int filebuf::sys_stat(void* st) ! 181: { ! 182: return _IO_file_stat (this, st); ! 183: } ! 184: ! 185: int filebuf::sys_close() ! 186: { ! 187: return _IO_file_close (this); ! 188: } ! 189: ! 190: streamsize filebuf::xsputn(const char *s, streamsize n) ! 191: { ! 192: return _IO_file_xsputn(this, s, n); ! 193: } ! 194: ! 195: streamsize filebuf::xsgetn(char *s, streamsize n) ! 196: { ! 197: // FIXME: OPTIMIZE THIS (specifically, when unbuffered()). ! 198: return streambuf::xsgetn(s, n); ! 199: } ! 200: ! 201: // Non-ANSI AT&T-ism: Default open protection. ! 202: const int filebuf::openprot = 0644;
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.