Scarab  v3.4.4
Project 8 C++ Utility Library
param_yaml.cc
Go to the documentation of this file.
1 /*
2  * param_yaml.cc
3  *
4  * Created on: Jan 19, 2016
5  * Author: bidishasen97
6  */
7 
8 #define SCARAB_API_EXPORTS
9 
10 #include "param_yaml.hh"
11 
12 #include "logger.hh"
13 #include "param.hh"
14 
15 #include <sstream>
16 #include <fstream>
17 
18 using std::string;
19 using std::stringstream;
20 
21 namespace scarab
22 {
23  LOGGER( slog, "param_yaml" );
24 
25  REGISTER_PARAM_INPUT_CODEC( param_input_yaml, "yaml" );
26 
28  {}
29 
31  {}
32 
33  param_ptr_t param_input_yaml::read_file( const std::string& a_filename, const param_node& )
34  {
35  try
36  {
37  YAML::Node root_node = YAML::LoadFile( a_filename );
38  return param_input_yaml::read_node_type( root_node );
39  }
40  catch( YAML::Exception& e )
41  {
42  LERROR( slog, "YAML error: " << e.what() );
43  return std::unique_ptr< param >();
44  }
45  }
46 
47  std::unique_ptr< param > param_input_yaml::read_string( const std::string& a_string, const param_node& )
48  {
49  try
50  {
51  YAML::Node root_node = YAML::Load( a_string );
52  return param_input_yaml::read_node_type( root_node );
53  }
54  catch( YAML::Exception& e )
55  {
56  LERROR( slog, "YAML error: " << e.what() );
57  return std::unique_ptr< param >();
58  }
59  }
60 
61  param_ptr_t param_input_yaml::read_node_type( const YAML::Node& a_node )
62  {
63  try
64  {
65  if( a_node.IsNull() )
66  {
67  return param_ptr_t( new param() );
68  }
69  if( a_node.IsScalar() )
70  {
71  return param_input_yaml::scalar_handler(a_node);
72  }
73  if( a_node.IsSequence() )
74  {
76  }
77  if( a_node.IsMap() )
78  {
79  return param_input_yaml::map_handler(a_node);
80  }
81  }
82  catch( YAML::Exception& e )
83  {
84  LERROR(slog, "YAML error in read_node_type: " << e.what());
85  throw error() << "YAML error: " << e.what();
86  }
87  LDEBUG( slog, "YAML unknown" );
88  throw error() << "Unknown YAML encountered";
89  return std::unique_ptr< param >();
90  }
91 
92  std::unique_ptr< param_array > param_input_yaml::sequence_handler( const YAML::Node& a_node )
93  {
94  try
95  {
96  std::unique_ptr< param_array > t_array_as_param( new param_array() );
97 
98  for( YAML::const_iterator counter = a_node.begin(); counter != a_node.end(); ++counter )
99  {
100  t_array_as_param->push_back( param_input_yaml::read_node_type( *counter ) );
101  }
102 
103  return t_array_as_param;
104  }
105  catch( YAML::Exception& e )
106  {
107  LERROR( slog, "YAML error in sequence_handler: " << e.what() );
108  return std::unique_ptr< param_array >();
109  }
110  }
111 
112  std::unique_ptr< param_node > param_input_yaml::map_handler( const YAML::Node& a_node )
113  {
114  try
115  {
116  std::unique_ptr< param_node > t_map_as_param( new param_node() );
117 
118  for( YAML::const_iterator counter = a_node.begin(); counter != a_node.end(); ++counter )
119  {
120  t_map_as_param->replace( counter->first.as< std::string >(), param_input_yaml::read_node_type( counter->second ) );
121  }
122 
123  return t_map_as_param;
124  }
125  catch( YAML::Exception& e )
126  {
127  LERROR( slog, "YAML error in map_handler: " << e.what() );
128  return std::unique_ptr< param_node >();
129  }
130  }
131 
132  std::unique_ptr< param_value > param_input_yaml::scalar_handler( const YAML::Node& a_node )
133  {
134  try
135  {
136  // the order of these try statements matters!
137  try
138  {
139  return std::unique_ptr< param_value >( new param_value( a_node.as< unsigned >() ) );
140  }
141  catch( const YAML::BadConversion& )
142  {
143  try
144  {
145  return std::unique_ptr< param_value >( new param_value( a_node.as< int >() ) );
146  }
147  catch( const YAML::BadConversion& )
148  {
149  try
150  {
151  return std::unique_ptr< param_value >( new param_value( a_node.as< double >() ) );
152  }
153  catch( const YAML::BadConversion& )
154  {
155  try
156  {
157  return std::unique_ptr< param_value >( new param_value( a_node.as< bool >() ) );
158  }
159  catch( const YAML::BadConversion& )
160  {
161  return std::unique_ptr< param_value >( new param_value( a_node.Scalar() ) );
162  }
163  }
164  }
165  }
166  }
167  catch ( YAML::Exception& e )
168  {
169  LERROR( slog, "YAML error in scalar_handler: " << e.what() )
170  return std::unique_ptr< param_value >();
171  }
172  return std::unique_ptr< param_value >();
173  }
174 
175 
177 
179  {}
180 
182  {}
183 
184  bool param_output_yaml::write_file( const param& a_to_write, const std::string& a_filename, const param_node& )
185  {
186  if( a_filename.empty() )
187  {
188  LERROR( slog, "Filename cannot be an empty string" );
189  return false;
190  }
191 
192  YAML::Node a_node = param_output_yaml::check_param_type(a_to_write);
193 
194  std::ofstream fout( a_filename.c_str() );
195  if (! fout.is_open() )
196  {
197  LERROR( slog, "Unable to open file: " << a_filename );
198  return false;
199  }
200 
201  fout << a_node;
202 
203  fout.close();
204 
205  return true;
206  }
207 
208  bool param_output_yaml::write_string( const param& a_to_write, std::string& a_string, const param_node& )
209  {
210  YAML::Node a_node = param_output_yaml::check_param_type( a_to_write );
211 
212  std::stringstream t_out;
213  t_out << a_node;
214  a_string = t_out.str();
215 
216  return true;
217  }
218 
219  YAML::Node param_output_yaml::check_param_type( const param& a_to_write )
220  {
221  if( a_to_write.is_null() )
222  {
223  return YAML::Node();
224  }
225  else if( a_to_write.is_node() )
226  {
227  return param_output_yaml::param_node_handler( a_to_write );
228  }
229  else if( a_to_write.is_array() )
230  {
231  return param_output_yaml::param_array_handler( a_to_write );
232  }
233  else if( a_to_write.is_value() )
234  {
235  return param_output_yaml::param_value_handler( a_to_write );
236  }
237  LWARN( slog, "Unknown param type encountered" );
238  return YAML::Node();
239  }
240 
241  YAML::Node param_output_yaml::param_node_handler( const param& a_to_write )
242  {
243  YAML::Node t_node;
244 
245  const param_node& p_node = a_to_write.as_node();
246  for( param_node::const_iterator counter = p_node.begin(); counter != p_node.end(); ++counter )
247  {
248  t_node[counter.name()] = param_output_yaml::check_param_type( *counter );
249  }
250 
251  return t_node;
252  }
253 
254  YAML::Node param_output_yaml::param_array_handler( const param& a_to_write )
255  {
256  YAML::Node t_node;
257 
258  const param_array& array = a_to_write.as_array();
259  for ( int count = 0; count != (int) array.size(); ++count )
260  {
261  t_node.push_back(param_output_yaml::check_param_type( array[count] ) );
262  }
263 
264  return t_node;
265  }
266 
267  YAML::Node param_output_yaml::param_value_handler( const param& a_to_write )
268  {
269  YAML::Node t_node;
270 
271  const param_value& value = static_cast< const param_value& >(a_to_write);
272  if( value.is_bool() )
273  {
274  return t_node = value.as_bool();
275  }
276 
277  else if( value.is_uint() )
278  {
279  return t_node = value.as_uint();
280  }
281 
282  else if( value.is_double() )
283  {
284  return t_node = value.as_double();
285  }
286 
287  else if( value.is_int() )
288  {
289  return t_node = value.as_int();
290  }
291 
292  else if( value.is_string() )
293  {
294  return t_node = value.as_string();
295  }
296 
297  LWARN( slog, "Unkown value type encountered" );
298  return YAML::Node();
299  }
300 }
301 /* namespace scarab */
302 
int64_t as_int() const
Definition: param_value.hh:524
bool is_bool() const
Definition: param_value.hh:489
#define LWARN(...)
Definition: logger.hh:393
virtual bool is_value() const
virtual param_ptr_t read_file(const std::string &a_filename, const param_node &a_options=param_node())
Definition: param_yaml.cc:33
virtual param_ptr_t read_string(const std::string &a_string, const param_node &a_options=param_node())
Definition: param_yaml.cc:47
iterator begin()
Definition: param_node.hh:328
std::unique_ptr< param_value > scalar_handler(const YAML::Node &a_node)
Definition: param_yaml.cc:132
LOGGER(mtlog, "authentication")
bool is_uint() const
Definition: param_value.hh:494
#define LERROR(...)
Definition: logger.hh:394
virtual bool write_string(const param &a_to_write, std::string &a_string, const param_node &a_options=param_node())
Definition: param_yaml.cc:208
double as_double() const
Definition: param_value.hh:529
virtual bool write_file(const param &a_to_write, const std::string &a_filename, const param_node &a_options=param_node())
Definition: param_yaml.cc:184
std::unique_ptr< param_node > map_handler(const YAML::Node &a_node)
Definition: param_yaml.cc:112
Contains the logger class and macros, based on Kasper&#39;s KLogger class.
virtual bool is_null() const
Convert Param to YAML.
Definition: param_yaml.hh:62
bool as_bool() const
Definition: param_value.hh:514
REGISTER_PARAM_INPUT_CODEC(param_input_json, "json")
#define LDEBUG(...)
Definition: logger.hh:389
virtual bool is_array() const
YAML::Node param_value_handler(const param &a_to_write)
Definition: param_yaml.cc:267
bool is_double() const
Definition: param_value.hh:504
virtual bool is_node() const
std::unique_ptr< param > param_ptr_t
Definition: param_base.hh:23
param_ptr_t read_node_type(const YAML::Node &a_node)
Definition: param_yaml.cc:61
YAML::Node param_array_handler(const param &a_to_write)
Definition: param_yaml.cc:254
REGISTER_PARAM_OUTPUT_CODEC(param_output_json, "json")
bool is_string() const
Definition: param_value.hh:509
param_node & as_node()
uint64_t as_uint() const
Definition: param_value.hh:519
param_array & as_array()
void push_back(const param &a_value)
Definition: param_array.hh:238
bool is_int() const
Definition: param_value.hh:499
YAML::Node check_param_type(const param &a_to_write)
Definition: param_yaml.cc:219
std::string as_string() const
Definition: param_value.hh:534
std::unique_ptr< param_array > sequence_handler(const YAML::Node &a_node)
Definition: param_yaml.cc:92
unsigned size() const
Definition: param_array.hh:162
YAML::Node param_node_handler(const param &a_to_write)
Definition: param_yaml.cc:241
virtual const char * what() const noexcept