Commit 01a39fe1 authored by Ivan Dokunov's avatar Ivan Dokunov

Removed the ubiquitous use of dynamic memory in the file system

parent b4d878f1
......@@ -24,17 +24,16 @@ void CFileSystem::addPathInPriorityArray(int id, int iPriority)
bool CFileSystem::isFileOrDirectory(const char *szPath, bool isFile)
{
char* path = getAbsoluteCanonizePath(szPath);
char path[SIZE_PATH];
getAbsoluteCanonizePath(szPath, path, SIZE_PATH);
if (!path)
if (CHECK_CORRECT_PATH(path))
{
return false;
}
DWORD flag = GetFileAttributes(path);
mem_delete_a(path);
//Проверка на то куда имено ведет путь - к файлу или папке
return (flag != INVALID_FILE_ATTRIBUTES) && (isFile ? !(flag & FILE_ATTRIBUTE_DIRECTORY) : (flag & FILE_ATTRIBUTE_DIRECTORY));
}
......@@ -96,35 +95,34 @@ bool CFileSystem::isAbsolutePathInRoot(const char *szPath)
return pos != nullptr;
}
char *CFileSystem::getAbsoluteCanonizePath(const char *szPath)
void CFileSystem::getAbsoluteCanonizePath(const char *szPath, char *outPath, int iOutMax)
{
bool absolute = isAbsolutePath(szPath);
bool correctPath = true;
int len = absolute ? strlen(szPath) + 1 : SIZE_PATH;
char *fullPath = new char[len];
if (absolute)
{
memcpy(fullPath, szPath, len);
memcpy(outPath, szPath, len);
}
else
{
correctPath = resolvePath(szPath, fullPath, len);
correctPath = resolvePath(szPath, outPath, len);
}
//Во время поиска пути могут произойти ошибки - путь может быть не найден, или слишком маленький буфер для записи
if (correctPath)
{
//Если все корректно прошло, то путь можно канонизировать
canonize_path(fullPath);
return fullPath;
canonize_path(outPath);
}
else
{
//Если что то пошло не так записываем -1 в память, потом это значение можно проверить
outPath[0] = -1;
outPath[1] = '\0';
}
mem_delete_a(fullPath);
return nullptr;
}
char *CFileSystem::getFullPathToBuild()
......@@ -285,25 +283,24 @@ bool CFileSystem::resolvePath(const char *szPath, char *szOut, int iOutMax)
bool CFileSystem::fileExists(const char *szPath)
{
char* path = getAbsoluteCanonizePath(szPath);
char path[SIZE_PATH];
getAbsoluteCanonizePath(szPath, path, SIZE_PATH);
if (!path)
if (CHECK_CORRECT_PATH(path))
{
//Если не удалось найти полный путь - на выход
return false;
}
size_t size = fileGetSize(path) != -1;
mem_delete_a(path);
return size;
return fileGetSize(path) != -1;
}
size_t CFileSystem::fileGetSize(const char *szPath)
{
char* path = getAbsoluteCanonizePath(szPath);
char path[SIZE_PATH];
getAbsoluteCanonizePath(szPath, path, SIZE_PATH);
if (!path)
if (CHECK_CORRECT_PATH(path))
{
return false;
}
......@@ -317,8 +314,6 @@ size_t CFileSystem::fileGetSize(const char *szPath)
sizeof(lpFileInformation.nFileSizeLow) * sizeof(ULONGLONG)) |
lpFileInformation.nFileSizeLow;
mem_delete_a(path);
//Если result != 0 то все хорошо, если 0 то файл не найден
return result != 0 ? FileSize : FILE_NOT_FOUND;
}
......@@ -335,9 +330,10 @@ bool CFileSystem::isDirectory(const char *szPath)
time_t CFileSystem::getFileModifyTime(const char *szPath)
{
char* path = getAbsoluteCanonizePath(szPath);
char path[SIZE_PATH];
getAbsoluteCanonizePath(szPath, path, SIZE_PATH);
if (!path)
if (CHECK_CORRECT_PATH(path))
{
return 0;
}
......@@ -346,8 +342,6 @@ time_t CFileSystem::getFileModifyTime(const char *szPath)
GetFileAttributesEx(path, GetFileExInfoStandard, &lpFileInformation);
mem_delete(path);
return filetimeToTime_t(lpFileInformation.ftLastWriteTime);
}
......@@ -432,10 +426,15 @@ IFile *CFileSystem::openFile(const char *szPath, FILE_OPEN_MODE mode = FILE_MODE
return nullptr;
}
char *fullPath = getAbsoluteCanonizePath(szPath);
//Зарезервированная строка, если вдруг не хватит SIZE_PATH
char reserveStr[SIZE_PATH * 2];
bool useReserveStr = false;
char fullPath[SIZE_PATH];
getAbsoluteCanonizePath(szPath, fullPath, SIZE_PATH);
//Если по каким либо причинам нельзя вернуть полный путь - на выход
if (!fullPath && mode == FILE_MODE_READ)
if (CHECK_CORRECT_PATH(fullPath) && mode == FILE_MODE_READ)
{
return nullptr;
}
......@@ -449,8 +448,6 @@ IFile *CFileSystem::openFile(const char *szPath, FILE_OPEN_MODE mode = FILE_MODE
{
mem_delete(file);
}
mem_delete_a(fullPath);
return file;
}
......@@ -462,7 +459,7 @@ IFile *CFileSystem::openFile(const char *szPath, FILE_OPEN_MODE mode = FILE_MODE
*newFileName += '/';
*newFileName += szPath;
fullPath = getAbsoluteCanonizePath(newFileName->c_str());
getAbsoluteCanonizePath(newFileName->c_str(), fullPath, SIZE_PATH);
mem_delete(newFileName);
}
......@@ -497,8 +494,8 @@ IFile *CFileSystem::openFile(const char *szPath, FILE_OPEN_MODE mode = FILE_MODE
if (lenPath < newFileName->length())
{
mem_delete_a(fullPath);
fullPath = new char[newFileName->length() + 1];
memcpy(reserveStr, newFileName->c_str(), newFileName->length() + 1);
useReserveStr = true;
}
memcpy(fullPath, newFileName->c_str(), newFileName->length() + 1);
......@@ -507,14 +504,17 @@ IFile *CFileSystem::openFile(const char *szPath, FILE_OPEN_MODE mode = FILE_MODE
int res = 0;
//Определяем использован ли дополнительный путь для записи в файловой системе
char* correctPath = useReserveStr ? reserveStr : fullPath;
switch (mode)
{
case FILE_MODE_WRITE:
res = file->create(fullPath, CORE_FILE_BIN);
res = file->create(correctPath, CORE_FILE_BIN);
break;
case FILE_MODE_APPEND:
res = file->add(fullPath, CORE_FILE_BIN);
res = file->add(correctPath, CORE_FILE_BIN);
break;
}
......@@ -523,7 +523,5 @@ IFile *CFileSystem::openFile(const char *szPath, FILE_OPEN_MODE mode = FILE_MODE
mem_delete(file);
}
mem_delete_a(fullPath);
return file;
}
\ No newline at end of file
......@@ -37,6 +37,8 @@ See the license in LICENSE
assert(size - 1 < id && "The path ID you entered does not exist"); \
}
#define CHECK_CORRECT_PATH(path) path[0] == -1
struct Pair
{
int priority;
......@@ -102,7 +104,7 @@ private:
bool isAbsolutePathInRoot(const char *szPath);
//! Возвращает абсолютный канонизированный путь
char *getAbsoluteCanonizePath(const char *szPath);
void getAbsoluteCanonizePath(const char *szPath, char *outPath, int iOutMax);
char *getFullPathToBuild();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment