From cadb9f79feb379acd6712fba97af4f6696a413c5 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 2 Nov 2010 05:49:11 +0000 Subject: [PATCH] Use ArticleNumber in shoppinglist Some other refactoring as well --- Build | 5 ++ source/shoppinglist/main.cpp | 108 ++++++++++++++++++----------------- 2 files changed, 62 insertions(+), 51 deletions(-) diff --git a/Build b/Build index 9be8021..a9b5308 100644 --- a/Build +++ b/Build @@ -6,6 +6,11 @@ package "märklin" program "shoppinglist" { source "source/shoppinglist"; + build_info + { + incpath "source"; + library "marklin"; + }; require "mspdatafile"; }; diff --git a/source/shoppinglist/main.cpp b/source/shoppinglist/main.cpp index f711222..2b8f5bf 100644 --- a/source/shoppinglist/main.cpp +++ b/source/shoppinglist/main.cpp @@ -6,19 +6,19 @@ Distributed under the GPL */ #include +#include #include #include #include #include +#include "libmarklin/articlenumber.h" using namespace std; using namespace Msp; +using namespace Marklin; -class ShoppingList +class ShoppingList: public Application { -public: - ShoppingList(int, char **); - void print(IO::Base &); private: class InventoryLoader: public DataFile::Loader { @@ -27,7 +27,7 @@ private: private: ShoppingList &sl; - void track(unsigned, int); + void track(ArticleNumber, int); }; class LayoutLoader: public DataFile::Loader @@ -37,98 +37,104 @@ private: private: ShoppingList &sl; - void track(unsigned); + void track(ArticleNumber); }; - map inventory; - map layout; + map inventory; + map layout; + + static Application::RegApp reg; +public: + ShoppingList(int, char **); + int main(); +private: void load_inventory(const string &); void load_layout(const string &); }; -int main(int argc, char **argv) -{ - ShoppingList sl(argc, argv); - sl.print(IO::cout); - return 0; -} + +Application::RegApp ShoppingList::reg; ShoppingList::ShoppingList(int argc, char **argv) { - string inv_fn="inventory"; + string inv_fn = "inventory"; GetOpt getopt; getopt.add_option('i', "inventory", inv_fn, GetOpt::REQUIRED_ARG); - getopt(argc,argv); + getopt(argc, argv); load_inventory(inv_fn); load_layout(getopt.get_args().front()); } -void ShoppingList::load_inventory(const string &fn) -{ - IO::File in(fn); - DataFile::Parser parser(in, fn); - InventoryLoader il(*this); - il.load(parser); -} - -void ShoppingList::load_layout(const string &fn) -{ - IO::File in(fn); - DataFile::Parser parser(in, fn); - LayoutLoader ll(*this); - ll.load(parser); -} - -void ShoppingList::print(IO::Base &out) +int ShoppingList::main() { - IO::print(out, "// Need to get:\n"); - for(map::iterator i=layout.begin(); i!=layout.end(); ++i) + IO::print("// Need to get:\n"); + for(map::iterator i=layout.begin(); i!=layout.end(); ++i) { - map::iterator j=inventory.find(i->first); + map::iterator j=inventory.find(i->first); if(j!=inventory.end()) { if(j->secondsecond) - IO::print(out, "track %d %d;\n", i->first, i->second-j->second); + IO::print("track %d %d;\n", i->first, i->second-j->second); } else - IO::print(out, "track %d %d;\n", i->first, i->second); + IO::print("track %d %d;\n", i->first, i->second); } - IO::print(out, "// Pre-existing:\n"); - for(map::iterator i=layout.begin(); i!=layout.end(); ++i) + IO::print("// Pre-existing:\n"); + for(map::iterator i=layout.begin(); i!=layout.end(); ++i) { - map::iterator j=inventory.find(i->first); + map::iterator j=inventory.find(i->first); if(j!=inventory.end()) - IO::print(out, "track %d %d;\n", i->first, min(i->second, j->second)); + IO::print("track %d %d;\n", i->first, min(i->second, j->second)); } - IO::print(out, "// Unused:\n"); - for(map::iterator i=inventory.begin(); i!=inventory.end(); ++i) + IO::print("// Unused:\n"); + for(map::iterator i=inventory.begin(); i!=inventory.end(); ++i) { - map::iterator j=layout.find(i->first); + map::iterator j=layout.find(i->first); if(j!=layout.end()) { if(j->secondsecond) - IO::print(out, "track %d %d;\n", i->first, i->second-j->second); + IO::print("track %d %d;\n", i->first, i->second-j->second); } else - IO::print(out, "track %d %d;\n", i->first, i->second); + IO::print("track %d %d;\n", i->first, i->second); } + + return 0; } +void ShoppingList::load_inventory(const string &fn) +{ + IO::File in(fn); + DataFile::Parser parser(in, fn); + InventoryLoader il(*this); + il.load(parser); +} + +void ShoppingList::load_layout(const string &fn) +{ + IO::File in(fn); + DataFile::Parser parser(in, fn); + LayoutLoader ll(*this); + ll.load(parser); +} + + ShoppingList::InventoryLoader::InventoryLoader(ShoppingList &s): sl(s) { add("track", &InventoryLoader::track); } -void ShoppingList::InventoryLoader::track(unsigned part, int count) +void ShoppingList::InventoryLoader::track(ArticleNumber art_nr, int count) { - sl.inventory[part]+=count; + sl.inventory[art_nr] += count; } + ShoppingList::LayoutLoader::LayoutLoader(ShoppingList &s): sl(s) { @@ -137,7 +143,7 @@ ShoppingList::LayoutLoader::LayoutLoader(ShoppingList &s): add("route"); } -void ShoppingList::LayoutLoader::track(unsigned part) +void ShoppingList::LayoutLoader::track(ArticleNumber art_nr) { - ++sl.layout[part]; + ++sl.layout[art_nr]; } -- 2.45.2