home *** CD-ROM | disk | FTP | other *** search
- -- 2000.02.26
- -- Clive Green <clivegreen@atlas.co.uk>
-
- ------------------------------------------------------------------------------------------------------
-
- -- provides fileIO services. Only partially implemented!
- -- currently, we have...
- --
- -- mGetFileObject -- supply some path and filename info, get back a fileIO object
- -- mClearFileObject -- supply an object to be disposed of cleanly
- -- mReadFile -- supply a file object to be read from
-
- ------------------------------------------------------------------------------------------------------
-
- -- declare properties:
- property main -- main code directory object
- property dataManager -- deals with programme setup data
- property defaultType -- the file access type to use when none is provided
- property accessTypes -- a linear list of known access type integers
-
- ------------------------------------------------------------------------------------------------------
-
- on new me,L
-
- -- (1) extract and check arguments:
-
- -- check for a parameter list:
- if ilk(L) <> #propList then return [#error:#noParamListSupplied, #msg:"fileIOxtra:new"]
-
- -- a reference to the parent codebase is REQUIRED:
- main = L[#main]
- if (ilk(main) <> #instance) then return [#error:#noMainObjectSupplied, #msg:"fileIOxtra:new"]
-
- --------------------
-
- -- (2) what access type integers does fileIO recognise?
- accessTypes = [#readWrite:0, #readOnly:1,#writeOnly:2]
-
- --------------------
-
- -- (3) attempt to obtain fileIO defaults:
-
- -- get the data manager:
- dm = main.getDataManager()
- if (ilk(dm) <> #instance) then return dm
- dataManager = dm
-
- -- what data are we looking for?
- L = [#set:#settings,#item:#fileIODefaults]
-
- -- is there a data entry with this signature?
- L = dm.getData(L)
- if ilk(L) <> #propList then L = [:]
-
- --------------------
-
- -- (4) obtain the default access type to be used:
- t = L[#defaultType]
-
- -- cater for missing or unrecognised access type:
- if not symbolP(t) then t = #readOnly
- if not integerP(accessTypes[t]) then t = #readOnly
-
- -- store a symbol corresponding to the fileIO access flag to be used:
- defaultType = t
-
- --------------------
-
- -- pass back my address:
- return me
-
- ----------------------------------------------------------------------------------------------------
-
- on importTextFile me,L
-
- -- see the comments for <mGetFileObject> for a description of L's signature.
-
- -- obtain a stream file controller:
- o = me.mGetFileObject(L)
- if (ilk(o) <> #instance) then return o
-
- -- attempt to read from this stream:
- t = me.mReadFile(o)
-
- -- cleanly dispose of object, then pass text obtained to the caller:
- me.mClearFileObject(o)
- return t
-
- ----------------------------------------------------------------------------------------------------
-
- on mGetFileObject me,L
-
- -- Use this method to obtain an object on which fileIO operations can be performed.
-
- --------------------
-
- -- the arguments listing L supplied must be a propList of the form:
- -- [#filePath:<string>{, #type:<integer>}]
- --
- -- where ...
- -- #filePath = the full fileName (including extension) we want to connect to;
- -- #type = a symbol identifying whether we intend to read, write, or read & write
- -- with this file connection:
- --
- -- #readWrite (0 = read/write access)
- -- #readOnly (1 = read access only - this is the default!)
- -- #writeOnly (2 = write access only)
-
- --------------------
-
- -- extract and check arguments listing:
- if ilk(L) <> #propList then return [#error:#e1021]
-
- -- a #file string is required:
- f = L[#filePath]
- if not stringP(f) then return [#error:#e1022]
- if not length(f) then return [#error:#e1023]
-
- -- obtain/set valid access type:
- t = L[#type]
-
- -- cater for missing or unrecognised access type:
- if not symbolP(t) then t = defaultType
-
- -- obtain integer file access flag:
- n = accessTypes[t]
- if not integerP(n) then n = accessTypes[defaultType]
-
- --------------------
-
- -- use buddyAPI to verify the existence of the file f:
- xm = main.getXtrasManager()
- buddy = xm.getBuddyAPIxtra()
- if (ilk(buddy) <> #instance) then return ¬
- [#error:#xtraControllerMissing, #msg:"fileIOxtra:mGetFileObject needs buddyAPIxtra"]
-
- -- stop if the file couldn't be found:
- f = buddy.fileExists(f)
- if not stringP(f) then return f
-
- --------------------
-
- -- f is now (hopefully) the full path to a valid text file we want to open:
-
- -- attempt to obtain IO instance:
- fileObject = new(xtra"fileIO")
-
- -- attempt to establish a connection to the file f, with the file access integer n:
- openFile(fileObject,f,n)
-
- -- check for errors (error codes OTHER than 0 are a problem):
- if status(fileObject) <> 0 then return [#error:#e1025]
-
- --------------------
-
- -- the object appears to be in good order; pass it to the caller:
- return fileObject
-
- ----------------------------------------------------------------------------------------------------
-
- on mReadFile me,o
-
- -- this attempts to retrieve the contents of the text file controlled by the object o:
-
- -- ensure legitimate fileIO object was passed:
- o = me.isFileIOobject(o)
- if (ilk(o) <> #instance) then return o
-
- --------------------
-
- -- attempt to read file contents:
- t = readFile(o)
-
- -- check for read errors (remember - error codes OTHER than 0 are a problem):
- if status(o) <> 0 then return [#error:#e1026]
-
- --------------------
-
- -- extract and return:
- return t
-
- ----------------------------------------------------------------------------------------------------
-
- on isFileIOobject me,o
-
- -- check that the object o supplied is a pukka fileIO object -- pass it back if it is:
- if ilk(o) = #instance then
-
- -- make a crude run time type identification by stringising the object address, and seeing
- -- whether the result contains the xtra name:
- if (string(o) contains "fileIO") then return o
-
- end if
-
- -- complain when asked to deal with impostors:
- return [#error:#e1027]
-
- ----------------------------------------------------------------------------------------------------
-
- on mClearFileObject me,o
-
- -- this cleanly disposes of the fileIO instance o:
- o = me.isFileIOobject(o)
- if (ilk(o) = #instance) then closeFile(o)
-
- ----------------------------------------------------------------------------------------------------