[fltk.coredev] Improve function Fl_X11_System_Driver::newUUID()

GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.coredev  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Improve function Fl_X11_System_Driver::newUUID() Manolo 06:55 Sep 30 top right image
 
I'd like to improve function Fl_X11_System_Driver::newUUID()
that generates UUIDs having it call function uuid_generate() from libuuid
when it's found at run-time. The code falls back to the present ad hoc method
when uuid_generate() isn't available.

That's in attached uuid_generate.patch.txt

The modified code can be tested adding
     fprintf(stderr, "UUID=%s\n", Fl_Preferences::newUUID());
as first statement in main() of test/preferences.cxx

--
You received this message because you are subscribed to the Google Groups "fltk.coredev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkcoredev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkcoredev/e216a838-1f1e-4962-807c-42352f2b8959o%40googlegroups.com.
diff --git a/src/drivers/X11/Fl_X11_System_Driver.cxx b/src/drivers/X11/Fl_X11_System_Driver.cxx
index 6dcaee06d..8a203fd76 100644
--- a/src/drivers/X11/Fl_X11_System_Driver.cxx
+++ b/src/drivers/X11/Fl_X11_System_Driver.cxx
@@ -28,6 +28,10 @@
 #include <pwd.h>
 #include <string.h>     // strerror(errno)
 #include <errno.h>      // errno
+#if HAVE_DLSYM && HAVE_DLFCN_H
+#include <dlfcn.h>   // for dlopen et al
+#endif
+
 
 #if defined(_AIX)
 extern "C" {
@@ -358,6 +362,22 @@ void Fl_X11_System_Driver::newUUID(char *uuidBuffer)
   // #include <uuid/uuid.h>
   // void uuid_generate(uuid_t out);
   unsigned char b[16];
+#if HAVE_DLSYM && HAVE_DLFCN_H
+  typedef void (*gener_f_type)(uchar*);
+  static bool looked_for_uuid_generate = false;
+  static gener_f_type uuid_generate_f = NULL;
+  if (!looked_for_uuid_generate) {
+    looked_for_uuid_generate = true;
+    void *libuuid = this->dlopen("libuuid.so");
+    if (libuuid) {
+      uuid_generate_f = (gener_f_type)dlsym(libuuid, "uuid_generate");
+    }
+  }
+  if (uuid_generate_f) {
+    uuid_generate_f(b);
+  } else
+#endif
+  {
   time_t t = time(0);                   // first 4 byte
   b[0] = (unsigned char)t;
   b[1] = (unsigned char)(t>>8);
@@ -394,6 +414,7 @@ void Fl_X11_System_Driver::newUUID(char *uuidBuffer)
   char name[80];                        // last four bytes
   gethostname(name, 79);
   memcpy(b+12, name, 4);
+}
   sprintf(uuidBuffer, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
           b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
           b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]);
@@ -549,9 +570,6 @@ int Fl_X11_System_Driver::utf8locale() {
   return ret;
 }
 
-#if HAVE_DLSYM && HAVE_DLFCN_H
-#include <dlfcn.h>   // for dlopen et al
-#endif
 #if HAVE_DLSYM && HAVE_DLFCN_H && defined(RTLD_DEFAULT)
 
 bool Fl_X11_System_Driver::probe_for_GTK(int major, int minor, void **ptr_gtk) {
Direct Link to Message ]
 
bottom left image   bottom right image
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2020 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.