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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 486 - (hide annotations) (download)
Mon Jun 3 20:11:43 2013 UTC (7 years, 11 months ago) by william
File size: 5444 byte(s)
+ allow the delete of result files, when reading (if delete is true)

1 william 486 #region Logging Defines
2     // include this any class or method that required logging, and comment-out what is not needed
3    
4     #region Enabled logging levels
5     #define LOGGING_ENABLE_INFO
6     #define LOGGING_ENABLE_WARN
7     #define LOGGING_ENABLE_DEBUG
8     #define LOGGING_ENABLE_VERBOSEDEBUG
9     #define LOGGING_ENABLE_ERROR
10     #define LOGGING_ENABLE_VERBOSEERROR
11     #define LOGGING_ENABLE_PROFILER
12     #endregion
13     #endregion
14     using System;
15 william 401 using System.Collections.Generic;
16     using System.Linq;
17     using System.Text;
18     using System.IO;
19 william 405 using System.Diagnostics;
20 william 401
21     namespace RomCheater.Serialization
22     {
23     public interface ISearchResultWriter
24     {
25     void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible;
26     }
27     public class SearchResultWriter : SerializationWriter, ISearchResultWriter
28     {
29 william 405
30     private int ResultsWritten = 0;
31 william 448 protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
32 william 444 //public SearchResultWriter() : base() { }
33 william 486 public SearchResultWriter(int resultCount, Guid guid) : this(resultCount, guid,false) { WriteHeader(guid); }
34     public SearchResultWriter(int resultCount, Guid guid, bool delete) : base(resultCount, guid,delete) { WriteHeader(guid); }
35 william 444 //public SearchResultWriter(string filename, Guid guid) : base(CreateFileName(filename,guid), guid) { WriteHeader(guid); }
36     //public SearchResultWriter(string filename, int resultCount, Guid guid) : base(filename, resultCount, guid, false) { WriteHeader(guid); }
37     //public SearchResultWriter(string filename, int resultCount, Guid guid, bool delete) : base(filename, resultCount, guid, delete) { WriteHeader(guid); }
38 william 401 #region ISearchResultWriter members
39     public void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible
40     {
41 william 445 try
42     {
43     Type t = typeof(TValue);
44 william 401
45 william 445 binWriter.Write(address);
46     switch (t.Name.ToLower())
47     {
48     case "byte":
49     binWriter.Write(Convert.ToByte(value));
50     break;
51     case "sbyte":
52     binWriter.Write(Convert.ToSByte(value));
53     break;
54     case "uint16":
55     binWriter.Write(Convert.ToUInt16(value));
56     break;
57     case "int16":
58     binWriter.Write(Convert.ToInt16(value));
59     break;
60     case "uint32":
61     binWriter.Write(Convert.ToUInt32(value));
62     break;
63     case "int32":
64     binWriter.Write(Convert.ToInt32(value));
65     break;
66     case "uint64":
67     binWriter.Write(Convert.ToUInt64(value));
68     break;
69     case "int64":
70     binWriter.Write(Convert.ToInt64(value));
71     break;
72     }
73     ResultsWritten++;
74     }
75     catch (Exception ex)
76 william 401 {
77 william 445 throw ex;
78 william 401 }
79     }
80     #endregion
81    
82 william 456 long ResultCountOffset = 0;
83 william 444 private void WriteHeader(Guid guid)
84 william 401 {
85 william 445 try
86     {
87     // write magic: SRD
88     binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
89 william 444
90 william 445 if (guid == Guid.Empty)
91     {
92     binWriter.Write((int)1);
93     }
94     else
95     {
96     binWriter.Write((int)2);
97     // related guid - length
98     binWriter.Write(guid.ToByteArray().Length);
99     // related guid - data
100     binWriter.Write(guid.ToByteArray());
101     }
102     // write count (int)
103     binWriter.Write(ResultCount);
104 william 456 ResultCountOffset = binWriter.BaseStream.Position - sizeof(int);
105 william 445 }
106     catch (Exception ex)
107 william 444 {
108 william 445 throw ex;
109 william 444 }
110 william 401 }
111 william 405
112     protected override void Dispose(bool disposing)
113 william 445 {
114 william 448 if (Cancelled) { binWriter.Close(); fileStream.Close(); return; }
115 william 445 try
116 william 405 {
117 william 456
118     //// ensure ResultCount and ResultsWritten are equal
119     //if (ResultCount != ResultsWritten)
120     //{
121     // string message = string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8} -- offset: 0x{2:x8}", ResultCount, ResultsWritten, ResultCount - ResultsWritten);
122     // Logging.logger.Error.WriteLine(message);
123     // throw new InvalidOperationException(message);
124     //}
125 william 445 if (ResultCount != ResultsWritten)
126     {
127 william 456 // update the number of results written
128     binWriter.BaseStream.Seek(ResultCountOffset, SeekOrigin.Begin);
129     binWriter.Write(ResultsWritten);
130 william 445 }
131 william 456 base.Dispose(disposing);
132 william 429 }
133 william 445 catch (Exception ex)
134     {
135     throw ex;
136     }
137 william 405 }
138 william 401 }
139     }

  ViewVC Help
Powered by ViewVC 1.1.22