Table of Contents
Chapter Objectives
This chapter describes features and limitations of the ML with regard to managing platform-independent file (system) accesses with file names that contain international characters.
Opening, reading, writing, manipulating and closing files is generally not platform-dependent; however, the way of coding file names to manage international characters is. The ML provides a set of functions to prevent file management from becoming difficult or from getting platform-dependent.
Generally, you should use these functions when you are not sure if the file names are unicoded or not.
Examples:
When you receive file names from string fields of ML modules,
you need to handle them as UTF8-coded strings, and files need to be
managed by using the functions provided by
mlFileSystem.h
.
When you have file names containing only ASCII characters, you
can use the normal file functions offered by the system. However, it
also possible to use the functions provided by
mlFileSystem.h
; it makes your code more
flexible concerning later changes to unicoded strings.
The following functions are available:
FILE *MLfopen(const char *fileName, const char *mode)
Opens the file fileName
with the
access mode mode
, returns a
FILE
pointer or NULL
on
failure. This method is equivalent to the
stdio
fopen
implementation, see the fopen
documentation
for available mode flags ("r","w","a", etc.). In contrast to the
original fopen
function, this method accepts
an UTF-8 encoded string and uses the unicode WIN32 API on Windows.
On Linux, this method maps to open
directly.
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. This function is equivalent to the
stdio open
implementation, see the open
documentation for available open
flags. In contrast
to the original open
method, this method accepts an
UTF-8 encoded string and uses the unicode WIN32 API on Windows. On
Unix systems, this method maps to open
directly.
pMode
specifies the access permissions of
the opened file.
int MLFileExists(const char *fileName)
Returns 1 if the file with the UTF8 coded name
fileName
exists, 0 otherwise.
int MLFileIsReadable(const char *fileName)
Returns 1 if the file with the UTF8 coded name
fileName
exists and is readable, 0
otherwise.
int MLFileIsWritable(const char *fileName)
Returns 1 if the file with the UTF8 coded name
fileName
exists and is writable, 0
otherwise.
int MLFileWriteString(const char *fileName, const char* data)
Creates/overwrites the file with the UTF8 coded name
fileName
with the null terminated given
data string data
and returns 1 on success
or 0 on error.
int MLFileAppendStringData(const char *fileName, const char* data)
Appends the null terminated data string
data
to the file with the UTF8 coded name
fileName
(creating a new file if it does
not exist), returns 1 on success and 0 on error.
char* MLFileReadAllAsString(const char *fileName)
Reads the complete file with UTF8 coded name
fileName
and returns its content as a
null-terminated string or returns NULL
on
error. The returned memory needs to be deallocated by calling
MLFree()
.
MLuint8* MLFileReadAllAsBinary(const char *fileName)
Reads the complete file with UTF8 coded name
fileName
and returns its content as binary
data or returns NULL
on error. The returned
memory needs to be deallocated by calling
MLFree()
.
There are a number of additional functions available, see
mlFileSystem.h
for details:
MLErrorCode MLfclose(FILE *file);
MLErrorCode MLremove(const char *fileName);
MLErrorCode MLrename(const char *oldName, const char *newName);
MLErrorCode MLclose(int fd);
MLErrorCode MLDeleteFile(const char *fileName);
char* MLGetNonExistingRandomFileName(const char *prefix);
MLErrorCode MLFileWriteStringData(const char *fileName, const char *str);
MLErrorCode MLFileWriteBinaryData(const char *fileName, const MLuint8 *data, unsigned int len);
MLErrorCode MLFileWriteBinaryDataAt(int fileDesc, MLint startPos, const MLuint8 *data, unsigned int len);
MLErrorCode MLFileAppendStringData(const char *fileName, const char *strData);
MLErrorCode MLFileAppendBinaryData(const char *fileName, const MLuint8 *data, unsigned int len);
MLErrorCode MLFileAppendBinaryDataWithDescriptor(int fileDesc, const MLuint8 *data, unsigned int len);
char* MLFileReadChunkAsString(const char *fileName, MLuint startPos, MLuint numBytes);
MLuint8* MLFileReadChunkAsBinary(const char *fileName, MLuint startPos, MLuint numBytes);
MLuint8* MLFileReadChunkAsBinaryFromDesc(int fileDesc, MLuint startPos, MLuint numBytes);
char* MLFileReadChunkAsStringFromDesc(int fileDesc, MLuint startPos, MLuint numBytes);
MLint MLFileGetSizeFromDescriptor(int fd);
MLint MLFileGetSizeFromName(const char *fileName);
MLint MLFileSetBytePos(int fd, MLint pos);
See also Section 2.6.2, “
MLUtilities
” and Chapter 9, Unicode Support for more information on helper functions for
the platform-independent implementation of file-system-related
functions.
© 2024 MeVis Medical Solutions AG