Gfy

SFV library

Gfy
Dec 10th, 2011
265
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from xaios, a better SFV (Simple File Verification) read library than the one in ReScene
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Text;
  9.  
  10. namespace CustomTasks.Library
  11. {
  12.     /// <summary>
  13.     /// A library to read Simple File Verification files
  14.     /// </summary>
  15.     public class SFVReader
  16.     {
  17.         /// <summary>
  18.         /// Convenience method to construct an SFVReader from an SFV file.
  19.         /// </summary>
  20.         /// <param name="fileName">
  21.         /// The complete file path to be read.
  22.         /// </param>
  23.         public SFVReader(string fileName)
  24.         {
  25.             if (fileName == null)
  26.             {
  27.                 throw new ArgumentNullException("fileName");
  28.             }
  29.  
  30.             using (StreamReader sfvStreamReader = new StreamReader(fileName, Encoding.ASCII))
  31.             {
  32.                 this.SFVEntries = _ParseSFVFile(sfvStreamReader);
  33.             }
  34.         }
  35.  
  36.         /// <summary>
  37.         /// Constructs an SFVReader from a stream
  38.         /// </summary>
  39.         /// <param name="sfvStream">
  40.         /// The stream to be read.
  41.         /// </param>
  42.         public SFVReader(Stream sfvStream)
  43.         {
  44.             if(sfvStream == null)
  45.             {
  46.                 throw new ArgumentNullException("sfvStream");
  47.             }
  48.  
  49.             using (StreamReader sfvStreamReader = new StreamReader(sfvStream))
  50.             {
  51.                 this.SFVEntries = _ParseSFVFile(sfvStreamReader);
  52.             }
  53.         }
  54.  
  55.         /// <summary>
  56.         /// The SFV Entries stored in this SFV File
  57.         /// </summary>
  58.         public ReadOnlyCollection<SFVEntry> SFVEntries
  59.         {
  60.             get;
  61.             private set;
  62.         }
  63.  
  64.         /// <summary>
  65.         /// Parses a Stream that represents an SFV File
  66.         /// </summary>
  67.         /// <param name="sfvStream">
  68.         /// A Stream representing an SFV file
  69.         /// </param>
  70.         /// <returns>
  71.         /// A read-only collection of SFV Entries contained in this SFV stream
  72.         /// </returns>
  73.         private ReadOnlyCollection<SFVEntry> _ParseSFVFile(StreamReader sfvStream)
  74.         {
  75.             List<SFVEntry> sfvEntries = new List<SFVEntry>();
  76.  
  77.             string currentLine;
  78.             while (!sfvStream.EndOfStream)
  79.             {
  80.                 // Grab a line from the stream
  81.                 currentLine = sfvStream.ReadLine();
  82.  
  83.                 //Trim Trailing White Space, it is not important
  84.                 currentLine.TrimEnd();
  85.  
  86.                 // Try to perform some validation first
  87.                 if (string.IsNullOrWhiteSpace(currentLine))
  88.                 {
  89.                     // If null, empty, or white space, skip the Line
  90.                     continue;
  91.                 }
  92.                 else if (currentLine.StartsWith(";"))
  93.                 {
  94.                     // If it starts with a ; skip the line
  95.                     continue;
  96.                 }
  97.                 else if (currentLine.Length < 10)
  98.                 {
  99.                     // If it is less than 9 (White Space+CRC32) then it is too
  100.                     // small to contain a file name and a check sum skip the line
  101.                     continue;
  102.                 }
  103.  
  104.                 // Now try to read the entry, because we've trimmed all white
  105.                 // space and nothing is valid after the CRC32 we make the
  106.                 // assumption that the last 8 characters represent the CRC32
  107.                 uint crc32;
  108.                 if (uint.TryParse(currentLine.Substring(currentLine.Length - 8), NumberStyles.HexNumber, null, out crc32))
  109.                 {
  110.                     // At this point we should have the CRC32 of the file, now
  111.                     // we need the file name, this should be the remainder of
  112.                     // the line, sans any trailing white space.
  113.                     string fileName = currentLine.Substring(0,currentLine.Length - 8).TrimEnd();
  114.  
  115.                     // Now add this SfvEntry to our List
  116.                     sfvEntries.Add(new SFVEntry(fileName, crc32));
  117.                 }
  118.                 else
  119.                 {
  120.                     // TODO: Globalization Concerns, this should be in a resource assembly
  121.                     string exception = string.Format("Invalid CRC32 found in line:{0}", currentLine);
  122.                     throw new FormatException(exception);
  123.                 }
  124.             }
  125.  
  126.             return new ReadOnlyCollection<SFVEntry>(sfvEntries);
  127.         }
  128.     }
  129.  
  130.     /// <summary>
  131.     /// Representation of an SFV Entry
  132.     /// </summary>
  133.     public class SFVEntry
  134.     {
  135.         /// <summary>
  136.         ///
  137.         /// </summary>
  138.         /// <param name="fileName"></param>
  139.         /// <param name="CRC32"></param>
  140.         public SFVEntry(string fileName, uint CRC32)
  141.         {
  142.             if(string.IsNullOrWhiteSpace(fileName))
  143.             {
  144.                 throw new ArgumentNullException("fileName");
  145.             }
  146.  
  147.             this.FileName = fileName;
  148.             this.CRC32 = CRC32;
  149.         }
  150.  
  151.         /// <summary>
  152.         /// The name of the file
  153.         /// </summary>
  154.         public string FileName { get; private set; }
  155.  
  156.         /// <summary>
  157.         /// Associated CRC32
  158.         /// </summary>
  159.         public uint CRC32 { get; private set; }
  160.     }
  161. }
  162.  
  163.  
RAW Paste Data