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;
42 perm.read = !fgValidatePath(path.str(), false).empty();
43 perm.write = !fgValidatePath(path.str(), true ).empty();
48 // TODO make exposing such function easier...
49 static naRef validatedPathToNasal( const nasal::CallContext& ctx,
52 return ctx.to_nasal( SGPathRef(new SGPath(p.str(), &checkIORules)) );
58 static naRef f_new_path(const nasal::CallContext& ctx)
60 return validatedPathToNasal(ctx, SGPath(ctx.getArg<std::string>(0)));
63 static int f_path_create_dir(SGPath& p, const nasal::CallContext& ctx)
65 // limit setable access rights for Nasal
66 return p.create_dir(ctx.getArg<mode_t>(0, 0755) & 0775);
72 static naRef f_desktop(const nasal::CallContext& ctx)
74 return validatedPathToNasal(ctx, SGPath::desktop(SGPath(&checkIORules)));
78 * os.path.standardLocation(type)
80 static naRef f_standardLocation(const nasal::CallContext& ctx)
82 const std::string type_str = ctx.requireArg<std::string>(0);
83 SGPath::StandardLocation type = SGPath::HOME;
84 if( type_str == "DESKTOP" )
85 type = SGPath::DESKTOP;
86 else if( type_str == "DOWNLOADS" )
87 type = SGPath::DOWNLOADS;
88 else if( type_str == "DOCUMENTS" )
89 type = SGPath::DOCUMENTS;
90 else if( type_str == "PICTURES" )
91 type = SGPath::PICTURES;
92 else if( type_str != "HOME" )
96 "os.path.standardLocation: unknown type %s", type_str.c_str()
99 return validatedPathToNasal(ctx, SGPath::standardLocation(type));
102 //------------------------------------------------------------------------------
103 naRef initNasalSGPath(naRef globals, naContext c)
105 // This wraps most of the SGPath APIs for use by Nasal
106 // See: http://docs.freeflightsim.org/simgear/classSGPath.html
108 NasalSGPath::init("os.path")
109 .method("set", &SGPath::set)
110 .method("append", &SGPath::append)
111 .method("add", &SGPath::add)
112 .method("concat", &SGPath::concat)
114 .member("realpath", &SGPath::realpath)
115 .member("file", &SGPath::file)
116 .member("dir", &SGPath::dir)
117 .member("base", &SGPath::base)
118 .member("file_base", &SGPath::file_base)
119 .member("extension", &SGPath::extension)
120 .member("lower_extension", &SGPath::lower_extension)
121 .member("complete_lower_extension", &SGPath::complete_lower_extension)
122 .member("str", &SGPath::str)
123 .member("str_native", &SGPath::str_native)
124 .member("mtime", &SGPath::modTime)
126 .method("exists", &SGPath::exists)
127 .method("canRead", &SGPath::canRead)
128 .method("canWrite", &SGPath::canWrite)
129 .method("isFile", &SGPath::isFile)
130 .method("isDir", &SGPath::isDir)
131 .method("isRelative", &SGPath::isRelative)
132 .method("isAbsolute", &SGPath::isAbsolute)
133 .method("isNull", &SGPath::isNull)
135 .method("create_dir", &f_path_create_dir)
136 .method("remove", &SGPath::remove)
137 .method("rename", &SGPath::rename);
139 nasal::Hash globals_module(globals, c),
140 path = globals_module.createHash("os")
143 path.set("new", f_new_path);
144 path.set("desktop", &f_desktop);
145 path.set("standardLocation", &f_standardLocation);