• File access - BAJA

    From Jim King@BNB to ALL on Monday, April 02, 2001 11:12:00
    I want to learn more about how to program computers. Baja seems to be
    simple enough yet complex enough at the same time.

    One aspect that seems inescapable is writing to and reading from a
    file.

    Would someone be willing to write and explain a simple (short) program
    that reads from and writes to a file in Baja?

    Use as many of the file accessing routines found in Baja.

    From Baja Docs:

    FOPEN <int_var> <#> <"str" or str_var> FILE_IO.INC
    args: handle access path_and_filename

    FREAD <int_var> <any_var> [int_var or #]
    args: handle dest length

    FCLOSE <int_var>
    args: handle

    I just don't have the back ground to use this. I know this is a tall
    order and an in depth discussion prolly wont happen, but some sort of
    thread on this MIGHT help and would certainly be welcomed here and
    possibly by others wishing to learn more.


    -
    James King, Coldwater, Mi And that's JIM to you guys!
    Tip of the day: http://setiathome.berkeley.edu

    ---
    ■ Synchronet ■ Bits-N-Bytes One hell of a BBS 24.241.127.99
  • From PistolGrip@WASTELND to Jim King on Monday, April 02, 2001 14:58:00
    RE: File access - BAJA
    BY: Jim King to ALL on Mon Apr 02 2001 06:12 pm

    Would someone be willing to write and explain a simple (short) program
    that reads from and writes to a file in Baja?

    This is an example using a binary file.

    ##### START ############################################################## !include file_io.inc

    # Variable Definitions
    int file_handle
    int number
    int number2

    # Sets a variable to a value
    set number2 25

    # Open file
    fopen file_handle O_RDWR "%n..\baja\somedir\somefile.dat"
    if_false
    print "\r\n\r\nnError Loading File"
    pause
    return
    end_if

    fread file_handle number 4 # reads 4 bytes from file
    fset_pos file_handle 20 SEEK_CUR # sets file position ahead 20 bytes
    fwrite file_handle number2 4 # writes number2 to file with 4 bytes

    fclose file_handle
    ##### END ##############################################################

    Hope this helps.

    PG

    ---
    ■ Synchronet ■ WasteLand BBS ■ telnet://wasteland.darktech.org
  • From Amcleod to Jim King on Monday, April 02, 2001 12:34:02
    RE: File access - BAJA
    BY: Jim King to ALL on Mon Apr 02 2001 06:12 pm

    Would someone be willing to write and explain a simple (short) program
    that reads from and writes to a file in Baja?

    Use as many of the file accessing routines found in Baja.

    From Baja Docs:

    FOPEN <int_var> <#> <"str" or str_var> FILE_IO.INC
    args: handle access path_and_filename

    FREAD <int_var> <any_var> [int_var or #]
    args: handle dest length

    FCLOSE <int_var>
    args: handle

    Essentially you have it covered, except: I'd recommend first a program that opened the file, WROTE some data and then closed it. You decide what goes into the file, what the format is, etc, etc, and you should have a pretty good idea what to expect in that file when the program has executed.

    THEN your SECOND program essentially does the opposite. Open the same file, read back the data and display it to confirm that you i) wrote it correctly; ii) read it back as expected.

    Do you understand the arguments to the commands listed above? ("handle", "access", etc) Or do you need someone to run through them with you first?
  • From Jim King@BNB to PistolGrip on Monday, April 02, 2001 22:22:00
    RE: File access - BAJA
    BY: Jim King to ALL on Mon Apr 02 2001 06:12 pm

    Would someone be willing to write and explain a simple (short) program that reads from and writes to a file in Baja?

    This is an example using a binary file.



    Hope this helps.

    PG

    Thanks PistolGrip, it's a start.

    -
    James King, Coldwater, Mi And that's JIM to you guys!
    Tip of the day: http://setiathome.berkeley.edu
    ---
    ■ OLXWin 1.00b ■ Jesus saves, passes to Moses, up the court, shoots scores
    ■ Synchronet ■ Bits-N-Bytes One hell of a BBS 24.241.127.99
  • From Jim King@BNB to Amcleod on Monday, April 02, 2001 23:17:00
    RE: File access - BAJA
    BY: Jim King to ALL on Mon Apr 02 2001 06:12 pm

    Would someone be willing to write and explain a simple (short) program that reads from and writes to a file in Baja?

    Use as many of the file accessing routines found in Baja.

    From Baja Docs:

    FOPEN <int_var> <#> <"str" or str_var> FILE_IO.INC
    args: handle access path_and_filename

    FREAD <int_var> <any_var> [int_var or #]
    args: handle dest length

    FCLOSE <int_var>
    args: handle

    Essentially you have it covered, except: I'd recommend first a program that A>opened the file, WROTE some data and then closed it. You decide what goes A>into the file, what the format is, etc, etc, and you should have a pretty good
    idea what to expect in that file when the program has executed.

    Exactly, the module would be reading from a structured file (ie a
    config file) to write to another structured file (a batch file)
    In addition to writing to xtrn.cnf (a file of undetermined structure
    :-)

    THEN your SECOND program essentially does the opposite. Open the same file, A>read back the data and display it to confirm that you i) wrote it correctly; A>ii) read it back as expected.

    Do you understand the arguments to the commands listed above? ("handle", A>"access", etc) Or do you need someone to run through them with you first?

    I have all the Baja Docs printed out however as it was in the two
    years of Latin class I find I learn fastest with examples.

    I have the basic premise worked out (in my head) and now I am trying
    to translate it into Bajaese. (and in the process learn about
    programming)

    I'm not saying that Baja is the simplest language, just the one with
    the most programmers I know.

    Enough blather...

    PG's examples are helpful, but as I said writing to xtrn.cnf can be
    wreckless if I'm not careful. So I need more info on set_pos

    It occurred to me to ask the man his self. I got back..
    from \sbbs\cnfs.fmt
    total_xtrns 2
    ... xtrn->sec 2
    ... xtrn->name 41
    ... xtrn->code 9
    ... xtrn->ar 41
    ... xtrn->run_ar 41
    ... xtrn->type 1
    ... xtrn->misc 4
    ... xtrn->event 1
    ... xtrn->cost 4
    ... xtrn->cmd 64
    ... xtrn->clean 64
    ... xtrn->path 64
    ... unused 16

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


    -
    James King, Coldwater, Mi And that's JIM to you guys!
    Tip of the day: http://setiathome.berkeley.edu
    ---
    ■ OLXWin 1.00b ■ Press "+" to see another tagline.
    ■ Synchronet ■ Bits-N-Bytes One hell of a BBS 24.241.127.99
  • From Amcleod to Jim King on Tuesday, April 03, 2001 01:37:32
    RE: File access - BAJA
    BY: Jim King to Amcleod on Tue Apr 03 2001 06:17 am

    PG's examples are helpful, but as I said writing to xtrn.cnf can be wreckless if I'm not careful. So I need more info on set_pos

    It occurred to me to ask the man his self. I got back..
    from \sbbs\cnfs.fmt
    total_xtrns 2
    ... xtrn->sec 2
    ... xtrn->name 41
    ... xtrn->code 9
    ... xtrn->ar 41
    ... xtrn->run_ar 41
    ... xtrn->type 1
    ... xtrn->misc 4
    ... xtrn->event 1
    ... xtrn->cost 4
    ... xtrn->cmd 64
    ... xtrn->clean 64
    ... xtrn->path 64
    ... unused 16

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

    Uhoh!

    You will need more than this to be going on with, I fear! This is only a _portion_ of the definition of XTRN.CNF and you need to have it all.

    Let me get back to you later...
  • From Amcleod to Jim King on Tuesday, April 03, 2001 02:21:34
    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!
  • From Jim King@BNB to Amcleod on Tuesday, April 03, 2001 08:46:00
    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.

    [ - Message Saved - ]

    Study time.


    -
    James King, Coldwater, Mi And that's JIM to you guys!
    Tip of the day: http://setiathome.berkeley.edu
    ---
    ■ OLXWin 1.00b ■ Hello, I am part number
    ■ Synchronet ■ Bits-N-Bytes One hell of a BBS 24.241.127.99