Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads
 
Add your own
alternative version

Building .NET Coverage Tool

, 25 Aug 2009 MIT
This article is a walkthrough for building a .NET coverage tool
Coverage.NET_demo.zip
TestApp
TestApp.exe
TestApp.pdb
Tool
Coverage.Counter.dll
Coverage.exe
Mono.Cecil.dll
Mono.Tools.Pdb2Mdb.dll
Coverage.NET_src.zip
Coverage
Common
Instrument
Properties
Report
Coverage.Counter
Properties
Mono.Cecil
src
AUTHORS
ChangeLog
CodeGen
cecil-gen-attributes.rb
cecil-gen-sources.rb
cecil-gen-tests.rb
cecil-gen-types.rb
cecil-gen.rb
cecil-mig.rb
cecil-update-rev
templates
configure
default.build
Makefile
mono-cecil.pc.in
Mono.Cecil
Mono.Cecil.Binary
Mono.Cecil.Cil
Mono.Cecil.dll.sources
Mono.Cecil.Metadata
Mono.Cecil.Signatures
Mono.Xml
ChangeLog
NEWS
README
standalone.make
TODO
Mono.Tools.Pdb2Mdb
Properties
src
ChangeLog
LICENSE
Makefile
pdb2mdb.exe.sources
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.IO;

namespace Microsoft.Cci.Pdb {
  internal class DataStream {
    internal DataStream() {
      this.contentSize = 0;
      this.pages = null;
    }

    internal DataStream(int contentSize, BitAccess bits, int count) {
      this.contentSize = contentSize;
      if (count > 0) {
        this.pages = new int[count];
        bits.ReadInt32(this.pages);
      }
    }

    internal void Read(PdbReader reader, BitAccess bits) {
      bits.MinCapacity(contentSize);
      Read(reader, 0, bits.Buffer, 0, contentSize);
    }

    internal void Read(PdbReader reader, int position,
                     byte[] bytes, int offset, int data) {
      if (position + data > contentSize) {
        throw new PdbException("DataStream can't read off end of stream. " +
                                       "(pos={0},siz={1})",
                               position, data);
      }
      if (position == contentSize) {
        return;
      }

      int left = data;
      int page = position / reader.pageSize;
      int rema = position % reader.pageSize;

      // First get remained of first page.
      if (rema != 0) {
        int todo = reader.pageSize - rema;
        if (todo > left) {
          todo = left;
        }

        reader.Seek(pages[page], rema);
        reader.Read(bytes, offset, todo);

        offset += todo;
        left -= todo;
        page++;
      }

      // Now get the remaining pages.
      while (left > 0) {
        int todo = reader.pageSize;
        if (todo > left) {
          todo = left;
        }

        reader.Seek(pages[page], 0);
        reader.Read(bytes, offset, todo);

        offset += todo;
        left -= todo;
        page++;
      }
    }

    internal void Write(PdbWriter writer, byte[] bytes) {
      Write(writer, bytes, bytes.Length);
    }

    internal void Write(PdbWriter writer, byte[] bytes, int data) {
      if (bytes == null || data == 0) {
        return;
      }

      int left = data;
      int used = 0;
      int rema = contentSize % writer.pageSize;
      if (rema != 0) {
        int todo = writer.pageSize - rema;
        if (todo > left) {
          todo = left;
        }

        int lastPage = pages[pages.Length - 1];
        writer.Seek(lastPage, rema);
        writer.Write(bytes, used, todo);
        used += todo;
        left -= todo;
      }

      if (left > 0) {
        int count = (left + writer.pageSize - 1) / writer.pageSize;
        int page0 = writer.AllocatePages(count);

        writer.Seek(page0, 0);
        writer.Write(bytes, used, left);

        AddPages(page0, count);
      }

      contentSize += data;
    }

    private void AddPages(int page0, int count) {
      if (pages == null) {
        pages = new int[count];
        for (int i = 0; i < count; i++) {
          pages[i] = page0 + i;
        }
      } else {
        int[] old = pages;
        int used = old.Length;

        pages = new int[used + count];
        Array.Copy(old, pages, used);
        for (int i = 0; i < count; i++) {
          pages[used + i] = page0 + i;
        }
      }
    }

    internal int Pages {
      get { return pages == null ? 0 : pages.Length; }
    }

    internal int Length {
      get { return contentSize; }
    }

    internal int GetPage(int index) {
      return pages[index];
    }

    internal int contentSize;
    internal int[] pages;
  }
}

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 MIT License

Share

About the Author

Sergiy Sakharov
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 25 Aug 2009
Article Copyright 2009 by Sergiy Sakharov
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid