From 327864be53415f1f5cd6a32f09fd55d9f2e60f4f Mon Sep 17 00:00:00 2001 From: Rabia Alhaffar Date: Sun, 14 Jun 2020 13:33:34 +0300 Subject: [PATCH] Added DownloadFile function --- src/core.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/raylib.h | 1 + 2 files changed, 55 insertions(+) diff --git a/src/core.c b/src/core.c index 94e3fcaa2..9d885a387 100644 --- a/src/core.c +++ b/src/core.c @@ -541,6 +541,45 @@ static void *GamepadThread(void *arg); // Mouse reading thread // Module Functions Definition - Window and OpenGL Context Functions //---------------------------------------------------------------------------------- +// Function got from Stack Overflow +// https://stackoverflow.com/questions/2736753/how-to-remove-extension-from-file-name +const char *remove_extension(char* myStr, char extSep, char pathSep) { + char *retStr, *lastExt, *lastPath; + + // Error checks and allocate string. + + if (myStr == NULL) return NULL; + if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL; + + // Make a copy and find the relevant characters. + + strcpy (retStr, myStr); + lastExt = strrchr (retStr, extSep); + lastPath = (pathSep == 0) ? NULL : strrchr (retStr, pathSep); + + // If it has an extension separator. + + if (lastExt != NULL) { + // and it's to the right of the path separator. + + if (lastPath != NULL) { + if (lastPath < lastExt) { + // then remove it. + + *lastExt = '\0'; + } + } else { + // Has extension separator with no path separator. + + *lastExt = '\0'; + } + } + + // Return the modified string. + + return retStr; +} + #if defined(PLATFORM_ANDROID) // To allow easier porting to android, we allow the user to define a // main function which we call from android_main, defined by ourselves @@ -2207,6 +2246,21 @@ long GetFileModTime(const char *fileName) return 0; } +// Downloads a file from source (URL) to directory +// NOTES: It uses cURL +void DownloadFile(const char *src,const char *dir) +{ + #ifdef __ANDROID__ || TARGET_OS_IPHONE || TARGET_OS_EMBEDDED || TARGET_IPHONE_SIMULATOR + return -1; + #else + if (system("curl --version") == 0) { + if (!system(FormatText("cd %s",remove_extension(dir,".","/"))) == 0) system(FormatText("mkdir %s",remove_extension(dir,".","/"))); + system(FormatText("curl -o %s/%s %s",dir,GetFileName(src),src)); + } + else return -1; + #endif +} + // Compress data (DEFLATE algorythm) unsigned char *CompressData(unsigned char *data, int dataLength, int *compDataLength) { diff --git a/src/raylib.h b/src/raylib.h index 53ad970a0..d48ac9f86 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1097,6 +1097,7 @@ RLAPI bool IsFileDropped(void); // Check if a RLAPI char **GetDroppedFiles(int *count); // Get dropped files names (memory should be freed) RLAPI void ClearDroppedFiles(void); // Clear dropped files paths buffer (free memory) RLAPI long GetFileModTime(const char *fileName); // Get file modification time (last write time) +RLAPI void DownloadFile(const char *src,const char *dir); // Downloads a file from URL to directory RLAPI unsigned char *CompressData(unsigned char *data, int dataLength, int *compDataLength); // Compress data (DEFLATE algorythm) RLAPI unsigned char *DecompressData(unsigned char *compData, int compDataLength, int *dataLength); // Decompress data (DEFLATE algorythm)