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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22