April 18, 2024, 06:20:40 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


How to use Projects and Combining/using several .eba files

Started by AdrianFox, November 12, 2008, 11:15:16 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

AdrianFox

 >:( This is probably a stupid question from someone who just knocks up the odd program to use with my French students learning English.  I've found my way round the Users' Guide and Help sufficiently to produce some very useful simple  programs but for anything more complex I need to split the program into modules.

Try as I might, I can't find sufficient help in the Users' Guide to understand how to do this, even though I've read the 'Projects' section twenty times!  For example, how would you simply call a second window with the code for this in a separate single source file? I've got two files listed in the project window.  I put $main where the program execution begins, but I am baffled about how do to call the second source file or a subroutine contained therein?  I tried coding with Global sub etc but just got masses or error statements at compile.

If anyone could send me an example of how to code this or tell me which commands/sections in the Users Guide I should look at I'd be very grateful.   I HAVE tried but perhaps I am very thick or missing something obvious.

I do find Emergence Basic a brilliant compiler as it produces such small stand-alone applications and although it's initially more difficult to learn than Liberty Basic, for example, it's infinitely superior.  I'm sure if someone could produce a simpler step-by-step guide for EB that goes further than just displaying windows and dialogs, that would assist beginners considerably.  The current Users' Guide is great as far as it goes, but then you are rather left in deep water if you are a beginner (or getting old like me!).  As for the example code under the 'projects' directory, I don't find many of them much use for the things which I need to do although some of the examples posted in the forums have been very useful.   Why are there no 'Project' file examples?

Regards and good luck with the future development of EB.
Adrian Fox

Hootie

Here is an example of an eba file that is used by other eba files in a project:


'****************************************************************************************
'*  amLog.eba                                                                           *
'*                                                                                      *
'*  Author: Mark O'Hara                                                                 *
'*  Description: Logging messages to a file                                             *
'*                                                                                      *
'*  This is a message and error logger that writes messages out to a log file.  The     *
'*  messages have different levels of severity:                                         *
'*                                                                                      *
'*                     Message = informational message                                  *
'*                     Warning = Possible error but won't end program                   *
'*                     Error = Major error, will stop execution of program              *
'*                                                                                      *
'*  Message strings can be formatted with the Ebasic USING command.  The messages are   *
'*  not buffered and are immediately written to the log file.  This is to preserve the  *
'*  messages in case of program crash.                                                  *
'*                                                                                      *
'*                                                                                      *
'*                                                                                      *
'*  Copyright (C) 2008,2009 Cheyenne Cloud, LLC. All Rights Reserved.                   *
'****************************************************************************************

'=================================================================================
' Includes
'=================================================================================
$INCLUDE "windowssdk.inc"
$INCLUDE ".\main\amShutdownStack.inc"



'=================================================================================
' Enums
'=================================================================================
ENUM ErrorLevel
AMLOG_MESSAGE
    AMLOG_WARNING
    AMLOG_ERROR
ENDENUM



'=================================================================================
' Source Global Variables
'=================================================================================
FILE fp = 0
STRING DefaultName = "log.log"



'=================================================================================
' amLogInit()
'
' This function creates the log file and opens it.
'
' Parameters:  logname - Desired log file name string
'
' Returns: Nothing
'     
'=================================================================================
GLOBAL SUB amLogInit(STRING logname)

IF (OPENFILE(fp, logname, "W")) <> 0
END
ENDIF

LogWrite(AMLOG_MESSAGE, "***** Log System Initializing *****")
amShutdownRegister(&amLogShutdown)

ENDSUB



'=================================================================================
' amLogShutdown()
'
' This function closes the log file.
'
' Parameters:  None
'
' Returns: Nothing
'     
'=================================================================================
GLOBAL SUB amLogShutdown()

    IF fp <> 0
LogWrite(AMLOG_MESSAGE, "***** Log System shutting down *****")
CLOSEFILE fp
ENDIF

    fp = 0

ENDSUB



'=================================================================================
' amLogError()
'
' Log Error level messages. End program.
'
' Parameters:  msg - Error message string
'
' Returns: Nothing
'     
'=================================================================================
GLOBAL SUB amLogError(STRING msg)

    ' Print message to log file
    LogWrite(AMLOG_ERROR, msg)

' Shutdown
    amShutdownAll()

    ' Abort program
    END

ENDSUB



'=================================================================================
' amLogWarning()
'
' Log Warning level messages.
'
' Parameters:  msg - Warning message string
'
' Returns: Nothing
'     
'=================================================================================
GLOBAL SUB amLogWarning(STRING msg)

    ' Print message to log file
    LogWrite(AMLOG_WARNING, msg)

ENDSUB



'=================================================================================
' amLogMessage()
'
' Log Informational level messages.
'
' Parameters:  msg - Message string
'
' Returns: Nothing
'     
'=================================================================================
GLOBAL SUB amLogMessage(STRING msg)

    ' Print message to log file
    LogWrite(AMLOG_MESSAGE, msg)

ENDSUB



'=================================================================================
' LogWrite()
'
' Write messages to file.
'
' Parameters:  level - Enum level of message
'              msg   - Message string
'
' Returns: Nothing
'     
'=================================================================================
SUB LogWrite(ErrorLevel level, STRING msg)

STRING msgtype
STRING finalmsg


    ' If file doesn't exist, create it
IF fp = 0
amLogInit(DefaultName)
ENDIF

    ' Determine message level label for message
    if level = AMLOG_MESSAGE
        msgtype = "INFO: "
ENDIF

    if level = AMLOG_WARNING
        msgtype = "WARNING: "
ENDIF

    if level = AMLOG_ERROR
        msgtype = "ERROR: "
ENDIF

finalmsg = msgtype + msg

    ' Write message to file and force buffer flush
    WRITE fp, finalmsg
    FlushFileBuffers(fp)

ENDSUB



Note that the subroutines I want to use in other eba's are tagged with "GLOBAL SUB" instead of just "SUB". 

Now to be able to use these routines in other eba's in the project I have to create a header file to go along with this eba.  The header (.inc) file defines the subroutines to other eba's like so:


'****************************************************************************************
'*  amLog.inc                                                                           *
'*                                                                                      *
'*  Author: Mark O'Hara                                                                 *
'*  Description: Logging messages to a file                                             *
'*                                                                                      *
'*  This is a message and error logger that writes messages out to a log file.  The     *
'*  messages have different levels of severity:                                         *
'*                                                                                      *
'*                     Message = informational message                                  *
'*                     Warning = Possible error but won't end program                   *
'*                     Error = Major error, will stop execution of program              *
'*                                                                                      *
'*  Message strings can be formatted with the Ebasic USING command.  The messages are   *
'*  not buffered and are immediately written to the log file.  This is to preserve the  *
'*  messages in case of program crash.                                                  *
'*                                                                                      *
'*                                                                                      *
'*                                                                                      *
'*  Copyright (C) 2008,2009 Cheyenne Cloud, LLC. All Rights Reserved.                   *
'****************************************************************************************



'================================================================================
' Functions
'=================================================================================
DECLARE EXTERN amLogInit(STRING logname)     ' Opens log file
DECLARE EXTERN amLogShutdown()               ' Closes log file
DECLARE EXTERN amLogError(STRING msg)        ' Logs an error, ends program
DECLARE EXTERN amLogWarning(STRING msg)      ' Logs an warning
DECLARE EXTERN amLogMessage(STRING msg)      ' Logs a message




Now, I can call these global subroutines by simply including this header (.inc) file in any eba that I want to use the routines in like so:


'=================================================================================
' Includes
'=================================================================================
$INCLUDE "malloc.inc"
$INCLUDE "memory.inc"

$INCLUDE ".\main\amShutdownStack.inc"
$INCLUDE ".\log\amLog.inc"
$INCLUDE ".\memory\amMemoryArray.inc"
$INCLUDE ".\time\amProfile.inc"
$INCLUDE ".\memory\amMemory.inc"


Note the line $INCLUDE ".\log\amLog.inc" brings the amLog header file into the new eba.  Then I can call the global subroutines in amLog.eba as if they were written inside the new .eba file:


    amLogMessage("This is a test")
    amLogWarning("This is another test")


I hope this helps!

AdrianFox

Thanks for the help, that looks to be just what I need.
Thanks also to Larry for the kind offer to look at my source files.
I've also found the other postings in the Forum (how to access subs in other files) from Jan 2008 now which gives some very helpful advice, so I guess I will be able to find my way through the coding now.

Thanks again..... both for the quality of the advice and the incredible speed with which it is given!

Adrian Fox

Allan

Have attached a zip containeing three examples I have used to make PROJECT.

The DIALOG one is simple to follow.

Allan

AdrianFox

Adrian Fox