Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "Filesystem.hpp"
- #include "exceptions.hpp"
- #include "strings.hpp"
- #include <algorithm>
- #include <iostream>
- #include <filesystem>
- #include <fstream>
- #include <regex>
- using namespace Space;
- using namespace Util;
- using namespace std::tr2::sys;
- std::wstring Filesystem::CleanPath(const std::wstring & path) {
- std::wstring result = path;
- CleanPath(result);
- return result;
- }
- void Filesystem::CleanPath(std::wstring & path) {
- // Reverse paths correctly
- std::replace(path.begin(), path.end(), L'\\', L'/');
- // Remove leading ./ if it exists
- std::replace(path.begin(), path.end(), L"./", L"");
- }
- std::vector<std::string> Filesystem::List(const std::wstring & directoryName, const std::wstring & regexMatch, bool recursive) {
- std::vector<std::string> results;
- std::wregex srchEx(regexMatch);
- auto processFile = [&results, regexMatch, srchEx] (const directory_entry & entry) {
- if (!regexMatch.length() > 0 && !std::regex_search(std::wstring(entry.path().c_str()), srchEx))
- return false;
- if (entry.status().type() == file_type::regular) {
- results.push_back(string_cast<std::string, std::wstring>(CleanPath(entry.path())));
- }
- };
- if (recursive) {
- recursive_directory_iterator dir_iter(directoryName);
- for (const auto & dir : dir_iter) {
- processFile(dir);
- }
- } else {
- directory_iterator dir_iter(directoryName);
- for (const auto & dir : dir_iter) {
- processFile(dir);
- }
- }
- return results;
- }
- dumb_buffer Filesystem::Load(const std::wstring & filename) {
- std::ifstream filestream;
- filestream.open(filename, std::ios::binary || std::ios::in);
- if (!filestream.is_open())
- throw Exceptions::FileNotFound(string_cast<std::string, std::wstring>(filename), filestream.rdstate(), "Could not open file");
- return Load(filestream);
- }
- dumb_buffer Filesystem::Load(std::istream & file) {
- if (file.good()) {
- std::streampos size = Size(file);
- dumb_buffer ret_buffer(size);
- file.read(&ret_buffer[0], size);
- if (file.gcount() != size)
- throw Exceptions::FileUknownError("stream", file.gcount(), "Read size did not match requested/expected size");
- return ret_buffer;
- }
- throw Exceptions::FileUknownError("stream", file.rdstate(), "bad bit was set on file stream");
- }
- std::streampos Filesystem::Size(const std::wstring & filename) {
- std::streampos fsize = 0;
- std::ifstream filestream(filename, std::ios::binary);
- if (!filestream.is_open())
- throw Exceptions::FileNotFound(string_cast<std::string, std::wstring>(filename), filestream.rdstate(), "Could not open file");
- fsize = filestream.tellg();
- filestream.seekg(0, std::ios::end);
- fsize = filestream.tellg() - fsize;
- return fsize;
- }
- std::streampos Filesystem::Size(std::istream & filestream) {
- std::streampos fsize = 0;
- fsize = filestream.tellg();
- filestream.seekg(0, std::ios::end);
- fsize = filestream.tellg() - fsize;
- filestream.seekg(0, std::ios::beg);
- return fsize;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement