Author Topic: Stylecop  (Read 2856 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8712
  • AKA Daniel
Stylecop
« on: January 04, 2009, 11:57:01 PM »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8712
  • AKA Daniel
Re: Stylecop
« Reply #1 on: January 05, 2009, 01:12:10 AM »
A little weird how it wants the using directives inside the namespace curlys

i.e
Code: [Select]
// <copyright file="RbIo.cs" company="DWM Cabinet Drawings LLC">
// Copyright (c) 2008 All Rights Reserved </copyright>
// <author>Daniel Marcotte</author>
// <email>Danielm103@gmail.com</email>
// <date>2008-01-04</date>
// <summary>Contains Code</summary>
// some code is from http://blogs.msdn.com/bclteam/archive/2005/06/15/429542.aspx

namespace AcIo
{
  using System;
  using System.IO;
  using System.IO.Compression;

  using Autodesk.AutoCAD.DatabaseServices;

  /// <summary>
  /// Contains static methods for converting
  /// a file to a Resultbuffer and back
  /// </summary>
  public class RbIo
  {
    /// <summary>
    /// Converts a file to a ResultBuffer
    /// </summary>
    /// <param name="path">String: Path to file</param>
    /// <param name="compress">Bool: use compression</param>
    /// <returns>a new ResultBuffer</returns>
    public static ResultBuffer FileToResultBuffer(string path, bool compress)
    {
      if (!File.Exists(path))
      {
        throw new ArgumentException("File Does not Exist", "path");
      }

      ResultBuffer buf = new ResultBuffer();
      using (FileStream fs = File.OpenRead(path))
      {
        using (MemoryStream ms = new MemoryStream())
        {
          if (compress)
          {
            Compress(fs, ms);
            StreamToResultBuffer(ms, buf);
          }
          else
          {
            StreamToResultBuffer(fs, buf);
          }
        }

        fs.Close();
      }

      return buf;
    }

    /// <summary>
    /// Coverts a ResultBuffer to a file
    /// </summary>
    /// <param name="buf">ResultBuffer: buffer to convert</param>
    /// <param name="path">String: File path</param>
    /// <param name="decompress">Bool: decompress</param>
    public static void ResultBufferToFile(ResultBuffer buf, string path, bool decompress)
    {
      if (buf == null)
      {
        throw new ArgumentNullException("ResultBuffer buffer");
      }

      using (FileStream fs = new FileStream(path, FileMode.Create))
      {
        using (MemoryStream ms = new MemoryStream())
        {
          if (decompress)
          {
            ResultBufferToStream(buf, ms);
            Decompress(ms, fs);
          }
          else
          {
            ResultBufferToStream(buf, fs);
          }
        }

        fs.Close();
      }
    }

    /// <summary>
    /// Converts a resulf buffer to a stream
    /// </summary>
    /// <param name="buf">ResultBuffer: buffer to convert</param>
    /// <param name="destination">destination stream</param>
    /// Close the stream when you are done
    public static void ResultBufferToStream(ResultBuffer buf, Stream destination)
    {
      if (buf == null)
      {
        throw new ArgumentNullException("ResultBuffer buffer");
      }

      BinaryWriter br = new BinaryWriter(destination);
      br.BaseStream.Position = 0;

      foreach (TypedValue t in buf)
      {
        br.Write((byte[])t.Value);
      }

      br.BaseStream.Position = 0;
    }

    /// <summary>
    /// Converts a Stream to a ResultBuffer
    /// </summary>
    /// <param name="source">Stream: Stream to convert</param>
    /// <param name="buf">ResultBuffer: destination buffer</param>
    public static void StreamToResultBuffer(Stream source, ResultBuffer buf)
    {
      BinaryReader br = new BinaryReader(source);
      br.BaseStream.Position = 0;
      while (source.Position < br.BaseStream.Length)
      {
        if ((br.BaseStream.Length - br.BaseStream.Position) > 127)
        {
          buf.Add(new TypedValue((short)DxfCode.BinaryChunk, br.ReadBytes(127)));
        }
        else
        {
          buf.Add(new TypedValue(
            (short)DxfCode.BinaryChunk,
              br.ReadBytes(Convert.ToInt32(br.BaseStream.Length - br.BaseStream.Position))));
        }
      }

      br.BaseStream.Position = 0;
    }

    /// <summary>
    /// Compress stream
    /// </summary>
    /// <param name="source">Stream: source</param>
    /// <param name="destination">Stream: destination</param>
    public static void Compress(Stream source, Stream destination)
    {
      using (GZipStream output = new GZipStream(destination, CompressionMode.Compress, true))
      {
        Pump(source, output);
      }
    }

    /// <summary>
    /// Decompress Stream
    /// </summary>
    /// <param name="source">Stream: source</param>
    /// <param name="destination">Stream: destination</param>
    public static void Decompress(Stream source, Stream destination)
    {
      using (GZipStream input = new GZipStream(source, CompressionMode.Decompress))
      {
        Pump(input, destination);
      }
    }

    /// <summary>
    /// Helper function for Compress and Decompress
    /// </summary>
    /// <param name="input">Stream: input</param>
    /// <param name="output">Stream: output</param>
    private static void Pump(Stream input, Stream output)
    {
      byte[] bytes = new byte[4096];
      int n;
      while ((n = input.Read(bytes, 0, bytes.Length)) != 0)
      {
        output.Write(bytes, 0, n);
      }
    }
  }
}


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Stylecop
« Reply #2 on: January 05, 2009, 03:58:59 AM »
Haven't tried the 'cop. Probably will when I sart writing code again :)

I've seen the using statements included in the namespace scope previously. My thought was that it makes the code a little more transportable .. and the using statement is for the compiler anyway ( well, effectively ) , so I don't see an issue.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8712
  • AKA Daniel
Re: Stylecop
« Reply #3 on: January 05, 2009, 09:03:44 AM »
I guess my biggest complaint would be the change, I already have tons of code that’s not compliant.
It also makes attributes before the namespace a little verbose (see sample). It seems like a great tool though.  8-)

Code: [Select]
// <copyright file="Commands.cs" company="DWM Cabinet Drawings LLC">
// Copyright (c) 2008 All Rights Reserved </copyright>
// <author>Daniel Marcotte</author>
// <email>Danielm103@gmail.com</email>
// <date>2008-01-04</date>
// <summary>Contains Sample Commands</summary>

[assembly: Autodesk.AutoCAD.Runtime.CommandClass(typeof(AcIo.Commands))] //<<<<----

namespace AcIo
{
  using Autodesk.AutoCAD.DatabaseServices;
  using Autodesk.AutoCAD.EditorInput;
  using Autodesk.AutoCAD.Runtime;
  using AcAp = Autodesk.AutoCAD.ApplicationServices;

  /// <summary>
  /// Test Commands
  /// </summary>
  public class Commands
  {
    /// <summary>
    /// Test Command
    /// </summary>
    [CommandMethod("doit")]
    public static void Doit()
    {
      Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
      try
      {
        // 5619 packets
        // 15836 packets
        bool compress = true;
        ResultBuffer buf = RbIo.FileToResultBuffer(@"c:\Northwind.mdb", compress);
        ed.WriteMessage("\n{0} packets", buf.AsArray().Length);
        RbIo.ResultBufferToFile(buf, @"c:\Northwind.zip", false);
      }
      catch (System.Exception ex)
      {
        ed.WriteMessage("\n{0}\n{1}", ex.Message, ex.StackTrace);
      }
    }
  }
}

Spike Wilbury

  • Guest
Re: Stylecop
« Reply #4 on: January 05, 2009, 11:07:47 AM »
<out of scope question from a c# noob per say>

Quote
    /// <summary>
    /// Decompress Stream
    /// </summary>
    /// <param name="source">Stream: source</param>
    /// <param name="destination">Stream: destination</param>

Have not used, the above on any of my c# code so far, but wonder what do you pros do or use to get that ?

(I need to write a small c# app, and maybe it is time to do it right - I guess)

:)

<end of out of scope question from a c# noob per say>
« Last Edit: January 05, 2009, 11:20:34 AM by LE »

sinc

  • Guest
Re: Stylecop
« Reply #5 on: January 05, 2009, 03:42:46 PM »
<out of scope question from a c# noob per say>

Quote
    /// <summary>
    /// Decompress Stream
    /// </summary>
    /// <param name="source">Stream: source</param>
    /// <param name="destination">Stream: destination</param>

Have not used, the above on any of my c# code so far, but wonder what do you pros do or use to get that ?

(I need to write a small c# app, and maybe it is time to do it right - I guess)

:)

<end of out of scope question from a c# noob per say>

Before a class or method, type three backslashes.

The benefit is that these comments can be used to create auto-generated documentation.  Also, when you hover over a reference to your method, Intellisense will display the comment in the "summary" tags.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8712
  • AKA Daniel
Re: Stylecop
« Reply #6 on: January 05, 2009, 03:45:18 PM »
VS does some of the work when typing ///
The rest is by hand,


This is how it looks when processed by Sandcastle


Glenn R

  • Guest
Re: Stylecop
« Reply #7 on: January 12, 2009, 11:53:15 AM »
Yeah, I've looked at it and I liked it, so I try to follow its suggestions.