New function ds_get_line().
authorBen Pfaff <blp@nicira.com>
Wed, 17 Dec 2008 00:39:20 +0000 (16:39 -0800)
committerBen Pfaff <blp@nicira.com>
Tue, 13 Jan 2009 22:18:20 +0000 (14:18 -0800)
lib/dynamic-string.c
lib/dynamic-string.h

index 2c2effb..03efac0 100644 (file)
@@ -178,6 +178,22 @@ ds_put_strftime(struct ds *ds, const char *template, const struct tm *tm)
     }
 }
 
+int
+ds_get_line(struct ds *ds, FILE *file)
+{
+    ds_clear(ds);
+    for (;;) {
+        int c = getc(file);
+        if (c == EOF) {
+            return ds->length ? 0 : EOF;
+        } else if (c == '\n') {
+            return 0;
+        } else {
+            ds_put_char(ds, c);
+        }
+    }
+}
+
 char *
 ds_cstr(struct ds *ds)
 {
index 0fb5ce4..5529d4b 100644 (file)
@@ -38,6 +38,7 @@
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
+#include <stdio.h>
 #include "compiler.h"
 
 struct tm;
@@ -67,6 +68,8 @@ void ds_put_strftime(struct ds *, const char *, const struct tm *)
     STRFTIME_FORMAT(2);
 void ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
                      uintptr_t ofs, bool ascii);
+int ds_get_line(struct ds *, FILE *);
+
 char *ds_cstr(struct ds *);
 void ds_destroy(struct ds *);