mac-helpers.h 685 B

12345678910111213141516171819202122232425262728293031323334
  1. #pragma once
  2. #include <util/dstr.h>
  3. static inline bool mac_success(OSStatus stat, const char *action)
  4. {
  5. if (stat != noErr) {
  6. blog(LOG_WARNING, "%s failed: %d", action, (int)stat);
  7. return false;
  8. }
  9. return true;
  10. }
  11. static inline bool cf_to_cstr(CFStringRef ref, char *buf, size_t size)
  12. {
  13. if (!ref) return false;
  14. return (bool)CFStringGetCString(ref, buf, size, kCFStringEncodingUTF8);
  15. }
  16. static inline bool cf_to_dstr(CFStringRef ref, struct dstr *str)
  17. {
  18. size_t size;
  19. if (!ref) return false;
  20. size = (size_t)CFStringGetLength(ref);
  21. if (!size)
  22. return false;
  23. dstr_resize(str, size);
  24. return (bool)CFStringGetCString(ref, str->array, size+1,
  25. kCFStringEncodingUTF8);
  26. }