vugen :: search and replace characters in a string

What happens when you have to submit special charaters, or reserverd characters in vugen? It can be quite a pain. Yes you can always do a web_convert_param(), but sometimes it doesnt do what you need. I’ve noticed it converts a space to a + not a %20….This has really confused me when submitting a webform…
The only other way to get around it is to write a function that searches your string, and replaces it.

My colleague Brady Wood wrote this, I’m just stealing it and claiming it as my own!….Thanks Woody.

char *replace_str(char *str, const char *orig, const char *rep)
{
    size_t buf_index = 0;
    char buffer[10000];
 
    if (!(char *)strstr(str, orig))  // Is 'orig' even in 'str'?
    {
        return str;
    }
 
    buffer[0] = 0;
    for(;;)
    {        
        char *p;
 
        if (!(p = (char *)strstr(str, orig)))  
        {
           strcpy(buffer + buf_index, str);
           return buffer;
        }
        strncpy(buffer + buf_index, str, p - str);
        strcpy(buffer + buf_index + (p - str), rep);
        buf_index += (p-str) + strlen(rep);
        str = p + strlen(orig);
    }
 
	lr_output_message("$$$$ str =%s ", str);
	lr_output_message("$$$$ orig =%s ", orig);
	lr_output_message("$$$$ rep =%s ", rep);
	lr_output_message("$$$$ buffer =%s ", buffer);
 
 
    return buffer;
}

Use it like this…

_otpUrlPtr = replace_str(lr_eval_string("{urlRedirect_3}"), "&", "&");

and you’re done.

Enjoy!

One comment

  • Rolando
    May 8, 2015 - 6:06 pm | Permalink

    Thanks for posting this function. It was very useful.

  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>