guid_darwin.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (C) 2021-2021 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <pj/guid.h>
  19. #include <pj/assert.h>
  20. #include <pj/os.h>
  21. #include <pj/string.h>
  22. #include <CoreFoundation/CoreFoundation.h>
  23. PJ_DEF_DATA(const unsigned) PJ_GUID_STRING_LENGTH=36;
  24. PJ_DEF(unsigned) pj_GUID_STRING_LENGTH()
  25. {
  26. return PJ_GUID_STRING_LENGTH;
  27. }
  28. PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str)
  29. {
  30. CFUUIDRef uuid_obj;
  31. CFStringRef uuid_str;
  32. pj_str_t sguid;
  33. PJ_ASSERT_RETURN(str->ptr != NULL, NULL);
  34. PJ_CHECK_STACK();
  35. /* Create universally unique identifier (object). */
  36. uuid_obj = CFUUIDCreate(kCFAllocatorDefault);
  37. /* Get the string representation of CFUUID object. */
  38. uuid_str = CFUUIDCreateString(kCFAllocatorDefault, uuid_obj);
  39. CFRelease(uuid_obj);
  40. sguid.ptr = (char*)CFStringGetCStringPtr(uuid_str, kCFStringEncodingUTF8);
  41. pj_assert(sguid.ptr);
  42. sguid.slen = pj_ansi_strlen(sguid.ptr);
  43. pj_strncpy(str, &sguid, PJ_GUID_STRING_LENGTH);
  44. CFRelease(uuid_str);
  45. return str;
  46. }