distributedlife

passionate about everything

Vanilla C with Sprinkles – Find and Replace find and replace for Vanilla C and HP Service Test Published by Ryan Boucher @ 11:55 pm

This isn’t the greatest implementation but it works for me. I use this function to change configuration files before I run a service test.

The first parameter is a char* pointer that is your source text. The next two are your find and replace char* pointers.

The result is returned to the caller as a pointer but you won’t have to worry about the memory as it is owned by HP Service Test.


const char* FindAndReplace (const char* Source, const char* const Find,const char* const Replace)
{
    //start by pointing to the start of the string
    long LengthOfSource = strlen (Source) ;
    long LengthOfStringToReplace = strlen (Find) ;

    char* LastFoundStart = (char*) Source ;
    char* NextFoundStart = 0 ;
    char* Buffer = 0 ;

    lr_set (Replace, "__FindAndReplace_Replace") ;
    lr_set ("", "__FindAndReplace_New") ;

The code looks for a substring and and replaces when found. It then jumps past that string before trying again. This is to avoid a scenario where our replaced string contains out find string and we get into a loop until memory expires.


    while (NextFoundStart = (char*) strstr (LastFoundStart, Find))
    {
        //Allocate a buffer to copy our string into. We want from LastFoundStart to NextFoundStart
        Buffer = (char*) malloc ( (NextFoundStart - LastFoundStart + 1) * sizeof (char)) ;

        //copy from LastFoundStart to NextFoundStart in to our new paramtere
        memcpy (Buffer, Source, (NextFoundStart - LastFoundStart) * sizeof (char)) ;

        //null terminate the string
        Buffer[(NextFoundStart - LastFoundStart) * sizeof (char)] = '\0' ;

        //Move our LastFoundStart to the end of the replaced string
        LastFoundStart = NextFoundStart + LengthOfStringToReplace ;

        //Copy our buffer into our new parameter
        lr_set (Buffer, "__FindAndReplace_Buffer") ;

        lr_set (lr_get ("{__FindAndReplace_New}{__FindAndReplace_Buffer}{__FindAndReplace_Replace}"), "__FindAndReplace_New") ;

        //tidy up for next round
        free (Buffer) ;
        Buffer = 0 ;
        lr_set ("", "__FindAndReplace_Buffer") ;
    }

Somewhat repeated code… because I’m somewhat lazy.



    NextFoundStart = ( (char*) Source) + LengthOfSource ;
    if (LastFoundStart != NextFoundStart)
    {
        //we still need to copy data
        //once we have found all our matches we need to copy the end of the string from the end of the last
        // find to the end of the string... assuming the last match was not at the end of the string
        //Allocate the final buffer
        Buffer = (char*) malloc ( (NextFoundStart - LastFoundStart + 1) * sizeof (char)) ;

        //copy from LastFoundStart to NextFoundStart in to our new paramtere
        memcpy (Buffer, LastFoundStart, (NextFoundStart - LastFoundStart) * sizeof (char)) ;

        //null terminate the string
        Buffer[(NextFoundStart - LastFoundStart) * sizeof (char)] = '\0' ;

        //Copy our buffer into our new parameter
        lr_set (Buffer, "__FindAndReplace_Buffer") ;

        lr_set (lr_get ("{__FindAndReplace_New}{__FindAndReplace_Buffer}"), "__FindAndReplace_New") ;

        //tidy up for next round
        free (Buffer) ;
        Buffer = 0 ;
        lr_set ("", "__FindAndReplace_Buffer") ;
    }

    return lr_get ("{__FindAndReplace_New}") ;
}
My Mug Ryan Boucher is a Software Inquisitor and is passionate about it. You can find a whole raft of articles and anecdotes about software testing and other topics he gets excited about.
Tags , , , , , , , , , ,