ncreader: C++ wrapper

pull/592/head
nick black 4 years ago committed by Nick Black
parent d174431187
commit 312e42add2

@ -0,0 +1,60 @@
#ifndef __NCPP_READER_HH
#define __NCPP_READER_HH
#include <notcurses/notcurses.h>
#include "Root.hh"
#include "NCAlign.hh"
#include "NotCurses.hh"
namespace ncpp
{
class Plane;
class NCPP_API_EXPORT Reader : public Root
{
public:
explicit Reader (NotCurses *nc, int y, int x, const ncreader_options *opts)
: Reader (reinterpret_cast<notcurses*>(nc), y, x, opts)
{}
explicit Reader (NotCurses const* nc, int y, int x, const ncreader_options *opts)
: Reader (const_cast<NotCurses*>(nc), y, x, opts)
{}
explicit Reader (NotCurses &nc, int y, int x, const ncreader_options *opts)
: Reader (reinterpret_cast<NotCurses*>(&nc), y, x, opts)
{}
explicit Reader (NotCurses const& nc, int y, int x, const ncreader_options *opts)
: Reader (const_cast<NotCurses*>(&nc), y, x, opts)
{}
explicit Reader (notcurses* nc, int y, int x, const ncreader_options *opts)
{
reader = ncreader_create (nc, y, x, opts);
if (reader == nullptr)
throw init_error ("notcurses failed to create a new reader");
}
~Reader ()
{
if (!is_notcurses_stopped ())
ncreader_destroy (reader, nullptr);
}
char* get_contents () const noexcept
{
return ncreader_contents(reader);
}
Plane* get_plane () const noexcept
{
return Plane::map_plane (ncreader_plane (reader));
}
private:
ncreader *reader;
};
}
#endif

@ -2789,8 +2789,12 @@ API int ncreader_clear(struct ncreader* n);
API struct ncplane* ncreader_plane(struct ncreader* n);
// destroy the reader and its bound plane.
API void ncreader_destroy(struct ncreader* n);
// return a heap-allocated copy of the current (UTF-8) contents.
API char* ncreader_contents(const struct ncreader* n);
// destroy the reader and its bound plane. if 'contents' is not NULL, the
// UTF-8 input will be heap-duplicated and written to 'contents'.
API void ncreader_destroy(struct ncreader* n, char** contents);
#undef API

@ -148,7 +148,8 @@ typedef struct ncsubproc {
} ncsubproc;
typedef struct ncreader {
ncplane* ncp;
ncplane* ncp; // always owned by ncreader
char* contents; // UTF-8 encoded, never NULL
} ncreader;
typedef struct ncmenu {

@ -15,9 +15,10 @@ ncreader* ncreader_create(notcurses* nc, int y, int x, const ncreader_options* o
return NULL;
}
if(ncplane_set_base(nr->ncp, opts->egc, opts->eattrword, opts->echannels) <= 0){
ncreader_destroy(nr);
ncreader_destroy(nr, NULL);
return NULL;
}
nr->contents = strdup("");
// FIXME
}
return nr;
@ -33,8 +34,16 @@ ncplane* ncreader_plane(ncreader* n){
return n->ncp;
}
void ncreader_destroy(ncreader* n){
char* ncreader_contents(const ncreader* n){
return strdup(n->contents);
}
void ncreader_destroy(ncreader* n, char** contents){
if(n){
if(contents){
*contents = strdup(n->contents);
}
free(n->contents);
ncplane_destroy(n->ncp);
free(n);
}

Loading…
Cancel
Save