Thread: Help please if possible!

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    21

    Unhappy Help please if possible!

    Hi everyone. I'm completely new to C but I'm using it to do some performance testing. One of the items I capture is a VIEWSTATE which is a massively long char. Basically it includes characters such as + and = which I need to find and replace to put the char into the right format to post to a website. Does anyone know a search and replace method which would allow me to do to this or whether there is somesort of encoding method which can make it valid for HTTP? I'm probably asking a ridiculously hard question but i've tried everything i can think of and looked around on the net for a long time and I still can't find any information.

    Many thanks to anyone who responds.

    Godders_2k

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is the new string always the same length, just replacing certain characters, or are will the replacement change the length of the string?

    If you have a fixed length, then you can use strchr() to find characters in the string, and simply replace the string found [and move the starting position to reflect the replacement].

    If you are looking to replace multiple characters, strcspn() is a related function where you give a list [in the form of a char array] to indicate what chars you are looking for, and will find the first of them.

    Note that strchr() returns the position where the character is found (or NULL if nothing is found), and strcspn() returns a size_t index to the char found.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    Basically I have a VIEWSTATE which I have captured and stored in a char. The problem is that I need to encode this into a format which will be understood when it is posted. I am looking to replace the + symbols with %2B and the = symbols with %3D. Therefore in answer to your question it looks like the char length will indeed grow, but would this make any difference if i'm just storing it in char* szViewState where I don't specify its size?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, it makes the difference that you need to have a buffer large enough to hold the expanded data - it is possible to do this in several ways:
    1. Guess the maximum ever length of the string and use a constant size string of that size to begin with.
    2. count the number of "replaced" characters and calculate the extra space, then mallo() or similar a string of the right size [don't forget the space for the end zero byte].
    3. "Guess" how much larger the new string should be [e.g. add 25%, 10%, 50% or whatever seems to be a good value], and use realloc if you get it wrong. This means counting the new string length and stopping to realloc if needs be.

    All have their benefits and drawbacks. #1 is the easiest [but requires your "guess" to be right or it fails completely] and particularly efficient if you load the string into this buffer in the first place, #2 is compute intensive but precise, #3 is the most complicated to track, but reasonably good efficency unless you often run into "realloc" scenario.

    I have written code using #3 in a project that required inserting escapes for certain characters on a serial line transfer protocol. It worked.

    Once we have overcome the "how much bigger does the buffer have to be" problem, we can focus on the replacement. Essentially, the same method for searching applies, but now you have to either copy the string "part by part" by finding location of a "need escape" character, insert the escaped sequence, and then copy the up to the next "need escape". Or you can do "in place shuffle forward", where you create a hole by using a memmove() call to move the data "behind" the place where you need to insert your escape.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    Hi,

    Thanks for your help on this its much appreciated. Is there any way I can extract a character one at a time? e.g. if its 100 characters long what I was thinking I could do is take the first one, compare it to see if its one of the characters I want to swap, if its not then I'll append it to a new string and then move onto the next character. I appreciate that the new string would have to be bigger than the old string to accomodate the changes but I can set this high enough to ensure it fits.

    If you could get back to me on this that would be brilliant. I can see how I do it just don't know how to make it so I can extract a character at a time from a long char.

    Cheers.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I like your idea of "extracting" each char from the original (long) chars, and appending that char onto the other char array. Two arrays (one for the original, and one for the new one you're making), seems simple to use for this. I'd be inclined to size them both statically, and just a good bit more than will ever be needed.

    I do this VERY seldomly, but I'd be quite tempted to set a char pointer to the first char in the original string, and then "walk" that pointer right up thru all the char's, processing as you go.

    The name of the array will give you the base char's address, or you can use a "full" pointer, and just set it equal to that base char's address, to start.

    Before I did anything else though, I'd elevate the long chars to a string status by adding the end of string char '\0', to the end of your original chars.

    Think that over, and see what you think. If you could post the relevant portions of code for this, and tell us what the problems are with it, that would help.

    The more specific your questions are, with your code posted, the more helpful we can answer.

Popular pages Recent additions subscribe to a feed