Re: reading strings

new topic     » goto parent     » topic index » view thread      » older message » newer message

Bernie Ryan wrote:
> 
> Hayden McKay wrote:
> > 
> > 
> > just wanted to share a small routine for reading strings. I use it to load
> > large
> > strings (no longer than 256 bytes) into euphoria.
> > 
> > nb. probably not very fast for smaller type strings.
> > 
> > you could change the 'TERMINMATOR' and 'MAXLENGTH' constants to suit your
> > application
> > 
> > }}}
<eucode>
> > --==============================================--
> > constant
> >     --
> >     TERMINATOR = 0,       -- the string terminator
> >     MAXLENGTH = 256       -- maximum string length
> > 	
> > --------------------------------------------------
> > -- returns sequence as a string from memory/device
> > 
> > global function string(atom lp)
> > 
> >     sequence chunk
> >     integer loc
> > 
> >     chunk = peek({lp, MAXLENGTH})
> > 
> >     loc = find(TERMINATOR, chunk)
> >     if loc then
> >         chunk = chunk[1..(loc-1)]
> >     end if
> > 
> >     return chunk
> > 
> > end function
> > </eucode>
{{{

> 
> Hayden:
>     Couldn't you do it this way ?
> 
> }}}
<eucode>
> constant
> --
> TERMINATOR = 0,       -- the string terminator
> MAXLENGTH = 256       -- maximum string length
> --
> global function string(atom lp)
>   return find(TERMINATOR, peek({lp, MAXLENGTH}))
> end function
> </eucode>
{{{

> 
> Bernie
> 
> My files in archive:
> WMOTOR, XMOTOR, W32ENGIN, MIXEDLIB, EU_ENGIN, WIN32ERU, WIN32API 
> 
> Can be downloaded here:
> <a
> href="http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan">http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan</a>

The result returned by your function is an 'integer' (the position of the zero
terminator) either 1..256 or 0 if none was found. where my function returns an
actual sequence.

nb. After reading your post and doing some testing I found that my routine will
crash if 'atom lp' points to a 0. I have'nt had to read empty strings before so
an application crash was never a problem for me, but I should change routine to
accomodate as follows.

--==============================================--
constant
    --
    TERMINATOR = 0,       -- the string terminator
    MAXLENGTH = 256       -- maximum string length
 	
--------------------------------------------------
-- changes...
--         function now returns "{}" if the string
--         is empty or the terminator is not found

global function string(atom lp)

    sequence chunk
    integer loc

    chunk = peek({lp, MAXLENGTH})

    loc = find(TERMINATOR, chunk)

--  if loc then
    if loc >= 1 then
        chunk = chunk[1..(loc-1)]
    else
        chunk = {}   -- return empty string.
    end if

    return chunk

end function


new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu