Skip to content

Commit

Permalink
Merge pull request #14460 from unknownbrackets/path-cleanup
Browse files Browse the repository at this point in the history
Fix headless test run, small cleanups to Path usage
  • Loading branch information
hrydgard authored May 15, 2021
2 parents 89ea962 + a097403 commit 138d81e
Show file tree
Hide file tree
Showing 31 changed files with 123 additions and 152 deletions.
6 changes: 4 additions & 2 deletions Common/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <cstring>

#include "Common/Buffer.h"
#include "Common/File/FileUtil.h"
#include "Common/File/Path.h"
#include "Common/Log.h"

Buffer::Buffer() { }
Expand Down Expand Up @@ -115,8 +117,8 @@ void Buffer::Printf(const char *fmt, ...) {
memcpy(ptr, buffer, retval);
}

bool Buffer::FlushToFile(const char *filename) {
FILE *f = fopen(filename, "wb");
bool Buffer::FlushToFile(const Path &filename) {
FILE *f = File::OpenCFile(filename, "wb");
if (!f)
return false;
if (data_.size()) {
Expand Down
4 changes: 3 additions & 1 deletion Common/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "Common/Common.h"

class Path;

// Acts as a queue. Intended to be as fast as possible for most uses.
// Does not do synchronization, must use external mutexes.
class Buffer {
Expand Down Expand Up @@ -66,7 +68,7 @@ class Buffer {
// Writes the entire buffer to the file descriptor. Also resets the
// size to zero. On failure, data remains in buffer and nothing is
// written.
bool FlushToFile(const char *filename);
bool FlushToFile(const Path &filename);

// Utilities. Try to avoid checking for size.
size_t size() const { return data_.size(); }
Expand Down
2 changes: 1 addition & 1 deletion Common/File/DirListing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ size_t GetFilesInDir(const Path &directory, std::vector<FileInfo> * files, const
info.size = 0;
info.isWritable = false; // TODO - implement some kind of check
if (!info.isDirectory) {
std::string ext = Path(info.fullName).GetFileExtension();
std::string ext = info.fullName.GetFileExtension();
if (!ext.empty()) {
ext = ext.substr(1); // Remove the dot.
if (filter && filters.find(ext) == filters.end()) {
Expand Down
8 changes: 0 additions & 8 deletions Common/File/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,14 +578,6 @@ std::string GetDir(const std::string &path) {
return cutpath;
}

std::string GetFilename(std::string path) {
size_t off = GetDir(path).size() + 1;
if (off < path.size())
return path.substr(off);
else
return path;
}

std::string GetFileExtension(const std::string & fn) {
size_t pos = fn.rfind(".");
if (pos == std::string::npos) {
Expand Down
3 changes: 0 additions & 3 deletions Common/File/FileUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ std::string GetFileExtension(const std::string &filename);
// Extracts the directory from a path.
std::string GetDir(const std::string &path);

// Extracts the filename from a path.
std::string GetFilename(std::string path);

// Returns struct with modification date of file
bool GetModifTime(const Path &filename, tm &return_time);

Expand Down
6 changes: 3 additions & 3 deletions Common/File/PathBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ bool PathBrowser::IsListingReady() {
}

std::string PathBrowser::GetFriendlyPath() const {
std::string str = GetPath().ToString();
std::string str = GetPath().ToVisualString();
// Show relative to memstick root if there.
std::string root = GetSysDirectory(DIRECTORY_MEMSTICK_ROOT).ToString();
std::string root = GetSysDirectory(DIRECTORY_MEMSTICK_ROOT).ToVisualString();

if (startsWith(str, root)) {
return std::string("ms:/") + str.substr(root.size());
Expand Down Expand Up @@ -258,7 +258,7 @@ bool PathBrowser::GetListing(std::vector<File::FileInfo> &fileInfo, const char *
fileInfo = ApplyFilter(pendingFiles_, filter);
return true;
} else {
File::GetFilesInDir(Path(path_), &fileInfo, filter);
File::GetFilesInDir(path_, &fileInfo, filter);
return true;
}
}
Expand Down
14 changes: 7 additions & 7 deletions Common/Net/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ int Client::ReadResponseEntity(net::Buffer *readbuf, const std::vector<std::stri
return 0;
}

Download::Download(const std::string &url, const std::string &outfile)
Download::Download(const std::string &url, const Path &outfile)
: progress_(&cancelled_), url_(url), outfile_(outfile) {
}

Expand Down Expand Up @@ -552,12 +552,12 @@ void Download::Do() {
}

if (resultCode == 200) {
INFO_LOG(IO, "Completed downloading %s to %s", url_.c_str(), outfile_.empty() ? "memory" : outfile_.c_str());
if (!outfile_.empty() && !buffer_.FlushToFile(outfile_.c_str())) {
ERROR_LOG(IO, "Failed writing download to %s", outfile_.c_str());
INFO_LOG(IO, "Completed downloading %s to %s", url_.c_str(), outfile_.empty() ? "memory" : outfile_.ToVisualString().c_str());
if (!outfile_.empty() && !buffer_.FlushToFile(outfile_)) {
ERROR_LOG(IO, "Failed writing download to %s", outfile_.ToVisualString().c_str());
}
} else {
ERROR_LOG(IO, "Error downloading %s to %s: %i", url_.c_str(), outfile_.c_str(), resultCode);
ERROR_LOG(IO, "Error downloading %s to %s: %i", url_.c_str(), outfile_.ToVisualString().c_str(), resultCode);
}
resultCode_ = resultCode;
}
Expand All @@ -569,7 +569,7 @@ void Download::Do() {
completed_ = true;
}

std::shared_ptr<Download> Downloader::StartDownload(const std::string &url, const std::string &outfile) {
std::shared_ptr<Download> Downloader::StartDownload(const std::string &url, const Path &outfile) {
std::shared_ptr<Download> dl(new Download(url, outfile));
downloads_.push_back(dl);
dl->Start();
Expand All @@ -578,7 +578,7 @@ std::shared_ptr<Download> Downloader::StartDownload(const std::string &url, cons

std::shared_ptr<Download> Downloader::StartDownloadWithCallback(
const std::string &url,
const std::string &outfile,
const Path &outfile,
std::function<void(Download &)> callback) {
std::shared_ptr<Download> dl(new Download(url, outfile));
dl->SetCallback(callback);
Expand Down
11 changes: 6 additions & 5 deletions Common/Net/HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <thread>
#include <cstdint>

#include "Common/File/Path.h"
#include "Common/Net/NetBuffer.h"
#include "Common/Net/Resolve.h"

Expand Down Expand Up @@ -90,7 +91,7 @@ class Client : public net::Connection {
// Not particularly efficient, but hey - it's a background download, that's pretty cool :P
class Download {
public:
Download(const std::string &url, const std::string &outfile);
Download(const std::string &url, const Path &outfile);
~Download();

void Start();
Expand All @@ -108,7 +109,7 @@ class Download {
int ResultCode() const { return resultCode_; }

std::string url() const { return url_; }
std::string outfile() const { return outfile_; }
const Path &outfile() const { return outfile_; }

// If not downloading to a file, access this to get the result.
Buffer &buffer() { return buffer_; }
Expand Down Expand Up @@ -147,7 +148,7 @@ class Download {
Buffer buffer_;
std::vector<std::string> responseHeaders_;
std::string url_;
std::string outfile_;
Path outfile_;
std::thread thread_;
int resultCode_ = 0;
bool completed_ = false;
Expand All @@ -166,11 +167,11 @@ class Downloader {
CancelAll();
}

std::shared_ptr<Download> StartDownload(const std::string &url, const std::string &outfile);
std::shared_ptr<Download> StartDownload(const std::string &url, const Path &outfile);

std::shared_ptr<Download> StartDownloadWithCallback(
const std::string &url,
const std::string &outfile,
const Path &outfile,
std::function<void(Download &)> callback);

// Drops finished downloads from the list.
Expand Down
4 changes: 2 additions & 2 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) {
// upgrade number in the ini.
if (iRunCount % 10 == 0 && bCheckForNewVersion) {
std::shared_ptr<http::Download> dl = g_DownloadManager.StartDownloadWithCallback(
"http://www.ppsspp.org/version.json", "", &DownloadCompletedCallback);
"http://www.ppsspp.org/version.json", Path(), &DownloadCompletedCallback);
dl->SetHidden(true);
}

Expand Down Expand Up @@ -1593,7 +1593,7 @@ void Config::RestoreDefaults() {
createGameConfig(gameId_);
} else {
if (File::Exists(iniFilename_))
File::Delete(Path(iniFilename_));
File::Delete(iniFilename_);
recentIsos.clear();
currentDirectory = "";
}
Expand Down
4 changes: 2 additions & 2 deletions Core/HLE/Plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ static std::vector<PluginInfo> FindPlugins(const std::string &gameID, const std:
GetFilesInDir(GetSysDirectory(DIRECTORY_PLUGINS), &pluginDirs);

std::vector<PluginInfo> found;
for (auto subdir : pluginDirs) {
Path subdirFullName(subdir.fullName);
for (const auto &subdir : pluginDirs) {
const Path &subdirFullName = subdir.fullName;
if (!subdir.isDirectory || !File::Exists(subdirFullName / "plugin.ini"))
continue;

Expand Down
30 changes: 15 additions & 15 deletions Core/SaveState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,22 +397,22 @@ namespace SaveState
return title + " (" + filename + ")";
}

std::string GetTitle(const std::string &filename) {
std::string GetTitle(const Path &filename) {
std::string title;
if (CChunkFileReader::GetFileTitle(Path(filename), &title) == CChunkFileReader::ERROR_NONE) {
if (CChunkFileReader::GetFileTitle(filename, &title) == CChunkFileReader::ERROR_NONE) {
if (title.empty()) {
return File::GetFilename(filename);
return filename.GetFilename();
}

return AppendSlotTitle(filename, title);
return AppendSlotTitle(filename.GetFilename(), title);
}

// The file can't be loaded - let's note that.
auto sy = GetI18NCategory("System");
return File::GetFilename(filename) + " " + sy->T("(broken)");
return filename.GetFilename() + " " + sy->T("(broken)");
}

Path GenerateSaveSlotFilename(const std::string &gameFilename, int slot, const char *extension)
Path GenerateSaveSlotFilename(const Path &gameFilename, int slot, const char *extension)
{
std::string discId = g_paramSFO.GetValueString("DISC_ID");
std::string discVer = g_paramSFO.GetValueString("DISC_VERSION");
Expand All @@ -437,7 +437,7 @@ namespace SaveState
g_Config.iCurrentStateSlot = (g_Config.iCurrentStateSlot + 1) % NUM_SLOTS;
}

void LoadSlot(const std::string &gameFilename, int slot, Callback callback, void *cbUserData)
void LoadSlot(const Path &gameFilename, int slot, Callback callback, void *cbUserData)
{
Path fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
if (!fn.empty()) {
Expand Down Expand Up @@ -471,7 +471,7 @@ namespace SaveState
}
}

void SaveSlot(const std::string &gameFilename, int slot, Callback callback, void *cbUserData)
void SaveSlot(const Path &gameFilename, int slot, Callback callback, void *cbUserData)
{
Path fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
Path shot = GenerateSaveSlotFilename(gameFilename, slot, SCREENSHOT_EXTENSION);
Expand Down Expand Up @@ -506,7 +506,7 @@ namespace SaveState
}
}

bool UndoSaveSlot(const std::string &gameFilename, int slot) {
bool UndoSaveSlot(const Path &gameFilename, int slot) {
Path fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
Path shot = GenerateSaveSlotFilename(gameFilename, slot, SCREENSHOT_EXTENSION);
Path fnUndo = GenerateSaveSlotFilename(gameFilename, slot, UNDO_STATE_EXTENSION);
Expand All @@ -523,19 +523,19 @@ namespace SaveState
return false;
}

bool HasSaveInSlot(const std::string &gameFilename, int slot)
bool HasSaveInSlot(const Path &gameFilename, int slot)
{
Path fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
return File::Exists(fn);
}

bool HasUndoSaveInSlot(const std::string &gameFilename, int slot)
bool HasUndoSaveInSlot(const Path &gameFilename, int slot)
{
Path fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
return File::Exists(fn.WithExtraExtension(".undo"));
}

bool HasScreenshotInSlot(const std::string &gameFilename, int slot)
bool HasScreenshotInSlot(const Path &gameFilename, int slot)
{
Path fn = GenerateSaveSlotFilename(gameFilename, slot, SCREENSHOT_EXTENSION);
return File::Exists(fn);
Expand Down Expand Up @@ -578,7 +578,7 @@ namespace SaveState
return true;
}

int GetNewestSlot(const std::string &gameFilename) {
int GetNewestSlot(const Path &gameFilename) {
int newestSlot = -1;
tm newestDate = {0};
for (int i = 0; i < NUM_SLOTS; i++) {
Expand All @@ -595,7 +595,7 @@ namespace SaveState
return newestSlot;
}

int GetOldestSlot(const std::string &gameFilename) {
int GetOldestSlot(const Path &gameFilename) {
int oldestSlot = -1;
tm oldestDate = {0};
for (int i = 0; i < NUM_SLOTS; i++) {
Expand All @@ -612,7 +612,7 @@ namespace SaveState
return oldestSlot;
}

std::string GetSlotDateAsString(const std::string &gameFilename, int slot) {
std::string GetSlotDateAsString(const Path &gameFilename, int slot) {
Path fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
if (File::Exists(fn)) {
tm time;
Expand Down
22 changes: 11 additions & 11 deletions Core/SaveState.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,24 @@ namespace SaveState

// Cycle through the 5 savestate slots
void NextSlot();
void SaveSlot(const std::string &gameFilename, int slot, Callback callback, void *cbUserData = 0);
void LoadSlot(const std::string &gameFilename, int slot, Callback callback, void *cbUserData = 0);
bool UndoSaveSlot(const std::string &gameFilename, int slot);
void SaveSlot(const Path &gameFilename, int slot, Callback callback, void *cbUserData = 0);
void LoadSlot(const Path &gameFilename, int slot, Callback callback, void *cbUserData = 0);
bool UndoSaveSlot(const Path &gameFilename, int slot);
// Checks whether there's an existing save in the specified slot.
bool HasSaveInSlot(const std::string &gameFilename, int slot);
bool HasUndoSaveInSlot(const std::string &gameFilename, int slot);
bool HasScreenshotInSlot(const std::string &gameFilename, int slot);
bool HasSaveInSlot(const Path &gameFilename, int slot);
bool HasUndoSaveInSlot(const Path &gameFilename, int slot);
bool HasScreenshotInSlot(const Path &gameFilename, int slot);

int GetCurrentSlot();

// Returns -1 if there's no oldest/newest slot.
int GetNewestSlot(const std::string &gameFilename);
int GetOldestSlot(const std::string &gameFilename);
int GetNewestSlot(const Path &gameFilename);
int GetOldestSlot(const Path &gameFilename);

std::string GetSlotDateAsString(const std::string &gameFilename, int slot);
Path GenerateSaveSlotFilename(const std::string &gameFilename, int slot, const char *extension);
std::string GetSlotDateAsString(const Path &gameFilename, int slot);
Path GenerateSaveSlotFilename(const Path &gameFilename, int slot, const char *extension);

std::string GetTitle(const std::string &filename);
std::string GetTitle(const Path &filename);

// Load the specified file into the current state (async.)
// Warning: callback will be called on a different thread.
Expand Down
4 changes: 2 additions & 2 deletions Core/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ bool DiscIDFromGEDumpPath(const Path &path, FileLoader *fileLoader, std::string
}

// Fall back to using the filename.
std::string filename = File::GetFilename(path.ToString());
std::string filename = path.GetFilename();
// Could be more discerning, but hey..
if (filename.size() > 10 && filename[0] == 'U' && filename[9] == '_') {
*id = filename.substr(0, 9);
Expand Down Expand Up @@ -676,7 +676,7 @@ void InitSysDirectories() {
INFO_LOG(COMMON, "Memstick directory not present, creating at '%s'", g_Config.memStickDirectory.c_str());
}

Path testFile = Path(g_Config.memStickDirectory) / "_writable_test.$$$";
Path testFile = g_Config.memStickDirectory / "_writable_test.$$$";

// If any directory is read-only, fall back to the Documents directory.
// We're screwed anyway if we can't write to Documents, or can't detect it.
Expand Down
4 changes: 2 additions & 2 deletions Core/TextureReplacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ void ReplacedTexture::Load(int level, void *out, int rowPitch) {
png_image png = {};
png.version = PNG_IMAGE_VERSION;

FILE *fp = File::OpenCFile(Path(info.file), "rb");
FILE *fp = File::OpenCFile(info.file, "rb");
if (!png_image_begin_read_from_stdio(&png, fp)) {
ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s", info.file.c_str(), png.message);
return;
Expand Down Expand Up @@ -733,7 +733,7 @@ bool TextureReplacer::GenerateIni(const std::string &gameID, Path &generatedFile
if (gameID.empty())
return false;

Path texturesDirectory = Path(GetSysDirectory(DIRECTORY_TEXTURES)) / gameID;
Path texturesDirectory = GetSysDirectory(DIRECTORY_TEXTURES) / gameID;
if (!File::Exists(texturesDirectory)) {
File::CreateFullPath(texturesDirectory);
}
Expand Down
Loading

0 comments on commit 138d81e

Please sign in to comment.