]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalSGPath.cxx
Expose SGPath to Nasal
[flightgear.git] / src / Scripting / NasalSGPath.cxx
1 // Expose SGPath module to Nasal
2 //
3 // Copyright (C) 2013 The FlightGear Community
4 //
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.
9 //
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.
14 //
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.
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #endif
22
23 #include "NasalSGPath.hxx"
24 #include <Main/globals.hxx>
25 #include <Main/util.hxx>
26 #include <simgear/misc/sg_path.hxx>
27
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>
32
33
34 typedef boost::shared_ptr<SGPath> SGPathRef;
35 typedef nasal::Ghost<SGPathRef> NasalSGPath;
36
37 SGPath::Permissions checkIORules(const SGPath& path)
38 {
39   SGPath::Permissions perm;
40
41   perm.read  = !fgValidatePath(path.str(), false).empty();
42   perm.write = !fgValidatePath(path.str(), true ).empty();
43
44   return perm;
45 }
46
47 // TODO make exposing such function easier...
48 /**
49  * os.path.new()
50  */
51 static naRef f_new_path(const nasal::CallContext& ctx)
52 {
53   return ctx.to_nasal
54   (
55     SGPathRef(new SGPath(ctx.getArg<std::string>(0), &checkIORules))
56   );
57 }
58
59 /**
60  * os.path.desktop()
61  */
62 static naRef f_desktop(const nasal::CallContext& ctx)
63 {
64   return ctx.to_nasal
65   (
66     SGPathRef(new SGPath(SGPath::desktop(SGPath(&checkIORules))))
67   );
68 }
69
70 //------------------------------------------------------------------------------
71 naRef initNasalSGPath(naRef globals, naContext c)
72 {
73   // This wraps most of the SGPath APIs for use by Nasal
74   // See: http://docs.freeflightsim.org/simgear/classSGPath.html
75
76   NasalSGPath::init("os.path")
77     .method("set", &SGPath::set)
78     .method("append", &SGPath::append)
79     .method("add", &SGPath::add)
80     .method("concat", &SGPath::concat)
81
82     .member("realpath", &SGPath::realpath)
83     .member("file", &SGPath::file)
84     .member("dir", &SGPath::dir)
85     .member("base", &SGPath::base)
86     .member("file_base", &SGPath::file_base)
87     .member("extension", &SGPath::extension)
88     .member("lower_extension", &SGPath::lower_extension)
89     .member("complete_lower_extension", &SGPath::complete_lower_extension)
90     .member("str", &SGPath::str)
91     .member("str_native", &SGPath::str_native)
92     .member("mtime", &SGPath::modTime)
93
94     .method("exists", &SGPath::exists)
95     .method("canRead", &SGPath::canRead)
96     .method("canWrite", &SGPath::canWrite)
97     .method("isFile", &SGPath::isFile)
98     .method("isDir", &SGPath::isDir)
99     .method("isRelative", &SGPath::isRelative)
100     .method("isAbsolute", &SGPath::isAbsolute)
101     .method("isNull", &SGPath::isNull)
102
103     .method("create_dir", &SGPath::create_dir)
104     .method("remove", &SGPath::remove)
105     .method("rename", &SGPath::rename);
106  
107   nasal::Hash globals_module(globals, c),
108               path = globals_module.createHash("os")
109                                    .createHash("path");
110
111   path.set("new", f_new_path);
112   path.set("desktop", &f_desktop);
113
114   return naNil();
115 }