2 #include <msp/core/application.h>
3 #include <msp/core/getopt.h>
4 #include <msp/datafile/loader.h>
5 #include <msp/datafile/parser.h>
6 #include <msp/io/print.h>
7 #include "libr2c2/articlenumber.h"
13 class ShoppingList: public RegisteredApplication<ShoppingList>
16 class InventoryLoader: public DataFile::Loader
19 InventoryLoader(ShoppingList &);
23 void track(ArticleNumber, int);
26 class LayoutLoader: public DataFile::Loader
29 LayoutLoader(ShoppingList &);
33 void track(ArticleNumber);
36 map<ArticleNumber, unsigned> inventory;
37 map<ArticleNumber, unsigned> layout;
40 ShoppingList(int, char **);
43 void load_inventory(const string &);
44 void load_layout(const string &);
48 ShoppingList::ShoppingList(int argc, char **argv)
50 string inv_fn = "inventory";
52 getopt.add_option('i', "inventory", inv_fn, GetOpt::REQUIRED_ARG);
55 load_inventory(inv_fn);
56 load_layout(getopt.get_args().front());
59 int ShoppingList::main()
61 IO::print("// Need to get:\n");
62 for(map<ArticleNumber, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
64 map<ArticleNumber, unsigned>::iterator j=inventory.find(i->first);
65 if(j!=inventory.end())
67 if(j->second<i->second)
68 IO::print("track %d %d;\n", i->first, i->second-j->second);
71 IO::print("track %d %d;\n", i->first, i->second);
74 IO::print("// Pre-existing:\n");
75 for(map<ArticleNumber, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
77 map<ArticleNumber, unsigned>::iterator j=inventory.find(i->first);
78 if(j!=inventory.end())
79 IO::print("track %d %d;\n", i->first, min(i->second, j->second));
82 IO::print("// Unused:\n");
83 for(map<ArticleNumber, unsigned>::iterator i=inventory.begin(); i!=inventory.end(); ++i)
85 map<ArticleNumber, unsigned>::iterator j=layout.find(i->first);
88 if(j->second<i->second)
89 IO::print("track %d %d;\n", i->first, i->second-j->second);
92 IO::print("track %d %d;\n", i->first, i->second);
98 void ShoppingList::load_inventory(const string &fn)
101 DataFile::Parser parser(in, fn);
102 InventoryLoader il(*this);
106 void ShoppingList::load_layout(const string &fn)
109 DataFile::Parser parser(in, fn);
110 LayoutLoader ll(*this);
115 ShoppingList::InventoryLoader::InventoryLoader(ShoppingList &s):
118 add("track", &InventoryLoader::track);
121 void ShoppingList::InventoryLoader::track(ArticleNumber art_nr, int count)
123 sl.inventory[art_nr] += count;
127 ShoppingList::LayoutLoader::LayoutLoader(ShoppingList &s):
130 add("track", &LayoutLoader::track);
136 void ShoppingList::LayoutLoader::track(ArticleNumber art_nr)