CByteVec
Overview
Module that defines the CByteVec type and the associated functions.
Example
#include "clingo/container/CByteVec.h"
#include "clingo/lang/expect.h"
int main( void )
{
init_tap_c_();
CByteVec* vec = make_byte_vec_c( 1 );
cVecInfo const* info = info_of_byte_vec_c( vec );
expect_c_( vec_is_empty_c( info ) );
expect_c_( info->cap == 1 );
expect_c_( info->count == 0 );
add_to_byte_vec_c( vec, 0xBA );
add_to_byte_vec_c( vec, 0xBE );
expect_c_( not vec_is_empty_c( info ) );
expect_c_( info->cap > 1 );
expect_c_( info->count == 2 );
add_many_to_byte_vec_c_( vec, 0xBE, 0xEF, 0xC0, 0xDE );
expect_c_( info->count == 6 );
expect_c_( *get_from_byte_vec_c( vec, 0 ) == 0xBA );
expect_c_( *get_from_byte_vec_c( vec, 3 ) == 0xEF );
expect_c_( *get_from_byte_vec_c( vec, 5 ) == 0xDE );
*get_var_from_byte_vec_c( vec, 3 ) = 0xBA;
set_on_byte_vec_c( vec, 5, 0x11 );
expect_c_( *get_from_byte_vec_c( vec, 3 ) == 0xBA );
insert_into_byte_vec_c( vec, 5, 0xC0 );
expect_c_( *get_from_byte_vec_c( vec, 6 ) == 0x11 );
expect_c_( info->count == 7 );
cRecorder* rec = &recorder_c_( 128 );
write_byte_vec_c( rec, vec, "X//2" );
expect_c_( recorded_is_c( rec, "BABE BEBA C0C0 11" ) );
resize_byte_vec_c( vec, info->count );
expect_c_( info->cap == info->count );
release_c( vec );
return finish_tap_c_();
}
Types and Definitions
Generated
CByteVec
struct CByteVec;
typedef struct CByteVec CByteVec;
Via the macros VAL_VEC_DEF_C_ and VAL_VEC_IMPL_C_ declared and implemented struct. The macros declare and implement also the following globals and functions.
/* create */
CByteVec* make_byte_vec_c( int64_t cap );
CByteVec* new_byte_vec_c( void );
/* manage */
cByte const* data_of_byte_vec_c( CByteVec const* vec );
cByte* var_data_of_byte_vec_c( CByteVec* vec );
/* resize */
bool resize_byte_vec_c( CByteVec* vec, int64_t cap );
/* info */
cVecInfo const* info_of_byte_vec_c( CByteVec const* vec );
/* use */
bool add_to_byte_vec_c( CByteVec* vec, cByte val );
bool add_array_to_byte_vec_c( CByteVec* vec, int64_t n, cByte const* arr );
cByte const* get_from_byte_vec_c( CByteVec const* vec, int64_t pos );
cByte* get_var_from_byte_vec_c( CByteVec* vec, int64_t pos );
bool insert_into_byte_vec_c( CByteVec* vec, int64_t pos, cByte val );
bool remove_from_byte_vec_c( CByteVec* vec, int64_t pos );
void set_on_byte_vec_c( CByteVec* vec, int64_t pos, cByte val );
Functions
overall
add_many_to_byte_vec_c
#define add_many_to_byte_vec_c_( Vec, ... ) \
add_many_to_byte_vec_c( (Vec), (cBytes)slice_c_( cByte, __VA_ARGS__ ) )
bool add_many_to_byte_vec_c( CByteVec* vec, cBytes many );
Adds all cByte values from the slice to the byte vector.
build_byte_vec_c
CByteVec* build_byte_vec_c( int64_t cap, int64_t count, cByte* data );
Creates a byte vector that can allready have some values. The byte vector takes ownership over data.
byte_vec_bytes_c
cBytes byte_vec_bytes_c( CByteVec const* vec );
Returns the internal data as bytes.
byte_vec_var_bytes_c
cVarBytes byte_vec_var_bytes_c( CByteVec* vec );
Returns the internal data as var bytes.
io
write_byte_vec_c
#define write_byte_vec_c_( Rec, Vec ) \
write_byte_vec_c( (Rec), (Vec), "" )
bool write_byte_vec_c( cRecorder rec[static 1],
CByteVec const* vec,
char const fmt[static 1] );
Writes the bytes from the byte vector in a text format into the recorder. It uses the same format as write_bytes_c.
Example
#include "clingo/container/CByteVec.h"
#include "clingo/lang/expect.h"
TEMP_SLICE_C_(
test,
{
CByteVec* vec;
char const* fmt;
char const* exp;
}
)
#define t_( ... ) ((test){__VA_ARGS__})
int main( void )
{
init_tap_c_();
CByteVec* vec = new_byte_vec_c( 2 );
for ( cByte byte = 0x00; byte < 32; ++byte )
{
require_c_( add_to_byte_vec_c( vec, byte ) );
}
testSlice tests = slice_c_( test,
t_( vec, "", "00 01 02 03 04 05 06 07 08 09 0a 0b "
"0c 0d 0e 0f 10 11 12 13 14 15 16 17\n"
"18 19 1a 1b 1c 1d 1e 1f" ),
t_( vec, "/c", "00 01 02 03 04 05 06 07 08 09 0a 0b "
"0c 0d 0e 0f 10 11 12 13 14 15 16 17 | 24\n"
"18 19 1a 1b 1c 1d 1e 1f | 32" ),
t_( vec, "X/16c/4", "00010203 04050607 08090A0B 0C0D0E0F | 16\n"
"10111213 14151617 18191A1B 1C1D1E1F | 32" ),
t_( vec, "/20/0", "000102030405060708090a0b0c0d0e0f10111213\n"
"1415161718191a1b1c1d1e1f" ),
t_( vec, "//8", "0001020304050607 08090a0b0c0d0e0f 1011121314151617\n"
"18191a1b1c1d1e1f" )
);
for_each_c_( test const*, t, tests )
{
cRecorder* rec = &recorder_c_( 256 );
bool res = write_byte_vec_c( rec, t->vec, t->fmt );
res &= recorded_is_c( rec, t->exp );
tap_descf_c( res, "test format: \"%s\"", t->fmt );
}
release_c( vec );
return finish_tap_c_();
}