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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 472 - (show annotations) (download)
Mon Jun 3 07:39:39 2013 UTC (8 years, 4 months ago) by william
File size: 6842 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22