// ---------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // ---------------------------------------------------------------------------- namespace Microsoft.Singularity.Security.AccessControl { using System; internal class StringReader : DataReader { private String s; private int pos; private int length; public StringReader(String s) { if (s == null) { throw new ArgumentNullException("s"); } this.s = s; this.length = s == null? 0: s.Length; } protected override void Dispose(bool disposing) { base.Dispose(disposing); s = null; pos = 0; length = 0; } // Returns the next available character without actually reading it from // the underlying string. The current position of the StringReader is not // changed by this operation. The returned value is -1 if no further // characters are available. public override int Peek() { if (s == null) { throw new Exception("Reader closed"); } if (pos == length) return -1; return s[pos]; } // Reads the next character from the underlying string. The returned value // is -1 if no further characters are available. public override int Read() { if (s == null) { throw new Exception("Reader closed"); } if (pos == length) return -1; return s[pos++]; } // Reads a block of characters. This method will read up to count // characters from this StringReader into the buffer character // array starting at position index. Returns the actual number of // characters read, or zero if the end of the string is reached. public override int Read(char[] buffer, int index, int count) { if (buffer == null) throw new ArgumentNullException("Argument null buffer"); if (index < 0) throw new ArgumentOutOfRangeException("Argument out of range"); if (count < 0) throw new ArgumentOutOfRangeException("Argument out of range"); if (buffer.Length - index < count) throw new ArgumentException("Invalid offset"); if (s == null) throw new Exception("Reader closed"); int n = length - pos; if (n > 0) { if (n > count) n = count; s.CopyTo(pos, buffer, index, n); pos += n; } return n; } //| public String ReadToEnd() { if (s == null) { throw new Exception("Reader closed"); } String result; if (pos == 0) result = s; else result = s.Substring(pos, length - pos); pos = length; return result; } // Reads a line. A line is defined as a sequence of characters followed by // a carriage return ('\r'), a line feed ('\n'), or a carriage return // immediately followed by a line feed. The resulting string does not // contain the terminating carriage return and/or line feed. The returned // value is null if the end of the underlying string has been reached. // public String ReadLine() { if (s == null) { throw new Exception("Reader closed"); } int i = pos; while (i < length) { char ch = s[i]; if (ch == '\r' || ch == '\n') { String result = s.Substring(pos, i - pos); pos = i + 1; if (ch == '\r' && pos < length && s[pos] == '\n') pos++; return result; } i++; } if (i > pos) { String result = s.Substring(pos, i - pos); pos = i; return result; } return null; } } }