Concord - C Discord API library
A Discord API wrapper library written in C
jsmn-find.h File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "chash.h"
Include dependency graph for jsmn-find.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  jsmnftok
 JSON token description. More...
 
struct  jsmnf_pair
 JSON object. More...
 
struct  jsmnf_loader
 Bucket (jsmnf_pair) loader, keeps track of pair array position. More...
 

Macros

#define CHASH_KEY_FIELD   k
 
#define CHASH_VALUE_FIELD   v
 
#define CHASH_BUCKETS_FIELD   fields
 
#define CHASH_LENGTH_FIELD   size
 
#define _jsmnf_key_hash(key, hash)
 
#define _jsmnf_key_compare(cmp_a, cmp_b)
 
#define _JSMNF_TABLE_HEAP   0
 
#define _JSMNF_TABLE_BUCKET   struct jsmnf_pair
 
#define _JSMNF_TABLE_FREE_KEY(_key)
 
#define _JSMNF_TABLE_HASH(_key, _hash)   _jsmnf_key_hash(_key, _hash)
 
#define _JSMNF_TABLE_FREE_VALUE(_value)
 
#define _JSMNF_TABLE_COMPARE(_cmp_a, _cmp_b)   _jsmnf_key_compare(_cmp_a, _cmp_b)
 
#define _JSMNF_TABLE_INIT(_bucket, _key, _value)    chash_default_init(_bucket, _key, _value)
 
#define _JSMNF_STRING_A   js
 
#define _JSMNF_STRING_B   js
 
#define _JSMNF_STRING_A   js
 
#define _JSMNF_STRING_B   key
 
#define RECALLOC_OR_ERROR(ptr, prev_size)
 
#define _JSMNF_UTF16_IS_FIRST_SURROGATE(c)    (0xD800 <= (unsigned)c && (unsigned)c <= 0xDBFF)
 
#define _JSMNF_UTF16_IS_SECOND_SURROGATE(c)    (0xDC00 <= (unsigned)c && (unsigned)c <= 0xDFFF)
 
#define _JSMNF_UTF16_JOIN_SURROGATE(c1, c2)    (((((unsigned long)c1 & 0x3FF) << 10) | ((unsigned)c2 & 0x3FF)) + 0x10000)
 
#define _JSMNF_UTF8_IS_VALID(c)
 
#define _JSMNF_UTF8_IS_TRAIL(c)   (((unsigned char)c & 0xC0) == 0x80)
 
#define _JSMNF_UTF_ILLEGAL   0xFFFFFFFFu
 
#define BUF_PUSH(buf_tok, c, buf_end)
 

Typedefs

typedef struct jsmnf_pair jsmnf_pair
 JSON object. More...
 
typedef struct jsmnf_loader jsmnf_loader
 Bucket (jsmnf_pair) loader, keeps track of pair array position. More...
 

Functions

JSMN_API void jsmnf_init (jsmnf_loader *loader)
 Initialize a jsmnf_loader. More...
 
JSMN_API int jsmnf_load (jsmnf_loader *loader, const char *js, const jsmntok_t tokens[], unsigned num_tokens, jsmnf_pair pairs[], unsigned num_pairs)
 Populate the jsmnf_pair pairs from jsmn tokens. More...
 
JSMN_API jsmnf_pairjsmnf_find (const jsmnf_pair *head, const char *js, const char key[], int length)
 Find a jsmnf_pair token by its associated key. More...
 
JSMN_API jsmnf_pairjsmnf_find_path (const jsmnf_pair *head, const char *js, char *const path[], unsigned depth)
 Find a jsmnf_pair token by its full key path. More...
 
JSMN_API int jsmnf_load_auto (jsmnf_loader *loader, const char *js, const jsmntok_t tokens[], unsigned num_tokens, jsmnf_pair **p_pairs, unsigned *num_pairs)
 Populate and automatically allocate the jsmnf_pair pairs from jsmn tokens. More...
 
JSMN_API int jsmn_parse_auto (jsmn_parser *parser, const char *js, size_t length, jsmntok_t **p_tokens, unsigned *num_tokens)
 jsmn_parse() counterpart that automatically allocates the necessary amount of tokens necessary for parsing the JSON string More...
 
JSMN_API long jsmnf_unescape (char buf[], size_t bufsize, const char src[], size_t length)
 Utility function for unescaping a Unicode string. More...
 
JSMN_API struct jsmnf_pairjsmnf_find (const struct jsmnf_pair *head, const char *js, const char key[], int length)
 
JSMN_API struct jsmnf_pairjsmnf_find_path (const struct jsmnf_pair *head, const char *js, char *const path[], unsigned depth)
 

Macro Definition Documentation

◆ CHASH_KEY_FIELD

#define CHASH_KEY_FIELD   k

◆ CHASH_VALUE_FIELD

#define CHASH_VALUE_FIELD   v

◆ CHASH_BUCKETS_FIELD

#define CHASH_BUCKETS_FIELD   fields

◆ CHASH_LENGTH_FIELD

#define CHASH_LENGTH_FIELD   size

◆ _jsmnf_key_hash

#define _jsmnf_key_hash (   key,
  hash 
)
Value:
5031; \
do { \
unsigned __CHASH_HINDEX; \
for (__CHASH_HINDEX = 0; __CHASH_HINDEX < (key).len; \
++__CHASH_HINDEX) { \
(hash) = (((hash) << 1) + (hash)) \
+ _JSMNF_STRING_B[(key).pos + __CHASH_HINDEX]; \
} \
} while (0)
#define _JSMNF_STRING_B
Definition: jsmn-find.h:352

◆ _jsmnf_key_compare

#define _jsmnf_key_compare (   cmp_a,
  cmp_b 
)
Value:
((cmp_a).len == (cmp_b).len \
&& !strncmp(_JSMNF_STRING_B + (cmp_a).pos, \
_JSMNF_STRING_A + (cmp_b).pos, (cmp_a).len))
#define _JSMNF_STRING_A
Definition: jsmn-find.h:351

◆ _JSMNF_TABLE_HEAP

#define _JSMNF_TABLE_HEAP   0

◆ _JSMNF_TABLE_BUCKET

#define _JSMNF_TABLE_BUCKET   struct jsmnf_pair

◆ _JSMNF_TABLE_FREE_KEY

#define _JSMNF_TABLE_FREE_KEY (   _key)

◆ _JSMNF_TABLE_HASH

#define _JSMNF_TABLE_HASH (   _key,
  _hash 
)    _jsmnf_key_hash(_key, _hash)

◆ _JSMNF_TABLE_FREE_VALUE

#define _JSMNF_TABLE_FREE_VALUE (   _value)

◆ _JSMNF_TABLE_COMPARE

#define _JSMNF_TABLE_COMPARE (   _cmp_a,
  _cmp_b 
)    _jsmnf_key_compare(_cmp_a, _cmp_b)

◆ _JSMNF_TABLE_INIT

#define _JSMNF_TABLE_INIT (   _bucket,
  _key,
  _value 
)     chash_default_init(_bucket, _key, _value)

◆ _JSMNF_STRING_A [1/2]

#define _JSMNF_STRING_A   js

◆ _JSMNF_STRING_B [1/2]

#define _JSMNF_STRING_B   js

◆ _JSMNF_STRING_A [2/2]

#define _JSMNF_STRING_A   js

◆ _JSMNF_STRING_B [2/2]

#define _JSMNF_STRING_B   key

◆ RECALLOC_OR_ERROR

#define RECALLOC_OR_ERROR (   ptr,
  prev_size 
)
Value:
do { \
const unsigned new_size = *prev_size * 2; \
void *tmp = realloc((ptr), new_size * sizeof *(ptr)); \
if (!tmp) return JSMN_ERROR_NOMEM; \
\
*prev_size = new_size; \
memset((ptr) + *(prev_size), 0, \
(new_size - *(prev_size)) * sizeof *(ptr)); \
(ptr) = tmp; \
} while (0)
@ JSMN_ERROR_NOMEM
Definition: jsmn.h:56

◆ _JSMNF_UTF16_IS_FIRST_SURROGATE

#define _JSMNF_UTF16_IS_FIRST_SURROGATE (   c)     (0xD800 <= (unsigned)c && (unsigned)c <= 0xDBFF)

◆ _JSMNF_UTF16_IS_SECOND_SURROGATE

#define _JSMNF_UTF16_IS_SECOND_SURROGATE (   c)     (0xDC00 <= (unsigned)c && (unsigned)c <= 0xDFFF)

◆ _JSMNF_UTF16_JOIN_SURROGATE

#define _JSMNF_UTF16_JOIN_SURROGATE (   c1,
  c2 
)     (((((unsigned long)c1 & 0x3FF) << 10) | ((unsigned)c2 & 0x3FF)) + 0x10000)

◆ _JSMNF_UTF8_IS_VALID

#define _JSMNF_UTF8_IS_VALID (   c)
Value:
(((unsigned long)c <= 0x10FFFF) \
&& ((unsigned long)c < 0xD800 || (unsigned long)c > 0xDFFF))

◆ _JSMNF_UTF8_IS_TRAIL

#define _JSMNF_UTF8_IS_TRAIL (   c)    (((unsigned char)c & 0xC0) == 0x80)

◆ _JSMNF_UTF_ILLEGAL

#define _JSMNF_UTF_ILLEGAL   0xFFFFFFFFu

◆ BUF_PUSH

#define BUF_PUSH (   buf_tok,
  c,
  buf_end 
)
Value:
do { \
if (buf_tok >= buf_end) return JSMN_ERROR_NOMEM; \
*buf_tok++ = c; \
} while (0)

Typedef Documentation

◆ jsmnf_pair

typedef struct jsmnf_pair jsmnf_pair

JSON object.

◆ jsmnf_loader

typedef struct jsmnf_loader jsmnf_loader

Bucket (jsmnf_pair) loader, keeps track of pair array position.

Function Documentation

◆ jsmnf_init()

JSMN_API void jsmnf_init ( jsmnf_loader loader)

Initialize a jsmnf_loader.

Parameters
[out]loaderjsmnf_loader to be initialized

◆ jsmnf_load()

JSMN_API int jsmnf_load ( jsmnf_loader loader,
const char *  js,
const jsmntok_t  tokens[],
unsigned  num_tokens,
jsmnf_pair  pairs[],
unsigned  num_pairs 
)

Populate the jsmnf_pair pairs from jsmn tokens.

Parameters
[in,out]loaderthe jsmnf_loader initialized with jsmnf_init()
[in]jsthe JSON data string
[in]tokensjsmn tokens initialized with jsmn_parse() / jsmn_parse_auto()
[in]num_tokensamount of tokens initialized with jsmn_parse() / jsmn_parse_auto()
[out]pairsjsmnf_pair pairs array
[in]num_pairsmaximum amount of pairs provided
Returns
a enum jsmnerr value for error or the amount of pairs used

◆ jsmnf_find() [1/2]

JSMN_API jsmnf_pair * jsmnf_find ( const jsmnf_pair head,
const char *  js,
const char  key[],
int  length 
)

Find a jsmnf_pair token by its associated key.

Parameters
[in]heada jsmnf_pair object or array loaded at jsmnf_start()
[in]jsthe JSON data string
[in]keythe key too be matched
[in]lengthlength of the key too be matched
Returns
the jsmnf_pair head's field matched to key, or NULL if not encountered

◆ jsmnf_find_path() [1/2]

JSMN_API jsmnf_pair * jsmnf_find_path ( const jsmnf_pair head,
const char *  js,
char *const  path[],
unsigned  depth 
)

Find a jsmnf_pair token by its full key path.

Parameters
[in]heada jsmnf_pair object or array loaded at jsmnf_start()
[in]jsthe JSON data string
[in]pathan array of key path strings, from least to highest depth
[in]depththe depth level of the last path key
Returns
the jsmnf_pair head's field matched to path, or NULL if not encountered

◆ jsmnf_load_auto()

JSMN_API int jsmnf_load_auto ( jsmnf_loader loader,
const char *  js,
const jsmntok_t  tokens[],
unsigned  num_tokens,
jsmnf_pair **  p_pairs,
unsigned *  num_pairs 
)

Populate and automatically allocate the jsmnf_pair pairs from jsmn tokens.

jsmnf_load() counterpart that automatically allocates the necessary amount of pairs necessary for sorting the JSON tokens

Parameters
[in,out]loaderthe jsmnf_loader initialized with jsmnf_init()
[in]jsthe JSON data string
[in]tokensjsmn tokens initialized with jsmn_parse() / jsmn_parse_auto()
[in]num_tokensamount of tokens initialized with jsmn_parse() / jsmn_parse_auto()
[out]p_pairspointer to jsmnf_pair to be dynamically increased
Note
must be free()'d once done being used
Parameters
[in,out]num_pairsinitial amount of pairs provided
Returns
a enum jsmnerr value for error or the amount of pairs used

◆ jsmn_parse_auto()

JSMN_API int jsmn_parse_auto ( jsmn_parser parser,
const char *  js,
size_t  length,
jsmntok_t **  p_tokens,
unsigned *  num_tokens 
)

jsmn_parse() counterpart that automatically allocates the necessary amount of tokens necessary for parsing the JSON string

Parameters
[in,out]parserthe jsmn_parser initialized with jsmn_init()
[in]jsthe JSON data string
[in]lengththe raw JSON string length
[out]p_tokenspointer to jsmntok_t to be dynamically increased
Note
must be free()'d once done being used
Parameters
[in,out]num_tokensamount of tokens
Returns
a enum jsmnerr value for error or the amount of tokens used

◆ jsmnf_unescape()

JSMN_API long jsmnf_unescape ( char  buf[],
size_t  bufsize,
const char  src[],
size_t  length 
)

Utility function for unescaping a Unicode string.

Parameters
[out]bufdestination buffer
[in]bufsizedestination buffer size
[in]srcsource string to be unescaped
[in]lengthsource string length
Returns
length of unescaped string if successful or a negative jsmn error code on failure

◆ jsmnf_find() [2/2]

JSMN_API struct jsmnf_pair * jsmnf_find ( const struct jsmnf_pair head,
const char *  js,
const char  key[],
int  length 
)

◆ jsmnf_find_path() [2/2]

JSMN_API struct jsmnf_pair * jsmnf_find_path ( const struct jsmnf_pair head,
const char *  js,
char *const  path[],
unsigned  depth 
)