OSDN Git Service

eaebdf25ad9eb6c7c9a82a225e33835188b1496c
[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   char
85   __basic_file<char>::sys_getc() { return getc (_M_cfile); }
86   
87   char
88   __basic_file<char>::sys_ungetc(char __s) { return ungetc (__s, _M_cfile); }
89   
90   __basic_file<char>* 
91   __basic_file<char>::open(const char* __name, ios_base::openmode __mode, 
92                            int /*__prot*/)
93   {
94     __basic_file* __ret = NULL;
95     int __p_mode = 0;
96     int __rw_mode = 0;
97     char __c_mode[4];
98       
99     _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
100
101     if (!this->is_open())
102       {
103         if ((_M_cfile = fopen(__name, __c_mode)))
104           {
105             _M_cfile_created = true;
106             __ret = this;
107           }
108       }
109     return __ret;
110   }
111   
112   bool 
113   __basic_file<char>::is_open() const { return _M_cfile != 0; }
114   
115   int 
116   __basic_file<char>::fd() { return fileno(_M_cfile) ; }
117   
118   __basic_file<char>* 
119   __basic_file<char>::close()
120   { 
121     __basic_file* __retval = static_cast<__basic_file*>(NULL);
122     if (this->is_open())
123       {
124         fflush(_M_cfile);
125         if ((_M_cfile_created && fclose(_M_cfile) == 0) || !_M_cfile_created)
126           {
127             _M_cfile = 0;
128             __retval = this;
129           }
130       }
131     return __retval;
132   }
133  
134   streamsize 
135   __basic_file<char>::xsgetn(char* __s, streamsize __n)
136   { return fread(__s, 1, __n, _M_cfile); }
137   
138   streamsize 
139   __basic_file<char>::xsputn(const char* __s, streamsize __n)
140   { return fwrite(__s, 1, __n, _M_cfile); }
141   
142   streamoff
143   __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way, 
144                               ios_base::openmode /*__mode*/)
145   { 
146     fseek(_M_cfile, __off, __way); 
147     return ftell(_M_cfile); 
148   }
149
150   streamoff
151   __basic_file<char>::seekpos(streamoff __pos, ios_base::openmode /*__mode*/)
152   { 
153     fseek(_M_cfile, __pos, ios_base::beg); 
154     return ftell(_M_cfile); 
155   }
156   
157   int 
158   __basic_file<char>::sync() { return fflush(_M_cfile); }
159 }  // namespace std