1 /*
2  * Copyright (C) 2006-2007 Marc Boris Duerner
3  * Copyright (C) 2006-2007 Laurentiu-Gheorghe Crisan
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
28  */
29 #include <cxxtools/filedevice.h>
30 #include "filedeviceimpl.h"
31 
32 namespace cxxtools
33 {
34 
FileDevice()35 FileDevice::FileDevice()
36 {
37     _impl = new FileDeviceImpl(*this);
38 }
39 
40 
FileDevice(const std::string & path,IODevice::OpenMode mode,bool inherit)41 FileDevice::FileDevice(const std::string& path, IODevice::OpenMode mode, bool inherit)
42 {
43     _impl = new FileDeviceImpl(*this);
44 
45     open(path, mode, inherit);
46 }
47 
48 
~FileDevice()49 FileDevice::~FileDevice()
50 {
51     try
52     {
53         close();
54     }
55     catch(...)
56     { }
57 
58     delete _impl;
59 }
60 
61 
open(const std::string & path,IODevice::OpenMode mode,bool inherit)62 void FileDevice::open( const std::string& path, IODevice::OpenMode mode, bool inherit)
63 {
64     close();
65     _impl->open(path, mode, inherit);
66     _path = path;
67 }
68 
69 
onClose()70 void FileDevice::onClose()
71 {
72     _impl->close();
73 }
74 
75 
onSetTimeout(size_t timeout)76 void FileDevice::onSetTimeout(size_t timeout)
77 {
78     _impl->setTimeout(timeout);
79 }
80 
81 
onBeginRead(char * buffer,size_t n,bool & eof)82 size_t FileDevice::onBeginRead(char* buffer, size_t n, bool& eof)
83 {
84     return _impl->beginRead(buffer, n, eof);
85 }
86 
87 
onEndRead(bool & eof)88 size_t FileDevice::onEndRead(bool& eof)
89 {
90     return _impl->endRead(eof);
91 }
92 
93 
onBeginWrite(const char * buffer,size_t n)94 size_t FileDevice::onBeginWrite(const char* buffer, size_t n)
95 {
96     return _impl->beginWrite(buffer, n);
97 }
98 
99 
onEndWrite()100 size_t FileDevice::onEndWrite()
101 {
102     return _impl->endWrite();
103 }
104 
105 
size() const106 size_t FileDevice::size() const
107 {
108     return _impl->size();
109 }
110 
111 
onSeek(off_type offset,std::ios::seekdir sd)112 FileDevice::pos_type FileDevice::onSeek(off_type offset, std::ios::seekdir sd)
113 {
114     return _impl->seek(offset, sd);
115 }
116 
117 
onRead(char * buffer,size_t count,bool & eof)118 size_t FileDevice::onRead( char* buffer, size_t count, bool& eof )
119 {
120     //if(count > SSIZE_MAX)
121     //    count = SSIZE_MAX;
122 
123     size_t ret = _impl->read( buffer, count, eof );
124     return ret;
125 }
126 
127 
onWrite(const char * buffer,size_t count)128 size_t FileDevice::onWrite(const char* buffer, size_t count)
129 {
130     return _impl->write(buffer, count);
131 }
132 
133 
onCancel()134 void FileDevice::onCancel()
135 {
136     _impl->cancel();
137 }
138 
139 
onPeek(char * buffer,size_t count)140 size_t FileDevice::onPeek(char* buffer, size_t count)
141 {
142     return _impl->peek(buffer, count);
143 }
144 
145 
onSync() const146 void FileDevice::onSync() const
147 {
148     _impl->sync();
149 }
150 
151 
152 } // namespace cxxtools
153