1 // Expose SGPath module to Nasal
3 // Copyright (C) 2013 The FlightGear Community
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "NasalSGPath.hxx"
24 #include <Main/globals.hxx>
25 #include <Main/util.hxx>
26 #include <simgear/misc/sg_path.hxx>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/weak_ptr.hpp>
31 #include <simgear/nasal/cppbind/NasalHash.hxx>
32 #include <simgear/nasal/cppbind/Ghost.hxx>
35 typedef boost::shared_ptr<SGPath> SGPathRef;
36 typedef nasal::Ghost<SGPathRef> NasalSGPath;
38 SGPath::Permissions checkIORules(const SGPath& path)
40 SGPath::Permissions perm;
41 if (!path.isAbsolute()) {
42 // SGPath caches permissions, which breaks for relative paths
43 // if the current directory changes
44 SG_LOG(SG_NASAL, SG_ALERT, "os.path: file operation on '" <<
45 path.str() << "' access denied (relative paths not accepted; use "
46 "realpath() to make a path absolute)");
49 perm.read = path.isAbsolute() && !fgValidatePath(path.str(), false).empty();
50 perm.write = path.isAbsolute() && !fgValidatePath(path.str(), true ).empty();
55 // TODO make exposing such function easier...
56 static naRef validatedPathToNasal( const nasal::CallContext& ctx,
59 return ctx.to_nasal( SGPathRef(new SGPath(p.str(), &checkIORules)) );
65 static naRef f_new_path(const nasal::CallContext& ctx)
67 return validatedPathToNasal(ctx, SGPath(ctx.getArg<std::string>(0)));
70 static int f_path_create_dir(SGPath& p, const nasal::CallContext& ctx)
72 // limit setable access rights for Nasal
73 return p.create_dir(ctx.getArg<mode_t>(0, 0755) & 0775);
79 static naRef f_desktop(const nasal::CallContext& ctx)
81 return validatedPathToNasal(ctx, SGPath::desktop(SGPath(&checkIORules)));
85 * os.path.standardLocation(type)
87 static naRef f_standardLocation(const nasal::CallContext& ctx)
89 const std::string type_str = ctx.requireArg<std::string>(0);
90 SGPath::StandardLocation type = SGPath::HOME;
91 if( type_str == "DESKTOP" )
92 type = SGPath::DESKTOP;
93 else if( type_str == "DOWNLOADS" )
94 type = SGPath::DOWNLOADS;
95 else if( type_str == "DOCUMENTS" )
96 type = SGPath::DOCUMENTS;
97 else if( type_str == "PICTURES" )
98 type = SGPath::PICTURES;
99 else if( type_str != "HOME" )
103 "os.path.standardLocation: unknown type %s", type_str.c_str()
106 return validatedPathToNasal(ctx, SGPath::standardLocation(type));
109 //------------------------------------------------------------------------------
110 naRef initNasalSGPath(naRef globals, naContext c)
112 // This wraps most of the SGPath APIs for use by Nasal
113 // See: http://docs.freeflightsim.org/simgear/classSGPath.html
115 NasalSGPath::init("os.path")
116 .method("set", &SGPath::set)
117 .method("append", &SGPath::append)
118 .method("add", &SGPath::add)
119 .method("concat", &SGPath::concat)
121 .member("realpath", &SGPath::realpath)
122 .member("file", &SGPath::file)
123 .member("dir", &SGPath::dir)
124 .member("base", &SGPath::base)
125 .member("file_base", &SGPath::file_base)
126 .member("extension", &SGPath::extension)
127 .member("lower_extension", &SGPath::lower_extension)
128 .member("complete_lower_extension", &SGPath::complete_lower_extension)
129 .member("str", &SGPath::str)
130 .member("str_native", &SGPath::str_native)
131 .member("mtime", &SGPath::modTime)
133 .method("exists", &SGPath::exists)
134 .method("canRead", &SGPath::canRead)
135 .method("canWrite", &SGPath::canWrite)
136 .method("isFile", &SGPath::isFile)
137 .method("isDir", &SGPath::isDir)
138 .method("isRelative", &SGPath::isRelative)
139 .method("isAbsolute", &SGPath::isAbsolute)
140 .method("isNull", &SGPath::isNull)
142 .method("create_dir", &f_path_create_dir)
143 .method("remove", &SGPath::remove)
144 .method("rename", &SGPath::rename);
146 nasal::Hash globals_module(globals, c),
147 path = globals_module.createHash("os")
150 path.set("new", f_new_path);
151 path.set("desktop", &f_desktop);
152 path.set("standardLocation", &f_standardLocation);