• BAJA Programming 101.2

    From PistolGrip@VERT/WASTELND to All on Sun Oct 21 14:59:00 2001
    ###############################################################################ã### 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 ã
  • From Digital Man@VERT to PistolGrip on Sun Oct 21 17:33:18 2001
    RE: BAJA Programming 101.2ãBY: PistolGrip to All on Sun Oct 21 2001 09:59 pmãã > ############################################################################ã > ### 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 (%). Refeã > ### 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 forwããAre you sure about this double forward-slash thing? You have to useãdouble-backslash to represent a single backslash character (because the Cãprogramming language uses backslash a special 'escape' character - e.g. '\r'ãindicates carriage return), but the same is not true of the forward slashãcharacter.ããFor example, print "\\test\\" would display "\test\"ã print "//test//" would display "//test//"ããJust another incentive to use forward slashes instead of backslashes inãpathnames. :-)ãã-Robã---ã þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.netã
  • From PistolGrip@VERT/WASTELND to Digital Man on Sun Oct 21 22:03:00 2001
    RE: BAJA Programming 101.2ãBY: Digital Man to PistolGrip on Mon Oct 22 2001 12:33 amãã > > ### 2.) The double forward slashes "/" in the path. When you specify pãatã > > ### within a sprintf statement you must use two (2) slashes, either fã > ã > Are you sure about this double forward-slash thing? You have to useã > double-backslash to represent a single backslash character (because the Cã > programming language uses backslash a special 'escape' character - e.g. '\r'ã > indicates carriage return), but the same is not true of the forward slashã > character.ã > ã > For example, print "\\test\\" would display "\test\"ã > print "//test//" would display "//test//"ã > ã > Just another incentive to use forward slashes instead of backslashes inã > pathnames. :-)ããHrrm... I will have to look into this further. I was going by what I thoughtãI remembered back when converting modules for linux compatibility. I thoughtãthe single forward slash did not work when used in a sprintf, and my code fromãthat time is and has been working with two forward slashes. ããBut, what you say makes sense. I would think it would only apply to theãbackslash in retrospect. I will do some tests here with this to see what turnsãup.ããPGãã---ã þ Synchronet þ WasteLand BBS þ telnet://wasteland-bbs.com ã