[futurebasic] STR#...again

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 1999 : Group Archive : Group : All Groups

From: "Terrald J. Smith, M.D." <tjsmith@...>
Date: Sun, 16 May 1999 07:40:17 -0500
Jay, I really appreciate your help.  But I still seem to be having, causing,
something I do not understand.

The way I am setting up the STR# the first time is in a LOCAL FN.  The DIM
48 DIAGNOSIS$(16000) is global and yes...strings are always 48 chars long.
This is the code.  When I check it out with the code or use PG to look at
STR# it appears to be perfect.  Generally it does not usually have to be
used (unless the data has been modified).

LONG IF itemnumber$<>MODIFIED$
  resHndl& = FN NEWHANDLE _clear(2)
  OSErr  = FN HNOPURGE(resHndl&):gOffSet&=0
  itemnumber=LOF(FILENUMBER,48):Filesize&=LOF(FILENUMBER,1)
  StrHndl&=FN NEWHANDLE(Filesize&)
  OSErr=FN HNOPURGE(StrHndl&)
  READ FILE#FILENUMBER,[StrHndl&],Filesize&
  FOR Myloop=1 TO itemnumber
    POKE @DIAGNOSIS$(Myloop),48             'Set length byte
    BLOCKMOVE [StrHndl&]+gOffSet&,@DIAGNOSIS$(Myloop)+1,48'move string
    gOffSet& = gOffSet& + 48                'get offset of next string
    DEF APNDSTR(DIAGNOSIS$(Myloop),resHndl&)
  NEXT
  FN repElement(2,_filedataSTR,itemnumber$)
  FN pGreplaceRes(resHndl&,_"STR#",_diagnosislistSTR,"DIAGNOSISLIST")
  OSErr  = FN HPURGE(resHndl&)
  OSErr  = FN HPURGE(StrHndl&)
END IF

If (and this is the usual case), no modification is done, the next code just
gets the DIAGNOSIS$(16000).  It also seems to work fine.

  itemnumber=FN countStr(_diagnosislistSTR)
LONG IF itemnumber>0
  StrHndl& = FN GETRESOURCE(_"STR#",_diagnosislistSTR)
  OSErr  = FN HNOPURGE(StrHndl&):gOffSet&=3
  FOR Myloop=1 TO itemnumber
    POKE @DIAGNOSIS$(Myloop),48           'Set length byte
    BLOCKMOVE [StrHndl&]+gOffSet&,@DIAGNOSIS$(Myloop)+1,49'move string
    gOffSet& = gOffSet& + 49              'get offset of next string
  NEXT
  OSErr  = FN HPURGE(StrHndl&)
END IF

What dings me between the ears is why gOffSet&=3 instead of gOffSet&=0 or
gOffSet&=2 is needed (although I assume it has to be because the first 2
bytes are giving the number of strings).  And why gOffSet& = gOffSet& + 49
instead of gOffSet& = gOffSet& + 48 works.  The original suggestion I was
given was gOffSet&=0 and then gOffSet& = gOffSet& + 48, but I had to do the
above to get the correct use.

What also bothers me is that the suggestion you gave me here seems
COMPLETELY good and logical.  But it does not work.  The offsets are way
off.

I know I have the code above that works but I feel uncomfortable about why
your logical code doesn't work for this.  Do you think I may be storing the
original STR# the wrong way?  Do you think I should just quit whining and
stay with the code that seems to work?

Your suggestion that I like is:

My understanding of STR# resources is that their strings already have
length bytes--so there's no need to add them. You do need to initialize
your offset to 2 to skip the word that tells how many strings are in the
STR#. If the strings are 48 chars long, then you have to add the length
byte and a pad byte to your offset number, making it 50. However, if ALL
the strings are the same length (48), there are much easier solutions:

DIM 48 DIAGNOSIS$(_numberOfStrings)      'FB will add length & pad
'Copy entire array in 1 fell swoop
StrHndl& = FN GETRESOURCE(_"STR#",_diagnosislistSTR)
LONG IF StrHndl&
  BLOCKMOVE [StrHndl&]+2,@DIAGNOSIS$(0),FN GETHANDLESIZE(StrHndl&)-2
END IF

Thanks alot for the help, Terrald