[Yunq] Create libyunq and move parsing code to it.

This commit is contained in:
Drew Galbraith 2023-12-01 09:30:52 -08:00
parent 91f3f8ee43
commit 7d9f9a7ae9
16 changed files with 310 additions and 260 deletions

View file

@ -1,2 +1,3 @@
add_subdirectory(glacier)
add_subdirectory(mammoth)
add_subdirectory(yunq)

View file

@ -1,7 +1,7 @@
#pragma once
#include "status/error.h"
#include "string/string.h"
#include "glacier/status/error.h"
#include "glacier/string/string.h"
namespace glcr {
@ -21,43 +21,57 @@ class Status {
ErrorCode code_;
String message_;
Status();
Status() : code_(OK) {}
};
Status InvalidArgument(StringView message) {
inline Status InvalidArgument(StringView message) {
return Status(INVALID_ARGUMENT, message);
}
Status NotFound(StringView message) { return Status(NOT_FOUND, message); }
inline Status NotFound(StringView message) {
return Status(NOT_FOUND, message);
}
Status PermissionDenied(StringView message) {
inline Status PermissionDenied(StringView message) {
return Status(PERMISSION_DENIED, message);
}
Status NullPtr(StringView message) { return Status(NULL_PTR, message); }
inline Status NullPtr(StringView message) { return Status(NULL_PTR, message); }
Status Empty(StringView message) { return Status(EMPTY, message); }
inline Status Empty(StringView message) { return Status(EMPTY, message); }
Status AlreadyExists(StringView message) {
inline Status AlreadyExists(StringView message) {
return Status(ALREADY_EXISTS, message);
}
Status BufferSize(StringView message) { return Status(BUFFER_SIZE, message); }
inline Status BufferSize(StringView message) {
return Status(BUFFER_SIZE, message);
}
Status FailedPrecondition(StringView message) {
inline Status FailedPrecondition(StringView message) {
return Status(FAILED_PRECONDITION, message);
}
Status Internal(StringView message) { return Status(INTERNAL, message); }
inline Status Internal(StringView message) { return Status(INTERNAL, message); }
Status Unimplemented(StringView message) {
inline Status Unimplemented(StringView message) {
return Status(UNIMPLEMENTED, message);
}
Status Exhausted(StringView message) { return Status(EXHAUSTED, message); }
inline Status Exhausted(StringView message) {
return Status(EXHAUSTED, message);
}
Status InvalidResponse(StringView message) {
inline Status InvalidResponse(StringView message) {
return Status(INVALID_RESPONSE, message);
}
#define RETURN_ERROR(expr) \
{ \
glcr::Status _tmp_err = expr; \
if (!_tmp_err) { \
return _tmp_err; \
} \
}
} // namespace glcr

17
lib/yunq/CMakeLists.txt Normal file
View file

@ -0,0 +1,17 @@
set(yunq_files
serialize.cpp
)
add_library(yunq STATIC
${yunq_files}
)
target_link_libraries(yunq
glacier)
target_include_directories(yunq
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/..")
set_target_properties(yunq PROPERTIES
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS}")

23
lib/yunq/serialize.cpp Normal file
View file

@ -0,0 +1,23 @@
#include "serialize.h"
namespace yunq {
glcr::Status CheckHeader(const glcr::ByteBuffer& buffer) {
// TODO: Check ident.
// TODO: Parse core size.
// TODO: Parse extension size.
// TODO: Check CRC32
// TODO: Parse options.
return glcr::Status::Ok();
}
void WriteHeader(glcr::ByteBuffer& bytes, uint64_t offset, uint32_t core_size,
uint32_t extension_size) {
bytes.WriteAt<uint32_t>(
offset + 0, 0xDEADBEEF); // TODO: Chose a more unique ident sequence.
bytes.WriteAt<uint32_t>(offset + 4, core_size);
bytes.WriteAt<uint32_t>(offset + 8, extension_size);
bytes.WriteAt<uint32_t>(offset + 12, 0); // TODO: Calculate CRC32.
}
} // namespace yunq

13
lib/yunq/serialize.h Normal file
View file

@ -0,0 +1,13 @@
#pragma once
#include <glacier/buffer/byte_buffer.h>
#include <glacier/status/status.h>
namespace yunq {
[[nodiscard]] glcr::Status CheckHeader(const glcr::ByteBuffer& buffer);
void WriteHeader(glcr::ByteBuffer& bytes, uint64_t offset, uint32_t core_size,
uint32_t extension_size);
} // namespace yunq