Click here to Skip to main content
15,894,410 members
Articles / Programming Languages / C#

Wrapper Class for Parsing Fixed-Width, Multiple Section Files

Rate me:
Please Sign up or sign in to vote.
4.07/5 (4 votes)
21 Apr 2006CPOL8 min read 51.6K   1.1K   33  
An article describing a wrapper class to import very large multiple section reports, typically from a legacy system, into the modern SQL Server or other RDBMS.
using System;

namespace ReportParser
{
	public class Section
	{
		private string m_Name;
		private int m_Length;								// Number of lines
		private string m_StartString;						// Only Necessary for new Master record indicator
		private string m_EndString;							// Only necessary for Detail Lines
		private TextFieldCollection m_TextFields = null;
		private SectionFormat m_Format = SectionFormat.FixedWidth;

		public Section(string Name, int Length, string StartString, string EndString, SectionFormat Format)
		{
			m_TextFields = new TextFieldCollection();
			m_StartString =	StartString;
			m_EndString =	EndString;
			m_Name =		Name;
			m_Length =		Length;
			m_Format =		Format;
		}

		public string Name
		{
			get{return m_Name;}
			set
			{
				if(value.Length < 1 || value == null || value == String.Empty)
					throw new ApplicationException("You can not set the Name property to a blank, empty or null value.");
				m_Name = value;
			}
		}

		public int Length
		{
			get{return m_Length;}
			set
			{
				if(value < 1)
					throw new ApplicationException("You can not set the Length property to a zero or negative value.");
				m_Length = value;
			}
		}

		public string StartString
		{
			get{return m_StartString;}
			set{m_StartString = value;}
		}

		public string EndString
		{
			get{return m_EndString;}
			set{m_EndString = value;}
		}

		public TextFieldCollection TextFields
		{
			get{return m_TextFields;}
			set{m_TextFields = value;}
		}

		public SectionFormat SectionFormat
		{
			get{return m_Format;}
			set{m_Format = value;}
		}
	}

	public class SectionCollection : System.Collections.CollectionBase
	{
		public SectionCollection() : base ()
		{
		}

		public SectionCollection(SectionCollection texValue) : base()
		{
			AddRange(texValue);
		}

		public SectionCollection(Section[] texValue) : base()
		{
			AddRange(texValue);
		}

		public Section this[int index]
		{
			get{return (Section)List[index];}
			set{List[index] = value;}
		}

		public int Add(Section texValue)
		{
			return List.Add(texValue);
		}

		public void AddRange(Section[] texValue)
		{
			int intCounter = 0;
			while(intCounter < texValue.Length)
			{
				Add(texValue[intCounter]);
				intCounter ++;
			}
		}

		public void AddRange(SectionCollection texValue)
		{
			int intCounter = 0;
			while(intCounter < texValue.Count)
			{
				Add(texValue[intCounter]);
				intCounter++;
			}
		}

		public bool Contains(Section texValue)
		{
			return List.Contains(texValue);
		}

		public void CopyTo(Section[] texArray, int intIndex)
		{
			List.CopyTo(texArray, intIndex);
		}

		public int IndexOf(Section texValue)
		{
			return List.IndexOf(texValue);
		}

		public void Insert(int intIndex, Section texValue)
		{
			List.Insert(intIndex, texValue);
		}

		public new SectionEnumerator GetEnumerator()
		{
			return new SectionEnumerator(this);
		}

		public void Remove(Section texValue)
		{
			List.Remove(texValue);
		}

		public class SectionEnumerator : System.Collections.IEnumerator
		{
			private System.Collections.IEnumerator iEnBase;
			private System.Collections.IEnumerable iEnLocal;

			public SectionEnumerator(SectionCollection texMappings) : base()
			{
				iEnLocal = (System.Collections.IEnumerable)texMappings;
				iEnBase = iEnLocal.GetEnumerator();
			}

			public Section Current
			{
				get
				{
					return (Section)iEnBase.Current;
				}
			}

			object System.Collections.IEnumerator.Current
			{
				get{return iEnBase.Current;}
			}

			public bool MoveNext()
			{
				return iEnBase.MoveNext();
			}

			bool System.Collections.IEnumerator.MoveNext()
			{
				return iEnBase.MoveNext();
			}

			public void Reset()
			{
				iEnBase.Reset();
			}

			void System.Collections.IEnumerator.Reset()
			{
				iEnBase.Reset();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Tampa, FL developer with about 11 years of experience.

Comments and Discussions