1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1999
20
* the Initial Developer. All Rights Reserved.
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the NPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the NPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
51
#include "morkObject.h"
54
//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
56
/*=============================================================================
57
* morkFile: abstract file interface
60
#define morkDerived_kFile /*i*/ 0x4669 /* ascii 'Fi' */
62
class morkFile /*d*/ : public morkObject, public nsIMdbFile { /* ````` simple file API ````` */
64
// public: // slots inherited from morkNode (meant to inform only)
65
// nsIMdbHeap* mNode_Heap;
67
// mork_base mNode_Base; // must equal morkBase_kNode
68
// mork_derived mNode_Derived; // depends on specific node subclass
70
// mork_access mNode_Access; // kOpen, kClosing, kShut, or kDead
71
// mork_usage mNode_Usage; // kHeap, kStack, kMember, kGlobal, kNone
72
// mork_able mNode_Mutable; // can this node be modified?
73
// mork_load mNode_Load; // is this node clean or dirty?
75
// mork_uses mNode_Uses; // refcount for strong refs
76
// mork_refs mNode_Refs; // refcount for strong refs + weak refs
78
// public: // slots inherited from morkObject (meant to inform only)
80
// mork_color mBead_Color; // ID for this bead
81
// morkHandle* mObject_Handle; // weak ref to handle for this object
83
// ````` ````` ````` ````` ````` ````` ````` `````
84
protected: // protected morkFile members (similar to public domain IronDoc)
86
mork_u1 mFile_Frozen; // 'F' => file allows only read access
87
mork_u1 mFile_DoTrace; // 'T' trace if ev->DoTrace()
88
mork_u1 mFile_IoOpen; // 'O' => io stream is open (& needs a close)
89
mork_u1 mFile_Active; // 'A' => file is active and usable
91
nsIMdbHeap* mFile_SlotHeap; // heap for Name and other allocated slots
92
char* mFile_Name; // can be nil if SetFileName() is never called
93
// mFile_Name convention: managed with morkEnv::CopyString()/FreeString()
95
nsIMdbFile* mFile_Thief; // from a call to orkinFile::Steal()
97
// { ===== begin morkNode interface =====
98
public: // morkNode virtual methods
99
NS_DECL_ISUPPORTS_INHERITED
100
virtual void CloseMorkNode(morkEnv* ev); // CloseFile() only if open
101
virtual ~morkFile(); // assert that CloseFile() executed earlier
103
public: // morkFile construction & destruction
104
morkFile(morkEnv* ev, const morkUsage& inUsage, nsIMdbHeap* ioHeap,
105
nsIMdbHeap* ioSlotHeap);
106
void CloseFile(morkEnv* ev); // called by CloseMorkNode();
108
private: // copying is not allowed
109
morkFile(const morkFile& other);
110
morkFile& operator=(const morkFile& other);
112
public: // dynamic type identification
113
mork_bool IsFile() const
114
{ return IsNode() && mNode_Derived == morkDerived_kFile; }
115
// } ===== end morkNode methods =====
117
// ````` ````` ````` ````` ````` ````` ````` `````
118
public: // public static standard file creation entry point
120
static morkFile* OpenOldFile(morkEnv* ev, nsIMdbHeap* ioHeap,
121
const char* inFilePath, mork_bool inFrozen);
122
// Choose some subclass of morkFile to instantiate, in order to read
123
// (and write if not frozen) the file known by inFilePath. The file
124
// returned should be open and ready for use, and presumably positioned
125
// at the first byte position of the file. The exact manner in which
126
// files must be opened is considered a subclass specific detail, and
127
// other portions or Mork source code don't want to know how it's done.
129
static morkFile* CreateNewFile(morkEnv* ev, nsIMdbHeap* ioHeap,
130
const char* inFilePath);
131
// Choose some subclass of morkFile to instantiate, in order to read
132
// (and write if not frozen) the file known by inFilePath. The file
133
// returned should be created and ready for use, and presumably positioned
134
// at the first byte position of the file. The exact manner in which
135
// files must be opened is considered a subclass specific detail, and
136
// other portions or Mork source code don't want to know how it's done.
138
public: // non-poly morkFile methods
140
nsIMdbFile* AcquireFileHandle(morkEnv* ev); // mObject_Handle
142
mork_bool FileFrozen() const { return mFile_Frozen == 'F'; }
143
mork_bool FileDoTrace() const { return mFile_DoTrace == 'T'; }
144
mork_bool FileIoOpen() const { return mFile_IoOpen == 'O'; }
145
mork_bool FileActive() const { return mFile_Active == 'A'; }
147
void SetFileFrozen(mork_bool b) { mFile_Frozen = (mork_u1) ((b)? 'F' : 0); }
148
void SetFileDoTrace(mork_bool b) { mFile_DoTrace = (mork_u1) ((b)? 'T' : 0); }
149
void SetFileIoOpen(mork_bool b) { mFile_IoOpen = (mork_u1) ((b)? 'O' : 0); }
150
void SetFileActive(mork_bool b) { mFile_Active = (mork_u1) ((b)? 'A' : 0); }
153
mork_bool IsOpenActiveAndMutableFile() const
154
{ return ( IsOpenNode() && FileActive() && !FileFrozen() ); }
155
// call IsOpenActiveAndMutableFile() before writing a file
157
mork_bool IsOpenAndActiveFile() const
158
{ return ( this->IsOpenNode() && this->FileActive() ); }
159
// call IsOpenAndActiveFile() before using a file
162
nsIMdbFile* GetThief() const { return mFile_Thief; }
163
void SetThief(morkEnv* ev, nsIMdbFile* ioThief); // ioThief can be nil
165
const char* GetFileNameString() const { return mFile_Name; }
166
void SetFileName(morkEnv* ev, const char* inName); // inName can be nil
167
static void NilSlotHeapError(morkEnv* ev);
168
static void NilFileNameError(morkEnv* ev);
169
static void NonFileTypeError(morkEnv* ev);
171
void NewMissingIoError(morkEnv* ev) const;
173
void NewFileDownError(morkEnv* ev) const;
174
// call NewFileDownError() when either IsOpenAndActiveFile()
175
// is false, or when IsOpenActiveAndMutableFile() is false.
177
void NewFileErrnoError(morkEnv* ev) const;
178
// call NewFileErrnoError() to convert std C errno into AB fault
180
mork_size WriteNewlines(morkEnv* ev, mork_count inNewlines);
181
// WriteNewlines() returns the number of bytes written.
183
public: // typesafe refcounting inlines calling inherited morkNode methods
184
static void SlotWeakFile(morkFile* me,
185
morkEnv* ev, morkFile** ioSlot)
186
{ morkNode::SlotWeakNode((morkNode*) me, ev, (morkNode**) ioSlot); }
188
static void SlotStrongFile(morkFile* me,
189
morkEnv* ev, morkFile** ioSlot)
190
{ morkNode::SlotStrongNode((morkNode*) me, ev, (morkNode**) ioSlot); }
192
virtual mork_pos Length(morkEnv* ev) const = 0; // eof
193
// nsIMdbFile methods
194
NS_IMETHOD Tell(nsIMdbEnv* ev, mdb_pos* outPos) const = 0;
195
NS_IMETHOD Seek(nsIMdbEnv* ev, mdb_pos inPos, mdb_pos *outPos) = 0;
196
NS_IMETHOD Eof(nsIMdbEnv* ev, mdb_pos* outPos);
197
// } ----- end pos methods -----
199
// { ----- begin read methods -----
200
NS_IMETHOD Read(nsIMdbEnv* ev, void* outBuf, mdb_size inSize,
201
mdb_size* outActualSize) = 0;
202
NS_IMETHOD Get(nsIMdbEnv* ev, void* outBuf, mdb_size inSize,
203
mdb_pos inPos, mdb_size* outActualSize);
204
// } ----- end read methods -----
206
// { ----- begin write methods -----
207
NS_IMETHOD Write(nsIMdbEnv* ev, const void* inBuf, mdb_size inSize,
208
mdb_size* outActualSize) = 0;
209
NS_IMETHOD Put(nsIMdbEnv* ev, const void* inBuf, mdb_size inSize,
210
mdb_pos inPos, mdb_size* outActualSize);
211
NS_IMETHOD Flush(nsIMdbEnv* ev) = 0;
212
// } ----- end attribute methods -----
214
// { ----- begin path methods -----
215
NS_IMETHOD Path(nsIMdbEnv* ev, mdbYarn* outFilePath);
216
// } ----- end path methods -----
218
// { ----- begin replacement methods -----
219
NS_IMETHOD Steal(nsIMdbEnv* ev, nsIMdbFile* ioThief) = 0;
220
NS_IMETHOD Thief(nsIMdbEnv* ev, nsIMdbFile** acqThief);
221
// } ----- end replacement methods -----
223
// { ----- begin versioning methods -----
224
NS_IMETHOD BecomeTrunk(nsIMdbEnv* ev) = 0;
226
NS_IMETHOD AcquireBud(nsIMdbEnv* ev, nsIMdbHeap* ioHeap,
227
nsIMdbFile** acqBud) = 0;
228
// } ----- end versioning methods -----
230
// } ===== end nsIMdbFile methods =====
234
/*=============================================================================
235
* morkStdioFile: concrete file using standard C file io
238
#define morkDerived_kStdioFile /*i*/ 0x7346 /* ascii 'sF' */
240
class morkStdioFile /*d*/ : public morkFile { /* `` copied from IronDoc `` */
242
// ````` ````` ````` ````` ````` ````` ````` `````
243
protected: // protected morkStdioFile members
245
void* mStdioFile_File;
246
// actually type FILE*, but using opaque void* type
248
// { ===== begin morkNode interface =====
249
public: // morkNode virtual methods
250
virtual void CloseMorkNode(morkEnv* ev); // CloseStdioFile() only if open
251
virtual ~morkStdioFile(); // assert that CloseStdioFile() executed earlier
253
public: // morkStdioFile construction & destruction
254
morkStdioFile(morkEnv* ev, const morkUsage& inUsage,
255
nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap);
256
void CloseStdioFile(morkEnv* ev); // called by CloseMorkNode();
258
private: // copying is not allowed
259
morkStdioFile(const morkStdioFile& other);
260
morkStdioFile& operator=(const morkStdioFile& other);
262
public: // dynamic type identification
263
mork_bool IsStdioFile() const
264
{ return IsNode() && mNode_Derived == morkDerived_kStdioFile; }
265
// } ===== end morkNode methods =====
268
static void NonStdioFileTypeError(morkEnv* ev);
270
// ````` ````` ````` ````` ````` ````` ````` `````
271
public: // compatible with the morkFile::OpenOldFile() entry point
273
static morkStdioFile* OpenOldStdioFile(morkEnv* ev, nsIMdbHeap* ioHeap,
274
const char* inFilePath, mork_bool inFrozen);
276
static morkStdioFile* CreateNewStdioFile(morkEnv* ev, nsIMdbHeap* ioHeap,
277
const char* inFilePath);
279
virtual mork_pos Length(morkEnv* ev) const; // eof
281
NS_IMETHOD Tell(nsIMdbEnv* ev, mdb_pos* outPos) const;
282
NS_IMETHOD Seek(nsIMdbEnv* ev, mdb_pos inPos, mdb_pos *outPos);
283
// NS_IMETHOD Eof(nsIMdbEnv* ev, mdb_pos* outPos);
284
// } ----- end pos methods -----
286
// { ----- begin read methods -----
287
NS_IMETHOD Read(nsIMdbEnv* ev, void* outBuf, mdb_size inSize,
288
mdb_size* outActualSize);
290
// { ----- begin write methods -----
291
NS_IMETHOD Write(nsIMdbEnv* ev, const void* inBuf, mdb_size inSize,
292
mdb_size* outActualSize);
293
// NS_IMETHOD Put(nsIMdbEnv* ev, const void* inBuf, mdb_size inSize,
294
// mdb_pos inPos, mdb_size* outActualSize);
295
NS_IMETHOD Flush(nsIMdbEnv* ev);
296
// } ----- end attribute methods -----
298
NS_IMETHOD Steal(nsIMdbEnv* ev, nsIMdbFile* ioThief);
301
// { ----- begin versioning methods -----
302
NS_IMETHOD BecomeTrunk(nsIMdbEnv* ev);
304
NS_IMETHOD AcquireBud(nsIMdbEnv* ev, nsIMdbHeap* ioHeap,
305
nsIMdbFile** acqBud);
306
// } ----- end versioning methods -----
308
// } ===== end nsIMdbFile methods =====
310
// ````` ````` ````` ````` ````` ````` ````` `````
312
// ````` ````` ````` ````` ````` ````` ````` `````
313
protected: // protected non-poly morkStdioFile methods
315
void new_stdio_file_fault(morkEnv* ev) const;
317
// ````` ````` ````` ````` ````` ````` ````` `````
318
public: // public non-poly morkStdioFile methods
320
morkStdioFile(morkEnv* ev, const morkUsage& inUsage,
321
nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap,
322
const char* inName, const char* inMode);
323
// calls OpenStdio() after construction
325
morkStdioFile(morkEnv* ev, const morkUsage& inUsage,
326
nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap,
327
void* ioFile, const char* inName, mork_bool inFrozen);
328
// calls UseStdio() after construction
330
void OpenStdio(morkEnv* ev, const char* inName, const char* inMode);
331
// Open a new FILE with name inName, using mode flags from inMode.
333
void UseStdio(morkEnv* ev, void* ioFile, const char* inName,
335
// Use an existing file, like stdin/stdout/stderr, which should not
336
// have the io stream closed when the file is closed. The ioFile
337
// parameter must actually be of type FILE (but we don't want to make
338
// this header file include the stdio.h header file).
340
void CloseStdio(morkEnv* ev);
341
// Close the stream io if both and FileActive() and FileIoOpen(), but
342
// this does not close this instances (like CloseStdioFile() does).
343
// If stream io was made active by means of calling UseStdio(),
344
// then this method does little beyond marking the stream inactive
345
// because FileIoOpen() is false.
347
public: // typesafe refcounting inlines calling inherited morkNode methods
348
static void SlotWeakStdioFile(morkStdioFile* me,
349
morkEnv* ev, morkStdioFile** ioSlot)
350
{ morkNode::SlotWeakNode((morkNode*) me, ev, (morkNode**) ioSlot); }
352
static void SlotStrongStdioFile(morkStdioFile* me,
353
morkEnv* ev, morkStdioFile** ioSlot)
354
{ morkNode::SlotStrongNode((morkNode*) me, ev, (morkNode**) ioSlot); }
357
//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
359
#endif /* _MORKFILE_ */