Re: I need a snippet of EU code?
- Posted by Johnny Kinsey <jkinsey at MINDSPRING.COM> Mar 04, 1998
- 788 views
-----Original Message----- From: David Cuny <dcuny at DSS.CA.GOV> To: Multiple recipients of list EUPHORIA <EUPHORIA at MIAMIU.ACS.MUOHIO.EDU> Date: Wednesday, March 04, 1998 8:47 PM Subject: Re: I need a snippet of EU code? 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 = 0, ptY = 4, SIZE_OF_POINT = 8 I prefer to have the program do it for me: ----------------- -- Type POINT constant ptX = allot( LONG ), ptY = allot( LONG ), SIZE_OF_POINT = allotted_size() ----------------- -- Type LOGFONT constant lfHeight = allot( LONG ), lfWidth = allot( LONG ), lfEscapement = allot( LONG ), lfOrientation = allot( LONG ), lfWeight = allot( LONG ), lfItalic = allot( BYTE ), lfUnderline = allot( BYTE ), lfStrikeOut = allot( BYTE ), lfCharSet = allot( BYTE ), lfOutPrecision = allot( BYTE ), lfClipPrecision = allot( BYTE ), lfQuality = allot( BYTE ), lfPitchAndFamily = allot( BYTE ), lfFaceName = allot( 32 ), SIZE_OF_LOGFONT = 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 = 1, WORD = 2, LONG = 4 integer allottedSize allottedSize = 0 ----------------- function allot( integer i ) -- returns allotted size, and adds new field -- used to build a data structure integer soFar soFar = allottedSize allottedSize = allottedSize + i return soFar end function ----------------- function allotted_size() -- returns allotted size, and clears size integer soFar soFar = allottedSize allottedSize = 0 return soFar end function ----------------- To use the structures, just allocate them: ----------------- -- point pt = 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 = -1, WORD = -2, LONG = -4 ----------------- function allot( integer i ) -- returns allotted size, and adds new field -- used to build a data structure integer soFar soFar = allottedSize if i < 0 then allottedSize = allottedSize - i else allottedSize = allottedSize + i end if return {soFar, i} 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 = s[1] size = s[2] if size = BYTE then poke( struct+offset, o ) elsif size = WORD then s = int_to_bytes( o ) poke( struct+offset, o[1..2] ) elsif size = LONG then poke4( struct+offset, o ) else 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 = s[1] size = s[2] if size = BYTE then return peek( struct+offset ) elsif size = WORD then return bytes_to_int( peek( {struct+offset} & {0,0} ) ) elsif size = LONG then peek4s( struct+offset ) else 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 Thanks David I was beginning to wander if you had to be born with this kind of Information stuck in your head or what. This will help allot to when you don't know the data size for each structure that you build. I will apply this code to mine and let you know how it goes. How did you come up with doing it this way. Also do you have a web site I haven't seen it listed anywhere? Thanks again JKinsey