         Better SWITCHing
         By R. Patrick Bill, James A. Pope
         As Appeared in Reference(Clipper)
          August 1988, p. 13

              We have been working on a number of development
         projects that are very memory hungry. While the programs do
         not do anything very elaborate, we must manipulate a number
         of very large arrays, and have run out of memory fairly
         often. After learning the tricks of coping with Clipper's
         method (or non-method) of "garbage collection", learning the
         ins and outs of overlay structure, and the use of the SET
         CLIPPER = command, we discovered "SWITCH.EXE". Our elation
         soon turned to dismay. SWITCH.EXE requires the use of 50K of
         precious memory! Why, we do not know, but such is life.
              What follows is a very simple function that builds and
         manipulates a batch file. The concept is simple: There are
         two batch files - START.BAT and SWITCH1.BAT, along with one
         function SWITCHFILE(). START.BAT contains three lines:

              ECHO OFF
              MASTPROG
              SWITCH1

              MASTPROG is the name of your master or start-up program,
         it will usually contain some kind of main menu. SWITCH1 is
         the name of the batch file that is created/maintained by the
         function SWITCHFILE(). ECHO OFF keeps the garbage off the
         screen while you change programs.

         SETUP

              Create a batch file (see START.BAT above); it must
         contain the three lines described. Modify line 2 to contain
         the name of your main EXE program. If you do not choose to
         call the "starting" batch file, START.BAT, change the name in
         the function to match (see source code listing below).

         FUNCTION SWITCHFILE
         * Syntax . . . . : SWITCHFILE(FILETORUN)
         * Notes  . . . . : If PARAMETER FILETORUN is empty, the
         *                  function will cause the overall program to
         *                  exit and end. Otherwise a batch file will
         *                  be created that will contain the name of
         *                  FILETORUN followed by the command to run
         *                  START.BAT again.

         PARAMETERS FILETORUN

         IF FILE("SWITCH1.BAT")
              ERASE SWITCH1.BAT
         ENDIF

         OUTFILE = FCREATE("SWITCH1.BAT")

         BUFFER = SPACE(79)
         IF !EMPTY(FILETORUN)
              BUFFER = ALLTRIM(FILETORUN) + CHR(13) + CHR(10)
              LENX = LEN(BUFFER)
              FWRITE(OUTFILE, BUFFER, LENX)
              BUFFER = SPACE(79)

              BUFFER = "START" + CHR(13) + CHR(10)    && If batch file is not
                                                      && to be named "START.BAT"
              LENX = LEN(BUFFER)                      && change the name in
              FWRITE(OUTFILE, BUFFER, LENX)           && function to match
              FCLOSE(OUTFILE)
              QUIT
         ELSE
              BUFFER = "CLS" + CHR(13) + CHR(10)
              LENX = LEN(BUFFER)
              FWRITE(OUTFILE, BUFFER, LENX)
              FCLOSE(OUTFILE)
              QUIT
         ENDIF

         * End of Function SWITCHFILE


         PROGRAM FLOW

              Most programs have a main menu that calls other menus,
         programs, etc. If you have run out of memory, and must split
         up your program into modules. Insert SWITCHFILE() into your
         main menu file and call it using the name of an EXE of COM as
         the parameter; this name is the name of the program you wish
         to run next. to QUIT, call SWITCHFILE() without a parameter.
         SWITCHFILE() will build a batch file called SWITCH1.BAT. It
         will contain the name of the next program, and the name of
         START.BAT as the second line. The function will then QUIT.
              Since you started your program with a batch file, DOS
         will return you to that file to look for the next command.
         The next command will be the name of the file you created,
         SWITCH1.BAT (the .BAT is not used). DOS will run SWITCH1.BAT.
         When this occurs, SWITCH1.BAT will contain the next program's
         name as the first command. This will cause that program to
         start.
              When you are through with the second program, a normal
         QUIT will return you to DOS, giving control to SWITCH1.BAT
         again. This time SWITCH1.BAT will execute the command START
         which will loop you back through the main program again.
              This method of switching takes up no memory, other than
         the function space, and very little disk space. the function
         could probably be expanded to allow you to chain programs
         together rather than returning to the main segment each time.
         One thin to remember: a DOS batch file, if called from
         another batch file, will not return to the file it was called
         from automatically. It must contain a command to invoke the
         next file as the last line or it will terminate and return
         you to the DOS level.
         ABOUT THE AUTHORS

              R. Patrick Bill and James A. Pope are the principles of
         Capital Software Development Corp in Denver, CO. They are
         involved in the development of software for the fixed-income
         securities investing community. They may be reached at 50 S
         Steele St. Suite 222, Denver CO 80209.
