iutest  1.17.1.0
iutest_genparams_from_file.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_GENPARAMS_FROM_FILE_HPP_81CDA6E7_945B_4F72_873B_104264DF7132_
16 #define INCG_IRIS_IUTEST_GENPARAMS_FROM_FILE_HPP_81CDA6E7_945B_4F72_873B_104264DF7132_
17 
18 #if IUTEST_HAS_CSVPARAMS
19 
20 namespace iutest {
21 namespace detail
22 {
23 
28 template<typename T>
29 class iuCsvFileParamsGenerator : public iuValuesInParamsGenerator<T>
30 {
31  typedef ::std::vector<T> params_t;
32 public:
38  explicit iuCsvFileParamsGenerator(const ::std::string& path, char delimiter = ',')
39  : iuValuesInParamsGenerator<T>(ReadParams(path, delimiter))
40  {
41  }
42 
43 private:
44  void AppendParams(params_t& params, const ::std::string& data)
45  {
46  if( StringIsBlank(data) )
47  {
48  return;
49  }
50  T param;
51  if( !StringToValue(data, param) )
52  {
53  return;
54  }
55  params.push_back(param);
56  }
57  params_t ReadParams(const ::std::string& path, char delimiter = ',')
58  {
59  params_t params;
60  IFile* fp = detail::IFileSystem::New();
61  if( (fp != NULL) && fp->Open(path.c_str(), IFile::OpenRead) )
62  {
63  const ::std::string dataset = fp->ReadAll();
64  ::std::string::size_type prev = 0;
65  ::std::string::size_type pos = 0;
66  while( static_cast<void>(pos = dataset.find(delimiter, prev)), pos != ::std::string::npos )
67  {
68  const ::std::string data = dataset.substr(prev, pos - prev);
69  AppendParams(params, data);
70  ++pos;
71  prev = pos;
72  }
73  AppendParams(params, dataset.substr(prev));
74 
75  if( params.empty() )
76  {
77  IUTEST_LOG_(WARNING) << "Empty params file \"" << path << "\".";
78  }
79  }
80  else
81  {
82  IUTEST_LOG_(WARNING) << "Unable to open file \"" << path << "\".";
83  }
84  detail::IFileSystem::Free(fp);
85  return params;
86  }
87 };
88 
89 } // end of namespace detail
90 } // end of namespace iutest
91 
92 #endif
93 
94 #endif // INCG_IRIS_IUTEST_GENPARAMS_FROM_FILE_HPP_81CDA6E7_945B_4F72_873B_104264DF7132_
iutest_config.hpp
iris unit test config
iutest
iutest root namespace
Definition: iutest_charcode.hpp:31
IUTEST_LOG_
#define IUTEST_LOG_(level)
ログメッセージストリーム
Definition: iutest_port.hpp:53
iutest::IFile::OpenRead
@ OpenRead
読み込み
Definition: iutest_file.hpp:38