123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- //
- // KSFileUtils.h
- //
- // Created by Karl Stenerud on 2012-01-28.
- //
- // Copyright (c) 2012 Karl Stenerud. All rights reserved.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall remain in place
- // in this source code.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- /* Basic file reading/writing functions.
- */
- #ifndef HDR_KSFileUtils_h
- #define HDR_KSFileUtils_h
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include <stdbool.h>
- #include <stdarg.h>
- #define KSFU_MAX_PATH_LENGTH 500
- /** Get the last entry in a file path. Assumes UNIX style separators.
- *
- * @param path The file path.
- *
- * @return the last entry in the path.
- */
- const char* ksfu_lastPathEntry(const char* path);
- /** Write bytes to a file descriptor.
- *
- * @param fd The file descriptor.
- *
- * @param bytes Buffer containing the bytes.
- *
- * @param length The number of bytes to write.
- *
- * @return true if the operation was successful.
- */
- bool ksfu_writeBytesToFD(const int fd, const char* bytes, int length);
- /** Read bytes from a file descriptor.
- *
- * @param fd The file descriptor.
- *
- * @param bytes Buffer to store the bytes in.
- *
- * @param length The number of bytes to read.
- *
- * @return true if the operation was successful.
- */
- bool ksfu_readBytesFromFD(const int fd, char* bytes, int length);
- /** Read an entire file. Returns a buffer of file size + 1, null terminated.
- *
- * @param path The path to the file.
- *
- * @param data Place to store a pointer to the loaded data (must be freed).
- *
- * @param length Place to store the length of the loaded data (can be NULL).
- *
- * @param maxLength the maximum amount of bytes to read. It will skip beginning
- * bytes if necessary, and always get the latter part of the file.
- * 0 = no maximum.
- *
- * @return true if the operation was successful.
- */
- bool ksfu_readEntireFile(const char* path, char** data, int* length, int maxLength);
- /** Write a string to a file.
- *
- * @param fd The file descriptor.
- *
- * @param string The string to write.
- *
- * @return true if successful.
- */
- bool ksfu_writeStringToFD(const int fd, const char* string);
- /** Write a formatted string to a file.
- *
- * @param fd The file descriptor.
- *
- * @param fmt The format specifier, followed by its arguments.
- *
- * @return true if successful.
- */
- bool ksfu_writeFmtToFD(const int fd, const char* fmt, ...);
- /** Write a formatted string to a file.
- *
- * @param fd The file descriptor.
- *
- * @param fmt The format specifier.
- *
- * @param args The arguments list.
- *
- * @return true if successful.
- */
- bool ksfu_writeFmtArgsToFD(const int fd, const char* fmt, va_list args);
- /** Read a single line from a file.
- *
- * @param fd The file descriptor.
- *
- * @param buffer The buffer to read into.
- *
- * @param maxLength The maximum length to read.
- *
- * @return The number of bytes read.
- */
- int ksfu_readLineFromFD(const int fd, char* buffer, int maxLength);
- /** Make all directories in a path.
- *
- * @param absolutePath The full, absolute path to create.
- *
- * @return true if successful.
- */
- bool ksfu_makePath(const char* absolutePath);
- /** Remove a file or directory.
- *
- * @param path Path to the file to remove.
- *
- * @param mustExist If true, and the path doesn't exist, log an error.
- *
- * @return true if successful.
- */
- bool ksfu_removeFile(const char* path, bool mustExist);
- /** Delete the contents of a directory.
- *
- * @param path The path of the directory whose contents to delete.
- *
- * @return true if successful.
- */
- bool ksfu_deleteContentsOfPath(const char* path);
- /** Buffered writer structure. Everything inside should be considered internal use only. */
- typedef struct
- {
- char* buffer;
- int bufferLength;
- int position;
- int fd;
- } KSBufferedWriter;
- /** Open a file for buffered writing.
- *
- * @param writer The writer to initialize.
- *
- * @param path The path of the file to open.
- *
- * @param writeBuffer Memory to use as the write buffer.
- *
- * @param writeBufferLength Length of the memory to use as the write buffer.
- *
- * @return True if the file was successfully opened.
- */
- bool ksfu_openBufferedWriter(KSBufferedWriter* writer, const char* const path, char* writeBuffer, int writeBufferLength);
- /** Close a buffered writer.
- *
- * @param writer The writer to close.
- */
- void ksfu_closeBufferedWriter(KSBufferedWriter* writer);
- /** Write to a buffered writer.
- *
- * @param writer The writer to write to.
- *
- * @param data The data to write.
- *
- * @param length The length of the data to write.
- *
- * @return True if the data was successfully written.
- */
- bool ksfu_writeBufferedWriter(KSBufferedWriter* writer, const char* restrict const data, const int length);
- /** Flush a buffered writer, writing all uncommitted data to disk.
- *
- * @param writer The writer to flush.
- *
- * @return True if the buffer was successfully flushed.
- */
- bool ksfu_flushBufferedWriter(KSBufferedWriter* writer);
- /** Buffered reader structure. Everything inside should be considered internal use only. */
- typedef struct
- {
- char* buffer;
- int bufferLength;
- int dataStartPos;
- int dataEndPos;
- int fd;
- } KSBufferedReader;
- /** Open a file for buffered reading.
- *
- * @param reader The reader to initialize.
- *
- * @param path The path to the file to open.
- *
- * @param readBuffer The memory to use for buffered reading.
- *
- * @param readBufferLength The length of the memory to use for buffered reading.
- *
- * @return True if the file was successfully opened.
- */
- bool ksfu_openBufferedReader(KSBufferedReader* reader, const char* const path, char* readBuffer, int readBufferLength);
- /** Close a buffered reader.
- *
- * @param reader The reader to close.
- */
- void ksfu_closeBufferedReader(KSBufferedReader* reader);
- /** Read from a buffered reader.
- *
- * @param reader The reader to read from.
- *
- * @param dstBuffer The buffer to read into.
- *
- * @param byteCount The number of bytes to read.
- *
- * @return The number of bytes actually read.
- */
- int ksfu_readBufferedReader(KSBufferedReader* reader, char* dstBuffer, int byteCount);
- /** Read from a buffered reader until the specified character is encountered.
- * All bytes up to and including the character will be read.
- *
- * @param reader The reader to read from.
- *
- * @param ch The character to look for.
- *
- * @param dstBuffer The buffer to read into.
- *
- * @param length in: The maximum number of bytes to read before giving up the search.
- * out: The actual number of bytes read.
- *
- * @return True if the character was found before giving up.
- */
- bool ksfu_readBufferedReaderUntilChar(KSBufferedReader* reader, int ch, char* dstBuffer, int* length);
- #ifdef __cplusplus
- }
- #endif
- #endif // HDR_KSFileUtils_h
|