add support for \n line endings as well
authorTero Marttila <terom@fixme.fi>
Sat, 28 Feb 2009 19:08:51 +0200
changeset 13 ca16f3a8f3b7
parent 12 4147fae232d9
child 14 3a70e5901f17
add support for \n line endings as well
src/line_proto.c
src/nexus.c
--- a/src/line_proto.c	Sat Feb 28 18:48:10 2009 +0200
+++ b/src/line_proto.c	Sat Feb 28 19:08:51 2009 +0200
@@ -122,21 +122,28 @@
  *
  */
 int _parse_line (char *buf, size_t len, size_t *hint) {
-    int i;
+    int i, next = 0;
 
     // empty buffer -> nothing
     if (len == 0)
         return 0;
 
-    // look for terminating '\r\n' sequence
-    for (i = *hint; i < len - 1; i++) {
-        // match this + next char
-        if (buf[i] == '\r' && buf[i + 1] == '\n')
+    // look for terminating '\r\n' or '\n' sequence
+    for (i = *hint; i < len; i++) {
+        // match this + next char?
+        if (i < len - 1 && buf[i] == '\r' && buf[i + 1] == '\n') {
+            next = i + 2;
             break;
+
+        } else if (buf[i] == '\n') {
+            next = i + 1;
+            break;
+        }
     }
 
-    // incomplete?
-    if (i >= len - 1) {
+    // searched the whole buffer?
+    if (i >= len) {
+        // do continue one char back, to keep any \r
         *hint = len - 1;
         return 0;
     }
@@ -144,8 +151,8 @@
     // mangle the newline off
     buf[i] = '\0';
 
-    // return offset to next line
-    return i + 2;
+    // return offset to next line, as set in loop based on delim
+    return next;
 }
 
 err_t line_proto_read (struct line_proto *lp, const char **line_ptr)
--- a/src/nexus.c	Sat Feb 28 18:48:10 2009 +0200
+++ b/src/nexus.c	Sat Feb 28 19:08:51 2009 +0200
@@ -13,7 +13,7 @@
 #include "line_proto.h"
 
 #define CONNECT_HOST "localhost"
-#define CONNECT_SERV "5001"
+#define CONNECT_SERV "5002"
 #define LINE_LENGTH 512
 
 void on_line (const char *line, void *arg) {
@@ -38,8 +38,8 @@
     if (sock_tcp_connect(&sock, CONNECT_HOST, CONNECT_SERV, &_err))
         errx(1, "sock_gnutls_connect: %s", error_msg(&_err));
 
-    // line protocol
-    if (line_proto_create(&lp, sock, LINE_LENGTH, on_line, NULL, &_err))
+    // line protocol, with safety margin for buffer
+    if (line_proto_create(&lp, sock, LINE_LENGTH * 2, on_line, NULL, &_err))
         errx(1, "line_proto_create: %s", error_msg(&_err));
 
     // run event loop