cCell

Overview

Module with functions and types to define and write text sectors.

Types and Definitions

cCell

struct cCell
{
   int16_t size;
   int8_t orient;
   char pad;
};
typedef struct cCell cCell;

A cCell defines how a sector should be written.

Functions

cell_c_

#define cell_c_( Size, Orient, Pad )

Creates a cell with defined values.

read_in_cell_c

bool read_in_cell_c( cScanner sca[static 1], cCell cell[static 1] );

Initialises a cell from a text representation that will be read with the scanner. The text representation of a cell has the following syntax:

([size][orient][pad])

All three values are mandatory. The size value must be between 1 and 32767. The orient value can be 'l', 'c' and 'r' and represent left, center and right text orientation. The final pad value is the char that will be used to fill up the cell.

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

TEMP_SLICE_DEF_C_(
   test,
   {
      char const* str;
      cCell exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "(8c )", cell_c_( 8, 0, ' ' ) ),
      t_( "(16l-)", cell_c_( 16, -1, '-' ) ),
      t_( "(256r.)", cell_c_( 256, 1, '.' ) )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cScanner *sca = &cstr_scanner_c_( t.str );
      cCell cell;

      bool res = read_in_cell_c( sca, &cell );
      res &= cell.orient == t.exp.orient;
      res &= cell.size == t.exp.size;
      res &= cell.pad == t.exp.pad;

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

write_ascii_into_cell_c

bool write_ascii_into_cell_c( cRecorder rec[static 1],
                              cChars ascii,
                              cCell const cell[static 1] );

Writes the characters into a cell. A to long ascii text will be reduced to the cell size.

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

TEMP_SLICE_DEF_C_(
   test,
   {
      cCell cell;
      char const* ascii;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( cell_c_( 6, -1, '.' ), "abc", "abc..." ),
      t_( cell_c_( 6, 0, '_' ), "cde", "__cde_" ),
      t_( cell_c_( 6, 1, ' ' ), "def", "   def" ),
      t_( cell_c_( 4, 1, '+' ), "abcdef", "abcd" )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_ascii_into_cell_c( rec, c_c( t.ascii ), &t.cell );
      res &= rec->pos == t.cell.size;
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}