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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 486 - (hide annotations) (download)
Mon Jun 3 20:11:43 2013 UTC (8 years, 3 months ago) by william
File size: 7245 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    
20     namespace RomCheater.Serialization
21     {
22 william 402 public interface ISerializationResultCount
23 william 401 {
24 william 405 int ResultCount { get; }
25 william 401 }
26 william 405
27 william 402 public abstract class SerializationWriter : ISerializationResultCount, IDisposable
28 william 401 {
29 william 472 private string CreateDynamicFilename(Guid guid)
30 william 444 {
31     if (guid == Guid.Empty)
32     {
33 william 448 if (this.TemporaryFolder != string.Empty)
34     {
35 william 472 return string.Format(@"{0}\{1}\{2}", DEFAULT_FOLDER, TemporaryFolder, DEFAULT_FILENAME);
36 william 448 }
37     else
38     {
39 william 472 return string.Format(@"{0}\{1}", DEFAULT_FOLDER, DEFAULT_FILENAME);
40 william 448 }
41 william 472
42 william 444 }
43     else
44     {
45 william 448 if (this.TemporaryFolder != string.Empty)
46     {
47 william 472 return string.Format(@"{0}\{1}\{2}", DEFAULT_FOLDER, TemporaryFolder, string.Format("{0}.tmp", guid.ToString()));
48 william 448 }
49     else
50     {
51 william 472 return string.Format(@"{0}\{1}", DEFAULT_FOLDER, string.Format("{0}.tmp", guid.ToString()));
52 william 448 }
53 william 444 }
54     }
55 william 472 private void CreateFilename(Guid guid)
56     {
57     FileName = CreateDynamicFilename(guid);
58     }
59 william 448 protected const string DEFAULT_FOLDER = "tmp";
60 william 401 private const string DEFAULT_FILENAME = "SerializedMemoryResults.tmp";
61 william 448 //private static string DEFAULT_PATH = string.Format(@"{0}\{1}", DEFAULT_FOLDER,DEFAULT_FILENAME);
62    
63     protected virtual string TemporaryFolder { get { return string.Empty; } }
64    
65 william 444 //public SerializationWriter() : this(CreateFilename(Guid.Empty)) { }
66 william 448
67    
68     public static void CleanupTemporarySearchResultFiles()
69     {
70     DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}\{1}", DEFAULT_FOLDER, SearchResultsConstants.SearchResultsFolder));
71     if (di.Exists)
72     {
73     di.Delete(true);
74     }
75     }
76    
77    
78 william 444 public SerializationWriter(int resultCount, Guid guid) : this(resultCount,guid, true) { }
79 william 448 public SerializationWriter(int resultCount, Guid guid, bool delete) { CreateFilename(guid); ResultCount = resultCount; CreateFile(delete); }
80     //protected SerializationWriter(Guid guid) : this(0, guid, true) { }
81     //protected SerializationWriter(string filename) { FileName = filename; }
82 william 401
83 william 402 #region ISerializationResultCount members
84 william 405 public int ResultCount { get; protected set; }
85 william 401 #endregion
86 william 448 protected FileStream fileStream;
87 william 401 protected BinaryWriter binWriter;
88     protected string FileName { get; private set; }
89 william 446
90     protected bool Cancelled { get; set; }
91     public void CancelRequest() { Cancelled = true; }
92 william 401 private bool DeleteResultFile()
93     {
94     try
95     {
96     FileInfo fi = new FileInfo(FileName);
97     if (fi.Exists)
98     {
99     fi.Delete();
100     return true;
101     }
102     return true;
103     }
104     catch
105     {
106     return false;
107     }
108     }
109 william 402 private void CreateFile(bool delete)
110 william 401 {
111 william 402 if (delete)
112     DeleteResultFile();
113 william 401 FileInfo fi = new FileInfo(FileName);
114     if (!fi.Exists)
115     {
116     if (!fi.Directory.Exists)
117     fi.Directory.Create();
118     FileStream t = fi.Create();
119     t.Close();
120     }
121     fi = null;
122     fileStream = new FileStream(FileName, FileMode.Append, FileAccess.Write, FileShare.Read);
123     binWriter = new BinaryWriter(fileStream);
124     }
125     //protected void Write(byte[] data)
126     //{
127     // binWriter.Write(data);
128     //}
129    
130     #region IDisposable Support
131     // Track whether Dispose has been called.
132     private bool disposed = false;
133     // Implement IDisposable.
134     // Do not make this method virtual.
135     // A derived class should not be able to override this method.
136     public void Dispose()
137     {
138     Dispose(true);
139     // This object will be cleaned up by the Dispose method.
140     // Therefore, you should call GC.SupressFinalize to
141     // take this object off the finalization queue
142     // and prevent finalization code for this object
143     // from executing a second time.
144     GC.SuppressFinalize(this);
145     }
146     // Dispose(bool disposing) executes in two distinct scenarios.
147     // If disposing equals true, the method has been called directly
148     // or indirectly by a user's code. Managed and unmanaged resources
149     // can be disposed.
150     // If disposing equals false, the method has been called by the
151     // runtime from inside the finalizer and you should not reference
152     // other objects. Only unmanaged resources can be disposed.
153     protected virtual void Dispose(bool disposing)
154     {
155     // Check to see if Dispose has already been called.
156     if (!this.disposed)
157     {
158     // If disposing equals true, dispose all managed
159     // and unmanaged resources.
160     if (disposing)
161     {
162     // Dispose managed resources.
163     //component.Dispose();
164     }
165    
166     // Call the appropriate methods to clean up
167     // unmanaged resources here.
168     // If disposing is false,
169     // only the following code is executed.
170    
171     // clean up any resources
172     binWriter.Flush();
173     binWriter.Close();
174     binWriter = null;
175     fileStream = null;
176     // Note disposing has been done.
177     disposed = true;
178    
179     }
180     }
181     // Use C# destructor syntax for finalization code.
182     // This destructor will run only if the Dispose method
183     // does not get called.
184     // It gives your base class the opportunity to finalize.
185     // Do not provide destructors in types derived from this class.
186     ~SerializationWriter()
187     {
188     // Do not re-create Dispose clean-up code here.
189     // Calling Dispose(false) is optimal in terms of
190     // readability and maintainability.
191     Dispose(false);
192     }
193     #endregion
194     }
195     }

  ViewVC Help
Powered by ViewVC 1.1.22