File: test75.c

package info (click to toggle)
libtpl 1.5-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 4,284 kB
  • ctags: 748
  • sloc: sh: 10,059; ansic: 5,644; perl: 1,062; makefile: 119; cpp: 32
file content (38 lines) | stat: -rwxr-xr-x 889 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( "sc#", &src.s1, &src.s2, S2_LEN);
    tpl_pack( tn, 0 );  
    tpl_dump( tn, TPL_FILE, "/tmp/test75.tpl" );
    tpl_free( tn );

    /* unpack it now into another struct */

    tn = tpl_map( "sc#", &dst.s1, &dst.s2, S2_LEN);
    tpl_load( tn, TPL_FILE, "/tmp/test75.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);
}