Advertisement
Ladislav

Pascal style text reader.

Apr 28th, 2011
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.09 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. namespace LH.Utils
  6. {
  7.     /// <summary>
  8.     /// Pascal-style text file reader.
  9.     /// </summary>
  10.     public class PascalReader
  11.     {
  12.         /// <summary>
  13.         /// Characters that can be used as delimiters.
  14.         /// </summary>
  15.         public char[] Delimiters;
  16.  
  17.         /// <summary>
  18.         /// Characters that are neither delimiters nor valid characters.
  19.         /// </summary>
  20.         public char[] DeadChars;
  21.  
  22.         #region Private fields
  23.         private TextReader reader;
  24.         private bool eof;
  25.         #endregion
  26.  
  27.         #region Public properties
  28.         /// <summary>
  29.         /// If true, the reader has reached the end of the file.
  30.         /// </summary>
  31.         public bool EOF
  32.         {
  33.             get { return reader.Peek() == -1; }
  34.         }
  35.  
  36.         /// <summary>
  37.         /// The internal reader used.
  38.         /// </summary>
  39.         public TextReader InternalReader
  40.         {
  41.             get { return reader; }
  42.         }
  43.         #endregion
  44.  
  45.         #region Constructors
  46.         /// <summary>
  47.         /// Initializes a new instance of LH.Utils and opens an input file.
  48.         /// </summary>
  49.         /// <param name="FileName">Name of the file to be read.</param>
  50.         public PascalReader(string FileName)
  51.         {
  52.             reader = new StreamReader(new FileStream(FileName, FileMode.Open));
  53.             InitDelimiters();
  54.         }
  55.         #endregion
  56.  
  57.         /// <summary>
  58.         /// Initializes Delimiters and DeadChars properties.
  59.         /// </summary>
  60.         public void InitDelimiters()
  61.         {
  62.             string newLine = Environment.NewLine;
  63.             if (newLine.Length == 1)
  64.             {
  65.                 Delimiters = new char[] { ' ', '\t', newLine[0] };
  66.                 DeadChars = new char[] { };
  67.             }
  68.             else
  69.             {
  70.                 Delimiters = new char[] { ' ', '\t', newLine[newLine.Length - 1] };
  71.                 DeadChars = newLine.ToCharArray(0, newLine.Length - 1);
  72.             }
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Check whether array contains character ch.
  77.         /// </summary>
  78.         /// <param name="array">The array.</param>
  79.         /// <param name="ch">The char.</param>
  80.         /// <returns>True if the array contains the char, false otherwise.</returns>
  81.         private bool Contains(char[] array, char ch)
  82.         {
  83.             for (int i = 0; i < array.Length; i++)
  84.                 if (array[i] == ch)
  85.                     return true;
  86.             return false;
  87.         }
  88.  
  89.         /// <summary>
  90.         /// Reads string from the input file until any of the delimiters is found or end of the file is reached.
  91.         /// </summary>
  92.         /// <returns>The read string.</returns>
  93.         private string ReadUntilDelim()
  94.         {
  95.             StringBuilder result = new StringBuilder();
  96.             bool term = false;
  97.             while (!term)
  98.             {
  99.                 if (reader.Peek() == -1)
  100.                     term = true;
  101.                 else
  102.                 {
  103.                     char readChar = (char)reader.Read();
  104.                     if (Contains(DeadChars, readChar))
  105.                         continue;
  106.                     else if (Contains(Delimiters, readChar) && (result.Length > 0))
  107.                         term = true;
  108.                     else
  109.                         result.Append(readChar);
  110.                 }
  111.             }
  112.             return result.ToString();
  113.         }
  114.  
  115.         #region Int readers
  116.         /// <summary>
  117.         /// Reads an integer from the input file.
  118.         /// </summary>
  119.         /// <param name="output">Read value.</param>
  120.         public void Read(out int value)
  121.         {
  122.             value = int.Parse(ReadUntilDelim());
  123.         }
  124.  
  125.         /// <summary>
  126.         /// Reads an integer from the current line in the input file.
  127.         /// </summary>
  128.         /// <param name="value">Read value.</param>
  129.         public void ReadLn(out int value)
  130.         {
  131.             string s = reader.ReadLine();
  132.             s = s.TrimEnd(Delimiters);
  133.             value = int.Parse(s);
  134.         }
  135.  
  136.         /// <summary>
  137.         /// Reads an integers from the current line in the input file.
  138.         /// </summary>
  139.         /// <param name="val1">Read value.</param>
  140.         /// <param name="val2">Read value.</param>
  141.         public void ReadLn(out int val1, out int val2)
  142.         {
  143.             string s = reader.ReadLine();
  144.             string[] split = s.Split(Delimiters);
  145.             val1 = (split.Length >= 1) ? int.Parse(split[0]) : 0;
  146.             val2 = (split.Length >= 2) ? int.Parse(split[1]) : 0;
  147.         }
  148.  
  149.         /// <summary>
  150.         /// Reads an integers from the current line in the input file.
  151.         /// </summary>
  152.         /// <param name="val1">Read value.</param>
  153.         /// <param name="val2">Read value.</param>
  154.         /// <param name="val3">Read value.</param>
  155.         public void ReadLn(out int val1, out int val2, out int val3)
  156.         {
  157.             string s = reader.ReadLine();
  158.             string[] split = s.Split(Delimiters);
  159.             val1 = (split.Length >= 1) ? int.Parse(split[0]) : 0;
  160.             val2 = (split.Length >= 2) ? int.Parse(split[1]) : 0;
  161.             val3 = (split.Length >= 3) ? int.Parse(split[2]) : 0;
  162.         }
  163.  
  164.         /// <summary>
  165.         /// Reads an integers from the current line in the input file.
  166.         /// </summary>
  167.         /// <param name="val1">Read value.</param>
  168.         /// <param name="val2">Read value.</param>
  169.         /// <param name="val3">Read value.</param>
  170.         /// <param name="val4">Read value.</param>
  171.         public void ReadLn(out int val1, out int val2, out int val3, out int val4)
  172.         {
  173.             string s = reader.ReadLine();
  174.             string[] split = s.Split(Delimiters);
  175.             val1 = (split.Length >= 1) ? int.Parse(split[0]) : 0;
  176.             val2 = (split.Length >= 2) ? int.Parse(split[1]) : 0;
  177.             val3 = (split.Length >= 3) ? int.Parse(split[2]) : 0;
  178.             val4 = (split.Length >= 4) ? int.Parse(split[3]) : 0;
  179.         }
  180.         #endregion
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement