[cafb38]: xbmc / lib / libhts / htsbuf.c  Maximize  Restore  History

Repo status: analyzing...

Download this file

256 lines (211 with data), 4.6 kB

  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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Buffer management functions
* Copyright (C) 2008 Andreas Öman
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "htsbuf.h"
#ifdef _MSC_VER
#include "msvc.h"
#endif
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
/**
*
*/
void
htsbuf_queue_init(htsbuf_queue_t *hq, unsigned int maxsize)
{
if(maxsize == 0)
maxsize = INT32_MAX;
TAILQ_INIT(&hq->hq_q);
hq->hq_size = 0;
hq->hq_maxsize = maxsize;
}
/**
*
*/
void
htsbuf_data_free(htsbuf_queue_t *hq, htsbuf_data_t *hd)
{
TAILQ_REMOVE(&hq->hq_q, hd, hd_link);
free(hd->hd_data);
free(hd);
}
/**
*
*/
void
htsbuf_queue_flush(htsbuf_queue_t *hq)
{
htsbuf_data_t *hd;
hq->hq_size = 0;
while((hd = TAILQ_FIRST(&hq->hq_q)) != NULL)
htsbuf_data_free(hq, hd);
}
/**
*
*/
void
htsbuf_append(htsbuf_queue_t *hq, const char *buf, size_t len)
{
htsbuf_data_t *hd = TAILQ_LAST(&hq->hq_q, htsbuf_data_queue);
int c;
hq->hq_size += len;
if(hd != NULL) {
/* Fill out any previous buffer */
c = MIN(hd->hd_data_size - hd->hd_data_len, len);
memcpy(hd->hd_data + hd->hd_data_len, buf, c);
hd->hd_data_len += c;
buf += c;
len -= c;
}
if(len == 0)
return;
hd = malloc(sizeof(htsbuf_data_t));
TAILQ_INSERT_TAIL(&hq->hq_q, hd, hd_link);
c = MAX(len, 1000); /* Allocate 1000 bytes to support lots of small writes */
hd->hd_data = malloc(c);
hd->hd_data_size = c;
hd->hd_data_len = len;
hd->hd_data_off = 0;
memcpy(hd->hd_data, buf, len);
}
/**
*
*/
void
htsbuf_append_prealloc(htsbuf_queue_t *hq, const char *buf, size_t len)
{
htsbuf_data_t *hd;
hq->hq_size += len;
hd = malloc(sizeof(htsbuf_data_t));
TAILQ_INSERT_TAIL(&hq->hq_q, hd, hd_link);
hd->hd_data = (void *)buf;
hd->hd_data_size = len;
hd->hd_data_len = len;
hd->hd_data_off = 0;
}
/**
*
*/
size_t
htsbuf_read(htsbuf_queue_t *hq, char *buf, size_t len)
{
size_t r = 0;
int c;
htsbuf_data_t *hd;
while(len > 0) {
hd = TAILQ_FIRST(&hq->hq_q);
if(hd == NULL)
break;
c = MIN(hd->hd_data_len - hd->hd_data_off, len);
memcpy(buf, hd->hd_data + hd->hd_data_off, c);
r += c;
buf += c;
len -= c;
hd->hd_data_off += c;
hq->hq_size -= c;
if(hd->hd_data_off == hd->hd_data_len)
htsbuf_data_free(hq, hd);
}
return r;
}
/**
*
*/
size_t
htsbuf_find(htsbuf_queue_t *hq, uint8_t v)
{
htsbuf_data_t *hd;
unsigned int i, o = 0;
TAILQ_FOREACH(hd, &hq->hq_q, hd_link) {
for(i = hd->hd_data_off; i < hd->hd_data_len; i++) {
if(hd->hd_data[i] == v)
return o + i - hd->hd_data_off;
}
o += hd->hd_data_len - hd->hd_data_off;
}
return -1;
}
/**
*
*/
size_t
htsbuf_peek(htsbuf_queue_t *hq, char *buf, size_t len)
{
size_t r = 0;
int c;
htsbuf_data_t *hd = TAILQ_FIRST(&hq->hq_q);
while(len > 0 && hd != NULL) {
c = MIN(hd->hd_data_len - hd->hd_data_off, len);
memcpy(buf, hd->hd_data + hd->hd_data_off, c);
buf += c;
len -= c;
hd = TAILQ_NEXT(hd, hd_link);
}
return r;
}
/**
*
*/
size_t
htsbuf_drop(htsbuf_queue_t *hq, size_t len)
{
size_t r = 0;
int c;
htsbuf_data_t *hd;
while(len > 0) {
hd = TAILQ_FIRST(&hq->hq_q);
if(hd == NULL)
break;
c = MIN(hd->hd_data_len - hd->hd_data_off, len);
len -= c;
hd->hd_data_off += c;
if(hd->hd_data_off == hd->hd_data_len)
htsbuf_data_free(hq, hd);
}
return r;
}
/**
*
*/
void
htsbuf_vqprintf(htsbuf_queue_t *hq, const char *fmt, va_list ap)
{
char buf[5000];
htsbuf_append(hq, buf, vsnprintf(buf, sizeof(buf), fmt, ap));
}
/**
*
*/
void
htsbuf_qprintf(htsbuf_queue_t *hq, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
htsbuf_vqprintf(hq, fmt, ap);
va_end(ap);
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks