IoTivity-Lite
oc_helpers.h File Reference
#include "oc_export.h"
#include "util/oc_compiler.h"
#include "util/oc_list.h"
#include "util/oc_mmem.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

Macros

#define oc_alloc_string(ocstring, size)   _oc_alloc_string((ocstring), (size))
 allocate oc_string More...
 
#define oc_bool_array(ocboolarray)   (oc_cast(ocboolarray, bool))
 
#define oc_bool_array_size(ocboolarray)   ((ocboolarray).size)
 
#define oc_byte_string_array_add_item(ocstringarray, str, str_len)    (_oc_byte_string_array_add_item(&(ocstringarray), str, str_len))
 
#define oc_byte_string_array_get_allocated_size(ocstringarray)    ((ocstringarray).size / STRING_ARRAY_ITEM_MAX_LEN)
 
#define oc_byte_string_array_get_item(ocstringarray, index)    (oc_string(ocstringarray) + (index) * STRING_ARRAY_ITEM_MAX_LEN + 1)
 
#define oc_byte_string_array_get_item_size(ocstringarray, index)    (*(oc_string(ocstringarray) + (index) * STRING_ARRAY_ITEM_MAX_LEN))
 
#define oc_byte_string_array_set_item(ocstringarray, str, str_len, index)    (_oc_copy_byte_string_to_array(&(ocstringarray), str, str_len, index))
 
#define oc_cast(block, type)   ((type *)(OC_MMEM_PTR(&(block))))
 
#define oc_double_array(ocdoublearray)   (oc_cast(ocdoublearray, double))
 
#define oc_double_array_size(ocdoublearray)   ((ocdoublearray).size)
 
#define oc_free_bool_array(ocarray)   (_oc_free_array(ocarray, BYTE_POOL))
 free array of booleans More...
 
#define oc_free_byte_string_array(ocstringarray)    (_oc_free_string(ocstringarray))
 
#define oc_free_double_array(ocarray)   (_oc_free_array(ocarray, DOUBLE_POOL))
 free array of doubles More...
 
#define oc_free_int_array(ocarray)   (_oc_free_array(ocarray, INT_POOL))
 free array of integers More...
 
#define oc_free_string(ocstring)   _oc_free_string(ocstring)
 free ocstring More...
 
#define oc_free_string_array(ocstringarray)   (_oc_free_string(ocstringarray))
 free oc string array More...
 
#define oc_int_array(ocintarray)   (oc_cast(ocintarray, int64_t))
 
#define oc_int_array_size(ocintarray)   ((ocintarray).size)
 
#define oc_new_bool_array(ocarray, size)    (_oc_new_array(ocarray, size, BYTE_POOL))
 new boolean array More...
 
#define oc_new_byte_string_array(ocstringarray, size)    (_oc_alloc_string_array(ocstringarray, size))
 
#define oc_new_double_array(ocarray, size)    (_oc_new_array(ocarray, size, DOUBLE_POOL))
 new double array More...
 
#define oc_new_int_array(ocarray, size)   (_oc_new_array(ocarray, size, INT_POOL))
 new integer array More...
 
#define oc_new_string(ocstring, str, str_len)    _oc_new_string(ocstring, str, str_len)
 create new string from string (not null terminated) More...
 
#define oc_new_string_array(ocstringarray, size)    (_oc_alloc_string_array(ocstringarray, size))
 new oc string array More...
 
#define oc_string(ocstring)   ((char *)(ocstring).ptr)
 cast oc_string to string More...
 
#define oc_string_array_add_item(ocstringarray, str)    (_oc_string_array_add_item(&(ocstringarray), str))
 
#define oc_string_array_get_allocated_size(ocstringarray)    ((ocstringarray).size / STRING_ARRAY_ITEM_MAX_LEN)
 
#define oc_string_array_get_item(ocstringarray, index)    (oc_string(ocstringarray) + (ptrdiff_t)((index) * STRING_ARRAY_ITEM_MAX_LEN))
 
#define oc_string_array_get_item_size(ocstringarray, index)    (strlen((const char *)oc_string_array_get_item(ocstringarray, index)))
 
#define oc_string_array_set_item(ocstringarray, str, index)    (_oc_copy_string_to_array(&(ocstringarray), str, index))
 
#define oc_string_len(ocstring)   ((ocstring).size != 0 ? (ocstring).size - 1 : 0)
 Get the length of the C-string stored by ocstring. More...
 
#define STRING_ARRAY_ITEM_MAX_LEN   (128)
 

Typedefs

typedef struct oc_mmem oc_array_t
 
typedef struct oc_mmem oc_byte_string_array_t
 
typedef struct oc_mmem oc_handle_t
 
typedef struct oc_mmem oc_string_array_t
 
typedef struct oc_mmem oc_string_t
 

Functions

void _oc_alloc_string (oc_string_t *ocstring, size_t size)
 allocate oc_string More...
 
void _oc_alloc_string_array (oc_string_array_t *ocstringarray, size_t size)
 allocate string array More...
 
bool _oc_byte_string_array_add_item (oc_string_array_t *ocstringarray, const char str[], size_t str_len)
 
bool _oc_copy_byte_string_to_array (oc_string_array_t *ocstringarray, const char str[], size_t str_len, size_t index)
 
bool _oc_copy_string_to_array (oc_string_array_t *ocstringarray, const char str[], size_t index)
 
void _oc_free_array (oc_array_t *ocarray, oc_mmem_pool_t type)
 free array More...
 
void _oc_free_string (oc_string_t *ocstring)
 free oc string More...
 
void _oc_new_array (oc_array_t *ocarray, size_t size, oc_mmem_pool_t type)
 new array More...
 
void _oc_new_string (oc_string_t *ocstring, const char *str, size_t str_len)
 new oc_string from string More...
 
bool _oc_string_array_add_item (oc_string_array_t *ocstringarray, const char str[])
 
void oc_concat_strings (oc_string_t *concat, const char *str1, const char *str2)
 Allocate a new oc_string and concat two non-empty C-strings into it. More...
 
int oc_conv_byte_array_to_hex_string (const uint8_t *array, size_t array_len, char *hex_str, size_t *hex_str_len)
 Conversions between hex encoded strings and byte arrays. More...
 
int oc_conv_hex_string_to_byte_array (const char *hex_str, size_t hex_str_len, uint8_t *array, size_t *array_len)
 convert hex string to byte array More...
 
void oc_copy_string (oc_string_t *dst, const oc_string_t *src)
 copy ocstring More...
 
void oc_join_string_array (oc_string_array_t *ocstringarray, oc_string_t *ocstring)
 Join a string array into a single string using ' ' as a delimiter. More...
 
void oc_set_string (oc_string_t *dst, const char *str, size_t str_len)
 reset ocstring contents More...
 
size_t oc_string_len_unsafe (oc_string_t str)
 Get the length of the non-NULL C-string. More...
 

Macro Definition Documentation

◆ oc_alloc_string

#define oc_alloc_string (   ocstring,
  size 
)    _oc_alloc_string((ocstring), (size))

allocate oc_string

◆ oc_free_bool_array

#define oc_free_bool_array (   ocarray)    (_oc_free_array(ocarray, BYTE_POOL))

free array of booleans

◆ oc_free_double_array

#define oc_free_double_array (   ocarray)    (_oc_free_array(ocarray, DOUBLE_POOL))

free array of doubles

◆ oc_free_int_array

#define oc_free_int_array (   ocarray)    (_oc_free_array(ocarray, INT_POOL))

free array of integers

◆ oc_free_string

#define oc_free_string (   ocstring)    _oc_free_string(ocstring)

free ocstring

◆ oc_free_string_array

#define oc_free_string_array (   ocstringarray)    (_oc_free_string(ocstringarray))

free oc string array

◆ oc_new_bool_array

#define oc_new_bool_array (   ocarray,
  size 
)     (_oc_new_array(ocarray, size, BYTE_POOL))

new boolean array

◆ oc_new_double_array

#define oc_new_double_array (   ocarray,
  size 
)     (_oc_new_array(ocarray, size, DOUBLE_POOL))

new double array

◆ oc_new_int_array

#define oc_new_int_array (   ocarray,
  size 
)    (_oc_new_array(ocarray, size, INT_POOL))

new integer array

◆ oc_new_string

#define oc_new_string (   ocstring,
  str,
  str_len 
)     _oc_new_string(ocstring, str, str_len)

create new string from string (not null terminated)

◆ oc_new_string_array

#define oc_new_string_array (   ocstringarray,
  size 
)     (_oc_alloc_string_array(ocstringarray, size))

new oc string array

◆ oc_string

#define oc_string (   ocstring)    ((char *)(ocstring).ptr)

cast oc_string to string

◆ oc_string_len

#define oc_string_len (   ocstring)    ((ocstring).size != 0 ? (ocstring).size - 1 : 0)

Get the length of the C-string stored by ocstring.

Function Documentation

◆ _oc_alloc_string()

void _oc_alloc_string ( oc_string_t ocstring,
size_t  size 
)

allocate oc_string

Parameters
ocstringocstring to be allocated
sizesize to be allocated

◆ _oc_alloc_string_array()

void _oc_alloc_string_array ( oc_string_array_t ocstringarray,
size_t  size 
)

allocate string array

Parameters
ocstringarrayarray to be allocated
sizethe size of the string array

◆ _oc_free_array()

void _oc_free_array ( oc_array_t ocarray,
oc_mmem_pool_t  type 
)

free array

Parameters
ocarrayocarray to be freed
typepool type

◆ _oc_free_string()

void _oc_free_string ( oc_string_t ocstring)

free oc string

Parameters
ocstringocstring to be freed

◆ _oc_new_array()

void _oc_new_array ( oc_array_t ocarray,
size_t  size,
oc_mmem_pool_t  type 
)

new array

Parameters
ocarrayocarray to be freed
sizesize to be allocated
typepool type

◆ _oc_new_string()

void _oc_new_string ( oc_string_t ocstring,
const char *  str,
size_t  str_len 
)

new oc_string from string

Parameters
ocstringocstring to be allocated
strnot terminated string
str_lensize of the string to be copied

◆ oc_concat_strings()

void oc_concat_strings ( oc_string_t concat,
const char *  str1,
const char *  str2 
)

Allocate a new oc_string and concat two non-empty C-strings into it.

Parameters
[out]concatpointer to output variable
str1first string (cannot be NULL)
str2second string (cannot be NULL)

◆ oc_conv_byte_array_to_hex_string()

int oc_conv_byte_array_to_hex_string ( const uint8_t *  array,
size_t  array_len,
char *  hex_str,
size_t *  hex_str_len 
)

Conversions between hex encoded strings and byte arrays.

convert array to hex

Parameters
[in]arrayarray of bytes (cannot be NULL)
[in]array_lenlength of the array
[out]hex_strdata as hex (cannot be NULL)
[in,out]hex_str_lenin: size of the hex_str array, out: string length of the output hex string (cannot be NULL)
Returns
int 0 success
int -1 on failure

◆ oc_conv_hex_string_to_byte_array()

int oc_conv_hex_string_to_byte_array ( const char *  hex_str,
size_t  hex_str_len,
uint8_t *  array,
size_t *  array_len 
)

convert hex string to byte array

Parameters
[in]hex_strhex string input (cannot be NULL)
[in]hex_str_lensize of the hex string
[out]arrayarray of bytes (cannot be NULL)
[in,out]array_lenin: size of the of the array, out: length of the output array (cannot be NULL)
Returns
int 0 success
int -1 on failure

◆ oc_copy_string()

void oc_copy_string ( oc_string_t dst,
const oc_string_t src 
)

copy ocstring

Parameters
dstdestination (cannot be NULL)
srcsource (if NULL data of destination is freed and the destination is memset to zeroes)

◆ oc_join_string_array()

void oc_join_string_array ( oc_string_array_t ocstringarray,
oc_string_t ocstring 
)

Join a string array into a single string using ' ' as a delimiter.

Parameters
[in]ocstringarraystring array (cannot be NULL)
[out]ocstringoutput string (cannot be NULL), function allocates the oc_string_t, the caller must then deallocate it

◆ oc_set_string()

void oc_set_string ( oc_string_t dst,
const char *  str,
size_t  str_len 
)

reset ocstring contents

Parameters
dstocstring to be reset (cannot be NULL)
strstring which will replace current str (if NULL then the data of ocstring is freed and ocstring is memset to zeroes)
str_lensize of the string

◆ oc_string_len_unsafe()

size_t oc_string_len_unsafe ( oc_string_t  str)

Get the length of the non-NULL C-string.

Warning
This is an unsafe version of oc_string_len, use only if you are sure that the C-string stored by ocstring is not NULL