OSDN Git Service

2002-04-28 Benjamin Kosnik <bkoz@redhat.com>
[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
36 namespace std 
37 {
38   // Definitions for __basic_file<char>.
39   __basic_file<char>::__basic_file(__c_lock* /*__lock*/) 
40   : _M_cfile(NULL), _M_cfile_created(false) { }
41
42   __basic_file<char>::~__basic_file()
43   { this->close(); }
44       
45   void 
46   __basic_file<char>::_M_open_mode(ios_base::openmode __mode, int&, int&, 
47                                    char* __c_mode)
48   {  
49     bool __testb = __mode & ios_base::binary;
50     bool __testi = __mode & ios_base::in;
51     bool __testo = __mode & ios_base::out;
52     bool __testt = __mode & ios_base::trunc;
53     bool __testa = __mode & ios_base::app;
54       
55     if (!__testi && __testo && !__testt && !__testa)
56       strcpy(__c_mode, "w");
57     if (!__testi && __testo && !__testt && __testa)
58       strcpy(__c_mode, "a");
59     if (!__testi && __testo && __testt && !__testa)
60       strcpy(__c_mode, "w");
61     if (__testi && !__testo && !__testt && !__testa)
62       strcpy(__c_mode, "r");
63     if (__testi && __testo && !__testt && !__testa)
64       strcpy(__c_mode, "r+");
65     if (__testi && __testo && __testt && !__testa)
66       strcpy(__c_mode, "w+");
67     if (__testb)
68       strcat(__c_mode, "b");
69   }
70   
71   __basic_file<char>*
72   __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode) 
73   {
74     __basic_file* __ret = NULL;
75     if (!this->is_open() && __file)
76       {
77         _M_cfile = __file;
78         _M_cfile_created = false;
79         __ret = this;
80       }
81     return __ret;
82   }
83
84   int
85   __basic_file<char>::sys_getc() 
86   { return getc(_M_cfile); }
87   
88   int
89   __basic_file<char>::sys_ungetc(int __c) 
90   { return ungetc(__c, _M_cfile); }
91   
92   __basic_file<char>* 
93   __basic_file<char>::open(const char* __name, ios_base::openmode __mode, 
94                            int /*__prot*/)
95   {
96     __basic_file* __ret = NULL;
97     int __p_mode = 0;
98     int __rw_mode = 0;
99     char __c_mode[4];
100       
101     _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
102
103     if (!this->is_open())
104       {
105         if ((_M_cfile = fopen(__name, __c_mode)))
106           {
107             _M_cfile_created = true;
108             __ret = this;
109           }
110       }
111     return __ret;
112   }
113   
114   bool 
115   __basic_file<char>::is_open() const { return _M_cfile != 0; }
116   
117   int 
118   __basic_file<char>::fd() { return fileno(_M_cfile) ; }
119   
120   __basic_file<char>* 
121   __basic_file<char>::close()
122   { 
123     __basic_file* __retval = static_cast<__basic_file*>(NULL);
124     if (this->is_open())
125       {
126         fflush(_M_cfile);
127         if ((_M_cfile_created && fclose(_M_cfile) == 0) || !_M_cfile_created)
128           {
129             _M_cfile = 0;
130             __retval = this;
131           }
132       }
133     return __retval;
134   }
135  
136   streamsize 
137   __basic_file<char>::xsgetn(char* __s, streamsize __n)
138   { return fread(__s, 1, __n, _M_cfile); }
139   
140   streamsize 
141   __basic_file<char>::xsputn(const char* __s, streamsize __n)
142   { return fwrite(__s, 1, __n, _M_cfile); }
143   
144   streamoff
145   __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way, 
146                               ios_base::openmode /*__mode*/)
147   { 
148     fseek(_M_cfile, __off, __way); 
149     return ftell(_M_cfile); 
150   }
151
152   streamoff
153   __basic_file<char>::seekpos(streamoff __pos, ios_base::openmode /*__mode*/)
154   { 
155     fseek(_M_cfile, __pos, ios_base::beg); 
156     return ftell(_M_cfile); 
157   }
158   
159   int 
160   __basic_file<char>::sync() { return fflush(_M_cfile); }
161 }  // namespace std