Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
/*
* SPDX-License-Identifier: ISC
*
* Copyright (C) 2017-2021 Michael Drake <tlsa@netsurf-browser.org>
*/
#ifndef CYAML_UTIL_H
#define CYAML_UTIL_H
#include "cyaml/cyaml.h"
#include "utf8.h"
#define cyaml_static_assert(e) \
{ \
enum { \
cyaml_static_assert_check = 1 / (!!(e)) \
}; \
}
#define CYAML_UNUSED(_x) ((void)(_x))
static inline bool cyaml__host_is_little_endian(void)
{
const uint16_t test = 1;
return ((const uint8_t *) &test)[0];
}
static inline bool cyaml__host_is_big_endian(void)
{
return !cyaml__host_is_little_endian();
}
typedef uint32_t cyaml_bitfield_t;
#define CYAML_BITFIELD_BITS (sizeof(cyaml_bitfield_t) * CHAR_BIT)
enum cyaml_state_e {
CYAML_STATE_START,
CYAML_STATE_IN_STREAM,
CYAML_STATE_IN_DOC,
CYAML_STATE_IN_MAP_KEY,
CYAML_STATE_IN_MAP_VALUE,
CYAML_STATE_IN_SEQUENCE,
CYAML_STATE__COUNT,
};
static inline const char * cyaml__state_to_str(enum cyaml_state_e state)
{
static const char * const strings[CYAML_STATE__COUNT] = {
[CYAML_STATE_START] = "start",
[CYAML_STATE_IN_STREAM] = "in stream",
[CYAML_STATE_IN_DOC] = "in doc",
[CYAML_STATE_IN_MAP_KEY] = "in mapping (key)",
[CYAML_STATE_IN_MAP_VALUE] = "in mapping (value)",
[CYAML_STATE_IN_SEQUENCE] = "in sequence",
};
if ((unsigned)state >= CYAML_STATE__COUNT) {
return "<invalid>";
}
return strings[state];
}
static inline const char * cyaml__type_to_str(cyaml_type_e type)
{
static const char * const strings[CYAML__TYPE_COUNT] = {
[CYAML_INT] = "INT",
[CYAML_UINT] = "UINT",
[CYAML_BOOL] = "BOOL",
[CYAML_ENUM] = "ENUM",
[CYAML_FLAGS] = "FLAGS",
[CYAML_FLOAT] = "FLOAT",
[CYAML_STRING] = "STRING",
[CYAML_MAPPING] = "MAPPING",
[CYAML_BITFIELD] = "BITFIELD",
[CYAML_SEQUENCE] = "SEQUENCE",
[CYAML_SEQUENCE_FIXED] = "SEQUENCE_FIXED",
[CYAML_IGNORE] = "IGNORE",
};
if ((unsigned)type >= CYAML__TYPE_COUNT) {
return "<invalid>";
}
return strings[type];
}
static inline void cyaml__log(
const cyaml_config_t *cfg,
cyaml_log_t level,
const char *fmt, ...)
{
if (level >= cfg->log_level && cfg->log_fn != NULL) {
va_list args;
va_start(args, fmt);
cfg->log_fn(level, cfg->log_ctx, fmt, args);
va_end(args);
}
}
static inline bool cyaml__is_case_sensitive(
const cyaml_config_t *config,
const cyaml_schema_value_t *schema)
{
if (schema->flags & CYAML_FLAG_CASE_INSENSITIVE) {
return false;
} else if (schema->flags & CYAML_FLAG_CASE_SENSITIVE) {
return true;
} else if (config->flags & CYAML_CFG_CASE_INSENSITIVE) {
return false;
}
return true;
}
static inline int cyaml__strcmp(
const cyaml_config_t *config,
const cyaml_schema_value_t *schema,
const void * const str1,
const void * const str2)
{
if (cyaml__is_case_sensitive(config, schema)) {
return strcmp(str1, str2);
}
return cyaml_utf8_casecmp(str1, str2);
}
static inline bool cyaml__flag_check_all(
enum cyaml_flag flags,
enum cyaml_flag mask)
{
return ((flags & mask) == mask);
}
#endif
cyaml.h
CYAML library public header.
cyaml_schema_value_t
struct cyaml_schema_value cyaml_schema_value_t
cyaml_log_t
enum cyaml_log_e cyaml_log_t
CYAML_INT
@ CYAML_INT
Definition cyaml.h:54
CYAML_UINT
@ CYAML_UINT
Definition cyaml.h:55
CYAML__TYPE_COUNT
@ CYAML__TYPE_COUNT
Definition cyaml.h:130
CYAML_SEQUENCE_FIXED
@ CYAML_SEQUENCE_FIXED
Definition cyaml.h:119
CYAML_IGNORE
@ CYAML_IGNORE
Definition cyaml.h:125
CYAML_FLOAT
@ CYAML_FLOAT
Definition cyaml.h:73
CYAML_MAPPING
@ CYAML_MAPPING
Definition cyaml.h:79
CYAML_BITFIELD
@ CYAML_BITFIELD
Definition cyaml.h:89
CYAML_ENUM
@ CYAML_ENUM
Definition cyaml.h:62
CYAML_BOOL
@ CYAML_BOOL
Definition cyaml.h:56
CYAML_SEQUENCE
@ CYAML_SEQUENCE
Definition cyaml.h:101
CYAML_STRING
@ CYAML_STRING
Definition cyaml.h:74
CYAML_FLAGS
@ CYAML_FLAGS
Definition cyaml.h:72
cyaml_config_t
struct cyaml_config cyaml_config_t
cyaml_flag
cyaml_flag
Definition cyaml.h:138
CYAML_FLAG_CASE_SENSITIVE
@ CYAML_FLAG_CASE_SENSITIVE
Definition cyaml.h:249
CYAML_FLAG_CASE_INSENSITIVE
@ CYAML_FLAG_CASE_INSENSITIVE
Definition cyaml.h:267
CYAML_CFG_CASE_INSENSITIVE
@ CYAML_CFG_CASE_INSENSITIVE
Definition cyaml.h:560
cyaml_type_e
enum cyaml_type cyaml_type_e
cyaml_config::log_fn
cyaml_log_fn_t log_fn
Definition cyaml.h:1454
cyaml_config::flags
cyaml_cfg_flags_t flags
Definition cyaml.h:1494
cyaml_config::log_ctx
void * log_ctx
Definition cyaml.h:1464
cyaml_config::log_level
cyaml_log_t log_level
Definition cyaml.h:1492
cyaml_schema_value::flags
enum cyaml_flag flags
Definition cyaml.h:374
cyaml_utf8_casecmp
int cyaml_utf8_casecmp(const void *const str1, const void *const str2)
Definition utf8.c:188
utf8.h
CYAML functions for handling utf8 text.
cyaml__host_is_big_endian
static bool cyaml__host_is_big_endian(void)
Definition util.h:48
cyaml__log
static void cyaml__log(const cyaml_config_t *cfg, cyaml_log_t level, const char *fmt,...)
Definition util.h:129
cyaml__state_to_str
static const char * cyaml__state_to_str(enum cyaml_state_e state)
Definition util.h:77
cyaml__strcmp
static int cyaml__strcmp(const cyaml_config_t *config, const cyaml_schema_value_t *schema, const void *const str1, const void *const str2)
Definition util.h:181
cyaml__is_case_sensitive
static bool cyaml__is_case_sensitive(const cyaml_config_t *config, const cyaml_schema_value_t *schema)
Definition util.h:151
cyaml__flag_check_all
static bool cyaml__flag_check_all(enum cyaml_flag flags, enum cyaml_flag mask)
Definition util.h:201
cyaml__type_to_str
static const char * cyaml__type_to_str(cyaml_type_e type)
Definition util.h:99
cyaml__host_is_little_endian
static bool cyaml__host_is_little_endian(void)
Definition util.h:36
cyaml_state_e
cyaml_state_e
Definition util.h:60
CYAML_STATE__COUNT
@ CYAML_STATE__COUNT
Definition util.h:67
CYAML_STATE_IN_MAP_VALUE
@ CYAML_STATE_IN_MAP_VALUE
Definition util.h:65
CYAML_STATE_IN_SEQUENCE
@ CYAML_STATE_IN_SEQUENCE
Definition util.h:66
CYAML_STATE_IN_STREAM
@ CYAML_STATE_IN_STREAM
Definition util.h:62
CYAML_STATE_IN_DOC
@ CYAML_STATE_IN_DOC
Definition util.h:63
CYAML_STATE_IN_MAP_KEY
@ CYAML_STATE_IN_MAP_KEY
Definition util.h:64
CYAML_STATE_START
@ CYAML_STATE_START
Definition util.h:61
cyaml_bitfield_t
uint32_t cyaml_bitfield_t
Definition util.h:54