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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22