1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 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 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 : #ifndef nsFileStreams_h__
39 : #define nsFileStreams_h__
40 :
41 : #include "nsAlgorithm.h"
42 : #include "nsIFileStreams.h"
43 : #include "nsIFile.h"
44 : #include "nsIInputStream.h"
45 : #include "nsIOutputStream.h"
46 : #include "nsISafeOutputStream.h"
47 : #include "nsISeekableStream.h"
48 : #include "nsILineInputStream.h"
49 : #include "nsCOMPtr.h"
50 : #include "prlog.h"
51 : #include "prio.h"
52 : #include "nsIIPCSerializable.h"
53 :
54 : template<class CharType> class nsLineBuffer;
55 :
56 : ////////////////////////////////////////////////////////////////////////////////
57 :
58 : class nsFileStream : public nsISeekableStream
59 : {
60 : public:
61 : NS_DECL_ISUPPORTS
62 : NS_DECL_NSISEEKABLESTREAM
63 :
64 : nsFileStream();
65 : virtual ~nsFileStream();
66 :
67 : nsresult Close();
68 :
69 : protected:
70 : PRFileDesc* mFD;
71 :
72 : /**
73 : * Flags describing our behavior. See the IDL file for possible values.
74 : */
75 : PRInt32 mBehaviorFlags;
76 :
77 : /**
78 : * Whether we have a pending open (see DEFER_OPEN in the IDL file).
79 : */
80 : bool mDeferredOpen;
81 :
82 39214 : struct OpenParams {
83 : nsCOMPtr<nsILocalFile> localFile;
84 : PRInt32 ioFlags;
85 : PRInt32 perm;
86 : };
87 :
88 : /**
89 : * Data we need to do an open.
90 : */
91 : OpenParams mOpenParams;
92 :
93 : /**
94 : * Prepares the data we need to open the file, and either does the open now
95 : * by calling DoOpen(), or leaves it to be opened later by a call to
96 : * DoPendingOpen().
97 : */
98 : nsresult MaybeOpen(nsILocalFile* aFile, PRInt32 aIoFlags, PRInt32 aPerm,
99 : bool aDeferred);
100 :
101 : /**
102 : * Cleans up data prepared in MaybeOpen.
103 : */
104 : void CleanUpOpen();
105 :
106 : /**
107 : * Open the file. This is called either from MaybeOpen (during Init)
108 : * or from DoPendingOpen (if DEFER_OPEN is used when initializing this
109 : * stream). The default behavior of DoOpen is to open the file and save the
110 : * file descriptor.
111 : */
112 : virtual nsresult DoOpen();
113 :
114 : /**
115 : * If there is a pending open, do it now. It's important for this to be
116 : * inline since we do it in almost every stream API call.
117 : */
118 : inline nsresult DoPendingOpen();
119 : };
120 :
121 : ////////////////////////////////////////////////////////////////////////////////
122 :
123 : class nsFileInputStream : public nsFileStream,
124 : public nsIFileInputStream,
125 : public nsILineInputStream,
126 : public nsIIPCSerializable
127 : {
128 : public:
129 : NS_DECL_ISUPPORTS_INHERITED
130 : NS_DECL_NSIINPUTSTREAM
131 : NS_DECL_NSIFILEINPUTSTREAM
132 : NS_DECL_NSILINEINPUTSTREAM
133 : NS_DECL_NSIIPCSERIALIZABLE
134 :
135 : // Overrided from nsFileStream
136 : NS_IMETHOD Seek(PRInt32 aWhence, PRInt64 aOffset);
137 :
138 15738 : nsFileInputStream() : nsFileStream()
139 : {
140 15738 : mLineBuffer = nsnull;
141 15738 : }
142 31414 : virtual ~nsFileInputStream()
143 31476 : {
144 15738 : Close();
145 62828 : }
146 :
147 : static nsresult
148 : Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
149 :
150 : protected:
151 : nsLineBuffer<char> *mLineBuffer;
152 :
153 : /**
154 : * The file being opened.
155 : */
156 : nsCOMPtr<nsIFile> mFile;
157 : /**
158 : * The IO flags passed to Init() for the file open.
159 : */
160 : PRInt32 mIOFlags;
161 : /**
162 : * The permissions passed to Init() for the file open.
163 : */
164 : PRInt32 mPerm;
165 :
166 : protected:
167 : /**
168 : * Internal, called to open a file. Parameters are the same as their
169 : * Init() analogues.
170 : */
171 : nsresult Open(nsIFile* file, PRInt32 ioFlags, PRInt32 perm);
172 : /**
173 : * Reopen the file (for OPEN_ON_READ only!)
174 : */
175 4 : nsresult Reopen() { return Open(mFile, mIOFlags, mPerm); }
176 : };
177 :
178 : ////////////////////////////////////////////////////////////////////////////////
179 :
180 : class nsPartialFileInputStream : public nsFileInputStream,
181 : public nsIPartialFileInputStream
182 310 : {
183 : public:
184 : NS_DECL_ISUPPORTS_INHERITED
185 : NS_DECL_NSIPARTIALFILEINPUTSTREAM
186 :
187 : NS_IMETHOD Tell(PRInt64 *aResult);
188 : NS_IMETHOD Available(PRUint32 *aResult);
189 : NS_IMETHOD Read(char* aBuf, PRUint32 aCount, PRUint32* aResult);
190 : NS_IMETHOD Seek(PRInt32 aWhence, PRInt64 aOffset);
191 :
192 : static nsresult
193 : Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
194 :
195 : private:
196 2202 : PRUint32 TruncateSize(PRUint32 aSize) {
197 2202 : return (PRUint32)NS_MIN<PRUint64>(mLength - mPosition, aSize);
198 : }
199 :
200 : PRUint64 mStart;
201 : PRUint64 mLength;
202 : PRUint64 mPosition;
203 : };
204 :
205 : ////////////////////////////////////////////////////////////////////////////////
206 :
207 : class nsFileOutputStream : public nsFileStream,
208 : public nsIFileOutputStream
209 : {
210 : public:
211 : NS_DECL_ISUPPORTS_INHERITED
212 : NS_DECL_NSIOUTPUTSTREAM
213 : NS_DECL_NSIFILEOUTPUTSTREAM
214 :
215 3869 : nsFileOutputStream() : nsFileStream() {}
216 13002 : virtual ~nsFileOutputStream() { nsFileOutputStream::Close(); }
217 :
218 : static nsresult
219 : Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
220 : };
221 :
222 : ////////////////////////////////////////////////////////////////////////////////
223 :
224 : class nsSafeFileOutputStream : public nsFileOutputStream,
225 : public nsISafeOutputStream
226 : {
227 : public:
228 : NS_DECL_ISUPPORTS_INHERITED
229 : NS_DECL_NSISAFEOUTPUTSTREAM
230 :
231 1237 : nsSafeFileOutputStream() :
232 : mTargetFileExists(true),
233 1237 : mWriteResult(NS_OK) {}
234 :
235 4948 : virtual ~nsSafeFileOutputStream() { nsSafeFileOutputStream::Close(); }
236 :
237 : virtual nsresult DoOpen();
238 :
239 : NS_IMETHODIMP Close();
240 : NS_IMETHODIMP Write(const char *buf, PRUint32 count, PRUint32 *result);
241 : NS_IMETHODIMP Init(nsIFile* file, PRInt32 ioFlags, PRInt32 perm, PRInt32 behaviorFlags);
242 :
243 : protected:
244 : nsCOMPtr<nsIFile> mTargetFile;
245 : nsCOMPtr<nsIFile> mTempFile;
246 :
247 : bool mTargetFileExists;
248 : nsresult mWriteResult; // Internally set in Write()
249 : };
250 :
251 : ////////////////////////////////////////////////////////////////////////////////
252 :
253 : #endif // nsFileStreams_h__
|