irccd  3.0.3
file_api.hpp
1 /*
2  * file_api.hpp -- Irccd.File API
3  *
4  * Copyright (c) 2013-2019 David Demelier <markand@malikania.fr>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef IRCCD_JS_FILE_API_HPP
20 #define IRCCD_JS_FILE_API_HPP
21 
27 #include <irccd/sysconfig.hpp>
28 
29 #if defined(IRCCD_HAVE_STAT)
30 # include <sys/types.h>
31 # include <sys/stat.h>
32 #endif
33 
34 #include <cassert>
35 #include <cerrno>
36 #include <cstdio>
37 #include <cstring>
38 #include <functional>
39 #include <stdexcept>
40 #include <string>
41 
42 #include "api.hpp"
43 
44 namespace irccd::js {
45 
56 class file {
57 private:
58  file(const file&) = delete;
59  file& operator=(const file&) = delete;
60 
61  file(file&&) = delete;
62  file& operator=(file&&) = delete;
63 
64 private:
65  std::string path_;
66  std::FILE* stream_;
67  std::function<void (std::FILE*)> destructor_;
68 
69 public:
77  inline file(std::string path, const std::string& mode)
78  : path_(std::move(path))
79  , destructor_([] (std::FILE* fp) { std::fclose(fp); })
80  {
81  if ((stream_ = std::fopen(path_.c_str(), mode.c_str())) == nullptr)
82  throw std::runtime_error(std::strerror(errno));
83  }
84 
94  inline file(std::FILE* fp, std::function<void (std::FILE*)> destructor) noexcept
95  : stream_(fp)
96  , destructor_(std::move(destructor))
97  {
98  assert(destructor_ != nullptr);
99  }
100 
104  virtual ~file() noexcept
105  {
106  close();
107  }
108 
115  inline const std::string& get_path() const noexcept
116  {
117  return path_;
118  }
119 
125  inline std::FILE* get_handle() noexcept
126  {
127  return stream_;
128  }
129 
133  inline void close() noexcept
134  {
135  if (stream_) {
136  destructor_(stream_);
137  stream_ = nullptr;
138  }
139  }
140 };
141 
146 class file_api : public api {
147 public:
151  auto get_name() const noexcept -> std::string_view override;
152 
156  void load(daemon::bot& bot, js::plugin& plugin) override;
157 };
158 
159 namespace duk {
160 
166 template <>
167 struct type_traits<std::shared_ptr<file>> {
175  static void push(duk_context* ctx, std::shared_ptr<file> fp);
176 
184  static auto require(duk_context* ctx, duk_idx_t index) -> std::shared_ptr<file>;
185 };
186 
187 #if defined(IRCCD_HAVE_STAT)
188 
194 template <>
195 struct type_traits<struct stat> {
202  static void push(duk_context* ctx, const struct stat& st);
203 };
204 
205 #endif // !IRCCD_HAVE_STAT
206 
207 } // !duk
208 
209 } // !irccd::js
210 
211 #endif // !IRCCD_JS_FILE_API_HPP
irccd::js::duk::push
auto push(duk_context *ctx, T &&value) -> int
Definition: duk.hpp:270
irccd::js::file::~file
virtual ~file() noexcept
Definition: file_api.hpp:104
irccd::js::file::close
void close() noexcept
Definition: file_api.hpp:133
irccd::js::file_api::load
void load(daemon::bot &bot, js::plugin &plugin) override
irccd::js::file::file
file(std::FILE *fp, std::function< void(std::FILE *)> destructor) noexcept
Definition: file_api.hpp:94
irccd::js
Javascript namespace.
Definition: api.hpp:42
irccd::js::file_api
Irccd.File Javascript API.
Definition: file_api.hpp:146
irccd::js::plugin
Javascript plugins for irccd.
Definition: plugin.hpp:44
irccd::js::file::get_handle
std::FILE * get_handle() noexcept
Definition: file_api.hpp:125
irccd::js::file::file
file(std::string path, const std::string &mode)
Definition: file_api.hpp:77
irccd::daemon::bot
Irccd main instance.
Definition: bot.hpp:58
irccd::js::api
Javascript API module.
Definition: api.hpp:51
irccd::js::file::get_path
const std::string & get_path() const noexcept
Definition: file_api.hpp:115
irccd::js::file_api::get_name
auto get_name() const noexcept -> std::string_view override
irccd::js::file
Object for Javascript to perform I/O.
Definition: file_api.hpp:56
std
Definition: bot.hpp:253
irccd::js::duk::require
auto require(duk_context *ctx, duk_idx_t index)
Definition: duk.hpp:314
irccd::js::duk::type_traits
Operations on different types.
Definition: duk.hpp:254