]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - string.c
string: Add discard feature for for_each_line().
[paraslash.git] / string.c
index c22dc7cfce51954c65ecd407857e0352b09a53f3..ba744bff2da640eed755fe14775e4f72b849766f 100644 (file)
--- a/string.c
+++ b/string.c
@@ -370,10 +370,10 @@ __malloc char *para_hostname(void)
  * incomplete line) is moved to the beginning of the buffer if FELF_READ_ONLY is
  * unset.
  *
- * \return If \p FELF_READ_ONLY is not set in \a flags, this function returns
- * the number of bytes not handled to \p line_handler, otherwise the number of
- * complete lines. On errors the negative error code of the \p line_handler is
- * returned.
+ * \return On success this function returns the number of bytes not handled to
+ * \p line_handler. The only possible error is a negative return value from the
+ * line handler. In this case processing stops and the return value of the line
+ * handler is returned to indicate failure.
  *
  * \sa \ref for_each_line_flags.
  */
@@ -399,26 +399,25 @@ int for_each_line(unsigned flags, char *buf, size_t size,
                } else
                        end = next_cr;
                num_lines++;
-               if (flags & FELF_READ_ONLY) {
-                       size_t s = end - start;
-                       char *b = para_malloc(s + 1);
-                       memcpy(b, start, s);
-                       b[s] = '\0';
-//                     PARA_NOTICE_LOG("b: %s, start: %s\n", b, start);
-                       ret = line_handler(b, private_data);
-                       free(b);
-               } else {
-                       *end = '\0';
-                       ret = line_handler(start, private_data);
+               if (!(flags & FELF_DISCARD_FIRST) || start != buf) {
+                       if (flags & FELF_READ_ONLY) {
+                               size_t s = end - start;
+                               char *b = para_malloc(s + 1);
+                               memcpy(b, start, s);
+                               b[s] = '\0';
+                               ret = line_handler(b, private_data);
+                               free(b);
+                       } else {
+                               *end = '\0';
+                               ret = line_handler(start, private_data);
+                       }
+                       if (ret < 0)
+                               return ret;
                }
-               if (ret < 0)
-                       return ret;
                start = ++end;
        }
-       if (flags & FELF_READ_ONLY)
-               return num_lines;
        i = buf + size - start;
-       if (i && i != size)
+       if (i && i != size && !(flags & FELF_READ_ONLY))
                memmove(buf, start, i);
        return i;
 }