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() );
108 if( a_value.IsNull() )
110 return std::unique_ptr< param >(
new param() );
112 if( a_value.IsObject() )
114 std::unique_ptr< param_node > t_obj_as_param(
new param_node() );
115 for( rapidjson::Value::ConstMemberIterator jsonIt = a_value.MemberBegin();
116 jsonIt != a_value.MemberEnd();
121 return t_obj_as_param;
123 if( a_value.IsArray() )
125 std::unique_ptr< param_array > t_array_as_param(
new param_array() );
126 for( rapidjson::Value::ConstValueIterator jsonIt = a_value.Begin();
127 jsonIt != a_value.End();
132 return t_array_as_param;
134 if( a_value.IsString() )
137 return std::unique_ptr< param_value >(
new param_value( a_value.GetString() ) );
139 if( a_value.IsBool() )
142 return std::unique_ptr< param_value >(
new param_value( a_value.GetBool() ) );
144 if( a_value.IsInt() )
147 return std::unique_ptr< param_value >(
new param_value( a_value.GetInt() ) ) ;
149 if( a_value.IsUint() )
152 return std::unique_ptr< param_value >(
new param_value( a_value.GetUint() ) );
154 if( a_value.IsInt64() )
157 return std::unique_ptr< param_value >(
new param_value( a_value.GetInt64() ) );
159 if( a_value.IsUint64() )
162 return std::unique_ptr< param_value >(
new param_value( a_value.GetUint64() ) );
164 if( a_value.IsDouble() )
167 return std::unique_ptr< param_value >(
new param_value( a_value.GetDouble() ) );
169 LWARN( dlog,
"(config_reader_json) unknown type; returning null value" );
170 return std::unique_ptr< param >(
new param() );
184 if( a_filename.empty() )
186 LERROR( dlog,
"Filename cannot be an empty string" );
190 FILE* file = fopen( a_filename.c_str(),
"w" );
193 LERROR( dlog,
"Unable to open file: " << a_filename );
197 char t_buffer[ RAPIDJSON_FILE_BUFFER_SIZE ];
198 rapidjson::FileWriteStream t_filestream( file, t_buffer,
sizeof(t_buffer) );
201 if( a_options.
has(
"style" ) )
203 if( a_options[
"style"]().is_uint() )
209 string t_style_string( a_options.
get_value(
"style",
"compact" ) );
210 if( t_style_string ==
string(
"pretty" ) ) t_style = k_pretty;
214 bool t_result =
false;
215 if( t_style == k_compact )
228 LERROR( dlog,
"Error while writing file" );
237 rapidjson::StringBuffer t_str_buff;
240 if( a_options.
has(
"style" ) )
242 if( a_options[
"style"]().is_uint() )
248 string t_style_string( a_options.
get_value(
"style",
"compact" ) );
249 if( t_style_string ==
string(
"pretty" ) ) t_style = k_pretty;
253 bool t_result =
false;
254 if( t_style == k_compact )
267 LERROR( dlog,
"Error while writing string" );
271 a_string.assign( t_str_buff.GetString() );
bool write_param(const param &a_to_write, XWriter *a_writer)
virtual ~param_output_json()
LOGGER(mtlog, "authentication")
rapidjson::PrettyWriter< rapidjson::StringBuffer > rj_pretty_string_writer
std::string get_value(const std::string &a_name, const std::string &a_default) const
Contains the logger class and macros, based on Kasper's KLogger class.
rapidjson::PrettyWriter< rapidjson::FileWriteStream > rj_pretty_file_writer
virtual bool write_string(const param &a_to_write, std::string &a_string, const param_node &a_options=param_node())
REGISTER_PARAM_INPUT_CODEC(param_input_json, "json")
std::unique_ptr< param > param_ptr_t
REGISTER_PARAM_OUTPUT_CODEC(param_output_json, "json")
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())
bool has(const std::string &a_name) const
rapidjson::Writer< rapidjson::StringBuffer > rj_string_writer