15 #define getcwd _getcwd
18 static const char* WS
= " \t\r\n";
19 static const char* WSEQ
= " =\t\r\n";
21 string working_directory
;
24 InitWorkingDirectory()
26 // store the current directory for path calculations
27 working_directory
.resize ( _MAX_PATH
);
28 working_directory
[0] = 0;
29 getcwd ( &working_directory
[0], working_directory
.size() );
30 working_directory
.resize ( strlen ( working_directory
.c_str() ) );
41 return _filelengthi64 ( _fileno(f
) );
43 struct stat64 file_stat
;
44 if ( fstat64(fileno(f
), &file_stat
) != 0 )
46 return file_stat
.st_size
;
52 if ( !working_directory
.size() )
53 InitWorkingDirectory();
54 string
s ( working_directory
);
55 const char* p
= strtok ( &s
[0], "/\\" );
60 p
= strtok ( NULL
, "/\\" );
64 Path::Path ( const Path
& cwd
, const string
& file
)
66 string
s ( cwd
.Fixup ( file
, false ) );
67 const char* p
= strtok ( &s
[0], "/\\" );
72 p
= strtok ( NULL
, "/\\" );
77 Path::Fixup ( const string
& file
, bool include_filename
) const
79 if ( strchr ( "/\\", file
[0] )
81 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
88 vector
<string
> pathtmp ( path
);
90 const char* prev
= strtok ( &tmp
[0], "/\\" );
91 const char* p
= strtok ( NULL
, "/\\" );
94 if ( !strcmp ( prev
, "." ) )
96 else if ( !strcmp ( prev
, ".." ) )
98 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
100 if ( pathtmp
.size() > 1 )
102 if ( pathtmp
.size() )
104 pathtmp
.resize ( pathtmp
.size() - 1 );
107 pathtmp
.push_back ( prev
);
109 p
= strtok ( NULL
, "/\\" );
111 if ( include_filename
)
112 pathtmp
.push_back ( prev
);
114 // reuse tmp variable to return recombined path
116 for ( size_t i
= 0; i
< pathtmp
.size(); i
++ )
118 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
130 Path::RelativeFromWorkingDirectory ( const string
& path
)
132 vector
<string
> vwork
, vpath
, vout
;
133 Path::Split ( vwork
, working_directory
, true );
134 Path::Split ( vpath
, path
, true );
136 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
137 // not possible to do relative across different drive letters
138 if ( vwork
[0] != vpath
[0] )
142 while ( i
< vwork
.size() && i
< vpath
.size() && vwork
[i
] == vpath
[i
] )
144 if ( i
< vwork
.size() )
146 // path goes above our working directory, we will need some ..'s
147 for ( size_t j
= 0; j
< i
; j
++ )
148 vout
.push_back ( ".." );
150 while ( i
< vpath
.size() )
151 vout
.push_back ( vpath
[i
++] );
153 // now merge vout into a string again
155 for ( i
= 0; i
< vout
.size(); i
++ )
157 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
169 Path::Split ( vector
<string
>& out
,
174 const char* prev
= strtok ( &s
[0], "/\\" );
175 const char* p
= strtok ( NULL
, "/\\" );
179 out
.push_back ( prev
);
181 p
= strtok ( NULL
, "/\\" );
184 out
.push_back ( prev
);
199 XMLFile::open(const string
& filename
)
202 FILE* f
= fopen ( filename
.c_str(), "rb" );
205 unsigned long len
= (unsigned long)filelen(f
);
207 fread ( &_buf
[0], 1, len
, f
);
215 // next_token() moves the pointer to next token, which may be
216 // an xml element or a text element, basically it's a glorified
217 // skipspace, normally the user of this class won't need to call
220 XMLFile::next_token()
222 _p
+= strspn ( _p
, WS
);
226 XMLFile::next_is_text()
232 XMLFile::more_tokens()
237 // get_token() is used to return a token, and move the pointer
240 XMLFile::get_token(string
& token
)
243 if ( !strncmp ( _p
, "<!--", 4 ) )
245 tokend
= strstr ( _p
, "-->" );
251 else if ( *_p
== '<' )
253 tokend
= strchr ( _p
, '>' );
261 tokend
= strchr ( _p
, '<' );
264 while ( tokend
> _p
&& isspace(tokend
[-1]) )
269 token
= string ( _p
, tokend
-_p
);
275 XMLAttribute::XMLAttribute()
279 XMLAttribute::XMLAttribute(const string
& name_
,
280 const string
& value_
)
281 : name(name_
), value(value_
)
285 XMLElement::XMLElement()
286 : parentElement(NULL
)
290 XMLElement::~XMLElement()
293 for ( i
= 0; i
< attributes
.size(); i
++ )
294 delete attributes
[i
];
295 for ( i
= 0; i
< subElements
.size(); i
++ )
296 delete subElements
[i
];
300 XMLElement::AddSubElement ( XMLElement
* e
)
302 subElements
.push_back ( e
);
303 e
->parentElement
= this;
307 // This function takes a single xml tag ( i.e. beginning with '<' and
308 // ending with '>', and parses out it's tag name and constituent
310 // Return Value: returns true if you need to look for a </tag> for
311 // the one it just parsed...
313 XMLElement::Parse(const string
& token
,
316 const char* p
= token
.c_str();
317 assert ( *p
== '<' );
319 p
+= strspn ( p
, WS
);
321 // check if this is a comment
322 if ( !strncmp ( p
, "!--", 3 ) )
326 return false; // never look for end tag to a comment
329 end_tag
= ( *p
== '/' );
333 p
+= strspn ( p
, WS
);
335 const char* end
= strpbrk ( p
, WS
);
338 end
= strpbrk ( p
, "/>" );
341 name
= string ( p
, end
-p
);
343 p
+= strspn ( p
, WS
);
344 while ( *p
!= '>' && *p
!= '/' )
346 end
= strpbrk ( p
, WSEQ
);
349 end
= strpbrk ( p
, "/>" );
352 string
attribute ( p
, end
-p
), value
;
354 p
+= strspn ( p
, WS
);
358 p
+= strspn ( p
, WS
);
360 if ( strchr ( "\"'", *p
) )
363 end
= strchr ( p
, quote
);
367 end
= strpbrk ( p
, WS
);
371 end
= strchr ( p
, '>' );
373 if ( end
[-1] == '/' )
376 value
= string ( p
, end
-p
);
378 if ( quote
&& *p
== quote
)
380 p
+= strspn ( p
, WS
);
382 attributes
.push_back ( new XMLAttribute ( attribute
, value
) );
384 return !( *p
== '/' ) && !end_tag
;
388 XMLElement::GetAttribute ( const string
& attribute
,
391 // this would be faster with a tree-based container, but our attribute
392 // lists are likely to stay so short as to not be an issue.
393 for ( size_t i
= 0; i
< attributes
.size(); i
++ )
395 if ( attribute
== attributes
[i
]->name
)
396 return attributes
[i
];
400 printf ( "syntax error: attribute '%s' required for <%s>\n",
401 attribute
.c_str(), name
.c_str() );
407 XMLElement::GetAttribute ( const string
& attribute
,
408 bool required
) const
410 // this would be faster with a tree-based container, but our attribute
411 // lists are likely to stay so short as to not be an issue.
412 for ( size_t i
= 0; i
< attributes
.size(); i
++ )
414 if ( attribute
== attributes
[i
]->name
)
415 return attributes
[i
];
419 printf ( "syntax error: attribute '%s' required for <%s>\n",
420 attribute
.c_str(), name
.c_str() );
426 // This function reads a "token" from the file loaded in XMLFile
427 // REM TODO FIXME: At the moment it can't handle comments or non-xml tags.
428 // if it finds a tag that is non-singular, it parses sub-elements and/or
429 // inner text into the XMLElement that it is building to return.
430 // Return Value: an XMLElement allocated via the new operator that contains
431 // it's parsed data. Keep calling this function until it returns NULL
436 bool* pend_tag
/*= NULL*/)
439 if ( !f
.get_token(token
) )
443 while ( token
[0] != '<' || !strncmp ( token
.c_str(), "<!--", 4 ) )
445 if ( token
[0] != '<' )
446 printf ( "syntax error: expecting xml tag, not '%s'\n", token
.c_str() );
447 if ( !f
.get_token(token
) )
451 XMLElement
* e
= new XMLElement
;
452 bool bNeedEnd
= e
->Parse ( token
, end_tag
);
454 if ( e
->name
== "xi:include" )
457 att
= e
->GetAttribute("href",true);
460 string
file ( path
.Fixup(att
->value
,true) );
461 string
top_file ( Path::RelativeFromWorkingDirectory ( file
) );
462 e
->attributes
.push_back ( new XMLAttribute ( "top_href", top_file
) );
464 if ( !fInc
.open ( file
) )
465 printf ( "xi:include error, couldn't find file '%s'\n", file
.c_str() );
468 Path
path2 ( path
, att
->value
);
471 XMLElement
* e2
= XMLParse ( fInc
, path2
);
474 e
->AddSubElement ( e2
);
487 printf ( "syntax error: end tag '%s' not expected\n", token
.c_str() );
492 bool bThisMixingErrorReported
= false;
493 while ( f
.more_tokens() )
495 if ( f
.next_is_text() )
497 if ( !f
.get_token ( token
) || !token
.size() )
499 printf ( "internal tool error - get_token() failed when more_tokens() returned true\n" );
502 if ( e
->subElements
.size() && !bThisMixingErrorReported
)
504 printf ( "syntax error: mixing of inner text with sub elements\n" );
505 bThisMixingErrorReported
= true;
507 if ( e
->value
.size() )
509 printf ( "syntax error: multiple instances of inner text\n" );
510 e
->value
+= " " + token
;
517 XMLElement
* e2
= XMLParse ( f
, path
, &end_tag
);
520 if ( e
->name
!= e2
->name
)
521 printf ( "end tag name mismatch\n" );
525 if ( e
->value
.size() && !bThisMixingErrorReported
)
527 printf ( "syntax error: mixing of inner text with sub elements\n" );
528 bThisMixingErrorReported
= true;
530 e
->AddSubElement ( e2
);