Merge from vendor branch CVS:
[dragonfly.git] / contrib / libio / fstream.cc
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 this library; see the file COPYING.  If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 (bothner@cygnus.com). */
26
27 #ifdef __GNUG__
28 #pragma implementation
29 #endif
30 #define _STREAM_COMPAT
31 extern "C" {
32 #include "libioP.h"
33 }
34 #include <fstream.h>
35
36 inline void
37 fstreambase::__fb_init()
38 {
39 #ifdef _IO_NEW_STREAMS
40 #if !_IO_UNIFIED_JUMPTABLES
41   /* Uses the _IO_file_jump jumptable, for eficiency. */
42   __my_fb._jumps = &_IO_file_jumps;
43   __my_fb._vtable() = builtinbuf_vtable;
44 #endif
45   init (&__my_fb);
46 #else
47   init(filebuf::__new());
48   _flags &= ~ios::dont_close;
49 #endif
50 }
51
52 fstreambase::fstreambase()
53 {
54   __fb_init ();
55 }
56
57 fstreambase::fstreambase(int fd)
58 {
59   __fb_init ();
60   _IO_file_attach(rdbuf(), fd);
61 }
62
63 fstreambase::fstreambase(const char *name, int mode, int prot)
64 {
65   __fb_init ();
66   if (!rdbuf()->open(name, mode, prot))
67     set(ios::badbit);
68 }
69
70 fstreambase::fstreambase(int fd, char *p, int l)
71 {
72 #ifdef _IO_NEW_STREAMS
73   __fb_init ();
74 #else
75   init(filebuf::__new());
76 #endif
77   _IO_file_attach(rdbuf(), fd);
78   _IO_file_setbuf(rdbuf(), p, l);
79 }
80
81 void fstreambase::open(const char *name, int mode, int prot)
82 {
83     clear();
84     if (!rdbuf()->open(name, mode, prot))
85         set(ios::badbit);
86 }
87
88 void fstreambase::close()
89 {
90     if (!rdbuf()->close())
91         set(ios::failbit);
92 }
93
94 void fstreambase::attach(int fd)
95 {
96   if (!rdbuf()->attach(fd))
97     set(ios::failbit);
98 }
99
100 #if 0
101 static int mode_to_sys(enum open_mode mode)
102 {
103     return O_WRONLY;
104 }
105
106 static char* fopen_cmd_arg(io_mode i)
107 {
108     return "w";
109 }
110 #endif