summaryrefslogtreecommitdiff
path: root/src/common/file_util.cpp
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/file_util.cpp
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 '')
-rw-r--r--src/common/file_util.cpp68
1 files changed, 35 insertions, 33 deletions
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) {