Tilde Friends
Utilities

Macros

#define tf_min(a, b)
 
#define tf_max(a, b)
 
#define tf_countof(a)   ((int)(sizeof((a)) / sizeof(*(a))))
 

Typedefs

typedef enum _tf_setting_kind_t tf_setting_kind_t
 
typedef struct uv_loop_s uv_loop_t
 

Enumerations

enum  _tf_setting_kind_t { k_kind_unknown , k_kind_bool , k_kind_int , k_kind_string }
 

Functions

void tf_util_register (JSContext *context)
 
JSValue tf_util_utf8_decode (JSContext *context, JSValue value)
 
uint8_t * tf_util_try_get_array_buffer (JSContext *context, size_t *psize, JSValueConst obj)
 
JSValue tf_util_try_get_typed_array_buffer (JSContext *context, JSValueConst obj, size_t *pbyte_offset, size_t *pbyte_length, size_t *pbytes_per_element)
 
bool tf_util_report_error (JSContext *context, JSValue value)
 
int tf_util_get_length (JSContext *context, JSValue value)
 
int tf_util_insert_index (const void *key, const void *base, size_t count, size_t size, int(*compare)(const void *, const void *))
 
JSValue tf_util_new_uint8_array (JSContext *context, const uint8_t *data, size_t size)
 
size_t tf_base64_encode (const uint8_t *source, size_t source_length, char *out, size_t out_length)
 
size_t tf_base64_decode (const char *source, size_t source_length, uint8_t *out, size_t out_length)
 
int tf_util_backtrace (void **buffer, int count)
 
const char * tf_util_backtrace_to_string (void *const *buffer, int count)
 
const char * tf_util_backtrace_string ()
 
void tf_util_print_backtrace ()
 
const char * tf_util_function_to_string (void *function)
 
bool tf_util_get_default_global_setting_bool (const char *name)
 
int tf_util_get_default_global_setting_int (const char *name)
 
const char * tf_util_get_default_global_setting_string (const char *name)
 
tf_setting_kind_t tf_util_get_global_setting_kind (const char *name)
 
void tf_util_document_settings (const char *line_prefix)
 
bool tf_util_is_mobile ()
 
size_t tf_string_set (char *buffer, size_t size, const char *string)
 

Detailed Description

This is becoming just a dumping ground of small helpers.

Macro Definition Documentation

◆ tf_countof

#define tf_countof (   a)    ((int)(sizeof((a)) / sizeof(*(a))))

Get the number of elements in an array.

Parameters
aThe array.
Returns
The number of array elements.

◆ tf_max

#define tf_max (   a,
 
)
Value:
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _a : _b; \
})

Get the maximum of two values.

Parameters
aThe first value.
bThe second value.
Returns
The maximum of a and b.

◆ tf_min

#define tf_min (   a,
 
)
Value:
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _b : _a; \
})

Get the minimum of two values.

Parameters
aThe first value.
bThe second value.
Returns
The minimum of a and b.

Typedef Documentation

◆ tf_setting_kind_t

Type of a setting.

◆ uv_loop_t

typedef struct uv_loop_s uv_loop_t

An event loop.

Enumeration Type Documentation

◆ _tf_setting_kind_t

Type of a setting.

Function Documentation

◆ tf_base64_decode()

size_t tf_base64_decode ( const char *  source,
size_t  source_length,
uint8_t *  out,
size_t  out_length 
)

Base64-decode data.

Parameters
sourceThe source data.
source_lengthThe length of the source data.
[out]outA buffer to receipve the decoded data.
out_lengthThe size of the buffer to receive decoded data.
Returns
The size of the decoded data.

◆ tf_base64_encode()

size_t tf_base64_encode ( const uint8_t *  source,
size_t  source_length,
char *  out,
size_t  out_length 
)

Base64-encode data.

Parameters
sourceThe source data.
source_lengthThe length of the source data.
[out]outA buffer to receive the encoded data.
out_lengthThe size of the buffer to receive encoded data.
Returns
The size of the encoded data.

◆ tf_string_set()

size_t tf_string_set ( char *  buffer,
size_t  size,
const char *  string 
)

Populate a string buffer, truncating if necessary.

Parameters
bufferThe buffer.
sizeThe size of the buffer.
stringThe value to set.
Returns
The number of bytes set, not including the NULL terminator.

◆ tf_util_backtrace()

int tf_util_backtrace ( void **  buffer,
int  count 
)

Capture a stack backtrace of the calling thread.

Parameters
[out]bufferA buffer with at least count element to receive the backtrace.
countThe size of buffer.
Returns
The numbef of captured frames.

◆ tf_util_backtrace_string()

const char * tf_util_backtrace_string ( )

Capture a stack backtrace of the calling thread and convert it immediately to string.

Returns
A string representation of the stack backtrace with function names, files, and line numbers, as possible. Must be freed with tf_free() by the caller.

◆ tf_util_backtrace_to_string()

const char * tf_util_backtrace_to_string ( void *const *  buffer,
int  count 
)

Convert a stack backtrace to string.

Parameters
bufferA stack backtrace.
countThe number of elements in the backtrace.
Returns
A string representation of the stack backtrace with function names, files, and line numbers, as possible. Must be freed with tf_free() by the caller.

◆ tf_util_document_settings()

void tf_util_document_settings ( const char *  line_prefix)

Log documentation for the available settings.

Parameters
line_prefixText to prefix each line with."

◆ tf_util_function_to_string()

const char * tf_util_function_to_string ( void *  function)

Convert a function pointer to its name, if possible.

Returns
The function name or null.

◆ tf_util_get_default_global_setting_bool()

bool tf_util_get_default_global_setting_bool ( const char *  name)

Get the default value of a global setting as a boolean.

Parameters
nameThe setting name.
Returns
The default value.

◆ tf_util_get_default_global_setting_int()

int tf_util_get_default_global_setting_int ( const char *  name)

Get the default value of a global setting as an integer.

Parameters
nameThe setting name.
Returns
The default value.

◆ tf_util_get_default_global_setting_string()

const char * tf_util_get_default_global_setting_string ( const char *  name)

Get the default value of a global setting as a string.

Parameters
nameThe setting name.
Returns
The default value.

◆ tf_util_get_global_setting_kind()

tf_setting_kind_t tf_util_get_global_setting_kind ( const char *  name)

Get the expected kind of a global setting.

Parameters
nameThe setting name.
Returns
The setting kind or unknown if nonexistent.

◆ tf_util_get_length()

int tf_util_get_length ( JSContext context,
JSValue  value 
)

Get the length of an array.

Parameters
contextThe JS context.
valueAn array with a "length" field.
Returns
The array's length.

◆ tf_util_insert_index()

int tf_util_insert_index ( const void *  key,
const void *  base,
size_t  count,
size_t  size,
int(*)(const void *, const void *)  compare 
)

Get the index at which to insert into an array in order to preserve sorted order.

Parameters
keyThe key being inserted.
baseThe beginning of the array.
countThe number of elements in the array.
sizeThe size of a single element of the array.
compareA comparison function comparing key and an element of the array.
Returns
The index at which to insert key in the array, between 0 and count inclusive.

◆ tf_util_is_mobile()

bool tf_util_is_mobile ( )

Check if the app is running on a mobile device.

Returns
true for iPhone/Android, false otherwise.

◆ tf_util_new_uint8_array()

JSValue tf_util_new_uint8_array ( JSContext context,
const uint8_t *  data,
size_t  size 
)

Create a Uint8Array from bytes.

Parameters
contextThe JS context.
dataThe bytes.
sizeThe number of bytes in data.
Returns
The created array.

◆ tf_util_print_backtrace()

void tf_util_print_backtrace ( )

Print a stack backtrace of the calling thread.

◆ tf_util_register()

void tf_util_register ( JSContext context)

Register utility script functions.

Parameters
contextThe JS context.

◆ tf_util_report_error()

bool tf_util_report_error ( JSContext context,
JSValue  value 
)

Print an error and message the owning task if possible.

Parameters
contextThe JS context.
valueThe value which might be an exception.
Returns
true If the value was an exception and an error was reported.

◆ tf_util_try_get_array_buffer()

uint8_t * tf_util_try_get_array_buffer ( JSContext context,
size_t *  psize,
JSValueConst  obj 
)

Get the data from what might be an ArrayBuffer.

Parameters
contextThe JS context.
[out]psizeThe size of the data in bytes.
objThe object which might be an ArrayBuffer.
Returns
The ArrayBuffer's data.

◆ tf_util_try_get_typed_array_buffer()

JSValue tf_util_try_get_typed_array_buffer ( JSContext context,
JSValueConst  obj,
size_t *  pbyte_offset,
size_t *  pbyte_length,
size_t *  pbytes_per_element 
)

Get the ArrayBuffer from what might be a typed ArrayBuffer.

Parameters
contextThe JS context.
objThe object which might be a typed ArrayBuffer.
[out]pbyte_offsetThe offset into the buffer at which the typed ArrayBuffer starts.
[out]pbyte_lengthThe length of the buffer.
[out]pbytes_per_elementElement size in bytes.
Returns
An ArrayBuffer if obj was a typed ArrayBuffer.

◆ tf_util_utf8_decode()

JSValue tf_util_utf8_decode ( JSContext context,
JSValue  value 
)

Convert UTF-8 bytes in a buffer or Uint8Array or similar to a String.

Parameters
contextThe JS context.
valueThe UTF-8 bytes.
Returns
A string representation of the data interpreted as UTF-8 bytes.