[futurebasic] [FB2] XREF Array of Records (lengthy)

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

From: Sean -G3- <mclaugh@...>
Date: Tue, 19 Oct 99 20:21:10 -0500
I am making a grading program for schools. I am currently working on 
importing files from other grading programs. I have successfully loaded 
the file and filtered the handle. If I do nothing more, and display the 
handle in an edit field, I get a nice list of student IDs and names. Each 
student gets his/her own line, and there is one space between the student 
ID and the name. Heres an example:

01056 Smith Bob
03138 Doe John

This information is stored in a handle, gTextH&. This handle has two 
bytes reserved for the handle length, so I can put it into an edit field.

Now, I want to load these into an array of records. Each student is given 
one 52-byte record. These contain a two-byte integer for their ID, and a 
50-byte string for their name:

DIM RECORD studentInfo
  DIM studentId%
  DIM 50 studentName$
DIM END RECORD.studentInfo

Instead of making a standard, fixed-length, defined at 
compile-time-array, I dynamically create and resize my array. I have a 
pretty firm grasp of the concept, but I have no hands-on experience. 
Heres how I XREF my array:

XREF@ gFormattedInfo.studentInfo(1000)'Could be any number, no mem 
allocated

The following function takes the contents of a handle (gTextH&), and 
loads it into the array. It adds one more record (52 bytes) to the handle 
during each iteration of the loop.

CLEAR LOCAL
LOCAL FN formatHandle
  DIM offSet&, lineStart&, textL%
  DIM arraySize%, arrayDepth%
  DIM stringStart&, idPointer&
  DIM deReffed&
  DIM osErr%
  DIM gTextState%, gFormattedState%
  DIM 1 char$
  DIM 1 rep$
  DIM copySize%
  
  'space = 9, return = 13
  
  'Initialization
  'offSet& = gTextH&
  offSet& = 2 'This was originally offSet& = gTextH&
  
  textL% = FN GETHANDLESIZE(gTextH&)
  rep$ = ""
  
  'Dynamic Allocation
  gFormattedInfo.Top& = FN NEWHANDLE(4)
  arraySize% = 0            'It will be equal to 4 once the loop starts
  arrayDepth% = 0
  
  'Lock the array's handle so data can be copied
  gTextState% = FN HGETSTATE(gTextH&)
  osErr% = FN HLOCK(gTextH&)
  
  DO
    'Make one more level in the array
    arraySize% = arraySize% + _studentInfo
    osErr% = FN SETHANDLESIZE(gFormattedInfo.Top&, arraySize%)
    INC(arrayDepth%)
    
    'Lock the array's handle so data can be copied
    gFormattedState% = FN HGETSTATE(gFormattedInfo.Top&)
    osErr% = FN HLOCK(gFormattedInfo.Top&)
    
    'Find the student ID
    idPointer& = VARPTR(gFormattedInfo.studentID%(arrayDepth%))
    'blockmove source, destination, amount
    BLOCKMOVE (gTextH& + offSet&), idPointer&, 2
    
    'Find the student name
    char$ = CHR$(9)
    'It locks up right here
    offSet& = FN Mung&(char$,rep$,gTextH&,offSet&,_search)
    lineStart& = offSet&
    INC(offSet&)
    offSet& = FN Mung&(char$,rep$,gTextH&,offSet&,_search)
    
    stringStart& = VARPTR(gFormattedInfo.studentName$(arrayDepth%))
    copySize% = (offSet& - lineStart&)
    POKE stringStart&, copySize%
    BLOCKMOVE lineStart&,(stringStart& + 1), copySize%
    
    'Move offSet& to next line
    char$ = CHR$(13)
    offSet& = FN Mung&(char$,rep$,gTextH&,offSet&,_search)
    INC(offSet&)
    
    'Return the array's handle to its original state
    osErr% = FN HSETSTATE(gFormattedInfo.Top&, gFormattedState%)
    
  UNTIL (offSet& <= 0) OR (offSet& >= textL%)
  
  'Return the array's handle to its original state
  osErr% = FN HSETSTATE(gTextH&, gTextState%)
  
  'PRINT gFormattedInfo.studentName$(3)
  'DELAY 2000
  
END FN

It freezes when calling FN Mung& on the first time through the loop. I 
know that function Mung& is not directly causing the problem, because I 
use it in the filter section of my program. I have been looking over this 
code for quite some time, but I haven't fixed it. I thought I'd try the 
list before spending another couple days on it.

I used to get an unexpected quit of type -1 (memory error). That was when 
I had
offSet& = gTextH&

Now that I changed it to
offSet& = 2

I get a box similar to the interrupt-button box. Absolutely frozen. 
Anyone know what causes this? A misunderstanding of what is going on...or 
a glaring typo?

Thanks,
Sean