3 This file is part of R²C²
4 Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/core/application.h>
10 #include <msp/core/getopt.h>
11 #include <msp/datafile/loader.h>
12 #include <msp/datafile/parser.h>
13 #include <msp/io/print.h>
14 #include "libr2c2/articlenumber.h"
20 class ShoppingList: public Application
23 class InventoryLoader: public DataFile::Loader
26 InventoryLoader(ShoppingList &);
30 void track(ArticleNumber, int);
33 class LayoutLoader: public DataFile::Loader
36 LayoutLoader(ShoppingList &);
40 void track(ArticleNumber);
43 map<ArticleNumber, unsigned> inventory;
44 map<ArticleNumber, unsigned> layout;
46 static Application::RegApp<ShoppingList> reg;
49 ShoppingList(int, char **);
52 void load_inventory(const string &);
53 void load_layout(const string &);
57 Application::RegApp<ShoppingList> ShoppingList::reg;
59 ShoppingList::ShoppingList(int argc, char **argv)
61 string inv_fn = "inventory";
63 getopt.add_option('i', "inventory", inv_fn, GetOpt::REQUIRED_ARG);
66 load_inventory(inv_fn);
67 load_layout(getopt.get_args().front());
70 int ShoppingList::main()
72 IO::print("// Need to get:\n");
73 for(map<ArticleNumber, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
75 map<ArticleNumber, unsigned>::iterator j=inventory.find(i->first);
76 if(j!=inventory.end())
78 if(j->second<i->second)
79 IO::print("track %d %d;\n", i->first, i->second-j->second);
82 IO::print("track %d %d;\n", i->first, i->second);
85 IO::print("// Pre-existing:\n");
86 for(map<ArticleNumber, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
88 map<ArticleNumber, unsigned>::iterator j=inventory.find(i->first);
89 if(j!=inventory.end())
90 IO::print("track %d %d;\n", i->first, min(i->second, j->second));
93 IO::print("// Unused:\n");
94 for(map<ArticleNumber, unsigned>::iterator i=inventory.begin(); i!=inventory.end(); ++i)
96 map<ArticleNumber, unsigned>::iterator j=layout.find(i->first);
99 if(j->second<i->second)
100 IO::print("track %d %d;\n", i->first, i->second-j->second);
103 IO::print("track %d %d;\n", i->first, i->second);
109 void ShoppingList::load_inventory(const string &fn)
112 DataFile::Parser parser(in, fn);
113 InventoryLoader il(*this);
117 void ShoppingList::load_layout(const string &fn)
120 DataFile::Parser parser(in, fn);
121 LayoutLoader ll(*this);
126 ShoppingList::InventoryLoader::InventoryLoader(ShoppingList &s):
129 add("track", &InventoryLoader::track);
132 void ShoppingList::InventoryLoader::track(ArticleNumber art_nr, int count)
134 sl.inventory[art_nr] += count;
138 ShoppingList::LayoutLoader::LayoutLoader(ShoppingList &s):
141 add("track", &LayoutLoader::track);
147 void ShoppingList::LayoutLoader::track(ArticleNumber art_nr)