From 8e8347533a4602e96b6f7bd16d03cdb402066965 Mon Sep 17 00:00:00 2001 From: Gabriel Jensen Date: Mon, 29 May 2023 22:22:40 +0200 Subject: Add base for C++ API: Install new headers; Bump extension version; Add class fil for file manipulations; Remove return value from cls: Breaks API, but the function wasn't able to fail previously, so there wasn't any need to do error checking; --- CHANGELOG.txt | 8 +++++ demo.cc | 33 ++++++++++--------- dux.svg | 4 +-- dux/GNUmakefile | 19 ++++++++++- dux/include-private/dux/prv/dux.h | 4 +++ dux/include-private/dux/prv/io.h | 4 +++ dux/include/dux/dux | 55 ++++++++++++++++++++++++++++++++ dux/include/dux/dux.h | 2 +- dux/include/dux/fs | 67 +++++++++++++++++++++++++++++++++++++++ dux/include/dux/io | 40 +++++++++++++++++++++++ dux/include/dux/io.h | 4 +-- dux/source/io/cxx/fil.cc | 24 ++++++++++++++ 12 files changed, 241 insertions(+), 23 deletions(-) create mode 100644 dux/include/dux/dux create mode 100644 dux/include/dux/fs create mode 100644 dux/include/dux/io create mode 100644 dux/source/io/cxx/fil.cc diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 54b0a76..8db4ff7 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,11 @@ +# 0.1.0 + +- Add base for C++ API: +- Install new headers; +- Bump extension version; +- Add class fil for file manipulations; +- Remove return value from cls: Breaks API, but the function wasn't able to fail previously, so there wasn't any need to do error checking; + | 0.0.0 - Revive and rewrite project (current features will be listed); diff --git a/demo.cc b/demo.cc index 236e316..078a4f5 100644 --- a/demo.cc +++ b/demo.cc @@ -1,10 +1,10 @@ #include -#include +#include #include #include -template static auto chk(long const lin,::zp::i8 const mth,ltyp const& lval,rtyp const& rval) { - auto const cmp = [](::zp::i8 const mth,auto const& lval,auto const& rval) -> bool { +template static auto cmp(long const lin,::zp::i8 const mth,ltyp const& lval,rtyp const& rval) -> void { + auto const cmp = [&lval,&mth,&rval]() -> bool { switch (mth) { default: ::zp::unrch(); @@ -21,9 +21,9 @@ template static auto chk(long const lin,::zp::i8 co case 0x5u: return lval != rval; } - }; + }(); - auto const str = [](::zp::i8 const mth) -> char const * { + auto const mthstr = [&mth]() -> char const* { switch (mth) { default: ::zp::unrch(); @@ -40,23 +40,23 @@ template static auto chk(long const lin,::zp::i8 co case 0x5u: return "=="; } - }; + }(); - if (!cmp(mth,lval,rval)) { - ::std::cout << lin << ": " << lval << ' ' << str(mth) << ' ' << rval << ::std::endl; + if (!cmp) { + ::std::cout << lin << ": " << lval << ' ' << mthstr << ' ' << rval << ::std::endl; ::std::exit(EXIT_SUCCESS); } }; -#define chkeq(lval,rval) (::chk(__LINE__,0x0u,(lval),(rval))) -#define chkge(lval,rval) (::chk(__LINE__,0x1u,(lval),(rval))) -#define chkgt(lval,rval) (::chk(__LINE__,0x2u,(lval),(rval))) -#define chkle(lval,rval) (::chk(__LINE__,0x3u,(lval),(rval))) -#define chklt(lval,rval) (::chk(__LINE__,0x4u,(lval),(rval))) -#define chkne(lval,rval) (::chk(__LINE__,0x5u,(lval),(rval))) +#define chkeq(lval,rval) (::cmp(__LINE__,0x0u,(lval),(rval))) +#define chkge(lval,rval) (::cmp(__LINE__,0x1u,(lval),(rval))) +#define chkgt(lval,rval) (::cmp(__LINE__,0x2u,(lval),(rval))) +#define chkle(lval,rval) (::cmp(__LINE__,0x3u,(lval),(rval))) +#define chklt(lval,rval) (::cmp(__LINE__,0x4u,(lval),(rval))) +#define chkne(lval,rval) (::cmp(__LINE__,0x5u,(lval),(rval))) int main() { //::dux_pri("dux {i04}.{i04}, demo\n",dux_api,dux_ext); - ::std::cout << "dux " << dux_api << '.' << dux_ext << ", demo" << ::std::endl; + ::std::cout << "dux " << ::dux::ver.api << '.' << ::dux::ver.ext << ", demo" << ::std::endl; ::dux_err err = ::dux_err_oky; @@ -136,8 +136,7 @@ int main() { chkeq(::zp::strequ(buf,src),true); - err = ::dux_cls(fil); - chkerr("unable to close file"); + ::dux_cls(fil); err = ::dux_wrtstr(dux_odfl,"Goodbye!\n"); chkerr("unable to say goodbye"); diff --git a/dux.svg b/dux.svg index e21a965..14c7a30 100644 --- a/dux.svg +++ b/dux.svg @@ -23,7 +23,7 @@ - + @@ -55,8 +55,8 @@ - + diff --git a/dux/GNUmakefile b/dux/GNUmakefile index 73d5140..dfdd82f 100644 --- a/dux/GNUmakefile +++ b/dux/GNUmakefile @@ -20,7 +20,8 @@ OBJS := \ source/io/fil.o \ source/io/opn.o \ source/io/red.o \ - source/io/wrt.o + source/io/wrt.o \ + source/io/cxx/fil.o HDRS := \ include/dux/dux.h \ @@ -44,6 +45,22 @@ CFLAGS := \ -pipe \ -std=c99 +CXXFLAGS := \ + -D_GNU_SOURCE \ + -Ddux_prv_dbg=$(dbg) \ + -Iinclude \ + -Iinclude-private \ + -Ofast \ + -Wall \ + -Wextra \ + -Wmissing-prototypes \ + -Wpedantic \ + -fpic \ + -fshort-enums \ + -g \ + -pipe \ + -std=c++11 + LDFLAGS := \ -shared diff --git a/dux/include-private/dux/prv/dux.h b/dux/include-private/dux/prv/dux.h index f6bcfc5..d72e067 100644 --- a/dux/include-private/dux/prv/dux.h +++ b/dux/include-private/dux/prv/dux.h @@ -12,6 +12,10 @@ #include +#if zp_std_cxx +#include +#endif + dux_prv_cdec dux_prv_cdecend diff --git a/dux/include-private/dux/prv/io.h b/dux/include-private/dux/prv/io.h index 99d2866..f800a3e 100644 --- a/dux/include-private/dux/prv/io.h +++ b/dux/include-private/dux/prv/io.h @@ -12,6 +12,10 @@ #include +#if zp_std_cxx +#include +#endif + dux_prv_cdec struct dux_det_fil { diff --git a/dux/include/dux/dux b/dux/include/dux/dux new file mode 100644 index 0000000..41d9740 --- /dev/null +++ b/dux/include/dux/dux @@ -0,0 +1,55 @@ +/* + Copyright 2019-2023 Gabriel Jensen. + + This file is part of dux. + dux is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + dux 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 Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public License along with dux. If not, see . +*/ + +#if !defined(dux_cxxhdr_dux) +#define dux_cxxhdr_dux + +#include +#include + +namespace dux { + namespace det { + template struct vertyp { + typ api; + typ ext; + }; + } + + constexpr ::dux::det::vertyp<::zp::i04> ver = { + dux_api, + dux_ext, + }; + + enum struct err { + oky = dux_err_oky, + err = dux_err_err, + badalc = dux_err_badalc, + badfil = dux_err_badfil, + badfmt = dux_err_badfmt, + badprv = dux_err_badprv, + badval = dux_err_badval, + eof = dux_err_eof, + exs = dux_err_exs, + io = dux_err_io, + isdir = dux_err_isdir, + memlim = dux_err_memlim, + nodir = dux_err_nodir, + nofil = dux_err_nofil, + redonl = dux_err_redonl, + spclim = dux_err_spclim, + }; + + [[noreturn]] zp_iln inline auto abr(char const* const msg) noexcept -> void {::dux_abr(msg);} + + zp_iln inline auto errmsg(char * buf,::dux::err const err) noexcept -> ::zp::sizerr {return ::dux_errmsg(buf,static_cast<::dux_err>(err));} + + zp_iln inline auto getenv(char * buf,char const* const nam) noexcept -> ::zp::sizerr {return ::dux_getenv(buf,nam);} +} + +#endif diff --git a/dux/include/dux/dux.h b/dux/include/dux/dux.h index 4ba484a..752004e 100644 --- a/dux/include/dux/dux.h +++ b/dux/include/dux/dux.h @@ -29,7 +29,7 @@ dux_prv_cdec #define dux_api ((zp_i04)+0x0u) -#define dux_ext ((zp_i04)+0x0u) +#define dux_ext ((zp_i04)+0x1u) typedef enum { dux_err_oky = 0x0u, diff --git a/dux/include/dux/fs b/dux/include/dux/fs new file mode 100644 index 0000000..21ed2f5 --- /dev/null +++ b/dux/include/dux/fs @@ -0,0 +1,67 @@ +/* + Copyright 2019-2023 Gabriel Jensen. + + This file is part of dux. + dux is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + dux 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 Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public License along with dux. If not, see . +*/ + +#if !defined(dux_cxxhdr_fs) +#define dux_cxxhdr_fs + +#include +#include + +namespace dux { + using prm = ::dux_prm; + + namespace det { + struct dsc { + bool _dsc; + }; + } + + constexpr ::dux::det::dsc kep = {false,}; + constexpr ::dux::det::dsc dsc = {true,}; + + struct pthinf { + ::zp::siz siz; + ::dux::prm prm; + bool isdir : 0x1; + bool isreg : 0x1; + }; + + zp_iln inline auto curdir(char * const buf) noexcept -> ::zp::sizerr {return ::dux_curdir(buf);} + + zp_iln inline auto homdir(char * const buf) noexcept -> ::zp::sizerr {return ::dux_homdir(buf);} + + zp_iln inline auto chgdir(char const* const pth) noexcept -> ::dux::err {return static_cast<::dux::err>(::dux_chgdir(pth));} + + zp_iln inline auto setprm(char const* const pth,::dux::prm const prm) noexcept -> ::dux::err {return static_cast<::dux::err>(::dux_setprm(pth,prm));} + + zp_iln inline auto sttpth(::dux::pthinf & inf,char const* pth) noexcept -> ::dux::err { + ::dux_pthinf cinf; + + auto const err = static_cast<::dux::err>(::dux_sttpth(&cinf,pth)); + + zp_unlik (err != ::dux::err::oky) {return err;} + + inf.siz = cinf.siz; + inf.prm = cinf.prm; + inf.isdir = cinf.isdir; + inf.isreg = cinf.isreg; + + return err; + } + + zp_iln inline auto crtdir(char const* const pth,::dux::prm const prm) noexcept -> ::dux::err {return static_cast<::dux::err>(::dux_crtdir(pth,prm));} + + zp_iln inline auto cpy(char const* const newpth,char const* pth,::dux::prm const prm) noexcept -> ::dux::err {return static_cast<::dux::err>(::dux_cpy(newpth,pth,prm));} + + zp_iln inline auto mov(char const* const newpth,char const* pth) noexcept -> ::dux::err {return static_cast<::dux::err>(::dux_mov(newpth,pth));} + + zp_iln inline auto rem(char const* const pth) noexcept -> ::dux::err {return static_cast<::dux::err>(::dux_rem(pth));} +} + +#endif diff --git a/dux/include/dux/io b/dux/include/dux/io new file mode 100644 index 0000000..79094b0 --- /dev/null +++ b/dux/include/dux/io @@ -0,0 +1,40 @@ +/* + Copyright 2019-2023 Gabriel Jensen. + + This file is part of dux. + dux is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + dux 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 Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public License along with dux. If not, see . +*/ + +#if !defined(dux_cxxhdr_io) +#define dux_cxxhdr_io + +#include +#include + +namespace dux { + class fil { + public: + fil() noexcept; + explicit fil(char const* pth) noexcept; + + ~fil() noexcept; + + auto crt( char const* pth,::dux::prm) noexcept -> ::dux::err; + auto opn( char const* pth) noexcept -> ::dux::err; + auto opnrw(char const* pth,::dux_det_dsc) noexcept -> ::dux::err; + + auto cls() noexcept -> void; + + auto red( void * buf,::zp::siz num) noexcept -> ::dux::err; + auto red( void * buf,::zp::siz num,::zp::siz & numred) noexcept -> ::dux::err; + auto wrt( void const* dat,::zp::siz num) noexcept -> ::dux::err; + auto wrtstr(char const* dat) noexcept -> ::dux::err; + + private: + ::dux_fil * _cfil; + }; +} + +#endif diff --git a/dux/include/dux/io.h b/dux/include/dux/io.h index 913427f..f068c6b 100644 --- a/dux/include/dux/io.h +++ b/dux/include/dux/io.h @@ -25,11 +25,11 @@ dux_err dux_crt( dux_fil * * fil,char const* pth,dux_prm prm); dux_err dux_opn( dux_fil * * fil,char const* pth); dux_err dux_opnrw(dux_fil * * fil,char const* pth,struct dux_det_dsc); -dux_err dux_cls(dux_fil * fil); +void dux_cls(dux_fil * fil); +dux_err dux_red( void * buf,dux_fil * fil,zp_siz num,zp_siz * numred); dux_err dux_wrt( dux_fil * fil,void const* dat,zp_siz num); dux_err dux_wrtstr(dux_fil * fil,char const* str); -dux_err dux_red( void * buf,dux_fil * fil,zp_siz num,zp_siz * numred); dux_prv_cdecend diff --git a/dux/source/io/cxx/fil.cc b/dux/source/io/cxx/fil.cc new file mode 100644 index 0000000..f459757 --- /dev/null +++ b/dux/source/io/cxx/fil.cc @@ -0,0 +1,24 @@ +/* + Copyright 2019-2023 Gabriel Jensen. + + This file is part of dux. + dux is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + dux 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 Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public License along with dux. If not, see . +*/ + +#include + +dux::fil::fil() noexcept { + this->_cfil = nullptr; +} + +dux::fil::fil(char const* pth) noexcept {this->opn(pth);} + +dux::fil::~fil() noexcept {this->cls();} + +auto dux::fil::opn(char const* pth) noexcept -> ::dux::err {this->_cfil = ::dux_opn(pth);} + +auto dux::fil::cls() noexcept -> void {::dux_cls(this->_cls);} + +auto dux::fil::wrt(void const) noexcept -> void {::dux_cls(this->_cls);} -- cgit v1.2.3