added exceptions to meminfomanager

develop
mizipzor 2010-02-28 21:44:14 +01:00
parent c13e37adf6
commit 160992ec9c
2 changed files with 148 additions and 69 deletions

@ -108,7 +108,96 @@ namespace DFHack
virtual const char* what() const throw()
{
return "memory definition missing";
std::stringstream s;
s << "memory definition missing: type " << type << " key " << key;
return s.str().c_str();
}
};
// Syntax errors and whatnot, the xml cant be read
class DFHACK_EXPORT MemoryXmlParse : public std::exception
{
public:
MemoryXmlParse(const char* _desc, int _id, int _row, int _col) :
desc(_desc), id(_id), row(_row), col(_col) {}
const std::string desc;
const int id;
const int row;
const int col;
virtual const char* what() const throw()
{
std::stringstream s;
s << "error " << id << ": " << desc << ", at row " << row << " col " << col;
return s.str().c_str();
}
};
class DFHACK_EXPORT MemoryXmlBadAttribute : public std::exception
{
public:
MemoryXmlBadAttribute(const char* _attr) : attr(_attr) {}
std::string attr;
virtual const char* what() const throw()
{
std::stringstream s;
s << "attribute is either missing or invalid: " << attr;
return s.str().c_str();
}
};
class DFHACK_EXPORT MemoryXmlNoRoot : public std::exception
{
public:
MemoryXmlNoRoot() {}
virtual const char* what() const throw()
{
return "no pElem found";
}
};
class DFHACK_EXPORT MemoryXmlNoDFExtractor : public std::exception
{
public:
MemoryXmlNoDFExtractor(const char* _name) : name(_name) {}
std::string name;
virtual const char* what() const throw()
{
std::stringstream s;
s << "DFExtractor != " << name;
return s.str().c_str();
}
};
class DFHACK_EXPORT MemoryXmlUnderspecifiedEntry : public std::exception
{
public:
MemoryXmlUnderspecifiedEntry() {}
virtual const char* what() const throw()
{
return "underspecified MemInfo entry, each entry needs to set both the name attribute and have a value";
}
};
class DFHACK_EXPORT MemoryXmlUnknownType : public std::exception
{
public:
MemoryXmlUnknownType(const char* _type) : type(_type) {}
std::string type;
virtual const char* what() const throw()
{
std::stringstream s;
s << "unknown MemInfo type: " << type;
return s.str().c_str();
}
};
}

@ -102,13 +102,11 @@ void MemInfoManager::ParseEntry (TiXmlElement* entry, memory_info* mem, map <str
ParseEntry(knownEntries[base], mem, knownEntries);
}
// mandatory attributes missing?
if(!(cstr_version && cstr_os))
{
cerr << "Bad entry in memory.xml detected, version or os attribute is missing.";
// skip if we don't have valid attributes
return;
}
if (!cstr_version)
throw Error::MemoryXmlBadAttribute("version");
if (!cstr_os)
throw Error::MemoryXmlBadAttribute("os");
string os = cstr_os;
mem->setVersion(cstr_version);
mem->setOS(cstr_os);
@ -139,8 +137,7 @@ void MemInfoManager::ParseEntry (TiXmlElement* entry, memory_info* mem, map <str
}
else
{
cerr << "unknown operating system " << os << endl;
return;
throw Error::MemoryXmlBadAttribute("os");
}
// process additional entries
@ -162,8 +159,7 @@ void MemInfoManager::ParseEntry (TiXmlElement* entry, memory_info* mem, map <str
}
if(!(cstr_name && cstr_value))
{
cerr << "underspecified MemInfo entry" << endl;
continue;
throw Error::MemoryXmlUnderspecifiedEntry();
}
name = cstr_name;
value = cstr_value;
@ -205,7 +201,7 @@ void MemInfoManager::ParseEntry (TiXmlElement* entry, memory_info* mem, map <str
}
else
{
cerr << "Unknown MemInfo type: " << type << endl;
throw Error::MemoryXmlUnknownType(type.c_str());
}
} // for
} // method
@ -220,30 +216,32 @@ MemInfoManager::MemInfoManager(string path_to_xml)
bool MemInfoManager::loadFile(string path_to_xml)
{
TiXmlDocument doc( path_to_xml.c_str() );
bool loadOkay = doc.LoadFile();
//bool loadOkay = doc.LoadFile();
if (!doc.LoadFile())
{
error = true;
throw Error::MemoryXmlParse(doc.ErrorDesc(), doc.ErrorId(), doc.ErrorRow(), doc.ErrorCol());
}
TiXmlHandle hDoc(&doc);
TiXmlElement* pElem;
TiXmlHandle hRoot(0);
memory_info mem;
if ( loadOkay )
{
// block: name
{
pElem=hDoc.FirstChildElement().Element();
// should always have a valid root but handle gracefully if it does
if (!pElem)
{
cerr << "no pElem found" << endl;
return false;
error = true;
throw Error::MemoryXmlNoRoot();
}
string m_name=pElem->Value();
if(m_name != "DFExtractor")
{
cerr << "DFExtractor != " << m_name << endl;
return false;
error = true;
throw Error::MemoryXmlNoDFExtractor(m_name.c_str());
}
//cout << "got DFExtractor XML!" << endl;
// save this for later
hRoot=TiXmlHandle(pElem);
}
@ -280,11 +278,3 @@ bool MemInfoManager::loadFile(string path_to_xml)
error = false;
return true;
}
else
{
// load failed
cerr << "Can't load memory offsets from memory.xml" << endl;
error = true;
return false;
}
}