コード例 #1
0
ファイル: PRDocument.cpp プロジェクト: MaddTheSane/tntbasic
void
PRDocument::CommonOpenFile(
	LFile*			inFile,
	PDContainer*	inRsrcContainer)
{

	// Validate pointers.

	ValidateThis_();
	ValidateObject_(mRMMap);
	ValidateObject_(inRsrcContainer);
	ValidateObject_(mSelection.GetObject());
	mRsrcContainer = inRsrcContainer;

	if (mIsFlattened)
	{
		// Configure resource container object to listen for RMMap's change messages.

		mRsrcContainer->SetRMMap(mRMMap);
		mRsrcContainer->AddListener(mSelection);
		mSelection->SetRootElement(mRsrcContainer);

		// If there is a file, open it and read its resources. Scanning the resource map
		// causes RMResource objects to get created and RMMap_ChangeMessage objects to
		// be sent. These change messages cause the container to create objects representing
		// the editable objects in the resource fork. This becomes the displayed
		// object hierarchy.
		
		mFile = inFile;
		if (mUnflattenedFile == nil)
		{
			FSSpec	flatSpec;
			inFile->GetSpecifier(flatSpec);
			mUnflattenedFile = CreateNewUnflattenedFile(&flatSpec);
		}

		if (mUnflattenedFile != nil) {

			ValidateObject_(mUnflattenedFile);
			OpenResourceFile(mUnflattenedFile);

			mRMMap->ScanNewMainFile(mUnflattenedFile);
			mIsSpecified = true;
			mRMMap->UpdateComplete();
			mRsrcContainer->ResetModified();
		}
		
		// Initialize the selection.

		mSelection->SetRootElement(mRsrcContainer);
	}
	else
	{
		RSDocument::CommonOpenFile(inFile, inRsrcContainer);
	}
}
コード例 #2
0
	static shapes::ShapeWrapper load(const char* name)
	{
		std::ifstream input;
		OpenResourceFile(input, "models", name, ".obj");
		if(!input.good())
			throw std::runtime_error("Error opening file for reading");

		return shapes::ShapeWrapper(
			List("Position").Get(),
			shapes::ObjMesh(
				input,
				shapes::ObjMesh::LoadingOptions(false)
			)
		);
	}
コード例 #3
0
	static shapes::ShapeWrapper load_objects(void)
	{
		std::ifstream input;
		OpenResourceFile(input, "models", "arrow_z", ".obj");
		if(!input.good())
			throw std::runtime_error("Error opening file for reading");

		return shapes::ShapeWrapper(
			List("Position")("Normal")("Material").Get(),
			shapes::ObjMesh(
				input,
				shapes::ObjMesh::LoadingOptions(false).Normals().Materials()
			)
		);
	}
コード例 #4
0
	MeshInputFile(void)
	{
		OpenResourceFile(stream, "models", "large_fan", ".obj");
	}
コード例 #5
0
ファイル: PRDocument.cpp プロジェクト: MaddTheSane/tntbasic
void
PRDocument::DoSave()
{

	// Validate pointers.
	
	ValidateThis_();
	ValidateObject_(mRFMap);
	ValidateObject_(mFile);

	// Ensure that memory allocations succeed.
	
	StCriticalSection crit;

	// Finalize any pending actions while resource file is still open.

	PostAction(nil);

 	// Make a name for temporary file using base string provided by caller and a time stamp.
	
	LStr255 tempFileName(STR_FileTitles, str_TempSwapFile);
	if (tempFileName.Length() > 20)
		tempFileName[(UInt8)0] = 20;
	
	UInt32 time;
	::GetDateTime(&time);
	LStr255 tempFileNumber((SInt32) time);
	tempFileName += tempFileNumber;

	// Find Temporary Items folder on source file's volume and create temp file there.

	FSSpec documentSpec;
	mFile->GetSpecifier(documentSpec);

	SInt16 theVRef;
	SInt32 theDirID;
	OSErr theErr = ::FindFolder(documentSpec.vRefNum, kTemporaryFolderType,
					kCreateFolder, &theVRef, &theDirID);
	
	// JWW - The volume might not be what we asked, especially if it's on OS X...
	// ...and on OS X, it looks like FindFolder returns an error, so don't throw
	if ((theErr != noErr) || (theVRef != documentSpec.vRefNum))
	{
		// ...and in that case, just use the document folder instead why not?
		theVRef = documentSpec.vRefNum;
		theDirID = documentSpec.parID;
	}

	FSSpec tempFileSpec;
	theErr = ::FSMakeFSSpec(theVRef, theDirID, tempFileName, &tempFileSpec);
	if ((theErr != noErr) && (theErr != fnfErr))
		ThrowOSErr_(theErr);

	try {


		if (mIsFlattened)
		{
			// Make a copy of the file.
		
			SaveCDEFs();							//* 2.1.2: BUG FIX #370

			FSSpec unflattenedSpec;
			mUnflattenedFile->GetSpecifier(unflattenedSpec);

			mUnflattenedFile->CloseResourceFork();	//ugh! required for saving on remote volumes
			::FSpDelete(&tempFileSpec);
			ThrowIfOSErr_(::FileCopy(unflattenedSpec.vRefNum, unflattenedSpec.parID,
								unflattenedSpec.name,
								tempFileSpec.vRefNum, tempFileSpec.parID, nil, tempFileName,
								nil, 0, true));

			// Open temp file and make changes.

			LFile tempFile(tempFileSpec);
			OpenResourceFile(&tempFile);
			mRFMap->SetMainFile(&tempFile);
			RawSave(tempFileSpec, documentSpec);
			
			// Save is done. Now swap files back.
			//
			// With flat files, we'll copy the temp resource fork into the
			// original file's flattened data fork.
			
			SInt16	srcRefNum;
			SInt16	destRefNum;

			StPointerBlock	buffer(bigCopyBuffSize, false, false);
			if (not buffer.IsValid())
			{
				StPointerBlock	smallBuffer(minCopyBuffSize, true, false);
				buffer.Adopt(smallBuffer.Release());
			}

			tempFile.CloseResourceFork();
			mFile->CloseDataFork();
			
			srcRefNum = tempFile.OpenResourceFork(fsRdPerm);
			destRefNum = mFile->OpenDataFork(fsRdWrPerm);

			ThrowIfOSErr_(::CopyFork(srcRefNum, destRefNum, buffer, ::GetPtrSize(buffer)));
			
			// The Save is done.  Now swap the temp and unflattened files.  This fixes
			// bug WB1-42415 where some changes to a flattened ppob file were overwritten
			// unless you closed and reopened the flat ppob after each save.
			tempFile.CloseResourceFork();
			ThrowIfOSErr_(::FSpExchangeFiles(&unflattenedSpec, &tempFileSpec));
			
			::FSpDelete(&tempFileSpec);		// deletes old document

		}
		else
		{
			// Make a copy of the file.
		
			SaveCDEFs();							//* 2.1.2: BUG FIX #370

			mFile->CloseResourceFork();				//ugh! required for saving on remote volumes
			::FSpDelete(&tempFileSpec);
			ThrowIfOSErr_(::FileCopy(documentSpec.vRefNum, documentSpec.parID, documentSpec.name,
								tempFileSpec.vRefNum, tempFileSpec.parID, nil, tempFileName,
								nil, 0, true));

			// Open temp file and make changes.

			LFile tempFile(tempFileSpec);
			OpenResourceFile(&tempFile);
			mRFMap->SetMainFile(&tempFile);
			RawSave(tempFileSpec, documentSpec);
			
			// Save is done. Now swap files back.

			tempFile.CloseResourceFork();
			ThrowIfOSErr_(::FSpExchangeFiles(&documentSpec, &tempFileSpec));
			::FSpDelete(&tempFileSpec);		// deletes old document
		}
	}
	catch (...) {
	
		// Get rid of temp file.

		::FSpDelete(&tempFileSpec);
		
		if (mIsFlattened)
		{
			// Reopen (untouched) main document.
			
			OpenResourceFile(mUnflattenedFile);
			mRFMap->SetMainFile(mUnflattenedFile);
		}
		else
		{
			// Reopen (untouched) main document.
			
			OpenResourceFile(mFile);
			mRFMap->SetMainFile(mFile);
		}

		// Rethrow exception so it gets reported.

		#if SKIPOMPARSE				// grrr-
			throw;
		#endif
	}

	if (mIsFlattened)
	{
		// Reopen (untouched) main document.
		
		OpenResourceFile(mUnflattenedFile);
		mRFMap->SetMainFile(mUnflattenedFile);
	}
	else
	{
		// Reopen (untouched) main document.
		
		OpenResourceFile(mFile);
		mRFMap->SetMainFile(mFile);
	}

}
コード例 #6
0
ファイル: PRDocument.cpp プロジェクト: MaddTheSane/tntbasic
void
PRDocument::DoAESave(
	FSSpec&		inFileSpec,
	OSType		/* inFileType */)
{

	// Validate pointers.
	
	ValidateThis_();
	ValidateObject_(mRFMap);
	ValidateObject_(mWindow);
	
	// Clear any pending actions.
	
	PostAction(nil);

	// Do we have a file already?
	
	OSErr	err;
	Boolean	oldFileIsFlat = false;

	if (mIsSpecified) {
	
		// Yes, cache its location.

		ValidateObject_(mFile);	

		FSSpec documentSpec;
		mFile->GetSpecifier(documentSpec);
				
		// Check if previous file is flat (mIsFlattened is for the new file already)
		err = ::IsFlattenedResourceFile(&documentSpec, &oldFileIsFlat);
		ThrowIfOSErr_(err);

		// First make sure we're not saving in place.
	
		if ((documentSpec.vRefNum == inFileSpec.vRefNum)
		  && (documentSpec.parID == inFileSpec.parID)
		  && (::EqualString(documentSpec.name, inFileSpec.name, false, true))) {

			// Filespecs are the same, just do a normal save.

		  	DoSave();
			return;
		}
		
		// We're saving to a different file, duplicate existing file to new filespec.

		if (mIsFlattened)
		{
			// The new file is a flattened resource file

			if (not mUnflattenedFile)
			{
				LFile	tempNewFile(inFileSpec);
				tempNewFile.CreateNewDataFile(Type_CreatorCode, Type_MacOSDocument, smSystemScript);
				mUnflattenedFile = CreateNewUnflattenedFile(&inFileSpec);
			}
			
			SaveCDEFs();							//* 2.1.2: BUG FIX #370
			
			FSSpec	unflattenedSpec;
			mUnflattenedFile->GetSpecifier(unflattenedSpec);
			
			mFile->CloseResourceFork();				// necessary for copying on some file servers
			mUnflattenedFile->CloseResourceFork();
			
			::FSpDelete(&inFileSpec);
			if (oldFileIsFlat)
			{
				// Moving data from data fork to data fork, so just use FileCopy()
				
				err = ::FileCopy(documentSpec.vRefNum, documentSpec.parID, documentSpec.name,
									inFileSpec.vRefNum, inFileSpec.parID, nil, inFileSpec.name,
									nil, 0, true);

				OpenResourceFile(mUnflattenedFile);	// reopen original file's resource fork
				mRFMap->SetMainFile(mUnflattenedFile);
				if (err)
					Throw_(err);
			}
			else
			{
				// Moving data from resource fork to data fork, so use CopyFork()
				
				LFile	tempNewFile(inFileSpec);
				tempNewFile.CreateNewDataFile(Type_CreatorCode, Type_MacOSDocument, smSystemScript);

				SInt16	srcRefNum;
				SInt16	destRefNum;

				StPointerBlock	buffer(bigCopyBuffSize, false, false);
				if (not buffer.IsValid())
				{
					StPointerBlock	smallBuffer(minCopyBuffSize, true, false);
					buffer.Adopt(smallBuffer.Release());
				}
				
				srcRefNum = mFile->OpenResourceFork(fsRdPerm);
				destRefNum = tempNewFile.OpenDataFork(fsRdWrPerm);
				
				ThrowIfOSErr_(::CopyFork(srcRefNum, destRefNum, buffer, ::GetPtrSize(buffer)));
				
				mUnflattenedFile = CreateNewUnflattenedFile(&inFileSpec);

				OpenResourceFile(mFile);				// reopen original file's resource fork
				mRFMap->SetMainFile(mFile);
			}
		}
		else
		{
			// The new file is a regular resource file

			SaveCDEFs();							//* 2.1.2: BUG FIX #370
			mFile->CloseResourceFork();				// necessary for copying on some file servers
			::FSpDelete(&inFileSpec);

			if (oldFileIsFlat)
			{
				// Copy mUnflattenedFile to new regular resource file
				FSSpec	unflattenedSpec;
				mUnflattenedFile->GetSpecifier(unflattenedSpec);
				mUnflattenedFile->CloseResourceFork();	// necessary for copying on some file servers
				err = ::FileCopy(unflattenedSpec.vRefNum, unflattenedSpec.parID, unflattenedSpec.name,
									inFileSpec.vRefNum, inFileSpec.parID, nil, inFileSpec.name,
									nil, 0, true);

				OpenResourceFile(mUnflattenedFile);		// reopen original file's resource fork
				mRFMap->SetMainFile(mUnflattenedFile);
				if (err)
					Throw_(err);
			}
			else
			{
				// Copy mFile to new regular resource file

				err = ::FileCopy(documentSpec.vRefNum, documentSpec.parID, documentSpec.name,
									inFileSpec.vRefNum, inFileSpec.parID, nil, inFileSpec.name,
									nil, 0, true);

				OpenResourceFile(mFile);				// reopen original file's resource fork
				mRFMap->SetMainFile(mFile);
				if (err)
					Throw_(err);
			}
		}
	}
	else
	{
		// No pre-existing file, make sure we start from an empty document.
	
		err = ::FSpCreate(&inFileSpec, GetCreatorCode(),
									  GetFileTypeCode(), smSystemScript);
		if (err == dupFNErr) {
		
			// File already exists, delete it and try again.
		
			ThrowIfOSErr_(::FSpDelete(&inFileSpec));
			ThrowIfOSErr_(::FSpCreate(&inFileSpec, GetCreatorCode(),
											GetFileTypeCode(), smSystemScript));
		}
		else
			ThrowIfOSErr_(err);
	
		// Add this new file to the recent items menu
		ProcessCommand(cmd_AddFileToRecentItems, &inFileSpec);
	}

	// Make sure there's a resource fork.
	
		// NOTE: This is safe. FSpCreateResFile is defined as a no-op in case the 
		// resource fork already exists.

	// We have already taken care of mUnflattenedFile having a resource fork
	if (not oldFileIsFlat and not mIsFlattened)
	{
		::FSpCreateResFile(&inFileSpec, GetCreatorCode(), GetFileTypeCode(), smSystemScript);
		err = ::ResError();
		if (err != dupFNErr)				// resource fork already exists
			ThrowIfOSErr_(err);
	}

	// Try saving to this file.
	
	LFile* newFile = nil;
	try {
	
		if (mIsFlattened)
		{
			// Create a new file & update resource context.
		
			newFile = new LFile(inFileSpec);
			ValidateObject_((LFile*) newFile);

			mUnflattenedFile = CreateNewUnflattenedFile(&inFileSpec);
			ValidateObject_(mUnflattenedFile);

			OpenResourceFile(mUnflattenedFile);
			mRFMap->SetMainFile(mUnflattenedFile);
		
			// Now save as though we already had the file open.
			
			RawSave(inFileSpec, inFileSpec);
			
			// Set file type/creator for this file.
			
			::FSpChangeCreatorType(&inFileSpec, GetCreatorCode(), GetFileTypeCode());
			
			// Copy the unflattened resource fork over to the flat data fork
			StPointerBlock	buffer(bigCopyBuffSize, false, false);
			if (not buffer.IsValid())
			{
				StPointerBlock	smallBuffer(minCopyBuffSize, true, false);
				buffer.Adopt(smallBuffer.Release());
			}

			SInt16	srcRefNum;
			SInt16	destRefNum;

			srcRefNum = mUnflattenedFile->OpenResourceFork(fsRdPerm);
			destRefNum = newFile->OpenDataFork(fsRdWrPerm);

			err = ::CopyFork(srcRefNum, destRefNum, buffer, ::GetPtrSize(buffer));

		}
		else
		{
			// Create a new file & update resource context.
		
			newFile = new LFile(inFileSpec);
			ValidateObject_((LFile*) newFile);

			OpenResourceFile((LFile*)newFile);
			mRFMap->SetMainFile((LFile*)newFile);
		
			// Now save as though we already had the file open.
			
			RawSave(inFileSpec, inFileSpec);
			
			// Set file type/creator for this file.
			
			::FSpChangeCreatorType(&inFileSpec, GetCreatorCode(), GetFileTypeCode());
		}
	}
	catch(...) {
	
		// Delete new file.
		
		if (newFile != nil) {
			ValidateObject_((LFile*) newFile);
			delete (LFile*)newFile;
		}
		::FSpDelete(&inFileSpec);
		
		// Revert to original resource context.
		
		mRFMap->SetMainFile(mFile);

		// Rethrow the exception.

		#if SKIPOMPARSE
			throw;
		#endif
			
	}

	// Now that we're done, get rid of old file reference.

	if (mFile != nil) {
		ValidateObject_(mFile);
		delete mFile;
	}
	mFile = (LFile*)newFile;
	
	// Update all window titles.
	
	mIsSpecified = true;
	mWindow->SetDescriptor(inFileSpec.name);
	
}
コード例 #7
0
ファイル: getvers.c プロジェクト: GnoConsortium/gno
/*
 * Read the version (and, optionally, comment) resources from the
 * indicated file
 */
static void
ReadResources(char *path_str)
{
	#define noPreload 0x8000
	Word		new_file_id;
	Word		old_file_id;
	Word		old_depth;
	GSString255Ptr	path_ptr;
	Handle		rVer_handle, rCom_handle;
	int		error;

	/* Let user know what file we're working on */
	(void)printf("%s: ",path_str);

	/* Convert C string into GSString */
	path_ptr = malloc_c(strlen(path_str)+2);
	path_ptr->length = strlen(path_str);
	strcpy(path_ptr->text, path_str);

	/* Open the resource fork of the file */
	new_file_id = OpenResourceFile(noPreload+readEnable,
			NULL, (Pointer)path_ptr);
	if ((error = toolerror()) != noError) {
		if (error == 0x0046)
			(void)printf("File not found\n");
		else if (error == 0x0063)
			(void)printf("No resource fork\n");
		else
			(void)printf("Error $%04X opening resource fork\n",
					error);
		rval = 1;
	}
	else {
		/* Set up to search only this resource file, remembering    */
		/* previous setting so it can be restored when closing file */
		old_file_id = GetCurResourceFile();
		SetCurResourceFile(new_file_id);
		old_depth = SetResourceFileDepth(1);

		/* Read the version resource */
		rVer_handle = LoadResource(0x8029,1);
		if ((error = toolerror()) != noError) {
			if (error == 0x1E06)
				(void)printf("No version resource\n");
			else
				(void)printf(
				  "Error $%04X getting version resource\n",
					error);
			rval = 1;
		}
		else {
			PrintVersionResource(*rVer_handle);
		}

		/* Read the comment resource, if requested */
		if ( cflag )   {
			rCom_handle = LoadResource(0x802A,1);
			if ((error = toolerror()) == noError) {
				PrintCommentResource(*rCom_handle);
			}
		}

		/* Close the resource file and restore original search values */
		CloseResourceFile(new_file_id);
		SetCurResourceFile(old_file_id);
		SetResourceFileDepth(old_depth);
	}
	/* Free the memory allocated for the path name */
	free(path_ptr);
}
コード例 #8
0
ファイル: abCalcNDA.c プロジェクト: jeremysrand/abCalc
GrafPortPtr NDAOpen(void)
{
    Pointer pathToSelf;
    unsigned int oldResourceApp;
    LevelRecGS levelDCB;
    unsigned int oldLevel;
    SysPrefsRecGS prefsDCB;
    unsigned int oldPrefs;
    int numOps;
    int i;
    Handle opListCtl;

    if (gCalcActive)
        return NULL;

    oldResourceApp = GetCurResourceApp();
    ResourceStartUp(gUserId);
    pathToSelf = LGetPathname2(gUserId, 1);

    levelDCB.pCount = 2;
    GetLevelGS(&levelDCB);
    oldLevel = levelDCB.level;
    levelDCB.level = 0;
    SetLevelGS(&levelDCB);

    prefsDCB.pCount = 1;
    GetSysPrefsGS(&prefsDCB);
    oldPrefs = prefsDCB.preferences;
    prefsDCB.preferences = (prefsDCB.preferences & 0x1fff) | 0x8000;
    SetSysPrefsGS(&prefsDCB);

    gResourceId = OpenResourceFile(readEnable, NULL, pathToSelf);

    gCalcWinPtr = NewWindow2("\p abCalc ", 0, DrawContents, NULL, refIsResource,
                             abCalcWinNum, rWindParam1);

    SetSysWindow(gCalcWinPtr);
    ShowWindow(gCalcWinPtr);
    SelectWindow(gCalcWinPtr);
    SetPort(gCalcWinPtr);

    if (gOpList == NULL) {
        numOps = abCalcOpNumOps();
        gOpList = malloc(sizeof(*gOpList) * numOps);
        for (i = 0; i < numOps; i++) {
            gOpList[i].memPtr = abCalcOpNth(i)->name;
            gOpList[i].memFlag = 0;
        }
    }

    opListCtl = (Handle)GetCtlHandleFromID(gCalcWinPtr, abCalcOpList);
    NewList2(NULL, 1, (Ref)gOpList, 0, numOps, opListCtl);

    UpdateStack();

    gCalcActive = TRUE;

    prefsDCB.preferences = oldPrefs;
    SetSysPrefsGS(&prefsDCB);

    levelDCB.level = oldLevel;
    SetLevelGS(&levelDCB);

    SetCurResourceApp(oldResourceApp);

    return gCalcWinPtr;
}