473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

the usage of sscanf

Hi, all

I am trying to use sscanf to parse the header for a web server,
according to the requirement, it need to neglect all the blanks in the
header
for example, all the following should be equvalient and the value should
be read correctly( get "Host" and "localhost" )
" Host: localhost "
" Host : localhost "
" Host :localhost "
"Host:localhost"
etc.

I have tried various ways and wrote the following code:
--------
st=sscanf(header, " %[a-zA-Z0-9_-] : %[^ ]" ,name, value);
---------
and so far it seems works..however, it only support a limit set of chars
and if I want more, I need to add all of them into the bracket, which
looks awkward. I am wondering if anyone has a better solution to my
problem and hope you could kindly help me out.

Many thanks.
--
Life is an opportunity to do something.
.-._
o_oo'_)
`._ `._
`, \
//_(_)_/
~~
Nov 14 '05 #1
4 5576
do*@dot.dot writes:
[...]
Use a #define with your character set in it...
Use the resulting constant in your code...

#define MY_CS a-zA-Z0-9_-

st = sscanf(header, " %[MY_CS] : %[^ ]" ,name, value)


Macros aren't expanded in string literals.

I suppose you could do:

#define MY_CS "a-zA-Z0-9_-"
st = sscanf(header, " %[" MY_CS "] : %[^ ]" ,name, value);

but that's just equivalent to:

st = sscanf(header, " %[a-zA-Z0-9_-] : %[^ ]" ,name, value);

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #2
Keith Thompson wrote:
do*@dot.dot writes:
[...]
Use a #define with your character set in it...
Use the resulting constant in your code...

#define MY_CS a-zA-Z0-9_-

st = sscanf(header, " %[MY_CS] : %[^ ]" ,name, value)

Macros aren't expanded in string literals.

I suppose you could do:

#define MY_CS "a-zA-Z0-9_-"
st = sscanf(header, " %[" MY_CS "] : %[^ ]" ,name, value);

but that's just equivalent to:

st = sscanf(header, " %[a-zA-Z0-9_-] : %[^ ]" ,name, value);

Many thanks.

Another question, is there any way to use another form of regular
expression without using the charset?

Thanks in advance again.
--
Life is an opportunity to do something.
.-._
o_oo'_)
`._ `._
`, \
//_(_)_/
~~
Nov 14 '05 #3
>Keith Thompson wrote:
[slight editing]
#define MY_CS "a-zA-Z0-9_-"
st = sscanf(header, " %[" MY_CS "] : %[^ ]" ,name, value);
but that's just equivalent to:
st = sscanf(header, " %[a-zA-Z0-9_-] : %[^ ]" ,name, value);

In article <9o*********************@news20.bellglobal.com>
Da Wang <Da******@THEgoogleMail.com> wrote:Another question, is there any way to use another form of regular
expression without using the charset?


No. In fact, scanf does not really do regular expressions at
all -- the character-class %[ conversion is the equivalent of
[class]+ (i.e., one or more characters from the scanset), but no
other regular-expression features are available. (As a result,
the scanf engine does not need the amount of code found in most
RE matchers. The obvious trivial algorithm has linear behavior
and never needs to back up.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #4
On Thu, 31 Mar 2005 22:37:16 -0500, Da Wang
<Da******@THEgoogleMail.com> wrote:
Hi, all

I am trying to use sscanf to parse the header for a web server,
according to the requirement, it need to neglect all the blanks in the
header
for example, all the following should be equvalient and the value should
be read correctly( get "Host" and "localhost" )
" Host: localhost "
" Host : localhost "
" Host :localhost "
"Host:localhost"
etc.
Your requirement is wrong. Treating a header line beginning with
whitespace as a new item is in violation of 2068 syntax, inherited via
1945 from 822, which makes it a continuation of the preceding "folded"
header. Space after the header name before the colon is also
explicitly forbidden, and I've never seen it used, although it can be
parsed unambiguously under the "liberal receive" principle.
I have tried various ways and wrote the following code:
--------
st=sscanf(header, " %[a-zA-Z0-9_-] : %[^ ]" ,name, value);
---------
The range syntax a-z etc. is not standard C and thus not guaranteed
portable, but in practice it probably works on all but EBCDIC systems.

This isn't _ignoring_ spaces in the value part, it is terminating the
value at a space. For Host in particular this is OK because a
domainname (or IPaddress) can't contain whitespace, but this may be
wrong for other header fields.
and so far it seems works..however, it only support a limit set of chars
and if I want more, I need to add all of them into the bracket, which
looks awkward. I am wondering if anyone has a better solution to my
problem and hope you could kindly help me out.

If you want to accept anything in the header label, except colon and
maybe space (or HWS?) just use %[^:] or %[^ :] etc. If you want to
restrict it to given characters, you have to state those characters
somehow. You might find some systems that allow POSIX-style classes in
a *scanf scanset (as well as a regex) like %[[:alpha:][:digit:]-_] ,
but this isn't required and isn't that much better anyway.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
10
by: baumann | last post by:
hi, 1) first test program code #include <stdio.h> int main(void) { char * file = "aaa 23 32 m 2.23 ammasd"; int i2,i3;
4
by: baumann | last post by:
hi all there has 2 program 1) the first test program code #include <stdio.h> int main(void) {
5
by: jchludzinski | last post by:
I'm using strtok() to parse thru a line and read different numbers: float value; char *token; token = strtok( line, " " ); .... sscanf( token, "%f", &value ); These results are less...
22
by: Superfox il Volpone | last post by:
Hello I have some problem with sscanf, I tryed this code but it doesn't works : char* stringa = "18/2005" char mese; char anno; int i_letture; i_letture = sscanf(stringa, "%2s/%4s",...
8
by: Artemio | last post by:
Dear folks, I need some help with using the sscanf() function. I need to parse a string which has several parameters given in a "A=... B=... C=..." way, and each has a different type (one is a...
20
by: AMP | last post by:
Hello, Anybody know if anything exists like sscanf in c. I found a few things OL but most were pretty old. Maybe something has come along since 2004? Thanks Mike
5
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sþ0þ%6s Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I...
104
by: jayapal | last post by:
Hi all, Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). why...? regards, jayapal.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.