Previous: , Up: pathname simple   [Contents][Index]


8.3.5 Simple pathname: usage examples

Let’s see how we can build a standalone instance of ccstructs_pathname_t and use it through the trait ccstructs_pathname_T:

char const *                  P = "/path/to/file.ext";
cce_location_t                L[1];
ccstructs_pathname_t const *  ptn;
ccstructs_pathname_T          ptn_T;
ccstructs_clean_handler_t     ptn_H[1];

if (cce_location(L)) {
  cce_run_catch_handlers_final(L);
} else {
  ptn   = ccname_new(ccstructs_pathname_t, from_chars)(L, P);
  ptn_T = ccname_trait_new(ccstructs_pathname_T, ccstructs_pathname_t)(ptn);
  ccstructs_init_and_register_handler(L, ptn_H,
    ccname_trait_new(ccstructs_dtor_T, ccstructs_pathname_T)(ptn_T));

  ...
  cce_run_body_handlers(L);
}

We can dump to stderr a string representation of the instance with:

ccstructs_dumpable_T  W =
  ccname_trait_new(ccstructs_dumpable_T, ccstructs_pathname_T)(L, ptn_T);

ccstructs_dumpable_dump(L, W);

We can create an ASCIIZ representation of the pathname with:

ccmem_asciiz_t  rep = ccstructs_pathname_asciiz(L, I);

We can serialise the pathname with:

ccstructs_serialiser_T  S =
  ccname_trait_new(ccstructs_serialiser_T, ccstructs_pathname_T)(L, ptn_T);

/* Allocate memory for the serialisation. */
ccmem_clean_handler_t  M_H[1];
ccmem_block_t          M =
  ccmem_block_malloc_guarded(L, M_H, ccmem_standard_allocator,
    ccstructs_serialiser_required_size(S));

/* Serialise the struct. */
ccmem_block_t  M_leftover = ccstructs_serialiser_write(L, S, M);

and we can deserialise a pathname instance from a memory block M with:

ccmem_block_t            M = ...
ccmem_block_t            M_leftover;
ccstructs_pathname_t *   other;
ccstructs_pathname_T     other_T;
ccstructs_deserialiser_T other_D;

other   = ccname_new(ccstructs_pathname_t, deserialisable)(L);
other_T = ccname_trait_new(ccstructs_pathname_T,
                           ccstructs_pathname_t)(other);
other_D = ccname_trait_new(ccstructs_deserialiser_T,
                           ccstructs_pathname_T)(L, other_T);

M_leftover = ccstructs_deserialiser_read(L, other_D, M);

Previous: , Up: pathname simple   [Contents][Index]

This document describes version 0.3.0-devel.3 of CCStructs.