UPDATING THE OPEN SOURCE EDITOR

ADDING INCLUDE FILES

© 2003, Alyce Watson


NewsLetter TOC

Newsletter Home

Blast

MDI

OS Editor

LBW Review

Fast Data

SQLite

Tsunami

Begin Prog II

Locate/LBCard

NOTE: The Open Source Editor is a learning lab for the newsletter. It has evolved over many issues and many years. Please see previous issues of this newsletter for explanations of all of the features of the editor.

What are "include" files?

From time to time, Liberty BASIC programmers have requested a compiler directive for "include" files. This would allow us to put an "include" command in our code that directed Liberty BASIC to add certain files to our source code at compile time.


Why would we want "include" files?

There isn't much reason to add this functionality for small programs. Large programs that are thousands of lines long can be difficult to navigate. Lots of scrolling! Placing significant portions of code in separate files would allow us to navigate the main program source much more easily.


Portability and Reusability

The ability to have separate files allows us to create libraries of functions and routines that we can use in many of our programs without doing a lot of copy/pasting or rewriting.


ADDING "INCLUDE" FILES TO THE OPEN SOURCE EDITOR

We can add this feature ourselves, by adding it to the Open Source Editor. We can use the Open Source Editor instead of the Liberty BASIC editor to write, modify, run and print our code.

We'll make some rules for our "include" functionality

  1. "Include" files must reside in the same folder as the main program file.
  2. The directive will be 'include filename.bas
  3. The first included file must be specified on the very first line of code.
  4. Each subsequent included file must follow on its own line.
  5. We must check for included files before running a program.
  6. We must allow the user to save all files in a single source code file.


For rules 1 - 4:

The top of the code would look like this, if there were two included files, called "first.bas" and "second.bas". (For some reason, I am now thinking of baseball??)

'include first.bas
'include second.bas


Rule 5 states, "We must check for included files before running a program." The first step is to get the text from the first line in the texteditor and see if it contains the string "'include". If it doesn't contain that string of text, then we simply run the program as we have always done. If it does contain the string, we set up a temporary filename for the project, and then concatenate the files into one. Here is the setup:

    'first, check for included files:
    #1.t "!Line 1 include$"
    If Instr(include$,"'include",1)<>0 Then
        project$=filePath$+"tempfile.bas"
        GoSub [saveProject]     'save primary file with filename project$
        GoSub [addToProject]    'add included files
        tempfilename$=project$  'temp file name to run

We must first save the contents of the texteditor, which we do in the [saveProject] sub:

    Print #1.t, "!contents? saveit$";

    Open project$ For Output As #file
    Print #file, saveit$
    Close #file

We then check for addional included files. We must check to make sure they are valid files. The path will be the same as the project's main file and the filename will be the part of the line after 'include. If the file exists on disk, we will open it and retrieve its contents into a string variable. After closing the file, we open our temporary project file for APPEND. This allows us to add data to the end of the file without losing the data that already exists. We check each subsequent line in the texteditor and repeat this routine, until we come to a line that doesn't have an 'include directive.

    i=1 'set counter var to first line
    While i>0
        #1.t "!Line ";i;" include$"
        If Instr(include$,"'include",1)>0 Then
            'get filename, after include directive:
            includeFile$=Right$(include$,Len(include$)-9)
            includeFile$=Trim$(includeFile$)    'trim blank spaces

            i=i+1   'increment line number to check for includes

            'see if such a file exists, abort if not
            If FileExist(DefaultDir$,includeFile$)<1 Then
                i=0
                Exit While
            End If

            'get contents of include file into var saveit$
            Open DefaultDir$ + "\" + includeFile$ For Input As #f
            saveit$=Input$(#f, Lof(#f))
            Close #f

            'add contents of include file to disk file
            Open project$ For Append As #f
            Print #f, saveit$
            Close #f
        Else
            i=0
            Exit While
        End If
    Wend
    Return

Once we have the file saved to disk, we can RUN it.

    Run libertyexe$+" -R -A  "+tempfilename$


Rule 6 states, "We must allow the user to save all files in a single source code file." This will be easy! We've already written the code to save the project as a single file in order to run it. We can reuse those routines here. The only difference is in the filename. When we saved it to disk to run it, we gave it a temporary filename. Now we will allow the user to choose a name for saving the project. We'll include a filedialog, and then we must check to see if there are included files. If there are not, we'll instruct the user to save his file in the usual way. If there are included files, we'll call our [saveProject] and [addToProject] subs to save the file to disk.

    #1.t "!Line 1 include$"
    If Instr(include$,"'include",1)=0 Then
        Notice "This file does not have included files.  Please save using the File->Save option."
        Wait
    End If

    FileDialog "Save file as...",filePath$+"*.bas;*.lba",project$ 
      If project$="" Then
        Notice "You must choose a file name."
        Wait
      End If

    GoSub [saveProject]  'save primary file with filename project$
    GoSub [addToProject] 'add included files
    Notice "Project saved as "+ project$
    Wait


CONSTRUCTING FILES FOR INCLUDE

Ah, this is easily the subject for a long article in itself on writing encapsulated code. Maybe in a future issue??


THE CODE

What follows here are most of the routines needed to add 'include files to the Open Source Editor. For the full program, please refer to the zip archive of this issue for "open32_06.bas"



[project]
    #1.t "!Line 1 include$"
    If Instr(include$,"'include",1)=0 Then
        Notice "This file does not have included files.  Please save using the File->Save option."
        Wait
    End If

    FileDialog "Save file as...",filePath$+"*.bas;*.lba",project$ 
      If project$="" Then
        Notice "You must choose a file name."
        Wait
      End If

    GoSub [saveProject]  'save primary file with filename project$
    GoSub [addToProject] 'add included files
    Notice "Project saved as "+ project$
    Wait


[saveProject] 'saves contents of editor as project$
    Cursor hourglass
    Print #1.t, "!contents? saveit$";

    Open project$ For Output As #file
    Print #file, saveit$
    Close #file
    Cursor normal
    Return


[addToProject]  'appends files to project$
                'Add included files to disk file.
                'Included files must be in same folder
                'as main project file.
    i=1 'set counter var to first line
    While i>0
        #1.t "!Line ";i;" include$"
        If Instr(include$,"'include",1)>0 Then
            'get filename, after include directive:
            includeFile$=Right$(include$,Len(include$)-9)
            includeFile$=Trim$(includeFile$)    'trim blank spaces

            i=i+1   'increment line number to check for includes

            'see if such a file exists, abort if not
            If FileExist(DefaultDir$,includeFile$)<1 Then
                i=0
                Exit While
            End If

            'get contents of include file into var saveit$
            Open DefaultDir$ + "\" + includeFile$ For Input As #f
            saveit$=Input$(#f, Lof(#f))
            Close #f

            'add contents of include file to disk file
            Open project$ For Append As #f
            Print #f, saveit$
            Close #f
        Else
            i=0
            Exit While
        End If
    Wend
    Return



[readyRun]'** MAKE A TEMP FILE TO RUN IN LIBERTY BASIC
    '** GET CURRENT TEXTEDIT ORIGIN
    Print #1.t, "!origin? rowVar,columnVar";

    'first, check for included files:
    #1.t "!Line 1 include$"
    If Instr(include$,"'include",1)<>0 Then
        project$=filePath$+"tempfile.bas"
        GoSub [saveProject]     'save primary file with filename project$
        GoSub [addToProject]    'add included files
        tempfilename$=project$  'temp file name to run
    Else
         '** GET CONTENTS OF TEXTEDITOR AND SAVE TO TEMPFILE
        If tempfilename$<>"" Then Kill tempfilename$
        Print #1.t, "!contents? saveit$"
        tempfilename$=filePath$+"tempfile.bas"
        Open tempfilename$ For Output As #temp
        Print #temp, saveit$
        Close #temp
    End If

    If libertyexe$="" Then GoSub [findLiberty]
    Return




NewsLetter TOC

Newsletter Home

Blast

MDI

OS Editor

LBW Review

Fast Data

SQLite

Tsunami

Begin Prog II

Locate/LBCard