Scarab  v3.9.3
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 
75  return read_string( a_string, t_encoding, a_options );
76  }
77 
78  param_ptr_t param_translator::read_string( const std::string& a_string, const std::string& a_encoding, const param_node& a_options )
79  {
81  if( t_codec == nullptr )
82  {
83  LERROR( slog, "Unable to find input codec for encoding <" << a_encoding << ">");
84  return nullptr;
85  }
86 
87  return t_codec->read_string( a_string, a_options );
88  }
89 
90  bool param_translator::write_file( const param& a_param, const std::string& a_filename, const param_node& a_options )
91  {
92  std::string t_encoding = a_options.get_value( "encoding", "" );
93  if( t_encoding.empty() )
94  {
95  path t_path = expand_path( a_filename );
96  t_encoding = t_path.extension().string().substr( 1 ); // remove the '.' at the beginning
97  }
98 
100  if( t_codec == nullptr )
101  {
102  LERROR( slog, "Unable to find output codec for encoding <" << t_encoding << ">");
103  return false;
104  }
105 
106  return t_codec->write_file( a_param, a_filename, a_options );
107  }
108 
109  bool param_translator::write_string( const param& a_param, std::string& a_string, const param_node& a_options )
110  {
111  std::string t_encoding = a_options.get_value( "encoding", "" );
112  if( t_encoding.empty() )
113  {
114  LERROR( slog, "Encoding-type option must be provided");
115  return false;
116  }
117 
118  return write_string( a_param, a_string, t_encoding, a_options );
119  }
120 
121  bool param_translator::write_string( const param& a_param, std::string& a_string, const std::string& a_encoding, const param_node& a_options )
122  {
124  if( t_codec == nullptr )
125  {
126  LERROR( slog, "Unable to find output codec for encoding <" << a_encoding << ">");
127  return false;
128  }
129 
130  return t_codec->write_string( a_param, a_string, a_options );
131  }
132 
133 } /* 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:26
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:20
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:90
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:109