SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
load.hpp
Go to the documentation of this file.
1 /*
2 Copyrightc 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_LOAD_HPP
9 #define SIG_UTIL_LOAD_HPP
10 
11 #include "../helper/helper_modules.hpp"
12 #include "../helper/maybe.hpp"
13 
14 #include <fstream>
15 #include <locale>
16 
17 
19 
20 namespace sig
21 {
22 
23 namespace impl
24 {
25 template <class T>
26 using IfsSelector = typename SameIf<
27  T, std::string,
28  std::ifstream,
29  typename SameIf<
30  T, std::wstring,
31  std::wifstream,
32  std::basic_ifstream<T>
33  >::type
34 >::type;
35 
36 } // impl
37 
39 
41 
47 template <
48  class IFS_CHAR,
49  class C,
50  class R = typename impl::container_traits<C>::value_type
51 >
52 bool load_line(
53  C& empty_dest,
54  std::basic_ifstream<IFS_CHAR>& ifs)
55 {
56  std::basic_string<IFS_CHAR> line;
57 
59 
60  while (ifs && std::getline(ifs, line)){
61  impl::container_traits<C>::add_element(empty_dest, std::move(line));
62  }
63  return static_cast<bool>(ifs);
64 }
65 
67 
81 template <
82  class C,
83  class R = typename impl::container_traits<C>::value_type
84 >
85 bool load_line(
86  C& empty_dest,
87  FilepassString const& file_pass)
88 {
89  impl::IfsSelector<R> ifs(file_pass);
90 
91  if (!ifs){
92  //FileOpenErrorPrint(file_pass);
93  return false;
94  }
95  return load_line(empty_dest, ifs);
96 }
97 
99 
101 
102 /*
103 // ファイルから1行ずつ読み込み、結果を返す(ifstreamを指定)
104 // R: 返す文字列型(std::string or std::wstring)
105 // ifs: std::ifstream or std::wifstream
106 // 読込失敗: return -> nothing (boost非使用時は空のコンテナ)
107 template <class IST, class R = IST, class C = std::vector<R>>
108 auto load_line(impl::IfsSelector<IST>& ifs) ->Maybe<C>
109 {
110 C tmp;
111 load_line(tmp, ifs);
112 return tmp.size() ? Just<C>(std::move(tmp)) : Nothing(std::move(tmp));
113 }
114 */
115 
117 
133 template <
134  class ISTR = std::string,
135  class C = std::vector<ISTR>
136 >
137 auto load_line(FilepassString const& file_pass) ->Maybe<C>
138 {
139  C tmp;
140  impl::IfsSelector<ISTR> ifs(file_pass);
141 
142  if (!ifs){
143  //FileOpenErrorPrint(file_pass);
144  return Nothing(C());
145  }
146 
147  load_line(tmp, ifs);
148 
149  return tmp.empty() ? Nothing(tmp) : Just<C>(std::move(tmp));
150 }
151 
157 template <
158  class ISTR = std::string,
159  class R = ISTR,
160  class C = std::vector<R>
161 >
162 auto load_line(FilepassStringC file_pass) ->Maybe<C>
163 {
164  return load_line<ISTR, C>(static_cast<impl::string_t<FilepassStringC>>(file_pass));
165 }
166 
168 
170 
172 
177 template <
178  class IFS_CHAR,
179  class F,
180  class C,
181  class R = typename impl::container_traits<C>::value_type
182 >
184  C& empty_dest,
185  std::basic_ifstream<IFS_CHAR>& ifs,
186  F const& conv)
187 {
188  std::basic_string<IFS_CHAR> line;
189 
191 
192  while (ifs && std::getline(ifs, line)){
193  impl::container_traits<C>::add_element(empty_dest, conv(std::move(line)));
194  }
195  return static_cast<bool>(ifs);
196 }
197 
199 
227 template <
228  class ISTR = std::string,
229  class F,
230  class C,
231  class R = typename impl::container_traits<C>::value_type
232 >
234  C& empty_dest,
235  FilepassString const& file_pass,
236  F const& conv)
237 {
239  typename impl::SameIf<R, std::string, R,
241  >::type
242  > ifs(file_pass);
243 
244  if (!ifs){
245  //FileOpenErrorPrint(file_pass);
246  return false;
247  }
248  return load_line(empty_dest, ifs, conv);
249 }
250 
252 
254 
256 
289 template <
290  class C,
291  class RT = typename impl::container_traits<C>::value_type, typename std::enable_if<!impl::container_traits<typename impl::container_traits<C>::value_type>::exist>::type*& = enabler
292 >
293 bool load_num(
294  C& empty_dest,
295  FilepassString const& file_pass,
296  std::string delimiter = "\n")
297 {
298  auto read_str = load_line<std::string>(file_pass);
299 
300  if (!isJust(read_str)) return false;
301 
302  if (delimiter == "\n"){
303  for (auto const& line : fromJust(read_str)){
305  }
306  }
307  else{
308  auto sp = split(fromJust(read_str)[0], delimiter);
309  for (auto v : sp) impl::container_traits<C>::add_element(empty_dest, impl::Str2NumSelector<RT>()(v));
310  }
311  return true;
312 }
313 
315 
347 template <
348  class R,
349  class C = std::vector<R>, typename std::enable_if<impl::container_traits<C>::exist && !impl::container_traits<typename impl::container_traits<C>::value_type>::exist>::type*& = enabler
350 >
351 auto load_num(
352  FilepassString const& file_pass,
353  std::string delimiter = "\n"
354  ) ->Maybe<C>
355 {
356  C tmp;
357  load_num(tmp, file_pass, delimiter);
358  return tmp.size() ? Just<C>(std::move(tmp)) : Nothing(std::move(tmp));
359 }
360 
362 
364 
366 
392 template <
393  class CC,
394  class RC = typename impl::container_traits<CC>::value_type,
395  class RT = typename impl::container_traits<RC>::value_type
396 >
398  CC& empty_dest,
399  FilepassString const& file_pass,
400  std::string delimiter)
401 {
402  auto read_str = load_line<std::string>(file_pass);
403  if (!isJust(read_str)) return false;
404 
405  for (auto const& line : fromJust(read_str)){
406  RC tmp;
407  auto sp = split(line, delimiter);
408 
409  for (auto const& v : sp){
411  }
412  impl::container_traits<CC>::add_element(empty_dest, std::move(tmp));
413  }
414  return true;
415 }
416 
417 
419 
445 template <
446  class R,
447  class CC = std::vector<std::vector<R>>,
448  typename std::enable_if<impl::container_traits<typename impl::container_traits<CC>::value_type>::exist>::type*& = enabler
449 >
451  FilepassString const& file_pass,
452  std::string delimiter
453  ) ->Maybe<CC>
454 {
455  CC tmp;
456  load_num2d(tmp, file_pass, delimiter);
457  return tmp.size() ? Just<CC>(std::move(tmp)) : Nothing(std::move(tmp));
458 }
459 
461 
462 }
463 #endif
bool load_line(C &empty_dest, std::basic_ifstream< IFS_CHAR > &ifs)
ファイルから1行ずつ読み込む(ifstreamを指定)
Definition: load.hpp:52
auto split(S const &src, impl::string_t< S > const &delimiter) -> CSeq< TS >
文字列(src)をある文字列(delimiter)を目印に分割する
Definition: manipulate.hpp:40
void * enabler
auto Nothing() -> typename impl::SameIf< T, void, typename boost::none_t, Maybe< T >>::type
値コンストラクタ
Definition: maybe.hpp:67
bool isJust(Maybe< T > const &m)
Justであるか調べる関数.Maybe a -> Bool.
Definition: maybe.hpp:84
typename std::conditional< std::is_same< T1, T2 >::value, TrueT, FalseT >::type type
bool load_num2d(CC &empty_dest, FilepassString const &file_pass, std::string delimiter)
2次元配列の数値(ex:行列)を読み込む
Definition: load.hpp:397
typename SameIf< T, std::string, std::ifstream, typename SameIf< T, std::wstring, std::wifstream, std::basic_ifstream< T > >::type >::type IfsSelector
Definition: load.hpp:34
T & fromJust(Maybe< T > &m)
Justから値を取り出す関数.Maybe a -> a.
Definition: maybe.hpp:113
#define SIG_FILE_LOCALE_INIT
Definition: array.hpp:15
typename StringId< typename remove_const_reference< typename std::decay< T >::type >::type >::type string_t
bool load_num(C &empty_dest, FilepassString const &file_pass, std::string delimiter="\n")
数値列を読み込む
Definition: load.hpp:293