-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileApps_base.cpp
More file actions
211 lines (180 loc) · 6.4 KB
/
fileApps_base.cpp
File metadata and controls
211 lines (180 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// fileapps_base.cpp
//
// Class to provide general access to a stored file. Includes basic capabilites to
// open as read only, write only or both. Also provides error response from file
// system. Extends capability of the Qt library for file handling.
// -------------------
// author : Robert R. White
// date : Wed Jan 23 2008
// copyright : (C) 2008 by NREL
// email : robert.white@nrel.gov
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (C): 2008 by Robert R. White and NREL
// This file is part of the Fileapps library.
//
// Fileapps library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Fileapps library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Fileapps library. If not, see <http://www.gnu.org/licenses/>.
//
// We also ask that you maintain this copyright block and that any publications
// surrounding, attributed to, or linked to this file or entire software system are also
// credited to the author and institution of this copyright
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "fileApps_base.h"
#include <iostream>
FileApps_Base::FileApps_Base()
{
return;
}
FileApps_Base::~FileApps_Base()
{
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Performs initial file creation
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FileApps_Base::initFile (const QString filename)
{
QFile tmpFile;
tmpFile.setFileName(filename);
if (!tmpFile.exists())
{
if (accessFile(WT, filename))
{
targetFile.close();
return true;
}
else
return false;
}
else
{
std::cout << "--> " << filename.toLatin1().constData() << " already exists. Not creating a new one\n";
return true;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Opens the file for either reading, writing or appending.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FileApps_Base::accessFile(const access_types process, const QString filename)
{
targetFile.setFileName(filename);
// Open file for particular transaction type
switch (process)
{
case RD:
if (targetFile.exists())
{
if (!targetFile.open(QIODevice::ReadOnly))
{
std::cerr << "ERROR: opening file, " << filename.toLatin1().constData() << " to read\n";
std::cerr << "Error detail: " << errorString(targetFile.error() ).toLatin1().constData() << "\n";
return false;
}
}
else
{
std::cerr << "ERROR: The file, " << targetFile.fileName().toLatin1().constData() << " does not exist\n";
return false;
}
break;
case WT: //Will create file if it does not exist
if (!targetFile.open(QIODevice::WriteOnly))
{
std::cerr << "Error creating file, " << filename.toLatin1().constData() << " to write\n";
std::cerr << "Error detail: " << errorString(targetFile.error() ).toLatin1().constData() << "\n";
return false;
}
break;
case AP:
if (targetFile.exists())
{
if (!targetFile.open(QIODevice::Append))
{
std::cerr << "Error opening file, " << filename.toLatin1().constData() << " to append\n";
std::cerr << "Error detail: " << errorString(targetFile.error() ).toLatin1().constData() << "\n";
return false;
}
}
else
{
std::cerr << "ERROR: The file, " << targetFile.fileName().toLatin1().constData() << " does not exist\n";
return false;
}
break;
default:
return false;
break;
}
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Opens the file for either reading, writing or appending.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
QString FileApps_Base::errorString(const int errorValue)
{
QString message;
switch (errorValue)
{
case (QFile::NoError):
message = "No error occurred.";
break;
case (QFile::ReadError):
message = "An error occurred when reading from the file";
break;
case (QFile::WriteError):
message = "An error occurred when writing to the file.";
break;
case (QFile::FatalError):
message = "A fatal error occurred.";
break;
case (QFile::ResourceError):
message = "Resources could not be allocated or were busy";
break;
case (QFile::OpenError):
message = "The file could not be opened.";
break;
case (QFile::AbortError):
message = "The operation was aborted.";
break;
case (QFile::TimeOutError):
message = "A timeout occurred.";
break;
case (QFile::UnspecifiedError):
message = "An unspecified error occurred.";
break;
case (QFile::RemoveError):
message = "The file could not be removed.";
break;
case (QFile::RenameError):
message = "The file could not be renamed.";
break;
case (QFile::PositionError):
message = "The position in the file could not be changed.";
break;
case (QFile::ResizeError):
message = "The file could not be resized.";
break;
case (QFile::PermissionsError):
message = "The file could not be accessed.";
break;
case (QFile::CopyError):
message = "The file could not be copied.";
break;
default:
message = "Unknown file error";
break;
}
return message;
}