[comp.sys.mac.programmer] Using a handle to get data: How?

wolf@mel.cipl.uiowa (07/09/90)

I do GetNamedResource, and it is a resource which I have data stored in.  I
want to be able to access indiviudally each byte within the data.  How do I do
this, I have played with it and looked through MRI,II, and III to see how.

Basically what I want to know is how to use a handle to get a value of a
certain byte at a specified offset from the handle.

If someone could enlighten me on this I would be very appreciative.  I am will
take responses in pascal (I am using turbo at the time).

Thanks
WOLF@MEL.CIPL.UIOWA.EDU

jackiw@cs.swarthmore.edu (Nick Jackiw) (07/09/90)

wolf@mel.cipl.uiowa writes:
> Basically what I want to know is how to use a handle to get a value of a
> certain byte at a specified offset from the handle.
> 
> If someone could enlighten me on this I would be very appreciative.  I am will
> take responses in pascal (I am using turbo at the time).
> 

If Turbo Pascal supports packed arrays, try this:

function ByteAtOffset(myHandle:Handle,myOffset:longint):Byte;

type ByteArray= packed array [0..3] of byte;
     pByteArray=^ByteArray;
     hByteArray=^pByteArray;

begin
 ByteAtOffset:=hByteArray(myHandle)^^[myOffset]
end;

This is known as "coercing" a data type: Handle normally points to a 
pointer which points to a byte; we coerce it to point indirectly to an
array of bytes.  We need "packed array" because otherwise the compiler
may allocate the array of bytes internally as an array of words, with
the byte value occupying only half the storage space of a given element.
We can define the array as only [0..3] and yet access any element (e. g.
[1000]) only if there is no implicit range-checking in the compiler;
if there is, we must explicitly declare a maximum size ([0..16383], for
instance), which is not quite as good, in that it prevents us from
accessing bytes #16384 and up.

Hope this helps.  
   

--
---
jackiw@cs.swarthmore.edu  |  Smoggo: Can you prevent the detonation of
jackiw@swarthmr.bitnet    |          the thermonuclear device?
Applelink: D3717          |   Jimbo: No ... I cannot.

mxmora@unix.SRI.COM (Matt Mora) (07/10/90)

In article <1990Jul8.171009.1@mel.cipl.uiowa> wolf@mel.cipl.uiowa writes:


[stuff deleted]

I gathered from your post that you obtained the handle from the resource
manager with success.

>Basically what I want to know is how to use a handle to get a value of a
>certain byte at a specified offset from the handle.
>
>If someone could enlighten me on this I would be very appreciative.  I am will
>take responses in pascal (I am using turbo at the time).

Try something like this:

 function GetByteFromHandle (TheHandle: Handle; offset: longint): Byte;
  var
   temp: byte;
 begin
  BlockMove(ptr(ord(TheHandle^) + offset), @temp, 1);
  GetByteFromHandle := temp;
 end;


Blockmove is not listed as a trap that might move memory so you can
probably get away with not locking down the handle before you call
this routine.


>Thanks
>WOLF@MEL.CIPL.UIOWA.EDU


-- 
___________________________________________________________
Matthew Mora                |   my Mac  Matt_Mora@sri.com
SRI International           |  my unix  mxmora@unix.sri.com
___________________________________________________________