grapes / src / Chunkiser / input-stream-dumb.c @ 4cdd16a7
History | View | Annotate | Download (1.66 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2010 Luca Abeni
|
3 |
*
|
4 |
* This is free software; see gpl-3.0.txt
|
5 |
*/
|
6 |
|
7 |
#include <sys/types.h> |
8 |
#include <sys/stat.h> |
9 |
#include <fcntl.h> |
10 |
#include <unistd.h> |
11 |
#include <stdint.h> |
12 |
#include <stdlib.h> |
13 |
#include <string.h> |
14 |
|
15 |
#include "chunkiser_iface.h" |
16 |
#include "config.h" |
17 |
|
18 |
struct chunkiser_ctx {
|
19 |
int chunk_size;
|
20 |
int fds[2]; |
21 |
}; |
22 |
#define DEFAULT_CHUNK_SIZE 2 * 1024 |
23 |
|
24 |
static struct chunkiser_ctx *dumb_open(const char *fname, int *period, const char *config) |
25 |
{ |
26 |
struct tag *cfg_tags;
|
27 |
struct chunkiser_ctx *res;
|
28 |
|
29 |
res = malloc(sizeof(struct chunkiser_ctx)); |
30 |
if (res == NULL) { |
31 |
return NULL; |
32 |
} |
33 |
|
34 |
res->fds[0] = open(fname, O_RDONLY);
|
35 |
if (res->fds[0] < 0) { |
36 |
free(res); |
37 |
|
38 |
return NULL; |
39 |
} |
40 |
res->fds[1] = -1; |
41 |
|
42 |
*period = 0;
|
43 |
res->chunk_size = DEFAULT_CHUNK_SIZE; |
44 |
cfg_tags = config_parse(config); |
45 |
if (cfg_tags) {
|
46 |
const char *access_mode; |
47 |
|
48 |
config_value_int(cfg_tags, "chunk_size", &res->chunk_size);
|
49 |
access_mode = config_value_str(cfg_tags, "mode");
|
50 |
if (!strcmp(access_mode, "nonblock")) { |
51 |
fcntl(res->fds[0], F_SETFL, O_NONBLOCK);
|
52 |
} |
53 |
} |
54 |
free(cfg_tags); |
55 |
|
56 |
return res;
|
57 |
} |
58 |
|
59 |
static void dumb_close(struct chunkiser_ctx *s) |
60 |
{ |
61 |
close(s->fds[0]);
|
62 |
free(s); |
63 |
} |
64 |
|
65 |
static uint8_t *dumb_chunkise(struct chunkiser_ctx *s, int id, int *size, uint64_t *ts) |
66 |
{ |
67 |
uint8_t *res; |
68 |
|
69 |
res = malloc(s->chunk_size); |
70 |
if (res == NULL) { |
71 |
*size = -1;
|
72 |
|
73 |
return NULL; |
74 |
} |
75 |
*ts = 0;
|
76 |
*size = read(s->fds[0], res, s->chunk_size);
|
77 |
|
78 |
return res;
|
79 |
} |
80 |
|
81 |
const int *dumb_get_fds(const struct chunkiser_ctx *s) |
82 |
{ |
83 |
return s->fds;
|
84 |
} |
85 |
|
86 |
struct chunkiser_iface in_dumb = {
|
87 |
.open = dumb_open, |
88 |
.close = dumb_close, |
89 |
.chunkise = dumb_chunkise, |
90 |
.get_fds = dumb_get_fds, |
91 |
}; |
92 |
|
93 |
|