Click here to Skip to main content
15,896,527 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 TextField
	{
		private string m_Name;
		private TypeCode m_DataType = TypeCode.String;
		private int m_Length;
		private int m_StartIndex;
		private int m_LineNumber;
		private bool m_Quoted = true;
		private object m_Value = null;

		public TextField(string Name, TypeCode DataType, int Length, int StartIndex) : this(Name, DataType, Length, false, StartIndex, 0)
		{
		}

		public TextField(string Name, TypeCode DataType, int Length, int StartIndex, int LineNumber) : this(Name, DataType, Length, false, StartIndex, LineNumber)
		{
		}

		private TextField(string Name, TypeCode DataType, int Length, bool Quoted, int StartIndex, int LineNumber)
		{
			m_Name = Name;
			m_DataType = DataType;
			m_Length = Length;
			m_StartIndex = StartIndex;
			m_Quoted = Quoted;
			m_Value = null;
			m_LineNumber = LineNumber;
		}

		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 TypeCode DataType
		{
			get{return m_DataType;}
			set{m_DataType = 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 int LineNumber
		{
			get{return m_LineNumber;}
			set
			{
				if(value < 1)
					throw new ApplicationException("You can not set the LineNumber property to a zero or negative value.");
				m_LineNumber = value;
			}
		}

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

		public bool Quoted
		{
			get{return m_Quoted;}
			set{m_Quoted = value;}
		}

		public object Value
		{
			get{return m_Value;}
			set
			{
				try
				{
					if(value.ToString().Trim().Length == 0) //Allow for null values.
					{
						m_Value = Convert.DBNull;
					}
					else if(m_DataType == TypeCode.Boolean) //special form boolean
					{
						if(value.ToString().Trim() == "1")
							m_Value = true;
						else if(value.ToString().Trim() == "0")
							m_Value = false;
						else
							m_Value = Convert.ChangeType(value, m_DataType);
					}
					else if ( (m_DataType == TypeCode.Int32) ||  
						(m_DataType == TypeCode.Int16)  ||  (m_DataType == TypeCode.Int64) ) // Remove Comma's from numbers.
					{
						string s = value.ToString().Replace(",", "");
						m_Value = Convert.ChangeType(s, m_DataType);
					}
					else
					{
						m_Value = Convert.ChangeType(value, m_DataType);
					}
				}
				catch
				{
					throw new ArgumentException(String.Format("There was an error converting the value \"{0}\" to a {1} for the field \"{2}\".", value, m_DataType.ToString(), m_Name));
				}
			}
		}
	}

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

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


		public TextFieldCollection(TextField[] texValue) : base()
		{
			AddRange(texValue);
		}

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

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

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

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

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

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

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

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

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

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

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

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

			public TextField Current
			{
				get
				{
					return (TextField)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