Subj : BAJA Programming 101.2
To   : All
From : PistolGrip
Date : Sun Oct 21 2001 02:59 pm

###############################################################################
### PistolGrip's BAJA Examples  (Version 2)
###############################################################################
###
### This version expands a little on the last, and includes a very crude
### implementation of a WALL or One-Liner module.  This example is primarily
### designed to show an example of both string-based and binary File I/O.
###
###############################################################################
### Note:  This file is an example only, it is by no means supposed to work or
###        or do anything other than hopefully help some people get started
###        with BAJA programming.  This code is not tested or thought-out very
###        well, it is only intended to show some basic concepts and common
###        tasks useful when coding custom modules. I'm sure there is easier
###        and better ways to do some of this with with BAJA, but again, this
###        code has just been 'thrown-together' to try to show some ideas.
###
###        THIS CODE MAY OR MAY NOT COMPILE WITHOUT ERRORS!
###
###############################################################################
### Required Include Files for SBBS Definitions, and our File I/O Functions
###############################################################################
!include sbbsdefs.inc
!include file_io.inc
###############################################################################
### Defines Global or Constant Variables
###############################################################################
!define USER _USERON.ALIAS
!define USER_NUM _USERON.NUMBER
!define MAX_ENTRY_LENGTH 100
!define WALL_VERSION "1.0"
###############################################################################
### Define our Integer variables
###############################################################################
int cfg_file
int dat_file
int access_lvl
int user_number

###############################################################################
### Define our String variables
###############################################################################
str sysop
str user_alias
str entry

###############################################################################
###
### When executing a BAJA module the command line arguments
### (for example "WALL.BIN DISPLAY")
### are available from the default String variable named 'str' in BAJA.
###
###############################################################################
### Copies the command line arguments for use later.
###############################################################################
truncsp str
copy cmd_line str
strupr cmd_line

###############################################################################
### Two things to notice about this next section:
### 1.)  When using Synchronet Command Line Specifiers (ie. "%n") with the
###      BAJA sprintf command, you must use two (2) percent signs (%).  Refer
###      to the BAJA docs for more information on using the sprintf function
###      and certain characters.
### 2.) The double forward slashes "/" in the path.  When you specify paths
###     within a sprintf statement you must use two (2) slashes, either forward
###     (/) or backward (\). We use the "/" to ensure Linux compatibility.
###      Refer to the BAJA docs for more information on using the sprintf
###      function and certain characters.
###

###############################################################################
### Open our modules config file for reading
###############################################################################
sprintf str "%%n..//baja//wall//wall.cfg"

fopen cfg_file O_RDONLY str
  if_false
    print "\r\n\r\nError Loading Configuration File - WALL.CFG"
    return
  end_if

###############################################################################
### Now we'll read our config file one line at a time and set the appropriate
### variables to the correct values for our CONFIG.
###############################################################################
fread_line cfg_file sysop
truncsp sysop

###############################################################################
###
### Note: On this next line read-in, we read in a string value and then
###       convert it to an integer when we use the copy command.
###

fread_line cfg_file str
truncsp str
copy access_lvl str

###############################################################################
### Close our config file  (IMPORTANT)
###############################################################################
fclose cfg_file

###############################################################################
### Note: You can use this technique to fairly easily create complex
###       configuration and customization options of your programs.
###############################################################################

###############################################################################
### Here's were we will see what argurments were passed to module upon exec.
###############################################################################
compare cmd_line "DISPLAY"
if_true
  goto display_mode
end_if

###############################################################################
###############################################################################
###
### Main Entry Point
###
###############################################################################
###############################################################################
compare _USERON.LEVEL access_lvl
 if_less
    print "\r\nSorry, your access is insufficient for this program.\1p"
    return
  end_if

###############################################################################
### Set return point for menu commands (via end_cmd)
###############################################################################
cmd_home

###############################################################################
### Display Header
###############################################################################
cls
printf "EZWALL v%s" WALL_VERSION
print  "\r\n-------------------------------"

###############################################################################
### This next sections checks for existence of menu file and then decides if
### we should display it or the internal menu
###############################################################################
sprintf str "%%n..//baja//wall//menu.*"
chkfile "%s"      ### Note: Here the "%s" is translated into str as sprintf'd
                 ###       above for the chkfile function.

if_false
 print  "\r\n(C)reate a Wall Entry
 compare _USERON.LEVEL 90
 if_greater_or_equal
   print  "\r\n(D)elete an Entry"
 end_if
 print  "\r\n(V)iew the Wall"
 print  "\r\n\r\n(Q)uit"
 print  "\r\n-------------------------------"
else
 printfile str
end_if

###############################################################################
### Now, let's grab some input from the user
###############################################################################
getcmd "CDVQ"

###############################################################################
### Now we execute the desired option.
###############################################################################
cmdkey C
 cls
 printf "EZWALL v%s" WALL_VERSION
 print  "\r\n-------------------------------"


 sprintf str "%%n..//baja//wall//colors.asc"
 chkfile str
 if_true
   printfile str
 end_if

 print  "\r\nEnter your Entry:\r\n"

 getstr entry MAX_ENTRY_LENGTH K_LOWPRIO|K_MSG
 truncsp entry
 compare entry ""
 if_true
   print "\r\n\r\nEntry Aborted!"
   return
 end_if

 cls
 printf "EZWALL v%s" WALL_VERSION
 print  "\r\n-------------------------------"
 print  "\r\n\r\nYou entered the following:"

 printf "\r\n%s" entry

 yes_no "Is this correct"
 if_false
   print "\r\n\r\nEntry Aborted!"
   return
 else

   ### Here's where we open our DATA file to write our new entry
   sprintf str "%%n..//baja//wall//wall.dat"
   fopen dat_file O_CREAT|O_WRONLY str
     if_false
       print "\r\n\r\nError Loading Data File - WALL.DAT"
       cmd_pop
       return
     end_if

   ### Goes to the end of the file
   fset_pos dat_file 0 SEEK_END

   ### Writes the data for our ENTRY to our file
   fwrite dat_file USER_NUM 4
   fwrite dat_file USER 40
   fwrite dat_file entry MAX_ENTRY_LENGTH

   ### Ummm.. yeah don't forget this
   fclose dat_file

   print "\r\n\r\nYour entry has been saved.\r\n"
   pause
 end_if
end_cmd

cmdkey D
 ##########################
 ### Not implemented yet!
 ##########################
end_cmd

cmdkey V
 cls
 printf "EZWALL v%s" WALL_VERSION
 print  "\r\n-------------------------------"

 ### Here's where we open our DATA file
 sprintf str "%%n..//baja//wall//wall.dat"
 fopen dat_file O_RDONLY str
   if_false
     print "\r\n\r\nError Loading Data File - WALL.DAT"
     cmd_pop
     return
   end_if

 ############################################################################
 ### This is the beginning of the loop we use to read in our data from file
 ############################################################################
 :wall_read_loop_view
 feof dat_file
   if_true
      fclose dat_file
      print  "\r\n-------------------------------\r\n"
      pause
      return
    end_if

 ### Read in one record
 fread dat_file user_number 4
 fread dat_file user_alias 40
 fread dat_file entry MAX_ENTRY_LENGTH

 ### Display that record
 printf "\r\n%s" entry
 printf "\r\nEntered By: %s" user_alias

 ### Continue our loop
 goto wall_read_loop_view
end_cmd

cmdkey Q
 cmd_pop
 return
end_cmd

### Fall-through
print "\r\nERROR: Fall through to the end of the Main Section of Module"
cmd_pop
return
###############################################################################
##### End Main Program Section
###############################################################################


###############################################################################
###############################################################################
### This is where we go if called with the DISPLAY parameter
###############################################################################
:display_mode
cls
 printf "EZWALL v%s" WALL_VERSION
 print  "\r\n-------------------------------"

 ### Here's where we open our DATA file
 sprintf str "%%n..//baja//wall//wall.dat"
 fopen dat_file O_RDONLY str
   if_false
     print "\r\n\r\nError Loading Data File - WALL.DAT"
     cmd_pop
     return
   end_if

 ############################################################################
 ### This is the beginning of the loop we use to read in our data from file
 ############################################################################
 :wall_read_loop_display
 feof dat_file
   if_true
      fclose dat_file
      print  "\r\n-------------------------------\r\n"
      pause
      return
    end_if

 ### Read in one record
 fread dat_file user_number 4
 fread dat_file user_alias 40
 fread dat_file entry MAX_ENTRY_LENGTH

 ### Display that record
 printf "\r\n%s" entry
 printf "\r\nEntered By: %s" user_alias

 ### Continue our loop
 goto wall_read_loop_display
### Fall-through
print "\r\nERROR: Fall through to the end of the Display Section of Module"
return
###############################################################################
###############################################################################
### End of Sample BAJA Source
###############################################################################

---
� Synchronet � WasteLand BBS � telnet://wasteland.darktech.org