aboutsummaryrefslogtreecommitdiff
path: root/flx/input.c
blob: 182fa83d34f1361c95d18500200efeae76fe0ccc (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <string.h>
#include "utils.h"
#include "source_type.h"
#include "input.h"
#include "flx_fcntl.h"


/* get data from current input and return the number of data readed 
 */
int input_read(t_db_input *in, void *tree) {
    t_device *current;
    int      nb = 0;

    /* look for pointer initializing */
    while (!in->eof && in->inputs) {
	/* get current pointer to input driver */
	current = SIMPLE_LIST_PTR(in->inputs);
	
	/* run read function associated with current source */
	if (current->opened->read) {
	    if (!(nb = current->opened->read(current->env, tree))) {
		if (current->opened->close)
		    current->env = current->opened->close(current->env);
		current->opened = NULL;
		FREE(current);
		SIMPLE_LIST_POP(in->inputs);
		in->eof = (in->inputs == NULL);
	    }
	    else {
		return (nb);
	    }
	}
    }
    return (nb);
}

int input_add(t_db_input *in, char *src, t_source_type *sourcestype) {
    char         tmp[BUFFER_LENGTH], *ptmp, *otmp = NULL;
    int          sel;
    t_device     *new;
    
    strcpy(tmp, src);
    if ((ptmp = backslashed_strchr(tmp, ':'))) {
	*ptmp++ = 0;
	
	/* look for options */
	if ((otmp = strchr(tmp, '+'))) *otmp++ = 0;
	
	/* find corresponding source */
	for (sel = 0; sourcestype[sel].name; sel++)
	    if (!strcmp(tmp, sourcestype[sel].name)) break;
	if (!sourcestype[sel].name) {
	    /* not found, error */
	    error("can't find source type %s (%s:%s)\n", tmp, tmp, ptmp); 
	    return (0);
	}
    }
    else {
	sel = 0;
	ptmp = tmp;
    }
    
    /* allocate memory for new device */
    new = MALLOC(sizeof(*new));
    new->opened = &sourcestype[sel];
    /* call of device initialize specific function */
    if (new->opened->open) {
	if (!(new->env = new->opened->open(ptmp, otmp))) {
	    error("can't open '%s'", src);
	    FREE(new);
	    return (0);
	}
	if (new->opened->fcntl) {
	    if (in->inputs) {
		Sorted &= ~INPUT_SORTED;
	    }
	    else {
		Sorted &=  (~INPUT_SORTED|
			    ((new->opened->fcntl(new->env, IS_SORTED) ? INPUT_SORTED : 0))); 
	    }
	}
    }
    else
	new->env = NULL;
    /* add in current driver list */
    SIMPLE_LIST_FILE(in->inputs, new);
    in->eof = 0;
    return (1);
}


inline int input_eof(t_db_input *in) {
    return (in->eof);
}

t_db_input *input_alloc() {
    t_db_input *in;

    in = MALLOC(sizeof(t_db_input));
    bzero(in, sizeof(t_db_input));
    return (in);
}

t_db_input *input_free(t_db_input *old) {
    t_device *current;
    
    while (old->inputs) {
	current = SIMPLE_LIST_POP(old->inputs);
	current->opened->close(current->env);
	FREE(current);
    }
    FREE(old);
    return (NULL);
}