summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Contreras <felipe.contreras@gmail.com>2010-06-14 01:58:56 +0300
committerFelipe Contreras <felipe.contreras@gmail.com>2010-06-14 03:33:31 +0300
commitf422fc1552c5c187ff1bcb68147dc78463377260 (patch)
tree5f3326462a998b2a1cd14099de2c08876d740544
parent292f7375ed00700e2a296369354f64d14fe21d18 (diff)
Add pn_parser
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
-rw-r--r--Makefile2
-rw-r--r--pn_parser.c50
-rw-r--r--pn_parser.h12
3 files changed, 63 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 6decf93..f36cbde 100644
--- a/Makefile
+++ b/Makefile
@@ -19,7 +19,7 @@ endif
test: test.o pn_core.o pn_session.o pn_node.o \
pn_trans.o pn_cmd_node.o pn_ns.o \
- pn_log.o pn_printf.o pn_buffer.o
+ pn_log.o pn_printf.o pn_buffer.o pn_parser.o
test: override CFLAGS += $(GIO_CFLAGS)
test: override LIBS += $(GIO_LIBS)
diff --git a/pn_parser.c b/pn_parser.c
new file mode 100644
index 0000000..efd68ae
--- /dev/null
+++ b/pn_parser.c
@@ -0,0 +1,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;
+}
diff --git a/pn_parser.h b/pn_parser.h
new file mode 100644
index 0000000..e803f3a
--- /dev/null
+++ b/pn_parser.h
@@ -0,0 +1,12 @@
+#ifndef PN_PARSER_H
+#define PN_PARSER_H
+
+#include <stdbool.h>
+
+struct pn_buffer;
+
+bool pn_buffer_read_line(struct pn_buffer *buffer,
+ gchar **ret_str,
+ gsize *ret_len);
+
+#endif /* PN_PARSER_H */