/[RomCheater]/trunk/RomCheater/Serialization/SearchResultWriter.cs
ViewVC logotype

Annotation of /trunk/RomCheater/Serialization/SearchResultWriter.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 405 - (hide annotations) (download)
Thu Jun 21 14:38:54 2012 UTC (9 years, 1 month ago) by william
File size: 3041 byte(s)

1 william 401 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using System.IO;
6 william 405 using System.Diagnostics;
7 william 401
8     namespace RomCheater.Serialization
9     {
10     public interface ISearchResultWriter
11     {
12     void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible;
13     }
14     public class SearchResultWriter : SerializationWriter, ISearchResultWriter
15     {
16 william 405
17     private int ResultsWritten = 0;
18    
19 william 401 public SearchResultWriter() : base() { }
20     public SearchResultWriter(int resultCount) : base(resultCount) { WriteHeader(); }
21 william 402 public SearchResultWriter(string filename) : base(filename) { WriteHeader(); }
22     public SearchResultWriter(string filename, int resultCount) : base(filename, resultCount, false) { WriteHeader(); }
23     public SearchResultWriter(string filename, int resultCount, bool delete) : base(filename, resultCount, delete) { WriteHeader(); }
24 william 401 #region ISearchResultWriter members
25     public void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible
26     {
27    
28     Type t = typeof(TValue);
29    
30     binWriter.Write(address);
31     switch (t.Name.ToLower())
32     {
33     case "byte":
34     binWriter.Write(Convert.ToByte(value));
35     break;
36     case "sbyte":
37     binWriter.Write(Convert.ToSByte(value));
38     break;
39     case "uint16":
40     binWriter.Write(Convert.ToUInt16(value));
41     break;
42     case "int16":
43     binWriter.Write(Convert.ToInt16(value));
44     break;
45     case "uint32":
46     binWriter.Write(Convert.ToUInt32(value));
47     break;
48     case "int32":
49     binWriter.Write(Convert.ToInt32(value));
50     break;
51     case "uint64":
52     binWriter.Write(Convert.ToUInt64(value));
53     break;
54     case "int64":
55     binWriter.Write(Convert.ToInt64(value));
56     break;
57     }
58 william 405 ResultsWritten++;
59 william 401 }
60     #endregion
61    
62     private void WriteHeader()
63     {
64     // write magic: SRD
65     binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
66     // write version (int)
67     binWriter.Write((int)1);
68     // write count (int)
69     binWriter.Write(ResultCount);
70     }
71 william 405
72     protected override void Dispose(bool disposing)
73     {
74     base.Dispose(disposing);
75     // ensure ResultCount and ResultsWritten are equal
76     if (ResultCount != ResultsWritten)
77     {
78     throw new InvalidOperationException(string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8}", ResultCount, ResultsWritten));
79     }
80     }
81 william 401 }
82     }

  ViewVC Help
Powered by ViewVC 1.1.22