fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / lisp / lisp.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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 <config.h>
21
22 #include "lisp.hpp"
23
24 namespace lisp
25 {
26
27 Lisp::Lisp(LispType newtype)
28   : type(newtype)
29 {
30 }
31
32 Lisp::~Lisp()
33 {
34   if(type == TYPE_SYMBOL || type == TYPE_STRING)
35     delete[] v.string;
36   if(type == TYPE_CONS) {
37     delete v.cons.cdr;
38     delete v.cons.car;
39   }
40 }
41
42 Lisp*
43 Lisp::get_lisp(const char* name) const
44 {
45   for(const Lisp* p = this; p != 0; p = p->get_cdr()) {
46     Lisp* child = p->get_car();
47     if(!child || child->get_type() != TYPE_CONS)
48       continue;
49     Lisp* childname = child->get_car();
50     if(!childname)
51       continue;
52     std::string childName;
53     if(!childname->get(childName))
54       continue;
55     if(childName == name) {
56       return child->get_cdr();
57     }
58   }
59
60   return 0;
61 }
62
63 void
64 Lisp::print(int indent) const
65 {
66   for(int i = 0; i < indent; ++i)
67     printf(" ");
68
69   if(type == TYPE_CONS) {
70     printf("(\n");
71     const Lisp* lisp = this;
72     while(lisp) {
73       if(lisp->v.cons.car)
74         lisp->v.cons.car->print(indent + 1);
75       lisp = lisp->v.cons.cdr;
76     }
77     for(int i = 0; i < indent; ++i)
78       printf(" ");
79     printf(")");
80   }
81   if(type == TYPE_STRING) {
82     printf("'%s' ", v.string);
83   }
84   if(type == TYPE_INTEGER) {
85     printf("%d", v.integer);
86   }
87   if(type == TYPE_REAL) {
88     printf("%f", v.real);
89   }
90   if(type == TYPE_SYMBOL) {
91     printf("%s ", v.string);
92   }
93   if(type == TYPE_BOOLEAN) {
94     printf("%s ", v.boolean ? "true" : "false");
95   }
96   printf("\n");
97 }
98
99 } // end of namespace lisp