| 1 |
/* Copyright (c) 2021, 2026, Oracle and/or its affiliates. |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License, version 2.0, |
| 5 |
as published by the Free Software Foundation. |
| 6 |
|
| 7 |
This program is designed to work with certain software (including |
| 8 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 9 |
as designated in a particular file or component or in included license |
| 10 |
documentation. The authors of MySQL hereby grant you an additional |
| 11 |
permission to link the program and your derivative works with the |
| 12 |
separately licensed software that they have either included with |
| 13 |
the program or referenced in the documentation. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License, version 2.0, for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 |
|
| 24 |
#ifndef CONFIG_READER_INCLUDED |
| 25 |
#define CONFIG_READER_INCLUDED |
| 26 |
|
| 27 |
#include <string> |
| 28 |
|
| 29 |
#include "my_rapidjson_size_t.h" |
| 30 |
|
| 31 |
#include <rapidjson/document.h> |
| 32 |
|
| 33 |
namespace keyring_common { |
| 34 |
namespace config { |
| 35 |
|
| 36 |
class Config_reader { |
| 37 |
public: |
| 38 |
/** |
| 39 |
Constructor |
| 40 |
|
| 41 |
Reads JSON from config file and stores it in memory. |
| 42 |
|
| 43 |
@param [in] config_file_path Full path to configuration file |
| 44 |
*/ |
| 45 |
explicit Config_reader(const std::string config_file_path); |
| 46 |
|
| 47 |
/** |
| 48 |
Get an element value from JSON document. |
| 49 |
Assumption: Type is compatible with Get() function and |
| 50 |
type of element is matching with template argument. |
| 51 |
|
| 52 |
@param [in] element_name Name of the element being searched |
| 53 |
@param [out] element_value Value of the element |
| 54 |
|
| 55 |
@returns status of search operation |
| 56 |
@retval false Element found. Refer to element_value |
| 57 |
@retval true Element missing. |
| 58 |
*/ |
| 59 |
template <typename T> |
| 60 |
bool get_element(const std::string element_name, T &element_value) { |
| 61 |
if (!valid_ || !data_.HasMember(element_name)) return true; |
| 62 |
element_value = data_[element_name].Get<T>(); |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
private: |
| 67 |
/** Configuration file path */ |
| 68 |
std::string config_file_path_; |
| 69 |
/** Configuration data in JSON */ |
| 70 |
rapidjson::Document data_; |
| 71 |
/** Validity of configuration data */ |
| 72 |
bool valid_; |
| 73 |
}; |
| 74 |
|
| 75 |
} // namespace config |
| 76 |
} // namespace keyring_common |
| 77 |
|
| 78 |
#endif // !CONFIG_READER_INCLUDED |