X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Flisp%2Fparser.cpp;h=d108ba014ecbf353ef010dbc3559a1e167283fd3;hb=77d00a6f464cc87c6b50cf1f3b38064a7ae9aead;hp=224ee3eb117b413b9952bd75183713c591749708;hpb=dbe5ccbee9182cc4d3704e10b95f2884bab9546d;p=supertux.git diff --git a/src/lisp/parser.cpp b/src/lisp/parser.cpp index 224ee3eb1..d108ba014 100644 --- a/src/lisp/parser.cpp +++ b/src/lisp/parser.cpp @@ -1,8 +1,7 @@ // $Id$ // -// TuxKart - a fun racing game with go-kart -// Copyright (C) 2004 Matthias Braun -// code in this file based on lispreader from Mark Probst +// SuperTux +// Copyright (C) 2006 Matthias Braun // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -17,6 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + #include #include @@ -25,10 +25,10 @@ #include #include -#include "tinygettext/tinygettext.h" -#include "parser.h" -#include "lisp.h" -#include "file_system.h" +#include "tinygettext/tinygettext.hpp" +#include "physfs/physfs_stream.hpp" +#include "parser.hpp" +#include "lisp.hpp" namespace lisp { @@ -48,10 +48,19 @@ Parser::~Parser() delete dictionary_manager; } +static std::string dirname(std::string filename) +{ + std::string::size_type p = filename.find_last_of('/'); + if(p == std::string::npos) + return ""; + + return filename.substr(0, p+1); +} + Lisp* Parser::parse(const std::string& filename) { - std::ifstream in(filename.c_str()); + IFileStream in(filename); if(!in.good()) { std::stringstream msg; msg << "Parser problem: Couldn't open file '" << filename << "'."; @@ -59,10 +68,10 @@ Parser::parse(const std::string& filename) } if(dictionary_manager) { - dictionary_manager->add_directory(FileSystem::dirname(filename)); + dictionary_manager->add_directory(dirname(filename)); dictionary = & (dictionary_manager->get_dictionary()); } - + return parse(in); } @@ -76,11 +85,11 @@ Parser::parse(std::istream& stream) Lisp* result = new Lisp(Lisp::TYPE_CONS); result->v.cons.car = read(); result->v.cons.cdr = 0; - + delete lexer; lexer = 0; - return result; + return result; } Lisp* @@ -102,7 +111,7 @@ Parser::read() } case Lexer::TOKEN_OPEN_PAREN: { result = new Lisp(Lisp::TYPE_CONS); - + token = lexer->getNextToken(); if(token == Lexer::TOKEN_CLOSE_PAREN) { result->v.cons.car = 0; @@ -115,21 +124,21 @@ Parser::read() // evaluate translation function (_ str) in place here token = lexer->getNextToken(); if(token != Lexer::TOKEN_STRING) - throw new std::runtime_error("Expected string after '(_'"); - + throw std::runtime_error("Expected string after '(_'"); + result = new Lisp(Lisp::TYPE_STRING); if(dictionary) { std::string translation = dictionary->translate(lexer->getString()); result->v.string = new char[translation.size()+1]; memcpy(result->v.string, translation.c_str(), translation.size()+1); } else { - size_t len = strlen(lexer->getString()) + 1; + size_t len = strlen(lexer->getString()) + 1; result->v.string = new char[len]; memcpy(result->v.string, lexer->getString(), len); } token = lexer->getNextToken(); if(token != Lexer::TOKEN_CLOSE_PAREN) - throw new std::runtime_error("Expected ')' after '(_ string'"); + throw std::runtime_error("Expected ')' after '(_ string'"); break; }