Author Topic: GUIDs in QB64  (Read 156 times)

Offline SpriggsySpriggs

  • Seasoned Forum Regular
  • Posts: 259
  • DIM JohnSilver AS LONG, yarn AS STRING
    • My GitHub
GUIDs in QB64
« on: June 29, 2020, 12:44:28 PM »
Hello, all. This may or may not be wanted/needed by anyone but here is a small function that will allow you to have a super unique string (GUID) for your programs. It's been touted as "you might have a duplicate in 30 trillion". It generates a new GUID each time you call the function. Use it as you see fit.
Code: QB64: [Select]
  1. FUNCTION NewGuid$
  2.     DIM GUID AS STRING
  3.     SHELL _HIDE "PowerShell New-Guid | Out-File -FilePath gU1d -Encoding ASCII"
  4.     G = FREEFILE
  5.     OPEN "gU1d" FOR BINARY AS #G
  6.     IF LOF(G) <> 0 THEN
  7.         DO
  8.             IF NOT EOF(G) THEN
  9.                 LINE INPUT #1, empty$
  10.             END IF
  11.         LOOP UNTIL INSTR(empty$, "----")
  12.         LINE INPUT #1, GUID
  13.  
  14.         CLOSE #1
  15.         KILL "gU1d"
  16.         NewGuid = GUID
  17.     ELSE
  18.         KILL "gU1d"
  19.         NewGuid = ""
  20.     END IF
« Last Edit: June 29, 2020, 12:46:19 PM by SpriggsySpriggs »

Offline SpriggsySpriggs

  • Seasoned Forum Regular
  • Posts: 259
  • DIM JohnSilver AS LONG, yarn AS STRING
    • My GitHub
Re: GUIDs in QB64
« Reply #1 on: June 29, 2020, 12:57:58 PM »
I realize this is slow so I'm going to also make a version using a C++ library. Hang tight!

Marked as best answer by SpriggsySpriggs on June 29, 2020, 05:25:52 PM

Offline SpriggsySpriggs

  • Seasoned Forum Regular
  • Posts: 259
  • DIM JohnSilver AS LONG, yarn AS STRING
    • My GitHub
Re: GUIDs in QB64
« Reply #2 on: June 29, 2020, 02:25:46 PM »
Bwahaha! I told you I would do it! Here you go, dudes. This will create you a GUID (or UUID, same difference) every time you call it and it will be sequential for the program's execution. Enjoy!
P.S.,
Works in both 32 and 64 bit!
Code: QB64: [Select]
  1. TYPE GUID
  2.     Data1 AS _UNSIGNED LONG
  3.     Data2 AS _UNSIGNED INTEGER
  4.     Data3 AS _UNSIGNED INTEGER
  5.     Data4 AS _UNSIGNED _BYTE
  6.     Data5 AS _UNSIGNED _BYTE
  7.     Data6 AS _UNSIGNED _BYTE
  8.     Data7 AS _UNSIGNED _BYTE
  9.     Data8 AS _UNSIGNED _BYTE
  10.     Data9 AS _UNSIGNED _BYTE
  11.     Data10 AS _UNSIGNED _BYTE
  12.     Data11 AS _UNSIGNED _BYTE
  13.  
  14.     FUNCTION UuidCreateSequential%& (GUID AS GUID)
  15.     FUNCTION UuidCreate%& (GUID AS GUID)
  16.  
  17.  
  18. FOR x = 1 TO 10
  19.     PRINT SequentialGUID
  20.  
  21. FOR x = 1 TO 10
  22.     PRINT RandomGUID
  23.  
  24. FUNCTION SequentialGUID$
  25.     DIM GUID AS GUID
  26.     DO
  27.         a%& = UuidCreateSequential(GUID)
  28.         IF a%& = 0 THEN
  29.             UUID$ = HEX$(GUID.Data1) + "-" + HEX$(GUID.Data2) + "-" + HEX$(GUID.Data3) + "-" + HEX$(GUID.Data4) + HEX$(GUID.Data5) + "-" + HEX$(GUID.Data6) + HEX$(GUID.Data7) + HEX$(GUID.Data8) + HEX$(GUID.Data9) + HEX$(GUID.Data10) + HEX$(GUID.Data11)
  30.             SequentialGUID = UUID$
  31.         ELSE
  32.             SequentialGUID = ""
  33.         END IF
  34.     LOOP UNTIL LEN(UUID$) = 36
  35.  
  36. FUNCTION RandomGUID$
  37.     DIM GUID AS GUID
  38.     DO
  39.         a%& = UuidCreate(GUID)
  40.         IF a%& = 0 THEN
  41.             UUID$ = HEX$(GUID.Data1) + "-" + HEX$(GUID.Data2) + "-" + HEX$(GUID.Data3) + "-" + HEX$(GUID.Data4) + HEX$(GUID.Data5) + "-" + HEX$(GUID.Data6) + HEX$(GUID.Data7) + HEX$(GUID.Data8) + HEX$(GUID.Data9) + HEX$(GUID.Data10) + HEX$(GUID.Data11)
  42.             RandomGUID = UUID$
  43.         ELSE
  44.             RandomGUID = ""
  45.         END IF
  46.     LOOP UNTIL LEN(UUID$) = 36

Proof that they are valid GUIDs:
 
« Last Edit: June 29, 2020, 03:23:06 PM by SpriggsySpriggs »