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
;
34 XMLIncludes::~XMLIncludes()
36 for ( size_t i
= 0; i
< this->size(); i
++ )
41 InitWorkingDirectory()
43 // store the current directory for path calculations
44 working_directory
.resize ( _MAX_PATH
);
45 working_directory
[0] = 0;
46 getcwd ( &working_directory
[0], working_directory
.size() );
47 working_directory
.resize ( strlen ( working_directory
.c_str() ) );
58 return _filelengthi64 ( _fileno(f
) );
60 struct stat64 file_stat
;
61 if ( fstat64(fileno(f
), &file_stat
) != 0 )
63 return file_stat
.st_size
;
69 if ( !working_directory
.size() )
70 InitWorkingDirectory();
71 string
s ( working_directory
);
72 const char* p
= strtok ( &s
[0], "/\\" );
77 p
= strtok ( NULL
, "/\\" );
81 Path::Path ( const Path
& cwd
, const string
& file
)
83 string
s ( cwd
.Fixup ( file
, false ) );
84 const char* p
= strtok ( &s
[0], "/\\" );
89 p
= strtok ( NULL
, "/\\" );
94 Path::Fixup ( const string
& file
, bool include_filename
) const
96 if ( strchr ( "/\\", file
[0] )
98 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
105 vector
<string
> pathtmp ( path
);
107 const char* prev
= strtok ( &tmp
[0], "/\\" );
108 const char* p
= strtok ( NULL
, "/\\" );
111 if ( !strcmp ( prev
, "." ) )
113 else if ( !strcmp ( prev
, ".." ) )
115 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
117 if ( pathtmp
.size() > 1 )
119 if ( pathtmp
.size() )
121 pathtmp
.resize ( pathtmp
.size() - 1 );
124 pathtmp
.push_back ( prev
);
126 p
= strtok ( NULL
, "/\\" );
128 if ( include_filename
)
129 pathtmp
.push_back ( prev
);
131 // reuse tmp variable to return recombined path
133 for ( size_t i
= 0; i
< pathtmp
.size(); i
++ )
135 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
147 Path::RelativeFromWorkingDirectory ()
150 for ( size_t i
= 0; i
< path
.size(); i
++ )
152 out
+= "/" + path
[i
];
154 return RelativeFromWorkingDirectory ( out
);
158 Path::RelativeFromWorkingDirectory ( const string
& path
)
160 vector
<string
> vwork
, vpath
, vout
;
161 Path::Split ( vwork
, working_directory
, true );
162 Path::Split ( vpath
, path
, true );
164 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
165 // not possible to do relative across different drive letters
166 if ( vwork
[0] != vpath
[0] )
170 while ( i
< vwork
.size() && i
< vpath
.size() && vwork
[i
] == vpath
[i
] )
172 if ( i
< vwork
.size() )
174 // path goes above our working directory, we will need some ..'s
175 for ( size_t j
= 0; j
< i
; j
++ )
176 vout
.push_back ( ".." );
178 while ( i
< vpath
.size() )
179 vout
.push_back ( vpath
[i
++] );
181 // now merge vout into a string again
182 string out
= vout
[0];
183 for ( i
= 1; i
< vout
.size(); i
++ )
185 out
+= "/" + vout
[i
];
191 Path::Split ( vector
<string
>& out
,
196 const char* prev
= strtok ( &s
[0], "/\\" );
197 const char* p
= strtok ( NULL
, "/\\" );
201 out
.push_back ( prev
);
203 p
= strtok ( NULL
, "/\\" );
206 out
.push_back ( prev
);
221 XMLFile::open(const string
& filename_
)
224 FILE* f
= fopen ( filename_
.c_str(), "rb" );
227 unsigned long len
= (unsigned long)filelen(f
);
229 fread ( &_buf
[0], 1, len
, f
);
233 _filename
= filename_
;
238 // next_token() moves the pointer to next token, which may be
239 // an xml element or a text element, basically it's a glorified
240 // skipspace, normally the user of this class won't need to call
243 XMLFile::next_token()
245 _p
+= strspn ( _p
, WS
);
249 XMLFile::next_is_text()
255 XMLFile::more_tokens()
260 // get_token() is used to return a token, and move the pointer
263 XMLFile::get_token(string
& token
)
266 if ( !strncmp ( _p
, "<!--", 4 ) )
268 tokend
= strstr ( _p
, "-->" );
274 else if ( !strncmp ( _p
, "<?", 2 ) )
276 tokend
= strstr ( _p
, "?>" );
282 else if ( *_p
== '<' )
284 tokend
= strchr ( _p
, '>' );
292 tokend
= strchr ( _p
, '<' );
295 while ( tokend
> _p
&& isspace(tokend
[-1]) )
300 token
= string ( _p
, tokend
-_p
);
307 XMLFile::Location() const
310 const char* p
= strchr ( _buf
.c_str(), '\n' );
311 while ( p
&& p
< _p
)
314 p
= strchr ( p
+1, '\n' );
316 return ssprintf ( "%s(%i)",_filename
.c_str(), line
);
319 XMLAttribute::XMLAttribute()
323 XMLAttribute::XMLAttribute(const string
& name_
,
324 const string
& value_
)
325 : name(name_
), value(value_
)
329 XMLAttribute::XMLAttribute ( const XMLAttribute
& src
)
330 : name(src
.name
), value(src
.value
)
335 XMLAttribute
& XMLAttribute::operator = ( const XMLAttribute
& src
)
342 XMLElement::XMLElement ( XMLFile
* xmlFile
,
343 const string
& location
)
344 : xmlFile ( xmlFile
),
345 location ( location
),
346 parentElement ( NULL
)
350 XMLElement::~XMLElement()
353 for ( i
= 0; i
< attributes
.size(); i
++ )
354 delete attributes
[i
];
355 for ( i
= 0; i
< subElements
.size(); i
++ )
356 delete subElements
[i
];
360 XMLElement::AddSubElement ( XMLElement
* e
)
362 subElements
.push_back ( e
);
363 e
->parentElement
= this;
367 // This function takes a single xml tag ( i.e. beginning with '<' and
368 // ending with '>', and parses out it's tag name and constituent
370 // Return Value: returns true if you need to look for a </tag> for
371 // the one it just parsed...
373 XMLElement::Parse(const string
& token
,
376 const char* p
= token
.c_str();
377 assert ( *p
== '<' );
379 p
+= strspn ( p
, WS
);
381 // check if this is a comment
382 if ( !strncmp ( p
, "!--", 3 ) )
386 return false; // never look for end tag to a comment
389 end_tag
= ( *p
== '/' );
393 p
+= strspn ( p
, WS
);
395 const char* end
= strpbrk ( p
, WS
);
398 end
= strpbrk ( p
, "/>" );
401 name
= string ( p
, end
-p
);
403 p
+= strspn ( p
, WS
);
404 while ( *p
!= '>' && *p
!= '/' )
406 end
= strpbrk ( p
, WSEQ
);
409 end
= strpbrk ( p
, "/>" );
412 string
attribute ( p
, end
-p
), value
;
414 p
+= strspn ( p
, WS
);
418 p
+= strspn ( p
, WS
);
420 if ( strchr ( "\"'", *p
) )
423 end
= strchr ( p
, quote
);
427 end
= strpbrk ( p
, WS
);
431 end
= strchr ( p
, '>' );
433 if ( end
[-1] == '/' )
436 value
= string ( p
, end
-p
);
438 if ( quote
&& *p
== quote
)
440 p
+= strspn ( p
, WS
);
442 else if ( name
[0] != '!' )
444 throw XMLSyntaxErrorException ( location
,
445 "attributes must have values" );
447 attributes
.push_back ( new XMLAttribute ( attribute
, value
) );
449 return !( *p
== '/' ) && !end_tag
;
453 XMLElement::GetAttribute ( const string
& attribute
,
456 // this would be faster with a tree-based container, but our attribute
457 // lists are likely to stay so short as to not be an issue.
458 for ( size_t i
= 0; i
< attributes
.size(); i
++ )
460 if ( attribute
== attributes
[i
]->name
)
461 return attributes
[i
];
465 throw RequiredAttributeNotFoundException ( location
,
473 XMLElement::GetAttribute ( const string
& attribute
,
474 bool required
) const
476 // this would be faster with a tree-based container, but our attribute
477 // lists are likely to stay so short as to not be an issue.
478 for ( size_t i
= 0; i
< attributes
.size(); i
++ )
480 if ( attribute
== attributes
[i
]->name
)
481 return attributes
[i
];
485 throw RequiredAttributeNotFoundException ( location
,
493 // This function reads a "token" from the file loaded in XMLFile
494 // if it finds a tag that is non-singular, it parses sub-elements and/or
495 // inner text into the XMLElement that it is building to return.
496 // Return Value: an XMLElement allocated via the new operator that contains
497 // it's parsed data. Keep calling this function until it returns NULL
500 XMLParse ( XMLFile
& f
,
501 XMLIncludes
* includes
,
503 bool* pend_tag
= NULL
)
506 if ( !f
.get_token(token
) )
508 bool end_tag
, is_include
= false;
510 while ( token
[0] != '<'
511 || !strncmp ( token
.c_str (), "<!--", 4 )
512 || !strncmp ( token
.c_str (), "<?", 2 ) )
514 if ( token
[0] != '<' )
515 throw XMLSyntaxErrorException ( f
.Location (),
516 "expecting xml tag, not '%s'",
518 if ( !f
.get_token(token
) )
522 XMLElement
* e
= new XMLElement ( &f
,
524 bool bNeedEnd
= e
->Parse ( token
, end_tag
);
526 if ( e
->name
== "xi:include" && includes
)
529 att
= e
->GetAttribute ( "href", true );
531 string
includeFile ( path
.Fixup ( att
->value
, true ) );
532 string
topIncludeFile ( Path::RelativeFromWorkingDirectory ( includeFile
) );
533 includes
->push_back ( new XMLInclude ( e
, path
, topIncludeFile
) );
544 throw XMLSyntaxErrorException ( f
.Location (),
545 "end tag '%s' not expected",
551 bool bThisMixingErrorReported
= false;
552 while ( f
.more_tokens () )
554 if ( f
.next_is_text () )
556 if ( !f
.get_token ( token
) || token
.size () == 0 )
558 throw InvalidBuildFileException (
560 "internal tool error - get_token() failed when more_tokens() returned true" );
563 if ( e
->subElements
.size() && !bThisMixingErrorReported
)
565 throw XMLSyntaxErrorException ( f
.Location (),
566 "mixing of inner text with sub elements" );
567 bThisMixingErrorReported
= true;
569 if ( strchr ( token
.c_str (), '>' ) )
571 throw XMLSyntaxErrorException ( f
.Location (),
572 "invalid symbol '>'" );
574 if ( e
->value
.size() > 0 )
576 throw XMLSyntaxErrorException ( f
.Location (),
577 "multiple instances of inner text" );
578 e
->value
+= " " + token
;
585 XMLElement
* e2
= XMLParse ( f
, is_include
? NULL
: includes
, path
, &end_tag
);
588 throw InvalidBuildFileException (
590 "end of file found looking for end tag" );
595 if ( e
->name
!= e2
->name
)
598 throw XMLSyntaxErrorException ( f
.Location (),
599 "end tag name mismatch" );
605 if ( e
->value
.size () > 0 && !bThisMixingErrorReported
)
607 throw XMLSyntaxErrorException ( f
.Location (),
608 "mixing of inner text with sub elements" );
609 bThisMixingErrorReported
= true;
611 e
->AddSubElement ( e2
);
618 XMLReadFile ( XMLFile
& f
, XMLElement
& head
, XMLIncludes
& includes
, const Path
& path
)
622 XMLElement
* e
= XMLParse ( f
, &includes
, path
);
625 head
.AddSubElement ( e
);
630 XMLLoadInclude ( XMLInclude
& include
,
631 XMLIncludes
& includes
)
634 att
= include
.e
->GetAttribute("href", true);
637 string
file ( include
.path
.Fixup(att
->value
, true) );
638 string
top_file ( Path::RelativeFromWorkingDirectory ( file
) );
639 include
.e
->attributes
.push_back ( new XMLAttribute ( "top_href", top_file
) );
640 XMLFile
* fInc
= new XMLFile();
641 if ( !fInc
->open ( file
) )
643 include
.fileExists
= false;
644 // look for xi:fallback element
645 for ( size_t i
= 0; i
< include
.e
->subElements
.size (); i
++ )
647 XMLElement
* e2
= include
.e
->subElements
[i
];
648 if ( e2
->name
== "xi:fallback" )
650 // now look for xi:include below...
651 for ( i
= 0; i
< e2
->subElements
.size (); i
++ )
653 XMLElement
* e3
= e2
->subElements
[i
];
654 if ( e3
->name
== "xi:include" )
656 att
= e3
->GetAttribute ( "href", true );
658 string
includeFile ( include
.path
.Fixup ( att
->value
, true ) );
659 string
topIncludeFile ( Path::RelativeFromWorkingDirectory ( includeFile
) );
660 XMLInclude
* fallbackInclude
= new XMLInclude ( e3
, include
.path
, topIncludeFile
);
661 return XMLLoadInclude ( *fallbackInclude
, includes
);
664 throw InvalidBuildFileException (
666 "<xi:fallback> must have a <xi:include> sub-element" );
674 include
.fileExists
= true;
675 XMLElement
* new_e
= new XMLElement ( fInc
,
676 include
.e
->location
);
677 new_e
->name
= "xi:included";
678 Path
path2 ( include
.path
, att
->value
);
679 XMLReadFile ( *fInc
, *new_e
, includes
, path2
);
685 XMLLoadFile ( const string
& filename
,
687 XMLIncludes
& includes
)
689 XMLFile
* f
= new XMLFile();
691 if ( !f
->open ( filename
) )
692 throw FileNotFoundException ( filename
);
694 XMLElement
* head
= new XMLElement ( f
,
697 XMLReadFile ( *f
, *head
, includes
, path
);
699 for ( size_t i
= 0; i
< includes
.size (); i
++ )
701 XMLElement
* e
= includes
[i
]->e
;
702 XMLElement
* e2
= XMLLoadInclude ( *includes
[i
], includes
);
705 throw FileNotFoundException (
706 ssprintf ( "%s (referenced from %s)",
707 e
->GetAttribute ( "top_href", true )->value
.c_str (),
708 f
->Location ().c_str () ) );
710 XMLElement
* parent
= e
->parentElement
;
711 XMLElement
** parent_container
= NULL
;
715 throw Exception ( "internal tool error: xi:include doesn't have a parent" );
718 for ( size_t j
= 0; j
< parent
->subElements
.size (); j
++ )
720 if ( parent
->subElements
[j
] == e
)
722 parent_container
= &parent
->subElements
[j
];
726 if ( !parent_container
)
729 throw Exception ( "internal tool error: couldn't find xi:include in parent's sub-elements" );
732 // replace inclusion tree with the imported tree
733 e2
->parentElement
= e
->parentElement
;
735 e2
->attributes
= e
->attributes
;
736 *parent_container
= e2
;
737 e
->attributes
.resize ( 0 );