~ubuntu-branches/ubuntu/precise/pingus/precise

« back to all changes in this revision

Viewing changes to src/sexpr_file_reader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-02-28 19:44:25 UTC
  • mfrom: (4.1.4 hardy)
  • Revision ID: james.westby@ubuntu.com-20080228194425-e8ilohlijv02kgcf
Tags: 0.7.2-2
* Fix FTBFS with gcc-4.3 by adding the missing include in
  src/input/evdev_device.cpp (Closes: #462238):
   + debian/patches/20_fix_FTBFS_with_gcc-4.3.
* Rename former patch so that the filename reflects the order in which
  the patches are applied:
   - debian/patches/data_dir.patch
   + debian/patches/10_fix_data_directory.
* Bump Standards-Version from 3.7.2 to 3.7.3, no changes needed.
* Add a dh_desktop call in the arch-dep part of debian/rules.
* Adjust the “missing-dep-for-interpreter guile” override since lintian
  now lists an alternative for that dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  $Id: sexpr_file_reader.cpp 2987 2007-08-17 16:25:49Z grumbel $
 
2
//
 
3
//  Pingus - A free Lemmings clone
 
4
//  Copyright (C) 2005 Ingo Ruhnke <grumbel@gmx.de>
 
5
//
 
6
//  This program is free software; you can redistribute it and/or
 
7
//  modify it under the terms of the GNU General Public License
 
8
//  as published by the Free Software Foundation; either version 2
 
9
//  of the License, or (at your option) any later version.
 
10
//
 
11
//  This program is distributed in the hope that it will be useful,
 
12
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
//  GNU General Public License for more details.
 
15
//
 
16
//  You should have received a copy of the GNU General Public License
 
17
//  along with this program; if not, write to the Free Software
 
18
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 
 
20
#include <assert.h>
 
21
#include "math/vector3f.hpp"
 
22
#include "math/color.hpp"
 
23
#include "math/size.hpp"
 
24
#include "res_descriptor.hpp"
 
25
#include "resource_modifier.hpp"
 
26
#include "math/vector2i.hpp"
 
27
#include "file_reader_impl.hpp"
 
28
#include "sexpr_file_reader.hpp"
 
29
 
 
30
class SExprFileReaderImpl: public FileReaderImpl
 
31
{
 
32
public:
 
33
  boost::shared_ptr<lisp::Lisp> sexpr;
 
34
 
 
35
  SExprFileReaderImpl(boost::shared_ptr<lisp::Lisp> sexpr_) 
 
36
    : sexpr(sexpr_)
 
37
  {
 
38
    assert(sexpr->get_type() == lisp::Lisp::TYPE_LIST &&
 
39
           sexpr->get_list_size() >= 1);
 
40
    
 
41
    for(size_t i = 1; i < sexpr->get_list_size(); ++i)
 
42
      { // iterate over subsections
 
43
        sexpr->get_list_elem(i);
 
44
      }
 
45
  }
 
46
 
 
47
  ~SExprFileReaderImpl()
 
48
  {
 
49
    // FIXME: Do we have to free the lisp pointer here or outside of the code?
 
50
  }
 
51
 
 
52
  std::string get_name() const 
 
53
  {
 
54
    return sexpr->get_list_elem(0)->get_symbol();
 
55
  }
 
56
 
 
57
  bool read_int   (const char* name, int& v) const 
 
58
  {
 
59
    boost::shared_ptr<lisp::Lisp> item = get_subsection_item(name);
 
60
    if (item && item->get_type() == lisp::Lisp::TYPE_INT)
 
61
      {
 
62
        v = item->get_int();
 
63
        return true;
 
64
      }
 
65
    return false;
 
66
  }
 
67
 
 
68
  bool read_float (const char* name, float& v) const 
 
69
  {
 
70
    boost::shared_ptr<lisp::Lisp> item = get_subsection_item(name);
 
71
    if (item)
 
72
      {
 
73
        if (item->get_type() == lisp::Lisp::TYPE_FLOAT)
 
74
          {
 
75
            v = item->get_float();
 
76
            return true;
 
77
          }
 
78
        else if (item->get_type() == lisp::Lisp::TYPE_INT)
 
79
          {
 
80
            v = (float)item->get_int();
 
81
            return true;
 
82
          }
 
83
        else
 
84
          {
 
85
            return false;
 
86
          }
 
87
      }
 
88
    return false;
 
89
  }
 
90
 
 
91
  bool read_bool  (const char* name, bool& v) const 
 
92
  {
 
93
    boost::shared_ptr<lisp::Lisp> item = get_subsection_item(name);
 
94
    if (item && item->get_type() == lisp::Lisp::TYPE_BOOL)
 
95
      {
 
96
        v = item->get_bool();
 
97
        return true;
 
98
      }
 
99
    else if (item && item->get_type() == lisp::Lisp::TYPE_INT)
 
100
      {
 
101
        v = item->get_int();
 
102
        return true;
 
103
      }
 
104
    return false;
 
105
  }
 
106
 
 
107
  bool read_string(const char* name, std::string& v) const 
 
108
  {
 
109
    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
 
110
    if (sub)
 
111
      {
 
112
        v = "";
 
113
        for(size_t i = 1; i < sub->get_list_size(); ++i)
 
114
          {
 
115
            boost::shared_ptr<lisp::Lisp> item = sub->get_list_elem(i);
 
116
            if (item->get_type() == lisp::Lisp::TYPE_STRING)
 
117
              {
 
118
                v += item->get_string();
 
119
              }
 
120
            else if (item->get_type() == lisp::Lisp::TYPE_SYMBOL)
 
121
              {
 
122
                v += item->get_symbol();
 
123
              }
 
124
          }
 
125
        return true;
 
126
      }
 
127
    return false;
 
128
  }
 
129
 
 
130
  bool read_vector(const char* name, Vector3f& v) const
 
131
  {
 
132
    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
 
133
    if (sub && sub->get_list_size() == 4)
 
134
      {
 
135
        v = Vector3f(sub->get_list_elem(1)->get_float(),
 
136
                     sub->get_list_elem(2)->get_float(),
 
137
                     sub->get_list_elem(3)->get_float());
 
138
        return true;
 
139
      }    
 
140
    return false;
 
141
  }
 
142
 
 
143
  bool read_size(const char* name, Size& v) const
 
144
  {
 
145
    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
 
146
    if (sub && sub->get_list_size() == 3)
 
147
      {
 
148
        v.width  = sub->get_list_elem(1)->get_int();
 
149
        v.height = sub->get_list_elem(2)->get_int();
 
150
        return true;
 
151
      }    
 
152
    return false;
 
153
  }
 
154
 
 
155
  bool read_vector2i(const char* name, Vector2i& v) const
 
156
  {
 
157
    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
 
158
    if (sub && sub->get_list_size() == 3)
 
159
      {
 
160
        v.x = sub->get_list_elem(1)->get_int();
 
161
        v.y = sub->get_list_elem(2)->get_int();
 
162
        return true;
 
163
      }    
 
164
    return false;
 
165
  }
 
166
 
 
167
  bool read_color (const char* name, Color& v) const
 
168
  {
 
169
    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
 
170
    if (sub && sub->get_list_size() == 5)
 
171
      {
 
172
        v = Color(int(sub->get_list_elem(1)->get_float() * 255),
 
173
                  int(sub->get_list_elem(2)->get_float() * 255),
 
174
                  int(sub->get_list_elem(3)->get_float() * 255),
 
175
                  int(sub->get_list_elem(4)->get_float() * 255));
 
176
        return true;
 
177
      }
 
178
    return false;
 
179
  }
 
180
 
 
181
  bool read_desc  (const char* name, ResDescriptor& v) const 
 
182
  {
 
183
    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
 
184
    if (sub)
 
185
      {
 
186
        SExprFileReader reader(sub);
 
187
        reader.read_string("image",  v.res_name);
 
188
        reader.read_enum("modifier", v.modifier, ResourceModifierNS::rs_from_string);
 
189
        return true;
 
190
      }
 
191
    return false;
 
192
  }
 
193
 
 
194
  bool read_section(const char* name, FileReader& v) const 
 
195
  {
 
196
    boost::shared_ptr<lisp::Lisp> cur = get_subsection(name);
 
197
    if (cur)
 
198
      {
 
199
        v = SExprFileReader(cur);
 
200
        return true;
 
201
      }
 
202
    return false;
 
203
  }
 
204
 
 
205
  std::vector<FileReader> get_sections() const 
 
206
  {
 
207
    std::vector<FileReader> lst;
 
208
    for(size_t i = 1; i < sexpr->get_list_size(); ++i)
 
209
      { // iterate over subsections
 
210
        lst.push_back(SExprFileReader(sexpr->get_list_elem(i)));
 
211
      }
 
212
    return lst;
 
213
  }
 
214
 
 
215
  std::vector<std::string> get_section_names() const 
 
216
  {
 
217
    std::vector<std::string> lst;
 
218
 
 
219
    for(size_t i = 1; i < sexpr->get_list_size(); ++i)
 
220
      { // iterate over subsections
 
221
        boost::shared_ptr<lisp::Lisp> sub = sexpr->get_list_elem(i);
 
222
        lst.push_back(sub->get_list_elem(0)->get_symbol());
 
223
      }
 
224
 
 
225
    return lst;
 
226
  }
 
227
 
 
228
private:
 
229
  boost::shared_ptr<lisp::Lisp> get_subsection_item(const char* name) const
 
230
  {
 
231
    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
 
232
    if (sub && sub->get_list_size() == 2)
 
233
      {
 
234
        return sub->get_list_elem(1);
 
235
      }
 
236
    return boost::shared_ptr<lisp::Lisp>();
 
237
  }
 
238
 
 
239
  boost::shared_ptr<lisp::Lisp> get_subsection(const char* name) const
 
240
  {
 
241
    for(size_t i = 1; i < sexpr->get_list_size(); ++i)
 
242
      { // iterate over subsections
 
243
        boost::shared_ptr<lisp::Lisp> sub = sexpr->get_list_elem(i);
 
244
        if (strcmp(sub->get_list_elem(0)->get_symbol(), name) == 0)
 
245
          return sub;
 
246
      }
 
247
    return boost::shared_ptr<lisp::Lisp>();
 
248
  } 
 
249
 
 
250
};
 
251
 
 
252
SExprFileReader::SExprFileReader(boost::shared_ptr<lisp::Lisp> lisp)
 
253
  : FileReader(boost::shared_ptr<FileReaderImpl>(new SExprFileReaderImpl(lisp)))
 
254
{
 
255
}
 
256
 
 
257
/* EOF */