*/
#include <map>
+#include <msp/core/application.h>
#include <msp/core/getopt.h>
#include <msp/datafile/loader.h>
#include <msp/datafile/parser.h>
#include <msp/io/print.h>
+#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
{
private:
ShoppingList &sl;
- void track(unsigned, int);
+ void track(ArticleNumber, int);
};
class LayoutLoader: public DataFile::Loader
private:
ShoppingList &sl;
- void track(unsigned);
+ void track(ArticleNumber);
};
- map<unsigned, unsigned> inventory;
- map<unsigned, unsigned> layout;
+ map<ArticleNumber, unsigned> inventory;
+ map<ArticleNumber, unsigned> layout;
+
+ static Application::RegApp<ShoppingList> 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> 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<unsigned, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
+ IO::print("// Need to get:\n");
+ for(map<ArticleNumber, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
{
- map<unsigned, unsigned>::iterator j=inventory.find(i->first);
+ map<ArticleNumber, unsigned>::iterator j=inventory.find(i->first);
if(j!=inventory.end())
{
if(j->second<i->second)
- 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<unsigned, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
+ IO::print("// Pre-existing:\n");
+ for(map<ArticleNumber, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
{
- map<unsigned, unsigned>::iterator j=inventory.find(i->first);
+ map<ArticleNumber, unsigned>::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<unsigned, unsigned>::iterator i=inventory.begin(); i!=inventory.end(); ++i)
+ IO::print("// Unused:\n");
+ for(map<ArticleNumber, unsigned>::iterator i=inventory.begin(); i!=inventory.end(); ++i)
{
- map<unsigned, unsigned>::iterator j=layout.find(i->first);
+ map<ArticleNumber, unsigned>::iterator j=layout.find(i->first);
if(j!=layout.end())
{
if(j->second<i->second)
- 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)
{
add("route");
}
-void ShoppingList::LayoutLoader::track(unsigned part)
+void ShoppingList::LayoutLoader::track(ArticleNumber art_nr)
{
- ++sl.layout[part];
+ ++sl.layout[art_nr];
}