SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pass.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_PASS_HPP
9 #define SIG_UTIL_PASS_HPP
10 
11 #include "../helper/helper_modules.hpp"
12 #include "../helper/maybe.hpp"
13 #include "../string/manipulate.hpp"
14 
15 #if SIG_MSVC_ENV
16 #define NOMINMAX
17  #include <windows.h>
18 #elif SIG_USE_BOOST_FILESYSTEM
19  #include "../string/regex.hpp"
20  #include <boost/filesystem.hpp>
21  namespace fs = boost::filesystem;
22 #endif
23 
24 
26 
27 namespace sig
28 {
29 
31 
43 inline auto modify_dirpass_tail(FilepassString const& directory_pass, bool const has_slash) ->FilepassString
44 {
45  if (directory_pass.empty()) return directory_pass;
46 
47  auto tail = directory_pass.back();
48 
49  if (has_slash){
50  //付ける場合
51  if (tail == '/' || tail == '\\') return directory_pass;
52  else return directory_pass + SIG_TO_FPSTR("/");
53  }
54  else{
55  if (tail != '/' && tail != '\\') return directory_pass;
56  else{
57  auto tmp = directory_pass;
58  tmp.pop_back();
59  return tmp;
60  }
61  }
62 };
63 
64 
66 
75 inline auto get_file_names(
76  FilepassString const& directory_pass,
77  bool hidden_file,
78  std::wstring extension = L""
79 )
80  ->Maybe<std::vector<std::wstring>>
81 {
82  using ResultType = std::vector<std::wstring>;
83 
84  ResultType result;
85 
86 #if SIG_MSVC_ENV
87  WIN32_FIND_DATA fd;
88  auto query = extension.empty() ? L"?*" : L"*" + extension;
89  auto pass = modify_dirpass_tail(directory_pass, true) + query;
90  auto hFind = FindFirstFile(pass.c_str(), &fd);
91 
92  if (hFind == INVALID_HANDLE_VALUE){
93  return Nothing(std::move(result));
94  }
95  else{
96  do{
97  if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && Consistency(hidden_file, (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)))
98  {
99  result.push_back(std::wstring(fd.cFileName));
100  }
101  } while (FindNextFile(hFind, &fd));
102 
103  FindClose(hFind);
104  return Just<ResultType>(std::move(result));
105  }
106 #elif SIG_ENABLE_BOOST
107  auto IsHidden = [](fs::path const& p){
108  auto name = p.filename();
109  if (name.c_str()[0] == '.' && name != ".." && name != ".") return true;
110  else return false;
111  };
112 
113  fs::directory_iterator end;
114  for (fs::directory_iterator it(directory_pass); it != end; ++it)
115  {
116  if (!fs::is_directory(*it) && Consistency(hidden_file, IsHidden(*it))){
117  auto leaf = sig::split(it->path().wstring(), L"/").back();
118  if (extension.empty()) result.push_back(leaf);
119  else{
120  auto query = L".*(" + sig::escape_regex(extension) + L")";
121  auto ext = sig::regex_search(leaf, SIG_WRegex(query));
122  if (isJust(ext) && fromJust(ext)[0][1] == extension) result.push_back(leaf);
123  }
124  }
125  }
126  return Just<ResultType>(std::move(result));
127 #else
128  std::cout << "I don't support this envirnment which is default. please include boost if any." << std::endl;
129  assert(false);
130 #endif
131 }
132 
133 
135 
143 inline auto get_folder_names(
144  FilepassString const& directory_pass,
145  bool hidden_file
146 )
147  ->Maybe<std::vector<std::wstring>>
148 {
149  using ResultType = std::vector<std::wstring>;
150 
151  ResultType result;
152 
153 #if SIG_MSVC_ENV
154  WIN32_FIND_DATA fd;
155  auto pass = modify_dirpass_tail(directory_pass, true) + L"*";
156  auto hFind = FindFirstFile(pass.c_str(), &fd);
157 
158  if (hFind == INVALID_HANDLE_VALUE){
159  return Nothing(std::move(result));
160  }
161  else{
162  do{
163  if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && Consistency(hidden_file, (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)))
164  {
165  auto tmp = std::wstring(fd.cFileName);
166  if(tmp != L"." && tmp != L"..") result.push_back(tmp);
167  }
168  } while (FindNextFile(hFind, &fd));
169 
170  FindClose(hFind);
171  return Just<ResultType>(std::move(result));
172  }
173 #elif SIG_ENABLE_BOOST
174  auto IsHidden = [](fs::path const& p){
175  auto name = p.filename();
176  if (name.c_str()[0] == '.' && name != ".." && name != ".") return true;
177  else return false;
178  };
179 
180  fs::directory_iterator end;
181  for (fs::directory_iterator it(directory_pass); it != end; ++it)
182  {
183  if (fs::is_directory(*it) && Consistency(hidden_file, IsHidden(*it))){
184  result.push_back(sig::split(it->path().wstring(), L"/").back());
185  }
186  }
187  return Just<ResultType>(std::move(result));
188 #else
189  std::cout << "I don't support this envirnment which is default. please include boost if any." << std::endl;
190  assert(false);
191 #endif
192 }
193 
194 }
195 
196 #endif
std::wregex SIG_WRegex
Definition: regex.hpp:32
auto get_folder_names(FilepassString const &directory_pass, bool hidden_file) -> Maybe< std::vector< std::wstring >>
指定ディレクトリにあるフォルダ名を取得
Definition: pass.hpp:143
auto split(S const &src, impl::string_t< S > const &delimiter) -> CSeq< TS >
文字列(src)をある文字列(delimiter)を目印に分割する
Definition: manipulate.hpp:40
auto end(C &&c) -> std::move_iterator< typename RC::iterator >
auto Nothing() -> typename impl::SameIf< T, void, typename boost::none_t, Maybe< T >>::type
値コンストラクタ
Definition: maybe.hpp:67
auto modify_dirpass_tail(FilepassString const &directory_pass, bool const has_slash) -> FilepassString
ディレクトリ・ファイルパスの末尾に'/'or'\'があるかチェックし、付けるか外すかどうかを指定して反映 ...
Definition: pass.hpp:43
auto get_file_names(FilepassString const &directory_pass, bool hidden_file, std::wstring extension=L"") -> Maybe< std::vector< std::wstring >>
指定ディレクトリにあるファイル名を取得
Definition: pass.hpp:75
bool Consistency(B1 &&a, B2 &&b)
AとBの真偽一致でtrueを返す (⇔ !xor)
#define SIG_TO_FPSTR(str)
bool isJust(Maybe< T > const &m)
Justであるか調べる関数.Maybe a -> Bool.
Definition: maybe.hpp:84
T & fromJust(Maybe< T > &m)
Justから値を取り出す関数.Maybe a -> a.
Definition: maybe.hpp:113
auto regex_search(S &&src, typename impl::Str2RegexSelector< TS >::regex const &expression) -> Maybe< std::vector< std::vector< TS >>>
std::regex_search のラッパ関数
Definition: regex.hpp:172
auto escape_regex(std::string const &expression) -> std::string
与えられた文字中に含まれる、正規表現の特殊文字をエスケープする
Definition: regex.hpp:88
Definition: array.hpp:15