File: NasalSGPath.cxx

package info (click to toggle)
flightgear 1%3A2020.3.19%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 51,068 kB
  • sloc: cpp: 245,588; ansic: 181,318; sh: 13,686; perl: 4,475; python: 3,139; xml: 899; asm: 642; makefile: 347; java: 314
file content (137 lines) | stat: -rw-r--r-- 4,390 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Expose SGPath module to Nasal
//
// Copyright (C) 2013 The FlightGear Community
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "NasalSGPath.hxx"
#include <Main/globals.hxx>
#include <simgear/misc/sg_path.hxx>

#include <simgear/nasal/cppbind/NasalHash.hxx>
#include <simgear/nasal/cppbind/Ghost.hxx>

typedef std::shared_ptr<SGPath> SGPathRef;
typedef nasal::Ghost<SGPathRef> NasalSGPath;

// TODO make exposing such function easier...
static naRef validatedPathToNasal( const nasal::CallContext& ctx,
                                   const SGPath& p )
{
    return ctx.to_nasal(SGPathRef(new SGPath(p.utf8Str(),
                                             &SGPath::NasalIORulesChecker)));
}

/**
 * os.path.new()
 */
static naRef f_new_path(const nasal::CallContext& ctx)
{
  return validatedPathToNasal(ctx, SGPath(ctx.getArg<std::string>(0)));
}

static int f_path_create_dir(SGPath& p, const nasal::CallContext& ctx)
{
  // limit setable access rights for Nasal
  return p.create_dir(ctx.getArg<mode_t>(0, 0755) & 0775);
}

static void f_path_set(SGPath& p, const nasal::CallContext& ctx)
{
    p = SGPath::fromUtf8(ctx.getArg<std::string>(0), p.getPermissionChecker());
}

/**
 * os.path.desktop()
 */
static naRef f_desktop(const nasal::CallContext& ctx)
{
    return validatedPathToNasal(
        ctx, SGPath::desktop(SGPath(&SGPath::NasalIORulesChecker)));
}

/**
 * os.path.standardLocation(type)
 */
static naRef f_standardLocation(const nasal::CallContext& ctx)
{
  const std::string type_str = ctx.requireArg<std::string>(0);
  SGPath::StandardLocation type = SGPath::HOME;
  if(      type_str == "DESKTOP" )
    type = SGPath::DESKTOP;
  else if( type_str == "DOWNLOADS" )
    type = SGPath::DOWNLOADS;
  else if( type_str == "DOCUMENTS" )
    type = SGPath::DOCUMENTS;
  else if( type_str == "PICTURES" )
    type = SGPath::PICTURES;
  else if( type_str != "HOME" )
    ctx.runtimeError
    (
      "os.path.standardLocation: unknown type %s", type_str.c_str()
    );

  return validatedPathToNasal(ctx, SGPath::standardLocation(type));
}

//------------------------------------------------------------------------------
naRef initNasalSGPath(naRef globals, naContext c)
{
  // This wraps most of the SGPath APIs for use by Nasal
  // See: http://docs.freeflightsim.org/simgear/classSGPath.html

  NasalSGPath::init("os.path")
    .method("set", &f_path_set)
    .method("append", &SGPath::append)
    .method("concat", &SGPath::concat)

    .member("realpath", &SGPath::realpath)
    .member("file", &SGPath::file)
    .member("dir", &SGPath::dir)
    .member("base", &SGPath::base)
    .member("file_base", &SGPath::file_base)
    .member("extension", &SGPath::extension)
    .member("lower_extension", &SGPath::lower_extension)
    .member("complete_lower_extension", &SGPath::complete_lower_extension)
    .member("str", &SGPath::utf8Str)
    .member("mtime", &SGPath::modTime)

    .method("exists", &SGPath::exists)
    .method("canRead", &SGPath::canRead)
    .method("canWrite", &SGPath::canWrite)
    .method("isFile", &SGPath::isFile)
    .method("isDir", &SGPath::isDir)
    .method("isRelative", &SGPath::isRelative)
    .method("isAbsolute", &SGPath::isAbsolute)
    .method("isNull", &SGPath::isNull)

    .method("create_dir", &f_path_create_dir)
    .method("remove", &SGPath::remove)
    .method("rename", &SGPath::rename);

  nasal::Hash globals_module(globals, c),
              path = globals_module.createHash("os")
                                   .createHash("path");

  path.set("new", f_new_path);
  path.set("desktop", &f_desktop);
  path.set("standardLocation", &f_standardLocation);

  return naNil();
}