本文整理匯總了C++中CFStringCreateExternalRepresentation函數的典型用法代碼示例。如果您正苦於以下問題:C++ CFStringCreateExternalRepresentation函數的具體用法?C++ CFStringCreateExternalRepresentation怎麽用?C++ CFStringCreateExternalRepresentation使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了CFStringCreateExternalRepresentation函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。
示例1: configWrite
/*
* Writes a new configuration file with the contents of a CFArrayRef. Each
* element of the array is a CFStringRef.
*/
void
configWrite(const char *path, CFArrayRef config)
{
CFStringRef str;
CFDataRef data;
int fd = -1;
int len;
str = CFStringCreateByCombiningStrings(NULL, config, CFSTR("\n"));
data = CFStringCreateExternalRepresentation(NULL, str, kCFStringEncodingMacRoman, '.');
CFRelease(str);
fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (fd < 0) {
goto done;
}
len = CFDataGetLength(data);
if (len) {
(void)write(fd, CFDataGetBytePtr(data), len);
}
done:
if (fd >= 0)
close(fd);
CFRelease(data);
return;
}
示例2: impExpImportDeleteExtension
/* do a [NSString stringByDeletingPathExtension] equivalent */
CFStringRef impExpImportDeleteExtension(
CFStringRef fileStr)
{
CFDataRef fileStrData = CFStringCreateExternalRepresentation(NULL, fileStr,
kCFStringEncodingUTF8, 0);
if(fileStrData == NULL) {
return NULL;
}
CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(NULL,
CFDataGetBytePtr(fileStrData), CFDataGetLength(fileStrData), false);
if(urlRef == NULL) {
CFRelease(fileStrData);
return NULL;
}
CFURLRef rtnUrl = CFURLCreateCopyDeletingPathExtension(NULL, urlRef);
CFStringRef rtnStr = NULL;
CFRelease(urlRef);
if(rtnUrl) {
rtnStr = CFURLGetString(rtnUrl);
CFRetain(rtnStr);
CFRelease(rtnUrl);
}
CFRelease(fileStrData);
return rtnStr;
}
示例3: CFStringCreateExternalRepresentation
std::wstring MacUtils::CFStringRefToWString(CFStringRef aString)
{
if (aString == NULL) {
return L"";
}
if (!aString)
return false;
CFDataRef cfData = CFStringCreateExternalRepresentation(kCFAllocatorDefault,
aString, kCFStringEncodingUTF32, 0);
CFRelease(aString);
if (!cfData)
return false;
int out_byte_len = CFDataGetLength(cfData);
out_byte_len -= sizeof(wchar_t); // don't count the 32 bit BOM char at start
int out_len = out_byte_len / sizeof(wchar_t);
wchar_t *tmp = new wchar_t[out_len + 1];
// start after the BOM, hence sizeof(wchar_t)
CFDataGetBytes(cfData, CFRangeMake(sizeof(wchar_t), out_byte_len),
(UInt8*)tmp);
CFRelease(cfData);
tmp[out_len] = 0;
std::wstring result = tmp;
delete[] tmp;
return result;
}
示例4: __JSONParseWithString
inline bool __JSONParseWithString(__JSONRef json, CFStringRef string, CFErrorRef *error) {
bool success = 1;
json->yajlParser = yajl_alloc(&json->yajlParserCallbacks, &json->yajlAllocFuncs, (void *)json);
if (json->yajlParser) {
// yajl_config(json->yajlParser, yajl_allow_comments, kJSONReadOptionAllowComments | options ? 1 : 0);
// yajl_config(json->yajlParser, yajl_dont_validate_strings, kJSONReadOptionCheckUTF8 | options ? 1 : 0);
CFDataRef data = CFStringCreateExternalRepresentation(json->allocator, string, kCFStringEncodingUTF8, 0);
if (data) {
if ((json->yajlParserStatus = yajl_parse(json->yajlParser, CFDataGetBytePtr(data), CFDataGetLength(data))) != yajl_status_ok) {
if (error) {
success = 0;
unsigned char * str = yajl_get_error(json->yajlParser, 1, CFDataGetBytePtr(data), CFDataGetLength(data));
fprintf(stderr, "%s", (const char *) str);
yajl_free_error(json->yajlParser, str);
*error = CFErrorCreateWithUserInfoKeysAndValues(json->allocator, CFSTR("com.github.mirek.CoreJSON"), (CFIndex)json->yajlParserStatus, (const void *) { kCFErrorDescriptionKey }, (const void *) { CFSTR("Test") }, 1);
}
// TODO: Error stuff
//printf("ERROR: %s\n", yajl_get_error(json->yajlParser, 1, __JSONUTF8StringGetBuffer(utf8), __JSONUTF8StringGetMaximumSize(utf8)));
}
json->yajlParserStatus = yajl_complete_parse(json->yajlParser);
CFRelease(data);
} else {
示例5: IPCCallback
static CFDataRef IPCCallback(__unused CFMessagePortRef port, SInt32 messageID, __unused CFDataRef data, __unused void *info)
{
switch(messageID)
{
case ServerCommandGetIP:
{
if(publicIPState == IPStateInvalid)
{
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(reachability, &flags);
ReachabilityCallback(reachability, flags, NULL);
}
if(publicIPData)
return publicIPData;
return CFStringCreateExternalRepresentation(kCFAllocatorDefault, CFSTR(""), kCFStringEncodingUTF8, 0);
}
case ServerCommandStop:
{
CFRunLoopStop(CFRunLoopGetMain());
return NULL;
}
case ServerCommandReload:
{
FetchIPAddress();
return NULL;
}
default:
return NULL;
}
}
示例6: encryptString
static CFDataRef
encryptString(SecKeyRef wrapKey, CFDataRef iv, CFStringRef str)
{
CFDataRef retval = NULL;
CFErrorRef error = NULL;
CFDataRef inputString = CFStringCreateExternalRepresentation(kCFAllocatorDefault, str, kCFStringEncodingMacRoman, 0xff);
SecTransformRef encryptTrans = SecEncryptTransformCreate(wrapKey, &error);
if(error == NULL) {
SecTransformRef group = SecTransformCreateGroupTransform();
SecTransformSetAttribute(encryptTrans, kSecEncryptionMode, kSecModeCBCKey, &error);
if(error == NULL) SecTransformSetAttribute(encryptTrans, kSecPaddingKey, kSecPaddingPKCS7Key, &error);
if(error == NULL) SecTransformSetAttribute(encryptTrans, kSecTransformInputAttributeName, inputString, &error);
if(error == NULL) SecTransformSetAttribute(encryptTrans, kSecIVKey, iv, &error);
SecTransformRef encodeTrans = SecEncodeTransformCreate(kSecBase64Encoding, &error);
SecTransformConnectTransforms(encryptTrans, kSecTransformOutputAttributeName, encodeTrans, kSecTransformInputAttributeName, group, &error);
CFRelease(encodeTrans);
CFRelease(encryptTrans);
if(error == NULL) retval = SecTransformExecute(group, &error);
if(error != NULL) secDebug(ASL_LEVEL_ERR, "Failed to encrypt recovery password\n", NULL);
CFRelease(group);
}
return retval;
}
示例7: main
int main (int argc, const char * argv[]) {
if (argv[1] != NULL)
{
CFDataRef data;
CFStringRef searchterm = CFStringCreateWithCString(NULL, argv[1],
kCFStringEncodingMacRoman);
CFRange searchRange = CFRangeMake(0, CFStringGetLength(searchterm));
CFStringRef dictResult = DCSCopyTextDefinition(NULL, searchterm, searchRange);
CFRelease(searchterm);
if (dictResult != NULL) {
data = CFStringCreateExternalRepresentation(NULL, dictResult, CFStringGetSystemEncoding(), '?');
CFRelease(dictResult);
}
if (data != NULL) {
printf ("%.*s\n\n", (int)CFDataGetLength(data), CFDataGetBytePtr(data));
CFRelease(data);
}
else {
printf("Could not find word in Dictionary.app.\n");
}
return 0;
}
else {
printf("Usage: dictlookup \"word\"\n");
return 1;
}
}
示例8: impExpAddStringAttr
/*
* Add a CFString to a crypto context handle.
*/
static CSSM_RETURN impExpAddStringAttr(
CSSM_CC_HANDLE ccHand,
CFStringRef str,
CSSM_ATTRIBUTE_TYPE attrType)
{
/* CFStrings are passed as external rep in UTF8 encoding by convention */
CFDataRef outData;
outData = CFStringCreateExternalRepresentation(NULL,
str, kCFStringEncodingUTF8, 0); // lossByte 0 ==> no loss allowed
if(outData == NULL) {
SecImpExpDbg("impExpAddStringAttr: bad string format");
return errSecParam;
}
CSSM_DATA attrData;
attrData.Data = (uint8 *)CFDataGetBytePtr(outData);
attrData.Length = CFDataGetLength(outData);
CSSM_RETURN crtn = impExpAddContextAttribute(ccHand, attrType, sizeof(CSSM_DATA),
&attrData);
CFRelease(outData);
if(crtn) {
SecImpExpDbg("impExpAddStringAttr: CSSM_UpdateContextAttributes error");
}
return crtn;
}
示例9: _SQLite3StatementCreate
inline SQLite3StatementRef _SQLite3StatementCreate(CFAllocatorRef allocator, SQLite3ConnectionRef connection, CFStringRef sql, CFErrorRef *error) {
SQLite3StatementRef statement = NULL;
if (connection && sql) {
statement = CFAllocatorAllocate(allocator, sizeof(__SQLite3Statement), 0);
if (statement) {
statement->connection = SQLite3ConnectionRetain(connection); // Let's retain connection
statement->allocator = allocator ? CFRetain(allocator) : NULL; // ...and allocator if not null
statement->retainCount = 1;
statement->stmt = NULL;
CFDataRef sqlData = CFStringCreateExternalRepresentation(statement->allocator, sql, kCFStringEncodingUTF8, 0);
if (sqlData) {
SQLite3Status status = sqlite3_prepare_v2(connection->db, (const char *)CFDataGetBytePtr(sqlData), -1, &statement->stmt, NULL);
if (kSQLite3StatusOK != status) {
if (error) {
*error = CFErrorCreate(allocator, CFSTR("com.github.mirek.SQLite3"), status, NULL);
}
printf("ERROR: %s\n", sqlite3_errmsg(connection->db));
statement = SQLite3StatementRelease(statement); // ...will be set to NULL
}
CFRelease(sqlData);
}
}
}
return statement;
}
示例10: iSCSIDaemonSetSharedSecret
/*! Sets or updates a shared secret.
* @param handle a handle to a daemon connection.
* @param authorization an authorization for the right kiSCSIAuthModifyRights.
* @param nodeIQN the node iSCSI qualified name.
* @param sharedSecret the secret to set.
* @return an error code indicating whether the operating was successful. */
errno_t iSCSIDaemonSetSharedSecret(iSCSIDaemonHandle handle,
AuthorizationRef authorization,
CFStringRef nodeIQN,
CFStringRef sharedSecret)
{
// Validate inputs
if(handle < 0 || !authorization || !nodeIQN || !sharedSecret)
return EINVAL;
AuthorizationExternalForm authExtForm;
AuthorizationMakeExternalForm(authorization,&authExtForm);
CFDataRef authData = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault,
(UInt8*)&authExtForm.bytes,
kAuthorizationExternalFormLength,
kCFAllocatorDefault);
CFDataRef nodeIQNData = CFStringCreateExternalRepresentation(kCFAllocatorDefault,nodeIQN,kCFStringEncodingASCII,0);
CFDataRef sharedSecretData = CFStringCreateExternalRepresentation(kCFAllocatorDefault,sharedSecret,kCFStringEncodingASCII,0);
iSCSIDMsgSetSharedSecretCmd cmd = iSCSIDMsgSetSharedSecretCmdInit;
cmd.authorizationLength = (UInt32)CFDataGetLength(authData);
cmd.nodeIQNLength = (UInt32)CFDataGetLength(nodeIQNData);
cmd.secretLength = (UInt32)CFDataGetLength(sharedSecretData);
errno_t error = iSCSIDaemonSendMsg(handle,(iSCSIDMsgGeneric *)&cmd,
authData,nodeIQNData,sharedSecretData,NULL);
if(nodeIQNData)
CFRelease(nodeIQNData);
if(sharedSecretData)
CFRelease(sharedSecretData);
if(error)
return error;
iSCSIDMsgSetSharedSecretRsp rsp;
if(recv(handle,&rsp,sizeof(rsp),0) != sizeof(rsp))
return EIO;
if(rsp.funcCode != kiSCSIDSetSharedSecret)
return EIO;
return rsp.errorCode;
}
示例11: stringGettingContentsAsCStringExample
void stringGettingContentsAsCStringExample(void) {
CFStringRef str;
CFDataRef data;
CFRange rangeToProcess;
const char *bytes;
show(CFSTR("------------------C String Manipulations---------------"));
// Create some test CFString
// Note that in general the string might contain Unicode characters which cannot
// be converted to a 8-bit character encoding
str = CFStringCreateWithCString(NULL, "Hello World", kCFStringEncodingASCII);
show(CFSTR("Original String : %@"), str);
// First, the fast but unpredictable way to get at the C String contents...
// This is O(1), meaning it takes constant time.
// This might return NULL!
bytes = CFStringGetCStringPtr(str, kCFStringEncodingASCII);
// If that fails, you can try to get the contents by copying it out
if (bytes == NULL) {
char localBuffer[10];
Boolean success;
// This might also fail, either if you provide a buffer that is too small,
// or the string cannot be converted into the specified encoding
success = CFStringGetCString(str, localBuffer, 10, kCFStringEncodingASCII);
}
else
show(CFSTR("From CStringPtr : %@"), bytes);
// A pretty simple solution is to use a CFData; this frees you from guessing at the buffer size
// But it does allocate a CFData...
data = CFStringCreateExternalRepresentation(NULL, str, kCFStringEncodingASCII, 0);
if (data) {
show(CFSTR("External Rep: %@"), data);
bytes = (const char *)CFDataGetBytePtr(data);
}
// More complicated but efficient solution is to use a fixed size buffer, and put a loop in
rangeToProcess = CFRangeMake(0, CFStringGetLength(str));
while (rangeToProcess.length > 0) {
UInt8 localBuffer[100];
CFIndex usedBufferLength;
CFIndex numChars = CFStringGetBytes(str, rangeToProcess, kCFStringEncodingASCII, 0, FALSE, (UInt8 *)localBuffer, 100, &usedBufferLength);
if (numChars == 0) break; // Means we failed to convert anything...
// Otherwise we converted some stuff; process localBuffer containing usedBufferLength bytes
// Note that the bytes in localBuffer are not NULL terminated
// Update the remaining range to continue looping
rangeToProcess.location += numChars;
rangeToProcess.length -= numChars;
}
}
示例12: TLProjectionGeoidCreateDataRepresentation
CFDataRef TLProjectionGeoidCreateDataRepresentation(TLProjectionGeoidRef projGeoid) {
CFStringRef ellipseValue = TLProjectionGeoidCreateEllipseValue(projGeoid);
CFStringRef geoidArgs = TLProjectionParametersCreateStringWithKeyAndValue(TLProjectionParametersEllipseNameKey, ellipseValue);
CFRelease(ellipseValue);
CFDataRef geoidAsData = CFStringCreateExternalRepresentation(kCFAllocatorDefault, geoidArgs, kCFStringEncodingASCII, 0);
CFRelease(geoidArgs);
return geoidAsData;
}
示例13: write_gdb_prep_cmds
void write_gdb_prep_cmds(AMDeviceRef device, CFURLRef disk_app_url) {
CFMutableStringRef cmds = CFStringCreateMutableCopy(NULL, 0, GDB_PREP_CMDS);
CFRange range = { 0, CFStringGetLength(cmds) };
CFStringRef ds_path = copy_device_support_path(device);
CFStringFindAndReplace(cmds, CFSTR("{ds_path}"), ds_path, range, 0);
range.length = CFStringGetLength(cmds);
if (args) {
CFStringRef cf_args = CFStringCreateWithCString(NULL, args, kCFStringEncodingASCII);
CFStringFindAndReplace(cmds, CFSTR("{args}"), cf_args, range, 0);
CFRelease(cf_args);
} else {
CFStringFindAndReplace(cmds, CFSTR(" {args}"), CFSTR(""), range, 0);
}
range.length = CFStringGetLength(cmds);
CFStringRef bundle_identifier = copy_disk_app_identifier(disk_app_url);
CFURLRef device_app_url = copy_device_app_url(device, bundle_identifier);
CFStringRef device_app_path = CFURLCopyFileSystemPath(device_app_url, kCFURLPOSIXPathStyle);
CFStringFindAndReplace(cmds, CFSTR("{device_app}"), device_app_path, range, 0);
range.length = CFStringGetLength(cmds);
CFStringRef disk_app_path = CFURLCopyFileSystemPath(disk_app_url, kCFURLPOSIXPathStyle);
CFStringFindAndReplace(cmds, CFSTR("{disk_app}"), disk_app_path, range, 0);
range.length = CFStringGetLength(cmds);
CFURLRef device_container_url = CFURLCreateCopyDeletingLastPathComponent(NULL, device_app_url);
CFStringRef device_container_path = CFURLCopyFileSystemPath(device_container_url, kCFURLPOSIXPathStyle);
CFMutableStringRef dcp_noprivate = CFStringCreateMutableCopy(NULL, 0, device_container_path);
range.length = CFStringGetLength(dcp_noprivate);
CFStringFindAndReplace(dcp_noprivate, CFSTR("/private/var/"), CFSTR("/var/"), range, 0);
range.length = CFStringGetLength(cmds);
CFStringFindAndReplace(cmds, CFSTR("{device_container}"), dcp_noprivate, range, 0);
range.length = CFStringGetLength(cmds);
CFURLRef disk_container_url = CFURLCreateCopyDeletingLastPathComponent(NULL, disk_app_url);
CFStringRef disk_container_path = CFURLCopyFileSystemPath(disk_container_url, kCFURLPOSIXPathStyle);
CFStringFindAndReplace(cmds, CFSTR("{disk_container}"), disk_container_path, range, 0);
CFDataRef cmds_data = CFStringCreateExternalRepresentation(NULL, cmds, kCFStringEncodingASCII, 0);
FILE *out = fopen(PREP_CMDS_PATH, "w");
fwrite(CFDataGetBytePtr(cmds_data), CFDataGetLength(cmds_data), 1, out);
fclose(out);
CFRelease(cmds);
if (ds_path != NULL) CFRelease(ds_path);
CFRelease(bundle_identifier);
CFRelease(device_app_url);
CFRelease(device_app_path);
CFRelease(disk_app_path);
CFRelease(device_container_url);
CFRelease(device_container_path);
CFRelease(dcp_noprivate);
CFRelease(disk_container_url);
CFRelease(disk_container_path);
CFRelease(cmds_data);
}
示例14: __copy_legacy_password
static CFDataRef
__copy_legacy_password(CFTypeRef password)
{
if (password == NULL) {
return NULL;
}
if (isA_CFData(password)) {
CFIndex n;
n = CFDataGetLength(password);
if ((n % sizeof(UniChar)) == 0) {
CFStringEncoding encoding;
CFStringRef str;
#if __BIG_ENDIAN__
encoding = (*(CFDataGetBytePtr(password) + 1) == 0x00) ? kCFStringEncodingUTF16LE : kCFStringEncodingUTF16BE;
#else // __LITTLE_ENDIAN__
encoding = (*(CFDataGetBytePtr(password) ) == 0x00) ? kCFStringEncodingUTF16BE : kCFStringEncodingUTF16LE;
#endif
str = CFStringCreateWithBytes(NULL,
(const UInt8 *)CFDataGetBytePtr(password),
n,
encoding,
FALSE);
password = CFStringCreateExternalRepresentation(NULL,
str,
kCFStringEncodingUTF8,
0);
CFRelease(str);
} else {
password = NULL;
}
} else if (isA_CFString(password) && (CFStringGetLength(password) > 0)) {
// convert password to CFData
password = CFStringCreateExternalRepresentation(NULL,
password,
kCFStringEncodingUTF8,
0);
} else {
password = NULL;
}
return password;
}
示例15: TSICTStringCreateWithStringAndFormat
TStringIRep* TSICTStringCreateWithStringAndFormat(CFStringRef string, TSITStringFormat format)
{
CFRetain(string);
CFDataRef data = CFStringCreateExternalRepresentation(kCFAllocatorDefault, string, kCFStringEncodingUTF8, '?');
TStringIRep* rep = TSICTStringCreateWithDataOfTypeAndFormat(data, kTSITStringTagString, format);
CFRelease(data);
CFRelease(string);
return rep;
}