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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 451 - (hide annotations) (download)
Sun Jun 2 21:37:09 2013 UTC (8 years, 1 month ago) by william
File size: 5070 byte(s)
attempt to reduce memory usage by:
1) opening the serialized results
2) seeking to the current result entry
3) read the data
4) close the data stream
5) return the result

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 451 public SerializationReader(Guid guid) { CreateFilename(guid); }
43 william 448 //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 william 451 //private FileStream fileStream;
55     //protected BinaryReader binReader;
56 william 402 protected string FileName { get; private set; }
57 william 451 protected FileStream CreateReader(Guid guid)
58 william 402 {
59 william 451 return new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
60 william 402 }
61     #region IDisposable Support
62     // Track whether Dispose has been called.
63     private bool disposed = false;
64     // Implement IDisposable.
65     // Do not make this method virtual.
66     // A derived class should not be able to override this method.
67     public void Dispose()
68     {
69     Dispose(true);
70     // This object will be cleaned up by the Dispose method.
71     // Therefore, you should call GC.SupressFinalize to
72     // take this object off the finalization queue
73     // and prevent finalization code for this object
74     // from executing a second time.
75     GC.SuppressFinalize(this);
76     }
77     // Dispose(bool disposing) executes in two distinct scenarios.
78     // If disposing equals true, the method has been called directly
79     // or indirectly by a user's code. Managed and unmanaged resources
80     // can be disposed.
81     // If disposing equals false, the method has been called by the
82     // runtime from inside the finalizer and you should not reference
83     // other objects. Only unmanaged resources can be disposed.
84     protected virtual void Dispose(bool disposing)
85     {
86     // Check to see if Dispose has already been called.
87     if (!this.disposed)
88     {
89     // If disposing equals true, dispose all managed
90     // and unmanaged resources.
91     if (disposing)
92     {
93     // Dispose managed resources.
94     //component.Dispose();
95     }
96    
97     // Call the appropriate methods to clean up
98     // unmanaged resources here.
99     // If disposing is false,
100     // only the following code is executed.
101    
102 william 451 //// clean up any resources
103     //binReader.Close();
104     //binReader = null;
105     //fileStream = null;
106 william 402 // Note disposing has been done.
107     disposed = true;
108    
109     }
110     }
111     // Use C# destructor syntax for finalization code.
112     // This destructor will run only if the Dispose method
113     // does not get called.
114     // It gives your base class the opportunity to finalize.
115     // Do not provide destructors in types derived from this class.
116     ~SerializationReader()
117     {
118     // Do not re-create Dispose clean-up code here.
119     // Calling Dispose(false) is optimal in terms of
120     // readability and maintainability.
121     Dispose(false);
122     }
123     #endregion
124 william 401 }
125     }

  ViewVC Help
Powered by ViewVC 1.1.22