source: Sophya/trunk/SophyaLib/BaseTools/rawstream.cc@ 2541

Last change on this file since 2541 was 2459, checked in by ansari, 22 years ago

Suite modifications PPersist - en particulier, utilisation des RawInOutStream - Reza 22 Nov 2003

File size: 4.4 KB
Line 
1#include "rawstream.h"
2#include "pexceptions.h"
3
4/*!
5 \class SOPHYA::RawInOutStream
6 \ingroup BaseTools
7 This class defines the interface for a raw input/output stream.
8 It has been designed primarily to be used by PPF streams
9 (Sophya Persistence).
10*/
11
12/* --Methode-- */
13RawInOutStream::RawInOutStream()
14{
15}
16
17/* --Methode-- */
18RawInOutStream::~RawInOutStream()
19{
20}
21
22/* --Methode-- */
23bool RawInOutStream::isSeekable() const
24{
25 return false;
26}
27
28/* --Methode-- */
29int_8 RawInOutStream::tellg()
30{
31 throw NotAvailableOperation("RawInOutStream::tellg() - Not implemented ");
32}
33
34/* --Methode-- */
35RawInOutStream& RawInOutStream::seekg(int_8, int)
36{
37 throw NotAvailableOperation("RawInOutStream::seekg() - Not implemented ");
38}
39
40/* --Methode-- */
41RawInOutStream& RawInOutStream::read(char* s, uint_8 n)
42{
43 throw NotAvailableOperation("RawInOutStream::seekg() - Not implemented ");
44}
45/* --Methode-- */
46int_8 RawInOutStream::tellp()
47{
48 throw NotAvailableOperation("RawInOutStream::tellp() - Not implemented ");
49}
50
51/* --Methode-- */
52RawInOutStream& RawInOutStream::seekp(int_8, int)
53{
54 throw NotAvailableOperation("RawInOutStream::seekp() - Not implemented ");
55}
56
57/* --Methode-- */
58RawInOutStream& RawInOutStream::write(const char* s, uint_8 n)
59{
60 throw NotAvailableOperation("RawInOutStream::seekp() - Not implemented ");
61}
62
63
64/*!
65 \class SOPHYA::RawInFileStream
66 \ingroup BaseTools
67 Implements the input interface (reading) of RawInOutStream
68 on a disk file using the standard C library (fopen, fread, ...)
69*/
70
71/* --Methode-- */
72RawInFileStream::RawInFileStream(const char * path)
73 : RawInOutStream()
74{
75 fip = fopen(path,"rb");
76 if (fip == NULL) {
77 string errmsg = "RawInFileStream() - fopen(rb) ERROR - File= ";
78 errmsg += path;
79 throw IOExc(errmsg);
80 }
81 _filename = path;
82}
83
84/* --Methode-- */
85RawInFileStream::RawInFileStream(string const & path)
86 : RawInOutStream()
87{
88 fip = fopen(path.c_str(),"rb");
89 if (fip == NULL) {
90 string errmsg = "RawInFileStream() - fopen(rb) ERROR - File= ";
91 errmsg += path;
92 throw IOExc(errmsg);
93 }
94 _filename = path;
95}
96
97/* --Methode-- */
98RawInFileStream::~RawInFileStream()
99{
100 fclose(fip);
101}
102
103
104/* --Methode-- */
105bool RawInFileStream::isSeekable() const
106{
107 return true;
108}
109
110/* --Methode-- */
111int_8 RawInFileStream::tellg()
112{
113 return ftell(fip) ;
114}
115
116/* --Methode-- */
117RawInOutStream& RawInFileStream::seekg(int_8 off, int dir)
118{
119 int fsd = SEEK_SET;
120 if (dir == ios::beg) fsd = SEEK_SET;
121 else if (dir == ios::end) fsd = SEEK_END;
122 else fsd = SEEK_CUR;
123 fseek(fip, (long int)off, fsd);
124 return (*this);
125}
126
127/* --Methode-- */
128RawInOutStream& RawInFileStream::read(char* s, uint_8 n)
129{
130 size_t rc = fread((void *)s, 1, (size_t)n, fip);
131 if (rc != (size_t)n) {
132 string errmsg = "RawInFileStream::read() ERROR - File= ";
133 errmsg += _filename;
134 throw IOExc(errmsg);
135 }
136 return (*this);
137}
138
139/*!
140 \class SOPHYA::RawOutFileStream
141 \ingroup BaseTools
142 Implements the output interface (writing) of RawInOutStream
143 on a disk file using the standard C library (fopen, fwrite, ...)
144*/
145
146/* --Methode-- */
147RawOutFileStream::RawOutFileStream(const char * path)
148 : RawInOutStream()
149{
150 fip = fopen(path,"wb");
151 if (fip == NULL) {
152 string errmsg = "RawOutFileStream() - fopen(wb) ERROR - File= ";
153 errmsg += path;
154 throw IOExc(errmsg);
155 }
156 _filename = path;
157}
158
159/* --Methode-- */
160RawOutFileStream::RawOutFileStream(string const & path)
161 : RawInOutStream()
162{
163 fip = fopen(path.c_str(),"wb");
164 if (fip == NULL) {
165 string errmsg = "RawOutFileStream() - fopen(wb) ERROR - File= ";
166 errmsg += path;
167 throw IOExc(errmsg);
168 }
169 _filename = path;
170}
171
172/* --Methode-- */
173RawOutFileStream::~RawOutFileStream()
174{
175 fclose(fip);
176}
177
178
179/* --Methode-- */
180bool RawOutFileStream::isSeekable() const
181{
182 return true;
183}
184
185/* --Methode-- */
186int_8 RawOutFileStream::tellp()
187{
188 return ftell(fip) ;
189}
190
191/* --Methode-- */
192RawInOutStream& RawOutFileStream::seekp(int_8 off, int dir)
193{
194 int fsd = SEEK_SET;
195 if (dir == ios::beg) fsd = SEEK_SET;
196 else if (dir == ios::end) fsd = SEEK_END;
197 else fsd = SEEK_CUR;
198 fseek(fip, (long int)off, fsd);
199 return (*this);
200}
201
202/* --Methode-- */
203RawInOutStream& RawOutFileStream::write(const char* s, uint_8 n)
204{
205 size_t rc = fwrite((void *)s, 1, (size_t)n, fip);
206 if (rc != (size_t)n) {
207 string errmsg = "RawOutFileStream::write() ERROR - File= ";
208 errmsg += _filename;
209 throw IOExc(errmsg);
210 }
211 return (*this);
212}
Note: See TracBrowser for help on using the repository browser.