1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
---|
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) 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 the GNU General Public License Version 2 or later (the "GPL"), or |
---|
26 | * 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 NPL, 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 NPL, 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(PRInt32 offset) |
---|
391 | { |
---|
392 | seek(PR_SEEK_SET, offset); |
---|
393 | } |
---|
394 | |
---|
395 | void seek(PRSeekWhence whence, PRInt32 offset) |
---|
396 | { |
---|
397 | set_at_eof(PR_FALSE); |
---|
398 | if (mStore) |
---|
399 | mResult = mStore->Seek(whence, offset); |
---|
400 | } |
---|
401 | PRIntn tell() |
---|
402 | { |
---|
403 | PRIntn result = -1; |
---|
404 | if (mStore) |
---|
405 | mResult = mStore->Tell((PRUint32 *)&result); |
---|
406 | return result; |
---|
407 | } |
---|
408 | |
---|
409 | protected: |
---|
410 | |
---|
411 | virtual PRBool get_at_eof() const |
---|
412 | { |
---|
413 | PRBool result = PR_TRUE; |
---|
414 | if (mStore) |
---|
415 | mStore->GetAtEOF(&result); |
---|
416 | return result; |
---|
417 | } |
---|
418 | |
---|
419 | virtual void set_at_eof(PRBool atEnd) |
---|
420 | { |
---|
421 | if (mStore) |
---|
422 | mStore->SetAtEOF(atEnd); |
---|
423 | } |
---|
424 | |
---|
425 | private: |
---|
426 | |
---|
427 | // private and unimplemented to disallow copies and assigns |
---|
428 | nsRandomAccessStoreClient(const nsRandomAccessStoreClient& rhs); |
---|
429 | nsRandomAccessStoreClient& operator=(const nsRandomAccessStoreClient& rhs); |
---|
430 | |
---|
431 | // DATA |
---|
432 | protected: |
---|
433 | nsCOMPtr<nsIRandomAccessStore> mStore; |
---|
434 | }; // class nsRandomAccessStoreClient |
---|
435 | |
---|
436 | //======================================================================================== |
---|
437 | class NS_COM_OBSOLETE nsRandomAccessInputStream |
---|
438 | // Please read the comments at the top of this file |
---|
439 | //======================================================================================== |
---|
440 | : public nsRandomAccessStoreClient |
---|
441 | , public nsInputStream |
---|
442 | { |
---|
443 | public: |
---|
444 | nsRandomAccessInputStream(nsIInputStream* inStream) |
---|
445 | : nsRandomAccessStoreClient(do_QueryInterface(inStream)) |
---|
446 | , nsInputStream(inStream) |
---|
447 | { |
---|
448 | } |
---|
449 | PRBool readline(char* s, PRInt32 n); |
---|
450 | // Result always null-terminated. |
---|
451 | // Check eof() before each call. |
---|
452 | // CAUTION: false result only indicates line was truncated |
---|
453 | // to fit buffer, or an error occurred (OTHER THAN eof). |
---|
454 | |
---|
455 | // Input streamers. Unfortunately, they don't inherit! |
---|
456 | nsInputStream& operator >> (char& ch) |
---|
457 | { return nsInputStream::operator >>(ch); } |
---|
458 | nsInputStream& operator >> (nsInputStream& (*pf)(nsInputStream&)) |
---|
459 | { return nsInputStream::operator >>(pf); } |
---|
460 | |
---|
461 | protected: |
---|
462 | nsRandomAccessInputStream() |
---|
463 | : nsInputStream(nsnull) |
---|
464 | { |
---|
465 | } |
---|
466 | |
---|
467 | virtual PRBool get_at_eof() const |
---|
468 | { |
---|
469 | return nsRandomAccessStoreClient::get_at_eof(); |
---|
470 | } |
---|
471 | |
---|
472 | virtual void set_at_eof(PRBool atEnd) |
---|
473 | { |
---|
474 | nsRandomAccessStoreClient::set_at_eof(atEnd); |
---|
475 | } |
---|
476 | |
---|
477 | private: |
---|
478 | |
---|
479 | // private and unimplemented to disallow copies and assigns |
---|
480 | nsRandomAccessInputStream(const nsRandomAccessInputStream& rhs); |
---|
481 | nsRandomAccessInputStream& operator=(const nsRandomAccessInputStream& rhs); |
---|
482 | |
---|
483 | }; // class nsRandomAccessInputStream |
---|
484 | |
---|
485 | //======================================================================================== |
---|
486 | class NS_COM_OBSOLETE nsInputStringStream |
---|
487 | //======================================================================================== |
---|
488 | : public nsRandomAccessInputStream |
---|
489 | { |
---|
490 | public: |
---|
491 | nsInputStringStream(const char* stringToRead); |
---|
492 | nsInputStringStream(const nsString& stringToRead); |
---|
493 | |
---|
494 | // Input streamers. Unfortunately, they don't inherit! |
---|
495 | nsInputStream& operator >> (char& ch) |
---|
496 | { return nsInputStream::operator >>(ch); } |
---|
497 | nsInputStream& operator >> (nsInputStream& (*pf)(nsInputStream&)) |
---|
498 | { return nsInputStream::operator >>(pf); } |
---|
499 | |
---|
500 | |
---|
501 | private: |
---|
502 | |
---|
503 | // private and unimplemented to disallow copies and assigns |
---|
504 | nsInputStringStream(const nsInputStringStream& rhs); |
---|
505 | nsInputStringStream& operator=(const nsInputStringStream& rhs); |
---|
506 | |
---|
507 | |
---|
508 | }; // class nsInputStringStream |
---|
509 | |
---|
510 | //======================================================================================== |
---|
511 | class NS_COM_OBSOLETE nsInputFileStream |
---|
512 | // Please read the comments at the top of this file |
---|
513 | //======================================================================================== |
---|
514 | : public nsRandomAccessInputStream |
---|
515 | , public nsFileClient |
---|
516 | { |
---|
517 | public: |
---|
518 | enum { kDefaultMode = PR_RDONLY }; |
---|
519 | nsInputFileStream(nsIInputStream* inStream) |
---|
520 | : nsRandomAccessInputStream(inStream) |
---|
521 | , nsFileClient(do_QueryInterface(inStream)) |
---|
522 | , mFileInputStream(do_QueryInterface(inStream)) |
---|
523 | { |
---|
524 | } |
---|
525 | nsInputFileStream( |
---|
526 | const nsFileSpec& inFile, |
---|
527 | int nsprMode = kDefaultMode, |
---|
528 | PRIntn accessMode = 00666); |
---|
529 | nsInputFileStream(nsIFileSpec* inFile); |
---|
530 | virtual ~nsInputFileStream(); |
---|
531 | |
---|
532 | void Open( |
---|
533 | const nsFileSpec& inFile, |
---|
534 | int nsprMode = kDefaultMode, |
---|
535 | PRIntn accessMode = 00666) |
---|
536 | { |
---|
537 | if (mFile) |
---|
538 | mFile->Open(inFile, nsprMode, accessMode); |
---|
539 | } |
---|
540 | |
---|
541 | // Input streamers. Unfortunately, they don't inherit! |
---|
542 | nsInputStream& operator >> (char& ch) |
---|
543 | { return nsInputStream::operator >>(ch); } |
---|
544 | nsInputStream& operator >> (nsInputStream& (*pf)(nsInputStream&)) |
---|
545 | { return nsInputStream::operator >>(pf); } |
---|
546 | |
---|
547 | protected: |
---|
548 | void AssignFrom(nsISupports* stream); |
---|
549 | |
---|
550 | private: |
---|
551 | |
---|
552 | // private and unimplemented to disallow copies and assigns |
---|
553 | nsInputFileStream(const nsInputFileStream& rhs); |
---|
554 | nsInputFileStream& operator=(const nsInputFileStream& rhs); |
---|
555 | |
---|
556 | // DATA |
---|
557 | protected: |
---|
558 | nsCOMPtr<nsIFileSpecInputStream> mFileInputStream; |
---|
559 | }; // class nsInputFileStream |
---|
560 | |
---|
561 | //======================================================================================== |
---|
562 | class NS_COM_OBSOLETE nsRandomAccessOutputStream |
---|
563 | // Please read the comments at the top of this file |
---|
564 | //======================================================================================== |
---|
565 | : public nsRandomAccessStoreClient |
---|
566 | , public nsOutputStream |
---|
567 | { |
---|
568 | public: |
---|
569 | nsRandomAccessOutputStream(nsIOutputStream* inStream) |
---|
570 | : nsRandomAccessStoreClient(do_QueryInterface(inStream)) |
---|
571 | , nsOutputStream(inStream) |
---|
572 | { |
---|
573 | } |
---|
574 | |
---|
575 | // Output streamers. Unfortunately, they don't inherit! |
---|
576 | nsOutputStream& operator << (const char* buf) |
---|
577 | { return nsOutputStream::operator << (buf); } |
---|
578 | nsOutputStream& operator << (char ch) |
---|
579 | { return nsOutputStream::operator << (ch); } |
---|
580 | nsOutputStream& operator << (short val) |
---|
581 | { return nsOutputStream::operator << (val); } |
---|
582 | nsOutputStream& operator << (unsigned short val) |
---|
583 | { return nsOutputStream::operator << (val); } |
---|
584 | nsOutputStream& operator << (long val) |
---|
585 | { return nsOutputStream::operator << (val); } |
---|
586 | nsOutputStream& operator << (unsigned long val) |
---|
587 | { return nsOutputStream::operator << (val); } |
---|
588 | nsOutputStream& operator << (int val) |
---|
589 | { return nsOutputStream::operator << (val); } |
---|
590 | nsOutputStream& operator << (unsigned int val) |
---|
591 | { return nsOutputStream::operator << (val); } |
---|
592 | nsOutputStream& operator << (nsOutputStream& (*pf)(nsOutputStream&)) |
---|
593 | { return nsOutputStream::operator << (pf); } |
---|
594 | |
---|
595 | protected: |
---|
596 | nsRandomAccessOutputStream() |
---|
597 | : nsOutputStream(nsnull) |
---|
598 | { |
---|
599 | } |
---|
600 | |
---|
601 | private: |
---|
602 | |
---|
603 | // private and unimplemented to disallow copies and assigns |
---|
604 | nsRandomAccessOutputStream(const nsRandomAccessOutputStream& rhs); |
---|
605 | nsRandomAccessOutputStream& operator=(const nsRandomAccessOutputStream& rhs); |
---|
606 | |
---|
607 | }; // class nsRandomAccessOutputStream |
---|
608 | |
---|
609 | //======================================================================================== |
---|
610 | class NS_COM_OBSOLETE nsOutputFileStream |
---|
611 | // Please read the comments at the top of this file |
---|
612 | //======================================================================================== |
---|
613 | : public nsRandomAccessOutputStream |
---|
614 | , public nsFileClient |
---|
615 | { |
---|
616 | public: |
---|
617 | enum { kDefaultMode = (PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE) }; |
---|
618 | |
---|
619 | nsOutputFileStream() {} |
---|
620 | nsOutputFileStream(nsIOutputStream* inStream) |
---|
621 | { |
---|
622 | AssignFrom(inStream); |
---|
623 | } |
---|
624 | nsOutputFileStream( |
---|
625 | const nsFileSpec& inFile, |
---|
626 | int nsprMode = kDefaultMode, |
---|
627 | PRIntn accessMode = 00666) |
---|
628 | { |
---|
629 | nsISupports* stream; |
---|
630 | if (NS_FAILED(NS_NewIOFileStream( |
---|
631 | &stream, |
---|
632 | inFile, nsprMode, accessMode))) |
---|
633 | return; |
---|
634 | AssignFrom(stream); |
---|
635 | NS_RELEASE(stream); |
---|
636 | } |
---|
637 | nsOutputFileStream(nsIFileSpec* inFile); |
---|
638 | virtual ~nsOutputFileStream(); |
---|
639 | |
---|
640 | virtual nsresult flush(); |
---|
641 | virtual void abort(); |
---|
642 | |
---|
643 | // Output streamers. Unfortunately, they don't inherit! |
---|
644 | nsOutputStream& operator << (const char* buf) |
---|
645 | { return nsOutputStream::operator << (buf); } |
---|
646 | nsOutputStream& operator << (char ch) |
---|
647 | { return nsOutputStream::operator << (ch); } |
---|
648 | nsOutputStream& operator << (short val) |
---|
649 | { return nsOutputStream::operator << (val); } |
---|
650 | nsOutputStream& operator << (unsigned short val) |
---|
651 | { return nsOutputStream::operator << (val); } |
---|
652 | nsOutputStream& operator << (long val) |
---|
653 | { return nsOutputStream::operator << (val); } |
---|
654 | nsOutputStream& operator << (unsigned long val) |
---|
655 | { return nsOutputStream::operator << (val); } |
---|
656 | nsOutputStream& operator << (int val) |
---|
657 | { return nsOutputStream::operator << (val); } |
---|
658 | nsOutputStream& operator << (unsigned int val) |
---|
659 | { return nsOutputStream::operator << (val); } |
---|
660 | nsOutputStream& operator << (nsOutputStream& (*pf)(nsOutputStream&)) |
---|
661 | { return nsOutputStream::operator << (pf); } |
---|
662 | |
---|
663 | protected: |
---|
664 | void AssignFrom(nsISupports* stream); |
---|
665 | |
---|
666 | private: |
---|
667 | |
---|
668 | // private and unimplemented to disallow copies and assigns |
---|
669 | nsOutputFileStream(const nsOutputFileStream& rhs); |
---|
670 | nsOutputFileStream& operator=(const nsOutputFileStream& rhs); |
---|
671 | |
---|
672 | // DATA |
---|
673 | protected: |
---|
674 | nsCOMPtr<nsIFileSpecOutputStream> mFileOutputStream; |
---|
675 | }; // class nsOutputFileStream |
---|
676 | |
---|
677 | |
---|
678 | //======================================================================================== |
---|
679 | class nsIOFileStream |
---|
680 | // Please read the comments at the top of this file |
---|
681 | //======================================================================================== |
---|
682 | : public nsInputFileStream |
---|
683 | , public nsOutputStream |
---|
684 | { |
---|
685 | public: |
---|
686 | enum { kDefaultMode = (PR_RDWR | PR_CREATE_FILE) }; |
---|
687 | |
---|
688 | nsIOFileStream( |
---|
689 | nsIInputStream* inInputStream |
---|
690 | , nsIOutputStream* inOutputStream) |
---|
691 | : nsInputFileStream(inInputStream) |
---|
692 | , nsOutputStream(inOutputStream) |
---|
693 | , mFileOutputStream(do_QueryInterface(inOutputStream)) |
---|
694 | { |
---|
695 | } |
---|
696 | nsIOFileStream( |
---|
697 | const nsFileSpec& inFile, |
---|
698 | int nsprMode = kDefaultMode, |
---|
699 | PRIntn accessMode = 00666) |
---|
700 | : nsInputFileStream((nsIInputStream*)nsnull) |
---|
701 | , nsOutputStream(nsnull) |
---|
702 | { |
---|
703 | nsISupports* stream; |
---|
704 | if (NS_FAILED(NS_NewIOFileStream( |
---|
705 | &stream, |
---|
706 | inFile, nsprMode, accessMode))) |
---|
707 | return; |
---|
708 | mFile = do_QueryInterface(stream); |
---|
709 | mStore = do_QueryInterface(stream); |
---|
710 | mInputStream = do_QueryInterface(stream); |
---|
711 | mOutputStream = do_QueryInterface(stream); |
---|
712 | mFileInputStream = do_QueryInterface(stream); |
---|
713 | mFileOutputStream = do_QueryInterface(stream); |
---|
714 | NS_RELEASE(stream); |
---|
715 | } |
---|
716 | |
---|
717 | virtual nsresult close() |
---|
718 | { |
---|
719 | // Doesn't matter which of the two we close: |
---|
720 | // they're hooked up to the same file. |
---|
721 | return nsInputFileStream::close(); |
---|
722 | } |
---|
723 | |
---|
724 | // Output streamers. Unfortunately, they don't inherit! |
---|
725 | nsOutputStream& operator << (const char* buf) |
---|
726 | { return nsOutputStream::operator << (buf); } |
---|
727 | nsOutputStream& operator << (char ch) |
---|
728 | { return nsOutputStream::operator << (ch); } |
---|
729 | nsOutputStream& operator << (short val) |
---|
730 | { return nsOutputStream::operator << (val); } |
---|
731 | nsOutputStream& operator << (unsigned short val) |
---|
732 | { return nsOutputStream::operator << (val); } |
---|
733 | nsOutputStream& operator << (long val) |
---|
734 | { return nsOutputStream::operator << (val); } |
---|
735 | nsOutputStream& operator << (unsigned long val) |
---|
736 | { return nsOutputStream::operator << (val); } |
---|
737 | nsOutputStream& operator << (int val) |
---|
738 | { return nsOutputStream::operator << (val); } |
---|
739 | nsOutputStream& operator << (unsigned int val) |
---|
740 | { return nsOutputStream::operator << (val); } |
---|
741 | nsOutputStream& operator << (nsOutputStream& (*pf)(nsOutputStream&)) |
---|
742 | { return nsOutputStream::operator << (pf); } |
---|
743 | |
---|
744 | // Input streamers. Unfortunately, they don't inherit! |
---|
745 | nsInputStream& operator >> (char& ch) |
---|
746 | { return nsInputStream::operator >>(ch); } |
---|
747 | nsInputStream& operator >> (nsInputStream& (*pf)(nsInputStream&)) |
---|
748 | { return nsInputStream::operator >>(pf); } |
---|
749 | |
---|
750 | virtual nsresult flush() {if (mFileOutputStream) mFileOutputStream->Flush(); return error(); } |
---|
751 | |
---|
752 | |
---|
753 | private: |
---|
754 | |
---|
755 | // private and unimplemented to disallow copies and assigns |
---|
756 | nsIOFileStream(const nsIOFileStream& rhs); |
---|
757 | nsIOFileStream& operator=(const nsIOFileStream& rhs); |
---|
758 | |
---|
759 | // DATA |
---|
760 | protected: |
---|
761 | nsCOMPtr<nsIFileSpecOutputStream> mFileOutputStream; |
---|
762 | }; // class nsIOFileStream |
---|
763 | |
---|
764 | //======================================================================================== |
---|
765 | // Manipulators |
---|
766 | //======================================================================================== |
---|
767 | |
---|
768 | NS_COM_OBSOLETE nsOutputStream& nsEndl(nsOutputStream& os); // outputs and FLUSHES. |
---|
769 | |
---|
770 | //======================================================================================== |
---|
771 | #endif /* _FILESTREAM_H_ */ |
---|