| 1 | // $Id: lisp.hpp 848 2006-10-29 02:14:04Z coz $ |
| 2 | // |
| 3 | // TuxKart - a fun racing game with go-kart |
| 4 | // Copyright (C) 2004 Matthias Braun <[email protected]> |
| 5 | // code in this file based on lispreader from Mark Probst |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License |
| 9 | // as published by the Free Software Foundation; either version 2 |
| 10 | // of the License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | #ifndef __LISPREADER_H__ |
| 21 | #define __LISPREADER_H__ |
| 22 | |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace lisp |
| 27 | { |
| 28 | |
| 29 | class Lisp |
| 30 | { |
| 31 | public: |
| 32 | ~Lisp(); |
| 33 | |
| 34 | enum LispType |
| 35 | { |
| 36 | TYPE_CONS, |
| 37 | TYPE_SYMBOL, |
| 38 | TYPE_INTEGER, |
| 39 | TYPE_STRING, |
| 40 | TYPE_REAL, |
| 41 | TYPE_BOOLEAN |
| 42 | }; |
| 43 | |
| 44 | LispType getType() const |
| 45 | { return m_type; } |
| 46 | |
| 47 | const Lisp* getCar() const |
| 48 | { return m_v.m_cons.m_car; } |
| 49 | const Lisp* getCdr() const |
| 50 | { return m_v.m_cons.m_cdr; } |
| 51 | bool get(std::string& val) const |
| 52 | { |
| 53 | if(m_type != TYPE_STRING && m_type != TYPE_SYMBOL) |
| 54 | return false; |
| 55 | val = m_v.m_string; |
| 56 | return true; |
| 57 | } |
| 58 | bool get(int& val) const |
| 59 | { |
| 60 | if(m_type != TYPE_INTEGER) |
| 61 | return false; |
| 62 | val = m_v.m_integer; |
| 63 | return true; |
| 64 | } |
| 65 | bool get(float& val) const |
| 66 | { |
| 67 | if(m_type != TYPE_REAL && m_type != TYPE_INTEGER) |
| 68 | return false; |
| 69 | val = m_type==TYPE_REAL ? m_v.m_real : m_v.m_integer; |
| 70 | return true; |
| 71 | } |
| 72 | bool get(bool& val) const |
| 73 | { |
| 74 | if(m_type != TYPE_BOOLEAN) |
| 75 | return false; |
| 76 | val = m_v.m_boolean; |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | /* conveniance functions which traverse the list until a child with a |
| 81 | * specified name is found. The value part is then interpreted in a specific |
| 82 | * way. The functions return true, if a child was found and could be |
| 83 | * interpreted correctly, otherwise false is returned and the variable value |
| 84 | * is not changed. |
| 85 | * (Please note that searching the lisp structure is O(n) so these functions |
| 86 | * are no good idea for performance critical areas) |
| 87 | */ |
| 88 | template<class T> |
| 89 | bool get(const char* name, T& val) const |
| 90 | { |
| 91 | const Lisp* lisp = getLisp(name); |
| 92 | if(!lisp) |
| 93 | return false; |
| 94 | |
| 95 | lisp = lisp->getCdr(); |
| 96 | if(!lisp) |
| 97 | return false; |
| 98 | lisp = lisp->getCar(); |
| 99 | if(!lisp) |
| 100 | return false; |
| 101 | return lisp->get(val); |
| 102 | } |
| 103 | template<class T> |
| 104 | bool getVector(const char* name, std::vector<T>& vec) const |
| 105 | { |
| 106 | const Lisp* child = getLisp(name); |
| 107 | if(!child) |
| 108 | return false; |
| 109 | |
| 110 | for(child = child->getCdr(); child != 0; child = child->getCdr()) |
| 111 | { |
| 112 | T val; |
| 113 | if(!child->getCar()) |
| 114 | continue; |
| 115 | if(child->getCar()->get(val)) |
| 116 | { |
| 117 | vec.insert(vec.begin(),val); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | const Lisp* getLisp(const char* name) const; |
| 125 | const Lisp* getLisp(const std::string& name) const |
| 126 | { return getLisp(name.c_str()); } |
| 127 | |
| 128 | // for debugging |
| 129 | void print(int indent = 0) const; |
| 130 | |
| 131 | private: |
| 132 | friend class Parser; |
| 133 | Lisp(LispType newtype); |
| 134 | |
| 135 | LispType m_type; |
| 136 | union |
| 137 | { |
| 138 | struct |
| 139 | { |
| 140 | Lisp* m_car; |
| 141 | Lisp* m_cdr; |
| 142 | } |
| 143 | m_cons; |
| 144 | |
| 145 | char* m_string; |
| 146 | int m_integer; |
| 147 | bool m_boolean; |
| 148 | float m_real; |
| 149 | } |
| 150 | m_v; |
| 151 | }; |
| 152 | |
| 153 | } // end of namespace lisp |
| 154 | |
| 155 | #endif |
| 156 |