2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.IO / StringReader.cs
blobf6fda239d5ea8c25ea910776aabafd33d919868e
1 //
2 // System.IO.StringReader
3 //
4 // Author: Marcin Szczepanski (marcins@zipworld.com.au)
5 //
6 // Copyright (C) 2004 Novell (http://www.novell.com)
7 //
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System;
33 using System.Globalization;
34 using System.Runtime.InteropServices;
36 namespace System.IO {
37 [Serializable]
38 [ComVisible (true)]
39 public class StringReader : TextReader {
41 string source;
42 int nextChar;
43 int sourceLength;
45 public StringReader( string s ) {
47 if (s == null)
48 throw new ArgumentNullException ("s");
50 this.source = s;
51 nextChar = 0;
52 sourceLength = s.Length;
55 public override void Close ()
57 Dispose (true);
60 protected override void Dispose (bool disposing)
62 source = null;
63 base.Dispose (disposing);
66 public override int Peek() {
68 CheckObjectDisposedException ();
70 if( nextChar >= sourceLength ) {
71 return -1;
72 } else {
73 return (int)source[ nextChar ];
77 public override int Read() {
79 CheckObjectDisposedException ();
81 if( nextChar >= sourceLength ) {
82 return -1;
83 } else {
84 return (int)source[ nextChar++ ];
89 // The method will read up to count characters from the StringReader
90 // into the buffer character array starting at position index. Returns
91 // the actual number of characters read, or zero if the end of the string
92 // has been reached and no characters are read.
94 public override int Read ([In, Out] char[] buffer, int index, int count)
96 CheckObjectDisposedException ();
98 if (buffer == null)
99 throw new ArgumentNullException ("buffer");
100 if (buffer.Length - index < count)
101 throw new ArgumentException ();
102 if (index < 0 || count < 0)
103 throw new ArgumentOutOfRangeException ();
105 int charsToRead;
107 // reordered to avoir possible integer overflow
108 if (nextChar > sourceLength - count)
109 charsToRead = sourceLength - nextChar;
110 else
111 charsToRead = count;
113 source.CopyTo (nextChar, buffer, index, charsToRead);
115 nextChar += charsToRead;
117 return charsToRead;
120 public override string ReadLine ()
122 // Reads until next \r or \n or \r\n, otherwise return null
124 // LAMESPEC:
125 // The Beta 2 SDK help says that the ReadLine method
126 // returns "The next line from the input stream [...] A
127 // line is defined as a sequence of characters followed by
128 // a carriage return (\r), a line feed (\n), or a carriage
129 // return immediately followed by a line feed (\r\n).
130 // [...] The returned value is a null reference if the end
131 // of the input stream has been reached."
133 // HOWEVER, the MS implementation returns the rest of
134 // the string if no \r and/or \n is found in the string
136 CheckObjectDisposedException ();
138 if (nextChar >= source.Length)
139 return null;
141 int nextCR = source.IndexOf ('\r', nextChar);
142 int nextLF = source.IndexOf ('\n', nextChar);
143 int readTo;
144 bool consecutive = false;
146 if (nextCR == -1) {
147 if (nextLF == -1)
148 return ReadToEnd ();
150 readTo = nextLF;
151 } else if (nextLF == -1) {
152 readTo = nextCR;
153 } else {
154 readTo = (nextCR > nextLF) ? nextLF : nextCR;
155 consecutive = (nextCR + 1 == nextLF);
158 string nextLine = source.Substring (nextChar, readTo - nextChar);
159 nextChar = readTo + ((consecutive) ? 2 : 1);
160 return nextLine;
163 public override string ReadToEnd() {
165 CheckObjectDisposedException ();
166 string toEnd = source.Substring( nextChar, sourceLength - nextChar );
167 nextChar = sourceLength;
168 return toEnd;
171 private void CheckObjectDisposedException ()
173 if (source == null)
174 throw new ObjectDisposedException ("StringReader",
175 Locale.GetText ("Cannot read from a closed StringReader"));