Scarab  v1.6.1
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 "param.hh"
15 #include "path.hh"
16 
17 LOGGER( slog, "param_codec" );
18 
19 namespace scarab
20 {
21 
23  {
24  }
25 
27  {
28  }
29 
30 
32  {
33  }
34 
36  {
37  }
38 
39 
41  {
42  }
43 
45  {
46  }
47 
48  param* param_translator::read_file( const std::string& a_filename, const param_node* a_options )
49  {
50  std::string t_encoding;
51  if( a_options != nullptr && a_options->has( "encoding" ) )
52  {
53  t_encoding = a_options->get_value( "encoding" );
54  }
55  else
56  {
57  path t_path = expand_path( a_filename );
58  t_encoding = t_path.extension().native().substr( 1 ); // remove the '.' at the beginning
59  }
60 
62  if( t_codec == nullptr )
63  {
64  LERROR( slog, "Unable to find input codec for encoding <" << t_encoding << ">");
65  return nullptr;
66  }
67 
68  return t_codec->read_file( a_filename, a_options );
69  }
70 
71  param* param_translator::read_string( const std::string& a_string, const param_node* a_options )
72  {
73  std::string t_encoding;
74  if( a_options != nullptr && a_options->has( "encoding" ) )
75  {
76  t_encoding = a_options->get_value( "encoding" );
77  }
78  else
79  {
80  LERROR( slog, "Encoding-type option must be provided");
81  return nullptr;
82  }
83 
85  if( t_codec == nullptr )
86  {
87  LERROR( slog, "Unable to find input codec for encoding <" << t_encoding << ">");
88  return nullptr;
89  }
90 
91  return t_codec->read_string( a_string, a_options );
92  }
93 
94  bool param_translator::write_file( const param& a_param, const std::string& a_filename, const param_node* a_options )
95  {
96  std::string t_encoding;
97  if( a_options != nullptr && a_options->has( "encoding" ) )
98  {
99  t_encoding = a_options->get_value( "encoding" );
100  }
101  else
102  {
103  path t_path = expand_path( a_filename );
104  t_encoding = t_path.extension().native().substr( 1 ); // remove the '.' at the beginning
105  }
106 
108  if( t_codec == nullptr )
109  {
110  LERROR( slog, "Unable to find output codec for encoding <" << t_encoding << ">");
111  return false;
112  }
113 
114  return t_codec->write_file( a_param, a_filename, a_options );
115  }
116 
117  bool param_translator::write_string( const param& a_param, std::string& a_string, const param_node* a_options )
118  {
119  std::string t_encoding;
120  if( a_options != nullptr && a_options->has( "encoding" ) )
121  {
122  t_encoding = a_options->get_value( "encoding" );
123  }
124  else
125  {
126  LERROR( slog, "Encoding-type option must be provided");
127  return false;
128  }
129 
131  if( t_codec == nullptr )
132  {
133  LERROR( slog, "Unable to find output codec for encoding <" << t_encoding << ">");
134  return false;
135  }
136 
137  return t_codec->write_string( a_param, a_string, a_options );
138  }
139 
140 
141 } /* namespace scarab */
virtual param * read_file(const std::string &a_filename, const param_node *a_options=nullptr)=0
fs::path path
Definition: path.hh:25
path expand_path(const string &a_path)
Definition: path.cc:19
static factory< XBaseType, XArgs... > * get_instance()
#define LERROR(...)
Definition: logger.hh:365
LOGGER(slog,"param_codec")
bool write_file(const param &a_param, const std::string &a_filename, const param_node *a_options=nullptr)
Definition: param_codec.cc:94
Contains the logger class and macros, based on Kasper&#39;s KLogger class.
virtual param * read_string(const std::string &a_string, const param_node *a_options=nullptr)=0
std::string get_value(const std::string &a_name) const
Definition: param.hh:1006
bool has(const std::string &a_name) const
Definition: param.hh:996
param * read_string(const std::string &a_string, const param_node *a_options=nullptr)
Definition: param_codec.cc:71
virtual bool write_string(const param &a_param, std::string &a_string, const param_node *a_options=nullptr)=0
virtual bool write_file(const param &a_param, const std::string &a_filename, const param_node *a_options=nullptr)=0
XBaseType * create(const std::string &a_class_name, XArgs...args)
Definition: factory.hh:178
bool write_string(const param &a_param, std::string &a_string, const param_node *a_options=nullptr)
Definition: param_codec.cc:117
param * read_file(const std::string &a_filename, const param_node *a_options=nullptr)
Definition: param_codec.cc:48