Problem with a (hidden) endless LOOP

Posted By: pararealist

Problem with a (hidden) endless LOOP - 04/10/08 14:06

Hello all,

I am having a problem with an endless loop that i only discovered
when i inserted a printf into the loop.
I have packed it into a function, all you have to do is create a little
program and call fDoTest() from main.

The function should remove the path leaving the filename
then remove the .wmb extension from filename and add .gsx
extension to the filename.

 Code:
// filled for testing - usually comes from fileselect dialog
char* filePathName = "D:\\GSTUDIO PROJECTS\\A7_PROJECTS\\PROJECTTEST\\atestfile.wmb";   // 
//
STRING* aCurrentPath = " ";
aCurrentPath = str_create("aCurrentPath");
STRING* camPosName_str = " ";  
camPosName_str = str_create("camPosName_str");

///////////////////////////////////////////////////////////////
function fDoTest()
{
	
draw_text("in fDoTest()",500,200,_vec(0,255,0 ));
	
	int i = 0, j=0;
	int count=0, len=0;
	int pcount = 0;
	// THIS LOOP seems to never end ????????????
	// is strange - only found out whilst debugging
	// yet app works (at least up to now) 
	// but is definately not right
	// if i insert a printf in the while loop then i notice it	
	while (str_stri(_str(filePathName),"*") == 0)
	{	            
		// search for "\" until no more
		len = str_len(_str(filePathName));
		count = str_stri(_str(filePathName),"\\");
		pcount = len-count;					
		// clip name from begin to "\"
		str_clip(_str(filePathName),count);
 		// trunc name from end by (pcount - to "\") dont work ????
		str_trunc(_str(aCurrentPath),pcount );				
	        // store levelname
		str_cpy(camPosName_str,_str(filePathName));	
		// split levelname. from ext (wmb))
		len = str_len(camPosName_str); 
		count = str_stri(camPosName_str,"."); // search for "."
		count = (len - count);
		str_trunc(camPosName_str,count);						
		//  
		// add file ext (gsx) )
		str_cat(camPosName_str,"gsx");	
		//
		//
// UN-COMMENT next line to see printf in endless loop
// and i have to end printf in task manager
// yet COMMENTED runs ok
//printf("camPosName_str= %s",_chr(camPosName_str));		
		//
      wait(1);
   }
   // DOES NOT seem to get to here
   printf("aCurrentPath= %s",_chr(aCurrentPath));   
   printf("camPosName_str= %s",_chr(camPosName_str));
   
   return;
} 


Thanks for any help. I just cant see (at this time) the reason.
Posted By: Excessus

Re: Problem with a (hidden) endless LOOP - 04/10/08 15:28

everytime you use _str, it creates a new temporary string. The variable filePathName never changes, so the condition will always stay true if it was at the beginning of the loop.

Also, why is there a wait(1) in that loop?
Posted By: pararealist

Re: Problem with a (hidden) endless LOOP - 04/10/08 15:45

Hi,

while
{

wait(1);
}
you need wait(1) in while loop i thought.
//
so i would need to use a do while loop with the condition at the end?
or how else can seperate the filename from the full pathname
and then remove the extension to add another (diferent) extension?
Posted By: pararealist

Re: Problem with a (hidden) endless LOOP - 04/10/08 15:55

forgot to say i add
str_cpy(aCurrentPath,filePathName);
before loop.

in loop i use the pcount var to
get the length.
but now i see that i should have used?
// store levelname
str_cpy(camPosName_str,_str(aCurrentPath));
instead of
// store levelname
str_cpy(camPosName_str,_str(aCurrentPath));

will try this again.
Posted By: pararealist

Re: Problem with a (hidden) endless LOOP - 04/10/08 15:56

IGNORE LAST

forgot to say i add
str_cpy(aCurrentPath,filePathName);
before loop.

in loop i use the pcount var to
get the length.
but now i see that i should have used?
// store levelname
str_cpy(camPosName_str,_str(aCurrentPath));
instead of
// store levelname
str_cpy(camPosName_str,_str(filePathName));

will try this again.
Posted By: pararealist

SOLVED: Problem with a (hidden) endless LOOP - 04/10/08 20:56

SOLVED
Decided to use a for LOOP.

 Code:
 
function fDoTest()
{	
	int i = 0;
	int count=0, len=0;
	int pcount = 0;	
	//
	str_cpy(aCurrentPath,filePathName);
	//	
	// get length of filePathName
	len = str_len(_str(filePathName));
	for (i = 0; i < len; i++) 
	{
		// search for "\" until no more
		count = str_stri(_str(filePathName),"\\");
		pcount = len-count;	
		// clip name from begin to "\"
		str_clip(_str(filePathName),count);				
	   // store levelname
		str_cpy(camPosName_str,_str(filePathName));	
		// split name. from ext (wmb))
		len = str_len(camPosName_str);
		// search for "." 
		count = str_stri(camPosName_str,"."); 
		count = (len - count);
		// truncate it
		str_trunc(camPosName_str,count);						
		//  
		// add file ext (gsx) )to get campos filename for this level
		str_cat(camPosName_str,"gsx");							
	}
	//   
   printf("camPosName_str= %s",_chr(camPosName_str));
   return;  
}

© 2024 lite-C Forums