Add copy of .ttf font with .eot extension for testing
[wine-gecko.git] / xpcom / obsolete / nsFileStream.cpp
blob7a57f9d55cde3fa567926a8116f25cb7976e6acc
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
13 * License.
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.
22 * Contributor(s):
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"
46 #include "nsInt64.h"
47 #include <string.h>
48 #include <stdio.h>
51 //========================================================================================
52 // nsInputStream
53 //========================================================================================
55 //----------------------------------------------------------------------------------------
56 nsInputStream::~nsInputStream()
57 //----------------------------------------------------------------------------------------
61 //----------------------------------------------------------------------------------------
62 char nsInputStream::get()
63 //----------------------------------------------------------------------------------------
65 char c;
66 if (read(&c, sizeof(c)) == sizeof(c))
67 return c;
68 return 0;
71 //----------------------------------------------------------------------------------------
72 PRInt32 nsInputStream::read(void* s, PRInt32 n)
73 //----------------------------------------------------------------------------------------
75 if (!mInputStream)
76 return 0;
77 PRInt32 result = 0;
78 PRInt32 status = mInputStream->Read((char*)s, n, (PRUint32*)&result);
79 if (result == 0)
80 set_at_eof(PR_TRUE);
81 if (status < 0)
82 return (status);
83 return 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 //----------------------------------------------------------------------------------------
93 char ch = *cp;
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 //----------------------------------------------------------------------------------------
103 c = get();
104 return *this;
107 //========================================================================================
108 // nsOutputStream
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 //----------------------------------------------------------------------------------------
128 if (!mOutputStream)
129 return 0;
130 PRInt32 result = 0;
131 mWriteStatus = mOutputStream->Write((char*)s, n, (PRUint32*)&result);
132 return result;
133 } // nsOutputStream::write
135 //----------------------------------------------------------------------------------------
136 nsresult nsOutputStream::flush()
137 //----------------------------------------------------------------------------------------
139 return NS_OK;
142 //----------------------------------------------------------------------------------------
143 nsresult nsOutputStream::lastWriteStatus()
144 //----------------------------------------------------------------------------------------
146 return mWriteStatus;
149 //----------------------------------------------------------------------------------------
150 nsOutputStream& nsOutputStream::operator << (char c)
151 //----------------------------------------------------------------------------------------
153 put(c);
154 return *this;
157 //----------------------------------------------------------------------------------------
158 nsOutputStream& nsOutputStream::operator << (const char* s)
159 //----------------------------------------------------------------------------------------
161 if (s)
162 write(s, strlen(s));
163 return *this;
166 //----------------------------------------------------------------------------------------
167 nsOutputStream& nsOutputStream::operator << (short val)
168 //----------------------------------------------------------------------------------------
170 char buf[30];
171 sprintf(buf, "%hd", val);
172 return (*this << buf);
175 //----------------------------------------------------------------------------------------
176 nsOutputStream& nsOutputStream::operator << (unsigned short val)
177 //----------------------------------------------------------------------------------------
179 char buf[30];
180 sprintf(buf, "%hu", val);
181 return (*this << buf);
184 //----------------------------------------------------------------------------------------
185 nsOutputStream& nsOutputStream::operator << (long val)
186 //----------------------------------------------------------------------------------------
188 char buf[30];
189 sprintf(buf, "%ld", val);
190 return (*this << buf);
193 //----------------------------------------------------------------------------------------
194 nsOutputStream& nsOutputStream::operator << (unsigned long val)
195 //----------------------------------------------------------------------------------------
197 char buf[30];
198 sprintf(buf, "%lu", val);
199 return (*this << buf);
202 //----------------------------------------------------------------------------------------
203 nsOutputStream& nsOutputStream::operator << (int val)
204 //----------------------------------------------------------------------------------------
206 char buf[30];
207 sprintf(buf, "%d", val);
208 return (*this << buf);
211 //----------------------------------------------------------------------------------------
212 nsOutputStream& nsOutputStream::operator << (unsigned int val)
213 //----------------------------------------------------------------------------------------
215 char buf[30];
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
230 if (!s || !n)
231 return PR_TRUE;
233 nsInt64 position = tell();
234 const nsInt64 zero(0);
235 if (position < zero)
236 return PR_FALSE;
237 PRInt32 bytesRead = read(s, n - 1);
238 if (failed() || bytesRead < 0)
239 return PR_FALSE;
240 s[bytesRead] = '\0'; // always terminate at the end of the buffer
241 char* tp = strpbrk(s, "\n\r");
242 if (tp)
244 TidyEndOfLine(tp);
245 bytesRead = (tp - s);
247 else if (!eof() && n-1 == bytesRead)
248 bufferLargeEnough = PR_FALSE;
249 position += bytesRead;
250 seek(position);
251 return bufferLargeEnough;
252 } // nsRandomAccessInputStream::readline
254 //========================================================================================
255 // nsInputFileStream
256 //========================================================================================
258 //----------------------------------------------------------------------------------------
259 nsInputFileStream::nsInputFileStream(
260 const nsFileSpec& inFile,
261 int nsprMode,
262 PRIntn accessMode)
263 //----------------------------------------------------------------------------------------
265 nsISupports* stream;
266 if (NS_FAILED(NS_NewIOFileStream(&stream, inFile, nsprMode, accessMode)))
267 return;
268 AssignFrom(stream);
269 NS_RELEASE(stream);
270 } // nsInputFileStream::nsInputFileStream
272 //----------------------------------------------------------------------------------------
273 nsInputFileStream::nsInputFileStream(nsIFileSpec* inSpec)
274 //----------------------------------------------------------------------------------------
276 nsIInputStream* stream;
277 if (NS_FAILED(inSpec->GetInputStream(&stream)))
278 return;
279 AssignFrom(stream);
280 NS_RELEASE(stream);
281 } // nsInputFileStream::nsInputFileStream
283 //----------------------------------------------------------------------------------------
284 nsInputFileStream::~nsInputFileStream()
285 //----------------------------------------------------------------------------------------
287 // if (is_open())
288 // close();
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 //----------------------------------------------------------------------------------------
309 if (!inSpec)
310 return;
311 nsIOutputStream* stream;
312 if (NS_FAILED(inSpec->GetOutputStream(&stream)))
313 return;
314 AssignFrom(stream);
315 NS_RELEASE(stream);
318 //----------------------------------------------------------------------------------------
319 nsOutputFileStream::~nsOutputFileStream()
320 //----------------------------------------------------------------------------------------
322 // if (is_open())
323 // close();
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();
341 return error();
344 //----------------------------------------------------------------------------------------
345 void nsOutputFileStream::abort()
346 //----------------------------------------------------------------------------------------
348 mResult = NS_FILE_FAILURE;
349 close();
352 //========================================================================================
353 // Manipulators
354 //========================================================================================
356 //----------------------------------------------------------------------------------------
357 nsOutputStream& nsEndl(nsOutputStream& os)
358 //----------------------------------------------------------------------------------------
360 #if defined(XP_WIN) || defined(XP_OS2)
361 os.write("\r\n", 2);
362 #else
363 os.put('\n');
364 #endif
365 //os.flush();
366 return os;
367 } // nsEndl