[futurebasic] Re: [FB] Containers, handles & str# resources

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 2001 : Group Archive : Group : All Groups

From: Alain Pastor <apastor@...>
Date: Tue, 27 Nov 2001 15:45:01 +0100
Bernie Wylde wrote:
> 
> Hi Guys,
> I'd like to copy the contents of an external str# resource to a local str#
> resource. This isn't a problem until the text is > 255 chrs.
> 
> Should I use a container for this or get the handles and do a blockmove or
> is there another way?
> 
> I'm using pg/fb3^5 and my function, is something like this:
> 
> '=== start of function ===
> local fn readScriptsResources
> dim scriptName$, scriptText$
> dim extResfileRef
> dim as int item, count
> 
> extResfileRef = fn openrfperm(gScriptsFileName$, gScriptsVol, _fsRdPerm)
> long if extResFileRef <> -1
> count = fn countStr(_extScriptsID)
> call closeresfile(extResFileRef)
> 
> long if count
> for item = 1 to count
> extResfileRef = fn openrfperm(gScriptsFileName$,gScriptsVol,_fsRdPerm)
> 
> scriptName$ = str#(_extScriptsID,item)
> scriptText$ = str#(_extTextsID,item)        '(1)
> 
> call closeresfile(extResFileRef)
> fn apndElement(_ScriptsListSTR, scriptName$
> fn apndElement(_textsSTR, scriptText$       '(2)
> next item
> end if
> 
> FN viewListItem(_ScriptsList,1)
> 
> end if
> end fn
> '=== end ===
> 
> I've tried substituting the container scriptText$$ at (1) and (2) above but
> I just get garbage in my local str# (_textsSTR). I'm obviously using the
> container incorrectly.
> 
> Also, as you can see, I'm repeatedly opening and closing the ext resource as
> I cycle through the elements. Is this bad practice?
> 
> Any help appreciated.
> 
Bernie,

STR# resources are lists of Pascal Strings all limited to 255
characters maxi.
If you want to steal a STR# resource, you just need to detach it
from the resource map:


myResH = FN Get1Resource(_"STR#",resID)
LONG IF myResH
DetachResource(myResH)'<= it's yours now
END IF

You can then use the STR& function provided by FB^3 to access the
items individually:

content$ = STR&(myResH,thisItem)

After this treatment, myResH is no longer a resource handle, so you
will have to dispose of the memory using the DisposeHandle call
(instead of ReleaseResource) when you are done with it, except if
you store that handle as a STR# resource again later in any resource
file using the AddResource call (in which case don't forget also to
make the resource purgeable).

In rare instances, I've seen a few pieces of code that use the
LoadResource call after having obtained a valid resource handle, but
I don't think it is required here.

The first two bytes of a STR# handle hold the string count, so you
can retrieve the number of items peeking those two bytes, there's no
real need to call a function for that:
strCount = {[myResH]}
or
strCount = myResH..nil%
or
strCount = myResH..0%

-- 

Cheers

Alain

-----------------------------------------------------
FB^3 in Europe:  http://euro.futurebasic.com/
FB II Pouch:     http://www.pixmix.com/FB/outils.html
-----------------------------------------------------