Chapter 10. File System Support

Table of Contents

10.1. File System

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.

10.1. File System

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:

  1. 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 .

  2. 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:

  1. 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.

  2. 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.

  3. int MLFileExists(const char *fileName)

    Returns 1 if the file with the UTF8 coded name fileName exists, 0 otherwise.

  4. int MLFileIsReadable(const char *fileName)

    Returns 1 if the file with the UTF8 coded name fileName exists and is readable, 0 otherwise.

  5. int MLFileIsWritable(const char *fileName)

    Returns 1 if the file with the UTF8 coded name fileName exists and is writable, 0 otherwise.

  6. 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.

  7. 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.

  8. 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().

  9. 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:

  1. MLErrorCode MLfclose(FILE *file);

  2. MLErrorCode MLremove(const char *fileName);

  3. MLErrorCode MLrename(const char *oldName, const char *newName);

  4. MLErrorCode MLclose(int fd);

  5. MLErrorCode MLDeleteFile(const char *fileName);

  6. char* MLGetNonExistingRandomFileName(const char *prefix);

  7. MLErrorCode MLFileWriteStringData(const char *fileName, const char *str);

  8. MLErrorCode MLFileWriteBinaryData(const char *fileName, const MLuint8 *data, unsigned int len);

  9. MLErrorCode MLFileWriteBinaryDataAt(int fileDesc, MLint startPos, const MLuint8 *data, unsigned int len);

  10. MLErrorCode MLFileAppendStringData(const char *fileName, const char *strData);

  11. MLErrorCode MLFileAppendBinaryData(const char *fileName, const MLuint8 *data, unsigned int len);

  12. MLErrorCode MLFileAppendBinaryDataWithDescriptor(int fileDesc, const MLuint8 *data, unsigned int len);

  13. char* MLFileReadChunkAsString(const char *fileName, MLuint startPos, MLuint numBytes);

  14. MLuint8* MLFileReadChunkAsBinary(const char *fileName, MLuint startPos, MLuint numBytes);

  15. MLuint8* MLFileReadChunkAsBinaryFromDesc(int fileDesc, MLuint startPos, MLuint numBytes);

  16. char* MLFileReadChunkAsStringFromDesc(int fileDesc, MLuint startPos, MLuint numBytes);

  17. MLint MLFileGetSizeFromDescriptor(int fd);

  18. MLint MLFileGetSizeFromName(const char *fileName);

  19. 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.