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