1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "tpl.h"
6
7 #define COUNT 10
8 #define BUF_SIZE 256
9 const char *filename = "/tmp/test115.tpl";
10
11 typedef struct {
12 char* s;
13 } s1_t;
14
15
16 typedef struct {
17 char* s;
18 int i;
19 } s2_t;
20
21
22 typedef struct {
23 char c[BUF_SIZE];
24 char* s;
25 int i;
26 } s3_t;
27
28
29 const char hw[]="hello, world!";
30
main()31 int main ()
32 {
33 tpl_node* tn;
34 s1_t* s1, *S1;
35 s2_t* s2, *S2;
36 s3_t* s3, *S3;
37 int i;
38
39 /* case 1: */
40 s1 = (s1_t*)calloc (sizeof (s1_t), COUNT);
41 for(i=0; i < COUNT; i++) {
42 s1[i].s = malloc(sizeof(hw));
43 memcpy(s1[i].s, hw, sizeof(hw));
44 s1[i].s[sizeof(hw)-2]='0'+i;
45 }
46 tn = tpl_map ("S(s)#", s1, COUNT);
47 tpl_pack (tn, 0);
48 tpl_dump (tn, TPL_FILE, filename);
49 tpl_free (tn);
50 for(i=0; i < COUNT; i++) free(s1[i].s);
51
52 S1 = (s1_t*)calloc (sizeof (s1_t), COUNT);
53 memset(S1, 0xff, sizeof(s1_t)*COUNT);
54 tn = tpl_map ("S(s)#", S1, COUNT);
55 tpl_load (tn, TPL_FILE, filename);
56 tpl_unpack (tn, 0);
57 tpl_free (tn);
58
59 for(i=0; i<COUNT; i++) {
60 printf("%s\n", S1[i].s);
61 }
62
63
64 /* case 2: */
65 s2 = (s2_t*)calloc (sizeof (s2_t), COUNT);
66 for(i=0; i < COUNT; i++) {
67 s2[i].s = malloc(sizeof(hw));
68 memcpy(s2[i].s, hw, sizeof(hw));
69 s2[i].s[sizeof(hw)-2]='0'+i;
70 s2[i].i=i;
71 }
72 tn = tpl_map ("S(si)#", s2, COUNT);
73 tpl_pack (tn, 0);
74 tpl_dump (tn, TPL_FILE, filename);
75 tpl_free (tn);
76 for(i=0; i < COUNT; i++) free(s2[i].s);
77
78 S2 = (s2_t*)calloc (sizeof (s2_t), COUNT);
79 memset(S2, 0xff, sizeof(s2_t)*COUNT);
80 tn = tpl_map ("S(si)#", S2, COUNT);
81 tpl_load (tn, TPL_FILE, filename);
82 tpl_unpack (tn, 0);
83 tpl_free (tn);
84
85 for(i=0; i<COUNT; i++) {
86 printf("%s, %u\n", S2[i].s, S2[i].i);
87 }
88
89
90 /* case 3: */
91 s3 = (s3_t*)calloc (sizeof (s3_t), COUNT);
92 for(i=0; i < COUNT; i++) {
93 memset(s3[i].c, 'a', BUF_SIZE);
94 s3[i].c[BUF_SIZE-1]='\0';
95 s3[i].s = malloc(sizeof(hw));
96 memcpy(s3[i].s, hw, sizeof(hw));
97 s3[i].s[sizeof(hw)-2]='0'+i;
98 s3[i].i=i;
99 }
100 tn = tpl_map ("S(c#si)#", s3, BUF_SIZE, COUNT);
101 tpl_pack (tn, 0);
102 tpl_dump (tn, TPL_FILE, filename);
103 tpl_free (tn);
104
105 S3 = (s3_t*)calloc (sizeof (s3_t), COUNT);
106 memset(S3, 0xff, sizeof(s3_t)*COUNT);
107 tn = tpl_map ("S(c#si)#", S3, BUF_SIZE, COUNT);
108 tpl_load (tn, TPL_FILE, filename);
109 tpl_unpack (tn, 0);
110 tpl_free (tn);
111
112 for(i=0; i<COUNT; i++) {
113 printf("%s, %u\n", S3[i].s, S3[i].i);
114 printf("%s\n", S3[i].c);
115 }
116
117 return 0;
118 }
119
120