cRecorder

Overview

Module that defines the cRecorder and functions to write data on a memory block.

Types and Definitions

cRecorder

struct cRecorder
{
   int64_t pos;
   int64_t space;
   void*   mem;
   int     err;
};
typedef struct cRecorder cRecorder;

cRecorder allows to record data on a memory block. The mem pointer points to the next byte that will be set. The pos value shows how much bytes are allready set. The space value tells how much bytes in the memory block are not set. The err value can be set to indicate the type of error that occurred.

Functions

init

heap_recorder_c_

#define heap_recorder_c_( Size )

Allocates a byte array at the heap and assigns the memory to a new created recorder.

make_recorder_c_

#define make_recorder_c_( Size, Memory )

Creates a recorder with the allready allocated memory.

null_recorder_c_

#define null_recorder_c_()

Creates a recorder where all attributes are set to 0 and NULL.

recorder_copy_c_

#define recorder_copy_c_( Rec )

Copies the recorder state without the error value.

recorder_c_

#define recorder_c_( Size )

Allocates a byte array at the stack and assigns the memory to the recorder.

mem

alloc_recorder_mem_c

bool alloc_recorder_mem_c( cRecorder rec[static 1], int64_t size );

Allocates a block of bytes of memory and assigns it to the recorder. If the allocation fails returns the function false, otherwise true.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &null_recorder_c_();

   expect_c_( alloc_recorder_mem_c( rec, 5 ) );
   expect_c_( rec->pos == 0 );
   expect_c_( rec->space == 5 );
   expect_c_( rec->mem != NULL );

   expect_c_( not record_chars_c_( rec, "abcdef" ) ); // to large

   expect_c_( record_chars_c_( rec, "abcde" ) );

   free_recorder_mem_c( rec );

   return finish_tap_c_();
}

realloc_recorder_mem_c

bool realloc_recorder_mem_c( cRecorder rec[static 1], int64_t size );

Changes the block of memory used by the recorder. If the reallocation fails returns the function false, otherwise true.

A shrinked memory moves also the pos value of the recorder to stay in range.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &null_recorder_c_();

   expect_c_( alloc_recorder_mem_c( rec, 3 ) );
   expect_c_( record_chars_c_( rec, "ab" ) );
   expect_c_( not record_chars_c_( rec, "cdef" ) ); // to large


   expect_c_( realloc_recorder_mem_c( rec, 6 ) );
   expect_c_( rec->pos == 2 );
   expect_c_( rec->space == 4 );
   expect_c_( rec->mem != NULL );

   expect_c_( record_chars_c_( rec, "cdef" ) );

   free_recorder_mem_c( rec );

   return finish_tap_c_();
}

free_recorder_mem_c

void free_recorder_mem_c( cRecorder rec[static 1] );

Frees the block of memory that the recorder uses.

recorder_cap_c

int64_t recorder_cap_c( cRecorder const rec[static 1] );

Returns the size of the block of memory that the recorder uses.

move

move_recorder_c

bool move_recorder_c( cRecorder rec[static 1], int64_t offset );

Moves the recorder forward or backward. The function blocks attempts to move the recorder out of range and returns in this cases false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

#define expect_mem_( Chars, Exp )                                              \
   expect_c_( chars_is_c( (Chars), (Exp) ) )

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 10 );
   cChars mem = { rec->space, rec->mem };

   record_pad_c( rec, '_', 10 );
   expect_c_( rec->pos == 10 );
   expect_c_( rec->space == 0 );
   expect_mem_( mem, "__________" );

   expect_c_( move_recorder_c( rec, -8 ) );
   record_chars_c_( rec, "cd" );
   expect_mem_( mem, "__cd______" );
   
   expect_c_( move_recorder_c( rec, 2 ) );
   record_chars_c_( rec, "gh" );
   expect_mem_( mem, "__cd__gh__" );

   expect_c_( move_recorder_c( rec, 1 ) );
   record_chars_c_( rec, "j" );
   expect_mem_( mem, "__cd__gh_j" );

   expect_c_( move_recorder_c( rec, -10 ) );
   record_chars_c_( rec, "a" );
   expect_mem_( mem, "a_cd__gh_j" );

   expect_c_( not move_recorder_c( rec, -20 ) );
   expect_c_( not move_recorder_c( rec, 20 ) );

   return finish_tap_c_();
}

move_recorder_to_c

bool move_recorder_to_c( cRecorder rec[static 1], int64_t pos );

Moves the recorder to position pos in the memory. If the pos value is out of range will the recorder not be moved and the function returns false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

#define expect_mem_( Chars, Exp )                                              \
   expect_c_( chars_is_c( (Chars), (Exp) ) )

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 10 );
   cChars mem = { rec->space, rec->mem };

   record_pad_c( rec, '_', 10 );
   expect_c_( rec->pos == 10 );
   expect_c_( rec->space == 0 );
   expect_mem_( mem, "__________" );

   expect_c_( move_recorder_to_c( rec, 2 ) );
   record_chars_c_( rec, "cd" );
   expect_mem_( mem, "__cd______" );

   expect_c_( move_recorder_to_c( rec, 6 ) );
   record_chars_c_( rec, "gh" );
   expect_mem_( mem, "__cd__gh__" );

   expect_c_( move_recorder_to_c( rec, 9 ) );
   record_chars_c_( rec, "j" );
   expect_mem_( mem, "__cd__gh_j" );

   expect_c_( move_recorder_to_c( rec, 0 ) );
   record_chars_c_( rec, "a" );
   expect_mem_( mem, "a_cd__gh_j" );

   expect_c_( not move_recorder_to_c( rec, 20 ) );

   return finish_tap_c_();
}

reset_recorder_c

void reset_recorder_c( cRecorder rec[static 1] );

Resets the recorder to the beginning of the memory.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 12 );

   record_pad_c( rec, '_', 12 );
   reset_recorder_c( rec );

   record_pad_c( rec, ' ', 10 );
   reset_recorder_c( rec );

   record_pad_c( rec, '+', 9 );
   reset_recorder_c( rec );

   record_pad_c( rec, 'X', 7 );
   reset_recorder_c( rec );

   record_pad_c( rec, '+', 5 );
   reset_recorder_c( rec );

   record_pad_c( rec, ' ', 3 );
   reset_recorder_c( rec );

   record_pad_c( rec, '_', 2 );
   move_recorder_c( rec, 10 );

   expect_c_( recorded_is_c( rec, "__ ++XX++ __" ) );

   return finish_tap_c_();
}

error

set_recorder_error_c

bool set_recorder_error_c( cRecorder rec[static 1], int err );

Sets the err value on the recorder and returns allways false.

access

recorded_bytes_c

cBytes recorded_bytes_c( cRecorder const rec[static 1] );

Util function to access the recoreded block of the memory as byte slice.

recorded_chars_c

cChars recorded_chars_c( cRecorder const rec[static 1] );

Util function to access the recorded block of the memory as chars.

recorded_is_c

bool recorded_is_c( cRecorder const rec[static 1], char const cstr[static 1] );

Util function to check the recorded chars.

recorded_var_bytes_c

cVarBytes recorded_var_bytes_c( cRecorder rec[static 1] );

Util function to access the recorded block of the memory as byte slice with variable elements.

recorded_var_chars_c

cVarChars recorded_var_chars_c( cRecorder rec[static 1] );

Util function to access the recorded block of the memory as chars with variable elements.

record

record_bytes_c

bool record_bytes_c( cRecorder rec[static 1], cBytes slice );

Writes the byte values of the byte slice to the memory of the recorder. Returns true if all byte values can be written, otherwise false.

record_chars_c

#define record_chars_c_( Rec, Cstr )                                           \
   record_chars_c( (Rec), c_c( Cstr ) )
bool record_chars_c( cRecorder rec[static 1], cChars chars );

Writes the char values of the chars to the memory of the recorder. Returns true if all char values can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 6 );

   // --------------------------------------------------------------- record abc
   expect_c_( record_chars_c( rec, c_c( "abc" ) ) );
   expect_c_( rec->pos == 3 );
   expect_c_( rec->space == 3 );

   cChars recorded = recorded_chars_c( rec );
   expect_c_( chars_is_c( recorded, "abc" ) );

   // ------------------------------------------------------- record empty chars
   expect_c_( record_chars_c_( rec, "" ) );
   expect_c_( rec->pos == 3 );
   expect_c_( rec->space == 3 );

   // ---------------------------------------------- try to record to long chars
   cChars defg = c_c( "defg" );
   expect_c_( not record_chars_c( rec, defg ) ); // to large
   expect_c_( rec->pos == 3 );
   expect_c_( rec->space == 3 );

   // --------------------------------------------- fill recorder with sub chars
   cChars subChars = left_c_( cChars, defg, rec->space );
   expect_c_( record_chars_c( rec, subChars ) );
   expect_c_( rec->pos == 6 );
   expect_c_( rec->space == 0 );

   return finish_tap_c_();
}

record_chars_slice_c

#define record_chars_slice_c_( Rec, Slice, Sep )                               \
   record_chars_slice_c( (Rec), (Slice), c_c( Sep ) )
bool record_chars_slice_c( cRecorder rec[static 1],
                           cCharsSlice slice,
                           cChars sep );

Writes the char values of all chars in the slice to the memory of the recorder. Each element will be separated by the chars in the given separator (sep). Returns true if all char values can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cCharsSlice.h"

#define expect_( Rec, Exp )                                                    \
   expect_c_( recorded_is_c( (Rec), (Exp) ) )

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 128 );

   cCharsSlice slice = cs_c_( "abc", "def", "gh", "ij", "klmn", "opq" );

   expect_c_( record_chars_slice_c_( rec, slice, "" ) );
   expect_( rec, "abcdefghijklmnopq" );
   reset_recorder_c( rec );

   expect_c_( record_chars_slice_c_( rec, slice, "-" ) );
   expect_( rec, "abc-def-gh-ij-klmn-opq" );
   reset_recorder_c( rec );

   expect_c_( record_chars_slice_c_( rec, slice, " - " ) );
   expect_( rec, "abc - def - gh - ij - klmn - opq" );
   reset_recorder_c( rec );

   return finish_tap_c_();
}

record_cstr_c

bool record_cstr_c( cRecorder rec[static 1], char const str[static 1] );

Writes all char values of a C-string, including the '\0' at the end, to the memory of the recorder. Returns true if all char values can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

#define expect_mem_( Slice, ... )                                              \
   expect_eq_c_( cmp_bytes_c_( (Slice), __VA_ARGS__ ) )

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 9 );

   // --------------------------------------------------------------- record abc
   expect_c_( record_cstr_c( rec, "abc" ) );
   expect_c_( rec->pos == 4 );
   expect_c_( rec->space == 5 );

   cBytes recorded = recorded_bytes_c( rec );
   expect_mem_( recorded, 'a', 'b', 'c', '\0' );

   // ------------------------------------------------------ record empty string
   expect_c_( record_cstr_c( rec, "" ) );
   expect_c_( rec->pos == 5 );
   expect_c_( rec->space == 4 );

   recorded = recorded_bytes_c( rec );
   expect_mem_( recorded, 'a', 'b', 'c', '\0', '\0' );

   // ----------------------------------------------- try to record to long cstr
   expect_c_( not record_cstr_c( rec, "defg" ) ); // to large
   expect_c_( rec->pos == 5 );
   expect_c_( rec->space == 4 );

   // --------------------------------------------------------------- record def
   expect_c_( record_cstr_c( rec, "def" ) );
   expect_c_( rec->pos == 9 );
   expect_c_( rec->space == 0 );

   recorded = recorded_bytes_c( rec );
   expect_mem_( recorded, 'a', 'b', 'c', '\0', '\0', 'd', 'e', 'f', '\0' );

   return finish_tap_c_();
}

record_endl_c

bool record_endl_c( cRecorder rec[static 1] );

Writes a new-line char to the memory of the recorder. Returns true if the char value can be written, otherwise false.

record_ends_c

bool record_ends_c( cRecorder rec[static 1] );

Writes a null char to the memory of the recorder. Returns true if the char value can be written, otherwise false.

record_mem_c

bool record_mem_c( cRecorder rec[static 1], void const* mem, int64_t len );

Writes with the value len defined number of bytes to the memory of the recorder. Returns true if all bytes can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 4 );

   uint32_t const first = 0x33333333;
   uint32_t const second = 0x77777777;

   uint8_t const* firstPtr = rec->mem;
   expect_c_( record_mem_c( rec, &first, 1 ) );
   expect_c_( *firstPtr == 0x33 );

   uint16_t const* secondPtr = rec->mem;
   expect_c_( record_mem_c( rec, &second, 2 ) );
   expect_c_( *secondPtr == 0x7777 );

   expect_c_( not record_mem_c( rec, "abc", 3 ) ); // to large
   expect_c_( rec->pos == 3 );
   expect_c_( rec->space == 1 );

   return finish_tap_c_();
}

record_pad_c

bool record_pad_c( cRecorder rec[static 1], char c, size_t n );

Repeats the writing of c to the memory of the recorder n times. Returns true if the writing can be executed, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

int main( void )
{
   init_tap_c_();

   // ------------------------------------------------------------ init recorder
   cRecorder* rec = &recorder_c_( 10 );
   expect_c_( rec->pos == 0 );
   expect_c_( rec->space == 10 );
   expect_c_( is_empty_c_( recorded_chars_c( rec ) ) );

   // -------------------------------------------------------------- record pads
   expect_c_( record_pad_c( rec, '-', 3 ) );
   expect_c_( record_pad_c( rec, 'a', 1 ) );
   expect_c_( record_pad_c( rec, 'b', 2 ) );
   expect_c_( record_pad_c( rec, 'x', 0 ) );
   expect_c_( record_pad_c( rec, 'a', 1 ) );

   expect_c_( rec->pos == 7 ); // 3 + 1 + 2 + 1
   expect_c_( rec->space == 3 );

   cChars recorded = recorded_chars_c( rec );
   expect_c_( recorded.s == 7 );
   expect_c_( chars_is_c( recorded, "---abba" ) );

   // ------------------------------------------------ try to record to long pad
   expect_c_( not record_pad_c( rec, '-', 4 ) );

   // ------------------------------------------------------------ fill with pad
   expect_c_( record_pad_c( rec, '-', rec->space ) );

   expect_c_( rec->pos == 10 );
   expect_c_( rec->space == 0 );

   recorded = recorded_chars_c( rec );
   expect_c_( recorded.s == 10 );
   expect_c_( chars_is_c( recorded, "---abba---" ) );

   return finish_tap_c_();
}

recordf_c

bool recordf_c( cRecorder rec[static 1], char const format[static 1], ... );

Writes a custom text(format) to the memory of the recorder. If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.

The format string follows the same specifications as format in printf.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 100 );

   // --------------------------------------------------------------- first step
   {
      expect_c_( recordf_c( rec, "%s\nThe half of %d is %d.", "Example:",
                                                              60,
                                                              60/2 ) );
      expect_c_( recorded_is_c( rec, "Example:\n"
                                     "The half of 60 is 30." ) );
   }

   // -------------------------------------------------------------- second step
   {
      move_recorder_c( rec, -1 ); // to remove the dot
      expect_c_( recordf_c( rec, ", and the half of that is %d.", 60/2/2 ) );
      char const* txt = "Example:\n"
                        "The half of 60 is 30, and the half of that is 15.";
      expect_c_( recorded_is_c( rec, txt ) );
   }

   return finish_tap_c_();
}

record type

record_byte_c

bool record_byte_c( cRecorder rec[static 1], cByte val );

Writes the byte to the memory of the recorder. Returns true if the byte value can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

#define expect_mem_( Slice, ... )                                              \
   expect_eq_c_( cmp_bytes_c_( (Slice), __VA_ARGS__ ) )

int main( void )
{
   init_tap_c_();

   // ------------------------------------------------------------ init recorder
   cRecorder* rec = &recorder_c_( 6 );
   expect_c_( rec->pos == 0 );
   expect_c_( rec->space == 6 );
   expect_c_( is_empty_c_( recorded_bytes_c( rec ) ) );

   // ----------------------------------------------------------- record 3 bytes
   expect_c_( record_byte_c( rec, 0xab ) );
   expect_c_( record_byte_c( rec, 0xcd ) );
   expect_c_( record_byte_c( rec, 0xef ) );

   expect_c_( rec->pos == 3 );
   expect_c_( rec->space == 3 );

   cBytes recorded = recorded_bytes_c( rec );
   expect_c_( recorded.s == 3 );
   expect_mem_( recorded, 0xab, 0xcd, 0xef );

   // --------------------------------------------------------------- fill space
   while ( record_byte_c( rec, 0xcc ) ){}

   recorded = recorded_bytes_c( rec );
   expect_c_( recorded.s == 6 );
   expect_mem_( recorded, 0xab, 0xcd, 0xef, 0xcc, 0xcc, 0xcc );

   return finish_tap_c_();
}

record_char_c

bool record_char_c( cRecorder rec[static 1], char val );

Writes the char to the memory of the recorder. Returns true if the char value can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

int main( void )
{
   init_tap_c_();

   // ------------------------------------------------------------ init recorder
   cRecorder* rec = &recorder_c_( 6 );
   expect_c_( rec->pos == 0 );
   expect_c_( rec->space == 6 );
   expect_c_( is_empty_c_( recorded_chars_c( rec ) ) );

   // ------------------------------------------------------------ record 3 char
   expect_c_( record_char_c( rec, 'a' ) );
   expect_c_( record_char_c( rec, 'b' ) );
   expect_c_( record_char_c( rec, 'c' ) );

   expect_c_( rec->pos == 3 );
   expect_c_( rec->space == 3 );

   cChars recorded = recorded_chars_c( rec );
   expect_c_( recorded.s == 3 );
   expect_c_( chars_is_c( recorded, "abc" ) );

   // ------------------------------------------------------ fill space with '-'
   while ( record_char_c( rec, '-' ) ){}

   expect_c_( rec->pos == 6 );
   expect_c_( rec->space == 0 );

   recorded = recorded_chars_c( rec );
   expect_c_( recorded.s == 6 );
   expect_c_( chars_is_c( recorded, "abc---" ) );

   return finish_tap_c_();
}

record_rune_c

bool record_rune_c( cRecorder rec[static 1], cRune r );

Writes a valid rune to the memory of the recorder. Returns false if the rune is invalid or not enough space exist.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 10 );
   expect_c_( record_rune_c( rec, rune_c( "🥛" ) ) );
   expect_c_( record_rune_c( rec, rune_c( "🔑" ) ) );
   expect_c_( not record_rune_c( rec, rune_c( "📊" ) ) );

   expect_c_( recorded_is_c( rec, "🥛🔑" ) );

   return finish_tap_c_();
}

record_int8_c

bool record_int8_c( cRecorder rec[static 1], int8_t val );

Writes the int8_t to the memory of the recorder. Returns true if the int8_t value can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 2 );

   // ------------------------------------------------------ write int8_t values
   int8_t const* plusVal = rec->mem;
   expect_c_( record_int8_c( rec, 127 ) );
   expect_c_( rec->pos == 1 );
   expect_c_( rec->space == 1 );
   expect_c_( *plusVal == 127 );

   int8_t const* minusVal = rec->mem;
   expect_c_( record_int8_c( rec, -128 ) );
   expect_c_( rec->pos == 2 );
   expect_c_( rec->space == 0 );
   expect_c_( *minusVal == -128 );

   // --------------------------------------- try to record without enough space
   expect_c_( not record_int8_c( rec, 32 ) );
   expect_c_( rec->pos == 2 );
   expect_c_( rec->space == 0 );

   return finish_tap_c_();
}

record_int16_c

bool record_int16_c( cRecorder rec[static 1], int16_t val );

Writes the int16_t to the memory of the recorder. Returns true if all bytes of the int16_t value can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/int16.h" // swap_int16_to_c

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 7 );

   // -------------------------------- value is written in the system byte order
   int16_t const* recVal = rec->mem;
   expect_c_( record_int16_c( rec, 12345 ) );
   expect_c_( rec->pos == 2 );
   expect_c_( rec->space == 5 );
   expect_c_( *recVal == 12345 ); 

   // ----------------------------------------- write with a specific byte order
   cByte const* recBig = rec->mem;
   int16_t const big = swap_int16_to_c( 0x3039, c_BigEndian );
   expect_c_( record_int16_c( rec, big ) );
   expect_c_( recBig[ 0 ] == 0x30 );
   expect_c_( recBig[ 1 ] == 0x39 );

   cByte const* recLittle = rec->mem;
   int16_t const little = swap_int16_to_c( 0x3039, c_LittleEndian );
   expect_c_( record_int16_c( rec, little ) );
   expect_c_( recLittle[ 0 ] == 0x39 );
   expect_c_( recLittle[ 1 ] == 0x30 );

   // --------------------------------------- try to record without enough space
   expect_c_( not record_int16_c( rec, 1 ) );
   expect_c_( rec->pos == 6 );
   expect_c_( rec->space == 1 );

   return finish_tap_c_();
}

record_int32_c

bool record_int32_c( cRecorder rec[static 1], int32_t val );

Writes the int32_t to the memory of the recorder. Returns true if all bytes of the int32_t value can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/int32.h" // swap_int32_to_c

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 13 );

   // -------------------------------- value is written in the system byte order
   int32_t const* recVal = rec->mem;
   expect_c_( record_int32_c( rec, 1234567890 ) );
   expect_c_( rec->pos == 4 );
   expect_c_( rec->space == 9 );
   expect_c_( *recVal == 1234567890 ); 

   // ----------------------------------------- write with a specific byte order
   cByte const* recBig = rec->mem;
   int32_t const big = swap_int32_to_c( 0x499602d2, c_BigEndian );
   expect_c_( record_int32_c( rec, big ) );
   expect_c_( recBig[ 0 ] == 0x49 );
   expect_c_( recBig[ 1 ] == 0x96 );
   expect_c_( recBig[ 2 ] == 0x02 );
   expect_c_( recBig[ 3 ] == 0xd2 );

   cByte const* recLittle = rec->mem;
   int32_t const little = swap_int32_to_c( 0x499602d2, c_LittleEndian );
   expect_c_( record_int32_c( rec, little ) );
   expect_c_( recLittle[ 0 ] == 0xd2 );
   expect_c_( recLittle[ 1 ] == 0x02 );
   expect_c_( recLittle[ 2 ] == 0x96 );
   expect_c_( recLittle[ 3 ] == 0x49 );

   // --------------------------------------- try to record without enough space
   expect_c_( not record_int32_c( rec, 1 ) );
   expect_c_( rec->pos == 12 );
   expect_c_( rec->space == 1 );

   return finish_tap_c_();
}

record_int64_c

bool record_int64_c( cRecorder rec[static 1], int64_t val );

Writes the int64_t to the memory of the recorder. Returns true if all bytes of the int64_t value can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/int64.h" // swap_int64_to_c

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 27 );

   // -------------------------------- value is written in the system byte order
   int64_t const* recVal = rec->mem;
   expect_c_( record_int64_c( rec, 11223344556677 ) );
   expect_c_( rec->pos == 8 );
   expect_c_( rec->space == 19 );
   expect_c_( *recVal == 11223344556677 );
   
   // ----------------------------------------- write with a specific byte order
   cByte const* recBig = rec->mem;
   int64_t const big = swap_int64_to_c( 0x00ab789cd456ef23, c_BigEndian );
   expect_c_( record_int64_c( rec, big ) );
   expect_c_( recBig[ 0 ] == 0x00 );
   expect_c_( recBig[ 1 ] == 0xab );
   expect_c_( recBig[ 2 ] == 0x78 );
   expect_c_( recBig[ 3 ] == 0x9c );
   expect_c_( recBig[ 4 ] == 0xd4 );
   expect_c_( recBig[ 5 ] == 0x56 );
   expect_c_( recBig[ 6 ] == 0xef );
   expect_c_( recBig[ 7 ] == 0x23 );

   cByte const* recLittle = rec->mem;
   int64_t const little = swap_int64_to_c( 0x00ab789cd456ef23, c_LittleEndian );
   expect_c_( record_int64_c( rec, little ) );
   expect_c_( recLittle[ 0 ] == 0x23 );
   expect_c_( recLittle[ 1 ] == 0xef );
   expect_c_( recLittle[ 2 ] == 0x56 );
   expect_c_( recLittle[ 3 ] == 0xd4 );
   expect_c_( recLittle[ 4 ] == 0x9c );
   expect_c_( recLittle[ 5 ] == 0x78 );
   expect_c_( recLittle[ 6 ] == 0xab );
   expect_c_( recLittle[ 7 ] == 0x00 );

   // --------------------------------------- try to record without enough space
   expect_c_( not record_int64_c( rec, 123 ) );
   expect_c_( rec->pos == 24 );
   expect_c_( rec->space == 3 );

   return finish_tap_c_();
}

record_uint8_c

bool record_uint8_c( cRecorder rec[static 1], uint8_t val );

Writes the uint8_t to the memory of the recorder. Returns true if the uint8_t value can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 2 );

   // ------------------------------------------------- ---- write int8_t values
   uint8_t const* val128 = rec->mem;
   expect_c_( record_uint8_c( rec, 128 ) );
   expect_c_( rec->pos == 1 );
   expect_c_( rec->space == 1 );
   expect_c_( *val128 == 128 );

   uint8_t const* val255 = rec->mem;
   expect_c_( record_uint8_c( rec, 255 ) );
   expect_c_( rec->pos == 2 );
   expect_c_( rec->space == 0 );
   expect_c_( *val255 == 255 );

   // --------------------------------------- try to record without enough space
   expect_c_( not record_uint8_c( rec, 32 ) );
   expect_c_( rec->pos == 2 );
   expect_c_( rec->space == 0 );

   return finish_tap_c_();
}

record_uint16_c

bool record_uint16_c( cRecorder rec[static 1], uint16_t val );

Writes the uint16_t to the memory of the recorder. Returns true if all bytes of the uint16_t value can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/uint16.h" // swap_uint16_to_c

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 7 );

   // -------------------------------- value is written in the system byte order
   uint16_t const* recVal = rec->mem;
   expect_c_( record_uint16_c( rec, 25386 ) );
   expect_c_( rec->pos == 2 );
   expect_c_( rec->space == 5 );
   expect_c_( *recVal == 25386 );

   // ----------------------------------------- write with a specific byte order
   cByte const* recBig = rec->mem;
   uint16_t const big = swap_uint16_to_c( 0x632a, c_BigEndian );
   expect_c_( record_uint16_c( rec, big ) );
   expect_c_( recBig[ 0 ] == 0x63 );
   expect_c_( recBig[ 1 ] == 0x2A );

   cByte const* recLittle = rec->mem;
   uint16_t const little = swap_uint16_to_c( 0x632a, c_LittleEndian );
   expect_c_( record_uint16_c( rec, little ) );
   expect_c_( recLittle[ 0 ] == 0x2A );
   expect_c_( recLittle[ 1 ] == 0x63 );

   // --------------------------------------- try tp record without enough space
   expect_c_( not record_uint16_c( rec, 1 ) );
   expect_c_( rec->pos == 6 );
   expect_c_( rec->space == 1 );

   return finish_tap_c_();
}

record_uint32_c

bool record_uint32_c( cRecorder rec[static 1], uint32_t val );

Writes the uint32_t to the memory of the recorder. Returns true if all bytes of the uint32_t value can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/uint32.h" // swap_uint32_to_c

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 13 );

   // -------------------------------- value is written in the system byte order
   uint32_t const* recVal = rec->mem;
   expect_c_( record_uint32_c( rec, 773388221 ) );
   expect_c_( rec->pos == 4 );
   expect_c_( rec->space == 9 );
   expect_c_( *recVal == 773388221 );

   // ----------------------------------------- write with a specific byte order
   cByte const* recBig = rec->mem;
   uint32_t const big = swap_uint32_to_c( 0x2e18f7bd, c_BigEndian );
   expect_c_( record_uint32_c( rec, big ) );
   expect_c_( recBig[ 0 ] == 0x2e );
   expect_c_( recBig[ 1 ] == 0x18 );
   expect_c_( recBig[ 2 ] == 0xf7 );
   expect_c_( recBig[ 3 ] == 0xbd );

   cByte const* recLittle = rec->mem;
   uint32_t const little = swap_uint32_to_c( 0x2e18f7bd, c_LittleEndian );
   expect_c_( record_uint32_c( rec, little ) );
   expect_c_( recLittle[ 0 ] == 0xbd );
   expect_c_( recLittle[ 1 ] == 0xf7 );
   expect_c_( recLittle[ 2 ] == 0x18 );
   expect_c_( recLittle[ 3 ] == 0x2e );

   // --------------------------------------- try to record without enough space
   expect_c_( not record_uint32_c( rec, 1 ) );
   expect_c_( rec->pos == 12 );
   expect_c_( rec->space == 1 );

   return finish_tap_c_();
}

record_uint64_c

bool record_uint64_c( cRecorder rec[static 1], uint64_t val );

Writes the uint64_t to the memory of the recorder. Returns true if all bytes of the uint64_t value can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/uint64.h" // swap_uint64_to_c

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 27 );

   // -------------------------------- value is written in the system byte order
   uint64_t const* recVal = rec->mem;
   expect_c_( record_uint64_c( rec, 7441822896345675318 ) );
   expect_c_( rec->pos == 8 );
   expect_c_( rec->space == 19 );
   expect_c_( *recVal == 7441822896345675318 );

   // ----------------------------------------- write with a specific byte order
   cByte const* recBig = rec->mem;
   uint64_t const big = swap_uint64_to_c( 0x00fe12dc34ba5678, c_BigEndian );
   expect_c_( record_uint64_c( rec, big ) );
   expect_c_( recBig[ 0 ] == 0x00 );
   expect_c_( recBig[ 1 ] == 0xfe );
   expect_c_( recBig[ 2 ] == 0x12 );
   expect_c_( recBig[ 3 ] == 0xdc );
   expect_c_( recBig[ 4 ] == 0x34 );
   expect_c_( recBig[ 5 ] == 0xba );
   expect_c_( recBig[ 6 ] == 0x56 );
   expect_c_( recBig[ 7 ] == 0x78 );

   cByte const* recLittle = rec->mem;
   uint64_t const little = swap_uint64_to_c( 0x00fe12dc34ba5678,
                                             c_LittleEndian);
   expect_c_( record_uint64_c( rec, little ) );
   expect_c_( recLittle[ 0 ] == 0x78 );
   expect_c_( recLittle[ 1 ] == 0x56 );
   expect_c_( recLittle[ 2 ] == 0xba );
   expect_c_( recLittle[ 3 ] == 0x34 );
   expect_c_( recLittle[ 4 ] == 0xdc );
   expect_c_( recLittle[ 5 ] == 0x12 );
   expect_c_( recLittle[ 6 ] == 0xfe );
   expect_c_( recLittle[ 7 ] == 0x00 );

   // --------------------------------------- try to record without enough space
   expect_c_( not record_uint64_c( rec, 123 ) );
   expect_c_( rec->pos == 24 );
   expect_c_( rec->space == 3 );

   return finish_tap_c_();
}

record_float_c

bool record_float_c( cRecorder rec[static 1], float val );

Writes the float to the memory of the recorder. Returns true if all bytes of the float value can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/float.h" // swap_float_to_c

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 13 );

   float const val = 123.456f;

   // -------------------------------- value is written in the system byte order
   float const* recVal = rec->mem;
   expect_c_( record_float_c( rec, val ), 4 );
   expect_c_( rec->pos == 4 );
   expect_c_( rec->space == 9 );
   expect_c_( *recVal == val );

   // ----------------------------------------- write with a specific byte order
   float const bigVal = swap_float_to_c( val, c_BigEndian );
   float const littleVal = swap_float_to_c( val, c_LittleEndian );

   float const* recBig = rec->mem;
   expect_c_( record_float_c( rec, bigVal ) );
   float const* recLittle = rec->mem;
   expect_c_( record_float_c( rec, littleVal ) );

   expect_c_( *recBig != *recLittle );

   // --------------------------------------- try to record without enough space
   expect_c_( not record_float_c( rec, 4.5f ) );
   expect_c_( rec->pos == 12 );
   expect_c_( rec->space == 1 );

   return finish_tap_c_();
}

record_double_c

bool record_double_c( cRecorder rec[static 1], double val );

Writes the double to the memory of the recorder. Returns true if all bytes of the double value can be written, otherwise false.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/double.h" // swap_double_to_c

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 27 );

   double const val = 209348.034;

   // -------------------------------- value is written in the system byte order
   double const* recVal = rec->mem;
   expect_true_c_( record_double_c( rec, val ) );
   expect_equal_c_( rec->pos, 8 );
   expect_equal_c_( rec->space, 19 );
   expect_equal_c_( *recVal, val );

   // ----------------------------------------- write with a specific byte order
   double const bigVal = swap_double_to_c( val, c_BigEndian );
   double const littleVal = swap_double_to_c( val, c_LittleEndian );

   double const* recBig = rec->mem;
   expect_true_c_( record_double_c( rec, bigVal ) );
   double const* recLittle = rec->mem;
   expect_true_c_( record_double_c( rec, littleVal ) );

   expect_not_equal_c_( *recBig, *recLittle );

   // --------------------------------------- try to record without enough space
   expect_false_c_( record_double_c( rec, 1.4 ) );
   expect_equal_c_( rec->pos, 24 );
   expect_equal_c_( rec->space, 3 );

   return finish_tap_c_();
}

util

void print_recorded_c( cRecorder rec[static 1] );

Prints the recorded bytes as characters to stdout.

#define print_scope_c_( Rec, Size )

Macro to define a for statement that defines a recorder instance and to loops one time. The recorded bytes will be printed as characters to stdout.

println_recorded_c

void println_recorded_c( cRecorder rec[static 1] );

Prints the recoreded bytes as characters to stdout and appends '\n' at the end.

println_scope_c_

#define println_scope_c_( Rec, Size )

Macro to define a for statement that defines a recorder instance and to loops one time. The recorded bytes will be printed as characters to stdout and appends '\n' at the end.

turn_into_cstr_c

char* turn_into_cstr_c( cRecorder rec[static 1] );

Records if necessary a null character, moves the recorder to the beginning and returns the recorded memory as C string. Returns NULL if the function can not finish the memory with a null character.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"

int main( void )
{
   init_tap_c_();

   cRecorder* rec = NULL;
   char const* cstr = "";

   rec = &null_recorder_c_();
   cstr = turn_into_cstr_c( rec );
   expect_c_( cstr == NULL );

   rec = &recorder_c_( 4 );
   cstr = turn_into_cstr_c( rec );
   expect_eq_c_( strcmp( cstr, "" ) );

   record_chars_c_( rec, "ab" );
   cstr = turn_into_cstr_c( rec );
   expect_eq_c_( strcmp( cstr, "ab" ) );

   record_cstr_c( rec, "abc" );
   cstr = turn_into_cstr_c( rec );
   expect_eq_c_( strcmp( cstr, "abc" ) );

   record_chars_c_( rec, "" );
   cstr = turn_into_cstr_c( rec );
   expect_eq_c_( strcmp( cstr, "" ) );

   record_chars_c_( rec, "abcd" );
   cstr = turn_into_cstr_c( rec );
   expect_c_( cstr == NULL );

   return finish_tap_c_();
}