Scarab  v1.6.0
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* 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 nullptr;
44  }
45  }
46 
47  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 nullptr;
58  }
59  }
60 
61  param* param_input_yaml::read_node_type( const YAML::Node& a_node )
62  {
63  try
64  {
65  if( a_node.IsNull() )
66  {
67  LDEBUG( slog, "YAML null" << a_node );
68  return new param();
69  }
70  if( a_node.IsScalar() )
71  {
72  LDEBUG( slog, "YAML scalar" << a_node );
73  return param_input_yaml::scalar_handler(a_node);
74  }
75  if( a_node.IsSequence() )
76  {
77  LDEBUG( slog, "YAML sequence" << a_node );
79  }
80  if( a_node.IsMap() )
81  {
82  LDEBUG( slog, "YAML map" << a_node );
83  return param_input_yaml::map_handler(a_node);
84  }
85  }
86  catch( YAML::Exception& e )
87  {
88  LERROR(slog, "YAML error in read_node_type: " << e.what());
89  throw error() << "YAML error: " << e.what();
90  }
91  LDEBUG( slog, "YAML unknown" );
92  throw error() << "Unknown YAML encountered";
93  return nullptr;
94  }
95 
96  param_array* param_input_yaml::sequence_handler( const YAML::Node& a_node )
97  {
98  try
99  {
100  param_array* t_config_array = new param_array();
101 
102  for( YAML::const_iterator counter = a_node.begin(); counter != a_node.end(); ++counter )
103  {
104  t_config_array->push_back( param_input_yaml::read_node_type( *counter ) );
105  }
106 
107  return t_config_array;
108  }
109  catch( YAML::Exception& e )
110  {
111  LERROR( slog, "YAML error in sequence_handler: " << e.what() );
112  return nullptr;
113  }
114  }
115 
116  param_node* param_input_yaml::map_handler( const YAML::Node& a_node )
117  {
118  try
119  {
120  param_node* t_config_object = new param_node();
121 
122  for( YAML::const_iterator counter = a_node.begin(); counter != a_node.end(); ++counter )
123  {
124  t_config_object->replace( counter->first.as< std::string >(), param_input_yaml::read_node_type( counter->second ) );
125  }
126 
127  return t_config_object;
128  }
129  catch( YAML::Exception& e )
130  {
131  LERROR( slog, "YAML error in map_handler: " << e.what() );
132  return nullptr;
133  }
134  }
135 
136  param_value* param_input_yaml::scalar_handler( const YAML::Node& a_node )
137  {
138  try
139  {
140  // YAML::Node stores values as strings (in YAML::detail::node_data), so don't worry about trying different value types
141  return new param_value( a_node.Scalar() );
142  }
143  catch ( YAML::Exception& e )
144  {
145  LERROR( slog, "YAML error in scalar_handler: " << e.what() )
146  return nullptr;
147  }
148  }
149 
150 
152 
154  {}
155 
157  {}
158 
159  bool param_output_yaml::write_file( const param& a_to_write, const std::string& a_filename, const param_node* )
160  {
161  if( a_filename.empty() )
162  {
163  LERROR( slog, "Filename cannot be an empty string" );
164  return false;
165  }
166 
167  FILE* file = fopen( a_filename.c_str(), "w" );
168 
169  if (file == NULL)
170  {
171  LERROR( slog, "Unable to open file: " << a_filename );
172  return false;
173  }
174 
175  YAML::Node a_node = param_output_yaml::check_param_type(a_to_write);
176 
177  std::ofstream fout( a_filename.c_str() );
178  fout << a_node;
179  fclose( file );
180 
181  return true;
182  }
183 
184  bool param_output_yaml::write_string( const param& a_to_write, std::string& a_string, const param_node* )
185  {
186  YAML::Node a_node = param_output_yaml::check_param_type( a_to_write );
187 
188  std::stringstream t_out;
189  t_out << a_node;
190  a_string = t_out.str();
191 
192  return true;
193  }
194 
195  YAML::Node param_output_yaml::check_param_type( const param& a_to_write )
196  {
197  if( a_to_write.is_null() )
198  {
199  return YAML::Node();
200  }
201  else if( a_to_write.is_node() )
202  {
203  return param_output_yaml::param_node_handler( a_to_write );
204  }
205  else if( a_to_write.is_array() )
206  {
207  return param_output_yaml::param_array_handler( a_to_write );
208  }
209  else if( a_to_write.is_value() )
210  {
211  return param_output_yaml::param_value_handler( a_to_write );
212  }
213  LWARN( slog, "Unknown param type encountered" );
214  return YAML::Node();
215  }
216 
217  YAML::Node param_output_yaml::param_node_handler( const param& a_to_write )
218  {
219  YAML::Node t_node;
220 
221  const param_node& p_node = static_cast< const param_node& >(a_to_write);
222  for( param_node::const_iterator counter = p_node.begin(); counter != p_node.end(); ++counter )
223  {
224  t_node[counter->first] = param_output_yaml::check_param_type( *counter->second );
225  }
226 
227  return t_node;
228  }
229 
230  YAML::Node param_output_yaml::param_array_handler( const param& a_to_write )
231  {
232  YAML::Node t_node;
233 
234  const param_array array = static_cast< const param_array& >( a_to_write );
235  for ( int count = 0; count != (int) array.size(); ++count )
236  {
237  t_node.push_back(param_output_yaml::check_param_type( *array.at( count ) ) );
238  }
239 
240  return t_node;
241  }
242 
243  YAML::Node param_output_yaml::param_value_handler( const param& a_to_write )
244  {
245  YAML::Node t_node;
246 
247  const param_value& value = static_cast< const param_value& >(a_to_write);
248  if( value.is_bool() )
249  {
250  return t_node = value.as_bool();
251  }
252 
253  else if( value.is_uint() )
254  {
255  return t_node = value.as_uint();
256  }
257 
258  else if( value.is_double() )
259  {
260  return t_node = value.as_double();
261  }
262 
263  else if( value.is_int() )
264  {
265  return t_node = value.as_int();
266  }
267 
268  else if( value.is_string() )
269  {
270  return t_node = value.as_string();
271  }
272 
273  LWARN( slog, "Unkown value type encountered" );
274  return YAML::Node();
275  }
276 }
277 /* namespace scarab */
278 
virtual bool is_node() const
Definition: param.hh:420
void replace(const std::string &a_name, const param &a_value)
creates a copy of a_value
Definition: param.hh:1142
REGISTER_PARAM_INPUT_CODEC(param_input_json,"json")
#define LWARN(...)
Definition: logger.hh:364
REGISTER_PARAM_OUTPUT_CODEC(param_output_json,"json")
virtual bool write_string(const param &a_to_write, std::string &a_string, const param_node *a_options=nullptr)
Definition: param_yaml.cc:184
virtual param * read_string(const std::string &a_json_str, const param_node *a_options=nullptr)
Definition: param_yaml.cc:47
iterator begin()
Definition: param.hh:1189
virtual bool is_value() const
Definition: param.hh:410
param_value * scalar_handler(const YAML::Node &a_node)
Definition: param_yaml.cc:136
bool is_uint() const
Definition: param.hh:578
int64_t as_int() const
Definition: param.cc:286
#define LERROR(...)
Definition: logger.hh:365
bool is_bool() const
Definition: param.hh:573
virtual const char * what() const
Definition: error.cc:25
virtual bool write_file(const param &a_to_write, const std::string &a_filename, const param_node *a_options=nullptr)
Definition: param_yaml.cc:159
Contains the logger class and macros, based on Kasper&#39;s KLogger class.
bool is_int() const
Definition: param.hh:583
Convert YAMl to JSON.
Definition: param_yaml.hh:62
unsigned size() const
Definition: param.hh:744
contents::const_iterator const_iterator
Definition: param.hh:305
#define LDEBUG(...)
Definition: logger.hh:360
iterator end()
Definition: param.hh:1199
YAML::Node param_value_handler(const param &a_to_write)
Definition: param_yaml.cc:243
const param * at(unsigned a_index) const
Definition: param.hh:772
virtual bool is_null() const
Definition: param.hh:405
virtual param * read_file(const std::string &a_filename, const param_node *a_options=nullptr)
Definition: param_yaml.cc:33
bool is_double() const
Definition: param.hh:588
YAML::Node param_array_handler(const param &a_to_write)
Definition: param_yaml.cc:230
const std::string & as_string() const
Definition: param.cc:318
bool is_string() const
Definition: param.hh:593
bool as_bool() const
Definition: param.cc:242
void push_back(const param &a_value)
Definition: param.hh:858
double as_double() const
Definition: param.cc:302
YAML::Node check_param_type(const param &a_to_write)
Definition: param_yaml.cc:195
param_array * sequence_handler(const YAML::Node &a_node)
Definition: param_yaml.cc:96
uint64_t as_uint() const
Definition: param.cc:270
param_node * map_handler(const YAML::Node &a_node)
Definition: param_yaml.cc:116
virtual bool is_array() const
Definition: param.hh:415
YAML::Node param_node_handler(const param &a_to_write)
Definition: param_yaml.cc:217
param * read_node_type(const YAML::Node &a_node)
Definition: param_yaml.cc:61
LOGGER(mtlog,"authentication")