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() );
103 std::unique_ptr< param_node > t_doc_as_param(
new param_node() );
104 for( rapidjson::Value::ConstMemberIterator jsonIt = a_doc.MemberBegin();
105 jsonIt != a_doc.MemberEnd();
110 return t_doc_as_param;
115 if( a_value.IsNull() )
117 return std::unique_ptr< param >(
new param() );
119 if( a_value.IsObject() )
121 std::unique_ptr< param_node > t_obj_as_param(
new param_node() );
122 for( rapidjson::Value::ConstMemberIterator jsonIt = a_value.MemberBegin();
123 jsonIt != a_value.MemberEnd();
128 return t_obj_as_param;
130 if( a_value.IsArray() )
132 std::unique_ptr< param_array > t_array_as_param(
new param_array() );
133 for( rapidjson::Value::ConstValueIterator jsonIt = a_value.Begin();
134 jsonIt != a_value.End();
139 return t_array_as_param;
141 if( a_value.IsString() )
144 return std::unique_ptr< param_value >(
new param_value( a_value.GetString() ) );
146 if( a_value.IsBool() )
149 return std::unique_ptr< param_value >(
new param_value( a_value.GetBool() ) );
151 if( a_value.IsInt() )
154 return std::unique_ptr< param_value >(
new param_value( a_value.GetInt() ) ) ;
156 if( a_value.IsUint() )
159 return std::unique_ptr< param_value >(
new param_value( a_value.GetUint() ) );
161 if( a_value.IsInt64() )
164 return std::unique_ptr< param_value >(
new param_value( a_value.GetInt64() ) );
166 if( a_value.IsUint64() )
169 return std::unique_ptr< param_value >(
new param_value( a_value.GetUint64() ) );
171 if( a_value.IsDouble() )
174 return std::unique_ptr< param_value >(
new param_value( a_value.GetDouble() ) );
176 LWARN( dlog,
"(config_reader_json) unknown type; returning null value" );
177 return std::unique_ptr< param >(
new param() );
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.
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.
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() );
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
std::string get_value(const std::string &a_name) const
virtual bool write_string(const param &a_to_write, std::string &a_string, const param_node &a_options=param_node())
bool has(const std::string &a_name) const
std::unique_ptr< param > param_ptr_t
rapidjson::Writer< rapidjson::FileWriteStream > rj_file_writer
virtual bool write_file(const param &a_to_write, const std::string &a_filename, const param_node &a_options=param_node())
rapidjson::Writer< rapidjson::StringBuffer > rj_string_writer
LOGGER(mtlog,"authentication")