File: sring.c

package info (click to toggle)
lcdproc 0.5.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,064 kB
  • sloc: ansic: 59,645; sh: 1,740; perl: 681; makefile: 417
file content (271 lines) | stat: -rw-r--r-- 5,459 bytes parent folder | download | duplicates (4)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/** \file shared/sring.c
 * Circular buffer implementation for string processing.
 *
 * \todo Implement sring_peek() and sring_skip().
 */

/*-
 * This file is part of LCDd, the lcdproc server.
 *
 * This file is released under the GNU General Public License.
 * Refer to the COPYING file distributed with this package.
 *
 * Copyright (c) 2009, Markus Dolze
 */

#include <stdlib.h>
#include <string.h>
#ifdef DEBUG
# include <stdio.h>
# include <ctype.h>
#endif

#include "sring.h"

/**
 * Allocate a new ring buffer data structure.
 * As this ring buffer is implemented using the 'Always Keep One Byte Open'
 * strategy, the internal data buffer is (iSize+1) large.
 *
 * \param iSize  Initial size of the ring buffer
 * \return       Pointer to the created ring buffer
 */
sring_buffer*
sring_create(int iSize)
{
	sring_buffer *buf;

	if ((buf = malloc(sizeof(*buf))) == NULL)
		return NULL;

	if ((buf->data = malloc(iSize + 1)) == NULL)
		return NULL;

	buf->size = iSize + 1;
	buf->w = 0;
	buf->r = 0;

	return buf;
}

/**
 * Free memory used by ring buffer.
 * \param buf  Ring buffer to work on
 */
void
sring_destroy(sring_buffer *buf)
{
	if (buf == NULL)
		return;

	free(buf->data);
	buf->data = NULL;
	free(buf);
}

/**
 * Clears the internal ring buffer.
 * Existing data is overwritten with NUL bytes.
 *
 * \param buf  Ring buffer to work on
 */
void
sring_clear(sring_buffer *buf)
{
	if (buf == NULL)
		return;

	buf->w = 0;
	buf->r = 0;
	memset(buf->data, '\0', buf->size);
}

/**
 * Get the number of bytes that can be written.
 * \param buf  Ring buffer to work on
 * \return     Byte count
 */
int
sring_getMaxWrite(sring_buffer *buf)
{
	int nBytes;

	if (buf == NULL)
		return 0;

	/* Use 'Always Keep One Byte Open' strategy */
	if (buf->w < buf->r)
		nBytes = buf->r - buf->w - 1;
	else
		nBytes = (buf->size - buf->w) + buf->r - 1;

	return nBytes;
}

/**
 * Get the number of bytes that can be read.
 * \param buf  Ring buffer to work on
 * \return     Byte count
 */
int
sring_getMaxRead(sring_buffer *buf)
{
	int nBytes;

	if (buf == NULL)
		return 0;

	if (buf->r <= buf->w)
		nBytes = buf->w - buf->r;
	else
		nBytes = (buf->size - buf->r) + buf->w;

	return nBytes;
}

/**
 * Write src_len bytes from src into ring buffer.
 * Fails if not all bytes can be written.
 *
 * \param buf  Ring buffer to work on
 * \param src  Pointer to source buffer
 * \param src_len  Number of bytes to write at most
 * \return     -1 if not all bytes can be written, 0 otherwise
 */
int
sring_write(sring_buffer *buf, char *src, int src_len)
{
	if (buf == NULL || src == NULL || src_len <= 0)
		return -1;

	/* XXX: Modify it to write as much as possible? */
	if (src_len > sring_getMaxWrite(buf))
		return -1;

	if (buf->w + src_len < buf->size) {
		memcpy(buf->data + buf->w, src, src_len);
		buf->w += src_len;
	}
	else {
		int firstBlockLen = buf->size - buf->w;
		int secondBlockLen = src_len - firstBlockLen;

		memcpy(buf->data + buf->w, src, firstBlockLen);
		memcpy(buf->data, src + firstBlockLen, secondBlockLen);

		buf->w = secondBlockLen;
	}

	return 0;
}

/**
 * Read dst_len bytes from ring buffer into destination.
 * Fails if buffer does not contains dst_len bytes to read from. The target
 * buffer must be allocated by the application before calling this function.
 *
 * \param buf  Ring buffer to work on
 * \param dst  Pointer to target buffer
 * \param dst_len  Number of bytes to read at most
 * \return     The number of bytes actually read
 */
int
sring_read(sring_buffer *buf, char *dst, int dst_len)
{
	if (buf == NULL || dst == NULL || dst_len <= 0)
		return -1;

	/* Do not read more than available */
	if (dst_len > sring_getMaxRead(buf))
		dst_len = sring_getMaxRead(buf);

	if (buf->r + dst_len < buf->size) {
		memcpy(dst, buf->data + buf->r, dst_len);
		buf->r += dst_len;
	}
	else {
		int firstBlockLen = buf->size - buf->r;
		int secondBlockLen = dst_len - firstBlockLen;

		memcpy(dst, buf->data + buf->r, firstBlockLen);

		if (secondBlockLen > 0)
			memcpy(dst + firstBlockLen, buf->data, secondBlockLen);

		buf->r = secondBlockLen;
	}

	return dst_len;
}

/**
 * Return the next string from the ring buffer.
 * The next string is a sequence of bytes terminated by \\r, \\n or \\0. The
 * memory for the string is allocated dynamically and must be free'd by the
 * application. The string is always NUL terminated, but does not include the
 * end character.
 *
 * \param buf  Ring buffer to work on
 * \return     Pointer to allocated string, NULL if no string is available
 */
char *
sring_read_string(sring_buffer *buf)
{
	int n;
	char *border;
	char *p;
	char *dst;
	int dst_len;

	if (buf == NULL)
		return NULL;

	n = sring_getMaxRead(buf);
	border = buf->data + buf->size;
	p = buf->data + buf->r;

	while (--n >= 0) {
		if (*p == '\r' || *p == '\n' || *p == '\0')
			break;
		p++;
		if (p == border)
			p = buf->data;
	};

	if (n == -1)
		return NULL;

	dst_len = sring_getMaxRead(buf) - n;
	if ((dst = malloc(dst_len)) == NULL)
		return NULL;

	sring_read(buf, dst, dst_len);
	dst[dst_len-1] = '\0';

	return dst;
}

/**
 * Print content of buffer to stdout.
 * Only enabled, if DEBUG is defined.
 *
 * \param buf  Ring buffer to work on
 */
void
sring_dump(sring_buffer *buf)
{
#ifdef DEBUG
	int a;

	if (buf == NULL)
		return;

	for (a = 0; a < buf->size; a++) {
		if (isprint(buf->data[a]))
			printf("'%c' ", buf->data[a]);
		else
			printf("0x%02X ", buf->data[a]);
	}
	printf("\n");
#endif
}