f4cec1e401347b961bdc92e7c784ae6e1888e751
6 #define MAX_PATH _MAX_PATH
13 # include <sys/stat.h>
14 # define MAX_PATH PATH_MAX
19 #include "exception.h"
26 #define getcwd _getcwd
29 static const char* WS
= " \t\r\n";
30 static const char* WSEQ
= " =\t\r\n";
32 string working_directory
;
40 XMLInclude ( XMLElement
* e_
, const Path
& path_
)
46 class XMLIncludes
: public vector
<XMLInclude
*>
51 for ( size_t i
= 0; i
< this->size(); i
++ )
57 InitWorkingDirectory()
59 // store the current directory for path calculations
60 working_directory
.resize ( _MAX_PATH
);
61 working_directory
[0] = 0;
62 getcwd ( &working_directory
[0], working_directory
.size() );
63 working_directory
.resize ( strlen ( working_directory
.c_str() ) );
74 return _filelengthi64 ( _fileno(f
) );
76 struct stat64 file_stat
;
77 if ( fstat64(fileno(f
), &file_stat
) != 0 )
79 return file_stat
.st_size
;
85 if ( !working_directory
.size() )
86 InitWorkingDirectory();
87 string
s ( working_directory
);
88 const char* p
= strtok ( &s
[0], "/\\" );
93 p
= strtok ( NULL
, "/\\" );
97 Path::Path ( const Path
& cwd
, const string
& file
)
99 string
s ( cwd
.Fixup ( file
, false ) );
100 const char* p
= strtok ( &s
[0], "/\\" );
104 path
.push_back ( p
);
105 p
= strtok ( NULL
, "/\\" );
110 Path::Fixup ( const string
& file
, bool include_filename
) const
112 if ( strchr ( "/\\", file
[0] )
114 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
121 vector
<string
> pathtmp ( path
);
123 const char* prev
= strtok ( &tmp
[0], "/\\" );
124 const char* p
= strtok ( NULL
, "/\\" );
127 if ( !strcmp ( prev
, "." ) )
129 else if ( !strcmp ( prev
, ".." ) )
131 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
133 if ( pathtmp
.size() > 1 )
135 if ( pathtmp
.size() )
137 pathtmp
.resize ( pathtmp
.size() - 1 );
140 pathtmp
.push_back ( prev
);
142 p
= strtok ( NULL
, "/\\" );
144 if ( include_filename
)
145 pathtmp
.push_back ( prev
);
147 // reuse tmp variable to return recombined path
149 for ( size_t i
= 0; i
< pathtmp
.size(); i
++ )
151 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
163 Path::RelativeFromWorkingDirectory ( const string
& path
)
165 vector
<string
> vwork
, vpath
, vout
;
166 Path::Split ( vwork
, working_directory
, true );
167 Path::Split ( vpath
, path
, true );
169 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
170 // not possible to do relative across different drive letters
171 if ( vwork
[0] != vpath
[0] )
175 while ( i
< vwork
.size() && i
< vpath
.size() && vwork
[i
] == vpath
[i
] )
177 if ( i
< vwork
.size() )
179 // path goes above our working directory, we will need some ..'s
180 for ( size_t j
= 0; j
< i
; j
++ )
181 vout
.push_back ( ".." );
183 while ( i
< vpath
.size() )
184 vout
.push_back ( vpath
[i
++] );
186 // now merge vout into a string again
188 for ( i
= 0; i
< vout
.size(); i
++ )
190 out
+= "/" + vout
[i
];
196 Path::Split ( vector
<string
>& out
,
201 const char* prev
= strtok ( &s
[0], "/\\" );
202 const char* p
= strtok ( NULL
, "/\\" );
206 out
.push_back ( prev
);
208 p
= strtok ( NULL
, "/\\" );
211 out
.push_back ( prev
);
226 XMLFile::open(const string
& filename_
)
229 FILE* f
= fopen ( filename_
.c_str(), "rb" );
232 unsigned long len
= (unsigned long)filelen(f
);
234 fread ( &_buf
[0], 1, len
, f
);
238 _filename
= filename_
;
243 // next_token() moves the pointer to next token, which may be
244 // an xml element or a text element, basically it's a glorified
245 // skipspace, normally the user of this class won't need to call
248 XMLFile::next_token()
250 _p
+= strspn ( _p
, WS
);
254 XMLFile::next_is_text()
260 XMLFile::more_tokens()
265 // get_token() is used to return a token, and move the pointer
268 XMLFile::get_token(string
& token
)
271 if ( !strncmp ( _p
, "<!--", 4 ) )
273 tokend
= strstr ( _p
, "-->" );
279 else if ( !strncmp ( _p
, "<?", 2 ) )
281 tokend
= strstr ( _p
, "?>" );
287 else if ( *_p
== '<' )
289 tokend
= strchr ( _p
, '>' );
297 tokend
= strchr ( _p
, '<' );
300 while ( tokend
> _p
&& isspace(tokend
[-1]) )
305 token
= string ( _p
, tokend
-_p
);
312 XMLFile::Location() const
315 const char* p
= strchr ( _buf
.c_str(), '\n' );
316 while ( p
&& p
< _p
)
319 p
= strchr ( p
+1, '\n' );
321 return ssprintf ( "%s(%i)",_filename
.c_str(), line
);
324 XMLAttribute::XMLAttribute()
328 XMLAttribute::XMLAttribute(const string
& name_
,
329 const string
& value_
)
330 : name(name_
), value(value_
)
334 XMLAttribute::XMLAttribute ( const XMLAttribute
& src
)
335 : name(src
.name
), value(src
.value
)
340 XMLAttribute
& XMLAttribute::operator = ( const XMLAttribute
& src
)
347 XMLElement::XMLElement ( const string
& location_
)
348 : location(location_
),
353 XMLElement::~XMLElement()
356 for ( i
= 0; i
< attributes
.size(); i
++ )
357 delete attributes
[i
];
358 for ( i
= 0; i
< subElements
.size(); i
++ )
359 delete subElements
[i
];
363 XMLElement::AddSubElement ( XMLElement
* e
)
365 subElements
.push_back ( e
);
366 e
->parentElement
= this;
370 // This function takes a single xml tag ( i.e. beginning with '<' and
371 // ending with '>', and parses out it's tag name and constituent
373 // Return Value: returns true if you need to look for a </tag> for
374 // the one it just parsed...
376 XMLElement::Parse(const string
& token
,
379 const char* p
= token
.c_str();
380 assert ( *p
== '<' );
382 p
+= strspn ( p
, WS
);
384 // check if this is a comment
385 if ( !strncmp ( p
, "!--", 3 ) )
389 return false; // never look for end tag to a comment
392 end_tag
= ( *p
== '/' );
396 p
+= strspn ( p
, WS
);
398 const char* end
= strpbrk ( p
, WS
);
401 end
= strpbrk ( p
, "/>" );
404 name
= string ( p
, end
-p
);
406 p
+= strspn ( p
, WS
);
407 while ( *p
!= '>' && *p
!= '/' )
409 end
= strpbrk ( p
, WSEQ
);
412 end
= strpbrk ( p
, "/>" );
415 string
attribute ( p
, end
-p
), value
;
417 p
+= strspn ( p
, WS
);
421 p
+= strspn ( p
, WS
);
423 if ( strchr ( "\"'", *p
) )
426 end
= strchr ( p
, quote
);
430 end
= strpbrk ( p
, WS
);
434 end
= strchr ( p
, '>' );
436 if ( end
[-1] == '/' )
439 value
= string ( p
, end
-p
);
441 if ( quote
&& *p
== quote
)
443 p
+= strspn ( p
, WS
);
445 else if ( name
[0] != '!' )
447 throw XMLSyntaxErrorException ( location
,
448 "attributes must have values" );
450 attributes
.push_back ( new XMLAttribute ( attribute
, value
) );
452 return !( *p
== '/' ) && !end_tag
;
456 XMLElement::GetAttribute ( const string
& attribute
,
459 // this would be faster with a tree-based container, but our attribute
460 // lists are likely to stay so short as to not be an issue.
461 for ( size_t i
= 0; i
< attributes
.size(); i
++ )
463 if ( attribute
== attributes
[i
]->name
)
464 return attributes
[i
];
468 throw RequiredAttributeNotFoundException ( location
,
476 XMLElement::GetAttribute ( const string
& attribute
,
477 bool required
) const
479 // this would be faster with a tree-based container, but our attribute
480 // lists are likely to stay so short as to not be an issue.
481 for ( size_t i
= 0; i
< attributes
.size(); i
++ )
483 if ( attribute
== attributes
[i
]->name
)
484 return attributes
[i
];
488 throw RequiredAttributeNotFoundException ( location
,
496 // This function reads a "token" from the file loaded in XMLFile
497 // if it finds a tag that is non-singular, it parses sub-elements and/or
498 // inner text into the XMLElement that it is building to return.
499 // Return Value: an XMLElement allocated via the new operator that contains
500 // it's parsed data. Keep calling this function until it returns NULL
504 XMLIncludes
* includes
,
506 bool* pend_tag
= NULL
)
509 if ( !f
.get_token(token
) )
511 bool end_tag
, is_include
= false;
513 while ( token
[0] != '<'
514 || !strncmp ( token
.c_str(), "<!--", 4 )
515 || !strncmp ( token
.c_str(), "<?", 2 ) )
517 if ( token
[0] != '<' )
518 throw XMLSyntaxErrorException ( f
.Location(),
519 "expecting xml tag, not '%s'",
521 if ( !f
.get_token(token
) )
525 XMLElement
* e
= new XMLElement ( f
.Location() );
526 bool bNeedEnd
= e
->Parse ( token
, end_tag
);
528 if ( e
->name
== "xi:include" && includes
)
530 includes
->push_back ( new XMLInclude ( e
, path
) );
541 throw XMLSyntaxErrorException ( f
.Location(),
542 "end tag '%s' not expected",
548 bool bThisMixingErrorReported
= false;
549 while ( f
.more_tokens() )
551 if ( f
.next_is_text() )
553 if ( !f
.get_token ( token
) || !token
.size() )
555 throw InvalidBuildFileException (
557 "internal tool error - get_token() failed when more_tokens() returned true" );
560 if ( e
->subElements
.size() && !bThisMixingErrorReported
)
562 throw XMLSyntaxErrorException ( f
.Location(),
563 "mixing of inner text with sub elements" );
564 bThisMixingErrorReported
= true;
566 if ( strchr ( token
.c_str(), '>' ) )
568 throw XMLSyntaxErrorException ( f
.Location(),
569 "invalid symbol '>'" );
571 if ( e
->value
.size() )
573 throw XMLSyntaxErrorException ( f
.Location(),
574 "multiple instances of inner text" );
575 e
->value
+= " " + token
;
582 XMLElement
* e2
= XMLParse ( f
, is_include
? NULL
: includes
, path
, &end_tag
);
585 throw InvalidBuildFileException (
587 "end of file found looking for end tag" );
592 if ( e
->name
!= e2
->name
)
595 throw XMLSyntaxErrorException ( f
.Location(),
596 "end tag name mismatch" );
602 if ( e
->value
.size() && !bThisMixingErrorReported
)
604 throw XMLSyntaxErrorException ( f
.Location(),
605 "mixing of inner text with sub elements" );
606 bThisMixingErrorReported
= true;
608 e
->AddSubElement ( e2
);
615 XMLReadFile ( XMLFile
& f
, XMLElement
& head
, XMLIncludes
& includes
, const Path
& path
)
619 XMLElement
* e
= XMLParse ( f
, &includes
, path
);
622 head
.AddSubElement ( e
);
627 XMLLoadInclude ( XMLElement
* e
, const Path
& path
, XMLIncludes
& includes
)
630 att
= e
->GetAttribute("href",true);
633 string
file ( path
.Fixup(att
->value
,true) );
634 string
top_file ( Path::RelativeFromWorkingDirectory ( file
) );
635 e
->attributes
.push_back ( new XMLAttribute ( "top_href", top_file
) );
637 if ( !fInc
.open ( file
) )
639 // look for xi:fallback element
640 for ( size_t i
= 0; i
< e
->subElements
.size(); i
++ )
642 XMLElement
* e2
= e
->subElements
[i
];
643 if ( e2
->name
== "xi:fallback" )
645 // now look for xi:include below...
646 for ( i
= 0; i
< e2
->subElements
.size(); i
++ )
648 XMLElement
* e3
= e2
->subElements
[i
];
649 if ( e3
->name
== "xi:include" )
651 return XMLLoadInclude ( e3
, path
, includes
);
654 throw InvalidBuildFileException (
656 "<xi:fallback> must have a <xi:include> sub-element" );
664 XMLElement
* new_e
= new XMLElement ( e
->location
);
665 new_e
->name
= "xi:included";
666 Path
path2 ( path
, att
->value
);
667 XMLReadFile ( fInc
, *new_e
, includes
, path2
);
673 XMLLoadFile ( const string
& filename
, const Path
& path
)
675 XMLIncludes includes
;
678 if ( !f
.open ( filename
) )
679 throw FileNotFoundException ( filename
);
681 XMLElement
* head
= new XMLElement("(virtual)");
683 XMLReadFile ( f
, *head
, includes
, path
);
685 for ( size_t i
= 0; i
< includes
.size(); i
++ )
687 XMLElement
* e
= includes
[i
]->e
;
688 XMLElement
* e2
= XMLLoadInclude ( includes
[i
]->e
, includes
[i
]->path
, includes
);
691 throw FileNotFoundException (
692 ssprintf("%s (referenced from %s)",
693 e
->GetAttribute("top_href",true)->value
.c_str(),
694 f
.Location().c_str() ) );
696 XMLElement
* parent
= e
->parentElement
;
697 XMLElement
** parent_container
= NULL
;
701 throw Exception ( "internal tool error: xi:include doesn't have a parent" );
704 for ( size_t j
= 0; j
< parent
->subElements
.size(); j
++ )
706 if ( parent
->subElements
[j
] == e
)
708 parent_container
= &parent
->subElements
[j
];
712 if ( !parent_container
)
715 throw Exception ( "internal tool error: couldn't find xi:include in parent's sub-elements" );
718 // replace inclusion tree with the imported tree
719 e2
->parentElement
= e
->parentElement
;
721 e2
->attributes
= e
->attributes
;
722 *parent_container
= e2
;
723 e
->attributes
.resize(0);