Page 1 of 1

More efficient string copying/comparing methods.

PostPosted: Sat Aug 30, 2008 6:11 am
by Sgt. Sparky
Hey everyone, I know it has been a long time since I have posted anything on the forums, but I'm going to make up for that with this example:

I have created a string comparing method that copies part of a string ( char array) onto another specified string.
It works the same as the strncpy() function, but its parameters are a bit different.
The strncpy function looks something like this:
Code: Select all
void strncpy ( char * dest, char * source, int amount )
{
    for ( int i = 0; i < ( amount < strlen ( source ) ) ? amount : strlen ( source ); i++ )
    {
        dest [ i ] = source [ i ];
    }
}


My function allows you select the range of characters.

Let's say that you have a file, and you want to get it's extension, here is what you would do.
Code: Select all
char Ext [ 3 ];
CopyPart ( Ext, FileName, strlen ( FileName ) - 3, -1 ); // Setting the last parameter to -1 means that it will use the length of your source.

If your file name was "MyMapFileName.map" it would return "map"

I also made a function that tells your if a certain string exists inside another.

Let's say that you were looking for the word "exit" inside a text box, and you wanted to know weather or not the game should exit if it finds this text. You could use the fallowing code with my function to detect weather or not you should exit the game.

Code: Select all
 if ( FindInText ( "exit", MyTextActor.text ) && !FoundInText ( "not", MyTextActor.text ) ) ExitGame();


These sort of functions can be used in many different ways.

Here is the source for my functions, if you have any questions, just e-mail me. :)

Stick the fallowing code in your global code box, then use it whenever you need it.

If there are any errors, please e-mail me.
I didn't have time to test this code in GE after I made it in C++.
I don't know if I converted it all properly. Please let me know of any problems.
Note: I won't be able to reply until 9/2/2008 since I will be on vacation.

Code: Select all
char * CopyPart ( char * Dest, char * Source, int Start, int End )
{
   if ( ( Start >= 0 && End <= strlen ( Source ) && End > Start ) || ( End == -1 && Start < strlen ( Source ) && Start >= 0 ) )
   {
      if ( End == -1 ) End = strlen ( Source );
      char * Return = malloc ( sizeof ( char ) * ( End - Start ) + 1 );
      for ( int i = 0; i < End - Start; i++ )
      {
         Return [ i ] = 0;
      }
      for ( int i = Start; i < End; i++ )
      {
         Return [ i - Start ] = Source [ i ];
      }
      strcpy ( Dest, Return );
      return Return;
   }
   return "(null)";
}

bool FindInText ( char * ToFind, char * SearchText )
{
   int len = strlen ( ToFind );
   if ( len <= strlen ( SearchText ) )
      for ( int i = 0; i <= strlen ( SearchText ) - len; i++)
      {
         if ( SearchText [ i ] == ToFind [ 0 ] && SearchText [ i + len - 1 ] == ToFind [ len - 1 ] )
         {
            char * Temp =  malloc ( sizeof ( char ) * len + 1 );
            for ( int i2 = 0; i2 < len; i2++ ) Temp [ i2 ] = 0;
            CopyPart ( Temp, SearchText, i, i + len );
            if ( strncmp ( Temp, ToFind, len ) == 0 )
            {
               return true;
               break;
            }
         }
      }
   return false;
}


If any of you have scripting questions of any kind, please ask and I will do my best to answer you.

Re: More efficient string copying/comparing methods.

PostPosted: Tue Sep 16, 2008 2:40 pm
by Bee-Ant
Eh, where should I put this function??? :mrgreen:
Any actor>Draw Actor ???
or in global script??? :roll: :mrgreen:

Re: More efficient string copying/comparing methods.

PostPosted: Tue Sep 16, 2008 2:56 pm
by Sgt. Sparky
Bee-Ant wrote:Eh, where should I put this function??? :mrgreen:
Any actor>Draw Actor ???
or in global script??? :roll: :mrgreen:


It must be in global script. :)