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
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/
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) 1998
20 * the Initial Developer. All Rights Reserved.
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.
36 * ***** END LICENSE BLOCK ***** */
38 // First checked in on 98/12/08 by John R. McMullen.
39 // Since nsFileStream.h is entirely templates, common code (such as open())
40 // which does not actually depend on the charT, can be placed here.
42 #include "nsFileStream.h"
43 #include "nsFileSpec.h"
44 #include "nsIFileSpec.h"
45 #include "nsIStringStream.h"
51 //========================================================================================
53 //========================================================================================
55 //----------------------------------------------------------------------------------------
56 nsInputStream::~nsInputStream()
57 //----------------------------------------------------------------------------------------
61 //----------------------------------------------------------------------------------------
62 char nsInputStream::get()
63 //----------------------------------------------------------------------------------------
66 if (read(&c
, sizeof(c
)) == sizeof(c
))
71 //----------------------------------------------------------------------------------------
72 PRInt32
nsInputStream::read(void* s
, PRInt32 n
)
73 //----------------------------------------------------------------------------------------
78 PRInt32 status
= mInputStream
->Read((char*)s
, n
, (PRUint32
*)&result
);
84 } // nsInputStream::read
86 //----------------------------------------------------------------------------------------
87 static void TidyEndOfLine(char*& cp
)
88 // Assumes that cp is pointing at \n or \r. Nulls out the character, checks for
89 // a second terminator (of the opposite persuasion), and returns cp pointing past the
90 // entire eol construct (one or two characters).
91 //----------------------------------------------------------------------------------------
94 *cp
++ = '\0'; // terminate at the newline, then skip past it
95 if ((ch
== '\n' && *cp
== '\r') || (ch
== '\r' && *cp
== '\n'))
96 cp
++; // possibly a pair.
99 //----------------------------------------------------------------------------------------
100 nsInputStream
& nsInputStream::operator >> (char& c
)
101 //----------------------------------------------------------------------------------------
107 //========================================================================================
109 //========================================================================================
111 //----------------------------------------------------------------------------------------
112 nsOutputStream::~nsOutputStream()
113 //----------------------------------------------------------------------------------------
117 //----------------------------------------------------------------------------------------
118 void nsOutputStream::put(char c
)
119 //----------------------------------------------------------------------------------------
121 write(&c
, sizeof(c
));
124 //----------------------------------------------------------------------------------------
125 PRInt32
nsOutputStream::write(const void* s
, PRInt32 n
)
126 //----------------------------------------------------------------------------------------
131 mWriteStatus
= mOutputStream
->Write((char*)s
, n
, (PRUint32
*)&result
);
133 } // nsOutputStream::write
135 //----------------------------------------------------------------------------------------
136 nsresult
nsOutputStream::flush()
137 //----------------------------------------------------------------------------------------
142 //----------------------------------------------------------------------------------------
143 nsresult
nsOutputStream::lastWriteStatus()
144 //----------------------------------------------------------------------------------------
149 //----------------------------------------------------------------------------------------
150 nsOutputStream
& nsOutputStream::operator << (char c
)
151 //----------------------------------------------------------------------------------------
157 //----------------------------------------------------------------------------------------
158 nsOutputStream
& nsOutputStream::operator << (const char* s
)
159 //----------------------------------------------------------------------------------------
166 //----------------------------------------------------------------------------------------
167 nsOutputStream
& nsOutputStream::operator << (short val
)
168 //----------------------------------------------------------------------------------------
171 sprintf(buf
, "%hd", val
);
172 return (*this << buf
);
175 //----------------------------------------------------------------------------------------
176 nsOutputStream
& nsOutputStream::operator << (unsigned short val
)
177 //----------------------------------------------------------------------------------------
180 sprintf(buf
, "%hu", val
);
181 return (*this << buf
);
184 //----------------------------------------------------------------------------------------
185 nsOutputStream
& nsOutputStream::operator << (long val
)
186 //----------------------------------------------------------------------------------------
189 sprintf(buf
, "%ld", val
);
190 return (*this << buf
);
193 //----------------------------------------------------------------------------------------
194 nsOutputStream
& nsOutputStream::operator << (unsigned long val
)
195 //----------------------------------------------------------------------------------------
198 sprintf(buf
, "%lu", val
);
199 return (*this << buf
);
202 //----------------------------------------------------------------------------------------
203 nsOutputStream
& nsOutputStream::operator << (int val
)
204 //----------------------------------------------------------------------------------------
207 sprintf(buf
, "%d", val
);
208 return (*this << buf
);
211 //----------------------------------------------------------------------------------------
212 nsOutputStream
& nsOutputStream::operator << (unsigned int val
)
213 //----------------------------------------------------------------------------------------
216 sprintf(buf
, "%u", val
);
217 return (*this << buf
);
220 //========================================================================================
221 // nsRandomAccessInputStream
222 //========================================================================================
224 //----------------------------------------------------------------------------------------
225 PRBool
nsRandomAccessInputStream::readline(char* s
, PRInt32 n
)
226 // This will truncate if the buffer is too small. Result will always be null-terminated.
227 //----------------------------------------------------------------------------------------
229 PRBool bufferLargeEnough
= PR_TRUE
; // result
233 nsInt64 position
= tell();
234 const nsInt64
zero(0);
237 PRInt32 bytesRead
= read(s
, n
- 1);
238 if (failed() || bytesRead
< 0)
240 s
[bytesRead
] = '\0'; // always terminate at the end of the buffer
241 char* tp
= strpbrk(s
, "\n\r");
245 bytesRead
= (tp
- s
);
247 else if (!eof() && n
-1 == bytesRead
)
248 bufferLargeEnough
= PR_FALSE
;
249 position
+= bytesRead
;
251 return bufferLargeEnough
;
252 } // nsRandomAccessInputStream::readline
254 //========================================================================================
256 //========================================================================================
258 //----------------------------------------------------------------------------------------
259 nsInputFileStream::nsInputFileStream(
260 const nsFileSpec
& inFile
,
263 //----------------------------------------------------------------------------------------
266 if (NS_FAILED(NS_NewIOFileStream(&stream
, inFile
, nsprMode
, accessMode
)))
270 } // nsInputFileStream::nsInputFileStream
272 //----------------------------------------------------------------------------------------
273 nsInputFileStream::nsInputFileStream(nsIFileSpec
* inSpec
)
274 //----------------------------------------------------------------------------------------
276 nsIInputStream
* stream
;
277 if (NS_FAILED(inSpec
->GetInputStream(&stream
)))
281 } // nsInputFileStream::nsInputFileStream
283 //----------------------------------------------------------------------------------------
284 nsInputFileStream::~nsInputFileStream()
285 //----------------------------------------------------------------------------------------
291 //----------------------------------------------------------------------------------------
292 void nsInputFileStream::AssignFrom(nsISupports
* stream
)
293 //----------------------------------------------------------------------------------------
295 mFile
= do_QueryInterface(stream
);
296 mInputStream
= do_QueryInterface(stream
);
297 mStore
= do_QueryInterface(stream
);
298 mFileInputStream
= do_QueryInterface(stream
);
301 //========================================================================================
302 // nsOutputFileStream
303 //========================================================================================
305 //----------------------------------------------------------------------------------------
306 nsOutputFileStream::nsOutputFileStream(nsIFileSpec
* inSpec
)
307 //----------------------------------------------------------------------------------------
311 nsIOutputStream
* stream
;
312 if (NS_FAILED(inSpec
->GetOutputStream(&stream
)))
318 //----------------------------------------------------------------------------------------
319 nsOutputFileStream::~nsOutputFileStream()
320 //----------------------------------------------------------------------------------------
325 //----------------------------------------------------------------------------------------
326 void nsOutputFileStream::AssignFrom(nsISupports
* stream
)
327 //----------------------------------------------------------------------------------------
329 mFile
= do_QueryInterface(stream
);
330 mOutputStream
= do_QueryInterface(stream
);
331 mStore
= do_QueryInterface(stream
);
332 mFileOutputStream
= do_QueryInterface(stream
);
335 //----------------------------------------------------------------------------------------
336 nsresult
nsOutputFileStream::flush()
337 //----------------------------------------------------------------------------------------
339 if (mFileOutputStream
)
340 mFileOutputStream
->Flush();
344 //----------------------------------------------------------------------------------------
345 void nsOutputFileStream::abort()
346 //----------------------------------------------------------------------------------------
348 mResult
= NS_FILE_FAILURE
;
352 //========================================================================================
354 //========================================================================================
356 //----------------------------------------------------------------------------------------
357 nsOutputStream
& nsEndl(nsOutputStream
& os
)
358 //----------------------------------------------------------------------------------------
360 #if defined(XP_WIN) || defined(XP_OS2)