src/line_proto.c
changeset 13 ca16f3a8f3b7
parent 12 4147fae232d9
child 17 5001564ac5fc
equal deleted inserted replaced
12:4147fae232d9 13:ca16f3a8f3b7
   120  * The given \a hint is an hint as to the offset at which to start scanning, used for incremental invocations of this
   120  * The given \a hint is an hint as to the offset at which to start scanning, used for incremental invocations of this
   121  * on the same buffer.
   121  * on the same buffer.
   122  *
   122  *
   123  */
   123  */
   124 int _parse_line (char *buf, size_t len, size_t *hint) {
   124 int _parse_line (char *buf, size_t len, size_t *hint) {
   125     int i;
   125     int i, next = 0;
   126 
   126 
   127     // empty buffer -> nothing
   127     // empty buffer -> nothing
   128     if (len == 0)
   128     if (len == 0)
   129         return 0;
   129         return 0;
   130 
   130 
   131     // look for terminating '\r\n' sequence
   131     // look for terminating '\r\n' or '\n' sequence
   132     for (i = *hint; i < len - 1; i++) {
   132     for (i = *hint; i < len; i++) {
   133         // match this + next char
   133         // match this + next char?
   134         if (buf[i] == '\r' && buf[i + 1] == '\n')
   134         if (i < len - 1 && buf[i] == '\r' && buf[i + 1] == '\n') {
   135             break;
   135             next = i + 2;
   136     }
   136             break;
   137 
   137 
   138     // incomplete?
   138         } else if (buf[i] == '\n') {
   139     if (i >= len - 1) {
   139             next = i + 1;
       
   140             break;
       
   141         }
       
   142     }
       
   143 
       
   144     // searched the whole buffer?
       
   145     if (i >= len) {
       
   146         // do continue one char back, to keep any \r
   140         *hint = len - 1;
   147         *hint = len - 1;
   141         return 0;
   148         return 0;
   142     }
   149     }
   143 
   150 
   144     // mangle the newline off
   151     // mangle the newline off
   145     buf[i] = '\0';
   152     buf[i] = '\0';
   146 
   153 
   147     // return offset to next line
   154     // return offset to next line, as set in loop based on delim
   148     return i + 2;
   155     return next;
   149 }
   156 }
   150 
   157 
   151 err_t line_proto_read (struct line_proto *lp, const char **line_ptr)
   158 err_t line_proto_read (struct line_proto *lp, const char **line_ptr)
   152 {
   159 {
   153     // offset to recv() new data into, offset to _parse_line hint, offset to next line from _parse_line
   160     // offset to recv() new data into, offset to _parse_line hint, offset to next line from _parse_line