FlightGear next
NasalSGPath.cxx
Go to the documentation of this file.
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 <simgear/misc/sg_path.hxx>
26
27#include <simgear/nasal/cppbind/NasalHash.hxx>
28#include <simgear/nasal/cppbind/Ghost.hxx>
29
30typedef std::shared_ptr<SGPath> SGPathRef;
31typedef nasal::Ghost<SGPathRef> NasalSGPath;
32
33// TODO make exposing such function easier...
34static naRef validatedPathToNasal( const nasal::CallContext& ctx,
35 const SGPath& p )
36{
37 return ctx.to_nasal(SGPathRef(new SGPath(p.utf8Str(),
38 &SGPath::NasalIORulesChecker)));
39}
40
44static naRef f_new_path(const nasal::CallContext& ctx)
45{
46 return validatedPathToNasal(ctx, SGPath(ctx.getArg<std::string>(0)));
47}
48
49static int f_path_create_dir(SGPath& p, const nasal::CallContext& ctx)
50{
51 // limit setable access rights for Nasal
52 return p.create_dir(ctx.getArg<mode_t>(0, 0755) & 0775);
53}
54
55static void f_path_set(SGPath& p, const nasal::CallContext& ctx)
56{
57 p = SGPath::fromUtf8(ctx.getArg<std::string>(0), p.getPermissionChecker());
58}
59
63static naRef f_desktop(const nasal::CallContext& ctx)
64{
66 ctx, SGPath::desktop(SGPath(&SGPath::NasalIORulesChecker)));
67}
68
72static naRef f_standardLocation(const nasal::CallContext& ctx)
73{
74 const std::string type_str = ctx.requireArg<std::string>(0);
75 SGPath::StandardLocation type = SGPath::HOME;
76 if( type_str == "DESKTOP" )
77 type = SGPath::DESKTOP;
78 else if( type_str == "DOWNLOADS" )
79 type = SGPath::DOWNLOADS;
80 else if( type_str == "DOCUMENTS" )
81 type = SGPath::DOCUMENTS;
82 else if( type_str == "PICTURES" )
83 type = SGPath::PICTURES;
84 else if( type_str != "HOME" )
85 ctx.runtimeError
86 (
87 "os.path.standardLocation: unknown type %s", type_str.c_str()
88 );
89
90 return validatedPathToNasal(ctx, SGPath::standardLocation(type));
91}
92
93//------------------------------------------------------------------------------
94naRef initNasalSGPath(naRef globals, naContext c)
95{
96 // This wraps most of the SGPath APIs for use by Nasal
97 // See: http://docs.freeflightsim.org/simgear/classSGPath.html
98
99 NasalSGPath::init("os.path")
100 .method("set", &f_path_set)
101 .method("append", &SGPath::append)
102 .method("concat", &SGPath::concat)
103
104 .member("realpath", &SGPath::realpath)
105 .member("file", &SGPath::file)
106 .member("dir", &SGPath::dir)
107 .member("base", &SGPath::base)
108 .member("file_base", &SGPath::file_base)
109 .member("extension", &SGPath::extension)
110 .member("lower_extension", &SGPath::lower_extension)
111 .member("complete_lower_extension", &SGPath::complete_lower_extension)
112 .member("str", &SGPath::utf8Str)
113 .member("mtime", &SGPath::modTime)
114
115 .method("exists", &SGPath::exists)
116 .method("canRead", &SGPath::canRead)
117 .method("canWrite", &SGPath::canWrite)
118 .method("isFile", &SGPath::isFile)
119 .method("isDir", &SGPath::isDir)
120 .method("isRelative", &SGPath::isRelative)
121 .method("isAbsolute", &SGPath::isAbsolute)
122 .method("isNull", &SGPath::isNull)
123
124 .method("create_dir", &f_path_create_dir)
125 .method("remove", &SGPath::remove)
126 .method("rename", &SGPath::rename);
127
128 nasal::Hash globals_module(globals, c),
129 path = globals_module.createHash("os")
130 .createHash("path");
131
132 path.set("new", f_new_path);
133 path.set("desktop", &f_desktop);
134 path.set("standardLocation", &f_standardLocation);
135
136 return naNil();
137}
#define p(x)
static naRef f_new_path(const nasal::CallContext &ctx)
os.path.new()
static naRef validatedPathToNasal(const nasal::CallContext &ctx, const SGPath &p)
static void f_path_set(SGPath &p, const nasal::CallContext &ctx)
naRef initNasalSGPath(naRef globals, naContext c)
static naRef f_standardLocation(const nasal::CallContext &ctx)
os.path.standardLocation(type)
static int f_path_create_dir(SGPath &p, const nasal::CallContext &ctx)
nasal::Ghost< SGPathRef > NasalSGPath
std::shared_ptr< SGPath > SGPathRef
static naRef f_desktop(const nasal::CallContext &ctx)
os.path.desktop()
FGGlobals * globals
Definition globals.cxx:142