]> git.tdb.fi Git - r2c2.git/blob - source/shoppinglist/main.cpp
8b0a6f6c9aca07b2a9c376902a341360d0897c95
[r2c2.git] / source / shoppinglist / main.cpp
1 #include <map>
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"
8
9 using namespace std;
10 using namespace Msp;
11 using namespace R2C2;
12
13 class ShoppingList: public RegisteredApplication<ShoppingList>
14 {
15 private:
16         class InventoryLoader: public DataFile::Loader
17         {
18         public:
19                 InventoryLoader(ShoppingList &);
20         private:
21                 ShoppingList &sl;
22
23                 void track(ArticleNumber, int);
24         };
25
26         class LayoutLoader: public DataFile::Loader
27         {
28         public:
29                 LayoutLoader(ShoppingList &);
30         private:
31                 ShoppingList &sl;
32
33                 void track(ArticleNumber);
34         };
35
36         map<ArticleNumber, unsigned> inventory;
37         map<ArticleNumber, unsigned> layout;
38
39 public:
40         ShoppingList(int, char **);
41         int main();
42 private:
43         void load_inventory(const string &);
44         void load_layout(const string &);
45 };
46
47
48 ShoppingList::ShoppingList(int argc, char **argv)
49 {
50         string inv_fn = "inventory";
51         GetOpt getopt;
52         getopt.add_option('i', "inventory", inv_fn, GetOpt::REQUIRED_ARG);
53         getopt(argc, argv);
54
55         load_inventory(inv_fn);
56         load_layout(getopt.get_args().front());
57 }
58
59 int ShoppingList::main()
60 {
61         IO::print("// Need to get:\n");
62         for(map<ArticleNumber, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
63         {
64                 map<ArticleNumber, unsigned>::iterator j=inventory.find(i->first);
65                 if(j!=inventory.end())
66                 {
67                         if(j->second<i->second)
68                                 IO::print("track %d %d;\n", i->first, i->second-j->second);
69                 }
70                 else
71                         IO::print("track %d %d;\n", i->first, i->second);
72         }
73
74         IO::print("// Pre-existing:\n");
75         for(map<ArticleNumber, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
76         {
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));
80         }
81
82         IO::print("// Unused:\n");
83         for(map<ArticleNumber, unsigned>::iterator i=inventory.begin(); i!=inventory.end(); ++i)
84         {
85                 map<ArticleNumber, unsigned>::iterator j=layout.find(i->first);
86                 if(j!=layout.end())
87                 {
88                         if(j->second<i->second)
89                                 IO::print("track %d %d;\n", i->first, i->second-j->second);
90                 }
91                 else
92                         IO::print("track %d %d;\n", i->first, i->second);
93         }
94
95         return 0;
96 }
97
98 void ShoppingList::load_inventory(const string &fn)
99 {
100         IO::File in(fn);
101         DataFile::Parser parser(in, fn);
102         InventoryLoader il(*this);
103         il.load(parser);
104 }
105
106 void ShoppingList::load_layout(const string &fn)
107 {
108         IO::File in(fn);
109         DataFile::Parser parser(in, fn);
110         LayoutLoader ll(*this);
111         ll.load(parser);
112 }
113
114
115 ShoppingList::InventoryLoader::InventoryLoader(ShoppingList &s):
116         sl(s)
117 {
118         add("track", &InventoryLoader::track);
119 }
120
121 void ShoppingList::InventoryLoader::track(ArticleNumber art_nr, int count)
122 {
123         sl.inventory[art_nr] += count;
124 }
125
126
127 ShoppingList::LayoutLoader::LayoutLoader(ShoppingList &s):
128         sl(s)
129 {
130         add("track", &LayoutLoader::track);
131         add("base");
132         add("route");
133         add("zone");
134 }
135
136 void ShoppingList::LayoutLoader::track(ArticleNumber art_nr)
137 {
138         ++sl.layout[art_nr];
139 }