summaryrefslogtreecommitdiff
path: root/src/common/file_util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/file_util.cpp')
-rw-r--r--src/common/file_util.cpp107
1 files changed, 81 insertions, 26 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 7213abe18..bf955386c 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -2,6 +2,7 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <sstream>
5#include "common/assert.h" 6#include "common/assert.h"
6#include "common/common_funcs.h" 7#include "common/common_funcs.h"
7#include "common/common_paths.h" 8#include "common/common_paths.h"
@@ -386,7 +387,7 @@ u64 GetSize(FILE* f) {
386bool CreateEmptyFile(const std::string& filename) { 387bool CreateEmptyFile(const std::string& filename) {
387 LOG_TRACE(Common_Filesystem, "{}", filename); 388 LOG_TRACE(Common_Filesystem, "{}", filename);
388 389
389 if (!FileUtil::IOFile(filename, "wb")) { 390 if (!FileUtil::IOFile(filename, "wb").IsOpen()) {
390 LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); 391 LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg());
391 return false; 392 return false;
392 } 393 }
@@ -750,7 +751,7 @@ size_t WriteStringToFile(bool text_file, const std::string& str, const char* fil
750size_t ReadFileToString(bool text_file, const char* filename, std::string& str) { 751size_t ReadFileToString(bool text_file, const char* filename, std::string& str) {
751 IOFile file(filename, text_file ? "r" : "rb"); 752 IOFile file(filename, text_file ? "r" : "rb");
752 753
753 if (!file) 754 if (!file.IsOpen())
754 return false; 755 return false;
755 756
756 str.resize(static_cast<u32>(file.GetSize())); 757 str.resize(static_cast<u32>(file.GetSize()));
@@ -799,6 +800,71 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam
799 } 800 }
800} 801}
801 802
803std::vector<std::string> SplitPathComponents(const std::string& filename) {
804 auto copy(filename);
805 std::replace(copy.begin(), copy.end(), '\\', '/');
806 std::vector<std::string> out;
807
808 std::stringstream stream(filename);
809 std::string item;
810 while (std::getline(stream, item, '/'))
811 out.push_back(std::move(item));
812
813 return out;
814}
815
816std::string GetParentPath(const std::string& path) {
817 auto out = path;
818 const auto name_bck_index = out.find_last_of('\\');
819 const auto name_fwd_index = out.find_last_of('/');
820 size_t name_index;
821 if (name_bck_index == std::string::npos || name_fwd_index == std::string::npos)
822 name_index = std::min<size_t>(name_bck_index, name_fwd_index);
823 else
824 name_index = std::max<size_t>(name_bck_index, name_fwd_index);
825
826 return out.erase(name_index);
827}
828
829std::string GetPathWithoutTop(std::string path) {
830 if (path.empty())
831 return "";
832 while (path[0] == '\\' || path[0] == '/') {
833 path = path.substr(1);
834 if (path.empty())
835 return "";
836 }
837 const auto name_bck_index = path.find_first_of('\\');
838 const auto name_fwd_index = path.find_first_of('/');
839 return path.substr(std::min<size_t>(name_bck_index, name_fwd_index) + 1);
840 return path.substr(std::min<size_t>(name_bck_index, name_fwd_index) + 1);
841}
842
843std::string GetFilename(std::string path) {
844 std::replace(path.begin(), path.end(), '\\', '/');
845 auto name_index = path.find_last_of('/');
846 if (name_index == std::string::npos)
847 return "";
848 return path.substr(name_index + 1);
849}
850
851std::string GetExtensionFromFilename(const std::string& name) {
852 size_t index = name.find_last_of('.');
853 if (index == std::string::npos)
854 return "";
855
856 return name.substr(index + 1);
857}
858
859std::string RemoveTrailingSlash(const std::string& path) {
860 if (path.empty())
861 return path;
862 if (path.back() == '\\' || path.back() == '/')
863 return path.substr(0, path.size() - 1);
864
865 return path;
866}
867
802IOFile::IOFile() {} 868IOFile::IOFile() {}
803 869
804IOFile::IOFile(const std::string& filename, const char openmode[], int flags) { 870IOFile::IOFile(const std::string& filename, const char openmode[], int flags) {
@@ -820,7 +886,6 @@ IOFile& IOFile::operator=(IOFile&& other) noexcept {
820 886
821void IOFile::Swap(IOFile& other) noexcept { 887void IOFile::Swap(IOFile& other) noexcept {
822 std::swap(m_file, other.m_file); 888 std::swap(m_file, other.m_file);
823 std::swap(m_good, other.m_good);
824} 889}
825 890
826bool IOFile::Open(const std::string& filename, const char openmode[], int flags) { 891bool IOFile::Open(const std::string& filename, const char openmode[], int flags) {
@@ -837,16 +902,15 @@ bool IOFile::Open(const std::string& filename, const char openmode[], int flags)
837 m_file = fopen(filename.c_str(), openmode); 902 m_file = fopen(filename.c_str(), openmode);
838#endif 903#endif
839 904
840 m_good = IsOpen(); 905 return IsOpen();
841 return m_good;
842} 906}
843 907
844bool IOFile::Close() { 908bool IOFile::Close() {
845 if (!IsOpen() || 0 != std::fclose(m_file)) 909 if (!IsOpen() || 0 != std::fclose(m_file))
846 m_good = false; 910 return false;
847 911
848 m_file = nullptr; 912 m_file = nullptr;
849 return m_good; 913 return true;
850} 914}
851 915
852u64 IOFile::GetSize() const { 916u64 IOFile::GetSize() const {
@@ -856,11 +920,8 @@ u64 IOFile::GetSize() const {
856 return 0; 920 return 0;
857} 921}
858 922
859bool IOFile::Seek(s64 off, int origin) { 923bool IOFile::Seek(s64 off, int origin) const {
860 if (!IsOpen() || 0 != fseeko(m_file, off, origin)) 924 return IsOpen() && 0 == fseeko(m_file, off, origin);
861 m_good = false;
862
863 return m_good;
864} 925}
865 926
866u64 IOFile::Tell() const { 927u64 IOFile::Tell() const {
@@ -871,26 +932,20 @@ u64 IOFile::Tell() const {
871} 932}
872 933
873bool IOFile::Flush() { 934bool IOFile::Flush() {
874 if (!IsOpen() || 0 != std::fflush(m_file)) 935 return IsOpen() && 0 == std::fflush(m_file);
875 m_good = false;
876
877 return m_good;
878} 936}
879 937
880bool IOFile::Resize(u64 size) { 938bool IOFile::Resize(u64 size) {
881 if (!IsOpen() || 0 != 939 return IsOpen() && 0 ==
882#ifdef _WIN32 940#ifdef _WIN32
883 // ector: _chsize sucks, not 64-bit safe 941 // ector: _chsize sucks, not 64-bit safe
884 // F|RES: changed to _chsize_s. i think it is 64-bit safe 942 // F|RES: changed to _chsize_s. i think it is 64-bit safe
885 _chsize_s(_fileno(m_file), size) 943 _chsize_s(_fileno(m_file), size)
886#else 944#else
887 // TODO: handle 64bit and growing 945 // TODO: handle 64bit and growing
888 ftruncate(fileno(m_file), size) 946 ftruncate(fileno(m_file), size)
889#endif 947#endif
890 ) 948 ;
891 m_good = false;
892
893 return m_good;
894} 949}
895 950
896} // namespace FileUtil 951} // namespace FileUtil