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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/RomCheater/Serialization/SerializationWriter.cs	2012/06/21 05:14:38	401
+++ trunk/RomCheater/Serialization/SerializationWriter.cs	2013/06/02 20:22:04	448
@@ -6,27 +6,72 @@
 
 namespace RomCheater.Serialization
 {
-    public interface ISerializationWriter
+    public interface ISerializationResultCount
     {
-        int ResultCount { get; }
+        int ResultCount { get; }     
     }
-    public abstract class SerializationWriter : ISerializationWriter, IDisposable
+   
+    public abstract class SerializationWriter : ISerializationResultCount, IDisposable
     {
-        private const string DEFAULT_FOLDER = "tmp";
+        private void CreateFilename(Guid guid)
+        {
+            if (guid == Guid.Empty)
+            {
+                if (this.TemporaryFolder != string.Empty)
+                {
+                    FileName= string.Format(@"{0}\{1}\{2}", DEFAULT_FOLDER, TemporaryFolder, DEFAULT_FILENAME);
+                }
+                else
+                {
+                    FileName = string.Format(@"{0}\{1}", DEFAULT_FOLDER, DEFAULT_FILENAME);
+                }
+                
+            }
+            else
+            {
+                if (this.TemporaryFolder != string.Empty)
+                {
+                    FileName = string.Format(@"{0}\{1}\{2}", DEFAULT_FOLDER, TemporaryFolder, string.Format("{0}.tmp", guid.ToString()));
+                }
+                else
+                {
+                    FileName = string.Format(@"{0}\{1}", DEFAULT_FOLDER, string.Format("{0}.tmp", guid.ToString()));
+                }
+            }
+        }
+        protected const string DEFAULT_FOLDER = "tmp";
         private const string DEFAULT_FILENAME = "SerializedMemoryResults.tmp";
-        private static string DEFAULT_PATH = string.Format(@"{0}\{1}", DEFAULT_FOLDER,DEFAULT_FILENAME);
-        public SerializationWriter() : this(DEFAULT_PATH) { }
-        protected SerializationWriter(string filename) : this(filename, 0) { }
-        public SerializationWriter(int resultCount) : this(DEFAULT_PATH, resultCount) { }
-        public SerializationWriter(string filename, int resultCount) { FileName = filename; ResultCount = resultCount; CreateFile(); }
+        //private static string DEFAULT_PATH = string.Format(@"{0}\{1}", DEFAULT_FOLDER,DEFAULT_FILENAME);
+
+        protected virtual string TemporaryFolder { get { return string.Empty; } }
+
+        //public SerializationWriter() : this(CreateFilename(Guid.Empty)) {  }
+
+
+        public static void CleanupTemporarySearchResultFiles()
+        {
+            DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}\{1}", DEFAULT_FOLDER, SearchResultsConstants.SearchResultsFolder));
+            if (di.Exists)
+            {
+                di.Delete(true);
+            }
+        }
 
-        #region ISerializationWriter members
+
+        public SerializationWriter(int resultCount, Guid guid) : this(resultCount,guid, true) { }
+        public SerializationWriter(int resultCount, Guid guid, bool delete) { CreateFilename(guid); ResultCount = resultCount; CreateFile(delete); }
+        //protected SerializationWriter(Guid guid) : this(0, guid, true) { }
+        //protected SerializationWriter(string filename) { FileName = filename; }
+
+        #region ISerializationResultCount members
         public int ResultCount { get; protected set; }
-        
         #endregion
-        private FileStream fileStream;
+        protected FileStream fileStream;
         protected BinaryWriter binWriter;
         protected string FileName { get; private set; }
+
+        protected bool Cancelled { get; set; }
+        public void CancelRequest() { Cancelled = true; }
         private bool DeleteResultFile()
         {
             try
@@ -44,9 +89,10 @@
                 return false;
             }
         }
-        private void CreateFile()
+        private void CreateFile(bool delete)
         {
-            DeleteResultFile();
+            if (delete)
+                DeleteResultFile();
             FileInfo fi = new FileInfo(FileName);
             if (!fi.Exists)
             {

 

  ViewVC Help
Powered by ViewVC 1.1.22