bindings/cpp/gaspardos.hpp¶
C++98 API source. Preserve the bindings/cpp/ layout when downloading multiple files.
Source files · Download gaspardos.hpp
#ifndef GASPARDOS_CPP_HPP
#define GASPARDOS_CPP_HPP
#include <cstdio>
#include <cstddef>
#include <cerrno>
#include "../native/gaspardos_x64_syscall.h"
namespace gaspardos
{
/* Native results are signed 64-bit; negative values are raw kernel errors.
close() retains ownership on failure. Destructors try once; callers should
close explicitly to handle -11 with yield and an application timeout. */
class File
{
std::FILE* file_;
File(const File&);
File& operator=(const File&);
public:
File() : file_(0)
{
}
~File()
{
close();
}
bool open(const char* path, const char* mode)
{
if (file_)
{
errno = EBUSY;
return false;
}
file_ = std::fopen(path, mode);
return file_ != 0;
}
int close()
{
if (!file_)
return 0;
std::FILE* f = file_;
file_ = 0;
return std::fclose(f);
}
std::size_t read(void* p, std::size_t n)
{
if (!file_)
{
errno = EBADF;
return 0;
}
return std::fread(p, 1, n, file_);
}
std::size_t write(const void* p, std::size_t n)
{
if (!file_)
{
errno = EBADF;
return 0;
}
return std::fwrite(p, 1, n, file_);
}
int seek(long offset, int origin)
{
if (!file_)
{
errno = EBADF;
return -1;
}
return std::fseek(file_, offset, origin);
}
int error() const
{
return file_ ? std::ferror(file_) : EBADF;
}
};
class NativeFile
{
int64_t handle_;
NativeFile(const NativeFile&);
NativeFile& operator=(const NativeFile&);
public:
NativeFile() : handle_(-1)
{
}
~NativeFile()
{
close();
}
int64_t handle() const
{
return handle_;
}
int64_t close()
{
if (handle_ < 0)
return 0;
int64_t r = GO64_fs_close(handle_);
if (r == 0)
{
handle_ = -1;
}
return r;
}
int64_t open(const wchar_t* path, uint64_t flags)
{
if (handle_ >= 0)
return -16;
int64_t r = GO64_fs_open(path, flags);
if (r >= 0)
handle_ = r;
return r;
}
int64_t read(void* p, uint64_t n)
{
return GO64_fs_read(handle_, p, n);
}
int64_t write(const void* p, uint64_t n)
{
return GO64_fs_write(handle_, p, n);
}
int64_t seek(int64_t offset, uint64_t origin)
{
return GO64_fs_seek(handle_, offset, origin);
}
int64_t stat(GO64_FSInfo& info)
{
return GO64_fs_fstat(handle_, &info);
}
int64_t truncate(uint64_t size)
{
return GO64_fs_truncate(handle_, size);
}
int64_t next(GO64_FSInfo& info)
{
return GO64_fs_readdir(handle_, &info);
}
int64_t flags(uint64_t command, uint64_t value = 0)
{
return GO64_fs_fcntl(handle_, command, value);
}
};
struct FileSystem
{
static int64_t stat(const wchar_t* path, GO64_FSInfo& info)
{
return GO64_fs_stat(path, &info);
}
static int64_t mkdir(const wchar_t* path)
{
return GO64_fs_mkdir(path);
}
static int64_t unlink(const wchar_t* path)
{
return GO64_fs_unlink(path);
}
static int64_t rmdir(const wchar_t* path)
{
return GO64_fs_rmdir(path);
}
static int64_t rename(const wchar_t* from, const wchar_t* to)
{
return GO64_fs_rename(from, to);
}
};
class Window
{
int64_t handle_;
bool ready_;
Window(const Window&);
Window& operator=(const Window&);
public:
Window() : handle_(-1), ready_(false)
{
}
~Window()
{
close();
}
int64_t handle() const
{
return handle_;
}
int64_t close()
{
if (handle_ < 0)
return 0;
int64_t r = GO64_window_destroy(handle_);
if (r == 0)
{
handle_ = -1;
ready_ = false;
}
return r;
}
bool ready() const
{
return ready_;
}
int64_t create(const GO64_WindowOptions& options)
{
if (handle_ >= 0)
return -16;
int64_t r = GO64_window_create(&options);
if (r >= 0)
{
handle_ = r;
ready_ = false;
}
return r;
}
/* Consume READY before drawing. Default polling never waits implicitly. */
int64_t event(GO64_Event& e, bool wait = false)
{
if (handle_ < 0)
return -9;
int64_t r = GO64_window_event(handle_, &e, wait ? 0 : 1);
if (r == 0 && e.type == GO64_READY)
ready_ = true;
if (r == 0 && e.type == GO64_ERROR)
ready_ = false;
return r;
}
int64_t fill(const GO64_Drawing& d)
{
if (handle_ < 0)
return -9;
return ready_ ? GO64_window_fill(handle_, &d) : -11;
}
int64_t text(const GO64_Drawing& d)
{
if (handle_ < 0)
return -9;
return ready_ ? GO64_window_text(handle_, &d) : -11;
}
int64_t focus()
{
return handle_ < 0 ? -9 : GO64_window_focus(handle_);
}
int64_t blit(const GO64_Blit& tile)
{
if (handle_ < 0)
return -9;
return ready_ ? GO64_window_blit(handle_, &tile) : -11;
}
};
inline GO64_SocketAddress ipv4(
unsigned char a, unsigned char b, unsigned char c, unsigned char d, uint16_t port)
{
GO64_SocketAddress address = { 0, 0, { 0, 0, 0, 0 } };
address.family = GO64_AF_INET;
unsigned char* bytes = reinterpret_cast<unsigned char*>(&address.port);
bytes[0] = static_cast<unsigned char>(port >> 8);
bytes[1] = static_cast<unsigned char>(port);
address.address[0] = a;
address.address[1] = b;
address.address[2] = c;
address.address[3] = d;
return address;
}
class Socket
{
int64_t handle_;
Socket(const Socket&);
Socket& operator=(const Socket&);
public:
Socket() : handle_(-1)
{
}
~Socket()
{
close();
}
int64_t handle() const
{
return handle_;
}
int64_t close()
{
if (handle_ < 0)
return 0;
int64_t r = GO64_socket_close(handle_);
if (r == 0)
{
handle_ = -1;
}
return r;
}
int64_t open(uint64_t type = GO64_STREAM, uint64_t protocol = 0)
{
if (handle_ >= 0)
return -16;
int64_t r = GO64_socket(GO64_AF_INET, type, protocol);
if (r >= 0)
handle_ = r;
return r;
}
int64_t bind(const GO64_SocketAddress& a)
{
return GO64_bind(handle_, &a, sizeof(a));
}
int64_t listen(int64_t backlog)
{
return GO64_listen(handle_, backlog);
}
int64_t accept(Socket& peer, GO64_SocketAddress* address = 0)
{
if (peer.handle_ >= 0)
return -16;
uint32_t size = sizeof(GO64_SocketAddress);
int64_t r = GO64_accept(handle_, address, address ? &size : 0);
if (r >= 0)
peer.handle_ = r;
return r;
}
int64_t connect(const GO64_SocketAddress& a)
{
return GO64_connect(handle_, &a, sizeof(a));
}
int64_t send(const void* p, uint64_t n)
{
return GO64_send(handle_, p, n, 0);
}
int64_t receive(void* p, uint64_t n)
{
return GO64_recv(handle_, p, n, 0);
}
int64_t sendTo(const void* p, uint64_t n, const GO64_SocketAddress* a)
{
return GO64_sendto(handle_, p, n, 0, a, a ? sizeof(*a) : 0);
}
int64_t receiveFrom(void* p, uint64_t n, GO64_SocketAddress* a = 0)
{
uint32_t size = sizeof(GO64_SocketAddress);
return GO64_recvfrom(handle_, p, n, 0, a, a ? &size : 0);
}
int64_t localAddress(GO64_SocketAddress& a)
{
uint32_t size = sizeof(a);
return GO64_getsockname(handle_, &a, &size);
}
int64_t peerAddress(GO64_SocketAddress& a)
{
uint32_t size = sizeof(a);
return GO64_getpeername(handle_, &a, &size);
}
};
} // namespace gaspardos
#endif