예제 #1
0
void parseStr(const char* input_str, char** ret, int count, int idx) {
	int i = 0;
	while (isspace(input_str[i])) 
		++i;
	
	int spaces = getSpaces(input_str, count);
	
	ret[idx] = (char*)malloc(count - spaces);				// *** error (need + 1)
	char* begin = ret[idx];
	
	while (!isspace(input_str[i]) && !ispunct(input_str[i])) {				// first line
		if (isCap(input_str[i]) && !isNum(input_str[i])) 
			*ret[idx] = (char)((input_str[i]) - 'A' + 'a');
		else *ret[idx] = input_str[i];
	
		ret[idx]++;
		++i;
	}

	++i;
	
	if (!ispunct(input_str[i])) {
		int prev = 1;
		for (; i < count; ++i) {			// maybe use isspace || isalpha
			if (prev && !isspace(input_str[i])) {			// make sure not more than one consecutive space
				if (!isCap(input_str[i]) && !isNum(input_str[i])) 
					*ret[idx] = (char)((input_str[i]) - 'a' + 'A');
				else *ret[idx] = input_str[i];
			
				ret[idx]++;
				prev = 0;
				
				if (isNum(input_str[i]))
					prev = 1;
			}
			else if (isCap(input_str[i]) && !isspace(input_str[i])) {
				*ret[idx] = (char)((input_str[i]) - 'A' + 'a');
				ret[idx]++;	
			}
			else if (!isCap(input_str[i]) && !isspace(input_str[i])) {
				*ret[idx] = input_str[i];
				ret[idx]++;	
			}
			else if (isspace(input_str[i])) {
				prev = 1;
			}
		}
	}
	
	*ret[idx] = '\0';				// end string
	ret[idx] = begin;
}
	void ScriptWriter::writeDefinition(std::fstream& stream, ScriptDefinition* d, int level)
	{
		// Write the definition header
		stream << getSpaces(level * mNumSpacesPerLevel) + d->getType() + " " + d->getID() << std::endl;
		// Write opening curly brace
		stream << getSpaces(level * mNumSpacesPerLevel) + "{" << std::endl;
		// Write out properties
		std::list<DefinitionProperty*> propList = d->getProperties();
		for(std::list<DefinitionProperty*>::iterator it = propList.begin(); it != propList.end(); ++it)
		{
			DefinitionProperty* p = (*it);
			stream << getSpaces((level + 1) * mNumSpacesPerLevel) + p->getPropertyName();

			Ogre::StringVector sv = p->getValues();
			for(Ogre::StringVector::iterator propertyIter = sv.begin(); propertyIter != sv.end(); ++propertyIter)
			{
				stream << " " + (*propertyIter);
			}

			stream << std::endl;
		}
		// Write out sub Definitions
		std::list<ScriptDefinition*> subDefList = d->getDefinitions();
		if(!subDefList.empty())
		{
			if(!propList.empty())
				stream << std::endl;

			for(std::list<ScriptDefinition*>::iterator it = subDefList.begin(); it != subDefList.end(); ++it)
			{
				writeDefinition(stream,(*it),(level + 1));
				stream << std::endl;
			}
		}
		// Write closing curly brace
		stream << getSpaces(level * mNumSpacesPerLevel) + "}" << std::endl;
	}
예제 #3
0
void test_getSpaces()
{
    assert( getSpaces(0) == "" );
    assert( getSpaces(1) == " " );
    assert( getSpaces(2) == "  " );
    assert( getSpaces(3) == "   " );
    assert( getSpaces(4) == "    " );
    assert( getSpaces(5) == "     " );
}
예제 #4
0
void dfsDirectory(char *directory ,char *keyFile ,int depth)
{
    DIR *dPtr;

    struct dirent *d;

    // Open directory
    if((dPtr = opendir(directory)) == NULL) 
    {
        fprintf(stderr,"Error on opening directory: %s\n", directory);
        exit(1);
    }

    // Change to next directory
    chdir(directory);

    struct stat sb;
    
    while((d = readdir(dPtr))) 
    {
    	// Check status of path for symbolic links
        if(lstat(d->d_name,&sb) == -1)
        {
        	perror("lstat\n");
        	exit(2);
        }
        // Base case:
        if(strcmp(d->d_name, keyFile) == 0)
    	{
    		
    		getSpaces(depth);
    		printf("%s/\n", keyFile);
    		exit(1);
    	} 

    	// If found directory
        if(S_ISDIR(sb.st_mode)) 
        {
        	// Prevent loop / going back
			if(strcmp(curDir,d->d_name) == 0 || strcmp(prevDir,d->d_name) == 0)
					continue; 

   			getSpaces(depth);
				
			printf("%s/\n", d->d_name);
         	
         	// Recursive call
         	dfsDirectory(d->d_name, keyFile, depth+1);
        }
        else
    		
    		getSpaces(depth);
    		
    		printf("%s\n", d->d_name);
    
    } // end while
    
    // Change Directory 
    chdir(prevDir);

    closedir(dPtr);

}
void PhysicsHandler::handleUpdate(EventDetails* const details)
{
    commitChanges();

    //Start the physics timing statistic
    StatTimeElem *PhysicsTimeStatElem = StatCollector::getGlobalElem(statPhysicsTime);
    if(PhysicsTimeStatElem) { PhysicsTimeStatElem->start(); }

    _TimeSinceLast += dynamic_cast<UpdateEventDetails* const>(details)->getElapsedTime();

    if(osgFloor(_TimeSinceLast/getStepSize()) > getMaxStepsPerUpdate())
    {
        SWARNING << "Physics Simulation slowing: dropping " << osgFloor(_TimeSinceLast/getStepSize())-getMaxStepsPerUpdate() << " steps" << std::endl;
        _TimeSinceLast = getMaxStepsPerUpdate()*getStepSize();
    }

    StatIntElem *NPhysicsStepsStatElem = StatCollector::getGlobalElem(statNPhysicsSteps);
    StatTimeElem *CollisionTimeStatElem = StatCollector::getGlobalElem(statCollisionTime);
    StatTimeElem *DynamicsTimeStatElem = StatCollector::getGlobalElem(statSimulationTime);
    while(_TimeSinceLast > getStepSize())
    {
        //Increment the steps statistic
        if(NPhysicsStepsStatElem) { NPhysicsStepsStatElem->inc(); }

        //*********** Collision Checks *************
        //Start the collision timing statistic
        if(CollisionTimeStatElem) { CollisionTimeStatElem->start(); }

        //Do collision checks
        for(UInt32 i(0) ; i<getMFSpaces()->size() ; ++i)
        {
            getSpaces(i)->Collide(getWorld());
        }

        //Stop the collision timing statistic
        if(CollisionTimeStatElem) { CollisionTimeStatElem->stop(); }


        //*********** Simulation step *************

        //Start the simulation timing statistic
        if(DynamicsTimeStatElem) { DynamicsTimeStatElem->start(); }

        //Step the dynamics simulation
        getWorld()->worldQuickStep(getStepSize());

        //Stop the simulation timing statistic
        if(DynamicsTimeStatElem) { DynamicsTimeStatElem->stop(); }

        //Decrease the time since last simulation step
        _TimeSinceLast -= getStepSize();
    }
    StatRealElem *NCollisionTestsStatElem = StatCollector::getGlobalElem(statNCollisionTests);
    if(NCollisionTestsStatElem) { NCollisionTestsStatElem->set(NCollisionTestsStatElem->get()/static_cast<Real32>(NPhysicsStepsStatElem->get())); }
    StatRealElem *NCollisionsStatElem = StatCollector::getGlobalElem(statNCollisions);
    if(NCollisionsStatElem) { NCollisionsStatElem->set(NCollisionsStatElem->get()/static_cast<Real32>(NPhysicsStepsStatElem->get())); }

    
    
    StatTimeElem *TransformUpdateTimeStatElem = StatCollector::getGlobalElem(statTransformUpdateTime);
    //Start the transform update timing statistic
    if(TransformUpdateTimeStatElem) { TransformUpdateTimeStatElem->start(); }
    //update matrices
    updateWorld(getUpdateNode());
    //Start the transform update timing statistic
    if(TransformUpdateTimeStatElem) { TransformUpdateTimeStatElem->stop(); }

    //Stop the physics timing statistic
    if(PhysicsTimeStatElem) { PhysicsTimeStatElem->stop(); }
}
예제 #6
0
파일: test.cpp 프로젝트: daoanhvu/libnmath
void jniJLexerGetSpace() {
	Function *f;
	DParam dp;
	TokenList tokens;
	char outString[256];
	char strbuff[256] = "f(x)=-x^2";
	int i, j, textLen, vertexCount, l=0;
	int tokenNum = 9;
	float bdarr[2] = {-2, 2};
	float epsilon = 0.2;
	ListFData *spaces;
	float temp;
	int k, r;
	
	f = (Function*)malloc(sizeof(Function));
	f->prefix = NULL;
	f->domain = NULL;
	f->criterias = NULL;
	f->str = NULL;
	f->len = 0;
	f->variableNode = NULL;
	f->numVarNode = 0;
	f->valLen = 0;
	
	tokens.loggedSize = tokenNum;
	tokens.list = (Token*)malloc(sizeof(Token) * tokens.loggedSize);

	tokens.list[0].type = 19;
	tokens.list[0].column = 0;
	tokens.list[0].priority = 0;
	tokens.list[0].text[0]='f';
	tokens.list[0].textLength = 1;

	tokens.list[1].type = 11;
	tokens.list[1].column = 1;
	tokens.list[1].priority = 0;
	tokens.list[1].text[0]='(';
	tokens.list[1].textLength = 1;

	tokens.list[2].type = 19;
	tokens.list[2].column = 2;
	tokens.list[2].priority = 0;
	tokens.list[2].text[0]='x';
	tokens.list[2].textLength = 1;

	tokens.list[3].type = 12;
	tokens.list[3].column = 3;
	tokens.list[3].priority = 0;
	tokens.list[3].text[0]=')';
	tokens.list[3].textLength = 1;

	tokens.list[4].type = 10;
	tokens.list[4].column = 4;
	tokens.list[4].priority = 0;
	tokens.list[4].text[0]='=';
	tokens.list[4].textLength = 1;

	tokens.list[5].type = 22;
	tokens.list[5].column = 5;
	tokens.list[5].priority = 4;
	tokens.list[5].text[0]='-';
	tokens.list[5].textLength = 1;

	tokens.list[6].type = 19;
	tokens.list[6].column = 6;
	tokens.list[6].priority = 0;
	tokens.list[6].text[0]='x';
	tokens.list[6].textLength = 1;

	tokens.list[7].type = 25;
	tokens.list[7].column = 7;
	tokens.list[7].priority = 6;
	tokens.list[7].text[0]='^';
	tokens.list[7].textLength = 1;

	tokens.list[8].type = 18;
	tokens.list[8].column = 8;
	tokens.list[8].priority = 0;
	tokens.list[8].text[0]='2';
	tokens.list[8].textLength = 1;

	tokens.size = tokenNum;

	parseFunctionExpression(&tokens, f);
	if( getErrorCode() == NMATH_NO_ERROR ) {
		spaces = getSpaces(f, bdarr, f->valLen * 2, epsilon);
		if(spaces != NULL) {
			for(i=0; i<spaces->size; i++) {
				for(j=0; j<spaces->list[i]->dataSize; j++) {
					printf("%lf ", (spaces->list[i]->data[j]));
				}
			}

			free(spaces->list[0]->data);
			free(spaces->list[0]->rowInfo);
			free(spaces->list[0]);
			free(spaces->list);
			free(spaces);
		}
	}
	
	free(tokens.list);
	releaseFunct(f);
	clearPool();
	free(f);
}
예제 #7
0
파일: test.cpp 프로젝트: daoanhvu/libnmath
int testGetSpaces() {
	int cmd, k, numOfV=0, i, j, vcount;
	float bd[]={-2, 2, -2, 2};
	float eps = 0.2f;
	ListFData *data;
	FILE *file;
	int *indice;
	int l, indiceSize = 0, indiceLoggedSize;
	char strbuff[256] = "f(x)=sin(x)";
	Function *f = NULL;


	//printf("Enter function: ");
	//fflush(stdin);
	//fgets(strbuff, 256, stdin);
	//scanf("%s", &strbuff);

	f = (Function*)malloc(sizeof(Function));
	f->prefix = NULL;
	f->domain = NULL;
	f->criterias = NULL;
	f->str = NULL;
	f->len = 0;
	f->variableNode = NULL;
	f->numVarNode = 0;
	f->valLen = 0;

	printf("Input function: ");
	scanf("%s", &strbuff);
	l = strlen(strbuff);
	parseFunction(strbuff, l, f);
	if(getErrorCode() != NMATH_NO_ERROR) {
		printError(getErrorColumn(), getErrorCode());
		releaseFunct(f);
		free(f);
		return getErrorCode();
	} 

	if( f->valLen==0 ) {
		printf("This expression is not a function due to variables not determined.\n");
	}
	
	printf("The range for each variable is [-2, 2]\n");
	printf("The epsilon = %lf \n", eps);
	printf("Do you want to change these values? Press 1 and Enter to change otherwise you will use the default\n");
	scanf("%d", &cmd);
	if(cmd == 1){
		printf("Enter the range for each variable, use space as delimiter: ");
		fflush(stdin);
		//fgets(strbuff, 256, stdin);
		scanf("%s", &strbuff);
		printf("Enter epsilon: ");
		scanf("%lf", &eps);
		// parseRange(strbuff, strlen(strbuff), bd, &numOfV);
	}
	
	printf("\n");
	data = getSpaces(f, bd, f->valLen * 2, eps);
	if(data != NULL) {
		file = fopen("../data.txt", "w+");
		for(i=0; i<data->size; i++){
			vcount = data->list[i]->dataSize/data->list[i]->dimension;
			printf("Mesh %d, row count: %d number of vertex: %d\n", i, data->list[i]->rowCount, vcount);
			fprintf(file, "number of vertex = %d\n", vcount );
			
			for(j=0; j<vcount; j++){
				fprintf(file, "%lf \t %lf \t %lf\n", data->list[i]->data[j*3], data->list[i]->data[j*3+1], data->list[i]->data[j*3+2]);
			}
			
			indice = buildIndicesForGLLINEs(vcount, data->list[i]->rowInfo, data->list[i]->rowCount, &indiceSize, &indiceLoggedSize);
			if(indice != NULL) {
				cmd = 0;
				for(j=0; j<data->list[i]->rowCount; j++) {
					fprintf(file, "\n ");
					for(k=0; k<data->list[i]->rowInfo[j]; k++)
						fprintf(file, "%d \t ", cmd++);
				}
				
				fprintf(file, "\nIndices length: %d \n", indiceSize);
				k = indiceSize/20;
				for(j=0; j<indiceSize; j++) {
					fprintf(file, "\t %d", indice[j] );
					if(j>0 && (j%k==0))
						fprintf(file, "\n");
				}
				free(indice);
			}
			
			free(data->list[i]->data);
			free(data->list[i]->rowInfo);
			free(data->list[i]);
#ifdef DEBUG
	descNumberOfDynamicObjectBy(3);
#endif
		}
		fclose(file);
		free(data->list);
		free(data);
#ifdef DEBUG
	descNumberOfDynamicObjectBy(2);
#endif
	}

	releaseFunct(f);
	clearPool();
	free(f);

	return 0;
}