]> git.mxchange.org Git - flightgear.git/commitdiff
Expose SGPath to Nasal
authorThomas Geymayer <tomgey@gmail.com>
Tue, 17 Dec 2013 18:07:08 +0000 (19:07 +0100)
committerThomas Geymayer <tomgey@gmail.com>
Wed, 18 Dec 2013 09:30:19 +0000 (10:30 +0100)
src/Scripting/CMakeLists.txt
src/Scripting/NasalSGPath.cxx [new file with mode: 0644]
src/Scripting/NasalSGPath.hxx [new file with mode: 0644]
src/Scripting/NasalSys.cxx

index 44024e55c654cd9c1162df0447866ba2e84105b0..413a137a759c411e9ee2df2d7cc0d26a40541e26 100644 (file)
@@ -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 (file)
index 0000000..51f2819
--- /dev/null
@@ -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 <Main/globals.hxx>
+#include <Main/util.hxx>
+#include <simgear/misc/sg_path.hxx>
+
+#include <simgear/nasal/cppbind/from_nasal.hxx>
+#include <simgear/nasal/cppbind/to_nasal.hxx>
+#include <simgear/nasal/cppbind/NasalHash.hxx>
+#include <simgear/nasal/cppbind/Ghost.hxx>
+
+
+typedef boost::shared_ptr<SGPath> SGPathRef;
+typedef nasal::Ghost<SGPathRef> 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<std::string>(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 (file)
index 0000000..8df106e
--- /dev/null
@@ -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 <simgear/nasal/nasal.h>
+
+naRef initNasalSGPath(naRef globals, naContext c);
+
+#endif // of SCRIPTING_NASAL_SGPATH_HXX
index 16a2699da197bacf31c076ab122c83cf3880d5f1..3b31e07859c1fee0b7e10c8244c4e8df7f0a0395 100644 (file)
@@ -33,6 +33,7 @@
 #include <simgear/nasal/cppbind/Ghost.hxx>
 #include <simgear/nasal/cppbind/NasalHash.hxx>
 
+#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)