[gnome-battery-bench] Add utility function go generate UUIDs



commit 34e0664cbc44c5cb7d251cae7146f554f2bbfb11
Author: Christian Kellner <gicmo gnome org>
Date:   Mon Mar 13 17:56:48 2017 +0100

    Add utility function go generate UUIDs

 src/util.c |   28 ++++++++++++++++++++++++++++
 src/util.h |    3 +++
 2 files changed, 31 insertions(+), 0 deletions(-)
---
diff --git a/src/util.c b/src/util.c
index 49cea60..5c1d59a 100644
--- a/src/util.c
+++ b/src/util.c
@@ -1,9 +1,12 @@
+/* -*- mode: C; c-file-style: "stroustrup"; indent-tabs-mode: nil; -*- */
+
 #include "util.h"
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <string.h>
 
 void
 die(const char *msg, ...)
@@ -40,3 +43,28 @@ break_time(double span,
     *m = span_seconds / 60 - *h * 60;
     *s = span_seconds - *h *3600 - *m * 60;
 }
+
+gchar *
+uuid_gen_new(void)
+{
+    guint8 bytes[32];
+    int i;
+
+    for (i = 0; i < 4; i++) {
+        guint32 n = g_random_int();
+        memcpy(bytes + i * sizeof(guint32), &n, sizeof(n));
+    }
+
+    bytes[6] &= 0x0f;
+    bytes[6] |= 4 << 4;
+    bytes[8] &= 0x3f;
+    bytes[8] |= 0x80;
+
+    return g_strdup_printf("%02x%02x%02x%02x-%02x%02x"
+                           "-%02x%02x-%02x%02x"
+                           "-%02x%02x%02x%02x%02x%02x",
+                           bytes[0],  bytes[1],  bytes[2],  bytes[3],
+                           bytes[4],  bytes[5],  bytes[6],  bytes[7],
+                           bytes[8],  bytes[9],  bytes[10], bytes[11],
+                           bytes[12], bytes[13], bytes[14], bytes[15]);
+}
diff --git a/src/util.h b/src/util.h
index a131c19..976f17e 100644
--- a/src/util.h
+++ b/src/util.h
@@ -1,6 +1,8 @@
 #ifndef __UTIL_H__
 #define __UTIL_H__
 
+#include <glib.h>
+
 void die(const char *msg, ...)
     __attribute__ ((noreturn))
     __attribute__ ((format (printf, 1, 2)));
@@ -13,5 +15,6 @@ void break_time(double span,
                 int *h, int *m, int *s);
 
 
+gchar *uuid_gen_new(void);
 
 #endif /* __UTIL_H__ */


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