19#ifndef NUMERE_FILE_HPP
20#define NUMERE_FILE_HPP
28#include "../utils/zip++.hpp"
29#include "../utils/stringtools.hpp"
30#include "../ui/error.hpp"
31#include "../datamanagement/tablecolumn.hpp"
96 void open(std::ios::openmode mode)
119 if (_sToStrip.find_first_not_of(
" \t") != std::string::npos)
120 _sToStrip.erase(_sToStrip.find_last_not_of(
" \t")+1);
134 for (
size_t i = 0; i < _sToReplace.length(); i++)
136 if (_sToReplace[i] ==
',')
137 _sToReplace[i] =
'.';
159 bool bKeepColumns =
false;
162 if (!bAddPlaceholders && _sToReplace.find(
' ') == std::string::npos)
166 if (_sToReplace[0] ==
'\t')
167 _sToReplace.insert(0,
"---");
170 for (
size_t i = 0; i < _sToReplace.length(); i++)
172 if (_sToReplace[i] ==
'\t')
174 _sToReplace.replace(i, 1,
" ");
177 if (bAddPlaceholders)
181 _sToReplace.insert(0, 1,
'_');
182 else if (_sToReplace[i-1] ==
' ')
183 _sToReplace.insert(i, 1,
'_');
185 if (i+2 == _sToReplace.length())
190 if (bKeepColumns && (i+2 == _sToReplace.length() || _sToReplace[i+2] ==
'\t'))
191 _sToReplace.insert(i + 2,
"---");
198 if (bAddPlaceholders && _sToReplace.find(
" ") != std::string::npos)
200 for (
size_t i = 1+(_sToReplace.front() ==
'#'); i < _sToReplace.length()-1; i++)
202 if (_sToReplace[i] ==
' ' && _sToReplace[i-1] !=
' ' && _sToReplace[i+1] !=
' ')
203 _sToReplace[i] =
'\1';
224 std::pair<size_t, size_t> pCellExtents(0u, 1u);
225 size_t nLastLineBreak = 0;
228 for (
size_t i = 0; i < sContents.length(); i++)
231 if (sContents[i] ==
'\n')
234 pCellExtents.second++;
238 if (i - nLastLineBreak > pCellExtents.first)
239 pCellExtents.first = i - nLastLineBreak;
259 if (sContents.length() - nLastLineBreak > pCellExtents.first)
260 pCellExtents.first = sContents.length() - nLastLineBreak;
279 size_t nLastLineBreak = 0u;
289 std::string sHeadLine =
fileData->at(nCol)->m_sHeadLine;
292 for (
size_t i = 0; i < sHeadLine.length(); i++)
295 if (sHeadLine[i] ==
'\n')
300 return sHeadLine.substr(nLastLineBreak, i - nLastLineBreak);
305 nLastLineBreak = i+1;
324 return sHeadLine.substr(nLastLineBreak);
355 size_t nLength = readNumField<size_t>();
363 char* buffer =
new char[nLength];
369 std::string sBuffer(buffer, nLength);
398 std::string sFileItem = _zip->
getZipItem(filename);
419 size = readNumField<long long int>();
426 T* data =
new T[size];
444 template <
typename T> T**
readDataArray(
long long int& rows,
long long int& cols)
447 rows = readNumField<long long int>();
448 cols = readNumField<long long int>();
455 T** data =
new T*[rows];
459 for (
long long int i = 0; i < rows; i++)
461 data[i] =
new T[cols];
479 size = readNumField<long long int>();
486 std::string* data =
new std::string[size];
490 for (
long long int i = 0; i < size; i++)
509 std::vector<std::string> vTextFile;
510 std::string currentLine;
524 if (!stripEmptyLines || currentLine.length())
525 vTextFile.push_back(currentLine);
544 std::vector<std::string>
tokenize(std::string sString,
const std::string& sSeparators,
bool skipEmptyTokens =
false)
546 std::vector<std::string> vTokens;
549 while (sString.length())
552 vTokens.push_back(sString.substr(0, sString.find_first_of(sSeparators)));
556 if (skipEmptyTokens && !vTokens.back().length())
561 if (sString.find_first_of(sSeparators) != std::string::npos)
562 sString.erase(0, sString.find_first_of(sSeparators)+1);
594 writeNumField<size_t>(sString.length());
595 fFileStream.write(sString.c_str(), sString.length());
611 writeNumField<long long int>(size);
626 template <
typename T>
void writeDataArray(T** data,
long long int rows,
long long int cols)
629 writeNumField<long long int>(rows);
630 writeNumField<long long int>(cols);
633 for (
long long int i = 0; i < rows; i++)
649 writeNumField<long long int>(size);
653 for (
long long int i = 0; i < size; i++)
717 template <
typename T>
void copyDataArray(T** from, T** to,
long long int rows,
long long int cols)
719 if (!from || !to || !rows || !cols)
722 for (
long long int i = 0; i < rows; i++)
724 for (
long long int j = 0; j < cols; j++)
725 to[i][j] = from[i][j];
742 if (!from || !to || !nElements)
745 for (
long long int i = 0; i < nElements; i++)
761 template <
typename T>
void copyArray(T* from, T* to,
long long int nElements)
763 if (!from || !to || !nElements)
766 for (
long long int i = 0; i < nElements; i++)
781 if (!sString.length())
815 for (
long long int col = 0; col <
nCols; col++)
986 for (
size_t i = 0; i <
sTableName.length(); i++)
1117 for (
long long int col = 0; col <
nCols; col++)
1120 data->at(col).reset(
fileData->at(col)->copy());
1212 for (
long long int col = 0; col <
nCols; col++)
1213 fileData->at(col).reset(data->at(col)->copy());
1361 return arr->at(col)->getValue(row);
1389 return arr->at(col)->getValueAsInternalString(row);
1416 return arr->at(col)->m_sHeadLine;
1439 void writeTableHeads(
const std::vector<size_t>& vColumnWidth,
size_t nNumberOfLines);
1441 void addSeparator(
const std::vector<size_t>& vColumnWidth);
1443 void decodeTableHeads(std::vector<std::string>& vFileContents,
long long int nComment);
1525 open(std::ios::in | std::ios::binary);
1627 vFileIndex = std::vector<size_t>(nTables, 0u);
1690 char findSeparator(
const std::vector<std::string>& vTextData);
1691 void countColumns(
const std::vector<std::string>& vTextData,
char& cSep);
1759 size_t readTable(std::vector<std::string>& vFileContents,
size_t nTableStart,
MetaData);
1761 std::vector<double>
parseLine(
const std::string& sLine);
1764 JcampDX(
const std::string& filename);
1792 std::string
expandLine(
const std::string& sLine);
1853 void evalIndices(
const std::string& sIndices,
int& nLine,
int& nCol);
1932 ZygoDat(
const std::string& filename);
This class implements the basic input/ output file system and provides functionalities to work with f...
std::vector< std::string > getFileParts(const std::string &sFilePath) const
This member function separates all path parts into single strings: the drive letter,...
void initializeFromKernel()
Member function to remote-initialize the class from the kernel. Cannot be used during kernel start-up...
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...
This class resembles the cache file used to autosave and recover the tables in memory....
CacheFile(const std::string &filename)
void readCacheHeader()
This member function will read the cache file header and ensure that the version of the file is not n...
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
void reset()
This member function will reset the string information and the internal storage. This is used before ...
void writeSome()
This member function will write the current contents in the internal storage to the target file....
void setNumberOfTables(size_t nTables)
Sets the number of tables to be stored in the referenced cache file.
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
std::vector< size_t > vFileIndex
size_t getPosition(size_t nthTable)
Returns the character position of the passed table index.
void readSome()
This member function will read the next table, which is availale in the cache file,...
void writeCacheHeader()
This member function will write the standard cache file header to the cache file.
size_t getNumberOfTables()
Returns the number of tables stored in the referenced cache file.
virtual ~CacheFile()
This destructor will write the offsets for the different tables to the file before the file stream wi...
This class resembles the CASSYLab *.labx file format, which is based upon XML. Only reading from this...
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
void readFile()
This member function will read the contents of the associated LABX file to the internal storage.
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
CassyLabx(const std::string &filename)
double extractValueFromTag(const std::string &sTag)
This simple member function extracts the numerical value of the XML tag string.
This class resembles a comma separated value file format (*.csv). The algorithm may detect the separa...
char findSeparator(const std::vector< std::string > &vTextData)
This member function determines the separator character used for the current file name....
virtual ~CommaSeparatedValues()
void readFile()
This member function is used to read the target file to memory.
void writeFile()
This member function is used to write the contents in the internal storage to the target file.
void countColumns(const std::vector< std::string > &vTextData, char &cSep)
This member function determines the number of columns available in the current file and alters the se...
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
CommaSeparatedValues(const std::string &filename)
Template class representing a generic file. This class may be specified for the main data type contai...
TableColumnArray * getData(long long int &rows, long long int &cols)
This method returns a pointer to the internal memory with read and write access. This pointer shall n...
void writeStringField(const std::string &sString)
This method may be used to write a string to file in binary mode.
void getData(TableColumnArray *data)
This method copies the internal data to the passed memory address. The target memory must already exi...
unsigned short nPrecFields
void writeNumBlock(T *data, long long int size)
This method template may be used to write a block of data of the selected type to the file in binary ...
void setTextfilePrecision(unsigned short nPrecision)
Set the precision, which shall be used to convert the floating point numbers into strings.
void replaceDecimalSign(std::string &_sToReplace)
This method simply replaces commas with dots in the passed string to enable correct parsing into a do...
void writeNumField(T num)
This method template can be used to write a numeric value to file in binary mode.
T readNumField()
This method is a template fo reading a numeric field of the selected template type in binary mode.
void close()
Wrapper for fstream::close(). Will also clear the internal memory.
std::string * readStringBlock(long long int &size)
This method can be used for reading a block of string data to memory in binary mode.
virtual ~GenericFile()
Virtual destructor: we'll work with instances on the heap, therefore we'll need virtual declared dest...
void setTableName(const std::string &name)
Set the table's name.
void writeStringBlock(std::string *data, long long int size)
This method may be used to write a block of strings into the file in binary mode.
virtual bool read()=0
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
std::string getTableName()
Returns the table name referenced in the file. Will default to the file name with non-alnum character...
bool isNumeric(const std::string &sString)
This method may be used to determine, whether a string contains only numeric data.
size_t tellg()
Wrapper for fstream::tellg()
size_t tellp()
Wrapper for fstream::tellp()
void writeDataArray(T **data, long long int rows, long long int cols)
This method may be used to write a two-dimensional array of data to the file in binary mode.
void seekp(size_t pos)
Wrapper for fstream::seekp() with start from the beginning of the stream.
std::string getComment()
Returns the comment stored with the referenced file.
void copyArray(T *from, T *to, long long int nElements)
This method template may be used to copy arrays of data of the selected type. Both source and target ...
std::pair< size_t, size_t > calculateCellExtents(const std::string &sContents)
This method calculates the extents of the passed string, if it is used as a table column headlines....
GenericFile & operator=(const GenericFile &file)
Assignment operator definition.
void setData(TableColumnArray *data, long long int rows, long long int cols)
This method refernces the passed external data internally. The data is not copied and must exist as l...
std::string getZipFileItem(const std::string &filename)
This method may be used to get the contents of an embedded file in a zipfile and return the contents ...
void copyDataArray(T **from, T **to, long long int rows, long long int cols)
This method may be used to copy two- dimensional arrays of data. Both source and target arrays have t...
void replaceTabSign(std::string &_sToReplace, bool bAddPlaceholders=false)
This method replaces tabulator characters with whitespaces to simplify the column determination (the ...
GenericFile(const GenericFile &file)
Copy constructor.
std::string getLineFromHead(long long int nCol, size_t nLineNumber)
This method gets the selected line number from the table column headline in the selected column....
bool good()
Wrapper for fstream::good()
void addData(TableColumnArray *data, long long int rows, long long int cols)
This method created the internal storage and copies the passed data to this storage.
void clearStorage()
This method cleares the internal storage. This method is called by the destructor automatically.
virtual FileHeaderInfo getFileHeaderInformation()
Returns the file header information structure.
bool is_open()
Wrapper for fstream::is_open()
std::ios::openmode openMode
std::string sFileExtension
TableColumnArray * fileData
std::vector< std::string > tokenize(std::string sString, const std::string &sSeparators, bool skipEmptyTokens=false)
This method may be used to separater a line into multiple tokens using a set of separator characters....
GenericFile(const std::string &fileName)
Constructor from filename.
long long int getRows()
Returns the number of rows.
virtual bool write()=0
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
void setDimensions(long long int rows, long long int cols)
Sets the dimensions of the data table, which will be used in the future. Clears the internal memory i...
void setComment(const std::string &comment)
Sets the comment to be written to the referencedfile.
std::string readStringField()
This mehtod can be used to read a string field from the file in binary mode.
void copyStringArray(std::string *from, std::string *to, long long int nElements)
This method may be used to copy string arrays. Both source and target arrays have to exist in advance...
void open(std::ios::openmode mode)
This method has to be used to open the target file in stream mode. If the file cannot be opened,...
T * readNumBlock(long long int &size)
This method template is for reading a block of numeric data into memory in binary mode.
std::string getFileName()
Returns the file name.
void createStorage()
This method prepares the internal storage, so that it may contain the read data. This method is only ...
void assign(const GenericFile &file)
This method is used by the assignment operator and the copy constructor to copy the contents of the p...
std::vector< std::string > readTextFile(bool stripEmptyLines)
This method may be used to read the file in text mode and to obtain the data as a vector.
void seekg(size_t pos)
Wrapper for fstream::seekg() with start from the beginning of the stream.
T ** readDataArray(long long int &rows, long long int &cols)
This method template is for reading a whole two-dimensional array of data into memory in binary mode.
long long int getCols()
Returns the number of columns.
std::string getExtension()
Returns the file extension.
void stripTrailingSpaces(std::string &_sToStrip)
This method strips trailing spaces from the passed string.
This class is a facet for an arbitrary GenericFile instance. It can be used to read the contents of t...
mu::value_type getElement(long long int row, long long int col) const
Returns the value stored at the passed positions. A default constructed mu::value_type object instanc...
GenericFile * getPtr()
Returns the internally stored GenericFile instance pointer.
long long int getCols() const
Returns the number of columns in the internally stored GenericFile instance.
void attach(GenericFile *_file)
Attaches a new GenericFile instance to this facet class.
GenericFileView(GenericFile *_file)
Constructor from an available GenericFile instance.
std::string getColumnHead(long long int col) const
Returns the column heading stored for the passed column. Returns an empty string, if the column does ...
long long int getRows() const
Returns the number of rows in the internally stored GenericFile instance.
std::string getStringElement(long long int row, long long int col) const
Returns the value stored at the passed positions. An empty string is returned, if the element does no...
GenericFileView()
Default constructor.
This class resembles an Igor binary wave file format (*.ibw). The data is read by the WaveMetrics imp...
void useXZSlicing()
Activates the XZ-slicing of the Igor binary wave, which is used to roll out 3D data in 2D slices.
IgorBinaryWave & operator=(const IgorBinaryWave &file)
This is an overload for the assignment operator of the GenericFile class.
void readFile()
This member function is used to read the contents of the IBW file into the internal storage....
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
virtual ~IgorBinaryWave()
IgorBinaryWave(const std::string &filename)
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
This class resembles a JCAMP-DX file format (*.jcm, *.jdx, *.dx). The data in this format may be hash...
size_t readTable(std::vector< std::string > &vFileContents, size_t nTableStart, MetaData)
Reads a single table from the currently opened JCAMP-DX file.
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
std::vector< double > parseLine(const std::string &sLine)
This member function parses the current data line into numerical values by decoding the JCAMP-DX enco...
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
void readFile()
This member function is used to read the conents of the JCAMP-DX file to the internal storage.
void parseLabel(std::string &sLine)
This member function parses JCAMP-DX labels by removing whitespaces, minus characters and underscores...
JcampDX(const std::string &filename)
This class resembles a LaTeX table. This class formats the data into this format using some heuristic...
LaTeXTable(const std::string &filename)
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
std::string replaceNonASCII(const std::string &sText)
This member function replaces all non- ASCII characters into their corresponding LaTeX entities.
std::string formatNumber(const mu::value_type &number)
This member function formats a complex as LaTeX number string.
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
void writeFile()
This member function is used to write the contents of the internal storage to the file.
void writeHeader()
This member function writes the legal header to the file.
void writeTableHeads()
This member function writes the table column heads to the file. The number of lines needed for the he...
size_t countHeadLines()
This member function calculates the number of lines needed for the complete table column heads.
This class resembles the binary NumeRe data file format. The data is red and written in binary mode u...
void writeDummyHeader()
This member function will write the dummy header, which is readable in older versions of NumeRe....
std::string getVersionString()
This simple member function returns the version string associated with the current file type.
const short fileSpecVersionMinor
NumeReDataFile(const std::string &filename)
void readColumnV4(TblColPtr &col)
Reads a single column from file in v4 format.
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
void * readGenericField(std::string &type, long long int &size)
This member function will read a generic field from the header (the three fields, which can be used i...
void writeHeader()
This member function writest the new standard header for NDAT files. It includes a dummy section,...
void skipDummyHeader()
This function jumps over the dummy section in the new file format, because it does not conatin any va...
void readFile()
This member function will read the contents of the target file.
__time64_t getTimeStamp()
Returns the file timestamp.
void readFileInformation()
Reads only the header of the referenced file.
void deleteGenericData(void *data, const std::string &type)
This member function will delete the data array obtained from the generic fields but convert them int...
const short fileSpecVersionMajor
virtual ~NumeReDataFile()
void readLegacyFormat()
This member function reads the data section of the target file in legacy format. The function readHea...
void writeFile()
This member function will write the data in the internal storage into the target file.
void writeColumn(const TblColPtr &col)
Writes a single column to the file.
void readHeader()
This member function will read the header in the selected file. It will automatically detect,...
virtual FileHeaderInfo getFileHeaderInformation() override
Returns the file header information structure.
void readColumn(TblColPtr &col)
Reads a single column from file.
NumeReDataFile & operator=(NumeReDataFile &file)
This member function is an overload for the assignment operator. It extends the already available ass...
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
This class resembles an OpenDocument spreadsheet (*.ods), which is based upon a zipped XML file....
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
std::string expandLine(const std::string &sLine)
This member function is used by the readFile() member function to expand the XML- based table row str...
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
void readFile()
This member function is used to read the targed file into the internal storage. ODS is a ZIP file con...
OpenDocumentSpreadSheet(const std::string &filename)
virtual ~OpenDocumentSpreadSheet()
This class resembles an arbitrary text data file, which is formatted in a table-like manner....
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
void writeTableHeads(const std::vector< size_t > &vColumnWidth, size_t nNumberOfLines)
This member function is used to write the table heads into the target file.
void readFile()
This method reads the data in the referenced text file to memory.
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
TextDataFile(const std::string &filename)
void writeHeader()
This member function writes the header lines or the text files.
void writeTableContents(const std::vector< size_t > &vColumnWidth)
This member function is used to write the data in memory to the target text file.
std::vector< size_t > calculateColumnWidths(size_t &nNumberOfLines)
This member function calculates the widths of the columns and also determines the number of lines nee...
void addSeparator(const std::vector< size_t > &vColumnWidth)
This member function draws a separator based upon the overall width of the columns.
void writeFile()
This method writes the data in memory to the referenced text file.
void decodeTableHeads(std::vector< std::string > &vFileContents, long long int nComment)
This member function decodes the table heads in the text file and stores them in memory.
This class resembles an Excel (97) workbook (*.xls), which is composed out of a compound file....
virtual ~XLSSpreadSheet()
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
void writeFile()
This member function is used to write the data in the internal storage to the target XLS spreadsheet.
XLSSpreadSheet(const std::string &filename)
void readFile()
This member function is used to read the data from the XLS spreadsheet ino the internal storage.
This class resembles an Excel (2003) spreadsheet (*.xlsx), which is based upon a zipped XML file....
XLSXSpreadSheet(const std::string &filename)
virtual ~XLSXSpreadSheet()
void evalIndices(const std::string &sIndices, int &nLine, int &nCol)
This member function converts the usual Excel indices into numerical ones.
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
void readFile()
This member function is used to read the data from the XLSX spreadsheet into the internal storage....
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
This class implements a Zygo MetroPro binary dat file. The data is read by accessing the ZygoLib.
void readFile()
This member function is used to read the contents of the Zygo Dat file into the internal storage....
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
ZygoDat(const std::string &filename)
ZygoDat & operator=(const ZygoDat &file)
This is an overload for the assignment operator of the GenericFile class.
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
Common exception class for all exceptions thrown in NumeRe.
@ DATAFILE_NOT_EXIST
INSERT HERE.
static size_t invalid_position
bool open(const std::string &sZipFilename, int nOpenmode=0)
std::string getZipItem(const std::string &sFilename)
GenericFile * getFileByType(const string &filename)
This function determines the correct class to be used for the filename passed to this function....
MUP_BASETYPE value_type
The numeric datatype used by the parser.
static std::string getDefaultColumnHead(size_t colNo)
Creates a default column headline for a column, which can be used without an instance of this class.
std::unique_ptr< TableColumn > TblColPtr
Typedef for simplifying the usage of a smart pointer in combination with a TableColumn instance.
std::vector< TblColPtr > TableColumnArray
This typedef represents the actual table, which is implemented using a std::vector.