File: test76.c

package info (click to toggle)
libtpl 1.6.1-1.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 2,028 kB
  • sloc: ansic: 5,669; perl: 1,062; makefile: 101; cpp: 32; sh: 18
file content (38 lines) | stat: -rw-r--r-- 907 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdlib.h>
#include "tpl.h"

#define S2_LEN 10

struct example {
    char *s1;         /* s1 is a pointer */
    char s2[S2_LEN];  /* s2 is a byte array */
};

int main() {
    tpl_node *tn;
    int i;
    struct example dst, src = {
        /* .s1 = */ "string",
        /* .s2 = */ {'b','y','t','e',' ','a','r','r','a','y'}
    };

    tn = tpl_map( "S(sc#)", &src, S2_LEN);   /* NOTE S(...) */
    tpl_pack( tn, 0 );
    tpl_dump( tn, TPL_FILE, "/tmp/test76.tpl" );
    tpl_free( tn );

    /* unpack it now into another struct */

    tn = tpl_map( "S(sc#)", &dst, S2_LEN);   /* NOTE S(...) */
    tpl_load( tn, TPL_FILE, "/tmp/test76.tpl" );
    tpl_unpack( tn, 0 );
    tpl_free( tn );

    printf("%s\n", dst.s1);
    for(i=0; i < S2_LEN; i++) printf("%c", dst.s2[i]);
    printf("\n");

    free(dst.s1);   /* tpl allocated it for us; we must free it */
    return(0);
}