Subj : File access - BAJA
To   : Jim King
From : Amcleod
Date : Tue Apr 03 2001 02:21 am

RE: File access - BAJA
BY: Jim King to Amcleod on Tue Apr 03 2001 06:17 am

> Knowing all this... how would I translate ... say "xtrn->code 9" into a
> fread statement? Using "%!\..\crtl\xtrn.cnf" as the source.

OK Jim, here's an example of accessing XTRN.CNF via BAJA.  I don't know if it
will lead you where you want to go or not.  But either way, here it is, RAABA
and not particularly  well tested

# We need the standard definitions in "FILE_IO.INC" so...
!include FILE_IO.INC

# define some variables we will need later
int FHnd Offs XProgCt XP_Sec
str FName XP_COde XP_Name XP_Cmd

# define name of config file (use a copy while debugging!)
set FName "/sbbs/ctrl/x_xtrn.cnf"

# open the file for reading, and get back a unique integer "handle" that
# allows us to refer to this open file in all subsequent operations.  Bomb
# if file fails to open
fopen FHnd O_RDONLY FName
if_false
   print "fopen() failed!\r\n";
   pause
   exit
end_if

# Refer to XTRN.CNF section of CNFS.FMT for details on file layout!
# First two bytes in file is "total_swaps", read this (no error checking
# from now on; duplicate if_false/end_if from above and change as needed).
fread FHnd Offs 2

# "total_swaps" is followed by that many "swap_cmd" at 64 bytes each,
# so compute how many bytes must be skipped to come to "External Editors"
# and reposition file pointer to that offset.
mul Offs 64
fset_pos FHnd Offs SEEK_CUR

# Now read "total_xedits".  Skip past 41+9+64+64+4+41+16 = 239 times
# "total_xedits" bytes to get to "External Program Section"
fread FHnd Offs 2
mul Offs 239
fset_pos FHnd Offs SEEK_CUR

# Find out how many "total_xtrnsecs" and compute offset to "External
# Program" (41+9+41+16 = 107)
fread FHnd Offs 2
mul Offs 107
fset_pos FHnd Offs SEEK_CUR

# Phew!  Finally pointing at "External Programs".  How many in total?
fread FHnd XProgCt 2

# This is the top of a loop that will iterate over the list of "External
# Programs", processing each one as we go.
:NextXP

   # If there are ZERO (or less ?!?) "External Programs" left, we
   # are finished processing the list and wanna call it quits.
   compare XProgCt 0
   if_less_or_equal
       goto WereDone
   end_if

   # Since we aren't done, print some basic info on the next
   # "External Program".....

   # Read some data...
   fread FHnd XP_Sec 2
   fread FHnd XP_Name 41
   fread FHnd XP_Code 9

   # Skip 41+41+1+4+1+4 = 92 bytes we're not interested in
   fset_pos FHnd 92 SEEK_CUR

   # Read some more data
   fread FHnd XP_Cmd 64

   # Skip 64+64+16 = 144 remaining (unwanted) bytes for this
   # "External Program"
   fset_pos FHnd 144 SEEK_CUR

   # Print out the info we picked up:
   printf "%5ld -- %-8s -- %-40s\r\n" XP_Sec XP_Code XP_Name
   printf "         %-64s\r\n\r\n" XP_Cmd

   # Reduce count by one and loooooop!
   sub XProgCt 1
   goto NextXP

# OK, this is where we come to when there are no more "External Programs"
# for us to process...
:WereDone
print "We're done!\r\n"

# Ok, we're done, so courteously close file before exiting
fclose FHnd

# Sayonara!
exit

There you have it; RAABA as promised!  Read it through step by step referring
to the CNFS.FMT file (XTRN.CNF section) and you should be able to easily see
what is happening.  As usual, programming turns out to be a boring, ste-by-step
exercise.

To make _changes_ to the file, you essentially follow a similar process to
this:  Carefully compute the location of the byte(s) you want to change,
possibly reading data and computing offsets to get there.  When your file
pointer is aimed exactly at where you want the data put, then you fwrite() the
exact number of bytes into the file, then reposition the file pointer for the
next write.  You have to open the file Read/Write instead of Read-Only, of
course.  Make _sure_ to use a copy of the file when debugging to avoid hosing
your valuable config data!

---
� Synchronet � Vertrauen � Home of Synchronet � telnet://vert.synchro.net