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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 456 - (hide annotations) (download)
Mon Jun 3 01:45:40 2013 UTC (8 years, 1 month ago) by william
File size: 4903 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 william 448 protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
19 william 444 //public SearchResultWriter() : base() { }
20     public SearchResultWriter(int resultCount, Guid guid) : base(resultCount, guid) { WriteHeader(guid); }
21     //public SearchResultWriter(string filename, Guid guid) : base(CreateFileName(filename,guid), guid) { WriteHeader(guid); }
22     //public SearchResultWriter(string filename, int resultCount, Guid guid) : base(filename, resultCount, guid, false) { WriteHeader(guid); }
23     //public SearchResultWriter(string filename, int resultCount, Guid guid, bool delete) : base(filename, resultCount, guid, delete) { WriteHeader(guid); }
24 william 401 #region ISearchResultWriter members
25     public void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible
26     {
27 william 445 try
28     {
29     Type t = typeof(TValue);
30 william 401
31 william 445 binWriter.Write(address);
32     switch (t.Name.ToLower())
33     {
34     case "byte":
35     binWriter.Write(Convert.ToByte(value));
36     break;
37     case "sbyte":
38     binWriter.Write(Convert.ToSByte(value));
39     break;
40     case "uint16":
41     binWriter.Write(Convert.ToUInt16(value));
42     break;
43     case "int16":
44     binWriter.Write(Convert.ToInt16(value));
45     break;
46     case "uint32":
47     binWriter.Write(Convert.ToUInt32(value));
48     break;
49     case "int32":
50     binWriter.Write(Convert.ToInt32(value));
51     break;
52     case "uint64":
53     binWriter.Write(Convert.ToUInt64(value));
54     break;
55     case "int64":
56     binWriter.Write(Convert.ToInt64(value));
57     break;
58     }
59     ResultsWritten++;
60     }
61     catch (Exception ex)
62 william 401 {
63 william 445 throw ex;
64 william 401 }
65     }
66     #endregion
67    
68 william 456 long ResultCountOffset = 0;
69 william 444 private void WriteHeader(Guid guid)
70 william 401 {
71 william 445 try
72     {
73     // write magic: SRD
74     binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
75 william 444
76 william 445 if (guid == Guid.Empty)
77     {
78     binWriter.Write((int)1);
79     }
80     else
81     {
82     binWriter.Write((int)2);
83     // related guid - length
84     binWriter.Write(guid.ToByteArray().Length);
85     // related guid - data
86     binWriter.Write(guid.ToByteArray());
87     }
88     // write count (int)
89     binWriter.Write(ResultCount);
90 william 456 ResultCountOffset = binWriter.BaseStream.Position - sizeof(int);
91 william 445 }
92     catch (Exception ex)
93 william 444 {
94 william 445 throw ex;
95 william 444 }
96 william 401 }
97 william 405
98     protected override void Dispose(bool disposing)
99 william 445 {
100 william 448 if (Cancelled) { binWriter.Close(); fileStream.Close(); return; }
101 william 445 try
102 william 405 {
103 william 456
104     //// ensure ResultCount and ResultsWritten are equal
105     //if (ResultCount != ResultsWritten)
106     //{
107     // string message = string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8} -- offset: 0x{2:x8}", ResultCount, ResultsWritten, ResultCount - ResultsWritten);
108     // Logging.logger.Error.WriteLine(message);
109     // throw new InvalidOperationException(message);
110     //}
111 william 445 if (ResultCount != ResultsWritten)
112     {
113 william 456 // update the number of results written
114     binWriter.BaseStream.Seek(ResultCountOffset, SeekOrigin.Begin);
115     binWriter.Write(ResultsWritten);
116 william 445 }
117 william 456 base.Dispose(disposing);
118 william 429 }
119 william 445 catch (Exception ex)
120     {
121     throw ex;
122     }
123 william 405 }
124 william 401 }
125     }

  ViewVC Help
Powered by ViewVC 1.1.22