Exemple #1
0
daeURI::daeURI(daeString uriString, daeBool nofrag)
{
	initialize();
	// !!!GAC this is inefficient as hell, but was the best way to isolate this functionality till the
	// !!!GAC daeURI class can be written to support modifying URIs better (should be possible to make a URI,
	// !!!GAC change any member and have getURI return the proper const string URI)
	if(nofrag)
	{
		// Strip off the fragment part before constructing the URI
		daeString temp		= safeCreate(uriString);
		daeChar* fragment	= (daeChar*)findCharacterReverse(temp, '#');
		if(fragment)
		{
			*fragment = 0;
		}
		setURI(temp);
		safeDelete(temp);
	}
	else
	{
		// Generate the URI without changing the string
		setURI(uriString);
	}
	if(nofrag)
		validate();
}
Exemple #2
0
void
daeURI::validate(daeURI* baseURI)
{
	// If no base URI was supplied, get the application base and use it
	if (baseURI == NULL)
	{
		if ( container == NULL || (baseURI = container->getDocumentURI()) == NULL ) {
			baseURI = getBaseURI();
		}
		if (this == baseURI ) {
			return;
		}
	}

#if 1
	// This is rewritten according to the updated rfc 3986
	if((protocol != NULL) && (strlen(protocol)>0))  // if defined(R.scheme) then
	{
		// Everything stays the same except path which we normalize
        // T.scheme    = R.scheme;
        // T.authority = R.authority;
        // T.path      = remove_dot_segments(R.path);
        // T.query     = R.query;
		normalizeURIPath((char *)filepath);
		if ( (file == NULL) || (strlen(file)==0) ) {
			//safeDelete(file);
			//safeDelete(extension);
			//file		= safeCreate(baseURI->file);
			//extension	= safeCreate(baseURI->extension);
		}
	}
	else
	{
		if((authority != NULL) && (strlen(authority)>0)) // if defined(R.authority) then
		{
			// Authority and query stay the same, path is normalized
            // T.authority = R.authority;
            // T.path      = remove_dot_segments(R.path);
            // T.query     = R.query;
			normalizeURIPath((char *)filepath);
			if ( (file == NULL) || (strlen(file)==0) ) {
				//safeDelete(file);
				//safeDelete(extension);
				//file		= safeCreate(baseURI->file);
				//extension	= safeCreate(baseURI->extension);
			}
		}
		else
		{
			if(((filepath == NULL) || (strlen(filepath)==0)) && ((file == NULL) || (strlen(file)==0)))  // if (R.path == "") then
			{
                // T.path = Base.path;
				safeDelete(filepath);
				safeDelete(file);
				safeDelete(extension);
				filepath	= safeCreate(baseURI->filepath);
				file		= safeCreate(baseURI->file);
				extension	= safeCreate(baseURI->extension);
				// We don't handle querys, but if we did
				//if defined(R.query) then
				//   T.query = R.query;
				//else
				//   T.query = Base.query;
				//endif;
			}
			else
			{
				if((filepath != NULL) && (*filepath == '/'))  //if (R.path starts-with "/") then
				{
					// T.path = remove_dot_segments(R.path);
					normalizeURIPath((char *)filepath);
				}
				else
				{
					//T.path = merge(Base.path, R.path);
					daeChar* newPath;
					if((strlen(baseURI->authority) != 0) && (strlen(baseURI->filepath)==0) && (strlen(baseURI->file) == 0)) //authority defined, path empty
					{
						newPath = (daeChar*)daeMemorySystem::malloc("uri", strlen(filepath) + 2);
						*newPath = '/';
						*(newPath+1) = 0;
						strcat(newPath,filepath);
					}
					else
					{
						newPath = (daeChar*)daeMemorySystem::malloc("uri", strlen(baseURI->filepath) + strlen(filepath) + 1);
						*newPath = 0;
						strcat(newPath,baseURI->filepath);
						strcat(newPath,filepath);
					}
					//T.path = remove_dot_segments(T.path);
					normalizeURIPath(newPath);
					safeDelete(filepath);
					filepath = newPath;
				}
               // T.query = R.query;
			}
			// T.authority = Base.authority;
			safeDelete(authority);
			authority = safeCreate(baseURI->authority);
		}
		// T.scheme = Base.scheme;
		safeDelete(protocol);
		protocol = safeCreate(baseURI->protocol);
	}
	// T.fragment = R.fragment;

	// Now for the purpose of maintaining the class members, we reassemble all this into a string version of the URI
	daeChar* newURI = (daeChar*)
		daeMemorySystem::malloc(
			"uri",
			strlen(protocol) +		// really scheme
			1 +						// space for ":"
			strlen(authority) +			// really authority
			2 +						// space for "//"
			strlen(filepath) +		// path without the filename
			strlen(file) +			// filename part of the path
			strlen(queryString) +   // "#"
			strlen(id) +			// really fragment
			1);						// terminating zero
	*newURI = 0;

	if(protocol != NULL && *protocol != 0)
	{
		strcat(newURI, protocol);
		strcat(newURI, ":");
	}
	strcat(newURI, "//");
	if(authority != NULL && *authority != 0)
	{
		strcat(newURI, authority);
	}	
	if(filepath != NULL)
		strcat(newURI, filepath);
	if(file != NULL)
		strcat(newURI, file);

	if(id != NULL && *id != 0)
	{
		strcat(newURI,"#");
		strcat(newURI,id);
	}
	// This becomes the new uriString, no need to call internalSetUri because all the class members are up to date
	safeDelete(uriString);
	uriString = newURI;
	state = uri_pending;

	if ( container != NULL ) {
	    daeString fp = container->getDocumentURI()->getFilepath();
	    daeString f = container->getDocumentURI()->getFile();
		if ( strcmp( fp, filepath ) != 0 || strcmp( f, file ) != 0 ) {
			//external reference
			container->getDocument()->addExternalReference( *this );
			external = true;
		}
		else if ( external ) {
			//was external but now isn't
			container->getDocument()->removeExternalReference( *this );
			external = false;
		}
	}

#else
	// RFC 2396 part 5.2 step 3, if the scheme (protocol here) is defined we are done, otherwise inherit the base URI's protocol
	if ((protocol == NULL)||(strlen(protocol)==0)) 
	{
		// Make a copy of the base's protocol, not a reference to it
		safeDelete(protocol);
		protocol = safeCreate(baseURI->protocol);
		// part 5.2 step 4, if the authority is defined we skip to step 7, otherwise inherit the base URI's authority
		if((authority == NULL) || (strlen(authority)== 0))
		{
			// Make a copy of the base's authority, not a reference to it
			safeDelete(authority);
            authority = safeCreate(baseURI->authority);
			// part 5.2 step 5 if the path part (filepath here) begins with a slash we skip to step 7, otherwise resolve the relative path against the base
			if((filepath == NULL) || (*filepath != '/'))
			{
				// part 5.2 step 2, if scheme, authority and path are all empty, this is a reference to the current doc
				// COLLADA DOM wants this to resolve to the URI of the document + the fragment
				// To make this happen we have the URI inherit the file part of the base (if present) and the path
				if( ((filepath == NULL) || (strlen(filepath)==0)) &&			// filepath empty
					((file == NULL) || (strlen(file)==0)) &&					// file empty
					((baseURI->file != NULL) && (strlen(baseURI->file) > 0)))	// baseURI file NOT empty
				{
					// inherit the base's filename
					safeDelete(file);
					file = safeCreate(baseURI->file);
				}
				// part 5.2 step 6, resolving a relative path reference
				// note that in this implementation the filepath does not contain the last segment (the filename.ext)
				// Allocate enough memory to hold the merged path
				daeChar* newPath = (daeChar*)daeMemorySystem::malloc(
						"uri",
						strlen(baseURI->filepath) + 
						strlen(filepath) + 1);
				*newPath = 0;
				// part 5.2 step 6(a) copy the baseURI filepath to the buffer
				strcat(newPath,baseURI->filepath);
				// part 5.2 step 6(b) copy this URI's filepath to the buffer
				if(*filepath != 0)
				{
					strcat(newPath,filepath);
				}
				// part 5.2 step 6(c-g) normalize the new path
				normalizeURIPath(newPath);
				// part 5.2 step 6(h) replace the old filepath with the new path
				safeDelete(filepath);
				filepath = newPath;
			}
		}
		// part 5.2 step 7 assemble the final complete URI
		// Allocate memory to hold the assembled version of the URI
		daeChar* newURI = (daeChar*)
			daeMemorySystem::malloc(
				"uri",
				strlen(protocol) +		// really scheme
				1 +						// space for ":"
				strlen(authority) +			// really authority
				2 +						// space for "//"
				strlen(filepath) +		// path without the filename
				strlen(file) +			// filename part of the path
				strlen(queryString) +   // "#"
				strlen(id) +			// really fragment
				1);						// terminating zero
		*newURI = 0;
		if(protocol != NULL && *protocol != 0)
		{
			strcat(newURI, protocol);
			strcat(newURI, ":");
		}
		if(authority != NULL && *authority != 0)
		{
			strcat(newURI, authority);
		}
		strcat(newURI, "//");
		if(filepath != NULL)
			strcat(newURI, filepath);
		if(file != NULL)
			strcat(newURI, file);
		if(id != NULL && *id != 0)
		{
			strcat(newURI,"#");
			strcat(newURI,id);
		}
		// Reset the URI to the new one
		// Just setting the uriString would probably be enough
		internalSetURI(newURI);
		daeMemorySystem::free("uri",newURI);
	}
	state = uri_pending;

#endif
#if 0
	// If there is no protocol, assume this is a relative URI that needs to be resolved against the base
	if ((protocol == NULL)||(strlen(protocol)==0)) 
	{
		// !!!GAC if the base URI contains a file and this uri does not, copy it over (why??)
		if (((baseURI->file != NULL) &&	(strlen(baseURI->file)>0)) &&
			((file == NULL)||(strlen(file)==0))) 
		{
			if (file != NULL)
				safeDelete(file);
			file = safeCreate(baseURI->file);
		}
		
		// !!!GAC this is a quick and dirty attempt to get the relative URIs properly resolved and the internal
		// !!!GAC paths normalized.  This code should be rewritten when there's time, I wanted to get this up quick
		// !!!GAC so we could test the rest of the system to make sure handing it "correct" URIs doesn't break things.
		
		// Start by allocating memory and putting together just the path component
		daeChar* newPath = (daeChar*)
			daeMemorySystem::malloc(
				"tmp",
				strlen(baseURI->filepath) + 
				strlen(filepathString) + 
				strlen(filepath) + 
				strlen(filepathString) + 
				strlen(file)+1);
		*newPath = 0;
		strcat(newPath,baseURI->filepath);
		strcat(newPath,filepathString);  // !!!GAC this may put in an extra / but if it does the normalization will fix it
		if(*filepath != 0)
		{
			strcat(newPath,filepath);			// !!!GAC only do this if filepath is not empty
			strcat(newPath,filepathString);		// !!!GAC only do this if filepath is not empty
		}
		strcat(newPath,file);
		// Normalize the path according to RFC 2396 (removes extra /, .., . and so on)
		normalizeURIPath(newPath);
		// !!!GAC Allocate memory for the complete URI and assemble it
		daeChar* newURI = (daeChar*)
			daeMemorySystem::malloc(
				"tmp",
				strlen(baseURI->protocol) +
				strlen(protoString) +
				strlen(authority) +
				strlen(hostString) +
				strlen(newPath) +
				strlen(queryString) + 
				strlen(id)+1);
		*newURI = 0;
		strcat(newURI,baseURI->protocol);	// Should be called "scheme" from RFC 2396
		strcat(newURI,protoString);
		strcat(newURI,authority);				// Should be called "authority" 
//		strcat(newURI,hostString);			// !!!GAC not necessary, path always begins with a /
		strcat(newURI,newPath);
		if(strlen(id) != 0)
		{
			// !!!GAC don't append the #id unless it's needed (bug 297)
			strcat(newURI,queryString);
			strcat(newURI,id);
		}
		internalSetURI(newURI);		
		daeMemorySystem::free("tmp",newPath);
		daeMemorySystem::free("tmp",newURI);
	}
	state = uri_pending;
#endif
}
Exemple #3
0
void
daeURI::internalSetURI(daeString _URIString)
{
	daeChar* tmp;

	// Store the originalURI so you can fix it post Reset
	daeString oURI = originalURIString;
	originalURIString = empty;

	// Reset everything
	reset();

	// Fix original URI String
	originalURIString = oURI;
	
	uriString = safeCreate(_URIString);
	
	tmp = (daeChar*)daeMemorySystem::malloc("tmp",strlen(_URIString)+1);
	if ((uriString == empty)||(tmp == NULL))
		return;
	strcpy(tmp,uriString);
	
	daeChar* curSrc = tmp;
	
#if 1
	// Check for a scheme, two or more characters followed by a :
	
//	daeChar* colon = (daeChar*)findCharacter(curSrc,':');
	
	daeChar* colon = validScheme(curSrc);

//	if(colon && (colon-tmp >= 2 ))
	if(colon)
	{
		// Found a scheme, remember it (protocol should be named scheme) 
		*colon = '\0';
		protocol = safeCreate(curSrc);
		// Advance the current pointer past the colon
		curSrc = colon+1;
	}

	// Check for a net path containing an authority, this would begin with //

	if(curSrc[0] == '/' && curSrc[1] == '/')
	{
		// Advance past the double slash to where the authority would start, then find the next slash
		curSrc = curSrc + 2;
		daeChar* slash = (daeChar*)findCharacter(curSrc,'/');
		// Temporarily remove that slash (avoids some branches)
		if ( slash != NULL ) {
			*slash = '\0';
		}
		// Save the text between the slashes as the authority 
		authority = safeCreate(curSrc);
		// Put the slash back and advance the current pointer to it, this puts us at the start of the path
		if (slash != NULL ) {
			*slash = '/';
			curSrc = slash;
		}
	}

	// Search backwards from the end of the URI for the # which denotes the fragment (called ID here)
	daeChar* idSymbol = (daeChar*)findCharacterReverse(curSrc,'#');
	if (idSymbol != NULL)
	{
		// There was a fragment, isolate it by changing the # to a null
		*idSymbol = '\0';
		idSymbol++;
	}
	id = safeCreate(idSymbol);

	// Search backwards for the last / in the path, everything after is the filename

	daeChar* fname = (daeChar*)findCharacterReverse(curSrc,'/');
	daeChar* dir;
	if (fname == NULL)
	{
		// No / found, so the whole thing is the file name and there is no path
		fname = curSrc;
		dir = NULL;
	}
	else 
	{
		// Found a slash, so the filename starts after it and dir starts at curSrc
		fname++;
		dir = curSrc;
	}
	file = safeCreate(fname);

	// Pull the extension (if any) off of the filepath
	
	daeString extStr = findCharacterReverse(fname,'.');
	if (extStr != NULL)
		extension = safeCreate(extStr+1);

	// Now pull off the directory path if it exists by putting a zero at the beginning of fname, this insures filepath will end in a slash
	
	if(fname != NULL) *fname = 0;
	filepath = safeCreate(dir);
	
	state = uri_loaded;
	daeMemorySystem::free("tmp",tmp);

#else
	daeBool isAbsolute;
	daeChar* colon = (daeChar*)findCharacter(curSrc,':');

	// IS ABSOLUTE REFERENCE
	if (colon && (strlen(colon) > 2) &&	(colon[1] == '/') && (colon[2] == '/')) 
	{
		*colon = '\0';
		protocol = safeCreate(curSrc);
		curSrc = colon+3;
		daeString hosttmp = curSrc;
		daeChar* slash = (daeChar*)findCharacter(curSrc,'/');
		if (slash != NULL) 
		{
			*slash = '\0';
			authority = safeCreate(hosttmp);
			curSrc = slash+1;
		}
		isAbsolute = true;
	}
	else {
		protocol = empty;
		isAbsolute = false;
	}

	// Look for the # which denotes the fragment (called ID here)
	daeChar* idSymbol = (daeChar*)findCharacterReverse(curSrc,'#');
	if (idSymbol != NULL)
	{
		// There was a fragment, isolate it by changing the # to a null
		*idSymbol = '\0';
		idSymbol++;
	}

	daeChar* dir = NULL;
	daeChar* fname = (daeChar*)findCharacterReverse(curSrc,'/');
	if (fname == NULL) 
		fname = curSrc;
	else {
		*fname = '\0';
		fname++;
		dir = curSrc;
	}

	filepath = safeCreate(dir);
	int dirLen = (int)strlen(filepath);

	// append a '/'
	if ((filepath != empty) && (dirLen > 0) &&
		(filepath[dirLen-1] != '/')) {
		daeMemorySystem::free("uri",(void*)filepath);
		filepath =	(daeString)daeMemorySystem::malloc("uri", dirLen+2);
		strcpy((daeChar*)filepath,dir);
		*((daeChar*)filepath+dirLen) = '/';
		*((daeChar*)filepath+dirLen+1) = '\0';
	}

	file = safeCreate(fname);
	id = safeCreate(idSymbol);

	daeString extStr = findCharacterReverse(fname,'.');
	if (extStr != NULL)
		extension = safeCreate(extStr+1);
	
	state = uri_loaded;
	daeMemorySystem::free("tmp",tmp);
#endif
}
Exemple #4
0
void
daeURI::setURI(daeString _URIString)
{
	originalURIString = safeCreate(_URIString);
	internalSetURI(_URIString);
}
void cSpawnScripted::doSpawn( cSpawnArea& c ) {
	if ( c.disabled ) {
		c.nextspawn=getClockmSecs()+ (60*RandomNum( mintime, maxtime)*SECS);
		return;
	}
	if( c.current >= max ) {
		c.nextspawn=getClockmSecs()+ (60*RandomNum( mintime, maxtime)*SECS);
		return;
	}

	if( npclists.size() > 0 )
	{
		uint32_t counter = rand()%npclists.size();
		{
			sLocation location;
			if( c.findValidLocation( location ) )
			{
				pChar npc = npcs::AddNPCxyz( INVALID, npclists[counter], location );
				if( npc )
				{
					safeCreate( npc, c );
					return;
				}
			}
		}
	}

	if( itemlists.size() > 0 )
	{

		uint32_t counter = rand()%itemlists.size();
		{
			sLocation location;
			if( c.findValidLocation( location) )
			{
				char list[512];
				sprintf( list, "%i", itemlists[counter] ); // morrolan
				int num = item::CreateRandomItem( list );
				pItem item = item::CreateScriptItem( INVALID, num, 0 );
				if( item ) {
					safeCreate( item, c );
					return;
				}
			}
		}
	}

	if( npcs.size()>0 )
	{
		uint32_t counter = rand()%npcs.size();
		{
			sLocation location;
			if( c.findValidLocation( location ) )
			{
				pChar npc = npcs::AddNPCxyz( INVALID, npcs[counter], location  );
				if ( npc )
				{
					safeCreate( npc, c );
					return;
				}
			}
		}
	}

	c.disabled = true;
	LogError("Scripted spawn %i [%s] couldn't find anything to spawn, check scripts.\n",serial, name.c_str());

}