summaryrefslogtreecommitdiff
path: root/pn_parser.c
blob: efd68ae33663a3d4ca62fa658f3972b1ddc19861 (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
#include <glib.h>

#include "pn_parser.h"
#include "pn_buffer.h"

#include "pn_log.h"

bool
pn_buffer_read_line(struct pn_buffer *b,
		    gchar **ret_str,
		    gsize *ret_len)
{
	bool more = true;
	gchar *cur, *next;
	gsize cur_len;

	cur = b->data + b->offset;
	next = g_strstr_len(cur, b->len - b->offset, "\r\n");

	if (!next) {
		/* the line is incomplete */
		goto missing;
	}

	cur_len = next - cur;

	if (ret_str)
		*ret_str = cur;

	if (ret_len)
		*ret_len = cur_len;

	b->offset += cur_len + 2;

	if (b->offset == b->len) {
		b->offset = b->len = 0;
		more = false;
	}

	return more;

missing:
	if (ret_str)
		*ret_str = NULL;

	if (ret_len)
		*ret_len = 0;

	return false;
}