]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalSGPath.cxx
Update includes (required by simgear changes)
[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 <boost/shared_ptr.hpp>
29 #include <boost/weak_ptr.hpp>
30
31 #include <simgear/nasal/cppbind/NasalHash.hxx>
32 #include <simgear/nasal/cppbind/Ghost.hxx>
33
34
35 typedef boost::shared_ptr<SGPath> SGPathRef;
36 typedef nasal::Ghost<SGPathRef> NasalSGPath;
37
38 SGPath::Permissions checkIORules(const SGPath& path)
39 {
40   SGPath::Permissions perm;
41
42   perm.read  = !fgValidatePath(path.str(), false).empty();
43   perm.write = !fgValidatePath(path.str(), true ).empty();
44
45   return perm;
46 }
47
48 // TODO make exposing such function easier...
49 static naRef validatedPathToNasal( const nasal::CallContext& ctx,
50                                    const SGPath& p )
51 {
52   return ctx.to_nasal( SGPathRef(new SGPath(p.str(), &checkIORules)) );
53 }
54
55 /**
56  * os.path.new()
57  */
58 static naRef f_new_path(const nasal::CallContext& ctx)
59 {
60   return validatedPathToNasal(ctx, SGPath(ctx.getArg<std::string>(0)));
61 }
62
63 /**
64  * os.path.desktop()
65  */
66 static naRef f_desktop(const nasal::CallContext& ctx)
67 {
68   return validatedPathToNasal(ctx, SGPath::desktop(SGPath(&checkIORules)));
69 }
70
71 /**
72  * os.path.standardLocation(type)
73  */
74 static naRef f_standardLocation(const nasal::CallContext& ctx)
75 {
76   const std::string type_str = ctx.requireArg<std::string>(0);
77   SGPath::StandardLocation type = SGPath::HOME;
78   if(      type_str == "DESKTOP" )
79     type = SGPath::DESKTOP;
80   else if( type_str == "DOWNLOADS" )
81     type = SGPath::DOWNLOADS;
82   else if( type_str == "DOCUMENTS" )
83     type = SGPath::DOCUMENTS;
84   else if( type_str == "PICTURES" )
85     type = SGPath::PICTURES;
86   else if( type_str != "HOME" )
87     naRuntimeError
88     (
89       ctx.c,
90       "os.path.standardLocation: unknown type %s", type_str.c_str()
91     );
92
93   return validatedPathToNasal(ctx, SGPath::standardLocation(type));
94 }
95
96 //------------------------------------------------------------------------------
97 naRef initNasalSGPath(naRef globals, naContext c)
98 {
99   // This wraps most of the SGPath APIs for use by Nasal
100   // See: http://docs.freeflightsim.org/simgear/classSGPath.html
101
102   NasalSGPath::init("os.path")
103     .method("set", &SGPath::set)
104     .method("append", &SGPath::append)
105     .method("add", &SGPath::add)
106     .method("concat", &SGPath::concat)
107
108     .member("realpath", &SGPath::realpath)
109     .member("file", &SGPath::file)
110     .member("dir", &SGPath::dir)
111     .member("base", &SGPath::base)
112     .member("file_base", &SGPath::file_base)
113     .member("extension", &SGPath::extension)
114     .member("lower_extension", &SGPath::lower_extension)
115     .member("complete_lower_extension", &SGPath::complete_lower_extension)
116     .member("str", &SGPath::str)
117     .member("str_native", &SGPath::str_native)
118     .member("mtime", &SGPath::modTime)
119
120     .method("exists", &SGPath::exists)
121     .method("canRead", &SGPath::canRead)
122     .method("canWrite", &SGPath::canWrite)
123     .method("isFile", &SGPath::isFile)
124     .method("isDir", &SGPath::isDir)
125     .method("isRelative", &SGPath::isRelative)
126     .method("isAbsolute", &SGPath::isAbsolute)
127     .method("isNull", &SGPath::isNull)
128
129     .method("create_dir", &SGPath::create_dir)
130     .method("remove", &SGPath::remove)
131     .method("rename", &SGPath::rename);
132  
133   nasal::Hash globals_module(globals, c),
134               path = globals_module.createHash("os")
135                                    .createHash("path");
136
137   path.set("new", f_new_path);
138   path.set("desktop", &f_desktop);
139   path.set("standardLocation", &f_standardLocation);
140
141   return naNil();
142 }