core_foundation.odin 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //+build darwin
  2. package darwin
  3. import "base:runtime"
  4. foreign import core_foundation "system:CoreFoundation.framework"
  5. CFTypeRef :: distinct rawptr
  6. CFStringRef :: distinct CFTypeRef
  7. CFIndex :: int
  8. CFRange :: struct {
  9. location: CFIndex,
  10. length: CFIndex,
  11. }
  12. CFStringEncoding :: enum u32 {
  13. ASCII = 1,
  14. NEXTSTEP = 2,
  15. JapaneseEUC = 3,
  16. UTF8 = 4,
  17. ISOLatin1 = 5,
  18. Symbol = 6,
  19. NonLossyASCII = 7,
  20. ShiftJIS = 8,
  21. ISOLatin2 = 9,
  22. Unicode = 10,
  23. WindowsCP1251 = 11,
  24. WindowsCP1252 = 12,
  25. WindowsCP1253 = 13,
  26. WindowsCP1254 = 14,
  27. WindowsCP1250 = 15,
  28. ISO2022JP = 21,
  29. MacOSRoman = 30,
  30. UTF16 = Unicode,
  31. UTF16BigEndian = 0x90000100,
  32. UTF16LittleEndian = 0x94000100,
  33. UTF32 = 0x8c000100,
  34. UTF32BigEndian = 0x98000100,
  35. UTF32LittleEndian = 0x9c000100,
  36. }
  37. foreign core_foundation {
  38. // Copies the character contents of a string to a local C string buffer after converting the characters to a given encoding.
  39. CFStringGetCString :: proc(theString: CFStringRef, buffer: [^]byte, bufferSize: CFIndex, encoding: CFStringEncoding) -> Bool ---
  40. // Returns the number (in terms of UTF-16 code pairs) of Unicode characters in a string.
  41. CFStringGetLength :: proc(theString: CFStringRef) -> CFIndex ---
  42. // Returns the maximum number of bytes a string of a specified length (in Unicode characters) will take up if encoded in a specified encoding.
  43. CFStringGetMaximumSizeForEncoding :: proc(length: CFIndex, encoding: CFStringEncoding) -> CFIndex ---
  44. // Fetches a range of the characters from a string into a byte buffer after converting the characters to a specified encoding.
  45. CFStringGetBytes :: proc(
  46. thestring: CFStringRef,
  47. range: CFRange,
  48. encoding: CFStringEncoding,
  49. lossByte: u8,
  50. isExternalRepresentation: Bool,
  51. buffer: [^]byte,
  52. maxBufLen: CFIndex,
  53. usedBufLen: ^CFIndex,
  54. ) -> CFIndex ---
  55. // Releases a Core Foundation object.
  56. @(link_name="CFRelease")
  57. _CFRelease :: proc(cf: CFTypeRef) ---
  58. }
  59. // Releases a Core Foundation object.
  60. CFRelease :: proc {
  61. CFReleaseString,
  62. }
  63. // Releases a Core Foundation string.
  64. CFReleaseString :: #force_inline proc(theString: CFStringRef) {
  65. _CFRelease(CFTypeRef(theString))
  66. }
  67. CFStringCopyToOdinString :: proc(theString: CFStringRef, allocator := context.allocator) -> (str: string, ok: bool) #optional_ok {
  68. length := CFStringGetLength(theString)
  69. max := CFStringGetMaximumSizeForEncoding(length, .UTF8)
  70. buf, err := make([]byte, max, allocator)
  71. if err != nil { return }
  72. raw_str := runtime.Raw_String{
  73. data = raw_data(buf),
  74. }
  75. CFStringGetBytes(theString, {0, length}, .UTF8, 0, false, raw_data(buf), max, &raw_str.len)
  76. return transmute(string)raw_str, true
  77. }