1 |
21 |
wfjm |
// $Id: Rw11VirtStream.cpp 516 2013-05-05 21:24:52Z mueller $
|
2 |
|
|
//
|
3 |
|
|
// Copyright 2013- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
|
4 |
|
|
//
|
5 |
|
|
// This program is free software; you may redistribute and/or modify it under
|
6 |
|
|
// the terms of the GNU General Public License as published by the Free
|
7 |
|
|
// Software Foundation, either version 2, or at your option any later version.
|
8 |
|
|
//
|
9 |
|
|
// This program is distributed in the hope that it will be useful, but
|
10 |
|
|
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
|
11 |
|
|
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
12 |
|
|
// for complete details.
|
13 |
|
|
//
|
14 |
|
|
// Revision History:
|
15 |
|
|
// Date Rev Version Comment
|
16 |
|
|
// 2013-05-05 516 1.0.1 Open(): support ?app and ?bck=n options
|
17 |
|
|
// 2013-05-04 515 1.0 Initial version
|
18 |
|
|
// 2013-05-01 513 0.1 First draft
|
19 |
|
|
// ---------------------------------------------------------------------------
|
20 |
|
|
|
21 |
|
|
/*!
|
22 |
|
|
\file
|
23 |
|
|
\version $Id: Rw11VirtStream.cpp 516 2013-05-05 21:24:52Z mueller $
|
24 |
|
|
\brief Implemenation of Rw11VirtStream.
|
25 |
|
|
*/
|
26 |
|
|
#include <memory>
|
27 |
|
|
|
28 |
|
|
#include "librtools/Rtools.hpp"
|
29 |
|
|
#include "librtools/Rexception.hpp"
|
30 |
|
|
#include "librtools/RparseUrl.hpp"
|
31 |
|
|
#include "librtools/RosFill.hpp"
|
32 |
|
|
|
33 |
|
|
#include "Rw11VirtStream.hpp"
|
34 |
|
|
|
35 |
|
|
using namespace std;
|
36 |
|
|
|
37 |
|
|
/*!
|
38 |
|
|
\class Retro::Rw11VirtStream
|
39 |
|
|
\brief FIXME_docs
|
40 |
|
|
*/
|
41 |
|
|
|
42 |
|
|
// all method definitions in namespace Retro
|
43 |
|
|
namespace Retro {
|
44 |
|
|
|
45 |
|
|
//------------------------------------------+-----------------------------------
|
46 |
|
|
//! Default constructor
|
47 |
|
|
|
48 |
|
|
Rw11VirtStream::Rw11VirtStream(Rw11Unit* punit)
|
49 |
|
|
: Rw11Virt(punit),
|
50 |
|
|
fIStream(false),
|
51 |
|
|
fOStream(false),
|
52 |
|
|
fFile(0)
|
53 |
|
|
{
|
54 |
|
|
fStats.Define(kStatNVSRead, "NVSRead", "Read() calls");
|
55 |
|
|
fStats.Define(kStatNVSReadByt, "NVSReadByt", "bytes read");
|
56 |
|
|
fStats.Define(kStatNVSWrite, "NVSWrite", "Write() calls");
|
57 |
|
|
fStats.Define(kStatNVSWriteByt,"NVSWriteByt", "bytes written");
|
58 |
|
|
fStats.Define(kStatNVSFlush, "NVSFlush", "Flush() calls");
|
59 |
|
|
fStats.Define(kStatNVSTell, "NVSTell", "Tell() calls");
|
60 |
|
|
fStats.Define(kStatNVSSeek, "NVSSeek", "Seek() calls");
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
//------------------------------------------+-----------------------------------
|
64 |
|
|
//! Destructor
|
65 |
|
|
|
66 |
|
|
Rw11VirtStream::~Rw11VirtStream()
|
67 |
|
|
{
|
68 |
|
|
if (fFile) ::fclose(fFile);
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
//------------------------------------------+-----------------------------------
|
72 |
|
|
//! FIXME_docs
|
73 |
|
|
|
74 |
|
|
bool Rw11VirtStream::Open(const std::string& url, RerrMsg& emsg)
|
75 |
|
|
{
|
76 |
|
|
RparseUrl opts;
|
77 |
|
|
if (!opts.Set(fpUnit->AttachOpts(), "|ronly|wonly|", emsg)) return false;
|
78 |
|
|
fIStream = opts.FindOpt("ronly");
|
79 |
|
|
fOStream = opts.FindOpt("wonly");
|
80 |
|
|
if (!(fIStream ^ fOStream))
|
81 |
|
|
throw Rexception("Rw11VirtStream::Open",
|
82 |
|
|
"Bad state: neither ronly nor wonly seen");
|
83 |
|
|
|
84 |
|
|
if (fOStream) { // handle output streams
|
85 |
|
|
if (!fUrl.Set(url, "|app|bck=|", emsg)) return false;
|
86 |
|
|
if (!Rtools::CreateBackupFile(fUrl, emsg)) return false;
|
87 |
|
|
|
88 |
|
|
fFile = ::fopen(fUrl.Path().c_str(), fUrl.FindOpt("app") ? "a" : "w");
|
89 |
|
|
|
90 |
|
|
} else { // handle input streams
|
91 |
|
|
if (!fUrl.Set(url, "", emsg)) return false;
|
92 |
|
|
fFile = ::fopen(fUrl.Path().c_str(), "r");
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
if (!fFile) {
|
96 |
|
|
emsg.InitErrno("Rw11VirtStream::Open()",
|
97 |
|
|
string("fopen() for '") + fUrl.Path() + "' failed: ",
|
98 |
|
|
errno);
|
99 |
|
|
return false;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
return true;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
//------------------------------------------+-----------------------------------
|
106 |
|
|
//! FIXME_docs
|
107 |
|
|
|
108 |
|
|
int Rw11VirtStream::Read(uint8_t* data, size_t count, RerrMsg& emsg)
|
109 |
|
|
{
|
110 |
|
|
if (!fIStream)
|
111 |
|
|
throw Rexception("Rw11VirtStream::Read",
|
112 |
|
|
"Bad state: Read() called but fIStream=false");
|
113 |
|
|
if (!fFile)
|
114 |
|
|
throw Rexception("Rw11VirtStream::Read", "Bad state: file not open");
|
115 |
|
|
|
116 |
|
|
fStats.Inc(kStatNVSRead);
|
117 |
|
|
size_t irc = ::fread(data, 1, count, fFile);
|
118 |
|
|
if (irc == 0 && ferror(fFile)) {
|
119 |
|
|
emsg.InitErrno("Rw11VirtStream::Read()", "fread() failed: ", errno);
|
120 |
|
|
return -1;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
fStats.Inc(kStatNVSReadByt, double(irc));
|
124 |
|
|
return int(irc);
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
//------------------------------------------+-----------------------------------
|
128 |
|
|
//! FIXME_docs
|
129 |
|
|
|
130 |
|
|
bool Rw11VirtStream::Write(const uint8_t* data, size_t count, RerrMsg& emsg)
|
131 |
|
|
{
|
132 |
|
|
if (!fOStream)
|
133 |
|
|
throw Rexception("Rw11VirtStream::Write",
|
134 |
|
|
"Bad state: Write() called but fOStream=false");
|
135 |
|
|
if (!fFile)
|
136 |
|
|
throw Rexception("Rw11VirtStream::Write", "Bad state: file not open");
|
137 |
|
|
|
138 |
|
|
fStats.Inc(kStatNVSWrite);
|
139 |
|
|
size_t irc = ::fwrite(data, 1, count, fFile);
|
140 |
|
|
if (irc != count) {
|
141 |
|
|
emsg.InitErrno("Rw11VirtStream::Write()", "fwrite() failed: ", errno);
|
142 |
|
|
return false;
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
fStats.Inc(kStatNVSWriteByt, double(count));
|
146 |
|
|
return true;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
//------------------------------------------+-----------------------------------
|
150 |
|
|
//! FIXME_docs
|
151 |
|
|
|
152 |
|
|
bool Rw11VirtStream::Flush(RerrMsg& emsg)
|
153 |
|
|
{
|
154 |
|
|
if (!fOStream) return true;
|
155 |
|
|
if (!fFile)
|
156 |
|
|
throw Rexception("Rw11VirtStream::Write", "Bad state: file not open");
|
157 |
|
|
|
158 |
|
|
fStats.Inc(kStatNVSFlush);
|
159 |
|
|
size_t irc = ::fflush(fFile);
|
160 |
|
|
if (irc != 0) {
|
161 |
|
|
emsg.InitErrno("Rw11VirtStream::Flush()", "fflush() failed: ", errno);
|
162 |
|
|
return false;
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
return true;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
//------------------------------------------+-----------------------------------
|
169 |
|
|
//! FIXME_docs
|
170 |
|
|
|
171 |
|
|
int Rw11VirtStream::Tell(RerrMsg& emsg)
|
172 |
|
|
{
|
173 |
|
|
if (!fFile)
|
174 |
|
|
throw Rexception("Rw11VirtStream::Tell", "Bad state: file not open");
|
175 |
|
|
|
176 |
|
|
fStats.Inc(kStatNVSTell);
|
177 |
|
|
long irc = ::ftell(fFile);
|
178 |
|
|
if (irc < 0) {
|
179 |
|
|
emsg.InitErrno("Rw11VirtStream::Tell()", "ftell() failed: ", errno);
|
180 |
|
|
return -1;
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
return int(irc);
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
//------------------------------------------+-----------------------------------
|
187 |
|
|
//! FIXME_docs
|
188 |
|
|
|
189 |
|
|
bool Rw11VirtStream::Seek(int pos, RerrMsg& emsg)
|
190 |
|
|
{
|
191 |
|
|
if (!fFile)
|
192 |
|
|
throw Rexception("Rw11VirtStream::Seek", "Bad state: file not open");
|
193 |
|
|
|
194 |
|
|
fStats.Inc(kStatNVSSeek);
|
195 |
|
|
int whence = SEEK_SET;
|
196 |
|
|
if (pos < 0) {
|
197 |
|
|
pos = 0;
|
198 |
|
|
whence = SEEK_END;
|
199 |
|
|
}
|
200 |
|
|
int irc = ::fseek(fFile, pos, whence);
|
201 |
|
|
|
202 |
|
|
if (irc < 0) {
|
203 |
|
|
emsg.InitErrno("Rw11VirtStream::Seek()", "fseek() failed: ", errno);
|
204 |
|
|
return false;
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
return true;
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
//------------------------------------------+-----------------------------------
|
211 |
|
|
//! FIXME_docs
|
212 |
|
|
|
213 |
|
|
void Rw11VirtStream::Dump(std::ostream& os, int ind, const char* text) const
|
214 |
|
|
{
|
215 |
|
|
RosFill bl(ind);
|
216 |
|
|
os << bl << (text?text:"--") << "Rw11VirtStream @ " << this << endl;
|
217 |
|
|
|
218 |
|
|
os << bl << " fIStream: " << fIStream << endl;
|
219 |
|
|
os << bl << " fOStream: " << fOStream << endl;
|
220 |
|
|
os << bl << " fFile: " << fFile << endl;
|
221 |
|
|
Rw11Virt::Dump(os, ind, " ^");
|
222 |
|
|
return;
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
//------------------------------------------+-----------------------------------
|
226 |
|
|
//! FIXME_docs
|
227 |
|
|
|
228 |
|
|
Rw11VirtStream* Rw11VirtStream::New(const std::string& url, Rw11Unit* punit,
|
229 |
|
|
RerrMsg& emsg)
|
230 |
|
|
{
|
231 |
|
|
unique_ptr<Rw11VirtStream> p;
|
232 |
|
|
p.reset(new Rw11VirtStream(punit));
|
233 |
|
|
if (p->Open(url, emsg)) return p.release();
|
234 |
|
|
return 0;
|
235 |
|
|
}
|
236 |
|
|
|
237 |
|
|
|
238 |
|
|
} // end namespace Retro
|