File: test115.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 (119 lines) | stat: -rwxr-xr-x 2,506 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "tpl.h"

#define COUNT 10
#define BUF_SIZE 256
const char *filename = "/tmp/test115.tpl";

typedef struct {
   char* s;
} s1_t;


typedef struct {
   char* s;
   int i;
} s2_t;


typedef struct {
   char c[BUF_SIZE];
   char* s;
   int i;
} s3_t;


const char hw[]="hello, world!";

int main ()
{
   tpl_node* tn;
   s1_t* s1, *S1;
   s2_t* s2, *S2;
   s3_t* s3, *S3;
   int i;

   /* case 1: */
   s1 = (s1_t*)calloc (sizeof (s1_t), COUNT);
   for(i=0; i < COUNT; i++) {
     s1[i].s = malloc(sizeof(hw));
     memcpy(s1[i].s, hw, sizeof(hw));
     s1[i].s[sizeof(hw)-2]='0'+i;
   }
   tn = tpl_map ("S(s)#", s1, COUNT);
   tpl_pack (tn, 0);
   tpl_dump (tn, TPL_FILE, filename);
   tpl_free (tn);
   for(i=0; i < COUNT; i++) free(s1[i].s);

   S1 = (s1_t*)calloc (sizeof (s1_t), COUNT);
   memset(S1, 0xff, sizeof(s1_t)*COUNT);
   tn = tpl_map ("S(s)#", S1, COUNT);
   tpl_load (tn, TPL_FILE, filename);
   tpl_unpack (tn, 0);
   tpl_free (tn);

   for(i=0; i<COUNT; i++) {
     printf("%s\n", S1[i].s);
   }


   /* case 2: */
   s2 = (s2_t*)calloc (sizeof (s2_t), COUNT);
   for(i=0; i < COUNT; i++) {
     s2[i].s = malloc(sizeof(hw));
     memcpy(s2[i].s, hw, sizeof(hw));
     s2[i].s[sizeof(hw)-2]='0'+i;
     s2[i].i=i;
   }
   tn = tpl_map ("S(si)#", s2, COUNT);
   tpl_pack (tn, 0);
   tpl_dump (tn, TPL_FILE, filename);
   tpl_free (tn);
   for(i=0; i < COUNT; i++) free(s2[i].s);

   S2 = (s2_t*)calloc (sizeof (s2_t), COUNT);
   memset(S2, 0xff, sizeof(s2_t)*COUNT);
   tn = tpl_map ("S(si)#", S2, COUNT);
   tpl_load (tn, TPL_FILE, filename);
   tpl_unpack (tn, 0);
   tpl_free (tn);

   for(i=0; i<COUNT; i++) {
     printf("%s, %u\n", S2[i].s, S2[i].i);
   }


   /* case 3: */
   s3 = (s3_t*)calloc (sizeof (s3_t), COUNT);
   for(i=0; i < COUNT; i++) {
     memset(s3[i].c, 'a', BUF_SIZE);
     s3[i].c[BUF_SIZE-1]='\0';
     s3[i].s = malloc(sizeof(hw));
     memcpy(s3[i].s, hw, sizeof(hw));
     s3[i].s[sizeof(hw)-2]='0'+i;
     s3[i].i=i;
   }
   tn = tpl_map ("S(c#si)#", s3, BUF_SIZE, COUNT);
   tpl_pack (tn, 0);
   tpl_dump (tn, TPL_FILE, filename);
   tpl_free (tn);

   S3 = (s3_t*)calloc (sizeof (s3_t), COUNT);
   memset(S3, 0xff, sizeof(s3_t)*COUNT);
   tn = tpl_map ("S(c#si)#", S3, BUF_SIZE, COUNT);
   tpl_load (tn, TPL_FILE, filename);
   tpl_unpack (tn, 0);
   tpl_free (tn);

   for(i=0; i<COUNT; i++) {
     printf("%s, %u\n", S3[i].s, S3[i].i);
     printf("%s\n", S3[i].c);
   }

   return 0;
}