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