Ejemplo n.º 1
0
        /// <summary>
        /// Filters the log file based on <see cref="shipLoc"/> and the sector range listed in <see cref="textRange.Text"/>.</summary>
        /// Range calculation is simplified into a box shape for speed; min and max sector values are calculated for all three axis and the Map Log coordinates are kept if they fit in this box.
        private bool filterLog()
        {
            // Temporary storage for the new contents of the log file
            MapList tempLog = new MapList ();
            int range = MapPoint.intParse (txtRange.Text);
            // calculate sector ranges (+1 to make the range inclusive)
            int xmin = (int)shipLoc [0] - (range + 1);
            int xmax = (int)shipLoc [0] + (range + 1);
            int ymin = (int)shipLoc [1] - (range + 1);
            int ymax = (int)shipLoc [1] + (range + 1);
            int zmin = (int)shipLoc [2] - (range + 1);
            int zmax = (int)shipLoc [2] + (range + 1);

            // search for Map Log entries that are within the specified range
            foreach (MapPoint point in lstMasterLog) {
                if (point.sx > xmin && point.sx < xmax &&
                    point.sy > ymin && point.sy < ymax &&
                    point.sz > zmin && point.sz < zmax) {
                    tempLog.Add (point);
                }
            }

            // sort the new list, if desired
            if (chkSort.Active && sortNeeded) {
                tempLog.Sort (sorter);
                sortNeeded = false;
            }

            // convert the new list into a string before opening the file for write (minimize the time the file is open)
            string tempLogText = tempLog.ToString();
            // Replace the Map Log with the filtered resaults
            try {
                // delete the contents of the file before re-writing it. It's the easy way to ensure the file is overwritten and not appended to.
                deleteFile(btnMapLog.Label);
                using (System.IO.StreamWriter newLog = new System.IO.StreamWriter(OpenFile(btnMapLog.Label,true))) {
                    newLog.Write(tempLog.ToString());
                    newLog.Close();
                    // remember what was written
                    textMapLogFile = tempLogText;
                    // remember when the file was last written
                    lastLogged = getLastWriteTimeUTC(btnMapLog.Label);
                }
            } catch (System.ArgumentNullException) {
                // OpenFile should have already given an error message since OpenFile() returns null on error
            }
            return true;
        }