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