File: test112.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 (47 lines) | stat: -rw-r--r-- 921 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
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "tpl.h"

const char *filename = "/tmp/test112.tpl";

#define NUM 10

struct st {
  int i;
  char c[8];
  double f;
  uint16_t v[2];
};


int main() {
  struct st s[NUM], d[NUM];
  tpl_node *tn; 
  int i;

  memset(s, 0, sizeof(s)); /* clear s */
  memset(d, 0, sizeof(d)); /* clear d */

  /* fill s with random stuff */
  for(i=0; i < NUM; i++) {
    s[i].i = i; s[i].f = 3.14159 * i; s[i].v[0] = NUM*i; s[i].v[1] = NUM+i; 
    strncpy(s[i].c, "abcdefg",8);
    s[i].c[0] += 1;
  }

  tn = tpl_map("S(ic#fv#)#", s, 8, 2, NUM);
  tpl_pack(tn,0);
  tpl_dump(tn,TPL_FILE,filename);
  tpl_free(tn);

  tn = tpl_map("S(ic#fv#)#", d, 8, 2, NUM);
  tpl_load(tn,TPL_FILE,filename);
  tpl_unpack(tn,0);
  tpl_free(tn);

  /* see if the result is the same as the s */
  printf("structures %s\n", (!memcmp(d,s,sizeof(d)))? "match" : "mismatch");
  return 0;
}