MeVisLab Toolbox Reference
mlFileSystem.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_FILE_SYSTEM_H
14 #define ML_FILE_SYSTEM_H
15 
39 
40 #include "mlUtilsSystem.h"
41 #include <cstdio>
42 
44 #include <ThirdPartyWarningsDisable.h>
45 #include <fcntl.h>
46 #include <sys/stat.h>
47 #include <ThirdPartyWarningsRestore.h>
48 #include <string>
49 #include <filesystem>
50 
51 
54 #ifdef WIN32
55 
57  #define ML_O_APPEND _O_APPEND
58 
60  #define ML_O_BINARY _O_BINARY
61 
63  #define ML_O_CREAT _O_CREAT
64 
66  #define ML_O_RANDOM _O_RANDOM
67 
69  #define ML_O_RDONLY _O_RDONLY
70 
72  #define ML_O_RDWR _O_RDWR
73 
75  #define ML_O_TEXT _O_TEXT
76 
78  #define ML_O_TRUNC _O_TRUNC
79 
81  #define ML_O_WRONLY _O_WRONLY
82 
84  #define ML_O_LARGEFILE 0
85 
87  #define ML_S_IWRITE _S_IWRITE
88 
90  #define ML_S_IREAD _S_IREAD
91 
93  #define ML_S_IEXEC _S_IEXEC
94 
97  #define ML_S_IWUSR ML_S_IWRITE
98 
101  #define ML_S_IRUSR ML_S_IREAD
102 
105  #define ML_S_IXUSR ML_S_IEXEC
106 
109  #define ML_S_IWGRP ML_S_IWRITE
110 
113  #define ML_S_IRGRP ML_S_IREAD
114 
117  #define ML_S_IXGRP ML_S_IEXEC
118 
121  #define ML_S_IWOTH ML_S_IWRITE
122 
125  #define ML_S_IROTH ML_S_IREAD
126 
129  #define ML_S_IXOTH ML_S_IEXEC
130 
131 #else
132 
134  #define ML_O_APPEND O_APPEND
135 
137  #define ML_O_BINARY 0
138 
140  #define ML_O_CREAT O_CREAT
141 
143  #define ML_O_RANDOM O_RANDOM
144 
146  #define ML_O_RDONLY O_RDONLY
147 
149  #define ML_O_RDWR O_RDWR
150 
152  #define ML_O_TEXT 0
153 
155  #define ML_O_TRUNC O_TRUNC
156 
158  #define ML_O_WRONLY O_WRONLY
159 
160  #if defined(MACOS)
162  #define ML_O_LARGEFILE 0
163  #else
165  #define ML_O_LARGEFILE O_LARGEFILE
166  #endif
167 
169  #define ML_S_IWRITE S_IWRITE
170 
172  #define ML_S_IREAD S_IREAD
173 
175  #define ML_S_IEXEC S_IEXEC
176 
179  #define ML_S_IWUSR S_IWUSR
180 
183  #define ML_S_IRUSR S_IRUSR
184 
187  #define ML_S_IXUSR S_IXUSR
188 
191  #define ML_S_IWGRP S_IWGRP
192 
195  #define ML_S_IRGRP S_IRGRP
196 
199  #define ML_S_IXGRP S_IXGRP
200 
203  #define ML_S_IWOTH S_IWOTH
204 
207  #define ML_S_IROTH S_IROTH
208 
211  #define ML_S_IXOTH S_IXOTH
212 
213 #endif
214 
215 
225 
226 
227 //-------------------------------------------------------------------------
232 //-------------------------------------------------------------------------
233 
242 [[nodiscard]]
243 ML_UTILS_EXPORT FILE* MLfopen(const char *fileName,
244  const char *mode);
245 
252 
257 ML_UTILS_EXPORT MLErrorCode MLremove(const char *fileName);
258 
265  const char *newName);
266 
277 [[nodiscard]]
278 ML_UTILS_EXPORT int MLopen(const char *fileName,
279  int openFlags,
280  int pMode);
281 
285 ML_UTILS_EXPORT MLErrorCode MLclose(int fileDescriptor);
286 
291 ML_UTILS_EXPORT int MLFileExists(const char *fileName);
292 
296 [[nodiscard]]
298 
313 [[nodiscard]]
315 
316 // Tag to identify whether MLGetNonExistingRandomFileNameWithPostFix exists to avoid
317 // failing mainline builds. To be removed after merge to trunk.
318 #define MLGetNonExistingRandomFileNameWithPostFix_Is_Available
319 
324 [[nodiscard]]
325 ML_UTILS_EXPORT char *MLGetNonExistingRandomFileNameWithPostFix(const char *prefix, const char *postFix);
326 
331 ML_UTILS_EXPORT int MLFileIsReadable(const char *fileName);
332 
337 ML_UTILS_EXPORT int MLFileIsWritable(const char *fileName);
338 
345 [[nodiscard]]
347  const char *str);
348 
354 [[nodiscard]]
356  const MLuint8 *data,
357  size_t numBytes);
358 
365 [[nodiscard]]
367  MLuint startPosition,
368  const MLuint8 *data,
369  size_t numBytes);
370 
376 [[nodiscard]]
378  const char *strData);
379 
385 [[nodiscard]]
387  const MLuint8 *data,
388  size_t numBytes);
389 
394 [[nodiscard]]
396  const MLuint8 *data,
397  size_t numBytes);
398 
404 [[nodiscard]]
405 ML_UTILS_EXPORT char* MLFileReadAllAsString(const char *fileName);
406 
411 [[nodiscard]]
413 
429 [[nodiscard]]
430 ML_UTILS_EXPORT char* MLFileReadChunkAsString(const char *fileName,
431  MLuint startPosition,
432  MLuint numBytes);
433 
446 [[nodiscard]]
448  MLuint startPosition,
449  MLuint numBytes);
450 
467 [[nodiscard]]
469  MLuint startPosition,
470  MLuint numBytes,
471  int useCurrentPosition);
472 
478  MLuint8* buffer,
479  MLuint startPosition,
480  MLuint numBytes);
481 
498 [[nodiscard]]
500  MLuint startPosition,
501  MLuint numBytes);
502 
503 
509 
515 
522 
528 [[nodiscard]]
530  MLuint position);
531 
536 [[nodiscard]]
538 
543 [[nodiscard]]
545 
548 [[nodiscard]]
549 ML_UTILS_EXPORT std::filesystem::path MLGetFileSystemPath(const std::string& filename);
550 
552 
553 
554 #endif // __mlFileSystem_H
MLint32 MLErrorCode
Type of an ML Error code.
Definition: mlTypeDefs.h:818
ML_UTILS_EXPORT MLErrorCode MLrename(const char *oldName, const char *newName)
Renames a file with name oldName to newName.
ML_UTILS_EXPORT char * MLGetTempPath()
Returns the path to the temporary directory of the current user or NULL on failure.
ML_UTILS_EXPORT MLint MLFileGetSizeFromName(const char *fileName)
Returns the size of the file given by the null-terminated file name fileName.
ML_UTILS_EXPORT char * MLFileReadAllAsString(const char *fileName)
Reads the complete file with a fileName as a string, returns NULL on error.
ML_UTILS_EXPORT MLuint8 * MLFileReadChunkAsBinary(const char *fileName, MLuint startPosition, MLuint numBytes)
Reads a data segment starting at startPosition from the file given by fileName and returns NULL on er...
ML_UTILS_EXPORT int MLFileIsReadable(const char *fileName)
Returns 1 if the given fileName exists and is readable, 0 otherwise.
ML_UTILS_EXPORT MLuint MLFileReadChunkAsBinaryFromDescToBuffer(int fileDescriptor, MLuint8 *buffer, MLuint startPosition, MLuint numBytes)
Reads a data segment starting at startPosition from the file given by fileDescriptor and returns the ...
ML_UTILS_EXPORT MLint MLFileGetSizeFromDescriptor(int fileDescriptor)
Returns the size of the file given by the file descriptor fileDescriptor A negative value is returned...
ML_UTILS_EXPORT MLErrorCode MLFileAppendBinaryData(const char *fileName, const MLuint8 *data, size_t numBytes)
Appends numBytes of data given by data to the file given by name fileName, creating a new file if it ...
ML_UTILS_EXPORT MLErrorCode MLFileWriteBinaryDataAt(int fileDescriptor, MLuint startPosition, const MLuint8 *data, size_t numBytes)
Overwrites a section of the file given by file descriptor fileDescriptor which must have been opened ...
ML_UTILS_EXPORT std::filesystem::path MLGetFileSystemPath(const std::string &filename)
Returns a file path object for the given filename that can be used as input for a file stream.
ML_UTILS_EXPORT MLuint8 * MLFileReadChunkAsBinaryFromDesc(int fileDescriptor, MLuint startPosition, MLuint numBytes, int useCurrentPosition)
Reads a data segment starting at startPosition from the file given by fileDescriptor and returns NULL...
ML_UTILS_EXPORT int MLopen(const char *fileName, int openFlags, int pMode)
Opens the file with name fileName with the given openFlags, returns a file descriptor or -1 on error.
ML_UTILS_EXPORT FILE * MLfopen(const char *fileName, const char *mode)
Opens the file with name fileName with the access permissions given by mode, and returns a FILE point...
ML_UTILS_EXPORT MLErrorCode MLFileWriteStringData(const char *fileName, const char *str)
Creates, opens, and overwrites the given file with name fileName with the given non NULL,...
ML_UTILS_EXPORT int MLFileIsWritable(const char *fileName)
Returns 1 if the given file with name fileName exists and is writable, 0 otherwise.
ML_UTILS_EXPORT MLErrorCode MLFileAppendBinaryDataWithDescriptor(int fileDescriptor, const MLuint8 *data, size_t numBytes)
Appends numBytes of data given by data to the file given by file descriptor fileDescriptor which must...
ML_UTILS_EXPORT MLint MLFileGetSizeFromFilePointer(FILE *file)
Returns the size of the file given by the FILE pointer file A negative value is returned on any error...
ML_UTILS_EXPORT char * MLGetNonExistingRandomFileNameWithPostFix(const char *prefix, const char *postFix)
Same as MLGetNonExistingRandomFileName() with the difference that the resulting unique file name also...
ML_UTILS_EXPORT void MLTranslateErrorCode(MLErrorCode *err)
Error code translation from global fileIO error code given by errno to ML error code *err if err is p...
ML_UTILS_EXPORT MLErrorCode MLclose(int fileDescriptor)
Closes an open file given by a the file descriptor fileDescriptor and returns ML_RESULT_OK on success...
ML_UTILS_EXPORT MLErrorCode MLFileWriteBinaryData(const char *fileName, const MLuint8 *data, size_t numBytes)
Creates, opens, and overwrites the given file with name fileName with the given data of a given numBy...
ML_UTILS_EXPORT char * MLGetNonExistingRandomFileName(const char *prefix)
If prefix is NULL or empty then a file name is returned which does not exist in the temp file directo...
ML_UTILS_EXPORT MLint MLFileGetBytePos(int fileDescriptor)
Returns current file position of the file given by descriptor fileDescriptor; it returns the positive...
ML_UTILS_EXPORT MLErrorCode MLFileAppendStringData(const char *fileName, const char *strData)
Appends the given null-terminated string strData to the file given by name fileName,...
ML_UTILS_EXPORT MLuint8 * MLFileReadAllAsBinary(const char *fileName)
Reads the complete file with a fileName as binary data, returns NULL on error.
ML_UTILS_EXPORT MLint MLFileSetBytePos(int fileDescriptor, MLuint position)
Sets the pointer of a file given by descriptor fileDescriptor to a position and returns the positive ...
ML_UTILS_EXPORT MLErrorCode MLCreateDirectory(const char *path)
The directory path is created.
ML_UTILS_EXPORT MLErrorCode MLremove(const char *fileName)
Deletes file with name fileName.
ML_UTILS_EXPORT MLErrorCode MLfclose(FILE *file)
Closes an open file given by descriptor file and returns 0 on success and non zero values on failure.
ML_UTILS_EXPORT char * MLFileReadChunkAsString(const char *fileName, MLuint startPosition, MLuint numBytes)
Reads a string segment starting at startPosition from the file given by fileName as a string and retu...
ML_UTILS_EXPORT char * MLFileReadChunkAsStringFromDesc(int fileDescriptor, MLuint startPosition, MLuint numBytes)
Reads a string segment starting at startingPosition from the file given by fileDescriptor as a string...
ML_UTILS_EXPORT int MLFileExists(const char *fileName)
Returns 1 if the given with name fileName exists, 0 otherwise or if fileName is NULL.
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
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
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.
Definition: mlUtilities.h:20