Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (1.82 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
  int lost;
24
  int first_id;
25
  FILE *f;
26
};
27

    
28
static struct dechunkiser_ctx *dummy_open(const char *fname, const char *config)
29
{
30
  struct dechunkiser_ctx *res;
31
  struct tag *cfg_tags;
32

    
33
  res = malloc(sizeof(struct dechunkiser_ctx));
34
  if (res == NULL) {
35
    return NULL;
36
  }
37
  res->f = stdout;
38
  res->type = chunk_id;
39
  if (fname) {
40
    res->f = fopen(fname, "w");
41
    if (res->f == NULL) {
42
      res->f = stdout;
43
    }
44
  }
45
  res->last_id = -1;
46
  cfg_tags = config_parse(config);
47
  if (cfg_tags) {
48
    const char *pt;
49

    
50
    pt = config_value_str(cfg_tags, "type");
51
    if (pt) {
52
      if (!strcmp(pt, "stats")) {
53
        res->type = stats;
54
      }
55
    }
56
  }
57
  free(cfg_tags);
58

    
59
  return res;
60
}
61

    
62
static void dummy_write(struct dechunkiser_ctx *o, int id, uint8_t *data, int size)
63
{
64
  switch (o->type) {
65
    case chunk_id:
66
      fprintf(o->f, "Chunk %d: size %d\n", id, size);
67
      break;
68
    case stats:
69
      if (o->last_id >= 0) {
70
        int i;
71

    
72
        for (i = 1; i < id - o->last_id; i++) {
73
          o->lost++;
74
          fprintf(o->f, "Lost chunk %d\n", o->last_id + i);
75
        }
76
        fprintf(o->f, "# Lost chunk ratio: %f\n", (double)o->lost / (double)(id - o->first_id));
77
      } else {
78
        o->first_id = id;
79
      }
80
      break;
81
    default:
82
      fprintf(stderr, "Internal error!\n");
83
      exit(-1);
84
  }
85
  fflush(o->f);
86
  o->last_id = id;
87
}
88

    
89
static void dummy_close(struct dechunkiser_ctx *s)
90
{
91
  free(s);
92
}
93

    
94
struct dechunkiser_iface out_dummy = {
95
  .open = dummy_open,
96
  .write = dummy_write,
97
  .close = dummy_close,
98
};