parsing a string

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

    #1

    parsing a string

    I've been trying to figure out a way to parse the following:

    192.168.0.7;use rname;password; database

    I'm stumped as to how to do that. Should I look for the first occurance
    of the ';', cut out position x through the position of the delimiter by 1,
    move that string to another string and repeat?

    -Alex
  • Peter Nilsson

    #2
    Re: parsing a string

    AlexB wrote:[color=blue]
    > I've been trying to figure out a way to parse the following:
    >
    > 192.168.0.7;use rname;password; database
    >
    > I'm stumped as to how to do that.[/color]

    Parsing IP addresses has been done to death. Search the archives
    of clc.
    [color=blue]
    > Should I look for the first occurance of the ';', cut out
    > position x through the position of the delimiter by 1,
    > move that string to another string and repeat?[/color]

    int ip[4];
    char username[256];
    char password[256];
    char database[256];
    char ignore;

    int r = sscanf(the_stri ng,
    "%3d.%3d.%3d.%3 d;%255[^;];%255[^;];%255[^;]%c",
    &ip[0], &ip[1], &ip[2], &ip[3],
    &username, &password, &database, &ignore);

    if (r == 7)
    {
    printf("%3d.%3d .%3d.%3d;%255[^;];%255[^;];%255[^;]%c",
    ip[0], ip[1], ip[2], ip[3],
    username, password, database);
    }

    --
    Peter

    Comment

    • Jens.Toerring@physik.fu-berlin.de

      #3
      Re: parsing a string

      AlexB <BartonekDragRa cing@yahoo.com> wrote:[color=blue]
      > I've been trying to figure out a way to parse the following:[/color]
      [color=blue]
      > 192.168.0.7;use rname;password; database[/color]
      [color=blue]
      > I'm stumped as to how to do that. Should I look for the first occurance
      > of the ';', cut out position x through the position of the delimiter by 1,
      > move that string to another string and repeat?[/color]

      What about something like the following? Depends of course a bit on
      what you exactly mean by "parsing" etc...

      #include <stdio.h>

      int main( void )
      {
      const char *sl = "192.168.0.7;us ername;password ;database";
      char ip[ 16 ];
      char un[ 100 ];
      char pw[ 100 ];
      char db[ 100 ];

      sscanf( sl, "%15[^;];%99[^;];%99[^;];%99s", ip, un, pw, db );
      printf( "%s - %s - %s - %s\n", ip, un, pw, db );
      return 0;
      }

      And, of course, there's the strtok() function, but you need to know
      what you are doing if you use it.

      Regards, Jens
      --
      \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
      \______________ ____________ http://www.toerring.de

      Comment

      • AlexB

        #4
        Re: parsing a string

        On Thu, 17 Mar 2005 18:01:50 -0800, Peter Nilsson wrote:
        [color=blue]
        > AlexB wrote:[color=green]
        >> I've been trying to figure out a way to parse the following:
        >>
        >> 192.168.0.7;use rname;password; database
        >>
        >> I'm stumped as to how to do that.[/color]
        >
        >[/color]

        Jens & Peter, thanks! Never thought of that suggestion.. I started going
        with strncpy source,dest,len gth and then parsing it like that..but yours
        sounds alot better.

        Thanks guys!!

        -Alex

        Comment

        • Peter Nilsson

          #5
          Re: parsing a string

          Peter Nilsson wrote:[color=blue]
          > printf("%3d.%3d .%3d.%3d;%255[^;];%255[^;];%255[^;]%c",
          > ip[0], ip[1], ip[2], ip[3],
          > username, password, database);[/color]

          Make that...

          "%d.%d.%d.%d;%s ;%s;%s"

          --
          Peter

          Comment

          • Tor Rustad

            #6
            Re: parsing a string [OT]

            "AlexB" <BartonekDragRa cing@yahoo.com> wrote in message[color=blue]
            > I've been trying to figure out a way to parse the following:
            >
            > 192.168.0.7;use rname;password; database
            >
            > I'm stumped as to how to do that. Should I look for the first[/color]
            occurance[color=blue]
            > of the ';', cut out position x through the position of the delimiter[/color]
            by 1,[color=blue]
            > move that string to another string and repeat?[/color]

            The proposed sscanf() solutions isn't very robust.

            1. Do you only need to consider IPv4 addresses (what about IPv6)?
            2. isn't ';' a legal character in passwords? If so, why not???
            3. consider using a more robust TLV scheme, instead of this comma
            separated list.


            --
            Tor <torust AT online DOT no>

            Comment

            • AlexB

              #7
              Re: parsing a string [OT]

              On Fri, 18 Mar 2005 14:38:11 +0100, Tor Rustad wrote:
              [color=blue]
              > "AlexB" <BartonekDragRa cing@yahoo.com> wrote in message[color=green]
              >> I've been trying to figure out a way to parse the following:
              >>
              >> 192.168.0.7;use rname;password; database
              >>
              >> I'm stumped as to how to do that. Should I look for the first[/color]
              > occurance[color=green]
              >> of the ';', cut out position x through the position of the delimiter[/color]
              > by 1,[color=green]
              >> move that string to another string and repeat?[/color]
              >
              > The proposed sscanf() solutions isn't very robust.
              >
              > 1. Do you only need to consider IPv4 addresses (what about IPv6)?
              > 2. isn't ';' a legal character in passwords? If so, why not???
              > 3. consider using a more robust TLV scheme, instead of this comma
              > separated list.[/color]

              yes just IPv4.. its basically for me.. I'm writing a little piece of
              software that connects to a MySQL db and stores info in it.
              You know..I tried the following and it didnt work..

              instead of a fgets I used:
              fscanf( temp, "%15[^;];%99[^;];%99[^;];%99s", &hostname, &username,
              &password, &database);

              all variables were parsed except the hostname..
              so I did a fgets into temp and then
              sscanf( temp, "%15[^;];%99[^;];%99[^;];%99s", &hostname, &username,
              &password, &database);

              and it worked.. weird.

              -Alex

              Comment

              • AlexB

                #8
                Re: parsing a string [OT]

                On Sat, 19 Mar 2005 16:36:12 +0000, AlexB wrote:
                [color=blue]
                > instead of a fgets I used:
                > fscanf( temp, "%15[^;];%99[^;];%99[^;];%99s", &hostname, &username,
                > &password, &database);[/color]

                little typo.. where temp is, I have fp.

                Comment

                Working...