heap: New library that implements a binary heap-based priority queue.
[sliver-openvswitch.git] / lib / dynamic-string.c
index 3af7fc9..8c675d1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -173,13 +173,19 @@ ds_put_printable(struct ds *ds, const char *s, size_t n)
     }
 }
 
+/* Writes the current time to 'string' based on 'template'.
+ * The current time is either localtime or UTC based on 'utc'. */
 void
-ds_put_strftime(struct ds *ds, const char *template, const struct tm *tm)
+ds_put_strftime(struct ds *ds, const char *template, bool utc)
 {
-    if (!tm) {
-        time_t now = time_wall();
+    const struct tm *tm;
+    time_t now = time_wall();
+    if (utc) {
+        tm = gmtime(&now);
+    } else {
         tm = localtime(&now);
     }
+
     for (;;) {
         size_t avail = ds->string ? ds->allocated - ds->length + 1 : 0;
         size_t used = strftime(&ds->string[ds->length], avail, template, tm);
@@ -207,6 +213,32 @@ ds_get_line(struct ds *ds, FILE *file)
     }
 }
 
+/* Reads a line from 'file' into 'ds', clearing anything initially in 'ds'.
+ * Deletes comments introduced by "#" and skips lines that contains only white
+ * space (after deleting comments).
+ *
+ * Returns 0 if successful, EOF if no non-blank line was found. */
+int
+ds_get_preprocessed_line(struct ds *ds, FILE *file)
+{
+    while (!ds_get_line(ds, file)) {
+        char *line = ds_cstr(ds);
+        char *comment;
+
+        /* Delete comments. */
+        comment = strchr(line, '#');
+        if (comment) {
+            *comment = '\0';
+        }
+
+        /* Return successfully unless the line is all spaces. */
+        if (line[strspn(line, " \t\n")] != '\0') {
+            return 0;
+        }
+    }
+    return EOF;
+}
+
 char *
 ds_cstr(struct ds *ds)
 {