mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
[Update] Add tomlc17 library
This commit is contained in:
1 parent
d01517f09c
commit
96aab8514f
8 files changed
+3267
No files matched your search
@@ -15,6 +15,7 @@ set(INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/includes)
|
||||
# libraries
|
||||
add_subdirectory(${INCLUDES}/ReflectiveDLLInjection)
|
||||
add_subdirectory(${INCLUDES}/log.c)
|
||||
add_subdirectory(${INCLUDES}/tomlc17)
|
||||
add_subdirectory(src/utils)
|
||||
|
||||
include(cmake/JuiceModule.cmake)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
/scanvalue
|
||||
/scankey
|
||||
/parser
|
||||
/*.so.*
|
||||
@@ -0,0 +1,10 @@
|
||||
# includes/tomlc17/CMakeLists.txt
|
||||
|
||||
add_library(tomlc17 STATIC tomlc17.c)
|
||||
set_target_properties(tomlc17 PROPERTIES
|
||||
OUTPUT_NAME "tomlc17"
|
||||
)
|
||||
|
||||
target_include_directories(tomlc17 PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
@@ -0,0 +1,41 @@
|
||||
HFILES = tomlc17.h
|
||||
CFILES = tomlc17.c
|
||||
OBJ = $(CFILES:.c=.o)
|
||||
|
||||
CFLAGS = -std=c17 -fpic -Wmissing-declarations -Wall -Wextra -MMD
|
||||
LIB_VERSION = 1.0
|
||||
LIB = libtomlc17.a
|
||||
LIB_SHARED = libtomlc17.so.$(LIB_VERSION)
|
||||
EXEC =
|
||||
|
||||
ifdef DEBUG
|
||||
CFLAGS += -O0 -g
|
||||
else
|
||||
CFLAGS += -O3 -DNDEBUG
|
||||
endif
|
||||
|
||||
all: $(EXEC) $(LIB) $(LIB_SHARED)
|
||||
|
||||
*.o: $(HFILES)
|
||||
|
||||
libtomlc17.a: tomlc17.o
|
||||
ar -rcs $@ $^
|
||||
|
||||
$(LIB_SHARED): tomlc17.o
|
||||
$(CC) -shared -o $@ $^
|
||||
|
||||
-include $(OBJ:%.o=%.d) $(EXEC:%=%.d)
|
||||
|
||||
test: all
|
||||
|
||||
clean:
|
||||
rm -f *.o *.d *.a *.so.* $(EXEC)
|
||||
|
||||
distclean: clean
|
||||
|
||||
format:
|
||||
clang-format -i *.[ch] *.hpp
|
||||
|
||||
|
||||
.PHONY: all clean distclean format
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,190 @@
|
||||
/* Copyright (c) 2024-2025, CK Tan.
|
||||
* https://github.com/cktan/tomlc17/blob/main/LICENSE
|
||||
*/
|
||||
#ifndef TOMLC17_H
|
||||
#define TOMLC17_H
|
||||
|
||||
/*
|
||||
* USAGE:
|
||||
*
|
||||
* 1. Call toml_parse(), toml_parse_file(), or toml_parse_file_ex()
|
||||
* 2. Check result.ok
|
||||
* 3. Use toml_get() or toml_seek() to query and traverse the
|
||||
* result.toptab
|
||||
* 4. Call toml_free() to release resources.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define TOML_EXTERN extern "C"
|
||||
#else
|
||||
#define TOML_EXTERN extern
|
||||
#endif
|
||||
|
||||
enum toml_type_t {
|
||||
TOML_UNKNOWN = 0,
|
||||
TOML_STRING,
|
||||
TOML_INT64,
|
||||
TOML_FP64,
|
||||
TOML_BOOLEAN,
|
||||
TOML_DATE,
|
||||
TOML_TIME,
|
||||
TOML_DATETIME,
|
||||
TOML_DATETIMETZ,
|
||||
TOML_ARRAY,
|
||||
TOML_TABLE,
|
||||
};
|
||||
typedef enum toml_type_t toml_type_t;
|
||||
|
||||
/* This is a Node in a Tree that represents a toml document rooted
|
||||
* at toml_result_t::toptab.
|
||||
*/
|
||||
typedef struct toml_datum_t toml_datum_t;
|
||||
struct toml_datum_t {
|
||||
toml_type_t type;
|
||||
uint32_t flag; // internal
|
||||
union {
|
||||
const char *s; // same as str.ptr; use if there are no NUL in string.
|
||||
struct {
|
||||
const char *ptr; // NUL terminated string
|
||||
int len; // length excluding the terminating NUL.
|
||||
} str;
|
||||
int64_t int64; // integer
|
||||
double fp64; // float
|
||||
bool boolean;
|
||||
struct { // date, time
|
||||
int16_t year, month, day;
|
||||
int16_t hour, minute, second;
|
||||
int32_t usec;
|
||||
int16_t tz; // in minutes
|
||||
} ts;
|
||||
struct { // array
|
||||
int32_t size; // count elem
|
||||
toml_datum_t *elem; // elem[]
|
||||
} arr;
|
||||
struct { // table
|
||||
int32_t size; // count key
|
||||
const char **key; // key[]
|
||||
int *len; // len[]
|
||||
toml_datum_t *value; // value[]
|
||||
} tab;
|
||||
} u;
|
||||
};
|
||||
|
||||
/* Result returned by toml_parse() */
|
||||
typedef struct toml_result_t toml_result_t;
|
||||
struct toml_result_t {
|
||||
bool ok; // success flag
|
||||
toml_datum_t toptab; // valid if ok
|
||||
char errmsg[200]; // valid if not ok
|
||||
void *__internal; // do not use
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse a toml document. Returns a toml_result which must be freed
|
||||
* using toml_free() eventually.
|
||||
*
|
||||
* IMPORTANT: src[] must be a NUL terminated string! The len parameter
|
||||
* does not include the NUL terminator.
|
||||
*/
|
||||
TOML_EXTERN toml_result_t toml_parse(const char *src, int len);
|
||||
|
||||
/**
|
||||
* Parse a toml file. Returns a toml_result which must be freed
|
||||
* using toml_free() eventually.
|
||||
*
|
||||
* IMPORTANT: you are still responsible to fclose(fp).
|
||||
*/
|
||||
TOML_EXTERN toml_result_t toml_parse_file(FILE *fp);
|
||||
|
||||
/**
|
||||
* Parse a toml file. Returns a toml_result which must be freed
|
||||
* using toml_free() eventually.
|
||||
*/
|
||||
TOML_EXTERN toml_result_t toml_parse_file_ex(const char *fname);
|
||||
|
||||
/**
|
||||
* Release the result.
|
||||
*/
|
||||
TOML_EXTERN void toml_free(toml_result_t result);
|
||||
|
||||
/**
|
||||
* Find a key in a toml_table. Return the value of the key if found,
|
||||
* or a TOML_UNKNOWN otherwise.
|
||||
*/
|
||||
TOML_EXTERN toml_datum_t toml_get(toml_datum_t table, const char *key);
|
||||
|
||||
/**
|
||||
* Locate a value starting from a toml_table. Return the value of the key if
|
||||
* found, or a TOML_UNKNOWN otherwise.
|
||||
*
|
||||
* Note: the multipart-key is separated by DOT, and must not have any escape
|
||||
* chars.
|
||||
*/
|
||||
TOML_EXTERN toml_datum_t toml_seek(toml_datum_t table,
|
||||
const char *multipart_key);
|
||||
|
||||
/**
|
||||
* OBSOLETE: use toml_get() instead.
|
||||
* Find a key in a toml_table. Return the value of the key if found,
|
||||
* or a TOML_UNKNOWN otherwise. (
|
||||
*/
|
||||
static inline toml_datum_t toml_table_find(toml_datum_t table,
|
||||
const char *key) {
|
||||
return toml_get(table, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override values in r1 using r2. Return a new result. All results
|
||||
* (i.e., r1, r2 and the returned result) must be freed using toml_free()
|
||||
* after use.
|
||||
*
|
||||
* LOGIC:
|
||||
* ret = copy of r1
|
||||
* for each item x in r2:
|
||||
* if x is not in ret:
|
||||
* override
|
||||
* elif x in ret is NOT of the same type:
|
||||
* override
|
||||
* elif x is an array of tables:
|
||||
* append r2.x to ret.x
|
||||
* elif x is a table:
|
||||
* merge r2.x to ret.x
|
||||
* else:
|
||||
* override
|
||||
*/
|
||||
TOML_EXTERN toml_result_t toml_merge(const toml_result_t *r1,
|
||||
const toml_result_t *r2);
|
||||
|
||||
/**
|
||||
* Check if two results are the same. Dictinary and array orders are
|
||||
* sensitive.
|
||||
*/
|
||||
TOML_EXTERN bool toml_equiv(const toml_result_t *r1, const toml_result_t *r2);
|
||||
|
||||
/* Options that override tomlc17 defaults globally */
|
||||
typedef struct toml_option_t toml_option_t;
|
||||
struct toml_option_t {
|
||||
bool check_utf8; // Check all chars are valid utf8; default: false.
|
||||
void *(*mem_realloc)(void *ptr, size_t size); // default: realloc()
|
||||
void (*mem_free)(void *ptr); // default: free()
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the default options. IF NECESSARY, use this to initialize
|
||||
* toml_option_t and override values before calling
|
||||
* toml_set_option().
|
||||
*/
|
||||
TOML_EXTERN toml_option_t toml_default_option(void);
|
||||
|
||||
/**
|
||||
* Set toml options globally. Do this ONLY IF you are not satisfied with the
|
||||
* defaults.
|
||||
*/
|
||||
TOML_EXTERN void toml_set_option(toml_option_t opt);
|
||||
|
||||
#endif // TOMLC17_H
|
||||
@@ -0,0 +1,231 @@
|
||||
#pragma once
|
||||
#if !defined(__cplusplus) || __cplusplus < 202002L
|
||||
#error "This code requires C++20 or later"
|
||||
#endif
|
||||
|
||||
#include "tomlc17.h"
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace toml {
|
||||
|
||||
|
||||
class Datum : public toml_datum_t {
|
||||
public:
|
||||
using datetime = std::chrono::sys_time<std::chrono::microseconds>;
|
||||
using time = std::chrono::hh_mm_ss<std::chrono::microseconds>;
|
||||
using date = std::chrono::year_month_day;
|
||||
|
||||
Datum(toml_datum_t dat = toml_datum_t{}) : toml_datum_t(dat) {}
|
||||
bool is_table() const { return type == TOML_TABLE; }
|
||||
bool is_array() const { return type == TOML_ARRAY; }
|
||||
|
||||
// Retrieve a string.
|
||||
std::optional<std::string_view> as_str() const {
|
||||
if (type != TOML_STRING)
|
||||
return std::nullopt;
|
||||
else
|
||||
return std::string_view{u.str.ptr, (size_t)u.str.len};
|
||||
}
|
||||
|
||||
// Retrieve an int.
|
||||
std::optional<std::int64_t> as_int() const {
|
||||
if (type != TOML_INT64)
|
||||
return std::nullopt;
|
||||
else
|
||||
return u.int64;
|
||||
}
|
||||
|
||||
// Retrieve a float.
|
||||
std::optional<double> as_real() const {
|
||||
if (type != TOML_FP64)
|
||||
return std::nullopt;
|
||||
else
|
||||
return u.fp64;
|
||||
}
|
||||
|
||||
// Retrieve a boolean.
|
||||
std::optional<bool> as_bool() const {
|
||||
if (type != TOML_BOOLEAN)
|
||||
return std::nullopt;
|
||||
else
|
||||
return !!u.boolean;
|
||||
}
|
||||
|
||||
// Retrieve a date.
|
||||
std::optional<date> as_date() const {
|
||||
using namespace std::chrono;
|
||||
if (type != TOML_DATE)
|
||||
return std::nullopt;
|
||||
else
|
||||
return date{year(u.ts.year), month(u.ts.month), day(u.ts.day)};
|
||||
}
|
||||
|
||||
// Retrieve a time.
|
||||
std::optional<time> as_time() const {
|
||||
using namespace std::chrono;
|
||||
if (type != TOML_TIME)
|
||||
return std::nullopt;
|
||||
else
|
||||
return time{hours(u.ts.hour) + minutes(u.ts.minute) +
|
||||
seconds(u.ts.second) + microseconds(u.ts.usec)};
|
||||
}
|
||||
|
||||
// Retrieve a datetime.
|
||||
std::optional<datetime> as_datetime() const {
|
||||
using namespace std::chrono;
|
||||
if (type != TOML_DATETIME) {
|
||||
return std::nullopt;
|
||||
}
|
||||
year_month_day ymd{year{u.ts.year}, month{(unsigned)u.ts.month},
|
||||
day{(unsigned)u.ts.day}};
|
||||
auto tod = hours{u.ts.hour} + minutes{u.ts.minute} + seconds{u.ts.second} +
|
||||
microseconds{u.ts.usec};
|
||||
return datetime{sys_days{ymd} + tod};
|
||||
}
|
||||
|
||||
// Retrieve a datetime and minute offset from UTC.
|
||||
std::optional<std::pair<datetime, int>> as_datetimetz() const {
|
||||
using namespace std::chrono;
|
||||
|
||||
if (type != TOML_DATETIMETZ) {
|
||||
return std::nullopt;
|
||||
}
|
||||
year_month_day ymd{year{u.ts.year}, month{(unsigned)u.ts.month},
|
||||
day{(unsigned)u.ts.day}};
|
||||
auto tod = hours{u.ts.hour} + minutes{u.ts.minute} + seconds{u.ts.second} +
|
||||
microseconds{u.ts.usec};
|
||||
return std::pair{datetime{sys_days{ymd} + tod}, u.ts.tz};
|
||||
}
|
||||
|
||||
// Retrieve an array of Datum from this datum.
|
||||
std::optional<std::vector<Datum>> as_vector() const {
|
||||
if (type != TOML_ARRAY) {
|
||||
return std::nullopt;
|
||||
}
|
||||
std::vector<Datum> ret;
|
||||
ret.assign(u.arr.elem, u.arr.elem + u.arr.size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Retrieve an array of strings from this datum.
|
||||
std::optional<std::vector<std::string_view>> as_strvec() const {
|
||||
try {
|
||||
auto vec = as_vector().value();
|
||||
std::vector<std::string_view> ret;
|
||||
ret.resize(vec.size());
|
||||
for (size_t i = 0; i < vec.size(); i++) {
|
||||
ret[i] = vec[i].as_str().value();
|
||||
}
|
||||
return ret;
|
||||
} catch (const std::bad_optional_access &ex) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve an array of ints from this datum
|
||||
std::optional<std::vector<int64_t>> as_intvec() const {
|
||||
try {
|
||||
auto vec = as_vector().value();
|
||||
std::vector<int64_t> ret;
|
||||
ret.resize(vec.size());
|
||||
for (size_t i = 0; i < vec.size(); i++) {
|
||||
ret[i] = *vec[i].as_int();
|
||||
}
|
||||
return ret;
|
||||
} catch (const std::bad_optional_access &ex) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
// For tables. Retrieve the value of a composite key from this datum
|
||||
// (which is a table).
|
||||
std::optional<Datum> get(std::initializer_list<std::string_view> keys) const {
|
||||
Datum tab = *this;
|
||||
Datum value;
|
||||
for (auto key : keys) {
|
||||
value = {};
|
||||
if (tab.type != TOML_TABLE) {
|
||||
return std::nullopt;
|
||||
}
|
||||
for (int i = 0; i < tab.u.tab.size; i++) {
|
||||
if (key ==
|
||||
std::string_view{tab.u.tab.key[i], (size_t)tab.u.tab.len[i]}) {
|
||||
value = tab.u.tab.value[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
tab = value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// Retrieve the value of a key
|
||||
std::optional<Datum> get(std::string_view key) const { return get({key}); }
|
||||
|
||||
// Retrieve the value of a multi-part key
|
||||
std::optional<Datum> seek(const char *multipart_key) const {
|
||||
auto ret = toml_seek(*this, multipart_key);
|
||||
if (!ret.type) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}; // class Datum
|
||||
|
||||
class Result : private toml_result_t {
|
||||
public:
|
||||
Result(const toml_result_t& result) : toml_result_t(result) {}
|
||||
~Result() noexcept { toml_free(*this); }
|
||||
|
||||
// Disallow copying
|
||||
Result(const Result&) = delete;
|
||||
Result& operator=(const Result&) = delete;
|
||||
|
||||
// Allow moving
|
||||
Result(Result&& other) noexcept : toml_result_t() {
|
||||
swap(*this, other);
|
||||
}
|
||||
Result& operator=(Result&& other) noexcept {
|
||||
swap(*this, other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Access methods
|
||||
bool ok() const { return toml_result_t::ok; }
|
||||
Datum toptab() const { return toml_result_t::toptab; }
|
||||
const char* errmsg() const { return toml_result_t::errmsg; }
|
||||
|
||||
// Shortcuts
|
||||
std::optional<Datum> get(std::initializer_list<std::string_view> keys) const {
|
||||
return toptab().get(keys);
|
||||
}
|
||||
std::optional<Datum> seek(const char *multipart_key) const {
|
||||
return toptab().seek(multipart_key);
|
||||
}
|
||||
|
||||
private:
|
||||
friend void swap(Result& a, Result& b) noexcept {
|
||||
std::swap(static_cast<toml_result_t&>(a), static_cast<toml_result_t&>(b));
|
||||
}
|
||||
};
|
||||
|
||||
static inline Result parse_file(FILE* fp) {
|
||||
return Result{toml_parse_file(fp)};
|
||||
}
|
||||
|
||||
static inline Result parse_file_ex(const char* fname) {
|
||||
return Result{toml_parse_file_ex(fname)};
|
||||
}
|
||||
|
||||
static inline Result parse(const std::string& s) {
|
||||
return Result{toml_parse(s.data(), s.size())};
|
||||
}
|
||||
|
||||
|
||||
|
||||
}; // namespace toml
|
||||
@@ -0,0 +1,22 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024-2025, CK Tan
|
||||
https://github.com/cktan/tomlc17
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
Reference in new issue
Block a user