NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
fileadapter.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2020 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19#include "fileadapter.hpp"
20#include "../utils/tools.hpp"
21#include "../io/logger.hpp"
22#include "memory.hpp"
23
24using namespace std;
25
26namespace NumeRe
27{
32 {
33 bLoadEmptyCols = false;
35 sOutputFile = "";
36 sDataFile = "";
37 sPrefix = "data";
38 sSavePath = "<savepath>";
39 }
40
41
51 {
52 return getTimeStamp(true);
53 }
54
55
67 {
68 if (!_mem || !_mem->isValid())
69 return;
70
71 // Shall we ignore empty columns?
73 {
75 return;
76 }
77
78 // Get an iterator to the beginning
79 auto iter = _mem->memArray.begin();
80
81 // Remove all empty columns
82 while (iter != _mem->memArray.end())
83 {
84 if (!iter->get() || !iter->get()->size())
85 iter = _mem->memArray.erase(iter);
86 else
87 ++iter;
88 }
89 }
90
91
108 FileHeaderInfo FileAdapter::openFile(std::string _sFile, bool loadToCache, bool overrideTarget, int _nHeadline, const std::string& sTargetTable)
109 {
110 FileHeaderInfo info;
111
112 // Ensure that the file name is valid
113 std::string sFile = ValidFileName(_sFile);
114
115 // If the file seems not to exist and the user did
116 // not provide the extension, try to detect it using
117 // wildcard
118 if (!fileExists(sFile) && (_sFile.find('.') == string::npos || _sFile.find('.') < _sFile.rfind('/')))
119 sFile = ValidFileName(_sFile+".*");
120
121 g_logger.info("Loading file '" + _sFile + "'.");
122
123 // Get an instance of the desired file type
124 GenericFile* file = getFileByType(sFile);
125
126 // Ensure that the instance is valid
127 if (!file)
129
130 // Try to read the contents of the file. This may
131 // either result in a read error or the read method
132 // is not defined for this function
133 try
134 {
135 // Igor binary waves might contain three-dimensional
136 // waves. We select the roll-out mode in this case
137 if (file->getExtension() == "ibw" && _nHeadline == -1)
138 static_cast<IgorBinaryWave*>(file)->useXZSlicing();
139
140 // Read the file
141 if (!file->read())
143 }
144 catch (...)
145 {
146 delete file;
147 throw;
148 }
149
150 g_logger.debug("File read. Preparing memory.");
151
152 // Get the header information structure
153 info = file->getFileHeaderInformation();
154
155 if (!info.nCols || !info.nRows)
156 {
157 delete file;
159 }
160
161 Memory* _mem = new Memory();
162 _mem->resizeMemory(info.nRows, info.nCols);
163
164 // If the dimensions were valid and the internal
165 // memory was created, copy the data to this
166 // memory
167 if (_mem->memArray.size())
168 {
169 // Copy them and delete the file instance
170 // afterwards
171 file->getData(&_mem->memArray);
172 delete file;
173 g_logger.debug("Data copied.");
174 _mem->convert();
175 g_logger.debug("Data converted.");
176 _mem->shrink();
177 condenseDataSet(_mem);
178 _mem->createTableHeaders();
179 _mem->setSaveStatus(false);
180
182 meta.comment = info.sComment;
183 meta.source = sFile;
184
185 _mem->setMetaData(meta);
186
187 // Melt or append the new instance. The
188 // melt() member function is responsible
189 // for freeing the passed memory.
190 if (loadToCache)
191 {
192 if (sTargetTable.length())
193 {
194 melt(_mem, sTargetTable, overrideTarget);
195 info.sTableName = sTargetTable;
196 }
197 else
198 melt(_mem, info.sTableName, overrideTarget);
199 }
200 else
201 melt(_mem, "data");
202
203 g_logger.info("File sucessfully loaded. Data file dimensions = {" + toString(info.nRows) + ", " + toString(info.nCols) + "}");
204 }
205 else
206 {
207 delete file;
208 delete _mem;
210 }
211
212 if (!loadToCache)
213 {
214 if (sDataFile.length())
215 sDataFile += ";" + sFile;
216 else
217 sDataFile = sFile;
218 }
219
220 // Return the file information header for
221 // further processing
222 return info;
223 }
224
225
238 bool FileAdapter::saveFile(const std::string& sTable, std::string _sFileName, unsigned short nPrecision)
239 {
240 if (!_sFileName.length())
242 else
243 {
244 string sTemp = sPath;
246
247 sOutputFile = ValidFileName(_sFileName, ".ndat");
248 setPath(sTemp, false, sExecutablePath);
249 }
250
251 return saveLayer(sOutputFile, sTable, nPrecision);
252 }
253
254
264 std::string FileAdapter::getDataFileName(const std::string& sTable) const
265 {
266 if (sTable != "data")
267 return sTable;
268
269 return sDataFile;
270 }
271
272
283 {
284 return shortenFileName(getDataFileName("data"));
285 }
286
287
297 {
298 return sOutputFile;
299 }
300
301
310 void FileAdapter::setSavePath(const std::string& _sPath)
311 {
312 sSavePath = _sPath;
313 }
314
315
324 void FileAdapter::setPrefix(const std::string& _sPrefix)
325 {
326 sPrefix = _sPrefix;
327 }
328
329
338 void FileAdapter::setbLoadEmptyCols(bool _bLoadEmptyCols)
339 {
340 bLoadEmptyCols = _bLoadEmptyCols;
341 }
342
343
353 {
354 bLoadEmptyColsInNextFile = _bLoadEmptyCols;
355 }
356
357
366 std::string FileAdapter::generateFileName(const std::string& sExtension)
367 {
368 string sTime;
369
370 if (sSavePath.find('"') != string::npos)
371 sTime = sSavePath.substr(1, sSavePath.length()-2);
372 else
373 sTime = sSavePath;
374
375 while (sTime.find('\\') != string::npos)
376 sTime[sTime.find('\\')] = '/';
377
378 sTime += "/" + sPrefix + "_"; // Prefix laden
379 sTime += getDate(); // Datum aus der Klasse laden
380 sTime += sExtension;
381 return sTime;
382 }
383}
384
void info(const std::string &sMessage)
Convenience member function.
Definition: logger.hpp:106
void debug(const std::string &sMessage)
Convenience member function.
Definition: logger.hpp:94
This class implements the basic input/ output file system and provides functionalities to work with f...
Definition: filesystem.hpp:92
std::string sPath
Definition: filesystem.hpp:98
std::string sExecutablePath
Definition: filesystem.hpp:99
std::string ValidFileName(std::string _sFileName, const std::string sExtension=".dat", bool checkExtension=true, bool doCleanPath=true) const
This member function evaluates, whether the passed filename is a valid filename. One may supply a pre...
Definition: filesystem.cpp:280
int setPath(std::string _sPath, bool bMkDir, std::string _sExePath)
This member function may be used to set the preferred file path of the current FileSystem instance.
Definition: filesystem.cpp:443
This class represents a single table in memory, or a - so to say - single memory page to be handled b...
Definition: memory.hpp:68
TableColumnArray memArray
Definition: memory.hpp:89
bool isValid() const
Returns true, if at least a single valid value is available in this table.
Definition: memory.cpp:833
void setSaveStatus(bool _bIsSaved)
This member function changes the saved state to the passed value.
Definition: memory.cpp:1518
bool resizeMemory(size_t _nLines, size_t _nCols)
This member function will handle all memory grow operations by doubling the base size,...
Definition: memory.cpp:221
bool shrink()
This member function shrinks the table memory to the smallest possible dimensions reachable in powers...
Definition: memory.cpp:847
void setMetaData(const NumeRe::TableMetaData &meta)
Update the internal meta data with the passed one.
Definition: memory.cpp:1109
void createTableHeaders()
This member function creates the column headlines, if they are empty.
Definition: memory.cpp:121
void convert()
This member function tries to convert all string columns to value columns, if it is possible.
Definition: memory.cpp:888
std::string getDataFileName(const std::string &sTable) const
This member function will return the file name of the selected table. Will default to the table name.
std::string getDate()
This private member function will return the current date as a timestamp for the file name.
Definition: fileadapter.cpp:50
std::string getOutputFileName() const
This member function will return the output file name, which was used for saving the last table.
FileAdapter()
FileAdapted default constructor.
Definition: fileadapter.cpp:31
void setbLoadEmptyColsInNextFile(bool _bLoadEmptyCols)
Set, whether empty columns shall be loaded in the next file.
std::string sOutputFile
Definition: fileadapter.hpp:41
FileHeaderInfo openFile(std::string _sFile, bool loadToCache=false, bool overrideTarget=false, int _nHeadline=0, const std::string &sTargetTable="")
This member function loads the contents of the selected file to a new Memory class instance....
virtual bool saveLayer(std::string _sFileName, const std::string &_sCache, unsigned short nPrecision)=0
std::string sPrefix
Definition: fileadapter.hpp:42
bool saveFile(const std::string &sTable, std::string _sFileName, unsigned short nPrecision=7)
This member function wraps the saving functionality of the Memory class. The passed filename is evalu...
void setSavePath(const std::string &_sPath)
This function may be used to update the target saving path of this class.
virtual void melt(Memory *_mem, const std::string &sTable, bool overrideTarget=false)=0
std::string getDataFileNameShort() const
This member function will return a shortened version of the data file name, where each "/Path/" strin...
void setbLoadEmptyCols(bool _bLoadEmptyCols)
Set, whether empty columns shall be loaded.
std::string sDataFile
Definition: fileadapter.hpp:44
void condenseDataSet(Memory *_mem)
This member function will condense the data set in the passed Memory instance, i.e....
Definition: fileadapter.cpp:66
void setPrefix(const std::string &_sPrefix)
This function is used to set a file prefix for the saving file name.
std::string generateFileName(const std::string &sExtension=".ndat")
This member function creates a file name from the file prefix and the time stamp.
std::string sSavePath
Definition: fileadapter.hpp:43
Template class representing a generic file. This class may be specified for the main data type contai...
Definition: file.hpp:68
void getData(TableColumnArray *data)
This method copies the internal data to the passed memory address. The target memory must already exi...
Definition: file.hpp:1113
virtual bool read()=0
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
virtual FileHeaderInfo getFileHeaderInformation()
Returns the file header information structure.
Definition: file.hpp:1055
std::string getExtension()
Returns the file extension.
Definition: file.hpp:951
This class resembles an Igor binary wave file format (*.ibw). The data is read by the WaveMetrics imp...
Definition: file.hpp:1881
Common exception class for all exceptions thrown in NumeRe.
Definition: error.hpp:32
@ CANNOT_READ_FILE
Definition: error.hpp:75
static size_t invalid_position
Definition: error.hpp:235
bool fileExists(const string &)
This function checks, whether the file with the passed file name exists.
Definition: tools.cpp:2500
DetachedLogger g_logger
Definition: logger.cpp:23
GenericFile * getFileByType(const string &filename)
This function determines the correct class to be used for the filename passed to this function....
Definition: file.cpp:45
std::string getTimeStamp(bool bGetStamp)
This function simple returns the current time as a default timestamp.
This structure wraps all necessary meta information of a single file.
Definition: file.hpp:41
long long int nCols
Definition: file.hpp:47
std::string sTableName
Definition: file.hpp:44
std::string sComment
Definition: file.hpp:45
long long int nRows
Definition: file.hpp:46
Encapsulating structure to gather all table meta data information.
Definition: table.hpp:32
std::string source
Definition: table.hpp:34
std::string comment
Definition: table.hpp:33
std::string toString(int)
Converts an integer to a string without the Settings bloat.
std::string shortenFileName(const std::string &sFullFileName)
This function will return a shortened version of the data file name, where each "/Path/" string part ...
Definition: tools.cpp:3953