Scarab  v3.9.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( YAML::TypedBadConversion<unsigned>& )
142  {
143  try
144  {
145  return std::unique_ptr< param_value >( new param_value( a_node.as< int >() ) );
146  }
147  catch( const YAML::TypedBadConversion<int>& )
148  {
149  try
150  {
151  return std::unique_ptr< param_value >( new param_value( a_node.as< double >() ) );
152  }
153  catch( const YAML::TypedBadConversion<double>& )
154  {
155  try
156  {
157  return std::unique_ptr< param_value >( new param_value( a_node.as< bool >() ) );
158  }
159  catch( const YAML::TypedBadConversion<bool>& )
160  {
161  try
162  {
163  return std::unique_ptr< param_value >( new param_value( a_node.as< std::string >() ) );
164  }
165  catch( const YAML::TypedBadConversion<std::string>& )
166  {
167  return std::unique_ptr< param_value >( new param_value( a_node.Scalar() ) );
168  }
169  }
170  }
171  }
172  }
173  }
174  catch ( YAML::Exception& e )
175  {
176  LERROR( slog, "YAML error in scalar_handler: " << e.what() )
177  return std::unique_ptr< param_value >();
178  }
179  return std::unique_ptr< param_value >();
180  }
181 
182 
184 
186  {}
187 
189  {}
190 
191  bool param_output_yaml::write_file( const param& a_to_write, const std::string& a_filename, const param_node& )
192  {
193  if( a_filename.empty() )
194  {
195  LERROR( slog, "Filename cannot be an empty string" );
196  return false;
197  }
198 
199  YAML::Node a_node = param_output_yaml::check_param_type(a_to_write);
200 
201  std::ofstream fout( a_filename.c_str() );
202  if (! fout.is_open() )
203  {
204  LERROR( slog, "Unable to open file: " << a_filename );
205  return false;
206  }
207 
208  fout << a_node;
209 
210  fout.close();
211 
212  return true;
213  }
214 
215  bool param_output_yaml::write_string( const param& a_to_write, std::string& a_string, const param_node& )
216  {
217  YAML::Node a_node = param_output_yaml::check_param_type( a_to_write );
218 
219  std::stringstream t_out;
220  t_out << a_node;
221  a_string = t_out.str();
222 
223  return true;
224  }
225 
226  YAML::Node param_output_yaml::check_param_type( const param& a_to_write )
227  {
228  if( a_to_write.is_null() )
229  {
230  return YAML::Node();
231  }
232  else if( a_to_write.is_node() )
233  {
234  return param_output_yaml::param_node_handler( a_to_write );
235  }
236  else if( a_to_write.is_array() )
237  {
238  return param_output_yaml::param_array_handler( a_to_write );
239  }
240  else if( a_to_write.is_value() )
241  {
242  return param_output_yaml::param_value_handler( a_to_write );
243  }
244  LWARN( slog, "Unknown param type encountered" );
245  return YAML::Node();
246  }
247 
248  YAML::Node param_output_yaml::param_node_handler( const param& a_to_write )
249  {
250  YAML::Node t_node;
251 
252  const param_node& p_node = a_to_write.as_node();
253  for( param_node::const_iterator counter = p_node.begin(); counter != p_node.end(); ++counter )
254  {
255  t_node[counter.name()] = param_output_yaml::check_param_type( *counter );
256  }
257 
258  return t_node;
259  }
260 
261  YAML::Node param_output_yaml::param_array_handler( const param& a_to_write )
262  {
263  YAML::Node t_node;
264 
265  const param_array& array = a_to_write.as_array();
266  for ( int count = 0; count != (int) array.size(); ++count )
267  {
268  t_node.push_back(param_output_yaml::check_param_type( array[count] ) );
269  }
270 
271  return t_node;
272  }
273 
274  YAML::Node param_output_yaml::param_value_handler( const param& a_to_write )
275  {
276  YAML::Node t_node;
277 
278  const param_value& value = static_cast< const param_value& >(a_to_write);
279  if( value.is_bool() )
280  {
281  return t_node = value.as_bool();
282  }
283 
284  else if( value.is_uint() )
285  {
286  return t_node = value.as_uint();
287  }
288 
289  else if( value.is_double() )
290  {
291  return t_node = value.as_double();
292  }
293 
294  else if( value.is_int() )
295  {
296  return t_node = value.as_int();
297  }
298 
299  else if( value.is_string() )
300  {
301  return t_node = value.as_string();
302  }
303 
304  LWARN( slog, "Unkown value type encountered" );
305  return YAML::Node();
306  }
307 }
308 /* namespace scarab */
309 
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:215
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:191
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:274
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:261
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:226
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:248
virtual const char * what() const noexcept