MeVisLab Toolbox Reference
mlByteDataReader.h
Go to the documentation of this file.
1 // Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
2 // **InsertLicense** code
3 //----------------------------------------------------------------------------------
5 
10 //----------------------------------------------------------------------------------
11 
12 #pragma once
13 
14 #include "MLReleaseToolsSystem.h"
15 #include "mlModuleIncludes.h"
16 
17 ML_START_NAMESPACE
18 
19 //----------------------------------------------------------------------------------
22 //----------------------------------------------------------------------------------
24  public:
25 
28 
30  ByteDataReader(const std::string &fileName);
31 
34  virtual ~ByteDataReader();
35 
37  virtual std::string getFileName() const;
38 
40  virtual void setFileName(const std::string &fileName);
41 
45  virtual void open();
46 
48  virtual bool isOpen() const;
49 
51  virtual void close();
52 
54  virtual bool isGood() const;
55 
57  virtual std::string getErrorState() const;
58 
60  virtual void clearErrorState();
61 
63  virtual void setIndent(const std::string &indent);
64 
66  virtual const std::string &getIndent() const;
67 
70  bool needsByteSwapping() const;
71 
76  virtual void prefetch();
77 
79  virtual const MLuint8* getPrefetchData() const;
80 
82  virtual void clearPrefetchData();
83 
86 
89 
92 
95 
98 
101 
104 
107  std::string getNonTerminatedDataAsString(MLuint originPosition,
108  MLuint numChars) const;
109 
113  MLuint8* getDataChunk(MLuint64 pos, MLuint64 numBytes) const;
114 
118  const MLuint8* getPrefetchDataChunk(MLuint64 pos, MLuint64 numBytes) const;
119 
129  virtual std::string getAsString(const std::string &prefix="",
130  const std::string &sep=", ",
131  const size_t maxNumBinaryEntries=8,
132  const size_t maxNumStringEntries=20) const;
133 
136 
138  void appendStringToState(const std::string &errDesc) const;
139 
140  protected:
141 
143  int _getFileDescriptor() const;
144 
147  void _appendErrNoToState(const std::string &errDesc) const;
148 
151  void _setNeedsByteSwapping(bool needsSwapping);
152 
153  private:
154 
157  mutable std::string _errorStateString;
158 
160  std::string _fileName;
161 
163  int _fileDescriptor;
164 
166  std::string _indent;
167 
169  bool _needsByteSwapping;
170 
173  MLuint8 *_prefetchData;
174 
177  MLuint64 _prefetchedDataInBytes;
178 
181 };
182 
184 MLRELEASE_TOOLS_EXPORT std::ostream& operator<<(std::ostream& os, const ByteDataReader &obj);
185 
186 ML_END_NAMESPACE
Project global and OS specific declarations.
#define MLRELEASE_TOOLS_EXPORT
DLL export macro definition.
Base class to implement raw data loaders which need byte swapping and basic file access on byte or ra...
MLuint32 get32BitUWord(MLuint64 pos) const
Read an unsigned 32 bit word from position pos.
void _setNeedsByteSwapping(bool needsSwapping)
To se set to true in open() by derived classes if raw file data needs byte swapping,...
bool needsByteSwapping() const
Returns true if raw file data needs byte swapping, otherwise false; needs to be set with _setNeedsByt...
MLuint64 get64BitUWord(MLuint64 pos) const
Read an unsigned 64 bit word from position pos.
virtual std::string getErrorState() const
Returns the current state string which is non empty on any error, empty otherwise.
MLint getFileSizeFromCurrentDescriptor()
Returns MLFileGetSizeFromDescriptor(_getFileDescriptor()).
void _appendErrNoToState(const std::string &errDesc) const
Appends the given string as error description, translates errno to an MLErrorCode an also adds that s...
MLuint16 get16BitUWord(MLuint64 pos) const
Read an unsigned 16 bit word from position pos.
void appendStringToState(const std::string &errDesc) const
Appends the given string to error state string (which also sets bad read state).
virtual void open()
Opens the file if possible, isGood() will return true on success, otherwise false; if byte swapping m...
std::string getNonTerminatedDataAsString(MLuint originPosition, MLuint numChars) const
Return a non-null terminated data area starting at position originPosition of length numChars as std:...
virtual const std::string & getIndent() const
The default indentation string for tag dumps.
virtual void clearErrorState()
Resets the error state to success, i.e. to an empty string.
MLuint16 get16BitUWordInLocalEndianess(MLuint64 pos) const
Read an unsigned 16 bit word from position pos and convert it in local endianness.
virtual void clearPrefetchData()
If any data is in the prefetch data memory then it is freed.
MLuint8 get8BitUWord(MLuint64 pos) const
Read an unsigned 8 bit word from position pos.
virtual bool isGood() const
Returns true if the recent operations since the last clearState() call were successful.
ByteDataReader()
Default constructor which gets its settings later.
ByteDataReader(const std::string &fileName)
Constructor already setting the file name.
virtual void setIndent(const std::string &indent)
Sets the default indentation string for tag dumps.
const MLuint8 * getPrefetchDataChunk(MLuint64 pos, MLuint64 numBytes) const
Returns a data pointer to position pos with size numBytes in the prefetch data if available and if ch...
virtual bool isOpen() const
Returns true if the file is open, otherwise false.
virtual ~ByteDataReader()
Destructor (automatically closes the current open file if there is any and clear allocated subimage d...
virtual std::string getAsString(const std::string &prefix="", const std::string &sep=", ", const size_t maxNumBinaryEntries=8, const size_t maxNumStringEntries=20) const
Convert content to a human readable string with the prefix prefix before all lines and separator sep ...
MLuint32 get32BitUWordInLocalEndianess(MLuint64 pos) const
Read an unsigned 32 bit word from position pos and convert it in local endianness.
virtual const MLuint8 * getPrefetchData() const
Returns pointer to prefetch data (or nullptr if not available).
virtual void close()
Closes the file if it was open, otherwise the call is ignored.
virtual std::string getFileName() const
Returns the currently used file name or "" on default case.
MLuint8 * getDataChunk(MLuint64 pos, MLuint64 numBytes) const
Reads numBytes from position pos in the file and returns it as allocated data chunk which has to be f...
virtual void setFileName(const std::string &fileName)
Sets the new file name.
virtual void prefetch()
Loads all file content into memory to avoid expensive file descriptor access on each get*Word call.
MLuint64 get64BitUWordInLocalEndianess(MLuint64 pos) const
Read an unsigned 64 bit word from position pos and convert it in local endianness.
int _getFileDescriptor() const
Returns the current file descriptor.
#define ML_ROOT_CLASS_HEADER(className)
UINT64 MLuint64
Introduce platform independent 64 bit unsigned integer type.
Definition: mlTypeDefs.h:513
unsigned int MLuint32
Definition: mlTypeDefs.h:191
unsigned char MLuint8
Definition: mlTypeDefs.h:115
MLuint64 MLuint
An unsigned ML integer type with at least 64 bits used for index calculations on very large images ev...
Definition: mlTypeDefs.h:594
unsigned short MLuint16
Definition: mlTypeDefs.h:148
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
Definition: mlTypeDefs.h:578
MLRELEASE_TOOLS_EXPORT std::ostream & operator<<(std::ostream &os, const ByteDataReader &obj)
Output to std::ostream.