1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-22 09:39:43 +01:00

Add a null terminator to loaded TinyXML files (#117)

* Add a null terminator to loaded TinyXML files

The TinyXML parse() function expect a C-like string, including terminator.
When xml loading was changed, it loaded the file, but included no such thing.
Thus, we load the file, then reallocate the memory so that we can insert a
null terminator to it, before passing it to parse().

* Tweak TinyXML file loading

Instead of first loading the file content into memory, then reallocate it
to add a null pointer, add an argument to the file load function for whether
to append a null terminator or not, defaulting to false. It still returns the
length without the null pointer in case a length ptr is passed.
This commit is contained in:
Fredrik Ljungdahl 2020-01-24 21:35:46 +01:00 committed by Ethan Lee
parent 3dee27db7b
commit 5862af4445
2 changed files with 14 additions and 4 deletions

View file

@ -123,7 +123,8 @@ char *FILESYSTEM_getUserLevelDirectory()
return levelDir;
}
void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem, size_t *len)
void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem,
size_t *len, bool addnull)
{
PHYSFS_File *handle = PHYSFS_openRead(name);
if (handle == NULL)
@ -135,7 +136,15 @@ void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem, size_t *
{
*len = length;
}
*mem = (unsigned char*) malloc(length);
if (addnull)
{
*mem = (unsigned char *) malloc(length + 1);
mem[length] = 0;
}
else
{
*mem = (unsigned char*) malloc(length);
}
PHYSFS_readBytes(handle, *mem, length);
PHYSFS_close(handle);
}
@ -165,7 +174,7 @@ bool FILESYSTEM_loadTiXmlDocument(const char *name, TiXmlDocument *doc)
{
/* TiXmlDocument.SaveFile doesn't account for Unicode paths, PHYSFS does */
unsigned char *mem = NULL;
FILESYSTEM_loadFileToMemory(name, &mem, NULL);
FILESYSTEM_loadFileToMemory(name, &mem, NULL, true);
if (mem == NULL)
{
return false;

View file

@ -12,7 +12,8 @@ void FILESYSTEM_deinit();
char *FILESYSTEM_getUserSaveDirectory();
char *FILESYSTEM_getUserLevelDirectory();
void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem, size_t *len);
void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem,
size_t *len, bool addnull = false);
void FILESYSTEM_freeMemory(unsigned char **mem);
bool FILESYSTEM_saveTiXmlDocument(const char *name, TiXmlDocument *doc);
bool FILESYSTEM_loadTiXmlDocument(const char *name, TiXmlDocument *doc);