[ox/claw] Add readClawHeader to public API
This commit is contained in:
parent
5f7bb2e1ae
commit
5e85f18b60
16
deps/ox/src/ox/claw/read.cpp
vendored
16
deps/ox/src/ox/claw/read.cpp
vendored
@ -6,13 +6,13 @@
|
|||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <ox/std/buffer.hpp>
|
||||||
|
|
||||||
#include "read.hpp"
|
#include "read.hpp"
|
||||||
|
|
||||||
namespace ox {
|
namespace ox {
|
||||||
|
|
||||||
namespace detail {
|
Result<ClawHeader> readClawHeader(const char *buff, std::size_t buffLen) noexcept {
|
||||||
|
|
||||||
Result<ClawHeader> readHeader(const char *buff, std::size_t buffLen) noexcept {
|
|
||||||
const auto s1End = ox_strchr(buff, ';', buffLen);
|
const auto s1End = ox_strchr(buff, ';', buffLen);
|
||||||
if (!s1End) {
|
if (!s1End) {
|
||||||
return OxError(1);
|
return OxError(1);
|
||||||
@ -57,13 +57,19 @@ Result<ClawHeader> readHeader(const char *buff, std::size_t buffLen) noexcept {
|
|||||||
return hdr;
|
return hdr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result<ClawHeader> readClawHeader(const ox::Buffer &buff) noexcept {
|
||||||
|
return readClawHeader(buff.data(), buff.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<Buffer> stripClawHeader(const char *buff, std::size_t buffLen) noexcept {
|
Result<Buffer> stripClawHeader(const char *buff, std::size_t buffLen) noexcept {
|
||||||
oxRequire(header, detail::readHeader(buff, buffLen));
|
oxRequire(header, readClawHeader(buff, buffLen));
|
||||||
Buffer out(header.dataSize);
|
Buffer out(header.dataSize);
|
||||||
ox_memcpy(out.data(), header.data, out.size());
|
ox_memcpy(out.data(), header.data, out.size());
|
||||||
return move(out);
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<Buffer> stripClawHeader(const ox::Buffer &buff) noexcept {
|
||||||
|
return stripClawHeader(buff.data(), buff.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
10
deps/ox/src/ox/claw/read.hpp
vendored
10
deps/ox/src/ox/claw/read.hpp
vendored
@ -19,8 +19,6 @@
|
|||||||
|
|
||||||
namespace ox {
|
namespace ox {
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
struct ClawHeader {
|
struct ClawHeader {
|
||||||
String typeName;
|
String typeName;
|
||||||
int typeVersion = -1;
|
int typeVersion = -1;
|
||||||
@ -29,15 +27,17 @@ struct ClawHeader {
|
|||||||
std::size_t dataSize = 0;
|
std::size_t dataSize = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
Result<ClawHeader> readHeader(const char *buff, std::size_t buffLen) noexcept;
|
Result<ClawHeader> readClawHeader(const char *buff, std::size_t buffLen) noexcept;
|
||||||
|
|
||||||
}
|
Result<ClawHeader> readClawHeader(const ox::Buffer &buff) noexcept;
|
||||||
|
|
||||||
Result<Buffer> stripClawHeader(const char *buff, std::size_t buffLen) noexcept;
|
Result<Buffer> stripClawHeader(const char *buff, std::size_t buffLen) noexcept;
|
||||||
|
|
||||||
|
Result<Buffer> stripClawHeader(const ox::Buffer &buff) noexcept;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Error readClaw(const char *buff, std::size_t buffLen, T *val) {
|
Error readClaw(const char *buff, std::size_t buffLen, T *val) {
|
||||||
oxRequire(header, detail::readHeader(buff, buffLen));
|
oxRequire(header, readClawHeader(buff, buffLen));
|
||||||
switch (header.fmt) {
|
switch (header.fmt) {
|
||||||
case ClawFormat::Metal:
|
case ClawFormat::Metal:
|
||||||
{
|
{
|
||||||
|
4
deps/ox/src/ox/claw/test/tests.cpp
vendored
4
deps/ox/src/ox/claw/test/tests.cpp
vendored
@ -108,7 +108,7 @@ std::map<std::string_view, ox::Error(*)()> tests = {
|
|||||||
"ClawHeaderReader",
|
"ClawHeaderReader",
|
||||||
[] {
|
[] {
|
||||||
ox::String hdr = "O1;com.drinkingtea.ox.claw.test.Header;2;";
|
ox::String hdr = "O1;com.drinkingtea.ox.claw.test.Header;2;";
|
||||||
auto [ch, err] = ox::detail::readHeader(hdr.c_str(), hdr.len() + 1);
|
auto [ch, err] = ox::readClawHeader(hdr.c_str(), hdr.len() + 1);
|
||||||
oxAssert(err, "Error parsing header");
|
oxAssert(err, "Error parsing header");
|
||||||
oxAssert(ch.fmt == ox::ClawFormat::Organic, "Format wrong");
|
oxAssert(ch.fmt == ox::ClawFormat::Organic, "Format wrong");
|
||||||
oxAssert(ch.typeName == "com.drinkingtea.ox.claw.test.Header", "Type name wrong");
|
oxAssert(ch.typeName == "com.drinkingtea.ox.claw.test.Header", "Type name wrong");
|
||||||
@ -120,7 +120,7 @@ std::map<std::string_view, ox::Error(*)()> tests = {
|
|||||||
"ClawHeaderReader2",
|
"ClawHeaderReader2",
|
||||||
[] {
|
[] {
|
||||||
ox::String hdr = "M1;com.drinkingtea.ox.claw.test.Header2;3;";
|
ox::String hdr = "M1;com.drinkingtea.ox.claw.test.Header2;3;";
|
||||||
auto [ch, err] = ox::detail::readHeader(hdr.c_str(), hdr.len() + 1);
|
auto [ch, err] = ox::readClawHeader(hdr.c_str(), hdr.len() + 1);
|
||||||
oxAssert(err, "Error parsing header");
|
oxAssert(err, "Error parsing header");
|
||||||
oxAssert(ch.fmt == ox::ClawFormat::Metal, "Format wrong");
|
oxAssert(ch.fmt == ox::ClawFormat::Metal, "Format wrong");
|
||||||
oxAssert(ch.typeName == "com.drinkingtea.ox.claw.test.Header2", "Type name wrong");
|
oxAssert(ch.typeName == "com.drinkingtea.ox.claw.test.Header2", "Type name wrong");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user