Back to index

lightning-sunbird  0.9+nobinonly
morkFile.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-  */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef _MORKFILE_
00039 #define _MORKFILE_ 1
00040 
00041 #ifndef _MORK_
00042 #include "mork.h"
00043 #endif
00044 
00045 #ifndef _MORKNODE_
00046 #include "morkNode.h"
00047 #endif
00048 
00049 #ifndef _MORKOBJECT_
00050 #include "morkObject.h"
00051 #endif
00052 
00053 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00054 
00055 /*=============================================================================
00056  * morkFile: abstract file interface
00057  */
00058 
00059 #define morkDerived_kFile     /*i*/ 0x4669 /* ascii 'Fi' */
00060 
00061 class morkFile /*d*/ : public morkObject, public nsIMdbFile { /* ````` simple file API ````` */
00062 
00063 // public: // slots inherited from morkNode (meant to inform only)
00064   // nsIMdbHeap*    mNode_Heap;
00065 
00066   // mork_base      mNode_Base;     // must equal morkBase_kNode
00067   // mork_derived   mNode_Derived;  // depends on specific node subclass
00068   
00069   // mork_access    mNode_Access;   // kOpen, kClosing, kShut, or kDead
00070   // mork_usage     mNode_Usage;    // kHeap, kStack, kMember, kGlobal, kNone
00071   // mork_able      mNode_Mutable;  // can this node be modified?
00072   // mork_load      mNode_Load;     // is this node clean or dirty?
00073   
00074   // mork_uses      mNode_Uses;     // refcount for strong refs
00075   // mork_refs      mNode_Refs;     // refcount for strong refs + weak refs
00076   
00077 // public: // slots inherited from morkObject (meant to inform only)
00078 
00079   // mork_color   mBead_Color;   // ID for this bead
00080   // morkHandle*  mObject_Handle;  // weak ref to handle for this object
00081 
00082 // ````` ````` ````` `````   ````` ````` ````` `````  
00083 protected: // protected morkFile members (similar to public domain IronDoc)
00084 
00085   mork_u1     mFile_Frozen;   // 'F' => file allows only read access
00086   mork_u1     mFile_DoTrace;  // 'T' trace if ev->DoTrace()
00087   mork_u1     mFile_IoOpen;   // 'O' => io stream is open (& needs a close)
00088   mork_u1     mFile_Active;   // 'A' => file is active and usable
00089   
00090   nsIMdbHeap* mFile_SlotHeap; // heap for Name and other allocated slots
00091   char*       mFile_Name; // can be nil if SetFileName() is never called
00092   // mFile_Name convention: managed with morkEnv::CopyString()/FreeString()
00093 
00094   nsIMdbFile* mFile_Thief; // from a call to orkinFile::Steal()
00095   
00096 // { ===== begin morkNode interface =====
00097 public: // morkNode virtual methods
00098   NS_DECL_ISUPPORTS_INHERITED
00099   virtual void CloseMorkNode(morkEnv* ev); // CloseFile() only if open
00100   virtual ~morkFile(); // assert that CloseFile() executed earlier
00101   
00102 public: // morkFile construction & destruction
00103   morkFile(morkEnv* ev, const morkUsage& inUsage, nsIMdbHeap* ioHeap,
00104     nsIMdbHeap* ioSlotHeap);
00105   void CloseFile(morkEnv* ev); // called by CloseMorkNode();
00106 
00107 private: // copying is not allowed
00108   morkFile(const morkFile& other);
00109   morkFile& operator=(const morkFile& other);
00110 
00111 public: // dynamic type identification
00112   mork_bool IsFile() const
00113   { return IsNode() && mNode_Derived == morkDerived_kFile; }
00114 // } ===== end morkNode methods =====
00115     
00116 // ````` ````` ````` `````   ````` ````` ````` `````  
00117 public: // public static standard file creation entry point
00118 
00119   static morkFile* OpenOldFile(morkEnv* ev, nsIMdbHeap* ioHeap,
00120     const char* inFilePath, mork_bool inFrozen);
00121   // Choose some subclass of morkFile to instantiate, in order to read
00122   // (and write if not frozen) the file known by inFilePath.  The file
00123   // returned should be open and ready for use, and presumably positioned
00124   // at the first byte position of the file.  The exact manner in which
00125   // files must be opened is considered a subclass specific detail, and
00126   // other portions or Mork source code don't want to know how it's done.
00127 
00128   static morkFile* CreateNewFile(morkEnv* ev, nsIMdbHeap* ioHeap,
00129     const char* inFilePath);
00130   // Choose some subclass of morkFile to instantiate, in order to read
00131   // (and write if not frozen) the file known by inFilePath.  The file
00132   // returned should be created and ready for use, and presumably positioned
00133   // at the first byte position of the file.  The exact manner in which
00134   // files must be opened is considered a subclass specific detail, and
00135   // other portions or Mork source code don't want to know how it's done.
00136   
00137 public: // non-poly morkFile methods
00138 
00139   nsIMdbFile* AcquireFileHandle(morkEnv* ev); // mObject_Handle
00140   
00141   mork_bool FileFrozen() const  { return mFile_Frozen == 'F'; }
00142   mork_bool FileDoTrace() const { return mFile_DoTrace == 'T'; }
00143   mork_bool FileIoOpen() const  { return mFile_IoOpen == 'O'; }
00144   mork_bool FileActive() const  { return mFile_Active == 'A'; }
00145 
00146   void SetFileFrozen(mork_bool b)  { mFile_Frozen = (mork_u1) ((b)? 'F' : 0); }
00147   void SetFileDoTrace(mork_bool b) { mFile_DoTrace = (mork_u1) ((b)? 'T' : 0); }
00148   void SetFileIoOpen(mork_bool b)  { mFile_IoOpen = (mork_u1) ((b)? 'O' : 0); }
00149   void SetFileActive(mork_bool b)  { mFile_Active = (mork_u1) ((b)? 'A' : 0); }
00150 
00151   
00152   mork_bool IsOpenActiveAndMutableFile() const
00153   { return ( IsOpenNode() && FileActive() && !FileFrozen() ); }
00154     // call IsOpenActiveAndMutableFile() before writing a file
00155   
00156   mork_bool IsOpenAndActiveFile() const
00157   { return ( this->IsOpenNode() && this->FileActive() ); }
00158     // call IsOpenAndActiveFile() before using a file
00159     
00160 
00161   nsIMdbFile* GetThief() const { return mFile_Thief; }
00162   void SetThief(morkEnv* ev, nsIMdbFile* ioThief); // ioThief can be nil
00163     
00164   const char* GetFileNameString() const { return mFile_Name; }
00165   void SetFileName(morkEnv* ev, const char* inName); // inName can be nil
00166   static void NilSlotHeapError(morkEnv* ev);
00167   static void NilFileNameError(morkEnv* ev);
00168   static void NonFileTypeError(morkEnv* ev);
00169     
00170   void NewMissingIoError(morkEnv* ev) const;
00171     
00172   void NewFileDownError(morkEnv* ev) const;
00173     // call NewFileDownError() when either IsOpenAndActiveFile()
00174     // is false, or when IsOpenActiveAndMutableFile() is false.
00175  
00176    void NewFileErrnoError(morkEnv* ev) const;
00177        // call NewFileErrnoError() to convert std C errno into AB fault
00178 
00179   mork_size WriteNewlines(morkEnv* ev, mork_count inNewlines);
00180   // WriteNewlines() returns the number of bytes written.
00181          
00182 public: // typesafe refcounting inlines calling inherited morkNode methods
00183   static void SlotWeakFile(morkFile* me,
00184     morkEnv* ev, morkFile** ioSlot)
00185   { morkNode::SlotWeakNode((morkNode*) me, ev, (morkNode**) ioSlot); }
00186   
00187   static void SlotStrongFile(morkFile* me,
00188     morkEnv* ev, morkFile** ioSlot)
00189   { morkNode::SlotStrongNode((morkNode*) me, ev, (morkNode**) ioSlot); }
00190 public:
00191   virtual mork_pos   Length(morkEnv* ev) const = 0; // eof
00192   // nsIMdbFile methods
00193   NS_IMETHOD Tell(nsIMdbEnv* ev, mdb_pos* outPos) const = 0;
00194   NS_IMETHOD Seek(nsIMdbEnv* ev, mdb_pos inPos, mdb_pos *outPos) = 0;
00195   NS_IMETHOD Eof(nsIMdbEnv* ev, mdb_pos* outPos);
00196   // } ----- end pos methods -----
00197 
00198   // { ----- begin read methods -----
00199   NS_IMETHOD Read(nsIMdbEnv* ev, void* outBuf, mdb_size inSize,
00200     mdb_size* outActualSize) = 0;
00201   NS_IMETHOD Get(nsIMdbEnv* ev, void* outBuf, mdb_size inSize,
00202     mdb_pos inPos, mdb_size* outActualSize);
00203   // } ----- end read methods -----
00204     
00205   // { ----- begin write methods -----
00206   NS_IMETHOD  Write(nsIMdbEnv* ev, const void* inBuf, mdb_size inSize,
00207     mdb_size* outActualSize) = 0;
00208   NS_IMETHOD  Put(nsIMdbEnv* ev, const void* inBuf, mdb_size inSize,
00209     mdb_pos inPos, mdb_size* outActualSize);
00210   NS_IMETHOD  Flush(nsIMdbEnv* ev) = 0;
00211   // } ----- end attribute methods -----
00212     
00213   // { ----- begin path methods -----
00214   NS_IMETHOD  Path(nsIMdbEnv* ev, mdbYarn* outFilePath);
00215   // } ----- end path methods -----
00216     
00217   // { ----- begin replacement methods -----
00218   NS_IMETHOD  Steal(nsIMdbEnv* ev, nsIMdbFile* ioThief) = 0;
00219   NS_IMETHOD  Thief(nsIMdbEnv* ev, nsIMdbFile** acqThief);
00220   // } ----- end replacement methods -----
00221 
00222   // { ----- begin versioning methods -----
00223   NS_IMETHOD BecomeTrunk(nsIMdbEnv* ev) = 0;
00224 
00225   NS_IMETHOD AcquireBud(nsIMdbEnv* ev, nsIMdbHeap* ioHeap,
00226     nsIMdbFile** acqBud) = 0; 
00227   // } ----- end versioning methods -----
00228 
00229 // } ===== end nsIMdbFile methods =====
00230 
00231 };
00232 
00233 /*=============================================================================
00234  * morkStdioFile: concrete file using standard C file io
00235  */
00236 
00237 #define morkDerived_kStdioFile     /*i*/ 0x7346 /* ascii 'sF' */
00238 
00239 class morkStdioFile /*d*/ : public morkFile { /* `` copied from IronDoc `` */
00240 
00241 // ````` ````` ````` `````   ````` ````` ````` `````  
00242 protected: // protected morkStdioFile members
00243 
00244   void* mStdioFile_File;
00245   // actually type FILE*, but using opaque void* type
00246   
00247 // { ===== begin morkNode interface =====
00248 public: // morkNode virtual methods
00249   virtual void CloseMorkNode(morkEnv* ev); // CloseStdioFile() only if open
00250   virtual ~morkStdioFile(); // assert that CloseStdioFile() executed earlier
00251   
00252 public: // morkStdioFile construction & destruction
00253   morkStdioFile(morkEnv* ev, const morkUsage& inUsage,
00254     nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap);
00255   void CloseStdioFile(morkEnv* ev); // called by CloseMorkNode();
00256 
00257 private: // copying is not allowed
00258   morkStdioFile(const morkStdioFile& other);
00259   morkStdioFile& operator=(const morkStdioFile& other);
00260 
00261 public: // dynamic type identification
00262   mork_bool IsStdioFile() const
00263   { return IsNode() && mNode_Derived == morkDerived_kStdioFile; }
00264 // } ===== end morkNode methods =====
00265 
00266 public: // typing
00267   static void NonStdioFileTypeError(morkEnv* ev);
00268     
00269 // ````` ````` ````` `````   ````` ````` ````` `````  
00270 public: // compatible with the morkFile::OpenOldFile() entry point
00271 
00272   static morkStdioFile* OpenOldStdioFile(morkEnv* ev, nsIMdbHeap* ioHeap,
00273     const char* inFilePath, mork_bool inFrozen);
00274 
00275   static morkStdioFile* CreateNewStdioFile(morkEnv* ev, nsIMdbHeap* ioHeap,
00276     const char* inFilePath);
00277 
00278   virtual mork_pos   Length(morkEnv* ev) const; // eof
00279 
00280   NS_IMETHOD Tell(nsIMdbEnv* ev, mdb_pos* outPos) const;
00281   NS_IMETHOD Seek(nsIMdbEnv* ev, mdb_pos inPos, mdb_pos *outPos);
00282 //  NS_IMETHOD Eof(nsIMdbEnv* ev, mdb_pos* outPos);
00283   // } ----- end pos methods -----
00284 
00285   // { ----- begin read methods -----
00286   NS_IMETHOD Read(nsIMdbEnv* ev, void* outBuf, mdb_size inSize,
00287     mdb_size* outActualSize);
00288     
00289   // { ----- begin write methods -----
00290   NS_IMETHOD  Write(nsIMdbEnv* ev, const void* inBuf, mdb_size inSize,
00291     mdb_size* outActualSize);
00292 //  NS_IMETHOD  Put(nsIMdbEnv* ev, const void* inBuf, mdb_size inSize,
00293 //    mdb_pos inPos, mdb_size* outActualSize);
00294   NS_IMETHOD  Flush(nsIMdbEnv* ev);
00295   // } ----- end attribute methods -----
00296     
00297   NS_IMETHOD  Steal(nsIMdbEnv* ev, nsIMdbFile* ioThief);
00298    
00299 
00300   // { ----- begin versioning methods -----
00301   NS_IMETHOD BecomeTrunk(nsIMdbEnv* ev);
00302 
00303   NS_IMETHOD AcquireBud(nsIMdbEnv* ev, nsIMdbHeap* ioHeap,
00304     nsIMdbFile** acqBud); 
00305   // } ----- end versioning methods -----
00306 
00307 // } ===== end nsIMdbFile methods =====
00308 
00309 // ````` ````` ````` `````   ````` ````` ````` `````  
00310 
00311 // ````` ````` ````` `````   ````` ````` ````` `````  
00312 protected: // protected non-poly morkStdioFile methods
00313 
00314   void new_stdio_file_fault(morkEnv* ev) const;
00315     
00316 // ````` ````` ````` `````   ````` ````` ````` `````  
00317 public: // public non-poly morkStdioFile methods
00318     
00319   morkStdioFile(morkEnv* ev, const morkUsage& inUsage, 
00320     nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap,
00321     const char* inName, const char* inMode);
00322     // calls OpenStdio() after construction
00323 
00324   morkStdioFile(morkEnv* ev, const morkUsage& inUsage,
00325     nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap,
00326      void* ioFile, const char* inName, mork_bool inFrozen);
00327     // calls UseStdio() after construction
00328   
00329   void OpenStdio(morkEnv* ev, const char* inName, const char* inMode);
00330     // Open a new FILE with name inName, using mode flags from inMode.
00331   
00332   void UseStdio(morkEnv* ev, void* ioFile, const char* inName,
00333     mork_bool inFrozen);
00334     // Use an existing file, like stdin/stdout/stderr, which should not
00335     // have the io stream closed when the file is closed.  The ioFile
00336     // parameter must actually be of type FILE (but we don't want to make
00337     // this header file include the stdio.h header file).
00338     
00339   void CloseStdio(morkEnv* ev);
00340     // Close the stream io if both and FileActive() and FileIoOpen(), but
00341     // this does not close this instances (like CloseStdioFile() does).
00342     // If stream io was made active by means of calling UseStdio(),
00343     // then this method does little beyond marking the stream inactive
00344     // because FileIoOpen() is false.
00345     
00346 public: // typesafe refcounting inlines calling inherited morkNode methods
00347   static void SlotWeakStdioFile(morkStdioFile* me,
00348     morkEnv* ev, morkStdioFile** ioSlot)
00349   { morkNode::SlotWeakNode((morkNode*) me, ev, (morkNode**) ioSlot); }
00350   
00351   static void SlotStrongStdioFile(morkStdioFile* me,
00352     morkEnv* ev, morkStdioFile** ioSlot)
00353   { morkNode::SlotStrongNode((morkNode*) me, ev, (morkNode**) ioSlot); }
00354 };
00355 
00356 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00357 
00358 #endif /* _MORKFILE_ */