1 // Copyright (C) 2009, Ben Asselstine
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 // 02110-1301, USA.
17
18 #include <iostream>
19 #include <sigc++/functors/mem_fun.h>
20
21 #include "namelist.h"
22 #include "defs.h"
23 #include "File.h"
24 #include "ucompose.hpp"
25 #include "xmlhelper.h"
26 #include "rnd.h"
27
28 #define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<std::flush;}
29 //#define debug(x)
30
31
NameList(Glib::ustring filename,Glib::ustring item_tag)32 NameList::NameList(Glib::ustring filename, Glib::ustring item_tag)
33 {
34 d_item_tag = item_tag;
35 XML_Helper helper(File::getMiscFile(filename), std::ios::in);
36
37 helper.registerTag(d_item_tag, sigc::mem_fun((*this), &NameList::load));
38
39 if (!helper.parseXML())
40 {
41 std::cerr << String::ucompose(_("Error can't load namelist `%1'"), filename) << std::endl;
42 exit(-1);
43 }
44 helper.close();
45 return;
46 }
47
load(Glib::ustring tag,XML_Helper * helper)48 bool NameList::load(Glib::ustring tag, XML_Helper *helper)
49 {
50 if (tag == d_item_tag)
51 {
52 Glib::ustring name;
53 helper->getData(name, "name");
54 push_back(name);
55 }
56 return true;
57 }
58
popRandomName()59 Glib::ustring NameList::popRandomName()
60 {
61 Glib::ustring name;
62 if (empty())
63 return "";
64 int randno = Rnd::rand() % size();
65 name = (*this)[randno];
66
67 NameList::iterator it = std::find(begin(), end(), name);
68 if (it != end())
69 erase(it);
70
71 return name;
72 }
73