Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #2082 from tru/cfstring_get_cstring_spring_cleaning

[OSX] Remove all direct calls to CFStringGetCString and CFStringGetCStringPtr
  • Loading branch information...
commit efd66862afe11c17b38dc2417551de97a1eeab10 2 parents 0cfa24e + 4b4bb27
@davilla davilla authored
View
6 xbmc/cores/AudioEngine/Engines/CoreAudio/CoreAudioHardware.cpp
@@ -22,6 +22,7 @@
#include "CoreAudioAEHAL.h"
#include "utils/log.h"
+#include "osx/DarwinUtils.h"
bool CCoreAudioHardware::GetAutoHogMode()
{
@@ -329,9 +330,8 @@ void CCoreAudioHardware::GetOutputDeviceName(std::string& name)
if (ret != noErr)
return;
- const char *cstr = CFStringGetCStringPtr(theDeviceName, CFStringGetSystemEncoding());
- if (cstr)
- name = cstr;
+ DarwinCFStringRefToString(theDeviceName, name);
+
CFRelease(theDeviceName);
}
}
View
54 xbmc/network/osx/ZeroconfBrowserOSX.cpp
@@ -26,33 +26,13 @@
#include "guilib/GUIMessage.h"
#include "threads/SingleLock.h"
#include "utils/log.h"
+#include "osx/DarwinUtils.h"
#include <arpa/inet.h>
#include <netinet/in.h>
namespace
{
- CStdString CFStringToCStdString(const CFStringRef cfstr)
- {
- //first try the short path
- const char *p_tmp = CFStringGetCStringPtr(cfstr, kCFStringEncodingUTF8);
- if (p_tmp)
- return CStdString(p_tmp);
-
- // i'm not sure if CFStringGetMaximumSizeForEncoding
- // includes space for the termination character or not?
- // so i add 1 here to make sure..
- CFIndex buf_len = 1 + CFStringGetMaximumSizeForEncoding(
- CFStringGetLength(cfstr), kCFStringEncodingUTF8);
-
- char *buffer = new char[buf_len];
- CFStringGetCString(cfstr, buffer, buf_len, kCFStringEncodingUTF8);
- CStdString myString(buffer);
- delete[] buffer;
-
- return myString;
- }
-
//helper for getting a the txt-records list
//returns true on success, false if nothing found or error
CZeroconfBrowser::ZeroconfService::tTxtRecordMap GetTxtRecords(CFNetServiceRef serviceRef)
@@ -78,12 +58,16 @@ namespace
for(idx = 0; idx < numValues; idx++)
{
- recordMap.insert(
- std::make_pair(
- CFStringToCStdString(keys[idx]),
- CStdString((const char *)CFDataGetBytePtr(values[idx]))
- )
- );
+ std::string key;
+ if (DarwinCFStringRefToString(keys[idx], key))
+ {
+ recordMap.insert(
+ std::make_pair(
+ key,
+ CStdString((const char *)CFDataGetBytePtr(values[idx]))
+ )
+ );
+ }
}
}
CFRelease(dict);
@@ -161,11 +145,19 @@ void CZeroconfBrowserOSX::BrowserCallback(CFNetServiceBrowserRef browser, CFOpti
assert(service);
//get our instance
CZeroconfBrowserOSX* p_this = reinterpret_cast<CZeroconfBrowserOSX*>(info);
+
//store the service
- ZeroconfService s(
- CFStringToCStdString(CFNetServiceGetName(service)),
- CFStringToCStdString(CFNetServiceGetType(service)),
- CFStringToCStdString(CFNetServiceGetDomain(service)));
+ std::string name, type, domain;
+ if (!DarwinCFStringRefToString(CFNetServiceGetName(service), name) ||
+ !DarwinCFStringRefToString(CFNetServiceGetType(service), type) ||
+ !DarwinCFStringRefToString(CFNetServiceGetDomain(service), domain))
+ {
+ CLog::Log(LOGWARNING, "CZeroconfBrowserOSX::BrowserCallback failed to convert service strings.");
+ return;
+ }
+
+ ZeroconfService s(name, type, domain);
+
if (flags & kCFNetServiceFlagRemove)
{
CLog::Log(LOGDEBUG, "CZeroconfBrowserOSX::BrowserCallback service named: %s, type: %s, domain: %s disappeared",
View
6 xbmc/osx/DarwinUtils.h
@@ -22,6 +22,11 @@
#include <string>
+// We forward declare CFStringRef in order to avoid
+// pulling in tons of Objective-C headers.
+struct __CFString;
+typedef const struct __CFString * CFStringRef;
+
#ifdef __cplusplus
extern "C"
{
@@ -35,6 +40,7 @@ extern "C"
bool DarwinHasVideoToolboxDecoder(void);
int DarwinBatteryLevel(void);
void DarwinSetScheduling(int message);
+ bool DarwinCFStringRefToString(CFStringRef source, std::string& destination);
#ifdef __cplusplus
}
#endif
View
29 xbmc/osx/DarwinUtils.mm
@@ -35,6 +35,7 @@
#import <sys/sysctl.h>
#else
#import <Cocoa/Cocoa.h>
+ #import <CoreFoundation/CoreFoundation.h>
#import <IOKit/ps/IOPowerSources.h>
#import <IOKit/ps/IOPSKeys.h>
#endif
@@ -316,4 +317,32 @@ void DarwinSetScheduling(int message)
result = pthread_setschedparam(this_pthread_self, policy, &param );
}
+bool DarwinCFStringRefToString(CFStringRef source, std::string &destination)
+{
+ const char *cstr = CFStringGetCStringPtr(source, CFStringGetSystemEncoding());
+ if (!cstr)
+ {
+ CFIndex strLen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(source) + 1,
+ CFStringGetSystemEncoding());
+ char *allocStr = (char*)malloc(strLen);
+
+ if(!allocStr)
+ return false;
+
+ if(!CFStringGetCString(source, allocStr, strLen, CFStringGetSystemEncoding()))
+ {
+ free((void*)allocStr);
+ return false;
+ }
+
+ destination = allocStr;
+ free((void*)allocStr);
+
+ return true;
+ }
+
+ destination = cstr;
+ return true;
+}
+
#endif
View
13 xbmc/peripherals/bus/osx/PeripheralBusUSB.cpp
@@ -21,6 +21,7 @@
#include "PeripheralBusUSB.h"
#include "peripherals/Peripherals.h"
#include "utils/log.h"
+#include "osx/DarwinUtils.h"
#include <sys/param.h>
@@ -224,7 +225,7 @@ void CPeripheralBusUSB::DeviceAttachCallback(CPeripheralBusUSB* refCon, io_itera
result = (*interfaceInterface)->GetInterfaceClass(interfaceInterface, &bInterfaceClass);
if (bInterfaceClass == kUSBHIDInterfaceClass || bInterfaceClass == kUSBCommunicationDataInterfaceClass)
{
- char ttlDeviceFilePath[MAXPATHLEN] = {0};
+ std::string ttlDeviceFilePath;
CFStringRef deviceFilePathAsCFString;
USBDevicePrivateData *privateDataRef;
privateDataRef = new USBDevicePrivateData;
@@ -248,16 +249,16 @@ void CPeripheralBusUSB::DeviceAttachCallback(CPeripheralBusUSB* refCon, io_itera
kIOServicePlane, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, kIORegistryIterateRecursively);
if (deviceFilePathAsCFString)
{
- // Convert the path from a CFString to a NULL-terminated C string
- CFStringGetCString((CFStringRef)deviceFilePathAsCFString,
- ttlDeviceFilePath, MAXPATHLEN - 1, kCFStringEncodingASCII);
+ // Convert the path from a CFString to a std::string
+ if (!DarwinCFStringRefToString(deviceFilePathAsCFString, ttlDeviceFilePath))
+ CLog::Log(LOGWARNING, "CPeripheralBusUSB::DeviceAttachCallback failed to convert CFStringRef");
CFRelease(deviceFilePathAsCFString);
}
IOObjectRelease(parent);
}
}
- if (strlen(ttlDeviceFilePath))
- privateDataRef->result.m_strLocation.Format("%s", ttlDeviceFilePath);
+ if (!ttlDeviceFilePath.empty())
+ privateDataRef->result.m_strLocation.Format("%s", ttlDeviceFilePath.c_str());
else
privateDataRef->result.m_strLocation.Format("%d", locationId);
View
6 xbmc/windowing/osx/WinSystemOSX.mm
@@ -36,6 +36,7 @@
#include "osx/XBMCHelper.h"
#include "utils/SystemInfo.h"
#include "osx/CocoaInterface.h"
+#include "osx/DarwinUtils.h"
#undef BOOL
#import <SDL/SDL_video.h>
@@ -1358,8 +1359,9 @@ static void DisplayReconfigured(CGDirectDisplayID display,
// if the windowBounds completely encloses our bounds, we are obscured.
if (!obscureLogged)
{
- const char* cstr = CFStringGetCStringPtr(ownerName, CFStringGetSystemEncoding());
- CLog::Log(LOGDEBUG, "WinSystemOSX: Fullscreen window %s obscures XBMC!", cstr);
+ std::string appName;
+ if (DarwinCFStringRefToString(ownerName, appName))
+ CLog::Log(LOGDEBUG, "WinSystemOSX: Fullscreen window %s obscures XBMC!", appName.c_str());
obscureLogged = true;
}
m_obscured = true;
Please sign in to comment.
Something went wrong with that request. Please try again.