3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/core/getopt.h>
10 #include <msp/datafile/loader.h>
11 #include <msp/datafile/parser.h>
12 #include <msp/io/print.h>
20 ShoppingList(int, char **);
21 void print(IO::Base &);
23 class InventoryLoader: public DataFile::Loader
26 InventoryLoader(ShoppingList &);
30 void track(unsigned, int);
33 class LayoutLoader: public DataFile::Loader
36 LayoutLoader(ShoppingList &);
43 map<unsigned, unsigned> inventory;
44 map<unsigned, unsigned> layout;
46 void load_inventory(const string &);
47 void load_layout(const string &);
50 int main(int argc, char **argv)
52 ShoppingList sl(argc, argv);
57 ShoppingList::ShoppingList(int argc, char **argv)
59 string inv_fn="inventory";
61 getopt.add_option('i', "inventory", inv_fn, GetOpt::REQUIRED_ARG);
64 load_inventory(inv_fn);
65 load_layout(getopt.get_args().front());
68 void ShoppingList::load_inventory(const string &fn)
71 DataFile::Parser parser(in, fn);
72 InventoryLoader il(*this);
76 void ShoppingList::load_layout(const string &fn)
79 DataFile::Parser parser(in, fn);
80 LayoutLoader ll(*this);
84 void ShoppingList::print(IO::Base &out)
86 IO::print(out, "// Need to get:\n");
87 for(map<unsigned, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
89 map<unsigned, unsigned>::iterator j=inventory.find(i->first);
90 if(j!=inventory.end())
92 if(j->second<i->second)
93 IO::print(out, "track %d %d;\n", i->first, i->second-j->second);
96 IO::print(out, "track %d %d;\n", i->first, i->second);
99 IO::print(out, "// Pre-existing:\n");
100 for(map<unsigned, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
102 map<unsigned, unsigned>::iterator j=inventory.find(i->first);
103 if(j!=inventory.end())
104 IO::print(out, "track %d %d;\n", i->first, min(i->second, j->second));
107 IO::print(out, "// Unused:\n");
108 for(map<unsigned, unsigned>::iterator i=inventory.begin(); i!=inventory.end(); ++i)
110 map<unsigned, unsigned>::iterator j=layout.find(i->first);
113 if(j->second<i->second)
114 IO::print(out, "track %d %d;\n", i->first, i->second-j->second);
117 IO::print(out, "track %d %d;\n", i->first, i->second);
121 ShoppingList::InventoryLoader::InventoryLoader(ShoppingList &s):
124 add("track", &InventoryLoader::track);
127 void ShoppingList::InventoryLoader::track(unsigned part, int count)
129 sl.inventory[part]+=count;
132 ShoppingList::LayoutLoader::LayoutLoader(ShoppingList &s):
135 add("track", &LayoutLoader::track);
140 void ShoppingList::LayoutLoader::track(unsigned part)