StringReader.cs 5.0 KB

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