Click here to Skip to main content
15,881,600 members
Articles / Web Development / HTML

Transformalizing NorthWind

Rate me:
Please Sign up or sign in to vote.
4.95/5 (29 votes)
24 Jul 2014GPL37 min read 57.5K   341   53  
Combining de-normalization, transformation, replication, and awesome-ness.
#region License
// /*
// See license included in this library folder.
// */
#endregion

using System;
using System.IO;
using System.Runtime.CompilerServices;

namespace Transformalize.Libs.ExcelDataReader.Core.BinaryFormat
{
	/// <summary>
	/// Represents a BIFF stream
	/// </summary>
	internal class XlsBiffStream : XlsStream
	{
		private readonly byte[] bytes;
		private readonly int m_size;
		private int m_offset;

		public XlsBiffStream(XlsHeader hdr, uint streamStart)
			: base(hdr, streamStart)
		{
			bytes = base.ReadStream();
			m_size = bytes.Length;
			m_offset = 0;
		}

		/// <summary>
		/// Returns size of BIFF stream in bytes
		/// </summary>
		public int Size
		{
			get { return m_size; }
		}

		/// <summary>
		/// Returns current position in BIFF stream
		/// </summary>
		public int Position
		{
			get { return m_offset; }
		}

		//TODO:Remove ReadStream
		/// <summary>
		/// Always returns null, use biff-specific methods
		/// </summary>
		/// <returns></returns>
		[Obsolete("Use BIFF-specific methods for this stream")]
		public new byte[] ReadStream()
		{
			return bytes;
		}

		/// <summary>
		/// Sets stream pointer to the specified offset
		/// </summary>
		/// <param name="offset">Offset value</param>
		/// <param name="origin">Offset origin</param>
		[MethodImpl(MethodImplOptions.Synchronized)]
		public void Seek(int offset, SeekOrigin origin)
		{
			switch (origin)
			{
				case SeekOrigin.Begin:
					m_offset = offset;
					break;
				case SeekOrigin.Current:
					m_offset += offset;
					break;
				case SeekOrigin.End:
					m_offset = m_size - offset;
					break;
			}
			if (m_offset < 0)
				throw new ArgumentOutOfRangeException(string.Format("{0} On offset={1}", Errors.ErrorBIFFIlegalBefore, offset));
			if (m_offset > m_size)
				throw new ArgumentOutOfRangeException(string.Format("{0} On offset={1}", Errors.ErrorBIFFIlegalAfter, offset));
		}

		/// <summary>
		/// Reads record under cursor and advances cursor position to next record
		/// </summary>
		/// <returns></returns>
		[MethodImpl(MethodImplOptions.Synchronized)]
		public XlsBiffRecord Read()
		{
			var rec = XlsBiffRecord.GetRecord(bytes, (uint)m_offset);
			m_offset += rec.Size;
			if (m_offset > m_size)
				return null;
			return rec;
		}

		/// <summary>
		/// Reads record at specified offset, does not change cursor position
		/// </summary>
		/// <param name="offset"></param>
		/// <returns></returns>
		public XlsBiffRecord ReadAt(int offset)
		{
			var rec = XlsBiffRecord.GetRecord(bytes, (uint)offset);
			if (m_offset + rec.Size > m_size)
				return null;
			return rec;
		}
	}
}

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 GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions