File: test54.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 (63 lines) | stat: -rwxr-xr-x 1,753 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
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <fcntl.h>
#include <errno.h>
#include "tpl.h"

#define DEBUG 0
#define FILE_BUFLEN 500

extern tpl_hook_t tpl_hook;
int num_tpls = 0, sum_tpls = 0;

int tpl_cb(void *tpl, size_t tpllen, void*data) {
    int i;
    tpl_node *tn;

    tpl_hook.oops = printf;

    if (DEBUG) printf("obtained tpl of length %d\n", (int)tpllen);
    tn = tpl_map("A(i)", &i);
    tpl_load(tn, TPL_MEM, tpl, tpllen);
    num_tpls++;
    while (tpl_unpack(tn,1) > 0) sum_tpls += i;
    tpl_free(tn); 
    /* this next line is a hack to test the callback's ability
     * to abort further tpl processing by returning < 0 */
    if (num_tpls == 3) return -1;
    return 0;
    
}

int main(int argc, char *argv[]) {
    char *files[] = {"test54_0.tpl", "test54_1.tpl", "test54_2.tpl", "test54_3.tpl","test54_4.tpl", NULL};
    char **f;
    char buf[FILE_BUFLEN];
    int rc,fd;
    tpl_gather_t *gs=NULL;

    for (f = files; *f; f++) {
        if (DEBUG) printf("file is %s\n", *f);
        if ( ( fd = open(*f, O_RDONLY) ) == -1) {
            printf("error - can't open %s: %s\n", *f, strerror(errno));
            exit(-1);
        }
        rc = read(fd,&buf,FILE_BUFLEN);  /* read whole file (no points for style) */
        if (rc == -1) {
            printf("error - can't read %s: %s\n", *f, strerror(errno));
            exit(-1);
        }
        if (tpl_gather(TPL_GATHER_MEM,buf, rc, &gs, tpl_cb, NULL) <= 0) {
            printf("tpl_gather_mem returned <= 0, exiting\n");
            exit(-1);
        }
        close(fd);
    }
    printf("num_tpls: %d, sum: %d\n", num_tpls, sum_tpls);
    return(0);
}