Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "Filesystem.hpp"
  4. #include "exceptions.hpp"
  5. #include "strings.hpp"
  6.  
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <filesystem>
  10. #include <fstream>
  11. #include <regex>
  12.  
  13. using namespace Space;
  14. using namespace Util;
  15.  
  16. using namespace std::tr2::sys;
  17.  
  18. std::wstring Filesystem::CleanPath(const std::wstring & path) {
  19.     std::wstring result = path;
  20.     CleanPath(result);
  21.     return result;
  22. }
  23.  
  24. void Filesystem::CleanPath(std::wstring & path) {
  25.     // Reverse paths correctly
  26.     std::replace(path.begin(), path.end(), L'\\', L'/');
  27.  
  28.     // Remove leading ./ if it exists
  29.     std::replace(path.begin(), path.end(), L"./", L"");
  30. }
  31.  
  32. std::vector<std::string> Filesystem::List(const std::wstring & directoryName, const std::wstring & regexMatch, bool recursive) {
  33.     std::vector<std::string> results;
  34.     std::wregex srchEx(regexMatch);
  35.  
  36.     auto processFile = [&results, regexMatch, srchEx] (const directory_entry & entry) {
  37.         if (!regexMatch.length() > 0 && !std::regex_search(std::wstring(entry.path().c_str()), srchEx))
  38.             return false;
  39.  
  40.         if (entry.status().type() == file_type::regular) {
  41.             results.push_back(string_cast<std::string, std::wstring>(CleanPath(entry.path())));
  42.         }
  43.     };
  44.  
  45.     if (recursive) {
  46.         recursive_directory_iterator dir_iter(directoryName);
  47.         for (const auto & dir : dir_iter) {
  48.             processFile(dir);
  49.         }
  50.     } else {
  51.         directory_iterator dir_iter(directoryName);
  52.         for (const auto & dir : dir_iter) {
  53.             processFile(dir);
  54.         }
  55.     }
  56.  
  57.     return results;
  58. }
  59. dumb_buffer Filesystem::Load(const std::wstring & filename) {
  60.     std::ifstream filestream;
  61.     filestream.open(filename, std::ios::binary || std::ios::in);
  62.     if (!filestream.is_open())
  63.         throw Exceptions::FileNotFound(string_cast<std::string, std::wstring>(filename), filestream.rdstate(), "Could not open file");
  64.    
  65.     return Load(filestream);
  66. }
  67. dumb_buffer Filesystem::Load(std::istream & file) {
  68.     if (file.good()) {
  69.         std::streampos size = Size(file);
  70.         dumb_buffer ret_buffer(size);
  71.  
  72.         file.read(&ret_buffer[0], size);
  73.         if (file.gcount() != size)
  74.             throw Exceptions::FileUknownError("stream", file.gcount(), "Read size did not match requested/expected size");
  75.  
  76.         return ret_buffer;
  77.     }
  78.     throw Exceptions::FileUknownError("stream", file.rdstate(), "bad bit was set on file stream");
  79. }
  80. std::streampos Filesystem::Size(const std::wstring & filename) {
  81.     std::streampos fsize = 0;
  82.    
  83.     std::ifstream filestream(filename, std::ios::binary);
  84.     if (!filestream.is_open())
  85.         throw Exceptions::FileNotFound(string_cast<std::string, std::wstring>(filename), filestream.rdstate(), "Could not open file");
  86.  
  87.     fsize = filestream.tellg();
  88.     filestream.seekg(0, std::ios::end);
  89.     fsize = filestream.tellg() - fsize;
  90.  
  91.     return fsize;
  92. }
  93. std::streampos Filesystem::Size(std::istream & filestream) {
  94.     std::streampos fsize = 0;
  95.  
  96.     fsize = filestream.tellg();
  97.     filestream.seekg(0, std::ios::end);
  98.     fsize = filestream.tellg() - fsize;
  99.     filestream.seekg(0, std::ios::beg);
  100.  
  101.     return fsize;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement