Statistics
| Branch: | Revision:

grapes / src / Chunkiser / output-stream-dummy.c @ 9e5d52d5

History | View | Annotate | Download (1.48 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6

    
7
#include <stdint.h>
8
#include <stdlib.h>
9
#include <stdio.h>
10
#include <string.h>
11

    
12
#include "config.h"
13
#include "dechunkiser_iface.h"
14

    
15
enum output_type {
16
  chunk_id,
17
  stats,
18
};
19

    
20
struct dechunkiser_ctx {
21
  enum output_type type;
22
  int last_id;
23
};
24

    
25
static struct dechunkiser_ctx *dummy_open(const char *fname, const char *config)
26
{
27
  struct dechunkiser_ctx *res;
28
  struct tag *cfg_tags;
29

    
30
  res = malloc(sizeof(struct dechunkiser_ctx));
31
  if (res == NULL) {
32
    return NULL;
33
  }
34
  res->type = chunk_id;
35
  res->last_id = -1;
36
  cfg_tags = config_parse(config);
37
  if (cfg_tags) {
38
    const char *pt;
39

    
40
    pt = config_value_str(cfg_tags, "type");
41
    if (pt) {
42
      if (!strcmp(pt, "stats")) {
43
        res->type = stats;
44
      }
45
    }
46
  }
47
  free(cfg_tags);
48

    
49
  return res;
50
}
51

    
52
static void dummy_write(struct dechunkiser_ctx *o, int id, uint8_t *data, int size)
53
{
54
  switch (o->type) {
55
    case chunk_id:
56
      printf("Chunk %d: size %d\n", id, size);
57
      break;
58
    case stats:
59
      if (o->last_id >= 0) {
60
        int i;
61

    
62
        for (i = 1; i < id - o->last_id; i++) {
63
          printf("Lost chunk %d\n", o->last_id + i);
64
        }
65
      }
66
      break;
67
    default:
68
      fprintf(stderr, "Internal error!\n");
69
      exit(-1);
70
  }
71
  o->last_id = id;
72
}
73

    
74
static void dummy_close(struct dechunkiser_ctx *s)
75
{
76
  free(s);
77
}
78

    
79
struct dechunkiser_iface out_dummy = {
80
  .open = dummy_open,
81
  .write = dummy_write,
82
  .close = dummy_close,
83
};