initial commit, lordsawar source, slightly modified
[lordsawar] / src / Itemlist.cpp
1 // Copyright (C) 2004, 2005 Ulf Lorenz
2 // Copyright (C) 2007, 2008 Ben Asselstine
3 //
4 //  This program is free software; you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation; either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU Library General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
17 //  02110-1301, USA.
18
19 #include <sigc++/functors/mem_fun.h>
20
21 #include "Itemlist.h"
22
23 #include "File.h"
24 #include "defs.h"
25
26 std::string Itemlist::d_tag = "itemlist";
27
28 Itemlist* Itemlist::d_instance = 0;
29
30 Itemlist* Itemlist::getInstance()
31 {
32     if (!d_instance)
33         d_instance = new Itemlist();
34
35     return d_instance;
36 }
37
38 Itemlist* Itemlist::getInstance(XML_Helper *helper)
39 {
40     if (!d_instance)
41         d_instance = new Itemlist();
42
43     d_instance = new Itemlist(helper);
44     return d_instance;
45 }
46
47 void Itemlist::createStandardInstance()
48 {
49     deleteInstance();
50
51     XML_Helper helper(File::getItemDescription(), std::ios::in, false);
52     d_instance = new Itemlist(&helper);
53
54     if (!helper.parse())
55     {
56         std::cerr <<_("Could not parse items description file. Exiting!\n");
57         exit(-1);
58     }
59     helper.close();
60 }
61
62 void Itemlist::deleteInstance()
63 {
64     if (d_instance != 0)
65         delete d_instance;
66
67     d_instance = 0;
68 }
69
70
71 Itemlist::Itemlist(XML_Helper* helper)
72 {
73     helper->registerTag(ItemProto::d_tag, sigc::mem_fun(*this, &Itemlist::loadItemProto));
74 }
75
76 Itemlist::Itemlist()
77 {
78 }
79
80 Itemlist::~Itemlist()
81 {
82     flClear();
83 }
84
85 bool Itemlist::loadItemProto(std::string tag, XML_Helper* helper)
86 {
87     if (tag != ItemProto::d_tag)
88         return false;
89
90     ItemProto* i = new ItemProto(helper);
91     i->setTypeId((*this).size());
92     (*this)[(*this).size()] = i;
93
94     return true;
95 }
96
97 void Itemlist::flErase(iterator it)
98 {
99     delete (*it).second;
100     erase(it);
101 }
102
103 void Itemlist::flClear()
104 {
105     while (!empty())
106         flErase(begin());
107 }
108
109 bool Itemlist::save(XML_Helper* helper) const
110 {
111     bool retval = true;
112
113     retval &= helper->openTag(d_tag);
114
115     for (const_iterator it = begin(); it != end(); it++)
116       (*it).second->save(helper);
117     
118     retval &= helper->closeTag();
119
120     return retval;
121 }
122
123 void Itemlist::remove(ItemProto *itemproto)
124 {
125   guint32 index = 0;
126   for (iterator it = begin(); it != end(); it++)
127     {
128       if ((*it).second == itemproto)
129         {
130           erase(index);
131           break;
132         }
133       index++;
134     }
135 }