ML 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
161 #define ML_O_LARGEFILE O_LARGEFILE
162
164 #define ML_S_IWRITE S_IWRITE
165
167 #define ML_S_IREAD S_IREAD
168
170 #define ML_S_IEXEC S_IEXEC
171
174 #define ML_S_IWUSR S_IWUSR
175
178 #define ML_S_IRUSR S_IRUSR
179
182 #define ML_S_IXUSR S_IXUSR
183
186 #define ML_S_IWGRP S_IWGRP
187
190 #define ML_S_IRGRP S_IRGRP
191
194 #define ML_S_IXGRP S_IXGRP
195
198 #define ML_S_IWOTH S_IWOTH
199
202 #define ML_S_IROTH S_IROTH
203
206 #define ML_S_IXOTH S_IXOTH
207
208#endif
209
210
220
221
222//-------------------------------------------------------------------------
227//-------------------------------------------------------------------------
228
237[[nodiscard]]
238ML_UTILS_EXPORT FILE* MLfopen(const char *fileName,
239 const char *mode);
240
247
253
260 const char *newName);
261
272[[nodiscard]]
273ML_UTILS_EXPORT int MLopen(const char *fileName,
274 int openFlags,
275 int pMode);
276
281
286ML_UTILS_EXPORT int MLFileExists(const char *fileName);
287
291[[nodiscard]]
293
308[[nodiscard]]
310
311// Tag to identify whether MLGetNonExistingRandomFileNameWithPostFix exists to avoid
312// failing mainline builds. To be removed after merge to trunk.
313#define MLGetNonExistingRandomFileNameWithPostFix_Is_Available
314
319[[nodiscard]]
320ML_UTILS_EXPORT char *MLGetNonExistingRandomFileNameWithPostFix(const char *prefix, const char *postFix);
321
326ML_UTILS_EXPORT int MLFileIsReadable(const char *fileName);
327
332ML_UTILS_EXPORT int MLFileIsWritable(const char *fileName);
333
340[[nodiscard]]
342 const char *str);
343
349[[nodiscard]]
351 const MLuint8 *data,
352 size_t numBytes);
353
360[[nodiscard]]
362 MLuint startPosition,
363 const MLuint8 *data,
364 size_t numBytes);
365
371[[nodiscard]]
373 const char *strData);
374
380[[nodiscard]]
382 const MLuint8 *data,
383 size_t numBytes);
384
389[[nodiscard]]
391 const MLuint8 *data,
392 size_t numBytes);
393
399[[nodiscard]]
400ML_UTILS_EXPORT char* MLFileReadAllAsString(const char *fileName);
401
406[[nodiscard]]
408
424[[nodiscard]]
425ML_UTILS_EXPORT char* MLFileReadChunkAsString(const char *fileName,
426 MLuint startPosition,
427 MLuint numBytes);
428
441[[nodiscard]]
443 MLuint startPosition,
444 MLuint numBytes);
445
462[[nodiscard]]
464 MLuint startPosition,
465 MLuint numBytes,
466 int useCurrentPosition);
467
473 MLuint8* buffer,
474 MLuint startPosition,
475 MLuint numBytes);
476
493[[nodiscard]]
495 MLuint startPosition,
496 MLuint numBytes);
497
498
504
510
517
523[[nodiscard]]
525 MLuint position);
526
531[[nodiscard]]
533
538[[nodiscard]]
540
543[[nodiscard]]
544ML_UTILS_EXPORT std::filesystem::path MLGetFileSystemPath(const std::string& filename);
545
547
548
549#endif // __mlFileSystem_H
MLint32 MLErrorCode
Type of an ML Error code.
Definition mlTypeDefs.h:716
ML_UTILS_EXPORT MLErrorCode MLrename(const char *oldName, const char *newName)
Renames a file with name oldName to newName.
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 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 * 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 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 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 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 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 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 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 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 MLuint8 * MLFileReadAllAsBinary(const char *fileName)
Reads the complete file with a fileName as binary data, returns NULL on error.
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 char * MLGetNonExistingRandomFileNameWithPostFix(const char *prefix, const char *postFix)
Same as MLGetNonExistingRandomFileName() with the difference that the resulting unique file name also...
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 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 char * MLGetTempPath()
Returns the path to the temporary directory of the current user or NULL on failure.
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 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 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 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:109
MLuint64 MLuint
An unsigned ML integer type with at least 64 bits used for index calculations on very large images ev...
Definition mlTypeDefs.h:506
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
Definition mlTypeDefs.h:490
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.
Definition mlUtilities.h:20