comparison dep/animia/src/util/osx.cc @ 189:649786bae914

*: etc. code cleanup I've removed most macros and stuff dep/animia: [UNTESTED] use raw C++ instead of Objective-C++
author Paper <mrpapersonic@gmail.com>
date Wed, 06 Dec 2023 19:42:33 -0500
parents c413e475f496
children 2d5823df870f
comparison
equal deleted inserted replaced
188:168382a89b21 189:649786bae914
1 #include "animia/util/osx.h" 1 #include "animia/util/osx.h"
2 2
3 #include <string> 3 #include <string>
4 #include <memory> 4 #include <memory>
5 5
6 #ifdef HAVE_COREFOUNDATION
7 #include <CoreFoundation/CoreFoundation.h>
8 #endif
9
10 namespace animia::internal::osx::util { 6 namespace animia::internal::osx::util {
11 7
12 #ifdef HAVE_COREFOUNDATION 8 #ifdef HAVE_COREFOUNDATION
13 /* I don't want to have to call CFRelease */
14 template<typename T>
15 struct CFDeleter {
16 using pointer = T;
17 void operator()(pointer p) { CFRelease(p); }
18 }
19
20 template<typename T>
21 typedef CFReference = std::unique_ptr<T, CFDeleter>;
22
23 /* all of these LaunchServices things use *internal functions* that are subject 9 /* all of these LaunchServices things use *internal functions* that are subject
24 * to change. Granted, it's not very likely that these will change very much 10 * to change. Granted, it's not very likely that these will change very much
25 * because I'm fairly sure Apple uses them lots in their own internal code. 11 * because I'm fairly sure Apple uses them lots in their own internal code.
26 */ 12 */
27 typedef CFTypeRef (*LSASNCreateWithPidSpec)(CFAllocatorRef, pid_t); 13 typedef CFTypeRef (*LSASNCreateWithPidSpec)(CFAllocatorRef, pid_t);
88 74
89 if (!CFStringGetCString(str.get(), &result.front(), result.length(), result.length())) 75 if (!CFStringGetCString(str.get(), &result.front(), result.length(), result.length()))
90 return false; 76 return false;
91 77
92 result.resize(result.find('\0')); 78 result.resize(result.find('\0'));
79
80 return true;
81 }
82
83 bool StringFromCFString(CFStringRef string, std::string& result) {
84 if (!string)
85 return false;
86
87 result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str.get()), kCFStringEncodingUTF8) + 1);
88 if (!CFStringGetCString(str.get(), &result.front(), result.length(), result.length()))
89 return false;
93 90
94 return true; 91 return true;
95 } 92 }
96 #endif // HAVE_COREFOUNDATION 93 #endif // HAVE_COREFOUNDATION
97 94