Parsing response coming from server

S

sumit369

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
 
S

Stefan Ram

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.
 
R

Rui Maciel

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
 
J

Jorgen Grahn

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
 
R

Rosario1903

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?
--------------
 
L

Luca Risolia

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");
}
 
R

Rosario1903

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?
--------------
 
J

Jorgen Grahn

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 said:
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
 
R

Rosario1903

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
 
J

James Kanze

(e-mail address removed) 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'.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top