break XML-parsing stuff into it's own files, and fix 'make test'
authorRoyce Mitchell III <royce3@ev1.net>
Tue, 4 Jan 2005 17:53:26 +0000 (17:53 +0000)
committerRoyce Mitchell III <royce3@ev1.net>
Tue, 4 Jan 2005 17:53:26 +0000 (17:53 +0000)
svn path=/branches/xmlbuildsystem/; revision=12792

reactos/tools/rbuild/XML.cpp [new file with mode: 0644]
reactos/tools/rbuild/XML.h [new file with mode: 0644]
reactos/tools/rbuild/makefile
reactos/tools/rbuild/rbuild.cpp
reactos/tools/rbuild/rbuild.h
reactos/tools/rbuild/tests/alltests.cpp

diff --git a/reactos/tools/rbuild/XML.cpp b/reactos/tools/rbuild/XML.cpp
new file mode 100644 (file)
index 0000000..26b25c3
--- /dev/null
@@ -0,0 +1,538 @@
+// XML.cpp\r
+\r
+#ifdef _MSC_VER\r
+#pragma warning ( disable : 4786 ) // identifier was truncated to '255' characters in the debug information\r
+#endif//_MSC_VER\r
+\r
+#include <direct.h>\r
+#include <io.h>\r
+#include <assert.h>\r
+\r
+#include "XML.h"\r
+\r
+using std::string;\r
+using std::vector;\r
+\r
+#ifdef WIN32\r
+#define getcwd _getcwd\r
+#endif//WIN32\r
+\r
+static const char* WS = " \t\r\n";\r
+static const char* WSEQ = " =\t\r\n";\r
+\r
+string working_directory;\r
+\r
+void\r
+InitWorkingDirectory()\r
+{\r
+       // store the current directory for path calculations\r
+       working_directory.resize ( _MAX_PATH );\r
+       working_directory[0] = 0;\r
+       getcwd ( &working_directory[0], working_directory.size() );\r
+       working_directory.resize ( strlen ( working_directory.c_str() ) );\r
+}\r
+\r
+#ifdef _MSC_VER\r
+unsigned __int64\r
+#else\r
+unsigned long long\r
+#endif\r
+filelen ( FILE* f )\r
+{\r
+#ifdef WIN32\r
+       return _filelengthi64 ( _fileno(f) );\r
+#elif defined(UNIX)\r
+       struct stat64 file_stat;\r
+       if ( fstat64(fileno(f), &file_stat) != 0 )\r
+               return 0;\r
+       return file_stat.st_size;\r
+#endif\r
+}\r
+\r
+Path::Path()\r
+{\r
+       string s ( working_directory );\r
+       const char* p = strtok ( &s[0], "/\\" );\r
+       while ( p )\r
+       {\r
+               if ( *p )\r
+                       path.push_back ( p );\r
+               p = strtok ( NULL, "/\\" );\r
+       }\r
+}\r
+\r
+Path::Path ( const Path& cwd, const string& file )\r
+{\r
+       string s ( cwd.Fixup ( file, false ) );\r
+       const char* p = strtok ( &s[0], "/\\" );\r
+       while ( p )\r
+       {\r
+               if ( *p )\r
+                       path.push_back ( p );\r
+               p = strtok ( NULL, "/\\" );\r
+       }\r
+}\r
+\r
+string\r
+Path::Fixup ( const string& file, bool include_filename ) const\r
+{\r
+       if ( strchr ( "/\\", file[0] )\r
+#ifdef WIN32\r
+               // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
+               || file[1] == ':'\r
+#endif//WIN32\r
+               )\r
+       {\r
+               return file;\r
+       }\r
+       vector<string> pathtmp ( path );\r
+       string tmp ( file );\r
+       const char* prev = strtok ( &tmp[0], "/\\" );\r
+       const char* p = strtok ( NULL, "/\\" );\r
+       while ( p )\r
+       {\r
+               if ( !strcmp ( prev, "." ) )\r
+                       ; // do nothing\r
+               else if ( !strcmp ( prev, ".." ) )\r
+               {\r
+                       // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
+#ifdef WIN32\r
+                       if ( pathtmp.size() > 1 )\r
+#else\r
+                       if ( pathtmp.size() )\r
+#endif\r
+                               pathtmp.resize ( pathtmp.size() - 1 );\r
+               }\r
+               else\r
+                       pathtmp.push_back ( prev );\r
+               prev = p;\r
+               p = strtok ( NULL, "/\\" );\r
+       }\r
+       if ( include_filename )\r
+               pathtmp.push_back ( prev );\r
+\r
+       // reuse tmp variable to return recombined path\r
+       tmp.resize(0);\r
+       for ( size_t i = 0; i < pathtmp.size(); i++ )\r
+       {\r
+               // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
+#ifdef WIN32\r
+               if ( i ) tmp += "/";\r
+#else\r
+               tmp += "/";\r
+#endif\r
+               tmp += pathtmp[i];\r
+       }\r
+       return tmp;\r
+}\r
+\r
+/*static*/ string\r
+Path::RelativeFromWorkingDirectory ( const string& path )\r
+{\r
+       vector<string> vwork, vpath, vout;\r
+       Path::Split ( vwork, working_directory, true );\r
+       Path::Split ( vpath, path, true );\r
+#ifdef WIN32\r
+       // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
+       // not possible to do relative across different drive letters\r
+       if ( vwork[0] != vpath[0] )\r
+               return path;\r
+#endif\r
+       size_t i = 0;\r
+       while ( i < vwork.size() && i < vpath.size() && vwork[i] == vpath[i] )\r
+               ++i;\r
+       if ( i < vwork.size() )\r
+       {\r
+               // path goes above our working directory, we will need some ..'s\r
+               for ( size_t j = 0; j < i; j++ )\r
+                       vout.push_back ( ".." );\r
+       }\r
+       while ( i < vpath.size() )\r
+               vout.push_back ( vpath[i++] );\r
+\r
+       // now merge vout into a string again\r
+       string out;\r
+       for ( i = 0; i < vout.size(); i++ )\r
+       {\r
+               // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
+#ifdef WIN32\r
+               if ( i ) out += "/";\r
+#else\r
+               out += "/";\r
+#endif\r
+               out += vout[i];\r
+       }\r
+       return out;\r
+}\r
+\r
+/*static*/ void\r
+Path::Split ( vector<string>& out,\r
+              const string& path,\r
+              bool include_last )\r
+{\r
+       string s ( path );\r
+       const char* prev = strtok ( &s[0], "/\\" );\r
+       const char* p = strtok ( NULL, "/\\" );\r
+       out.resize ( 0 );\r
+       while ( p )\r
+       {\r
+               out.push_back ( prev );\r
+               prev = p;\r
+               p = strtok ( NULL, "/\\" );\r
+       }\r
+       if ( include_last )\r
+               out.push_back ( prev );\r
+}\r
+\r
+XMLFile::XMLFile()\r
+{\r
+}\r
+\r
+void\r
+XMLFile::close()\r
+{\r
+       while ( _f.size() )\r
+       {\r
+               fclose ( _f.back() );\r
+               _f.pop_back();\r
+       }\r
+       _buf.resize(0);\r
+       _p = _end = NULL;\r
+}\r
+\r
+bool\r
+XMLFile::open(const string& filename)\r
+{\r
+       close();\r
+       FILE* f = fopen ( filename.c_str(), "rb" );\r
+       if ( !f )\r
+               return false;\r
+       unsigned long len = (unsigned long)filelen(f);\r
+       _buf.resize ( len );\r
+       fread ( &_buf[0], 1, len, f );\r
+       _p = _buf.c_str();\r
+       _end = _p + len;\r
+       _f.push_back ( f );\r
+       next_token();\r
+       return true;\r
+}\r
+\r
+// next_token() moves the pointer to next token, which may be\r
+// an xml element or a text element, basically it's a glorified\r
+// skipspace, normally the user of this class won't need to call\r
+// this function\r
+void\r
+XMLFile::next_token()\r
+{\r
+       _p += strspn ( _p, WS );\r
+}\r
+\r
+bool\r
+XMLFile::next_is_text()\r
+{\r
+       return *_p != '<';\r
+}\r
+\r
+bool\r
+XMLFile::more_tokens()\r
+{\r
+       return _p != _end;\r
+}\r
+\r
+// get_token() is used to return a token, and move the pointer\r
+// past the token\r
+bool\r
+XMLFile::get_token(string& token)\r
+{\r
+       const char* tokend;\r
+       if ( !strncmp ( _p, "<!--", 4 ) )\r
+       {\r
+               tokend = strstr ( _p, "-->" );\r
+               if ( !tokend )\r
+                       tokend = _end;\r
+               else\r
+                       tokend += 3;\r
+       }\r
+       else if ( *_p == '<' )\r
+       {\r
+               tokend = strchr ( _p, '>' );\r
+               if ( !tokend )\r
+                       tokend = _end;\r
+               else\r
+                       ++tokend;\r
+       }\r
+       else\r
+       {\r
+               tokend = strchr ( _p, '<' );\r
+               if ( !tokend )\r
+                       tokend = _end;\r
+               while ( tokend > _p && isspace(tokend[-1]) )\r
+                       --tokend;\r
+       }\r
+       if ( tokend == _p )\r
+               return false;\r
+       token = string ( _p, tokend-_p );\r
+       _p = tokend;\r
+       next_token();\r
+       return true;\r
+}\r
+\r
+XMLAttribute::XMLAttribute()\r
+{\r
+}\r
+\r
+XMLAttribute::XMLAttribute(const string& name_,\r
+                           const string& value_)\r
+       : name(name_), value(value_)\r
+{\r
+}\r
+\r
+XMLElement::XMLElement()\r
+       : parentElement(NULL)\r
+{\r
+}\r
+\r
+XMLElement::~XMLElement()\r
+{\r
+       size_t i;\r
+       for ( i = 0; i < attributes.size(); i++ )\r
+               delete attributes[i];\r
+       for ( i = 0; i < subElements.size(); i++ )\r
+               delete subElements[i];\r
+}\r
+\r
+void\r
+XMLElement::AddSubElement ( XMLElement* e )\r
+{\r
+       subElements.push_back ( e );\r
+       e->parentElement = this;\r
+}\r
+\r
+// Parse()\r
+// This function takes a single xml tag ( i.e. beginning with '<' and\r
+// ending with '>', and parses out it's tag name and constituent\r
+// attributes.\r
+// Return Value: returns true if you need to look for a </tag> for\r
+// the one it just parsed...\r
+bool\r
+XMLElement::Parse(const string& token,\r
+                  bool& end_tag)\r
+{\r
+       const char* p = token.c_str();\r
+       assert ( *p == '<' );\r
+       ++p;\r
+       p += strspn ( p, WS );\r
+\r
+       // check if this is a comment\r
+       if ( !strncmp ( p, "!--", 3 ) )\r
+       {\r
+               name = "!--";\r
+               end_tag = false;\r
+               return false; // never look for end tag to a comment\r
+       }\r
+\r
+       end_tag = ( *p == '/' );\r
+       if ( end_tag )\r
+       {\r
+               ++p;\r
+               p += strspn ( p, WS );\r
+       }\r
+       const char* end = strpbrk ( p, WS );\r
+       if ( !end )\r
+       {\r
+               end = strpbrk ( p, "/>" );\r
+               assert ( end );\r
+       }\r
+       name = string ( p, end-p );\r
+       p = end;\r
+       p += strspn ( p, WS );\r
+       while ( *p != '>' && *p != '/' )\r
+       {\r
+               end = strpbrk ( p, WSEQ );\r
+               if ( !end )\r
+               {\r
+                       end = strpbrk ( p, "/>" );\r
+                       assert ( end );\r
+               }\r
+               string attribute ( p, end-p ), value;\r
+               p = end;\r
+               p += strspn ( p, WS );\r
+               if ( *p == '=' )\r
+               {\r
+                       ++p;\r
+                       p += strspn ( p, WS );\r
+                       char quote = 0;\r
+                       if ( strchr ( "\"'", *p ) )\r
+                       {\r
+                               quote = *p++;\r
+                               end = strchr ( p, quote );\r
+                       }\r
+                       else\r
+                       {\r
+                               end = strpbrk ( p, WS );\r
+                       }\r
+                       if ( !end )\r
+                       {\r
+                               end = strchr ( p, '>' );\r
+                               assert(end);\r
+                               if ( end[-1] == '/' )\r
+                                       end--;\r
+                       }\r
+                       value = string ( p, end-p );\r
+                       p = end;\r
+                       if ( quote && *p == quote )\r
+                               p++;\r
+                       p += strspn ( p, WS );\r
+               }\r
+               attributes.push_back ( new XMLAttribute ( attribute, value ) );\r
+       }\r
+       return !( *p == '/' ) && !end_tag;\r
+}\r
+\r
+XMLAttribute*\r
+XMLElement::GetAttribute ( const string& attribute,\r
+                           bool required )\r
+{\r
+       // this would be faster with a tree-based container, but our attribute\r
+       // lists are likely to stay so short as to not be an issue.\r
+       for ( size_t i = 0; i < attributes.size(); i++ )\r
+       {\r
+               if ( attribute == attributes[i]->name )\r
+                       return attributes[i];\r
+       }\r
+       if ( required )\r
+       {\r
+               printf ( "syntax error: attribute '%s' required for <%s>\n",\r
+                       attribute.c_str(), name.c_str() );\r
+       }\r
+       return NULL;\r
+}\r
+\r
+const XMLAttribute*\r
+XMLElement::GetAttribute ( const string& attribute,\r
+                           bool required ) const\r
+{\r
+       // this would be faster with a tree-based container, but our attribute\r
+       // lists are likely to stay so short as to not be an issue.\r
+       for ( size_t i = 0; i < attributes.size(); i++ )\r
+       {\r
+               if ( attribute == attributes[i]->name )\r
+                       return attributes[i];\r
+       }\r
+       if ( required )\r
+       {\r
+               printf ( "syntax error: attribute '%s' required for <%s>\n",\r
+                       attribute.c_str(), name.c_str() );\r
+       }\r
+       return NULL;\r
+}\r
+\r
+// XMLParse()\r
+// This function reads a "token" from the file loaded in XMLFile\r
+// REM TODO FIXME: At the moment it can't handle comments or non-xml tags.\r
+// if it finds a tag that is non-singular, it parses sub-elements and/or\r
+// inner text into the XMLElement that it is building to return.\r
+// Return Value: an XMLElement allocated via the new operator that contains\r
+// it's parsed data. Keep calling this function until it returns NULL\r
+// (no more data)\r
+XMLElement*\r
+XMLParse(XMLFile& f,\r
+         const Path& path,\r
+         bool* pend_tag /*= NULL*/)\r
+{\r
+       string token;\r
+       if ( !f.get_token(token) )\r
+               return NULL;\r
+       bool end_tag;\r
+\r
+       while ( token[0] != '<' )\r
+       {\r
+               printf ( "syntax error: expecting xml tag, not '%s'\n", token.c_str() );\r
+               if ( !f.get_token(token) )\r
+                       return NULL;\r
+       }\r
+\r
+       XMLElement* e = new XMLElement;\r
+       bool bNeedEnd = e->Parse ( token, end_tag );\r
+\r
+       if ( e->name == "xi:include" )\r
+       {\r
+               XMLAttribute* att;\r
+               att = e->GetAttribute("href",true);\r
+               if ( att )\r
+               {\r
+                       string file ( path.Fixup(att->value,true) );\r
+                       string top_file ( Path::RelativeFromWorkingDirectory ( file ) );\r
+                       e->attributes.push_back ( new XMLAttribute ( "top_href", top_file ) );\r
+                       XMLFile fInc;\r
+                       if ( !fInc.open ( file ) )\r
+                               printf ( "xi:include error, couldn't find file '%s'\n", file.c_str() );\r
+                       else\r
+                       {\r
+                               Path path2 ( path, att->value );\r
+                               for ( ;; )\r
+                               {\r
+                                       XMLElement* e2 = XMLParse ( fInc, path2 );\r
+                                       if ( !e2 )\r
+                                               break;\r
+                                       e->AddSubElement ( e2 );\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       if ( !bNeedEnd )\r
+       {\r
+               if ( pend_tag )\r
+                       *pend_tag = end_tag;\r
+               else if ( end_tag )\r
+               {\r
+                       delete e;\r
+                       printf ( "syntax error: end tag '%s' not expected\n", token.c_str() );\r
+                       return NULL;\r
+               }\r
+               return e;\r
+       }\r
+       bool bThisMixingErrorReported = false;\r
+       while ( f.more_tokens() )\r
+       {\r
+               if ( f.next_is_text() )\r
+               {\r
+                       if ( !f.get_token ( token ) || !token.size() )\r
+                       {\r
+                               printf ( "internal tool error - get_token() failed when more_tokens() returned true\n" );\r
+                               break;\r
+                       }\r
+                       if ( e->subElements.size() && !bThisMixingErrorReported )\r
+                       {\r
+                               printf ( "syntax error: mixing of inner text with sub elements\n" );\r
+                               bThisMixingErrorReported = true;\r
+                       }\r
+                       if ( e->value.size() )\r
+                       {\r
+                               printf ( "syntax error: multiple instances of inner text\n" );\r
+                               e->value += " " + token;\r
+                       }\r
+                       else\r
+                               e->value = token;\r
+               }\r
+               else\r
+               {\r
+                       XMLElement* e2 = XMLParse ( f, path, &end_tag );\r
+                       if ( end_tag )\r
+                       {\r
+                               if ( e->name != e2->name )\r
+                                       printf ( "end tag name mismatch\n" );\r
+                               delete e2;\r
+                               break;\r
+                       }\r
+                       if ( e->value.size() && !bThisMixingErrorReported )\r
+                       {\r
+                               printf ( "syntax error: mixing of inner text with sub elements\n" );\r
+                               bThisMixingErrorReported = true;\r
+                       }\r
+                       e->AddSubElement ( e2 );\r
+               }\r
+       }\r
+       return e;\r
+}\r
diff --git a/reactos/tools/rbuild/XML.h b/reactos/tools/rbuild/XML.h
new file mode 100644 (file)
index 0000000..f3cbec8
--- /dev/null
@@ -0,0 +1,83 @@
+// XML.h\r
+\r
+#ifndef __XML_H\r
+#define __XML_H\r
+\r
+#include <string>\r
+#include <vector>\r
+\r
+void\r
+InitWorkingDirectory();\r
+\r
+class Path\r
+{\r
+       std::vector<std::string> path;\r
+public:\r
+       Path(); // initializes path to getcwd();\r
+       Path ( const Path& cwd, const std::string& filename );\r
+       std::string Fixup ( const std::string& filename, bool include_filename ) const;\r
+\r
+       static std::string RelativeFromWorkingDirectory ( const std::string& path );\r
+\r
+       static void Split ( std::vector<std::string>& out,\r
+                           const std::string& path,\r
+                           bool include_last );\r
+};\r
+\r
+class XMLFile\r
+{\r
+       friend class XMLElement;\r
+public:\r
+       XMLFile();\r
+       void close();\r
+       bool open(const std::string& filename);\r
+       void next_token();\r
+       bool next_is_text();\r
+       bool more_tokens();\r
+       bool get_token(std::string& token);\r
+\r
+private:\r
+       std::vector<FILE*> _f;\r
+       std::string _buf;\r
+\r
+       const char *_p, *_end;\r
+};\r
+\r
+\r
+class XMLAttribute\r
+{\r
+public:\r
+       std::string name;\r
+       std::string value;\r
+\r
+       XMLAttribute();\r
+       XMLAttribute ( const std::string& name_, const std::string& value_ );\r
+};\r
+\r
+\r
+class XMLElement\r
+{\r
+public:\r
+       std::string name;\r
+       std::vector<XMLAttribute*> attributes;\r
+       XMLElement* parentElement;\r
+       std::vector<XMLElement*> subElements;\r
+       std::string value;\r
+\r
+       XMLElement();\r
+       ~XMLElement();\r
+       bool Parse(const std::string& token,\r
+                  bool& end_tag);\r
+       void AddSubElement ( XMLElement* e );\r
+       XMLAttribute* GetAttribute ( const std::string& attribute,\r
+                                    bool required);\r
+       const XMLAttribute* GetAttribute ( const std::string& attribute,\r
+                                          bool required) const;\r
+};\r
+\r
+XMLElement*\r
+XMLParse(XMLFile& f,\r
+         const Path& path,\r
+         bool* pend_tag = NULL);\r
+\r
+#endif//__XML_H\r
index ffa8224..1192726 100644 (file)
@@ -4,7 +4,7 @@ TARGET = rbuild$(EXE_POSTFIX)
 \r
 all: $(TARGET)\r
 \r
-BASE_OBJECTS = module.o\r
+BASE_OBJECTS = xml.o module.o\r
 \r
 OBJECTS = $(BASE_OBJECTS) rbuild.o\r
 \r
index 8fe50af..ea087ec 100644 (file)
 #include <stdio.h>\r
 #include <io.h>\r
 #include <assert.h>\r
-#include <direct.h>\r
 #include "rbuild.h"\r
 \r
 using std::string;\r
 using std::vector;\r
 \r
-#ifdef WIN32\r
-#define getcwd _getcwd\r
-#endif//WIN32\r
-string working_directory;\r
-\r
-#ifdef _MSC_VER\r
-unsigned __int64\r
-#else\r
-unsigned long long\r
-#endif\r
-filelen ( FILE* f )\r
-{\r
-#ifdef WIN32\r
-       return _filelengthi64 ( _fileno(f) );\r
-#elif defined(UNIX)\r
-       struct stat64 file_stat;\r
-       if ( fstat64(fileno(f), &file_stat) != 0 )\r
-               return 0;\r
-       return file_stat.st_size;\r
-#endif\r
-}\r
-\r
-static const char* WS = " \t\r\n";\r
-static const char* WSEQ = " =\t\r\n";\r
-\r
-Path::Path()\r
-{\r
-       string s ( working_directory );\r
-       const char* p = strtok ( &s[0], "/\\" );\r
-       while ( p )\r
-       {\r
-               if ( *p )\r
-                       path.push_back ( p );\r
-               p = strtok ( NULL, "/\\" );\r
-       }\r
-}\r
-\r
-Path::Path ( const Path& cwd, const string& file )\r
-{\r
-       string s ( cwd.Fixup ( file, false ) );\r
-       const char* p = strtok ( &s[0], "/\\" );\r
-       while ( p )\r
-       {\r
-               if ( *p )\r
-                       path.push_back ( p );\r
-               p = strtok ( NULL, "/\\" );\r
-       }\r
-}\r
-\r
-string\r
-Path::Fixup ( const string& file, bool include_filename ) const\r
-{\r
-       if ( strchr ( "/\\", file[0] )\r
-#ifdef WIN32\r
-               // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
-               || file[1] == ':'\r
-#endif//WIN32\r
-               )\r
-       {\r
-               return file;\r
-       }\r
-       vector<string> pathtmp ( path );\r
-       string tmp ( file );\r
-       const char* prev = strtok ( &tmp[0], "/\\" );\r
-       const char* p = strtok ( NULL, "/\\" );\r
-       while ( p )\r
-       {\r
-               if ( !strcmp ( prev, "." ) )\r
-                       ; // do nothing\r
-               else if ( !strcmp ( prev, ".." ) )\r
-               {\r
-                       // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
-#ifdef WIN32\r
-                       if ( pathtmp.size() > 1 )\r
-#else\r
-                       if ( pathtmp.size() )\r
-#endif\r
-                               pathtmp.resize ( pathtmp.size() - 1 );\r
-               }\r
-               else\r
-                       pathtmp.push_back ( prev );\r
-               prev = p;\r
-               p = strtok ( NULL, "/\\" );\r
-       }\r
-       if ( include_filename )\r
-               pathtmp.push_back ( prev );\r
-\r
-       // reuse tmp variable to return recombined path\r
-       tmp.resize(0);\r
-       for ( size_t i = 0; i < pathtmp.size(); i++ )\r
-       {\r
-               // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
-#ifdef WIN32\r
-               if ( i ) tmp += "/";\r
-#else\r
-               tmp += "/";\r
-#endif\r
-               tmp += pathtmp[i];\r
-       }\r
-       return tmp;\r
-}\r
-\r
-/*static*/ string\r
-Path::RelativeFromWorkingDirectory ( const string& path )\r
-{\r
-       vector<string> vwork, vpath, vout;\r
-       Path::Split ( vwork, working_directory, true );\r
-       Path::Split ( vpath, path, true );\r
-#ifdef WIN32\r
-       // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
-       // not possible to do relative across different drive letters\r
-       if ( vwork[0] != vpath[0] )\r
-               return path;\r
-#endif\r
-       size_t i = 0;\r
-       while ( i < vwork.size() && i < vpath.size() && vwork[i] == vpath[i] )\r
-               ++i;\r
-       if ( i < vwork.size() )\r
-       {\r
-               // path goes above our working directory, we will need some ..'s\r
-               for ( size_t j = 0; j < i; j++ )\r
-                       vout.push_back ( ".." );\r
-       }\r
-       while ( i < vpath.size() )\r
-               vout.push_back ( vpath[i++] );\r
-\r
-       // now merge vout into a string again\r
-       string out;\r
-       for ( i = 0; i < vout.size(); i++ )\r
-       {\r
-               // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
-#ifdef WIN32\r
-               if ( i ) out += "/";\r
-#else\r
-               out += "/";\r
-#endif\r
-               out += vout[i];\r
-       }\r
-       return out;\r
-}\r
-\r
-/*static*/ void\r
-Path::Split ( vector<string>& out,\r
-              const string& path,\r
-              bool include_last )\r
-{\r
-       string s ( path );\r
-       const char* prev = strtok ( &s[0], "/\\" );\r
-       const char* p = strtok ( NULL, "/\\" );\r
-       out.resize ( 0 );\r
-       while ( p )\r
-       {\r
-               out.push_back ( prev );\r
-               prev = p;\r
-               p = strtok ( NULL, "/\\" );\r
-       }\r
-       if ( include_last )\r
-               out.push_back ( prev );\r
-}\r
-\r
-XMLFile::XMLFile()\r
-{\r
-}\r
-\r
-void\r
-XMLFile::close()\r
-{\r
-       while ( _f.size() )\r
-       {\r
-               fclose ( _f.back() );\r
-               _f.pop_back();\r
-       }\r
-       _buf.resize(0);\r
-       _p = _end = NULL;\r
-}\r
-\r
-bool\r
-XMLFile::open(const string& filename)\r
-{\r
-       close();\r
-       FILE* f = fopen ( filename.c_str(), "rb" );\r
-       if ( !f )\r
-               return false;\r
-       unsigned long len = (unsigned long)filelen(f);\r
-       _buf.resize ( len );\r
-       fread ( &_buf[0], 1, len, f );\r
-       _p = _buf.c_str();\r
-       _end = _p + len;\r
-       _f.push_back ( f );\r
-       next_token();\r
-       return true;\r
-}\r
-\r
-// next_token() moves the pointer to next token, which may be\r
-// an xml element or a text element, basically it's a glorified\r
-// skipspace, normally the user of this class won't need to call\r
-// this function\r
-void\r
-XMLFile::next_token()\r
-{\r
-       _p += strspn ( _p, WS );\r
-}\r
-\r
-bool\r
-XMLFile::next_is_text()\r
-{\r
-       return *_p != '<';\r
-}\r
-\r
-bool\r
-XMLFile::more_tokens()\r
-{\r
-       return _p != _end;\r
-}\r
-\r
-// get_token() is used to return a token, and move the pointer\r
-// past the token\r
-bool\r
-XMLFile::get_token(string& token)\r
-{\r
-       const char* tokend;\r
-       if ( !strncmp ( _p, "<!--", 4 ) )\r
-       {\r
-               tokend = strstr ( _p, "-->" );\r
-               if ( !tokend )\r
-                       tokend = _end;\r
-               else\r
-                       tokend += 3;\r
-       }\r
-       else if ( *_p == '<' )\r
-       {\r
-               tokend = strchr ( _p, '>' );\r
-               if ( !tokend )\r
-                       tokend = _end;\r
-               else\r
-                       ++tokend;\r
-       }\r
-       else\r
-       {\r
-               tokend = strchr ( _p, '<' );\r
-               if ( !tokend )\r
-                       tokend = _end;\r
-               while ( tokend > _p && isspace(tokend[-1]) )\r
-                       --tokend;\r
-       }\r
-       if ( tokend == _p )\r
-               return false;\r
-       token = string ( _p, tokend-_p );\r
-       _p = tokend;\r
-       next_token();\r
-       return true;\r
-}\r
-\r
-XMLAttribute::XMLAttribute()\r
-{\r
-}\r
-\r
-XMLAttribute::XMLAttribute(const string& name_,\r
-                           const string& value_)\r
-       : name(name_), value(value_)\r
-{\r
-}\r
-\r
-XMLElement::XMLElement()\r
-       : parentElement(NULL)\r
-{\r
-}\r
-\r
-XMLElement::~XMLElement()\r
-{\r
-       size_t i;\r
-       for ( i = 0; i < attributes.size(); i++ )\r
-               delete attributes[i];\r
-       for ( i = 0; i < subElements.size(); i++ )\r
-               delete subElements[i];\r
-}\r
-\r
-void\r
-XMLElement::AddSubElement ( XMLElement* e )\r
-{\r
-       subElements.push_back ( e );\r
-       e->parentElement = this;\r
-}\r
-\r
-// Parse()\r
-// This function takes a single xml tag ( i.e. beginning with '<' and\r
-// ending with '>', and parses out it's tag name and constituent\r
-// attributes.\r
-// Return Value: returns true if you need to look for a </tag> for\r
-// the one it just parsed...\r
-bool\r
-XMLElement::Parse(const string& token,\r
-                  bool& end_tag)\r
-{\r
-       const char* p = token.c_str();\r
-       assert ( *p == '<' );\r
-       ++p;\r
-       p += strspn ( p, WS );\r
-\r
-       // check if this is a comment\r
-       if ( !strncmp ( p, "!--", 3 ) )\r
-       {\r
-               name = "!--";\r
-               end_tag = false;\r
-               return false; // never look for end tag to a comment\r
-       }\r
-\r
-       end_tag = ( *p == '/' );\r
-       if ( end_tag )\r
-       {\r
-               ++p;\r
-               p += strspn ( p, WS );\r
-       }\r
-       const char* end = strpbrk ( p, WS );\r
-       if ( !end )\r
-       {\r
-               end = strpbrk ( p, "/>" );\r
-               assert ( end );\r
-       }\r
-       name = string ( p, end-p );\r
-       p = end;\r
-       p += strspn ( p, WS );\r
-       while ( *p != '>' && *p != '/' )\r
-       {\r
-               end = strpbrk ( p, WSEQ );\r
-               if ( !end )\r
-               {\r
-                       end = strpbrk ( p, "/>" );\r
-                       assert ( end );\r
-               }\r
-               string attribute ( p, end-p ), value;\r
-               p = end;\r
-               p += strspn ( p, WS );\r
-               if ( *p == '=' )\r
-               {\r
-                       ++p;\r
-                       p += strspn ( p, WS );\r
-                       char quote = 0;\r
-                       if ( strchr ( "\"'", *p ) )\r
-                       {\r
-                               quote = *p++;\r
-                               end = strchr ( p, quote );\r
-                       }\r
-                       else\r
-                       {\r
-                               end = strpbrk ( p, WS );\r
-                       }\r
-                       if ( !end )\r
-                       {\r
-                               end = strchr ( p, '>' );\r
-                               assert(end);\r
-                               if ( end[-1] == '/' )\r
-                                       end--;\r
-                       }\r
-                       value = string ( p, end-p );\r
-                       p = end;\r
-                       if ( quote && *p == quote )\r
-                               p++;\r
-                       p += strspn ( p, WS );\r
-               }\r
-               attributes.push_back ( new XMLAttribute ( attribute, value ) );\r
-       }\r
-       return !( *p == '/' ) && !end_tag;\r
-}\r
-\r
-XMLAttribute*\r
-XMLElement::GetAttribute ( const string& attribute,\r
-                           bool required )\r
-{\r
-       // this would be faster with a tree-based container, but our attribute\r
-       // lists are likely to stay so short as to not be an issue.\r
-       for ( size_t i = 0; i < attributes.size(); i++ )\r
-       {\r
-               if ( attribute == attributes[i]->name )\r
-                       return attributes[i];\r
-       }\r
-       if ( required )\r
-       {\r
-               printf ( "syntax error: attribute '%s' required for <%s>\n",\r
-                       attribute.c_str(), name.c_str() );\r
-       }\r
-       return NULL;\r
-}\r
-\r
-const XMLAttribute*\r
-XMLElement::GetAttribute ( const string& attribute,\r
-                           bool required ) const\r
-{\r
-       // this would be faster with a tree-based container, but our attribute\r
-       // lists are likely to stay so short as to not be an issue.\r
-       for ( size_t i = 0; i < attributes.size(); i++ )\r
-       {\r
-               if ( attribute == attributes[i]->name )\r
-                       return attributes[i];\r
-       }\r
-       if ( required )\r
-       {\r
-               printf ( "syntax error: attribute '%s' required for <%s>\n",\r
-                       attribute.c_str(), name.c_str() );\r
-       }\r
-       return NULL;\r
-}\r
-\r
-// XMLParse()\r
-// This function reads a "token" from the file loaded in XMLFile\r
-// REM TODO FIXME: At the moment it can't handle comments or non-xml tags.\r
-// if it finds a tag that is non-singular, it parses sub-elements and/or\r
-// inner text into the XMLElement that it is building to return.\r
-// Return Value: an XMLElement allocated via the new operator that contains\r
-// it's parsed data. Keep calling this function until it returns NULL\r
-// (no more data)\r
-XMLElement*\r
-XMLParse(XMLFile& f,\r
-         const Path& path,\r
-         bool* pend_tag = NULL)\r
-{\r
-       string token;\r
-       if ( !f.get_token(token) )\r
-               return NULL;\r
-       bool end_tag;\r
-\r
-       while ( token[0] != '<' )\r
-       {\r
-               printf ( "syntax error: expecting xml tag, not '%s'\n", token.c_str() );\r
-               if ( !f.get_token(token) )\r
-                       return NULL;\r
-       }\r
-\r
-       XMLElement* e = new XMLElement;\r
-       bool bNeedEnd = e->Parse ( token, end_tag );\r
-\r
-       if ( e->name == "xi:include" )\r
-       {\r
-               XMLAttribute* att;\r
-               att = e->GetAttribute("href",true);\r
-               if ( att )\r
-               {\r
-                       string file ( path.Fixup(att->value,true) );\r
-                       string top_file ( Path::RelativeFromWorkingDirectory ( file ) );\r
-                       e->attributes.push_back ( new XMLAttribute ( "top_href", top_file ) );\r
-                       XMLFile fInc;\r
-                       if ( !fInc.open ( file ) )\r
-                               printf ( "xi:include error, couldn't find file '%s'\n", file.c_str() );\r
-                       else\r
-                       {\r
-                               Path path2 ( path, att->value );\r
-                               for ( ;; )\r
-                               {\r
-                                       XMLElement* e2 = XMLParse ( fInc, path2 );\r
-                                       if ( !e2 )\r
-                                               break;\r
-                                       e->AddSubElement ( e2 );\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-\r
-       if ( !bNeedEnd )\r
-       {\r
-               if ( pend_tag )\r
-                       *pend_tag = end_tag;\r
-               else if ( end_tag )\r
-               {\r
-                       delete e;\r
-                       printf ( "syntax error: end tag '%s' not expected\n", token.c_str() );\r
-                       return NULL;\r
-               }\r
-               return e;\r
-       }\r
-       bool bThisMixingErrorReported = false;\r
-       while ( f.more_tokens() )\r
-       {\r
-               if ( f.next_is_text() )\r
-               {\r
-                       if ( !f.get_token ( token ) || !token.size() )\r
-                       {\r
-                               printf ( "internal tool error - get_token() failed when more_tokens() returned true\n" );\r
-                               break;\r
-                       }\r
-                       if ( e->subElements.size() && !bThisMixingErrorReported )\r
-                       {\r
-                               printf ( "syntax error: mixing of inner text with sub elements\n" );\r
-                               bThisMixingErrorReported = true;\r
-                       }\r
-                       if ( e->value.size() )\r
-                       {\r
-                               printf ( "syntax error: multiple instances of inner text\n" );\r
-                               e->value += " " + token;\r
-                       }\r
-                       else\r
-                               e->value = token;\r
-               }\r
-               else\r
-               {\r
-                       XMLElement* e2 = XMLParse ( f, path, &end_tag );\r
-                       if ( end_tag )\r
-                       {\r
-                               if ( e->name != e2->name )\r
-                                       printf ( "end tag name mismatch\n" );\r
-                               delete e2;\r
-                               break;\r
-                       }\r
-                       if ( e->value.size() && !bThisMixingErrorReported )\r
-                       {\r
-                               printf ( "syntax error: mixing of inner text with sub elements\n" );\r
-                               bThisMixingErrorReported = true;\r
-                       }\r
-                       e->AddSubElement ( e2 );\r
-               }\r
-       }\r
-       return e;\r
-}\r
-\r
 Project::~Project()\r
 {\r
        for ( size_t i = 0; i < modules.size(); i++ )\r
@@ -570,11 +56,7 @@ Project::ProcessXML ( const XMLElement& e, const string& path )
 int\r
 main ( int argc, char** argv )\r
 {\r
-       // store the current directory for path calculations\r
-       working_directory.resize ( _MAX_PATH );\r
-       working_directory[0] = 0;\r
-       getcwd ( &working_directory[0], working_directory.size() );\r
-       working_directory.resize ( strlen ( working_directory.c_str() ) );\r
+       InitWorkingDirectory();\r
 \r
        XMLFile f;\r
        Path path;\r
index b99b6be..d50dcaa 100644 (file)
@@ -1,75 +1,11 @@
 #ifndef __RBUILD_H\r
 #define __RBUILD_H\r
 \r
+#include "XML.h"\r
+\r
 #include <string>\r
 #include <vector>\r
 \r
-class Path\r
-{\r
-       std::vector<std::string> path;\r
-public:\r
-       Path(); // initializes path to getcwd();\r
-       Path ( const Path& cwd, const std::string& filename );\r
-       std::string Fixup ( const std::string& filename, bool include_filename ) const;\r
-\r
-       static std::string RelativeFromWorkingDirectory ( const std::string& path );\r
-\r
-       static void Split ( std::vector<std::string>& out,\r
-                           const std::string& path,\r
-                           bool include_last );\r
-};\r
-\r
-class XMLFile\r
-{\r
-       friend class XMLElement;\r
-public:\r
-       XMLFile();\r
-       void close();\r
-       bool open(const std::string& filename);\r
-       void next_token();\r
-       bool next_is_text();\r
-       bool more_tokens();\r
-       bool get_token(std::string& token);\r
-\r
-private:\r
-       std::vector<FILE*> _f;\r
-       std::string _buf;\r
-\r
-       const char *_p, *_end;\r
-};\r
-\r
-\r
-class XMLAttribute\r
-{\r
-public:\r
-       std::string name;\r
-       std::string value;\r
-\r
-       XMLAttribute();\r
-       XMLAttribute ( const std::string& name_, const std::string& value_ );\r
-};\r
-\r
-\r
-class XMLElement\r
-{\r
-public:\r
-       std::string name;\r
-       std::vector<XMLAttribute*> attributes;\r
-       XMLElement* parentElement;\r
-       std::vector<XMLElement*> subElements;\r
-       std::string value;\r
-\r
-       XMLElement();\r
-       ~XMLElement();\r
-       bool Parse(const std::string& token,\r
-                  bool& end_tag);\r
-       void AddSubElement ( XMLElement* e );\r
-       XMLAttribute* GetAttribute ( const std::string& attribute,\r
-                                    bool required);\r
-       const XMLAttribute* GetAttribute ( const std::string& attribute,\r
-                                          bool required) const;\r
-};\r
-\r
 class Project;\r
 class Module;\r
 class File;\r
index ce3032c..4a3287b 100644 (file)
@@ -81,7 +81,7 @@ void BaseTest::Fail()
        Failed = true;\r
 }\r
 \r
-class BaseTestList : public vector<BaseTest*>\r
+class BaseTestList : public std::vector<BaseTest*>\r
 {\r
 public:\r
        ~BaseTestList()\r