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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 812 - (hide annotations) (download)
Tue Apr 15 14:52:10 2014 UTC (7 years, 3 months ago) by william
File size: 6988 byte(s)
+ update logging to use Enterpise.Logging -- more work is still needed

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

  ViewVC Help
Powered by ViewVC 1.1.22