New upstream version 24.0.1+dfsg1
This commit is contained in:
parent
b14f9eae6d
commit
5a730d6ec3
842 changed files with 42245 additions and 33385 deletions
|
|
@ -18,22 +18,22 @@
|
|||
|
||||
#include "bmem.h"
|
||||
|
||||
inline void* operator new(size_t size)
|
||||
inline void *operator new(size_t size)
|
||||
{
|
||||
return bmalloc(size);
|
||||
}
|
||||
|
||||
inline void operator delete(void* data)
|
||||
inline void operator delete(void *data)
|
||||
{
|
||||
bfree(data);
|
||||
}
|
||||
|
||||
inline void* operator new[](size_t size)
|
||||
inline void *operator new[](size_t size)
|
||||
{
|
||||
return bmalloc(size);
|
||||
}
|
||||
|
||||
inline void operator delete[](void* data)
|
||||
inline void operator delete[](void *data)
|
||||
{
|
||||
bfree(data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ extern "C" {
|
|||
|
||||
EXPORT char *cfstr_copy_cstr(CFStringRef cfstr, CFStringEncoding cfstr_enc);
|
||||
|
||||
EXPORT bool cfstr_copy_dstr(CFStringRef cfstr, CFStringEncoding cfstr_enc,
|
||||
struct dstr *str);
|
||||
EXPORT bool cfstr_copy_dstr(CFStringRef cfstr, CFStringEncoding cfstr_enc,
|
||||
struct dstr *str);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,12 +31,12 @@ static int64_t array_output_get_pos(void *param)
|
|||
}
|
||||
|
||||
void array_output_serializer_init(struct serializer *s,
|
||||
struct array_output_data *data)
|
||||
struct array_output_data *data)
|
||||
{
|
||||
memset(s, 0, sizeof(struct serializer));
|
||||
memset(data, 0, sizeof(struct array_output_data));
|
||||
s->data = data;
|
||||
s->write = array_output_write;
|
||||
s->data = data;
|
||||
s->write = array_output_write;
|
||||
s->get_pos = array_output_get_pos;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,5 +24,5 @@ struct array_output_data {
|
|||
};
|
||||
|
||||
EXPORT void array_output_serializer_init(struct serializer *s,
|
||||
struct array_output_data *data);
|
||||
struct array_output_data *data);
|
||||
EXPORT void array_output_serializer_free(struct array_output_data *data);
|
||||
|
|
|
|||
|
|
@ -26,12 +26,12 @@ static int log_output_level = LOG_DEBUG;
|
|||
static int log_output_level = LOG_INFO;
|
||||
#endif
|
||||
|
||||
static int crashing = 0;
|
||||
static void *log_param = NULL;
|
||||
static int crashing = 0;
|
||||
static void *log_param = NULL;
|
||||
static void *crash_param = NULL;
|
||||
|
||||
static void def_log_handler(int log_level, const char *format,
|
||||
va_list args, void *param)
|
||||
static void def_log_handler(int log_level, const char *format, va_list args,
|
||||
void *param)
|
||||
{
|
||||
char out[4096];
|
||||
vsnprintf(out, sizeof(out), format, args);
|
||||
|
|
@ -69,7 +69,7 @@ static void def_log_handler(int log_level, const char *format,
|
|||
#endif
|
||||
|
||||
NORETURN static void def_crash_handler(const char *format, va_list args,
|
||||
void *param)
|
||||
void *param)
|
||||
{
|
||||
vfprintf(stderr, format, args);
|
||||
exit(0);
|
||||
|
|
@ -93,15 +93,14 @@ void base_set_log_handler(log_handler_t handler, void *param)
|
|||
if (!handler)
|
||||
handler = def_log_handler;
|
||||
|
||||
log_param = param;
|
||||
log_param = param;
|
||||
log_handler = handler;
|
||||
}
|
||||
|
||||
void base_set_crash_handler(
|
||||
void (*handler)(const char *, va_list, void *),
|
||||
void *param)
|
||||
void base_set_crash_handler(void (*handler)(const char *, va_list, void *),
|
||||
void *param)
|
||||
{
|
||||
crash_param = param;
|
||||
crash_param = param;
|
||||
crash_handler = handler;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ enum {
|
|||
* Use in creation functions and core subsystem functions. Places that
|
||||
* should definitely not fail.
|
||||
*/
|
||||
LOG_ERROR = 100,
|
||||
LOG_ERROR = 100,
|
||||
|
||||
/**
|
||||
* Use if a problem occurs that doesn't affect the program and is
|
||||
|
|
@ -57,12 +57,12 @@ enum {
|
|||
/**
|
||||
* Informative message to be displayed in the log.
|
||||
*/
|
||||
LOG_INFO = 300,
|
||||
LOG_INFO = 300,
|
||||
|
||||
/**
|
||||
* Debug message to be used mostly by developers.
|
||||
*/
|
||||
LOG_DEBUG = 400
|
||||
LOG_DEBUG = 400
|
||||
};
|
||||
|
||||
typedef void (*log_handler_t)(int lvl, const char *msg, va_list args, void *p);
|
||||
|
|
@ -70,9 +70,9 @@ typedef void (*log_handler_t)(int lvl, const char *msg, va_list args, void *p);
|
|||
EXPORT void base_get_log_handler(log_handler_t *handler, void **param);
|
||||
EXPORT void base_set_log_handler(log_handler_t handler, void *param);
|
||||
|
||||
EXPORT void base_set_crash_handler(
|
||||
void (*handler)(const char *, va_list, void *),
|
||||
void *param);
|
||||
EXPORT void base_set_crash_handler(void (*handler)(const char *, va_list,
|
||||
void *),
|
||||
void *param);
|
||||
|
||||
EXPORT void blogva(int log_level, const char *format, va_list args);
|
||||
|
||||
|
|
|
|||
|
|
@ -43,10 +43,10 @@ static void *a_malloc(size_t size)
|
|||
void *ptr = NULL;
|
||||
long diff;
|
||||
|
||||
ptr = malloc(size + ALIGNMENT);
|
||||
ptr = malloc(size + ALIGNMENT);
|
||||
if (ptr) {
|
||||
diff = ((~(long)ptr) & (ALIGNMENT - 1)) + 1;
|
||||
ptr = (char *)ptr + diff;
|
||||
ptr = (char *)ptr + diff;
|
||||
((char *)ptr)[-1] = (char)diff;
|
||||
}
|
||||
|
||||
|
|
@ -66,7 +66,7 @@ static void *a_realloc(void *ptr, size_t size)
|
|||
if (!ptr)
|
||||
return a_malloc(size);
|
||||
diff = ((char *)ptr)[-1];
|
||||
ptr = realloc((char*)ptr - diff, size + diff);
|
||||
ptr = realloc((char *)ptr - diff, size + diff);
|
||||
if (ptr)
|
||||
ptr = (char *)ptr + diff;
|
||||
return ptr;
|
||||
|
|
@ -81,7 +81,7 @@ static void a_free(void *ptr)
|
|||
_aligned_free(ptr);
|
||||
#elif ALIGNMENT_HACK
|
||||
if (ptr)
|
||||
free((char *)ptr - ((char*)ptr)[-1]);
|
||||
free((char *)ptr - ((char *)ptr)[-1]);
|
||||
#else
|
||||
free(ptr);
|
||||
#endif
|
||||
|
|
@ -103,7 +103,7 @@ void *bmalloc(size_t size)
|
|||
if (!ptr) {
|
||||
os_breakpoint();
|
||||
bcrash("Out of memory while trying to allocate %lu bytes",
|
||||
(unsigned long)size);
|
||||
(unsigned long)size);
|
||||
}
|
||||
|
||||
os_atomic_inc_long(&num_allocs);
|
||||
|
|
@ -121,7 +121,7 @@ void *brealloc(void *ptr, size_t size)
|
|||
if (!ptr) {
|
||||
os_breakpoint();
|
||||
bcrash("Out of memory while trying to allocate %lu bytes",
|
||||
(unsigned long)size);
|
||||
(unsigned long)size);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ static inline char *bstrdup_n(const char *str, size_t n)
|
|||
if (!str)
|
||||
return NULL;
|
||||
|
||||
dup = (char*)bmemdup(str, n+1);
|
||||
dup = (char *)bmemdup(str, n + 1);
|
||||
dup[n] = 0;
|
||||
|
||||
return dup;
|
||||
|
|
@ -69,7 +69,7 @@ static inline wchar_t *bwstrdup_n(const wchar_t *str, size_t n)
|
|||
if (!str)
|
||||
return NULL;
|
||||
|
||||
dup = (wchar_t*)bmemdup(str, (n+1) * sizeof(wchar_t));
|
||||
dup = (wchar_t *)bmemdup(str, (n + 1) * sizeof(wchar_t));
|
||||
dup[n] = 0;
|
||||
|
||||
return dup;
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@
|
|||
#define DEPRECATED __declspec(deprecated)
|
||||
#define FORCE_INLINE __forceinline
|
||||
#else
|
||||
#define DEPRECATED __attribute__ ((deprecated))
|
||||
#define DEPRECATED __attribute__((deprecated))
|
||||
#define FORCE_INLINE inline __attribute__((always_inline))
|
||||
#endif
|
||||
|
||||
|
|
|
|||
|
|
@ -20,38 +20,62 @@
|
|||
#include "cf-lexer.h"
|
||||
|
||||
static inline void cf_convert_from_escape_literal(char **p_dst,
|
||||
const char **p_src)
|
||||
const char **p_src)
|
||||
{
|
||||
char *dst = *p_dst;
|
||||
const char *src = *p_src;
|
||||
|
||||
switch (*(src++)) {
|
||||
case '\'': *(dst++) = '\''; break;
|
||||
case '\"': *(dst++) = '\"'; break;
|
||||
case '\?': *(dst++) = '\?'; break;
|
||||
case '\\': *(dst++) = '\\'; break;
|
||||
case '0': *(dst++) = '\0'; break;
|
||||
case 'a': *(dst++) = '\a'; break;
|
||||
case 'b': *(dst++) = '\b'; break;
|
||||
case 'f': *(dst++) = '\f'; break;
|
||||
case 'n': *(dst++) = '\n'; break;
|
||||
case 'r': *(dst++) = '\r'; break;
|
||||
case 't': *(dst++) = '\t'; break;
|
||||
case 'v': *(dst++) = '\v'; break;
|
||||
case '\'':
|
||||
*(dst++) = '\'';
|
||||
break;
|
||||
case '\"':
|
||||
*(dst++) = '\"';
|
||||
break;
|
||||
case '\?':
|
||||
*(dst++) = '\?';
|
||||
break;
|
||||
case '\\':
|
||||
*(dst++) = '\\';
|
||||
break;
|
||||
case '0':
|
||||
*(dst++) = '\0';
|
||||
break;
|
||||
case 'a':
|
||||
*(dst++) = '\a';
|
||||
break;
|
||||
case 'b':
|
||||
*(dst++) = '\b';
|
||||
break;
|
||||
case 'f':
|
||||
*(dst++) = '\f';
|
||||
break;
|
||||
case 'n':
|
||||
*(dst++) = '\n';
|
||||
break;
|
||||
case 'r':
|
||||
*(dst++) = '\r';
|
||||
break;
|
||||
case 't':
|
||||
*(dst++) = '\t';
|
||||
break;
|
||||
case 'v':
|
||||
*(dst++) = '\v';
|
||||
break;
|
||||
|
||||
/* hex */
|
||||
case 'X':
|
||||
case 'x':
|
||||
*(dst++) = (char)strtoul(src, NULL, 16);
|
||||
src += 2;
|
||||
break;
|
||||
/* hex */
|
||||
case 'X':
|
||||
case 'x':
|
||||
*(dst++) = (char)strtoul(src, NULL, 16);
|
||||
src += 2;
|
||||
break;
|
||||
|
||||
/* oct */
|
||||
default:
|
||||
if (isdigit(*src)) {
|
||||
*(dst++) = (char)strtoul(src, NULL, 8);
|
||||
src += 3;
|
||||
}
|
||||
/* oct */
|
||||
default:
|
||||
if (isdigit(*src)) {
|
||||
*(dst++) = (char)strtoul(src, NULL, 8);
|
||||
src += 3;
|
||||
}
|
||||
|
||||
/* case 'u':
|
||||
case 'U': */
|
||||
|
|
@ -71,7 +95,7 @@ char *cf_literal_to_str(const char *literal, size_t count)
|
|||
|
||||
if (count < 2)
|
||||
return NULL;
|
||||
if (literal[0] != literal[count-1])
|
||||
if (literal[0] != literal[count - 1])
|
||||
return NULL;
|
||||
if (literal[0] != '\"' && literal[0] != '\'')
|
||||
return NULL;
|
||||
|
|
@ -95,7 +119,7 @@ char *cf_literal_to_str(const char *literal, size_t count)
|
|||
}
|
||||
|
||||
static bool cf_is_token_break(struct base_token *start_token,
|
||||
const struct base_token *token)
|
||||
const struct base_token *token)
|
||||
{
|
||||
switch (start_token->type) {
|
||||
case BASETOKEN_ALPHA:
|
||||
|
|
@ -105,9 +129,9 @@ static bool cf_is_token_break(struct base_token *start_token,
|
|||
break;
|
||||
|
||||
case BASETOKEN_DIGIT:
|
||||
if (token->type == BASETOKEN_WHITESPACE
|
||||
|| (token->type == BASETOKEN_OTHER
|
||||
&& *token->text.array != '.'))
|
||||
if (token->type == BASETOKEN_WHITESPACE ||
|
||||
(token->type == BASETOKEN_OTHER &&
|
||||
*token->text.array != '.'))
|
||||
return true;
|
||||
break;
|
||||
|
||||
|
|
@ -141,7 +165,7 @@ static inline bool cf_is_splice(const char *array)
|
|||
static inline void cf_pass_any_splices(const char **parray)
|
||||
{
|
||||
while (cf_is_splice(*parray))
|
||||
*parray += 1 + newline_size((*parray)+1);
|
||||
*parray += 1 + newline_size((*parray) + 1);
|
||||
}
|
||||
|
||||
static inline bool cf_is_comment(const char *array)
|
||||
|
|
@ -157,7 +181,7 @@ static inline bool cf_is_comment(const char *array)
|
|||
}
|
||||
|
||||
static bool cf_lexer_process_comment(struct cf_lexer *lex,
|
||||
struct cf_token *out_token)
|
||||
struct cf_token *out_token)
|
||||
{
|
||||
const char *offset;
|
||||
|
||||
|
|
@ -200,7 +224,7 @@ static bool cf_lexer_process_comment(struct cf_lexer *lex,
|
|||
}
|
||||
|
||||
static inline void cf_lexer_write_strref(struct cf_lexer *lex,
|
||||
const struct strref *ref)
|
||||
const struct strref *ref)
|
||||
{
|
||||
strncpy(lex->write_offset, ref->array, ref->len);
|
||||
lex->write_offset[ref->len] = 0;
|
||||
|
|
@ -214,14 +238,14 @@ static bool cf_lexer_is_include(struct cf_lexer *lex)
|
|||
size_t i;
|
||||
|
||||
for (i = lex->tokens.num; i > 0; i--) {
|
||||
struct cf_token *token = lex->tokens.array+(i-1);
|
||||
struct cf_token *token = lex->tokens.array + (i - 1);
|
||||
|
||||
if (is_space_or_tab(*token->str.array))
|
||||
continue;
|
||||
|
||||
if (!found_include_import) {
|
||||
if (strref_cmp(&token->str, "include") != 0 &&
|
||||
strref_cmp(&token->str, "import") != 0)
|
||||
strref_cmp(&token->str, "import") != 0)
|
||||
break;
|
||||
|
||||
found_include_import = true;
|
||||
|
|
@ -242,8 +266,8 @@ static bool cf_lexer_is_include(struct cf_lexer *lex)
|
|||
}
|
||||
|
||||
static void cf_lexer_getstrtoken(struct cf_lexer *lex,
|
||||
struct cf_token *out_token, char delimiter,
|
||||
bool allow_escaped_delimiters)
|
||||
struct cf_token *out_token, char delimiter,
|
||||
bool allow_escaped_delimiters)
|
||||
{
|
||||
const char *offset = lex->base_lexer.offset;
|
||||
bool escaped = false;
|
||||
|
|
@ -280,7 +304,7 @@ static void cf_lexer_getstrtoken(struct cf_lexer *lex,
|
|||
}
|
||||
|
||||
static bool cf_lexer_process_string(struct cf_lexer *lex,
|
||||
struct cf_token *out_token)
|
||||
struct cf_token *out_token)
|
||||
{
|
||||
char ch = *out_token->unmerged_str.array;
|
||||
|
||||
|
|
@ -290,15 +314,16 @@ static bool cf_lexer_process_string(struct cf_lexer *lex,
|
|||
|
||||
} else if (ch == '"' || ch == '\'') {
|
||||
cf_lexer_getstrtoken(lex, out_token, ch,
|
||||
!cf_lexer_is_include(lex));
|
||||
!cf_lexer_is_include(lex));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline enum cf_token_type cf_get_token_type(const struct cf_token *token,
|
||||
const struct base_token *start_token)
|
||||
static inline enum cf_token_type
|
||||
cf_get_token_type(const struct cf_token *token,
|
||||
const struct base_token *start_token)
|
||||
{
|
||||
switch (start_token->type) {
|
||||
case BASETOKEN_ALPHA:
|
||||
|
|
@ -338,13 +363,13 @@ static bool cf_lexer_nexttoken(struct cf_lexer *lex, struct cf_token *out_token)
|
|||
/* ignore escaped newlines to merge spliced lines */
|
||||
if (cf_is_splice(token.text.array)) {
|
||||
lex->base_lexer.offset +=
|
||||
newline_size(token.text.array+1);
|
||||
newline_size(token.text.array + 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!wrote_data) {
|
||||
out_token->unmerged_str.array = token.text.array;
|
||||
out_token->str.array = lex->write_offset;
|
||||
out_token->str.array = lex->write_offset;
|
||||
|
||||
/* if comment then output a space */
|
||||
if (cf_lexer_process_comment(lex, out_token))
|
||||
|
|
@ -368,8 +393,8 @@ static bool cf_lexer_nexttoken(struct cf_lexer *lex, struct cf_token *out_token)
|
|||
}
|
||||
|
||||
if (wrote_data) {
|
||||
out_token->unmerged_str.len = (size_t)(lex->base_lexer.offset -
|
||||
out_token->unmerged_str.array);
|
||||
out_token->unmerged_str.len = (size_t)(
|
||||
lex->base_lexer.offset - out_token->unmerged_str.array);
|
||||
out_token->type = cf_get_token_type(out_token, &start_token);
|
||||
}
|
||||
|
||||
|
|
@ -381,9 +406,9 @@ void cf_lexer_init(struct cf_lexer *lex)
|
|||
lexer_init(&lex->base_lexer);
|
||||
da_init(lex->tokens);
|
||||
|
||||
lex->file = NULL;
|
||||
lex->reformatted = NULL;
|
||||
lex->write_offset = NULL;
|
||||
lex->file = NULL;
|
||||
lex->reformatted = NULL;
|
||||
lex->write_offset = NULL;
|
||||
lex->unexpected_eof = false;
|
||||
}
|
||||
|
||||
|
|
@ -394,9 +419,9 @@ void cf_lexer_free(struct cf_lexer *lex)
|
|||
lexer_free(&lex->base_lexer);
|
||||
da_free(lex->tokens);
|
||||
|
||||
lex->file = NULL;
|
||||
lex->reformatted = NULL;
|
||||
lex->write_offset = NULL;
|
||||
lex->file = NULL;
|
||||
lex->reformatted = NULL;
|
||||
lex->write_offset = NULL;
|
||||
lex->unexpected_eof = false;
|
||||
}
|
||||
|
||||
|
|
@ -420,8 +445,7 @@ bool cf_lexer_lex(struct cf_lexer *lex, const char *str, const char *file)
|
|||
lex->write_offset = lex->reformatted;
|
||||
|
||||
while (cf_lexer_nexttoken(lex, &token)) {
|
||||
if (last_token &&
|
||||
is_space_or_tab(*last_token->str.array) &&
|
||||
if (last_token && is_space_or_tab(*last_token->str.array) &&
|
||||
is_space_or_tab(*token.str.array)) {
|
||||
cf_token_add(last_token, &token);
|
||||
continue;
|
||||
|
|
@ -476,20 +500,19 @@ static inline void macro_params_free(struct macro_params *params)
|
|||
{
|
||||
size_t i;
|
||||
for (i = 0; i < params->params.num; i++)
|
||||
macro_param_free(params->params.array+i);
|
||||
macro_param_free(params->params.array + i);
|
||||
da_free(params->params);
|
||||
}
|
||||
|
||||
static inline struct macro_param *get_macro_param(
|
||||
const struct macro_params *params,
|
||||
const struct strref *name)
|
||||
static inline struct macro_param *
|
||||
get_macro_param(const struct macro_params *params, const struct strref *name)
|
||||
{
|
||||
size_t i;
|
||||
if (!params)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < params->params.num; i++) {
|
||||
struct macro_param *param = params->params.array+i;
|
||||
struct macro_param *param = params->params.array + i;
|
||||
if (strref_cmp_strref(¶m->name.str, name) == 0)
|
||||
return param;
|
||||
}
|
||||
|
|
@ -499,10 +522,10 @@ static inline struct macro_param *get_macro_param(
|
|||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
static bool cf_preprocessor(struct cf_preprocessor *pp,
|
||||
bool if_block, struct cf_token **p_cur_token);
|
||||
static void cf_preprocess_tokens(struct cf_preprocessor *pp,
|
||||
bool if_block, struct cf_token **p_cur_token);
|
||||
static bool cf_preprocessor(struct cf_preprocessor *pp, bool if_block,
|
||||
struct cf_token **p_cur_token);
|
||||
static void cf_preprocess_tokens(struct cf_preprocessor *pp, bool if_block,
|
||||
struct cf_token **p_cur_token);
|
||||
|
||||
static inline bool go_to_newline(struct cf_token **p_cur_token)
|
||||
{
|
||||
|
|
@ -524,7 +547,7 @@ static inline bool next_token(struct cf_token **p_cur_token, bool preprocessor)
|
|||
cur_token++;
|
||||
|
||||
/* if preprocessor, stop at newline */
|
||||
while (cur_token->type == CFTOKEN_SPACETAB &&
|
||||
while (cur_token->type == CFTOKEN_SPACETAB &&
|
||||
(preprocessor || cur_token->type == CFTOKEN_NEWLINE))
|
||||
cur_token++;
|
||||
|
||||
|
|
@ -533,79 +556,82 @@ static inline bool next_token(struct cf_token **p_cur_token, bool preprocessor)
|
|||
}
|
||||
|
||||
static inline void cf_gettokenoffset(struct cf_preprocessor *pp,
|
||||
const struct cf_token *token, uint32_t *row, uint32_t *col)
|
||||
const struct cf_token *token,
|
||||
uint32_t *row, uint32_t *col)
|
||||
{
|
||||
lexer_getstroffset(&pp->lex->base_lexer,
|
||||
token->unmerged_str.array, row, col);
|
||||
lexer_getstroffset(&pp->lex->base_lexer, token->unmerged_str.array, row,
|
||||
col);
|
||||
}
|
||||
|
||||
static void cf_addew(struct cf_preprocessor *pp, const struct cf_token *token,
|
||||
const char *message, int error_level,
|
||||
const char *val1, const char *val2, const char *val3)
|
||||
const char *message, int error_level, const char *val1,
|
||||
const char *val2, const char *val3)
|
||||
{
|
||||
uint32_t row, col;
|
||||
cf_gettokenoffset(pp, token, &row, &col);
|
||||
|
||||
if (!val1 && !val2 && !val3) {
|
||||
error_data_add(pp->ed, token->lex->file, row, col,
|
||||
message, error_level);
|
||||
error_data_add(pp->ed, token->lex->file, row, col, message,
|
||||
error_level);
|
||||
} else {
|
||||
struct dstr formatted;
|
||||
dstr_init(&formatted);
|
||||
dstr_safe_printf(&formatted, message, val1, val2, val3, NULL);
|
||||
|
||||
error_data_add(pp->ed, token->lex->file, row, col,
|
||||
formatted.array, error_level);
|
||||
formatted.array, error_level);
|
||||
dstr_free(&formatted);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void cf_adderror(struct cf_preprocessor *pp,
|
||||
const struct cf_token *token, const char *error,
|
||||
const char *val1, const char *val2, const char *val3)
|
||||
const struct cf_token *token, const char *error,
|
||||
const char *val1, const char *val2,
|
||||
const char *val3)
|
||||
{
|
||||
cf_addew(pp, token, error, LEX_ERROR, val1, val2, val3);
|
||||
}
|
||||
|
||||
static inline void cf_addwarning(struct cf_preprocessor *pp,
|
||||
const struct cf_token *token, const char *warning,
|
||||
const char *val1, const char *val2, const char *val3)
|
||||
const struct cf_token *token,
|
||||
const char *warning, const char *val1,
|
||||
const char *val2, const char *val3)
|
||||
{
|
||||
cf_addew(pp, token, warning, LEX_WARNING, val1, val2, val3);
|
||||
}
|
||||
|
||||
static inline void cf_adderror_expecting(struct cf_preprocessor *pp,
|
||||
const struct cf_token *token, const char *expecting)
|
||||
const struct cf_token *token,
|
||||
const char *expecting)
|
||||
{
|
||||
cf_adderror(pp, token, "Expected $1", expecting,
|
||||
NULL, NULL);
|
||||
cf_adderror(pp, token, "Expected $1", expecting, NULL, NULL);
|
||||
}
|
||||
|
||||
static inline void cf_adderror_expected_newline(struct cf_preprocessor *pp,
|
||||
const struct cf_token *token)
|
||||
const struct cf_token *token)
|
||||
{
|
||||
cf_adderror(pp, token,
|
||||
"Unexpected token after preprocessor, expected "
|
||||
"newline",
|
||||
NULL, NULL, NULL);
|
||||
"Unexpected token after preprocessor, expected "
|
||||
"newline",
|
||||
NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
static inline void cf_adderror_unexpected_endif_eof(struct cf_preprocessor *pp,
|
||||
const struct cf_token *token)
|
||||
static inline void
|
||||
cf_adderror_unexpected_endif_eof(struct cf_preprocessor *pp,
|
||||
const struct cf_token *token)
|
||||
{
|
||||
cf_adderror(pp, token, "Unexpected end of file before #endif",
|
||||
NULL, NULL, NULL);
|
||||
cf_adderror(pp, token, "Unexpected end of file before #endif", NULL,
|
||||
NULL, NULL);
|
||||
}
|
||||
|
||||
static inline void cf_adderror_unexpected_eof(struct cf_preprocessor *pp,
|
||||
const struct cf_token *token)
|
||||
const struct cf_token *token)
|
||||
{
|
||||
cf_adderror(pp, token, "Unexpected end of file",
|
||||
NULL, NULL, NULL);
|
||||
cf_adderror(pp, token, "Unexpected end of file", NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
static inline void insert_path(struct cf_preprocessor *pp,
|
||||
struct dstr *str_file)
|
||||
struct dstr *str_file)
|
||||
{
|
||||
const char *file;
|
||||
const char *slash;
|
||||
|
|
@ -623,7 +649,7 @@ static inline void insert_path(struct cf_preprocessor *pp,
|
|||
}
|
||||
|
||||
static void cf_include_file(struct cf_preprocessor *pp,
|
||||
const struct cf_token *file_token)
|
||||
const struct cf_token *file_token)
|
||||
{
|
||||
struct cf_lexer new_lex;
|
||||
struct dstr str_file;
|
||||
|
|
@ -634,12 +660,12 @@ static void cf_include_file(struct cf_preprocessor *pp,
|
|||
|
||||
dstr_init(&str_file);
|
||||
dstr_copy_strref(&str_file, &file_token->str);
|
||||
dstr_mid(&str_file, &str_file, 1, str_file.len-2);
|
||||
dstr_mid(&str_file, &str_file, 1, str_file.len - 2);
|
||||
insert_path(pp, &str_file);
|
||||
|
||||
/* if dependency already exists, run preprocessor on it */
|
||||
for (i = 0; i < pp->dependencies.num; i++) {
|
||||
struct cf_lexer *dep = pp->dependencies.array+i;
|
||||
struct cf_lexer *dep = pp->dependencies.array + i;
|
||||
|
||||
if (strcmp(dep->file, str_file.array) == 0) {
|
||||
tokens = cf_lexer_get_tokens(dep);
|
||||
|
|
@ -651,7 +677,7 @@ static void cf_include_file(struct cf_preprocessor *pp,
|
|||
file = os_fopen(str_file.array, "rb");
|
||||
if (!file) {
|
||||
cf_adderror(pp, file_token, "Could not open file '$1'",
|
||||
file_token->str.array, NULL, NULL);
|
||||
file_token->str.array, NULL, NULL);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -672,18 +698,18 @@ exit:
|
|||
|
||||
static inline bool is_sys_include(struct strref *ref)
|
||||
{
|
||||
return ref->len >= 2 &&
|
||||
ref->array[0] == '<' && ref->array[ref->len-1] == '>';
|
||||
return ref->len >= 2 && ref->array[0] == '<' &&
|
||||
ref->array[ref->len - 1] == '>';
|
||||
}
|
||||
|
||||
static inline bool is_loc_include(struct strref *ref)
|
||||
{
|
||||
return ref->len >= 2 &&
|
||||
ref->array[0] == '"' && ref->array[ref->len-1] == '"';
|
||||
return ref->len >= 2 && ref->array[0] == '"' &&
|
||||
ref->array[ref->len - 1] == '"';
|
||||
}
|
||||
|
||||
static void cf_preprocess_include(struct cf_preprocessor *pp,
|
||||
struct cf_token **p_cur_token)
|
||||
struct cf_token **p_cur_token)
|
||||
{
|
||||
struct cf_token *cur_token = *p_cur_token;
|
||||
|
||||
|
|
@ -706,8 +732,8 @@ static void cf_preprocess_include(struct cf_preprocessor *pp,
|
|||
if (!pp->ignore_state)
|
||||
cf_include_file(pp, cur_token);
|
||||
} else {
|
||||
cf_adderror(pp, cur_token, "Invalid or incomplete string",
|
||||
NULL, NULL, NULL);
|
||||
cf_adderror(pp, cur_token, "Invalid or incomplete string", NULL,
|
||||
NULL, NULL);
|
||||
go_to_newline(&cur_token);
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -719,7 +745,8 @@ exit:
|
|||
}
|
||||
|
||||
static bool cf_preprocess_macro_params(struct cf_preprocessor *pp,
|
||||
struct cf_def *def, struct cf_token **p_cur_token)
|
||||
struct cf_def *def,
|
||||
struct cf_token **p_cur_token)
|
||||
{
|
||||
struct cf_token *cur_token = *p_cur_token;
|
||||
bool success = false;
|
||||
|
|
@ -736,9 +763,9 @@ static bool cf_preprocess_macro_params(struct cf_preprocessor *pp,
|
|||
cf_def_addparam(def, cur_token);
|
||||
|
||||
next_token(&cur_token, true);
|
||||
if (cur_token->type != CFTOKEN_OTHER
|
||||
|| (*cur_token->str.array != ','
|
||||
&& *cur_token->str.array != ')')) {
|
||||
if (cur_token->type != CFTOKEN_OTHER ||
|
||||
(*cur_token->str.array != ',' &&
|
||||
*cur_token->str.array != ')')) {
|
||||
|
||||
cf_adderror_expecting(pp, cur_token, "',' or ')'");
|
||||
go_to_newline(&cur_token);
|
||||
|
|
@ -758,13 +785,13 @@ exit:
|
|||
#define INVALID_INDEX ((size_t)-1)
|
||||
|
||||
static inline size_t cf_preprocess_get_def_idx(struct cf_preprocessor *pp,
|
||||
const struct strref *def_name)
|
||||
const struct strref *def_name)
|
||||
{
|
||||
struct cf_def *array = pp->defines.array;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < pp->defines.num; i++) {
|
||||
struct cf_def *cur_def = array+i;
|
||||
struct cf_def *cur_def = array + i;
|
||||
|
||||
if (strref_cmp_strref(&cur_def->name.str, def_name) == 0)
|
||||
return i;
|
||||
|
|
@ -773,23 +800,24 @@ static inline size_t cf_preprocess_get_def_idx(struct cf_preprocessor *pp,
|
|||
return INVALID_INDEX;
|
||||
}
|
||||
|
||||
static inline struct cf_def *cf_preprocess_get_def(struct cf_preprocessor *pp,
|
||||
const struct strref *def_name)
|
||||
static inline struct cf_def *
|
||||
cf_preprocess_get_def(struct cf_preprocessor *pp, const struct strref *def_name)
|
||||
{
|
||||
size_t idx = cf_preprocess_get_def_idx(pp, def_name);
|
||||
if (idx == INVALID_INDEX)
|
||||
return NULL;
|
||||
|
||||
return pp->defines.array+idx;
|
||||
return pp->defines.array + idx;
|
||||
}
|
||||
|
||||
static char space_filler[2] = " ";
|
||||
|
||||
static inline void append_space(struct cf_preprocessor *pp,
|
||||
struct darray *tokens, const struct cf_token *base)
|
||||
struct darray *tokens,
|
||||
const struct cf_token *base)
|
||||
{
|
||||
struct cf_token token;
|
||||
|
||||
|
||||
strref_set(&token.str, space_filler, 1);
|
||||
token.type = CFTOKEN_SPACETAB;
|
||||
if (base) {
|
||||
|
|
@ -811,7 +839,7 @@ static inline void append_end_token(struct darray *tokens)
|
|||
}
|
||||
|
||||
static void cf_preprocess_define(struct cf_preprocessor *pp,
|
||||
struct cf_token **p_cur_token)
|
||||
struct cf_token **p_cur_token)
|
||||
{
|
||||
struct cf_token *cur_token = *p_cur_token;
|
||||
struct cf_def def;
|
||||
|
|
@ -860,18 +888,18 @@ exit:
|
|||
}
|
||||
|
||||
static inline void cf_preprocess_remove_def_strref(struct cf_preprocessor *pp,
|
||||
const struct strref *ref)
|
||||
const struct strref *ref)
|
||||
{
|
||||
size_t def_idx = cf_preprocess_get_def_idx(pp, ref);
|
||||
if (def_idx != INVALID_INDEX) {
|
||||
struct cf_def *array = pp->defines.array;
|
||||
cf_def_free(array+def_idx);
|
||||
cf_def_free(array + def_idx);
|
||||
da_erase(pp->defines, def_idx);
|
||||
}
|
||||
}
|
||||
|
||||
static void cf_preprocess_undef(struct cf_preprocessor *pp,
|
||||
struct cf_token **p_cur_token)
|
||||
struct cf_token **p_cur_token)
|
||||
{
|
||||
struct cf_token *cur_token = *p_cur_token;
|
||||
|
||||
|
|
@ -896,7 +924,8 @@ exit:
|
|||
|
||||
/* Processes an #ifdef/#ifndef/#if/#else/#elif sub block recursively */
|
||||
static inline bool cf_preprocess_subblock(struct cf_preprocessor *pp,
|
||||
bool ignore, struct cf_token **p_cur_token)
|
||||
bool ignore,
|
||||
struct cf_token **p_cur_token)
|
||||
{
|
||||
bool eof;
|
||||
|
||||
|
|
@ -905,7 +934,7 @@ static inline bool cf_preprocess_subblock(struct cf_preprocessor *pp,
|
|||
|
||||
if (!pp->ignore_state) {
|
||||
pp->ignore_state = ignore;
|
||||
cf_preprocess_tokens(pp, true, p_cur_token);
|
||||
cf_preprocess_tokens(pp, true, p_cur_token);
|
||||
pp->ignore_state = false;
|
||||
} else {
|
||||
cf_preprocess_tokens(pp, true, p_cur_token);
|
||||
|
|
@ -917,8 +946,8 @@ static inline bool cf_preprocess_subblock(struct cf_preprocessor *pp,
|
|||
return !eof;
|
||||
}
|
||||
|
||||
static void cf_preprocess_ifdef(struct cf_preprocessor *pp,
|
||||
bool ifnot, struct cf_token **p_cur_token)
|
||||
static void cf_preprocess_ifdef(struct cf_preprocessor *pp, bool ifnot,
|
||||
struct cf_token **p_cur_token)
|
||||
{
|
||||
struct cf_token *cur_token = *p_cur_token;
|
||||
struct cf_def *def;
|
||||
|
|
@ -940,7 +969,7 @@ static void cf_preprocess_ifdef(struct cf_preprocessor *pp,
|
|||
if (strref_cmp(&cur_token->str, "else") == 0) {
|
||||
if (!cf_preprocess_subblock(pp, is_true, &cur_token))
|
||||
goto exit;
|
||||
/*} else if (strref_cmp(&cur_token->str, "elif") == 0) {*/
|
||||
/*} else if (strref_cmp(&cur_token->str, "elif") == 0) {*/
|
||||
}
|
||||
|
||||
cur_token++;
|
||||
|
|
@ -949,8 +978,8 @@ exit:
|
|||
*p_cur_token = cur_token;
|
||||
}
|
||||
|
||||
static bool cf_preprocessor(struct cf_preprocessor *pp,
|
||||
bool if_block, struct cf_token **p_cur_token)
|
||||
static bool cf_preprocessor(struct cf_preprocessor *pp, bool if_block,
|
||||
struct cf_token **p_cur_token)
|
||||
{
|
||||
struct cf_token *cur_token = *p_cur_token;
|
||||
|
||||
|
|
@ -969,17 +998,18 @@ static bool cf_preprocessor(struct cf_preprocessor *pp,
|
|||
} else if (strref_cmp(&cur_token->str, "ifndef") == 0) {
|
||||
cf_preprocess_ifdef(pp, true, p_cur_token);
|
||||
|
||||
/*} else if (strref_cmp(&cur_token->str, "if") == 0) {
|
||||
/*} else if (strref_cmp(&cur_token->str, "if") == 0) {
|
||||
TODO;*/
|
||||
} else if (strref_cmp(&cur_token->str, "else") == 0 ||
|
||||
/*strref_cmp(&cur_token->str, "elif") == 0 ||*/
|
||||
strref_cmp(&cur_token->str, "endif") == 0) {
|
||||
/*strref_cmp(&cur_token->str, "elif") == 0 ||*/
|
||||
strref_cmp(&cur_token->str, "endif") == 0) {
|
||||
if (!if_block) {
|
||||
struct dstr name;
|
||||
dstr_init_copy_strref(&name, &cur_token->str);
|
||||
cf_adderror(pp, cur_token,"#$1 outside of "
|
||||
"#if/#ifdef/#ifndef block",
|
||||
name.array, NULL, NULL);
|
||||
cf_adderror(pp, cur_token,
|
||||
"#$1 outside of "
|
||||
"#if/#ifdef/#ifndef block",
|
||||
name.array, NULL, NULL);
|
||||
dstr_free(&name);
|
||||
(*p_cur_token)++;
|
||||
|
||||
|
|
@ -989,7 +1019,7 @@ static bool cf_preprocessor(struct cf_preprocessor *pp,
|
|||
return false;
|
||||
|
||||
} else if (cur_token->type != CFTOKEN_NEWLINE &&
|
||||
cur_token->type != CFTOKEN_NONE) {
|
||||
cur_token->type != CFTOKEN_NONE) {
|
||||
/*
|
||||
* TODO: language-specific preprocessor stuff should be sent to
|
||||
* handler of some sort
|
||||
|
|
@ -1001,10 +1031,10 @@ static bool cf_preprocessor(struct cf_preprocessor *pp,
|
|||
}
|
||||
|
||||
static void cf_preprocess_addtoken(struct cf_preprocessor *pp,
|
||||
struct darray *dst, /* struct cf_token */
|
||||
struct cf_token **p_cur_token,
|
||||
const struct cf_token *base,
|
||||
const struct macro_params *params);
|
||||
struct darray *dst, /* struct cf_token */
|
||||
struct cf_token **p_cur_token,
|
||||
const struct cf_token *base,
|
||||
const struct macro_params *params);
|
||||
|
||||
/*
|
||||
* collects tokens for a macro parameter
|
||||
|
|
@ -1013,10 +1043,10 @@ static void cf_preprocess_addtoken(struct cf_preprocessor *pp,
|
|||
* within a macro parameter is preserved, example MACRO(func(1, 2), 3), do not
|
||||
* let it stop on the comma at "1,"
|
||||
*/
|
||||
static void cf_preprocess_save_macro_param(struct cf_preprocessor *pp,
|
||||
struct cf_token **p_cur_token, struct macro_param *param,
|
||||
const struct cf_token *base,
|
||||
const struct macro_params *cur_params)
|
||||
static void cf_preprocess_save_macro_param(
|
||||
struct cf_preprocessor *pp, struct cf_token **p_cur_token,
|
||||
struct macro_param *param, const struct cf_token *base,
|
||||
const struct macro_params *cur_params)
|
||||
{
|
||||
struct cf_token *cur_token = *p_cur_token;
|
||||
int brace_count = 0;
|
||||
|
|
@ -1037,7 +1067,7 @@ static void cf_preprocess_save_macro_param(struct cf_preprocessor *pp,
|
|||
}
|
||||
|
||||
cf_preprocess_addtoken(pp, ¶m->tokens.da, &cur_token, base,
|
||||
cur_params);
|
||||
cur_params);
|
||||
}
|
||||
|
||||
if (cur_token->type == CFTOKEN_NONE)
|
||||
|
|
@ -1064,11 +1094,10 @@ static inline bool param_is_whitespace(const struct macro_param *param)
|
|||
}
|
||||
|
||||
/* collects parameter tokens of a used macro and stores them for the unwrap */
|
||||
static void cf_preprocess_save_macro_params(struct cf_preprocessor *pp,
|
||||
struct cf_token **p_cur_token, const struct cf_def *def,
|
||||
const struct cf_token *base,
|
||||
const struct macro_params *cur_params,
|
||||
struct macro_params *dst)
|
||||
static void cf_preprocess_save_macro_params(
|
||||
struct cf_preprocessor *pp, struct cf_token **p_cur_token,
|
||||
const struct cf_def *def, const struct cf_token *base,
|
||||
const struct macro_params *cur_params, struct macro_params *dst)
|
||||
{
|
||||
struct cf_token *cur_token = *p_cur_token;
|
||||
size_t count = 0;
|
||||
|
|
@ -1086,10 +1115,10 @@ static void cf_preprocess_save_macro_params(struct cf_preprocessor *pp,
|
|||
count++;
|
||||
|
||||
cf_preprocess_save_macro_param(pp, &cur_token, ¶m, base,
|
||||
cur_params);
|
||||
if (cur_token->type != CFTOKEN_OTHER
|
||||
|| (*cur_token->str.array != ','
|
||||
&& *cur_token->str.array != ')')) {
|
||||
cur_params);
|
||||
if (cur_token->type != CFTOKEN_OTHER ||
|
||||
(*cur_token->str.array != ',' &&
|
||||
*cur_token->str.array != ')')) {
|
||||
|
||||
macro_param_free(¶m);
|
||||
cf_adderror_expecting(pp, cur_token, "',' or ')'");
|
||||
|
|
@ -1107,7 +1136,7 @@ static void cf_preprocess_save_macro_params(struct cf_preprocessor *pp,
|
|||
|
||||
if (count <= def->params.num) {
|
||||
cf_token_copy(¶m.name,
|
||||
cf_def_getparam(def, count-1));
|
||||
cf_def_getparam(def, count - 1));
|
||||
da_push_back(dst->params, ¶m);
|
||||
} else {
|
||||
macro_param_free(¶m);
|
||||
|
|
@ -1116,18 +1145,17 @@ static void cf_preprocess_save_macro_params(struct cf_preprocessor *pp,
|
|||
|
||||
if (count != def->params.num)
|
||||
cf_adderror(pp, cur_token,
|
||||
"Mismatching number of macro parameters",
|
||||
NULL, NULL, NULL);
|
||||
"Mismatching number of macro parameters", NULL,
|
||||
NULL, NULL);
|
||||
|
||||
exit:
|
||||
*p_cur_token = cur_token;
|
||||
}
|
||||
|
||||
static inline void cf_preprocess_unwrap_param(struct cf_preprocessor *pp,
|
||||
struct darray *dst, /* struct cf_token */
|
||||
struct cf_token **p_cur_token,
|
||||
const struct cf_token *base,
|
||||
const struct macro_param *param)
|
||||
static inline void cf_preprocess_unwrap_param(
|
||||
struct cf_preprocessor *pp, struct darray *dst, /* struct cf_token */
|
||||
struct cf_token **p_cur_token, const struct cf_token *base,
|
||||
const struct macro_param *param)
|
||||
{
|
||||
struct cf_token *cur_token = *p_cur_token;
|
||||
struct cf_token *cur_param_token = param->tokens.array;
|
||||
|
|
@ -1139,12 +1167,10 @@ static inline void cf_preprocess_unwrap_param(struct cf_preprocessor *pp,
|
|||
*p_cur_token = cur_token;
|
||||
}
|
||||
|
||||
static inline void cf_preprocess_unwrap_define(struct cf_preprocessor *pp,
|
||||
struct darray *dst, /* struct cf_token */
|
||||
struct cf_token **p_cur_token,
|
||||
const struct cf_token *base,
|
||||
const struct cf_def *def,
|
||||
const struct macro_params *cur_params)
|
||||
static inline void cf_preprocess_unwrap_define(
|
||||
struct cf_preprocessor *pp, struct darray *dst, /* struct cf_token */
|
||||
struct cf_token **p_cur_token, const struct cf_token *base,
|
||||
const struct cf_def *def, const struct macro_params *cur_params)
|
||||
{
|
||||
struct cf_token *cur_token = *p_cur_token;
|
||||
struct macro_params new_params;
|
||||
|
|
@ -1154,11 +1180,11 @@ static inline void cf_preprocess_unwrap_define(struct cf_preprocessor *pp,
|
|||
|
||||
if (def->macro)
|
||||
cf_preprocess_save_macro_params(pp, &cur_token, def, base,
|
||||
cur_params, &new_params);
|
||||
cur_params, &new_params);
|
||||
|
||||
while (cur_def_token->type != CFTOKEN_NONE)
|
||||
cf_preprocess_addtoken(pp, dst, &cur_def_token, base,
|
||||
&new_params);
|
||||
&new_params);
|
||||
|
||||
macro_params_free(&new_params);
|
||||
|
||||
|
|
@ -1167,10 +1193,10 @@ static inline void cf_preprocess_unwrap_define(struct cf_preprocessor *pp,
|
|||
}
|
||||
|
||||
static void cf_preprocess_addtoken(struct cf_preprocessor *pp,
|
||||
struct darray *dst, /* struct cf_token */
|
||||
struct cf_token **p_cur_token,
|
||||
const struct cf_token *base,
|
||||
const struct macro_params *params)
|
||||
struct darray *dst, /* struct cf_token */
|
||||
struct cf_token **p_cur_token,
|
||||
const struct cf_token *base,
|
||||
const struct macro_params *params)
|
||||
{
|
||||
struct cf_token *cur_token = *p_cur_token;
|
||||
|
||||
|
|
@ -1187,14 +1213,14 @@ static void cf_preprocess_addtoken(struct cf_preprocessor *pp,
|
|||
param = get_macro_param(params, &cur_token->str);
|
||||
if (param) {
|
||||
cf_preprocess_unwrap_param(pp, dst, &cur_token, base,
|
||||
param);
|
||||
param);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
def = cf_preprocess_get_def(pp, &cur_token->str);
|
||||
if (def) {
|
||||
cf_preprocess_unwrap_define(pp, dst, &cur_token, base,
|
||||
def, params);
|
||||
def, params);
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
|
@ -1208,16 +1234,16 @@ exit:
|
|||
*p_cur_token = cur_token;
|
||||
}
|
||||
|
||||
static void cf_preprocess_tokens(struct cf_preprocessor *pp,
|
||||
bool if_block, struct cf_token **p_cur_token)
|
||||
static void cf_preprocess_tokens(struct cf_preprocessor *pp, bool if_block,
|
||||
struct cf_token **p_cur_token)
|
||||
{
|
||||
bool newline = true;
|
||||
bool preprocessor_line = if_block;
|
||||
struct cf_token *cur_token = *p_cur_token;
|
||||
|
||||
while (cur_token->type != CFTOKEN_NONE) {
|
||||
if(cur_token->type != CFTOKEN_SPACETAB &&
|
||||
cur_token->type != CFTOKEN_NEWLINE) {
|
||||
if (cur_token->type != CFTOKEN_SPACETAB &&
|
||||
cur_token->type != CFTOKEN_NEWLINE) {
|
||||
if (preprocessor_line) {
|
||||
cf_adderror_expected_newline(pp, cur_token);
|
||||
if (!go_to_newline(&cur_token))
|
||||
|
|
@ -1244,7 +1270,7 @@ static void cf_preprocess_tokens(struct cf_preprocessor *pp,
|
|||
}
|
||||
|
||||
cf_preprocess_addtoken(pp, &pp->tokens.da, &cur_token, NULL,
|
||||
NULL);
|
||||
NULL);
|
||||
}
|
||||
|
||||
*p_cur_token = cur_token;
|
||||
|
|
@ -1268,12 +1294,12 @@ void cf_preprocessor_free(struct cf_preprocessor *pp)
|
|||
struct cf_def *defs = pp->defines.array;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i <pp->defines.num; i++)
|
||||
cf_def_free(defs+i);
|
||||
for (i = 0; i < pp->defines.num; i++)
|
||||
cf_def_free(defs + i);
|
||||
for (i = 0; i < pp->sys_include_dirs.num; i++)
|
||||
bfree(sys_include_dirs[i]);
|
||||
for (i = 0; i < pp->dependencies.num; i++)
|
||||
cf_lexer_free(dependencies+i);
|
||||
cf_lexer_free(dependencies + i);
|
||||
|
||||
da_free(pp->defines);
|
||||
da_free(pp->sys_include_dirs);
|
||||
|
|
@ -1286,7 +1312,7 @@ void cf_preprocessor_free(struct cf_preprocessor *pp)
|
|||
}
|
||||
|
||||
bool cf_preprocess(struct cf_preprocessor *pp, struct cf_lexer *lex,
|
||||
struct error_data *ed)
|
||||
struct error_data *ed)
|
||||
{
|
||||
struct cf_token *token = cf_lexer_get_tokens(lex);
|
||||
if (!token)
|
||||
|
|
@ -1308,10 +1334,10 @@ void cf_preprocessor_add_def(struct cf_preprocessor *pp, struct cf_def *def)
|
|||
struct dstr name;
|
||||
dstr_init_copy_strref(&name, &def->name.str);
|
||||
cf_addwarning(pp, &def->name, "Token $1 already defined",
|
||||
name.array, NULL, NULL);
|
||||
name.array, NULL, NULL);
|
||||
cf_addwarning(pp, &existing->name,
|
||||
"Previous definition of $1 is here",
|
||||
name.array, NULL, NULL);
|
||||
"Previous definition of $1 is here", name.array,
|
||||
NULL, NULL);
|
||||
|
||||
cf_def_free(existing);
|
||||
memcpy(existing, def, sizeof(struct cf_def));
|
||||
|
|
@ -1321,10 +1347,10 @@ void cf_preprocessor_add_def(struct cf_preprocessor *pp, struct cf_def *def)
|
|||
}
|
||||
|
||||
void cf_preprocessor_remove_def(struct cf_preprocessor *pp,
|
||||
const char *def_name)
|
||||
const char *def_name)
|
||||
{
|
||||
struct strref ref;
|
||||
ref.array = def_name;
|
||||
ref.len = strlen(def_name);
|
||||
ref.len = strlen(def_name);
|
||||
cf_preprocess_remove_def_strref(pp, &ref);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,13 +57,13 @@ static inline void cf_token_clear(struct cf_token *t)
|
|||
}
|
||||
|
||||
static inline void cf_token_copy(struct cf_token *dst,
|
||||
const struct cf_token *src)
|
||||
const struct cf_token *src)
|
||||
{
|
||||
memcpy(dst, src, sizeof(struct cf_token));
|
||||
}
|
||||
|
||||
static inline void cf_token_add(struct cf_token *dst,
|
||||
const struct cf_token *add)
|
||||
const struct cf_token *add)
|
||||
{
|
||||
strref_add(&dst->str, &add->str);
|
||||
strref_add(&dst->unmerged_str, &add->unmerged_str);
|
||||
|
|
@ -99,7 +99,7 @@ static inline struct cf_token *cf_lexer_get_tokens(struct cf_lexer *lex)
|
|||
}
|
||||
|
||||
EXPORT bool cf_lexer_lex(struct cf_lexer *lex, const char *str,
|
||||
const char *file);
|
||||
const char *file);
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* c-family preprocessor definition */
|
||||
|
|
@ -130,9 +130,9 @@ static inline void cf_def_addtoken(struct cf_def *cfd, struct cf_token *token)
|
|||
}
|
||||
|
||||
static inline struct cf_token *cf_def_getparam(const struct cf_def *cfd,
|
||||
size_t idx)
|
||||
size_t idx)
|
||||
{
|
||||
return cfd->params.array+idx;
|
||||
return cfd->params.array + idx;
|
||||
}
|
||||
|
||||
static inline void cf_def_free(struct cf_def *cfd)
|
||||
|
|
@ -170,8 +170,8 @@ static inline void cf_def_free(struct cf_def *cfd)
|
|||
struct cf_preprocessor {
|
||||
struct cf_lexer *lex;
|
||||
struct error_data *ed;
|
||||
DARRAY(struct cf_def) defines;
|
||||
DARRAY(char*) sys_include_dirs;
|
||||
DARRAY(struct cf_def) defines;
|
||||
DARRAY(char *) sys_include_dirs;
|
||||
DARRAY(struct cf_lexer) dependencies;
|
||||
DARRAY(struct cf_token) tokens;
|
||||
bool ignore_state;
|
||||
|
|
@ -181,22 +181,23 @@ EXPORT void cf_preprocessor_init(struct cf_preprocessor *pp);
|
|||
EXPORT void cf_preprocessor_free(struct cf_preprocessor *pp);
|
||||
|
||||
EXPORT bool cf_preprocess(struct cf_preprocessor *pp, struct cf_lexer *lex,
|
||||
struct error_data *ed);
|
||||
struct error_data *ed);
|
||||
|
||||
static inline void cf_preprocessor_add_sys_include_dir(
|
||||
struct cf_preprocessor *pp, const char *include_dir)
|
||||
static inline void
|
||||
cf_preprocessor_add_sys_include_dir(struct cf_preprocessor *pp,
|
||||
const char *include_dir)
|
||||
{
|
||||
if (include_dir)
|
||||
da_push_back(pp->sys_include_dirs, bstrdup(include_dir));
|
||||
}
|
||||
|
||||
EXPORT void cf_preprocessor_add_def(struct cf_preprocessor *pp,
|
||||
struct cf_def *def);
|
||||
struct cf_def *def);
|
||||
EXPORT void cf_preprocessor_remove_def(struct cf_preprocessor *pp,
|
||||
const char *def_name);
|
||||
const char *def_name);
|
||||
|
||||
static inline struct cf_token *cf_preprocessor_get_tokens(
|
||||
struct cf_preprocessor *pp)
|
||||
static inline struct cf_token *
|
||||
cf_preprocessor_get_tokens(struct cf_preprocessor *pp)
|
||||
{
|
||||
return pp->tokens.array;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,23 +17,22 @@
|
|||
#include "cf-parser.h"
|
||||
|
||||
void cf_adderror(struct cf_parser *p, const char *error, int level,
|
||||
const char *val1, const char *val2, const char *val3)
|
||||
const char *val1, const char *val2, const char *val3)
|
||||
{
|
||||
uint32_t row, col;
|
||||
lexer_getstroffset(&p->cur_token->lex->base_lexer,
|
||||
p->cur_token->unmerged_str.array,
|
||||
&row, &col);
|
||||
p->cur_token->unmerged_str.array, &row, &col);
|
||||
|
||||
if (!val1 && !val2 && !val3) {
|
||||
error_data_add(&p->error_list, p->cur_token->lex->file,
|
||||
row, col, error, level);
|
||||
error_data_add(&p->error_list, p->cur_token->lex->file, row,
|
||||
col, error, level);
|
||||
} else {
|
||||
struct dstr formatted;
|
||||
dstr_init(&formatted);
|
||||
dstr_safe_printf(&formatted, error, val1, val2, val3, NULL);
|
||||
|
||||
error_data_add(&p->error_list, p->cur_token->lex->file,
|
||||
row, col, formatted.array, level);
|
||||
error_data_add(&p->error_list, p->cur_token->lex->file, row,
|
||||
col, formatted.array, level);
|
||||
|
||||
dstr_free(&formatted);
|
||||
}
|
||||
|
|
@ -53,7 +52,7 @@ bool cf_pass_pair(struct cf_parser *p, char in, char out)
|
|||
break;
|
||||
continue;
|
||||
|
||||
} else if(*p->cur_token->str.array == out) {
|
||||
} else if (*p->cur_token->str.array == out) {
|
||||
p->cur_token++;
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,19 +30,19 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define PARSE_SUCCESS 0
|
||||
#define PARSE_CONTINUE -1
|
||||
#define PARSE_BREAK -2
|
||||
#define PARSE_SUCCESS 0
|
||||
#define PARSE_CONTINUE -1
|
||||
#define PARSE_BREAK -2
|
||||
#define PARSE_UNEXPECTED_CONTINUE -3
|
||||
#define PARSE_UNEXPECTED_BREAK -4
|
||||
#define PARSE_EOF -5
|
||||
#define PARSE_UNEXPECTED_BREAK -4
|
||||
#define PARSE_EOF -5
|
||||
|
||||
struct cf_parser {
|
||||
struct cf_lexer lex;
|
||||
struct cf_lexer lex;
|
||||
struct cf_preprocessor pp;
|
||||
struct error_data error_list;
|
||||
struct error_data error_list;
|
||||
|
||||
struct cf_token *cur_token;
|
||||
struct cf_token *cur_token;
|
||||
};
|
||||
|
||||
static inline void cf_parser_init(struct cf_parser *parser)
|
||||
|
|
@ -63,8 +63,8 @@ static inline void cf_parser_free(struct cf_parser *parser)
|
|||
parser->cur_token = NULL;
|
||||
}
|
||||
|
||||
static inline bool cf_parser_parse(struct cf_parser *parser,
|
||||
const char *str, const char *file)
|
||||
static inline bool cf_parser_parse(struct cf_parser *parser, const char *str,
|
||||
const char *file)
|
||||
{
|
||||
if (!cf_lexer_lex(&parser->lex, str, file))
|
||||
return false;
|
||||
|
|
@ -76,26 +76,23 @@ static inline bool cf_parser_parse(struct cf_parser *parser,
|
|||
return true;
|
||||
}
|
||||
|
||||
EXPORT void cf_adderror(struct cf_parser *parser, const char *error,
|
||||
int level, const char *val1, const char *val2,
|
||||
const char *val3);
|
||||
EXPORT void cf_adderror(struct cf_parser *parser, const char *error, int level,
|
||||
const char *val1, const char *val2, const char *val3);
|
||||
|
||||
static inline void cf_adderror_expecting(struct cf_parser *p,
|
||||
const char *expected)
|
||||
const char *expected)
|
||||
{
|
||||
cf_adderror(p, "Expected '$1'", LEX_ERROR, expected, NULL, NULL);
|
||||
}
|
||||
|
||||
static inline void cf_adderror_unexpected_eof(struct cf_parser *p)
|
||||
{
|
||||
cf_adderror(p, "Unexpected EOF", LEX_ERROR,
|
||||
NULL, NULL, NULL);
|
||||
cf_adderror(p, "Unexpected EOF", LEX_ERROR, NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
static inline void cf_adderror_syntax_error(struct cf_parser *p)
|
||||
{
|
||||
cf_adderror(p, "Syntax error", LEX_ERROR,
|
||||
NULL, NULL, NULL);
|
||||
cf_adderror(p, "Syntax error", LEX_ERROR, NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
static inline bool cf_next_token(struct cf_parser *p)
|
||||
|
|
@ -124,8 +121,8 @@ static inline bool cf_next_valid_token(struct cf_parser *p)
|
|||
|
||||
EXPORT bool cf_pass_pair(struct cf_parser *p, char in, char out);
|
||||
|
||||
static inline bool cf_go_to_token(struct cf_parser *p,
|
||||
const char *str1, const char *str2)
|
||||
static inline bool cf_go_to_token(struct cf_parser *p, const char *str1,
|
||||
const char *str2)
|
||||
{
|
||||
while (cf_next_token(p)) {
|
||||
if (strref_cmp(&p->cur_token->str, str1) == 0) {
|
||||
|
|
@ -141,8 +138,8 @@ static inline bool cf_go_to_token(struct cf_parser *p,
|
|||
return false;
|
||||
}
|
||||
|
||||
static inline bool cf_go_to_valid_token(struct cf_parser *p,
|
||||
const char *str1, const char *str2)
|
||||
static inline bool cf_go_to_valid_token(struct cf_parser *p, const char *str1,
|
||||
const char *str2)
|
||||
{
|
||||
if (!cf_go_to_token(p, str1, str2)) {
|
||||
cf_adderror_unexpected_eof(p);
|
||||
|
|
@ -153,17 +150,16 @@ static inline bool cf_go_to_valid_token(struct cf_parser *p,
|
|||
}
|
||||
|
||||
static inline bool cf_go_to_token_type(struct cf_parser *p,
|
||||
enum cf_token_type type)
|
||||
enum cf_token_type type)
|
||||
{
|
||||
while (p->cur_token->type != CFTOKEN_NONE &&
|
||||
p->cur_token->type != type)
|
||||
while (p->cur_token->type != CFTOKEN_NONE && p->cur_token->type != type)
|
||||
p->cur_token++;
|
||||
|
||||
return p->cur_token->type != CFTOKEN_NONE;
|
||||
}
|
||||
|
||||
static inline int cf_token_should_be(struct cf_parser *p,
|
||||
const char *str, const char *goto1, const char *goto2)
|
||||
static inline int cf_token_should_be(struct cf_parser *p, const char *str,
|
||||
const char *goto1, const char *goto2)
|
||||
{
|
||||
if (strref_cmp(&p->cur_token->str, str) == 0)
|
||||
return PARSE_SUCCESS;
|
||||
|
|
@ -177,8 +173,8 @@ static inline int cf_token_should_be(struct cf_parser *p,
|
|||
return PARSE_CONTINUE;
|
||||
}
|
||||
|
||||
static inline int cf_next_token_should_be(struct cf_parser *p,
|
||||
const char *str, const char *goto1, const char *goto2)
|
||||
static inline int cf_next_token_should_be(struct cf_parser *p, const char *str,
|
||||
const char *goto1, const char *goto2)
|
||||
{
|
||||
if (!cf_next_token(p)) {
|
||||
cf_adderror_unexpected_eof(p);
|
||||
|
|
@ -208,7 +204,7 @@ static inline bool cf_peek_token(struct cf_parser *p, struct cf_token *peek)
|
|||
}
|
||||
|
||||
static inline bool cf_peek_valid_token(struct cf_parser *p,
|
||||
struct cf_token *peek)
|
||||
struct cf_token *peek)
|
||||
{
|
||||
bool success = cf_peek_token(p, peek);
|
||||
if (!success)
|
||||
|
|
@ -221,9 +217,9 @@ static inline bool cf_token_is(struct cf_parser *p, const char *val)
|
|||
return strref_cmp(&p->cur_token->str, val) == 0;
|
||||
}
|
||||
|
||||
static inline int cf_token_is_type(struct cf_parser *p,
|
||||
enum cf_token_type type, const char *type_expected,
|
||||
const char *goto_token)
|
||||
static inline int cf_token_is_type(struct cf_parser *p, enum cf_token_type type,
|
||||
const char *type_expected,
|
||||
const char *goto_token)
|
||||
{
|
||||
if (p->cur_token->type != type) {
|
||||
cf_adderror_expecting(p, type_expected);
|
||||
|
|
@ -242,8 +238,8 @@ static inline void cf_copy_token(struct cf_parser *p, char **dst)
|
|||
*dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
|
||||
}
|
||||
|
||||
static inline int cf_get_name(struct cf_parser *p, char **dst,
|
||||
const char *name, const char *goto_token)
|
||||
static inline int cf_get_name(struct cf_parser *p, char **dst, const char *name,
|
||||
const char *goto_token)
|
||||
{
|
||||
int errcode;
|
||||
|
||||
|
|
@ -256,7 +252,7 @@ static inline int cf_get_name(struct cf_parser *p, char **dst,
|
|||
}
|
||||
|
||||
static inline int cf_next_name(struct cf_parser *p, char **dst,
|
||||
const char *name, const char *goto_token)
|
||||
const char *name, const char *goto_token)
|
||||
{
|
||||
if (!cf_next_valid_token(p))
|
||||
return PARSE_EOF;
|
||||
|
|
@ -274,7 +270,7 @@ static inline int cf_next_token_copy(struct cf_parser *p, char **dst)
|
|||
}
|
||||
|
||||
static inline int cf_get_name_ref(struct cf_parser *p, struct strref *dst,
|
||||
const char *name, const char *goto_token)
|
||||
const char *name, const char *goto_token)
|
||||
{
|
||||
int errcode;
|
||||
|
||||
|
|
@ -287,7 +283,7 @@ static inline int cf_get_name_ref(struct cf_parser *p, struct strref *dst,
|
|||
}
|
||||
|
||||
static inline int cf_next_name_ref(struct cf_parser *p, struct strref *dst,
|
||||
const char *name, const char *goto_token)
|
||||
const char *name, const char *goto_token)
|
||||
{
|
||||
if (!cf_next_valid_token(p))
|
||||
return PARSE_EOF;
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ extern "C" {
|
|||
/* Dynamic circular buffer */
|
||||
|
||||
struct circlebuf {
|
||||
void *data;
|
||||
void *data;
|
||||
size_t size;
|
||||
|
||||
size_t start_pos;
|
||||
|
|
@ -50,7 +50,7 @@ static inline void circlebuf_free(struct circlebuf *cb)
|
|||
}
|
||||
|
||||
static inline void circlebuf_reorder_data(struct circlebuf *cb,
|
||||
size_t new_capacity)
|
||||
size_t new_capacity)
|
||||
{
|
||||
size_t difference;
|
||||
uint8_t *data;
|
||||
|
|
@ -59,8 +59,8 @@ static inline void circlebuf_reorder_data(struct circlebuf *cb,
|
|||
return;
|
||||
|
||||
difference = new_capacity - cb->capacity;
|
||||
data = (uint8_t*)cb->data + cb->start_pos;
|
||||
memmove(data+difference, data, cb->capacity - cb->start_pos);
|
||||
data = (uint8_t *)cb->data + cb->start_pos;
|
||||
memmove(data + difference, data, cb->capacity - cb->start_pos);
|
||||
cb->start_pos += difference;
|
||||
}
|
||||
|
||||
|
|
@ -70,7 +70,7 @@ static inline void circlebuf_ensure_capacity(struct circlebuf *cb)
|
|||
if (cb->size <= cb->capacity)
|
||||
return;
|
||||
|
||||
new_capacity = cb->capacity*2;
|
||||
new_capacity = cb->capacity * 2;
|
||||
if (cb->size > new_capacity)
|
||||
new_capacity = cb->size;
|
||||
|
||||
|
|
@ -105,12 +105,12 @@ static inline void circlebuf_upsize(struct circlebuf *cb, size_t size)
|
|||
size_t loop_size = add_size - back_size;
|
||||
|
||||
if (back_size)
|
||||
memset((uint8_t*)cb->data + cb->end_pos, 0, back_size);
|
||||
memset((uint8_t *)cb->data + cb->end_pos, 0, back_size);
|
||||
|
||||
memset(cb->data, 0, loop_size);
|
||||
new_end_pos -= cb->capacity;
|
||||
} else {
|
||||
memset((uint8_t*)cb->data + cb->end_pos, 0, add_size);
|
||||
memset((uint8_t *)cb->data + cb->end_pos, 0, add_size);
|
||||
}
|
||||
|
||||
cb->end_pos = new_end_pos;
|
||||
|
|
@ -118,7 +118,7 @@ static inline void circlebuf_upsize(struct circlebuf *cb, size_t size)
|
|||
|
||||
/** Overwrites data at a specific point in the buffer (relative). */
|
||||
static inline void circlebuf_place(struct circlebuf *cb, size_t position,
|
||||
const void *data, size_t size)
|
||||
const void *data, size_t size)
|
||||
{
|
||||
size_t end_point = position + size;
|
||||
size_t data_end_pos;
|
||||
|
|
@ -136,15 +136,15 @@ static inline void circlebuf_place(struct circlebuf *cb, size_t position,
|
|||
size_t loop_size = size - back_size;
|
||||
|
||||
if (back_size)
|
||||
memcpy((uint8_t*)cb->data + position, data, loop_size);
|
||||
memcpy(cb->data, (uint8_t*)data + loop_size, back_size);
|
||||
memcpy((uint8_t *)cb->data + position, data, loop_size);
|
||||
memcpy(cb->data, (uint8_t *)data + loop_size, back_size);
|
||||
} else {
|
||||
memcpy((uint8_t*)cb->data + position, data, size);
|
||||
memcpy((uint8_t *)cb->data + position, data, size);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void circlebuf_push_back(struct circlebuf *cb, const void *data,
|
||||
size_t size)
|
||||
size_t size)
|
||||
{
|
||||
size_t new_end_pos = cb->end_pos + size;
|
||||
|
||||
|
|
@ -156,20 +156,20 @@ static inline void circlebuf_push_back(struct circlebuf *cb, const void *data,
|
|||
size_t loop_size = size - back_size;
|
||||
|
||||
if (back_size)
|
||||
memcpy((uint8_t*)cb->data + cb->end_pos, data,
|
||||
back_size);
|
||||
memcpy(cb->data, (uint8_t*)data + back_size, loop_size);
|
||||
memcpy((uint8_t *)cb->data + cb->end_pos, data,
|
||||
back_size);
|
||||
memcpy(cb->data, (uint8_t *)data + back_size, loop_size);
|
||||
|
||||
new_end_pos -= cb->capacity;
|
||||
} else {
|
||||
memcpy((uint8_t*)cb->data + cb->end_pos, data, size);
|
||||
memcpy((uint8_t *)cb->data + cb->end_pos, data, size);
|
||||
}
|
||||
|
||||
cb->end_pos = new_end_pos;
|
||||
}
|
||||
|
||||
static inline void circlebuf_push_front(struct circlebuf *cb, const void *data,
|
||||
size_t size)
|
||||
size_t size)
|
||||
{
|
||||
cb->size += size;
|
||||
circlebuf_ensure_capacity(cb);
|
||||
|
|
@ -178,14 +178,14 @@ static inline void circlebuf_push_front(struct circlebuf *cb, const void *data,
|
|||
size_t back_size = size - cb->start_pos;
|
||||
|
||||
if (cb->start_pos)
|
||||
memcpy(cb->data, (uint8_t*)data + back_size,
|
||||
cb->start_pos);
|
||||
memcpy(cb->data, (uint8_t *)data + back_size,
|
||||
cb->start_pos);
|
||||
|
||||
cb->start_pos = cb->capacity - back_size;
|
||||
memcpy((uint8_t*)cb->data + cb->start_pos, data, back_size);
|
||||
memcpy((uint8_t *)cb->data + cb->start_pos, data, back_size);
|
||||
} else {
|
||||
cb->start_pos -= size;
|
||||
memcpy((uint8_t*)cb->data + cb->start_pos, data, size);
|
||||
memcpy((uint8_t *)cb->data + cb->start_pos, data, size);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -201,12 +201,12 @@ static inline void circlebuf_push_back_zero(struct circlebuf *cb, size_t size)
|
|||
size_t loop_size = size - back_size;
|
||||
|
||||
if (back_size)
|
||||
memset((uint8_t*)cb->data + cb->end_pos, 0, back_size);
|
||||
memset((uint8_t *)cb->data + cb->end_pos, 0, back_size);
|
||||
memset(cb->data, 0, loop_size);
|
||||
|
||||
new_end_pos -= cb->capacity;
|
||||
} else {
|
||||
memset((uint8_t*)cb->data + cb->end_pos, 0, size);
|
||||
memset((uint8_t *)cb->data + cb->end_pos, 0, size);
|
||||
}
|
||||
|
||||
cb->end_pos = new_end_pos;
|
||||
|
|
@ -224,15 +224,15 @@ static inline void circlebuf_push_front_zero(struct circlebuf *cb, size_t size)
|
|||
memset(cb->data, 0, cb->start_pos);
|
||||
|
||||
cb->start_pos = cb->capacity - back_size;
|
||||
memset((uint8_t*)cb->data + cb->start_pos, 0, back_size);
|
||||
memset((uint8_t *)cb->data + cb->start_pos, 0, back_size);
|
||||
} else {
|
||||
cb->start_pos -= size;
|
||||
memset((uint8_t*)cb->data + cb->start_pos, 0, size);
|
||||
memset((uint8_t *)cb->data + cb->start_pos, 0, size);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void circlebuf_peek_front(struct circlebuf *cb, void *data,
|
||||
size_t size)
|
||||
size_t size)
|
||||
{
|
||||
assert(size <= cb->size);
|
||||
|
||||
|
|
@ -240,18 +240,18 @@ static inline void circlebuf_peek_front(struct circlebuf *cb, void *data,
|
|||
size_t start_size = cb->capacity - cb->start_pos;
|
||||
|
||||
if (start_size < size) {
|
||||
memcpy(data, (uint8_t*)cb->data + cb->start_pos,
|
||||
start_size);
|
||||
memcpy((uint8_t*)data + start_size, cb->data,
|
||||
size - start_size);
|
||||
memcpy(data, (uint8_t *)cb->data + cb->start_pos,
|
||||
start_size);
|
||||
memcpy((uint8_t *)data + start_size, cb->data,
|
||||
size - start_size);
|
||||
} else {
|
||||
memcpy(data, (uint8_t*)cb->data + cb->start_pos, size);
|
||||
memcpy(data, (uint8_t *)cb->data + cb->start_pos, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void circlebuf_peek_back(struct circlebuf *cb, void *data,
|
||||
size_t size)
|
||||
size_t size)
|
||||
{
|
||||
assert(size <= cb->size);
|
||||
|
||||
|
|
@ -262,19 +262,19 @@ static inline void circlebuf_peek_back(struct circlebuf *cb, void *data,
|
|||
size_t front_size = size - back_size;
|
||||
size_t new_end_pos = cb->capacity - front_size;
|
||||
|
||||
memcpy((uint8_t*)data + (size - back_size), cb->data,
|
||||
back_size);
|
||||
memcpy(data, (uint8_t*)cb->data + new_end_pos,
|
||||
front_size);
|
||||
memcpy((uint8_t *)data + (size - back_size), cb->data,
|
||||
back_size);
|
||||
memcpy(data, (uint8_t *)cb->data + new_end_pos,
|
||||
front_size);
|
||||
} else {
|
||||
memcpy(data, (uint8_t*)cb->data + cb->end_pos - size,
|
||||
size);
|
||||
memcpy(data, (uint8_t *)cb->data + cb->end_pos - size,
|
||||
size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void circlebuf_pop_front(struct circlebuf *cb, void *data,
|
||||
size_t size)
|
||||
size_t size)
|
||||
{
|
||||
circlebuf_peek_front(cb, data, size);
|
||||
|
||||
|
|
@ -290,7 +290,7 @@ static inline void circlebuf_pop_front(struct circlebuf *cb, void *data,
|
|||
}
|
||||
|
||||
static inline void circlebuf_pop_back(struct circlebuf *cb, void *data,
|
||||
size_t size)
|
||||
size_t size)
|
||||
{
|
||||
circlebuf_peek_back(cb, data, size);
|
||||
|
||||
|
|
@ -308,7 +308,7 @@ static inline void circlebuf_pop_back(struct circlebuf *cb, void *data,
|
|||
|
||||
static inline void *circlebuf_data(struct circlebuf *cb, size_t idx)
|
||||
{
|
||||
uint8_t *ptr = (uint8_t*)cb->data;
|
||||
uint8_t *ptr = (uint8_t *)cb->data;
|
||||
size_t offset = cb->start_pos + idx;
|
||||
|
||||
if (idx >= cb->size)
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ static inline void config_section_free(struct config_section *section)
|
|||
size_t i;
|
||||
|
||||
for (i = 0; i < section->items.num; i++)
|
||||
config_item_free(items+i);
|
||||
config_item_free(items + i);
|
||||
|
||||
darray_free(§ion->items);
|
||||
bfree(section->name);
|
||||
|
|
@ -100,13 +100,12 @@ static inline void remove_ref_whitespace(struct strref *ref)
|
|||
ref->len--;
|
||||
}
|
||||
|
||||
while (ref->len && is_whitespace(ref->array[ref->len-1]))
|
||||
while (ref->len && is_whitespace(ref->array[ref->len - 1]))
|
||||
ref->len--;
|
||||
}
|
||||
}
|
||||
|
||||
static bool config_parse_string(struct lexer *lex, struct strref *ref,
|
||||
char end)
|
||||
static bool config_parse_string(struct lexer *lex, struct strref *ref, char end)
|
||||
{
|
||||
bool success = end != 0;
|
||||
struct base_token token;
|
||||
|
|
@ -149,7 +148,7 @@ static void unescape(struct dstr *str)
|
|||
} else if (next == 'r') {
|
||||
cur = '\r';
|
||||
read++;
|
||||
} else if (next =='n') {
|
||||
} else if (next == 'n') {
|
||||
cur = '\n';
|
||||
read++;
|
||||
}
|
||||
|
|
@ -164,7 +163,7 @@ static void unescape(struct dstr *str)
|
|||
}
|
||||
|
||||
static void config_add_item(struct darray *items, struct strref *name,
|
||||
struct strref *value)
|
||||
struct strref *value)
|
||||
{
|
||||
struct config_item item;
|
||||
struct dstr item_value;
|
||||
|
|
@ -172,13 +171,13 @@ static void config_add_item(struct darray *items, struct strref *name,
|
|||
|
||||
unescape(&item_value);
|
||||
|
||||
item.name = bstrdup_n(name->array, name->len);
|
||||
item.name = bstrdup_n(name->array, name->len);
|
||||
item.value = item_value.array;
|
||||
darray_push_back(sizeof(struct config_item), items, &item);
|
||||
}
|
||||
|
||||
static void config_parse_section(struct config_section *section,
|
||||
struct lexer *lex)
|
||||
struct lexer *lex)
|
||||
{
|
||||
struct base_token token;
|
||||
|
||||
|
|
@ -193,8 +192,9 @@ static void config_parse_section(struct config_section *section,
|
|||
if (token.type == BASETOKEN_OTHER) {
|
||||
if (*token.text.array == '#') {
|
||||
do {
|
||||
if (!lexer_getbasetoken(lex, &token,
|
||||
PARSE_WHITESPACE))
|
||||
if (!lexer_getbasetoken(
|
||||
lex, &token,
|
||||
PARSE_WHITESPACE))
|
||||
return;
|
||||
} while (!is_newline(*token.text.array));
|
||||
|
||||
|
|
@ -214,10 +214,10 @@ static void config_parse_section(struct config_section *section,
|
|||
|
||||
if (strref_is_empty(&value)) {
|
||||
struct config_item item;
|
||||
item.name = bstrdup_n(name.array, name.len);
|
||||
item.name = bstrdup_n(name.array, name.len);
|
||||
item.value = bzalloc(1);
|
||||
darray_push_back(sizeof(struct config_item),
|
||||
§ion->items, &item);
|
||||
§ion->items, &item);
|
||||
} else {
|
||||
config_add_item(§ion->items, &name, &value);
|
||||
}
|
||||
|
|
@ -255,15 +255,14 @@ static void parse_config_data(struct darray *sections, struct lexer *lex)
|
|||
return;
|
||||
|
||||
section = darray_push_back_new(sizeof(struct config_section),
|
||||
sections);
|
||||
section->name = bstrdup_n(section_name.array,
|
||||
section_name.len);
|
||||
sections);
|
||||
section->name = bstrdup_n(section_name.array, section_name.len);
|
||||
config_parse_section(section, lex);
|
||||
}
|
||||
}
|
||||
|
||||
static int config_parse_file(struct darray *sections, const char *file,
|
||||
bool always_open)
|
||||
bool always_open)
|
||||
{
|
||||
char *file_data;
|
||||
struct lexer lex;
|
||||
|
|
@ -379,10 +378,10 @@ int config_save(config_t *config)
|
|||
|
||||
for (i = 0; i < config->sections.num; i++) {
|
||||
struct config_section *section = darray_item(
|
||||
sizeof(struct config_section),
|
||||
&config->sections, i);
|
||||
sizeof(struct config_section), &config->sections, i);
|
||||
|
||||
if (i) dstr_cat(&str, "\n");
|
||||
if (i)
|
||||
dstr_cat(&str, "\n");
|
||||
|
||||
dstr_cat(&str, "[");
|
||||
dstr_cat(&str, section->name);
|
||||
|
|
@ -390,8 +389,7 @@ int config_save(config_t *config)
|
|||
|
||||
for (j = 0; j < section->items.num; j++) {
|
||||
struct config_item *item = darray_item(
|
||||
sizeof(struct config_item),
|
||||
§ion->items, j);
|
||||
sizeof(struct config_item), §ion->items, j);
|
||||
|
||||
dstr_copy(&tmp, item->value ? item->value : "");
|
||||
dstr_replace(&tmp, "\\", "\\\\");
|
||||
|
|
@ -426,7 +424,7 @@ cleanup:
|
|||
}
|
||||
|
||||
int config_save_safe(config_t *config, const char *temp_ext,
|
||||
const char *backup_ext)
|
||||
const char *backup_ext)
|
||||
{
|
||||
struct dstr temp_file = {0};
|
||||
struct dstr backup_file = {0};
|
||||
|
|
@ -435,7 +433,7 @@ int config_save_safe(config_t *config, const char *temp_ext,
|
|||
|
||||
if (!temp_ext || !*temp_ext) {
|
||||
blog(LOG_ERROR, "config_save_safe: invalid "
|
||||
"temporary extension specified");
|
||||
"temporary extension specified");
|
||||
return CONFIG_ERROR;
|
||||
}
|
||||
|
||||
|
|
@ -451,8 +449,10 @@ int config_save_safe(config_t *config, const char *temp_ext,
|
|||
config->file = file;
|
||||
|
||||
if (ret != CONFIG_SUCCESS) {
|
||||
blog(LOG_ERROR, "config_save_safe: failed to "
|
||||
"write to %s", temp_file.array);
|
||||
blog(LOG_ERROR,
|
||||
"config_save_safe: failed to "
|
||||
"write to %s",
|
||||
temp_file.array);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
|
|
@ -478,15 +478,16 @@ void config_close(config_t *config)
|
|||
struct config_section *defaults, *sections;
|
||||
size_t i;
|
||||
|
||||
if (!config) return;
|
||||
if (!config)
|
||||
return;
|
||||
|
||||
defaults = config->defaults.array;
|
||||
sections = config->sections.array;
|
||||
|
||||
for (i = 0; i < config->defaults.num; i++)
|
||||
config_section_free(defaults+i);
|
||||
config_section_free(defaults + i);
|
||||
for (i = 0; i < config->sections.num; i++)
|
||||
config_section_free(sections+i);
|
||||
config_section_free(sections + i);
|
||||
|
||||
darray_free(&config->defaults);
|
||||
darray_free(&config->sections);
|
||||
|
|
@ -511,7 +512,7 @@ const char *config_get_section(config_t *config, size_t idx)
|
|||
goto unlock;
|
||||
|
||||
section = darray_item(sizeof(struct config_section), &config->sections,
|
||||
idx);
|
||||
idx);
|
||||
name = section->name;
|
||||
|
||||
unlock:
|
||||
|
|
@ -520,19 +521,20 @@ unlock:
|
|||
}
|
||||
|
||||
static const struct config_item *config_find_item(const struct darray *sections,
|
||||
const char *section, const char *name)
|
||||
const char *section,
|
||||
const char *name)
|
||||
{
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < sections->num; i++) {
|
||||
const struct config_section *sec = darray_item(
|
||||
sizeof(struct config_section), sections, i);
|
||||
const struct config_section *sec =
|
||||
darray_item(sizeof(struct config_section), sections, i);
|
||||
|
||||
if (astrcmpi(sec->name, section) == 0) {
|
||||
for (j = 0; j < sec->items.num; j++) {
|
||||
struct config_item *item = darray_item(
|
||||
sizeof(struct config_item),
|
||||
&sec->items, j);
|
||||
struct config_item *item =
|
||||
darray_item(sizeof(struct config_item),
|
||||
&sec->items, j);
|
||||
|
||||
if (astrcmpi(item->name, name) == 0)
|
||||
return item;
|
||||
|
|
@ -544,7 +546,7 @@ static const struct config_item *config_find_item(const struct darray *sections,
|
|||
}
|
||||
|
||||
static void config_set_item(config_t *config, struct darray *sections,
|
||||
const char *section, const char *name, char *value)
|
||||
const char *section, const char *name, char *value)
|
||||
{
|
||||
struct config_section *sec = NULL;
|
||||
struct config_section *array = sections->array;
|
||||
|
|
@ -554,12 +556,12 @@ static void config_set_item(config_t *config, struct darray *sections,
|
|||
pthread_mutex_lock(&config->mutex);
|
||||
|
||||
for (i = 0; i < sections->num; i++) {
|
||||
struct config_section *cur_sec = array+i;
|
||||
struct config_section *cur_sec = array + i;
|
||||
struct config_item *items = cur_sec->items.array;
|
||||
|
||||
if (astrcmpi(cur_sec->name, section) == 0) {
|
||||
for (j = 0; j < cur_sec->items.num; j++) {
|
||||
item = items+j;
|
||||
item = items + j;
|
||||
|
||||
if (astrcmpi(item->name, name) == 0) {
|
||||
bfree(item->value);
|
||||
|
|
@ -575,20 +577,20 @@ static void config_set_item(config_t *config, struct darray *sections,
|
|||
|
||||
if (!sec) {
|
||||
sec = darray_push_back_new(sizeof(struct config_section),
|
||||
sections);
|
||||
sections);
|
||||
sec->name = bstrdup(section);
|
||||
}
|
||||
|
||||
item = darray_push_back_new(sizeof(struct config_item), &sec->items);
|
||||
item->name = bstrdup(name);
|
||||
item->name = bstrdup(name);
|
||||
item->value = value;
|
||||
|
||||
unlock:
|
||||
pthread_mutex_unlock(&config->mutex);
|
||||
}
|
||||
|
||||
void config_set_string(config_t *config, const char *section,
|
||||
const char *name, const char *value)
|
||||
void config_set_string(config_t *config, const char *section, const char *name,
|
||||
const char *value)
|
||||
{
|
||||
if (!value)
|
||||
value = "";
|
||||
|
|
@ -596,33 +598,33 @@ void config_set_string(config_t *config, const char *section,
|
|||
bstrdup(value));
|
||||
}
|
||||
|
||||
void config_set_int(config_t *config, const char *section,
|
||||
const char *name, int64_t value)
|
||||
void config_set_int(config_t *config, const char *section, const char *name,
|
||||
int64_t value)
|
||||
{
|
||||
struct dstr str;
|
||||
dstr_init(&str);
|
||||
dstr_printf(&str, "%"PRId64, value);
|
||||
dstr_printf(&str, "%" PRId64, value);
|
||||
config_set_item(config, &config->sections, section, name, str.array);
|
||||
}
|
||||
|
||||
void config_set_uint(config_t *config, const char *section,
|
||||
const char *name, uint64_t value)
|
||||
void config_set_uint(config_t *config, const char *section, const char *name,
|
||||
uint64_t value)
|
||||
{
|
||||
struct dstr str;
|
||||
dstr_init(&str);
|
||||
dstr_printf(&str, "%"PRIu64, value);
|
||||
dstr_printf(&str, "%" PRIu64, value);
|
||||
config_set_item(config, &config->sections, section, name, str.array);
|
||||
}
|
||||
|
||||
void config_set_bool(config_t *config, const char *section,
|
||||
const char *name, bool value)
|
||||
void config_set_bool(config_t *config, const char *section, const char *name,
|
||||
bool value)
|
||||
{
|
||||
char *str = bstrdup(value ? "true" : "false");
|
||||
config_set_item(config, &config->sections, section, name, str);
|
||||
}
|
||||
|
||||
void config_set_double(config_t *config, const char *section,
|
||||
const char *name, double value)
|
||||
void config_set_double(config_t *config, const char *section, const char *name,
|
||||
double value)
|
||||
{
|
||||
char *str = bzalloc(64);
|
||||
os_dtostr(value, str, 64);
|
||||
|
|
@ -630,7 +632,7 @@ void config_set_double(config_t *config, const char *section,
|
|||
}
|
||||
|
||||
void config_set_default_string(config_t *config, const char *section,
|
||||
const char *name, const char *value)
|
||||
const char *name, const char *value)
|
||||
{
|
||||
if (!value)
|
||||
value = "";
|
||||
|
|
@ -639,32 +641,32 @@ void config_set_default_string(config_t *config, const char *section,
|
|||
}
|
||||
|
||||
void config_set_default_int(config_t *config, const char *section,
|
||||
const char *name, int64_t value)
|
||||
const char *name, int64_t value)
|
||||
{
|
||||
struct dstr str;
|
||||
dstr_init(&str);
|
||||
dstr_printf(&str, "%"PRId64, value);
|
||||
dstr_printf(&str, "%" PRId64, value);
|
||||
config_set_item(config, &config->defaults, section, name, str.array);
|
||||
}
|
||||
|
||||
void config_set_default_uint(config_t *config, const char *section,
|
||||
const char *name, uint64_t value)
|
||||
const char *name, uint64_t value)
|
||||
{
|
||||
struct dstr str;
|
||||
dstr_init(&str);
|
||||
dstr_printf(&str, "%"PRIu64, value);
|
||||
dstr_printf(&str, "%" PRIu64, value);
|
||||
config_set_item(config, &config->defaults, section, name, str.array);
|
||||
}
|
||||
|
||||
void config_set_default_bool(config_t *config, const char *section,
|
||||
const char *name, bool value)
|
||||
const char *name, bool value)
|
||||
{
|
||||
char *str = bstrdup(value ? "true" : "false");
|
||||
config_set_item(config, &config->defaults, section, name, str);
|
||||
}
|
||||
|
||||
void config_set_default_double(config_t *config, const char *section,
|
||||
const char *name, double value)
|
||||
const char *name, double value)
|
||||
{
|
||||
struct dstr str;
|
||||
dstr_init(&str);
|
||||
|
|
@ -673,7 +675,7 @@ void config_set_default_double(config_t *config, const char *section,
|
|||
}
|
||||
|
||||
const char *config_get_string(config_t *config, const char *section,
|
||||
const char *name)
|
||||
const char *name)
|
||||
{
|
||||
const struct config_item *item;
|
||||
const char *value = NULL;
|
||||
|
|
@ -712,8 +714,7 @@ static inline uint64_t str_to_uint64(const char *str)
|
|||
return strtoull(str, NULL, 10);
|
||||
}
|
||||
|
||||
int64_t config_get_int(config_t *config, const char *section,
|
||||
const char *name)
|
||||
int64_t config_get_int(config_t *config, const char *section, const char *name)
|
||||
{
|
||||
const char *value = config_get_string(config, section, name);
|
||||
if (value)
|
||||
|
|
@ -723,7 +724,7 @@ int64_t config_get_int(config_t *config, const char *section,
|
|||
}
|
||||
|
||||
uint64_t config_get_uint(config_t *config, const char *section,
|
||||
const char *name)
|
||||
const char *name)
|
||||
{
|
||||
const char *value = config_get_string(config, section, name);
|
||||
if (value)
|
||||
|
|
@ -732,19 +733,17 @@ uint64_t config_get_uint(config_t *config, const char *section,
|
|||
return 0;
|
||||
}
|
||||
|
||||
bool config_get_bool(config_t *config, const char *section,
|
||||
const char *name)
|
||||
bool config_get_bool(config_t *config, const char *section, const char *name)
|
||||
{
|
||||
const char *value = config_get_string(config, section, name);
|
||||
if (value)
|
||||
return astrcmpi(value, "true") == 0 ||
|
||||
!!str_to_uint64(value);
|
||||
return astrcmpi(value, "true") == 0 || !!str_to_uint64(value);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
double config_get_double(config_t *config, const char *section,
|
||||
const char *name)
|
||||
const char *name)
|
||||
{
|
||||
const char *value = config_get_string(config, section, name);
|
||||
if (value)
|
||||
|
|
@ -754,7 +753,7 @@ double config_get_double(config_t *config, const char *section,
|
|||
}
|
||||
|
||||
bool config_remove_value(config_t *config, const char *section,
|
||||
const char *name)
|
||||
const char *name)
|
||||
{
|
||||
struct darray *sections = &config->sections;
|
||||
bool success = false;
|
||||
|
|
@ -762,21 +761,20 @@ bool config_remove_value(config_t *config, const char *section,
|
|||
pthread_mutex_lock(&config->mutex);
|
||||
|
||||
for (size_t i = 0; i < sections->num; i++) {
|
||||
struct config_section *sec = darray_item(
|
||||
sizeof(struct config_section), sections, i);
|
||||
struct config_section *sec =
|
||||
darray_item(sizeof(struct config_section), sections, i);
|
||||
|
||||
if (astrcmpi(sec->name, section) != 0)
|
||||
continue;
|
||||
|
||||
for (size_t j = 0; j < sec->items.num; j++) {
|
||||
struct config_item *item = darray_item(
|
||||
sizeof(struct config_item),
|
||||
&sec->items, j);
|
||||
sizeof(struct config_item), &sec->items, j);
|
||||
|
||||
if (astrcmpi(item->name, name) == 0) {
|
||||
config_item_free(item);
|
||||
darray_erase(sizeof(struct config_item),
|
||||
&sec->items, j);
|
||||
&sec->items, j);
|
||||
success = true;
|
||||
goto unlock;
|
||||
}
|
||||
|
|
@ -788,8 +786,8 @@ unlock:
|
|||
return success;
|
||||
}
|
||||
|
||||
const char *config_get_default_string(config_t *config,
|
||||
const char *section, const char *name)
|
||||
const char *config_get_default_string(config_t *config, const char *section,
|
||||
const char *name)
|
||||
{
|
||||
const struct config_item *item;
|
||||
const char *value = NULL;
|
||||
|
|
@ -805,7 +803,7 @@ const char *config_get_default_string(config_t *config,
|
|||
}
|
||||
|
||||
int64_t config_get_default_int(config_t *config, const char *section,
|
||||
const char *name)
|
||||
const char *name)
|
||||
{
|
||||
const char *value = config_get_default_string(config, section, name);
|
||||
if (value)
|
||||
|
|
@ -815,7 +813,7 @@ int64_t config_get_default_int(config_t *config, const char *section,
|
|||
}
|
||||
|
||||
uint64_t config_get_default_uint(config_t *config, const char *section,
|
||||
const char *name)
|
||||
const char *name)
|
||||
{
|
||||
const char *value = config_get_default_string(config, section, name);
|
||||
if (value)
|
||||
|
|
@ -825,18 +823,17 @@ uint64_t config_get_default_uint(config_t *config, const char *section,
|
|||
}
|
||||
|
||||
bool config_get_default_bool(config_t *config, const char *section,
|
||||
const char *name)
|
||||
const char *name)
|
||||
{
|
||||
const char *value = config_get_default_string(config, section, name);
|
||||
if (value)
|
||||
return astrcmpi(value, "true") == 0 ||
|
||||
!!str_to_uint64(value);
|
||||
return astrcmpi(value, "true") == 0 || !!str_to_uint64(value);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
double config_get_default_double(config_t *config, const char *section,
|
||||
const char *name)
|
||||
const char *name)
|
||||
{
|
||||
const char *value = config_get_default_string(config, section, name);
|
||||
if (value)
|
||||
|
|
@ -846,7 +843,7 @@ double config_get_default_double(config_t *config, const char *section,
|
|||
}
|
||||
|
||||
bool config_has_user_value(config_t *config, const char *section,
|
||||
const char *name)
|
||||
const char *name)
|
||||
{
|
||||
bool success;
|
||||
pthread_mutex_lock(&config->mutex);
|
||||
|
|
@ -856,7 +853,7 @@ bool config_has_user_value(config_t *config, const char *section,
|
|||
}
|
||||
|
||||
bool config_has_default_value(config_t *config, const char *section,
|
||||
const char *name)
|
||||
const char *name)
|
||||
{
|
||||
bool success;
|
||||
pthread_mutex_lock(&config->mutex);
|
||||
|
|
@ -864,4 +861,3 @@ bool config_has_default_value(config_t *config, const char *section,
|
|||
pthread_mutex_unlock(&config->mutex);
|
||||
return success;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,9 +32,9 @@ extern "C" {
|
|||
struct config_data;
|
||||
typedef struct config_data config_t;
|
||||
|
||||
#define CONFIG_SUCCESS 0
|
||||
#define CONFIG_SUCCESS 0
|
||||
#define CONFIG_FILENOTFOUND -1
|
||||
#define CONFIG_ERROR -2
|
||||
#define CONFIG_ERROR -2
|
||||
|
||||
enum config_open_type {
|
||||
CONFIG_OPEN_EXISTING,
|
||||
|
|
@ -43,40 +43,40 @@ enum config_open_type {
|
|||
|
||||
EXPORT config_t *config_create(const char *file);
|
||||
EXPORT int config_open(config_t **config, const char *file,
|
||||
enum config_open_type open_type);
|
||||
enum config_open_type open_type);
|
||||
EXPORT int config_open_string(config_t **config, const char *str);
|
||||
EXPORT int config_save(config_t *config);
|
||||
EXPORT int config_save_safe(config_t *config, const char *temp_ext,
|
||||
const char *backup_ext);
|
||||
const char *backup_ext);
|
||||
EXPORT void config_close(config_t *config);
|
||||
|
||||
EXPORT size_t config_num_sections(config_t *config);
|
||||
EXPORT const char *config_get_section(config_t *config, size_t idx);
|
||||
|
||||
EXPORT void config_set_string(config_t *config, const char *section,
|
||||
const char *name, const char *value);
|
||||
const char *name, const char *value);
|
||||
EXPORT void config_set_int(config_t *config, const char *section,
|
||||
const char *name, int64_t value);
|
||||
const char *name, int64_t value);
|
||||
EXPORT void config_set_uint(config_t *config, const char *section,
|
||||
const char *name, uint64_t value);
|
||||
const char *name, uint64_t value);
|
||||
EXPORT void config_set_bool(config_t *config, const char *section,
|
||||
const char *name, bool value);
|
||||
const char *name, bool value);
|
||||
EXPORT void config_set_double(config_t *config, const char *section,
|
||||
const char *name, double value);
|
||||
const char *name, double value);
|
||||
|
||||
EXPORT const char *config_get_string(config_t *config,
|
||||
const char *section, const char *name);
|
||||
EXPORT const char *config_get_string(config_t *config, const char *section,
|
||||
const char *name);
|
||||
EXPORT int64_t config_get_int(config_t *config, const char *section,
|
||||
const char *name);
|
||||
const char *name);
|
||||
EXPORT uint64_t config_get_uint(config_t *config, const char *section,
|
||||
const char *name);
|
||||
const char *name);
|
||||
EXPORT bool config_get_bool(config_t *config, const char *section,
|
||||
const char *name);
|
||||
const char *name);
|
||||
EXPORT double config_get_double(config_t *config, const char *section,
|
||||
const char *name);
|
||||
const char *name);
|
||||
|
||||
EXPORT bool config_remove_value(config_t *config, const char *section,
|
||||
const char *name);
|
||||
const char *name);
|
||||
|
||||
/*
|
||||
* DEFAULT VALUES
|
||||
|
|
@ -95,33 +95,34 @@ EXPORT bool config_remove_value(config_t *config, const char *section,
|
|||
EXPORT int config_open_defaults(config_t *config, const char *file);
|
||||
|
||||
EXPORT void config_set_default_string(config_t *config, const char *section,
|
||||
const char *name, const char *value);
|
||||
const char *name, const char *value);
|
||||
EXPORT void config_set_default_int(config_t *config, const char *section,
|
||||
const char *name, int64_t value);
|
||||
const char *name, int64_t value);
|
||||
EXPORT void config_set_default_uint(config_t *config, const char *section,
|
||||
const char *name, uint64_t value);
|
||||
const char *name, uint64_t value);
|
||||
EXPORT void config_set_default_bool(config_t *config, const char *section,
|
||||
const char *name, bool value);
|
||||
const char *name, bool value);
|
||||
EXPORT void config_set_default_double(config_t *config, const char *section,
|
||||
const char *name, double value);
|
||||
const char *name, double value);
|
||||
|
||||
/* These functions allow you to get the current default values rather than get
|
||||
* the actual values. Probably almost never really needed */
|
||||
EXPORT const char *config_get_default_string(config_t *config,
|
||||
const char *section, const char *name);
|
||||
EXPORT int64_t config_get_default_int(config_t *config,
|
||||
const char *section, const char *name);
|
||||
EXPORT uint64_t config_get_default_uint(config_t *config,
|
||||
const char *section, const char *name);
|
||||
EXPORT bool config_get_default_bool(config_t *config,
|
||||
const char *section, const char *name);
|
||||
EXPORT double config_get_default_double(config_t *config,
|
||||
const char *section, const char *name);
|
||||
const char *section,
|
||||
const char *name);
|
||||
EXPORT int64_t config_get_default_int(config_t *config, const char *section,
|
||||
const char *name);
|
||||
EXPORT uint64_t config_get_default_uint(config_t *config, const char *section,
|
||||
const char *name);
|
||||
EXPORT bool config_get_default_bool(config_t *config, const char *section,
|
||||
const char *name);
|
||||
EXPORT double config_get_default_double(config_t *config, const char *section,
|
||||
const char *name);
|
||||
|
||||
EXPORT bool config_has_user_value(config_t *config,
|
||||
const char *section, const char *name);
|
||||
EXPORT bool config_has_default_value(config_t *config,
|
||||
const char *section, const char *name);
|
||||
EXPORT bool config_has_user_value(config_t *config, const char *section,
|
||||
const char *name);
|
||||
EXPORT bool config_has_default_value(config_t *config, const char *section,
|
||||
const char *name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,16 +21,16 @@
|
|||
|
||||
static uint32_t crc32_tab[] = {
|
||||
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
|
||||
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
|
||||
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
|
||||
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
|
||||
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
|
||||
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
|
||||
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
|
||||
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
|
||||
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
|
||||
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
|
||||
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
|
||||
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
|
||||
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
|
||||
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
|
||||
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
|
||||
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
|
||||
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
|
||||
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
|
||||
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
|
||||
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
|
||||
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
|
||||
|
|
@ -62,8 +62,7 @@ static uint32_t crc32_tab[] = {
|
|||
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
|
||||
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
|
||||
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
|
||||
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
||||
};
|
||||
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
|
||||
|
||||
uint32_t calc_crc32(uint32_t crc, const void *buf, size_t size)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -46,50 +46,50 @@ struct darray {
|
|||
|
||||
static inline void darray_init(struct darray *dst)
|
||||
{
|
||||
dst->array = NULL;
|
||||
dst->num = 0;
|
||||
dst->array = NULL;
|
||||
dst->num = 0;
|
||||
dst->capacity = 0;
|
||||
}
|
||||
|
||||
static inline void darray_free(struct darray *dst)
|
||||
{
|
||||
bfree(dst->array);
|
||||
dst->array = NULL;
|
||||
dst->num = 0;
|
||||
dst->array = NULL;
|
||||
dst->num = 0;
|
||||
dst->capacity = 0;
|
||||
}
|
||||
|
||||
static inline size_t darray_alloc_size(const size_t element_size,
|
||||
const struct darray *da)
|
||||
const struct darray *da)
|
||||
{
|
||||
return element_size*da->num;
|
||||
return element_size * da->num;
|
||||
}
|
||||
|
||||
static inline void *darray_item(const size_t element_size,
|
||||
const struct darray *da, size_t idx)
|
||||
const struct darray *da, size_t idx)
|
||||
{
|
||||
return (void*)(((uint8_t*)da->array) + element_size*idx);
|
||||
return (void *)(((uint8_t *)da->array) + element_size * idx);
|
||||
}
|
||||
|
||||
static inline void *darray_end(const size_t element_size,
|
||||
const struct darray *da)
|
||||
const struct darray *da)
|
||||
{
|
||||
if (!da->num)
|
||||
return NULL;
|
||||
|
||||
return darray_item(element_size, da, da->num-1);
|
||||
return darray_item(element_size, da, da->num - 1);
|
||||
}
|
||||
|
||||
static inline void darray_reserve(const size_t element_size,
|
||||
struct darray *dst, const size_t capacity)
|
||||
static inline void darray_reserve(const size_t element_size, struct darray *dst,
|
||||
const size_t capacity)
|
||||
{
|
||||
void *ptr;
|
||||
if (capacity == 0 || capacity <= dst->num)
|
||||
return;
|
||||
|
||||
ptr = bmalloc(element_size*capacity);
|
||||
ptr = bmalloc(element_size * capacity);
|
||||
if (dst->num)
|
||||
memcpy(ptr, dst->array, element_size*dst->num);
|
||||
memcpy(ptr, dst->array, element_size * dst->num);
|
||||
if (dst->array)
|
||||
bfree(dst->array);
|
||||
dst->array = ptr;
|
||||
|
|
@ -97,27 +97,28 @@ static inline void darray_reserve(const size_t element_size,
|
|||
}
|
||||
|
||||
static inline void darray_ensure_capacity(const size_t element_size,
|
||||
struct darray *dst, const size_t new_size)
|
||||
struct darray *dst,
|
||||
const size_t new_size)
|
||||
{
|
||||
size_t new_cap;
|
||||
void *ptr;
|
||||
if (new_size <= dst->capacity)
|
||||
return;
|
||||
|
||||
new_cap = (!dst->capacity) ? new_size : dst->capacity*2;
|
||||
new_cap = (!dst->capacity) ? new_size : dst->capacity * 2;
|
||||
if (new_size > new_cap)
|
||||
new_cap = new_size;
|
||||
ptr = bmalloc(element_size*new_cap);
|
||||
ptr = bmalloc(element_size * new_cap);
|
||||
if (dst->capacity)
|
||||
memcpy(ptr, dst->array, element_size*dst->capacity);
|
||||
memcpy(ptr, dst->array, element_size * dst->capacity);
|
||||
if (dst->array)
|
||||
bfree(dst->array);
|
||||
dst->array = ptr;
|
||||
dst->capacity = new_cap;
|
||||
}
|
||||
|
||||
static inline void darray_resize(const size_t element_size,
|
||||
struct darray *dst, const size_t size)
|
||||
static inline void darray_resize(const size_t element_size, struct darray *dst,
|
||||
const size_t size)
|
||||
{
|
||||
int b_clear;
|
||||
size_t old_num;
|
||||
|
|
@ -137,38 +138,40 @@ static inline void darray_resize(const size_t element_size,
|
|||
|
||||
if (b_clear)
|
||||
memset(darray_item(element_size, dst, old_num), 0,
|
||||
element_size * (dst->num-old_num));
|
||||
element_size * (dst->num - old_num));
|
||||
}
|
||||
|
||||
static inline void darray_copy(const size_t element_size, struct darray *dst,
|
||||
const struct darray *da)
|
||||
const struct darray *da)
|
||||
{
|
||||
if (da->num == 0) {
|
||||
darray_free(dst);
|
||||
} else {
|
||||
darray_resize(element_size, dst, da->num);
|
||||
memcpy(dst->array, da->array, element_size*da->num);
|
||||
memcpy(dst->array, da->array, element_size * da->num);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void darray_copy_array(const size_t element_size,
|
||||
struct darray *dst, const void *array, const size_t num)
|
||||
struct darray *dst, const void *array,
|
||||
const size_t num)
|
||||
{
|
||||
darray_resize(element_size, dst, num);
|
||||
memcpy(dst->array, array, element_size*dst->num);
|
||||
memcpy(dst->array, array, element_size * dst->num);
|
||||
}
|
||||
|
||||
static inline void darray_move(struct darray *dst, struct darray *src)
|
||||
{
|
||||
darray_free(dst);
|
||||
memcpy(dst, src, sizeof(struct darray));
|
||||
src->array = NULL;
|
||||
src->array = NULL;
|
||||
src->capacity = 0;
|
||||
src->num = 0;
|
||||
src->num = 0;
|
||||
}
|
||||
|
||||
static inline size_t darray_find(const size_t element_size,
|
||||
const struct darray *da, const void *item, const size_t idx)
|
||||
const struct darray *da, const void *item,
|
||||
const size_t idx)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
|
|
@ -184,16 +187,16 @@ static inline size_t darray_find(const size_t element_size,
|
|||
}
|
||||
|
||||
static inline size_t darray_push_back(const size_t element_size,
|
||||
struct darray *dst, const void *item)
|
||||
struct darray *dst, const void *item)
|
||||
{
|
||||
darray_ensure_capacity(element_size, dst, ++dst->num);
|
||||
memcpy(darray_end(element_size, dst), item, element_size);
|
||||
|
||||
return dst->num-1;
|
||||
return dst->num - 1;
|
||||
}
|
||||
|
||||
static inline void *darray_push_back_new(const size_t element_size,
|
||||
struct darray *dst)
|
||||
struct darray *dst)
|
||||
{
|
||||
void *last;
|
||||
|
||||
|
|
@ -205,7 +208,8 @@ static inline void *darray_push_back_new(const size_t element_size,
|
|||
}
|
||||
|
||||
static inline size_t darray_push_back_array(const size_t element_size,
|
||||
struct darray *dst, const void *array, const size_t num)
|
||||
struct darray *dst,
|
||||
const void *array, const size_t num)
|
||||
{
|
||||
size_t old_num;
|
||||
if (!dst)
|
||||
|
|
@ -214,21 +218,22 @@ static inline size_t darray_push_back_array(const size_t element_size,
|
|||
return dst->num;
|
||||
|
||||
old_num = dst->num;
|
||||
darray_resize(element_size, dst, dst->num+num);
|
||||
darray_resize(element_size, dst, dst->num + num);
|
||||
memcpy(darray_item(element_size, dst, old_num), array,
|
||||
element_size*num);
|
||||
element_size * num);
|
||||
|
||||
return old_num;
|
||||
}
|
||||
|
||||
static inline size_t darray_push_back_darray(const size_t element_size,
|
||||
struct darray *dst, const struct darray *da)
|
||||
struct darray *dst,
|
||||
const struct darray *da)
|
||||
{
|
||||
return darray_push_back_array(element_size, dst, da->array, da->num);
|
||||
}
|
||||
|
||||
static inline void darray_insert(const size_t element_size, struct darray *dst,
|
||||
const size_t idx, const void *item)
|
||||
const size_t idx, const void *item)
|
||||
{
|
||||
void *new_item;
|
||||
size_t move_count;
|
||||
|
|
@ -245,13 +250,13 @@ static inline void darray_insert(const size_t element_size, struct darray *dst,
|
|||
|
||||
new_item = darray_item(element_size, dst, idx);
|
||||
|
||||
memmove(darray_item(element_size, dst, idx+1), new_item,
|
||||
move_count*element_size);
|
||||
memmove(darray_item(element_size, dst, idx + 1), new_item,
|
||||
move_count * element_size);
|
||||
memcpy(new_item, item, element_size);
|
||||
}
|
||||
|
||||
static inline void *darray_insert_new(const size_t element_size,
|
||||
struct darray *dst, const size_t idx)
|
||||
struct darray *dst, const size_t idx)
|
||||
{
|
||||
void *item;
|
||||
size_t move_count;
|
||||
|
|
@ -264,16 +269,16 @@ static inline void *darray_insert_new(const size_t element_size,
|
|||
|
||||
move_count = dst->num - idx;
|
||||
darray_ensure_capacity(element_size, dst, ++dst->num);
|
||||
memmove(darray_item(element_size, dst, idx+1), item,
|
||||
move_count*element_size);
|
||||
memmove(darray_item(element_size, dst, idx + 1), item,
|
||||
move_count * element_size);
|
||||
|
||||
memset(item, 0, element_size);
|
||||
return item;
|
||||
}
|
||||
|
||||
static inline void darray_insert_array(const size_t element_size,
|
||||
struct darray *dst, const size_t idx,
|
||||
const void *array, const size_t num)
|
||||
struct darray *dst, const size_t idx,
|
||||
const void *array, const size_t num)
|
||||
{
|
||||
size_t old_num;
|
||||
|
||||
|
|
@ -282,22 +287,23 @@ static inline void darray_insert_array(const size_t element_size,
|
|||
assert(idx < dst->num);
|
||||
|
||||
old_num = dst->num;
|
||||
darray_resize(element_size, dst, dst->num+num);
|
||||
darray_resize(element_size, dst, dst->num + num);
|
||||
|
||||
memmove(darray_item(element_size, dst, idx+num),
|
||||
darray_item(element_size, dst, idx),
|
||||
element_size*(old_num-idx));
|
||||
memcpy(darray_item(element_size, dst, idx), array, element_size*num);
|
||||
memmove(darray_item(element_size, dst, idx + num),
|
||||
darray_item(element_size, dst, idx),
|
||||
element_size * (old_num - idx));
|
||||
memcpy(darray_item(element_size, dst, idx), array, element_size * num);
|
||||
}
|
||||
|
||||
static inline void darray_insert_darray(const size_t element_size,
|
||||
struct darray *dst, const size_t idx, const struct darray *da)
|
||||
struct darray *dst, const size_t idx,
|
||||
const struct darray *da)
|
||||
{
|
||||
darray_insert_array(element_size, dst, idx, da->array, da->num);
|
||||
}
|
||||
|
||||
static inline void darray_erase(const size_t element_size, struct darray *dst,
|
||||
const size_t idx)
|
||||
const size_t idx)
|
||||
{
|
||||
assert(idx < dst->num);
|
||||
|
||||
|
|
@ -305,12 +311,12 @@ static inline void darray_erase(const size_t element_size, struct darray *dst,
|
|||
return;
|
||||
|
||||
memmove(darray_item(element_size, dst, idx),
|
||||
darray_item(element_size, dst, idx+1),
|
||||
element_size*(dst->num-idx));
|
||||
darray_item(element_size, dst, idx + 1),
|
||||
element_size * (dst->num - idx));
|
||||
}
|
||||
|
||||
static inline void darray_erase_item(const size_t element_size,
|
||||
struct darray *dst, const void *item)
|
||||
struct darray *dst, const void *item)
|
||||
{
|
||||
size_t idx = darray_find(element_size, dst, item, 0);
|
||||
if (idx != DARRAY_INVALID)
|
||||
|
|
@ -318,7 +324,8 @@ static inline void darray_erase_item(const size_t element_size,
|
|||
}
|
||||
|
||||
static inline void darray_erase_range(const size_t element_size,
|
||||
struct darray *dst, const size_t start, const size_t end)
|
||||
struct darray *dst, const size_t start,
|
||||
const size_t end)
|
||||
{
|
||||
size_t count, move_count;
|
||||
|
||||
|
|
@ -326,7 +333,7 @@ static inline void darray_erase_range(const size_t element_size,
|
|||
assert(end <= dst->num);
|
||||
assert(end > start);
|
||||
|
||||
count = end-start;
|
||||
count = end - start;
|
||||
if (count == 1) {
|
||||
darray_erase(element_size, dst, start);
|
||||
return;
|
||||
|
|
@ -338,30 +345,31 @@ static inline void darray_erase_range(const size_t element_size,
|
|||
move_count = dst->num - end;
|
||||
if (move_count)
|
||||
memmove(darray_item(element_size, dst, start),
|
||||
darray_item(element_size, dst, end),
|
||||
move_count * element_size);
|
||||
darray_item(element_size, dst, end),
|
||||
move_count * element_size);
|
||||
|
||||
dst->num -= count;
|
||||
}
|
||||
|
||||
static inline void darray_pop_back(const size_t element_size,
|
||||
struct darray *dst)
|
||||
struct darray *dst)
|
||||
{
|
||||
assert(dst->num != 0);
|
||||
|
||||
if (dst->num)
|
||||
darray_erase(element_size, dst, dst->num-1);
|
||||
darray_erase(element_size, dst, dst->num - 1);
|
||||
}
|
||||
|
||||
static inline void darray_join(const size_t element_size, struct darray *dst,
|
||||
struct darray *da)
|
||||
struct darray *da)
|
||||
{
|
||||
darray_push_back_darray(element_size, dst, da);
|
||||
darray_free(da);
|
||||
}
|
||||
|
||||
static inline void darray_split(const size_t element_size, struct darray *dst1,
|
||||
struct darray *dst2, const struct darray *da, const size_t idx)
|
||||
struct darray *dst2, const struct darray *da,
|
||||
const size_t idx)
|
||||
{
|
||||
struct darray temp;
|
||||
|
||||
|
|
@ -377,43 +385,44 @@ static inline void darray_split(const size_t element_size, struct darray *dst1,
|
|||
if (da->num) {
|
||||
if (idx)
|
||||
darray_copy_array(element_size, dst1, temp.array,
|
||||
temp.num);
|
||||
if (idx < temp.num-1)
|
||||
temp.num);
|
||||
if (idx < temp.num - 1)
|
||||
darray_copy_array(element_size, dst2,
|
||||
darray_item(element_size, &temp, idx),
|
||||
temp.num-idx);
|
||||
darray_item(element_size, &temp, idx),
|
||||
temp.num - idx);
|
||||
}
|
||||
|
||||
darray_free(&temp);
|
||||
}
|
||||
|
||||
static inline void darray_move_item(const size_t element_size,
|
||||
struct darray *dst, const size_t from, const size_t to)
|
||||
struct darray *dst, const size_t from,
|
||||
const size_t to)
|
||||
{
|
||||
void *temp, *p_from, *p_to;
|
||||
|
||||
if (from == to)
|
||||
return;
|
||||
|
||||
temp = malloc(element_size);
|
||||
temp = malloc(element_size);
|
||||
p_from = darray_item(element_size, dst, from);
|
||||
p_to = darray_item(element_size, dst, to);
|
||||
p_to = darray_item(element_size, dst, to);
|
||||
|
||||
memcpy(temp, p_from, element_size);
|
||||
|
||||
if (to < from)
|
||||
memmove(darray_item(element_size, dst, to+1), p_to,
|
||||
element_size*(from-to));
|
||||
memmove(darray_item(element_size, dst, to + 1), p_to,
|
||||
element_size * (from - to));
|
||||
else
|
||||
memmove(p_from, darray_item(element_size, dst, from+1),
|
||||
element_size*(to-from));
|
||||
memmove(p_from, darray_item(element_size, dst, from + 1),
|
||||
element_size * (to - from));
|
||||
|
||||
memcpy(p_to, temp, element_size);
|
||||
free(temp);
|
||||
}
|
||||
|
||||
static inline void darray_swap(const size_t element_size,
|
||||
struct darray *dst, const size_t a, const size_t b)
|
||||
static inline void darray_swap(const size_t element_size, struct darray *dst,
|
||||
const size_t a, const size_t b)
|
||||
{
|
||||
void *temp, *a_ptr, *b_ptr;
|
||||
|
||||
|
|
@ -423,13 +432,13 @@ static inline void darray_swap(const size_t element_size,
|
|||
if (a == b)
|
||||
return;
|
||||
|
||||
temp = malloc(element_size);
|
||||
temp = malloc(element_size);
|
||||
a_ptr = darray_item(element_size, dst, a);
|
||||
b_ptr = darray_item(element_size, dst, b);
|
||||
|
||||
memcpy(temp, a_ptr, element_size);
|
||||
memcpy(temp, a_ptr, element_size);
|
||||
memcpy(a_ptr, b_ptr, element_size);
|
||||
memcpy(b_ptr, temp, element_size);
|
||||
memcpy(b_ptr, temp, element_size);
|
||||
|
||||
free(temp);
|
||||
}
|
||||
|
|
@ -457,7 +466,7 @@ static inline void darray_swap(const size_t element_size,
|
|||
|
||||
#define da_free(v) darray_free(&v.da)
|
||||
|
||||
#define da_alloc_size(v) (sizeof(*v.array)*v.num)
|
||||
#define da_alloc_size(v) (sizeof(*v.array) * v.num)
|
||||
|
||||
#define da_end(v) darray_end(sizeof(*v.array), &v.da)
|
||||
|
||||
|
|
@ -466,16 +475,14 @@ static inline void darray_swap(const size_t element_size,
|
|||
|
||||
#define da_resize(v, size) darray_resize(sizeof(*v.array), &v.da, size)
|
||||
|
||||
#define da_copy(dst, src) \
|
||||
darray_copy(sizeof(*dst.array), &dst.da, &src.da)
|
||||
#define da_copy(dst, src) darray_copy(sizeof(*dst.array), &dst.da, &src.da)
|
||||
|
||||
#define da_copy_array(dst, src_array, n) \
|
||||
darray_copy_array(sizeof(*dst.array), &dst.da, src_array, n)
|
||||
|
||||
#define da_move(dst, src) darray_move(&dst.da, &src.da)
|
||||
|
||||
#define da_find(v, item, idx) \
|
||||
darray_find(sizeof(*v.array), &v.da, item, idx)
|
||||
#define da_find(v, item, idx) darray_find(sizeof(*v.array), &v.da, item, idx)
|
||||
|
||||
#define da_push_back(v, item) darray_push_back(sizeof(*v.array), &v.da, item)
|
||||
|
||||
|
|
@ -490,19 +497,15 @@ static inline void darray_swap(const size_t element_size,
|
|||
#define da_insert(v, idx, item) \
|
||||
darray_insert(sizeof(*v.array), &v.da, idx, item)
|
||||
|
||||
#define da_insert_new(v, idx) \
|
||||
darray_insert_new(sizeof(*v.array), &v.da, idx)
|
||||
#define da_insert_new(v, idx) darray_insert_new(sizeof(*v.array), &v.da, idx)
|
||||
|
||||
#define da_insert_array(dst, idx, src_array, n) \
|
||||
darray_insert_array(sizeof(*dst.array), &dst.da, idx, \
|
||||
src_array, n)
|
||||
darray_insert_array(sizeof(*dst.array), &dst.da, idx, src_array, n)
|
||||
|
||||
#define da_insert_da(dst, idx, src) \
|
||||
darray_insert_darray(sizeof(*dst.array), &dst.da, idx, \
|
||||
&src.da)
|
||||
darray_insert_darray(sizeof(*dst.array), &dst.da, idx, &src.da)
|
||||
|
||||
#define da_erase(dst, idx) \
|
||||
darray_erase(sizeof(*dst.array), &dst.da, idx)
|
||||
#define da_erase(dst, idx) darray_erase(sizeof(*dst.array), &dst.da, idx)
|
||||
|
||||
#define da_erase_item(dst, item) \
|
||||
darray_erase_item(sizeof(*dst.array), &dst.da, item)
|
||||
|
|
@ -510,21 +513,17 @@ static inline void darray_swap(const size_t element_size,
|
|||
#define da_erase_range(dst, from, to) \
|
||||
darray_erase_range(sizeof(*dst.array), &dst.da, from, to)
|
||||
|
||||
#define da_pop_back(dst) \
|
||||
darray_pop_back(sizeof(*dst.array), &dst.da);
|
||||
#define da_pop_back(dst) darray_pop_back(sizeof(*dst.array), &dst.da);
|
||||
|
||||
#define da_join(dst, src) \
|
||||
darray_join(sizeof(*dst.array), &dst.da, &src.da)
|
||||
#define da_join(dst, src) darray_join(sizeof(*dst.array), &dst.da, &src.da)
|
||||
|
||||
#define da_split(dst1, dst2, src, idx) \
|
||||
darray_split(sizeof(*src.array), &dst1.da, &dst2.da, \
|
||||
&src.da, idx)
|
||||
darray_split(sizeof(*src.array), &dst1.da, &dst2.da, &src.da, idx)
|
||||
|
||||
#define da_move_item(v, from, to) \
|
||||
darray_move_item(sizeof(*v.array), &v.da, from, to)
|
||||
|
||||
#define da_swap(v, idx1, idx2) \
|
||||
darray_swap(sizeof(*v.array), &v.da, idx1, idx2)
|
||||
#define da_swap(v, idx1, idx2) darray_swap(sizeof(*v.array), &v.da, idx1, idx2)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -174,7 +174,7 @@ char *astrstri(const char *str, const char *find)
|
|||
|
||||
do {
|
||||
if (astrcmpi_n(str, find, len) == 0)
|
||||
return (char*)str;
|
||||
return (char *)str;
|
||||
} while (*str++);
|
||||
|
||||
return NULL;
|
||||
|
|
@ -191,7 +191,7 @@ wchar_t *wstrstri(const wchar_t *str, const wchar_t *find)
|
|||
|
||||
do {
|
||||
if (wstrcmpi_n(str, find, len) == 0)
|
||||
return (wchar_t*)str;
|
||||
return (wchar_t *)str;
|
||||
} while (*str++);
|
||||
|
||||
return NULL;
|
||||
|
|
@ -205,7 +205,7 @@ static inline bool is_padding(char ch)
|
|||
char *strdepad(char *str)
|
||||
{
|
||||
char *temp;
|
||||
size_t len;
|
||||
size_t len;
|
||||
|
||||
if (!str)
|
||||
return str;
|
||||
|
|
@ -223,7 +223,7 @@ char *strdepad(char *str)
|
|||
memmove(str, temp, len + 1);
|
||||
|
||||
if (len) {
|
||||
temp = str + (len-1);
|
||||
temp = str + (len - 1);
|
||||
while (is_padding(*temp))
|
||||
*(temp--) = 0;
|
||||
}
|
||||
|
|
@ -234,7 +234,7 @@ char *strdepad(char *str)
|
|||
wchar_t *wcsdepad(wchar_t *str)
|
||||
{
|
||||
wchar_t *temp;
|
||||
size_t len;
|
||||
size_t len;
|
||||
|
||||
if (!str)
|
||||
return str;
|
||||
|
|
@ -249,10 +249,10 @@ wchar_t *wcsdepad(wchar_t *str)
|
|||
|
||||
len = wcslen(str);
|
||||
if (temp != str)
|
||||
memmove(str, temp, (len+1) * sizeof(wchar_t));
|
||||
memmove(str, temp, (len + 1) * sizeof(wchar_t));
|
||||
|
||||
if (len) {
|
||||
temp = str + (len-1);
|
||||
temp = str + (len - 1);
|
||||
while (*temp == ' ' || *temp == '\t')
|
||||
*(temp--) = 0;
|
||||
}
|
||||
|
|
@ -264,9 +264,9 @@ char **strlist_split(const char *str, char split_ch, bool include_empty)
|
|||
{
|
||||
const char *cur_str = str;
|
||||
const char *next_str;
|
||||
char * out = NULL;
|
||||
size_t count = 0;
|
||||
size_t total_size = 0;
|
||||
char *out = NULL;
|
||||
size_t count = 0;
|
||||
size_t total_size = 0;
|
||||
|
||||
if (str) {
|
||||
char **table;
|
||||
|
|
@ -295,16 +295,16 @@ char **strlist_split(const char *str, char split_ch, bool include_empty)
|
|||
|
||||
/* ------------------ */
|
||||
|
||||
cur_pos = (count + 1) * sizeof(char *);
|
||||
cur_pos = (count + 1) * sizeof(char *);
|
||||
total_size += cur_pos;
|
||||
out = bmalloc(total_size);
|
||||
offset = out + cur_pos;
|
||||
table = (char **)out;
|
||||
out = bmalloc(total_size);
|
||||
offset = out + cur_pos;
|
||||
table = (char **)out;
|
||||
|
||||
/* ------------------ */
|
||||
|
||||
next_str = strchr(str, split_ch);
|
||||
cur_str = str;
|
||||
cur_str = str;
|
||||
|
||||
while (next_str) {
|
||||
size_t size = next_str - cur_str;
|
||||
|
|
@ -328,7 +328,7 @@ char **strlist_split(const char *str, char split_ch, bool include_empty)
|
|||
table[cur_idx] = NULL;
|
||||
}
|
||||
|
||||
return (char**)out;
|
||||
return (char **)out;
|
||||
}
|
||||
|
||||
void strlist_free(char **strlist)
|
||||
|
|
@ -379,7 +379,7 @@ void dstr_ncopy(struct dstr *dst, const char *array, const size_t len)
|
|||
return;
|
||||
|
||||
dst->array = bmemdup(array, len + 1);
|
||||
dst->len = len;
|
||||
dst->len = len;
|
||||
dst->capacity = len + 1;
|
||||
|
||||
dst->array[len] = 0;
|
||||
|
|
@ -397,7 +397,7 @@ void dstr_ncopy_dstr(struct dstr *dst, const struct dstr *str, const size_t len)
|
|||
|
||||
newlen = size_min(len, str->len);
|
||||
dst->array = bmemdup(str->array, newlen + 1);
|
||||
dst->len = newlen;
|
||||
dst->len = newlen;
|
||||
dst->capacity = newlen + 1;
|
||||
|
||||
dst->array[newlen] = 0;
|
||||
|
|
@ -412,7 +412,7 @@ void dstr_cat_dstr(struct dstr *dst, const struct dstr *str)
|
|||
new_len = dst->len + str->len;
|
||||
|
||||
dstr_ensure_capacity(dst, new_len + 1);
|
||||
memcpy(dst->array+dst->len, str->array, str->len + 1);
|
||||
memcpy(dst->array + dst->len, str->array, str->len + 1);
|
||||
dst->len = new_len;
|
||||
}
|
||||
|
||||
|
|
@ -430,7 +430,7 @@ void dstr_ncat(struct dstr *dst, const char *array, const size_t len)
|
|||
new_len = dst->len + len;
|
||||
|
||||
dstr_ensure_capacity(dst, new_len + 1);
|
||||
memcpy(dst->array+dst->len, array, len);
|
||||
memcpy(dst->array + dst->len, array, len);
|
||||
|
||||
dst->len = new_len;
|
||||
dst->array[new_len] = 0;
|
||||
|
|
@ -446,7 +446,7 @@ void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str, const size_t len)
|
|||
new_len = dst->len + in_len;
|
||||
|
||||
dstr_ensure_capacity(dst, new_len + 1);
|
||||
memcpy(dst->array+dst->len, str->array, in_len);
|
||||
memcpy(dst->array + dst->len, str->array, in_len);
|
||||
|
||||
dst->len = new_len;
|
||||
dst->array[new_len] = 0;
|
||||
|
|
@ -467,14 +467,14 @@ void dstr_insert(struct dstr *dst, const size_t idx, const char *array)
|
|||
|
||||
dstr_ensure_capacity(dst, new_len + 1);
|
||||
|
||||
memmove(dst->array+idx+len, dst->array+idx, dst->len - idx + 1);
|
||||
memcpy(dst->array+idx, array, len);
|
||||
memmove(dst->array + idx + len, dst->array + idx, dst->len - idx + 1);
|
||||
memcpy(dst->array + idx, array, len);
|
||||
|
||||
dst->len = new_len;
|
||||
}
|
||||
|
||||
void dstr_insert_dstr(struct dstr *dst, const size_t idx,
|
||||
const struct dstr *str)
|
||||
const struct dstr *str)
|
||||
{
|
||||
size_t new_len;
|
||||
if (!str->len)
|
||||
|
|
@ -486,10 +486,11 @@ void dstr_insert_dstr(struct dstr *dst, const size_t idx,
|
|||
|
||||
new_len = dst->len + str->len;
|
||||
|
||||
dstr_ensure_capacity(dst, (new_len+1));
|
||||
dstr_ensure_capacity(dst, (new_len + 1));
|
||||
|
||||
memmove(dst->array+idx+str->len, dst->array+idx, dst->len - idx + 1);
|
||||
memcpy(dst->array+idx, str->array, str->len);
|
||||
memmove(dst->array + idx + str->len, dst->array + idx,
|
||||
dst->len - idx + 1);
|
||||
memcpy(dst->array + idx, str->array, str->len);
|
||||
|
||||
dst->len = new_len;
|
||||
}
|
||||
|
|
@ -501,8 +502,8 @@ void dstr_insert_ch(struct dstr *dst, const size_t idx, const char ch)
|
|||
return;
|
||||
}
|
||||
|
||||
dstr_ensure_capacity(dst, (++dst->len+1));
|
||||
memmove(dst->array+idx+1, dst->array+idx, dst->len - idx + 1);
|
||||
dstr_ensure_capacity(dst, (++dst->len + 1));
|
||||
memmove(dst->array + idx + 1, dst->array + idx, dst->len - idx + 1);
|
||||
dst->array[idx] = ch;
|
||||
}
|
||||
|
||||
|
|
@ -516,13 +517,13 @@ void dstr_remove(struct dstr *dst, const size_t idx, const size_t count)
|
|||
return;
|
||||
}
|
||||
|
||||
end = idx+count;
|
||||
end = idx + count;
|
||||
if (end == dst->len)
|
||||
dst->array[idx] = 0;
|
||||
else
|
||||
memmove(dst->array+idx, dst->array+end, dst->len - end + 1);
|
||||
memmove(dst->array + idx, dst->array + end, dst->len - end + 1);
|
||||
|
||||
dst->len -= count;
|
||||
dst->len -= count;
|
||||
}
|
||||
|
||||
void dstr_printf(struct dstr *dst, const char *format, ...)
|
||||
|
|
@ -549,7 +550,8 @@ void dstr_vprintf(struct dstr *dst, const char *format, va_list args)
|
|||
int len = vsnprintf(NULL, 0, format, args_cp);
|
||||
va_end(args_cp);
|
||||
|
||||
if (len < 0) len = 4095;
|
||||
if (len < 0)
|
||||
len = 4095;
|
||||
|
||||
dstr_ensure_capacity(dst, ((size_t)len) + 1);
|
||||
len = vsnprintf(dst->array, ((size_t)len) + 1, format, args);
|
||||
|
|
@ -570,8 +572,9 @@ void dstr_vcatf(struct dstr *dst, const char *format, va_list args)
|
|||
int len = vsnprintf(NULL, 0, format, args_cp);
|
||||
va_end(args_cp);
|
||||
|
||||
if (len < 0) len = 4095;
|
||||
|
||||
if (len < 0)
|
||||
len = 4095;
|
||||
|
||||
dstr_ensure_capacity(dst, dst->len + ((size_t)len) + 1);
|
||||
len = vsnprintf(dst->array + dst->len, ((size_t)len) + 1, format, args);
|
||||
|
||||
|
|
@ -583,9 +586,8 @@ void dstr_vcatf(struct dstr *dst, const char *format, va_list args)
|
|||
dst->len += len < 0 ? strlen(dst->array + dst->len) : (size_t)len;
|
||||
}
|
||||
|
||||
void dstr_safe_printf(struct dstr *dst, const char *format,
|
||||
const char *val1, const char *val2, const char *val3,
|
||||
const char *val4)
|
||||
void dstr_safe_printf(struct dstr *dst, const char *format, const char *val1,
|
||||
const char *val2, const char *val3, const char *val4)
|
||||
{
|
||||
dstr_copy(dst, format);
|
||||
if (val1)
|
||||
|
|
@ -598,8 +600,7 @@ void dstr_safe_printf(struct dstr *dst, const char *format,
|
|||
dstr_replace(dst, "$4", val4);
|
||||
}
|
||||
|
||||
void dstr_replace(struct dstr *str, const char *find,
|
||||
const char *replace)
|
||||
void dstr_replace(struct dstr *str, const char *find, const char *replace)
|
||||
{
|
||||
size_t find_len, replace_len;
|
||||
char *temp;
|
||||
|
|
@ -610,7 +611,7 @@ void dstr_replace(struct dstr *str, const char *find,
|
|||
if (!replace)
|
||||
replace = "";
|
||||
|
||||
find_len = strlen(find);
|
||||
find_len = strlen(find);
|
||||
replace_len = strlen(replace);
|
||||
temp = str->array;
|
||||
|
||||
|
|
@ -618,11 +619,11 @@ void dstr_replace(struct dstr *str, const char *find,
|
|||
unsigned long count = 0;
|
||||
|
||||
while ((temp = strstr(temp, find)) != NULL) {
|
||||
char *end = temp+find_len;
|
||||
char *end = temp + find_len;
|
||||
size_t end_len = strlen(end);
|
||||
|
||||
if (end_len) {
|
||||
memmove(temp+replace_len, end, end_len + 1);
|
||||
memmove(temp + replace_len, end, end_len + 1);
|
||||
if (replace_len)
|
||||
memcpy(temp, replace, replace_len);
|
||||
} else {
|
||||
|
|
@ -634,7 +635,7 @@ void dstr_replace(struct dstr *str, const char *find,
|
|||
}
|
||||
|
||||
if (count)
|
||||
str->len += (replace_len-find_len) * count;
|
||||
str->len += (replace_len - find_len) * count;
|
||||
|
||||
} else if (replace_len > find_len) {
|
||||
unsigned long count = 0;
|
||||
|
|
@ -647,16 +648,16 @@ void dstr_replace(struct dstr *str, const char *find,
|
|||
if (!count)
|
||||
return;
|
||||
|
||||
str->len += (replace_len-find_len) * count;
|
||||
str->len += (replace_len - find_len) * count;
|
||||
dstr_ensure_capacity(str, str->len + 1);
|
||||
temp = str->array;
|
||||
|
||||
while ((temp = strstr(temp, find)) != NULL) {
|
||||
char *end = temp+find_len;
|
||||
char *end = temp + find_len;
|
||||
size_t end_len = strlen(end);
|
||||
|
||||
if (end_len) {
|
||||
memmove(temp+replace_len, end, end_len + 1);
|
||||
memmove(temp + replace_len, end, end_len + 1);
|
||||
memcpy(temp, replace, replace_len);
|
||||
} else {
|
||||
strcpy(temp, replace);
|
||||
|
|
@ -692,12 +693,12 @@ void dstr_left(struct dstr *dst, const struct dstr *str, const size_t pos)
|
|||
}
|
||||
|
||||
void dstr_mid(struct dstr *dst, const struct dstr *str, const size_t start,
|
||||
const size_t count)
|
||||
const size_t count)
|
||||
{
|
||||
struct dstr temp;
|
||||
dstr_init(&temp);
|
||||
dstr_copy_dstr(&temp, str);
|
||||
dstr_ncopy(dst, temp.array+start, count);
|
||||
dstr_ncopy(dst, temp.array + start, count);
|
||||
dstr_free(&temp);
|
||||
}
|
||||
|
||||
|
|
@ -705,7 +706,7 @@ void dstr_right(struct dstr *dst, const struct dstr *str, const size_t pos)
|
|||
{
|
||||
struct dstr temp;
|
||||
dstr_init(&temp);
|
||||
dstr_ncopy(&temp, str->array+pos, str->len-pos);
|
||||
dstr_ncopy(&temp, str->array + pos, str->len - pos);
|
||||
dstr_copy_dstr(dst, &temp);
|
||||
dstr_free(&temp);
|
||||
}
|
||||
|
|
@ -736,7 +737,7 @@ void dstr_from_wcs(struct dstr *dst, const wchar_t *wstr)
|
|||
|
||||
if (len) {
|
||||
dstr_resize(dst, len);
|
||||
wchar_to_utf8(wstr, 0, dst->array, len+1, 0);
|
||||
wchar_to_utf8(wstr, 0, dst->array, len + 1, 0);
|
||||
} else {
|
||||
dstr_free(dst);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ static inline void dstr_init_move(struct dstr *dst, struct dstr *src);
|
|||
static inline void dstr_init_move_array(struct dstr *dst, char *str);
|
||||
static inline void dstr_init_copy(struct dstr *dst, const char *src);
|
||||
static inline void dstr_init_copy_dstr(struct dstr *dst,
|
||||
const struct dstr *src);
|
||||
const struct dstr *src);
|
||||
EXPORT void dstr_init_copy_strref(struct dstr *dst, const struct strref *src);
|
||||
|
||||
static inline void dstr_free(struct dstr *dst);
|
||||
|
|
@ -79,10 +79,9 @@ EXPORT void dstr_copy(struct dstr *dst, const char *array);
|
|||
static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src);
|
||||
EXPORT void dstr_copy_strref(struct dstr *dst, const struct strref *src);
|
||||
|
||||
EXPORT void dstr_ncopy(struct dstr *dst, const char *array,
|
||||
const size_t len);
|
||||
EXPORT void dstr_ncopy(struct dstr *dst, const char *array, const size_t len);
|
||||
EXPORT void dstr_ncopy_dstr(struct dstr *dst, const struct dstr *src,
|
||||
const size_t len);
|
||||
const size_t len);
|
||||
|
||||
static inline void dstr_resize(struct dstr *dst, const size_t num);
|
||||
static inline void dstr_reserve(struct dstr *dst, const size_t num);
|
||||
|
|
@ -97,14 +96,12 @@ static inline void dstr_cat_ch(struct dstr *dst, char ch);
|
|||
|
||||
EXPORT void dstr_ncat(struct dstr *dst, const char *array, const size_t len);
|
||||
EXPORT void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str,
|
||||
const size_t len);
|
||||
const size_t len);
|
||||
|
||||
EXPORT void dstr_insert(struct dstr *dst, const size_t idx,
|
||||
const char *array);
|
||||
EXPORT void dstr_insert(struct dstr *dst, const size_t idx, const char *array);
|
||||
EXPORT void dstr_insert_dstr(struct dstr *dst, const size_t idx,
|
||||
const struct dstr *str);
|
||||
EXPORT void dstr_insert_ch(struct dstr *dst, const size_t idx,
|
||||
const char ch);
|
||||
const struct dstr *str);
|
||||
EXPORT void dstr_insert_ch(struct dstr *dst, const size_t idx, const char ch);
|
||||
|
||||
EXPORT void dstr_remove(struct dstr *dst, const size_t idx, const size_t count);
|
||||
|
||||
|
|
@ -117,32 +114,30 @@ EXPORT void dstr_vprintf(struct dstr *dst, const char *format, va_list args);
|
|||
EXPORT void dstr_vcatf(struct dstr *dst, const char *format, va_list args);
|
||||
|
||||
EXPORT void dstr_safe_printf(struct dstr *dst, const char *format,
|
||||
const char *val1, const char *val2, const char *val3,
|
||||
const char *val4);
|
||||
const char *val1, const char *val2,
|
||||
const char *val3, const char *val4);
|
||||
|
||||
static inline const char *dstr_find_i(const struct dstr *str,
|
||||
const char *find);
|
||||
static inline const char *dstr_find(const struct dstr *str,
|
||||
const char *find);
|
||||
static inline const char *dstr_find_i(const struct dstr *str, const char *find);
|
||||
static inline const char *dstr_find(const struct dstr *str, const char *find);
|
||||
|
||||
EXPORT void dstr_replace(struct dstr *str, const char *find,
|
||||
const char *replace);
|
||||
const char *replace);
|
||||
|
||||
static inline int dstr_cmp(const struct dstr *str1, const char *str2);
|
||||
static inline int dstr_cmpi(const struct dstr *str1, const char *str2);
|
||||
static inline int dstr_ncmp(const struct dstr *str1, const char *str2,
|
||||
const size_t n);
|
||||
const size_t n);
|
||||
static inline int dstr_ncmpi(const struct dstr *str1, const char *str2,
|
||||
const size_t n);
|
||||
const size_t n);
|
||||
|
||||
EXPORT void dstr_depad(struct dstr *dst);
|
||||
|
||||
EXPORT void dstr_left(struct dstr *dst, const struct dstr *str,
|
||||
const size_t pos);
|
||||
const size_t pos);
|
||||
EXPORT void dstr_mid(struct dstr *dst, const struct dstr *str,
|
||||
const size_t start, const size_t count);
|
||||
const size_t start, const size_t count);
|
||||
EXPORT void dstr_right(struct dstr *dst, const struct dstr *str,
|
||||
const size_t pos);
|
||||
const size_t pos);
|
||||
|
||||
static inline char dstr_end(const struct dstr *str);
|
||||
|
||||
|
|
@ -160,15 +155,15 @@ EXPORT void dstr_to_lower(struct dstr *str);
|
|||
|
||||
static inline void dstr_init(struct dstr *dst)
|
||||
{
|
||||
dst->array = NULL;
|
||||
dst->len = 0;
|
||||
dst->array = NULL;
|
||||
dst->len = 0;
|
||||
dst->capacity = 0;
|
||||
}
|
||||
|
||||
static inline void dstr_init_move_array(struct dstr *dst, char *str)
|
||||
{
|
||||
dst->array = str;
|
||||
dst->len = (!str) ? 0 : strlen(str);
|
||||
dst->array = str;
|
||||
dst->len = (!str) ? 0 : strlen(str);
|
||||
dst->capacity = dst->len + 1;
|
||||
}
|
||||
|
||||
|
|
@ -193,8 +188,8 @@ static inline void dstr_init_copy_dstr(struct dstr *dst, const struct dstr *src)
|
|||
static inline void dstr_free(struct dstr *dst)
|
||||
{
|
||||
bfree(dst->array);
|
||||
dst->array = NULL;
|
||||
dst->len = 0;
|
||||
dst->array = NULL;
|
||||
dst->len = 0;
|
||||
dst->capacity = 0;
|
||||
}
|
||||
|
||||
|
|
@ -202,14 +197,14 @@ static inline void dstr_array_free(struct dstr *array, const size_t count)
|
|||
{
|
||||
size_t i;
|
||||
for (i = 0; i < count; i++)
|
||||
dstr_free(array+i);
|
||||
dstr_free(array + i);
|
||||
}
|
||||
|
||||
static inline void dstr_move_array(struct dstr *dst, char *str)
|
||||
{
|
||||
dstr_free(dst);
|
||||
dst->array = str;
|
||||
dst->len = (!str) ? 0 : strlen(str);
|
||||
dst->array = str;
|
||||
dst->len = (!str) ? 0 : strlen(str);
|
||||
dst->capacity = dst->len + 1;
|
||||
}
|
||||
|
||||
|
|
@ -225,10 +220,10 @@ static inline void dstr_ensure_capacity(struct dstr *dst, const size_t new_size)
|
|||
if (new_size <= dst->capacity)
|
||||
return;
|
||||
|
||||
new_cap = (!dst->capacity) ? new_size : dst->capacity*2;
|
||||
new_cap = (!dst->capacity) ? new_size : dst->capacity * 2;
|
||||
if (new_size > new_cap)
|
||||
new_cap = new_size;
|
||||
dst->array = (char*)brealloc(dst->array, new_cap);
|
||||
dst->array = (char *)brealloc(dst->array, new_cap);
|
||||
dst->capacity = new_cap;
|
||||
}
|
||||
|
||||
|
|
@ -249,7 +244,7 @@ static inline void dstr_reserve(struct dstr *dst, const size_t capacity)
|
|||
if (capacity == 0 || capacity <= dst->len)
|
||||
return;
|
||||
|
||||
dst->array = (char*)brealloc(dst->array, capacity);
|
||||
dst->array = (char *)brealloc(dst->array, capacity);
|
||||
dst->capacity = capacity;
|
||||
}
|
||||
|
||||
|
|
@ -288,8 +283,8 @@ static inline void dstr_cat(struct dstr *dst, const char *array)
|
|||
static inline void dstr_cat_ch(struct dstr *dst, char ch)
|
||||
{
|
||||
dstr_ensure_capacity(dst, ++dst->len + 1);
|
||||
dst->array[dst->len-1] = ch;
|
||||
dst->array[dst->len] = 0;
|
||||
dst->array[dst->len - 1] = ch;
|
||||
dst->array[dst->len] = 0;
|
||||
}
|
||||
|
||||
static inline const char *dstr_find_i(const struct dstr *str, const char *find)
|
||||
|
|
@ -313,13 +308,13 @@ static inline int dstr_cmpi(const struct dstr *str1, const char *str2)
|
|||
}
|
||||
|
||||
static inline int dstr_ncmp(const struct dstr *str1, const char *str2,
|
||||
const size_t n)
|
||||
const size_t n)
|
||||
{
|
||||
return astrcmp_n(str1->array, str2, n);
|
||||
}
|
||||
|
||||
static inline int dstr_ncmpi(const struct dstr *str1, const char *str2,
|
||||
const size_t n)
|
||||
const size_t n)
|
||||
{
|
||||
return astrcmpi_n(str1->array, str2, n);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,15 +21,12 @@
|
|||
class DStr {
|
||||
dstr str;
|
||||
|
||||
DStr(DStr const&) = delete;
|
||||
DStr &operator=(DStr const&) = delete;
|
||||
DStr(DStr const &) = delete;
|
||||
DStr &operator=(DStr const &) = delete;
|
||||
|
||||
public:
|
||||
inline DStr() {dstr_init(&str);}
|
||||
inline DStr(DStr &&other) : DStr()
|
||||
{
|
||||
dstr_move(&str, &other.str);
|
||||
}
|
||||
inline DStr() { dstr_init(&str); }
|
||||
inline DStr(DStr &&other) : DStr() { dstr_move(&str, &other.str); }
|
||||
|
||||
inline DStr &operator=(DStr &&other)
|
||||
{
|
||||
|
|
@ -37,13 +34,13 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
inline ~DStr() {dstr_free(&str);}
|
||||
inline ~DStr() { dstr_free(&str); }
|
||||
|
||||
inline operator dstr*() {return &str;}
|
||||
inline operator const dstr*() const {return &str;}
|
||||
inline operator dstr *() { return &str; }
|
||||
inline operator const dstr *() const { return &str; }
|
||||
|
||||
inline operator char*() {return str.array;}
|
||||
inline operator const char*() const {return str.array;}
|
||||
inline operator char *() { return str.array; }
|
||||
inline operator const char *() const { return str.array; }
|
||||
|
||||
inline dstr *operator->() {return &str;}
|
||||
inline dstr *operator->() { return &str; }
|
||||
};
|
||||
|
|
|
|||
|
|
@ -24,14 +24,20 @@ static size_t file_input_read(void *file, void *data, size_t size)
|
|||
}
|
||||
|
||||
static int64_t file_input_seek(void *file, int64_t offset,
|
||||
enum serialize_seek_type seek_type)
|
||||
enum serialize_seek_type seek_type)
|
||||
{
|
||||
int origin = SEEK_SET;
|
||||
|
||||
switch (seek_type) {
|
||||
case SERIALIZE_SEEK_START: origin = SEEK_SET; break;
|
||||
case SERIALIZE_SEEK_CURRENT: origin = SEEK_CUR; break;
|
||||
case SERIALIZE_SEEK_END: origin = SEEK_END; break;
|
||||
case SERIALIZE_SEEK_START:
|
||||
origin = SEEK_SET;
|
||||
break;
|
||||
case SERIALIZE_SEEK_CURRENT:
|
||||
origin = SEEK_CUR;
|
||||
break;
|
||||
case SERIALIZE_SEEK_END:
|
||||
origin = SEEK_END;
|
||||
break;
|
||||
}
|
||||
|
||||
if (os_fseeki64(file, offset, origin) == -1)
|
||||
|
|
@ -79,15 +85,21 @@ static size_t file_output_write(void *sdata, const void *data, size_t size)
|
|||
}
|
||||
|
||||
static int64_t file_output_seek(void *sdata, int64_t offset,
|
||||
enum serialize_seek_type seek_type)
|
||||
enum serialize_seek_type seek_type)
|
||||
{
|
||||
struct file_output_data *out = sdata;
|
||||
int origin = SEEK_SET;
|
||||
|
||||
switch (seek_type) {
|
||||
case SERIALIZE_SEEK_START: origin = SEEK_SET; break;
|
||||
case SERIALIZE_SEEK_CURRENT: origin = SEEK_CUR; break;
|
||||
case SERIALIZE_SEEK_END: origin = SEEK_END; break;
|
||||
case SERIALIZE_SEEK_START:
|
||||
origin = SEEK_SET;
|
||||
break;
|
||||
case SERIALIZE_SEEK_CURRENT:
|
||||
origin = SEEK_CUR;
|
||||
break;
|
||||
case SERIALIZE_SEEK_END:
|
||||
origin = SEEK_END;
|
||||
break;
|
||||
}
|
||||
|
||||
if (os_fseeki64(out->file, offset, origin) == -1)
|
||||
|
|
@ -121,8 +133,8 @@ bool file_output_serializer_init(struct serializer *s, const char *path)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool file_output_serializer_init_safe(struct serializer *s,
|
||||
const char *path, const char *temp_ext)
|
||||
bool file_output_serializer_init_safe(struct serializer *s, const char *path,
|
||||
const char *temp_ext)
|
||||
{
|
||||
struct dstr temp_name = {0};
|
||||
struct file_output_data *out;
|
||||
|
|
|
|||
|
|
@ -23,5 +23,6 @@ EXPORT void file_input_serializer_free(struct serializer *s);
|
|||
|
||||
EXPORT bool file_output_serializer_init(struct serializer *s, const char *path);
|
||||
EXPORT bool file_output_serializer_init_safe(struct serializer *s,
|
||||
const char *path, const char *temp_ext);
|
||||
const char *path,
|
||||
const char *temp_ext);
|
||||
EXPORT void file_output_serializer_free(struct serializer *s);
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ int strref_cmp(const struct strref *str1, const char *str2)
|
|||
char ch1, ch2;
|
||||
|
||||
ch1 = (i < str1->len) ? str1->array[i] : 0;
|
||||
ch2 = *str2;
|
||||
ch2 = *str2;
|
||||
|
||||
if (ch1 < ch2)
|
||||
return -1;
|
||||
|
|
@ -56,7 +56,7 @@ int strref_cmpi(const struct strref *str1, const char *str2)
|
|||
char ch1, ch2;
|
||||
|
||||
ch1 = (i < str1->len) ? (char)toupper(str1->array[i]) : 0;
|
||||
ch2 = (char)toupper(*str2);
|
||||
ch2 = (char)toupper(*str2);
|
||||
|
||||
if (ch1 < ch2)
|
||||
return -1;
|
||||
|
|
@ -80,7 +80,7 @@ int strref_cmp_strref(const struct strref *str1, const struct strref *str2)
|
|||
char ch1, ch2;
|
||||
|
||||
ch1 = (i < str1->len) ? str1->array[i] : 0;
|
||||
ch2 = (i < str2->len) ? str2->array[i] : 0;
|
||||
ch2 = (i < str2->len) ? str2->array[i] : 0;
|
||||
|
||||
if (ch1 < ch2)
|
||||
return -1;
|
||||
|
|
@ -139,7 +139,7 @@ bool valid_int_str(const char *str, size_t n)
|
|||
return false;
|
||||
|
||||
found_num = true;
|
||||
} while(*++str && --n);
|
||||
} while (*++str && --n);
|
||||
|
||||
return found_num;
|
||||
}
|
||||
|
|
@ -183,26 +183,26 @@ bool valid_float_str(const char *str, size_t n)
|
|||
} else {
|
||||
found_num = true;
|
||||
}
|
||||
} while(*++str && --n);
|
||||
} while (*++str && --n);
|
||||
|
||||
return found_num;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
void error_data_add(struct error_data *data, const char *file,
|
||||
uint32_t row, uint32_t column, const char *msg, int level)
|
||||
void error_data_add(struct error_data *data, const char *file, uint32_t row,
|
||||
uint32_t column, const char *msg, int level)
|
||||
{
|
||||
struct error_item item;
|
||||
|
||||
if (!data)
|
||||
return;
|
||||
|
||||
item.file = file;
|
||||
item.row = row;
|
||||
item.file = file;
|
||||
item.row = row;
|
||||
item.column = column;
|
||||
item.level = level;
|
||||
item.error = bstrdup(msg);
|
||||
item.level = level;
|
||||
item.error = bstrdup(msg);
|
||||
|
||||
da_push_back(data->errors, &item);
|
||||
}
|
||||
|
|
@ -215,9 +215,9 @@ char *error_data_buildstring(struct error_data *ed)
|
|||
|
||||
dstr_init(&str);
|
||||
for (i = 0; i < ed->errors.num; i++) {
|
||||
struct error_item *item = items+i;
|
||||
struct error_item *item = items + i;
|
||||
dstr_catf(&str, "%s (%u, %u): %s\n", item->file, item->row,
|
||||
item->column, item->error);
|
||||
item->column, item->error);
|
||||
}
|
||||
|
||||
return str.array;
|
||||
|
|
@ -238,7 +238,7 @@ static inline enum base_token_type get_char_token_type(const char ch)
|
|||
}
|
||||
|
||||
bool lexer_getbasetoken(struct lexer *lex, struct base_token *token,
|
||||
enum ignore_whitespace iws)
|
||||
enum ignore_whitespace iws)
|
||||
{
|
||||
const char *offset = lex->offset;
|
||||
const char *token_start = NULL;
|
||||
|
|
@ -254,10 +254,10 @@ bool lexer_getbasetoken(struct lexer *lex, struct base_token *token,
|
|||
|
||||
if (type == BASETOKEN_NONE) {
|
||||
if (new_type == BASETOKEN_WHITESPACE &&
|
||||
ignore_whitespace)
|
||||
ignore_whitespace)
|
||||
continue;
|
||||
|
||||
token_start = offset-1;
|
||||
token_start = offset - 1;
|
||||
type = new_type;
|
||||
|
||||
if (type != BASETOKEN_DIGIT &&
|
||||
|
|
@ -277,7 +277,7 @@ bool lexer_getbasetoken(struct lexer *lex, struct base_token *token,
|
|||
lex->offset = offset;
|
||||
|
||||
if (token_start && offset > token_start) {
|
||||
strref_set(&token->text, token_start, offset-token_start);
|
||||
strref_set(&token->text, token_start, offset - token_start);
|
||||
token->type = type;
|
||||
return true;
|
||||
}
|
||||
|
|
@ -285,8 +285,8 @@ bool lexer_getbasetoken(struct lexer *lex, struct base_token *token,
|
|||
return false;
|
||||
}
|
||||
|
||||
void lexer_getstroffset(const struct lexer *lex, const char *str,
|
||||
uint32_t *row, uint32_t *col)
|
||||
void lexer_getstroffset(const struct lexer *lex, const char *str, uint32_t *row,
|
||||
uint32_t *col)
|
||||
{
|
||||
uint32_t cur_col = 1, cur_row = 1;
|
||||
const char *text = lex->text;
|
||||
|
|
@ -296,7 +296,7 @@ void lexer_getstroffset(const struct lexer *lex, const char *str,
|
|||
|
||||
while (text < str) {
|
||||
if (is_newline(*text)) {
|
||||
text += newline_size(text)-1;
|
||||
text += newline_size(text) - 1;
|
||||
cur_col = 1;
|
||||
cur_row++;
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -35,19 +35,19 @@ struct strref {
|
|||
static inline void strref_clear(struct strref *dst)
|
||||
{
|
||||
dst->array = NULL;
|
||||
dst->len = 0;
|
||||
dst->len = 0;
|
||||
}
|
||||
|
||||
static inline void strref_set(struct strref *dst, const char *array, size_t len)
|
||||
{
|
||||
dst->array = array;
|
||||
dst->len = len;
|
||||
dst->len = len;
|
||||
}
|
||||
|
||||
static inline void strref_copy(struct strref *dst, const struct strref *src)
|
||||
{
|
||||
dst->array = src->array;
|
||||
dst->len = src->len;
|
||||
dst->len = src->len;
|
||||
}
|
||||
|
||||
static inline void strref_add(struct strref *dst, const struct strref *t)
|
||||
|
|
@ -66,9 +66,9 @@ static inline bool strref_is_empty(const struct strref *str)
|
|||
EXPORT int strref_cmp(const struct strref *str1, const char *str2);
|
||||
EXPORT int strref_cmpi(const struct strref *str1, const char *str2);
|
||||
EXPORT int strref_cmp_strref(const struct strref *str1,
|
||||
const struct strref *str2);
|
||||
const struct strref *str2);
|
||||
EXPORT int strref_cmpi_strref(const struct strref *str1,
|
||||
const struct strref *str2);
|
||||
const struct strref *str2);
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
|
|
@ -102,8 +102,7 @@ static inline bool is_space_or_tab(const char ch)
|
|||
|
||||
static inline bool is_newline_pair(char ch1, char ch2)
|
||||
{
|
||||
return (ch1 == '\r' && ch2 == '\n') ||
|
||||
(ch1 == '\n' && ch2 == '\r');
|
||||
return (ch1 == '\r' && ch2 == '\n') || (ch1 == '\n' && ch2 == '\r');
|
||||
}
|
||||
|
||||
static inline int newline_size(const char *array)
|
||||
|
|
@ -146,14 +145,14 @@ static inline void base_token_clear(struct base_token *t)
|
|||
}
|
||||
|
||||
static inline void base_token_copy(struct base_token *dst,
|
||||
struct base_token *src)
|
||||
struct base_token *src)
|
||||
{
|
||||
memcpy(dst, src, sizeof(struct base_token));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
#define LEX_ERROR 0
|
||||
#define LEX_ERROR 0
|
||||
#define LEX_WARNING 1
|
||||
|
||||
struct error_item {
|
||||
|
|
@ -178,7 +177,7 @@ static inline void error_item_array_free(struct error_item *array, size_t num)
|
|||
{
|
||||
size_t i;
|
||||
for (i = 0; i < num; i++)
|
||||
error_item_free(array+i);
|
||||
error_item_free(array + i);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
|
@ -199,18 +198,18 @@ static inline void error_data_free(struct error_data *data)
|
|||
}
|
||||
|
||||
static inline const struct error_item *error_data_item(struct error_data *ed,
|
||||
size_t idx)
|
||||
size_t idx)
|
||||
{
|
||||
return ed->errors.array+idx;
|
||||
return ed->errors.array + idx;
|
||||
}
|
||||
|
||||
EXPORT char *error_data_buildstring(struct error_data *ed);
|
||||
|
||||
EXPORT void error_data_add(struct error_data *ed, const char *file,
|
||||
uint32_t row, uint32_t column, const char *msg, int level);
|
||||
uint32_t row, uint32_t column, const char *msg,
|
||||
int level);
|
||||
|
||||
static inline size_t error_data_type_count(struct error_data *ed,
|
||||
int type)
|
||||
static inline size_t error_data_type_count(struct error_data *ed, int type)
|
||||
{
|
||||
size_t count = 0, i;
|
||||
for (i = 0; i < ed->errors.num; i++) {
|
||||
|
|
@ -252,14 +251,14 @@ static inline void lexer_free(struct lexer *lex)
|
|||
static inline void lexer_start(struct lexer *lex, const char *text)
|
||||
{
|
||||
lexer_free(lex);
|
||||
lex->text = bstrdup(text);
|
||||
lex->text = bstrdup(text);
|
||||
lex->offset = lex->text;
|
||||
}
|
||||
|
||||
static inline void lexer_start_move(struct lexer *lex, char *text)
|
||||
{
|
||||
lexer_free(lex);
|
||||
lex->text = text;
|
||||
lex->text = text;
|
||||
lex->offset = lex->text;
|
||||
}
|
||||
|
||||
|
|
@ -268,16 +267,13 @@ static inline void lexer_reset(struct lexer *lex)
|
|||
lex->offset = lex->text;
|
||||
}
|
||||
|
||||
enum ignore_whitespace {
|
||||
PARSE_WHITESPACE,
|
||||
IGNORE_WHITESPACE
|
||||
};
|
||||
enum ignore_whitespace { PARSE_WHITESPACE, IGNORE_WHITESPACE };
|
||||
|
||||
EXPORT bool lexer_getbasetoken(struct lexer *lex, struct base_token *t,
|
||||
enum ignore_whitespace iws);
|
||||
enum ignore_whitespace iws);
|
||||
|
||||
EXPORT void lexer_getstroffset(const struct lexer *lex, const char *str,
|
||||
uint32_t *row, uint32_t *col);
|
||||
uint32_t *row, uint32_t *col);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ struct os_process_pipe {
|
|||
};
|
||||
|
||||
os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
|
||||
const char *type)
|
||||
const char *type)
|
||||
{
|
||||
struct os_process_pipe pipe = {0};
|
||||
struct os_process_pipe *out;
|
||||
|
|
@ -38,7 +38,7 @@ os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
|
|||
pipe.file = popen(cmd_line, type);
|
||||
pipe.read_pipe = *type == 'r';
|
||||
|
||||
if (pipe.file == (FILE*)-1 || pipe.file == NULL) {
|
||||
if (pipe.file == (FILE *)-1 || pipe.file == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
@ -73,7 +73,8 @@ size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)
|
|||
return fread(data, 1, len, pp->file);
|
||||
}
|
||||
|
||||
size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data, size_t len)
|
||||
size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data,
|
||||
size_t len)
|
||||
{
|
||||
/* XXX: unsupported on posix */
|
||||
UNUSED_PARAMETER(pp);
|
||||
|
|
@ -83,7 +84,7 @@ size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data, size_t len
|
|||
}
|
||||
|
||||
size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data,
|
||||
size_t len)
|
||||
size_t len)
|
||||
{
|
||||
if (!pp) {
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -43,7 +43,8 @@ static bool create_pipe(HANDLE *input, HANDLE *output)
|
|||
}
|
||||
|
||||
static inline bool create_process(const char *cmd_line, HANDLE stdin_handle,
|
||||
HANDLE stdout_handle, HANDLE stderr_handle, HANDLE *process)
|
||||
HANDLE stdout_handle, HANDLE stderr_handle,
|
||||
HANDLE *process)
|
||||
{
|
||||
PROCESS_INFORMATION pi = {0};
|
||||
wchar_t *cmd_line_w = NULL;
|
||||
|
|
@ -59,7 +60,8 @@ static inline bool create_process(const char *cmd_line, HANDLE stdin_handle,
|
|||
os_utf8_to_wcs_ptr(cmd_line, 0, &cmd_line_w);
|
||||
if (cmd_line_w) {
|
||||
success = !!CreateProcessW(NULL, cmd_line_w, NULL, NULL, true,
|
||||
CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
|
||||
CREATE_NO_WINDOW, NULL, NULL, &si,
|
||||
&pi);
|
||||
|
||||
if (success) {
|
||||
*process = pi.hProcess;
|
||||
|
|
@ -73,7 +75,7 @@ static inline bool create_process(const char *cmd_line, HANDLE stdin_handle,
|
|||
}
|
||||
|
||||
os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
|
||||
const char *type)
|
||||
const char *type)
|
||||
{
|
||||
os_process_pipe_t *pp = NULL;
|
||||
bool read_pipe;
|
||||
|
|
@ -100,7 +102,7 @@ os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
|
|||
read_pipe = *type == 'r';
|
||||
|
||||
success = !!SetHandleInformation(read_pipe ? input : output,
|
||||
HANDLE_FLAG_INHERIT, false);
|
||||
HANDLE_FLAG_INHERIT, false);
|
||||
if (!success) {
|
||||
goto error;
|
||||
}
|
||||
|
|
@ -111,7 +113,8 @@ os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
|
|||
}
|
||||
|
||||
success = create_process(cmd_line, read_pipe ? NULL : input,
|
||||
read_pipe ? output : NULL, err_output, &process);
|
||||
read_pipe ? output : NULL, err_output,
|
||||
&process);
|
||||
if (!success) {
|
||||
goto error;
|
||||
}
|
||||
|
|
@ -174,7 +177,8 @@ size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)
|
|||
return 0;
|
||||
}
|
||||
|
||||
size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data, size_t len)
|
||||
size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data,
|
||||
size_t len)
|
||||
{
|
||||
DWORD bytes_read;
|
||||
bool success;
|
||||
|
|
@ -183,18 +187,18 @@ size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data, size_t len
|
|||
return 0;
|
||||
}
|
||||
|
||||
success = !!ReadFile(pp->handle_err, data, (DWORD)len, &bytes_read, NULL);
|
||||
success =
|
||||
!!ReadFile(pp->handle_err, data, (DWORD)len, &bytes_read, NULL);
|
||||
if (success && bytes_read) {
|
||||
return bytes_read;
|
||||
}
|
||||
else
|
||||
} else
|
||||
bytes_read = GetLastError();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data,
|
||||
size_t len)
|
||||
size_t len)
|
||||
{
|
||||
DWORD bytes_written;
|
||||
bool success;
|
||||
|
|
@ -206,8 +210,8 @@ size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data,
|
|||
return 0;
|
||||
}
|
||||
|
||||
success = !!WriteFile(pp->handle, data, (DWORD)len, &bytes_written,
|
||||
NULL);
|
||||
success =
|
||||
!!WriteFile(pp->handle, data, (DWORD)len, &bytes_written, NULL);
|
||||
if (success && bytes_written) {
|
||||
return bytes_written;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,12 +22,12 @@ struct os_process_pipe;
|
|||
typedef struct os_process_pipe os_process_pipe_t;
|
||||
|
||||
EXPORT os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
|
||||
const char *type);
|
||||
const char *type);
|
||||
EXPORT int os_process_pipe_destroy(os_process_pipe_t *pp);
|
||||
|
||||
EXPORT size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data,
|
||||
size_t len);
|
||||
size_t len);
|
||||
EXPORT size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data,
|
||||
size_t len);
|
||||
size_t len);
|
||||
EXPORT size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data,
|
||||
size_t len);
|
||||
size_t len);
|
||||
|
|
|
|||
|
|
@ -54,7 +54,8 @@ static double ns_time_compute_factor()
|
|||
static uint64_t ns_time_full()
|
||||
{
|
||||
static double factor = 0.;
|
||||
if (factor == 0.) factor = ns_time_compute_factor();
|
||||
if (factor == 0.)
|
||||
factor = ns_time_compute_factor();
|
||||
return (uint64_t)(mach_absolute_time() * factor);
|
||||
}
|
||||
|
||||
|
|
@ -72,18 +73,19 @@ static time_func ns_time_select_func()
|
|||
uint64_t os_gettime_ns(void)
|
||||
{
|
||||
static time_func f = NULL;
|
||||
if (!f) f = ns_time_select_func();
|
||||
if (!f)
|
||||
f = ns_time_select_func();
|
||||
return f();
|
||||
}
|
||||
|
||||
/* gets the location [domain mask]/Library/Application Support/[name] */
|
||||
static int os_get_path_internal(char *dst, size_t size, const char *name,
|
||||
NSSearchPathDomainMask domainMask)
|
||||
NSSearchPathDomainMask domainMask)
|
||||
{
|
||||
NSArray *paths = NSSearchPathForDirectoriesInDomains(
|
||||
NSApplicationSupportDirectory, domainMask, YES);
|
||||
NSApplicationSupportDirectory, domainMask, YES);
|
||||
|
||||
if([paths count] == 0)
|
||||
if ([paths count] == 0)
|
||||
bcrash("Could not get home directory (platform-cocoa)");
|
||||
|
||||
NSString *application_support = paths[0];
|
||||
|
|
@ -96,12 +98,12 @@ static int os_get_path_internal(char *dst, size_t size, const char *name,
|
|||
}
|
||||
|
||||
static char *os_get_path_ptr_internal(const char *name,
|
||||
NSSearchPathDomainMask domainMask)
|
||||
NSSearchPathDomainMask domainMask)
|
||||
{
|
||||
NSArray *paths = NSSearchPathForDirectoriesInDomains(
|
||||
NSApplicationSupportDirectory, domainMask, YES);
|
||||
NSApplicationSupportDirectory, domainMask, YES);
|
||||
|
||||
if([paths count] == 0)
|
||||
if ([paths count] == 0)
|
||||
bcrash("Could not get home directory (platform-cocoa)");
|
||||
|
||||
NSString *application_support = paths[0];
|
||||
|
|
@ -109,7 +111,7 @@ static char *os_get_path_ptr_internal(const char *name,
|
|||
NSUInteger len = [application_support
|
||||
lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
|
||||
|
||||
char *path_ptr = bmalloc(len+1);
|
||||
char *path_ptr = bmalloc(len + 1);
|
||||
|
||||
path_ptr[len] = 0;
|
||||
|
||||
|
|
@ -174,47 +176,47 @@ char *os_get_executable_path_ptr(const char *name)
|
|||
struct os_cpu_usage_info {
|
||||
int64_t last_cpu_time;
|
||||
int64_t last_sys_time;
|
||||
int core_count;
|
||||
int core_count;
|
||||
};
|
||||
|
||||
static inline void add_time_value(time_value_t *dst, time_value_t *a,
|
||||
time_value_t *b)
|
||||
time_value_t *b)
|
||||
{
|
||||
dst->microseconds = a->microseconds + b->microseconds;
|
||||
dst->seconds = a->seconds + b->seconds;
|
||||
dst->seconds = a->seconds + b->seconds;
|
||||
|
||||
if (dst->microseconds >= 1000000) {
|
||||
dst->seconds += dst->microseconds / 1000000;
|
||||
dst->seconds += dst->microseconds / 1000000;
|
||||
dst->microseconds %= 1000000;
|
||||
}
|
||||
}
|
||||
|
||||
static bool get_time_info(int64_t *cpu_time, int64_t *sys_time)
|
||||
{
|
||||
mach_port_t task = mach_task_self();
|
||||
mach_port_t task = mach_task_self();
|
||||
struct task_thread_times_info thread_data;
|
||||
struct task_basic_info_64 task_data;
|
||||
mach_msg_type_number_t count;
|
||||
kern_return_t kern_ret;
|
||||
time_value_t cur_time;
|
||||
struct task_basic_info_64 task_data;
|
||||
mach_msg_type_number_t count;
|
||||
kern_return_t kern_ret;
|
||||
time_value_t cur_time;
|
||||
|
||||
*cpu_time = 0;
|
||||
*sys_time = 0;
|
||||
|
||||
count = TASK_THREAD_TIMES_INFO_COUNT;
|
||||
kern_ret = task_info(task, TASK_THREAD_TIMES_INFO,
|
||||
(task_info_t)&thread_data, &count);
|
||||
(task_info_t)&thread_data, &count);
|
||||
if (kern_ret != KERN_SUCCESS)
|
||||
return false;
|
||||
|
||||
count = TASK_BASIC_INFO_64_COUNT;
|
||||
kern_ret = task_info(task, TASK_BASIC_INFO_64,
|
||||
(task_info_t)&task_data, &count);
|
||||
kern_ret = task_info(task, TASK_BASIC_INFO_64, (task_info_t)&task_data,
|
||||
&count);
|
||||
if (kern_ret != KERN_SUCCESS)
|
||||
return false;
|
||||
|
||||
add_time_value(&cur_time, &thread_data.user_time,
|
||||
&thread_data.system_time);
|
||||
&thread_data.system_time);
|
||||
add_time_value(&cur_time, &cur_time, &task_data.user_time);
|
||||
add_time_value(&cur_time, &cur_time, &task_data.system_time);
|
||||
|
||||
|
|
@ -238,7 +240,7 @@ os_cpu_usage_info_t *os_cpu_usage_info_start(void)
|
|||
|
||||
double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
|
||||
{
|
||||
int64_t sys_time, cpu_time;
|
||||
int64_t sys_time, cpu_time;
|
||||
int64_t sys_time_delta, cpu_time_delta;
|
||||
|
||||
if (!info || !get_time_info(&cpu_time, &sys_time))
|
||||
|
|
@ -254,7 +256,7 @@ double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
|
|||
info->last_cpu_time = cpu_time;
|
||||
|
||||
return (double)sys_time_delta * 100.0 / (double)cpu_time_delta /
|
||||
(double)info->core_count;
|
||||
(double)info->core_count;
|
||||
}
|
||||
|
||||
void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
|
||||
|
|
@ -272,7 +274,7 @@ os_performance_token_t *os_request_high_performance(const char *reason)
|
|||
return nil;
|
||||
|
||||
//taken from http://stackoverflow.com/a/20100906
|
||||
id activity = [pi beginActivityWithOptions:0x00FFFFFF
|
||||
id activity = [pi beginActivityWithOptions:0x00FFFFFF
|
||||
reason:@(reason)];
|
||||
|
||||
return CFBridgingRetain(activity);
|
||||
|
|
@ -302,11 +304,11 @@ os_inhibit_t *os_inhibit_sleep_create(const char *reason)
|
|||
{
|
||||
struct os_inhibit_info *info = bzalloc(sizeof(*info));
|
||||
if (!reason)
|
||||
info->reason = CFStringCreateWithCString(kCFAllocatorDefault,
|
||||
reason, kCFStringEncodingUTF8);
|
||||
info->reason = CFStringCreateWithCString(
|
||||
kCFAllocatorDefault, reason, kCFStringEncodingUTF8);
|
||||
else
|
||||
info->reason = CFStringCreateCopy(kCFAllocatorDefault,
|
||||
CFSTR(""));
|
||||
info->reason =
|
||||
CFStringCreateCopy(kCFAllocatorDefault, CFSTR(""));
|
||||
|
||||
return info;
|
||||
}
|
||||
|
|
@ -321,12 +323,11 @@ bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
|
|||
return false;
|
||||
|
||||
if (active) {
|
||||
IOPMAssertionDeclareUserActivity(info->reason,
|
||||
kIOPMUserActiveLocal, &info->user_id);
|
||||
IOPMAssertionDeclareUserActivity(
|
||||
info->reason, kIOPMUserActiveLocal, &info->user_id);
|
||||
success = IOPMAssertionCreateWithName(
|
||||
kIOPMAssertionTypeNoDisplaySleep,
|
||||
kIOPMAssertionLevelOn, info->reason,
|
||||
&info->sleep_id);
|
||||
kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn,
|
||||
info->reason, &info->sleep_id);
|
||||
|
||||
if (success != kIOReturnSuccess) {
|
||||
blog(LOG_WARNING, "Failed to disable sleep");
|
||||
|
|
@ -361,16 +362,16 @@ static void os_get_cores_internal(void)
|
|||
core_count_initialized = true;
|
||||
|
||||
size_t size;
|
||||
int ret;
|
||||
int ret;
|
||||
|
||||
size = sizeof(physical_cores);
|
||||
ret = sysctlbyname("machdep.cpu.core_count", &physical_cores,
|
||||
&size, NULL, 0);
|
||||
ret = sysctlbyname("machdep.cpu.core_count", &physical_cores, &size,
|
||||
NULL, 0);
|
||||
if (ret != 0)
|
||||
return;
|
||||
|
||||
ret = sysctlbyname("machdep.cpu.thread_count", &logical_cores,
|
||||
&size, NULL, 0);
|
||||
ret = sysctlbyname("machdep.cpu.thread_count", &logical_cores, &size,
|
||||
NULL, 0);
|
||||
}
|
||||
|
||||
int os_get_physical_cores(void)
|
||||
|
|
@ -390,8 +391,8 @@ int os_get_logical_cores(void)
|
|||
static inline bool os_get_sys_memory_usage_internal(vm_statistics_t vmstat)
|
||||
{
|
||||
mach_msg_type_number_t out_count = HOST_VM_INFO_COUNT;
|
||||
if (host_statistics(mach_host_self(), HOST_VM_INFO,
|
||||
(host_info_t)vmstat, &out_count) != KERN_SUCCESS)
|
||||
if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)vmstat,
|
||||
&out_count) != KERN_SUCCESS)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
|
@ -409,8 +410,8 @@ uint64_t os_get_sys_free_size(void)
|
|||
typedef task_basic_info_data_t mach_task_basic_info_data_t;
|
||||
#endif
|
||||
|
||||
static inline bool os_get_proc_memory_usage_internal(
|
||||
mach_task_basic_info_data_t *taskinfo)
|
||||
static inline bool
|
||||
os_get_proc_memory_usage_internal(mach_task_basic_info_data_t *taskinfo)
|
||||
{
|
||||
#ifdef MACH_TASK_BASIC_INFO
|
||||
const task_flavor_t flavor = MACH_TASK_BASIC_INFO;
|
||||
|
|
@ -419,8 +420,8 @@ static inline bool os_get_proc_memory_usage_internal(
|
|||
const task_flavor_t flavor = TASK_BASIC_INFO;
|
||||
mach_msg_type_number_t out_count = TASK_BASIC_INFO_COUNT;
|
||||
#endif
|
||||
if (task_info(mach_task_self(), flavor,
|
||||
(task_info_t)taskinfo, &out_count) != KERN_SUCCESS)
|
||||
if (task_info(mach_task_self(), flavor, (task_info_t)taskinfo,
|
||||
&out_count) != KERN_SUCCESS)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
|
@ -432,7 +433,7 @@ bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
|
|||
return false;
|
||||
|
||||
usage->resident_size = taskinfo.resident_size;
|
||||
usage->virtual_size = taskinfo.virtual_size;
|
||||
usage->virtual_size = taskinfo.virtual_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -461,8 +462,8 @@ char *cfstr_copy_cstr(CFStringRef cfstring, CFStringEncoding cfstring_encoding)
|
|||
return NULL;
|
||||
|
||||
// Try the quick way to obtain the buffer
|
||||
const char *tmp_buffer = CFStringGetCStringPtr(cfstring,
|
||||
cfstring_encoding);
|
||||
const char *tmp_buffer =
|
||||
CFStringGetCStringPtr(cfstring, cfstring_encoding);
|
||||
|
||||
if (tmp_buffer != NULL)
|
||||
return bstrdup(tmp_buffer);
|
||||
|
|
@ -486,8 +487,8 @@ char *cfstr_copy_cstr(CFStringRef cfstring, CFStringEncoding cfstring_encoding)
|
|||
}
|
||||
|
||||
// Copy CFString in requested encoding to buffer
|
||||
Boolean success =
|
||||
CFStringGetCString(cfstring, buffer, max_size, cfstring_encoding);
|
||||
Boolean success = CFStringGetCString(cfstring, buffer, max_size,
|
||||
cfstring_encoding);
|
||||
|
||||
if (!success) {
|
||||
bfree(buffer);
|
||||
|
|
@ -500,15 +501,15 @@ char *cfstr_copy_cstr(CFStringRef cfstring, CFStringEncoding cfstring_encoding)
|
|||
* Returns true on success or false on failure.
|
||||
* In case of failure, the dstr capacity but not size is changed.
|
||||
*/
|
||||
bool cfstr_copy_dstr(CFStringRef cfstring,
|
||||
CFStringEncoding cfstring_encoding, struct dstr *str)
|
||||
bool cfstr_copy_dstr(CFStringRef cfstring, CFStringEncoding cfstring_encoding,
|
||||
struct dstr *str)
|
||||
{
|
||||
if (!cfstring)
|
||||
return false;
|
||||
|
||||
// Try the quick way to obtain the buffer
|
||||
const char *tmp_buffer = CFStringGetCStringPtr(cfstring,
|
||||
cfstring_encoding);
|
||||
const char *tmp_buffer =
|
||||
CFStringGetCStringPtr(cfstring, cfstring_encoding);
|
||||
|
||||
if (tmp_buffer != NULL) {
|
||||
dstr_copy(str, tmp_buffer);
|
||||
|
|
@ -530,8 +531,8 @@ bool cfstr_copy_dstr(CFStringRef cfstring,
|
|||
dstr_ensure_capacity(str, max_size);
|
||||
|
||||
// Copy CFString in requested encoding to dstr buffer
|
||||
Boolean success = CFStringGetCString(
|
||||
cfstring, str->array, max_size, cfstring_encoding);
|
||||
Boolean success = CFStringGetCString(cfstring, str->array, max_size,
|
||||
cfstring_encoding);
|
||||
|
||||
if (success)
|
||||
dstr_resize(str, max_size);
|
||||
|
|
|
|||
|
|
@ -35,26 +35,30 @@ struct service_info {
|
|||
};
|
||||
|
||||
static const struct service_info services[] = {
|
||||
[FREEDESKTOP_SS] = {
|
||||
.name = "org.freedesktop.ScreenSaver",
|
||||
.path = "/ScreenSaver",
|
||||
.uninhibit = "UnInhibit",
|
||||
},
|
||||
[FREEDESKTOP_PM] = {
|
||||
.name = "org.freedesktop.PowerManagement.Inhibit",
|
||||
.path = "/org/freedesktop/PowerManagement",
|
||||
.uninhibit = "UnInhibit",
|
||||
},
|
||||
[MATE_SM] = {
|
||||
.name = "org.mate.SessionManager",
|
||||
.path = "/org/mate/SessionManager",
|
||||
.uninhibit = "Uninhibit",
|
||||
},
|
||||
[GNOME_SM] = {
|
||||
.name = "org.gnome.SessionManager",
|
||||
.path = "/org/gnome/SessionManager",
|
||||
.uninhibit = "Uninhibit",
|
||||
},
|
||||
[FREEDESKTOP_SS] =
|
||||
{
|
||||
.name = "org.freedesktop.ScreenSaver",
|
||||
.path = "/ScreenSaver",
|
||||
.uninhibit = "UnInhibit",
|
||||
},
|
||||
[FREEDESKTOP_PM] =
|
||||
{
|
||||
.name = "org.freedesktop.PowerManagement.Inhibit",
|
||||
.path = "/org/freedesktop/PowerManagement",
|
||||
.uninhibit = "UnInhibit",
|
||||
},
|
||||
[MATE_SM] =
|
||||
{
|
||||
.name = "org.mate.SessionManager",
|
||||
.path = "/org/mate/SessionManager",
|
||||
.uninhibit = "Uninhibit",
|
||||
},
|
||||
[GNOME_SM] =
|
||||
{
|
||||
.name = "org.gnome.SessionManager",
|
||||
.path = "/org/gnome/SessionManager",
|
||||
.uninhibit = "Uninhibit",
|
||||
},
|
||||
};
|
||||
|
||||
static const size_t num_services =
|
||||
|
|
@ -92,7 +96,7 @@ struct dbus_sleep_info *dbus_sleep_info_create(void)
|
|||
info->c = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
|
||||
if (!info->c) {
|
||||
blog(LOG_ERROR, "Could not create dbus connection: %s",
|
||||
err.message);
|
||||
err.message);
|
||||
bfree(info);
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -105,7 +109,7 @@ struct dbus_sleep_info *dbus_sleep_info_create(void)
|
|||
|
||||
if (dbus_bus_name_has_owner(info->c, service->name, NULL)) {
|
||||
blog(LOG_DEBUG, "Found dbus service: %s",
|
||||
service->name);
|
||||
service->name);
|
||||
info->service = service;
|
||||
info->type = (enum service_type)i;
|
||||
return info;
|
||||
|
|
@ -117,7 +121,7 @@ struct dbus_sleep_info *dbus_sleep_info_create(void)
|
|||
}
|
||||
|
||||
void dbus_inhibit_sleep(struct dbus_sleep_info *info, const char *reason,
|
||||
bool active)
|
||||
bool active)
|
||||
{
|
||||
DBusMessage *reply;
|
||||
const char *method;
|
||||
|
|
@ -132,8 +136,9 @@ void dbus_inhibit_sleep(struct dbus_sleep_info *info, const char *reason,
|
|||
|
||||
if (reply) {
|
||||
success = dbus_message_get_args(reply, NULL,
|
||||
DBUS_TYPE_UINT32, &info->id,
|
||||
DBUS_TYPE_INVALID);
|
||||
DBUS_TYPE_UINT32,
|
||||
&info->id,
|
||||
DBUS_TYPE_INVALID);
|
||||
if (!success)
|
||||
info->id = 0;
|
||||
dbus_message_unref(reply);
|
||||
|
|
@ -146,7 +151,8 @@ void dbus_inhibit_sleep(struct dbus_sleep_info *info, const char *reason,
|
|||
method = active ? "Inhibit" : info->service->uninhibit;
|
||||
|
||||
reply = dbus_message_new_method_call(info->service->name,
|
||||
info->service->path, info->service->name, method);
|
||||
info->service->path,
|
||||
info->service->name, method);
|
||||
if (reply == NULL) {
|
||||
blog(LOG_ERROR, "dbus_message_new_method_call failed");
|
||||
return;
|
||||
|
|
@ -162,31 +168,28 @@ void dbus_inhibit_sleep(struct dbus_sleep_info *info, const char *reason,
|
|||
switch (info->type) {
|
||||
case MATE_SM:
|
||||
case GNOME_SM:
|
||||
success = dbus_message_append_args(reply,
|
||||
DBUS_TYPE_STRING, &program,
|
||||
DBUS_TYPE_UINT32, &xid,
|
||||
DBUS_TYPE_STRING, &reason,
|
||||
DBUS_TYPE_UINT32, &flags,
|
||||
DBUS_TYPE_INVALID);
|
||||
success = dbus_message_append_args(
|
||||
reply, DBUS_TYPE_STRING, &program,
|
||||
DBUS_TYPE_UINT32, &xid, DBUS_TYPE_STRING,
|
||||
&reason, DBUS_TYPE_UINT32, &flags,
|
||||
DBUS_TYPE_INVALID);
|
||||
break;
|
||||
default:
|
||||
success = dbus_message_append_args(reply,
|
||||
DBUS_TYPE_STRING, &program,
|
||||
DBUS_TYPE_STRING, &reason,
|
||||
DBUS_TYPE_INVALID);
|
||||
success = dbus_message_append_args(
|
||||
reply, DBUS_TYPE_STRING, &program,
|
||||
DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID);
|
||||
}
|
||||
|
||||
if (success) {
|
||||
success = dbus_connection_send_with_reply(info->c,
|
||||
reply, &info->pending, -1);
|
||||
success = dbus_connection_send_with_reply(
|
||||
info->c, reply, &info->pending, -1);
|
||||
if (!success)
|
||||
info->pending = NULL;
|
||||
}
|
||||
} else {
|
||||
assert(info->id != 0);
|
||||
success = dbus_message_append_args(reply,
|
||||
DBUS_TYPE_UINT32, &info->id,
|
||||
DBUS_TYPE_INVALID);
|
||||
success = dbus_message_append_args(
|
||||
reply, DBUS_TYPE_UINT32, &info->id, DBUS_TYPE_INVALID);
|
||||
if (success)
|
||||
success = dbus_connection_send(info->c, reply, NULL);
|
||||
if (!success)
|
||||
|
|
|
|||
|
|
@ -73,8 +73,8 @@ void *os_dlopen(const char *path)
|
|||
void *res = dlopen(dylib_name.array, RTLD_LAZY);
|
||||
#endif
|
||||
if (!res)
|
||||
blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
|
||||
path, dylib_name.array, dlerror());
|
||||
blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n", path,
|
||||
dylib_name.array, dlerror());
|
||||
|
||||
dstr_free(&dylib_name);
|
||||
return res;
|
||||
|
|
@ -101,20 +101,20 @@ struct os_cpu_usage_info {
|
|||
os_cpu_usage_info_t *os_cpu_usage_info_start(void)
|
||||
{
|
||||
struct os_cpu_usage_info *info = bmalloc(sizeof(*info));
|
||||
struct tms time_sample;
|
||||
struct tms time_sample;
|
||||
|
||||
info->last_cpu_time = times(&time_sample);
|
||||
info->last_sys_time = time_sample.tms_stime;
|
||||
info->last_cpu_time = times(&time_sample);
|
||||
info->last_sys_time = time_sample.tms_stime;
|
||||
info->last_user_time = time_sample.tms_utime;
|
||||
info->core_count = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
info->core_count = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
return info;
|
||||
}
|
||||
|
||||
double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
|
||||
{
|
||||
struct tms time_sample;
|
||||
clock_t cur_cpu_time;
|
||||
double percent;
|
||||
clock_t cur_cpu_time;
|
||||
double percent;
|
||||
|
||||
if (!info)
|
||||
return 0.0;
|
||||
|
|
@ -126,12 +126,12 @@ double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
|
|||
return 0.0;
|
||||
|
||||
percent = (double)(time_sample.tms_stime - info->last_sys_time +
|
||||
(time_sample.tms_utime - info->last_user_time));
|
||||
(time_sample.tms_utime - info->last_user_time));
|
||||
percent /= (double)(cur_cpu_time - info->last_cpu_time);
|
||||
percent /= (double)info->core_count;
|
||||
|
||||
info->last_cpu_time = cur_cpu_time;
|
||||
info->last_sys_time = time_sample.tms_stime;
|
||||
info->last_cpu_time = cur_cpu_time;
|
||||
info->last_sys_time = time_sample.tms_stime;
|
||||
info->last_user_time = time_sample.tms_utime;
|
||||
|
||||
return percent * 100.0;
|
||||
|
|
@ -156,8 +156,8 @@ bool os_sleepto_ns(uint64_t time_target)
|
|||
struct timespec req, remain;
|
||||
memset(&req, 0, sizeof(req));
|
||||
memset(&remain, 0, sizeof(remain));
|
||||
req.tv_sec = time_target/1000000000;
|
||||
req.tv_nsec = time_target%1000000000;
|
||||
req.tv_sec = time_target / 1000000000;
|
||||
req.tv_nsec = time_target % 1000000000;
|
||||
|
||||
while (nanosleep(&req, &remain)) {
|
||||
req = remain;
|
||||
|
|
@ -169,7 +169,7 @@ bool os_sleepto_ns(uint64_t time_target)
|
|||
|
||||
void os_sleep_ms(uint32_t duration)
|
||||
{
|
||||
usleep(duration*1000);
|
||||
usleep(duration * 1000);
|
||||
}
|
||||
|
||||
#if !defined(__APPLE__)
|
||||
|
|
@ -178,7 +178,7 @@ uint64_t os_gettime_ns(void)
|
|||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return ((uint64_t) ts.tv_sec * 1000000000ULL + (uint64_t) ts.tv_nsec);
|
||||
return ((uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec);
|
||||
}
|
||||
|
||||
/* should return $HOME/.[name], or when using XDG,
|
||||
|
|
@ -263,7 +263,8 @@ int os_get_program_data_path(char *dst, size_t size, const char *name)
|
|||
|
||||
char *os_get_program_data_path_ptr(const char *name)
|
||||
{
|
||||
size_t len = snprintf(NULL, 0, "/usr/local/share/%s", !!name ? name : "");
|
||||
size_t len =
|
||||
snprintf(NULL, 0, "/usr/local/share/%s", !!name ? name : "");
|
||||
char *str = bmalloc(len + 1);
|
||||
snprintf(str, len + 1, "/usr/local/share/%s", !!name ? name : "");
|
||||
str[len] = 0;
|
||||
|
|
@ -332,23 +333,23 @@ char *os_get_abs_path_ptr(const char *path)
|
|||
}
|
||||
|
||||
struct os_dir {
|
||||
const char *path;
|
||||
DIR *dir;
|
||||
struct dirent *cur_dirent;
|
||||
const char *path;
|
||||
DIR *dir;
|
||||
struct dirent *cur_dirent;
|
||||
struct os_dirent out;
|
||||
};
|
||||
|
||||
os_dir_t *os_opendir(const char *path)
|
||||
{
|
||||
struct os_dir *dir;
|
||||
DIR *dir_val;
|
||||
DIR *dir_val;
|
||||
|
||||
dir_val = opendir(path);
|
||||
if (!dir_val)
|
||||
return NULL;
|
||||
|
||||
dir = bzalloc(sizeof(struct os_dir));
|
||||
dir->dir = dir_val;
|
||||
dir->dir = dir_val;
|
||||
dir->path = path;
|
||||
return dir;
|
||||
}
|
||||
|
|
@ -367,7 +368,8 @@ struct os_dirent *os_readdir(os_dir_t *dir)
|
|||
{
|
||||
struct dstr file_path = {0};
|
||||
|
||||
if (!dir) return NULL;
|
||||
if (!dir)
|
||||
return NULL;
|
||||
|
||||
dir->cur_dirent = readdir(dir->dir);
|
||||
if (!dir->cur_dirent)
|
||||
|
|
@ -442,7 +444,7 @@ int os_glob(const char *pattern, int flags, os_glob_t **pglob)
|
|||
void os_globfree(os_glob_t *pglob)
|
||||
{
|
||||
if (pglob) {
|
||||
struct posix_glob_info *pgi = (struct posix_glob_info*)pglob;
|
||||
struct posix_glob_info *pgi = (struct posix_glob_info *)pglob;
|
||||
globfree(&pgi->gl);
|
||||
|
||||
bfree(pgi->base.gl_pathv);
|
||||
|
|
@ -544,7 +546,7 @@ struct dbus_sleep_info;
|
|||
|
||||
extern struct dbus_sleep_info *dbus_sleep_info_create(void);
|
||||
extern void dbus_inhibit_sleep(struct dbus_sleep_info *dbus, const char *sleep,
|
||||
bool active);
|
||||
bool active);
|
||||
extern void dbus_sleep_info_destroy(struct dbus_sleep_info *dbus);
|
||||
#endif
|
||||
|
||||
|
|
@ -575,8 +577,8 @@ os_inhibit_t *os_inhibit_sleep_create(const char *reason)
|
|||
posix_spawnattr_setsigmask(&info->attr, &set);
|
||||
sigaddset(&set, SIGPIPE);
|
||||
posix_spawnattr_setsigdefault(&info->attr, &set);
|
||||
posix_spawnattr_setflags(&info->attr,
|
||||
POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK);
|
||||
posix_spawnattr_setflags(&info->attr, POSIX_SPAWN_SETSIGDEF |
|
||||
POSIX_SPAWN_SETSIGMASK);
|
||||
|
||||
info->reason = bstrdup(reason);
|
||||
return info;
|
||||
|
|
@ -586,14 +588,15 @@ extern char **environ;
|
|||
|
||||
static void reset_screensaver(os_inhibit_t *info)
|
||||
{
|
||||
char *argv[3] = {(char*)"xdg-screensaver", (char*)"reset", NULL};
|
||||
char *argv[3] = {(char *)"xdg-screensaver", (char *)"reset", NULL};
|
||||
pid_t pid;
|
||||
|
||||
int err = posix_spawnp(&pid, "xdg-screensaver", NULL, &info->attr,
|
||||
argv, environ);
|
||||
int err = posix_spawnp(&pid, "xdg-screensaver", NULL, &info->attr, argv,
|
||||
environ);
|
||||
if (err == 0) {
|
||||
int status;
|
||||
while (waitpid(pid, &status, 0) == -1);
|
||||
while (waitpid(pid, &status, 0) == -1)
|
||||
;
|
||||
} else {
|
||||
blog(LOG_WARNING, "Failed to create xdg-screensaver: %d", err);
|
||||
}
|
||||
|
|
@ -628,10 +631,10 @@ bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
|
|||
|
||||
if (active) {
|
||||
ret = pthread_create(&info->screensaver_thread, NULL,
|
||||
&screensaver_thread, info);
|
||||
&screensaver_thread, info);
|
||||
if (ret < 0) {
|
||||
blog(LOG_ERROR, "Failed to create screensaver "
|
||||
"inhibitor thread");
|
||||
"inhibitor thread");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
|
|
@ -714,8 +717,8 @@ static void os_get_cores_internal(void)
|
|||
continue;
|
||||
|
||||
if (dstr_is_empty(&proc_phys_ids) ||
|
||||
(!dstr_is_empty(&proc_phys_ids) &&
|
||||
!dstr_find(&proc_phys_ids, proc_phys_id.array))) {
|
||||
(!dstr_is_empty(&proc_phys_ids) &&
|
||||
!dstr_find(&proc_phys_ids, proc_phys_id.array))) {
|
||||
dstr_cat_dstr(&proc_phys_ids, &proc_phys_id);
|
||||
dstr_cat(&proc_phys_ids, " ");
|
||||
core_count += atoi(start);
|
||||
|
|
@ -774,7 +777,7 @@ static void os_get_cores_internal(void)
|
|||
len = strcspn(core_count, " ");
|
||||
dstr_ncopy(&proc_cores, core_count, len);
|
||||
|
||||
FreeBSD_cores_cleanup:
|
||||
FreeBSD_cores_cleanup:
|
||||
if (!dstr_is_empty(&proc_packages))
|
||||
packages = atoi(proc_packages.array);
|
||||
if (!dstr_is_empty(&proc_cores))
|
||||
|
|
@ -814,8 +817,8 @@ uint64_t os_get_sys_free_size(void)
|
|||
{
|
||||
uint64_t mem_free = 0;
|
||||
size_t length = sizeof(mem_free);
|
||||
if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length,
|
||||
NULL, 0) < 0)
|
||||
if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length, NULL,
|
||||
0) < 0)
|
||||
return 0;
|
||||
return mem_free;
|
||||
}
|
||||
|
|
@ -824,8 +827,8 @@ static inline bool os_get_proc_memory_usage_internal(struct kinfo_proc *kinfo)
|
|||
{
|
||||
int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
|
||||
size_t length = sizeof(*kinfo);
|
||||
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), kinfo, &length,
|
||||
NULL, 0) < 0)
|
||||
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), kinfo, &length, NULL, 0) <
|
||||
0)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
|
@ -837,8 +840,8 @@ bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
|
|||
return false;
|
||||
|
||||
usage->resident_size =
|
||||
(uint64_t)kinfo.ki_rssize * sysconf(_SC_PAGESIZE);
|
||||
usage->virtual_size = (uint64_t)kinfo.ki_size;
|
||||
(uint64_t)kinfo.ki_rssize * sysconf(_SC_PAGESIZE);
|
||||
usage->virtual_size = (uint64_t)kinfo.ki_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -858,10 +861,12 @@ uint64_t os_get_proc_virtual_size(void)
|
|||
return (uint64_t)kinfo.ki_size;
|
||||
}
|
||||
#else
|
||||
uint64_t os_get_sys_free_size(void) {return 0;}
|
||||
|
||||
typedef struct
|
||||
uint64_t os_get_sys_free_size(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
unsigned long virtual_size;
|
||||
unsigned long resident_size;
|
||||
unsigned long share_pages;
|
||||
|
|
@ -879,14 +884,9 @@ static inline bool os_get_proc_memory_usage_internal(statm_t *statm)
|
|||
if (!f)
|
||||
return false;
|
||||
|
||||
if (fscanf(f, "%lu %lu %lu %lu %lu %lu %lu",
|
||||
&statm->virtual_size,
|
||||
&statm->resident_size,
|
||||
&statm->share_pages,
|
||||
&statm->text,
|
||||
&statm->library,
|
||||
&statm->data,
|
||||
&statm->dirty_pages) != 7) {
|
||||
if (fscanf(f, "%lu %lu %lu %lu %lu %lu %lu", &statm->virtual_size,
|
||||
&statm->resident_size, &statm->share_pages, &statm->text,
|
||||
&statm->library, &statm->data, &statm->dirty_pages) != 7) {
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
|
|
@ -902,7 +902,7 @@ bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
|
|||
return false;
|
||||
|
||||
usage->resident_size = statm.resident_size;
|
||||
usage->virtual_size = statm.virtual_size;
|
||||
usage->virtual_size = statm.virtual_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -101,20 +101,19 @@ void *os_dlopen(const char *path)
|
|||
char *message = NULL;
|
||||
|
||||
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS |
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
||||
NULL, error,
|
||||
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
|
||||
(LPSTR)&message, 0, NULL);
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS |
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
||||
NULL, error,
|
||||
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
|
||||
(LPSTR)&message, 0, NULL);
|
||||
|
||||
blog(LOG_INFO, "LoadLibrary failed for '%s': %s (%lu)",
|
||||
path, message, error);
|
||||
blog(LOG_INFO, "LoadLibrary failed for '%s': %s (%lu)", path,
|
||||
message, error);
|
||||
|
||||
if (message)
|
||||
LocalFree(message);
|
||||
}
|
||||
|
||||
|
||||
return h_library;
|
||||
}
|
||||
|
||||
|
|
@ -122,7 +121,7 @@ void *os_dlsym(void *module, const char *func)
|
|||
{
|
||||
void *handle;
|
||||
|
||||
handle = (void*)GetProcAddress(module, func);
|
||||
handle = (void *)GetProcAddress(module, func);
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
|
@ -133,7 +132,7 @@ void os_dlclose(void *module)
|
|||
}
|
||||
|
||||
union time_data {
|
||||
FILETIME ft;
|
||||
FILETIME ft;
|
||||
unsigned long long val;
|
||||
};
|
||||
|
||||
|
|
@ -145,8 +144,8 @@ struct os_cpu_usage_info {
|
|||
os_cpu_usage_info_t *os_cpu_usage_info_start(void)
|
||||
{
|
||||
struct os_cpu_usage_info *info = bzalloc(sizeof(*info));
|
||||
SYSTEM_INFO si;
|
||||
FILETIME dummy;
|
||||
SYSTEM_INFO si;
|
||||
FILETIME dummy;
|
||||
|
||||
GetSystemInfo(&si);
|
||||
GetSystemTimeAsFileTime(&info->last_time.ft);
|
||||
|
|
@ -160,23 +159,23 @@ os_cpu_usage_info_t *os_cpu_usage_info_start(void)
|
|||
double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
|
||||
{
|
||||
union time_data cur_time, cur_sys_time, cur_user_time;
|
||||
FILETIME dummy;
|
||||
double percent;
|
||||
FILETIME dummy;
|
||||
double percent;
|
||||
|
||||
if (!info)
|
||||
return 0.0;
|
||||
|
||||
GetSystemTimeAsFileTime(&cur_time.ft);
|
||||
GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
|
||||
&cur_sys_time.ft, &cur_user_time.ft);
|
||||
GetProcessTimes(GetCurrentProcess(), &dummy, &dummy, &cur_sys_time.ft,
|
||||
&cur_user_time.ft);
|
||||
|
||||
percent = (double)(cur_sys_time.val - info->last_sys_time.val +
|
||||
(cur_user_time.val - info->last_user_time.val));
|
||||
(cur_user_time.val - info->last_user_time.val));
|
||||
percent /= (double)(cur_time.val - info->last_time.val);
|
||||
percent /= (double)info->core_count;
|
||||
|
||||
info->last_time.val = cur_time.val;
|
||||
info->last_sys_time.val = cur_sys_time.val;
|
||||
info->last_time.val = cur_time.val;
|
||||
info->last_sys_time.val = cur_sys_time.val;
|
||||
info->last_user_time.val = cur_user_time.val;
|
||||
|
||||
return percent * 100.0;
|
||||
|
|
@ -196,9 +195,9 @@ bool os_sleepto_ns(uint64_t time_target)
|
|||
if (t >= time_target)
|
||||
return false;
|
||||
|
||||
milliseconds = (uint32_t)((time_target - t)/1000000);
|
||||
milliseconds = (uint32_t)((time_target - t) / 1000000);
|
||||
if (milliseconds > 1)
|
||||
Sleep(milliseconds-1);
|
||||
Sleep(milliseconds - 1);
|
||||
|
||||
for (;;) {
|
||||
t = os_gettime_ns();
|
||||
|
|
@ -237,12 +236,11 @@ uint64_t os_gettime_ns(void)
|
|||
|
||||
/* returns [folder]\[name] on windows */
|
||||
static int os_get_path_internal(char *dst, size_t size, const char *name,
|
||||
int folder)
|
||||
int folder)
|
||||
{
|
||||
wchar_t path_utf16[MAX_PATH];
|
||||
|
||||
SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT,
|
||||
path_utf16);
|
||||
SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, path_utf16);
|
||||
|
||||
if (os_wcs_to_utf8(path_utf16, 0, dst, size) != 0) {
|
||||
if (!name || !*name) {
|
||||
|
|
@ -265,8 +263,7 @@ static char *os_get_path_ptr_internal(const char *name, int folder)
|
|||
wchar_t path_utf16[MAX_PATH];
|
||||
struct dstr path;
|
||||
|
||||
SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT,
|
||||
path_utf16);
|
||||
SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, path_utf16);
|
||||
|
||||
os_wcs_to_utf8_ptr(path_utf16, 0, &ptr);
|
||||
dstr_init_move_array(&path, ptr);
|
||||
|
|
@ -369,19 +366,19 @@ char *os_get_abs_path_ptr(const char *path)
|
|||
}
|
||||
|
||||
struct os_dir {
|
||||
HANDLE handle;
|
||||
WIN32_FIND_DATA wfd;
|
||||
bool first;
|
||||
HANDLE handle;
|
||||
WIN32_FIND_DATA wfd;
|
||||
bool first;
|
||||
struct os_dirent out;
|
||||
};
|
||||
|
||||
os_dir_t *os_opendir(const char *path)
|
||||
{
|
||||
struct dstr path_str = {0};
|
||||
struct os_dir *dir = NULL;
|
||||
struct dstr path_str = {0};
|
||||
struct os_dir *dir = NULL;
|
||||
WIN32_FIND_DATA wfd;
|
||||
HANDLE handle;
|
||||
wchar_t *w_path;
|
||||
HANDLE handle;
|
||||
wchar_t *w_path;
|
||||
|
||||
dstr_copy(&path_str, path);
|
||||
dstr_cat(&path_str, "/*.*");
|
||||
|
|
@ -389,10 +386,10 @@ os_dir_t *os_opendir(const char *path)
|
|||
if (os_utf8_to_wcs_ptr(path_str.array, path_str.len, &w_path) > 0) {
|
||||
handle = FindFirstFileW(w_path, &wfd);
|
||||
if (handle != INVALID_HANDLE_VALUE) {
|
||||
dir = bzalloc(sizeof(struct os_dir));
|
||||
dir = bzalloc(sizeof(struct os_dir));
|
||||
dir->handle = handle;
|
||||
dir->first = true;
|
||||
dir->wfd = wfd;
|
||||
dir->first = true;
|
||||
dir->wfd = wfd;
|
||||
}
|
||||
|
||||
bfree(w_path);
|
||||
|
|
@ -421,7 +418,7 @@ struct os_dirent *os_readdir(os_dir_t *dir)
|
|||
}
|
||||
|
||||
os_wcs_to_utf8(dir->wfd.cFileName, 0, dir->out.d_name,
|
||||
sizeof(dir->out.d_name));
|
||||
sizeof(dir->out.d_name));
|
||||
|
||||
dir->out.directory = is_dir(&dir->wfd);
|
||||
|
||||
|
|
@ -438,15 +435,15 @@ void os_closedir(os_dir_t *dir)
|
|||
|
||||
int64_t os_get_free_space(const char *path)
|
||||
{
|
||||
ULARGE_INTEGER remainingSpace;
|
||||
char abs_path[512];
|
||||
wchar_t w_abs_path[512];
|
||||
ULARGE_INTEGER remainingSpace;
|
||||
char abs_path[512];
|
||||
wchar_t w_abs_path[512];
|
||||
|
||||
if (os_get_abs_path(path, abs_path, 512) > 0) {
|
||||
if (os_utf8_to_wcs(abs_path, 0, w_abs_path, 512) > 0) {
|
||||
BOOL success = GetDiskFreeSpaceExW(w_abs_path,
|
||||
(PULARGE_INTEGER)&remainingSpace,
|
||||
NULL, NULL);
|
||||
BOOL success = GetDiskFreeSpaceExW(
|
||||
w_abs_path, (PULARGE_INTEGER)&remainingSpace,
|
||||
NULL, NULL);
|
||||
if (success)
|
||||
return (int64_t)remainingSpace.QuadPart;
|
||||
}
|
||||
|
|
@ -456,7 +453,7 @@ int64_t os_get_free_space(const char *path)
|
|||
}
|
||||
|
||||
static void make_globent(struct os_globent *ent, WIN32_FIND_DATA *wfd,
|
||||
const char *pattern)
|
||||
const char *pattern)
|
||||
{
|
||||
struct dstr name = {0};
|
||||
struct dstr path = {0};
|
||||
|
|
@ -480,10 +477,10 @@ static void make_globent(struct os_globent *ent, WIN32_FIND_DATA *wfd,
|
|||
int os_glob(const char *pattern, int flags, os_glob_t **pglob)
|
||||
{
|
||||
DARRAY(struct os_globent) files;
|
||||
HANDLE handle;
|
||||
WIN32_FIND_DATA wfd;
|
||||
int ret = -1;
|
||||
wchar_t *w_path;
|
||||
HANDLE handle;
|
||||
WIN32_FIND_DATA wfd;
|
||||
int ret = -1;
|
||||
wchar_t *w_path;
|
||||
|
||||
da_init(files);
|
||||
|
||||
|
|
@ -567,8 +564,8 @@ int os_mkdir(const char *path)
|
|||
bfree(path_utf16);
|
||||
|
||||
if (!success)
|
||||
return (GetLastError() == ERROR_ALREADY_EXISTS) ?
|
||||
MKDIR_EXISTS : MKDIR_ERROR;
|
||||
return (GetLastError() == ERROR_ALREADY_EXISTS) ? MKDIR_EXISTS
|
||||
: MKDIR_ERROR;
|
||||
|
||||
return MKDIR_SUCCESS;
|
||||
}
|
||||
|
|
@ -587,7 +584,9 @@ int os_rename(const char *old_path, const char *new_path)
|
|||
}
|
||||
|
||||
code = MoveFileExW(old_path_utf16, new_path_utf16,
|
||||
MOVEFILE_REPLACE_EXISTING) ? 0 : -1;
|
||||
MOVEFILE_REPLACE_EXISTING)
|
||||
? 0
|
||||
: -1;
|
||||
|
||||
error:
|
||||
bfree(old_path_utf16);
|
||||
|
|
@ -615,7 +614,8 @@ int os_safe_replace(const char *target, const char *from, const char *backup)
|
|||
code = 0;
|
||||
} else if (GetLastError() == ERROR_FILE_NOT_FOUND) {
|
||||
code = MoveFileExW(wfrom, wtarget, MOVEFILE_REPLACE_EXISTING)
|
||||
? 0 : -1;
|
||||
? 0
|
||||
: -1;
|
||||
}
|
||||
|
||||
fail:
|
||||
|
|
@ -726,19 +726,12 @@ int os_chdir(const char *path)
|
|||
return ret;
|
||||
}
|
||||
|
||||
typedef DWORD (WINAPI *get_file_version_info_size_w_t)(
|
||||
LPCWSTR module,
|
||||
LPDWORD unused);
|
||||
typedef BOOL (WINAPI *get_file_version_info_w_t)(
|
||||
LPCWSTR module,
|
||||
DWORD unused,
|
||||
DWORD len,
|
||||
LPVOID data);
|
||||
typedef BOOL (WINAPI *ver_query_value_w_t)(
|
||||
LPVOID data,
|
||||
LPCWSTR subblock,
|
||||
LPVOID *buf,
|
||||
PUINT sizeout);
|
||||
typedef DWORD(WINAPI *get_file_version_info_size_w_t)(LPCWSTR module,
|
||||
LPDWORD unused);
|
||||
typedef BOOL(WINAPI *get_file_version_info_w_t)(LPCWSTR module, DWORD unused,
|
||||
DWORD len, LPVOID data);
|
||||
typedef BOOL(WINAPI *ver_query_value_w_t)(LPVOID data, LPCWSTR subblock,
|
||||
LPVOID *buf, PUINT sizeout);
|
||||
|
||||
static get_file_version_info_size_w_t get_file_version_info_size = NULL;
|
||||
static get_file_version_info_w_t get_file_version_info = NULL;
|
||||
|
|
@ -761,15 +754,15 @@ static bool initialize_version_functions(void)
|
|||
}
|
||||
}
|
||||
|
||||
get_file_version_info_size = (get_file_version_info_size_w_t)
|
||||
GetProcAddress(ver, "GetFileVersionInfoSizeW");
|
||||
get_file_version_info = (get_file_version_info_w_t)
|
||||
GetProcAddress(ver, "GetFileVersionInfoW");
|
||||
ver_query_value = (ver_query_value_w_t)
|
||||
GetProcAddress(ver, "VerQueryValueW");
|
||||
get_file_version_info_size =
|
||||
(get_file_version_info_size_w_t)GetProcAddress(
|
||||
ver, "GetFileVersionInfoSizeW");
|
||||
get_file_version_info = (get_file_version_info_w_t)GetProcAddress(
|
||||
ver, "GetFileVersionInfoW");
|
||||
ver_query_value =
|
||||
(ver_query_value_w_t)GetProcAddress(ver, "VerQueryValueW");
|
||||
|
||||
if (!get_file_version_info_size ||
|
||||
!get_file_version_info ||
|
||||
if (!get_file_version_info_size || !get_file_version_info ||
|
||||
!ver_query_value) {
|
||||
blog(LOG_ERROR, "Failed to load windows version "
|
||||
"functions");
|
||||
|
|
@ -809,9 +802,10 @@ bool get_dll_ver(const wchar_t *lib, struct win_version_info *ver_info)
|
|||
return false;
|
||||
}
|
||||
|
||||
success = ver_query_value(data, L"\\", (LPVOID*)&info, &len);
|
||||
success = ver_query_value(data, L"\\", (LPVOID *)&info, &len);
|
||||
if (!success || !info || !len) {
|
||||
blog(LOG_ERROR, "Failed to get %s version info value", utf8_lib);
|
||||
blog(LOG_ERROR, "Failed to get %s version info value",
|
||||
utf8_lib);
|
||||
bfree(data);
|
||||
return false;
|
||||
}
|
||||
|
|
@ -836,7 +830,7 @@ bool is_64_bit_windows(void)
|
|||
}
|
||||
|
||||
void get_reg_dword(HKEY hkey, LPCWSTR sub_key, LPCWSTR value_name,
|
||||
struct reg_dword *info)
|
||||
struct reg_dword *info)
|
||||
{
|
||||
struct reg_dword reg = {0};
|
||||
HKEY key;
|
||||
|
|
@ -854,7 +848,7 @@ void get_reg_dword(HKEY hkey, LPCWSTR sub_key, LPCWSTR value_name,
|
|||
reg.size = sizeof(reg.return_value);
|
||||
|
||||
reg.status = RegQueryValueExW(key, value_name, NULL, NULL,
|
||||
(LPBYTE)®.return_value, ®.size);
|
||||
(LPBYTE)®.return_value, ®.size);
|
||||
|
||||
RegCloseKey(key);
|
||||
|
||||
|
|
@ -876,22 +870,24 @@ void get_win_ver(struct win_version_info *info)
|
|||
got_version = true;
|
||||
|
||||
if (ver.major == 10) {
|
||||
HKEY key;
|
||||
DWORD size, win10_revision;
|
||||
HKEY key;
|
||||
DWORD size, win10_revision;
|
||||
LSTATUS status;
|
||||
|
||||
status = RegOpenKeyW(HKEY_LOCAL_MACHINE,
|
||||
WINVER_REG_KEY, &key);
|
||||
status = RegOpenKeyW(HKEY_LOCAL_MACHINE, WINVER_REG_KEY,
|
||||
&key);
|
||||
if (status != ERROR_SUCCESS)
|
||||
return;
|
||||
|
||||
size = sizeof(win10_revision);
|
||||
|
||||
status = RegQueryValueExW(key, L"UBR", NULL, NULL,
|
||||
(LPBYTE)&win10_revision, &size);
|
||||
(LPBYTE)&win10_revision,
|
||||
&size);
|
||||
if (status == ERROR_SUCCESS)
|
||||
ver.revis = (int)win10_revision > ver.revis ?
|
||||
(int)win10_revision : ver.revis;
|
||||
ver.revis = (int)win10_revision > ver.revis
|
||||
? (int)win10_revision
|
||||
: ver.revis;
|
||||
|
||||
RegCloseKey(key);
|
||||
}
|
||||
|
|
@ -923,11 +919,9 @@ bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
|
|||
return false;
|
||||
|
||||
if (active) {
|
||||
SetThreadExecutionState(
|
||||
ES_CONTINUOUS |
|
||||
ES_SYSTEM_REQUIRED |
|
||||
ES_AWAYMODE_REQUIRED |
|
||||
ES_DISPLAY_REQUIRED);
|
||||
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED |
|
||||
ES_AWAYMODE_REQUIRED |
|
||||
ES_DISPLAY_REQUIRED);
|
||||
} else {
|
||||
SetThreadExecutionState(ES_CONTINUOUS);
|
||||
}
|
||||
|
|
@ -951,13 +945,13 @@ void os_breakpoint(void)
|
|||
|
||||
DWORD num_logical_cores(ULONG_PTR mask)
|
||||
{
|
||||
DWORD left_shift = sizeof(ULONG_PTR) * 8 - 1;
|
||||
DWORD bit_set_count = 0;
|
||||
ULONG_PTR bit_test = (ULONG_PTR)1 << left_shift;
|
||||
DWORD left_shift = sizeof(ULONG_PTR) * 8 - 1;
|
||||
DWORD bit_set_count = 0;
|
||||
ULONG_PTR bit_test = (ULONG_PTR)1 << left_shift;
|
||||
|
||||
for (DWORD i = 0; i <= left_shift; ++i) {
|
||||
bit_set_count += ((mask & bit_test) ? 1 : 0);
|
||||
bit_test /= 2;
|
||||
bit_test /= 2;
|
||||
}
|
||||
|
||||
return bit_set_count;
|
||||
|
|
@ -1031,7 +1025,8 @@ uint64_t os_get_sys_free_size(void)
|
|||
return msex.ullAvailPhys;
|
||||
}
|
||||
|
||||
static inline bool os_get_proc_memory_usage_internal(PROCESS_MEMORY_COUNTERS *pmc)
|
||||
static inline bool
|
||||
os_get_proc_memory_usage_internal(PROCESS_MEMORY_COUNTERS *pmc)
|
||||
{
|
||||
if (!GetProcessMemoryInfo(GetCurrentProcess(), pmc, sizeof(*pmc)))
|
||||
return false;
|
||||
|
|
@ -1045,7 +1040,7 @@ bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
|
|||
return false;
|
||||
|
||||
usage->resident_size = pmc.WorkingSetSize;
|
||||
usage->virtual_size = pmc.PagefileUsage;
|
||||
usage->virtual_size = pmc.PagefileUsage;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ size_t os_fread_mbs(FILE *file, char **pstr)
|
|||
*pstr = NULL;
|
||||
|
||||
if (size > 0) {
|
||||
char *mbstr = bmalloc(size+1);
|
||||
char *mbstr = bmalloc(size + 1);
|
||||
|
||||
fseek(file, 0, SEEK_SET);
|
||||
size = fread(mbstr, 1, size, file);
|
||||
|
|
@ -192,7 +192,7 @@ size_t os_fread_utf8(FILE *file, char **pstr)
|
|||
if (size == 0)
|
||||
return 0;
|
||||
|
||||
utf8str = bmalloc(size+1);
|
||||
utf8str = bmalloc(size + 1);
|
||||
fseek(file, offset, SEEK_SET);
|
||||
|
||||
size = fread(utf8str, 1, size, file);
|
||||
|
|
@ -256,7 +256,7 @@ bool os_quick_write_mbs_file(const char *path, const char *str, size_t len)
|
|||
}
|
||||
|
||||
bool os_quick_write_utf8_file(const char *path, const char *str, size_t len,
|
||||
bool marker)
|
||||
bool marker)
|
||||
{
|
||||
FILE *f = os_fopen(path, "wb");
|
||||
if (!f)
|
||||
|
|
@ -282,8 +282,8 @@ bool os_quick_write_utf8_file(const char *path, const char *str, size_t len,
|
|||
}
|
||||
|
||||
bool os_quick_write_utf8_file_safe(const char *path, const char *str,
|
||||
size_t len, bool marker, const char *temp_ext,
|
||||
const char *backup_ext)
|
||||
size_t len, bool marker,
|
||||
const char *temp_ext, const char *backup_ext)
|
||||
{
|
||||
struct dstr backup_path = {0};
|
||||
struct dstr temp_path = {0};
|
||||
|
|
@ -291,7 +291,7 @@ bool os_quick_write_utf8_file_safe(const char *path, const char *str,
|
|||
|
||||
if (!temp_ext || !*temp_ext) {
|
||||
blog(LOG_ERROR, "os_quick_write_utf8_file_safe: invalid "
|
||||
"temporary extension specified");
|
||||
"temporary extension specified");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -301,8 +301,10 @@ bool os_quick_write_utf8_file_safe(const char *path, const char *str,
|
|||
dstr_cat(&temp_path, temp_ext);
|
||||
|
||||
if (!os_quick_write_utf8_file(temp_path.array, str, len, marker)) {
|
||||
blog(LOG_ERROR, "os_quick_write_utf8_file_safe: failed to "
|
||||
"write to %s", temp_path.array);
|
||||
blog(LOG_ERROR,
|
||||
"os_quick_write_utf8_file_safe: failed to "
|
||||
"write to %s",
|
||||
temp_path.array);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
|
|
@ -324,7 +326,7 @@ cleanup:
|
|||
|
||||
int64_t os_get_file_size(const char *path)
|
||||
{
|
||||
FILE* f = os_fopen(path, "rb");
|
||||
FILE *f = os_fopen(path, "rb");
|
||||
if (!f)
|
||||
return -1;
|
||||
|
||||
|
|
@ -357,7 +359,7 @@ size_t os_mbs_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size)
|
|||
}
|
||||
|
||||
size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst,
|
||||
size_t dst_size)
|
||||
size_t dst_size)
|
||||
{
|
||||
size_t in_len;
|
||||
size_t out_len;
|
||||
|
|
@ -373,8 +375,8 @@ size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst,
|
|||
return 0;
|
||||
|
||||
if (out_len)
|
||||
out_len = utf8_to_wchar(str, in_len,
|
||||
dst, out_len + 1, 0);
|
||||
out_len =
|
||||
utf8_to_wchar(str, in_len, dst, out_len + 1, 0);
|
||||
|
||||
dst[out_len] = 0;
|
||||
}
|
||||
|
|
@ -405,7 +407,7 @@ size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst, size_t dst_size)
|
|||
}
|
||||
|
||||
size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst,
|
||||
size_t dst_size)
|
||||
size_t dst_size)
|
||||
{
|
||||
size_t in_len;
|
||||
size_t out_len;
|
||||
|
|
@ -421,8 +423,8 @@ size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst,
|
|||
return 0;
|
||||
|
||||
if (out_len)
|
||||
out_len = wchar_to_utf8(str, in_len,
|
||||
dst, out_len + 1, 0);
|
||||
out_len =
|
||||
wchar_to_utf8(str, in_len, dst, out_len + 1, 0);
|
||||
|
||||
dst[out_len] = 0;
|
||||
}
|
||||
|
|
@ -484,12 +486,12 @@ size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr)
|
|||
|
||||
size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr)
|
||||
{
|
||||
char *dst = NULL;
|
||||
size_t out_len = 0;
|
||||
char *dst = NULL;
|
||||
size_t out_len = 0;
|
||||
|
||||
if (str) {
|
||||
wchar_t *wstr = NULL;
|
||||
size_t wlen = os_utf8_to_wcs_ptr(str, len, &wstr);
|
||||
size_t wlen = os_utf8_to_wcs_ptr(str, len, &wstr);
|
||||
out_len = os_wcs_to_mbs_ptr(wstr, wlen, &dst);
|
||||
bfree(wstr);
|
||||
}
|
||||
|
|
@ -500,12 +502,12 @@ size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr)
|
|||
|
||||
size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr)
|
||||
{
|
||||
char *dst = NULL;
|
||||
size_t out_len = 0;
|
||||
char *dst = NULL;
|
||||
size_t out_len = 0;
|
||||
|
||||
if (str) {
|
||||
wchar_t *wstr = NULL;
|
||||
size_t wlen = os_mbs_to_wcs_ptr(str, len, &wstr);
|
||||
size_t wlen = os_mbs_to_wcs_ptr(str, len, &wstr);
|
||||
out_len = os_wcs_to_utf8_ptr(wstr, wlen, &dst);
|
||||
bfree(wstr);
|
||||
}
|
||||
|
|
@ -522,13 +524,13 @@ static inline void to_locale(char *str)
|
|||
char *pos;
|
||||
|
||||
point = localeconv()->decimal_point;
|
||||
if(*point == '.') {
|
||||
if (*point == '.') {
|
||||
/* No conversion needed */
|
||||
return;
|
||||
}
|
||||
|
||||
pos = strchr(str, '.');
|
||||
if(pos)
|
||||
if (pos)
|
||||
*pos = *point;
|
||||
}
|
||||
|
||||
|
|
@ -538,13 +540,13 @@ static inline void from_locale(char *buffer)
|
|||
char *pos;
|
||||
|
||||
point = localeconv()->decimal_point;
|
||||
if(*point == '.') {
|
||||
if (*point == '.') {
|
||||
/* No conversion needed */
|
||||
return;
|
||||
}
|
||||
|
||||
pos = strchr(buffer, *point);
|
||||
if(pos)
|
||||
if (pos)
|
||||
*pos = '.';
|
||||
}
|
||||
|
||||
|
|
@ -563,19 +565,19 @@ int os_dtostr(double value, char *dst, size_t size)
|
|||
size_t length;
|
||||
|
||||
ret = snprintf(dst, size, "%.17g", value);
|
||||
if(ret < 0)
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
length = (size_t)ret;
|
||||
if(length >= size)
|
||||
if (length >= size)
|
||||
return -1;
|
||||
|
||||
from_locale(dst);
|
||||
|
||||
/* Make sure there's a dot or 'e' in the output. Otherwise
|
||||
a real is converted to an integer when decoding */
|
||||
if(strchr(dst, '.') == NULL && strchr(dst, 'e') == NULL) {
|
||||
if(length + 3 >= size) {
|
||||
if (strchr(dst, '.') == NULL && strchr(dst, 'e') == NULL) {
|
||||
if (length + 3 >= size) {
|
||||
/* No space to append ".0" */
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -588,17 +590,17 @@ int os_dtostr(double value, char *dst, size_t size)
|
|||
/* Remove leading '+' from positive exponent. Also remove leading
|
||||
zeros from exponents (added by some printf() implementations) */
|
||||
start = strchr(dst, 'e');
|
||||
if(start) {
|
||||
if (start) {
|
||||
start++;
|
||||
end = start + 1;
|
||||
|
||||
if(*start == '-')
|
||||
if (*start == '-')
|
||||
start++;
|
||||
|
||||
while(*end == '0')
|
||||
while (*end == '0')
|
||||
end++;
|
||||
|
||||
if(end != start) {
|
||||
if (end != start) {
|
||||
memmove(start, end, length - (size_t)(end - dst));
|
||||
length -= (size_t)(end - start);
|
||||
}
|
||||
|
|
@ -677,7 +679,7 @@ static inline bool valid_string(const char *str)
|
|||
}
|
||||
|
||||
static void replace_text(struct dstr *str, size_t pos, size_t len,
|
||||
const char *new_text)
|
||||
const char *new_text)
|
||||
{
|
||||
struct dstr front = {0};
|
||||
struct dstr back = {0};
|
||||
|
|
@ -701,7 +703,7 @@ static void erase_ch(struct dstr *str, size_t pos)
|
|||
}
|
||||
|
||||
char *os_generate_formatted_filename(const char *extension, bool space,
|
||||
const char *format)
|
||||
const char *format)
|
||||
{
|
||||
time_t now = time(0);
|
||||
struct tm *cur_time;
|
||||
|
|
@ -709,30 +711,13 @@ char *os_generate_formatted_filename(const char *extension, bool space,
|
|||
|
||||
const size_t spec_count = 23;
|
||||
static const char *spec[][2] = {
|
||||
{"%CCYY", "%Y"},
|
||||
{"%YY", "%y"},
|
||||
{"%MM", "%m"},
|
||||
{"%DD", "%d"},
|
||||
{"%hh", "%H"},
|
||||
{"%mm", "%M"},
|
||||
{"%ss", "%S"},
|
||||
{"%%", "%%"},
|
||||
{"%CCYY", "%Y"}, {"%YY", "%y"}, {"%MM", "%m"}, {"%DD", "%d"},
|
||||
{"%hh", "%H"}, {"%mm", "%M"}, {"%ss", "%S"}, {"%%", "%%"},
|
||||
|
||||
{"%a", ""},
|
||||
{"%A", ""},
|
||||
{"%b", ""},
|
||||
{"%B", ""},
|
||||
{"%d", ""},
|
||||
{"%H", ""},
|
||||
{"%I", ""},
|
||||
{"%m", ""},
|
||||
{"%M", ""},
|
||||
{"%p", ""},
|
||||
{"%S", ""},
|
||||
{"%y", ""},
|
||||
{"%Y", ""},
|
||||
{"%z", ""},
|
||||
{"%Z", ""},
|
||||
{"%a", ""}, {"%A", ""}, {"%b", ""}, {"%B", ""},
|
||||
{"%d", ""}, {"%H", ""}, {"%I", ""}, {"%m", ""},
|
||||
{"%M", ""}, {"%p", ""}, {"%S", ""}, {"%y", ""},
|
||||
{"%Y", ""}, {"%z", ""}, {"%Z", ""},
|
||||
};
|
||||
|
||||
char convert[128] = {0};
|
||||
|
|
@ -751,11 +736,10 @@ char *os_generate_formatted_filename(const char *extension, bool space,
|
|||
if (astrcmp_n(cmp, spec[i][0], len) == 0) {
|
||||
if (strlen(spec[i][1]))
|
||||
strftime(convert, sizeof(convert),
|
||||
spec[i][1], cur_time);
|
||||
spec[i][1], cur_time);
|
||||
else
|
||||
strftime(convert, sizeof(convert),
|
||||
spec[i][0], cur_time);
|
||||
|
||||
spec[i][0], cur_time);
|
||||
|
||||
dstr_copy(&c, convert);
|
||||
if (c.len && valid_string(c.array))
|
||||
|
|
|
|||
|
|
@ -49,25 +49,26 @@ EXPORT size_t os_fread_utf8(FILE *file, char **pstr);
|
|||
/* functions purely for convenience */
|
||||
EXPORT char *os_quick_read_utf8_file(const char *path);
|
||||
EXPORT bool os_quick_write_utf8_file(const char *path, const char *str,
|
||||
size_t len, bool marker);
|
||||
size_t len, bool marker);
|
||||
EXPORT bool os_quick_write_utf8_file_safe(const char *path, const char *str,
|
||||
size_t len, bool marker, const char *temp_ext,
|
||||
const char *backup_ext);
|
||||
size_t len, bool marker,
|
||||
const char *temp_ext,
|
||||
const char *backup_ext);
|
||||
EXPORT char *os_quick_read_mbs_file(const char *path);
|
||||
EXPORT bool os_quick_write_mbs_file(const char *path, const char *str,
|
||||
size_t len);
|
||||
size_t len);
|
||||
|
||||
EXPORT int64_t os_get_file_size(const char *path);
|
||||
EXPORT int64_t os_get_free_space(const char *path);
|
||||
|
||||
EXPORT size_t os_mbs_to_wcs(const char *str, size_t str_len, wchar_t *dst,
|
||||
size_t dst_size);
|
||||
size_t dst_size);
|
||||
EXPORT size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst,
|
||||
size_t dst_size);
|
||||
size_t dst_size);
|
||||
EXPORT size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst,
|
||||
size_t dst_size);
|
||||
size_t dst_size);
|
||||
EXPORT size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst,
|
||||
size_t dst_size);
|
||||
size_t dst_size);
|
||||
|
||||
EXPORT size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr);
|
||||
EXPORT size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr);
|
||||
|
|
@ -88,12 +89,12 @@ struct os_cpu_usage_info;
|
|||
typedef struct os_cpu_usage_info os_cpu_usage_info_t;
|
||||
|
||||
EXPORT os_cpu_usage_info_t *os_cpu_usage_info_start(void);
|
||||
EXPORT double os_cpu_usage_info_query(os_cpu_usage_info_t *info);
|
||||
EXPORT void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info);
|
||||
EXPORT double os_cpu_usage_info_query(os_cpu_usage_info_t *info);
|
||||
EXPORT void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info);
|
||||
|
||||
typedef const void os_performance_token_t;
|
||||
EXPORT os_performance_token_t *os_request_high_performance(const char *reason);
|
||||
EXPORT void os_end_high_performance(os_performance_token_t *);
|
||||
EXPORT void os_end_high_performance(os_performance_token_t *);
|
||||
|
||||
/**
|
||||
* Sleeps to a specific time (in nanoseconds). Doesn't have to be super
|
||||
|
|
@ -138,7 +139,7 @@ struct os_globent {
|
|||
};
|
||||
|
||||
struct os_glob_info {
|
||||
size_t gl_pathc;
|
||||
size_t gl_pathc;
|
||||
struct os_globent *gl_pathv;
|
||||
};
|
||||
|
||||
|
|
@ -157,19 +158,19 @@ EXPORT int os_chdir(const char *path);
|
|||
|
||||
EXPORT uint64_t os_get_free_disk_space(const char *dir);
|
||||
|
||||
#define MKDIR_EXISTS 1
|
||||
#define MKDIR_SUCCESS 0
|
||||
#define MKDIR_ERROR -1
|
||||
#define MKDIR_EXISTS 1
|
||||
#define MKDIR_SUCCESS 0
|
||||
#define MKDIR_ERROR -1
|
||||
|
||||
EXPORT int os_mkdir(const char *path);
|
||||
EXPORT int os_mkdirs(const char *path);
|
||||
EXPORT int os_rename(const char *old_path, const char *new_path);
|
||||
EXPORT int os_copyfile(const char *file_in, const char *file_out);
|
||||
EXPORT int os_safe_replace(const char *target_path, const char *from_path,
|
||||
const char *backup_path);
|
||||
const char *backup_path);
|
||||
|
||||
EXPORT char *os_generate_formatted_filename(const char *extension, bool space,
|
||||
const char *format);
|
||||
const char *format);
|
||||
|
||||
struct os_inhibit_info;
|
||||
typedef struct os_inhibit_info os_inhibit_t;
|
||||
|
|
@ -202,6 +203,7 @@ EXPORT uint64_t os_get_proc_virtual_size(void);
|
|||
#endif
|
||||
#endif
|
||||
|
||||
/* clang-format off */
|
||||
#ifdef __APPLE__
|
||||
# define ARCH_BITS 64
|
||||
#else
|
||||
|
|
@ -219,6 +221,7 @@ EXPORT uint64_t os_get_proc_virtual_size(void);
|
|||
# endif
|
||||
# endif
|
||||
#endif
|
||||
/* clang-format on */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,16 +93,16 @@ static inline uint64_t diff_ns_to_usec(uint64_t prev, uint64_t next)
|
|||
|
||||
static inline void update_max_probes(profile_times_table *map, size_t val)
|
||||
{
|
||||
map->max_probe_count = map->max_probe_count < val ?
|
||||
val : map->max_probe_count;
|
||||
map->max_probe_count =
|
||||
map->max_probe_count < val ? val : map->max_probe_count;
|
||||
}
|
||||
|
||||
static void migrate_old_entries(profile_times_table *map, bool limit_items);
|
||||
static void grow_hashmap(profile_times_table *map,
|
||||
uint64_t usec, uint64_t count);
|
||||
static void grow_hashmap(profile_times_table *map, uint64_t usec,
|
||||
uint64_t count);
|
||||
|
||||
static void add_hashmap_entry(profile_times_table *map, uint64_t usec,
|
||||
uint64_t count)
|
||||
uint64_t count)
|
||||
{
|
||||
size_t probes = 1;
|
||||
|
||||
|
|
@ -127,14 +127,14 @@ static void add_hashmap_entry(profile_times_table *map, uint64_t usec,
|
|||
if (entry->probes >= probes)
|
||||
continue;
|
||||
|
||||
if (map->occupied/(double)map->size > 0.7) {
|
||||
if (map->occupied / (double)map->size > 0.7) {
|
||||
grow_hashmap(map, usec, count);
|
||||
return;
|
||||
}
|
||||
|
||||
size_t old_probes = entry->probes;
|
||||
uint64_t old_count = entry->entry.count;
|
||||
uint64_t old_usec = entry->entry.time_delta;
|
||||
size_t old_probes = entry->probes;
|
||||
uint64_t old_count = entry->entry.count;
|
||||
uint64_t old_usec = entry->entry.time_delta;
|
||||
|
||||
entry->probes = probes;
|
||||
entry->entry.count = count;
|
||||
|
|
@ -143,8 +143,8 @@ static void add_hashmap_entry(profile_times_table *map, uint64_t usec,
|
|||
update_max_probes(map, probes);
|
||||
|
||||
probes = old_probes;
|
||||
count = old_count;
|
||||
usec = old_usec;
|
||||
count = old_count;
|
||||
usec = old_usec;
|
||||
|
||||
start = usec % map->size;
|
||||
}
|
||||
|
|
@ -182,24 +182,24 @@ static void migrate_old_entries(profile_times_table *map, bool limit_items)
|
|||
continue;
|
||||
|
||||
add_hashmap_entry(map, entry->entry.time_delta,
|
||||
entry->entry.count);
|
||||
entry->entry.count);
|
||||
map->old_occupied -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void grow_hashmap(profile_times_table *map,
|
||||
uint64_t usec, uint64_t count)
|
||||
static void grow_hashmap(profile_times_table *map, uint64_t usec,
|
||||
uint64_t count)
|
||||
{
|
||||
migrate_old_entries(map, false);
|
||||
|
||||
size_t old_size = map->size;
|
||||
size_t old_size = map->size;
|
||||
size_t old_occupied = map->occupied;
|
||||
profile_times_table_entry *entries = map->entries;
|
||||
|
||||
init_hashmap(map, (old_size * 2 < 16) ? 16 : (old_size * 2));
|
||||
|
||||
map->old_occupied = old_occupied;
|
||||
map->old_entries = entries;
|
||||
map->old_entries = entries;
|
||||
|
||||
add_hashmap_entry(map, usec, count);
|
||||
}
|
||||
|
|
@ -229,7 +229,7 @@ static profile_entry *get_child(profile_entry *parent, const char *name)
|
|||
}
|
||||
|
||||
static void merge_call(profile_entry *entry, profile_call *call,
|
||||
profile_call *prev_call)
|
||||
profile_call *prev_call)
|
||||
{
|
||||
const size_t num = call->children.num;
|
||||
for (size_t i = 0; i < num; i++) {
|
||||
|
|
@ -240,7 +240,7 @@ static void merge_call(profile_entry *entry, profile_call *call,
|
|||
if (entry->expected_time_between_calls != 0 && prev_call) {
|
||||
migrate_old_entries(&entry->times_between_calls, true);
|
||||
uint64_t usec = diff_ns_to_usec(prev_call->start_time,
|
||||
call->start_time);
|
||||
call->start_time);
|
||||
add_hashmap_entry(&entry->times_between_calls, usec, 1);
|
||||
}
|
||||
|
||||
|
|
@ -250,7 +250,7 @@ static void merge_call(profile_entry *entry, profile_call *call,
|
|||
|
||||
#ifdef TRACK_OVERHEAD
|
||||
migrate_old_entries(&entry->overhead, true);
|
||||
usec = diff_ns_to_usec(call->overhead_start, call->start_time);
|
||||
usec = diff_ns_to_usec(call->overhead_start, call->start_time);
|
||||
usec += diff_ns_to_usec(call->end_time, call->overhead_end);
|
||||
add_hashmap_entry(&entry->overhead, usec, 1);
|
||||
#endif
|
||||
|
|
@ -324,7 +324,7 @@ static profile_root_entry *get_root_entry(const char *name)
|
|||
}
|
||||
|
||||
void profile_register_root(const char *name,
|
||||
uint64_t expected_time_between_calls)
|
||||
uint64_t expected_time_between_calls)
|
||||
{
|
||||
if (!lock_root())
|
||||
return;
|
||||
|
|
@ -349,8 +349,8 @@ static void merge_context(profile_call *context)
|
|||
|
||||
profile_root_entry *r_entry = get_root_entry(context->name);
|
||||
|
||||
mutex = r_entry->mutex;
|
||||
entry = r_entry->entry;
|
||||
mutex = r_entry->mutex;
|
||||
entry = r_entry->entry;
|
||||
prev_call = r_entry->prev_call;
|
||||
|
||||
r_entry->prev_call = context;
|
||||
|
|
@ -408,12 +408,13 @@ void profile_end(const char *name)
|
|||
call->name = name;
|
||||
|
||||
if (call->name != name) {
|
||||
blog(LOG_ERROR, "Called profile end with mismatching name: "
|
||||
"start(\"%s\"[%p]) <-> end(\"%s\"[%p])",
|
||||
call->name, call->name, name, name);
|
||||
blog(LOG_ERROR,
|
||||
"Called profile end with mismatching name: "
|
||||
"start(\"%s\"[%p]) <-> end(\"%s\"[%p])",
|
||||
call->name, call->name, name, name);
|
||||
|
||||
profile_call *parent = call->parent;
|
||||
while (parent && parent->parent && parent->name != name)
|
||||
while (parent && parent->parent && parent->name != name)
|
||||
parent = parent->parent;
|
||||
|
||||
if (!parent || parent->name != name)
|
||||
|
|
@ -440,14 +441,14 @@ void profile_end(const char *name)
|
|||
|
||||
static int profiler_time_entry_compare(const void *first, const void *second)
|
||||
{
|
||||
int64_t diff = ((profiler_time_entry*)second)->time_delta -
|
||||
((profiler_time_entry*)first)->time_delta;
|
||||
int64_t diff = ((profiler_time_entry *)second)->time_delta -
|
||||
((profiler_time_entry *)first)->time_delta;
|
||||
return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
static uint64_t copy_map_to_array(profile_times_table *map,
|
||||
profiler_time_entries_t *entry_buffer,
|
||||
uint64_t *min_, uint64_t *max_)
|
||||
profiler_time_entries_t *entry_buffer,
|
||||
uint64_t *min_, uint64_t *max_)
|
||||
{
|
||||
migrate_old_entries(map, false);
|
||||
|
||||
|
|
@ -480,16 +481,18 @@ static uint64_t copy_map_to_array(profile_times_table *map,
|
|||
}
|
||||
|
||||
typedef void (*profile_entry_print_func)(profiler_snapshot_entry_t *entry,
|
||||
struct dstr *indent_buffer, struct dstr *output_buffer,
|
||||
unsigned indent, uint64_t active, uint64_t parent_calls);
|
||||
struct dstr *indent_buffer,
|
||||
struct dstr *output_buffer,
|
||||
unsigned indent, uint64_t active,
|
||||
uint64_t parent_calls);
|
||||
|
||||
/* UTF-8 characters */
|
||||
#define VPIPE_RIGHT " \xe2\x94\xa3"
|
||||
#define VPIPE " \xe2\x94\x83"
|
||||
#define DOWN_RIGHT " \xe2\x94\x97"
|
||||
#define VPIPE " \xe2\x94\x83"
|
||||
#define DOWN_RIGHT " \xe2\x94\x97"
|
||||
|
||||
static void make_indent_string(struct dstr *indent_buffer, unsigned indent,
|
||||
uint64_t active)
|
||||
uint64_t active)
|
||||
{
|
||||
indent_buffer->len = 0;
|
||||
|
||||
|
|
@ -505,15 +508,15 @@ static void make_indent_string(struct dstr *indent_buffer, unsigned indent,
|
|||
fragment = last ? VPIPE_RIGHT : VPIPE;
|
||||
else
|
||||
fragment = last ? DOWN_RIGHT : " ";
|
||||
|
||||
|
||||
dstr_cat(indent_buffer, fragment);
|
||||
}
|
||||
}
|
||||
|
||||
static void gather_stats(uint64_t expected_time_between_calls,
|
||||
profiler_time_entries_t *entries,
|
||||
uint64_t calls, uint64_t *percentile99, uint64_t *median,
|
||||
double *percent_within_bounds)
|
||||
profiler_time_entries_t *entries, uint64_t calls,
|
||||
uint64_t *percentile99, uint64_t *median,
|
||||
double *percent_within_bounds)
|
||||
{
|
||||
if (!entries->num) {
|
||||
*percentile99 = 0;
|
||||
|
|
@ -559,8 +562,9 @@ static void gather_stats(uint64_t expected_time_between_calls,
|
|||
#define G_MS "g\xC2\xA0ms"
|
||||
|
||||
static void profile_print_entry(profiler_snapshot_entry_t *entry,
|
||||
struct dstr *indent_buffer, struct dstr *output_buffer,
|
||||
unsigned indent, uint64_t active, uint64_t parent_calls)
|
||||
struct dstr *indent_buffer,
|
||||
struct dstr *output_buffer, unsigned indent,
|
||||
uint64_t active, uint64_t parent_calls)
|
||||
{
|
||||
uint64_t calls = entry->overall_count;
|
||||
uint64_t min_ = entry->min_time;
|
||||
|
|
@ -568,28 +572,26 @@ static void profile_print_entry(profiler_snapshot_entry_t *entry,
|
|||
uint64_t percentile99 = 0;
|
||||
uint64_t median = 0;
|
||||
double percent_within_bounds = 0.;
|
||||
gather_stats(entry->expected_time_between_calls,
|
||||
&entry->times, calls,
|
||||
&percentile99, &median, &percent_within_bounds);
|
||||
gather_stats(entry->expected_time_between_calls, &entry->times, calls,
|
||||
&percentile99, &median, &percent_within_bounds);
|
||||
|
||||
make_indent_string(indent_buffer, indent, active);
|
||||
|
||||
if (min_ == max_) {
|
||||
dstr_printf(output_buffer, "%s%s: %"G_MS,
|
||||
indent_buffer->array, entry->name,
|
||||
min_ / 1000.);
|
||||
dstr_printf(output_buffer, "%s%s: %" G_MS, indent_buffer->array,
|
||||
entry->name, min_ / 1000.);
|
||||
} else {
|
||||
dstr_printf(output_buffer, "%s%s: min=%"G_MS", median=%"G_MS", "
|
||||
"max=%"G_MS", 99th percentile=%"G_MS,
|
||||
indent_buffer->array, entry->name,
|
||||
min_ / 1000., median / 1000., max_ / 1000.,
|
||||
percentile99 / 1000.);
|
||||
dstr_printf(output_buffer,
|
||||
"%s%s: min=%" G_MS ", median=%" G_MS ", "
|
||||
"max=%" G_MS ", 99th percentile=%" G_MS,
|
||||
indent_buffer->array, entry->name, min_ / 1000.,
|
||||
median / 1000., max_ / 1000., percentile99 / 1000.);
|
||||
|
||||
if (entry->expected_time_between_calls) {
|
||||
double expected_ms =
|
||||
entry->expected_time_between_calls / 1000.;
|
||||
dstr_catf(output_buffer, ", %g%% below %"G_MS,
|
||||
percent_within_bounds, expected_ms);
|
||||
dstr_catf(output_buffer, ", %g%% below %" G_MS,
|
||||
percent_within_bounds, expected_ms);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -597,7 +599,7 @@ static void profile_print_entry(profiler_snapshot_entry_t *entry,
|
|||
double calls_per_parent = (double)calls / parent_calls;
|
||||
if (lround(calls_per_parent * 10) != 10)
|
||||
dstr_catf(output_buffer, ", %g calls per parent call",
|
||||
calls_per_parent);
|
||||
calls_per_parent);
|
||||
}
|
||||
|
||||
blog(LOG_INFO, "%s", output_buffer->array);
|
||||
|
|
@ -606,16 +608,16 @@ static void profile_print_entry(profiler_snapshot_entry_t *entry,
|
|||
for (size_t i = 0; i < entry->children.num; i++) {
|
||||
if ((i + 1) == entry->children.num)
|
||||
active &= (1 << indent) - 1;
|
||||
profile_print_entry(&entry->children.array[i],
|
||||
indent_buffer, output_buffer,
|
||||
indent + 1, active, calls);
|
||||
profile_print_entry(&entry->children.array[i], indent_buffer,
|
||||
output_buffer, indent + 1, active, calls);
|
||||
}
|
||||
}
|
||||
|
||||
static void gather_stats_between(profiler_time_entries_t *entries,
|
||||
uint64_t calls, uint64_t lower_bound, uint64_t upper_bound,
|
||||
uint64_t min_, uint64_t max_, uint64_t *median,
|
||||
double *percent, double *lower, double *higher)
|
||||
uint64_t calls, uint64_t lower_bound,
|
||||
uint64_t upper_bound, uint64_t min_,
|
||||
uint64_t max_, uint64_t *median,
|
||||
double *percent, double *lower, double *higher)
|
||||
{
|
||||
*median = 0;
|
||||
*percent = 0.;
|
||||
|
|
@ -679,8 +681,10 @@ static void gather_stats_between(profiler_time_entries_t *entries,
|
|||
}
|
||||
|
||||
static void profile_print_entry_expected(profiler_snapshot_entry_t *entry,
|
||||
struct dstr *indent_buffer, struct dstr *output_buffer,
|
||||
unsigned indent, uint64_t active, uint64_t parent_calls)
|
||||
struct dstr *indent_buffer,
|
||||
struct dstr *output_buffer,
|
||||
unsigned indent, uint64_t active,
|
||||
uint64_t parent_calls)
|
||||
{
|
||||
UNUSED_PARAMETER(parent_calls);
|
||||
|
||||
|
|
@ -696,33 +700,31 @@ static void profile_print_entry_expected(profiler_snapshot_entry_t *entry,
|
|||
double lower = 0.;
|
||||
double higher = 0.;
|
||||
gather_stats_between(&entry->times_between_calls,
|
||||
entry->overall_between_calls_count,
|
||||
(uint64_t)(expected_time * 0.98),
|
||||
(uint64_t)(expected_time * 1.02 + 0.5),
|
||||
min_, max_,
|
||||
&median, &percent, &lower, &higher);
|
||||
entry->overall_between_calls_count,
|
||||
(uint64_t)(expected_time * 0.98),
|
||||
(uint64_t)(expected_time * 1.02 + 0.5), min_, max_,
|
||||
&median, &percent, &lower, &higher);
|
||||
|
||||
make_indent_string(indent_buffer, indent, active);
|
||||
|
||||
blog(LOG_INFO, "%s%s: min=%"G_MS", median=%"G_MS", max=%"G_MS", %g%% "
|
||||
"within ±2%% of %"G_MS" (%g%% lower, %g%% higher)",
|
||||
indent_buffer->array, entry->name,
|
||||
min_ / 1000., median / 1000., max_ / 1000., percent,
|
||||
expected_time / 1000.,
|
||||
lower, higher);
|
||||
blog(LOG_INFO,
|
||||
"%s%s: min=%" G_MS ", median=%" G_MS ", max=%" G_MS ", %g%% "
|
||||
"within ±2%% of %" G_MS " (%g%% lower, %g%% higher)",
|
||||
indent_buffer->array, entry->name, min_ / 1000., median / 1000.,
|
||||
max_ / 1000., percent, expected_time / 1000., lower, higher);
|
||||
|
||||
active |= (uint64_t)1 << indent;
|
||||
for (size_t i = 0; i < entry->children.num; i++) {
|
||||
if ((i + 1) == entry->children.num)
|
||||
active &= (1 << indent) - 1;
|
||||
profile_print_entry_expected(&entry->children.array[i],
|
||||
indent_buffer, output_buffer,
|
||||
indent + 1, active, 0);
|
||||
indent_buffer, output_buffer,
|
||||
indent + 1, active, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void profile_print_func(const char *intro, profile_entry_print_func print,
|
||||
profiler_snapshot_t *snap)
|
||||
profiler_snapshot_t *snap)
|
||||
{
|
||||
struct dstr indent_buffer = {0};
|
||||
struct dstr output_buffer = {0};
|
||||
|
|
@ -733,8 +735,8 @@ void profile_print_func(const char *intro, profile_entry_print_func print,
|
|||
|
||||
blog(LOG_INFO, "%s", intro);
|
||||
for (size_t i = 0; i < snap->roots.num; i++) {
|
||||
print(&snap->roots.array[i],
|
||||
&indent_buffer, &output_buffer, 0, 0, 0);
|
||||
print(&snap->roots.array[i], &indent_buffer, &output_buffer, 0,
|
||||
0, 0);
|
||||
}
|
||||
blog(LOG_INFO, "=================================================");
|
||||
|
||||
|
|
@ -748,13 +750,13 @@ void profile_print_func(const char *intro, profile_entry_print_func print,
|
|||
void profiler_print(profiler_snapshot_t *snap)
|
||||
{
|
||||
profile_print_func("== Profiler Results =============================",
|
||||
profile_print_entry, snap);
|
||||
profile_print_entry, snap);
|
||||
}
|
||||
|
||||
void profiler_print_time_between_calls(profiler_snapshot_t *snap)
|
||||
{
|
||||
profile_print_func("== Profiler Time Between Calls ==================",
|
||||
profile_print_entry_expected, snap);
|
||||
profile_print_entry_expected, snap);
|
||||
}
|
||||
|
||||
static void free_call_children(profile_call *call)
|
||||
|
|
@ -825,19 +827,18 @@ void profiler_free(void)
|
|||
da_free(old_root_entries);
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* Profiler name storage */
|
||||
|
||||
struct profiler_name_store {
|
||||
pthread_mutex_t mutex;
|
||||
DARRAY(char*) names;
|
||||
DARRAY(char *) names;
|
||||
};
|
||||
|
||||
profiler_name_store_t *profiler_name_store_create(void)
|
||||
{
|
||||
profiler_name_store_t *store = bzalloc(sizeof(profiler_name_store_t));
|
||||
|
||||
|
||||
if (pthread_mutex_init(&store->mutex, NULL))
|
||||
goto error;
|
||||
|
||||
|
|
@ -860,8 +861,8 @@ void profiler_name_store_free(profiler_name_store_t *store)
|
|||
bfree(store);
|
||||
}
|
||||
|
||||
const char *profile_store_name(profiler_name_store_t *store,
|
||||
const char *format, ...)
|
||||
const char *profile_store_name(profiler_name_store_t *store, const char *format,
|
||||
...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
|
|
@ -881,44 +882,41 @@ const char *profile_store_name(profiler_name_store_t *store,
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* Profiler data access */
|
||||
|
||||
static void add_entry_to_snapshot(profile_entry *entry,
|
||||
profiler_snapshot_entry_t *s_entry)
|
||||
profiler_snapshot_entry_t *s_entry)
|
||||
{
|
||||
s_entry->name = entry->name;
|
||||
|
||||
s_entry->overall_count = copy_map_to_array(&entry->times,
|
||||
&s_entry->times,
|
||||
&s_entry->min_time, &s_entry->max_time);
|
||||
s_entry->overall_count =
|
||||
copy_map_to_array(&entry->times, &s_entry->times,
|
||||
&s_entry->min_time, &s_entry->max_time);
|
||||
|
||||
if ((s_entry->expected_time_between_calls =
|
||||
entry->expected_time_between_calls))
|
||||
if ((s_entry->expected_time_between_calls =
|
||||
entry->expected_time_between_calls))
|
||||
s_entry->overall_between_calls_count =
|
||||
copy_map_to_array(&entry->times_between_calls,
|
||||
&s_entry->times_between_calls,
|
||||
&s_entry->min_time_between_calls,
|
||||
&s_entry->max_time_between_calls);
|
||||
&s_entry->times_between_calls,
|
||||
&s_entry->min_time_between_calls,
|
||||
&s_entry->max_time_between_calls);
|
||||
|
||||
da_reserve(s_entry->children, entry->children.num);
|
||||
for (size_t i = 0; i < entry->children.num; i++)
|
||||
add_entry_to_snapshot(&entry->children.array[i],
|
||||
da_push_back_new(s_entry->children));
|
||||
da_push_back_new(s_entry->children));
|
||||
}
|
||||
|
||||
static void sort_snapshot_entry(profiler_snapshot_entry_t *entry)
|
||||
{
|
||||
qsort(entry->times.array, entry->times.num,
|
||||
sizeof(profiler_time_entry),
|
||||
profiler_time_entry_compare);
|
||||
qsort(entry->times.array, entry->times.num, sizeof(profiler_time_entry),
|
||||
profiler_time_entry_compare);
|
||||
|
||||
if (entry->expected_time_between_calls)
|
||||
qsort(entry->times_between_calls.array,
|
||||
entry->times_between_calls.num,
|
||||
sizeof(profiler_time_entry),
|
||||
profiler_time_entry_compare);
|
||||
entry->times_between_calls.num,
|
||||
sizeof(profiler_time_entry), profiler_time_entry_compare);
|
||||
|
||||
for (size_t i = 0; i < entry->children.num; i++)
|
||||
sort_snapshot_entry(&entry->children.array[i]);
|
||||
|
|
@ -933,7 +931,7 @@ profiler_snapshot_t *profile_snapshot_create(void)
|
|||
for (size_t i = 0; i < root_entries.num; i++) {
|
||||
pthread_mutex_lock(root_entries.array[i].mutex);
|
||||
add_entry_to_snapshot(root_entries.array[i].entry,
|
||||
da_push_back_new(snap->roots));
|
||||
da_push_back_new(snap->roots));
|
||||
pthread_mutex_unlock(root_entries.array[i].mutex);
|
||||
}
|
||||
pthread_mutex_unlock(&root_mutex);
|
||||
|
|
@ -968,48 +966,50 @@ void profile_snapshot_free(profiler_snapshot_t *snap)
|
|||
|
||||
typedef void (*dump_csv_func)(void *data, struct dstr *buffer);
|
||||
static void entry_dump_csv(struct dstr *buffer,
|
||||
const profiler_snapshot_entry_t *parent,
|
||||
const profiler_snapshot_entry_t *entry,
|
||||
dump_csv_func func, void *data)
|
||||
const profiler_snapshot_entry_t *parent,
|
||||
const profiler_snapshot_entry_t *entry,
|
||||
dump_csv_func func, void *data)
|
||||
{
|
||||
const char *parent_name = parent ? parent->name : NULL;
|
||||
|
||||
for (size_t i = 0; i < entry->times.num; i++) {
|
||||
dstr_printf(buffer, "%p,%p,%p,%p,%s,0,"
|
||||
"%"PRIu64",%"PRIu64"\n", entry,
|
||||
parent, entry->name, parent_name, entry->name,
|
||||
entry->times.array[i].time_delta,
|
||||
entry->times.array[i].count);
|
||||
dstr_printf(buffer,
|
||||
"%p,%p,%p,%p,%s,0,"
|
||||
"%" PRIu64 ",%" PRIu64 "\n",
|
||||
entry, parent, entry->name, parent_name,
|
||||
entry->name, entry->times.array[i].time_delta,
|
||||
entry->times.array[i].count);
|
||||
func(data, buffer);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < entry->times_between_calls.num; i++) {
|
||||
dstr_printf(buffer,"%p,%p,%p,%p,%s,"
|
||||
"%"PRIu64",%"PRIu64",%"PRIu64"\n", entry,
|
||||
parent, entry->name, parent_name, entry->name,
|
||||
entry->expected_time_between_calls,
|
||||
entry->times_between_calls.array[i].time_delta,
|
||||
entry->times_between_calls.array[i].count);
|
||||
dstr_printf(buffer,
|
||||
"%p,%p,%p,%p,%s,"
|
||||
"%" PRIu64 ",%" PRIu64 ",%" PRIu64 "\n",
|
||||
entry, parent, entry->name, parent_name,
|
||||
entry->name, entry->expected_time_between_calls,
|
||||
entry->times_between_calls.array[i].time_delta,
|
||||
entry->times_between_calls.array[i].count);
|
||||
func(data, buffer);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < entry->children.num; i++)
|
||||
entry_dump_csv(buffer, entry, &entry->children.array[i],
|
||||
func, data);
|
||||
entry_dump_csv(buffer, entry, &entry->children.array[i], func,
|
||||
data);
|
||||
}
|
||||
|
||||
static void profiler_snapshot_dump(const profiler_snapshot_t *snap,
|
||||
dump_csv_func func, void *data)
|
||||
dump_csv_func func, void *data)
|
||||
{
|
||||
struct dstr buffer = {0};
|
||||
|
||||
dstr_init_copy(&buffer, "id,parent_id,name_id,parent_name_id,name,"
|
||||
"time_between_calls,time_delta_µs,count\n");
|
||||
"time_between_calls,time_delta_µs,count\n");
|
||||
func(data, &buffer);
|
||||
|
||||
for (size_t i = 0; i < snap->roots.num; i++)
|
||||
entry_dump_csv(&buffer, NULL,
|
||||
&snap->roots.array[i], func, data);
|
||||
entry_dump_csv(&buffer, NULL, &snap->roots.array[i], func,
|
||||
data);
|
||||
|
||||
dstr_free(&buffer);
|
||||
}
|
||||
|
|
@ -1020,7 +1020,7 @@ static void dump_csv_fwrite(void *data, struct dstr *buffer)
|
|||
}
|
||||
|
||||
bool profiler_snapshot_dump_csv(const profiler_snapshot_t *snap,
|
||||
const char *filename)
|
||||
const char *filename)
|
||||
{
|
||||
FILE *f = os_fopen(filename, "wb+");
|
||||
if (!f)
|
||||
|
|
@ -1038,7 +1038,7 @@ static void dump_csv_gzwrite(void *data, struct dstr *buffer)
|
|||
}
|
||||
|
||||
bool profiler_snapshot_dump_csv_gz(const profiler_snapshot_t *snap,
|
||||
const char *filename)
|
||||
const char *filename)
|
||||
{
|
||||
gzFile gz;
|
||||
#ifdef _WIN32
|
||||
|
|
@ -1068,7 +1068,8 @@ size_t profiler_snapshot_num_roots(profiler_snapshot_t *snap)
|
|||
}
|
||||
|
||||
void profiler_snapshot_enumerate_roots(profiler_snapshot_t *snap,
|
||||
profiler_entry_enum_func func, void *context)
|
||||
profiler_entry_enum_func func,
|
||||
void *context)
|
||||
{
|
||||
if (!snap)
|
||||
return;
|
||||
|
|
@ -1079,7 +1080,7 @@ void profiler_snapshot_enumerate_roots(profiler_snapshot_t *snap,
|
|||
}
|
||||
|
||||
void profiler_snapshot_filter_roots(profiler_snapshot_t *snap,
|
||||
profiler_name_filter_func func, void *data)
|
||||
profiler_name_filter_func func, void *data)
|
||||
{
|
||||
for (size_t i = 0; i < snap->roots.num;) {
|
||||
bool remove = false;
|
||||
|
|
@ -1104,7 +1105,8 @@ size_t profiler_snapshot_num_children(profiler_snapshot_entry_t *entry)
|
|||
}
|
||||
|
||||
void profiler_snapshot_enumerate_children(profiler_snapshot_entry_t *entry,
|
||||
profiler_entry_enum_func func, void *context)
|
||||
profiler_entry_enum_func func,
|
||||
void *context)
|
||||
{
|
||||
if (!entry)
|
||||
return;
|
||||
|
|
@ -1119,14 +1121,13 @@ const char *profiler_snapshot_entry_name(profiler_snapshot_entry_t *entry)
|
|||
return entry ? entry->name : NULL;
|
||||
}
|
||||
|
||||
profiler_time_entries_t *profiler_snapshot_entry_times(
|
||||
profiler_snapshot_entry_t *entry)
|
||||
profiler_time_entries_t *
|
||||
profiler_snapshot_entry_times(profiler_snapshot_entry_t *entry)
|
||||
{
|
||||
return entry ? &entry->times : NULL;
|
||||
}
|
||||
|
||||
uint64_t profiler_snapshot_entry_overall_count(
|
||||
profiler_snapshot_entry_t *entry)
|
||||
uint64_t profiler_snapshot_entry_overall_count(profiler_snapshot_entry_t *entry)
|
||||
{
|
||||
return entry ? entry->overall_count : 0;
|
||||
}
|
||||
|
|
@ -1141,32 +1142,32 @@ uint64_t profiler_snapshot_entry_max_time(profiler_snapshot_entry_t *entry)
|
|||
return entry ? entry->max_time : 0;
|
||||
}
|
||||
|
||||
profiler_time_entries_t *profiler_snapshot_entry_times_between_calls(
|
||||
profiler_snapshot_entry_t *entry)
|
||||
profiler_time_entries_t *
|
||||
profiler_snapshot_entry_times_between_calls(profiler_snapshot_entry_t *entry)
|
||||
{
|
||||
return entry ? &entry->times_between_calls : NULL;
|
||||
}
|
||||
|
||||
uint64_t profiler_snapshot_entry_expected_time_between_calls(
|
||||
profiler_snapshot_entry_t *entry)
|
||||
profiler_snapshot_entry_t *entry)
|
||||
{
|
||||
return entry ? entry->expected_time_between_calls : 0;
|
||||
}
|
||||
|
||||
uint64_t profiler_snapshot_entry_min_time_between_calls(
|
||||
profiler_snapshot_entry_t *entry)
|
||||
uint64_t
|
||||
profiler_snapshot_entry_min_time_between_calls(profiler_snapshot_entry_t *entry)
|
||||
{
|
||||
return entry ? entry->min_time_between_calls : 0;
|
||||
}
|
||||
|
||||
uint64_t profiler_snapshot_entry_max_time_between_calls(
|
||||
profiler_snapshot_entry_t *entry)
|
||||
uint64_t
|
||||
profiler_snapshot_entry_max_time_between_calls(profiler_snapshot_entry_t *entry)
|
||||
{
|
||||
return entry ? entry->max_time_between_calls : 0;
|
||||
}
|
||||
|
||||
uint64_t profiler_snapshot_entry_overall_between_calls_count(
|
||||
profiler_snapshot_entry_t *entry)
|
||||
profiler_snapshot_entry_t *entry)
|
||||
{
|
||||
return entry ? entry->overall_between_calls_count : 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ typedef struct profiler_time_entry profiler_time_entry_t;
|
|||
/* Profiling */
|
||||
|
||||
EXPORT void profile_register_root(const char *name,
|
||||
uint64_t expected_time_between_calls);
|
||||
uint64_t expected_time_between_calls);
|
||||
|
||||
EXPORT void profile_start(const char *name);
|
||||
EXPORT void profile_end(const char *name);
|
||||
|
|
@ -49,7 +49,7 @@ EXPORT void profiler_name_store_free(profiler_name_store_t *store);
|
|||
|
||||
PRINTFATTR(2, 3)
|
||||
EXPORT const char *profile_store_name(profiler_name_store_t *store,
|
||||
const char *format, ...);
|
||||
const char *format, ...);
|
||||
|
||||
#undef PRINTFATTR
|
||||
|
||||
|
|
@ -64,52 +64,55 @@ struct profiler_time_entry {
|
|||
typedef DARRAY(profiler_time_entry_t) profiler_time_entries_t;
|
||||
|
||||
typedef bool (*profiler_entry_enum_func)(void *context,
|
||||
profiler_snapshot_entry_t *entry);
|
||||
profiler_snapshot_entry_t *entry);
|
||||
|
||||
EXPORT profiler_snapshot_t *profile_snapshot_create(void);
|
||||
EXPORT void profile_snapshot_free(profiler_snapshot_t *snap);
|
||||
|
||||
EXPORT bool profiler_snapshot_dump_csv(const profiler_snapshot_t *snap,
|
||||
const char *filename);
|
||||
const char *filename);
|
||||
EXPORT bool profiler_snapshot_dump_csv_gz(const profiler_snapshot_t *snap,
|
||||
const char *filename);
|
||||
const char *filename);
|
||||
|
||||
EXPORT size_t profiler_snapshot_num_roots(profiler_snapshot_t *snap);
|
||||
EXPORT void profiler_snapshot_enumerate_roots(profiler_snapshot_t *snap,
|
||||
profiler_entry_enum_func func, void *context);
|
||||
profiler_entry_enum_func func,
|
||||
void *context);
|
||||
|
||||
typedef bool (*profiler_name_filter_func)(void *data, const char *name,
|
||||
bool *remove);
|
||||
bool *remove);
|
||||
EXPORT void profiler_snapshot_filter_roots(profiler_snapshot_t *snap,
|
||||
profiler_name_filter_func func, void *data);
|
||||
profiler_name_filter_func func,
|
||||
void *data);
|
||||
|
||||
EXPORT size_t profiler_snapshot_num_children(profiler_snapshot_entry_t *entry);
|
||||
EXPORT void profiler_snapshot_enumerate_children(
|
||||
profiler_snapshot_entry_t *entry,
|
||||
profiler_entry_enum_func func, void *context);
|
||||
EXPORT void
|
||||
profiler_snapshot_enumerate_children(profiler_snapshot_entry_t *entry,
|
||||
profiler_entry_enum_func func,
|
||||
void *context);
|
||||
|
||||
EXPORT const char *profiler_snapshot_entry_name(
|
||||
profiler_snapshot_entry_t *entry);
|
||||
EXPORT const char *
|
||||
profiler_snapshot_entry_name(profiler_snapshot_entry_t *entry);
|
||||
|
||||
EXPORT profiler_time_entries_t *profiler_snapshot_entry_times(
|
||||
profiler_snapshot_entry_t *entry);
|
||||
EXPORT uint64_t profiler_snapshot_entry_min_time(
|
||||
profiler_snapshot_entry_t *entry);
|
||||
EXPORT uint64_t profiler_snapshot_entry_max_time(
|
||||
profiler_snapshot_entry_t *entry);
|
||||
EXPORT uint64_t profiler_snapshot_entry_overall_count(
|
||||
profiler_snapshot_entry_t *entry);
|
||||
EXPORT profiler_time_entries_t *
|
||||
profiler_snapshot_entry_times(profiler_snapshot_entry_t *entry);
|
||||
EXPORT uint64_t
|
||||
profiler_snapshot_entry_min_time(profiler_snapshot_entry_t *entry);
|
||||
EXPORT uint64_t
|
||||
profiler_snapshot_entry_max_time(profiler_snapshot_entry_t *entry);
|
||||
EXPORT uint64_t
|
||||
profiler_snapshot_entry_overall_count(profiler_snapshot_entry_t *entry);
|
||||
|
||||
EXPORT profiler_time_entries_t *profiler_snapshot_entry_times_between_calls(
|
||||
profiler_snapshot_entry_t *entry);
|
||||
EXPORT profiler_time_entries_t *
|
||||
profiler_snapshot_entry_times_between_calls(profiler_snapshot_entry_t *entry);
|
||||
EXPORT uint64_t profiler_snapshot_entry_expected_time_between_calls(
|
||||
profiler_snapshot_entry_t *entry);
|
||||
profiler_snapshot_entry_t *entry);
|
||||
EXPORT uint64_t profiler_snapshot_entry_min_time_between_calls(
|
||||
profiler_snapshot_entry_t *entry);
|
||||
profiler_snapshot_entry_t *entry);
|
||||
EXPORT uint64_t profiler_snapshot_entry_max_time_between_calls(
|
||||
profiler_snapshot_entry_t *entry);
|
||||
profiler_snapshot_entry_t *entry);
|
||||
EXPORT uint64_t profiler_snapshot_entry_overall_between_calls_count(
|
||||
profiler_snapshot_entry_t *entry);
|
||||
profiler_snapshot_entry_t *entry);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,18 +6,13 @@ struct ScopeProfiler {
|
|||
const char *name;
|
||||
bool enabled = true;
|
||||
|
||||
ScopeProfiler(const char *name)
|
||||
: name(name)
|
||||
{
|
||||
profile_start(name);
|
||||
}
|
||||
ScopeProfiler(const char *name) : name(name) { profile_start(name); }
|
||||
|
||||
~ScopeProfiler() { Stop(); }
|
||||
|
||||
ScopeProfiler(const ScopeProfiler &) = delete;
|
||||
ScopeProfiler(ScopeProfiler &&other)
|
||||
: name(other.name),
|
||||
enabled(other.enabled)
|
||||
: name(other.name), enabled(other.enabled)
|
||||
{
|
||||
other.enabled = false;
|
||||
}
|
||||
|
|
@ -37,7 +32,7 @@ struct ScopeProfiler {
|
|||
|
||||
#ifndef NO_PROFILER_MACROS
|
||||
|
||||
#define ScopeProfiler_NameConcatImpl(x, y) x ## y
|
||||
#define ScopeProfiler_NameConcatImpl(x, y) x##y
|
||||
#define ScopeProfiler_NameConcat(x, y) ScopeProfiler_NameConcatImpl(x, y)
|
||||
|
||||
#ifdef __COUNTER__
|
||||
|
|
@ -46,7 +41,7 @@ struct ScopeProfiler {
|
|||
#define ScopeProfiler_Name(x) ScopeProfiler_NameConcat(x, __LINE__)
|
||||
#endif
|
||||
|
||||
#define ProfileScope(x) ScopeProfiler \
|
||||
ScopeProfiler_Name(SCOPE_PROFILE){x}
|
||||
#define ProfileScope(x) \
|
||||
ScopeProfiler ScopeProfiler_Name(SCOPE_PROFILE) { x }
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -34,26 +34,26 @@ enum serialize_seek_type {
|
|||
};
|
||||
|
||||
struct serializer {
|
||||
void *data;
|
||||
void *data;
|
||||
|
||||
size_t (*read)(void *, void *, size_t);
|
||||
size_t (*write)(void *, const void *, size_t);
|
||||
int64_t (*seek)(void *, int64_t, enum serialize_seek_type);
|
||||
int64_t (*get_pos)(void *);
|
||||
size_t (*read)(void *, void *, size_t);
|
||||
size_t (*write)(void *, const void *, size_t);
|
||||
int64_t (*seek)(void *, int64_t, enum serialize_seek_type);
|
||||
int64_t (*get_pos)(void *);
|
||||
};
|
||||
|
||||
static inline size_t s_read(struct serializer *s, void *data, size_t size)
|
||||
{
|
||||
if (s && s->read && data && size)
|
||||
return s->read(s->data, (void*)data, size);
|
||||
return s->read(s->data, (void *)data, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline size_t s_write(struct serializer *s, const void *data,
|
||||
size_t size)
|
||||
size_t size)
|
||||
{
|
||||
if (s && s->write && data && size)
|
||||
return s->write(s->data, (void*)data, size);
|
||||
return s->write(s->data, (void *)data, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -70,7 +70,7 @@ static inline size_t serialize(struct serializer *s, void *data, size_t len)
|
|||
}
|
||||
|
||||
static inline int64_t serializer_seek(struct serializer *s, int64_t offset,
|
||||
enum serialize_seek_type seek_type)
|
||||
enum serialize_seek_type seek_type)
|
||||
{
|
||||
if (s && s->seek)
|
||||
return s->seek(s->data, offset, seek_type);
|
||||
|
|
@ -99,7 +99,7 @@ static inline void s_wl16(struct serializer *s, uint16_t u16)
|
|||
|
||||
static inline void s_wl24(struct serializer *s, uint32_t u24)
|
||||
{
|
||||
s_w8 (s, (uint8_t)u24);
|
||||
s_w8(s, (uint8_t)u24);
|
||||
s_wl16(s, (uint16_t)(u24 >> 8));
|
||||
}
|
||||
|
||||
|
|
@ -117,12 +117,12 @@ static inline void s_wl64(struct serializer *s, uint64_t u64)
|
|||
|
||||
static inline void s_wlf(struct serializer *s, float f)
|
||||
{
|
||||
s_wl32(s, *(uint32_t*)&f);
|
||||
s_wl32(s, *(uint32_t *)&f);
|
||||
}
|
||||
|
||||
static inline void s_wld(struct serializer *s, double d)
|
||||
{
|
||||
s_wl64(s, *(uint64_t*)&d);
|
||||
s_wl64(s, *(uint64_t *)&d);
|
||||
}
|
||||
|
||||
static inline void s_wb16(struct serializer *s, uint16_t u16)
|
||||
|
|
@ -134,7 +134,7 @@ static inline void s_wb16(struct serializer *s, uint16_t u16)
|
|||
static inline void s_wb24(struct serializer *s, uint32_t u24)
|
||||
{
|
||||
s_wb16(s, (uint16_t)(u24 >> 8));
|
||||
s_w8 (s, (uint8_t)u24);
|
||||
s_w8(s, (uint8_t)u24);
|
||||
}
|
||||
|
||||
static inline void s_wb32(struct serializer *s, uint32_t u32)
|
||||
|
|
@ -151,12 +151,12 @@ static inline void s_wb64(struct serializer *s, uint64_t u64)
|
|||
|
||||
static inline void s_wbf(struct serializer *s, float f)
|
||||
{
|
||||
s_wb32(s, *(uint32_t*)&f);
|
||||
s_wb32(s, *(uint32_t *)&f);
|
||||
}
|
||||
|
||||
static inline void s_wbd(struct serializer *s, double d)
|
||||
{
|
||||
s_wb64(s, *(uint64_t*)&d);
|
||||
s_wb64(s, *(uint64_t *)&d);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ static struct text_node *text_node_bychar(struct text_node *node, char ch)
|
|||
}
|
||||
|
||||
static struct text_node *text_node_byname(struct text_node *node,
|
||||
const char *name)
|
||||
const char *name)
|
||||
{
|
||||
struct text_node *subnode = node->first_subnode;
|
||||
|
||||
|
|
@ -102,8 +102,8 @@ struct text_lookup {
|
|||
struct text_node *top;
|
||||
};
|
||||
|
||||
static void lookup_createsubnode(const char *lookup_val,
|
||||
struct text_leaf *leaf, struct text_node *node)
|
||||
static void lookup_createsubnode(const char *lookup_val, struct text_leaf *leaf,
|
||||
struct text_node *node)
|
||||
{
|
||||
struct text_node *new = bzalloc(sizeof(struct text_node));
|
||||
new->leaf = leaf;
|
||||
|
|
@ -114,11 +114,11 @@ static void lookup_createsubnode(const char *lookup_val,
|
|||
}
|
||||
|
||||
static void lookup_splitnode(const char *lookup_val, size_t len,
|
||||
struct text_leaf *leaf, struct text_node *node)
|
||||
struct text_leaf *leaf, struct text_node *node)
|
||||
{
|
||||
struct text_node *split = bzalloc(sizeof(struct text_node));
|
||||
|
||||
dstr_copy(&split->str, node->str.array+len);
|
||||
dstr_copy(&split->str, node->str.array + len);
|
||||
split->leaf = node->leaf;
|
||||
split->first_subnode = node->first_subnode;
|
||||
node->first_subnode = split;
|
||||
|
|
@ -127,21 +127,21 @@ static void lookup_splitnode(const char *lookup_val, size_t len,
|
|||
|
||||
if (lookup_val[len] != 0) {
|
||||
node->leaf = NULL;
|
||||
lookup_createsubnode(lookup_val+len, leaf, node);
|
||||
lookup_createsubnode(lookup_val + len, leaf, node);
|
||||
} else {
|
||||
node->leaf = leaf;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void lookup_replaceleaf(struct text_node *node,
|
||||
struct text_leaf *leaf)
|
||||
struct text_leaf *leaf)
|
||||
{
|
||||
text_leaf_destroy(node->leaf);
|
||||
node->leaf = leaf;
|
||||
}
|
||||
|
||||
static void lookup_addstring(const char *lookup_val, struct text_leaf *leaf,
|
||||
struct text_node *node)
|
||||
struct text_node *node)
|
||||
{
|
||||
struct text_node *child;
|
||||
|
||||
|
|
@ -157,7 +157,7 @@ static void lookup_addstring(const char *lookup_val, struct text_leaf *leaf,
|
|||
|
||||
for (len = 0; len < child->str.len; len++) {
|
||||
char val1 = child->str.array[len],
|
||||
val2 = lookup_val[len];
|
||||
val2 = lookup_val[len];
|
||||
|
||||
if (val1 >= 'A' && val1 <= 'Z')
|
||||
val1 += 0x20;
|
||||
|
|
@ -169,7 +169,7 @@ static void lookup_addstring(const char *lookup_val, struct text_leaf *leaf,
|
|||
}
|
||||
|
||||
if (len == child->str.len) {
|
||||
lookup_addstring(lookup_val+len, leaf, child);
|
||||
lookup_addstring(lookup_val + len, leaf, child);
|
||||
return;
|
||||
} else {
|
||||
lookup_splitnode(lookup_val, len, leaf, child);
|
||||
|
|
@ -182,7 +182,7 @@ static void lookup_addstring(const char *lookup_val, struct text_leaf *leaf,
|
|||
static void lookup_getstringtoken(struct lexer *lex, struct strref *token)
|
||||
{
|
||||
const char *temp = lex->offset;
|
||||
bool was_backslash = false;
|
||||
bool was_backslash = false;
|
||||
|
||||
while (*temp != 0 && *temp != '\n') {
|
||||
if (!was_backslash) {
|
||||
|
|
@ -205,7 +205,7 @@ static void lookup_getstringtoken(struct lexer *lex, struct strref *token)
|
|||
token->array++;
|
||||
token->len--;
|
||||
|
||||
if (*(temp-1) == '"')
|
||||
if (*(temp - 1) == '"')
|
||||
token->len--;
|
||||
}
|
||||
|
||||
|
|
@ -225,7 +225,7 @@ static bool lookup_gettoken(struct lexer *lex, struct strref *str)
|
|||
if (!str->array) {
|
||||
/* comments are designated with a #, and end at LF */
|
||||
if (ch == '#') {
|
||||
while(ch != '\n' && ch != 0)
|
||||
while (ch != '\n' && ch != 0)
|
||||
ch = *(++lex->offset);
|
||||
} else if (temp.type == BASETOKEN_WHITESPACE) {
|
||||
strref_copy(str, &temp.text);
|
||||
|
|
@ -280,9 +280,9 @@ static inline bool lookup_goto_nextline(struct lexer *p)
|
|||
static char *convert_string(const char *str, size_t len)
|
||||
{
|
||||
struct dstr out;
|
||||
out.array = bstrdup_n(str, len);
|
||||
out.capacity = len+1;
|
||||
out.len = len;
|
||||
out.array = bstrdup_n(str, len);
|
||||
out.capacity = len + 1;
|
||||
out.len = len;
|
||||
|
||||
dstr_replace(&out, "\\n", "\n");
|
||||
dstr_replace(&out, "\\t", "\t");
|
||||
|
|
@ -293,7 +293,7 @@ static char *convert_string(const char *str, size_t len)
|
|||
}
|
||||
|
||||
static void lookup_addfiledata(struct text_lookup *lookup,
|
||||
const char *file_data)
|
||||
const char *file_data)
|
||||
{
|
||||
struct lexer lex;
|
||||
struct strref name, value;
|
||||
|
|
@ -309,7 +309,7 @@ static void lookup_addfiledata(struct text_lookup *lookup,
|
|||
|
||||
if (*name.array == '\n')
|
||||
continue;
|
||||
getval:
|
||||
getval:
|
||||
if (!lookup_gettoken(&lex, &value))
|
||||
break;
|
||||
if (*value.array == '\n')
|
||||
|
|
@ -320,8 +320,8 @@ getval:
|
|||
}
|
||||
|
||||
leaf = bmalloc(sizeof(struct text_leaf));
|
||||
leaf->lookup = bstrdup_n(name.array, name.len);
|
||||
leaf->value = convert_string(value.array, value.len);
|
||||
leaf->lookup = bstrdup_n(name.array, name.len);
|
||||
leaf->value = convert_string(value.array, value.len);
|
||||
|
||||
lookup_addstring(leaf->lookup, leaf, lookup->top);
|
||||
|
||||
|
|
@ -332,8 +332,8 @@ getval:
|
|||
lexer_free(&lex);
|
||||
}
|
||||
|
||||
static inline bool lookup_getstring(const char *lookup_val,
|
||||
const char **out, struct text_node *node)
|
||||
static inline bool lookup_getstring(const char *lookup_val, const char **out,
|
||||
struct text_node *node)
|
||||
{
|
||||
struct text_node *child;
|
||||
char ch;
|
||||
|
|
@ -409,7 +409,7 @@ void text_lookup_destroy(lookup_t *lookup)
|
|||
}
|
||||
|
||||
bool text_lookup_getstr(lookup_t *lookup, const char *lookup_val,
|
||||
const char **out)
|
||||
const char **out)
|
||||
{
|
||||
if (lookup)
|
||||
return lookup_getstring(lookup_val, out, lookup->top);
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ EXPORT lookup_t *text_lookup_create(const char *path);
|
|||
EXPORT bool text_lookup_add(lookup_t *lookup, const char *path);
|
||||
EXPORT void text_lookup_destroy(lookup_t *lookup);
|
||||
EXPORT bool text_lookup_getstr(lookup_t *lookup, const char *lookup_val,
|
||||
const char **out);
|
||||
const char **out);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,9 +35,9 @@
|
|||
|
||||
struct os_event_data {
|
||||
pthread_mutex_t mutex;
|
||||
pthread_cond_t cond;
|
||||
volatile bool signalled;
|
||||
bool manual;
|
||||
pthread_cond_t cond;
|
||||
volatile bool signalled;
|
||||
bool manual;
|
||||
};
|
||||
|
||||
int os_event_init(os_event_t **event, enum os_event_type type)
|
||||
|
|
@ -89,11 +89,10 @@ int os_event_wait(os_event_t *event)
|
|||
return code;
|
||||
}
|
||||
|
||||
static inline void add_ms_to_ts(struct timespec *ts,
|
||||
unsigned long milliseconds)
|
||||
static inline void add_ms_to_ts(struct timespec *ts, unsigned long milliseconds)
|
||||
{
|
||||
ts->tv_sec += milliseconds/1000;
|
||||
ts->tv_nsec += (milliseconds%1000)*1000000;
|
||||
ts->tv_sec += milliseconds / 1000;
|
||||
ts->tv_nsec += (milliseconds % 1000) * 1000000;
|
||||
if (ts->tv_nsec > 1000000000) {
|
||||
ts->tv_sec += 1;
|
||||
ts->tv_nsec -= 1000000000;
|
||||
|
|
@ -109,7 +108,7 @@ int os_event_timedwait(os_event_t *event, unsigned long milliseconds)
|
|||
#if defined(__APPLE__) || defined(__MINGW32__)
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
ts.tv_sec = tv.tv_sec;
|
||||
ts.tv_sec = tv.tv_sec;
|
||||
ts.tv_nsec = tv.tv_usec * 1000;
|
||||
#else
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
|
|
@ -166,13 +165,13 @@ void os_event_reset(os_event_t *event)
|
|||
|
||||
struct os_sem_data {
|
||||
semaphore_t sem;
|
||||
task_t task;
|
||||
task_t task;
|
||||
};
|
||||
|
||||
int os_sem_init(os_sem_t **sem, int value)
|
||||
int os_sem_init(os_sem_t **sem, int value)
|
||||
{
|
||||
semaphore_t new_sem;
|
||||
task_t task = mach_task_self();
|
||||
task_t task = mach_task_self();
|
||||
|
||||
if (semaphore_create(task, &new_sem, 0, value) != KERN_SUCCESS)
|
||||
return -1;
|
||||
|
|
@ -181,7 +180,7 @@ int os_sem_init(os_sem_t **sem, int value)
|
|||
if (!*sem)
|
||||
return -2;
|
||||
|
||||
(*sem)->sem = new_sem;
|
||||
(*sem)->sem = new_sem;
|
||||
(*sem)->task = task;
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -194,15 +193,17 @@ void os_sem_destroy(os_sem_t *sem)
|
|||
}
|
||||
}
|
||||
|
||||
int os_sem_post(os_sem_t *sem)
|
||||
int os_sem_post(os_sem_t *sem)
|
||||
{
|
||||
if (!sem) return -1;
|
||||
if (!sem)
|
||||
return -1;
|
||||
return (semaphore_signal(sem->sem) == KERN_SUCCESS) ? 0 : -1;
|
||||
}
|
||||
|
||||
int os_sem_wait(os_sem_t *sem)
|
||||
int os_sem_wait(os_sem_t *sem)
|
||||
{
|
||||
if (!sem) return -1;
|
||||
if (!sem)
|
||||
return -1;
|
||||
return (semaphore_wait(sem->sem) == KERN_SUCCESS) ? 0 : -1;
|
||||
}
|
||||
|
||||
|
|
@ -212,7 +213,7 @@ struct os_sem_data {
|
|||
sem_t sem;
|
||||
};
|
||||
|
||||
int os_sem_init(os_sem_t **sem, int value)
|
||||
int os_sem_init(os_sem_t **sem, int value)
|
||||
{
|
||||
sem_t new_sem;
|
||||
int ret = sem_init(&new_sem, 0, value);
|
||||
|
|
@ -232,15 +233,17 @@ void os_sem_destroy(os_sem_t *sem)
|
|||
}
|
||||
}
|
||||
|
||||
int os_sem_post(os_sem_t *sem)
|
||||
int os_sem_post(os_sem_t *sem)
|
||||
{
|
||||
if (!sem) return -1;
|
||||
if (!sem)
|
||||
return -1;
|
||||
return sem_post(&sem->sem);
|
||||
}
|
||||
|
||||
int os_sem_wait(os_sem_t *sem)
|
||||
int os_sem_wait(os_sem_t *sem)
|
||||
{
|
||||
if (!sem) return -1;
|
||||
if (!sem)
|
||||
return -1;
|
||||
return sem_wait(&sem->sem);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -36,8 +36,8 @@ static inline long os_atomic_load_long(const volatile long *ptr)
|
|||
return __atomic_load_n(ptr, __ATOMIC_SEQ_CST);
|
||||
}
|
||||
|
||||
static inline bool os_atomic_compare_swap_long(volatile long *val,
|
||||
long old_val, long new_val)
|
||||
static inline bool os_atomic_compare_swap_long(volatile long *val, long old_val,
|
||||
long new_val)
|
||||
{
|
||||
return __sync_bool_compare_and_swap(val, old_val, new_val);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@
|
|||
#define __try
|
||||
#endif
|
||||
#ifndef __except
|
||||
#define __except(x) if (0)
|
||||
#define __except (x) if (0)
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
|
@ -43,7 +43,7 @@ int os_event_init(os_event_t **event, enum os_event_type type)
|
|||
if (!handle)
|
||||
return -1;
|
||||
|
||||
*event = (os_event_t*)handle;
|
||||
*event = (os_event_t *)handle;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -118,13 +118,13 @@ void os_event_reset(os_event_t *event)
|
|||
ResetEvent((HANDLE)event);
|
||||
}
|
||||
|
||||
int os_sem_init(os_sem_t **sem, int value)
|
||||
int os_sem_init(os_sem_t **sem, int value)
|
||||
{
|
||||
HANDLE handle = CreateSemaphore(NULL, (LONG)value, 0x7FFFFFFF, NULL);
|
||||
if (!handle)
|
||||
return -1;
|
||||
|
||||
*sem = (os_sem_t*)handle;
|
||||
*sem = (os_sem_t *)handle;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -134,24 +134,26 @@ void os_sem_destroy(os_sem_t *sem)
|
|||
CloseHandle((HANDLE)sem);
|
||||
}
|
||||
|
||||
int os_sem_post(os_sem_t *sem)
|
||||
int os_sem_post(os_sem_t *sem)
|
||||
{
|
||||
if (!sem) return -1;
|
||||
if (!sem)
|
||||
return -1;
|
||||
return ReleaseSemaphore((HANDLE)sem, 1, NULL) ? 0 : -1;
|
||||
}
|
||||
|
||||
int os_sem_wait(os_sem_t *sem)
|
||||
int os_sem_wait(os_sem_t *sem)
|
||||
{
|
||||
DWORD ret;
|
||||
|
||||
if (!sem) return -1;
|
||||
if (!sem)
|
||||
return -1;
|
||||
ret = WaitForSingleObject((HANDLE)sem, INFINITE);
|
||||
return (ret == WAIT_OBJECT_0) ? 0 : -1;
|
||||
}
|
||||
|
||||
#define VC_EXCEPTION 0x406D1388
|
||||
|
||||
#pragma pack(push,8)
|
||||
#pragma pack(push, 8)
|
||||
struct vs_threadname_info {
|
||||
DWORD type; /* 0x1000 */
|
||||
const char *name;
|
||||
|
|
@ -175,16 +177,18 @@ void os_set_thread_name(const char *name)
|
|||
info.flags = 0;
|
||||
|
||||
#ifdef NO_SEH_MINGW
|
||||
__try1(EXCEPTION_EXECUTE_HANDLER) {
|
||||
__try1(EXCEPTION_EXECUTE_HANDLER)
|
||||
{
|
||||
#else
|
||||
__try {
|
||||
#endif
|
||||
RaiseException(VC_EXCEPTION, 0, THREADNAME_INFO_SIZE,
|
||||
(ULONG_PTR*)&info);
|
||||
(ULONG_PTR *)&info);
|
||||
#ifdef NO_SEH_MINGW
|
||||
} __except1 {
|
||||
}
|
||||
__except1{
|
||||
#else
|
||||
} __except(EXCEPTION_EXECUTE_HANDLER) {
|
||||
} __except (EXCEPTION_EXECUTE_HANDLER) {
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -30,26 +30,26 @@ static inline long os_atomic_dec_long(volatile long *val)
|
|||
|
||||
static inline long os_atomic_set_long(volatile long *ptr, long val)
|
||||
{
|
||||
return (long)_InterlockedExchange((volatile long*)ptr, (long)val);
|
||||
return (long)_InterlockedExchange((volatile long *)ptr, (long)val);
|
||||
}
|
||||
|
||||
static inline long os_atomic_load_long(const volatile long *ptr)
|
||||
{
|
||||
return (long)_InterlockedOr((volatile long*)ptr, 0);
|
||||
return (long)_InterlockedOr((volatile long *)ptr, 0);
|
||||
}
|
||||
|
||||
static inline bool os_atomic_compare_swap_long(volatile long *val,
|
||||
long old_val, long new_val)
|
||||
static inline bool os_atomic_compare_swap_long(volatile long *val, long old_val,
|
||||
long new_val)
|
||||
{
|
||||
return _InterlockedCompareExchange(val, new_val, old_val) == old_val;
|
||||
}
|
||||
|
||||
static inline bool os_atomic_set_bool(volatile bool *ptr, bool val)
|
||||
{
|
||||
return !!_InterlockedExchange8((volatile char*)ptr, (char)val);
|
||||
return !!_InterlockedExchange8((volatile char *)ptr, (char)val);
|
||||
}
|
||||
|
||||
static inline bool os_atomic_load_bool(const volatile bool *ptr)
|
||||
{
|
||||
return !!_InterlockedOr8((volatile char*)ptr, 0);
|
||||
return !!_InterlockedOr8((volatile char *)ptr, 0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,26 +55,26 @@ static inline void pthread_mutex_init_value(pthread_mutex_t *mutex)
|
|||
|
||||
enum os_event_type {
|
||||
OS_EVENT_TYPE_AUTO,
|
||||
OS_EVENT_TYPE_MANUAL
|
||||
OS_EVENT_TYPE_MANUAL,
|
||||
};
|
||||
|
||||
struct os_event_data;
|
||||
struct os_sem_data;
|
||||
typedef struct os_event_data os_event_t;
|
||||
typedef struct os_sem_data os_sem_t;
|
||||
typedef struct os_sem_data os_sem_t;
|
||||
|
||||
EXPORT int os_event_init(os_event_t **event, enum os_event_type type);
|
||||
EXPORT int os_event_init(os_event_t **event, enum os_event_type type);
|
||||
EXPORT void os_event_destroy(os_event_t *event);
|
||||
EXPORT int os_event_wait(os_event_t *event);
|
||||
EXPORT int os_event_timedwait(os_event_t *event, unsigned long milliseconds);
|
||||
EXPORT int os_event_try(os_event_t *event);
|
||||
EXPORT int os_event_signal(os_event_t *event);
|
||||
EXPORT int os_event_wait(os_event_t *event);
|
||||
EXPORT int os_event_timedwait(os_event_t *event, unsigned long milliseconds);
|
||||
EXPORT int os_event_try(os_event_t *event);
|
||||
EXPORT int os_event_signal(os_event_t *event);
|
||||
EXPORT void os_event_reset(os_event_t *event);
|
||||
|
||||
EXPORT int os_sem_init(os_sem_t **sem, int value);
|
||||
EXPORT int os_sem_init(os_sem_t **sem, int value);
|
||||
EXPORT void os_sem_destroy(os_sem_t *sem);
|
||||
EXPORT int os_sem_post(os_sem_t *sem);
|
||||
EXPORT int os_sem_wait(os_sem_t *sem);
|
||||
EXPORT int os_sem_post(os_sem_t *sem);
|
||||
EXPORT int os_sem_wait(os_sem_t *sem);
|
||||
|
||||
EXPORT void os_set_thread_name(const char *name);
|
||||
|
||||
|
|
@ -84,7 +84,6 @@ EXPORT void os_set_thread_name(const char *name);
|
|||
#define THREAD_LOCAL __thread
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -24,12 +24,12 @@
|
|||
|
||||
static inline bool has_utf8_bom(const char *in_char)
|
||||
{
|
||||
uint8_t *in = (uint8_t*)in_char;
|
||||
uint8_t *in = (uint8_t *)in_char;
|
||||
return (in && in[0] == 0xef && in[1] == 0xbb && in[2] == 0xbf);
|
||||
}
|
||||
|
||||
size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out,
|
||||
size_t outsize, int flags)
|
||||
size_t outsize, int flags)
|
||||
{
|
||||
int i_insize = (int)insize;
|
||||
int ret;
|
||||
|
|
@ -52,7 +52,7 @@ size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out,
|
|||
}
|
||||
|
||||
size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out,
|
||||
size_t outsize, int flags)
|
||||
size_t outsize, int flags)
|
||||
{
|
||||
int i_insize = (int)insize;
|
||||
int ret;
|
||||
|
|
@ -61,7 +61,7 @@ size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out,
|
|||
i_insize = (int)wcslen(in);
|
||||
|
||||
ret = WideCharToMultiByte(CP_UTF8, 0, in, i_insize, out, (int)outsize,
|
||||
NULL, NULL);
|
||||
NULL, NULL);
|
||||
|
||||
UNUSED_PARAMETER(flags);
|
||||
return (ret > 0) ? (size_t)ret : 0;
|
||||
|
|
@ -69,14 +69,14 @@ size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out,
|
|||
|
||||
#else
|
||||
|
||||
#define _NXT 0x80
|
||||
#define _SEQ2 0xc0
|
||||
#define _SEQ3 0xe0
|
||||
#define _SEQ4 0xf0
|
||||
#define _SEQ5 0xf8
|
||||
#define _SEQ6 0xfc
|
||||
#define _NXT 0x80
|
||||
#define _SEQ2 0xc0
|
||||
#define _SEQ3 0xe0
|
||||
#define _SEQ4 0xf0
|
||||
#define _SEQ5 0xf8
|
||||
#define _SEQ6 0xfc
|
||||
|
||||
#define _BOM 0xfeff
|
||||
#define _BOM 0xfeff
|
||||
|
||||
static int wchar_forbidden(wchar_t sym);
|
||||
static int utf8_forbidden(unsigned char octet);
|
||||
|
|
@ -131,7 +131,7 @@ static int utf8_forbidden(unsigned char octet)
|
|||
* function.
|
||||
*/
|
||||
size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out,
|
||||
size_t outsize, int flags)
|
||||
size_t outsize, int flags)
|
||||
{
|
||||
unsigned char *p, *lim;
|
||||
wchar_t *wlim, high;
|
||||
|
|
@ -142,21 +142,20 @@ size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out,
|
|||
|
||||
total = 0;
|
||||
p = (unsigned char *)in;
|
||||
lim = (insize != 0) ? (p + insize) : (unsigned char*)-1;
|
||||
lim = (insize != 0) ? (p + insize) : (unsigned char *)-1;
|
||||
wlim = out + outsize;
|
||||
|
||||
for (; p < lim; p += n) {
|
||||
if (!*p)
|
||||
break;
|
||||
|
||||
if (utf8_forbidden(*p) != 0 &&
|
||||
(flags & UTF8_IGNORE_ERROR) == 0)
|
||||
if (utf8_forbidden(*p) != 0 && (flags & UTF8_IGNORE_ERROR) == 0)
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* Get number of bytes for one wide character.
|
||||
*/
|
||||
n = 1; /* default: 1 byte. Used when skipping bytes. */
|
||||
n = 1; /* default: 1 byte. Used when skipping bytes. */
|
||||
if ((*p & 0x80) == 0)
|
||||
high = (wchar_t)*p;
|
||||
else if ((*p & 0xe0) == _SEQ2) {
|
||||
|
|
@ -185,7 +184,7 @@ size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out,
|
|||
if ((flags & UTF8_IGNORE_ERROR) == 0)
|
||||
return 0;
|
||||
n = 1;
|
||||
continue; /* skip */
|
||||
continue; /* skip */
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -201,7 +200,7 @@ size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out,
|
|||
if ((flags & UTF8_IGNORE_ERROR) == 0)
|
||||
return 0;
|
||||
n = 1;
|
||||
continue; /* skip */
|
||||
continue; /* skip */
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -211,19 +210,19 @@ size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out,
|
|||
continue;
|
||||
|
||||
if (out >= wlim)
|
||||
return 0; /* no space left */
|
||||
return 0; /* no space left */
|
||||
|
||||
*out = 0;
|
||||
n_bits = 0;
|
||||
for (i = 1; i < n; i++) {
|
||||
*out |= (wchar_t)(p[n - i] & 0x3f) << n_bits;
|
||||
n_bits += 6; /* 6 low bits in every byte */
|
||||
n_bits += 6; /* 6 low bits in every byte */
|
||||
}
|
||||
*out |= high << n_bits;
|
||||
|
||||
if (wchar_forbidden(*out) != 0) {
|
||||
if ((flags & UTF8_IGNORE_ERROR) == 0)
|
||||
return 0; /* forbidden character */
|
||||
return 0; /* forbidden character */
|
||||
else {
|
||||
total--;
|
||||
out--;
|
||||
|
|
@ -261,7 +260,7 @@ size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out,
|
|||
* as regular symbols.
|
||||
*/
|
||||
size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out,
|
||||
size_t outsize, int flags)
|
||||
size_t outsize, int flags)
|
||||
{
|
||||
wchar_t *w, *wlim, ch = 0;
|
||||
unsigned char *p, *lim, *oc;
|
||||
|
|
@ -271,7 +270,7 @@ size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out,
|
|||
return 0;
|
||||
|
||||
w = (wchar_t *)in;
|
||||
wlim = (insize != 0) ? (w + insize) : (wchar_t*)-1;
|
||||
wlim = (insize != 0) ? (w + insize) : (wchar_t *)-1;
|
||||
p = (unsigned char *)out;
|
||||
lim = p + outsize;
|
||||
total = 0;
|
||||
|
|
@ -313,7 +312,7 @@ size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out,
|
|||
continue;
|
||||
|
||||
if ((size_t)(lim - p) <= n - 1)
|
||||
return 0; /* no space left */
|
||||
return 0; /* no space left */
|
||||
|
||||
ch = *w;
|
||||
oc = (unsigned char *)&ch;
|
||||
|
|
@ -337,7 +336,7 @@ size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out,
|
|||
p[3] = _NXT | (oc[0] & 0x3f);
|
||||
p[2] = _NXT | (oc[0] >> 6) | ((oc[1] & 0x0f) << 2);
|
||||
p[1] = _NXT | ((oc[1] & 0xf0) >> 4) |
|
||||
((oc[2] & 0x03) << 4);
|
||||
((oc[2] & 0x03) << 4);
|
||||
p[0] = _SEQ4 | ((oc[2] & 0x1f) >> 2);
|
||||
break;
|
||||
|
||||
|
|
@ -345,7 +344,7 @@ size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out,
|
|||
p[4] = _NXT | (oc[0] & 0x3f);
|
||||
p[3] = _NXT | (oc[0] >> 6) | ((oc[1] & 0x0f) << 2);
|
||||
p[2] = _NXT | ((oc[1] & 0xf0) >> 4) |
|
||||
((oc[2] & 0x03) << 4);
|
||||
((oc[2] & 0x03) << 4);
|
||||
p[1] = _NXT | (oc[2] >> 2);
|
||||
p[0] = _SEQ5 | (oc[3] & 0x03);
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -25,12 +25,12 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
#define UTF8_IGNORE_ERROR 0x01
|
||||
#define UTF8_SKIP_BOM 0x02
|
||||
#define UTF8_SKIP_BOM 0x02
|
||||
|
||||
size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out,
|
||||
size_t outsize, int flags);
|
||||
size_t outsize, int flags);
|
||||
size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out,
|
||||
size_t outsize, int flags);
|
||||
size_t outsize, int flags);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,31 +30,41 @@
|
|||
template<typename T> class BPtr {
|
||||
T *ptr;
|
||||
|
||||
BPtr(BPtr const&) = delete;
|
||||
BPtr(BPtr const &) = delete;
|
||||
|
||||
BPtr &operator=(BPtr const&) = delete;
|
||||
BPtr &operator=(BPtr const &) = delete;
|
||||
|
||||
public:
|
||||
inline BPtr(T *p=nullptr) : ptr(p) {}
|
||||
inline BPtr(BPtr &&other) : ptr(other.ptr) {other.ptr = nullptr;}
|
||||
inline ~BPtr() {bfree(ptr);}
|
||||
inline BPtr(T *p = nullptr) : ptr(p) {}
|
||||
inline BPtr(BPtr &&other) : ptr(other.ptr) { other.ptr = nullptr; }
|
||||
inline ~BPtr() { bfree(ptr); }
|
||||
|
||||
inline T *operator=(T *p) {bfree(ptr); ptr = p; return p;}
|
||||
inline operator T*() {return ptr;}
|
||||
inline T **operator&() {bfree(ptr); ptr = nullptr; return &ptr;}
|
||||
inline T *operator=(T *p)
|
||||
{
|
||||
bfree(ptr);
|
||||
ptr = p;
|
||||
return p;
|
||||
}
|
||||
inline operator T *() { return ptr; }
|
||||
inline T **operator&()
|
||||
{
|
||||
bfree(ptr);
|
||||
ptr = nullptr;
|
||||
return &ptr;
|
||||
}
|
||||
|
||||
inline bool operator!() {return ptr == NULL;}
|
||||
inline bool operator==(T p) {return ptr == p;}
|
||||
inline bool operator!=(T p) {return ptr != p;}
|
||||
inline bool operator!() { return ptr == NULL; }
|
||||
inline bool operator==(T p) { return ptr == p; }
|
||||
inline bool operator!=(T p) { return ptr != p; }
|
||||
|
||||
inline T *Get() const {return ptr;}
|
||||
inline T *Get() const { return ptr; }
|
||||
};
|
||||
|
||||
class ConfigFile {
|
||||
config_t *config;
|
||||
|
||||
ConfigFile(ConfigFile const&) = delete;
|
||||
ConfigFile &operator=(ConfigFile const&) = delete;
|
||||
ConfigFile(ConfigFile const &) = delete;
|
||||
ConfigFile &operator=(ConfigFile const &) = delete;
|
||||
|
||||
public:
|
||||
inline ConfigFile() : config(NULL) {}
|
||||
|
|
@ -62,10 +72,7 @@ public:
|
|||
{
|
||||
other.config = nullptr;
|
||||
}
|
||||
inline ~ConfigFile()
|
||||
{
|
||||
config_close(config);
|
||||
}
|
||||
inline ~ConfigFile() { config_close(config); }
|
||||
|
||||
inline bool Create(const char *file)
|
||||
{
|
||||
|
|
@ -87,13 +94,10 @@ public:
|
|||
return config_open(&config, file, openType);
|
||||
}
|
||||
|
||||
inline int Save()
|
||||
{
|
||||
return config_save(config);
|
||||
}
|
||||
inline int Save() { return config_save(config); }
|
||||
|
||||
inline int SaveSafe(const char *temp_ext,
|
||||
const char *backup_ext = nullptr)
|
||||
const char *backup_ext = nullptr)
|
||||
{
|
||||
return config_save_safe(config, temp_ext, backup_ext);
|
||||
}
|
||||
|
|
@ -104,32 +108,32 @@ public:
|
|||
config = NULL;
|
||||
}
|
||||
|
||||
inline operator config_t*() const {return config;}
|
||||
inline operator config_t *() const { return config; }
|
||||
};
|
||||
|
||||
class TextLookup {
|
||||
lookup_t *lookup;
|
||||
|
||||
TextLookup(TextLookup const&) = delete;
|
||||
TextLookup(TextLookup const &) = delete;
|
||||
|
||||
TextLookup &operator=(TextLookup const&) = delete;
|
||||
TextLookup &operator=(TextLookup const &) = delete;
|
||||
|
||||
public:
|
||||
inline TextLookup(lookup_t *lookup=nullptr) : lookup(lookup) {}
|
||||
inline TextLookup(lookup_t *lookup = nullptr) : lookup(lookup) {}
|
||||
inline TextLookup(TextLookup &&other) : lookup(other.lookup)
|
||||
{
|
||||
other.lookup = nullptr;
|
||||
}
|
||||
inline ~TextLookup() {text_lookup_destroy(lookup);}
|
||||
inline ~TextLookup() { text_lookup_destroy(lookup); }
|
||||
|
||||
inline TextLookup& operator=(lookup_t *val)
|
||||
inline TextLookup &operator=(lookup_t *val)
|
||||
{
|
||||
text_lookup_destroy(lookup);
|
||||
lookup = val;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline operator lookup_t*() const {return lookup;}
|
||||
inline operator lookup_t *() const { return lookup; }
|
||||
|
||||
inline const char *GetString(const char *lookupVal) const
|
||||
{
|
||||
|
|
|
|||
|
|
@ -45,201 +45,202 @@
|
|||
// 7.8 Format conversion of integer types
|
||||
|
||||
typedef struct {
|
||||
intmax_t quot;
|
||||
intmax_t rem;
|
||||
intmax_t quot;
|
||||
intmax_t rem;
|
||||
} imaxdiv_t;
|
||||
|
||||
// 7.8.1 Macros for format specifiers
|
||||
|
||||
#if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS) // [ See footnote 185 at page 198
|
||||
#if !defined(__cplusplus) || \
|
||||
defined(__STDC_FORMAT_MACROS) // [ See footnote 185 at page 198
|
||||
|
||||
// The fprintf macros for signed integers are:
|
||||
#define PRId8 "d"
|
||||
#define PRIi8 "i"
|
||||
#define PRIdLEAST8 "d"
|
||||
#define PRIiLEAST8 "i"
|
||||
#define PRIdFAST8 "d"
|
||||
#define PRIiFAST8 "i"
|
||||
#define PRId8 "d"
|
||||
#define PRIi8 "i"
|
||||
#define PRIdLEAST8 "d"
|
||||
#define PRIiLEAST8 "i"
|
||||
#define PRIdFAST8 "d"
|
||||
#define PRIiFAST8 "i"
|
||||
|
||||
#define PRId16 "hd"
|
||||
#define PRIi16 "hi"
|
||||
#define PRIdLEAST16 "hd"
|
||||
#define PRIiLEAST16 "hi"
|
||||
#define PRIdFAST16 "hd"
|
||||
#define PRIiFAST16 "hi"
|
||||
#define PRId16 "hd"
|
||||
#define PRIi16 "hi"
|
||||
#define PRIdLEAST16 "hd"
|
||||
#define PRIiLEAST16 "hi"
|
||||
#define PRIdFAST16 "hd"
|
||||
#define PRIiFAST16 "hi"
|
||||
|
||||
#define PRId32 "I32d"
|
||||
#define PRIi32 "I32i"
|
||||
#define PRIdLEAST32 "I32d"
|
||||
#define PRIiLEAST32 "I32i"
|
||||
#define PRIdFAST32 "I32d"
|
||||
#define PRIiFAST32 "I32i"
|
||||
#define PRId32 "I32d"
|
||||
#define PRIi32 "I32i"
|
||||
#define PRIdLEAST32 "I32d"
|
||||
#define PRIiLEAST32 "I32i"
|
||||
#define PRIdFAST32 "I32d"
|
||||
#define PRIiFAST32 "I32i"
|
||||
|
||||
#define PRId64 "I64d"
|
||||
#define PRIi64 "I64i"
|
||||
#define PRIdLEAST64 "I64d"
|
||||
#define PRIiLEAST64 "I64i"
|
||||
#define PRIdFAST64 "I64d"
|
||||
#define PRIiFAST64 "I64i"
|
||||
#define PRId64 "I64d"
|
||||
#define PRIi64 "I64i"
|
||||
#define PRIdLEAST64 "I64d"
|
||||
#define PRIiLEAST64 "I64i"
|
||||
#define PRIdFAST64 "I64d"
|
||||
#define PRIiFAST64 "I64i"
|
||||
|
||||
#define PRIdMAX "I64d"
|
||||
#define PRIiMAX "I64i"
|
||||
#define PRIdMAX "I64d"
|
||||
#define PRIiMAX "I64i"
|
||||
|
||||
#define PRIdPTR "Id"
|
||||
#define PRIiPTR "Ii"
|
||||
#define PRIdPTR "Id"
|
||||
#define PRIiPTR "Ii"
|
||||
|
||||
// The fprintf macros for unsigned integers are:
|
||||
#define PRIo8 "o"
|
||||
#define PRIu8 "u"
|
||||
#define PRIx8 "x"
|
||||
#define PRIX8 "X"
|
||||
#define PRIoLEAST8 "o"
|
||||
#define PRIuLEAST8 "u"
|
||||
#define PRIxLEAST8 "x"
|
||||
#define PRIXLEAST8 "X"
|
||||
#define PRIoFAST8 "o"
|
||||
#define PRIuFAST8 "u"
|
||||
#define PRIxFAST8 "x"
|
||||
#define PRIXFAST8 "X"
|
||||
#define PRIo8 "o"
|
||||
#define PRIu8 "u"
|
||||
#define PRIx8 "x"
|
||||
#define PRIX8 "X"
|
||||
#define PRIoLEAST8 "o"
|
||||
#define PRIuLEAST8 "u"
|
||||
#define PRIxLEAST8 "x"
|
||||
#define PRIXLEAST8 "X"
|
||||
#define PRIoFAST8 "o"
|
||||
#define PRIuFAST8 "u"
|
||||
#define PRIxFAST8 "x"
|
||||
#define PRIXFAST8 "X"
|
||||
|
||||
#define PRIo16 "ho"
|
||||
#define PRIu16 "hu"
|
||||
#define PRIx16 "hx"
|
||||
#define PRIX16 "hX"
|
||||
#define PRIoLEAST16 "ho"
|
||||
#define PRIuLEAST16 "hu"
|
||||
#define PRIxLEAST16 "hx"
|
||||
#define PRIXLEAST16 "hX"
|
||||
#define PRIoFAST16 "ho"
|
||||
#define PRIuFAST16 "hu"
|
||||
#define PRIxFAST16 "hx"
|
||||
#define PRIXFAST16 "hX"
|
||||
#define PRIo16 "ho"
|
||||
#define PRIu16 "hu"
|
||||
#define PRIx16 "hx"
|
||||
#define PRIX16 "hX"
|
||||
#define PRIoLEAST16 "ho"
|
||||
#define PRIuLEAST16 "hu"
|
||||
#define PRIxLEAST16 "hx"
|
||||
#define PRIXLEAST16 "hX"
|
||||
#define PRIoFAST16 "ho"
|
||||
#define PRIuFAST16 "hu"
|
||||
#define PRIxFAST16 "hx"
|
||||
#define PRIXFAST16 "hX"
|
||||
|
||||
#define PRIo32 "I32o"
|
||||
#define PRIu32 "I32u"
|
||||
#define PRIx32 "I32x"
|
||||
#define PRIX32 "I32X"
|
||||
#define PRIoLEAST32 "I32o"
|
||||
#define PRIuLEAST32 "I32u"
|
||||
#define PRIxLEAST32 "I32x"
|
||||
#define PRIXLEAST32 "I32X"
|
||||
#define PRIoFAST32 "I32o"
|
||||
#define PRIuFAST32 "I32u"
|
||||
#define PRIxFAST32 "I32x"
|
||||
#define PRIXFAST32 "I32X"
|
||||
#define PRIo32 "I32o"
|
||||
#define PRIu32 "I32u"
|
||||
#define PRIx32 "I32x"
|
||||
#define PRIX32 "I32X"
|
||||
#define PRIoLEAST32 "I32o"
|
||||
#define PRIuLEAST32 "I32u"
|
||||
#define PRIxLEAST32 "I32x"
|
||||
#define PRIXLEAST32 "I32X"
|
||||
#define PRIoFAST32 "I32o"
|
||||
#define PRIuFAST32 "I32u"
|
||||
#define PRIxFAST32 "I32x"
|
||||
#define PRIXFAST32 "I32X"
|
||||
|
||||
#define PRIo64 "I64o"
|
||||
#define PRIu64 "I64u"
|
||||
#define PRIx64 "I64x"
|
||||
#define PRIX64 "I64X"
|
||||
#define PRIoLEAST64 "I64o"
|
||||
#define PRIuLEAST64 "I64u"
|
||||
#define PRIxLEAST64 "I64x"
|
||||
#define PRIXLEAST64 "I64X"
|
||||
#define PRIoFAST64 "I64o"
|
||||
#define PRIuFAST64 "I64u"
|
||||
#define PRIxFAST64 "I64x"
|
||||
#define PRIXFAST64 "I64X"
|
||||
#define PRIo64 "I64o"
|
||||
#define PRIu64 "I64u"
|
||||
#define PRIx64 "I64x"
|
||||
#define PRIX64 "I64X"
|
||||
#define PRIoLEAST64 "I64o"
|
||||
#define PRIuLEAST64 "I64u"
|
||||
#define PRIxLEAST64 "I64x"
|
||||
#define PRIXLEAST64 "I64X"
|
||||
#define PRIoFAST64 "I64o"
|
||||
#define PRIuFAST64 "I64u"
|
||||
#define PRIxFAST64 "I64x"
|
||||
#define PRIXFAST64 "I64X"
|
||||
|
||||
#define PRIoMAX "I64o"
|
||||
#define PRIuMAX "I64u"
|
||||
#define PRIxMAX "I64x"
|
||||
#define PRIXMAX "I64X"
|
||||
#define PRIoMAX "I64o"
|
||||
#define PRIuMAX "I64u"
|
||||
#define PRIxMAX "I64x"
|
||||
#define PRIXMAX "I64X"
|
||||
|
||||
#define PRIoPTR "Io"
|
||||
#define PRIuPTR "Iu"
|
||||
#define PRIxPTR "Ix"
|
||||
#define PRIXPTR "IX"
|
||||
#define PRIoPTR "Io"
|
||||
#define PRIuPTR "Iu"
|
||||
#define PRIxPTR "Ix"
|
||||
#define PRIXPTR "IX"
|
||||
|
||||
// The fscanf macros for signed integers are:
|
||||
#define SCNd16 "hd"
|
||||
#define SCNi16 "hi"
|
||||
#define SCNdLEAST16 "hd"
|
||||
#define SCNiLEAST16 "hi"
|
||||
#define SCNdFAST16 "hd"
|
||||
#define SCNiFAST16 "hi"
|
||||
#define SCNd16 "hd"
|
||||
#define SCNi16 "hi"
|
||||
#define SCNdLEAST16 "hd"
|
||||
#define SCNiLEAST16 "hi"
|
||||
#define SCNdFAST16 "hd"
|
||||
#define SCNiFAST16 "hi"
|
||||
|
||||
#define SCNd32 "ld"
|
||||
#define SCNi32 "li"
|
||||
#define SCNdLEAST32 "ld"
|
||||
#define SCNiLEAST32 "li"
|
||||
#define SCNdFAST32 "ld"
|
||||
#define SCNiFAST32 "li"
|
||||
#define SCNd32 "ld"
|
||||
#define SCNi32 "li"
|
||||
#define SCNdLEAST32 "ld"
|
||||
#define SCNiLEAST32 "li"
|
||||
#define SCNdFAST32 "ld"
|
||||
#define SCNiFAST32 "li"
|
||||
|
||||
#define SCNd64 "I64d"
|
||||
#define SCNi64 "I64i"
|
||||
#define SCNdLEAST64 "I64d"
|
||||
#define SCNiLEAST64 "I64i"
|
||||
#define SCNdFAST64 "I64d"
|
||||
#define SCNiFAST64 "I64i"
|
||||
#define SCNd64 "I64d"
|
||||
#define SCNi64 "I64i"
|
||||
#define SCNdLEAST64 "I64d"
|
||||
#define SCNiLEAST64 "I64i"
|
||||
#define SCNdFAST64 "I64d"
|
||||
#define SCNiFAST64 "I64i"
|
||||
|
||||
#define SCNdMAX "I64d"
|
||||
#define SCNiMAX "I64i"
|
||||
#define SCNdMAX "I64d"
|
||||
#define SCNiMAX "I64i"
|
||||
|
||||
#ifdef _WIN64 // [
|
||||
# define SCNdPTR "I64d"
|
||||
# define SCNiPTR "I64i"
|
||||
#else // _WIN64 ][
|
||||
# define SCNdPTR "ld"
|
||||
# define SCNiPTR "li"
|
||||
#endif // _WIN64 ]
|
||||
#define SCNdPTR "I64d"
|
||||
#define SCNiPTR "I64i"
|
||||
#else // _WIN64 ][
|
||||
#define SCNdPTR "ld"
|
||||
#define SCNiPTR "li"
|
||||
#endif // _WIN64 ]
|
||||
|
||||
// The fscanf macros for unsigned integers are:
|
||||
#define SCNo16 "ho"
|
||||
#define SCNu16 "hu"
|
||||
#define SCNx16 "hx"
|
||||
#define SCNX16 "hX"
|
||||
#define SCNoLEAST16 "ho"
|
||||
#define SCNuLEAST16 "hu"
|
||||
#define SCNxLEAST16 "hx"
|
||||
#define SCNXLEAST16 "hX"
|
||||
#define SCNoFAST16 "ho"
|
||||
#define SCNuFAST16 "hu"
|
||||
#define SCNxFAST16 "hx"
|
||||
#define SCNXFAST16 "hX"
|
||||
#define SCNo16 "ho"
|
||||
#define SCNu16 "hu"
|
||||
#define SCNx16 "hx"
|
||||
#define SCNX16 "hX"
|
||||
#define SCNoLEAST16 "ho"
|
||||
#define SCNuLEAST16 "hu"
|
||||
#define SCNxLEAST16 "hx"
|
||||
#define SCNXLEAST16 "hX"
|
||||
#define SCNoFAST16 "ho"
|
||||
#define SCNuFAST16 "hu"
|
||||
#define SCNxFAST16 "hx"
|
||||
#define SCNXFAST16 "hX"
|
||||
|
||||
#define SCNo32 "lo"
|
||||
#define SCNu32 "lu"
|
||||
#define SCNx32 "lx"
|
||||
#define SCNX32 "lX"
|
||||
#define SCNoLEAST32 "lo"
|
||||
#define SCNuLEAST32 "lu"
|
||||
#define SCNxLEAST32 "lx"
|
||||
#define SCNXLEAST32 "lX"
|
||||
#define SCNoFAST32 "lo"
|
||||
#define SCNuFAST32 "lu"
|
||||
#define SCNxFAST32 "lx"
|
||||
#define SCNXFAST32 "lX"
|
||||
#define SCNo32 "lo"
|
||||
#define SCNu32 "lu"
|
||||
#define SCNx32 "lx"
|
||||
#define SCNX32 "lX"
|
||||
#define SCNoLEAST32 "lo"
|
||||
#define SCNuLEAST32 "lu"
|
||||
#define SCNxLEAST32 "lx"
|
||||
#define SCNXLEAST32 "lX"
|
||||
#define SCNoFAST32 "lo"
|
||||
#define SCNuFAST32 "lu"
|
||||
#define SCNxFAST32 "lx"
|
||||
#define SCNXFAST32 "lX"
|
||||
|
||||
#define SCNo64 "I64o"
|
||||
#define SCNu64 "I64u"
|
||||
#define SCNx64 "I64x"
|
||||
#define SCNX64 "I64X"
|
||||
#define SCNoLEAST64 "I64o"
|
||||
#define SCNuLEAST64 "I64u"
|
||||
#define SCNxLEAST64 "I64x"
|
||||
#define SCNXLEAST64 "I64X"
|
||||
#define SCNoFAST64 "I64o"
|
||||
#define SCNuFAST64 "I64u"
|
||||
#define SCNxFAST64 "I64x"
|
||||
#define SCNXFAST64 "I64X"
|
||||
#define SCNo64 "I64o"
|
||||
#define SCNu64 "I64u"
|
||||
#define SCNx64 "I64x"
|
||||
#define SCNX64 "I64X"
|
||||
#define SCNoLEAST64 "I64o"
|
||||
#define SCNuLEAST64 "I64u"
|
||||
#define SCNxLEAST64 "I64x"
|
||||
#define SCNXLEAST64 "I64X"
|
||||
#define SCNoFAST64 "I64o"
|
||||
#define SCNuFAST64 "I64u"
|
||||
#define SCNxFAST64 "I64x"
|
||||
#define SCNXFAST64 "I64X"
|
||||
|
||||
#define SCNoMAX "I64o"
|
||||
#define SCNuMAX "I64u"
|
||||
#define SCNxMAX "I64x"
|
||||
#define SCNXMAX "I64X"
|
||||
#define SCNoMAX "I64o"
|
||||
#define SCNuMAX "I64u"
|
||||
#define SCNxMAX "I64x"
|
||||
#define SCNXMAX "I64X"
|
||||
|
||||
#ifdef _WIN64 // [
|
||||
# define SCNoPTR "I64o"
|
||||
# define SCNuPTR "I64u"
|
||||
# define SCNxPTR "I64x"
|
||||
# define SCNXPTR "I64X"
|
||||
#else // _WIN64 ][
|
||||
# define SCNoPTR "lo"
|
||||
# define SCNuPTR "lu"
|
||||
# define SCNxPTR "lx"
|
||||
# define SCNXPTR "lX"
|
||||
#endif // _WIN64 ]
|
||||
#define SCNoPTR "I64o"
|
||||
#define SCNuPTR "I64u"
|
||||
#define SCNxPTR "I64x"
|
||||
#define SCNXPTR "I64X"
|
||||
#else // _WIN64 ][
|
||||
#define SCNoPTR "lo"
|
||||
#define SCNuPTR "lu"
|
||||
#define SCNxPTR "lx"
|
||||
#define SCNXPTR "lX"
|
||||
#endif // _WIN64 ]
|
||||
|
||||
#endif // __STDC_FORMAT_MACROS ]
|
||||
|
||||
|
|
@ -254,23 +255,23 @@ typedef struct {
|
|||
// in %MSVC.NET%\crt\src\div.c
|
||||
#ifdef STATIC_IMAXDIV // [
|
||||
static
|
||||
#else // STATIC_IMAXDIV ][
|
||||
#else // STATIC_IMAXDIV ][
|
||||
_inline
|
||||
#endif // STATIC_IMAXDIV ]
|
||||
imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom)
|
||||
imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom)
|
||||
{
|
||||
imaxdiv_t result;
|
||||
imaxdiv_t result;
|
||||
|
||||
result.quot = numer / denom;
|
||||
result.rem = numer % denom;
|
||||
result.quot = numer / denom;
|
||||
result.rem = numer % denom;
|
||||
|
||||
if (numer < 0 && result.rem > 0) {
|
||||
// did division wrong; must fix up
|
||||
++result.quot;
|
||||
result.rem -= denom;
|
||||
}
|
||||
if (numer < 0 && result.rem > 0) {
|
||||
// did division wrong; must fix up
|
||||
++result.quot;
|
||||
result.rem -= denom;
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 7.8.2.3 The strtoimax and strtoumax functions
|
||||
|
|
@ -281,5 +282,4 @@ imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom)
|
|||
#define wcstoimax _wcstoi64
|
||||
#define wcstoumax _wcstoui64
|
||||
|
||||
|
||||
#endif // _MSC_INTTYPES_H_ ]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#if !defined(__cplusplus)
|
||||
typedef int8_t _Bool;
|
||||
typedef int8_t _Bool;
|
||||
#define bool _Bool
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
|
|
|||
|
|
@ -24,23 +24,23 @@
|
|||
|
||||
/* 7.18.1.1 Exact-width integer types */
|
||||
typedef signed char int8_t;
|
||||
typedef unsigned char uint8_t;
|
||||
typedef short int16_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef int int32_t;
|
||||
typedef unsigned uint32_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef unsigned char uint8_t;
|
||||
typedef short int16_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef int int32_t;
|
||||
typedef unsigned uint32_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
|
||||
/* 7.18.1.2 Minimum-width integer types */
|
||||
typedef signed char int_least8_t;
|
||||
typedef unsigned char uint_least8_t;
|
||||
typedef short int_least16_t;
|
||||
typedef unsigned short uint_least16_t;
|
||||
typedef int int_least32_t;
|
||||
typedef unsigned uint_least32_t;
|
||||
typedef __int64 int_least64_t;
|
||||
typedef unsigned __int64 uint_least64_t;
|
||||
typedef unsigned char uint_least8_t;
|
||||
typedef short int_least16_t;
|
||||
typedef unsigned short uint_least16_t;
|
||||
typedef int int_least32_t;
|
||||
typedef unsigned uint_least32_t;
|
||||
typedef __int64 int_least64_t;
|
||||
typedef unsigned __int64 uint_least64_t;
|
||||
|
||||
/* 7.18.1.3 Fastest minimum-width integer types
|
||||
* Not actually guaranteed to be fastest for all purposes
|
||||
|
|
@ -48,38 +48,38 @@ typedef unsigned __int64 uint_least64_t;
|
|||
*/
|
||||
typedef char int_fast8_t;
|
||||
typedef unsigned char uint_fast8_t;
|
||||
typedef short int_fast16_t;
|
||||
typedef unsigned short uint_fast16_t;
|
||||
typedef int int_fast32_t;
|
||||
typedef unsigned int uint_fast32_t;
|
||||
typedef __int64 int_fast64_t;
|
||||
typedef unsigned __int64 uint_fast64_t;
|
||||
typedef short int_fast16_t;
|
||||
typedef unsigned short uint_fast16_t;
|
||||
typedef int int_fast32_t;
|
||||
typedef unsigned int uint_fast32_t;
|
||||
typedef __int64 int_fast64_t;
|
||||
typedef unsigned __int64 uint_fast64_t;
|
||||
|
||||
/* 7.18.1.4 Integer types capable of holding object pointers */
|
||||
/*typedef int intptr_t;
|
||||
typedef unsigned uintptr_t;*/
|
||||
|
||||
/* 7.18.1.5 Greatest-width integer types */
|
||||
typedef __int64 intmax_t;
|
||||
typedef unsigned __int64 uintmax_t;
|
||||
typedef __int64 intmax_t;
|
||||
typedef unsigned __int64 uintmax_t;
|
||||
|
||||
/* 7.18.2 Limits of specified-width integer types */
|
||||
#if !defined ( __cplusplus) || defined (__STDC_LIMIT_MACROS)
|
||||
#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS)
|
||||
|
||||
/* 7.18.2.1 Limits of exact-width integer types */
|
||||
#define INT8_MIN (-128)
|
||||
#define INT16_MIN (-32768)
|
||||
#define INT32_MIN (-2147483647 - 1)
|
||||
#define INT64_MIN (-9223372036854775807LL - 1)
|
||||
#define INT64_MIN (-9223372036854775807LL - 1)
|
||||
|
||||
#define INT8_MAX 127
|
||||
#define INT16_MAX 32767
|
||||
#define INT32_MAX 2147483647
|
||||
#define INT64_MAX 9223372036854775807LL
|
||||
|
||||
#define UINT8_MAX 0xff /* 255U */
|
||||
#define UINT16_MAX 0xffff /* 65535U */
|
||||
#define UINT32_MAX 0xffffffff /* 4294967295U */
|
||||
#define UINT8_MAX 0xff /* 255U */
|
||||
#define UINT16_MAX 0xffff /* 65535U */
|
||||
#define UINT32_MAX 0xffffffff /* 4294967295U */
|
||||
#define UINT64_MAX 0xffffffffffffffffULL /* 18446744073709551615ULL */
|
||||
|
||||
/* 7.18.2.2 Limits of minimum-width integer types */
|
||||
|
|
@ -152,7 +152,7 @@ typedef unsigned __int64 uintmax_t;
|
|||
#endif
|
||||
|
||||
#if 0
|
||||
#ifndef WCHAR_MIN /* also in wchar.h */
|
||||
#ifndef WCHAR_MIN /* also in wchar.h */
|
||||
#define WCHAR_MIN 0
|
||||
#define WCHAR_MAX ((wchar_t)-1) /* UINT16_MAX */
|
||||
#endif
|
||||
|
|
@ -166,9 +166,8 @@ typedef unsigned __int64 uintmax_t;
|
|||
|
||||
#endif /* !defined ( __cplusplus) || defined __STDC_LIMIT_MACROS */
|
||||
|
||||
|
||||
/* 7.18.4 Macros for integer constants */
|
||||
#if !defined ( __cplusplus) || defined (__STDC_CONSTANT_MACROS)
|
||||
#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS)
|
||||
|
||||
/* 7.18.4.1 Macros for minimum-width integer constants
|
||||
|
||||
|
|
@ -185,20 +184,20 @@ typedef unsigned __int64 uintmax_t;
|
|||
The trick used here is from Clive D W Feather.
|
||||
*/
|
||||
|
||||
#define INT8_C(val) (INT_LEAST8_MAX-INT_LEAST8_MAX+(val))
|
||||
#define INT16_C(val) (INT_LEAST16_MAX-INT_LEAST16_MAX+(val))
|
||||
#define INT32_C(val) (INT_LEAST32_MAX-INT_LEAST32_MAX+(val))
|
||||
#define INT64_C(val) (INT_LEAST64_MAX-INT_LEAST64_MAX+(val))
|
||||
#define INT8_C(val) (INT_LEAST8_MAX - INT_LEAST8_MAX + (val))
|
||||
#define INT16_C(val) (INT_LEAST16_MAX - INT_LEAST16_MAX + (val))
|
||||
#define INT32_C(val) (INT_LEAST32_MAX - INT_LEAST32_MAX + (val))
|
||||
#define INT64_C(val) (INT_LEAST64_MAX - INT_LEAST64_MAX + (val))
|
||||
|
||||
#define UINT8_C(val) (UINT_LEAST8_MAX-UINT_LEAST8_MAX+(val))
|
||||
#define UINT16_C(val) (UINT_LEAST16_MAX-UINT_LEAST16_MAX+(val))
|
||||
#define UINT32_C(val) (UINT_LEAST32_MAX-UINT_LEAST32_MAX+(val))
|
||||
#define UINT64_C(val) (UINT_LEAST64_MAX-UINT_LEAST64_MAX+(val))
|
||||
#define UINT8_C(val) (UINT_LEAST8_MAX - UINT_LEAST8_MAX + (val))
|
||||
#define UINT16_C(val) (UINT_LEAST16_MAX - UINT_LEAST16_MAX + (val))
|
||||
#define UINT32_C(val) (UINT_LEAST32_MAX - UINT_LEAST32_MAX + (val))
|
||||
#define UINT64_C(val) (UINT_LEAST64_MAX - UINT_LEAST64_MAX + (val))
|
||||
|
||||
/* 7.18.4.2 Macros for greatest-width integer constants */
|
||||
#define INTMAX_C(val) (INTMAX_MAX-INTMAX_MAX+(val))
|
||||
#define UINTMAX_C(val) (UINTMAX_MAX-UINTMAX_MAX+(val))
|
||||
#define INTMAX_C(val) (INTMAX_MAX - INTMAX_MAX + (val))
|
||||
#define UINTMAX_C(val) (UINTMAX_MAX - UINTMAX_MAX + (val))
|
||||
|
||||
#endif /* !defined ( __cplusplus) || defined __STDC_CONSTANT_MACROS */
|
||||
#endif /* !defined ( __cplusplus) || defined __STDC_CONSTANT_MACROS */
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -19,25 +19,29 @@
|
|||
template<typename T> class CoTaskMemPtr {
|
||||
T *ptr;
|
||||
|
||||
inline void Clear() {if (ptr) CoTaskMemFree(ptr);}
|
||||
inline void Clear()
|
||||
{
|
||||
if (ptr)
|
||||
CoTaskMemFree(ptr);
|
||||
}
|
||||
|
||||
public:
|
||||
inline CoTaskMemPtr() : ptr(NULL) {}
|
||||
inline CoTaskMemPtr() : ptr(NULL) {}
|
||||
inline CoTaskMemPtr(T *ptr_) : ptr(ptr_) {}
|
||||
inline ~CoTaskMemPtr() {Clear();}
|
||||
inline ~CoTaskMemPtr() { Clear(); }
|
||||
|
||||
inline operator T*() const {return ptr;}
|
||||
inline T *operator->() const {return ptr;}
|
||||
inline operator T *() const { return ptr; }
|
||||
inline T *operator->() const { return ptr; }
|
||||
|
||||
inline const T *Get() const {return ptr;}
|
||||
inline const T *Get() const { return ptr; }
|
||||
|
||||
inline CoTaskMemPtr& operator=(T* val)
|
||||
inline CoTaskMemPtr &operator=(T *val)
|
||||
{
|
||||
Clear();
|
||||
ptr = val;
|
||||
}
|
||||
|
||||
inline T** operator&()
|
||||
inline T **operator&()
|
||||
{
|
||||
Clear();
|
||||
ptr = NULL;
|
||||
|
|
|
|||
|
|
@ -33,18 +33,28 @@ protected:
|
|||
inline void Replace(T *p)
|
||||
{
|
||||
if (ptr != p) {
|
||||
if (p) p->AddRef();
|
||||
if (ptr) ptr->Release();
|
||||
if (p)
|
||||
p->AddRef();
|
||||
if (ptr)
|
||||
ptr->Release();
|
||||
ptr = p;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
inline ComPtr() : ptr(nullptr) {}
|
||||
inline ComPtr(T *p) : ptr(p) {if (ptr) ptr->AddRef();}
|
||||
inline ComPtr(const ComPtr<T> &c) : ptr(c.ptr) {if (ptr) ptr->AddRef();}
|
||||
inline ComPtr(ComPtr<T> &&c) : ptr(c.ptr) {c.ptr = nullptr;}
|
||||
inline ~ComPtr() {Kill();}
|
||||
inline ComPtr() : ptr(nullptr) {}
|
||||
inline ComPtr(T *p) : ptr(p)
|
||||
{
|
||||
if (ptr)
|
||||
ptr->AddRef();
|
||||
}
|
||||
inline ComPtr(const ComPtr<T> &c) : ptr(c.ptr)
|
||||
{
|
||||
if (ptr)
|
||||
ptr->AddRef();
|
||||
}
|
||||
inline ComPtr(ComPtr<T> &&c) : ptr(c.ptr) { c.ptr = nullptr; }
|
||||
inline ~ComPtr() { Kill(); }
|
||||
|
||||
inline void Clear()
|
||||
{
|
||||
|
|
@ -87,7 +97,8 @@ public:
|
|||
inline void CopyTo(T **out)
|
||||
{
|
||||
if (out) {
|
||||
if (ptr) ptr->AddRef();
|
||||
if (ptr)
|
||||
ptr->AddRef();
|
||||
*out = ptr;
|
||||
}
|
||||
}
|
||||
|
|
@ -96,26 +107,35 @@ public:
|
|||
{
|
||||
ULONG ref;
|
||||
|
||||
if (!ptr) return 0;
|
||||
if (!ptr)
|
||||
return 0;
|
||||
ref = ptr->Release();
|
||||
ptr = nullptr;
|
||||
return ref;
|
||||
}
|
||||
|
||||
inline T **Assign() {Clear(); return &ptr;}
|
||||
inline void Set(T *p) {Kill(); ptr = p;}
|
||||
inline T **Assign()
|
||||
{
|
||||
Clear();
|
||||
return &ptr;
|
||||
}
|
||||
inline void Set(T *p)
|
||||
{
|
||||
Kill();
|
||||
ptr = p;
|
||||
}
|
||||
|
||||
inline T *Get() const {return ptr;}
|
||||
inline T *Get() const { return ptr; }
|
||||
|
||||
inline T **operator&() {return Assign();}
|
||||
inline T **operator&() { return Assign(); }
|
||||
|
||||
inline operator T*() const {return ptr;}
|
||||
inline T *operator->() const {return ptr;}
|
||||
inline operator T *() const { return ptr; }
|
||||
inline T *operator->() const { return ptr; }
|
||||
|
||||
inline bool operator==(T *p) const {return ptr == p;}
|
||||
inline bool operator!=(T *p) const {return ptr != p;}
|
||||
inline bool operator==(T *p) const { return ptr == p; }
|
||||
inline bool operator!=(T *p) const { return ptr != p; }
|
||||
|
||||
inline bool operator!() const {return !ptr;}
|
||||
inline bool operator!() const { return !ptr; }
|
||||
};
|
||||
|
||||
#ifdef _WIN32
|
||||
|
|
@ -126,13 +146,13 @@ public:
|
|||
inline ComQIPtr(IUnknown *unk)
|
||||
{
|
||||
this->ptr = nullptr;
|
||||
unk->QueryInterface(__uuidof(T), (void**)&this->ptr);
|
||||
unk->QueryInterface(__uuidof(T), (void **)&this->ptr);
|
||||
}
|
||||
|
||||
inline ComPtr<T> &operator=(IUnknown *unk)
|
||||
{
|
||||
ComPtr<T>::Clear();
|
||||
unk->QueryInterface(__uuidof(T), (void**)&this->ptr);
|
||||
unk->QueryInterface(__uuidof(T), (void **)&this->ptr);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -20,8 +20,5 @@ struct HRError {
|
|||
const char *str;
|
||||
HRESULT hr;
|
||||
|
||||
inline HRError(const char *str, HRESULT hr)
|
||||
: str(str), hr (hr)
|
||||
{
|
||||
}
|
||||
inline HRError(const char *str, HRESULT hr) : str(str), hr(hr) {}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -26,13 +26,13 @@ class WinHandle {
|
|||
}
|
||||
|
||||
public:
|
||||
inline WinHandle() {}
|
||||
inline WinHandle() {}
|
||||
inline WinHandle(HANDLE handle_) : handle(handle_) {}
|
||||
inline ~WinHandle() {Clear();}
|
||||
inline ~WinHandle() { Clear(); }
|
||||
|
||||
inline operator HANDLE() const {return handle;}
|
||||
inline operator HANDLE() const { return handle; }
|
||||
|
||||
inline WinHandle& operator=(HANDLE handle_)
|
||||
inline WinHandle &operator=(HANDLE handle_)
|
||||
{
|
||||
if (handle_ != handle) {
|
||||
Clear();
|
||||
|
|
@ -42,10 +42,7 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
inline HANDLE* operator&()
|
||||
{
|
||||
return &handle;
|
||||
}
|
||||
inline HANDLE *operator&() { return &handle; }
|
||||
|
||||
inline bool Valid() const
|
||||
{
|
||||
|
|
|
|||
|
|
@ -26,12 +26,12 @@ extern "C" {
|
|||
|
||||
struct reg_dword {
|
||||
LSTATUS status;
|
||||
DWORD size;
|
||||
DWORD return_value;
|
||||
DWORD size;
|
||||
DWORD return_value;
|
||||
};
|
||||
|
||||
EXPORT void get_reg_dword(HKEY hkey, LPCWSTR sub_key, LPCWSTR value_name,
|
||||
struct reg_dword *info);
|
||||
struct reg_dword *info);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue