mirror of
https://github.com/recastnavigation/recastnavigation.git
synced 2026-10-08 09:55:43 +00:00
Convert file IO to use FileIO class in SampleInterfaces.h
de-duping a lot of standard file IO
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
bool tryReadFile(const std::string& path, char** outBuffer, size_t* outBufferLen);
|
||||
void scanDirectory(const std::string& path, const std::string& ext, std::vector<std::string>& fileList);
|
||||
@@ -19,7 +19,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "ChunkyTriMesh.h"
|
||||
#include "MeshLoaderObj.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
@@ -115,10 +114,7 @@ private:
|
||||
|
||||
public:
|
||||
InputGeom() = default;
|
||||
~InputGeom()
|
||||
{
|
||||
delete chunkyMesh;
|
||||
}
|
||||
~InputGeom() { delete chunkyMesh; }
|
||||
InputGeom(const InputGeom&) = delete;
|
||||
InputGeom& operator=(const InputGeom&) = delete;
|
||||
InputGeom(InputGeom&&) = delete;
|
||||
|
||||
@@ -78,8 +78,8 @@ public:
|
||||
FileIO() = default;
|
||||
FileIO(const FileIO&) = delete;
|
||||
FileIO& operator=(const FileIO&) = delete;
|
||||
FileIO(FileIO&&) = delete;
|
||||
FileIO& operator=(FileIO&&) = delete;
|
||||
FileIO(FileIO&&) = default;
|
||||
FileIO& operator=(FileIO&&) = default;
|
||||
virtual ~FileIO();
|
||||
|
||||
bool openForWrite(const char* path);
|
||||
@@ -88,8 +88,11 @@ public:
|
||||
virtual bool isReading() const;
|
||||
virtual bool write(const void* ptr, const size_t size);
|
||||
virtual bool read(void* ptr, const size_t size);
|
||||
size_t getFileSize();
|
||||
|
||||
static void scanDirectory(const std::string& path, const std::string& ext, std::vector<std::string>& fileList);
|
||||
private:
|
||||
FILE* fp = nullptr;
|
||||
int mode = -1;
|
||||
enum class Mode { none, reading, writing };
|
||||
Mode mode = Mode::none;
|
||||
};
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
|
||||
#include "Filelist.h"
|
||||
|
||||
#include <algorithm>
|
||||
#ifdef WIN32
|
||||
# include <io.h>
|
||||
#else
|
||||
# include <dirent.h>
|
||||
# include <cstring>
|
||||
#endif
|
||||
|
||||
bool tryReadFile(const std::string& path, char** outBuffer, size_t* outBufferLen)
|
||||
{
|
||||
FILE* file = fopen(path.c_str(), "rb");
|
||||
if (!file)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fseek(file, 0, SEEK_END) != 0)
|
||||
{
|
||||
(void)fclose(file);
|
||||
return false;
|
||||
}
|
||||
|
||||
const int fileSize = ftell(file);
|
||||
if (fileSize < 0)
|
||||
{
|
||||
(void)fclose(file);
|
||||
return false;
|
||||
}
|
||||
*outBufferLen = fileSize;
|
||||
|
||||
if (fseek(file, 0, SEEK_SET) != 0)
|
||||
{
|
||||
(void)fclose(file);
|
||||
return false;
|
||||
}
|
||||
|
||||
*outBuffer = new char[*outBufferLen];
|
||||
const size_t readLen = fread(*outBuffer, *outBufferLen, 1, file);
|
||||
(void)fclose(file);
|
||||
if (readLen != 1)
|
||||
{
|
||||
delete[] *outBuffer;
|
||||
*outBuffer = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void scanDirectory(const std::string& path, const std::string& ext, std::vector<std::string>& filelist)
|
||||
{
|
||||
#ifdef WIN32
|
||||
std::string pathWithExt = path + "/*" + ext;
|
||||
|
||||
_finddata_t dir;
|
||||
intptr_t findHandle = _findfirst(pathWithExt.c_str(), &dir);
|
||||
if (findHandle == -1L)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
filelist.emplace_back(dir.name);
|
||||
} while (_findnext(findHandle, &dir) == 0);
|
||||
_findclose(findHandle);
|
||||
#else
|
||||
dirent* current = 0;
|
||||
DIR* dp = opendir(path.c_str());
|
||||
if (!dp)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
size_t extLen = strlen(ext.c_str());
|
||||
while ((current = readdir(dp)) != 0)
|
||||
{
|
||||
size_t len = strlen(current->d_name);
|
||||
if (len > extLen && strncmp(current->d_name + len - extLen, ext.c_str(), extLen) == 0)
|
||||
{
|
||||
filelist.emplace_back(current->d_name);
|
||||
}
|
||||
}
|
||||
closedir(dp);
|
||||
#endif
|
||||
|
||||
// Sort the list of files alphabetically.
|
||||
std::sort(filelist.begin(), filelist.end());
|
||||
}
|
||||
@@ -136,13 +136,22 @@ static char* parseRow(char* buf, char* bufEnd, char* row, int len)
|
||||
|
||||
bool InputGeom::loadMesh(rcContext* ctx, const std::string& filepath)
|
||||
{
|
||||
char* buffer;
|
||||
size_t bufferLen;
|
||||
if (!tryReadFile(filepath, &buffer, &bufferLen))
|
||||
FileIO file;
|
||||
if (!file.openForRead(filepath.c_str()))
|
||||
{
|
||||
ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not load '%s'", filepath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t bufferLen = file.getFileSize();
|
||||
char* buffer = new char[bufferLen];
|
||||
|
||||
if (!file.read(buffer, bufferLen))
|
||||
{
|
||||
ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not load '%s'", filepath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
filename = filepath;
|
||||
|
||||
delete chunkyMesh;
|
||||
|
||||
@@ -4,11 +4,16 @@
|
||||
#include "Recast.h"
|
||||
#include "SDL_opengl.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
|
||||
#ifdef WIN32
|
||||
# define snprintf _snprintf
|
||||
# include <io.h>
|
||||
#else
|
||||
# include <dirent.h>
|
||||
# include <cstring>
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -272,7 +277,7 @@ bool FileIO::openForWrite(const char* path)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
mode = 1;
|
||||
mode = Mode::writing;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -287,23 +292,23 @@ bool FileIO::openForRead(const char* path)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
mode = 2;
|
||||
mode = Mode::reading;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileIO::isWriting() const
|
||||
{
|
||||
return mode == 1;
|
||||
return mode == Mode::writing;
|
||||
}
|
||||
|
||||
bool FileIO::isReading() const
|
||||
{
|
||||
return mode == 2;
|
||||
return mode == Mode::reading;
|
||||
}
|
||||
|
||||
bool FileIO::write(const void* ptr, const size_t size)
|
||||
{
|
||||
if (!fp || mode != 1)
|
||||
if (!fp || mode != Mode::writing)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -313,10 +318,70 @@ bool FileIO::write(const void* ptr, const size_t size)
|
||||
|
||||
bool FileIO::read(void* ptr, const size_t size)
|
||||
{
|
||||
if (!fp || mode != 2)
|
||||
if (!fp || mode != Mode::reading)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
size_t readLen = fread(ptr, size, 1, fp);
|
||||
return readLen == 1;
|
||||
}
|
||||
|
||||
size_t FileIO::getFileSize()
|
||||
{
|
||||
if (!fp || mode != Mode::reading)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
size_t currentPos = ftell(fp);
|
||||
if (fseek(fp, 0, SEEK_END) != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
size_t size = ftell(fp);
|
||||
if (fseek(fp, 0, currentPos) != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
void FileIO::scanDirectory(const std::string& path, const std::string& ext, std::vector<std::string>& filelist)
|
||||
{
|
||||
#ifdef WIN32
|
||||
std::string pathWithExt = path + "/*" + ext;
|
||||
|
||||
_finddata_t dir;
|
||||
intptr_t findHandle = _findfirst(pathWithExt.c_str(), &dir);
|
||||
if (findHandle == -1L)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
filelist.emplace_back(dir.name);
|
||||
} while (_findnext(findHandle, &dir) == 0);
|
||||
_findclose(findHandle);
|
||||
#else
|
||||
dirent* current = 0;
|
||||
DIR* dp = opendir(path.c_str());
|
||||
if (!dp)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
size_t extLen = strlen(ext.c_str());
|
||||
while ((current = readdir(dp)) != 0)
|
||||
{
|
||||
size_t len = strlen(current->d_name);
|
||||
if (len > extLen && strncmp(current->d_name + len - extLen, ext.c_str(), extLen) == 0)
|
||||
{
|
||||
filelist.emplace_back(current->d_name);
|
||||
}
|
||||
}
|
||||
closedir(dp);
|
||||
#endif
|
||||
|
||||
// Sort the list of files alphabetically.
|
||||
std::sort(filelist.begin(), filelist.end());
|
||||
}
|
||||
|
||||
@@ -276,7 +276,7 @@ int main(int /*argc*/, char** /*argv*/)
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
scanDirectory(app.meshesFolder, ".obj", app.files);
|
||||
FileIO::scanDirectory(app.meshesFolder, ".obj", app.files);
|
||||
app.meshName = app.files[0];
|
||||
app.inputGeometry = new InputGeom;
|
||||
app.inputGeometry->load(&app.buildContext, app.meshesFolder + "/" + app.meshName);
|
||||
@@ -326,7 +326,7 @@ int main(int /*argc*/, char** /*argv*/)
|
||||
case SDLK_t:
|
||||
app.showTestCases = true;
|
||||
app.files.clear();
|
||||
scanDirectory(app.testCasesFolder, ".txt", app.files);
|
||||
FileIO::scanDirectory(app.testCasesFolder, ".txt", app.files);
|
||||
break;
|
||||
case SDLK_TAB:
|
||||
app.showMenu = !app.showMenu;
|
||||
@@ -660,8 +660,8 @@ int main(int /*argc*/, char** /*argv*/)
|
||||
if (ImGui::BeginCombo("##levelCombo", app.meshName.c_str(), 0))
|
||||
{
|
||||
app.files.clear();
|
||||
scanDirectory(app.meshesFolder, ".obj", app.files);
|
||||
scanDirectory(app.meshesFolder, ".gset", app.files);
|
||||
FileIO::scanDirectory(app.meshesFolder, ".obj", app.files);
|
||||
FileIO::scanDirectory(app.meshesFolder, ".gset", app.files);
|
||||
|
||||
for (const auto& file : app.files)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user