source: trunk/third/firefox/xpcom/obsolete/nsFileStream.cpp @ 21695

Revision 21695, 14.3 KB checked in by rbasch, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21694, which included commits to RCS files with non-trunk default branches.
Line 
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/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
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    PRIntn position = tell();
232    if (position < 0)
233        return PR_FALSE;
234    PRInt32 bytesRead = read(s, n - 1);
235    if (failed())
236        return PR_FALSE;
237    s[bytesRead] = '\0'; // always terminate at the end of the buffer
238    char* tp = strpbrk(s, "\n\r");
239    if (tp)
240    {
241        TidyEndOfLine(tp);
242        bytesRead = (tp - s);
243    }
244    else if (!eof() && n-1 == bytesRead)
245        bufferLargeEnough = PR_FALSE;
246    position += bytesRead;
247    seek(position);
248    return bufferLargeEnough;
249} // nsRandomAccessInputStream::readline
250
251//========================================================================================
252//          nsInputStringStream
253//========================================================================================
254
255//----------------------------------------------------------------------------------------
256nsInputStringStream::nsInputStringStream(const char* stringToRead)
257//----------------------------------------------------------------------------------------
258{
259        nsCOMPtr<nsIInputStream> stream;
260        if (NS_FAILED(NS_NewCharInputStream(getter_AddRefs(stream), stringToRead)))
261                return;
262        mInputStream = stream;
263        mStore = do_QueryInterface(stream);
264}
265
266//----------------------------------------------------------------------------------------
267nsInputStringStream::nsInputStringStream(const nsString& stringToRead)
268//----------------------------------------------------------------------------------------
269{
270        if (NS_FAILED(NS_NewStringInputStream(getter_AddRefs(mInputStream), stringToRead)))
271                return;
272        mStore = do_QueryInterface(mInputStream);
273}
274
275
276//========================================================================================
277//          nsInputFileStream
278//========================================================================================
279
280//----------------------------------------------------------------------------------------
281nsInputFileStream::nsInputFileStream(
282        const nsFileSpec& inFile,
283        int nsprMode,
284        PRIntn accessMode)
285//----------------------------------------------------------------------------------------
286{
287        nsISupports* stream;
288        if (NS_FAILED(NS_NewIOFileStream(&stream, inFile, nsprMode, accessMode)))
289        return;
290        AssignFrom(stream);
291        NS_RELEASE(stream);
292} // nsInputFileStream::nsInputFileStream
293
294//----------------------------------------------------------------------------------------
295nsInputFileStream::nsInputFileStream(nsIFileSpec* inSpec)
296//----------------------------------------------------------------------------------------
297{
298        nsIInputStream* stream;
299        if (NS_FAILED(inSpec->GetInputStream(&stream)))
300        return;
301        AssignFrom(stream);
302        NS_RELEASE(stream);
303} // nsInputFileStream::nsInputFileStream
304
305//----------------------------------------------------------------------------------------
306nsInputFileStream::~nsInputFileStream()
307//----------------------------------------------------------------------------------------
308{
309//    if (is_open())
310//      close();
311}
312
313//----------------------------------------------------------------------------------------
314void nsInputFileStream::AssignFrom(nsISupports* stream)
315//----------------------------------------------------------------------------------------
316{
317        mFile = do_QueryInterface(stream);
318        mInputStream = do_QueryInterface(stream);
319        mStore = do_QueryInterface(stream);
320        mFileInputStream = do_QueryInterface(stream);
321}
322
323//========================================================================================
324//          nsOutputFileStream
325//========================================================================================
326
327//----------------------------------------------------------------------------------------
328nsOutputFileStream::nsOutputFileStream(nsIFileSpec* inSpec)
329//----------------------------------------------------------------------------------------
330{
331        if (!inSpec)
332                return;
333        nsIOutputStream* stream;
334        if (NS_FAILED(inSpec->GetOutputStream(&stream)))
335          return;
336        AssignFrom(stream);
337        NS_RELEASE(stream);
338}
339
340//----------------------------------------------------------------------------------------
341nsOutputFileStream::~nsOutputFileStream()
342//----------------------------------------------------------------------------------------
343{
344//    if (is_open())
345//      close();
346}
347//----------------------------------------------------------------------------------------
348void nsOutputFileStream::AssignFrom(nsISupports* stream)
349//----------------------------------------------------------------------------------------
350{
351    mFile = do_QueryInterface(stream);
352    mOutputStream = do_QueryInterface(stream);
353    mStore = do_QueryInterface(stream);
354    mFileOutputStream = do_QueryInterface(stream);
355}
356
357//----------------------------------------------------------------------------------------
358nsresult nsOutputFileStream::flush()
359//----------------------------------------------------------------------------------------
360{
361        if (mFileOutputStream)
362                mFileOutputStream->Flush();
363    return error();
364}
365
366//----------------------------------------------------------------------------------------
367void nsOutputFileStream::abort()
368//----------------------------------------------------------------------------------------
369{
370        mResult = NS_FILE_FAILURE;
371        close();
372}
373
374//========================================================================================
375//        Manipulators
376//========================================================================================
377
378//----------------------------------------------------------------------------------------
379nsOutputStream& nsEndl(nsOutputStream& os)
380//----------------------------------------------------------------------------------------
381{
382#if defined(XP_WIN) || defined(XP_OS2)
383    os.write("\r\n", 2);
384#elif defined (XP_MAC)
385    os.put('\r');
386#else
387    os.put('\n');
388#endif
389    //os.flush();
390    return os;
391} // nsEndl
Note: See TracBrowser for help on using the repository browser.