Exemple #1
0
void
XMLMacCarbonFile::write(const long byteCount, const XMLByte* const buffer)
{
    long bytesWritten = 0;
    OSErr err = noErr;

    if (byteCount <= 0 || buffer == NULL)
        return;

    if (!mFileValid)
    {
        ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotWriteToFile);
    }

    if (gHasHFSPlusAPIs)
    {
        ByteCount actualCount;
        err = FSWriteFork(mFileRefNum, fsFromMark, 0, byteCount, buffer, &actualCount);
        bytesWritten = actualCount;
    }
    else
    {
        long count = byteCount;
        err = FSWrite(mFileRefNum, &count, buffer);
        bytesWritten = count;
    }

    if ((err != noErr && err != eofErr) || (bytesWritten != byteCount))
    {
        ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotWriteToFile);
    }
}
Exemple #2
0
int
prWriteFile (
    imFileRef		refNum,
    const void 		*data,
    csSDK_uint32	*bytes)
{
    csSDK_uint32		orig_bytes = *bytes;
    csSDK_int32		err;
#ifdef PRWIN_ENV
    err = !WriteFile(refNum, data, (DWORD)*bytes, (LPDWORD)bytes, NULL);
    if (err)
    {
        err = GetLastError ();
    }
#else
    err = FSWriteFork(	reinterpret_cast<intptr_t>(refNum),
                        fsAtMark,
                        0,
                        orig_bytes,
                        data,
                        reinterpret_cast<ByteCount*>(bytes));
#endif

    if (!err && (*bytes != orig_bytes)) {
        err = exportReturn_OutOfDiskSpace;
    }
    return err;
}
Exemple #3
0
	void LFA_Write ( LFA_FileRef file, const void * buffer, XMP_Int32 bytes )
	{
		long refNum = (long)file;	// ! Use long to avoid size warnings for SInt16 cast.
		ByteCount bytesWritten;

		OSErr err = FSWriteFork ( refNum, fsAtMark, 0, bytes, buffer, &bytesWritten );
		if ( (err != noErr) | (bytesWritten != (ByteCount)bytes) ) LFA_Throw ( "LFA_Write: FSWriteFork failure", kLFAErr_ExternalFailure );

	}	// LFA_Write
OSErr PSSDKWrite(int32 refNum, int32 * count, void * buffPtr)
{
	if (NULL == count || NULL == buffPtr)
		return writErr;
		
	ByteCount bytes = *count;

	ByteCount bCount = *count;
	
	OSErr err = FSWriteFork(refNum, fsAtMark | noCacheMask, 0, bytes, buffPtr, &bCount);

	if (noErr != err)
		return err;
	
	if (bytes != bCount)
		return writErr;
		
	*count = bCount;

	return err;
}
static PyObject *
rf_write(rfobject *self, PyObject *args)
{
    char *buffer;
    long size;
    OSErr err;

    if (self->isclosed) {
        PyErr_SetString(PyExc_ValueError, "Operation on closed file");
        return NULL;
    }
    if (!PyArg_ParseTuple(args, "s#", &buffer, &size))
        return NULL;
    err = FSWriteFork(self->fRefNum, fsAtMark, 0, size, buffer, NULL);
    if (err) {
        PyMac_Error(err);
        return NULL;
    }
    Py_INCREF(Py_None);
    return Py_None;
}
Exemple #6
0
OSErr SplitFileIfNeeded(FSRef * inFileReference,FSRef * inParentReference,FSCatalogInfo * inFileCatalogInfo,HFSUniStr255 * inFileName)
{
	OSErr tErr;
	Boolean splitNeeded=FALSE;
	SInt16 tForkRefNum;
	UInt32 tResourceForkSize=0;
	static HFSUniStr255 sResourceForkName={0,{}};
	Boolean hasResourceFork=FALSE;
	static UInt8 tPOSIXPath[PATH_MAX*2+1];
	static UInt32 tPOSIXPathMaxLength=PATH_MAX*2;
	struct stat tFileStat;
	SInt16 tNewFileRefNum;
	
	if (sResourceForkName.length==0)
	{
		tErr=FSGetResourceForkName(&sResourceForkName);
	
		if (tErr!=noErr)
		{
			logerror("An error occurred when obtaining the ResourceFork name\n");
			
			return -1;
		}
	}
	
	// 1. Check for the presence of a resource fork
		
	tErr=FSOpenFork(inFileReference,sResourceForkName.length,sResourceForkName.unicode,fsRdPerm,&tForkRefNum);
	
	if (tErr==noErr)
	{
		SInt64 tForkSize;
		
		// Get the size of the resource fork
		
		tErr=FSGetForkSize(tForkRefNum,&tForkSize);
		
		if (tErr!=noErr)
		{
			logerror("An error occurred on getting the resource fork size of a file or director\n");
			
			FSCloseFork(tForkRefNum);
			
			return -1;
		}
		
		if (tForkSize>0xFFFFFFFF)
		{
			FSCloseFork(tForkRefNum);
			
			// AppleDouble File format does not support forks bigger than 2GB
			
			logerror("AppleDouble file format does not support forks bigger than 2 GB\n");
			
			return -1;
		}
		
		tResourceForkSize=tForkSize;
		
		if (tForkSize>0)
		{
			hasResourceFork=TRUE;
		
			splitNeeded=TRUE;
		}
		else
		{
			FSCloseFork(tForkRefNum);
		}
	}
	else
	{
		switch(tErr)
		{
			case errFSForkNotFound:
			case eofErr:
				// No resource Fork
				
				tErr=noErr;
				break;
			default:
				
				logerror("Unable to open fork\n");
				
				return -1;
				
				break;
		}
	}
	
	// 2. Check for the presence of FinderInfo or ExtFinderInfo
	
	if (splitNeeded==FALSE)
	{
		UInt32 * tUnsignedInt32Ptr;
		int i;
		
		
		// 1. We need to save the Folder(Ext) Info in the ._ file if there are any folder/finder or extend folder/finder info
			
		tUnsignedInt32Ptr= (UInt32 *) inFileCatalogInfo->finderInfo;
		
		for(i=0;i<4;i++)
		{
			if (tUnsignedInt32Ptr[i]!=0)
			{
				// We need to create a ._file
			
				splitNeeded=TRUE;
				break;
			}
		}
		
		if (splitNeeded==TRUE)		// 01/02/07: Symbolic link looks like this
		{
			UInt32 tSymbolicLink;

			tSymbolicLink='s';
			tSymbolicLink='l'+(tSymbolicLink<<8);
			tSymbolicLink='n'+(tSymbolicLink<<8);
			tSymbolicLink='k'+(tSymbolicLink<<8);

			if (tUnsignedInt32Ptr[0]==tSymbolicLink)
			{
				splitNeeded=FALSE;
			}
		}
		else
		{
			tUnsignedInt32Ptr= (UInt32 *) inFileCatalogInfo->extFinderInfo;
		
			for(i=0;i<4;i++)
			{
				if (tUnsignedInt32Ptr[i]!=0)
				{
					// We need to create a ._file
				
					splitNeeded=TRUE;
					break;
				}
			}
		}
	}
	
	// 3. Split if needed
	
	if (splitNeeded==TRUE)
	{
		FSRef tNewFileReference;
		HFSUniStr255 tNewFileName;
	
		// Get the absolute Posix Path Name
		
		tErr=FSRefMakePath(inFileReference,tPOSIXPath,tPOSIXPathMaxLength);
		
		if (tErr==noErr)
		{
			if (lstat((char *) tPOSIXPath,&tFileStat)==-1)
			{
				switch(errno)
				{
					case ENOENT:
						// A COMPLETER
						
						break;
					default:
						// A COMPLETER
						
						break;
				}
				
				tErr=-1;
				
				goto byebye;
			}
		}
		else
		{
			logerror("An error occurred when trying to get the absolute path of a file or directory\n");
			
			tErr=-1;
				
			goto byebye;
		}
		
		if (gVerboseMode==TRUE)
		{
			printf("    splitting %s...\n",tPOSIXPath);
		}
		
		// Check that we do not explode the current limit for file names
		
		if (inFileName->length>gMaxFileNameLength)
		{
			// We do not have enough space to add the ._ prefix
		
			// The file name is too long
					
			// Write the error
			
			logerror("File name is too long. The maximum length allowed is %ld characters\n",gMaxFileNameLength+2);
			
			return -1;
		}
		
		tNewFileName.length=inFileName->length+2;
		
		tNewFileName.unicode[0]='.';
		tNewFileName.unicode[1]='_';
		
		BlockMoveData(inFileName->unicode,tNewFileName.unicode+2,inFileName->length*sizeof(UniChar));
		
		// We need to create a ._file
		
tryagain:

		tErr=FSCreateFileUnicode(inParentReference,tNewFileName.length,tNewFileName.unicode,0,NULL,&tNewFileReference,NULL);
		
		if (tErr!=noErr)
		{
			switch(tErr)
			{
				case bdNamErr:
				case fsmBadFFSNameErr:
				case errFSNameTooLong:
					// The file name is too long
					
					// Write the error
					
					logerror("File name is too long. The maximum length allowed is %ld characters\n",gMaxFileNameLength+2);
					
					break;
				case dskFulErr:
					
					logerror("Disk is full\n");
					
					break;
					
				case errFSQuotaExceeded:
					
					logerror("Your quota are exceeded\n");
					
					break;
				case dupFNErr:
				
					// The file already exists, we need to try to delete it before recreating it
					
					tErr=FSMakeFSRefUnicode(inParentReference,tNewFileName.length,tNewFileName.unicode,kTextEncodingDefaultFormat,&tNewFileReference);
					
					if (tErr==noErr)
					{
						// Delete the current ._file
						
						tErr=FSDeleteObject(&tNewFileReference);
						
						if (tErr==noErr)
						{
							goto tryagain;
						}
						else
						{
							// A COMPLETER
						}
					}
					else
					{
						// A COMPLETER
					}
					
					break;
				
				case afpVolLocked:
					// A COMPLETER
					break;
					
				default:
					// A COMPLETER
					break;
			}
			
			return -1;
		}
		
		tErr=FSOpenFork(&tNewFileReference,0,NULL,fsWrPerm,&tNewFileRefNum);
		
		if (tErr==noErr)
		{
			unsigned char tAppleDoubleMagicNumber[4]=  {0x00,0x05,0x16,0x07};
			unsigned char tAppleDoubleVersionNumber[4]={0x00,0x02,0x00,0x00};
			unsigned char tAppleDoubleFiller[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
			ByteCount tRequestCount;
			UInt16 tNumberOfEntries;
			UInt16 tSwappedNumberOfEntries;

			UInt32 tEntryID;
			UInt32 tEntryOffset;
			UInt32 tEntryLength;
			
			// Write the Magic Number
			
			tRequestCount=4;
			
			tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tRequestCount,tAppleDoubleMagicNumber,NULL);
			
			if (tErr!=noErr)
			{
				goto writebail;
			}
			
			// Write the Version Number
			
			tRequestCount=4;
			
			tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tRequestCount,tAppleDoubleVersionNumber,NULL);
			
			if (tErr!=noErr)
			{
				goto writebail;
			}
			
			// Write the Filler
			
			tRequestCount=16;
			
			tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tRequestCount,tAppleDoubleFiller,NULL);
			
			if (tErr!=noErr)
			{
				goto writebail;
			}
			
			// Compute the Number of Entries
			
			tNumberOfEntries=0x0002;
			
			tSwappedNumberOfEntries=tNumberOfEntries;
			
#ifdef __LITTLE_ENDIAN__
			
			// Swap for Intel processor
			
			tSwappedNumberOfEntries=CFSwapInt16(tSwappedNumberOfEntries);

#endif			
			tRequestCount=2;
			
			tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tRequestCount,&tSwappedNumberOfEntries,NULL);
			
			if (tErr!=noErr)
			{
				goto writebail;
			}
			
			// Write the Entries Descriptor
			
			// **** Finder Info
			
			tEntryID=0x00000009;		// Finder Info ID
			
			tEntryOffset=0x0000001A+tNumberOfEntries*12;
			
			tEntryLength=0x00000020;	// 32 bytes
			
#ifdef __LITTLE_ENDIAN__
			
			// Swap for Intel processor
			
			tEntryID=CFSwapInt32(tEntryID);
			
			tEntryOffset=CFSwapInt32(tEntryOffset);
			
			tEntryLength=CFSwapInt32(tEntryLength);
			
#endif			
			tRequestCount=4;
			
			tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tRequestCount,&tEntryID,NULL);
			
			if (tErr!=noErr)
			{
				goto writebail;
			}
			
			tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tRequestCount,&tEntryOffset,NULL);
			
			if (tErr!=noErr)
			{
				goto writebail;
			}
			
			tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tRequestCount,&tEntryLength,NULL);
			
			if (tErr!=noErr)
			{
				goto writebail;
			}
			
			tEntryID=0x00000002;		// Resource Fork ID
				
			tEntryOffset=0x00000052;
				
			if (hasResourceFork==TRUE)
			{
				// **** Finder Info
			
				tEntryLength=tResourceForkSize;	// As you can see the AppleDouble format file is not ready for forks bigger than 2 GB
			}
			else
			{
				tEntryLength=0;
			}
			
#ifdef __LITTLE_ENDIAN__
				
			// Swap for Intel processor
		
			tEntryID=CFSwapInt32(tEntryID);
			
			tEntryOffset=CFSwapInt32(tEntryOffset);
			
			tEntryLength=CFSwapInt32(tEntryLength);
#endif

			tRequestCount=4;
			
			tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tRequestCount,&tEntryID,NULL);
			
			if (tErr!=noErr)
			{
				goto writebail;
			}
			
			tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tRequestCount,&tEntryOffset,NULL);
			
			if (tErr!=noErr)
			{
				goto writebail;
			}
		
			tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tRequestCount,&tEntryLength,NULL);
		
			if (tErr!=noErr)
			{
				goto writebail;
			}
			
			// Write the Entries
			
			// **** Write Finder Info
			
#ifdef __LITTLE_ENDIAN__

			// Intel Processors
			
			// Even though it's referenced as a bytes field in the File API, this is actually a structure we need to swap...

			if (inFileCatalogInfo->nodeFlags & kFSNodeIsDirectoryMask)
			{
				// It's a fragging folder
			
				FolderInfo * tFolderInfoStruct;
				ExtendedFolderInfo * tExtendedFolderInfoStruct;
				
				// Swap FolderInfo Structure
				
				tFolderInfoStruct=(FolderInfo *) inFileCatalogInfo->finderInfo;
				
				SWAP_RECT(tFolderInfoStruct->windowBounds);
				tFolderInfoStruct->finderFlags=CFSwapInt16(tFolderInfoStruct->finderFlags);
				SWAP_POINT(tFolderInfoStruct->location);
				tFolderInfoStruct->reservedField=CFSwapInt16(tFolderInfoStruct->reservedField);
				
				// Swap ExtendedFolderInfo Info Structure
				
				tExtendedFolderInfoStruct=(ExtendedFolderInfo *) inFileCatalogInfo->extFinderInfo;
				
				SWAP_POINT(tExtendedFolderInfoStruct->scrollPosition);
				tExtendedFolderInfoStruct->reserved1=CFSwapInt32(tExtendedFolderInfoStruct->reserved1);
				tExtendedFolderInfoStruct->extendedFinderFlags=CFSwapInt16(tExtendedFolderInfoStruct->extendedFinderFlags);
				tExtendedFolderInfoStruct->reserved2=CFSwapInt16(tExtendedFolderInfoStruct->reserved2);
				tExtendedFolderInfoStruct->putAwayFolderID=CFSwapInt32(tExtendedFolderInfoStruct->putAwayFolderID);
			}
			else
			{
				// I'm just a file, you know
				
				FileInfo * tFileInfoStruct;
				ExtendedFileInfo * tExtendedFileInfoStruct;
				
				// Swap FileInfo Structure
				
				tFileInfoStruct=(FileInfo *) inFileCatalogInfo->finderInfo;
				
				tFileInfoStruct->fileType=CFSwapInt32(tFileInfoStruct->fileType);
				tFileInfoStruct->fileCreator=CFSwapInt32(tFileInfoStruct->fileCreator);
				tFileInfoStruct->finderFlags=CFSwapInt16(tFileInfoStruct->finderFlags);
				SWAP_POINT(tFileInfoStruct->location);
				tFileInfoStruct->reservedField=CFSwapInt16(tFileInfoStruct->reservedField);
				
				// Swap ExtendedFileInfo Structure
				
				tExtendedFileInfoStruct=(ExtendedFileInfo *) inFileCatalogInfo->extFinderInfo;
				
				tExtendedFileInfoStruct->reserved1[0]=CFSwapInt16(tExtendedFileInfoStruct->reserved1[0]);
				tExtendedFileInfoStruct->reserved1[1]=CFSwapInt16(tExtendedFileInfoStruct->reserved1[1]);
				tExtendedFileInfoStruct->reserved1[2]=CFSwapInt16(tExtendedFileInfoStruct->reserved1[2]);
				tExtendedFileInfoStruct->reserved1[3]=CFSwapInt16(tExtendedFileInfoStruct->reserved1[3]);
				tExtendedFileInfoStruct->extendedFinderFlags=CFSwapInt16(tExtendedFileInfoStruct->extendedFinderFlags);
				tExtendedFileInfoStruct->reserved2=CFSwapInt16(tExtendedFileInfoStruct->reserved2);
				tExtendedFileInfoStruct->putAwayFolderID=CFSwapInt32(tExtendedFileInfoStruct->putAwayFolderID);
			}

#endif
			
			tRequestCount=16;
				
			tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tRequestCount,inFileCatalogInfo->finderInfo,NULL);
				
			if (tErr!=noErr)
			{
				goto writebail;
			}
				
			tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tRequestCount,inFileCatalogInfo->extFinderInfo,NULL);
				
			if (tErr!=noErr)
			{
				goto writebail;
			}
			
			// **** Write Resource Fork?
			
			if (hasResourceFork==TRUE)
			{
				// We need to be clever and copy the Resource Fork by chunks to avoid using too much memory
				
				static UInt8 * tBuffer=NULL;
				static ByteCount tReadRequestCount=0;
				ByteCount tReadActualCount;
				OSErr tReadErr;

#define GOLDIN_BUFFER_ONE_MEGABYTE_SIZE		1048576
				
				if (tBuffer==NULL)
				{
					tReadRequestCount=GOLDIN_BUFFER_ONE_MEGABYTE_SIZE;
					
					do
					{
						tBuffer=(UInt8 *) malloc(tReadRequestCount*sizeof(UInt8));
					
						tReadRequestCount/=2;
					}
					while (tBuffer==NULL && tReadRequestCount>1);
					
					if (tBuffer!=NULL && tReadRequestCount>1)
					{
						tReadRequestCount*=2;
					}
					else
					{
						// A COMPLETER
					}
				}
				
				do
				{
					tReadErr=FSReadFork(tForkRefNum, fsAtMark,0, tReadRequestCount, tBuffer, &tReadActualCount);
					
					if (tReadErr==noErr || tReadErr==eofErr)
					{
						tErr=FSWriteFork(tNewFileRefNum,fsAtMark,0,tReadActualCount,tBuffer,NULL);
						
						if (tErr!=noErr)
						{
							break;
						}
					}
					else
					{
						break;
					}
				
				}
				while (tReadErr!=eofErr);
				
				if (tReadErr!=eofErr)
				{
					// A problem occurred while reading the Resource Fork
					
					goto writebail;
				}
				else
				if (tErr!=noErr)
				{
					// A problem occurred while writing the Resource Fork Data to the AppleDouble file
					
					goto writebail;
				}
			}
		
			tErr=FSCloseFork(tNewFileRefNum);
			
			tErr=noErr;
			
			// Set the owner
			
			tErr=FSSetCatalogInfo(&tNewFileReference,kFSCatInfoPermissions,inFileCatalogInfo);
			
			if (tErr!=noErr)
			{
				//logerror("Permissions, owner and group could not be set for the AppleDouble file of %s\n",tPOSIXPath);
				
				tErr=-1;
				
				goto byebye;
			}
		}
		else
		{
			// A COMPLETER
		}
		
		// Close the Resource Fork if needed
		
		if (hasResourceFork==TRUE)
		{
			tErr=FSCloseFork(tForkRefNum);
		
			if (gStripResourceForks==TRUE && tErr==noErr)
			{
				// Strip the resource fork
				
				tErr=FSDeleteFork(inFileReference,sResourceForkName.length,sResourceForkName.unicode);
				
				if (tErr!=noErr)
				{
					switch(tErr)
					{
						case errFSForkNotFound:
							// This is not important
							tErr=noErr;
							break;
						default:
							// A COMPLETER
							break;
					}
				}
			}
			else
			{
				if (gStripResourceForks==TRUE && tErr!=noErr)
				{
					logerror("Resource Fork could not be stripped from %s\n",tPOSIXPath);
				
					// A COMPLETER
				}
			}
		}
	}
	
	return tErr;
	
writebail:

	switch(tErr)
	{
		case dskFulErr:
			logerror("Disk is full\n");
			break;
		case errFSQuotaExceeded:
			logerror("Your quota are exceeded\n");
			break;
		default:
			logerror("An unknown error occurred while writing the AppleDouble file of %s\n",tPOSIXPath);
			break;
	}
	
	FSCloseFork(tNewFileRefNum);
	
byebye:

	if (hasResourceFork==TRUE)
	{
		FSCloseFork(tForkRefNum);
	}
	
	return tErr;
}
static OSErr mainQTInst(void					*unused,
						OSType					order,				// Order to execute
						InstrData				*InsHeader,			// Ptr on instrument header
						sData					**sample,			// Ptr on samples data
						short					*sampleID,			// If you need to replace/add only a sample, not replace the entire instrument (by example for 'AIFF' sound)
																	// If sampleID == -1 : add sample else replace selected sample.
						CFURLRef				AlienFileURLRef,	// IN/OUT file
						PPInfoPlug				*thePPInfoPlug)
{
	MADErr		myErr = MADNoErr;
	FSIORefNum	iFileRefI = -1;
	ByteCount	inOutBytes;
	FSSpec		tmpSpec;
	OSErr		QTErr = noErr;
	
	myErr = CFURLToFSSpec(AlienFileURLRef, &tmpSpec);
	switch (myErr) {
		case memFullErr:
			return MADNeedMemory;
			break;
			
		default:
		case fnfErr:
			return order == MADPlugExport ? MADWritingErr : MADReadingErr;
			break;
						
		case noErr:
			break;
	}
	
	switch (order) {
		case MADPlugPlay:
			break;
			
		case MADPlugImport:
		{
			Ptr				theSound;
			int				lS, lE;
			short			sS;
			unsigned long	rate;
			bool			stereo;
			FSSpec			newFile;
			
			myErr = ConvertDataToWAVE(tmpSpec, &newFile, thePPInfoPlug);
			if (myErr == noErr) {
				theSound = ConvertWAV(&newFile, &lS, &lE, &sS, &rate, &stereo);
				
				if (theSound) {
					long sndSize = GetPtrSize(theSound);
					Ptr newSound = malloc(sndSize);
					memcpy(newSound, theSound, sndSize);
					DisposePtr(theSound);
					myErr = inAddSoundToMAD(newSound, sndSize, lS, lE, sS, 60, rate, stereo, newFile.name, InsHeader, sample, sampleID);
				} else {
					myErr = MADNeedMemory;
				}
				
				FSpDelete(&newFile);
			}
		}
			break;
			
		case MADPlugTest:
		{
			Boolean canOpenAsMovie = false;
			FInfo fInfo;
			
			FSpGetFInfo(&tmpSpec, &fInfo);
			QTErr = CanQuickTimeOpenFile(&tmpSpec, fInfo.fdType, 0, NULL, &canOpenAsMovie, NULL, 0);
			if (QTErr == noErr && canOpenAsMovie == true) {
				myErr = MADNoErr;
			} else {
				myErr = MADIncompatibleFile;
			}
		}
			break;
			
		case MADPlugExport:
			if (*sampleID >= 0) {
				OSType			compType = 'NONE';
				unsigned long	rate;
				sData 			*curData = sample[*sampleID];
				short			numChan;
				
				FSpDelete(&tmpSpec);
				myErr = FSpCreate(&tmpSpec, 'TVOD', 'AIFF', smCurrentScript);
				if(myErr == noErr)
					myErr = FSpOpenDF(&tmpSpec, fsCurPerm, &iFileRefI);
				
				if (myErr == noErr) {
					inOutBytes 	= curData->size;
					rate		= curData->c2spd;
					
					if (curData->stereo)
						numChan = 2;
					else
						numChan = 1;
					
					myErr = SetupAIFFHeader(iFileRefI, numChan, rate << 16L, curData->amp, compType, inOutBytes, 0);
					
					if(myErr == noErr)
						myErr = FSWriteFork(iFileRefI, fsAtMark, 0, inOutBytes, curData->data, &inOutBytes);
					FSCloseFork(iFileRefI);
				}
			}
			break;
			
		default:
			myErr = MADOrderNotImplemented;
			break;
	}
	
	return myErr;
}
Exemple #8
0
void	SMACsdec::GetSourceData(SoundComponentData** outData)
{
	//	initialize the return value
	*outData = NULL;
	
	//	stuff as many packets as we can into the decoder
	UInt32 theError = 0;
	void*	theInputData = NULL;
	UInt32	theInputDataByteSize = 0;
	UInt32	theInputDataFrameSize = 0;
#if	TARGET_API_MAC_OSX
	bool threadLocked;
#endif

#if !(TARGET_MAC_OS && IS_COMPILER_WORKING)
	UInt32 totalFloats;
#endif

#if	TARGET_API_MAC_OSX
	// Lock the thread. The last thing we want is to have our state change in the middle of
	// a decode -- it will typically cause a crash
	threadLocked = mThreadStateMutex->Lock();
#endif
	//	prime things with the current packet
	GetCurrentInputPacket(theInputData, theInputDataByteSize, theInputDataFrameSize);
	// Trying to mimic Decompressor.c in this case
    if((theInputData == NULL) || (theInputDataFrameSize == 0 && theInputDataByteSize == 0) )
    {
    	// silence with non-zero duration.
 		mOutputData.buffer = NULL;
		*outData = mSourceData;
    }
    else
    {
		//	loop and stuff the decoder with packets
		bool isDone = false;
		while((theInputData != NULL) && (theInputDataByteSize != 0) && (!isDone))
		{
			//	give the current packet to the decoder
			UInt32	theNumberInputPackets = 1;
			UInt32	theNumberInputBytesConsumed = theInputDataByteSize;
			AudioStreamPacketDescription theInputPacketDescription = { 0, 0, theInputDataByteSize };
			theError = AudioCodecAppendInputData(mDecoder, theInputData, &theNumberInputBytesConsumed, &theNumberInputPackets, &theInputPacketDescription);
			//printf("mDecoder == %lu, Appended %ld bytes in %ld packets\n", (UInt32)(&mDecoder), theNumberInputBytesConsumed, theNumberInputPackets);
			ThrowIfError(theError, (CAException)theError, "SMACsdec::GetSourceData: got an error from AudioCodecAppendInputData");
			
			//	we know that the decoder is full if it ever says that it didn't take any data from the input buffer
			if( (theNumberInputBytesConsumed > 0) && (theNumberInputBytesConsumed == theInputDataByteSize) )
			{
				//	consume the data the decoder used
				ConsumeCurrentInputPacket(theInputDataByteSize, 0);
				
				//	get the next packet
				GetCurrentInputPacket(theInputData, theInputDataByteSize, theInputDataFrameSize);
			}
			else if (theNumberInputBytesConsumed > 0)
			{
				ConsumeCurrentInputPacket(theNumberInputBytesConsumed, 0);
				isDone = true;
			}
			else
			{
				isDone = true;
			}
		}
		//	the decoder is as full of data as we can make it at this time,
		//	so pull out 1 packet of output data
		UInt32 theOutputDataByteSize = mPacketFrameSize * sizeof(float) * mOutputData.numChannels;
		UInt32 theNumberOutputFrames = mPacketFrameSize;
		UInt32 theDecoderStatus = kAudioCodecProduceOutputPacketFailure;
		theError = AudioCodecProduceOutputPackets(mDecoder, mFloatBuffer, &theOutputDataByteSize, &theNumberOutputFrames, NULL, &theDecoderStatus);
		//printf ("        theNumberOutputFrames == %ld\n",theNumberOutputFrames);
		ThrowIfError(theError, (CAException)theError, "SMACsdec::GetSourceData: got an error from AudioCodecProduceOutputPackets");
		
		//	set up the return values if we got a packet back from the decoder
		if(theNumberOutputFrames)
		{
			//theNumberOutputFrames = theOutputDataByteSize / mOutputData.numChannels / sizeof(float);
		//	ThrowIfError(theNumberOutputFrames != theOutputDataByteSize / mOutputData.numChannels / sizeof(float), "SMACsdec::GetSourceData: got an error from AudioCodecProduceOutputPackets");

#if TARGET_MAC_OS && IS_COMPILER_WORKING
			Float32ToNativeInt16(mHasAltiVec, mFloatBuffer, ((short *)mOutputBuffer), theOutputDataByteSize >> 2);
#else
			totalFloats = theOutputDataByteSize >> 2;
			for (unsigned int i = 0; i < totalFloats; i++)
			{
				((short *)mOutputBuffer)[i] = (SInt16)(32768.0 * mFloatBuffer[i]);
			}
#endif
			mOutputData.buffer = mOutputBuffer;
			if (mOutputData.numChannels == 2) // It's either 1 or 2 here in SMAC land
			{
				mOutputData.sampleCount = (theOutputDataByteSize >> 3); // These really are frames. It's a QuickTime/SoundManager ism
			}
			else
			{
				mOutputData.sampleCount = (theOutputDataByteSize >> 2);
			}
			*outData = &mOutputData;
			
	#if	CaptureDataToFile
			if(mOutputFileRefNum != -1)
			{
				UInt32 theActualBytesWritten = 0;
				FSWriteFork(mOutputFileRefNum, fsAtMark, 0, theOutputDataByteSize, mOutputBuffer, &theActualBytesWritten);
			}
	#endif
		}