ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater/Serialization/SerializationWriter.cs
Revision: 446
Committed: Sun Jun 2 19:52:03 2013 UTC (9 years, 10 months ago) by william
File size: 5700 byte(s)
Log Message:
+ fix search cancel/reset

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 william 446
44     protected bool Cancelled { get; set; }
45     public void CancelRequest() { Cancelled = true; }
46 william 401 private bool DeleteResultFile()
47     {
48     try
49     {
50     FileInfo fi = new FileInfo(FileName);
51     if (fi.Exists)
52     {
53     fi.Delete();
54     return true;
55     }
56     return true;
57     }
58     catch
59     {
60     return false;
61     }
62     }
63 william 402 private void CreateFile(bool delete)
64 william 401 {
65 william 402 if (delete)
66     DeleteResultFile();
67 william 401 FileInfo fi = new FileInfo(FileName);
68     if (!fi.Exists)
69     {
70     if (!fi.Directory.Exists)
71     fi.Directory.Create();
72     FileStream t = fi.Create();
73     t.Close();
74     }
75     fi = null;
76     fileStream = new FileStream(FileName, FileMode.Append, FileAccess.Write, FileShare.Read);
77     binWriter = new BinaryWriter(fileStream);
78     }
79     //protected void Write(byte[] data)
80     //{
81     // binWriter.Write(data);
82     //}
83    
84     #region IDisposable Support
85     // Track whether Dispose has been called.
86     private bool disposed = false;
87     // Implement IDisposable.
88     // Do not make this method virtual.
89     // A derived class should not be able to override this method.
90     public void Dispose()
91     {
92     Dispose(true);
93     // This object will be cleaned up by the Dispose method.
94     // Therefore, you should call GC.SupressFinalize to
95     // take this object off the finalization queue
96     // and prevent finalization code for this object
97     // from executing a second time.
98     GC.SuppressFinalize(this);
99     }
100     // Dispose(bool disposing) executes in two distinct scenarios.
101     // If disposing equals true, the method has been called directly
102     // or indirectly by a user's code. Managed and unmanaged resources
103     // can be disposed.
104     // If disposing equals false, the method has been called by the
105     // runtime from inside the finalizer and you should not reference
106     // other objects. Only unmanaged resources can be disposed.
107     protected virtual void Dispose(bool disposing)
108     {
109     // Check to see if Dispose has already been called.
110     if (!this.disposed)
111     {
112     // If disposing equals true, dispose all managed
113     // and unmanaged resources.
114     if (disposing)
115     {
116     // Dispose managed resources.
117     //component.Dispose();
118     }
119    
120     // Call the appropriate methods to clean up
121     // unmanaged resources here.
122     // If disposing is false,
123     // only the following code is executed.
124    
125     // clean up any resources
126     binWriter.Flush();
127     binWriter.Close();
128     binWriter = null;
129     fileStream = null;
130     // Note disposing has been done.
131     disposed = true;
132    
133     }
134     }
135     // Use C# destructor syntax for finalization code.
136     // This destructor will run only if the Dispose method
137     // does not get called.
138     // It gives your base class the opportunity to finalize.
139     // Do not provide destructors in types derived from this class.
140     ~SerializationWriter()
141     {
142     // Do not re-create Dispose clean-up code here.
143     // Calling Dispose(false) is optimal in terms of
144     // readability and maintainability.
145     Dispose(false);
146     }
147     #endregion
148     }
149     }