1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
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) 1998
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
26 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | // First checked in on 98/11/20 by John R. McMullen in the wrong directory.
|
---|
39 | // Checked in again 98/12/04.
|
---|
40 | // Polished version 98/12/08.
|
---|
41 | // Completely rewritten to integrate with nsIInputStream and nsIOutputStream (the
|
---|
42 | // xpcom stream objects.
|
---|
43 |
|
---|
44 | //========================================================================================
|
---|
45 | //
|
---|
46 | // Classes defined:
|
---|
47 | //
|
---|
48 | // nsInputStream, nsOutputStream
|
---|
49 | // These are the lightweight STATICALLY LINKED wrappers for
|
---|
50 | // the xpcom objects nsIInputStream and nsIOutputstream.
|
---|
51 | // Possible uses:
|
---|
52 | // If you are implementing a function that accepts one of these xpcom
|
---|
53 | // streams, just make one of these little jobbies on the stack, and
|
---|
54 | // the handy << or >> notation can be yours.
|
---|
55 | //
|
---|
56 | // nsInputFileStream, nsOutputFileStream
|
---|
57 | // These are the STATICALLY LINKED wrappers for the file-related
|
---|
58 | // versions of the above.
|
---|
59 | // nsIOFileStream
|
---|
60 | // An input and output file stream attached to the same file.
|
---|
61 | //
|
---|
62 | // This suite provide the following services:
|
---|
63 | //
|
---|
64 | // 1. Encapsulates all platform-specific file details, so that file i/o
|
---|
65 | // can be done correctly without any platform #ifdefs
|
---|
66 | //
|
---|
67 | // 2. Uses NSPR file services (NOT ansi file I/O), in order to get best
|
---|
68 | // native performance. This performance difference is especially large on
|
---|
69 | // macintosh.
|
---|
70 | //
|
---|
71 | // 3. Allows all the power of the ansi stream syntax.
|
---|
72 | //
|
---|
73 | // Basic example:
|
---|
74 | //
|
---|
75 | // nsFileSpec myPath("/Development/iotest.txt");
|
---|
76 | //
|
---|
77 | // nsOutputFileStream testStream(myPath);
|
---|
78 | // testStream << "Hello World" << nsEndl;
|
---|
79 | //
|
---|
80 | // 4. Requires streams to be constructed using typesafe nsFileSpec specifier
|
---|
81 | // (not the notorious and bug prone const char*), namely nsFileSpec. See
|
---|
82 | // nsFileSpec.h for more details.
|
---|
83 | //
|
---|
84 | // 5. Fixes a bug that have been there for a long time, and
|
---|
85 | // is inevitable if you use NSPR alone:
|
---|
86 | //
|
---|
87 | // The problem on platforms (Macintosh) in which a path does not fully
|
---|
88 | // specify a file, because two volumes can have the same name.
|
---|
89 | //
|
---|
90 | // Not yet provided:
|
---|
91 | //
|
---|
92 | // Endian-awareness for reading and writing crossplatform binary files. At this
|
---|
93 | // time there seems to be no demand for this.
|
---|
94 | //
|
---|
95 | //========================================================================================
|
---|
96 |
|
---|
97 | #ifndef _FILESTREAM_H_
|
---|
98 | #define _FILESTREAM_H_
|
---|
99 |
|
---|
100 | #include "xpcomobsolete.h"
|
---|
101 | #include "nsStringFwd.h"
|
---|
102 |
|
---|
103 | #ifdef XP_MAC
|
---|
104 | #include "pprio.h" // To get PR_ImportFile
|
---|
105 | #else
|
---|
106 | #include "prio.h"
|
---|
107 | #endif
|
---|
108 |
|
---|
109 | #include "nsCOMPtr.h"
|
---|
110 | #include "nsIFileStream.h"
|
---|
111 |
|
---|
112 | // Defined elsewhere
|
---|
113 | class nsFileSpec;
|
---|
114 | class nsIInputStream;
|
---|
115 | class nsIOutputStream;
|
---|
116 | class nsIFileSpec;
|
---|
117 |
|
---|
118 | //========================================================================================
|
---|
119 | // Compiler-specific macros, as needed
|
---|
120 | //========================================================================================
|
---|
121 | #if !defined(NS_USING_NAMESPACE) && (defined(__MWERKS__) || defined(_MSC_VER))
|
---|
122 | #define NS_USING_NAMESPACE
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | #ifdef NS_USING_NAMESPACE
|
---|
126 |
|
---|
127 | #define NS_NAMESPACE_PROTOTYPE
|
---|
128 | #define NS_NAMESPACE namespace
|
---|
129 | #define NS_NAMESPACE_END
|
---|
130 |
|
---|
131 | #else
|
---|
132 |
|
---|
133 | #define NS_NAMESPACE_PROTOTYPE static
|
---|
134 | #define NS_NAMESPACE struct
|
---|
135 | #define NS_NAMESPACE_END ;
|
---|
136 |
|
---|
137 | #endif // NS_USING_NAMESPACE
|
---|
138 |
|
---|
139 | #if !defined(XP_MAC) && !defined(__KCC)
|
---|
140 | // PR_STDOUT and PR_STDIN are fatal on Macintosh. So for console i/o, we must use the std
|
---|
141 | // stream stuff instead. However, we have to require that cout and cin are passed in
|
---|
142 | // to the constructor because in the current build, there is a copy in the base.shlb,
|
---|
143 | // and another in the caller's file. Passing it in as a parameter ensures that the
|
---|
144 | // caller and this library are using the same global object. Groan.
|
---|
145 | //
|
---|
146 | // Unix currently does not support iostreams at all. Their compilers do not support
|
---|
147 | // ANSI C++, or even ARM C++.
|
---|
148 | //
|
---|
149 | // Windows supports them, but only if you turn on the -GX compile flag, to support
|
---|
150 | // exceptions.
|
---|
151 |
|
---|
152 | // Catch 22.
|
---|
153 | #define NS_USE_PR_STDIO
|
---|
154 | #endif
|
---|
155 |
|
---|
156 | #ifdef NS_USE_PR_STDIO
|
---|
157 | class istream;
|
---|
158 | class ostream;
|
---|
159 | #define CONSOLE_IN 0
|
---|
160 | #define CONSOLE_OUT 0
|
---|
161 | #else
|
---|
162 | #include <iostream>
|
---|
163 | using std::istream;
|
---|
164 | using std::ostream;
|
---|
165 | #define CONSOLE_IN &std::cin
|
---|
166 | #define CONSOLE_OUT &std::cout
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | //=========================== End Compiler-specific macros ===============================
|
---|
170 |
|
---|
171 | //========================================================================================
|
---|
172 | class NS_COM_OBSOLETE nsInputStream
|
---|
173 | // This is a convenience class, for use on the STACK ("new" junkies: get detoxed first).
|
---|
174 | // Given a COM-style stream, this allows you to use the >> operators. It also acquires and
|
---|
175 | // reference counts its stream.
|
---|
176 | // Please read the comments at the top of this file
|
---|
177 | //========================================================================================
|
---|
178 | {
|
---|
179 | public:
|
---|
180 | nsInputStream(nsIInputStream* inStream)
|
---|
181 | : mInputStream(do_QueryInterface(inStream))
|
---|
182 | , mEOF(PR_FALSE)
|
---|
183 | {}
|
---|
184 | virtual ~nsInputStream();
|
---|
185 |
|
---|
186 | nsCOMPtr<nsIInputStream> GetIStream() const
|
---|
187 | {
|
---|
188 | return mInputStream;
|
---|
189 | }
|
---|
190 | PRBool eof() const { return get_at_eof(); }
|
---|
191 | char get();
|
---|
192 | nsresult close()
|
---|
193 | {
|
---|
194 | NS_ASSERTION(mInputStream, "mInputStream is null!");
|
---|
195 | if (mInputStream) {
|
---|
196 | return mInputStream->Close();
|
---|
197 | }
|
---|
198 | return NS_OK;
|
---|
199 | }
|
---|
200 | PRInt32 read(void* s, PRInt32 n);
|
---|
201 |
|
---|
202 | // Input streamers. Add more as needed (int&, unsigned int& etc). (but you have to
|
---|
203 | // add delegators to the derived classes, too, because these operators don't inherit).
|
---|
204 | nsInputStream& operator >> (char& ch);
|
---|
205 |
|
---|
206 | // Support manipulators
|
---|
207 | nsInputStream& operator >> (nsInputStream& (*pf)(nsInputStream&))
|
---|
208 | {
|
---|
209 | return pf(*this);
|
---|
210 | }
|
---|
211 |
|
---|
212 | protected:
|
---|
213 |
|
---|
214 | // These certainly need to be overridden, they give the best shot we can at detecting
|
---|
215 | // eof in a simple nsIInputStream.
|
---|
216 | virtual void set_at_eof(PRBool atEnd)
|
---|
217 | {
|
---|
218 | mEOF = atEnd;
|
---|
219 | }
|
---|
220 | virtual PRBool get_at_eof() const
|
---|
221 | {
|
---|
222 | return mEOF;
|
---|
223 | }
|
---|
224 | private:
|
---|
225 |
|
---|
226 | nsInputStream& operator >> (char* buf); // TOO DANGEROUS. DON'T DEFINE.
|
---|
227 |
|
---|
228 | // private and unimplemented to disallow copies and assigns
|
---|
229 | nsInputStream(const nsInputStream& rhs);
|
---|
230 | nsInputStream& operator=(const nsInputStream& rhs);
|
---|
231 |
|
---|
232 | // DATA
|
---|
233 | protected:
|
---|
234 | nsCOMPtr<nsIInputStream> mInputStream;
|
---|
235 | PRBool mEOF;
|
---|
236 | }; // class nsInputStream
|
---|
237 |
|
---|
238 | typedef nsInputStream nsBasicInStream; // historic support for this name
|
---|
239 |
|
---|
240 | //========================================================================================
|
---|
241 | class NS_COM_OBSOLETE nsOutputStream
|
---|
242 | // This is a convenience class, for use on the STACK ("new" junkies, get detoxed first).
|
---|
243 | // Given a COM-style stream, this allows you to use the << operators. It also acquires and
|
---|
244 | // reference counts its stream.
|
---|
245 | // Please read the comments at the top of this file
|
---|
246 | //========================================================================================
|
---|
247 | {
|
---|
248 | public:
|
---|
249 | nsOutputStream() {}
|
---|
250 | nsOutputStream(nsIOutputStream* inStream)
|
---|
251 | : mOutputStream(do_QueryInterface(inStream))
|
---|
252 | {}
|
---|
253 |
|
---|
254 | virtual ~nsOutputStream();
|
---|
255 |
|
---|
256 | nsCOMPtr<nsIOutputStream> GetIStream() const
|
---|
257 | {
|
---|
258 | return mOutputStream;
|
---|
259 | }
|
---|
260 | nsresult close()
|
---|
261 | {
|
---|
262 | if (mOutputStream)
|
---|
263 | return mOutputStream->Close();
|
---|
264 | return NS_OK;
|
---|
265 | }
|
---|
266 | void put(char c);
|
---|
267 | PRInt32 write(const void* s, PRInt32 n);
|
---|
268 | virtual nsresult flush();
|
---|
269 | nsresult lastWriteStatus();
|
---|
270 |
|
---|
271 | // Output streamers. Add more as needed (but you have to add delegators to the derived
|
---|
272 | // classes, too, because these operators don't inherit).
|
---|
273 | nsOutputStream& operator << (const char* buf);
|
---|
274 | nsOutputStream& operator << (char ch);
|
---|
275 | nsOutputStream& operator << (short val);
|
---|
276 | nsOutputStream& operator << (unsigned short val);
|
---|
277 | nsOutputStream& operator << (long val);
|
---|
278 | nsOutputStream& operator << (unsigned long val);
|
---|
279 | nsOutputStream& operator << (int val);
|
---|
280 | nsOutputStream& operator << (unsigned int val);
|
---|
281 |
|
---|
282 | // Support manipulators
|
---|
283 | nsOutputStream& operator << (nsOutputStream& (*pf)(nsOutputStream&))
|
---|
284 | {
|
---|
285 | return pf(*this);
|
---|
286 | }
|
---|
287 |
|
---|
288 | private:
|
---|
289 |
|
---|
290 | // private and unimplemented to disallow copies and assigns
|
---|
291 | nsOutputStream(const nsOutputStream& rhs);
|
---|
292 | nsOutputStream& operator=(const nsOutputStream& rhs);
|
---|
293 |
|
---|
294 | nsresult mWriteStatus;
|
---|
295 |
|
---|
296 | // DATA
|
---|
297 | protected:
|
---|
298 | nsCOMPtr<nsIOutputStream> mOutputStream;
|
---|
299 | }; // class nsOutputStream
|
---|
300 |
|
---|
301 | typedef nsOutputStream nsBasicOutStream; // Historic support for this name
|
---|
302 |
|
---|
303 | //========================================================================================
|
---|
304 | class NS_COM_OBSOLETE nsErrorProne
|
---|
305 | // Common (virtual) base class for remembering errors on demand
|
---|
306 | //========================================================================================
|
---|
307 | {
|
---|
308 | public:
|
---|
309 | nsErrorProne() // for delayed opening
|
---|
310 | : mResult(NS_OK)
|
---|
311 | {
|
---|
312 | }
|
---|
313 | PRBool failed() const
|
---|
314 | {
|
---|
315 | return NS_FAILED(mResult);
|
---|
316 | }
|
---|
317 | nsresult error() const
|
---|
318 | {
|
---|
319 | return mResult;
|
---|
320 | }
|
---|
321 |
|
---|
322 | // DATA
|
---|
323 | protected:
|
---|
324 | nsresult mResult;
|
---|
325 | }; // class nsErrorProne
|
---|
326 |
|
---|
327 | //========================================================================================
|
---|
328 | class NS_COM_OBSOLETE nsFileClient
|
---|
329 | // Because COM does not allow us to write functions which return a boolean value etc,
|
---|
330 | // this class is here to take care of the tedious "declare variable then call with
|
---|
331 | // the address of the variable" chores.
|
---|
332 | //========================================================================================
|
---|
333 | : public virtual nsErrorProne
|
---|
334 | {
|
---|
335 | public:
|
---|
336 | nsFileClient(const nsCOMPtr<nsIOpenFile>& inFile)
|
---|
337 | : mFile(do_QueryInterface(inFile))
|
---|
338 | {
|
---|
339 | }
|
---|
340 | virtual ~nsFileClient() {}
|
---|
341 |
|
---|
342 | void open(
|
---|
343 | const nsFileSpec& inFile,
|
---|
344 | int nsprMode,
|
---|
345 | PRIntn accessMode)
|
---|
346 | {
|
---|
347 | if (mFile)
|
---|
348 | mResult = mFile->Open(inFile, nsprMode, accessMode);
|
---|
349 | }
|
---|
350 | PRBool is_open() const
|
---|
351 | {
|
---|
352 | PRBool result = PR_FALSE;
|
---|
353 | if (mFile)
|
---|
354 | mFile->GetIsOpen(&result);
|
---|
355 | return result;
|
---|
356 | }
|
---|
357 | PRBool is_file() const
|
---|
358 | {
|
---|
359 | return mFile ? PR_TRUE : PR_FALSE;
|
---|
360 | }
|
---|
361 |
|
---|
362 | protected:
|
---|
363 |
|
---|
364 | nsFileClient() // for delayed opening
|
---|
365 | {
|
---|
366 | }
|
---|
367 | // DATA
|
---|
368 | protected:
|
---|
369 | nsCOMPtr<nsIOpenFile> mFile;
|
---|
370 | }; // class nsFileClient
|
---|
371 |
|
---|
372 | //========================================================================================
|
---|
373 | class NS_COM_OBSOLETE nsRandomAccessStoreClient
|
---|
374 | // Because COM does not allow us to write functions which return a boolean value etc,
|
---|
375 | // this class is here to take care of the tedious "declare variable then call with
|
---|
376 | // the address of the variable" chores.
|
---|
377 | //========================================================================================
|
---|
378 | : public virtual nsErrorProne
|
---|
379 | {
|
---|
380 | public:
|
---|
381 | nsRandomAccessStoreClient() // for delayed opening
|
---|
382 | {
|
---|
383 | }
|
---|
384 | nsRandomAccessStoreClient(const nsCOMPtr<nsIRandomAccessStore>& inStore)
|
---|
385 | : mStore(do_QueryInterface(inStore))
|
---|
386 | {
|
---|
387 | }
|
---|
388 | virtual ~nsRandomAccessStoreClient() {}
|
---|
389 |
|
---|
390 | void seek(PRInt64 offset)
|
---|
391 | {
|
---|
392 | seek(PR_SEEK_SET, offset);
|
---|
393 | }
|
---|
394 |
|
---|
395 | void seek(PRSeekWhence whence, PRInt64 offset)
|
---|
396 | {
|
---|
397 | set_at_eof(PR_FALSE);
|
---|
398 | if (mStore)
|
---|
399 | mResult = mStore->Seek(whence, offset);
|
---|
400 | }
|
---|
401 | PRInt64 tell()
|
---|
402 | {
|
---|
403 | PRInt64 result;
|
---|
404 | LL_I2L(result, -1);
|
---|
405 | if (mStore)
|
---|
406 | mResult = mStore->Tell(&result);
|
---|
407 | return result;
|
---|
408 | }
|
---|
409 |
|
---|
410 | protected:
|
---|
411 |
|
---|
412 | virtual PRBool get_at_eof() const
|
---|
413 | {
|
---|
414 | PRBool result = PR_TRUE;
|
---|
415 | if (mStore)
|
---|
416 | mStore->GetAtEOF(&result);
|
---|
417 | return result;
|
---|
418 | }
|
---|
419 |
|
---|
420 | virtual void set_at_eof(PRBool atEnd)
|
---|
421 | {
|
---|
422 | if (mStore)
|
---|
423 | mStore->SetAtEOF(atEnd);
|
---|
424 | }
|
---|
425 |
|
---|
426 | private:
|
---|
427 |
|
---|
428 | // private and unimplemented to disallow copies and assigns
|
---|
429 | nsRandomAccessStoreClient(const nsRandomAccessStoreClient& rhs);
|
---|
430 | nsRandomAccessStoreClient& operator=(const nsRandomAccessStoreClient& rhs);
|
---|
431 |
|
---|
432 | // DATA
|
---|
433 | protected:
|
---|
434 | nsCOMPtr<nsIRandomAccessStore> mStore;
|
---|
435 | }; // class nsRandomAccessStoreClient
|
---|
436 |
|
---|
437 | //========================================================================================
|
---|
438 | class NS_COM_OBSOLETE nsRandomAccessInputStream
|
---|
439 | // Please read the comments at the top of this file
|
---|
440 | //========================================================================================
|
---|
441 | : public nsRandomAccessStoreClient
|
---|
442 | , public nsInputStream
|
---|
443 | {
|
---|
444 | public:
|
---|
445 | nsRandomAccessInputStream(nsIInputStream* inStream)
|
---|
446 | : nsRandomAccessStoreClient(do_QueryInterface(inStream))
|
---|
447 | , nsInputStream(inStream)
|
---|
448 | {
|
---|
449 | }
|
---|
450 | PRBool readline(char* s, PRInt32 n);
|
---|
451 | // Result always null-terminated.
|
---|
452 | // Check eof() before each call.
|
---|
453 | // CAUTION: false result only indicates line was truncated
|
---|
454 | // to fit buffer, or an error occurred (OTHER THAN eof).
|
---|
455 |
|
---|
456 | // Input streamers. Unfortunately, they don't inherit!
|
---|
457 | nsInputStream& operator >> (char& ch)
|
---|
458 | { return nsInputStream::operator >>(ch); }
|
---|
459 | nsInputStream& operator >> (nsInputStream& (*pf)(nsInputStream&))
|
---|
460 | { return nsInputStream::operator >>(pf); }
|
---|
461 |
|
---|
462 | protected:
|
---|
463 | nsRandomAccessInputStream()
|
---|
464 | : nsInputStream(nsnull)
|
---|
465 | {
|
---|
466 | }
|
---|
467 |
|
---|
468 | virtual PRBool get_at_eof() const
|
---|
469 | {
|
---|
470 | return nsRandomAccessStoreClient::get_at_eof();
|
---|
471 | }
|
---|
472 |
|
---|
473 | virtual void set_at_eof(PRBool atEnd)
|
---|
474 | {
|
---|
475 | nsRandomAccessStoreClient::set_at_eof(atEnd);
|
---|
476 | }
|
---|
477 |
|
---|
478 | private:
|
---|
479 |
|
---|
480 | // private and unimplemented to disallow copies and assigns
|
---|
481 | nsRandomAccessInputStream(const nsRandomAccessInputStream& rhs);
|
---|
482 | nsRandomAccessInputStream& operator=(const nsRandomAccessInputStream& rhs);
|
---|
483 |
|
---|
484 | }; // class nsRandomAccessInputStream
|
---|
485 |
|
---|
486 | //========================================================================================
|
---|
487 | class NS_COM_OBSOLETE nsInputStringStream
|
---|
488 | //========================================================================================
|
---|
489 | : public nsRandomAccessInputStream
|
---|
490 | {
|
---|
491 | public:
|
---|
492 | nsInputStringStream(const char* stringToRead);
|
---|
493 | nsInputStringStream(const nsString& stringToRead);
|
---|
494 |
|
---|
495 | // Input streamers. Unfortunately, they don't inherit!
|
---|
496 | nsInputStream& operator >> (char& ch)
|
---|
497 | { return nsInputStream::operator >>(ch); }
|
---|
498 | nsInputStream& operator >> (nsInputStream& (*pf)(nsInputStream&))
|
---|
499 | { return nsInputStream::operator >>(pf); }
|
---|
500 |
|
---|
501 |
|
---|
502 | private:
|
---|
503 |
|
---|
504 | // private and unimplemented to disallow copies and assigns
|
---|
505 | nsInputStringStream(const nsInputStringStream& rhs);
|
---|
506 | nsInputStringStream& operator=(const nsInputStringStream& rhs);
|
---|
507 |
|
---|
508 |
|
---|
509 | }; // class nsInputStringStream
|
---|
510 |
|
---|
511 | //========================================================================================
|
---|
512 | class NS_COM_OBSOLETE nsInputFileStream
|
---|
513 | // Please read the comments at the top of this file
|
---|
514 | //========================================================================================
|
---|
515 | : public nsRandomAccessInputStream
|
---|
516 | , public nsFileClient
|
---|
517 | {
|
---|
518 | public:
|
---|
519 | enum { kDefaultMode = PR_RDONLY };
|
---|
520 | nsInputFileStream(nsIInputStream* inStream)
|
---|
521 | : nsRandomAccessInputStream(inStream)
|
---|
522 | , nsFileClient(do_QueryInterface(inStream))
|
---|
523 | , mFileInputStream(do_QueryInterface(inStream))
|
---|
524 | {
|
---|
525 | }
|
---|
526 | nsInputFileStream(
|
---|
527 | const nsFileSpec& inFile,
|
---|
528 | int nsprMode = kDefaultMode,
|
---|
529 | PRIntn accessMode = 00666);
|
---|
530 | nsInputFileStream(nsIFileSpec* inFile);
|
---|
531 | virtual ~nsInputFileStream();
|
---|
532 |
|
---|
533 | void Open(
|
---|
534 | const nsFileSpec& inFile,
|
---|
535 | int nsprMode = kDefaultMode,
|
---|
536 | PRIntn accessMode = 00666)
|
---|
537 | {
|
---|
538 | if (mFile)
|
---|
539 | mFile->Open(inFile, nsprMode, accessMode);
|
---|
540 | }
|
---|
541 |
|
---|
542 | // Input streamers. Unfortunately, they don't inherit!
|
---|
543 | nsInputStream& operator >> (char& ch)
|
---|
544 | { return nsInputStream::operator >>(ch); }
|
---|
545 | nsInputStream& operator >> (nsInputStream& (*pf)(nsInputStream&))
|
---|
546 | { return nsInputStream::operator >>(pf); }
|
---|
547 |
|
---|
548 | protected:
|
---|
549 | void AssignFrom(nsISupports* stream);
|
---|
550 |
|
---|
551 | private:
|
---|
552 |
|
---|
553 | // private and unimplemented to disallow copies and assigns
|
---|
554 | nsInputFileStream(const nsInputFileStream& rhs);
|
---|
555 | nsInputFileStream& operator=(const nsInputFileStream& rhs);
|
---|
556 |
|
---|
557 | // DATA
|
---|
558 | protected:
|
---|
559 | nsCOMPtr<nsIFileSpecInputStream> mFileInputStream;
|
---|
560 | }; // class nsInputFileStream
|
---|
561 |
|
---|
562 | //========================================================================================
|
---|
563 | class NS_COM_OBSOLETE nsRandomAccessOutputStream
|
---|
564 | // Please read the comments at the top of this file
|
---|
565 | //========================================================================================
|
---|
566 | : public nsRandomAccessStoreClient
|
---|
567 | , public nsOutputStream
|
---|
568 | {
|
---|
569 | public:
|
---|
570 | nsRandomAccessOutputStream(nsIOutputStream* inStream)
|
---|
571 | : nsRandomAccessStoreClient(do_QueryInterface(inStream))
|
---|
572 | , nsOutputStream(inStream)
|
---|
573 | {
|
---|
574 | }
|
---|
575 |
|
---|
576 | // Output streamers. Unfortunately, they don't inherit!
|
---|
577 | nsOutputStream& operator << (const char* buf)
|
---|
578 | { return nsOutputStream::operator << (buf); }
|
---|
579 | nsOutputStream& operator << (char ch)
|
---|
580 | { return nsOutputStream::operator << (ch); }
|
---|
581 | nsOutputStream& operator << (short val)
|
---|
582 | { return nsOutputStream::operator << (val); }
|
---|
583 | nsOutputStream& operator << (unsigned short val)
|
---|
584 | { return nsOutputStream::operator << (val); }
|
---|
585 | nsOutputStream& operator << (long val)
|
---|
586 | { return nsOutputStream::operator << (val); }
|
---|
587 | nsOutputStream& operator << (unsigned long val)
|
---|
588 | { return nsOutputStream::operator << (val); }
|
---|
589 | nsOutputStream& operator << (int val)
|
---|
590 | { return nsOutputStream::operator << (val); }
|
---|
591 | nsOutputStream& operator << (unsigned int val)
|
---|
592 | { return nsOutputStream::operator << (val); }
|
---|
593 | nsOutputStream& operator << (nsOutputStream& (*pf)(nsOutputStream&))
|
---|
594 | { return nsOutputStream::operator << (pf); }
|
---|
595 |
|
---|
596 | protected:
|
---|
597 | nsRandomAccessOutputStream()
|
---|
598 | : nsOutputStream(nsnull)
|
---|
599 | {
|
---|
600 | }
|
---|
601 |
|
---|
602 | private:
|
---|
603 |
|
---|
604 | // private and unimplemented to disallow copies and assigns
|
---|
605 | nsRandomAccessOutputStream(const nsRandomAccessOutputStream& rhs);
|
---|
606 | nsRandomAccessOutputStream& operator=(const nsRandomAccessOutputStream& rhs);
|
---|
607 |
|
---|
608 | }; // class nsRandomAccessOutputStream
|
---|
609 |
|
---|
610 | //========================================================================================
|
---|
611 | class NS_COM_OBSOLETE nsOutputFileStream
|
---|
612 | // Please read the comments at the top of this file
|
---|
613 | //========================================================================================
|
---|
614 | : public nsRandomAccessOutputStream
|
---|
615 | , public nsFileClient
|
---|
616 | {
|
---|
617 | public:
|
---|
618 | enum { kDefaultMode = (PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE) };
|
---|
619 |
|
---|
620 | nsOutputFileStream() {}
|
---|
621 | nsOutputFileStream(nsIOutputStream* inStream)
|
---|
622 | {
|
---|
623 | AssignFrom(inStream);
|
---|
624 | }
|
---|
625 | nsOutputFileStream(
|
---|
626 | const nsFileSpec& inFile,
|
---|
627 | int nsprMode = kDefaultMode,
|
---|
628 | PRIntn accessMode = 00666)
|
---|
629 | {
|
---|
630 | nsISupports* stream;
|
---|
631 | if (NS_FAILED(NS_NewIOFileStream(
|
---|
632 | &stream,
|
---|
633 | inFile, nsprMode, accessMode)))
|
---|
634 | return;
|
---|
635 | AssignFrom(stream);
|
---|
636 | NS_RELEASE(stream);
|
---|
637 | }
|
---|
638 | nsOutputFileStream(nsIFileSpec* inFile);
|
---|
639 | virtual ~nsOutputFileStream();
|
---|
640 |
|
---|
641 | virtual nsresult flush();
|
---|
642 | virtual void abort();
|
---|
643 |
|
---|
644 | // Output streamers. Unfortunately, they don't inherit!
|
---|
645 | nsOutputStream& operator << (const char* buf)
|
---|
646 | { return nsOutputStream::operator << (buf); }
|
---|
647 | nsOutputStream& operator << (char ch)
|
---|
648 | { return nsOutputStream::operator << (ch); }
|
---|
649 | nsOutputStream& operator << (short val)
|
---|
650 | { return nsOutputStream::operator << (val); }
|
---|
651 | nsOutputStream& operator << (unsigned short val)
|
---|
652 | { return nsOutputStream::operator << (val); }
|
---|
653 | nsOutputStream& operator << (long val)
|
---|
654 | { return nsOutputStream::operator << (val); }
|
---|
655 | nsOutputStream& operator << (unsigned long val)
|
---|
656 | { return nsOutputStream::operator << (val); }
|
---|
657 | nsOutputStream& operator << (int val)
|
---|
658 | { return nsOutputStream::operator << (val); }
|
---|
659 | nsOutputStream& operator << (unsigned int val)
|
---|
660 | { return nsOutputStream::operator << (val); }
|
---|
661 | nsOutputStream& operator << (nsOutputStream& (*pf)(nsOutputStream&))
|
---|
662 | { return nsOutputStream::operator << (pf); }
|
---|
663 |
|
---|
664 | protected:
|
---|
665 | void AssignFrom(nsISupports* stream);
|
---|
666 |
|
---|
667 | private:
|
---|
668 |
|
---|
669 | // private and unimplemented to disallow copies and assigns
|
---|
670 | nsOutputFileStream(const nsOutputFileStream& rhs);
|
---|
671 | nsOutputFileStream& operator=(const nsOutputFileStream& rhs);
|
---|
672 |
|
---|
673 | // DATA
|
---|
674 | protected:
|
---|
675 | nsCOMPtr<nsIFileSpecOutputStream> mFileOutputStream;
|
---|
676 | }; // class nsOutputFileStream
|
---|
677 |
|
---|
678 |
|
---|
679 | //========================================================================================
|
---|
680 | class nsIOFileStream
|
---|
681 | // Please read the comments at the top of this file
|
---|
682 | //========================================================================================
|
---|
683 | : public nsInputFileStream
|
---|
684 | , public nsOutputStream
|
---|
685 | {
|
---|
686 | public:
|
---|
687 | enum { kDefaultMode = (PR_RDWR | PR_CREATE_FILE) };
|
---|
688 |
|
---|
689 | nsIOFileStream(
|
---|
690 | nsIInputStream* inInputStream
|
---|
691 | , nsIOutputStream* inOutputStream)
|
---|
692 | : nsInputFileStream(inInputStream)
|
---|
693 | , nsOutputStream(inOutputStream)
|
---|
694 | , mFileOutputStream(do_QueryInterface(inOutputStream))
|
---|
695 | {
|
---|
696 | }
|
---|
697 | nsIOFileStream(
|
---|
698 | const nsFileSpec& inFile,
|
---|
699 | int nsprMode = kDefaultMode,
|
---|
700 | PRIntn accessMode = 00666)
|
---|
701 | : nsInputFileStream((nsIInputStream*)nsnull)
|
---|
702 | , nsOutputStream(nsnull)
|
---|
703 | {
|
---|
704 | nsISupports* stream;
|
---|
705 | if (NS_FAILED(NS_NewIOFileStream(
|
---|
706 | &stream,
|
---|
707 | inFile, nsprMode, accessMode)))
|
---|
708 | return;
|
---|
709 | mFile = do_QueryInterface(stream);
|
---|
710 | mStore = do_QueryInterface(stream);
|
---|
711 | mInputStream = do_QueryInterface(stream);
|
---|
712 | mOutputStream = do_QueryInterface(stream);
|
---|
713 | mFileInputStream = do_QueryInterface(stream);
|
---|
714 | mFileOutputStream = do_QueryInterface(stream);
|
---|
715 | NS_RELEASE(stream);
|
---|
716 | }
|
---|
717 |
|
---|
718 | virtual nsresult close()
|
---|
719 | {
|
---|
720 | // Doesn't matter which of the two we close:
|
---|
721 | // they're hooked up to the same file.
|
---|
722 | return nsInputFileStream::close();
|
---|
723 | }
|
---|
724 |
|
---|
725 | // Output streamers. Unfortunately, they don't inherit!
|
---|
726 | nsOutputStream& operator << (const char* buf)
|
---|
727 | { return nsOutputStream::operator << (buf); }
|
---|
728 | nsOutputStream& operator << (char ch)
|
---|
729 | { return nsOutputStream::operator << (ch); }
|
---|
730 | nsOutputStream& operator << (short val)
|
---|
731 | { return nsOutputStream::operator << (val); }
|
---|
732 | nsOutputStream& operator << (unsigned short val)
|
---|
733 | { return nsOutputStream::operator << (val); }
|
---|
734 | nsOutputStream& operator << (long val)
|
---|
735 | { return nsOutputStream::operator << (val); }
|
---|
736 | nsOutputStream& operator << (unsigned long val)
|
---|
737 | { return nsOutputStream::operator << (val); }
|
---|
738 | nsOutputStream& operator << (int val)
|
---|
739 | { return nsOutputStream::operator << (val); }
|
---|
740 | nsOutputStream& operator << (unsigned int val)
|
---|
741 | { return nsOutputStream::operator << (val); }
|
---|
742 | nsOutputStream& operator << (nsOutputStream& (*pf)(nsOutputStream&))
|
---|
743 | { return nsOutputStream::operator << (pf); }
|
---|
744 |
|
---|
745 | // Input streamers. Unfortunately, they don't inherit!
|
---|
746 | nsInputStream& operator >> (char& ch)
|
---|
747 | { return nsInputStream::operator >>(ch); }
|
---|
748 | nsInputStream& operator >> (nsInputStream& (*pf)(nsInputStream&))
|
---|
749 | { return nsInputStream::operator >>(pf); }
|
---|
750 |
|
---|
751 | virtual nsresult flush() {if (mFileOutputStream) mFileOutputStream->Flush(); return error(); }
|
---|
752 |
|
---|
753 |
|
---|
754 | private:
|
---|
755 |
|
---|
756 | // private and unimplemented to disallow copies and assigns
|
---|
757 | nsIOFileStream(const nsIOFileStream& rhs);
|
---|
758 | nsIOFileStream& operator=(const nsIOFileStream& rhs);
|
---|
759 |
|
---|
760 | // DATA
|
---|
761 | protected:
|
---|
762 | nsCOMPtr<nsIFileSpecOutputStream> mFileOutputStream;
|
---|
763 | }; // class nsIOFileStream
|
---|
764 |
|
---|
765 | //========================================================================================
|
---|
766 | // Manipulators
|
---|
767 | //========================================================================================
|
---|
768 |
|
---|
769 | NS_COM_OBSOLETE nsOutputStream& nsEndl(nsOutputStream& os); // outputs and FLUSHES.
|
---|
770 |
|
---|
771 | //========================================================================================
|
---|
772 | #endif /* _FILESTREAM_H_ */
|
---|