I have seen some code in the forum using pb functions but i wanted to try the API versions.
I think in linux the lib is included but if not use: sudo apt-get install uuid-dev
In mac i used an autorelease pool for each call, i guess everything is released correctly. Wilbert?
Very easy to use, function names are self explanatory, see example.
Edit: Fixed objects release.
Edit2: Fixed objects release again.
Code: Select all
;Crossplatform UUID functions.
;Justin 11/19
;Ver: 1.2
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
	
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
	ImportC "-luuid"
		uuid_generate_time_safe_(id.i) As "uuid_generate_time_safe"
		uuid_unparse_(uuid.i, c.i) As "uuid_unparse"
		uuid_parse_(st.p-UTF8, uid.i) As "uuid_parse"
	EndImport
	
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
CompilerEndIf 
;- UUID
Structure UUID
	data1.l
	data2.w
	data3.w
	data4.b[8]
EndStructure
Macro uuid_IsEqual(uid1, uid2)
	CompareMemory(uid1, uid2, SizeOf(UUID))
EndMacro 
;Creates a new UUID in binary format.
Procedure uuid_Create(*uid.UUID)
	Protected ret
	
	If *uid = #Null : ProcedureReturn #False : EndIf
	
	ret = #False
	
	CompilerIf #PB_Compiler_OS = #PB_OS_Windows
		If UuidCreate_(*uid) = #RPC_S_OK
			ret = #True
		EndIf 
	CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux		
		If uuid_generate_time_safe_(*uid) = 0
			ret = #True
		EndIf 
	
	CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
		Protected.i idref
				
		idref = CocoaMessage(0, CocoaMessage(0, 0, "NSUUID alloc"), "init")
		If idref
			CocoaMessage(0, idref, "getUUIDBytes:", *uid)
			CocoaMessage(0, idref, "release") ;alloc
			CocoaMessage(0, idref, "release") ;init
			ret = #True
		EndIf 
	CompilerEndIf 
	
	ProcedureReturn ret 
EndProcedure
;Transforms a binary uuid to its string representation.
Procedure.s uuid_ToString(*uid.UUID)
	Protected.s st
	
	If *uid = #Null : ProcedureReturn "" : EndIf 
	CompilerIf #PB_Compiler_OS = #PB_OS_Windows
		Protected.i pst
		
		If UuidToString_(*uid, @pst) = #RPC_S_OK
			If pst
				st = PeekS(pst, -1, #PB_Unicode)
				RpcStringFree_(@pst)
			EndIf
		EndIf 
	CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
		Protected.i buf
		buf = AllocateMemory(37)
		uuid_unparse_(*uid, buf)
		st = PeekS(buf, -1, #PB_UTF8)
		FreeMemory(buf)
		
	CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
		Protected.i idref, nsStr, strBuf
				
		idref = CocoaMessage(0, CocoaMessage(0, 0, "NSUUID alloc"), "initWithUUIDBytes:", *uid)
		If idref
			nsStr = CocoaMessage(0, idref, "UUIDString")
			If nsStr
				strBuf = CocoaMessage(0, nsStr, "UTF8String")
				If strBuf
					st = PeekS(strBuf, -1, #PB_UTF8)
				EndIf
				
				CocoaMessage(0, nsStr, "release")
			EndIf 
			
			CocoaMessage(0, idref, "release") ;alloc
			CocoaMessage(0, idref, "release") ;init
		EndIf 
	CompilerEndIf 
	
	ProcedureReturn st
EndProcedure
;Transfrom a string uuid to its binary format.
Procedure uuid_FromString(st.s, *uid.UUID)
	Protected ret
	
	If *uid = #Null : ProcedureReturn #Null : EndIf
	
	ret = #False
	
	CompilerIf #PB_Compiler_OS = #PB_OS_Windows
		If UuidFromString_(st, *uid) = #RPC_S_OK
			ret = #True
		EndIf 
		
	CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
		If uuid_parse_(st, *uid) = 0
			ret = #True
		EndIf
		
	CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
		Protected.i idref
				
		idref = CocoaMessage(0, CocoaMessage(0, 0, "NSUUID alloc"), "initWithUUIDString:$", @st)
		If idref
			CocoaMessage(0, idref, "getUUIDBytes:", *uid)
			CocoaMessage(0, idref, "release") ;alloc
			CocoaMessage(0, idref, "release") ;init
			ret = #True
		EndIf 	
	CompilerEndIf 
	
	ProcedureReturn ret
EndProcedure
Procedure uuid_IsNull(*uid.UUID)
	Protected.UUID idNull
	
	ProcedureReturn uuid_IsEqual(*uid, @idNull)
EndProcedure
;- TEST
EnableExplicit
Define.s sid2
Define.UUID id1, id2
sid2 = "59a7461e-a234-4b41-8237-9be9e338b58e"
uuid_Create(@id1)
uuid_FromString(sid2, @id2)
Debug uuid_ToString(@id1)
Debug uuid_ToString(@id2)
Debug "Are equal " + uuid_IsEqual(@id1, @id2)




