| 1 | // Expose SGPath module to Nasal |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | // SPDX-FileCopyrightText: Copyright (C) 2013 James Turner - [email protected] |
| 4 | |
| 5 | #include "config.h" |
| 6 | |
| 7 | #include "simgear/structure/exception.hxx" |
| 8 | |
| 9 | #include "NasalSGPath.hxx" |
| 10 | #include <Main/globals.hxx> |
| 11 | #include <simgear/misc/sg_path.hxx> |
| 12 | |
| 13 | #include <simgear/nasal/cppbind/Ghost.hxx> |
| 14 | #include <simgear/nasal/cppbind/NasalHash.hxx> |
| 15 | |
| 16 | typedef std::shared_ptr<SGPath> SGPathRef; |
| 17 | typedef nasal::Ghost<SGPathRef> NasalSGPath; |
| 18 | |
| 19 | // TODO make exposing such function easier... |
| 20 | static naRef validatedPathToNasal(const nasal::CallContext& ctx, |
| 21 | const SGPath& p) |
| 22 | { |
| 23 | return ctx.to_nasal(SGPathRef(new SGPath(p.utf8Str(), |
| 24 | &SGPath::NasalIORulesChecker))); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * os.path.new() |
| 29 | */ |
| 30 | static naRef f_new_path(const nasal::CallContext& ctx) |
| 31 | { |
| 32 | return validatedPathToNasal(ctx, SGPath(ctx.getArg<std::string>(0))); |
| 33 | } |
| 34 | |
| 35 | static int f_path_create_dir(SGPath& p, const nasal::CallContext& ctx) |
| 36 | { |
| 37 | // limit setable access rights for Nasal |
| 38 | return p.create_dir(ctx.getArg<mode_t>(0, 0755) & 0775); |
| 39 | } |
| 40 | |
| 41 | static void f_path_set(SGPath& p, const nasal::CallContext& ctx) |
| 42 | { |
| 43 | p = SGPath::fromUtf8(ctx.getArg<std::string>(0), p.getPermissionChecker()); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * os.path.desktop() |
| 48 | */ |
| 49 | static naRef f_desktop(const nasal::CallContext& ctx) |
| 50 | { |
| 51 | return validatedPathToNasal( |
| 52 | ctx, SGPath::desktop(SGPath(&SGPath::NasalIORulesChecker))); |
| 53 | } |
| 54 | |
| 55 | SGPath::StandardLocation standardLocationFromString(const std::string& s) |
| 56 | { |
| 57 | if (s == "DESKTOP") return SGPath::DESKTOP; |
| 58 | if (s == "DOWNLOADS") return SGPath::DOWNLOADS; |
| 59 | if (s == "DOCUMENTS") return SGPath::DOCUMENTS; |
| 60 | if (s == "PICTURES") return SGPath::PICTURES; |
| 61 | if (s == "HOME") return SGPath::HOME; |
| 62 | |
| 63 | throw sg_range_exception("Unrecognized standard location: '" + s + "'"); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * os.path.standardLocation(type) |
| 68 | */ |
| 69 | static naRef f_standardLocation(const nasal::CallContext& ctx) |
| 70 | { |
| 71 | const std::string type_str = ctx.requireArg<std::string>(0); |
| 72 | try { |
| 73 | SGPath::StandardLocation type = standardLocationFromString(type_str); |
| 74 | return validatedPathToNasal(ctx, SGPath::standardLocation(type)); |
| 75 | } catch (sg_exception&) { |
| 76 | ctx.runtimeError( |
| 77 | "os.path.standardLocation: unknown type %s", type_str.c_str()); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | //------------------------------------------------------------------------------ |
| 82 | naRef initNasalSGPath(naRef globals, naContext c) |
| 83 | { |
| 84 | // This wraps most of the SGPath APIs for use by Nasal |
| 85 | // See: http://docs.freeflightsim.org/simgear/classSGPath.html |
| 86 | |
| 87 | NasalSGPath::init("os.path") |
| 88 | .method("set", &f_path_set) |
| 89 | .method("append", &SGPath::append) |
| 90 | .method("concat", &SGPath::concat) |
| 91 | |
| 92 | .member("realpath", &SGPath::realpath) |
| 93 | .member("file", &SGPath::file) |
| 94 | .member("dir", &SGPath::dir) |
| 95 | .member("base", &SGPath::base) |
| 96 | .member("file_base", &SGPath::file_base) |
| 97 | .member("extension", &SGPath::extension) |
| 98 | .member("lower_extension", &SGPath::lower_extension) |
| 99 | .member("complete_lower_extension", &SGPath::complete_lower_extension) |
| 100 | .member("str", &SGPath::utf8Str) |
| 101 | .member("mtime", &SGPath::modTime) |
| 102 | |
| 103 | .method("exists", &SGPath::exists) |
| 104 | .method("canRead", &SGPath::canRead) |
| 105 | .method("canWrite", &SGPath::canWrite) |
| 106 | .method("isFile", &SGPath::isFile) |
| 107 | .method("isDir", &SGPath::isDir) |
| 108 | .method("isRelative", &SGPath::isRelative) |
| 109 | .method("isAbsolute", &SGPath::isAbsolute) |
| 110 | .method("isNull", &SGPath::isNull) |
| 111 | |
| 112 | .method("create_dir", &f_path_create_dir) |
| 113 | .method("remove", &SGPath::remove) |
| 114 | .method("rename", &SGPath::rename); |
| 115 | |
| 116 | nasal::Hash globals_module(globals, c), |
| 117 | path = globals_module.createHash("os") |
| 118 | .createHash("path"); |
| 119 | |
| 120 | path.set("new", f_new_path); |
| 121 | path.set("desktop", &f_desktop); |
| 122 | path.set("standardLocation", &f_standardLocation); |
| 123 | |
| 124 | return naNil(); |
| 125 | } |