Parsing response coming from server

Discussion in 'C++' started by sumit369@gmail.com, May 18, 2013.

  1. Guest

    I am getting below response from server -

    Main stream options:
    EncType1=H.264
    Resolution1=704*576
    KeyInterval1=50
    FrameRate1=25
    BitflowType1=VBR
    NormalBitrate1=2048

    Now I need to parse the parameter and its value, I have list of parameter in client, I just need the value of the parameter.

    I tried by using string operation and I used -
    string::find(), string::substr() and string::find_first_not_of() function
    and some how I have complete the code . and its working.

    the string for allowed_chars for find_first_not_of() ,
    "abcdefghijklmnopqrstuvvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:=-1234567890*"

    but its very bulky code and need every time compare with each characters.

    please give me some batter idea for parsing .

    this should be my function -
    string value = getParamValue(const string& response , const string& paramName)
    its returns the value of the parameter .

    eg.

    if I pass "EncType1" function should returns "H.264"

    Thanks
    Sumit
     
    , May 18, 2013
    #1
    1. Advertising

  2. Stefan Ram Guest

    writes:
    >please give me some batter idea for parsing .


    When I am supposed to write a parser, I do
    not want an example of the input language,
    but a grammar of the input language. Usually,
    I cannot derive the grammar from an example.
    so I cannot write a parser from an example.
     
    Stefan Ram, May 18, 2013
    #2
    1. Advertising

  3. Rui Maciel Guest

    wrote:

    > Now I need to parse the parameter and its value, I have list of parameter
    > in client, I just need the value of the parameter.
    >
    > I tried by using string operation and I used -
    > string::find(), string::substr() and string::find_first_not_of() function
    > and some how I have complete the code . and its working.


    Why don't you write a proper parser? For that grammar, it's more than
    trivial.


    Rui Maciel
     
    Rui Maciel, May 19, 2013
    #3
  4. Jorgen Grahn Guest

    On Sat, 2013-05-18, Stefan Ram wrote:
    > writes:
    >>please give me some batter idea for parsing .

    >
    > When I am supposed to write a parser, I do
    > not want an example of the input language,
    > but a grammar of the input language. Usually,
    > I cannot derive the grammar from an example.
    > so I cannot write a parser from an example.


    +1. Also it worries me that it's so common that questions like these
    come with only examples. The actual language is an /aid/ to the
    programmer, and part of the input requirements. You cannot safely
    ignore it.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, May 19, 2013
    #4
  5. Rosario1903 Guest

    On Sat, 18 May 2013 09:30:00 -0700 (PDT), wrote:

    >the string for allowed_chars for find_first_not_of() ,
    >"abcdefghijklmnopqrstuvvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:=-1234567890*"
    >
    >but its very bulky code and need every time compare with each characters.
    >
    >please give me some batter idea for parsing .


    i would do something as:

    #include <iostream>

    using namespace std;
    #define u8 unsigned char

    u8 vect[256];
    u8
    *avc="abcdefghijklmnopqrstuvvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:=-1234567890*";

    void inivect(void)
    {unsigned i,c;

    for(i=0; i<256; ++i)
    vect='?';
    for(i=0;avc;++i)
    vect[avc]=avc;
    }

    #define CharIsOk(x) (vect[x]==(x))
    #define safe(x) vect[x]

    int main(void)
    {unsigned i;
    u8 v[]="allow this ò ^ bb\n", vv[128], m;

    inivect();
    cout <<"inizio: "<< v;
    for(i=0; v; ++i)
    {m=v;
    if( CharIsOk(m)==0 ) cout << v;
    vv=safe(m);
    }
    cout << "not ok\n";
    vv=0;
    cout <<" fine: "<< vv;
    return 0;
    }

    --------------
    inizio: allow this = ^ bb
    = ^
    not ok
    fine: allow?this?????bb?
    --------------

    >this should be my function -
    >string value = getParamValue(const string& response , const string& paramName)
    >its returns the value of the parameter .
    >
    >eg.
    >
    >if I pass "EncType1" function should returns "H.264"
    >
    >Thanks
    >Sumit
     
    Rosario1903, May 19, 2013
    #5
  6. Luca Risolia Guest

    wrote:

    > I am getting below response from server -
    >
    > Main stream options:
    > EncType1=H.264
    > Resolution1=704*576
    > KeyInterval1=50
    > FrameRate1=25
    > BitflowType1=VBR
    > NormalBitrate1=2048
    >
    > Now I need to parse the parameter and its value, I have list of parameter in
    > client, I just need the value of the parameter.


    > please give me some batter idea for parsing .


    #include <iostream>
    #include <string>
    #include <unordered_map>
    #include <boost/regex.hpp>

    using Map = std::unordered_map<std::string, std::string>;

    Map parse(const std::string& response) {
    static const boost::regex expr("(\\w+)=([^\n]*)");
    boost::sregex_token_iterator i{response.begin(),
    response.end(),
    expr,
    {1, 2}},
    end;
    Map m;
    for (; i != end; ++i) {
    auto& key = *i++, &value = *i;
    m.emplace(key, value);
    }
    return m;
    }

    int main() {
    std::string response =
    R"(Main stream options:
    EncType1=H.264
    Resolution1=704*576
    Resolution1=704*577
    KeyInterval1=50
    FrameRate1=25
    BitflowType1=VBR
    NormalBitrate1=2048)";

    auto m = parse(response);

    std::cout << m.at("EncType1");
    }
     
    Luca Risolia, May 19, 2013
    #6
  7. Rosario1903 Guest

    On Sun, 19 May 2013 19:15:52 +0200, Rosario1903
    <> wrote:

    >i would do something as:
    >
    >#include <iostream>
    >
    >using namespace std;
    >#define u8 unsigned char
    >
    >u8 vect[256];
    >u8
    >*avc="abcdefghijklmnopqrstuvvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:=-1234567890*";
    >
    >void inivect(void)
    >{unsigned i,c;
    >
    > for(i=0; i<256; ++i)
    > vect='?';
    > for(i=0;avc;++i)
    > vect[avc]=avc;
    >}
    >
    >#define CharIsOk(x) (vect[x]==(x))


    #define CharIsOk(x) (vect[x]==(x)&& (x)!='?')

    >#define safe(x) vect[x]
    >
    >int main(void)
    >{unsigned i;
    > u8 v[]="allow this ò ^ bb\n", vv[128], m;
    >
    > inivect();
    > cout <<"inizio: "<< v;
    > for(i=0; v; ++i)
    > {m=v;
    > if( CharIsOk(m)==0 ) cout << v;
    > vv=safe(m);
    > }
    > cout << "not ok\n";
    > vv=0;
    > cout <<" fine: "<< vv;
    > return 0;
    >}
    >
    >--------------
    >inizio: allow this = ^ bb
    > = ^
    >not ok
    > fine: allow?this?????bb?
    >--------------
     
    Rosario1903, May 19, 2013
    #7
  8. Jorgen Grahn Guest

    On Sat, 2013-05-18, wrote:
    > I am getting below response from server -
    >
    > Main stream options:
    > EncType1=H.264
    > Resolution1=704*576
    > KeyInterval1=50
    > FrameRate1=25
    > BitflowType1=VBR
    > NormalBitrate1=2048


    Lets assume for a moment the simplest possible grammar: the language
    is a set of lines ended by CRLF. Each line is on the form
    "foo=bar\r\n" with no whitespace anywhere. There are no "continuation
    lines" like in HTTP or mail headers.

    > Now I need to parse the parameter and its value, I have list of
    > parameter in client, I just need the value of the parameter.
    >
    > I tried by using string operation and I used -
    > string::find(), string::substr() and string::find_first_not_of() function
    > and some how I have complete the code . and its working.


    Note that the functions in <algorithm> are at least as useful as the
    std::string member functions (which I find a bit hard to work with).
    Also there's the stuff in <cctype>.

    > the string for allowed_chars for find_first_not_of() ,
    > "abcdefghijklmnopqrstuvvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:=-1234567890*"
    >
    > but its very bulky code and need every time compare with each characters.
    >
    > please give me some batter idea for parsing .
    >
    > this should be my function -
    > string value = getParamValue(const string& response,
    > const string& paramName)


    That interface says a few interesting things:

    - You are not interested in the difference between "EncType1 isn't
    mentioned in the response" and "EncType1 has an empty value".
    - You're not interested in the same parameter name being listed twice.
    - You have to start from scratch every time you get() a value -- the
    work isn't split into one parsing step and one get(name) step. That
    means less-than-optimal performance, but perhaps that doesn't
    matter.
    - You have some way to get the parameters, and nothing but the
    parameters, into a single string. I often find that this part is a
    bit inconvenient; better to parse line by line into some data
    structure until you find end-of-parameters.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, May 19, 2013
    #8
  9. Rosario1903 Guest

    On Sat, 18 May 2013 09:30:00 -0700 (PDT), sumit369 wrote:
    >I am getting below response from server -
    >
    >Main stream options:
    >EncType1=H.264
    >Resolution1=704*576
    >KeyInterval1=50
    >FrameRate1=25
    >BitflowType1=VBR
    >NormalBitrate1=2048


    i would write global struct one for each names EncType1, Resolution1
    etc that contain one unsigned for optuion

    than i would do one lookup table from words as "EncType1=" etc that
    has as result the right address of the global struct above
    than i would parse afther "=" and fill the struct of the type
     
    Rosario1903, May 20, 2013
    #9
  10. James Kanze Guest

    On Saturday, May 18, 2013 10:00:05 PM UTC+1, Sam wrote:
    > writes:


    [...]
    > std::istringstream i(response);
    > std::string line;
    > while (std::getline(i, line).good())


    The above line is wrong. The use of `std::istream::good()`
    means that you're no longer sure when the loop will terminate.
    (In particular, it will probably not handle the last line
    correctly if it is missing a final '\n'.)

    --
    James
     
    James Kanze, May 20, 2013
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    2
    Views:
    8,018
    Charles A. Lackman
    Jan 11, 2006
  2. Drebin
    Replies:
    15
    Views:
    13,885
    slyi .
    Nov 23, 2004
  3. Steve Holden

    PyCon is Coming! PyCon is Coming!

    Steve Holden, Jan 5, 2006, in forum: Python
    Replies:
    0
    Views:
    316
    Steve Holden
    Jan 5, 2006
  4. =?Utf-8?B?TWFuaXNoIEJhZm5h?=

    Application server error is coming in Ajax Asp.NET site

    =?Utf-8?B?TWFuaXNoIEJhZm5h?=, Feb 2, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    347
    =?Utf-8?B?TWFuaXNoIEJhZm5h?=
    Feb 2, 2007
  5. Angus
    Replies:
    5
    Views:
    419
    Oliver Wong
    Nov 27, 2006
Loading...

Share This Page