GUID Generator - Module

Share your advanced PureBasic knowledge/code with the community.
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

GUID Generator - Module

Post by StarBootics »

Hello everyone,

A small module created from a small utility program created by ts-soft.

Best regards
StarBootics

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : GUID Generator
; File Name : GUID Generator - Module.pb
; File version: 1.1.0
; Programming : OK
; Programmed by : StarBootics
; Date : 27-01-2016
; Last Update : 27-01-2016
; PureBasic code : V5.41 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Notes
;
; This code is an excerpt from the GUID Generator
; tiny tool for PB IDE created by ts-soft 
; (english and german forum)
;
; I deserve credit only to convert the original 
; code into a Module.
;
; This code is free to be use where ever you like 
; but you use it at your own risk.
;
; The author can in no way be held responsible 
; for data loss, damage or other annoying 
; situations that may occur.
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule GUID
  
  Declare.s Make(Wrap_With_Brackets.b = #False)
  
EndDeclareModule

Module GUID
  
  CompilerIf Defined(GUID, #PB_Structure) = #False
    
    Structure GUID
      Data1.l
      Data2.w
      Data3.w
      Data4.b[8]
    EndStructure
    
  CompilerEndIf
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    
    ; requires uuid-dev (sudo apt-get install uuid-dev)
    ImportC "-luuid"
      uuid_generate(*generated.GUID)
      uuid_unparse_upper(*uu.GUID, *out)
    EndImport
    
  CompilerEndIf
  
  Procedure.s Make(Wrap_With_Brackets.b = #False)
    
    Protected guid.GUID, result.s
    
    CompilerSelect #PB_Compiler_OS
        
      CompilerCase #PB_OS_Linux
        
        result = Space(36)
        uuid_generate(@guid)
        uuid_unparse_upper(@guid, @result)
        
        If Wrap_With_Brackets = #True
          result = "{" + PeekS(@result, -1, #PB_Ascii) + "}"
        Else
          result = PeekS(@result, -1, #PB_Ascii)
        EndIf  
        
      CompilerCase #PB_OS_Windows
        
        Protected lpsz.s{78}
        
        If CoCreateGuid_(@guid) = #S_OK
          result = PeekS(@lpsz, StringFromGUID2_(guid, @lpsz, 76), #PB_Unicode)
          If Wrap_With_Brackets = #False
            result = RemoveString(RemoveString(result, "}"), "{")
          EndIf
        EndIf     
        
      CompilerDefault ; #PB_OS_MacOS
        
        For Index = 0 To 15
          
          If Index = 7 
            Byte.a = 64 + Random(15)
          ElseIf Index = 9
            Byte = 128 + Random(63)
          Else
            Byte = Random(255)
          EndIf
          
          If Index = 4 Or Index = 6 Or Index = 8 Or Index = 10
            result.s + "-"
          EndIf
          
          result + RSet(Hex(Byte),2,"0")
          
        Next
        
        If Wrap_With_Brackets = #True
          result = "{" + result + "}"
        EndIf
        
    CompilerEndSelect
    
    ProcedureReturn result
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  For Index = 0 To 99
    Debug GUID::Make()
  Next
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
The Stone Age did not end due to a shortage of stones !
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: GUID Generator - Module

Post by StarBootics »

Hello everyone,

A Tiny update about GUID, see first post.
A variant, UUID based on Mistrel's code

Best regards
StarBootics

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : UUID Generator
; File Name : UUID Generator - Module.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 19-04-2016
; Last Update : 19-04-2016
; PureBasic code : V5.42 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Notes
;
; This code is inspired from the UUID Generator created by Mistrel 
; (english forum)
; http://www.purebasic.fr/english/viewtopic.php?f=12&t=38011
;
; I deserve credit only to convert the original code into a Module.
; I also make simplification over the original code.
;
; This code is free to be use where ever you like but you use it at 
; your own risk.
;
; The author can in no way be held responsible For Data loss, damage 
; or other annoying situations that may occur.
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule UUID
  
  Declare.s Make(Wrap_With_Brackets.b = #False)

EndDeclareModule

Module UUID
  
  Procedure.s Make(Wrap_With_Brackets.b = #False)

    For Index = 0 To 15
      
      If Index = 7 
        Byte.a = 64 + Random(15)
      ElseIf Index = 9
        Byte = 128 + Random(63)
      Else
        Byte = Random(255)
      EndIf
      
      If Index = 4 Or Index = 6 Or Index = 8 Or Index = 10
        UUID_String.s + "-"
      EndIf
      
      UUID_String + RSet(Hex(Byte, #PB_Ascii), 2, "0")
      
    Next
    
    If Wrap_With_Brackets = #True
      UUID_String = "{" + UUID_String + "}"
    EndIf
    
    ProcedureReturn UUID_String
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  For Index = 0 To 99
    Debug UUID::Make()
  Next
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
The Stone Age did not end due to a shortage of stones !
Post Reply