示例#1
0
// MW-2012-02-17: [[ LogFonts ]] Actually load the stack file (wrapped by readfile
//   to handle font table cleanup).
IO_stat MCDispatch::doreadfile(const char *openpath, const char *inname, IO_handle &stream, MCStack *&sptr)
{
	Boolean loadhome = False;
	char version[8];

	if (readheader(stream, version) == IO_NORMAL)
	{
		if (strcmp(version, MCversionstring) > 0)
		{
			MCresult->sets("stack was produced by a newer version");
			return IO_ERROR;
		}

		// MW-2008-10-20: [[ ParentScripts ]] Set the boolean flag that tells us whether
		//   parentscript resolution is required to false.
		s_loaded_parent_script_reference = false;

		uint1 charset, type;
		char *newsf;
		if (IO_read_uint1(&charset, stream) != IO_NORMAL
		        || IO_read_uint1(&type, stream) != IO_NORMAL
		        || IO_read_string(newsf, stream) != IO_NORMAL)
		{
			MCresult->sets("stack is corrupted, check for ~ backup file");
			return IO_ERROR;
		}
		delete newsf; // stackfiles is obsolete
		MCtranslatechars = charset != CHARSET;
		sptr = nil;
		/* UNCHECKED */ MCStackSecurityCreateStack(sptr);
		if (stacks == NULL)
			sptr->setparent(this);
		else
			sptr->setparent(stacks);
		sptr->setfilename(strclone(openpath));

		if (MCModeCanLoadHome() && type == OT_HOME)
		{
			char *lstring = NULL;
			char *cstring = NULL;
			IO_read_string(lstring, stream);
			IO_read_string(cstring, stream);
			delete lstring;
			delete cstring;
		}

		MCresult -> clear();

		if (IO_read_uint1(&type, stream) != IO_NORMAL
		    || type != OT_STACK && type != OT_ENCRYPT_STACK
		    || sptr->load(stream, version, type) != IO_NORMAL)
		{
			if (MCresult -> isclear())
				MCresult->sets("stack is corrupted, check for ~ backup file");
			destroystack(sptr, False);
			sptr = NULL;
			return IO_ERROR;
		}
		
		// MW-2011-08-09: [[ Groups ]] Make sure F_GROUP_SHARED is set
		//   appropriately.
		sptr -> checksharedgroups();
		
		if (sptr->load_substacks(stream, version) != IO_NORMAL
		        || IO_read_uint1(&type, stream) != IO_NORMAL
		        || type != OT_END)
		{
			if (MCresult -> isclear())
				MCresult->sets("stack is corrupted, check for ~ backup file");
			destroystack(sptr, False);
			sptr = NULL;
			return IO_ERROR;
		}

		if (stacks != NULL)
		{
			MCStack *tstk = stacks;
			do
			{
				if (sptr->hasname(tstk->getname()))
				{
					MCAutoNameRef t_stack_name;
					/* UNCHECKED */ t_stack_name . Clone(sptr -> getname());

					delete sptr;
					sptr = NULL;

					if (strequal(tstk->getfilename(), openpath))
						sptr = tstk;
					else
					{
						MCdefaultstackptr->getcard()->message_with_args(MCM_reload_stack, MCNameGetOldString(tstk->getname()), openpath);
						tstk = stacks;
						do
						{
							if (MCNameIsEqualTo(t_stack_name, tstk->getname(), kMCCompareCaseless))
							{
								sptr = tstk;
								break;
							}
							tstk = (MCStack *)tstk->next();
						}
						while (tstk != stacks);
					}

					return IO_NORMAL;
				}
				tstk = (MCStack *)tstk->next();
			}
			while (tstk != stacks);
		}
		
		appendstack(sptr);
		
		sptr->extraopen(false);

		// MW-2008-10-28: [[ ParentScript ]]
		// We just loaded a stackfile, so check to see if parentScript resolution
		// is required and if so do it.
		// MW-2009-01-28: [[ Inherited parentScripts ]]
		// Resolving parentScripts may allocate memory, so 'resolveparentscripts'
		// will return false if it fails to allocate what it needs. At some point
		// this needs to be dealt with by deleting the stack and returning an error,
		// *However* at the time of writing, 'readfile' isn't designed to handle
		// this - so we just ignore the result for now (note that all the 'load'
		// methods *fail* to check for no-memory errors!).
		if (s_loaded_parent_script_reference)
			sptr -> resolveparentscripts();
		
	}
	else
	{
		MCS_seek_set(stream, 0);
		if (stacks == NULL)
		{
			MCnoui = True;
			MCscreen = new MCUIDC;
			/* UNCHECKED */ MCStackSecurityCreateStack(stacks);
			MCdefaultstackptr = MCstaticdefaultstackptr = stacks;
			stacks->setparent(this);
			stacks->setname_cstring("revScript");
			uint4 size = (uint4)MCS_fsize(stream);
			char *script = new char[size + 2];
			script[size] = '\n';
			script[size + 1] = '\0';
			if (IO_read(script, sizeof(char), size, stream) != IO_NORMAL
			        || !stacks->setscript(script))
			{
				delete script;
				return IO_ERROR;
			}
		}
		else
		{
			char *tname = strclone(inname);
			
			// MW-2008-06-12: [[ Bug 6476 ]] Media won't open HC stacks
			if (!MCdispatcher->cut(True) || hc_import(tname, stream, sptr) != IO_NORMAL)
			{
				MCresult->sets("file is not a stack");
				delete tname;
				return IO_ERROR;
			}
		}
	}
	return IO_NORMAL;
}