Пример #1
0
void PathToFileViaFSRef(char *pathName, int pathNameMax, FSRef *theFSRef,CFStringEncoding encoding) {        
        CFURLRef sillyThing;
        CFStringRef filePath;
        Boolean isDirectory;
		
		pathName[0]=  0x00;
        sillyThing =  CFURLCreateFromFSRef (kCFAllocatorDefault, theFSRef);
		if (sillyThing == NULL)
			return;
        isDirectory = CFURLHasDirectoryPath(sillyThing);
        
        filePath = CFURLCopyFileSystemPath (sillyThing, kCFURLPOSIXPathStyle);
        CFRelease(sillyThing);
        
  		CFMutableStringRef mutableStr= CFStringCreateMutableCopy(NULL, 0, filePath);
          CFRelease(filePath);
  
  		// HFS+ imposes Unicode2.1 decomposed UTF-8 encoding on all path elements
  		if (encoding == kCFStringEncodingUTF8) 
  			CFStringNormalize(mutableStr, kCFStringNormalizationFormC); // pre-combined
  
          CFStringGetCString (mutableStr, pathName,pathNameMax, encoding);
			CFRelease(mutableStr);
        
        if (isDirectory)
            strcat(pathName,"/");
}
Пример #2
0
 bool URL::HasDirectoryPath( void )
 {
     if( this->_cfObject == NULL )
     {
         return false;
     }
     
     return ( CFURLHasDirectoryPath( this->_cfObject ) ) ? true : false;
 }
Пример #3
0
/* FSSpec -> FSRef -> URL(Unix) -> HPFS+ */
int PathToFile(char *pathName, int pathNameMax, FSSpec *where,UInt32 encoding) {        
        CFURLRef sillyThing;
        CFStringRef filePath;
        FSSpec	failureRetry;
        FSRef	theFSRef;
        OSErr	error;
        Boolean isDirectory=false,retryWithDirectory=false;
        char	rememberName[256];
        
        *pathName = 0x00;
        error = FSpMakeFSRef (where, &theFSRef);
        if (error != noErr) {
            retryWithDirectory = true;
            failureRetry = *where;
            CopyCStringToPascal(":",failureRetry.name);
            CopyPascalStringToC(where->name,(char *) &rememberName);
            error = FSpMakeFSRef(&failureRetry,&theFSRef);
            if (error != noErr) 
                return -1;
	}
        
        sillyThing =  CFURLCreateFromFSRef (kCFAllocatorDefault, &theFSRef);
        isDirectory = CFURLHasDirectoryPath(sillyThing);
        
        filePath = CFURLCopyFileSystemPath (sillyThing, kCFURLHFSPathStyle);
        CFRelease(sillyThing);
        
  		CFMutableStringRef mutableStr= CFStringCreateMutableCopy(NULL, 0, filePath);
          CFRelease(filePath);
  
  		// HFS+ imposes Unicode2.1 decomposed UTF-8 encoding on all path elements
  		if (gCurrentVMEncoding == kCFStringEncodingUTF8) 
  			CFStringNormalize(mutableStr, kCFStringNormalizationFormKC); // pre-combined
  
          CFStringGetCString (mutableStr, pathName,pathNameMax, encoding);
        
        if (retryWithDirectory) {
            strcat(pathName,":");
            strcat(pathName,rememberName);
            isDirectory = false;
        }
        if (isDirectory)
            strcat(pathName,":");
        return 0;
}
Пример #4
0
pascal OSStatus MoreAEOCreateObjSpecifierFromCFURLRef(const CFURLRef pCFURLRef, AEDesc *pObjSpecifier){
	OSErr anErr = paramErr;

	if (nil != pCFURLRef) {
		Boolean isDirectory = CFURLHasDirectoryPath(pCFURLRef);
		CFStringRef tCFStringRef = CFURLCopyFileSystemPath(pCFURLRef, kCFURLHFSPathStyle);
		AEDesc containerDesc = {
			typeNull, NULL
		};
		AEDesc nameDesc = {
			typeNull, NULL
		};
		UniCharPtr buf = nil;

		if (nil != tCFStringRef) {
			Size bufSize = ( CFStringGetLength(tCFStringRef) + ( isDirectory ? 1 : 0 ) ) * sizeof( UniChar );

			buf = (UniCharPtr) NewPtr(bufSize);

			if ( ( anErr = MemError() ) == noErr) {
				CFStringGetCharacters(tCFStringRef, CFRangeMake(0, bufSize / 2), buf);
				if (isDirectory) ( buf )[ ( bufSize - 1 ) / 2 ] = (UniChar) 0x003A;
			}
		} else
			anErr = coreFoundationUnknownErr;

		if (anErr == noErr)
			anErr = AECreateDesc(typeUnicodeText, buf, GetPtrSize( (Ptr) buf), &nameDesc);
		if (anErr == noErr)
			if (isDirectory) {
				anErr = CreateObjSpecifier(cFolder, &containerDesc, formName, &nameDesc, false, pObjSpecifier);
			} else {
				anErr = CreateObjSpecifier(cFile, &containerDesc, formName, &nameDesc, false, pObjSpecifier);
			}

		MoreAEDisposeDesc(&nameDesc);

		if (buf)
			DisposePtr( (Ptr) buf);
	}
	return anErr;
}
Пример #5
0
pascal OSStatus MoreAEOCreateObjSpecifierFromCFURLRef(const CFURLRef pCFURLRef,AEDesc *pObjSpecifier)
{
	OSErr 		anErr = paramErr;

	if (NULL != pCFURLRef)
	{
		CFStringRef tCFStringRef = CFURLCopyFileSystemPath(pCFURLRef,kCFURLHFSPathStyle);

		anErr = coreFoundationUnknownErr;
		if (NULL != tCFStringRef)
		{
			Boolean 		isDirectory = CFURLHasDirectoryPath(pCFURLRef);
			AEDesc 			containerDesc = {typeNull, NULL};
			AEDesc 			nameDesc = {typeNull, NULL};
				Size			bufSize = (CFStringGetLength(tCFStringRef) + (isDirectory ? 1 : 0)) * sizeof(UniChar);
				UniCharPtr		buf = (UniCharPtr) NewPtr(bufSize);

				if ((anErr = MemError()) == noErr)
				{
					CFStringGetCharacters(tCFStringRef, CFRangeMake(0,bufSize/2), buf);
					if (isDirectory) (buf)[(bufSize-1)/2] = (UniChar) 0x003A;				
				}
			
			if (anErr == noErr)
				anErr = AECreateDesc(typeUnicodeText, buf, GetPtrSize((Ptr) buf), &nameDesc);
			if (anErr == noErr)
				{
					if (isDirectory)	// we use cObject here since this might be a package (and we have no way to tell)
						anErr = CreateObjSpecifier(cObject, &containerDesc, formName, &nameDesc, false, pObjSpecifier);
					else
						anErr = CreateObjSpecifier(cFile, &containerDesc, formName, &nameDesc, false, pObjSpecifier);
				}
			MoreAEDisposeDesc(&nameDesc);

			if (buf)
				DisposePtr((Ptr)buf);
		}
	}
	return anErr;
}//end MoreAEOCreateObjSpecifierFromCFURLRef
Boolean IOURLWriteDataAndPropertiesToResource(CFURLRef url, CFDataRef data, CFDictionaryRef propertyDict, SInt32 *errorCode) {
    CFStringRef scheme = CFURLCopyScheme(url);
    if (!scheme) {
        if (errorCode) *errorCode = kIOURLImproperArgumentsError;
        return FALSE;
    } else if (CFStringCompare(scheme, CFSTR("file"), 0) == kCFCompareEqualTo) {
        Boolean success = TRUE;
        CFRelease(scheme);
        if (errorCode) *errorCode = 0;
        if (data) {
            char cPath[CFMaxPathSize];
            if (!CFURLGetFileSystemRepresentation(url, TRUE, cPath, CFMaxPathSize)) {
                if (errorCode) *errorCode = kIOURLImproperArgumentsError;
                success = FALSE;
            } else if (CFURLHasDirectoryPath(url)) {
                // Create a directory
                success = !mkdir(cPath, 0777);
                if (!success && errorCode) *errorCode = kIOURLUnknownError;
            } else {
               // Write data
                SInt32 length = CFDataGetLength(data);
                const void *bytes = (0 == length) ? (const void *)"" : CFDataGetBytePtr(data);
                success = _IOWriteBytesToFile(cPath, bytes, length);
                if (!success && errorCode) *errorCode = kIOURLUnknownError;
            }
        }
        if (propertyDict) {
            if (!_IOFileURLWritePropertiesToResource(url, propertyDict, errorCode))
                success = FALSE;
        }
        return success;
    } else {
        if (errorCode) *errorCode = kIOURLUnknownSchemeError;
        return FALSE;
    }
}
Пример #7
0
/* MySimpleDownload implements the download command.  It sets up a MyStreamInfo 'object' 
with the read stream being an FTP stream of the file to download and the write stream being 
a file stream of the destination file.  It then returns, and the real work happens 
asynchronously in the runloop.  The function returns true if the stream setup succeeded, 
and false if it failed. */
static Boolean
MySimpleDownload(CFStringRef urlString, CFURLRef destinationFolder, CFStringRef username, CFStringRef password)
{
    CFReadStreamRef        readStream;
    CFWriteStreamRef       writeStream;
    CFStreamClientContext  context = { 0, NULL, NULL, NULL, NULL };
    CFURLRef               downloadPath, downloadURL;
    CFStringRef            fileName;
    Boolean                dirPath, success = true;
    MyStreamInfo           *streamInfo;

    assert(urlString != NULL);
    assert(destinationFolder != NULL);
    assert( (username != NULL) || (password == NULL) );

    /* Returns true if the CFURL path represents a directory. */
    dirPath = CFURLHasDirectoryPath(destinationFolder);
    if (!dirPath) {
        fprintf(stderr, "Download destination must be a directory.\n");
        return false;
    }
    
    /* Create a CFURL from the urlString. */
    downloadURL = CFURLCreateWithString(kCFAllocatorDefault, urlString, NULL);
    assert(downloadURL != NULL);

    /* Copy the end of the file path and use it as the file name. */
    fileName = CFURLCopyLastPathComponent(downloadURL);
    assert(fileName != NULL);

    /* Create the downloadPath by taking the destination folder and appending the file name. */
    downloadPath = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, destinationFolder, fileName, false);
    assert(downloadPath != NULL);
    CFRelease(fileName);

    /* Create a CFWriteStream for the file being downloaded. */
    writeStream = CFWriteStreamCreateWithFile(kCFAllocatorDefault, downloadPath);
    assert(writeStream != NULL);
    CFRelease(downloadPath);
    
    /* CFReadStreamCreateWithFTPURL creates an FTP read stream for downloading from an FTP URL. */
    readStream = CFReadStreamCreateWithFTPURL(kCFAllocatorDefault, downloadURL);
    assert(readStream != NULL);
    CFRelease(downloadURL);
    
    /* Initialize our MyStreamInfo structure, which we use to store some information about the stream. */
    MyStreamInfoCreate(&streamInfo, readStream, writeStream);
    context.info = (void *)streamInfo;

    /* CFWriteStreamOpen will return success/failure.  Opening a stream causes it to reserve all the
    system resources it requires.  If the stream can open non-blocking, this will always return TRUE;
    listen to the run loop source to find out when the open completes and whether it was successful. */
    success = CFWriteStreamOpen(writeStream);
    if (success) {
    
        /* CFReadStreamSetClient registers a callback to hear about interesting events that occur on a stream. */
        success = CFReadStreamSetClient(readStream, kNetworkEvents, MyDownloadCallBack, &context);
        if (success) {

            /* Schedule a run loop on which the client can be notified about stream events.  The client
            callback will be triggered via the run loop.  It's the caller's responsibility to ensure that
            the run loop is running. */
            CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
            
            MyCFStreamSetUsernamePassword(readStream, username, password);
            MyCFStreamSetFTPProxy(readStream, &streamInfo->proxyDict);
            
            /* Setting the kCFStreamPropertyFTPFetchResourceInfo property will instruct the FTP stream
            to fetch the file size before downloading the file.  Note that fetching the file size adds
            some time to the length of the download.  Fetching the file size allows you to potentially
            provide a progress dialog during the download operation. You will retrieve the actual file
            size after your CFReadStream Callback gets called with a kCFStreamEventOpenCompleted event. */
            CFReadStreamSetProperty(readStream, kCFStreamPropertyFTPFetchResourceInfo, kCFBooleanTrue);
            
            /* CFReadStreamOpen will return success/failure.  Opening a stream causes it to reserve all the
            system resources it requires.  If the stream can open non-blocking, this will always return TRUE;
            listen to the run loop source to find out when the open completes and whether it was successful. */
            success = CFReadStreamOpen(readStream);
            if (success == false) {
                fprintf(stderr, "CFReadStreamOpen failed\n");
                MyStreamInfoDestroy(streamInfo);
            }
        } else {
            fprintf(stderr, "CFReadStreamSetClient failed\n");
            MyStreamInfoDestroy(streamInfo);
        }
    } else {
        fprintf(stderr, "CFWriteStreamOpen failed\n");
        MyStreamInfoDestroy(streamInfo);
    }

    return success;
}