4 #ifndef tools_xml_loader
5 #define tools_xml_loader
14 #ifndef TOOLS_USE_OUREX_EXPAT
17 #include <ourex_expat.h>
24 inline void* xml_malloc(
size_t a_size){
25 tools::mem::increment(tools::s_malloc().c_str());
26 return ::malloc(a_size);
28 inline void* xml_realloc(
void* a_ptr,
size_t a_size){
29 if(a_ptr==NULL) tools::mem::increment(tools::s_malloc().c_str());
32 inline void xml_free(
void* a_ptr){
33 if(a_ptr!=NULL) tools::mem::decrement(tools::s_malloc().c_str());
48 std::ostream& a_out,
bool a_verbose =
false)
62 tools::mem::increment(s_class().c_str());
70 tools::mem::decrement(s_class().c_str());
89 tools::mem::increment(s_class().c_str());
93 if(&a_from==
this)
return *
this;
128 bool load_file(
const std::string& a_file,
bool a_compressed) {
133 this,a_compressed)) {
171 bool is_tag(
const std::string& a_string)
const {
172 size_t number =
m_tags.size();
173 for(
size_t index=0;index<number;index++) {
174 if(a_string==
m_tags[index])
return true;
188 XML_StartElementHandler a_start,XML_EndElementHandler a_end,
191 if(!aSize)
return true;
196 XML_Memory_Handling_Suite mem;
197 mem.malloc_fcn = xml_malloc;
198 mem.realloc_fcn = xml_realloc;
199 mem.free_fcn = xml_free;
200 XML_Parser _parser = XML_ParserCreate_MM(NULL,&mem,NULL);
202 XML_Parser _parser = XML_ParserCreate(NULL);
205 XML_SetUserData(_parser,a_tag);
206 XML_SetElementHandler(_parser,a_start,a_end);
209 char* buf = (
char*)aBuffer;
213 size_t len = tools::mn<size_t>(l,BUFSIZ);
214 done = len < BUFSIZ ? 1 : 0;
215 if(XML_Parse(_parser, buf, (
int)len, done)==XML_STATUS_ERROR) {
216 m_out <<
"parse_buffer :"
217 <<
" " << XML_ErrorString(XML_GetErrorCode(_parser))
218 <<
" at line " << (int)XML_GetCurrentLineNumber(_parser)
219 <<
" at byte index " << (int)XML_GetCurrentByteIndex(_parser)
221 {XML_Index pos = XML_GetCurrentByteIndex(_parser);
222 XML_Index pmn = tools::mx<XML_Index>(pos-10,0);
223 XML_Index pmx = tools::mn<XML_Index>(pos+10,XML_Index(aSize)-1);
225 {
for(XML_Index p=pmn;p<=pmx;p++) {c[0] = *(aBuffer+p);
m_out << c;}
227 {
for(XML_Index p=pmn;p<pos;p++)
m_out <<
" ";
228 m_out <<
"^" << std::endl;}}
229 XML_ParserFree(_parser);
233 XML_ParserFree(_parser);
239 XML_ParserFree(_parser);
244 XML_StartElementHandler a_start,XML_EndElementHandler a_end,
245 void* a_tag,
bool a_compressed){
247 m_out <<
"parse_file :"
248 <<
" parse file " <<
tools::sout(a_file) <<
"..." << std::endl;
252 bool use_zlib =
false;
255 m_out <<
"parse_file :"
256 <<
" uncompress requested for file "
265 m_out <<
"parse_file :"
266 <<
" tools::file::is_gzip() failed for " << a_file <<
"."
270 if(compressed) use_zlib =
true;
274 bool delete_freader =
false;
277 m_out <<
"parse_file :"
278 <<
" no compressed reader given."
285 delete_freader =
true;
287 if(!freader->
open(a_file)) {
288 m_out <<
"parse_file :"
289 <<
" can't open file " << a_file << std::endl;
290 if(delete_freader)
delete freader;
298 XML_Memory_Handling_Suite mem;
299 mem.malloc_fcn = xml_malloc;
300 mem.realloc_fcn = xml_realloc;
301 mem.free_fcn = xml_free;
302 XML_Parser _parser = XML_ParserCreate_MM(NULL,&mem,NULL);
304 XML_Parser _parser = XML_ParserCreate(NULL);
307 XML_SetUserData(_parser,a_tag);
308 XML_SetElementHandler(_parser,a_start,a_end);
319 if(!freader->
read(buf,
sizeof(buf),len)) {
320 XML_ParserFree(_parser);
322 if(delete_freader)
delete freader;
325 done = len <
sizeof(buf) ? 1 : 0;
326 if(XML_Parse(_parser, buf, (
int)len, done)==XML_STATUS_ERROR) {
327 m_out <<
"parse_file :"
329 <<
" " << XML_ErrorString(XML_GetErrorCode(_parser))
330 <<
" at line " << (int)XML_GetCurrentLineNumber(_parser)
332 XML_ParserFree(_parser);
334 if(delete_freader)
delete freader;
338 XML_ParserFree(_parser);
340 if(delete_freader)
delete freader;
344 XML_ParserFree(_parser);
347 m_out <<
"parse_file :"
348 <<
" parse file " <<
tools::sout(a_file) <<
" done." << std::endl;
350 if(delete_freader)
delete freader;
360 char* p = (
char*)a_string;
361 for (
int i = 0; i < aLength; i++, p++) {
373 static void start_element(
void* aUserData,
const XML_Char* a_name,
const XML_Char** a_atbs){
380 std::string name = a_name;
387 This->
m_out <<
"start_element :"
388 <<
" no tag with a depth of " << This->
m_depth
396 This->
m_out <<
"start_element :"
398 <<
" tag with a delta depth of " << delta
405 std::vector<tools::xml::tree::atb> atbs;
406 {
const XML_Char** a_atts = a_atbs;
407 while((*a_atts)&&(*(a_atts+1))) {
415 This->
m_out <<
"start_element :"
416 <<
" can't create a tree for tag " <<
tools::sout(name)
446 This->
m_out <<
"start_element :"
448 <<
" non-tag without some parent tag."
458 This->
m_out <<
"start_element :"
460 <<
" grand child of a tag."
465 }
else if(delta==1) {
468 {
const XML_Char** a_atts = a_atbs;
469 while((*a_atts)&&(*(a_atts+1))) {
476 This->
m_out <<
"start_element :"
478 <<
" non-tag with a delta depth of " << delta
538 if(!cont) This->
m_abort =
true;
540 }
else if(delta==1) {
548 This->
m_out <<
"end_element :"
549 <<
" problem for element " <<
tools::sout(std::string(a_name))
550 <<
" : delta depth of " << delta
573 std::vector< std::pair<std::string,std::string> >
m_atbs;