1
0

nsguid.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "nsguid.h"
  2. #include <bfc/wasabi_std.h>
  3. #ifdef LINUX
  4. #include <uuid/uuid.h>
  5. #endif
  6. char *nsGUID::toChar(const GUID &guid, char *target)
  7. {
  8. // {1B3CA60C-DA98-4826-B4A9-D79748A5FD73}
  9. SPRINTF( target, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  10. (int)guid.Data1, (int)guid.Data2, (int)guid.Data3,
  11. (int)guid.Data4[0], (int)guid.Data4[1],
  12. (int)guid.Data4[2], (int)guid.Data4[3],
  13. (int)guid.Data4[4], (int)guid.Data4[5],
  14. (int)guid.Data4[6], (int)guid.Data4[7] );
  15. return target;
  16. }
  17. char *nsGUID::toCode(const GUID &guid, char *target)
  18. {
  19. //{ 0x1b3ca60c, 0xda98, 0x4826, { 0xb4, 0xa9, 0xd7, 0x97, 0x48, 0xa5, 0xfd, 0x73 } };
  20. SPRINTF( target, "{ 0x%08x, 0x%04x, 0x%04x, { 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x } };",
  21. (int)guid.Data1, (int)guid.Data2, (int)guid.Data3,
  22. (int)guid.Data4[0], (int)guid.Data4[1],
  23. (int)guid.Data4[2], (int)guid.Data4[3],
  24. (int)guid.Data4[4], (int)guid.Data4[5],
  25. (int)guid.Data4[6], (int)guid.Data4[7] );
  26. return target;
  27. }
  28. GUID nsGUID::fromCode(const char *source) {
  29. GUID guid = GUID_NULL;
  30. int Data1, Data2, Data3;
  31. int Data4[8] = {0};
  32. //{ 0x1b3ca60c, 0xda98, 0x4826, { 0xb4, 0xa9, 0xd7, 0x97, 0x48, 0xa5, 0xfd, 0x73 } };
  33. SSCANF( source, " { 0x%08x, 0x%04x, 0x%04x, { 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x } } ; ",
  34. &Data1, &Data2, &Data3, Data4 + 0, Data4 + 1,
  35. Data4 + 2, Data4 + 3, Data4 + 4, Data4 + 5, Data4 + 6, Data4 + 7 );
  36. // Cross assign all the values
  37. guid.Data1 = Data1;
  38. guid.Data2 = Data2;
  39. guid.Data3 = Data3;
  40. guid.Data4[0] = Data4[0];
  41. guid.Data4[1] = Data4[1];
  42. guid.Data4[2] = Data4[2];
  43. guid.Data4[3] = Data4[3];
  44. guid.Data4[4] = Data4[4];
  45. guid.Data4[5] = Data4[5];
  46. guid.Data4[6] = Data4[6];
  47. guid.Data4[7] = Data4[7];
  48. return guid;
  49. }
  50. int nsGUID::compare(const GUID &a, const GUID &b) {
  51. int delta1 = a.Data1 - b.Data1;
  52. if (delta1 == 0) {
  53. int delta2 = a.Data2 - b.Data2;
  54. if (delta2 == 0) {
  55. int delta3 = a.Data3 - b.Data3;
  56. if (delta3 == 0) {
  57. int i;
  58. for (i = 0; i < 8; i++ ) {
  59. int delta4 = a.Data4[i] - b.Data4[i];
  60. if (delta4 == 0) {
  61. continue; // :)
  62. } else {
  63. return delta4;
  64. }
  65. }
  66. } else {
  67. return delta3;
  68. }
  69. } else {
  70. return delta2;
  71. }
  72. } else {
  73. return delta1;
  74. }
  75. return 0;
  76. }
  77. #ifdef WASABI_COMPILE_CREATEGUID
  78. void nsGUID::createGuid(GUID *g) {
  79. #ifdef WIN32
  80. CoCreateGuid(g);
  81. #else
  82. uuid_t uid;
  83. uuid_generate(uid);
  84. MEMCPY(g, &uid, MIN((int)sizeof(GUID), (int)sizeof(uuid_t)));
  85. #endif
  86. }
  87. #endif
  88. wchar_t *nsGUID::toCharW(const GUID &guid, wchar_t *target)
  89. {
  90. // {1B3CA60C-DA98-4826-B4A9-D79748A5FD73}
  91. WCSNPRINTF(target, 39, L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  92. (int)guid.Data1, (int)guid.Data2, (int)guid.Data3,
  93. (int)guid.Data4[0], (int)guid.Data4[1],
  94. (int)guid.Data4[2], (int)guid.Data4[3],
  95. (int)guid.Data4[4], (int)guid.Data4[5],
  96. (int)guid.Data4[6], (int)guid.Data4[7] );
  97. return target;
  98. }
  99. GUID nsGUID::fromCharW(const wchar_t *source)
  100. {
  101. if (source == NULL) return INVALID_GUID;
  102. if (!WCSICMP(source, L"@all@"))
  103. {
  104. static GUID g={ 0xFFFFFFFF, 0xFFFF, 0xFFFF, { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } };
  105. return g;
  106. }
  107. if (WCSNICMP(source, L"guid:{", 6)==0)
  108. {
  109. source+=5;
  110. }
  111. if(WCSICMP(source, L"guid:avs")==0) {
  112. static GUID g={ 10, 12, 16, { 255, 123, 1, 1, 66, 99, 69, 12 } };
  113. return g;
  114. }
  115. if (WCSICMP(source, L"guid:pl")==0 || WCSICMP(source, L"guid:playlist")==0) {
  116. static GUID g={ 0x45f3f7c1, 0xa6f3, 0x4ee6, { 0xa1, 0x5e, 0x12, 0x5e, 0x92, 0xfc, 0x3f, 0x8d } };
  117. return g;
  118. }
  119. if (WCSICMP(source, L"guid:ml")==0 || WCSICMP(source, L"guid:musiclibrary")==0 || WCSICMP(source, L"guid:library")==0) {
  120. static GUID g={ 0x6b0edf80, 0xc9a5, 0x11d3, { 0x9f, 0x26, 0x00, 0xc0, 0x4f, 0x39, 0xff, 0xc6 } };
  121. return g;
  122. }
  123. if (WCSICMP(source, L"guid:default")==0) {
  124. static GUID g={ 0xFFFFFFFF, 0xFFFF, 0xFFFF, { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } };
  125. return g;
  126. }
  127. if (!wcschr(source, '{') || !wcschr(source, '}'))
  128. return INVALID_GUID;
  129. GUID guid = GUID_NULL;
  130. int Data1, Data2, Data3;
  131. int Data4[8] = {0};
  132. // {1B3CA60C-DA98-4826-B4A9-D79748A5FD73}
  133. int n = swscanf( source, L" { %08x - %04x - %04x - %02x%02x - %02x%02x%02x%02x%02x%02x } ",
  134. &Data1, &Data2, &Data3, Data4 + 0, Data4 + 1,
  135. Data4 + 2, Data4 + 3, Data4 + 4, Data4 + 5, Data4 + 6, Data4 + 7 );
  136. if (n != 11) return INVALID_GUID;
  137. // Cross assign all the values
  138. guid.Data1 = Data1;
  139. guid.Data2 = Data2;
  140. guid.Data3 = Data3;
  141. guid.Data4[0] = Data4[0];
  142. guid.Data4[1] = Data4[1];
  143. guid.Data4[2] = Data4[2];
  144. guid.Data4[3] = Data4[3];
  145. guid.Data4[4] = Data4[4];
  146. guid.Data4[5] = Data4[5];
  147. guid.Data4[6] = Data4[6];
  148. guid.Data4[7] = Data4[7];
  149. return guid;
  150. }