summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorGravatar Lioncash2018-07-21 15:52:42 -0400
committerGravatar Lioncash2018-07-21 16:21:19 -0400
commitd66b43dadfac1e9324fee48e97361e2f858f8af5 (patch)
tree6007104127ffaa62cb5cb94f5c47fadf4c192fb8 /src/common
parentMerge pull request #754 from lioncash/part (diff)
downloadyuzu-d66b43dadfac1e9324fee48e97361e2f858f8af5.tar.gz
yuzu-d66b43dadfac1e9324fee48e97361e2f858f8af5.tar.xz
yuzu-d66b43dadfac1e9324fee48e97361e2f858f8af5.zip
file_util: Use an enum class for GetUserPath()
Instead of using an unsigned int as a parameter and expecting a user to always pass in the correct values, we can just convert the enum into an enum class and use that type as the parameter type instead, which makes the interface more type safe. We also get rid of the bookkeeping "NUM_" element in the enum by just using an unordered map. This function is generally low-frequency in terms of calls (and I'd hope so, considering otherwise would mean we're slamming the disk with IO all the time) so I'd consider this acceptable in this case.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/common_paths.h6
-rw-r--r--src/common/file_util.cpp68
-rw-r--r--src/common/file_util.h27
3 files changed, 51 insertions, 50 deletions
diff --git a/src/common/common_paths.h b/src/common/common_paths.h
index 9bf3efaf2..6799a357a 100644
--- a/src/common/common_paths.h
+++ b/src/common/common_paths.h
@@ -26,7 +26,7 @@
26#define USA_DIR "USA" 26#define USA_DIR "USA"
27#define JAP_DIR "JAP" 27#define JAP_DIR "JAP"
28 28
29// Subdirs in the User dir returned by GetUserPath(D_USER_IDX) 29// Subdirs in the User dir returned by GetUserPath(UserPath::UserDir)
30#define CONFIG_DIR "config" 30#define CONFIG_DIR "config"
31#define CACHE_DIR "cache" 31#define CACHE_DIR "cache"
32#define SDMC_DIR "sdmc" 32#define SDMC_DIR "sdmc"
@@ -35,11 +35,11 @@
35#define LOG_DIR "log" 35#define LOG_DIR "log"
36 36
37// Filenames 37// Filenames
38// Files in the directory returned by GetUserPath(D_CONFIG_IDX) 38// Files in the directory returned by GetUserPath(UserPath::ConfigDir)
39#define EMU_CONFIG "emu.ini" 39#define EMU_CONFIG "emu.ini"
40#define DEBUGGER_CONFIG "debugger.ini" 40#define DEBUGGER_CONFIG "debugger.ini"
41#define LOGGER_CONFIG "logger.ini" 41#define LOGGER_CONFIG "logger.ini"
42// Files in the directory returned by GetUserPath(D_LOGS_IDX) 42// Files in the directory returned by GetUserPath(UserPath::LogDir)
43#define LOG_FILE "yuzu_log.txt" 43#define LOG_FILE "yuzu_log.txt"
44 44
45// Sys files 45// Sys files
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index c882ab39f..1e28f7cbb 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -3,6 +3,7 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <sstream> 5#include <sstream>
6#include <unordered_map>
6#include "common/assert.h" 7#include "common/assert.h"
7#include "common/common_funcs.h" 8#include "common/common_funcs.h"
8#include "common/common_paths.h" 9#include "common/common_paths.h"
@@ -681,67 +682,68 @@ std::string GetSysDirectory() {
681 682
682// Returns a string with a yuzu data dir or file in the user's home 683// Returns a string with a yuzu data dir or file in the user's home
683// directory. To be used in "multi-user" mode (that is, installed). 684// directory. To be used in "multi-user" mode (that is, installed).
684const std::string& GetUserPath(const unsigned int DirIDX, const std::string& newPath) { 685const std::string& GetUserPath(UserPath path, const std::string& new_path) {
685 static std::string paths[NUM_PATH_INDICES]; 686 static std::unordered_map<UserPath, std::string> paths;
687 auto& user_path = paths[UserPath::UserDir];
686 688
687 // Set up all paths and files on the first run 689 // Set up all paths and files on the first run
688 if (paths[D_USER_IDX].empty()) { 690 if (user_path.empty()) {
689#ifdef _WIN32 691#ifdef _WIN32
690 paths[D_USER_IDX] = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP; 692 user_path = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP;
691 if (!FileUtil::IsDirectory(paths[D_USER_IDX])) { 693 if (!FileUtil::IsDirectory(user_path)) {
692 paths[D_USER_IDX] = AppDataRoamingDirectory() + DIR_SEP EMU_DATA_DIR DIR_SEP; 694 user_path = AppDataRoamingDirectory() + DIR_SEP EMU_DATA_DIR DIR_SEP;
693 } else { 695 } else {
694 LOG_INFO(Common_Filesystem, "Using the local user directory"); 696 LOG_INFO(Common_Filesystem, "Using the local user directory");
695 } 697 }
696 698
697 paths[D_CONFIG_IDX] = paths[D_USER_IDX] + CONFIG_DIR DIR_SEP; 699 paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP);
698 paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP; 700 paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP);
699#else 701#else
700 if (FileUtil::Exists(ROOT_DIR DIR_SEP USERDATA_DIR)) { 702 if (FileUtil::Exists(ROOT_DIR DIR_SEP USERDATA_DIR)) {
701 paths[D_USER_IDX] = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP; 703 user_path = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP;
702 paths[D_CONFIG_IDX] = paths[D_USER_IDX] + CONFIG_DIR DIR_SEP; 704 paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP);
703 paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP; 705 paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP);
704 } else { 706 } else {
705 std::string data_dir = GetUserDirectory("XDG_DATA_HOME"); 707 std::string data_dir = GetUserDirectory("XDG_DATA_HOME");
706 std::string config_dir = GetUserDirectory("XDG_CONFIG_HOME"); 708 std::string config_dir = GetUserDirectory("XDG_CONFIG_HOME");
707 std::string cache_dir = GetUserDirectory("XDG_CACHE_HOME"); 709 std::string cache_dir = GetUserDirectory("XDG_CACHE_HOME");
708 710
709 paths[D_USER_IDX] = data_dir + DIR_SEP EMU_DATA_DIR DIR_SEP; 711 user_path = data_dir + DIR_SEP EMU_DATA_DIR DIR_SEP;
710 paths[D_CONFIG_IDX] = config_dir + DIR_SEP EMU_DATA_DIR DIR_SEP; 712 paths.emplace(UserPath::ConfigDir, config_dir + DIR_SEP EMU_DATA_DIR DIR_SEP);
711 paths[D_CACHE_IDX] = cache_dir + DIR_SEP EMU_DATA_DIR DIR_SEP; 713 paths.emplace(UserPath::CacheDir, cache_dir + DIR_SEP EMU_DATA_DIR DIR_SEP);
712 } 714 }
713#endif 715#endif
714 paths[D_SDMC_IDX] = paths[D_USER_IDX] + SDMC_DIR DIR_SEP; 716 paths.emplace(UserPath::SDMCDir, user_path + SDMC_DIR DIR_SEP);
715 paths[D_NAND_IDX] = paths[D_USER_IDX] + NAND_DIR DIR_SEP; 717 paths.emplace(UserPath::NANDDir, user_path + NAND_DIR DIR_SEP);
716 paths[D_SYSDATA_IDX] = paths[D_USER_IDX] + SYSDATA_DIR DIR_SEP; 718 paths.emplace(UserPath::SysDataDir, user_path + SYSDATA_DIR DIR_SEP);
717 // TODO: Put the logs in a better location for each OS 719 // TODO: Put the logs in a better location for each OS
718 paths[D_LOGS_IDX] = paths[D_USER_IDX] + LOG_DIR DIR_SEP; 720 paths.emplace(UserPath::LogDir, user_path + LOG_DIR DIR_SEP);
719 } 721 }
720 722
721 if (!newPath.empty()) { 723 if (!new_path.empty()) {
722 if (!FileUtil::IsDirectory(newPath)) { 724 if (!FileUtil::IsDirectory(new_path)) {
723 LOG_ERROR(Common_Filesystem, "Invalid path specified {}", newPath); 725 LOG_ERROR(Common_Filesystem, "Invalid path specified {}", new_path);
724 return paths[DirIDX]; 726 return paths[path];
725 } else { 727 } else {
726 paths[DirIDX] = newPath; 728 paths[path] = new_path;
727 } 729 }
728 730
729 switch (DirIDX) { 731 switch (path) {
730 case D_ROOT_IDX: 732 case UserPath::RootDir:
731 paths[D_USER_IDX] = paths[D_ROOT_IDX] + DIR_SEP; 733 user_path = paths[UserPath::RootDir] + DIR_SEP;
732 break; 734 break;
733 735
734 case D_USER_IDX: 736 case UserPath::UserDir:
735 paths[D_USER_IDX] = paths[D_ROOT_IDX] + DIR_SEP; 737 user_path = paths[UserPath::RootDir] + DIR_SEP;
736 paths[D_CONFIG_IDX] = paths[D_USER_IDX] + CONFIG_DIR DIR_SEP; 738 paths[UserPath::ConfigDir] = user_path + CONFIG_DIR DIR_SEP;
737 paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP; 739 paths[UserPath::CacheDir] = user_path + CACHE_DIR DIR_SEP;
738 paths[D_SDMC_IDX] = paths[D_USER_IDX] + SDMC_DIR DIR_SEP; 740 paths[UserPath::SDMCDir] = user_path + SDMC_DIR DIR_SEP;
739 paths[D_NAND_IDX] = paths[D_USER_IDX] + NAND_DIR DIR_SEP; 741 paths[UserPath::NANDDir] = user_path + NAND_DIR DIR_SEP;
740 break; 742 break;
741 } 743 }
742 } 744 }
743 745
744 return paths[DirIDX]; 746 return paths[path];
745} 747}
746 748
747size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename) { 749size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename) {
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 1f38b1560..ff01bf0ff 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -16,21 +16,20 @@
16#include "common/string_util.h" 16#include "common/string_util.h"
17#endif 17#endif
18 18
19// User directory indices for GetUserPath
20enum {
21 D_USER_IDX,
22 D_ROOT_IDX,
23 D_CONFIG_IDX,
24 D_CACHE_IDX,
25 D_SDMC_IDX,
26 D_NAND_IDX,
27 D_SYSDATA_IDX,
28 D_LOGS_IDX,
29 NUM_PATH_INDICES
30};
31
32namespace FileUtil { 19namespace FileUtil {
33 20
21// User paths for GetUserPath
22enum class UserPath {
23 CacheDir,
24 ConfigDir,
25 LogDir,
26 NANDDir,
27 RootDir,
28 SDMCDir,
29 SysDataDir,
30 UserDir,
31};
32
34// FileSystem tree node/ 33// FileSystem tree node/
35struct FSTEntry { 34struct FSTEntry {
36 bool isDirectory; 35 bool isDirectory;
@@ -123,7 +122,7 @@ bool SetCurrentDir(const std::string& directory);
123 122
124// Returns a pointer to a string with a yuzu data dir in the user's home 123// Returns a pointer to a string with a yuzu data dir in the user's home
125// directory. To be used in "multi-user" mode (that is, installed). 124// directory. To be used in "multi-user" mode (that is, installed).
126const std::string& GetUserPath(const unsigned int DirIDX, const std::string& newPath = ""); 125const std::string& GetUserPath(UserPath path, const std::string& new_path = "");
127 126
128// Returns the path to where the sys file are 127// Returns the path to where the sys file are
129std::string GetSysDirectory(); 128std::string GetSysDirectory();