1
by Viktor Bursian
first usable version 0.2.0 |
1 |
////////////////////////////////////////////////////////////////////////////////
|
2 |
/*! @file Files.cpp Минимальный интерфейс файловой системы.
|
|
3 |
- This implementation uses Qt v.4.6 - http://qt.nokia.com/
|
|
4 |
- Part of RANet - Research Assistant Net Library (based on ANSI C++).
|
|
5 |
- Copyright(C) 2010, Viktor E. Bursian, St.Petersburg, Russia.
|
|
6 |
Viktor.Bursian@mail.ioffe.ru
|
|
7 |
*/////////////////////////////////////////////////////////////////////////////// |
|
8 |
#include "Files.h" |
|
9 |
#include "Excepts.h" |
|
10 |
//#include <unistd.h> //for getcwd()
|
|
11 |
#include <QFileInfo> |
|
12 |
#include <QFile> |
|
13 |
#include <QDir> |
|
14 |
#include <QString> |
|
4.43.1
by Viktor Bursian at blin-ubuntu
SOS2RA: further development |
15 |
#include <QDateTime> |
1
by Viktor Bursian
first usable version 0.2.0 |
16 |
namespace RA { |
17 |
//------------------------------------------------------------------------------
|
|
18 |
||
2
by Viktor Bursian
version 0.3.0 |
19 |
inline QString ToQString (sString S) |
1
by Viktor Bursian
first usable version 0.2.0 |
20 |
{
|
21 |
return QString::fromUtf8(literal(S)); |
|
22 |
}
|
|
23 |
||
24 |
||
2
by Viktor Bursian
version 0.3.0 |
25 |
inline sString FromQString (QString S) |
1
by Viktor Bursian
first usable version 0.2.0 |
26 |
{
|
27 |
return sString(literal(S.toUtf8())); |
|
28 |
}
|
|
29 |
||
2
by Viktor Bursian
version 0.3.0 |
30 |
|
1
by Viktor Bursian
first usable version 0.2.0 |
31 |
/*
|
2
by Viktor Bursian
version 0.3.0 |
32 |
FromQString( QFileInfo(___).absoluteFilePath() );
|
1
by Viktor Bursian
first usable version 0.2.0 |
33 |
*/
|
34 |
//-------------------------------------------------------------------- sPath ---
|
|
35 |
||
36 |
sString sPath::Validate (rcsString S) |
|
37 |
{
|
|
38 |
return S.Translate(':','_') |
|
39 |
.Translate('*','_') |
|
40 |
.Translate('?','_') |
|
41 |
.Translate('"','_') |
|
42 |
.Translate('<','_') |
|
43 |
.Translate('>','_') |
|
44 |
.Translate('|','_') |
|
45 |
.Translate('\\','_'); |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
46 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
47 |
|
48 |
||
49 |
sString sPath::ValidateName (rcsString S) |
|
50 |
{
|
|
51 |
return sPath::Validate(S).Translate('/','_'); |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
52 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
53 |
|
54 |
||
55 |
sPath::sPath () |
|
56 |
{
|
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
57 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
58 |
|
59 |
||
60 |
sPath::sPath (rcsString path_str) |
|
61 |
:ThePathStr(path_str) |
|
62 |
{
|
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
63 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
64 |
|
65 |
||
66 |
//sPath::sPath (rcsString dir
|
|
67 |
// ,rcsString path_relative_to_the_dir)
|
|
68 |
//{
|
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
69 |
//}
|
1
by Viktor Bursian
first usable version 0.2.0 |
70 |
|
71 |
||
72 |
//sPath::sPath (rcsString dir
|
|
73 |
// ,rcsString path_relative_to_the_dir
|
|
74 |
// ,rcsString extension)
|
|
75 |
//{
|
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
76 |
//}
|
1
by Viktor Bursian
first usable version 0.2.0 |
77 |
|
78 |
||
79 |
sPath::sPath (rcsPath a_path) |
|
80 |
:ThePathStr(a_path.ThePathStr) |
|
81 |
{
|
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
82 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
83 |
|
84 |
||
85 |
rsPath sPath::operator = (rcsPath a_path) |
|
86 |
{
|
|
87 |
ThePathStr = a_path.ThePathStr; |
|
88 |
return *this; |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
89 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
90 |
|
91 |
||
92 |
sPath sPath::Dir () const |
|
93 |
{
|
|
94 |
sString dir; |
|
95 |
sString name; |
|
96 |
sString extension; |
|
97 |
Split(dir,name,extension); |
|
98 |
return sPath(dir); |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
99 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
100 |
|
101 |
||
102 |
sString sPath::Name () const |
|
103 |
{
|
|
104 |
sString dir; |
|
105 |
sString name; |
|
106 |
sString extension; |
|
107 |
Split(dir,name,extension); |
|
108 |
return name; |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
109 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
110 |
|
111 |
||
112 |
sString sPath::Ext () const |
|
113 |
{
|
|
114 |
sString dir; |
|
115 |
sString name; |
|
116 |
sString extension; |
|
117 |
Split(dir,name,extension); |
|
118 |
return extension; |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
119 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
120 |
|
121 |
||
122 |
void sPath::Validate () |
|
123 |
{
|
|
124 |
ThePathStr=Validate(ThePathStr); |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
125 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
126 |
|
127 |
||
128 |
sPath sPath::CurrentDir () |
|
129 |
{
|
|
130 |
sString D; |
|
2
by Viktor Bursian
version 0.3.0 |
131 |
D=FromQString( QDir::currentPath() ); |
1
by Viktor Bursian
first usable version 0.2.0 |
132 |
return D; |
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
133 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
134 |
|
135 |
||
136 |
bool sPath::Exists () const |
|
137 |
{
|
|
2
by Viktor Bursian
version 0.3.0 |
138 |
return QFileInfo( ToQString(ThePathStr) ).exists(); |
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
139 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
140 |
|
141 |
||
4.43.1
by Viktor Bursian at blin-ubuntu
SOS2RA: further development |
142 |
sTime sPath::ModificationTime () const |
143 |
{
|
|
144 |
return sTime::FromMillisecondsSinceEpoch( |
|
145 |
QFileInfo( ToQString(ThePathStr) ).lastModified().toMSecsSinceEpoch() |
|
146 |
);
|
|
147 |
}
|
|
148 |
||
149 |
||
150 |
sTime sPath::AccessTime () const |
|
151 |
{
|
|
152 |
return sTime::FromMillisecondsSinceEpoch( |
|
153 |
QFileInfo( ToQString(ThePathStr) ).lastRead().toMSecsSinceEpoch() |
|
154 |
);
|
|
155 |
}
|
|
156 |
||
157 |
||
158 |
sTime sPath::CreationTime () const |
|
159 |
{
|
|
160 |
return sTime::FromMillisecondsSinceEpoch( |
|
161 |
QFileInfo( ToQString(ThePathStr) ).created().toMSecsSinceEpoch() |
|
162 |
);
|
|
163 |
}
|
|
164 |
||
165 |
||
1
by Viktor Bursian
first usable version 0.2.0 |
166 |
bool sPath::Erase () const |
167 |
{
|
|
168 |
bool OK = false; |
|
2
by Viktor Bursian
version 0.3.0 |
169 |
OK = QFile::remove( ToQString(ThePathStr) ); |
4.4.42
by Viktor Bursian at blin-Ubuntu
Небольшая чистка мусора, украшательство и пересмотр todo. |
170 |
/*! @todo{env} remove empty dirs */
|
1
by Viktor Bursian
first usable version 0.2.0 |
171 |
return OK; |
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
172 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
173 |
|
174 |
// <cstdio>
|
|
175 |
//
|
|
176 |
//int remove ( const char * filename );
|
|
177 |
// int i;
|
|
178 |
// if (system(NULL)) cout<<"Ok ";
|
|
179 |
// else exit (1);
|
|
180 |
// cout << "Executing command mkdir..." << endl;
|
|
181 |
// i=system ("mkdir -p ratemp/radata");
|
|
182 |
// cout << "The value returned was:" << i << endl;
|
|
183 |
||
184 |
||
185 |
||
186 |
bool sPath::CreateDirForIt () const |
|
187 |
{
|
|
188 |
QDir CD(QDir::current()); |
|
189 |
bool OK = false; |
|
2
by Viktor Bursian
version 0.3.0 |
190 |
OK = CD.mkpath( ToQString(Dir().PathStr()) ); |
1
by Viktor Bursian
first usable version 0.2.0 |
191 |
return OK; |
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
192 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
193 |
|
194 |
||
195 |
void sPath::Split (rsString dir |
|
196 |
,rsString name |
|
197 |
,rsString extension) const |
|
198 |
{
|
|
199 |
sString tail(ThePathStr); |
|
200 |
size_t i=tail.Pos("/"); |
|
201 |
while( i < tail.Length() ){ |
|
202 |
dir+=tail.Substr(0,i+1); |
|
203 |
tail=tail.Tail(i+1); |
|
204 |
i=tail.Pos("/"); |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
205 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
206 |
if( dir.Length() > 1 ){ |
207 |
dir=dir.Substr(0,dir.Length()-1); |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
208 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
209 |
if( ! tail.Empty() ){ |
210 |
i=tail.Pos("."); |
|
211 |
while( i < tail.Length() ){ |
|
212 |
name+=tail.Substr(0,i+1); |
|
213 |
tail=tail.Tail(i+1); |
|
214 |
i=tail.Pos("."); |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
215 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
216 |
if( name.Length() <= 1 ){ |
217 |
name+=tail; |
|
218 |
}else{ |
|
219 |
name=name.Substr(0,name.Length()-1); |
|
220 |
extension=tail; |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
221 |
}
|
222 |
}
|
|
223 |
}
|
|
1
by Viktor Bursian
first usable version 0.2.0 |
224 |
|
225 |
//-------------------------------------------------------------------- sFile ---
|
|
226 |
||
227 |
sFile::~sFile() |
|
228 |
{
|
|
229 |
if( State==Opened ){ |
|
230 |
Close(); |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
231 |
}
|
232 |
}
|
|
1
by Viktor Bursian
first usable version 0.2.0 |
233 |
|
234 |
||
235 |
void sFile::Assign(rcsString fname, eTypeMode type) |
|
236 |
{
|
|
237 |
if( State == Opened ){ |
|
238 |
Close(); |
|
9.10.1
by Viktor Bursian at blin-ubuntu
Exception class tree reorganized. |
239 |
throw xProgramError("sFile::Assign - it is open" |
240 |
,__FILE__,__LINE__); |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
241 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
242 |
FullName=fname; |
243 |
Type=type; |
|
244 |
if( FullName.Empty() ){ |
|
245 |
State=NotAssigned; |
|
246 |
}else{ |
|
247 |
State=Closed; |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
248 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
249 |
}
|
250 |
||
251 |
||
252 |
void sFile::Assign(rcsPath file_path, eTypeMode type) |
|
253 |
{
|
|
254 |
if( State == Opened ){ |
|
255 |
Close(); |
|
9.10.1
by Viktor Bursian at blin-ubuntu
Exception class tree reorganized. |
256 |
throw xProgramError("sFile::Assign - it is open" |
257 |
,__FILE__,__LINE__); |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
258 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
259 |
FullName=file_path.PathStr(); |
260 |
Type=type; |
|
261 |
if( FullName.Empty() ){ |
|
262 |
State=NotAssigned; |
|
263 |
}else{ |
|
264 |
State=Closed; |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
265 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
266 |
}
|
267 |
||
268 |
/*
|
|
269 |
FILE * fopen ( const char * filename, const char * mode );
|
|
270 |
||
271 |
Open file
|
|
272 |
Opens the file whose name is specified in the parameter filename and associates it with a stream that can be identified in future operations by the FILE object whose pointer is returned. The operations that are allowed on the stream and how these are performed are defined by the mode parameter.
|
|
273 |
The running environment supports at least FOPEN_MAX files open simultaneously; FOPEN_MAX is a macro constant defined in <cstdio>.
|
|
274 |
||
275 |
Parameters
|
|
276 |
||
277 |
filename
|
|
278 |
C string containing the name of the file to be opened. This paramenter must follow the file name specifications of the running environment and can include a path if the system supports it.
|
|
279 |
mode
|
|
280 |
C string containing a file access modes. It can be:
|
|
281 |
"r" Open a file for reading. The file must exist.
|
|
282 |
"w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
|
|
283 |
"a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
|
|
284 |
"r+" Open a file for update both reading and writing. The file must exist.
|
|
285 |
"w+" Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
|
|
286 |
"a+" Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.
|
|
287 |
||
288 |
||
289 |
With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
|
|
290 |
Additional characters may follow the sequence, although they should have no effect. For example, "t" is sometimes appended to make explicit the file is a text file.
|
|
291 |
In the case of text files, depending on the environment where the application runs, some special character conversion may occur in input/output operations to adapt them to a system-specific text file format. In many environments, such as most UNIX-based systems, it makes no difference to open a file as a text file or a binary file; Both are treated exactly the same way, but differentiation is recommended for a better portability.
|
|
292 |
For the modes where both read and writing (or appending) are allowed (those which include a "+" sign), the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a reading operation followed by a writing operation or a writing operation followed by a reading operation.
|
|
293 |
------------------------------------
|
|
294 |
void fstream::open ( const char * filename,
|
|
295 |
ios_base::openmode mode = ios_base::in | ios_base::out );
|
|
296 |
||
297 |
Open file
|
|
298 |
Opens a file whose name is s, associating its content with the stream object to perform input/output operations on it. The operations allowed and some operating details depend on parameter mode.
|
|
299 |
||
300 |
The function effectively calls rdbuf()->open(filename,mode).
|
|
301 |
||
302 |
If the object already has a file associated (open), the function fails.
|
|
303 |
||
304 |
On failure, the failbit flag is set (which can be checked with member fail), and depending on the value set with exceptions an exception may be thrown.
|
|
305 |
||
306 |
Parameters
|
|
307 |
||
308 |
filename
|
|
309 |
C-string containing the name of the file to be opened.
|
|
310 |
mode
|
|
311 |
Flags describing the requested i/o mode for the file. This is an object of type ios_base::openmode, which consists on a combination of one or more of the following flags defined as member constants:
|
|
312 |
flag value opening mode
|
|
313 |
app (append) Set the stream's position indicator to the end of the stream before each output operation.
|
|
314 |
ate (at end) Set the stream's position indicator to the end of the stream on opening.
|
|
315 |
binary (binary) Consider stream as binary rather than text.
|
|
316 |
in (input) Allow input operations on the stream.
|
|
317 |
out (output) Allow output operations on the stream.
|
|
318 |
trunc (truncate) Any current content is discarded, assuming a length of zero on opening.
|
|
319 |
||
320 |
||
321 |
||
322 |
*/
|
|
323 |
||
324 |
void sFile::Open(eOpenMode mode) |
|
325 |
{
|
|
326 |
if( State == NotAssigned ){ |
|
9.10.1
by Viktor Bursian at blin-ubuntu
Exception class tree reorganized. |
327 |
throw xProgramError("Opening an sFile before it is assigned" |
328 |
,__FILE__,__LINE__); |
|
1
by Viktor Bursian
first usable version 0.2.0 |
329 |
}else if( State == Opened ){ |
9.10.1
by Viktor Bursian at blin-ubuntu
Exception class tree reorganized. |
330 |
throw xProgramError("already opened" |
331 |
,__FILE__,__LINE__); //"File \"",FullName,"\" already opened" |
|
1
by Viktor Bursian
first usable version 0.2.0 |
332 |
}else{ |
333 |
std::ios_base::openmode flags; |
|
334 |
switch (mode) { |
|
335 |
case Reading: |
|
336 |
flags= std::ios_base::in; |
|
337 |
break; |
|
338 |
case Writing: |
|
339 |
flags= std::ios_base::out | std::ios_base::trunc; |
|
340 |
break; |
|
341 |
case Updating: |
|
342 |
flags= std::ios_base::in | std::ios_base::out; |
|
343 |
break; |
|
4.2.30
by Viktor Bursian (@black317-Ubuntu)
logging improved |
344 |
case Appending: |
345 |
flags= std::ios_base::out | std::ios_base::app; |
|
346 |
break; |
|
1
by Viktor Bursian
first usable version 0.2.0 |
347 |
default: |
9.10.1
by Viktor Bursian at blin-ubuntu
Exception class tree reorganized. |
348 |
throw xProgramError("",__FILE__,__LINE__); |
1
by Viktor Bursian
first usable version 0.2.0 |
349 |
};
|
350 |
if( Type == Binary ){ |
|
351 |
flags= flags | std::ios_base::binary; |
|
352 |
};
|
|
2
by Viktor Bursian
version 0.3.0 |
353 |
open((literal)(QFile::encodeName(ToQString(FullName))),flags); |
1
by Viktor Bursian
first usable version 0.2.0 |
354 |
if( is_open() ){ |
355 |
State=Opened; |
|
356 |
Mode=mode; |
|
357 |
}else{ |
|
358 |
Mode=mode;//DUMMY FOR DEBUG |
|
4.4.42
by Viktor Bursian at blin-Ubuntu
Небольшая чистка мусора, украшательство и пересмотр todo. |
359 |
//! @todo{env} throw xError();
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
360 |
}
|
361 |
}
|
|
1
by Viktor Bursian
first usable version 0.2.0 |
362 |
}
|
363 |
||
364 |
||
365 |
void sFile::Close() |
|
366 |
{
|
|
367 |
if( State == NotAssigned ){ |
|
9.10.1
by Viktor Bursian at blin-ubuntu
Exception class tree reorganized. |
368 |
throw xProgramError("Closing an sFile before it is assigned" |
369 |
,__FILE__,__LINE__); |
|
1
by Viktor Bursian
first usable version 0.2.0 |
370 |
}else if( State==Closed ){ |
371 |
}else{ |
|
372 |
close(); |
|
373 |
State=Closed; |
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
374 |
}
|
1
by Viktor Bursian
first usable version 0.2.0 |
375 |
}
|
376 |
||
377 |
||
378 |
//bool sFile::TryToOpen (eOpenMode mode)
|
|
379 |
//{
|
|
380 |
// try{
|
|
381 |
// Open(mode);
|
|
382 |
// return true;
|
|
383 |
// }catch(...){
|
|
384 |
// return false;
|
|
385 |
// };
|
|
386 |
//};
|
|
387 |
//
|
|
388 |
//
|
|
389 |
//bool sFile::TryToClose ()
|
|
390 |
//{
|
|
391 |
// try{
|
|
392 |
// Close();
|
|
393 |
// return true;
|
|
394 |
// }catch(...){
|
|
395 |
// return false;
|
|
396 |
// };
|
|
397 |
//};
|
|
398 |
||
399 |
||
400 |
//------------------------------------------------------------------------------
|
|
4.19.4
by Viktor Bursian at blin-ubuntu
Help engine restored |
401 |
} //namespace RA |