/* View.fil */
/* ARexx Macro to enable Viewing of records in a Filer database file. */

Address 'AFiler'  /* Message Port Name of the Filer ARexx Port. */

Options results   /* We will be getting feedback from the host program. */

 'filename'   /* Request the name of the current file. */

If RC ~= 0 Then Exit RC  /* Exit returning error code */
Current_Name = result
If Current_Name = '' Then Exit 5  /* Exit with a warning. */

 'recordstruct'  /* Request the record structure used by the current file. */

If RC ~= 0 Then Exit RC
Record_Struct = result
If Record_Struct = '' Then Exit 5  /* Exit with a warning. */

Total_Fields = Words(Record_Struct)/3  /* three elements per field. */

tempstr = Record_Struct /* Parse using a temporary string variable. */
keycount = 0 /* Counter for the number of key fields in definition. */

/* Break the record structure down into its individual components and */
/* determine the number of key fields.  Also, record the position of*/
/* each key field. */
Do index = 0 to Total_Fields - 1
   Parse VAR tempstr Name.index Type.index Size.index tempstr
   If Type.index ~= 0 Then Iterate /* skip if not a key field */
   KeyPsn.keycount = index
   keycount = keycount + 1
End

If keycount = 0 Then Exit 5 /* Can't search a file w/o a key field */

 'recordsize' /* Request the record size. */

If RC ~= 0 Then Exit RC
Size = result
If Size = '' Then Exit 5  /* Exit with a warning. */

Record_Size = Value(Size) /* Convert the string value to a numeric value. */

If ~ Open('DataFile',Current_Name, 'R') Then Exit 5

/* Open an input channel */
bit_bucket = Open('KeyConsole', 'CON:135/90/350/40/KeyString')
bit_bucket = WriteLn('KeyConsole', ' ')
bit_bucket = WriteCh('KeyConsole','Enter search key: ')
Search_String = ReadLn('KeyConsole') /* Get the key to search for. */
bit_bucket = Close('KeyConsole')

If Search_String = '' Then Exit 5 /* Can't continue w/o a search string. */

/* Skip the record definition */
bit_bucket = ReadCh('DataFile', (30 * Total_Fields) + 4)

Do While ~ EOF('DataFile')
   Current_Record = ReadCh('DataFile', Record_Size)
   tempstr = Current_Record /* Parse using a temporary variable. */

   /* The record fields are stored in reverse order and need to be */
   /* parsed in the same way. */
   index = Total_Fields - 1
   Do loop_count = 0 to Total_Fields - 1
      position = Size.index + 1
      Parse VAR tempstr 1 Field.index +position tempstr
      bit_bucket = Strip(Field.index)
      index = index - 1
   End

   Do index = 0 to keycount - 1
      key = KeyPsn.index
      Total_Keys = Words(Field.key)
      tempstr = Field.key
      Do index2 = 0 to Total_Keys - 1
         Parse VAR tempstr tempkey tempstr
         If Search_String = tempkey Then show(Current_Record)
      end
   end
end

bit_bucket = Close('DataFile')

exit

show:
arg record
longest = 0

Do index3 = 0 to Total_Fields - 1
   If Size.index3 > longest Then longest = Size.index3
End

length = (longest + 14 + 3) * 8
height = (Total_Fields + 4) * 10

Ycenter = (200 - height)/2
Xcenter = (640 - length)/2

bit_bucket = Open('ShowRecord', 'CON:'Xcenter'/'Ycenter'/'length'/'height'/Show Record')

Do index3 = 0 to Total_Fields - 1
   bit_bucket = WriteLn('ShowRecord', Name.index3 ' ' Field.index3)
End

bit_bucket = WriteCh('ShowRecord', 'Press any key to continue...')
bit_bucket = ReadCh('ShowRecord', 1)

return
