irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

irods_configuration_parser.cpp
Go to the documentation of this file.
2 #include "irods_log.hpp"
3 #include "irods_exception.hpp"
4 
5 #include <fstream>
6 #include <algorithm>
7 
8 #include <boost/algorithm/string.hpp>
9 #include <boost/filesystem.hpp>
10 #include <boost/any.hpp>
11 
12 namespace fs = boost::filesystem;
14 
15 namespace irods {
16 
18 
19  } // ctor
20 
22 
23  } // dtor
24 
26  const configuration_parser& _rhs ) {
27 
28  irods::error ret = copy_and_swap( _rhs.root_ );
29  if ( !ret.ok() ) {
30  irods::log( PASS( ret ) );
31  }
32 
33  } // cctor
34 
36  const std::string& _file ) {
37 
38  load( _file );
39 
40  } // ctor
41 
43  const configuration_parser& _rhs ) {
44 
45  copy_and_swap( _rhs.root_ );
46 
47  return *this;
48 
49  } // operator=
50 
52 
53  root_.clear();
54 
55  } // clear
56 
58  const std::unordered_map<std::string, boost::any>& _rhs ) {
59  // more could possibly be done here
60  root_.clear();
61  root_ = _rhs;
62 
63  return SUCCESS();
64 
65  } // copy_and_swap
66 
67  bool configuration_parser::has_entry( const std::string& _key ) {
68  return root_.count( _key ) != 0;
69 
70  } // has_entry
71 
72  error configuration_parser::load( const std::string& _file ) {
73  if ( _file.empty() ) {
74  return ERROR( SYS_INVALID_INPUT_PARAM, "file is empty" );
75  }
76 
77  error ret = load_json_object( _file );
78 
79  return ret;
80 
81  } // load
82 
84  {
85  json config;
86 
87  {
88  std::ifstream in{_file};
89 
90  if (!in) {
91  std::string msg{"failed to open file ["};
92  msg += _file;
93  msg += ']';
94  return ERROR( -1, msg );
95  }
96 
97  try {
98  in >> config;
99  }
100  catch (const json::parse_error& e) {
101  return ERROR(-1, boost::format("failed to parse json [file=%s, error=%s].") % _file % e.what());
102  }
103  }
104 
105  irods::error ret = SUCCESS();
106  try {
107  if ( root_.empty() ) {
108  root_ = boost::any_cast<std::unordered_map<std::string, boost::any>>(convert_json(config));
109  } else {
110  const auto json_object_any = convert_json(config);
111  const auto& json_object = boost::any_cast<const std::unordered_map<std::string, boost::any>&>(json_object_any);
112  for ( auto it = json_object.cbegin(); it != json_object.cend(); ++it ) {
113  root_[it->first] = it->second;
114  }
115  }
116  } catch ( const irods::exception& e ) {
117  ret = irods::error(e);
118  } catch ( const boost::bad_any_cast& ) {
119  ret = ERROR(INVALID_ANY_CAST, "configuration file did not contain a json object.");
120  }
121 
122  return ret;
123 
124  } // load_json_object
125 
126  boost::any configuration_parser::convert_json(const json& _json)
127  {
128  switch (_json.type()) {
129  case json::value_t::string:
130  return _json.get<std::string>();
131 
132  case json::value_t::number_float:
133  return _json.get<double>();
134 
135  case json::value_t::number_integer:
136  case json::value_t::number_unsigned:
137  return _json.get<int>();
138 
139  case json::value_t::boolean:
140  return _json.get<bool>();
141 
142  case json::value_t::array: {
143  std::vector<boost::any> array;
144 
145  for (auto&& jj : _json) {
146  array.push_back(convert_json(jj));
147  }
148 
149  return array;
150  }
151 
152  case json::value_t::object: {
153  std::unordered_map<std::string, boost::any> object;
154 
155  //for (auto&& [k, v] : j.items()) { // Supported in later versions :(
156  for (auto it = std::begin(_json); it != std::end(_json); ++it) {
157  object.insert({it.key(), convert_json(it.value())});
158  }
159 
160  return object;
161  }
162 
163  case json::value_t::null:
164  return std::string{"NULL"};
165 
166  default:
167  const auto type = static_cast<std::uint8_t>(_json.type());
168  THROW(-1, (boost::format("unhandled type in json_typeof: %d") % type));
169  }
170  } // parse_json_object
171 
172  std::string to_env(
173  const std::string& _v ) {
174  return boost::to_upper_copy <
175  std::string > ( _v );
176  }
177 
178  void configuration_parser::remove(const std::string& _key) {
179  if ( !root_.erase(_key) ) {
180  THROW ( KEY_NOT_FOUND, (boost::format("key \"%s\" not found in map.") % _key).str() );
181  }
182  }
183 
184 }; // namespace irods
THROW
#define THROW(_code, _msg)
Definition: irods_exception.hpp:68
PASS
#define PASS(prev_error_)
Definition: irods_error.hpp:118
irods::configuration_parser::remove
T remove(const std::string &_key)
Definition: irods_configuration_parser.hpp:119
irods::configuration_parser::clear
void clear()
Definition: irods_configuration_parser.cpp:51
irods_exception.hpp
json
nlohmann::json json
Definition: irods_configuration_parser.cpp:13
irods::to_env
std::string to_env(const std::string &)
Definition: irods_configuration_parser.cpp:172
json
nlohmann::json json
Definition: group.cpp:5
SYS_INVALID_INPUT_PARAM
@ SYS_INVALID_INPUT_PARAM
Definition: rodsErrorTable.h:195
irods::configuration_parser::has_entry
bool has_entry(const std::string &)
Definition: irods_configuration_parser.cpp:67
irods::experimental::filesystem::client::end
auto end(const collection_iterator &) noexcept -> const collection_iterator
Definition: collection_iterator.hpp:88
SUCCESS
#define SUCCESS()
Definition: irods_error.hpp:121
irods::experimental::filesystem::client::begin
auto begin(collection_iterator _iter) noexcept -> collection_iterator
Definition: collection_iterator.hpp:83
irods
Definition: apiHandler.hpp:35
irods::configuration_parser::convert_json
boost::any convert_json(const nlohmann::json &)
Definition: irods_configuration_parser.cpp:126
irods::configuration_parser
Definition: irods_configuration_parser.hpp:20
irods::configuration_parser::load
error load(const std::string &)
Definition: irods_configuration_parser.cpp:72
terminate_irods_processes.e
e
Definition: terminate_irods_processes.py:19
INVALID_ANY_CAST
@ INVALID_ANY_CAST
Definition: rodsErrorTable.h:771
irods::configuration_parser::root_
std::unordered_map< std::string, boost::any > root_
Definition: irods_configuration_parser.hpp:145
KEY_NOT_FOUND
@ KEY_NOT_FOUND
Definition: rodsErrorTable.h:749
irods::log
void log(const error &)
Definition: irods_log.cpp:13
irods::configuration_parser::~configuration_parser
~configuration_parser()
Definition: irods_configuration_parser.cpp:21
ERROR
#define ERROR(code_, message_)
Definition: irods_error.hpp:117
irods::error
Definition: irods_error.hpp:23
irods::configuration_parser::copy_and_swap
error copy_and_swap(const std::unordered_map< std::string, boost::any > &)
Definition: irods_configuration_parser.cpp:57
error
int error
Definition: filesystem.cpp:101
irods::exception
Definition: irods_exception.hpp:15
irods_configuration_parser.hpp
irods::configuration_parser::operator=
configuration_parser & operator=(const configuration_parser &)
Definition: irods_configuration_parser.cpp:42
irods::configuration_parser::load_json_object
error load_json_object(const std::string &)
Definition: irods_configuration_parser.cpp:83
irods::error::ok
bool ok()
Definition: irods_error.cpp:258
type
int type
Definition: filesystem.cpp:103
irods_log.hpp
irods::configuration_parser::configuration_parser
configuration_parser()
Definition: irods_configuration_parser.cpp:17