General HydroStatics
Ship Stability Software
Command of the Week
(New or interesting aspects of GHS that you may not know about)

IF FEXIST
(Requires GHS version 17.36 or later)

A longtime handy feature in GHS is testing IF FEXIST for whether a file exists before accessing it. That way a run file system can ignore the missing file or provide more robust error handling than trapping out "Error: File not found". For example, if an optional STAB.RF file should be run if it exists, that can be done safely using:

IF FEXIST STAB.RF THEN RUN STAB.RF

Pro tip: IF FEXIST returns true for both file names and directory names. So if STAB.RF were somehow a directory instead of a run file, then IF FEXIST still wouldn't protect from trapping an error. That rare risk is usually not worth bothering about, but when writing general-use programs dealing with input file names on varying computers, you definitely want to be able to distinquish between files and directories!

So how to do that in GHS? Trusty DIR /MACRO (see COW030) comes to the rescue by only running STAB.RF if it's not a directory:

MACRO RUNEXIST
    IF NOT FEXIST "%1\%2" THEN RUN "%1"
/
DIR STAB.RF /MACRO:RUNEXIST STAB.RF

This logic can be generalized by packaging it inside a MACRO /FUNction that was introduced in version 17.36 (see COW181). Here's a new FEXIST function that returns 1 for existing non-directory files and 0 otherwise:

MACRO FEXIST /FUN
    VARIABLE FEXIST_V = 0
    MACRO FEXIST_M
      IF NOT FEXIST "%%91\%%92" THEN FEXIST_V:= FEXIST_V + 1
    //
    DIR "%1" /MACRO:FEXIST_M "%1"
    FEXIST := FEXIST_V
    CLEAR (FEXIST_V) VAR | CLEAR (FEXIST_M) MAC
/
IF {FEXIST("STAB.RF")}=1 THEN RUN STAB.RF

Likewise DEXIST can be defined to return 1 for existing directory names:

MACRO DEXIST /FUN
    DEXIST := 0
    IF FEXIST "%1" THEN IF {FEXIST("%1")}=0 THEN DEXIST := 1
/

Questions, comments, or requests?
Contact Creative Systems, Inc.

support@ghsport.com

USA phone: 360-385-6212 Fax: 360-385-6213
Office hours: 7:00 am - 4:00 pm Pacific Time, Monday - Friday

Mailing address:
PO Box 1910
Port Townsend, WA 98368 USA

www.ghsport.com

Click here for an index to this and previous COWs