]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scripting/NasalSGPath.cxx
Remove debug console output in FGApproachController
[flightgear.git] / src / Scripting / NasalSGPath.cxx
index 51f281902f1f290d3bf7bca4aa78c70b7c11fd61..19a74aff2a0d6c0952498c5dab72ee8e10c7c9e7 100644 (file)
@@ -25,8 +25,9 @@
 #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 <boost/shared_ptr.hpp>
+#include <boost/weak_ptr.hpp>
+
 #include <simgear/nasal/cppbind/NasalHash.hxx>
 #include <simgear/nasal/cppbind/Ghost.hxx>
 
@@ -45,15 +46,24 @@ SGPath::Permissions checkIORules(const SGPath& path)
 }
 
 // TODO make exposing such function easier...
+static naRef validatedPathToNasal( const nasal::CallContext& ctx,
+                                   const SGPath& p )
+{
+  return ctx.to_nasal( SGPathRef(new SGPath(p.str(), &checkIORules)) );
+}
+
 /**
  * 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))
-  );
+  return validatedPathToNasal(ctx, SGPath(ctx.getArg<std::string>(0)));
+}
+
+static int f_path_create_dir(SGPath& p, const nasal::CallContext& ctx)
+{
+  // limit setable access rights for Nasal
+  return p.create_dir(ctx.getArg<mode_t>(0, 0755) & 0775);
 }
 
 /**
@@ -61,10 +71,32 @@ static naRef f_new_path(const nasal::CallContext& ctx)
  */
 static naRef f_desktop(const nasal::CallContext& ctx)
 {
-  return ctx.to_nasal
-  (
-    SGPathRef(new SGPath(SGPath::desktop(SGPath(&checkIORules))))
-  );
+  return validatedPathToNasal(ctx, SGPath::desktop(SGPath(&checkIORules)));
+}
+
+/**
+ * os.path.standardLocation(type)
+ */
+static naRef f_standardLocation(const nasal::CallContext& ctx)
+{
+  const std::string type_str = ctx.requireArg<std::string>(0);
+  SGPath::StandardLocation type = SGPath::HOME;
+  if(      type_str == "DESKTOP" )
+    type = SGPath::DESKTOP;
+  else if( type_str == "DOWNLOADS" )
+    type = SGPath::DOWNLOADS;
+  else if( type_str == "DOCUMENTS" )
+    type = SGPath::DOCUMENTS;
+  else if( type_str == "PICTURES" )
+    type = SGPath::PICTURES;
+  else if( type_str != "HOME" )
+    naRuntimeError
+    (
+      ctx.c,
+      "os.path.standardLocation: unknown type %s", type_str.c_str()
+    );
+
+  return validatedPathToNasal(ctx, SGPath::standardLocation(type));
 }
 
 //------------------------------------------------------------------------------
@@ -100,16 +132,17 @@ naRef initNasalSGPath(naRef globals, naContext c)
     .method("isAbsolute", &SGPath::isAbsolute)
     .method("isNull", &SGPath::isNull)
 
-    .method("create_dir", &SGPath::create_dir)
+    .method("create_dir", &f_path_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);
+  path.set("standardLocation", &f_standardLocation);
 
   return naNil();
 }