os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/macosx/tclMacOSXBundle.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/macosx/tclMacOSXBundle.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,232 @@
     1.4 +/*
     1.5 + * tclMacOSXBundle.c --
     1.6 + *
     1.7 + *	This file implements functions that inspect CFBundle structures on
     1.8 + *	MacOS X.
     1.9 + *
    1.10 + * Copyright 2001, Apple Computer, Inc.
    1.11 + * Copyright (c) 2003-2007 Daniel A. Steffen <das@users.sourceforge.net>
    1.12 + *
    1.13 + * See the file "license.terms" for information on usage and redistribution of
    1.14 + * this file, and for a DISCLAIMER OF ALL WARRANTIES.
    1.15 + *
    1.16 + *	The following terms apply to all files originating from Apple
    1.17 + *	Computer, Inc. ("Apple") and associated with the software unless
    1.18 + *	explicitly disclaimed in individual files.
    1.19 + *
    1.20 + *	Apple hereby grants permission to use, copy, modify, distribute, and
    1.21 + *	license this software and its documentation for any purpose, provided
    1.22 + *	that existing copyright notices are retained in all copies and that
    1.23 + *	this notice is included verbatim in any distributions. No written
    1.24 + *	agreement, license, or royalty fee is required for any of the
    1.25 + *	authorized uses. Modifications to this software may be copyrighted by
    1.26 + *	their authors and need not follow the licensing terms described here,
    1.27 + *	provided that the new terms are clearly indicated on the first page of
    1.28 + *	each file where they apply.
    1.29 + *
    1.30 + *	IN NO EVENT SHALL APPLE, THE AUTHORS OR DISTRIBUTORS OF THE SOFTWARE
    1.31 + *	BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
    1.32 + *	CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS
    1.33 + *	DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF APPLE OR THE
    1.34 + *	AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. APPLE,
    1.35 + *	THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
    1.36 + *	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    1.37 + *	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
    1.38 + *	NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND
    1.39 + *	APPLE,THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
    1.40 + *	MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
    1.41 + *
    1.42 + *	GOVERNMENT USE: If you are acquiring this software on behalf of the
    1.43 + *	U.S. government, the Government shall have only "Restricted Rights" in
    1.44 + *	the software and related documentation as defined in the Federal
    1.45 + *	Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you are
    1.46 + *	acquiring the software on behalf of the Department of Defense, the
    1.47 + *	software shall be classified as "Commercial Computer Software" and the
    1.48 + *	Government shall have only "Restricted Rights" as defined in Clause
    1.49 + *	252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the
    1.50 + *	authors grant the U.S. Government and others acting in its behalf
    1.51 + *	permission to use and distribute the software in accordance with the
    1.52 + *	terms specified in this license.
    1.53 + *
    1.54 + * RCS: @(#) $Id: tclMacOSXBundle.c,v 1.3.2.6 2007/04/29 02:21:33 das Exp $
    1.55 + */
    1.56 +
    1.57 +#include "tclPort.h"
    1.58 +
    1.59 +#ifdef HAVE_COREFOUNDATION
    1.60 +#include <CoreFoundation/CoreFoundation.h>
    1.61 +#include <mach-o/dyld.h>
    1.62 +#endif /* HAVE_COREFOUNDATION */
    1.63 +
    1.64 +/*
    1.65 + *----------------------------------------------------------------------
    1.66 + *
    1.67 + * Tcl_MacOSXOpenBundleResources --
    1.68 + *
    1.69 + *	Given the bundle name for a shared library, this routine sets
    1.70 + *	libraryPath to the Resources/Scripts directory in the framework
    1.71 + *	package. If hasResourceFile is true, it will also open the main
    1.72 + *	resource file for the bundle.
    1.73 + *
    1.74 + * Results:
    1.75 + *	TCL_OK if the bundle could be opened, and the Scripts folder found.
    1.76 + *	TCL_ERROR otherwise.
    1.77 + *
    1.78 + * Side effects:
    1.79 + *	libraryVariableName may be set, and the resource file opened.
    1.80 + *
    1.81 + *----------------------------------------------------------------------
    1.82 + */
    1.83 +
    1.84 +int
    1.85 +Tcl_MacOSXOpenBundleResources(
    1.86 +    Tcl_Interp *interp,
    1.87 +    CONST char *bundleName,
    1.88 +    int hasResourceFile,
    1.89 +    int maxPathLen,
    1.90 +    char *libraryPath)
    1.91 +{
    1.92 +    return Tcl_MacOSXOpenVersionedBundleResources(interp, bundleName,
    1.93 +	    NULL, hasResourceFile, maxPathLen, libraryPath);
    1.94 +}
    1.95 +
    1.96 +/*
    1.97 + *----------------------------------------------------------------------
    1.98 + *
    1.99 + * Tcl_MacOSXOpenVersionedBundleResources --
   1.100 + *
   1.101 + *	Given the bundle and version name for a shared library (version name
   1.102 + *	can be NULL to indicate latest version), this routine sets libraryPath
   1.103 + *	to the Resources/Scripts directory in the framework package. If
   1.104 + *	hasResourceFile is true, it will also open the main resource file for
   1.105 + *	the bundle.
   1.106 + *
   1.107 + * Results:
   1.108 + *	TCL_OK if the bundle could be opened, and the Scripts folder found.
   1.109 + *	TCL_ERROR otherwise.
   1.110 + *
   1.111 + * Side effects:
   1.112 + *	libraryVariableName may be set, and the resource file opened.
   1.113 + *
   1.114 + *----------------------------------------------------------------------
   1.115 + */
   1.116 +
   1.117 +int
   1.118 +Tcl_MacOSXOpenVersionedBundleResources(
   1.119 +    Tcl_Interp *interp,
   1.120 +    CONST char *bundleName,
   1.121 +    CONST char *bundleVersion,
   1.122 +    int hasResourceFile,
   1.123 +    int maxPathLen,
   1.124 +    char *libraryPath)
   1.125 +{
   1.126 +#ifdef HAVE_COREFOUNDATION
   1.127 +    CFBundleRef bundleRef;
   1.128 +    CFStringRef bundleNameRef;
   1.129 +    CFURLRef libURL;
   1.130 +
   1.131 +    libraryPath[0] = '\0';
   1.132 +
   1.133 +    bundleNameRef = CFStringCreateWithCString(NULL, bundleName,
   1.134 +	    kCFStringEncodingUTF8);
   1.135 +
   1.136 +    bundleRef = CFBundleGetBundleWithIdentifier(bundleNameRef);
   1.137 +    CFRelease(bundleNameRef);
   1.138 +
   1.139 +    if (bundleVersion && bundleRef) {
   1.140 +	/*
   1.141 +	 * Create bundle from bundleVersion subdirectory of 'Versions'.
   1.142 +	 */
   1.143 +
   1.144 +	CFBundleRef versionedBundleRef = NULL;
   1.145 +	CFURLRef versionedBundleURL = NULL;
   1.146 +	CFStringRef bundleVersionRef = CFStringCreateWithCString(NULL,
   1.147 +		bundleVersion, kCFStringEncodingUTF8);
   1.148 +	CFURLRef bundleURL = CFBundleCopyBundleURL(bundleRef);
   1.149 +
   1.150 +	if (bundleURL) {
   1.151 +	    CFStringRef bundleTailRef = CFURLCopyLastPathComponent(bundleURL);
   1.152 +
   1.153 +	    if (bundleTailRef) {
   1.154 +		if (CFStringCompare(bundleTailRef, bundleVersionRef, 0) ==
   1.155 +			kCFCompareEqualTo) {
   1.156 +		    versionedBundleRef = bundleRef;
   1.157 +		}
   1.158 +		CFRelease(bundleTailRef);
   1.159 +	    }
   1.160 +	}
   1.161 +
   1.162 +	if (bundleURL && !versionedBundleRef) {
   1.163 +	    CFURLRef versURL = CFURLCreateCopyAppendingPathComponent(NULL,
   1.164 +		    bundleURL, CFSTR("Versions"), TRUE);
   1.165 +
   1.166 +	    if (versURL) {
   1.167 +		versionedBundleURL = CFURLCreateCopyAppendingPathComponent(
   1.168 +			NULL, versURL, bundleVersionRef, TRUE);
   1.169 +		CFRelease(versURL);
   1.170 +	    }
   1.171 +	    CFRelease(bundleURL);
   1.172 +	}
   1.173 +	CFRelease(bundleVersionRef);
   1.174 +	if (versionedBundleURL) {
   1.175 +	    versionedBundleRef = CFBundleCreate(NULL, versionedBundleURL);
   1.176 +	    CFRelease(versionedBundleURL);
   1.177 +	}
   1.178 +	bundleRef = versionedBundleRef;
   1.179 +    }
   1.180 +
   1.181 +    if (bundleRef) {
   1.182 +	if (hasResourceFile) {
   1.183 +	    /*
   1.184 +	     * Dynamically acquire address for CFBundleOpenBundleResourceMap
   1.185 +	     * symbol, since it is only present in full CoreFoundation on Mac
   1.186 +	     * OS X and not in CFLite on pure Darwin.
   1.187 +	     */
   1.188 +
   1.189 +	    static int initialized = FALSE;
   1.190 +	    static short (*openresourcemap)(CFBundleRef) = NULL;
   1.191 +
   1.192 +	    if (!initialized) {
   1.193 +		NSSymbol nsSymbol = NULL;
   1.194 +		if (NSIsSymbolNameDefinedWithHint(
   1.195 +			"_CFBundleOpenBundleResourceMap", "CoreFoundation")) {
   1.196 +		    nsSymbol = NSLookupAndBindSymbolWithHint(
   1.197 +			    "_CFBundleOpenBundleResourceMap","CoreFoundation");
   1.198 +		    if (nsSymbol) {
   1.199 +			openresourcemap = NSAddressOfSymbol(nsSymbol);
   1.200 +		    }
   1.201 +		}
   1.202 +		initialized = TRUE;
   1.203 +	    }
   1.204 +
   1.205 +	    if (openresourcemap) {
   1.206 +		short refNum;
   1.207 +
   1.208 +		refNum = openresourcemap(bundleRef);
   1.209 +	    }
   1.210 +	}
   1.211 +
   1.212 +	libURL = CFBundleCopyResourceURL(bundleRef, CFSTR("Scripts"),
   1.213 +		NULL, NULL);
   1.214 +
   1.215 +	if (libURL) {
   1.216 +	    /*
   1.217 +	     * FIXME: This is a quick fix, it is probably not right for
   1.218 +	     * internationalization.
   1.219 +	     */
   1.220 +
   1.221 +	    CFURLGetFileSystemRepresentation(libURL, TRUE,
   1.222 +		    (unsigned char*) libraryPath, maxPathLen);
   1.223 +	    CFRelease(libURL);
   1.224 +	}
   1.225 +    }
   1.226 +
   1.227 +    if (libraryPath[0]) {
   1.228 +	return TCL_OK;
   1.229 +    } else {
   1.230 +	return TCL_ERROR;
   1.231 +    }
   1.232 +#else  /* HAVE_COREFOUNDATION */
   1.233 +    return TCL_ERROR;
   1.234 +#endif /* HAVE_COREFOUNDATION */
   1.235 +}