This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / System.IO / StringReader.cs
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 // 
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
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.
30 //
31
32 using System;
33 using System.Globalization;
34 using System.Runtime.InteropServices;
35
36 namespace System.IO {
37         [Serializable]
38         public class StringReader : TextReader {
39
40                 private string source;
41                 private char[] sourceChars;
42
43                 private int nextChar;
44                 private int sourceLength;
45                 private bool disposed = false;
46
47                 public StringReader( string s ) {
48
49                         if (s == null) 
50                                 throw new ArgumentNullException ("s");
51
52                         this.source = s;
53                         nextChar = 0;
54                         sourceLength = s.Length;
55                         sourceChars = s.ToCharArray();
56                 }
57
58                 public override void Close() {
59                         Dispose( true );
60                         disposed = true;
61                 }
62
63                 protected override void Dispose (bool disposing)
64                 {
65                         sourceChars = null;
66                         base.Dispose (disposing);
67                 }
68
69                 public override int Peek() {
70
71                         CheckObjectDisposedException ();
72
73                         if( nextChar >= sourceLength ) {
74                                 return -1;
75                         } else {
76                                 return (int)source[ nextChar ];
77                         }
78                 }
79
80                 public override int Read() {
81
82                         CheckObjectDisposedException ();
83
84                         if( nextChar >= sourceLength ) {
85                                 return -1;
86                         } else {
87                                 return (int)source[ nextChar++ ];
88                         }
89                 }
90
91
92                 // The method will read up to count characters from the StringReader
93                 // into the buffer character array starting at position index. Returns
94                 // the actual number of characters read, or zero if the end of the string
95                 // has been reached and no characters are read.
96
97                 public override int Read ([In, Out] char[] buffer, int index, int count )
98                 {
99                         CheckObjectDisposedException ();
100
101                         if( buffer == null ) {
102                                 throw new ArgumentNullException ("buffer");
103                         } else if( buffer.Length - index < count ) {
104                                 throw new ArgumentException();
105                         } else if( index < 0 || count < 0 ) {
106                                 throw new ArgumentOutOfRangeException();
107                         }
108
109                         int charsToRead;
110
111                         // reordered to avoir possible integer overflow
112                         if (nextChar > sourceLength - count) {
113                                 charsToRead = sourceLength - nextChar;
114                         } else {
115                                 charsToRead = count;
116                         }
117
118                         Array.Copy(sourceChars, nextChar, buffer, index, charsToRead );
119
120                         nextChar += charsToRead;
121
122                         return charsToRead;
123                 }
124
125                 public override string ReadLine ()
126                 {
127                         // Reads until next \r or \n or \r\n, otherwise return null
128
129                         // LAMESPEC:
130                         // The Beta 2 SDK help says that the ReadLine method
131                         // returns "The next line from the input stream [...] A
132                         // line is defined as a sequence of characters followed by
133                         // a carriage return (\r), a line feed (\n), or a carriage
134                         // return immediately followed by a line feed (\r\n).
135                         // [...] The returned value is a null reference if the end
136                         // of the input stream has been reached."
137                         //
138                         // HOWEVER, the MS implementation returns the rest of
139                         // the string if no \r and/or \n is found in the string
140
141                         CheckObjectDisposedException ();
142
143                         if (nextChar >= source.Length)
144                                 return null;
145
146                         int nextCR = source.IndexOf ('\r', nextChar);
147                         int nextLF = source.IndexOf ('\n', nextChar);
148                         int readTo;
149                         bool consecutive = false;
150
151                         if (nextCR == -1) {
152                                 if (nextLF == -1)
153                                         return ReadToEnd ();
154
155                                 readTo = nextLF;
156                         } else if (nextLF == -1) {
157                                 readTo = nextCR;
158                         } else {
159                                 readTo = (nextCR > nextLF) ? nextLF : nextCR;
160                                 consecutive = (nextCR + 1 == nextLF || nextLF + 1 == nextCR);
161                         }
162
163                         string nextLine = source.Substring (nextChar, readTo - nextChar);
164                         nextChar = readTo + ((consecutive) ? 2 : 1);
165                         return nextLine;
166                 }
167
168                 public override string ReadToEnd() {
169
170                         CheckObjectDisposedException ();
171                         string toEnd = source.Substring( nextChar, sourceLength - nextChar );
172                         nextChar = sourceLength;
173                         return toEnd;
174                 }
175
176                 private void CheckObjectDisposedException ()
177                 {
178                         if (disposed) {
179                                 throw new ObjectDisposedException ("StringReader", 
180                                         Locale.GetText ("Cannot read from a closed StringReader"));
181                         }
182                 }
183         }
184 }