Thread: Strange String replacement...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have to #include <string> and refer to it as std::string or use a using declaration or directive.

  2. #17
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    could anyone convert my pseudocode to C++? thanks a lot...

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yeah, .c files usually mean it's a C program.
    Rename it to be a .cpp (or .cc) file, then see what happens.

    Perhaps you need to clarify which language you want to program this in, since you posted on the C++ board, and the first comment you said was 'C'.
    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.

  4. #19
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    I tryed... seams that I can't inclued that... if I do pop thousands of errors.

    I'm changing a game, so seams that string is not used, and can't be used.

  5. #20
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    I think is C++... is Quake 2 Engine.

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

    Lightbulb Well, let me explain it better!

    I'm trying to fix a bug in Quake 2, with the Quake 2 Engine.

    if is really in C and not C++, please move this topic to the right place. thanks!

    well, I know the cause of this bug and the solution, and I could fix with 2 easy ways, but I can't do it in this MOD because I don't have the MOD source where the exact line can't be changed, so override my fix in the original code (which I have the source) when loaded.

    So I need to change in the harder way, changing the positions of the spawnpoints to a little higher when load the maps, and in this funcion below is where the entity list of the map is loaded:

    Code:
    /*
     =================
     CM_LoadEntityString
     =================
    */
    static void CM_LoadEntityString (const byte *data, const lump_t *l){
    
       cm_numEntityChars = l->fileLen;
       if (cm_numEntityChars < 1)
          return;
       if (cm_numEntityChars > MAX_MAP_ENTSTRING)
          Com_Error(ERR_DROP, "CM_LoadMap: map '%s' has too large entity lump", cm_map);
    
       cm_entityString = Hunk_Alloc(cm_numEntityChars + 1);
       memcpy(cm_entityString, data + l->fileOfs, cm_numEntityChars);
    }
    than I need this:

    Code:
    /*
     =================
     CM_LoadEntityString
     =================
    */
    static void CM_LoadEntityString (const byte *data, const lump_t *l){
    
       char   *game = Cvar_VariableString("fs_game"); 
    
       cm_numEntityChars = l->fileLen;
       if (cm_numEntityChars < 1)
          return;
       if (cm_numEntityChars > MAX_MAP_ENTSTRING)
          Com_Error(ERR_DROP, "CM_LoadMap: map '%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")){
          // do string manipulation over cm_entityString
       }
    }
    so will only do the change when the game is "arena" (which means the MOD arena), which is compiling and working...

    now I need to make that pseudo-code that I created to make this work.

    Code:
    set working = 1;
    while working == 1 {
       if "{" not found{
          set working = 0;
       else{
           subs = GetSubstring(String, first {, first });
          Str1 = GetSubstring (String[0], subs[0]);
          Str2 = GetSubstring (subs[size],String[size]);
          if found (info_player_deathmatch || info_player_start) in subs {
               find "origin" location;
             posA = find 3rd space after origin location;
             posB = find next " after posA;
             Subs1 = GetSubstring (subs[0],posA);
             Subs2 = GetSubstring (posB,subs[size]);
             num = GetSubstring (posA, posB);
             ToNumber(num) += 15;
               subs = Subs1 + num + Subs2;
             ReplaceAll (subs, {, [);
             ReplaceAll (subs, }, ]);
          }
       }
    }
    ReplaceAll (String, [, {);
    ReplaceAll (String, ], });
    than I started:

    Code:
    /*
     =================
     CM_LoadEntityString
     =================
    */
    static void CM_LoadEntityString (const byte *data, const lump_t *l){
    
        char   *game = Cvar_VariableString("fs_game");
    	int		working = 1;
    
    	cm_numEntityChars = l->fileLen;
    	if (cm_numEntityChars < 1)
    		return;
    	if (cm_numEntityChars > MAX_MAP_ENTSTRING)
    		Com_Error(ERR_DROP, "CM_LoadMap: map '%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")){
          // do string manipulation over cm_entityString
    	   while (working == 1){
    		   if (cm_entityString.find_first_of("{"){
    				working = 0;
    		   }else{
    		      // other stuff
    		   }
    	   }
    	}
    }
    than I'm receiving this error:

    Code:
    qcommon\cmodel.c(508) : error C2224: left of '.find_first_of' must have struct/union type
    qcommon\cmodel.c(508) : error C2143: syntax error : missing ')' before '{'
    more info about this problem at this link

    that's all! anyone could help with that?

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

    more info

    here all functions available on Q2E

  8. #23
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    ok, I'm here now...

    Code:
    static void CM_LoadEntityString (const byte *data, const lump_t *l){
    
        char   *game = Cvar_VariableString("fs_game");
    	int		working = 1;
    	char		*p;
    
    	cm_numEntityChars = l->fileLen;
    	if (cm_numEntityChars < 1)
    		return;
    	if (cm_numEntityChars > MAX_MAP_ENTSTRING)
    		Com_Error(ERR_DROP, "CM_LoadMap: map '%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")){
          // do string manipulation over cm_entityString
    	   while (working == 1){
    		   p = strchr(cm_entityString, '{');
    		   if (!p){
    				working = 0;
    		   }else{
    		   
    		   
    		   }
    	   }
    	}
    }
    seams that I need functions at string.h, I'm trying at google since I don't have the Visual Studio .NET documentation....

  9. #24
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    so it's C, sorry... please can you move to C programming? thanks!

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Done.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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

    Question

    exist any function that I sent a string, point A and point B, then returns a substring between these points?

    I can't find!

    thanks!

  12. #27
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Heres one I just knocked up. It may not be the best way to do this, but it seems to work:
    Code:
    #include <stdio.h>
    
    #define str_size 100
    
    void SubStr(char *str, char *str_bit, int start, int end)
    {
        --start;
        for(int i=start; i<end; i++)
            str_bit[i-start]=str[i]; 
        
        str_bit[end-start]='\0';
    }
    
    int main()
    {
        char quit='x';
        char str[str_size];
        char str_bit[str_size];
        int start=3, end=8;
        
        fgets(str, str_size, stdin);
         
        SubStr(&str[0], &str_bit[0], start, end); 
        
        printf("%s", str_bit);
        while((quit=getchar())!='\n');
    }   
    
    /* Test data: 1234567890
       Output: 345678 */

  13. #28
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    strncpy can help
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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

    Question

    I was thinking in do something like this with strtok inside the loop:

    Code:
    SubString = strtok(String,'}');
    correct me if I'm wrong, with that I'll have everything in the String until the first } char copied to that SubString variable.

    than I can work with that substring, and create another variable that will save the new string, bla bla bla....

    and when this line be executed again will copy from the next char after last char copied first, until the next }, and goes on...

    so in each passage of the loop will copy each entity to that SubString... am I right?

    remember, the string will always be like something like this:

    Code:
    {"classname" "worldspawn"}{"origin" "-256 248 16""classname" "info_player_deathmatch"}{"classname" "info_player_deathmatch""origin" "248 256 16"}{"origin" "248 -256 16""classname" "info_player_deathmatch"}{"classname" "info_player_deathmatch""origin" "-264 -256 16"}{"origin" "-176 -200 24""classname" "weapon_rocketlauncher"}{"classname" "weapon_rocketlauncher""origin" "-184 192 24"}{"origin" "168 184 24""classname" "weapon_rocketlauncher"}{"origin" "-184 -264 24""classname" "ammo_rockets"}{"classname" "ammo_rockets""origin" "192 -256 24"}{"origin" "184 128 24""classname" "ammo_rockets"}{"classname" "ammo_rockets""origin" "-184 128 24"}{"origin" "216 -192 24""classname" "weapon_rocketlauncher"}{"light" "400""origin" "-8 0 208""classname" "light"}
    thanks again!

  15. #30
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    strtok does not copy strings.
    It just puts the null char intead of the delimiter found and returns pointer to the token. This pointer points to some location in the original buffer - not to the copy of the substring
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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, 02: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, 11:06 PM