OSDN Git Service

* config/io/basic_file_stdio.cc (__basic_file::_M_open_mode):
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / config / io / basic_file_stdio.cc
1 // Wrapper of C-language FILE struct -*- C++ -*-
2
3 // Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 //
31 // ISO C++ 14882: 27.8  File-based streams
32 //
33
34 #include <bits/basic_file.h>
35 #include <fcntl.h>
36
37 namespace std 
38 {
39   // Definitions for __basic_file<char>.
40   __basic_file<char>::__basic_file(__c_lock* /*__lock*/) 
41   : _M_cfile(NULL), _M_cfile_created(false) { }
42
43   __basic_file<char>::~__basic_file()
44   { this->close(); }
45       
46   void 
47   __basic_file<char>::_M_open_mode(ios_base::openmode __mode, int& __p_mode, 
48                                    int&, char* __c_mode)
49   {  
50     bool __testb = __mode & ios_base::binary;
51     bool __testi = __mode & ios_base::in;
52     bool __testo = __mode & ios_base::out;
53     bool __testt = __mode & ios_base::trunc;
54     bool __testa = __mode & ios_base::app;
55       
56     // Set __c_mode for use in fopen.
57     // Set __p_mode for use in open.
58     if (!__testi && __testo && !__testt && !__testa)
59       {
60         strcpy(__c_mode, "w");
61         __p_mode = (O_WRONLY | O_CREAT);
62       }
63     if (!__testi && __testo && !__testt && __testa)
64       {
65         strcpy(__c_mode, "a");
66         __p_mode |=  O_WRONLY | O_CREAT | O_APPEND;
67       }
68     if (!__testi && __testo && __testt && !__testa)
69       {
70         strcpy(__c_mode, "w");
71         __p_mode |=  O_WRONLY | O_CREAT | O_TRUNC;
72       }
73
74     if (__testi && !__testo && !__testt && !__testa)
75       {
76         strcpy(__c_mode, "r");
77 #if defined (O_NONBLOCK)
78         __p_mode |=  O_RDONLY | O_NONBLOCK;
79 #else
80         __p_mode |=  O_RDONLY;
81 #endif
82       }
83     if (__testi && __testo && !__testt && !__testa)
84       {
85         strcpy(__c_mode, "r+");
86         __p_mode |=  O_RDWR | O_CREAT;
87       }
88     if (__testi && __testo && __testt && !__testa)
89       {
90         strcpy(__c_mode, "w+");
91         __p_mode |=  O_RDWR | O_CREAT | O_TRUNC;
92       }
93     if (__testb)
94       strcat(__c_mode, "b");
95   }
96   
97   __basic_file<char>*
98   __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode) 
99   {
100     __basic_file* __ret = NULL;
101     if (!this->is_open() && __file)
102       {
103         _M_cfile = __file;
104         _M_cfile_created = false;
105         __ret = this;
106       }
107     return __ret;
108   }
109   
110   __basic_file<char>*
111   __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode, 
112                                bool __del) 
113   {
114     __basic_file* __ret = NULL;
115     int __p_mode = 0;
116     int __rw_mode = 0;
117     char __c_mode[4];
118     
119     _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
120     if (!this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
121       {
122         // Iff __del is true, then close will fclose the fd.
123         _M_cfile_created = __del;
124
125         if (__fd == 0)
126           setvbuf(_M_cfile, reinterpret_cast<char*>(NULL), _IONBF, 0);
127
128         __ret = this;
129       }
130     return __ret;
131   }
132
133   int
134   __basic_file<char>::sys_getc() 
135   { return getc(_M_cfile); }
136
137   int
138   __basic_file<char>::sys_ungetc(int __c) 
139   { return ungetc(__c, _M_cfile); }
140   
141   __basic_file<char>* 
142   __basic_file<char>::open(const char* __name, ios_base::openmode __mode, 
143                            int /*__prot*/)
144   {
145     __basic_file* __ret = NULL;
146     int __p_mode = 0;
147     int __rw_mode = 0;
148     char __c_mode[4];
149       
150     _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
151
152     if (!this->is_open())
153       {
154         if ((_M_cfile = fopen(__name, __c_mode)))
155           {
156             _M_cfile_created = true;
157
158 #if defined (F_SETFL) && defined (O_NONBLOCK)
159             // Set input to nonblocking for fifos.
160             if (__mode & ios_base::in)
161               fcntl(this->fd(), F_SETFL, O_NONBLOCK);
162 #endif
163
164             __ret = this;
165           }
166       }
167     return __ret;
168   }
169   
170   bool 
171   __basic_file<char>::is_open() const 
172   { return _M_cfile != 0; }
173   
174   int 
175   __basic_file<char>::fd() 
176   { return fileno(_M_cfile) ; }
177   
178   __basic_file<char>* 
179   __basic_file<char>::close()
180   { 
181     __basic_file* __retval = static_cast<__basic_file*>(NULL);
182     if (this->is_open())
183       {
184         fflush(_M_cfile);
185         if ((_M_cfile_created && fclose(_M_cfile) == 0) || !_M_cfile_created)
186           {
187             _M_cfile = 0;
188             __retval = this;
189           }
190       }
191     return __retval;
192   }
193  
194   streamsize 
195   __basic_file<char>::xsgetn(char* __s, streamsize __n)
196   { return fread(__s, 1, __n, _M_cfile); }
197   
198   streamsize 
199   __basic_file<char>::xsputn(const char* __s, streamsize __n)
200   { return fwrite(__s, 1, __n, _M_cfile); }
201   
202   streamoff
203   __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way, 
204                               ios_base::openmode /*__mode*/)
205   { 
206     fseek(_M_cfile, __off, __way); 
207     return ftell(_M_cfile); 
208   }
209
210   streamoff
211   __basic_file<char>::seekpos(streamoff __pos, ios_base::openmode /*__mode*/)
212   { 
213     fseek(_M_cfile, __pos, ios_base::beg); 
214     return ftell(_M_cfile); 
215   }
216   
217   int 
218   __basic_file<char>::sync() 
219   { return fflush(_M_cfile); }
220 }  // namespace std