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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 450 - (hide annotations) (download)
Sun Jun 2 20:38:58 2013 UTC (8 years, 4 months ago) by william
File size: 5171 byte(s)

1 william 401 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5 william 402 using System.IO;
6 william 401
7     namespace RomCheater.Serialization
8     {
9 william 402 public abstract class SerializationReader : ISerializationResultCount, IDisposable
10 william 401 {
11 william 450 private void CreateFilename(Guid guid)
12 william 444 {
13     if (guid == Guid.Empty)
14     {
15 william 448 if (this.TemporaryFolder != string.Empty)
16     {
17 william 450 FileName = string.Format(@"{0}\{1}\{2}", DEFAULT_FOLDER, TemporaryFolder, DEFAULT_FILENAME);
18 william 448 }
19     else
20     {
21 william 450 FileName = string.Format(@"{0}\{1}", DEFAULT_FOLDER, DEFAULT_FILENAME);
22 william 448 }
23 william 450
24 william 444 }
25     else
26     {
27 william 448 if (this.TemporaryFolder != string.Empty)
28     {
29 william 450 FileName = string.Format(@"{0}\{1}\{2}", DEFAULT_FOLDER, TemporaryFolder, string.Format("{0}.tmp", guid.ToString()));
30 william 448 }
31     else
32     {
33 william 450 FileName = string.Format(@"{0}\{1}", DEFAULT_FOLDER, string.Format("{0}.tmp", guid.ToString()));
34 william 448 }
35 william 444 }
36     }
37 william 448
38     public const string DEFAULT_FOLDER = "tmp";
39     protected const string DEFAULT_FILENAME = "SerializedMemoryResults.tmp";
40     //private static string DEFAULT_PATH = string.Format(@"{0}\{1}", DEFAULT_FOLDER, DEFAULT_FILENAME);
41 william 444 //public SerializationReader() : this(CreateFilename(Guid.Empty)) { CreateReader(Guid.Empty); }
42 william 448 public SerializationReader(Guid guid) { CreateFilename(guid); CreateReader(guid); }
43     //protected SerializationReader(string filename) { FileName = filename; }
44 william 402
45 william 448 protected virtual string TemporaryFolder { get { return string.Empty; } }
46 william 402
47 william 446 protected bool Cancelled { get; set; }
48    
49     public void CancelRequest() { Cancelled = true; }
50    
51 william 402 #region ISerializationResultCount members
52     public int ResultCount { get; protected set; }
53     #endregion
54     private FileStream fileStream;
55     protected BinaryReader binReader;
56     protected string FileName { get; private set; }
57 william 444 private void CreateReader(Guid guid)
58 william 402 {
59 william 446 if (Cancelled) { return; }
60 william 402 fileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
61     binReader = new BinaryReader(fileStream);
62     }
63     #region IDisposable Support
64     // Track whether Dispose has been called.
65     private bool disposed = false;
66     // Implement IDisposable.
67     // Do not make this method virtual.
68     // A derived class should not be able to override this method.
69     public void Dispose()
70     {
71     Dispose(true);
72     // This object will be cleaned up by the Dispose method.
73     // Therefore, you should call GC.SupressFinalize to
74     // take this object off the finalization queue
75     // and prevent finalization code for this object
76     // from executing a second time.
77     GC.SuppressFinalize(this);
78     }
79     // Dispose(bool disposing) executes in two distinct scenarios.
80     // If disposing equals true, the method has been called directly
81     // or indirectly by a user's code. Managed and unmanaged resources
82     // can be disposed.
83     // If disposing equals false, the method has been called by the
84     // runtime from inside the finalizer and you should not reference
85     // other objects. Only unmanaged resources can be disposed.
86     protected virtual void Dispose(bool disposing)
87     {
88     // Check to see if Dispose has already been called.
89     if (!this.disposed)
90     {
91     // If disposing equals true, dispose all managed
92     // and unmanaged resources.
93     if (disposing)
94     {
95     // Dispose managed resources.
96     //component.Dispose();
97     }
98    
99     // Call the appropriate methods to clean up
100     // unmanaged resources here.
101     // If disposing is false,
102     // only the following code is executed.
103    
104     // clean up any resources
105     binReader.Close();
106     binReader = null;
107     fileStream = null;
108     // Note disposing has been done.
109     disposed = true;
110    
111     }
112     }
113     // Use C# destructor syntax for finalization code.
114     // This destructor will run only if the Dispose method
115     // does not get called.
116     // It gives your base class the opportunity to finalize.
117     // Do not provide destructors in types derived from this class.
118     ~SerializationReader()
119     {
120     // Do not re-create Dispose clean-up code here.
121     // Calling Dispose(false) is optimal in terms of
122     // readability and maintainability.
123     Dispose(false);
124     }
125     #endregion
126 william 401 }
127     }

  ViewVC Help
Powered by ViewVC 1.1.22