Re: I need a snippet of EU code?

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

------ =_NextPart_000_01BD4795.4BCF06C0
Content-Transfer-Encoding: quoted-printable

Hi, Johnny.

You are correct in how to allocate structures. You don't have to worry =
about overwriting system memory, because the allocate() function returns =
"safe" memory, as long as you stay within the area allotted.

Some people define structures by doing the math themselves:

-----------------
-- Type POINT
constant
    ptX                 =3D 0,
    ptY                 =3D 4,
    SIZE_OF_POINT       =3D 8

I prefer to have the program do it for me:

-----------------
-- Type POINT
constant
    ptX                 =3D allot( LONG ),
    ptY                 =3D allot( LONG ),
    SIZE_OF_POINT       =3D allotted_size()

-----------------
-- Type LOGFONT
constant
    lfHeight            =3D allot( LONG ),
    lfWidth             =3D allot( LONG ),
    lfEscapement        =3D allot( LONG ),
    lfOrientation       =3D allot( LONG ),
    lfWeight            =3D allot( LONG ),
    lfItalic            =3D allot( BYTE ),
    lfUnderline         =3D allot( BYTE ),
    lfStrikeOut         =3D allot( BYTE ),
    lfCharSet           =3D allot( BYTE ),
    lfOutPrecision      =3D allot( BYTE ),
    lfClipPrecision     =3D allot( BYTE ),
    lfQuality           =3D allot( BYTE ),
    lfPitchAndFamily    =3D allot( BYTE ),
    lfFaceName          =3D allot( 32 ),
    SIZE_OF_LOGFONT     =3D allotted_size()


If you are interested, here's my code for creating structures. Notice =
that there's a bit of a trick; allotted_size() clears the count. This =
should look awfully familiar to any Forth coder.

Mind you, I *should* take the word boundary into account for strings!


-----------------

constant
    BYTE    =3D 1,
    WORD    =3D 2,
    LONG    =3D 4                                =20

integer allottedSize
allottedSize =3D 0
-----------------
function allot( integer i )

    -- returns allotted size, and adds new field
    -- used to build a data structure
   =20
    integer soFar
   =20
    soFar =3D allottedSize

    allottedSize =3D allottedSize + i
   =20
    return soFar
   =20
end function

-----------------
function allotted_size()

    -- returns allotted size, and clears size

    integer soFar
   =20
    soFar =3D allottedSize

    allottedSize =3D 0
   =20
    return soFar
   =20
end function
   =20
-----------------

To use the structures, just allocate them:

-----------------
-- point
pt  =3D allocate( SIZE_OF_POINT )
-----------------

and populate:

-----------------
poke4( pt+ptX, iDeciPtWidth * cxDpi / 72 )
poke4( pt+ptY, iDeciPtHeight * cyDpi / 72 )
-----------------


Just don't forget to release them when you are done:

-----------------
-- release structures
free( pt )
-----------------

If you wanted to be *real* lazy, you could have allot() return the size =
AND the offset. (I don't make any promises about the code that follows, =
other than to say it's untested):

-----------------
constant
    BYTE    =3D -1,
    WORD    =3D -2,
    LONG    =3D -4                                =20
-----------------
function allot( integer i )

    -- returns allotted size, and adds new field
    -- used to build a data structure
   =20
    integer soFar
   =20
    soFar =3D allottedSize

    if i < 0 then
        allottedSize =3D allottedSize - i
    else
        allottedSize =3D allottedSize + i
    end if
   =20
    return {soFar, i}
   =20
end function
-----------------

and write a procedure that looks at the size when it pokes:

-----------------
procedure store( atom struct, sequence s, object o )

   -- store based on the size
   integer offset, size

   -- get the offset and data type

   offset =3D s[1]
   size =3D s[2]

   if size =3D BYTE then
        poke( struct+offset, o )

   elsif size =3D WORD then
        s =3D int_to_bytes( o )
        poke( struct+offset, o[1..2] )

   elsif size =3D LONG then
        poke4( struct+offset, o )

   else  =20
        poke( struct+offset, o )

   end if

end procedure


-----------------

As well as an inverse routine for peeks:

-----------------
procedure fetch( atom struct, sequence s )

   -- store based on the size
   integer offset, size

   -- get the offset and data type

   offset =3D s[1]
   size =3D s[2]

   if size =3D BYTE then
       return peek( struct+offset )

   elsif size =3D WORD then
       return bytes_to_int(
          peek( {struct+offset} & {0,0} ) )
=20
   elsif size =3D LONG then
        peek4s( struct+offset )

   else  =20
        return peek( {struct+offset, size} )

   end if

end procedure
-----------------

So you can write:

   store( pt, ptX, 12 )
   store( pt, ptY, 12 )

and

   fetch( pt, ptX )
   fetch( pt, ptY )

and not even *care* about the data size.


-- David Cuny

------ =_NextPart_000_01BD4795.4BCF06C0

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

Search



Quick Links

User menu

Not signed in.

Misc Menu