replace string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sumit

    replace string

    hi,
    I have a url which is a char *

    char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";

    when i send this URL to http server it was like -

    "/url=/home/sumit/pacakges/myxml.xml"

    but when it get by http server it convert '/' to '%2F' i have to
    reconvert it to '/'
    i did so many string operation but i did not get real string ,
    still i am trying. can anyone please give me idea how to replace '%2F' to
    '/'
    Thanks




  • Eric Sosman

    #2
    Re: replace string

    Sumit wrote:
    hi,
    I have a url which is a char *
    >
    char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
    >
    when i send this URL to http server it was like -
    >
    "/url=/home/sumit/pacakges/myxml.xml"
    >
    but when it get by http server it convert '/' to '%2F' i have to
    reconvert it to '/'
    i did so many string operation but i did not get real string ,
    still i am trying. can anyone please give me idea how to replace '%2F' to
    '/'
    char url[] = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
    char *p;
    while ((p = strstr(url, "%2F")) != NULL) {
    *p = '/';
    memmove (p+1, p+3, strlen(p+2));
    }

    Note the change in the first line. Also note that this is not
    especially efficient, nor tremendously flexible -- but it's what you
    asked for, so ...

    --
    Eric.Sosman@sun .com

    Comment

    • Antoninus Twink

      #3
      Re: replace string

      On 4 Nov 2008 at 17:02, Eric Sosman wrote:
      char url[] = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
      char *p;
      while ((p = strstr(url, "%2F")) != NULL) {
      *p = '/';
      memmove (p+1, p+3, strlen(p+2));
      }
      >
      Also note that this is not especially efficient, nor tremendously
      flexible
      Talk about the understatement of the century - that's right out of the
      Richard Heath Field school of efficiency.

      Do you get a kick out of mocking new posters by trying to lead them down
      the garden path with a deliberately silly solution Eric?

      Comment

      • Flash Gordon

        #4
        Re: replace string

        Sumit wrote, On 04/11/08 16:53:
        hi,
        I have a url which is a char *
        >
        char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
        >
        when i send this URL to http server it was like -
        >
        "/url=/home/sumit/pacakges/myxml.xml"
        It may look like that, but if the string is defined using "%2F" then I
        would expect that to be what is actually transmitted.
        but when it get by http server it convert '/' to '%2F' i have to
        reconvert it to '/'
        i did so many string operation but i did not get real string ,
        still i am trying. can anyone please give me idea how to replace '%2F' to
        '/'
        Show us your attempt. Doing this processing is not difficult, just scan
        the string for a % and treat the following two characters as a hex
        number representing the required character then copy the rest of the
        string (whilst still looking for % characters). Of course, you also have
        to decide what to do with invalid input.
        --
        Flash Gordon
        If spamming me sent it to smap@spam.cause way.com
        If emailing me use my reply-to address
        See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

        Comment

        • Bartc

          #5
          Re: replace string


          "Sumit" <sumit15nov@gma il.comwrote in message
          news:49107def$0 $90268$14726298 @news.sunsite.d k...
          hi,
          I have a url which is a char *
          >
          char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
          >
          when i send this URL to http server it was like -
          >
          "/url=/home/sumit/pacakges/myxml.xml"
          >
          but when it get by http server it convert '/' to '%2F' i have to
          reconvert it to '/'
          i did so many string operation but i did not get real string ,
          still i am trying. can anyone please give me idea how to replace '%2F' to
          '/'
          The following might work. But you probably need a more general solution.

          #include <string.h>

          void fixstring(char *s) {
          char *t,*u,*v;

          t=u=s;
          v=s+strlen(s)-3; /* Last possible %2F position */

          /* copy chars from *u to *t, replacing %2F sequences with / */

          while (*u) {
          if (u<=v && *u=='%' && *(u+1)=='2' && *(u+2)=='F') {
          *t++='/';
          u+=3;
          }
          else
          *t++=*u++;
          }
          *t=0;
          }


          Comment

          • Ben Bacarisse

            #6
            Re: replace string

            "Bartc" <bc@freeuk.comw rites:
            "Sumit" <sumit15nov@gma il.comwrote in message
            news:49107def$0 $90268$14726298 @news.sunsite.d k...
            >hi,
            >I have a url which is a char *
            >>
            >char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
            >>
            >when i send this URL to http server it was like -
            >>
            >"/url=/home/sumit/pacakges/myxml.xml"
            >>
            >but when it get by http server it convert '/' to '%2F' i have to
            >reconvert it to '/'
            >i did so many string operation but i did not get real string ,
            >still i am trying. can anyone please give me idea how to replace '%2F' to
            >'/'
            >
            The following might work. But you probably need a more general solution.
            >
            #include <string.h>
            >
            void fixstring(char *s) {
            char *t,*u,*v;
            >
            t=u=s;
            v=s+strlen(s)-3; /* Last possible %2F position */
            This can make v invalid. You can get undefined behaviour just by
            computing it. Having computed it you can't ask if u <= v (and get a
            defined result) although it often works.
            /* copy chars from *u to *t, replacing %2F sequences with / */
            >
            while (*u) {
            if (u<=v && *u=='%' && *(u+1)=='2' && *(u+2)=='F') {
            I like to write this sort of test like this:

            if (u <= v && strncmp(u, "%2F", 3) == 0)

            since it makes what is sought so very clear.
            *t++='/';
            u+=3;
            }
            else
            *t++=*u++;
            }
            *t=0;
            }
            I now feel I must show an tell so I am not just being negative. I'd
            write it like this:

            char *decode_hex(cha r *dest, const char *src)
            {
            char c, *dp = dest;
            while ((c = *src++) != '\0') {
            int nc;
            unsigned int uc;
            if (c == '%' && sscanf(src, "%2x%n", &uc, &nc) == 1 && nc == 2) {
            src += nc;
            *dp++ = uc;
            }
            else *dp++ = c;
            }
            *dp = '\0';
            return dest;
            }

            The return of the string is just for convenience in larger expressions
            and separating the source from the destination (which can, of course,
            be the same) allows the function to be used on unmodifiable strings.

            --
            Ben.

            Comment

            • Bartc

              #7
              Re: replace string


              "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
              news:8763n32gpr .fsf@bsb.me.uk. ..
              "Bartc" <bc@freeuk.comw rites:
              >
              >"Sumit" <sumit15nov@gma il.comwrote in message
              >news:49107def$ 0$90268$1472629 8@news.sunsite. dk...
              >>hi,
              >>I have a url which is a char *
              >>>
              >>char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
              >>>
              >>when i send this URL to http server it was like -
              >>>
              >>"/url=/home/sumit/pacakges/myxml.xml"
              >>>
              >>but when it get by http server it convert '/' to '%2F' i have to
              >>reconvert it to '/'
              >>i did so many string operation but i did not get real string ,
              >>still i am trying. can anyone please give me idea how to replace '%2F'
              >>to
              >>'/'
              >>
              >The following might work. But you probably need a more general solution.
              >>
              >#include <string.h>
              >>
              >void fixstring(char *s) {
              >char *t,*u,*v;
              >>
              >t=u=s;
              >v=s+strlen(s )-3; /* Last possible %2F position */
              >
              This can make v invalid. You can get undefined behaviour just by
              computing it. Having computed it you can't ask if u <= v (and get a
              defined result) although it often works.
              Yes I was wondering if there was a problem in the rare case where v becomes
              (or would become) negative.

              --
              Bartc

              Comment

              • Bill Reid

                #8
                Re: replace string


                Sumit <sumit15nov@gma il.comwrote in message
                news:49107def$0 $90268$14726298 @news.sunsite.d k...
                hi,
                I have a url which is a char *
                >
                char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
                >
                when i send this URL to http server it was like -
                >
                "/url=/home/sumit/pacakges/myxml.xml"
                >
                but when it get by http server it convert '/' to '%2F' i have to
                reconvert it to '/'
                i did so many string operation but i did not get real string ,
                still i am trying. can anyone please give me idea how to replace '%2F' to
                '/'
                Well, I could tell you exactly how to do it, but it might be easier
                to use an existing library that can encode/decode URLs, which
                appears to be what you want to do (the big advantage is of
                course that you don't have to re-invent not just one wheel, but
                dozens of them).

                I hate to mention a specific one, but Microsoft (shudder!)
                has something called "wininet" which is an API that works
                with the DLLs that provide client/server Internet functions
                to Windows Internet applications; there might be others available
                for other systems if you look for them.

                It really does look like you have a very specific system issue
                that requires that URLs be encoded (because of the "reserved"
                nature of certain URL characters), and if you find a suitable API,
                you can just call a function to encode/decode the URLs as needed.

                For some specific background info on the topic, you can
                go to the original RFC 2396 by Berners-Lee...

                ---
                William Ernest Reid

                Comment

                • arnuld

                  #9
                  Re: replace string

                  On Tue, 04 Nov 2008 18:31:35 +0000, Antoninus Twink wrote:
                  >On 4 Nov 2008 at 17:02, Eric Sosman wrote:
                  > char url[] = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
                  > char *p;
                  > while ((p = strstr(url, "%2F")) != NULL) {
                  > *p = '/';
                  > memmove (p+1, p+3, strlen(p+2));
                  > }
                  ...SNIP...
                  Do you get a kick out of mocking new posters by trying to lead them down
                  the garden path with a deliberately silly solution Eric?

                  I don't see anything silly in it, except if:

                  1) it is totally inefficient.
                  2) it does not handle the last %2F. I mean if %2F is at end of the url,
                  then will memove go beyond the array ?





                  --

                  my email is @ the above blog.
                  Google Groups is UnBlocked now :)


                  Comment

                  • arnuld

                    #10
                    Re: replace string

                    On Tue, 04 Nov 2008 23:50:56 +0000, Ben Bacarisse wrote:
                    ....SNIP...
                    I now feel I must show an tell so I am not just being negative. I'd
                    write it like this:
                    >
                    char *decode_hex(cha r *dest, const char *src)
                    {
                    char c, *dp = dest;
                    while ((c = *src++) != '\0') {
                    int nc;
                    unsigned int uc;
                    if (c == '%' && sscanf(src, "%2x%n", &uc, &nc) == 1 && nc == 2) {
                    src += nc;
                    *dp++ = uc;
                    }
                    else *dp++ = c;
                    }
                    *dp = '\0';
                    return dest;
                    }
                    The return of the string is just for convenience in larger expressions
                    and separating the source from the destination (which can, of course,
                    be the same) allows the function to be used on unmodifiable strings.


                    How about this ?


                    #include <stdio.h>
                    #include <stdlib.h>
                    #include <string.h>

                    int slash_converter ( const char* );


                    int main( void )
                    {
                    char url[] = "%2F%2Fhttp::%2 F%2F%2F%2Fwww.g oogle.com%2F";

                    if( slash_converter (url) )
                    {
                    fprintf(stderr, "Can not allocate memory\n");
                    exit(EXIT_FAILU RE);
                    }

                    return 0;
                    }


                    int slash_converter ( const char* pc )
                    {
                    const int SUCCESS = 0;
                    const int FAILURE = 1;

                    char REPLACE_WORD[] = "%2F";
                    const int REPLACE_WORD_LE NGTH = strlen(REPLACE_ WORD);
                    int INSIDE_MOD = 0;

                    char* new_arr = NULL;
                    const long arr_size = strlen(pc) + 1;

                    new_arr = malloc( arr_size + sizeof(*new_arr ));


                    if( !new_arr )
                    {
                    return FAILURE;
                    }


                    while( *pc )
                    {
                    if( strncmp(pc, REPLACE_WORD, REPLACE_WORD_LE NGTH) == 0 )
                    {
                    INSIDE_MOD = 1;
                    }

                    if( INSIDE_MOD )
                    {
                    *new_arr++ = '/';
                    pc += REPLACE_WORD_LE NGTH ;
                    INSIDE_MOD = 0;
                    }
                    else
                    {
                    *new_arr++ = *pc++;
                    }
                    }


                    return SUCCESS;
                    }





                    --

                    my email is @ the above blog.
                    Google Groups is UnBlocked now :)


                    Comment

                    • Eric Sosman

                      #11
                      Re: replace string

                      arnuld wrote:
                      >On Tue, 04 Nov 2008 18:31:35 +0000, Antoninus Twink wrote:
                      >
                      >>On 4 Nov 2008 at 17:02, Eric Sosman wrote:
                      >> char url[] = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
                      >> char *p;
                      >> while ((p = strstr(url, "%2F")) != NULL) {
                      >> *p = '/';
                      >> memmove (p+1, p+3, strlen(p+2));
                      >> }
                      >
                      >...SNIP...
                      >
                      >Do you get a kick out of mocking new posters by trying to lead them down
                      >the garden path with a deliberately silly solution Eric?
                      >
                      >
                      I don't see anything silly in it, except if:
                      >
                      1) it is totally inefficient.
                      Any measurements? Against what alternatives? Describe
                      the use case more fully: How long are these strings, how many
                      "%2F" sequences appear in them, how many of them need to be
                      processed? How many strings must you process with the (as yet
                      unknown) faster alternative before you break even on the time
                      spent writing it?

                      Sorry for the rant, but phrases like "totally inefficient"
                      when unaccompanied by even the barest whisper of an estimate,
                      much less a measurement, are totally stupid.
                      2) it does not handle the last %2F. I mean if %2F is at end of the url,
                      then will memove go beyond the array ?
                      Yes it does, and no it won't.

                      --
                      Eric Sosman
                      esosman@ieee-dot-org.invalid

                      Comment

                      • Ben Bacarisse

                        #12
                        Re: replace string

                        arnuld <sunrise@invali d.addresswrites :
                        >On Tue, 04 Nov 2008 23:50:56 +0000, Ben Bacarisse wrote:
                        <snip>
                        [All about replacing %2F with '/' in a string.]
                        How about this ?
                        >
                        <snip headers and main>
                        >
                        int slash_converter ( const char* pc )
                        An int returning function that is passed a const char * can only
                        compute an int based on that string. It can't change the string, and
                        it can't return a new string. This must be wrong.
                        {
                        const int SUCCESS = 0;
                        const int FAILURE = 1;
                        >
                        char REPLACE_WORD[] = "%2F";
                        const int REPLACE_WORD_LE NGTH = strlen(REPLACE_ WORD);
                        int INSIDE_MOD = 0;
                        >
                        char* new_arr = NULL;
                        const long arr_size = strlen(pc) + 1;
                        >
                        new_arr = malloc( arr_size + sizeof(*new_arr ));
                        Two things. (1) Why can't new_arr be initialised with the value you
                        want it to have? You do that for all the other variables. (2) This
                        space will be leaked since you don't free it and you can't get the
                        pointer out of this function.
                        if( !new_arr )
                        {
                        return FAILURE;
                        }
                        >
                        >
                        while( *pc )
                        {
                        if( strncmp(pc, REPLACE_WORD, REPLACE_WORD_LE NGTH) == 0 )
                        {
                        INSIDE_MOD = 1;
                        }
                        >
                        if( INSIDE_MOD )
                        {
                        *new_arr++ = '/';
                        pc += REPLACE_WORD_LE NGTH ;
                        INSIDE_MOD = 0;
                        }
                        else
                        {
                        *new_arr++ = *pc++;
                        }
                        }
                        You need to re-work this to get rid of INSIDE_MOD. You set it to 1,
                        immediately test it and set it to zero again. Just put the code it
                        controls where you set it!
                        return SUCCESS;
                        }
                        --
                        Ben.

                        Comment

                        • James Kuyper

                          #13
                          Re: replace string

                          Eric Sosman wrote:
                          arnuld wrote:
                          >>On Tue, 04 Nov 2008 18:31:35 +0000, Antoninus Twink wrote:
                          >>
                          >>>On 4 Nov 2008 at 17:02, Eric Sosman wrote:
                          >>> char url[] = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
                          >>> char *p;
                          >>> while ((p = strstr(url, "%2F")) != NULL) {
                          >>> *p = '/';
                          >>> memmove (p+1, p+3, strlen(p+2));
                          >>> }
                          ....
                          > 1) it is totally inefficient.
                          >
                          Any measurements? Against what alternatives? Describe
                          It restarts the search from the beginning of the url for each pass
                          through the loop. It should resume the search just after the point where
                          the previous search ended. I don't think that "totally" is justified;
                          depending upon the typical size of the search string and the probability
                          of finding "%2F", it might be a completely insignificant inefficiency;
                          but it is inefficient.

                          Comment

                          • Phil Carmody

                            #14
                            Re: replace string

                            Eric Sosman <esosman@ieee-dot-org.invalidwrit es:
                            arnuld wrote:
                            >>On Tue, 04 Nov 2008 18:31:35 +0000, Antoninus Twink wrote:
                            >>
                            >>>On 4 Nov 2008 at 17:02, Eric Sosman wrote:
                            >>> char url[] = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
                            >>> char *p;
                            >>> while ((p = strstr(url, "%2F")) != NULL) {
                            >>> *p = '/';
                            >>> memmove (p+1, p+3, strlen(p+2));
                            >>> }
                            >>
                            >>...SNIP...
                            >>
                            >>Do you get a kick out of mocking new posters by trying to lead them down
                            >>the garden path with a deliberately silly solution Eric?
                            >>
                            >>
                            >I don't see anything silly in it, except if:
                            >>
                            > 1) it is totally inefficient.
                            >
                            Any measurements? Against what alternatives? Describe
                            the use case more fully: How long are these strings, how many
                            "%2F" sequences appear in them, how many of them need to be
                            processed? How many strings must you process with the (as yet
                            unknown) faster alternative before you break even on the time
                            spent writing it?
                            >
                            Sorry for the rant, but phrases like "totally inefficient"
                            when unaccompanied by even the barest whisper of an estimate,
                            much less a measurement, are totally stupid.
                            Its worst case behaviour is Theta(n^2). There's a trivial
                            Theta(n) worst case solution, so there's no reason for
                            anything omega(n).

                            We're not told anything about the possible inputs, so assuming
                            they're always small and with a small number of '/' replacements
                            isn't necessarily justified. How inefficient is it? How long's
                            a URL?

                            Phil
                            --
                            We must respect the other fellow's religion, but only in the sense and to the
                            extent that we respect his theory that his wife is beautiful and his children
                            smart. -- Henry Louis Mencken (1880-1956), American editor and critic

                            Comment

                            • arnuld

                              #15
                              Re: replace string

                              On Wed, 05 Nov 2008 14:09:06 +0000, Ben Bacarisse wrote:
                              An int returning function that is passed a const char * can only
                              compute an int based on that string. It can't change the string, and
                              it can't return a new string. This must be wrong.

                              SO should I return void (which is what I thought earlier) but what if
                              there is some error in the process ?


                              Two things. (1) Why can't new_arr be initialised with the value you
                              want it to have? You do that for all the other variables. (2) This
                              space will be leaked since you don't free it and you can't get the
                              pointer out of this function.

                              ouch! I forgot to free(). Anyway, I am doing a static allocation.
                              Malloc() was used there because my friend wanted it anyway without any
                              solid reason :-\


                              You need to re-work this to get rid of INSIDE_MOD. You set it to 1,
                              immediately test it and set it to zero again.
                              Thats is intended because it will not be set to 1 most of the times. If
                              the comparison is false then it will simply go to next if-else clause.
                              2nd, there is anew problem now. I have to work on 19 different comparisons
                              instead of one. A URI has 19 conversion taking place e.g. %3F for a "?" in
                              URI. See the Percent-encoding_reserv ed_characters section here:





                              Just put the code it controls where you set it!
                              I don't know what you mean. Here is the new uncompilable code:


                              #include <stdio.h>
                              #include <stdlib.h>
                              #include <string.h>

                              void slash_converter ( const char*, const int );


                              int main( void )
                              {
                              char url[] = "%2F%2Fhttp ://google.com%2F";
                              const int arr_size = strlen(url);


                              slash_converter (url, arr_size);

                              return 0;
                              }


                              void slash_converter ( const char* pc, const int arr_size )
                              {
                              char new_arr[arr_size+1] = {0};

                              char replace_word[] = "%2F";
                              char replacement = '/';
                              const int REPLACE_WORD_LE NGTH = strlen(replace_ word);


                              while( *pc )
                              {
                              if( strncmp(pc, replace_word, REPLACE_WORD_LE NGTH) == 0 )
                              {
                              *new_arr++ = replacement;
                              pc += REPLACE_WORD_LE NGTH ;
                              }
                              else
                              {
                              *new_arr++ = *pc++;
                              }
                              }
                              }

                              =============== ===== OUTPUT =============== ===========
                              [arnuld@dune C]$ gcc4 -ansi -pedantic -Wall -Wextra slash-converter.c
                              slash-converter.c: In function ‘slash_conver ter’:
                              slash-converter.c:22: warning: ISO C90 forbids variable-size array ‘new_arr’
                              slash-converter.c:22: error: variable-sized object may not be initialized
                              slash-converter.c:22: warning: excess elements in array initializer
                              slash-converter.c:22: warning: (near initialization for ‘new_arr’)
                              slash-converter.c:33: error: invalid lvalue in increment
                              slash-converter.c:38: error: invalid lvalue in increment
                              [arnuld@dune C]$




                              Line 22: char new_arr[arr_size] = {0};

                              I really don't understand how this is variable size array when I am giving
                              the array size at compile time.


                              --

                              my email is @ the above blog.
                              Google Groups is UnBlocked now :)


                              Comment

                              Working...