added support for dump files in createDefaultKeywordList.cpp

This commit is contained in:
atleh
2014-04-01 08:52:07 +02:00
parent 47713c7a00
commit a6cd64afb7

View File

@@ -2,6 +2,8 @@
#include <stdio.h>
#include <fstream>
#include <string.h>
// http://www.ridgesolutions.ie/index.php/2013/05/30/boost-link-error-undefined-reference-to-boostfilesystemdetailcopy_file/
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#include <opm/parser/eclipse/Parser/ParserItem.hpp>
@@ -13,6 +15,7 @@
#include <opm/parser/eclipse/Parser/Parser.hpp>
using namespace Opm;
using namespace boost::filesystem;
typedef void (*keywordGenerator)(std::ofstream& of, const std::string keywordName, Json::JsonObject* jsonKeyword);
@@ -82,26 +85,99 @@ void scanAllKeywords(const boost::filesystem::path& directory , std::ofstream& o
}
}
bool areFilesEqual (path file1, path file2) {
const int BUFFER_SIZE = 1000*1024;
std::ifstream file1stream(file1.c_str(), std::ifstream::in | std::ifstream::binary);
std::ifstream file2stream(file2.c_str(), std::ifstream::in | std::ifstream::binary);
if(!file1stream.is_open() || !file2stream.is_open())
{
return false;
}
char *buffer1 = new char[BUFFER_SIZE]();
char *buffer2 = new char[BUFFER_SIZE]();
do {
file1stream.read(buffer1, BUFFER_SIZE);
file2stream.read(buffer2, BUFFER_SIZE);
if (std::memcmp(buffer1, buffer2, BUFFER_SIZE) != 0)
{
delete[] buffer1;
delete[] buffer2;
return false;
}
} while (file1stream.good() || file2stream.good());
delete[] buffer1;
delete[] buffer2;
return true;
}
path generateDumpFile(boost::filesystem::path keywordPath)
{
path root = unique_path("/tmp/%%%%-%%%%");
create_directories(root);
path temp_dump_file = root / "opm_createDefaultKeywordList_dumpfile";
std::string temp_dump_file_name = temp_dump_file.string().c_str();
std::ofstream temp_dump_file_stream( temp_dump_file_name );
scanAllKeywords( keywordPath , temp_dump_file_stream, &generateDumpForKeyword );
temp_dump_file_stream.close();
return temp_dump_file;
}
void generateSourceFile(const char* source_file_name, boost::filesystem::path keywordPath)
{
std::ofstream source_file_stream( source_file_name );
createHeader(source_file_stream);
startFunction(source_file_stream);
scanAllKeywords( keywordPath , source_file_stream, &generateSourceForKeyword );
endFunction(source_file_stream);
source_file_stream << "}" << std::endl;
source_file_stream.close();
}
void printUsage() {
std::cout << "Generates source code for populating the parser's list of known keywords." << std::endl;
std::cout << "Usage: createDefaultKeywordList <configroot> <sourcefilename> [<dumpfilename>]" << std::endl;
std::cout << " <configroot>: Path to keyword (JSON) files" << std::endl;
std::cout << " <sourcefilename>: Path to source file to generate" << std::endl;
std::cout << " <dumpfilename>: Path to dump file containing state of keyword list at" << std::endl;
std::cout << " last build (used for build triggering)." << std::endl;
}
int main(int argc , char ** argv) {
const char * config_root = argv[1];
const char * source_file_name = argv[2];
const char * dump_file_name = argv[3];
boost::filesystem::path directory(config_root);
if (dump_file_name) {
std::ofstream dump_file_stream( dump_file_name );
scanAllKeywords( directory , dump_file_stream, &generateDumpForKeyword );
dump_file_stream.close();
if (!argv[1]) {
printUsage();
return 0;
}
std::ofstream source_file_stream( source_file_name );
createHeader(source_file_stream);
startFunction(source_file_stream);
scanAllKeywords( directory , source_file_stream, &generateSourceForKeyword );
endFunction(source_file_stream);
source_file_stream << "}" << std::endl;
source_file_stream.close();
boost::filesystem::path keywordPath(config_root);
if (dump_file_name) {
path temp_dump_file = generateDumpFile(keywordPath);
path dump_file_reference = dump_file_name;
bool areEqual = areFilesEqual(dump_file_reference, temp_dump_file);
copy_file(temp_dump_file,dump_file_reference,copy_option::overwrite_if_exists);
if (areEqual) {
std::cout << "No keyword changes detected - quitting" << std::endl;
return 1;
}
else {
std::cout << "Keyword changes detected - generating keywords" << std::endl;
}
}
generateSourceFile(source_file_name, keywordPath);
}