~ft/treason

ref: febd271a231c83b67d94e6cb2b9942edfe37e363 treason/stream.c -rw-r--r-- 981 bytes
febd271a — Sigrid Solveig Haflínudóttir ivf: correct code check 1 year, 3 months ago
                                                                                
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
64
65
#include <u.h>
#include <libc.h>
#include "stream.h"

extern Streamops ivfops, mcops, audops;

static struct {
	char *name;
	Streamops *o;
}ops[] = {
	{"ivf", &ivfops},
	{"mc", &mcops},
	{"audio", &audops},
};

Stream *
Sopen(char *filename, int *num)
{
	int i, failed;
	Stream *s;
	char *f;

	*num = 0;
	for(i = 0; i < nelem(ops); i++){
		failed = 0;
		if((s = ops[i].o->open(filename, num, &failed)) != nil){
			f = utfrrune(filename, '/');
			if(f == nil)
				f = filename;
			else
				f++;
			snprint(s->info, sizeof(s->info), "%s [%s]", ops[i].name, f);
			return s;
		}
		if(failed){
			werrstr("%s: %r", ops[i].name);
			return nil;
		}
	}

	werrstr("unknown format");

	return nil;
}

void
Sclose(Stream *s)
{
	if(s == nil || s->type < 0)
		return;
	s->ops.close(s);
	s->type = -1;
	if(s->freeaux != nil)
		s->freeaux(s->aux);
}

int
Sread(Stream *s, Streamframe *f)
{
	if(s == nil || s->type < 0){
		werrstr("invalid stream");
		return -1;
	}
	return s->ops.read(s, f);
}