• C

String Manipulation

Hi Experts,
I have a problem with string manipulation in C.  I am using Loadrunner, which is based on C

Briefly ...I am trying to
-Capture a URL location and save it into a parameter. (DONE)
- Now use this saved URL location (saved parameter) in a new request to the server.

PROBLEM:

-I am able to achieve all the above. However, there is one problem.
- the URL location is something like this: http://print/pdflocation/id:MSGH1234&sdID=887778/

-This is causing a problem because of the '&' in the above string. As you know, this is a special character and the new request fails over because of this XML phaser. Because '&' needs to be escaped by '&'

-This means I must manipulate the string captured before using it as input to send the new request.


-- There is no specific function to 'manipulate strings' in loadrunner.

--My problem is because the next call to the WebService uses this string to palce a request.
and as soon as it comes to the '&' it fails over...because it is not escaped.

--The only solution I see to this is:

--- is to capture this string and delete everything after '&' character and then save it in a new parameter before using it to send the next request.

OR

--find the '&' occurance in the string and replace it with '&' and save it in a new parameter.

Could you please give me your guidance on how to acheive this?
I am able to save the parameter and use it in the new request. However, it is the string maipulation I am having trouble with.
Thanks in advance.
TourcherTouchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrjoltcolaCommented:
If you were using C++ it would be simple, use the std::string.replace function

But in standard C, I don't think there is a string replace function, but you could surely write an escape function for this special case. A quick and dirty sample is below. It isn't the safest code but demonstrates the concept.

#include <stdio.h>
 
void escape(const char *, char *);
 
int main() {
  const char * url = "http://print/pdflocation/id:MSGH1234&sdID=887778/";
  char esc[256];
  escape(url, esc);
  printf("%s\n", esc);
}
 
void escape(const char * s, char * d) {
  while(*s) {
    *d = *s;
    d++;
    if(*s == '&') {
       strcpy(d, "amp;");
       d += 4;
    }
    s++;
  }
  *d = 0;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TourcherTouchAuthor Commented:
Hi Mrjoltcola,

Thank you for the very quick response.
the url I need to save is dynamic and changes every time.
I tried the solution you gave me and it does not seem to work.

I think a better solution is to find the '&' in the string and replace it with '&amp;'
Not sure how to do it though...

I'll keep you posted.

Thanks again...

0
mrjoltcolaCommented:
>>I think a better solution is to find the '&' in the string and replace it with '&amp;'

That is exactly what the code does. Change the print line and you will see.

The function is a demonstration of the approach. I am not sure how you tested it or used it. I did test it and it works. It is a complete program. Did you compile and run it?

  printf("before: %s\nafter: %s", url, esc);

Open in new window

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

mrjoltcolaCommented:
>>the url I need to save is dynamic and changes every time.

So call the replacement function everytime.
0
TourcherTouchAuthor Commented:
Mrjoltcola,

Thank you, I am now trying to get aournd some syntax errors. This is code is throwing some exceptions...perhaps because I am using it for Loadrunner. And it is not recognising the
#include <stdio.h>...

will keep you posted.
0
mrjoltcolaCommented:
>>#include <stdio.h>

Only needed for the printf(), you can remove it as well as the printf and the main() function.

The main() was only to demonstrate how to use the function, all you need is the escape() function itself.
0
Subrat (C++ windows/Linux)Software EngineerCommented:
using strcpy() needs to include "string.h". So I think it'd be better if we'r replacing it with our own my_strcpy() like below.

char* my_strcpy(char* d, const char* s) {
      char* temp = d;
      while(*d++ = *s++); // semicolon is important...
      return temp;
}

Instead of calling strcpy() call my_strcpy() inside escape() function.

Hope may help u.
0
mrjoltcolaCommented:
I assumed strcpy() was available for his "embedded" environment, but you have a good point, for such a simple function there is no point in external dependency.

Here is a version with no library calls.

void escape(const char * s, char * d) {
  while(*s) {
    *d = *s;
    d++;
    if(*s == '&') {
       *d++ = 'a';
       *d++ = 'm';
       *d++ = 'p';
       *d++ = ';';
    }
    s++;
  }
  *d = 0;

Open in new window

0
TourcherTouchAuthor Commented:
Hi All,
Thank you for your support to try to resolve this issue. Mrjoltcola, I've used your code as a concept and improvised it and got the solution.
Here is the solution and it works great!

int offset;

      char * position;
      char * location = "http://.xyzcompany.extranet:81/file-app/stream?id=TEwzOTk5MVg%3D&sd=28.05.09/postage/";
      char * search_str = "&";
      char * escape_str = "&amp;";
      char modified_location[1024];

       int iniLength = strlen(location);
       int finalLength = iniLength+4;
       int extraLength = iniLength - offset;
       int escapeLength = strlen(escape_str);

      position = (char *)strstr(location, search_str);

      // strstr has returned the address. Now calculate * the offset from the beginning of str
       offset = (int)(position - location + 1);

       strncpy(modified_location,location,offset-1); //Copying in modified_location all characters until the search string
       strncat(modified_location,escape_str,escapeLength); //Concatenating modified_location with the escape_str
       strncat(modified_location,&location[offset],iniLength-offset); //Concatenating modified_location with the rest of the location string
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.