Example #1
0
        /// <summary>
        /// Read an encoded line of text from the stream, with a specified line ending
        /// </summary>
        /// <param name="encoding">The text encoding to use</param>
        /// <param name="lineEnding">Specify the line ending required for the line</param>
        /// <param name="maxLength">Maximum length of a line, &lt;= indicates to end of string</param>
        /// <returns>The string, empty string on end of stream</returns>
        public string ReadLine(Encoding encoding, TextLineEnding lineEnding, int maxLength)
        {
            StringBuilder buf   = new StringBuilder();
            bool          hasCr = false;

            if (!_eof)
            {
                try
                {
                    while (true)
                    {
                        char currChar = ReadChar(encoding);
                        buf.Append(currChar);

                        if ((maxLength > 0) && (buf.Length == maxLength))
                        {
                            break;
                        }

                        if (currChar == '\n')
                        {
                            if (lineEnding == TextLineEnding.LineFeed)
                            {
                                break;
                            }
                            else if ((lineEnding == TextLineEnding.CarriageReturnLineFeed) && hasCr)
                            {
                                break;
                            }
                        }
                        else if (currChar == '\r')
                        {
                            if (lineEnding == TextLineEnding.CarriageReturn)
                            {
                                break;
                            }
                            else
                            {
                                hasCr = true;
                            }
                        }
                        else
                        {
                            hasCr = false;
                        }
                    }
                }
                catch (EndOfStreamException)
                {
                    if (buf.Length == 0)
                    {
                        // Rethrow if buffer was 0
                        throw;
                    }
                }
            }

            return(buf.ToString());
        }
Example #2
0
        /// <summary>
        /// Write a line to the stream using a specified encoding and line ending
        /// </summary>
        /// <param name="line">The line to write</param>
        /// <param name="encoding">The encoding to use</param>
        /// <param name="lineEnding">The type of line ending</param>
        public void WriteLine(string line, Encoding encoding, TextLineEnding lineEnding)
        {
            string lineEndingString = "\n";

            switch (lineEnding)
            {
            case TextLineEnding.LineFeed:
                lineEndingString = "\n";
                break;

            case TextLineEnding.CarriageReturn:
                lineEndingString = "\r";
                break;

            case TextLineEnding.CarriageReturnLineFeed:
                lineEndingString = "\r\n";
                break;
            }

            Write(line, encoding);
            Write(lineEndingString, encoding);
        }
Example #3
0
 /// <summary>
 /// Read an encoded line of text from the stream, with a specified line ending
 /// </summary>
 /// <param name="encoding">The text encoding to use</param>
 /// <param name="lineEnding">Specify the line ending required for the line</param>
 /// <returns>The string, empty string on end of stream</returns>
 public string ReadLine(Encoding encoding, TextLineEnding lineEnding)
 {
     return(ReadLine(encoding, lineEnding, 0));
 }
Example #4
0
 /// <summary>
 /// Read a ASCII python compatible string from the stream with a specified line ending
 /// </summary>
 /// <param name="lineEnding">The line ending</param>
 /// <returns>The string, empty string on end of stream</returns>
 public string ReadLine(TextLineEnding lineEnding)
 {
     return(ReadLine(BinaryEncoding.Instance, lineEnding));
 }
Example #5
0
 /// <summary>
 /// Write a line to the stream using a binary encoding and a specified line ending
 /// </summary>
 /// <param name="line">The line to write</param>
 /// <param name="lineEnding">The type of line ending</param>
 public void WriteLine(string line, TextLineEnding lineEnding)
 {
     WriteLine(line, BinaryEncoding.Instance, lineEnding);
 }