Scarab  v3.8.0
Project 8 C++ Utility Library
param_codec.cc
Go to the documentation of this file.
1 /*
2  * param_codec.cc
3  *
4  * Created on: Aug 10, 2016
5  * Author: obla999
6  */
7 
8 #define SCARAB_API_EXPORTS
9 
10 #include "param_codec.hh"
11 
12 #include "factory.hh"
13 #include "logger.hh"
14 #include "path.hh"
15 
16 LOGGER( slog, "param_codec" );
17 
18 namespace scarab
19 {
20 
22  {
23  }
24 
26  {
27  }
28 
29 
31  {
32  }
33 
35  {
36  }
37 
38 
40  {
41  }
42 
44  {
45  }
46 
47  param_ptr_t param_translator::read_file( const std::string& a_filename, const param_node& a_options )
48  {
49  std::string t_encoding = a_options.get_value( "encoding", "" );
50  if( t_encoding.empty() )
51  {
52  path t_path = expand_path( a_filename );
53  t_encoding = t_path.extension().string().substr( 1 ); // remove the '.' at the beginning
54  }
55 
57  if( t_codec == nullptr )
58  {
59  LERROR( slog, "Unable to find input codec for encoding <" << t_encoding << ">");
60  return nullptr;
61  }
62 
63  return t_codec->read_file( a_filename, a_options );
64  }
65 
66  param_ptr_t param_translator::read_string( const std::string& a_string, const param_node& a_options )
67  {
68  std::string t_encoding = a_options.get_value( "encoding", "" );
69  if( t_encoding.empty() )
70  {
71  LERROR( slog, "Encoding-type option must be provided");
72  return nullptr;
73  }
74 
76  if( t_codec == nullptr )
77  {
78  LERROR( slog, "Unable to find input codec for encoding <" << t_encoding << ">");
79  return nullptr;
80  }
81 
82  return t_codec->read_string( a_string, a_options );
83  }
84 
85  bool param_translator::write_file( const param& a_param, const std::string& a_filename, const param_node& a_options )
86  {
87  std::string t_encoding = a_options.get_value( "encoding", "" );
88  if( t_encoding.empty() )
89  {
90  path t_path = expand_path( a_filename );
91  t_encoding = t_path.extension().string().substr( 1 ); // remove the '.' at the beginning
92  }
93 
95  if( t_codec == nullptr )
96  {
97  LERROR( slog, "Unable to find output codec for encoding <" << t_encoding << ">");
98  return false;
99  }
100 
101  return t_codec->write_file( a_param, a_filename, a_options );
102  }
103 
104  bool param_translator::write_string( const param& a_param, std::string& a_string, const param_node& a_options )
105  {
106  std::string t_encoding = a_options.get_value( "encoding", "" );
107  if( t_encoding.empty() )
108  {
109  LERROR( slog, "Encoding-type option must be provided");
110  return false;
111  }
112 
114  if( t_codec == nullptr )
115  {
116  LERROR( slog, "Unable to find output codec for encoding <" << t_encoding << ">");
117  return false;
118  }
119 
120  return t_codec->write_string( a_param, a_string, a_options );
121  }
122 
123 
124 } /* namespace scarab */
virtual param_ptr_t read_string(const std::string &a_string, const param_node &a_options=param_node())=0
fs::path path
Definition: path.hh:25
param_ptr_t read_string(const std::string &a_string, const param_node &a_options=param_node())
Definition: param_codec.cc:66
virtual bool write_string(const param &a_param, std::string &a_string, const param_node &a_options=param_node())=0
path expand_path(const string &a_path)
Definition: path.cc:19
XBaseType * create(const XIndexType &a_index, XArgs ... args)
static indexed_factory< XIndexType, XBaseType, XArgs... > * get_instance()
Definition: singleton.hh:80
#define LERROR(...)
Definition: logger.hh:394
virtual param_ptr_t read_file(const std::string &a_filename, const param_node &a_options=param_node())=0
std::string get_value(const std::string &a_name, const std::string &a_default) const
Definition: param_node.hh:208
Contains the logger class and macros, based on Kasper&#39;s KLogger class.
bool write_file(const param &a_param, const std::string &a_filename, const param_node &a_options=param_node())
Definition: param_codec.cc:85
virtual bool write_file(const param &a_param, const std::string &a_filename, const param_node &a_options=param_node())=0
param_ptr_t read_file(const std::string &a_filename, const param_node &a_options=param_node())
Definition: param_codec.cc:47
LOGGER(slog, "param_codec")
std::unique_ptr< param > param_ptr_t
Definition: param_base.hh:23
bool write_string(const param &a_param, std::string &a_string, const param_node &a_options=param_node())
Definition: param_codec.cc:104