From: Holger Schemel Date: Sat, 11 Nov 2017 19:27:43 +0000 (+0100) Subject: added function to copy file X-Git-Tag: 4.1.0.0~55 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=b5793d558cadada689358997736a16bcf346a462;p=rocksndiamonds.git added function to copy file --- diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 701d38fb..06ded936 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -2135,6 +2135,8 @@ void dumpList(ListNode *node_first) /* functions for file handling */ /* ------------------------------------------------------------------------- */ +#define MAX_BUFFER_SIZE 4096 + File *openFile(char *filename, char *mode) { File *file = checked_calloc(sizeof(File)); @@ -2217,6 +2219,11 @@ size_t readFile(File *file, void *buffer, size_t item_size, size_t num_items) return fread(buffer, item_size, num_items, file->file); } +size_t writeFile(File *file, void *buffer, size_t item_size, size_t num_items) +{ + return fwrite(buffer, item_size, num_items, file->file); +} + int seekFile(File *file, long offset, int whence) { #if defined(PLATFORM_ANDROID) @@ -2286,6 +2293,36 @@ char *getStringFromFile(File *file, char *line, int size) return fgets(line, size, file->file); } +int copyFile(char *filename_from, char *filename_to) +{ + File *file_from, *file_to; + + if ((file_from = openFile(filename_from, MODE_READ)) == NULL) + { + return -1; + } + + if ((file_to = openFile(filename_to, MODE_WRITE)) == NULL) + { + closeFile(file_from); + + return -1; + } + + while (!checkEndOfFile(file_from)) + { + byte buffer[MAX_BUFFER_SIZE]; + size_t bytes_read = readFile(file_from, buffer, 1, MAX_BUFFER_SIZE); + + writeFile(file_to, buffer, 1, bytes_read); + } + + closeFile(file_from); + closeFile(file_to); + + return 0; +} + /* ------------------------------------------------------------------------- */ /* functions for directory handling */ diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 6c654cb5..078a10f3 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -246,9 +246,11 @@ File *openFile(char *, char *); int closeFile(File *); int checkEndOfFile(File *); size_t readFile(File *, void *, size_t, size_t); +size_t writeFile(File *, void *, size_t, size_t); int seekFile(File *, long, int); int getByteFromFile(File *); char *getStringFromFile(File *, char *, int); +int copyFile(char *, char *); Directory *openDirectory(char *); int closeDirectory(Directory *);