1. scanf
- Posted by akusaya at gmx.net
May 26, 2002
Is there a library that can mimic the function
scanf(fileNumber, format, data) in C/C++ ?
it's opposite of printf
I could make it but it's better if already avaliable.
Thanks!
And thanks for open_dll("") explanation from
Elliott Sales de Andrade <quantum_analyst at hotmail.com>
Ray Smith <smithr at ix.net.au>
Martin Stachon <martin.stachon at worldonline.cz>
jbrown105 at speedymail.org
2. Re: scanf
This is a multi-part message in MIME format.
------=_NextPart_000_0005_01C204AB.2E26AB00
charset="iso-8859-1"
Here is something I wrote in January which might serve your purpose.
Colin Taylor
----- Original Message -----
From: <akusaya at gmx.net>
To: "EUforum" <EUforum at topica.com>
Sent: Sunday, May 26, 2002 8:42 AM
Subject: scanf
>
>
> Is there a library that can mimic the function
> scanf(fileNumber, format, data) in C/C++ ?
>
> it's opposite of printf
>
> I could make it but it's better if already avaliable.
>
> Thanks!
>
> And thanks for open_dll("") explanation from
> Elliott Sales de Andrade <quantum_analyst at hotmail.com>
> Ray Smith <smithr at ix.net.au>
> Martin Stachon <martin.stachon at worldonline.cz>
> jbrown105 at speedymail.org
>
>
>
>
------=_NextPart_000_0005_01C204AB.2E26AB00
Content-Type: application/octet-stream;
name="PARSEF.E"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="PARSEF.E"
-- PARSEF.E -- formatted parser
-- Colin Taylor - cetaylor at compuserve.com
-- 21/01/02
-- PARSEF reads data from formatted strings, sort of like sprintf()
-- working in reverse. The format string follows the same rules as
-- for printf and sprintf, but additionally there must be at least
-- one delimiter character after every field.
include get.e
function read_octal(sequence octal_number)
-- returns {ec, nv} where
-- ec = 0 for success, 1 for fail
-- nv = integer value of octal_number
integer v
sequence s
v = 0
s = "01234567"
for i = 1 to length(octal_number) do
if find(octal_number[i], s) then
v += (octal_number[i]-'0')*power(8, length(octal_number)-i)
else
return {1, v}
end if
end for
return {0, v}
end function
global function parsef(sequence format, sequence target)
-- format is a format string following the same conventions as printf(),
-- with each data field followed by at least 1 character.
-- target is a target string containing 1 or more embedded values
-- (note: target can have extra trailing characters without causing error)
-- returns {error status, data} where:
-- error status codes are:
-- 0 - success
-- 1 - format string syntax error
-- 2 - format string data field error
-- 3 - target string syntax error
-- 4 - target string data field error
-- data is a sequence containing the parsed value(s)
sequence fp, tp, dlen, result, parsed
integer l1, l2, p1, p2, last_ptr
-- parse format string
-- 1st pass - remove %% fields (replace with -1)
while 1 do
p1 = match("%%", format)
if p1 then
format = format[1..p1-1] & -1 & format[p1+2..length(format)]
else
exit
end if
end while
-- 2nd pass - find beginning and end of data fields
fp = {}
last_ptr = 0
while 1 do
p1 = find('%', format[last_ptr+1..length(format)])
if p1 then
-- find end of field
p1 += last_ptr
p2 = p1+1
while 1 do
if find(format[p2], "defgosx") then
fp &= {p1, p2}
exit
else
p2 += 1
end if
end while
else
exit
end if
last_ptr = p1
end while
-- 3rd pass - check for illegal or ambiguous syntax
if length(fp) then
-- compute length of delimiter strings between values
dlen = {fp[1]-1} -- 1st delim
for i = 2 to length(fp)-1 by 2 do
dlen &= fp[i+1]-fp[i]-1 -- middle delims
end for
dlen &= length(format)-fp[length(fp)] -- last delim
if length(dlen) > 2 then
for i = 2 to length(dlen) do
if not dlen[i] then
return {1, {}} -- error: zero-length delim after data field
end if
end for
end if
else
return {1, {}} -- error: format string has no data fields
end if
-- 4th pass - replace all -1 with '%'
while 1 do
p1 = find(-1, format)
if p1 then
format[p1] = '%'
else
exit
end if
end while
-- finished parsing format string, now parse data string
-- 1st pass - find delimiters and save pointers to data entries
l1 = fp[1]
-- 1st delim
if equal(format[1..l1-1], target[1..l1-1]) then
tp = {l1} -- start of 1st data field
else
return {3, {}} -- error: 1st delim doesn't match
end if
-- the other delims
p2 = l1
for i = 2 to length(fp) by 2 do
l1 = fp[i]+1
if i < length(fp) then
l2 = fp[i+1]-1
else
l2 = length(format)
end if
p1 = match(format[l1..l2], target[p2+1..length(target)])
if p1 then
p1 += p2
tp &= p1-1 -- end of previous data field
if i < length(fp) then
p2 = p1+dlen[i/2+1]
tp &= p2 -- start of next data field
end if
else
return {3, {}} -- error: delim not found
end if
end for
-- 2nd pass - extract data and save values
parsed = {}
for i = 1 to length(fp) by 2 do
p1 = format[fp[i+1]]
if find(p1, "defg") then
result = value(target[tp[i]..tp[i+1]])
elsif p1 = 'o' then
result = read_octal(target[tp[i]..tp[i+1]])
elsif p1 = 'x' then
result = value("#" & target[tp[i]..tp[i+1]])
elsif p1 = 's' then
result = {0, {target[tp[i]..tp[i+1]]}}
else
return {2, {999}} -- shoudn't happen
end if
if result[1] then
return {4, parsed} -- error: couldn't read data field
else
parsed &= result[2]
end if
end for
return {0, parsed}
end function
------=_NextPart_000_0005_01C204AB.2E26AB00--
3. Re: scanf
This is a multi-part message in MIME format.
--_----------=_102243760761001
On 0, akusaya at gmx.net wrote:
>
> Is there a library that can mimic the function
> scanf(fileNumber, format, data) in C/C++ ?
>
> it's opposite of printf
>
> I could make it but it's better if already avaliable.
>
> Thanks!
>
> And thanks for open_dll("") explanation from
> Elliott Sales de Andrade <quantum_analyst at hotmail.com>
> Ray Smith <smithr at ix.net.au>
> Martin Stachon <martin.stachon at worldonline.cz>
> jbrown105 at speedymail.org
>
No, there is no scanf() in Euphoria. You could take a look at
get.e, which may serve your purpose.
Also included was my failed attempt to emulate scanf() and sscanf()
in Euphoria.
jbrown
--
http://fastmail.fm
- Taking the "ail" out of email!
--_----------=_102243760761001
Content-Disposition: attachment; filename="scanf.e"
4. scanf
Has anybody written a Euphoria version of the C scanf ?
(It's just about the only useful thing in C, IMO)
Irv
----------------------------------------------------------------------------
-----------
Visit my Euphoria programming web site:
http://www.mindspring.com/~mountains
----------------------------------------------------------------------------
-----------
5. Re: scanf
On Thu, 5 Mar 1998, Irv Mullins wrote:
> Has anybody written a Euphoria version of the C scanf ?
> (It's just about the only useful thing in C, IMO)
What's wrong with GET.E's get(x) and plain old getc(x) and gets(x)?
I'm sure if you're desperate, using those three and a few simple
"if extype(variable) then"s a simple emulation can be written.
--
Carl R White - "Infinity equals Zero in more ways than (just) one - CRW"
E-mail...: cyrek- at -bigfoot.com / Remove the hyphens before
Finger...: crwhite- at -dcsun1.comp.brad.ac.uk \ mailing or fingering...
Url......: http://www.bigfoot.com/~cyrek/
6. Re: scanf
> Has anybody written a Euphoria version of the C scanf ?
> (It's just about the only useful thing in C, IMO)
Actual-mont, I'd like to see Perl-esque regular expression handling.
I dig language with fantastic string handling, which is why I hate C, and
love languages like Pike and SNOBOL.
--
Cameron Kaiser * spectre at sserv.com * http://www.sserv.com/
--
Visit the leading Internet starting point today!
http://www.sserv.com/
--