8 #define SCARAB_API_EXPORTS 12 using std::stringstream;
18 #include "rapidjson/document.h" 19 #include "rapidjson/filereadstream.h" 20 #include "rapidjson/filewritestream.h" 21 #include "rapidjson/stringbuffer.h" 40 FILE* t_config_file = fopen( a_filename.c_str(),
"r" );
41 if( t_config_file == NULL )
43 LERROR( dlog,
"file <" << a_filename <<
"> did not open" );
47 char t_buffer[ RAPIDJSON_FILE_BUFFER_SIZE ];
48 rapidjson::FileReadStream t_file_stream( t_config_file, t_buffer,
sizeof(t_buffer) );
50 rapidjson::Document t_config_doc;
51 if( t_config_doc.ParseStream<0>( t_file_stream ).HasParseError() )
53 unsigned errorPos = t_config_doc.GetErrorOffset();
54 rewind( t_config_file );
55 unsigned iChar, newlineCount = 1, lastNewlinePos = 0;
57 for( iChar = 0; iChar != errorPos; ++iChar )
59 thisChar = fgetc( t_config_file );
64 if( thisChar ==
'\n' || thisChar ==
'\r' )
67 lastNewlinePos = iChar + 1;
70 if( iChar == errorPos )
72 LERROR( dlog,
"error parsing config file :\n" <<
73 '\t' << t_config_doc.GetParseError() <<
'\n' <<
74 "\tThe error was reported at line " << newlineCount <<
", character " << errorPos - lastNewlinePos );
78 LERROR( dlog,
"error parsing config file :\n" <<
79 '\t' << t_config_doc.GetParseError() <<
80 "\tend of file reached before error location was found" );
82 fclose( t_config_file );
85 fclose( t_config_file );
92 rapidjson::Document t_config_doc;
93 if( t_config_doc.Parse<0>( a_json_string.c_str() ).HasParseError() )
95 LERROR( dlog,
"error parsing string:\n" << t_config_doc.GetParseError() );
104 for( rapidjson::Value::ConstMemberIterator jsonIt = a_doc.MemberBegin();
105 jsonIt != a_doc.MemberEnd();
115 if( a_value.IsNull() )
119 if( a_value.IsObject() )
122 for( rapidjson::Value::ConstMemberIterator jsonIt = a_value.MemberBegin();
123 jsonIt != a_value.MemberEnd();
128 return t_config_object;
130 if( a_value.IsArray() )
133 for( rapidjson::Value::ConstValueIterator jsonIt = a_value.Begin();
134 jsonIt != a_value.End();
139 return t_config_array;
141 if( a_value.IsString() )
146 if( a_value.IsBool() )
151 if( a_value.IsInt() )
156 if( a_value.IsUint() )
161 if( a_value.IsInt64() )
166 if( a_value.IsUint64() )
171 if( a_value.IsDouble() )
176 LWARN( dlog,
"(config_reader_json) unknown type; returning null value" );
191 if( a_filename.empty() )
193 LERROR( dlog,
"Filename cannot be an empty string" );
197 FILE* file = fopen( a_filename.c_str(),
"w" );
200 LERROR( dlog,
"Unable to open file: " << a_filename );
204 char t_buffer[ RAPIDJSON_FILE_BUFFER_SIZE ];
205 rapidjson::FileWriteStream t_filestream( file, t_buffer,
sizeof(t_buffer) );
208 if( a_options !=
nullptr && a_options->
has(
"style" ) )
216 string t_style_string( a_options->
get_value(
"style",
"compact" ) );
217 if( t_style_string ==
string(
"pretty" ) ) t_style = k_pretty;
221 bool t_result =
false;
222 if( t_style == k_compact )
235 LERROR( dlog,
"Error while writing file" );
244 rapidjson::StringBuffer t_str_buff;
247 if( a_options !=
nullptr && a_options->
has(
"style" ) )
255 string t_style_string( a_options->
get_value(
"style",
"compact" ) );
256 if( t_style_string ==
string(
"pretty" ) ) t_style = k_pretty;
260 bool t_result =
false;
261 if( t_style == k_compact )
274 LERROR( dlog,
"Error while writing string" );
278 a_string.assign( t_str_buff.GetString() );
void replace(const std::string &a_name, const param &a_value)
creates a copy of a_value
REGISTER_PARAM_INPUT_CODEC(param_input_json,"json")
REGISTER_PARAM_OUTPUT_CODEC(param_output_json,"json")
bool write_param(const param &a_to_write, XWriter *a_writer)
const param_value & value_at(const std::string &a_name) const
virtual ~param_output_json()
rapidjson::PrettyWriter< rapidjson::StringBuffer > rj_pretty_string_writer
Contains the logger class and macros, based on Kasper's KLogger class.
rapidjson::PrettyWriter< rapidjson::FileWriteStream > rj_pretty_file_writer
virtual bool write_file(const param &a_to_write, const std::string &a_filename, const param_node *a_options=nullptr)
std::string get_value(const std::string &a_name) const
bool has(const std::string &a_name) const
virtual bool write_string(const param &a_to_write, std::string &a_string, const param_node *a_options=nullptr)
rapidjson::Writer< rapidjson::FileWriteStream > rj_file_writer
void push_back(const param &a_value)
rapidjson::Writer< rapidjson::StringBuffer > rj_string_writer
LOGGER(mtlog,"authentication")