39#include <boost/algorithm/string.hpp>
40#include <boost/core/ignore_unused.hpp>
55 TiXmlElement *file = metric->FirstChildElement(
"file");
56 ASSERTL0(file,
"Missing file tag for file metric!");
60 std::string filename, sha1hash;
62 if (file->Attribute(
"filename"))
64 filename = file->Attribute(
"filename");
68 ASSERTL0(
false,
"Missing filename for file tag!");
73 TiXmlElement *
sha1 = file->FirstChildElement(
"sha1");
74 ASSERTL0(
sha1,
"Missing SHA1 hash for file " + filename);
75 sha1hash =
sha1->GetText();
76 ASSERTL0(sha1hash.size() == 40,
"Incorrect length for SHA1 hash");
81 file = file->NextSiblingElement(
"file");
87 int fdot = pfilename.find_last_of(
'.');
88 string ending = pfilename.substr(fdot);
91 if (ending ==
".fld" || ending ==
".chk" || ending ==
".rst")
93 TiXmlDocument *xmlFldFile;
94 fs::path pathfilename(pfilename);
96 if (fs::is_directory(pathfilename))
98 std::vector<fs::path> dirfiles;
101 copy(fs::directory_iterator(pathfilename), fs::directory_iterator(),
102 back_inserter(dirfiles));
104 xmlFldFile =
new TiXmlDocument;
107 for (
int i = 0; i < dirfiles.size(); ++i)
110 std::ifstream file(infile.c_str());
111 ASSERTL0(file.good(),
"Unable to open file: " + infile);
112 file >> (*xmlFldFile);
117 xmlFldFile =
new TiXmlDocument(pfilename);
118 xmlFldFile->LoadFile(pfilename);
122 TiXmlElement *vNektar = xmlFldFile->FirstChildElement(
"NEKTAR");
126 TiXmlNode *vMetaData = vNektar->FirstChild(
"Metadata");
131 vNektar->RemoveChild(vMetaData);
133 vNektar = vNektar->NextSiblingElement(
"NEKTAR");
136 filename = pfilename +
".tmp";
137 xmlFldFile->SaveFile(filename);
141 filename = pfilename;
145 std::ifstream testFile(filename.c_str(), std::ios::binary);
146 ASSERTL0(testFile.is_open(),
"Error opening file " + filename);
149 std::vector<char> fileContents((std::istreambuf_iterator<char>(testFile)),
150 std::istreambuf_iterator<char>());
153 unsigned char hash[20];
156 sha1::calc((
void *)&fileContents[0], fileContents.size(), hash);
162 return std::string(strhash);
167 boost::ignore_unused(pStdout, pStderr);
169 std::map<std::string, std::string>::iterator it;
175 if (!boost::iequals(filehash, it->second))
177 std::cerr <<
"Failed SHA1 hash test." << std::endl;
178 std::cerr <<
" Expected: " << it->second << std::endl;
179 std::cerr <<
" Result: " << filehash << std::endl;
189 boost::ignore_unused(pStdout, pStderr);
191 std::map<std::string, std::string>::iterator it;
201 TiXmlElement *file =
m_metric->FirstChildElement(
"file");
204 std::string filename = file->Attribute(
"filename");
207 TiXmlElement *
sha1 =
new TiXmlElement(
"sha1");
211 " in list of calculated"
215 file->LinkEndChild(
sha1);
216 file = file->NextSiblingElement(
"file");
#define ASSERTL0(condition, msg)
std::string RegisterCreatorFunction(std::string key, CreatorFunction func)
std::string CalculateHash(std::string filename)
MetricFile(TiXmlElement *metric, bool generate)
std::map< std::string, std::string > m_filehash
Stores filenames to perform hash on.
bool v_Test(std::istream &pStdout, std::istream &pStderr) override
Virtual function to test the metric. Should be redefined in derived classes.
static MetricSharedPtr create(TiXmlElement *metric, bool generate)
void v_Generate(std::istream &pStdout, std::istream &pStderr) override
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.
static std::string PortablePath(const fs::path &path)
create portable path on different platforms for std::filesystem path.
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.