ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater/Serialization/SerializationWriter.cs
Revision: 405
Committed: Thu Jun 21 14:38:54 2012 UTC (11 years, 5 months ago) by william
File size: 5146 byte(s)
Log Message:

File Contents

# Content
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 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 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
24 #region ISerializationResultCount members
25 public int ResultCount { get; protected set; }
26 #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 private void CreateFile(bool delete)
48 {
49 if (delete)
50 DeleteResultFile();
51 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 }