VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/obsolete/nsFileStream.cpp@ 4837

Last change on this file since 4837 was 1, checked in by vboxsync, 54 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.4 KB
Line 
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/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.
41
42#include "nsFileStream.h"
43#include "nsFileSpec.h"
44#include "nsIFileSpec.h"
45#include "nsIStringStream.h"
46#include "nsInt64.h"
47#include <string.h>
48#include <stdio.h>
49
50
51//========================================================================================
52// nsInputStream
53//========================================================================================
54
55//----------------------------------------------------------------------------------------
56nsInputStream::~nsInputStream()
57//----------------------------------------------------------------------------------------
58{
59}
60
61//----------------------------------------------------------------------------------------
62char nsInputStream::get()
63//----------------------------------------------------------------------------------------
64{
65 char c;
66 if (read(&c, sizeof(c)) == sizeof(c))
67 return c;
68 return 0;
69}
70
71//----------------------------------------------------------------------------------------
72PRInt32 nsInputStream::read(void* s, PRInt32 n)
73//----------------------------------------------------------------------------------------
74{
75 if (!mInputStream)
76 return 0;
77 PRInt32 result = 0;
78 mInputStream->Read((char*)s, n, (PRUint32*)&result);
79 if (result == 0)
80 set_at_eof(PR_TRUE);
81 return result;
82} // nsInputStream::read
83
84//----------------------------------------------------------------------------------------
85static void TidyEndOfLine(char*& cp)
86// Assumes that cp is pointing at \n or \r. Nulls out the character, checks for
87// a second terminator (of the opposite persuasion), and returns cp pointing past the
88// entire eol construct (one or two characters).
89//----------------------------------------------------------------------------------------
90{
91 char ch = *cp;
92 *cp++ = '\0'; // terminate at the newline, then skip past it
93 if ((ch == '\n' && *cp == '\r') || (ch == '\r' && *cp == '\n'))
94 cp++; // possibly a pair.
95}
96
97//----------------------------------------------------------------------------------------
98nsInputStream& nsInputStream::operator >> (char& c)
99//----------------------------------------------------------------------------------------
100{
101 c = get();
102 return *this;
103}
104
105//========================================================================================
106// nsOutputStream
107//========================================================================================
108
109//----------------------------------------------------------------------------------------
110nsOutputStream::~nsOutputStream()
111//----------------------------------------------------------------------------------------
112{
113}
114
115//----------------------------------------------------------------------------------------
116void nsOutputStream::put(char c)
117//----------------------------------------------------------------------------------------
118{
119 write(&c, sizeof(c));
120}
121
122//----------------------------------------------------------------------------------------
123PRInt32 nsOutputStream::write(const void* s, PRInt32 n)
124//----------------------------------------------------------------------------------------
125{
126 if (!mOutputStream)
127 return 0;
128 PRInt32 result = 0;
129 mWriteStatus = mOutputStream->Write((char*)s, n, (PRUint32*)&result);
130 return result;
131} // nsOutputStream::write
132
133//----------------------------------------------------------------------------------------
134nsresult nsOutputStream::flush()
135//----------------------------------------------------------------------------------------
136{
137 return NS_OK;
138}
139
140//----------------------------------------------------------------------------------------
141nsresult nsOutputStream::lastWriteStatus()
142//----------------------------------------------------------------------------------------
143{
144 return mWriteStatus;
145}
146
147//----------------------------------------------------------------------------------------
148nsOutputStream& nsOutputStream::operator << (char c)
149//----------------------------------------------------------------------------------------
150{
151 put(c);
152 return *this;
153}
154
155//----------------------------------------------------------------------------------------
156nsOutputStream& nsOutputStream::operator << (const char* s)
157//----------------------------------------------------------------------------------------
158{
159 if (s)
160 write(s, strlen(s));
161 return *this;
162}
163
164//----------------------------------------------------------------------------------------
165nsOutputStream& nsOutputStream::operator << (short val)
166//----------------------------------------------------------------------------------------
167{
168 char buf[30];
169 sprintf(buf, "%hd", val);
170 return (*this << buf);
171}
172
173//----------------------------------------------------------------------------------------
174nsOutputStream& nsOutputStream::operator << (unsigned short val)
175//----------------------------------------------------------------------------------------
176{
177 char buf[30];
178 sprintf(buf, "%hu", val);
179 return (*this << buf);
180}
181
182//----------------------------------------------------------------------------------------
183nsOutputStream& nsOutputStream::operator << (long val)
184//----------------------------------------------------------------------------------------
185{
186 char buf[30];
187 sprintf(buf, "%ld", val);
188 return (*this << buf);
189}
190
191//----------------------------------------------------------------------------------------
192nsOutputStream& nsOutputStream::operator << (unsigned long val)
193//----------------------------------------------------------------------------------------
194{
195 char buf[30];
196 sprintf(buf, "%lu", val);
197 return (*this << buf);
198}
199
200//----------------------------------------------------------------------------------------
201nsOutputStream& nsOutputStream::operator << (int val)
202//----------------------------------------------------------------------------------------
203{
204 char buf[30];
205 sprintf(buf, "%d", val);
206 return (*this << buf);
207}
208
209//----------------------------------------------------------------------------------------
210nsOutputStream& nsOutputStream::operator << (unsigned int val)
211//----------------------------------------------------------------------------------------
212{
213 char buf[30];
214 sprintf(buf, "%u", val);
215 return (*this << buf);
216}
217
218//========================================================================================
219// nsRandomAccessInputStream
220//========================================================================================
221
222//----------------------------------------------------------------------------------------
223PRBool nsRandomAccessInputStream::readline(char* s, PRInt32 n)
224// This will truncate if the buffer is too small. Result will always be null-terminated.
225//----------------------------------------------------------------------------------------
226{
227 PRBool bufferLargeEnough = PR_TRUE; // result
228 if (!s || !n)
229 return PR_TRUE;
230
231 nsInt64 position = tell();
232 const nsInt64 zero(0);
233 if (position < zero)
234 return PR_FALSE;
235 PRInt32 bytesRead = read(s, n - 1);
236 if (failed())
237 return PR_FALSE;
238 s[bytesRead] = '\0'; // always terminate at the end of the buffer
239 char* tp = strpbrk(s, "\n\r");
240 if (tp)
241 {
242 TidyEndOfLine(tp);
243 bytesRead = (tp - s);
244 }
245 else if (!eof() && n-1 == bytesRead)
246 bufferLargeEnough = PR_FALSE;
247 position += bytesRead;
248 seek(position);
249 return bufferLargeEnough;
250} // nsRandomAccessInputStream::readline
251
252//========================================================================================
253// nsInputStringStream
254//========================================================================================
255
256//----------------------------------------------------------------------------------------
257nsInputStringStream::nsInputStringStream(const char* stringToRead)
258//----------------------------------------------------------------------------------------
259{
260 nsCOMPtr<nsIInputStream> stream;
261 if (NS_FAILED(NS_NewCharInputStream(getter_AddRefs(stream), stringToRead)))
262 return;
263 mInputStream = stream;
264 mStore = do_QueryInterface(stream);
265}
266
267//----------------------------------------------------------------------------------------
268nsInputStringStream::nsInputStringStream(const nsString& stringToRead)
269//----------------------------------------------------------------------------------------
270{
271 if (NS_FAILED(NS_NewStringInputStream(getter_AddRefs(mInputStream), stringToRead)))
272 return;
273 mStore = do_QueryInterface(mInputStream);
274}
275
276
277//========================================================================================
278// nsInputFileStream
279//========================================================================================
280
281//----------------------------------------------------------------------------------------
282nsInputFileStream::nsInputFileStream(
283 const nsFileSpec& inFile,
284 int nsprMode,
285 PRIntn accessMode)
286//----------------------------------------------------------------------------------------
287{
288 nsISupports* stream;
289 if (NS_FAILED(NS_NewIOFileStream(&stream, inFile, nsprMode, accessMode)))
290 return;
291 AssignFrom(stream);
292 NS_RELEASE(stream);
293} // nsInputFileStream::nsInputFileStream
294
295//----------------------------------------------------------------------------------------
296nsInputFileStream::nsInputFileStream(nsIFileSpec* inSpec)
297//----------------------------------------------------------------------------------------
298{
299 nsIInputStream* stream;
300 if (NS_FAILED(inSpec->GetInputStream(&stream)))
301 return;
302 AssignFrom(stream);
303 NS_RELEASE(stream);
304} // nsInputFileStream::nsInputFileStream
305
306//----------------------------------------------------------------------------------------
307nsInputFileStream::~nsInputFileStream()
308//----------------------------------------------------------------------------------------
309{
310// if (is_open())
311// close();
312}
313
314//----------------------------------------------------------------------------------------
315void nsInputFileStream::AssignFrom(nsISupports* stream)
316//----------------------------------------------------------------------------------------
317{
318 mFile = do_QueryInterface(stream);
319 mInputStream = do_QueryInterface(stream);
320 mStore = do_QueryInterface(stream);
321 mFileInputStream = do_QueryInterface(stream);
322}
323
324//========================================================================================
325// nsOutputFileStream
326//========================================================================================
327
328//----------------------------------------------------------------------------------------
329nsOutputFileStream::nsOutputFileStream(nsIFileSpec* inSpec)
330//----------------------------------------------------------------------------------------
331{
332 if (!inSpec)
333 return;
334 nsIOutputStream* stream;
335 if (NS_FAILED(inSpec->GetOutputStream(&stream)))
336 return;
337 AssignFrom(stream);
338 NS_RELEASE(stream);
339}
340
341//----------------------------------------------------------------------------------------
342nsOutputFileStream::~nsOutputFileStream()
343//----------------------------------------------------------------------------------------
344{
345// if (is_open())
346// close();
347}
348//----------------------------------------------------------------------------------------
349void nsOutputFileStream::AssignFrom(nsISupports* stream)
350//----------------------------------------------------------------------------------------
351{
352 mFile = do_QueryInterface(stream);
353 mOutputStream = do_QueryInterface(stream);
354 mStore = do_QueryInterface(stream);
355 mFileOutputStream = do_QueryInterface(stream);
356}
357
358//----------------------------------------------------------------------------------------
359nsresult nsOutputFileStream::flush()
360//----------------------------------------------------------------------------------------
361{
362 if (mFileOutputStream)
363 mFileOutputStream->Flush();
364 return error();
365}
366
367//----------------------------------------------------------------------------------------
368void nsOutputFileStream::abort()
369//----------------------------------------------------------------------------------------
370{
371 mResult = NS_FILE_FAILURE;
372 close();
373}
374
375//========================================================================================
376// Manipulators
377//========================================================================================
378
379//----------------------------------------------------------------------------------------
380nsOutputStream& nsEndl(nsOutputStream& os)
381//----------------------------------------------------------------------------------------
382{
383#if defined(XP_WIN) || defined(XP_OS2)
384 os.write("\r\n", 2);
385#elif defined (XP_MAC)
386 os.put('\r');
387#else
388 os.put('\n');
389#endif
390 //os.flush();
391 return os;
392} // nsEndl
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use