39#include <boost/algorithm/string.hpp>
40#include <boost/core/ignore_unused.hpp>
53 TiXmlElement *file = metric->FirstChildElement(
"file");
54 ASSERTL0(file,
"Missing file tag for file metric!");
58 std::string filename, sha1hash;
60 if (file->Attribute(
"filename"))
62 filename = file->Attribute(
"filename");
66 ASSERTL0(
false,
"Missing filename for file tag!");
71 TiXmlElement *
sha1 = file->FirstChildElement(
"sha1");
72 ASSERTL0(
sha1,
"Missing SHA1 hash for file " + filename);
73 sha1hash =
sha1->GetText();
74 ASSERTL0(sha1hash.size() == 40,
"Incorrect length for SHA1 hash");
79 file = file->NextSiblingElement(
"file");
85 int fdot = pfilename.find_last_of(
'.');
86 string ending = pfilename.substr(fdot);
89 if (ending ==
".fld" || ending ==
".chk" || ending ==
".rst")
91 TiXmlDocument *xmlFldFile;
92 fs::path pathfilename(pfilename);
94 if (fs::is_directory(pathfilename))
96 std::vector<fs::path> dirfiles;
99 copy(fs::directory_iterator(pathfilename), fs::directory_iterator(),
100 back_inserter(dirfiles));
102 xmlFldFile =
new TiXmlDocument;
105 for (
int i = 0; i < dirfiles.size(); ++i)
108 std::ifstream file(infile.c_str());
109 ASSERTL0(file.good(),
"Unable to open file: " + infile);
110 file >> (*xmlFldFile);
115 xmlFldFile =
new TiXmlDocument(pfilename);
116 xmlFldFile->LoadFile(pfilename);
120 TiXmlElement *vNektar = xmlFldFile->FirstChildElement(
"NEKTAR");
124 TiXmlNode *vMetaData = vNektar->FirstChild(
"Metadata");
129 vNektar->RemoveChild(vMetaData);
131 vNektar = vNektar->NextSiblingElement(
"NEKTAR");
134 filename = pfilename +
".tmp";
135 xmlFldFile->SaveFile(filename);
139 filename = pfilename;
143 std::ifstream testFile(filename.c_str(), std::ios::binary);
144 ASSERTL0(testFile.is_open(),
"Error opening file " + filename);
147 std::vector<char> fileContents((std::istreambuf_iterator<char>(testFile)),
148 std::istreambuf_iterator<char>());
151 unsigned char hash[20];
154 sha1::calc((
void *)&fileContents[0], fileContents.size(), hash);
160 return std::string(strhash);
165 boost::ignore_unused(pStdout, pStderr);
167 std::map<std::string, std::string>::iterator it;
173 if (!boost::iequals(filehash, it->second))
175 std::cerr <<
"Failed SHA1 hash test." << std::endl;
176 std::cerr <<
" Expected: " << it->second << std::endl;
177 std::cerr <<
" Result: " << filehash << std::endl;
187 boost::ignore_unused(pStdout, pStderr);
189 std::map<std::string, std::string>::iterator it;
199 TiXmlElement *file =
m_metric->FirstChildElement(
"file");
202 std::string filename = file->Attribute(
"filename");
205 TiXmlElement *
sha1 =
new TiXmlElement(
"sha1");
209 " in list of calculated"
213 file->LinkEndChild(
sha1);
214 file = file->NextSiblingElement(
"file");
#define ASSERTL0(condition, msg)
std::string RegisterCreatorFunction(std::string key, CreatorFunction func)
std::string CalculateHash(std::string filename)
virtual bool v_Test(std::istream &pStdout, std::istream &pStderr)
Virtual function to test the metric. Should be redefined in derived classes.
MetricFile(TiXmlElement *metric, bool generate)
std::map< std::string, std::string > m_filehash
Stores filenames to perform hash on.
static MetricSharedPtr create(TiXmlElement *metric, bool generate)
virtual void v_Generate(std::istream &pStdout, std::istream &pStderr)
Virtual function to generate the metric. Should be redefined in derived classes.
Base class for all metrics. Metric represents a test metric that can be used to evaluate the function...
TiXmlElement * m_metric
Pointer to XML structure containing metric definition.
bool m_generate
Determines whether to generate this metric or not.
std::string PortablePath(const boost::filesystem::path &path)
create portable path on different platforms for boost::filesystem path
The above copyright notice and this permission notice shall be included.
MetricFactory & GetMetricFactory()
void toHexString(const unsigned char *hash, char *hexstring)
Calculate a string which represents the SHA1 hash as a hexadecimal number.
void calc(const void *src, const int bytelength, unsigned char *hash)
Calculate the SHA1 hash of some data set.