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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22