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