Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text;
- namespace LH.Utils
- {
- /// <summary>
- /// Pascal-style text file reader.
- /// </summary>
- public class PascalReader
- {
- /// <summary>
- /// Characters that can be used as delimiters.
- /// </summary>
- public char[] Delimiters;
- /// <summary>
- /// Characters that are neither delimiters nor valid characters.
- /// </summary>
- public char[] DeadChars;
- #region Private fields
- private TextReader reader;
- private bool eof;
- #endregion
- #region Public properties
- /// <summary>
- /// If true, the reader has reached the end of the file.
- /// </summary>
- public bool EOF
- {
- get { return reader.Peek() == -1; }
- }
- /// <summary>
- /// The internal reader used.
- /// </summary>
- public TextReader InternalReader
- {
- get { return reader; }
- }
- #endregion
- #region Constructors
- /// <summary>
- /// Initializes a new instance of LH.Utils and opens an input file.
- /// </summary>
- /// <param name="FileName">Name of the file to be read.</param>
- public PascalReader(string FileName)
- {
- reader = new StreamReader(new FileStream(FileName, FileMode.Open));
- InitDelimiters();
- }
- #endregion
- /// <summary>
- /// Initializes Delimiters and DeadChars properties.
- /// </summary>
- public void InitDelimiters()
- {
- string newLine = Environment.NewLine;
- if (newLine.Length == 1)
- {
- Delimiters = new char[] { ' ', '\t', newLine[0] };
- DeadChars = new char[] { };
- }
- else
- {
- Delimiters = new char[] { ' ', '\t', newLine[newLine.Length - 1] };
- DeadChars = newLine.ToCharArray(0, newLine.Length - 1);
- }
- }
- /// <summary>
- /// Check whether array contains character ch.
- /// </summary>
- /// <param name="array">The array.</param>
- /// <param name="ch">The char.</param>
- /// <returns>True if the array contains the char, false otherwise.</returns>
- private bool Contains(char[] array, char ch)
- {
- for (int i = 0; i < array.Length; i++)
- if (array[i] == ch)
- return true;
- return false;
- }
- /// <summary>
- /// Reads string from the input file until any of the delimiters is found or end of the file is reached.
- /// </summary>
- /// <returns>The read string.</returns>
- private string ReadUntilDelim()
- {
- StringBuilder result = new StringBuilder();
- bool term = false;
- while (!term)
- {
- if (reader.Peek() == -1)
- term = true;
- else
- {
- char readChar = (char)reader.Read();
- if (Contains(DeadChars, readChar))
- continue;
- else if (Contains(Delimiters, readChar) && (result.Length > 0))
- term = true;
- else
- result.Append(readChar);
- }
- }
- return result.ToString();
- }
- #region Int readers
- /// <summary>
- /// Reads an integer from the input file.
- /// </summary>
- /// <param name="output">Read value.</param>
- public void Read(out int value)
- {
- value = int.Parse(ReadUntilDelim());
- }
- /// <summary>
- /// Reads an integer from the current line in the input file.
- /// </summary>
- /// <param name="value">Read value.</param>
- public void ReadLn(out int value)
- {
- string s = reader.ReadLine();
- s = s.TrimEnd(Delimiters);
- value = int.Parse(s);
- }
- /// <summary>
- /// Reads an integers from the current line in the input file.
- /// </summary>
- /// <param name="val1">Read value.</param>
- /// <param name="val2">Read value.</param>
- public void ReadLn(out int val1, out int val2)
- {
- string s = reader.ReadLine();
- string[] split = s.Split(Delimiters);
- val1 = (split.Length >= 1) ? int.Parse(split[0]) : 0;
- val2 = (split.Length >= 2) ? int.Parse(split[1]) : 0;
- }
- /// <summary>
- /// Reads an integers from the current line in the input file.
- /// </summary>
- /// <param name="val1">Read value.</param>
- /// <param name="val2">Read value.</param>
- /// <param name="val3">Read value.</param>
- public void ReadLn(out int val1, out int val2, out int val3)
- {
- string s = reader.ReadLine();
- string[] split = s.Split(Delimiters);
- val1 = (split.Length >= 1) ? int.Parse(split[0]) : 0;
- val2 = (split.Length >= 2) ? int.Parse(split[1]) : 0;
- val3 = (split.Length >= 3) ? int.Parse(split[2]) : 0;
- }
- /// <summary>
- /// Reads an integers from the current line in the input file.
- /// </summary>
- /// <param name="val1">Read value.</param>
- /// <param name="val2">Read value.</param>
- /// <param name="val3">Read value.</param>
- /// <param name="val4">Read value.</param>
- public void ReadLn(out int val1, out int val2, out int val3, out int val4)
- {
- string s = reader.ReadLine();
- string[] split = s.Split(Delimiters);
- val1 = (split.Length >= 1) ? int.Parse(split[0]) : 0;
- val2 = (split.Length >= 2) ? int.Parse(split[1]) : 0;
- val3 = (split.Length >= 3) ? int.Parse(split[2]) : 0;
- val4 = (split.Length >= 4) ? int.Parse(split[3]) : 0;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement