Skip to content

Commit c264c86

Browse files
committed
ADDED: Some useful functions for Files and Text management
// File management functions - int FileRename(const char *fileName, const char *fileRename); // Rename file (if exists) - iint FileRemove(const char *fileName); // Remove file (if exists) - iint FileCopy(const char *srcPath, const char *dstPath); // Copy file from one path to another, dstPath created if it doesn't exist - iint FileMove(const char *srcPath, const char *dstPath); // Move file from one directory to another, dstPath created if it doesn't exist - int FileTextReplace(const char *fileName, const char *search, const char *replacement); // Replace text in an existing file - iint FileTextFindIndex(const char *fileName, const char *search); // Find text in existing file // Text management functions - const char *TextRemoveSpaces(const char *text); // Remove text spaces, concat words - char *GetTextBetween(const char *text, const char *begin, const char *end); // Get text between two strings - char *TextReplace(const char *text, const char *search, const char *replacement); // Replace text string (WARNING: memory must be freed!) - char *TextReplaceBetween(const char *text, const char *begin, const char *end, const char *replacement); // Replace text between two specific strings (WARNING: memory must be freed!)
1 parent bd6065a commit c264c86

File tree

3 files changed

+278
-78
lines changed

3 files changed

+278
-78
lines changed

src/raylib.h

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,45 +1110,51 @@ RLAPI void MemFree(void *ptr); // Internal me
11101110

11111111
// Set custom callbacks
11121112
// WARNING: Callbacks setup is intended for advanced users
1113-
RLAPI void SetTraceLogCallback(TraceLogCallback callback); // Set custom trace log
1114-
RLAPI void SetLoadFileDataCallback(LoadFileDataCallback callback); // Set custom file binary data loader
1115-
RLAPI void SetSaveFileDataCallback(SaveFileDataCallback callback); // Set custom file binary data saver
1116-
RLAPI void SetLoadFileTextCallback(LoadFileTextCallback callback); // Set custom file text data loader
1117-
RLAPI void SetSaveFileTextCallback(SaveFileTextCallback callback); // Set custom file text data saver
1113+
RLAPI void SetTraceLogCallback(TraceLogCallback callback); // Set custom trace log
1114+
RLAPI void SetLoadFileDataCallback(LoadFileDataCallback callback); // Set custom file binary data loader
1115+
RLAPI void SetSaveFileDataCallback(SaveFileDataCallback callback); // Set custom file binary data saver
1116+
RLAPI void SetLoadFileTextCallback(LoadFileTextCallback callback); // Set custom file text data loader
1117+
RLAPI void SetSaveFileTextCallback(SaveFileTextCallback callback); // Set custom file text data saver
11181118

11191119
// Files management functions
11201120
RLAPI unsigned char *LoadFileData(const char *fileName, int *dataSize); // Load file data as byte array (read)
1121-
RLAPI void UnloadFileData(unsigned char *data); // Unload file data allocated by LoadFileData()
1121+
RLAPI void UnloadFileData(unsigned char *data); // Unload file data allocated by LoadFileData()
11221122
RLAPI bool SaveFileData(const char *fileName, void *data, int dataSize); // Save data to file from byte array (write), returns true on success
11231123
RLAPI bool ExportDataAsCode(const unsigned char *data, int dataSize, const char *fileName); // Export data to code (.h), returns true on success
1124-
RLAPI char *LoadFileText(const char *fileName); // Load text data from file (read), returns a '\0' terminated string
1125-
RLAPI void UnloadFileText(char *text); // Unload file text data allocated by LoadFileText()
1126-
RLAPI bool SaveFileText(const char *fileName, const char *text); // Save text data to file (write), string must be '\0' terminated, returns true on success
1124+
RLAPI char *LoadFileText(const char *fileName); // Load text data from file (read), returns a '\0' terminated string
1125+
RLAPI void UnloadFileText(char *text); // Unload file text data allocated by LoadFileText()
1126+
RLAPI bool SaveFileText(const char *fileName, const char *text); // Save text data to file (write), string must be '\0' terminated, returns true on success
11271127
//------------------------------------------------------------------
11281128

11291129
// File system functions
1130-
RLAPI bool FileExists(const char *fileName); // Check if file exists
1131-
RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists
1132-
RLAPI bool IsFileExtension(const char *fileName, const char *ext); // Check file extension (recommended include point: .png, .wav)
1133-
RLAPI int GetFileLength(const char *fileName); // Get file length in bytes (NOTE: GetFileSize() conflicts with windows.h)
1134-
RLAPI const char *GetFileExtension(const char *fileName); // Get pointer to extension for a filename string (includes dot: '.png')
1135-
RLAPI const char *GetFileName(const char *filePath); // Get pointer to filename for a path string
1136-
RLAPI const char *GetFileNameWithoutExt(const char *filePath); // Get filename string without extension (uses static string)
1137-
RLAPI const char *GetDirectoryPath(const char *filePath); // Get full path for a given fileName with path (uses static string)
1138-
RLAPI const char *GetPrevDirectoryPath(const char *dirPath); // Get previous directory path for a given path (uses static string)
1139-
RLAPI const char *GetWorkingDirectory(void); // Get current working directory (uses static string)
1140-
RLAPI const char *GetApplicationDirectory(void); // Get the directory of the running application (uses static string)
1141-
RLAPI int MakeDirectory(const char *dirPath); // Create directories (including full path requested), returns 0 on success
1142-
RLAPI bool ChangeDirectory(const char *dir); // Change working directory, return true on success
1143-
RLAPI bool IsPathFile(const char *path); // Check if a given path is a file or a directory
1144-
RLAPI bool IsFileNameValid(const char *fileName); // Check if fileName is valid for the platform/OS
1145-
RLAPI FilePathList LoadDirectoryFiles(const char *dirPath); // Load directory filepaths
1130+
RLAPI int FileRename(const char *fileName, const char *fileRename); // Rename file (if exists)
1131+
RLAPI int FileRemove(const char *fileName); // Remove file (if exists)
1132+
RLAPI int FileCopy(const char *srcPath, const char *dstPath); // Copy file from one path to another, dstPath created if it doesn't exist
1133+
RLAPI int FileMove(const char *srcPath, const char *dstPath); // Move file from one directory to another, dstPath created if it doesn't exist
1134+
RLAPI int FileTextReplace(const char *fileName, const char *search, const char *replacement); // Replace text in an existing file
1135+
RLAPI int FileTextFindIndex(const char *fileName, const char *search); // Find text in existing file
1136+
RLAPI bool FileExists(const char *fileName); // Check if file exists
1137+
RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists
1138+
RLAPI bool IsFileExtension(const char *fileName, const char *ext); // Check file extension (recommended include point: .png, .wav)
1139+
RLAPI int GetFileLength(const char *fileName); // Get file length in bytes (NOTE: GetFileSize() conflicts with windows.h)
1140+
RLAPI long GetFileModTime(const char *fileName); // Get file modification time (last write time)
1141+
RLAPI const char *GetFileExtension(const char *fileName); // Get pointer to extension for a filename string (includes dot: '.png')
1142+
RLAPI const char *GetFileName(const char *filePath); // Get pointer to filename for a path string
1143+
RLAPI const char *GetFileNameWithoutExt(const char *filePath); // Get filename string without extension (uses static string)
1144+
RLAPI const char *GetDirectoryPath(const char *filePath); // Get full path for a given fileName with path (uses static string)
1145+
RLAPI const char *GetPrevDirectoryPath(const char *dirPath); // Get previous directory path for a given path (uses static string)
1146+
RLAPI const char *GetWorkingDirectory(void); // Get current working directory (uses static string)
1147+
RLAPI const char *GetApplicationDirectory(void); // Get the directory of the running application (uses static string)
1148+
RLAPI int MakeDirectory(const char *dirPath); // Create directories (including full path requested), returns 0 on success
1149+
RLAPI bool ChangeDirectory(const char *dir); // Change working directory, return true on success
1150+
RLAPI bool IsPathFile(const char *path); // Check if a given path is a file or a directory
1151+
RLAPI bool IsFileNameValid(const char *fileName); // Check if fileName is valid for the platform/OS
1152+
RLAPI FilePathList LoadDirectoryFiles(const char *dirPath); // Load directory filepaths
11461153
RLAPI FilePathList LoadDirectoryFilesEx(const char *basePath, const char *filter, bool scanSubdirs); // Load directory filepaths with extension filtering and recursive directory scan. Use 'DIR' in the filter string to include directories in the result
1147-
RLAPI void UnloadDirectoryFiles(FilePathList files); // Unload filepaths
1148-
RLAPI bool IsFileDropped(void); // Check if a file has been dropped into window
1149-
RLAPI FilePathList LoadDroppedFiles(void); // Load dropped filepaths
1150-
RLAPI void UnloadDroppedFiles(FilePathList files); // Unload dropped filepaths
1151-
RLAPI long GetFileModTime(const char *fileName); // Get file modification time (last write time)
1154+
RLAPI void UnloadDirectoryFiles(FilePathList files); // Unload filepaths
1155+
RLAPI bool IsFileDropped(void); // Check if a file has been dropped into window
1156+
RLAPI FilePathList LoadDroppedFiles(void); // Load dropped filepaths
1157+
RLAPI void UnloadDroppedFiles(FilePathList files); // Unload dropped filepaths
11521158

11531159
// Compression/Encoding functionality
11541160
RLAPI unsigned char *CompressData(const unsigned char *data, int dataSize, int *compDataSize); // Compress data (DEFLATE algorithm), memory must be MemFree()
@@ -1504,7 +1510,7 @@ RLAPI int GetCodepointPrevious(const char *text, int *codepointSize);
15041510
RLAPI const char *CodepointToUTF8(int codepoint, int *utf8Size); // Encode one codepoint into UTF-8 byte array (array length returned as parameter)
15051511

15061512
// Text strings management functions (no UTF-8 strings, only byte chars)
1507-
// WARNING 1: Most of these functions use internal static buffers, it's recommended to store returned data on user-side for re-use
1513+
// WARNING 1: Most of these functions use internal static buffers[], it's recommended to store returned data on user-side for re-use
15081514
// WARNING 2: Some strings allocate memory internally for the returned strings, those strings must be free by user using MemFree()
15091515
RLAPI char **LoadTextLines(const char *text, int *count); // Load text as separate lines ('\n')
15101516
RLAPI void UnloadTextLines(char **text, int lineCount); // Unload text lines
@@ -1513,12 +1519,15 @@ RLAPI bool TextIsEqual(const char *text1, const char *text2);
15131519
RLAPI unsigned int TextLength(const char *text); // Get text length, checks for '\0' ending
15141520
RLAPI const char *TextFormat(const char *text, ...); // Text formatting with variables (sprintf() style)
15151521
RLAPI const char *TextSubtext(const char *text, int position, int length); // Get a piece of a text string
1516-
RLAPI char *TextReplace(const char *text, const char *replace, const char *by); // Replace text string (WARNING: memory must be freed!)
1522+
RLAPI const char *TextRemoveSpaces(const char *text); // Remove text spaces, concat words
1523+
RLAPI char *GetTextBetween(const char *text, const char *begin, const char *end); // Get text between two strings
1524+
RLAPI char *TextReplace(const char *text, const char *search, const char *replacement); // Replace text string (WARNING: memory must be freed!)
1525+
RLAPI char *TextReplaceBetween(const char *text, const char *begin, const char *end, const char *replacement); // Replace text between two specific strings (WARNING: memory must be freed!)
15171526
RLAPI char *TextInsert(const char *text, const char *insert, int position); // Insert text in a position (WARNING: memory must be freed!)
15181527
RLAPI char *TextJoin(char **textList, int count, const char *delimiter); // Join text strings with delimiter
15191528
RLAPI char **TextSplit(const char *text, char delimiter, int *count); // Split text into multiple strings, using MAX_TEXTSPLIT_COUNT static strings
1520-
RLAPI void TextAppend(char *text, const char *append, int *position); // Append text at specific position and move cursor!
1521-
RLAPI int TextFindIndex(const char *text, const char *find); // Find first text occurrence within a string, -1 if not found
1529+
RLAPI void TextAppend(char *text, const char *append, int *position); // Append text at specific position and move cursor
1530+
RLAPI int TextFindIndex(const char *text, const char *search); // Find first text occurrence within a string, -1 if not found
15221531
RLAPI char *TextToUpper(const char *text); // Get upper case version of provided string
15231532
RLAPI char *TextToLower(const char *text); // Get lower case version of provided string
15241533
RLAPI char *TextToPascal(const char *text); // Get Pascal case notation version of provided string

src/rcore.c

Lines changed: 130 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,115 @@ void SetConfigFlags(unsigned int flags)
19271927
// Module Functions Definition: File system
19281928
//----------------------------------------------------------------------------------
19291929

1930+
// Rename file (if exists)
1931+
// NOTE: Only rename file name required, not full path
1932+
int FileRename(const char *fileName, const char *fileRename)
1933+
{
1934+
int result = 0;
1935+
1936+
if (FileExists(fileName))
1937+
{
1938+
result = rename(fileName, fileRename);
1939+
}
1940+
else result = -1;
1941+
1942+
return result;
1943+
}
1944+
1945+
// Remove file (if exists)
1946+
int FileRemove(const char *fileName)
1947+
{
1948+
int result = 0;
1949+
1950+
if (FileExists(fileName))
1951+
{
1952+
result = remove(fileName);
1953+
}
1954+
else result = -1;
1955+
1956+
return result;
1957+
}
1958+
1959+
// Copy file from one path to another
1960+
// NOTE: If destination path does not exist, it is created!
1961+
int FileCopy(const char *srcPath, const char *dstPath)
1962+
{
1963+
int result = 0;
1964+
int srcDataSize = 0;
1965+
unsigned char *srcFileData = LoadFileData(srcPath, &srcDataSize);
1966+
1967+
// Create required paths if they do not exist
1968+
if (!DirectoryExists(GetDirectoryPath(dstPath)))
1969+
result = MakeDirectory(GetDirectoryPath(dstPath));
1970+
1971+
if (result == 0) // Directory created successfully (or already exists)
1972+
{
1973+
if ((srcFileData != NULL) && (srcDataSize > 0))
1974+
result = SaveFileData(dstPath, srcFileData, srcDataSize);
1975+
}
1976+
1977+
UnloadFileData(srcFileData);
1978+
1979+
return result;
1980+
}
1981+
1982+
// Move file from one directory to another
1983+
// NOTE: If dst directories do not exists they are created
1984+
int FileMove(const char *srcPath, const char *dstPath)
1985+
{
1986+
int result = 0;
1987+
1988+
if (FileExists(srcPath))
1989+
{
1990+
FileCopy(srcPath, dstPath);
1991+
FileRemove(srcPath);
1992+
}
1993+
else result = -1;
1994+
1995+
return result;
1996+
}
1997+
1998+
// Replace text in an existing file
1999+
// WARNING: DEPENDENCY: [rtext] module
2000+
int FileTextReplace(const char *fileName, const char *search, const char *replacement)
2001+
{
2002+
int result = 0;
2003+
char *fileText = NULL;
2004+
char *fileTextUpdated = { 0 };
2005+
2006+
#if defined(SUPPORT_MODULE_RTEXT)
2007+
if (FileExists(fileName))
2008+
{
2009+
fileText = LoadFileText(fileName);
2010+
fileTextUpdated = TextReplace(fileText, search, replacement);
2011+
result = SaveFileText(fileName, fileTextUpdated);
2012+
MemFree(fileTextUpdated);
2013+
UnloadFileText(fileText);
2014+
}
2015+
#else
2016+
TRACELOG(LOG_WARNING, "FILE: File text replace requires [rtext] module");
2017+
#endif
2018+
2019+
return result;
2020+
}
2021+
2022+
// Find text index position in existing file
2023+
// WARNING: DEPENDENCY: [rtext] module
2024+
int FileTextFindIndex(const char *fileName, const char *search)
2025+
{
2026+
int result = -1;
2027+
2028+
if (FileExists(fileName))
2029+
{
2030+
char *fileText = LoadFileText(fileName);
2031+
char *ptr = strstr(fileText, search);
2032+
if (ptr != NULL) result = (int)(ptr - fileText);
2033+
UnloadFileText(fileText);
2034+
}
2035+
2036+
return result;
2037+
}
2038+
19302039
// Check if the file exists
19312040
bool FileExists(const char *fileName)
19322041
{
@@ -2054,6 +2163,21 @@ int GetFileLength(const char *fileName)
20542163
return size;
20552164
}
20562165

2166+
// Get file modification time (last write time)
2167+
long GetFileModTime(const char *fileName)
2168+
{
2169+
struct stat result = { 0 };
2170+
long modTime = 0;
2171+
2172+
if (stat(fileName, &result) == 0)
2173+
{
2174+
time_t mod = result.st_mtime;
2175+
modTime = (long)mod;
2176+
}
2177+
2178+
return modTime;
2179+
}
2180+
20572181
// Get pointer to extension for a filename string (includes the dot: .png)
20582182
// WARNING: We just get the ptr but not the extension as a separate string
20592183
const char *GetFileExtension(const char *fileName)
@@ -2367,7 +2491,7 @@ void UnloadDirectoryFiles(FilePathList files)
23672491
// Create directories (including full path requested), returns 0 on success
23682492
int MakeDirectory(const char *dirPath)
23692493
{
2370-
if ((dirPath == NULL) || (dirPath[0] == '\0')) return 1; // Path is not valid
2494+
if ((dirPath == NULL) || (dirPath[0] == '\0')) return -1; // Path is not valid
23712495
if (DirectoryExists(dirPath)) return 0; // Path already exists (is valid)
23722496

23732497
// Copy path string to avoid modifying original
@@ -2392,8 +2516,11 @@ int MakeDirectory(const char *dirPath)
23922516

23932517
// Create final directory
23942518
if (!DirectoryExists(pathcpy)) MKDIR(pathcpy);
2395-
23962519
RL_FREE(pathcpy);
2520+
2521+
// In case something failed and requested directory
2522+
// was not successfully created, return -1
2523+
if (!DirectoryExists(dirPath)) return -1;
23972524

23982525
return 0;
23992526
}
@@ -2513,22 +2640,6 @@ void UnloadDroppedFiles(FilePathList files)
25132640
}
25142641
}
25152642

2516-
// Get file modification time (last write time)
2517-
long GetFileModTime(const char *fileName)
2518-
{
2519-
struct stat result = { 0 };
2520-
long modTime = 0;
2521-
2522-
if (stat(fileName, &result) == 0)
2523-
{
2524-
time_t mod = result.st_mtime;
2525-
2526-
modTime = (long)mod;
2527-
}
2528-
2529-
return modTime;
2530-
}
2531-
25322643
//----------------------------------------------------------------------------------
25332644
// Module Functions Definition: Compression and Encoding
25342645
//----------------------------------------------------------------------------------
@@ -2903,7 +3014,7 @@ unsigned int *ComputeSHA1(unsigned char *data, int dataSize)
29033014
for (int offset = 0; offset < newDataSize; offset += (512/8))
29043015
{
29053016
// Break chunk into sixteen 32-bit words w[j], 0 <= j <= 15
2906-
unsigned int w[80] = {0};
3017+
unsigned int w[80] = { 0 };
29073018
for (int i = 0; i < 16; i++)
29083019
{
29093020
w[i] = (msg[offset + (i*4) + 0] << 24) |

0 commit comments

Comments
 (0)