home *** CD-ROM | disk | FTP | other *** search
/ LineOne ISP Sign-Up 5 / LineOne.iso / assets / cxt / scripts / parentScripts.cst / 00042_fileIOxtra parent.ls < prev    next >
Encoding:
Text File  |  2001-01-27  |  6.4 KB  |  206 lines

  1. -- 2000.02.26
  2. -- Clive Green <clivegreen@atlas.co.uk>
  3.  
  4. ------------------------------------------------------------------------------------------------------
  5.  
  6. -- provides fileIO services. Only partially implemented!
  7. -- currently, we have...
  8. --
  9. --     mGetFileObject   -- supply some path and filename info, get back a fileIO object
  10. --     mClearFileObject -- supply an object to be disposed of cleanly
  11. --     mReadFile        -- supply a file object to be read from
  12.  
  13. ------------------------------------------------------------------------------------------------------
  14.  
  15. -- declare properties:
  16. property main         -- main code directory object
  17. property dataManager  -- deals with programme setup data
  18. property defaultType  -- the file access type to use when none is provided
  19. property accessTypes  -- a linear list of known access type integers
  20.  
  21. ------------------------------------------------------------------------------------------------------
  22.  
  23. on new me,L
  24.   
  25.   -- (1) extract and check arguments:
  26.   
  27.   -- check for a parameter list:
  28.   if ilk(L) <> #propList then return [#error:#noParamListSupplied, #msg:"fileIOxtra:new"]
  29.   
  30.   -- a reference to the parent codebase is REQUIRED:
  31.   main = L[#main]
  32.   if (ilk(main) <> #instance) then return [#error:#noMainObjectSupplied, #msg:"fileIOxtra:new"]
  33.   
  34.   --------------------
  35.   
  36.   -- (2) what access type integers does fileIO recognise?
  37.   accessTypes = [#readWrite:0, #readOnly:1,#writeOnly:2]
  38.   
  39.   --------------------
  40.   
  41.   -- (3) attempt to obtain fileIO defaults:
  42.   
  43.   -- get the data manager:
  44.   dm = main.getDataManager()
  45.   if (ilk(dm) <> #instance) then return dm
  46.   dataManager = dm
  47.   
  48.   -- what data are we looking for?
  49.   L = [#set:#settings,#item:#fileIODefaults]
  50.   
  51.   -- is there a data entry with this signature?
  52.   L = dm.getData(L)
  53.   if ilk(L) <> #propList then L = [:]
  54.   
  55.   --------------------
  56.   
  57.   -- (4) obtain the default access type to be used:
  58.   t = L[#defaultType]
  59.   
  60.   -- cater for missing or unrecognised access type:
  61.   if not symbolP(t) then t = #readOnly
  62.   if not integerP(accessTypes[t]) then t = #readOnly
  63.   
  64.   -- store a symbol corresponding to the fileIO access flag to be used:
  65.   defaultType = t
  66.   
  67.   --------------------
  68.   
  69.   -- pass back my address:
  70.   return me
  71.   
  72.   ----------------------------------------------------------------------------------------------------
  73.   
  74. on importTextFile me,L
  75.   
  76.   -- see the comments for <mGetFileObject> for a description of L's signature.
  77.   
  78.   -- obtain a stream file controller:
  79.   o = me.mGetFileObject(L)
  80.   if (ilk(o) <> #instance) then return o
  81.   
  82.   -- attempt to read from this stream:
  83.   t = me.mReadFile(o)
  84.   
  85.   -- cleanly dispose of object, then pass text obtained to the caller:
  86.   me.mClearFileObject(o)
  87.   return t
  88.   
  89.   ----------------------------------------------------------------------------------------------------
  90.   
  91. on mGetFileObject me,L
  92.   
  93.   -- Use this method to obtain an object on which fileIO operations can be performed. 
  94.   
  95.   --------------------
  96.   
  97.   -- the arguments listing L supplied must be a propList of the form:
  98.   -- [#filePath:<string>{, #type:<integer>}]  
  99.   --
  100.   -- where ...
  101.   -- #filePath = the full fileName (including extension) we want to connect to;
  102.   -- #type     = a symbol identifying whether we intend to read, write, or read & write
  103.   --             with this file connection:
  104.   -- 
  105.   --               #readWrite (0 = read/write access)
  106.   --               #readOnly  (1 = read access only - this is the default!)
  107.   --               #writeOnly (2 = write access only)
  108.   
  109.   --------------------
  110.   
  111.   -- extract and check arguments listing:
  112.   if ilk(L) <> #propList then return [#error:#e1021]
  113.   
  114.   -- a #file string is required:
  115.   f = L[#filePath] 
  116.   if not stringP(f) then return [#error:#e1022] 
  117.   if not length(f)  then return [#error:#e1023]
  118.   
  119.   -- obtain/set valid access type:
  120.   t = L[#type]
  121.   
  122.   -- cater for missing or unrecognised access type:
  123.   if not symbolP(t) then t = defaultType
  124.   
  125.   -- obtain integer file access flag:
  126.   n = accessTypes[t]
  127.   if not integerP(n) then n = accessTypes[defaultType]
  128.    
  129.   --------------------
  130.   
  131.   -- use buddyAPI to verify the existence of the file f:
  132.   xm = main.getXtrasManager()
  133.   buddy = xm.getBuddyAPIxtra()
  134.   if (ilk(buddy) <> #instance) then return ┬¼
  135.   [#error:#xtraControllerMissing, #msg:"fileIOxtra:mGetFileObject needs buddyAPIxtra"]
  136.   
  137.   -- stop if the file couldn't be found:
  138.   f = buddy.fileExists(f)
  139.   if not stringP(f) then return f
  140.   
  141.   --------------------
  142.   
  143.   -- f is now (hopefully) the full path to a valid text file we want to open:
  144.   
  145.   -- attempt to obtain IO instance:
  146.   fileObject = new(xtra"fileIO")
  147.   
  148.   -- attempt to establish a connection to the file f, with the file access integer n:
  149.   openFile(fileObject,f,n)
  150.   
  151.   -- check for errors (error codes OTHER than 0 are a problem):
  152.   if status(fileObject) <> 0 then return [#error:#e1025]
  153.   
  154.   --------------------
  155.   
  156.   -- the object appears to be in good order; pass it to the caller:
  157.   return fileObject
  158.   
  159.   ----------------------------------------------------------------------------------------------------
  160.   
  161. on mReadFile me,o 
  162.   
  163.   -- this attempts to retrieve the contents of the text file controlled by the object o:
  164.   
  165.   -- ensure legitimate fileIO object was passed:
  166.   o = me.isFileIOobject(o)
  167.   if (ilk(o) <> #instance) then return o
  168.   
  169.   --------------------
  170.   
  171.   -- attempt to read file contents:
  172.   t = readFile(o)
  173.   
  174.   -- check for read errors (remember - error codes OTHER than 0 are a problem):
  175.   if status(o) <> 0 then return [#error:#e1026]
  176.   
  177.   --------------------
  178.   
  179.   -- extract and return:
  180.   return t
  181.   
  182.   ----------------------------------------------------------------------------------------------------
  183.   
  184. on isFileIOobject me,o
  185.   
  186.   -- check that the object o supplied is a pukka fileIO object -- pass it back if it is:
  187.   if ilk(o) = #instance then
  188.     
  189.     -- make a crude run time type identification by stringising the object address, and seeing
  190.     -- whether the result contains the xtra name:
  191.     if (string(o) contains "fileIO") then return o
  192.     
  193.   end if
  194.   
  195.   -- complain when asked to deal with impostors:
  196.   return [#error:#e1027] 
  197.   
  198.   ----------------------------------------------------------------------------------------------------
  199.   
  200. on mClearFileObject me,o
  201.   
  202.   -- this cleanly disposes of the fileIO instance o:
  203.   o = me.isFileIOobject(o)
  204.   if (ilk(o) = #instance) then closeFile(o)
  205.   
  206.   ----------------------------------------------------------------------------------------------------