1/*
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 *  CCLArgFunctions.c
25 *  CCLLauncher
26 *
27 *  Created by kevine on 4/11/06.
28 *  Copyright 2006-7 Apple, Inc.  All rights reserved.
29 *
30 */
31
32#include "CCLArgFunctions.h"
33#include <CoreFoundation/CoreFoundation.h>
34
35// For compatibility with previous usage, the boolean-return Get*FromDict()
36// functions return errors only if the key's value is non-NULL but cannot
37// be extracted.  If no value is stored for key or the value is NULL, the
38// by-reference return buffers are not modified.
39
40CFDictionaryRef
41GetCFDictionaryFromDict(CFDictionaryRef dict, const CFStringRef key)
42{
43    CFDictionaryRef retVal= (CFDictionaryRef) CFDictionaryGetValue(dict, key);
44    if((retVal!= NULL) && (CFDictionaryGetTypeID()== CFGetTypeID(retVal)))
45    {
46        return retVal;
47    }
48    return NULL;
49}
50
51bool
52GetCFStringFromDict(CFDictionaryRef dict, CFStringRef *s, const CFStringRef key)
53{
54    CFStringRef tempString= (CFStringRef) CFDictionaryGetValue(dict, key);
55
56    if (tempString == NULL)
57        return true;
58
59    if (CFStringGetTypeID() == CFGetTypeID(tempString)) {
60        *s = tempString;
61        return true;
62    }
63
64    return false;
65}
66
67bool
68CopyCStringFromDict(CFDictionaryRef dict, char** string, const CFStringRef key)
69{
70    CFStringRef str = NULL;
71    CFIndex bufSize;
72    char *buf = NULL;
73
74    // fallout cases
75    if (!GetCFStringFromDict(dict, &str, key))
76        return false;
77    if (str == NULL)
78        return true;
79
80    bufSize = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str),
81                                                kCFStringEncodingUTF8) + 1;
82    buf = malloc(bufSize);
83    if(buf && CFStringGetCString(str, buf, bufSize, kCFStringEncodingUTF8)) {
84        *string = buf;
85        return true;
86    }
87
88    if (buf)  free(buf);
89    return false;
90}
91
92bool
93GetCFNumberFromDict(CFDictionaryRef dict, CFNumberRef *n, const CFStringRef key)
94{
95    CFNumberRef tempNum = (CFNumberRef)CFDictionaryGetValue(dict, key);
96
97    if (tempNum == NULL)
98        return true;
99
100    if(CFNumberGetTypeID() == CFGetTypeID(tempNum)) {
101        *n = tempNum;
102        return true;
103    }
104
105    return false;
106}
107
108bool
109GetIntFromDict(CFDictionaryRef dict, int* intRef, const CFStringRef key)
110{
111    CFNumberRef num = NULL;
112
113    // fallout cases
114    if (!GetCFNumberFromDict(dict, &num, key))
115        return false;
116    if (num == NULL)
117        return true;
118
119    return CFNumberGetValue(num, kCFNumberSInt32Type, intRef);
120}
121