From: Thomas Geymayer Date: Tue, 17 Dec 2013 18:07:08 +0000 (+0100) Subject: Expose SGPath to Nasal X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=67be73fdde6c5fbc4bb61a92585e2d37e344c501;p=flightgear.git Expose SGPath to Nasal --- diff --git a/src/Scripting/CMakeLists.txt b/src/Scripting/CMakeLists.txt index 44024e55c..413a137a7 100644 --- a/src/Scripting/CMakeLists.txt +++ b/src/Scripting/CMakeLists.txt @@ -11,6 +11,7 @@ set(SOURCES NasalHTTP.cxx NasalString.cxx NasalModelData.cxx + NasalSGPath.cxx ) set(HEADERS @@ -23,6 +24,7 @@ set(HEADERS NasalHTTP.hxx NasalString.hxx NasalModelData.hxx + NasalSGPath.hxx ) if(WIN32) diff --git a/src/Scripting/NasalSGPath.cxx b/src/Scripting/NasalSGPath.cxx new file mode 100644 index 000000000..51f281902 --- /dev/null +++ b/src/Scripting/NasalSGPath.cxx @@ -0,0 +1,115 @@ +// Expose SGPath module to Nasal +// +// Copyright (C) 2013 The FlightGear Community +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "NasalSGPath.hxx" +#include
+#include
+#include + +#include +#include +#include +#include + + +typedef boost::shared_ptr SGPathRef; +typedef nasal::Ghost NasalSGPath; + +SGPath::Permissions checkIORules(const SGPath& path) +{ + SGPath::Permissions perm; + + perm.read = !fgValidatePath(path.str(), false).empty(); + perm.write = !fgValidatePath(path.str(), true ).empty(); + + return perm; +} + +// TODO make exposing such function easier... +/** + * os.path.new() + */ +static naRef f_new_path(const nasal::CallContext& ctx) +{ + return ctx.to_nasal + ( + SGPathRef(new SGPath(ctx.getArg(0), &checkIORules)) + ); +} + +/** + * os.path.desktop() + */ +static naRef f_desktop(const nasal::CallContext& ctx) +{ + return ctx.to_nasal + ( + SGPathRef(new SGPath(SGPath::desktop(SGPath(&checkIORules)))) + ); +} + +//------------------------------------------------------------------------------ +naRef initNasalSGPath(naRef globals, naContext c) +{ + // This wraps most of the SGPath APIs for use by Nasal + // See: http://docs.freeflightsim.org/simgear/classSGPath.html + + NasalSGPath::init("os.path") + .method("set", &SGPath::set) + .method("append", &SGPath::append) + .method("add", &SGPath::add) + .method("concat", &SGPath::concat) + + .member("realpath", &SGPath::realpath) + .member("file", &SGPath::file) + .member("dir", &SGPath::dir) + .member("base", &SGPath::base) + .member("file_base", &SGPath::file_base) + .member("extension", &SGPath::extension) + .member("lower_extension", &SGPath::lower_extension) + .member("complete_lower_extension", &SGPath::complete_lower_extension) + .member("str", &SGPath::str) + .member("str_native", &SGPath::str_native) + .member("mtime", &SGPath::modTime) + + .method("exists", &SGPath::exists) + .method("canRead", &SGPath::canRead) + .method("canWrite", &SGPath::canWrite) + .method("isFile", &SGPath::isFile) + .method("isDir", &SGPath::isDir) + .method("isRelative", &SGPath::isRelative) + .method("isAbsolute", &SGPath::isAbsolute) + .method("isNull", &SGPath::isNull) + + .method("create_dir", &SGPath::create_dir) + .method("remove", &SGPath::remove) + .method("rename", &SGPath::rename); + + nasal::Hash globals_module(globals, c), + path = globals_module.createHash("os") + .createHash("path"); + + path.set("new", f_new_path); + path.set("desktop", &f_desktop); + + return naNil(); +} diff --git a/src/Scripting/NasalSGPath.hxx b/src/Scripting/NasalSGPath.hxx new file mode 100644 index 000000000..8df106e54 --- /dev/null +++ b/src/Scripting/NasalSGPath.hxx @@ -0,0 +1,26 @@ +//@file Expose SGPath module to Nasal +// +// Copyright (C) 2013 The FlightGear Community +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#ifndef SCRIPTING_NASAL_SGPATH_HXX +#define SCRIPTING_NASAL_SGPATH_HXX + +#include + +naRef initNasalSGPath(naRef globals, naContext c); + +#endif // of SCRIPTING_NASAL_SGPATH_HXX diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx index 16a2699da..3b31e0785 100644 --- a/src/Scripting/NasalSys.cxx +++ b/src/Scripting/NasalSys.cxx @@ -33,6 +33,7 @@ #include #include +#include "NasalSGPath.hxx" #include "NasalSys.hxx" #include "NasalSys_private.hxx" #include "NasalModelData.hxx" @@ -776,6 +777,7 @@ void FGNasalSys::init() initNasalCanvas(_globals, _context); initNasalCondition(_globals, _context); initNasalHTTP(_globals, _context); + initNasalSGPath(_globals, _context); NasalTimerObj::init("Timer") .method("start", &TimerObj::start)