Daniele Bartolini 9 år sedan
förälder
incheckning
350fe880b1
2 ändrade filer med 123 tillägg och 0 borttagningar
  1. 74 0
      src/core/guid.cpp
  2. 49 0
      src/core/guid.h

+ 74 - 0
src/core/guid.cpp

@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "dynamic_string.h"
+#include "guid.h"
+#include "platform.h"
+
+#if CROWN_PLATFORM_POSIX
+	#include <fcntl.h>
+	#include <unistd.h>
+	#include <errno.h>
+#elif CROWN_PLATFORM_WINDOWS
+	#include <objbase.h>
+#endif // CROWN_PLATFORM_POSIX
+
+namespace crown
+{
+namespace guid
+{
+	Guid new_guid()
+	{
+		Guid guid;
+#if CROWN_PLATFORM_POSIX
+		int fd = open("/dev/urandom", O_RDONLY);
+		CE_ASSERT(fd != -1, "open: erron = %d", errno);
+		read(fd, &guid, sizeof(guid));
+		close(fd);
+		guid.data3 = (guid.data3 & 0x4fffu) | 0x4000u;
+		guid.data4 = (guid.data4 & 0x3fffffffffffffffu) | 0x8000000000000000u;
+#elif CROWN_PLATFORM_WINDOWS
+		HRESULT hr = CoCreateGuid((GUID*)&guid);
+		CE_ASSERT(hr == S_OK, "CoCreateGuid: error");
+		CE_UNUSED(hr);
+#endif // CROWN_PLATFORM_POSIX
+		return guid;
+	}
+
+	Guid parse(const char* str)
+	{
+		Guid guid;
+		try_parse(str, guid);
+		return guid;
+	}
+
+	bool try_parse(const char* str, Guid& guid)
+	{
+		CE_ASSERT_NOT_NULL(str);
+		u32 a, b, c, d, e, f;
+		int num = sscanf(str, "%8x-%4x-%4x-%4x-%4x%8x", &a, &b, &c, &d, &e, &f);
+		guid.data1 = a;
+		guid.data2 = (u16)(b & 0x0000ffffu);
+		guid.data3 = (u16)(c & 0x0000ffffu);
+		guid.data4 = (u64)(d & 0x0000ffffu) << 48 | (u64)(e & 0x0000ffffu) << 32 | (u64)f;
+		return num == 6;
+	}
+
+	void to_string(const Guid& guid, DynamicString& s)
+	{
+		char str[36+1];
+		snprintf(str, sizeof(str), "%.8x-%.4x-%.4x-%.4x-%.4x%.8x"
+			, guid.data1
+			, guid.data2
+			, guid.data3
+			, (u16)((guid.data4 & 0xffff000000000000u) >> 48)
+			, (u16)((guid.data4 & 0x0000ffff00000000u) >> 32)
+			, (u32)((guid.data4 & 0x00000000ffffffffu) >>  0)
+			);
+		s.set(str, sizeof(str)-1);
+	}
+}
+
+} // namespace crown

+ 49 - 0
src/core/guid.h

@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "string_types.h"
+#include "types.h"
+
+namespace crown
+{
+struct Guid
+{
+	u32 data1;
+	u16 data2;
+	u16 data3;
+	u64 data4;
+};
+
+/// Functions to manipulate Guid.
+///
+/// @ingroup Core
+namespace guid
+{
+	/// Returns a new randomly generated Guid.
+	Guid new_guid();
+
+	/// Parses the guid from @a str.
+	Guid parse(const char* str);
+
+	/// Parses the @a guid from @a str and returns true if success.
+	bool try_parse(const char* str, Guid& guid);
+
+	/// Fills @a s with the string representation of the @a guid.
+	void to_string(const Guid& guid, DynamicString& s);
+}
+
+/// Returns whether Guid @a and @b are equal.
+inline bool operator==(const Guid& a, const Guid& b)
+{
+	return a.data1 == b.data1
+		&& a.data2 == b.data2
+		&& a.data3 == b.data3
+		&& a.data4 == b.data4
+		;
+}
+
+const Guid GUID_ZERO = { 0u, 0u, 0u, 0u };
+
+} // namespace crown