1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
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)
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.
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,
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.
31 // ISO C++ 14882: 27.8 File-based streams
34 #include <bits/basic_file.h>
38 // Definitions for __basic_file<char>.
39 __basic_file<char>::__basic_file(__c_lock* /*__lock*/)
40 : _M_cfile(NULL), _M_cfile_created(false) { }
42 __basic_file<char>::~__basic_file()
46 __basic_file<char>::_M_open_mode(ios_base::openmode __mode, int&, int&,
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;
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+");
68 strcat(__c_mode, "b");
72 __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
74 __basic_file* __ret = NULL;
75 if (!this->is_open() && __file)
78 _M_cfile_created = false;
85 __basic_file<char>::sys_getc()
86 { return getc(_M_cfile); }
89 __basic_file<char>::sys_ungetc(int __c)
90 { return ungetc(__c, _M_cfile); }
93 __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
96 __basic_file* __ret = NULL;
101 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
103 if (!this->is_open())
105 if ((_M_cfile = fopen(__name, __c_mode)))
107 _M_cfile_created = true;
115 __basic_file<char>::is_open() const { return _M_cfile != 0; }
118 __basic_file<char>::fd() { return fileno(_M_cfile) ; }
121 __basic_file<char>::close()
123 __basic_file* __retval = static_cast<__basic_file*>(NULL);
127 if ((_M_cfile_created && fclose(_M_cfile) == 0) || !_M_cfile_created)
137 __basic_file<char>::xsgetn(char* __s, streamsize __n)
138 { return fread(__s, 1, __n, _M_cfile); }
141 __basic_file<char>::xsputn(const char* __s, streamsize __n)
142 { return fwrite(__s, 1, __n, _M_cfile); }
145 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way,
146 ios_base::openmode /*__mode*/)
148 fseek(_M_cfile, __off, __way);
149 return ftell(_M_cfile);
153 __basic_file<char>::seekpos(streamoff __pos, ios_base::openmode /*__mode*/)
155 fseek(_M_cfile, __pos, ios_base::beg);
156 return ftell(_M_cfile);
160 __basic_file<char>::sync() { return fflush(_M_cfile); }