public void Read(ReadContext c)
        {
            // time scale
            var epoch = c.Reader.ReadInt64();
            var ticksPerDay = c.Reader.ReadInt64();
            c.Description.Timescale = Timescale.FromEpoch(epoch, ticksPerDay);

            // time fields
            var timeFieldsCount = c.Reader.ReadInt32();
            var offsets = new List<int>();
            timeFieldsCount.Times(() =>
                                  offsets.Add(c.Reader.ReadInt32()));
            c.Description.TimeFieldOffsets = offsets.AsReadOnly();

            // adorn item description with time aspects, if available
            var id = c.Description.ItemDescription;
            if (id != null)
            {
                bool isFirstTimeField = true;
                foreach (var offset in offsets)
                {
                    var f = id.FindFieldByOffset(offset);
                    if (f == null) throw new FileFormatException("Time format section contains an entry for a field at offset {0} but no such field was found in the item description.");
                    f.IsTime = true;
                    f.IsEventTime = isFirstTimeField;
                    isFirstTimeField = false;
                }
            }
        }
 public void Read(ReadContext c)
 {
     if (c == null) throw new ArgumentNullException("c");
     int count = c.Reader.ReadInt32();
     c.Description.NameValues = new NameValueCollection();
     count.Times(() => c.Description.NameValues.Add(c.Reader.ReadNameValue()));
 }
        public void Read(ReadContext c)
        {
            // time scale
            var epoch       = c.Reader.ReadInt64();
            var ticksPerDay = c.Reader.ReadInt64();

            c.Description.Timescale = Timescale.FromEpoch(epoch, ticksPerDay);

            // time fields
            var timeFieldsCount = c.Reader.ReadInt32();
            var offsets         = new List <int>();

            timeFieldsCount.Times(() =>
                                  offsets.Add(c.Reader.ReadInt32()));
            c.Description.TimeFieldOffsets = offsets.AsReadOnly();

            // adorn item description with time aspects, if available
            var id = c.Description.ItemDescription;

            if (id != null)
            {
                bool isFirstTimeField = true;
                foreach (var offset in offsets)
                {
                    var f = id.FindFieldByOffset(offset);
                    if (f == null)
                    {
                        throw new FileFormatException("Time format section contains an entry for a field at offset {0} but no such field was found in the item description.");
                    }
                    f.IsTime         = true;
                    f.IsEventTime    = isFirstTimeField;
                    isFirstTimeField = false;
                }
            }
        }
Example #4
0
 public void Read(ReadContext c)
 {
     if (c == null)
     {
         throw new ArgumentNullException("c");
     }
     c.Description.ContentDescription = c.Reader.ReadText();
 }
        public void Read(ReadContext c)
        {
            if (c == null)
            {
                throw new ArgumentNullException("c");
            }
            int count = c.Reader.ReadInt32();

            c.Description.NameValues = new NameValueCollection();
            count.Times(() => c.Description.NameValues.Add(c.Reader.ReadNameValue()));
        }
        public ReadContext ReadHeader(IFormattedReader r)
        {
            var rc = new ReadContext(r);
            try
            {
                var bom = r.ReadInt64();
                if (bom != 0x0d0e0a0402080500) throw new FileFormatException("Expected Signature not found. Either this file is not a TeaFile or the byte order (endianness) differs between the machine were the file was written and the local machine. Expected:'{0:x}'\nFound:   '{1:x}'\n".Formatted(0x0d0e0a0402080500, bom));
                rc.ItemAreaStart = r.ReadInt64();
                rc.ItemAreaEnd = r.ReadInt64();
                rc.SectionCount = r.ReadInt64();

                for (int i = 0; i < rc.SectionCount; i++)
                {
                    int sectionId = r.ReadInt32();
                    int nextSectionOffset = r.ReadInt32();
                    int nextSectionStart = (int)r.Position + nextSectionOffset;
                    if (nextSectionStart > rc.ItemAreaStart) throw new FileFormatException("NextSectionOffset of section number {0} is wrong: Next Section Start would be beyond ItemStart.".Formatted(i));

                    ISectionFormatter sectionFormatter = this.sectionFormatters.FirstOrDefault(f => f.Id == sectionId);
                    if (sectionFormatter != null)
                    {
                        sectionFormatter.Read(rc);
                        if (r.Position > nextSectionStart) throw new FileFormatException("Section read too many bytes from the stream. SectionId:{0} SectionName:{1}".Formatted(sectionId, sectionFormatter.GetType().Name.Replace("Formatter", "")));
                    }

                    int bytesToSkip = nextSectionStart - (int)r.Position;
                    if (bytesToSkip < 0) throw new FileFormatException("Reading sections from the file header failed. Section with id {0} reads more bytes than reserved for that section".Formatted(sectionId));
                    r.SkipBytes(bytesToSkip);
                }

                if (r.Position > rc.ItemAreaStart) throw new FileFormatException("Stream position is behind start of item area.");
                r.SkipBytes((int)rc.ItemAreaStart - (int)r.Position);

                if (rc.ItemAreaStart != r.Position) throw new FileFormatException("Stream Position could not be set to start of item area.");
                return rc;
            }
            catch (FileFormatException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // communicate any unexpected Header Read Error (e.g. an error while reading from disc)
                throw new FileFormatException("Error reading TeaFile Header: {0}".Formatted(ex.Message), ex);
            }
        }
        public ReadContext ReadHeader(IFormattedReader r)
        {
            var rc = new ReadContext(r);

            try
            {
                var bom = r.ReadInt64();
                if (bom != 0x0d0e0a0402080500)
                {
                    throw new FileFormatException("Expected Signature not found. Either this file is not a TeaFile or the byte order (endianness) differs between the machine were the file was written and the local machine. Expected:'{0:x}'\nFound:   '{1:x}'\n".Formatted(0x0d0e0a0402080500, bom));
                }
                rc.ItemAreaStart = r.ReadInt64();
                rc.ItemAreaEnd   = r.ReadInt64();
                rc.SectionCount  = r.ReadInt64();

                for (int i = 0; i < rc.SectionCount; i++)
                {
                    int sectionId         = r.ReadInt32();
                    int nextSectionOffset = r.ReadInt32();
                    int nextSectionStart  = (int)r.Position + nextSectionOffset;
                    if (nextSectionStart > rc.ItemAreaStart)
                    {
                        throw new FileFormatException("NextSectionOffset of section number {0} is wrong: Next Section Start would be beyond ItemStart.".Formatted(i));
                    }

                    ISectionFormatter sectionFormatter = this.sectionFormatters.FirstOrDefault(f => f.Id == sectionId);
                    if (sectionFormatter != null)
                    {
                        sectionFormatter.Read(rc);
                        if (r.Position > nextSectionStart)
                        {
                            throw new FileFormatException("Section read too many bytes from the stream. SectionId:{0} SectionName:{1}".Formatted(sectionId, sectionFormatter.GetType().Name.Replace("Formatter", "")));
                        }
                    }

                    int bytesToSkip = nextSectionStart - (int)r.Position;
                    if (bytesToSkip < 0)
                    {
                        throw new FileFormatException("Reading sections from the file header failed. Section with id {0} reads more bytes than reserved for that section".Formatted(sectionId));
                    }
                    r.SkipBytes(bytesToSkip);
                }

                if (r.Position > rc.ItemAreaStart)
                {
                    throw new FileFormatException("Stream position is behind start of item area.");
                }
                r.SkipBytes((int)rc.ItemAreaStart - (int)r.Position);

                if (rc.ItemAreaStart != r.Position)
                {
                    throw new FileFormatException("Stream Position could not be set to start of item area.");
                }
                return(rc);
            }
            catch (FileFormatException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // communicate any unexpected Header Read Error (e.g. an error while reading from disc)
                throw new FileFormatException("Error reading TeaFile Header: {0}".Formatted(ex.Message), ex);
            }
        }
 public void Read(ReadContext c)
 {
     if (c == null) throw new ArgumentNullException("c");
     c.Description.ContentDescription = c.Reader.ReadText();
 }