ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater/Serialization/SerializationWriter.cs
Revision: 444
Committed: Sun Jun 2 18:56:41 2013 UTC (10 years, 5 months ago) by william
File size: 5591 byte(s)
Log Message:
+ update serialization reader/writer to include a search results guid

File Contents

# User Rev Content
1 william 401 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using System.IO;
6    
7     namespace RomCheater.Serialization
8     {
9 william 402 public interface ISerializationResultCount
10 william 401 {
11 william 405 int ResultCount { get; }
12 william 401 }
13 william 405
14 william 402 public abstract class SerializationWriter : ISerializationResultCount, IDisposable
15 william 401 {
16 william 444 private static string CreateFilename(Guid guid)
17     {
18     if (guid == Guid.Empty)
19     {
20     return DEFAULT_PATH;
21     }
22     else
23     {
24     return string.Format(@"{0}\{1}", DEFAULT_FOLDER, string.Format("{0}.tmp", guid.ToString()));
25     }
26     }
27 william 401 private const string DEFAULT_FOLDER = "tmp";
28     private const string DEFAULT_FILENAME = "SerializedMemoryResults.tmp";
29     private static string DEFAULT_PATH = string.Format(@"{0}\{1}", DEFAULT_FOLDER,DEFAULT_FILENAME);
30 william 444 //public SerializationWriter() : this(CreateFilename(Guid.Empty)) { }
31    
32     public SerializationWriter(int resultCount, Guid guid) : this(resultCount,guid, true) { }
33     public SerializationWriter(int resultCount, Guid guid, bool delete) : this(CreateFilename(guid)) { ResultCount = resultCount; CreateFile(delete); }
34     protected SerializationWriter(Guid guid) : this(0, guid, true) { }
35     protected SerializationWriter(string filename) { FileName = filename; }
36 william 401
37 william 402 #region ISerializationResultCount members
38 william 405 public int ResultCount { get; protected set; }
39 william 401 #endregion
40     private FileStream fileStream;
41     protected BinaryWriter binWriter;
42     protected string FileName { get; private set; }
43     private bool DeleteResultFile()
44     {
45     try
46     {
47     FileInfo fi = new FileInfo(FileName);
48     if (fi.Exists)
49     {
50     fi.Delete();
51     return true;
52     }
53     return true;
54     }
55     catch
56     {
57     return false;
58     }
59     }
60 william 402 private void CreateFile(bool delete)
61 william 401 {
62 william 402 if (delete)
63     DeleteResultFile();
64 william 401 FileInfo fi = new FileInfo(FileName);
65     if (!fi.Exists)
66     {
67     if (!fi.Directory.Exists)
68     fi.Directory.Create();
69     FileStream t = fi.Create();
70     t.Close();
71     }
72     fi = null;
73     fileStream = new FileStream(FileName, FileMode.Append, FileAccess.Write, FileShare.Read);
74     binWriter = new BinaryWriter(fileStream);
75     }
76     //protected void Write(byte[] data)
77     //{
78     // binWriter.Write(data);
79     //}
80    
81     #region IDisposable Support
82     // Track whether Dispose has been called.
83     private bool disposed = false;
84     // Implement IDisposable.
85     // Do not make this method virtual.
86     // A derived class should not be able to override this method.
87     public void Dispose()
88     {
89     Dispose(true);
90     // This object will be cleaned up by the Dispose method.
91     // Therefore, you should call GC.SupressFinalize to
92     // take this object off the finalization queue
93     // and prevent finalization code for this object
94     // from executing a second time.
95     GC.SuppressFinalize(this);
96     }
97     // Dispose(bool disposing) executes in two distinct scenarios.
98     // If disposing equals true, the method has been called directly
99     // or indirectly by a user's code. Managed and unmanaged resources
100     // can be disposed.
101     // If disposing equals false, the method has been called by the
102     // runtime from inside the finalizer and you should not reference
103     // other objects. Only unmanaged resources can be disposed.
104     protected virtual void Dispose(bool disposing)
105     {
106     // Check to see if Dispose has already been called.
107     if (!this.disposed)
108     {
109     // If disposing equals true, dispose all managed
110     // and unmanaged resources.
111     if (disposing)
112     {
113     // Dispose managed resources.
114     //component.Dispose();
115     }
116    
117     // Call the appropriate methods to clean up
118     // unmanaged resources here.
119     // If disposing is false,
120     // only the following code is executed.
121    
122     // clean up any resources
123     binWriter.Flush();
124     binWriter.Close();
125     binWriter = null;
126     fileStream = null;
127     // Note disposing has been done.
128     disposed = true;
129    
130     }
131     }
132     // Use C# destructor syntax for finalization code.
133     // This destructor will run only if the Dispose method
134     // does not get called.
135     // It gives your base class the opportunity to finalize.
136     // Do not provide destructors in types derived from this class.
137     ~SerializationWriter()
138     {
139     // Do not re-create Dispose clean-up code here.
140     // Calling Dispose(false) is optimal in terms of
141     // readability and maintainability.
142     Dispose(false);
143     }
144     #endregion
145     }
146     }