[recipes] Add a utility function to generate IDs



commit d201e8745329d4884a5b9ccdbdb61e34ba543358
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jan 2 10:14:51 2017 -0500

    Add a utility function to generate IDs
    
    This will be used to generate recipe IDs that are usable
    as CSS element names.
    
    This commit also includes a simple facility for printing out
    timestamps for performance debugging that is currently unused.

 src/gr-utils.c |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gr-utils.h |    6 ++++++
 2 files changed, 61 insertions(+), 0 deletions(-)
---
diff --git a/src/gr-utils.c b/src/gr-utils.c
index 2b0083b..2c0cd5d 100644
--- a/src/gr-utils.c
+++ b/src/gr-utils.c
@@ -22,6 +22,7 @@
 
 #include <glib/gi18n.h>
 #include <stdlib.h>
+#include <stdarg.h>
 
 #include "gr-utils.h"
 
@@ -327,3 +328,57 @@ translate_multiline_string (const char *s)
 
         return g_string_free (out, FALSE);
 }
+
+char *
+generate_id (const char *first_string, ...)
+{
+        va_list ap;
+
+        const char *s;
+        const char *q;
+        GString *str;
+
+        va_start (ap, first_string);
+
+        str = g_string_new ("");
+
+        s = first_string;
+        while (s) {
+                for (q = s; *q; q = g_utf8_find_next_char (q, NULL)) {
+                        gunichar ch = g_utf8_get_char (q);
+                        if (ch > 128 || ch == '.' || ch == ' ' ||
+                            ch == ']' || ch == '[' || g_ascii_iscntrl ((char)ch))
+                                g_string_append_c (str, '_');
+                        else
+                                g_string_append_c (str, (char)ch);
+                }
+
+                s = va_arg (ap, const char *);
+        }
+
+        va_end (ap);
+
+        return g_string_free (str, FALSE);
+}
+
+static gint64 start_time;
+
+void
+start_recording (void)
+{
+        start_time = g_get_monotonic_time ();
+}
+
+void
+record_step (const char *blurb)
+{
+        if (start_time != 0)
+                g_print ("%0.3f %s\n", 0.001 * (g_get_monotonic_time () - start_time), blurb);
+}
+
+void
+stop_recording (void)
+{
+        start_time = 0;
+}
+
diff --git a/src/gr-utils.h b/src/gr-utils.h
index 79807d5..126b66f 100644
--- a/src/gr-utils.h
+++ b/src/gr-utils.h
@@ -49,3 +49,9 @@ gboolean skip_whitespace (char **input);
 gboolean space_or_nul    (char p);
 
 char *translate_multiline_string (const char *s);
+
+char *generate_id (const char *s, ...);
+
+void start_recording (void);
+void stop_recording (void);
+void record_step (const char *blurb);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]