summaryrefslogtreecommitdiff
path: root/libdvbmpeg/ringbuffy.c
blob: 8451009533d46acb20116d9f5e8d0aacbf68599c (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* 
    Ringbuffer Implementation for gtvscreen

    Copyright (C) 2000 Marcus Metzler (mocm@metzlerbros.de)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "ringbuffy.h"

int ring_init (ringbuffy *rbuf, int size)
{
	if (size > 0){
		rbuf->size = size;
		if( !(rbuf->buffy = (char *) malloc(sizeof(char)*size)) ){
			fprintf(stderr,"Not enough memory for ringbuffy\n");
			return -1;
		}
	} else {
		fprintf(stderr,"Wrong size for ringbuffy\n");
		return -1;
	}
	rbuf->read_pos = 0;	
	rbuf->write_pos = 0;
	return 0;
}


void ring_destroy(ringbuffy *rbuf)
{
	free(rbuf->buffy);
}


int ring_write(ringbuffy *rbuf, char *data, int count)
{

	int diff, free, pos, rest;

	if (count <=0 ) return 0;
       	pos  = rbuf->write_pos;
	rest = rbuf->size - pos;
	diff = rbuf->read_pos - pos;
	free = (diff > 0) ? diff-1 : rbuf->size+diff-1;

	if ( free <= 0 ) return FULL_BUFFER;
	if ( free < count ) count = free;
	
	if (count >= rest){
		memcpy (rbuf->buffy+pos, data, rest);
		if (count - rest)
			memcpy (rbuf->buffy, data+rest, count - rest);
		rbuf->write_pos = count - rest;
	} else {
		memcpy (rbuf->buffy+pos, data, count);
		rbuf->write_pos += count;
	}

	return count;
}




int ring_peek(ringbuffy *rbuf, char *data, int count, long off)
{

	int diff, free, pos, rest;

	if (count <=0 ) return 0;
	pos  = rbuf->read_pos+off;
	rest = rbuf->size - pos ;
	diff = rbuf->write_pos - pos;
	free = (diff >= 0) ? diff : rbuf->size+diff;

	if ( free <= 0 ) return FULL_BUFFER;
	if ( free < count ) count = free;
	
	if ( count < rest ){
		memcpy(data, rbuf->buffy+pos, count);
	} else {
		memcpy(data, rbuf->buffy+pos, rest);
		if ( count - rest)
			memcpy(data+rest, rbuf->buffy, count - rest);
	}

	return count;
}

int ring_read(ringbuffy *rbuf, char *data, int count)
{

	int diff, free, pos, rest;

	if (count <=0 ) return 0;
	pos  = rbuf->read_pos;
	rest = rbuf->size - pos;
	diff = rbuf->write_pos - pos;
	free = (diff >= 0) ? diff : rbuf->size+diff;

	if ( rest <= 0 ) return 0;
	if ( free < count ) count = free;
	
	if ( count < rest ){
		memcpy(data, rbuf->buffy+pos, count);
		rbuf->read_pos += count;
	} else {
		memcpy(data, rbuf->buffy+pos, rest);
		if ( count - rest)
			memcpy(data+rest, rbuf->buffy, count - rest);
		rbuf->read_pos = count - rest;
	}

	return count;
}



int ring_write_file(ringbuffy *rbuf, int fd, int count)
{

	int diff, free, pos, rest, rr;

	if (count <=0 ) return 0;
       	pos  = rbuf->write_pos;
	rest = rbuf->size - pos;
	diff = rbuf->read_pos - pos;
	free = (diff > 0) ? diff-1 : rbuf->size+diff-1;

	if ( rest <= 0 ) return 0;
	if ( free < count ) count = free;
	
	if (count >= rest){
		rr = read (fd, rbuf->buffy+pos, rest);
		if (rr == rest && count - rest)
			rr += read (fd, rbuf->buffy, count - rest);
		if (rr >=0)
			rbuf->write_pos = (pos + rr) % rbuf->size;
	} else {
		rr = read (fd, rbuf->buffy+pos, count);
		if (rr >=0)
			rbuf->write_pos += rr;
	}

	return rr;
}



int ring_read_file(ringbuffy *rbuf, int fd, int count)
{

	int diff, free, pos, rest, rr;

	if (count <=0 ) return 0;
	pos  = rbuf->read_pos;
	rest = rbuf->size - pos;
	diff = rbuf->write_pos - pos;
	free = (diff >= 0) ? diff : rbuf->size+diff;

	if ( free <= 0 ) return FULL_BUFFER;
	if ( free < count ) count = free;

	if (count >= rest){
		rr = write (fd, rbuf->buffy+pos, rest);
		if (rr == rest && count - rest)
			rr += write (fd, rbuf->buffy, count - rest);
		if (rr >=0)
			rbuf->read_pos = (pos + rr) % rbuf->size;
	} else {
		rr = write (fd, rbuf->buffy+pos, count);
		if (rr >=0)
			rbuf->read_pos += rr;
	}


	return rr;
}

int ring_rest(ringbuffy *rbuf){
       	int diff, free, pos, rest;
	pos  = rbuf->read_pos;
	rest = rbuf->size - pos;
	diff = rbuf->write_pos - pos;
	free = (diff >= 0) ? diff : rbuf->size+diff;
	
	return free;
}