Exception handling.

svn path=/branches/xmlbuildsystem/; revision=12797
This commit is contained in:
Casper Hornstrup
2005-01-04 20:37:48 +00:00
parent 6332037cc9
commit 8b3d554adb
6 changed files with 148 additions and 38 deletions
+50
View File
@@ -0,0 +1,50 @@
#ifdef _MSC_VER
#pragma warning ( disable : 4786 ) // identifier was truncated to '255' characters in the debug information
#endif//_MSC_VER
#include <stdarg.h>
#include "rbuild.h"
Exception::Exception()
{
}
Exception::Exception(string message)
{
Message = message;
}
Exception::Exception(const char* format,
...)
{
va_list args;
va_start(args,
format);
Message = ssvprintf(format,
args);
va_end(args);
}
void Exception::SetMessage(const char* message,
va_list args)
{
Message = ssvprintf(message,
args);
}
FileNotFoundException::FileNotFoundException(string filename)
: Exception ( "File '%s' not found.", filename.c_str() )
{
Filename = filename;
}
InvalidBuildFileException::InvalidBuildFileException(const char* message,
...)
{
va_list args;
va_start( args, message);
SetMessage(message, args);
va_end(args);
}
+37
View File
@@ -0,0 +1,37 @@
#ifndef __EXCEPTION_H
#define __EXCEPTION_H
#include <string>
using std::string;
class Exception
{
public:
Exception(string message);
Exception(const char* format,
...);
string Message;
protected:
Exception();
void SetMessage(const char* message,
va_list args);
};
class FileNotFoundException : public Exception
{
public:
FileNotFoundException(string filename);
string Filename;
};
class InvalidBuildFileException : public Exception
{
public:
InvalidBuildFileException(const char* message,
...);
};
#endif /* __EXCEPTION_H */
+6 -1
View File
@@ -4,7 +4,12 @@ TARGET = rbuild$(EXE_POSTFIX)
all: $(TARGET)
BASE_OBJECTS = xml.o project.o module.o
BASE_OBJECTS = \
exception.o \
module.o \
project.o \
ssprintf.o \
xml.o
OBJECTS = $(BASE_OBJECTS) rbuild.o
+32
View File
@@ -7,12 +7,44 @@
using std::string;
using std::vector;
Project::Project(string filename)
{
if ( !xmlfile.open ( filename ) )
throw FileNotFoundException ( filename );
ReadXml();
}
Project::~Project()
{
for ( size_t i = 0; i < modules.size(); i++ )
delete modules[i];
}
void Project::ReadXml()
{
Path path;
bool projectFound = false;
do
{
XMLElement* head = XMLParse ( xmlfile, path );
if ( !head )
throw InvalidBuildFileException ( "Document contains no 'project' tag." );
if ( head->name == "!--" )
continue; // ignore comments
if ( head->name != "project" )
{
throw InvalidBuildFileException ( "Expected 'project', got '%s'.",
head->name.c_str());
}
this->ProcessXML ( *head, "." );
delete head;
projectFound = true;
} while (!projectFound);
}
void
Project::ProcessXML ( const XMLElement& e, const string& path )
{
+15 -34
View File
@@ -17,40 +17,17 @@ main ( int argc, char** argv )
{
InitWorkingDirectory();
XMLFile f;
Path path;
string xml_file ( "ReactOS.xml" );
if ( !f.open ( xml_file ) )
try
{
printf ( "couldn't open ReactOS.xml!\n" );
return -1;
}
vector<string> xml_dependencies;
xml_dependencies.push_back ( xml_file );
for ( ;; )
{
XMLElement* head = XMLParse ( f, path );
if ( !head )
break; // end of file
if ( head->name == "!--" )
continue; // ignore comments
if ( head->name != "project" )
{
printf ( "error: expecting 'project', got '%s'\n", head->name.c_str() );
continue;
}
Project* proj = new Project;
proj->ProcessXML ( *head, "." );
string projectFilename ( "ReactOS.xml" );
Project* project = new Project( projectFilename );
Path path;
// REM TODO FIXME actually do something with Project object...
printf ( "Found %d modules:\n", proj->modules.size() );
for ( size_t i = 0; i < proj->modules.size(); i++ )
printf ( "Found %d modules:\n", project->modules.size() );
for ( size_t i = 0; i < project->modules.size(); i++ )
{
Module& m = *proj->modules[i];
Module& m = *project->modules[i];
printf ( "\t%s in folder: %s\n",
m.name.c_str(),
m.path.c_str() );
@@ -74,10 +51,14 @@ main ( int argc, char** argv )
printf ( "\t\t%s\n", m.files[j]->name.c_str() );
}
}
delete project;
delete proj;
delete head;
return 0;
}
catch (Exception& ex)
{
printf ( ex.Message.c_str() );
return 1;
}
return 0;
}
+8 -3
View File
@@ -1,10 +1,11 @@
#ifndef __RBUILD_H
#define __RBUILD_H
#include "XML.h"
#include <string>
#include <vector>
#include "ssprintf.h"
#include "exception.h"
#include "XML.h"
class Project;
class Module;
@@ -16,8 +17,12 @@ public:
std::string name;
std::vector<Module*> modules;
~Project();
Project ( string filename );
~Project ();
void ProcessXML ( const XMLElement& e, const std::string& path );
private:
void ReadXml ();
XMLFile xmlfile;
};
class Module