FileIO Parent: Source Code
This parent script provides a quick way to read and write plain text files within projectors.
General notes:
- This is a Parent script. This is NOT a behaviour, NOT a movie script.
- The script is a wrapper for the built-in FileIO Xtra and requires Macromedia Director 7.0 or greater.
Copy and paste the source code below into a parent script.
-- FILE IO PARENT
-- Copyright (c) 2001 Kendall Anderson. All rights reserved.
-- http://invisiblethreads.com
-- This script was originally located at:
-- http://invisiblethreads.com/lingo/fileioparent/index.html
-- OVERVIEW:
-- This parent script provides an simple wrapper around the built-in
-- FileIO Xtra in Director.
-- Each instance of the script refers to an individual file. Creating
-- new instance of the script opens up a file pointer which remains
-- until the instance is destroyed (via object._Kill()).
-- ** NOT ALL METHODS OF THE FILE IO XTRA ARE INCLUDED **
-- This is intended as a simple wrapper for basic functionality, upon
-- which any extra functions can easily be inserted.
-- The basic functionality is:
-- opening a file (#read or #write mode)
-- reading a line of text (with option to strip CR/LF characters)
-- writing a line of text (with option to append CR/LF characters)
-- checking if we are past/at the end of the file
-- requesting the current error status
-- The script requires MacroMedia Director 7.0 or greater, due to dot syntax.
-----------------------------------
-- PROPERTIES
property pFP -- file pointer
property pEOL -- end-of-line (carriage return) character
property pTempSCF -- temporarily stores state of "the searchCurrentFolder" when handling file io operations
property pTempSearchPaths -- temporarily stores state of "the searchPaths" when doing file io operations
-----------------------------------
-----------------------------------
-- EXTERNAL METHODS
-- sFilename = string, name of file to deal with
-- sMode = symbol, #read existing file, #write a new file, #append to an existing file
on new me, sFilename, sMode
me._Init()
me._ClearSearchPaths()
pFP = new(xtra "fileio")
case (sMode) of
#read:
openfile(pFP, sFilename, 1)
#write:
-- #write assumes we are creating a new file
-- so if the file already exists, delete it
openfile(pFP, sFilename, 1)
delete(pFP)
createfile(pFP, sFilename)
openfile(pFP, sFilename, 2)
#append:
openfile(pFP, sFilename, 2)
if (me._GetErrorStatus(#message) = "Bad file name") then
-- we can't append to the file yet because it doesn't exist
-- therefore create the file
createfile(pFP, sFilename)
openfile(pFP, sFilename, 2)
else
me._SetPosition(me._GetLength()) -- position ourselves at end of file
end if
end case
me._RestoreSearchPaths()
return me
end
on _Kill me
closefile(pFP)
pFP = void
pEOL = void
pTempSCF = void
pTempSearchPaths = void
end
-- define the line terminator to use
-- this allows us to force the terminator, regardless of actual platform
-- by default, the terminator is chosen by current platform
-- #mac = CR (ascii 13)
-- #win = CR + LF (ascii 13 + ascii 10)
-- #unix = LF (ascii 10)
on _DefineTerminator me, sType
case (sType) of
#mac: pEOL = numtochar(13)
#win: pEOL = numtochar(13) & numtochar(10)
#unix: pEOL = numtochar(10)
end case
end
-- Read 1 line from the file
-- if sMode = #stripcr - clean the c/r from beginning and end of string
on _Readline me, sMode
sLine = ReadLine(pFP)
if (sMode = #stripcr) then
sLine = me._CleanString(sLine)
end if
return sLine
end
-- Write the given string to the file
-- if sMode = #addcr then add c/r
on _WriteLine me, sLine, sMode
if (sMode = #addcr) then
sLine = sLine & pEOL -- add defined terminator if specified
end if
WriteString(pFP, sLine)
end
-- return boolean TRUE or FALSE to: Are we past/at the end-of-file?
on _IsPastEOF me
return (pFP.getPosition() >= pFP.getLength())
end
-- return either the current error status #number, or the text #message of the error
on _GetErrorStatus me, sType
case (sType) of
#number: dReturn = pFP.status()
#message: dReturn = pFP.error(pFP.status())
end case
return dReturn
end
-- These 3 functions are simple wrappers for existing FileIO methods
-- and do not provide any additional functionality.
on _SetPosition me, dPosition
pFP._SetPosition(dPosition)
end
on _GetPosition me
return pFP.getPosition()
end
on _GetLength me
return pFP.getLength()
end
-----------------------------------
-----------------------------------
-- INTERNAL METHODS
-- decide which default line terminator to use based on our platform
on _Init me
if (the platform contains "Windows") then
pEOL = me._DefineTerminator(#win)
else
pEOL = me._DefineTerminator(#mac)
end if
end
-- remove CR/LF terminator from end of string
-- in Windows, the LF is typically found at the beginning of the string and
-- the CR is found at the end (the LF at the beginning is from the previously
-- read line, why they get split I don't know..)
-- NOTE: this method may not fully take into account circumstances of strings
-- on Macintosh or UNIX systems - it has only been tested on Windows.
on _CleanString me, dString
if (dString.char[1] = numtochar(10)) then dString = dString.char[2..dString.length]
if (dString.char[dString.length] = numtochar(13)) then dString = dString.char[1..dString.length-1]
return dString
end
-- documented issue with FileIO - it will resolve a path/filename according
-- to how the searchpaths is configured - this temporarily removes
-- the searchpaths and restores them once the file pointer is set, in
-- an attempt to guarantee you get the file you asked for
on _ClearSearchPaths me
pTempSCF = the searchCurrentFolder
pTempSearchPaths = the searchPaths
the searchCurrentFolder = FALSE
the searchPaths = []
end
on _RestoreSearchPaths me
the searchPaths = pTempSearchPaths
the searchCurrentFolder = pTempSCF
end
