From 127db48a266e9fbab925fe64788bf8319fe57468 Mon Sep 17 00:00:00 2001 From: Wolfgang Thaller Date: Thu, 9 Oct 2014 22:15:13 +0200 Subject: [PATCH] Rez: command line options --- Rez/CMakeLists.txt | 2 +- Rez/ResourceCompiler.cc | 21 ++++++---- Rez/ResourceCompiler.h | 3 +- Rez/Rez.cc | 92 ++++++++++++++++++++++++++++++++++------- Rez/RezLexer.cc | 1 - Rez/RezParser.yy | 4 +- Rez/RezWorld.cc | 6 ++- Rez/RezWorld.h | 2 + Rez/Test.r | 11 +++-- 9 files changed, 108 insertions(+), 34 deletions(-) diff --git a/Rez/CMakeLists.txt b/Rez/CMakeLists.txt index 2e7059a25e..cd200af5fb 100644 --- a/Rez/CMakeLists.txt +++ b/Rez/CMakeLists.txt @@ -19,7 +19,7 @@ cmake_minimum_required(VERSION 2.8) set(CMAKE_CXX_FLAGS "--std=c++11") -find_package(Boost COMPONENTS wave filesystem system thread regex) +find_package(Boost COMPONENTS wave filesystem system thread regex program_options) find_package(BISON REQUIRED) diff --git a/Rez/ResourceCompiler.cc b/Rez/ResourceCompiler.cc index bfb73cfd53..b22419b727 100644 --- a/Rez/ResourceCompiler.cc +++ b/Rez/ResourceCompiler.cc @@ -2,8 +2,13 @@ #include #include "ResourceDefinitions.h" -ResourceCompiler::ResourceCompiler(TypeDefinitionPtr type, CompoundExprPtr body) - : typeDefinition(type), body(body), currentOffset(0), currentField(nullptr) +ResourceCompiler::ResourceCompiler( + TypeDefinitionPtr type, CompoundExprPtr body, bool verboseFlag) + : typeDefinition(type), + body(body), + currentOffset(0), + currentField(nullptr), + verboseFlag(verboseFlag) { } @@ -15,7 +20,8 @@ std::string ResourceCompiler::resourceData() void ResourceCompiler::write(int nBits, int value) { - std::cout << "[" << nBits << " bits] = " << std::hex << value << std::dec << std::endl; + if(verboseFlag) + std::cout << "[" << nBits << " bits] = " << std::hex << value << std::dec << std::endl; unsigned mask = 1 << (nBits-1); @@ -41,7 +47,6 @@ ExprPtr ResourceCompiler::lookupIdentifier(std::string name, const Subscripts &s { if(ExprPtr val = currentField->lookupNamedValue(name)) { - std::cout << "current field: " << name << " found."; return val; } } @@ -50,7 +55,7 @@ ExprPtr ResourceCompiler::lookupIdentifier(std::string name, const Subscripts &s if(p != labelValues.end()) return p->second; - std::cout << "ID lookup failed: " << name << std::endl; + std::cerr << "ID lookup failed: " << name << std::endl; return nullptr; } @@ -62,15 +67,15 @@ void ResourceCompiler::defineLabel(const std::string &name) void ResourceCompiler::compile() { - std::cout << "(first pass)\n"; + if(verboseFlag) std::cout << "(first pass)\n"; currentOffset = 0; data.clear(); typeDefinition->compile(body, this, true); - std::cout << "(second pass)\n"; + if(verboseFlag) std::cout << "(second pass)\n"; currentOffset = 0; data.clear(); typeDefinition->compile(body, this, false); - std::cout << "(done)\n"; + if(verboseFlag) std::cout << "(done)\n"; } diff --git a/Rez/ResourceCompiler.h b/Rez/ResourceCompiler.h index bafb10e5ce..2ae56d4c58 100644 --- a/Rez/ResourceCompiler.h +++ b/Rez/ResourceCompiler.h @@ -32,10 +32,11 @@ class ResourceCompiler Subscripts currentSubscripts; std::vector data; + bool verboseFlag; void beginArrayScope(std::string& arrayName, int index); public: - ResourceCompiler(TypeDefinitionPtr type, CompoundExprPtr body); + ResourceCompiler(TypeDefinitionPtr type, CompoundExprPtr body, bool verboseFlag); std::string resourceData(); diff --git a/Rez/Rez.cc b/Rez/Rez.cc index 69a75e2a38..cae9d57e63 100644 --- a/Rez/Rez.cc +++ b/Rez/Rez.cc @@ -1,6 +1,7 @@ -#include #include - +#include "boost/program_options.hpp" +#include "boost/filesystem.hpp" +#include "boost/filesystem/fstream.hpp" #include "RezParser.generated.hh" #include "RezLexer.h" @@ -9,24 +10,83 @@ #include "ResourceFiles.h" #include "BinaryIO.h" -int main() -{ - //RezLexer lexer("/home/wolfgang/Projects/Retro68/RIncludes/Types.r"); - RezLexer lexer("/home/wolfgang/Projects/Retro68/Rez/Test.r"); - RezWorld world; - RezParser parser(lexer, world); +namespace po = boost::program_options; +namespace fs = boost::filesystem; - parser.parse(); +static po::options_description desc; + +static void usage() +{ + std::cerr << "Usage: " << "Rez [options] input-file\n"; + std::cerr << desc << std::endl; +} + +int main(int argc, const char *argv[]) +{ + desc.add_options() + ("help,h", "show this help message") + ("output,o", po::value()->default_value("rez.output.rsrc"), "output file") + ("type,t", po::value()->default_value("rsrc"), "output file finder type code") + ("creator,c", po::value()->default_value("RSED"), "output file finder creator code") + ("debug,d", "debug logging") + ; + po::options_description hidden, alldesc; + hidden.add_options() + ("input", po::value>(), "input file" ) + ; + alldesc.add(desc).add(hidden); + + po::variables_map options; + try { - std::ofstream dataOut("test.rsrc"); - system("mkdir -p .rsrc"); - std::ofstream rsrcOut(".rsrc/test.rsrc"); + auto parsed = po::command_line_parser(argc, argv) + .options(alldesc) + .positional(po::positional_options_description().add("input", -1)) + .style(po::command_line_style::default_style | + po::command_line_style::allow_long_disguise) + .run(); + + po::store(parsed, options); + } + catch(po::error& e) + { + std::cerr << "ERROR: " << e.what() << std::endl << std::endl; + usage(); + return 1; + } + + po::notify(options); + + if(options.count("help") || !options.count("input")) + { + usage(); + return 0; + } + + RezWorld world; + + if(options.count("debug")) + world.verboseFlag = true; + + for(std::string fn : options["input"].as>()) + { + RezLexer lexer(fn); + RezParser parser(lexer, world); + parser.parse(); + } + + std::string outfile = options["output"].as(); + { + fs::path dataPath(outfile); + fs::create_directory(dataPath.parent_path() / ".rsrc"); + fs::create_directory(dataPath.parent_path() / ".finf"); + fs::ofstream dataOut(dataPath); + fs::ofstream rsrcOut(dataPath.parent_path() / ".rsrc" / dataPath.filename()); + fs::ofstream finfOut(dataPath.parent_path() / ".finf" / dataPath.filename()); world.getResources().writeFork(rsrcOut); - system("mkdir -p .finf"); - std::ofstream finfOut(".finf/test.rsrc"); - ostype(finfOut, "rsrc"); - ostype(finfOut, "RSED"); + ostype(finfOut, options["type"].as()); + ostype(finfOut, options["creator"].as()); for(int i = 8; i < 32; i++) byte(finfOut, 0); } diff --git a/Rez/RezLexer.cc b/Rez/RezLexer.cc index a53a2cdd04..07f23a2861 100644 --- a/Rez/RezLexer.cc +++ b/Rez/RezLexer.cc @@ -113,7 +113,6 @@ RezLexer::WaveToken RezLexer::nextWave() else { WaveToken tok = *pImpl->iter++; - std::cout << tok.get_value(); return tok; } } diff --git a/Rez/RezParser.yy b/Rez/RezParser.yy index f383d60ca0..952f9a8ba8 100644 --- a/Rez/RezParser.yy +++ b/Rez/RezParser.yy @@ -167,9 +167,9 @@ type_definition : "type" type_spec world.fieldLists.push(td); } "{" field_definitions "}" - { world.fieldLists.pop(); std::cout << "TYPE " << $2 << std::endl; } + { world.fieldLists.pop(); if(world.verboseFlag) std::cout << "TYPE " << $2 << std::endl; } | "type" type_spec "as" type_spec - { std::cout << "TYPE " << $2 << std::endl; } + { if(world.verboseFlag) std::cout << "TYPE " << $2 << std::endl; } ; %type res_type; diff --git a/Rez/RezWorld.cc b/Rez/RezWorld.cc index 68b77897ea..7c33d5345b 100644 --- a/Rez/RezWorld.cc +++ b/Rez/RezWorld.cc @@ -5,6 +5,7 @@ #include RezWorld::RezWorld() + : verboseFlag(false) { } @@ -27,9 +28,10 @@ TypeDefinitionPtr RezWorld::getTypeDefinition(ResType type, int id) void RezWorld::addResource(ResType type, int id, CompoundExprPtr body) { - std::cout << "RESOURCE " << type << "(" << id << ")" << std::endl; + if(verboseFlag) + std::cout << "RESOURCE " << type << "(" << id << ")" << std::endl; TypeDefinitionPtr def = getTypeDefinition(type, id); - ResourceCompiler compiler(def, body); + ResourceCompiler compiler(def, body, verboseFlag); compiler.compile(); resources.addResource(Resource(type, id, compiler.resourceData())); diff --git a/Rez/RezWorld.h b/Rez/RezWorld.h index fa0cf24608..50277b55ea 100644 --- a/Rez/RezWorld.h +++ b/Rez/RezWorld.h @@ -26,6 +26,8 @@ public: void addResource(ResType type, int id, CompoundExprPtr body); Resources& getResources() { return resources; } + + bool verboseFlag; }; diff --git a/Rez/Test.r b/Rez/Test.r index 6679348422..a0b7903b5a 100644 --- a/Rez/Test.r +++ b/Rez/Test.r @@ -3,12 +3,12 @@ */ -//#include "/home/wolfgang/Projects/Retro68/CExamples/Sample.r" +#include "/home/wolfgang/Projects/Retro68/CExamples/Sample.r" type 'TEST' { integer zero, one, two, answer = 42, missed; longint; - //integer = (after - before) / 32; + integer = (after - before) / 8; integer = $$CountOf(foo); before: array foo { @@ -16,6 +16,7 @@ type 'TEST' { integer; integer; }; + string; after: ; }; @@ -23,6 +24,10 @@ type 'TEST' { resource 'TEST' (128) { answer, 0x1234, - { 1, 2; 3, 4; } + { 1, 2; 3, 4; }, + "Hello, " + "world: " + $"Abcd 1234"; }; +