~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/db/mork/src/morkFile.h

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
24
 *
 
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.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#ifndef _MORKFILE_
 
40
#define _MORKFILE_ 1
 
41
 
 
42
#ifndef _MORK_
 
43
#include "mork.h"
 
44
#endif
 
45
 
 
46
#ifndef _MORKNODE_
 
47
#include "morkNode.h"
 
48
#endif
 
49
 
 
50
#ifndef _MORKOBJECT_
 
51
#include "morkObject.h"
 
52
#endif
 
53
 
 
54
//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
 
55
 
 
56
/*=============================================================================
 
57
 * morkFile: abstract file interface
 
58
 */
 
59
 
 
60
#define morkDerived_kFile     /*i*/ 0x4669 /* ascii 'Fi' */
 
61
 
 
62
class morkFile /*d*/ : public morkObject, public nsIMdbFile { /* ````` simple file API ````` */
 
63
 
 
64
// public: // slots inherited from morkNode (meant to inform only)
 
65
  // nsIMdbHeap*    mNode_Heap;
 
66
 
 
67
  // mork_base      mNode_Base;     // must equal morkBase_kNode
 
68
  // mork_derived   mNode_Derived;  // depends on specific node subclass
 
69
  
 
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?
 
74
  
 
75
  // mork_uses      mNode_Uses;     // refcount for strong refs
 
76
  // mork_refs      mNode_Refs;     // refcount for strong refs + weak refs
 
77
  
 
78
// public: // slots inherited from morkObject (meant to inform only)
 
79
 
 
80
  // mork_color   mBead_Color;   // ID for this bead
 
81
  // morkHandle*  mObject_Handle;  // weak ref to handle for this object
 
82
 
 
83
// ````` ````` ````` `````   ````` ````` ````` `````  
 
84
protected: // protected morkFile members (similar to public domain IronDoc)
 
85
 
 
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
 
90
  
 
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()
 
94
 
 
95
  nsIMdbFile* mFile_Thief; // from a call to orkinFile::Steal()
 
96
  
 
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
 
102
  
 
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();
 
107
 
 
108
private: // copying is not allowed
 
109
  morkFile(const morkFile& other);
 
110
  morkFile& operator=(const morkFile& other);
 
111
 
 
112
public: // dynamic type identification
 
113
  mork_bool IsFile() const
 
114
  { return IsNode() && mNode_Derived == morkDerived_kFile; }
 
115
// } ===== end morkNode methods =====
 
116
    
 
117
// ````` ````` ````` `````   ````` ````` ````` `````  
 
118
public: // public static standard file creation entry point
 
119
 
 
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.
 
128
 
 
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.
 
137
  
 
138
public: // non-poly morkFile methods
 
139
 
 
140
  nsIMdbFile* AcquireFileHandle(morkEnv* ev); // mObject_Handle
 
141
  
 
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'; }
 
146
 
 
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); }
 
151
 
 
152
  
 
153
  mork_bool IsOpenActiveAndMutableFile() const
 
154
  { return ( IsOpenNode() && FileActive() && !FileFrozen() ); }
 
155
    // call IsOpenActiveAndMutableFile() before writing a file
 
156
  
 
157
  mork_bool IsOpenAndActiveFile() const
 
158
  { return ( this->IsOpenNode() && this->FileActive() ); }
 
159
    // call IsOpenAndActiveFile() before using a file
 
160
    
 
161
 
 
162
  nsIMdbFile* GetThief() const { return mFile_Thief; }
 
163
  void SetThief(morkEnv* ev, nsIMdbFile* ioThief); // ioThief can be nil
 
164
    
 
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);
 
170
    
 
171
  void NewMissingIoError(morkEnv* ev) const;
 
172
    
 
173
  void NewFileDownError(morkEnv* ev) const;
 
174
    // call NewFileDownError() when either IsOpenAndActiveFile()
 
175
    // is false, or when IsOpenActiveAndMutableFile() is false.
 
176
 
 
177
   void NewFileErrnoError(morkEnv* ev) const;
 
178
       // call NewFileErrnoError() to convert std C errno into AB fault
 
179
 
 
180
  mork_size WriteNewlines(morkEnv* ev, mork_count inNewlines);
 
181
  // WriteNewlines() returns the number of bytes written.
 
182
         
 
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); }
 
187
  
 
188
  static void SlotStrongFile(morkFile* me,
 
189
    morkEnv* ev, morkFile** ioSlot)
 
190
  { morkNode::SlotStrongNode((morkNode*) me, ev, (morkNode**) ioSlot); }
 
191
public:
 
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 -----
 
198
 
 
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 -----
 
205
    
 
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 -----
 
213
    
 
214
  // { ----- begin path methods -----
 
215
  NS_IMETHOD  Path(nsIMdbEnv* ev, mdbYarn* outFilePath);
 
216
  // } ----- end path methods -----
 
217
    
 
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 -----
 
222
 
 
223
  // { ----- begin versioning methods -----
 
224
  NS_IMETHOD BecomeTrunk(nsIMdbEnv* ev) = 0;
 
225
 
 
226
  NS_IMETHOD AcquireBud(nsIMdbEnv* ev, nsIMdbHeap* ioHeap,
 
227
    nsIMdbFile** acqBud) = 0; 
 
228
  // } ----- end versioning methods -----
 
229
 
 
230
// } ===== end nsIMdbFile methods =====
 
231
 
 
232
};
 
233
 
 
234
/*=============================================================================
 
235
 * morkStdioFile: concrete file using standard C file io
 
236
 */
 
237
 
 
238
#define morkDerived_kStdioFile     /*i*/ 0x7346 /* ascii 'sF' */
 
239
 
 
240
class morkStdioFile /*d*/ : public morkFile { /* `` copied from IronDoc `` */
 
241
 
 
242
// ````` ````` ````` `````   ````` ````` ````` `````  
 
243
protected: // protected morkStdioFile members
 
244
 
 
245
  void* mStdioFile_File;
 
246
  // actually type FILE*, but using opaque void* type
 
247
  
 
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
 
252
  
 
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();
 
257
 
 
258
private: // copying is not allowed
 
259
  morkStdioFile(const morkStdioFile& other);
 
260
  morkStdioFile& operator=(const morkStdioFile& other);
 
261
 
 
262
public: // dynamic type identification
 
263
  mork_bool IsStdioFile() const
 
264
  { return IsNode() && mNode_Derived == morkDerived_kStdioFile; }
 
265
// } ===== end morkNode methods =====
 
266
 
 
267
public: // typing
 
268
  static void NonStdioFileTypeError(morkEnv* ev);
 
269
    
 
270
// ````` ````` ````` `````   ````` ````` ````` `````  
 
271
public: // compatible with the morkFile::OpenOldFile() entry point
 
272
 
 
273
  static morkStdioFile* OpenOldStdioFile(morkEnv* ev, nsIMdbHeap* ioHeap,
 
274
    const char* inFilePath, mork_bool inFrozen);
 
275
 
 
276
  static morkStdioFile* CreateNewStdioFile(morkEnv* ev, nsIMdbHeap* ioHeap,
 
277
    const char* inFilePath);
 
278
 
 
279
  virtual mork_pos   Length(morkEnv* ev) const; // eof
 
280
 
 
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 -----
 
285
 
 
286
  // { ----- begin read methods -----
 
287
  NS_IMETHOD Read(nsIMdbEnv* ev, void* outBuf, mdb_size inSize,
 
288
    mdb_size* outActualSize);
 
289
    
 
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 -----
 
297
    
 
298
  NS_IMETHOD  Steal(nsIMdbEnv* ev, nsIMdbFile* ioThief);
 
299
   
 
300
 
 
301
  // { ----- begin versioning methods -----
 
302
  NS_IMETHOD BecomeTrunk(nsIMdbEnv* ev);
 
303
 
 
304
  NS_IMETHOD AcquireBud(nsIMdbEnv* ev, nsIMdbHeap* ioHeap,
 
305
    nsIMdbFile** acqBud); 
 
306
  // } ----- end versioning methods -----
 
307
 
 
308
// } ===== end nsIMdbFile methods =====
 
309
 
 
310
// ````` ````` ````` `````   ````` ````` ````` `````  
 
311
 
 
312
// ````` ````` ````` `````   ````` ````` ````` `````  
 
313
protected: // protected non-poly morkStdioFile methods
 
314
 
 
315
  void new_stdio_file_fault(morkEnv* ev) const;
 
316
    
 
317
// ````` ````` ````` `````   ````` ````` ````` `````  
 
318
public: // public non-poly morkStdioFile methods
 
319
    
 
320
  morkStdioFile(morkEnv* ev, const morkUsage& inUsage, 
 
321
    nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap,
 
322
    const char* inName, const char* inMode);
 
323
    // calls OpenStdio() after construction
 
324
 
 
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
 
329
  
 
330
  void OpenStdio(morkEnv* ev, const char* inName, const char* inMode);
 
331
    // Open a new FILE with name inName, using mode flags from inMode.
 
332
  
 
333
  void UseStdio(morkEnv* ev, void* ioFile, const char* inName,
 
334
    mork_bool inFrozen);
 
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).
 
339
    
 
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.
 
346
    
 
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); }
 
351
  
 
352
  static void SlotStrongStdioFile(morkStdioFile* me,
 
353
    morkEnv* ev, morkStdioFile** ioSlot)
 
354
  { morkNode::SlotStrongNode((morkNode*) me, ev, (morkNode**) ioSlot); }
 
355
};
 
356
 
 
357
//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
 
358
 
 
359
#endif /* _MORKFILE_ */