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 <simgear/nasal/cppbind/from_nasal.hxx>
29 #include <simgear/nasal/cppbind/to_nasal.hxx>
30 #include <simgear/nasal/cppbind/NasalHash.hxx>
31 #include <simgear/nasal/cppbind/Ghost.hxx>
34 typedef boost::shared_ptr<SGPath> SGPathRef;
35 typedef nasal::Ghost<SGPathRef> NasalSGPath;
37 SGPath::Permissions checkIORules(const SGPath& path)
39 SGPath::Permissions perm;
41 perm.read = !fgValidatePath(path.str(), false).empty();
42 perm.write = !fgValidatePath(path.str(), true ).empty();
47 // TODO make exposing such function easier...
48 static naRef validatedPathToNasal( const nasal::CallContext& ctx,
51 return ctx.to_nasal( SGPathRef(new SGPath(p.str(), &checkIORules)) );
57 static naRef f_new_path(const nasal::CallContext& ctx)
59 return validatedPathToNasal(ctx, SGPath(ctx.getArg<std::string>(0)));
65 static naRef f_desktop(const nasal::CallContext& ctx)
67 return validatedPathToNasal(ctx, SGPath::desktop(SGPath(&checkIORules)));
71 * os.path.standardLocation(type)
73 static naRef f_standardLocation(const nasal::CallContext& ctx)
75 const std::string type_str = ctx.requireArg<std::string>(0);
76 SGPath::StandardLocation type = SGPath::HOME;
77 if( type_str == "DESKTOP" )
78 type = SGPath::DESKTOP;
79 else if( type_str == "DOWNLOADS" )
80 type = SGPath::DOWNLOADS;
81 else if( type_str == "DOCUMENTS" )
82 type = SGPath::DOCUMENTS;
83 else if( type_str == "PICTURES" )
84 type = SGPath::PICTURES;
85 else if( type_str != "HOME" )
89 "os.path.standardLocation: unknown type %s", type_str.c_str()
92 return validatedPathToNasal(ctx, SGPath::standardLocation(type));
95 //------------------------------------------------------------------------------
96 naRef initNasalSGPath(naRef globals, naContext c)
98 // This wraps most of the SGPath APIs for use by Nasal
99 // See: http://docs.freeflightsim.org/simgear/classSGPath.html
101 NasalSGPath::init("os.path")
102 .method("set", &SGPath::set)
103 .method("append", &SGPath::append)
104 .method("add", &SGPath::add)
105 .method("concat", &SGPath::concat)
107 .member("realpath", &SGPath::realpath)
108 .member("file", &SGPath::file)
109 .member("dir", &SGPath::dir)
110 .member("base", &SGPath::base)
111 .member("file_base", &SGPath::file_base)
112 .member("extension", &SGPath::extension)
113 .member("lower_extension", &SGPath::lower_extension)
114 .member("complete_lower_extension", &SGPath::complete_lower_extension)
115 .member("str", &SGPath::str)
116 .member("str_native", &SGPath::str_native)
117 .member("mtime", &SGPath::modTime)
119 .method("exists", &SGPath::exists)
120 .method("canRead", &SGPath::canRead)
121 .method("canWrite", &SGPath::canWrite)
122 .method("isFile", &SGPath::isFile)
123 .method("isDir", &SGPath::isDir)
124 .method("isRelative", &SGPath::isRelative)
125 .method("isAbsolute", &SGPath::isAbsolute)
126 .method("isNull", &SGPath::isNull)
128 .method("create_dir", &SGPath::create_dir)
129 .method("remove", &SGPath::remove)
130 .method("rename", &SGPath::rename);
132 nasal::Hash globals_module(globals, c),
133 path = globals_module.createHash("os")
136 path.set("new", f_new_path);
137 path.set("desktop", &f_desktop);
138 path.set("standardLocation", &f_standardLocation);