commit 426f20a328c39f582bc74048356f3494ff264dc0
parent 5117a064aaa8bba9a2b8c8dd95bab6e20d2a611c
Author: dsp56300 <dsp56300@users.noreply.github.com>
Date: Thu, 17 Mar 2022 22:37:07 +0100
add more directory helper functions
Diffstat:
2 files changed, 94 insertions(+), 75 deletions(-)
diff --git a/source/synthLib/os.cpp b/source/synthLib/os.cpp
@@ -1,8 +1,5 @@
#include "os.h"
-#include <cassert>
-
-#include "../dsp56300/source/dsp56kEmu/buildconfig.h"
#include "../dsp56300/source/dsp56kEmu/logging.h"
#ifndef _WIN32
@@ -14,6 +11,7 @@
#ifdef USE_DIRENT
#include <dirent.h>
#include <unistd.h>
+#include <sys/stat.h>
#else
#include <filesystem>
#endif
@@ -45,12 +43,12 @@ namespace synthLib
reinterpret_cast<LPCSTR>(&getModulePath), &hm) == 0)
{
LOG("GetModuleHandle failed, error = " << GetLastError());
- return std::string();
+ return {};
}
if (GetModuleFileName(hm, buffer, sizeof(buffer)) == 0)
{
LOG("GetModuleFileName failed, error = " << GetLastError());
- return std::string();
+ return {};
}
path = buffer;
@@ -95,108 +93,121 @@ namespace synthLib
return path;
}
- static std::string lowercase(const std::string &_src)
+ std::string getCurrentDirectory()
{
- std::string str(_src);
- for (size_t i = 0; i < str.size(); ++i)
- str[i] = tolower(str[i]);
- return str;
+#ifdef USE_DIRENT
+ char temp[1024];
+ getcwd(temp, sizeof(temp));
+ return temp;
+#else
+ return std::filesystem::current_path().string();
+#endif
}
- static std::string getExtension(const std::string &_name)
+ bool createDirectory(const std::string& _dir)
{
- const auto pos = _name.find_last_of('.');
- if (pos != std::string::npos)
- return _name.substr(pos);
- return std::string();
+#ifdef USE_DIRENT
+ return mkdir(_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0;
+#else
+ return std::filesystem::create_directories(_dir);
+#endif
}
- std::string findROM(size_t _minSize, size_t _maxSize)
+ bool getDirectoryEntries(std::vector<std::string>& _files, const std::string& _folder)
{
- std::string path = getModulePath();
-
#ifdef USE_DIRENT
- if (path.empty())
- {
- char temp[1024];
- getcwd(temp, sizeof(temp));
- path = temp;
- }
-
DIR *dir;
struct dirent *ent;
- if ((dir = opendir(path.c_str())))
+ if ((dir = opendir(_folder.c_str())))
{
- /* print all the files and directories within directory */
while ((ent = readdir(dir)))
{
- const std::string file = path + ent->d_name;
-
- const std::string ext = lowercase(getExtension(file));
-
- if (ext != ".bin")
- continue;
-
- if (_minSize || _maxSize)
- {
- FILE *hFile = fopen(file.c_str(), "rb");
- if (!hFile)
- continue;
-
- fseek(hFile, 0, SEEK_END);
- const auto size = ftell(hFile);
- fclose(hFile);
+ const std::string file = _folder + ent->d_name;
- if (_minSize && size < _minSize)
- continue;
- if (_maxSize && size > _maxSize)
- continue;
-
- LOG("Found ROM at path " << file);
-
- return file;
- }
+ _files.push_back(file);
}
closedir(dir);
}
else
{
- return std::string();
+ return false;
}
#else
- if (path.empty())
- path = std::filesystem::current_path().string();
-
- try
+ try
{
- for (const std::filesystem::directory_entry &entry : std::filesystem::directory_iterator(path))
+ for (const std::filesystem::directory_entry &entry : std::filesystem::directory_iterator(_folder))
{
const auto &file = entry.path();
- if (!file.has_extension())
- continue;
-
- if (_minSize && entry.file_size() < _minSize)
- continue;
- if (_maxSize && entry.file_size() > _maxSize)
- continue;
-
- std::string ext = lowercase(file.extension().string());
-
- if (ext != ".bin")
- continue;
-
- LOG("Found ROM at path " << file);
-
- return file.string();
+ _files.push_back(file.string());
}
}
catch (...)
{
+ return false;
}
#endif
+ return !_files.empty();
+ }
+
+ static std::string lowercase(const std::string &_src)
+ {
+ std::string str(_src);
+ for (char& i : str)
+ i = static_cast<char>(tolower(i));
+ return str;
+ }
+
+ static std::string getExtension(const std::string &_name)
+ {
+ const auto pos = _name.find_last_of('.');
+ if (pos != std::string::npos)
+ return _name.substr(pos);
+ return {};
+ }
+
+ std::string findROM(const size_t _minSize, const size_t _maxSize)
+ {
+ std::string path = getModulePath();
+
+ if(path.empty())
+ path = getCurrentDirectory();
+
+ std::vector<std::string> files;
+
+ getDirectoryEntries(files, path);
+
+ for (const auto& file : files)
+ {
+ const std::string ext = lowercase(getExtension(file));
+
+ if (ext != ".bin")
+ continue;
- return std::string();
+ if (!_minSize && !_maxSize)
+ {
+ LOG("Found ROM at path " << file);
+ return file;
+ }
+
+ FILE *hFile = fopen(file.c_str(), "rb");
+ if (!hFile)
+ continue;
+
+ fseek(hFile, 0, SEEK_END);
+ const auto size = static_cast<size_t>(ftell(hFile));
+ fclose(hFile);
+
+ if (_minSize && size < _minSize)
+ continue;
+ if (_maxSize && size > _maxSize)
+ continue;
+
+ LOG("Found ROM at path " << file);
+
+ return file;
+ }
+ return {};
}
std::string findROM(const size_t _expectedSize)
diff --git a/source/synthLib/os.h b/source/synthLib/os.h
@@ -1,10 +1,18 @@
#pragma once
#include <string>
+#include <vector>
namespace synthLib
{
std::string getModulePath();
+
+ std::string getCurrentDirectory();
+ bool createDirectory(const std::string& _dir);;
+
+ bool getDirectoryEntries(std::vector<std::string>& _files, const std::string& _folder);
+
std::string findROM(size_t _minSize, size_t _maxSize);
std::string findROM(size_t _expectedSize = 524288);
+
void setFlushDenormalsToZero();
} // namespace synthLib