StringReader.cs 4.8 KB

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