Strange String replacement...

This is a discussion on Strange String replacement... within the C Programming forums, part of the General Programming Boards category; hmmm... ok, so is not possible to copy this to another string like I thought? I can print in my ...

  1. #31
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48

    Red face

    hmmm... ok, so is not possible to copy this to another string like I thought?

    I can print in my exemple the var SubString and got the exact substring that I want to work, but I can't copy that to a another string? are you sure?

  2. #32
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    you can copy, strtok does not do it for you. so you should do it by yourself... and be aware that the original buffer you passed to the strtok was changed
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #33
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Are you sure you're going about this the right way?

    I mean, you're not that experienced in C (by your admission), and string handling in C quite frankly sucks, what with all the effort you have to put into memory management and making sure the nul terminator is always present and correct.

    > "origin" "-256 248 16"
    How about finding where this gets converted to numeric form, say where the code eventually does
    origin.z = 16;

    Then all you need to do is one simple line of
    origin.z += 15;
    and the job's done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #34
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    is not a bad idea, I'll try to look more into it...

    my first solution was only changing a line, and should work if I have the MOD source... I'll see if have something like that in the server/client source, not on the MOD one...

    if I find that stuff will be really easy to fix the siamese twins problem.

    thanks!

  5. #35
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    don't have such thing unfortunately... what have was in the DLL part which I can change, but will be ignored when the MOD is loaded...

    but I found other interesting stuff in code, what could lead me to another solution...

  6. #36
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48

    Lightbulb Done! need only few convertions...

    well, I quit to try find good functions, and done everything by hand... so please take a look at this code:

    Code:
    static void CM_LoadEntityString (const byte *data, const lump_t *l){
    
        char   *game = Cvar_VariableString("fs_game");
    	int		i, j, Tam, TamNew;
    	int		PA, PB, PC, PD, PL, L1, Num, Espaco, Aspas;
    	char	*p, *ori, *HStr, *NewStr, *FinalStr;
    	char	*PartA, *PartB, *PartC, *PartD;
    
    	cm_numEntityChars = l->fileLen;
    	if (cm_numEntityChars < 1)
    		return;
    	if (cm_numEntityChars > MAX_MAP_ENTSTRING)
    		Com_Error(ERR_DROP, "CM_LoadMap: map '&#37;s' has too large entity lump", cm_map);
    
    	cm_entityString = Hunk_Alloc(cm_numEntityChars + 1);
    	memcpy(cm_entityString, data + l->fileOfs, cm_numEntityChars);
    
    	if (!Q_stricmp(game, "arena")){
    		HStr = cm_entityString; // copy the original string to mine
    		Tam = strlen(HStr);
          // do string manipulation over cm_entityString
    		i=0;
    		j=0;
    	   while (j<Tam){
    		   while (HStr[j] != '}'){ // copy to NewStr each Class
    			   NewStr[i] = HStr[j];
    			   i++;j++;
    		   }
    		   NewStr[i] = HStr[j]; //add '}' to the end of the class
    		   p = strstr(NewStr, "info_player"); //see if is a spawn class
    		   if (!p){
    			   FinalStr = strcat( FinalStr, NewStr ); //if not, save and go to the next class
    		   }else{
    			   //if yes, start the hard work
                   ori = strstr(NewStr, "origin"); //find the origin position
    			   // than add +8 (origin" ") to that position, which will reach the numbers of the axis
    			   L1=ori+8; //------------------------------------------------- NEED TO CONVERT????!!!
    			   // find PartA from 0 to L1
    			   for (PA=0;PA==L1;PA++) //seta parte 1/4
    			   {
    					PartA[PA]=NewStr[PA];
    			   }
    			   PL=PA; //creat a new var to control the place in NewStr
    			   Espaco=0; //set to have zero spaces count
    			   // find PartB from start of numbers until 2nd space
    			   for (PB=0;Espaco!=2;PB++) //set part 2/4
    			   {
    					PL++;
    					PartB[PB]=NewStr[PL];
    					if (PartB[PB]==" ")
    						Espaco++;
    			   }
    			   Aspas=0; //set to plics to zero count
    			   // find PartC which is the Number, until reach next "
    			   for (PC=0;Aspas==1;PC++) //set part 3/4 = Num
    			   {
    				   PL++;
    					if (NewStr[PL]=='"')
    						Aspas = 1;
    					else
    						PartC[PC]=NewStr[PL];
    			   }
    			   TamNew = strlen(NewStr); //find the size of NewStr
    			   // find PartD from " after number until the end of class
    			   for (PD=0;PL==TamNew;PD++) //set part 4/4
    			   {
    				   PartD[PD]=NewStr[PL];
    				   PL++;
    			   }
    			   // Convert to Number
    			   Num=PartC; //------------------------------------------------- HOW TO CONVERT????!!!
    			   Num += 15; //add 15
    			   NewStr = strcat( PartA, PartB ); // make all 4 parts into one
    			   NewStr = strcat( NewStr, Num ); //------------------------------------------------- NEED TO CONVERT????!!!
    			   NewStr = strcat( NewStr, PartD );
    			   FinalStr = strcat( FinalStr, NewStr ); // add all NewStr to FinalStr
    		   }
    		i=0; //NewStr count to zero
    		j++;
    		NewStr=""; //erase NewStr
    	   }
    	cm_entityString = FinalStr; //replace the original by the new altered one
    	}
    }
    ok, I know need few funcions of convertions or some other thing must be done in the wrong way... please fix everything that you find not good.

    thanks a lot!
    Last edited by hajas; 06-14-2007 at 08:23 AM.

  7. #37
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48

    Question

    ok, here with atoi convertion....

    Code:
    static void CM_LoadEntityString (const byte *data, const lump_t *l){
    
        char   *game = Cvar_VariableString("fs_game");
    	int		i, j, Tam, TamNew;
    	int		PA, PB, PC, PD, PL, L1, Num, Espaco, Aspas;
    	char	*p, *ori, *HStr, *NewStr, *FinalStr;
    	char	*PartA, *PartB, *PartC, *PartD;
    
    	cm_numEntityChars = l->fileLen;
    	if (cm_numEntityChars < 1)
    		return;
    	if (cm_numEntityChars > MAX_MAP_ENTSTRING)
    		Com_Error(ERR_DROP, "CM_LoadMap: map '&#37;s' has too large entity lump", cm_map);
    
    	cm_entityString = Hunk_Alloc(cm_numEntityChars + 1);
    	memcpy(cm_entityString, data + l->fileOfs, cm_numEntityChars);
    
    	// do string manipulation over cm_entityString if game = arena
    	if (!Q_stricmp(game, "arena")){
    		HStr = cm_entityString; // copy the original string to mine
    		Tam = strlen(HStr);
    		i=0;
    		j=0;
    	   while (j<Tam){
    		   while (HStr[j] != '}'){ // copy to NewStr each Class
    			   NewStr[i] = HStr[j];
    			   i++;j++;
    		   }
    		   NewStr[i] = HStr[j]; //add '}' to the end of the class
    		   p = strstr(NewStr, "info_player"); //see if is a spawn class
    		   if (!p){
    			   FinalStr = strcat( FinalStr, NewStr ); //if not, save and go to the next class
    		   }else{
    			   //if yes, start the hard work
                   ori = strstr(NewStr, "origin"); //find the origin position
    			   L1=atoi(ori); // converted to number
    			   L1 += 8; // than add +8 (origin" ") to that position, which will reach the numbers of the axis
    			   // find PartA from 0 to L1
    			   for (PA=0;PA==L1;PA++) //seta parte 1/4
    			   {
    					PartA[PA]=NewStr[PA];
    			   }
    			   PL=PA; //creat a new var to control the place in NewStr
    			   Espaco=0; //set to have zero spaces count
    			   // find PartB from start of numbers until 2nd space
    			   for (PB=0;Espaco!=2;PB++) //set part 2/4
    			   {
    					PL++;
    					PartB[PB]=NewStr[PL];
    					if (PartB[PB]==" ")
    						Espaco++;
    			   }
    			   Aspas=0; //set to plics to zero count
    			   // find PartC which is the Number, until reach next "
    			   for (PC=0;Aspas==1;PC++) //set part 3/4 = Num
    			   {
    				   PL++;
    					if (NewStr[PL]=='"')
    						Aspas = 1;
    					else
    						PartC[PC]=NewStr[PL];
    			   }
    			   TamNew = strlen(NewStr); //find the size of NewStr
    			   // find PartD from " after number until the end of class
    			   for (PD=0;PL==TamNew;PD++) //set part 4/4
    			   {
    				   PartD[PD]=NewStr[PL];
    				   PL++;
    			   }
    			   Num=atoi(PartC); // Convert to Number
    			   Num += 15; //add 15
    			   NewStr = strcat( PartA, PartB ); // make all 4 parts into one
    			   NewStr = strcat( NewStr, Num ); //------------------------------------------------- NEED TO CONVERT????!!!
    			   NewStr = strcat( NewStr, PartD );
    			   FinalStr = strcat( FinalStr, NewStr ); // add all NewStr to FinalStr
    		   }
    		i=0; //NewStr count to zero
    		j++;
    		NewStr=""; //erase NewStr
    	   }
    	cm_entityString = FinalStr; //replace the original by the new altered one
    	}
    }
    about the rest? is everything ok?

  8. #38
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48

    Question

    My main doubts...

    HStr = cm_entityString;
    is this the right way to copy a string? then HStr will have the same value of cm_entityString??

    Tam = strlen(HStr);
    is this the right way to get the lenght of a string? Tam is a number right?

    ori = strstr(NewStr, "origin");
    is this the right way to receive the position of the NewStr that have origin??? in this case the position of "o"
    ori is a number or a pointer to the number that I want? or how can I get that number?

    NewStr="";
    is this the right way to empty a string?

    any other stuff made wrong in my code?

    thanks!

  9. #39
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21