[comp.sys.mac.programmer] creating resources

jgreene@ub.d.umn.edu (john greene) (03/22/91)

I am an experienced pascal programmer but a novice Mac programmer.
I want to create a resource that contains several thousand
integers.  It is easy to create a resource that contains strings
and then convert the strings to integers but this was too slow.
Could some kind soul tell me how to create a resource that
contains a list of integers?  Preferably, I would like a program
to create the resource file and fill it with the integers rather
than typing the thousands of integers in by hand.
Thanks for any help.
John Greene  jgreene@ub.d.umn.edu

resnick@cogsci.uiuc.edu (Pete Resnick) (03/22/91)

jgreene@ub.d.umn.edu (john greene) writes:

>I want to create a resource that contains several thousand
>integers.  It is easy to create a resource that contains strings
>and then convert the strings to integers but this was too slow.
>Could some kind soul tell me how to create a resource that
>contains a list of integers?  Preferably, I would like a program
>to create the resource file and fill it with the integers rather
>than typing the thousands of integers in by hand.

Three things:
1. You *really* don't want to create a resource with thousands
of integers anyway. There is a Tech Note on why not to abuse
the Resource Manager in just this way. I would be much better
to use the data fork of the file to store your integers. If
you need them in memory, just read them into a handle from there.

2. If you **REALLY** want them in a resource, it is quite easy.
Remember that in memory, a resource is just a handle to some
stuff, whether it be character or numeric data. So create a
handle to a bunch of integers in your program and call
AddResource on that handle. Easy.

3. If the problem is that you are using ResEdit and want to
type in a resource made up of integers, you can just type
the values in hex into the hex editor, or you could create
a template (TMPL resource) for your integer resource type
which defines a list of DWRD's or HWRD's. See the ResEdit
manual for how to do this. I don't think you will want to
type them in anyway, and I would choose either of my above
recommedations over this.

Good luck,
pr
--
Pete Resnick             (...so what is a mojo, and why would one be rising?)
Graduate assistant - Philosophy Department, Gregory Hall, UIUC
System manager - Cognitive Science Group, Beckman Institute, UIUC
Internet/ARPAnet/EDUnet  : resnick@cogsci.uiuc.edu
BITNET (if no other way) : FREE0285@UIUCVMD

CXT105@psuvm.psu.edu (Christopher Tate) (03/22/91)

In article <1991Mar21.234809.26603@ux1.cso.uiuc.edu>, resnick@cogsci.uiuc.edu
(Pete Resnick) says:

>1. You *really* don't want to create a resource with thousands
>of integers anyway. There is a Tech Note on why not to abuse
>the Resource Manager in just this way. I would be much better
>to use the data fork of the file to store your integers. If
>you need them in memory, just read them into a handle from there.

Why not?

A resource with a thousand integers is only a couple or four thousand
bytes -- peanuts, compared to a lot of the color PICT resources floating
around out there....  What's wrong with putting them in a resource, and
using GetResource() to load them into a relocatable block in the heap?
At that point, you can call DetachResource() to let the Resource Mangler
forget about them, and then play whatever games with the block that you
want.

IMHO, that's a lot prettier than storing them in your app's data fork....

-------
Christopher Tate                   |
Student-Type Person                |     Migratory lifeform with a
cxt105@psuvm.psu.edu               |      tropism for bookstores.
{...}!psuvax1!psuvm.bitnet!cxt105  |
cxt105@psuvm.bitnet                |

lippin@ragu.berkeley.edu (The Apathist) (03/22/91)

Recently resnick@cogsci.uiuc.edu (Pete Resnick) wrote:

> You *really* don't want to create a resource with thousands of
> integers anyway. There is a Tech Note on why not to abuse the Resource
> Manager in just this way.

This won't cause a problem.  What the Resource Manager objects to is
creating thousands of resources.  Creating a single large resource
will slow down updates a bit, but won't really bother it.  (Early
versions of the Resource Manager had problems with resources bigger
than 32K, but that's long gone.)

Also, the scale of this problem may not be that big: ten thousand integers
(assuming the Pascal integer type) is less than 20K.  If it gets up
around fifty thousand, one might want to look at putting them in the
data fork, where one can read them in pieces and not keep the whole
table in memory at once.

					--Tom Lippincott
					  lippin@math.berkeley.edu

	"The living dead don't need to solve word problems!"
					--Calvin

u2zj@vax5.cit.cornell.edu (Stanton Loh) (03/23/91)

In article <1991Mar21.234809.26603@ux1.cso.uiuc.edu>,
resnick@cogsci.uiuc.edu (Pete Resnick) writes:
[...]
> Three things:
> 1. You *really* don't want to create a resource with thousands
> of integers anyway. There is a Tech Note on why not to abuse
> the Resource Manager in just this way. I would be much better
> to use the data fork of the file to store your integers. If
> you need them in memory, just read them into a handle from there.
>

I believe you misunderstand tech note 74.  What is not allowed
is to treat the resource fork in the same way as the data fork,
ie you cannot use PBOpenRFto open the resource fork and just start
blasting in your own data.  A valid resource fork begins with
a resource header - not your data.

What is fine (and what I think the original question asked) is
how to create your own resource, fill it with whatever you want,
so you can use GetResource() to get a Handle to it during run time.

For example (error checking deleted for brevity sake) to create
a resource file with your own resource N integer long of type
'MINE', number 200 named "MyResource" (in ThinkC):

Handle resHandle;
int *resPtr, refNum;

resHandle = NewHandle(N*sizeof(int)); /* create Handle to res */
HLock(resHandle);                     /* lock and dereference */
resPtr = (int *) *resHandle;

for (i=0; i<N; i++) *resPtr++ = i;    /* initialize */

CreateResFile("pMyResFile");
refNum = OpenResFile("pMyResFile");
AddResource(resHandle, 'MINE', 200, "pMyResource");
CloseResFile(refNum);

HUnlock(resHandle);
DisposHandle(resHandle);

-Stanton Loh
u2zj@vax5.cit.cornell.edu

resnick@cogsci.uiuc.edu (Pete Resnick) (03/23/91)

People have been complaining about my first statement, that the person
should really not create a resource and instead use the data fork,
accusing me of misunderstanding the problem. Let me clarify:

The tech note that I cited (I don't have the number handy) is entitled
"Don't Abuse the Managers". In reference to the Resource Manager, it
says not to use the Resource Manager as a data base. The original poster
wanted to save "thousands" of integers. When we get up into the range
of 4K of data that isn't particularly complex in format (unlike a PICT)
and may not *need* to be in memory all at once, it is more reasonable
to create a separate data file (not in your applications data fork, as
one person misunderstood) and save the information there. Not that 4K
is ridiculous to be in memory at once, but once we get into data that
size, using the data fork starts to seem reasonable. If, of course,
the person wanted to keep it all in memory, and needed it read in
conveniently, a resource I agree might be reasonable. The original
poster, however, was not indicating one way or the other.

The Resource Manager is a nice tool, but using direct file i/o to the
data fork is more efficent and more flexible. I was trying to give
the poster an alternative to what might have been a quick and possibly
misinformed judgement about what tool to use. The person sounded like
somewhat of a beginner to the Resource Manager, and I was providing
as much info as I could to be helpful.

Hope that clears some things up.

pr
--
Pete Resnick             (...so what is a mojo, and why would one be rising?)
Graduate assistant - Philosophy Department, Gregory Hall, UIUC
System manager - Cognitive Science Group, Beckman Institute, UIUC
Internet/ARPAnet/EDUnet  : resnick@cogsci.uiuc.edu
BITNET (if no other way) : FREE0285@UIUCVMD

Jim.Spencer@p510.f22.n282.z1.fidonet.org.org (Jim Spencer) (03/24/91)

Pete Resnick writes in a message to All

PR> 1. You *really* don't want to create a resource with thousands 
PR> of integers anyway. There is a Tech Note on why not to abuse 
PR> the Resource Manager in just this way. I would be much better 
PR> to use the data fork of the file to store your integers. If you 
PR> need them in memory, just read them into a handle from there. 

There's nothing inherently abusive about storing even thousands of integers in a resource.  The key here is "a" resource.  The warning from Apple would apply to creating seperate resources for each integer.  At the same time, there doesn't seem to be any reason not to directly write them to the data fork as you suggest.

 

russotto@eng.umd.edu (Matthew T. Russotto) (03/25/91)

In article <1065@ub.d.umn.edu> jgreene@ub.d.umn.edu (john greene) writes:
>I am an experienced pascal programmer but a novice Mac programmer.
>I want to create a resource that contains several thousand
>integers.  It is easy to create a resource that contains strings
>and then convert the strings to integers but this was too slow.
>Could some kind soul tell me how to create a resource that
>contains a list of integers?  Preferably, I would like a program
>to create the resource file and fill it with the integers rather
>than typing the thousands of integers in by hand.
>Thanks for any help.

I don't know why you would want thousands of integers in a resource-- I would
think a random-access data file would be a better idea, but:

Type
  INTS = array [1..numofints] of Integer;
  IntPtr = ^INTS;
  IntHandle = ^IntPtr

Var
  ThousInt: Handle;
  myints: IntPtr;

  ThousInt = NewHandle (SizeOf(INTS));
(* I  THINK mac pascals all have SizeOf *)
  HLock(ThousInt);
  myints = IntPtr(ThousInt^)
  myints[1] = whatever;
  myints[2] = whatever;
  myints[3] = whatever;
  ...
  myints[numofints] = whatever;
  AddResource(Handle(ThousInt), 'Ints', 3838 (* ID # *), "Name");

--
Matthew T. Russotto	russotto@eng.umd.edu	russotto@wam.umd.edu
     .sig under construction, like the rest of this campus.

russotto@eng.umd.edu (Matthew T. Russotto) (03/25/91)

In article <1991Mar24.193356.11251@eng.umd.edu> russotto@eng.umd.edu (Matthew T. Russotto) writes:

>I don't know why you would want thousands of integers in a resource-- I would
>think a random-access data file would be a better idea, but:
>
>Type
>  INTS = array [1..numofints] of Integer;
>  IntPtr = ^INTS;
>  IntHandle = ^IntPtr
>
>Var
>  ThousInt: Handle;
>  myints: IntPtr;
>
>  ThousInt = NewHandle (SizeOf(INTS));
>(* I  THINK mac pascals all have SizeOf *)
>  HLock(ThousInt);
>  myints = IntPtr(ThousInt^)
|  myints^[1] = whatever;
|  myints^[2] = whatever;
|  myints^[3] = whatever;
>  ...
|  myints^[numofints] = whatever;
|  AddResource(Handle(ThousInt), 'Ints', 3838 (* ID # *), 'Name');

note the changes-- it's been too long since I did pascal.  Other, less obvious
syntactic errors probably included :-)
--
Matthew T. Russotto	russotto@eng.umd.edu	russotto@wam.umd.edu
     .sig under construction, like the rest of this campus.

kent@sunfs1.camex.com (Kent Borg) (03/25/91)

In article <1991Mar22.025750.17269@agate.berkeley.edu> lippin@math.berkeley.edu writes:
...
>(assuming the Pascal integer type) is less than 20K.  If it gets up
>around fifty thousand, one might want to look at putting them in the
>data fork, where one can read them in pieces and not keep the whole
>table in memory at once.

Ah, but under 7.0, you can read and write big resources a little at a
time.  (I haven't tried it, but that is what the ads say...)

(Fodder,
fodder...)

--
Kent Borg                            internet: kent@camex.com   AOL: kent borg
                                            H:(617) 776-6899  W:(617) 426-3577
"We foolishly did not realize that he was stupid."  - April Glasbie 3-20-91