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

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

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

--- trunk/RomCheater/Serialization/SearchResultWriter.cs	2013/06/02 18:56:41	444
+++ trunk/RomCheater/Serialization/SearchResultWriter.cs	2013/06/03 01:45:40	456
@@ -15,7 +15,7 @@
     {
 
         private int ResultsWritten = 0;
-
+        protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
         //public SearchResultWriter() : base() { }
         public SearchResultWriter(int resultCount, Guid guid) : base(resultCount, guid) { WriteHeader(guid); }
         //public SearchResultWriter(string filename, Guid guid) : base(CreateFileName(filename,guid), guid) { WriteHeader(guid); }
@@ -24,71 +24,101 @@
         #region ISearchResultWriter members
         public void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible
         {
+            try
+            {
+                Type t = typeof(TValue);
 
-            Type t = typeof(TValue);
-
-            binWriter.Write(address);
-            switch (t.Name.ToLower())
+                binWriter.Write(address);
+                switch (t.Name.ToLower())
+                {
+                    case "byte":
+                        binWriter.Write(Convert.ToByte(value));
+                        break;
+                    case "sbyte":
+                        binWriter.Write(Convert.ToSByte(value));
+                        break;
+                    case "uint16":
+                        binWriter.Write(Convert.ToUInt16(value));
+                        break;
+                    case "int16":
+                        binWriter.Write(Convert.ToInt16(value));
+                        break;
+                    case "uint32":
+                        binWriter.Write(Convert.ToUInt32(value));
+                        break;
+                    case "int32":
+                        binWriter.Write(Convert.ToInt32(value));
+                        break;
+                    case "uint64":
+                        binWriter.Write(Convert.ToUInt64(value));
+                        break;
+                    case "int64":
+                        binWriter.Write(Convert.ToInt64(value));
+                        break;
+                }
+                ResultsWritten++;
+            }
+            catch (Exception ex)
             {
-                case "byte":
-                    binWriter.Write(Convert.ToByte(value));
-                    break;
-                case "sbyte":
-                    binWriter.Write(Convert.ToSByte(value));
-                    break;
-                case "uint16":
-                    binWriter.Write(Convert.ToUInt16(value));
-                    break;
-                case "int16":
-                    binWriter.Write(Convert.ToInt16(value));
-                    break;
-                case "uint32":
-                    binWriter.Write(Convert.ToUInt32(value));
-                    break;
-                case "int32":
-                    binWriter.Write(Convert.ToInt32(value));
-                    break;
-                case "uint64":
-                    binWriter.Write(Convert.ToUInt64(value));
-                    break;
-                case "int64":
-                    binWriter.Write(Convert.ToInt64(value));
-                    break;
+                throw ex;
             }
-            ResultsWritten++;
         }
         #endregion
 
+        long ResultCountOffset = 0;
         private void WriteHeader(Guid guid)
         {
-            // write magic: SRD
-            binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
-
-            if (guid == Guid.Empty)
+            try
             {
-                binWriter.Write((int)1);
+                // write magic: SRD
+                binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
+
+                if (guid == Guid.Empty)
+                {
+                    binWriter.Write((int)1);
+                }
+                else
+                {
+                    binWriter.Write((int)2);
+                    // related guid - length
+                    binWriter.Write(guid.ToByteArray().Length);
+                    // related guid - data
+                    binWriter.Write(guid.ToByteArray());
+                }
+                // write count (int)
+                binWriter.Write(ResultCount);
+                ResultCountOffset = binWriter.BaseStream.Position - sizeof(int);
             }
-            else
+            catch (Exception ex)
             {
-                binWriter.Write((int)2);
-                // related guid - length
-                binWriter.Write(guid.ToByteArray().Length);
-                // related guid - data
-                binWriter.Write(guid.ToByteArray());
-            }            
-            // write count (int)
-            binWriter.Write(ResultCount);
+                throw ex;
+            }
         }
 
         protected override void Dispose(bool disposing)
-        {            
-            base.Dispose(disposing);
-            // ensure ResultCount and ResultsWritten are equal            
-            if (ResultCount != ResultsWritten)
-            {
-                string message = string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8} -- offset: 0x{2:x8}", ResultCount, ResultsWritten, ResultCount - ResultsWritten);
-                Logging.logger.Error.WriteLine(message);
-                throw new InvalidOperationException(message);
+        {
+            if (Cancelled) { binWriter.Close(); fileStream.Close(); return; }
+            try
+            {
+               
+                //// ensure ResultCount and ResultsWritten are equal            
+                //if (ResultCount != ResultsWritten)
+                //{
+                //    string message = string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8} -- offset: 0x{2:x8}", ResultCount, ResultsWritten, ResultCount - ResultsWritten);
+                //    Logging.logger.Error.WriteLine(message);
+                //    throw new InvalidOperationException(message);
+                //}
+                if (ResultCount != ResultsWritten)
+                {
+                    // update the number of results written
+                    binWriter.BaseStream.Seek(ResultCountOffset, SeekOrigin.Begin);
+                    binWriter.Write(ResultsWritten);
+                }
+                base.Dispose(disposing);
+            }
+            catch (Exception ex)
+            {
+                throw ex;
             }
         }
     }

 

  ViewVC Help
Powered by ViewVC 1.1.22