GlobalAccessSoftware icon

SearchForm3.cs

GlobalAccessSoftware | PRO | 10/04/19 12:26:02 AM UTC | 0 ⭐ | 993 👁️ | Never ⏰ | []
C# |

13.84 KB

|

None

|

0 👍

/

0 👎

 
#region Top Part
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// MyCustomLibrary.SearchForm3.cs 
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// v5.2.7.12 05-May-2019 Created and Roughed
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// v5.2.7.14 12-May-2019 Great Design Stage.
// v5.2.8.15 15-May-2019 Comin along nicely!
// v5.2.8.16 16-May-2019 EyeOutBug Fixed ++
// v5.2.8.17 18-May Split out from SF1 for
// Buttons & getting ready for After-Dialog!
// v5.2.8.18 26-May-2019 Worked into SF3:
// v5.2.8.19 31-May Mechanisms such as the
// PropertyChanged Events & App Detect plus
// Tie-ins to Both MyEditor & PrintBlaster.
// v5.2.9.19 June 2019, Ready Mods ++
// v5.2.9.20 07-June Integration w/MyEditor!
// v5.3.0.21 10-June Brought MySearchType &
// SearchCom into SearchForm2, so now
// it's MST for the Mutexes AND MySearchType
// and SearchForm Class for what was MSC b4.
// v5.3.0.21 15-June Getting Serious Now!
// v5.3.1.23 25-Jun Coming Together! Rev-1;
// v5.3.1.24 03-Jul FindByName2 Mod + Comm.
// v5.3.1.25 10-July SearchForm Features plus.
// v5.3.3.27 18-Jul-2019 4 way comms.
// v5.3.3.28 21-Jul SmesaSearch.cs rockin it
// -=[ Design Revision 4 ]=- My Custom Type.
// v5.3.4.29 25-Jul-2019 Spelling things out
// Class{in-Class{in-Class}}; Custom Types:
// v5.3.5.30 30-July ReCoded4 WorkList2
// v5.3.5.31 03-Aug ReCoded4 WorkList2
// v5.4.5.32 07-Aug-2019 Search & Replace.
// v5.4.5.33 12-Aug SF4 Split Out & Polish.
// v5.4.5.34 18-Aug Sorting things out.
// v5.4.6.36 29-Aug Work down testwells &
// BreakPoints, Clean and Polish++
// v5.4.7.40 24-Sep-2019 Settling down after
// some evolution during tracing & testing.
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
 
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Security;
using System.Threading;
using System.Windows.Forms;
 
// Class Aliases
using MCD = MyCustomLibrary.Dialogs;
using MMF = MyCustomLibrary.MemoryMapper;
using MCT = MyCustomLibrary.MyToolbox;
 
// Property Aliases
using MCR = MyCustomLibrary.Properties.Resources;
using MCS = MyCustomLibrary.Properties.Settings;
 
#endregion Pop Tart
    //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
 
 
namespace MyCustomLibrary
{
  public partial class SearchForm
  {
#region Lower Level / Instrumental Methods
 
#region By Name Method Group
 
    // REM: This Method is called from Loops
    void FindByName1()
    {
      const bool  calledByMe = false;
      FindByName1(calledByMe);
    }
    // This OverLoad is ONLY directly called
    // from  FindByName2(); & the overload
    // above. (True only if ME calling.)
    void FindByName1(bool find2)
    {
      Indexes = new Collection<int>();
      foreach (var file in MCD.WholeList
        .Where(file => !string
          .IsNullOrEmpty(searchBox.Text)
        && !string.IsNullOrEmpty(
          file) && File.Exists(file)))
      {
        if (wordCheckBox.Checked)
          WordUp(file);
        else 
          BasketCase(file);
      }
      if ( Indexes != null
        && Indexes.Count > 0
        && !find2) UpdateSelected();
      else if (!find2)
        MessageBox.Show(MCR.Nada);
    }
 
    void UpdateSelected()
    {
      var temp = new Collection<string>();//
      var hole = MCD.WholeList;
      foreach (var index in Indexes
        .Where(index => index >= 0
          && index < hole.Count))
            temp.Add(hole[index]);
      var t = 0;
      if (!MCT.P1Running) t = RunBlaster();
      MCD.Selected = temp;
      if (t < 9)
      {
        #region Design Note:
        // Psuedo Property.Changed   |
        // Event, Triggers call      |
        // to: SendSelected(below);  v
        #endregion Design Note:
        IndexesFound = Indexes;// <-===<<
        Indexes.Clear();
        Validate();
        return;
      }
      if (!MCT.Testing) return;
      #region Permanent TestCode
      MessageBox.Show(string.Format(
        "{0}{1}{2}{3}{4}",
        MCR.TimeOut1,          // 0
        Path.GetFileName(      // 1
          MCT.GetRoot + MCR.BlasterApp),
        MCR.TimeOut2,          // 2
        MCR.UpDate,            // 3
        "\n\nCycles === " + t)); // 4
      #endregion
    }
 
    void SendSelected()
    {
      var ct = IndexesFound.Count;
      if (ct < 1) return;
      var sl = new Collection<string>();
      for (var i = 0; i < ct; ++i)
        sl.Add(MCD.WholeList[
          IndexesFound[i]]);
 
      // uses strings now no nums past here!
      var fn = Application.StartupPath +
        MCR.Selected;
      var args = MCD.GetArgs(fn, 0);
      args[3] = sl;
      if (!MMF.WriteStrings(args))
        MessageBox.Show(MCR.Err10);
    }
 
    static int RunBlaster()
    {
      #region testcode UpdateSelected
      //const string txt =
      //  "Update Selected PB NOT" +
      //  " Running, HIT!";
      //MessageBox.Show(txt);
      #endregion
      var file =
        MCT.GetRoot + MCR.BlasterApp;
      if (!File.Exists(file)) return 9;//\\
      var args = new[]
      { file, MCR.Blaster, MCR.Blaster };
      // breakpoint RunBlaster()
      MCT.RunApp(args);
      var t = 0;
      do Thread.Sleep(333);
      while (!MCT.P1Running && ++t < 9);
      Thread.Sleep(1212);
      return t;
    }
 
    void BasketCase(string file)
    {
      if (caseCheckBox.Checked)
      {
        if (file.Contains(searchBox.Text))//
          Indexes.Add(
            MCD.WholeList.IndexOf(file));
      }
      else // case not checked.
      {
        if (file.ToLower().Contains(
          searchBox.Text.ToLower()))
          Indexes.Add(
            MCD.WholeList.IndexOf(file));
      }
    }
    
    void WordUp(string file)
    {
      var fn = Path.
        GetFileNameWithoutExtension(file);
      if (string.IsNullOrEmpty(fn)) return;
      if (wordCheckBox.Checked)
      {
        if (searchBox.Text.Contains(fn))
          Indexes.Add(
            MCD.WholeList.IndexOf(file));
      }
      else // word not checked.
      {
        if (searchBox.Text.ToLower()
          .Contains(fn.ToLower()))
          Indexes.Add(
            MCD.WholeList.IndexOf(file));
      }
    }
#endregion By Name Method Group
    //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
 
 
#region By Content Method Group
 
    string[] Lines { get; set; }
 
    void ContentOfCurrent(bool solo)
    {
      #region Note: Search4 var is reused
      // it has done it's initial job of
      // bringing in the search string to
      // Load. Once SearchForm is loaded
      // that value becomes searchBox.Text
      // & we can reuse Search2 (4) as the
      // filename to pass in at invokation.
      #endregion
      if (!solo) return;
      Search2 = MCD.Current;
      ContentOfCurrent();
    }
 
    bool ReadActualFile()
    {
      var fn = Search2;
      if (string.IsNullOrEmpty(fn)
        || !File.Exists(fn)) return false;
      try
      {
        var test = File.ReadAllLines(fn);
        if (test.Length < 1) return false;
        Lines = test;
        return true;
      }
      #region Catches
      catch (ArgumentException)
      { return false; }
      catch (PathTooLongException)
      { return false; }
      catch (DirectoryNotFoundException)
      { return false; }
      catch (IOException)
      { return false; }
      catch (UnauthorizedAccessException)
      { return false; }
      catch (NotSupportedException)
      { return false; }
      catch (SecurityException)
      { return false; }
      #endregion Catches
    }
 
    // This is also called from Loops.
    void ContentOfCurrent()
    {
      if ( MCD.Selected == null
        || MCD.Selected.Count < 1
        || !MCD.IsFile(Search2)
        || !ReadActualFile()
        || Lines == null
        || Lines.Length < 1) return;
 
      var  file = Get1Record(Search2);
      if (file == null
        || file.Line == null
        || file.Line.Count < 1
        || string.IsNullOrEmpty(file.Name))
        return;
 
      Info.File.Add(file);
    }
 
    // -=[ Updated for Custom Type ]=-
    // v5.3.5.31 02-Aug-2019 by -JpE-
    Files Get1Record(string fn)
    {
      if (string.IsNullOrEmpty(fn)
        || !File.Exists(fn)) return null;
 
      var  fileN = new Files(
        new Collection<Lines>())
      { Name = fn };
 
      var  lines = GetLines();
      if ( lines!= null
        && lines.Star != null
        && lines.Star.Count > 0
        ) fileN.Line.Add(lines);
 
      return fileN;
    }
 
    Lines GetLines()
    {
      Lines line = null;
      var c = Lines.Length;
      for (var y = 0; y < c; ++y)
      {
        if (wordCheckBox.Checked)
        {
          // breakpoint wordCheckBox.Checked
          if (string.IsNullOrEmpty(
            Lines[y]) || !Lines[y].Equals
            (searchBox.Text)) continue;
        }
        else
          if (string.IsNullOrEmpty(Lines[y])
            || !Lines[y].Contains(
              searchBox.Text)
            ) continue;
 
        var test = FindHits(Lines[y]);
        if (test == null
          ||test.Count < 1
          ) continue;
 
        // Hits Found & Set, So is Y;
        line = new Lines(test)
        { LineNumber = y };
      }
      return line;//s;
    }
 
    // v5.4.5.32 07-Aug-2019
    Collection<int> FindHits(string line)
    {
      var case11 = GetCase();
      var search = searchBox.Text;
 
      var word12 = wordCheckBox.Checked;
      if (word12) search = string.Format(
        " {0} ", search);
 
      if (word12 && !line.Contains(
        search)) return null;
 
      var hits = new Collection<int>();
      var accu = 0;
 
      while (line.Length >=
        searchBox.Text.Length
        && line.Contains(search))
      {
        var xCoord = line.IndexOf(
          search, case11);
        if (xCoord < 0) continue;
 
        // Accumulator: Orig Line Positions.
        hits.Add(accu += xCoord);
        if (search.Length >
          line.Length) break;
 
        line = line.Remove(
          0, xCoord + search.Length);
      }
      return hits;
    }
 
    StringComparison GetCase()
    {
      return caseCheckBox.Checked
        ? StringComparison
        // case sensitive
            .InvariantCulture
        : StringComparison
        // ignore case
            .InvariantCultureIgnoreCase;
    }
#endregion By-Content Method Group
    //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
 
 
#endregion Instrumental Low Level Members
    //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
 
 
#region Higher Level Methods ALL use Instr
 
#region Higher Level: Content Method Group..
 
    void ContentOfSelected()
    {
      if (MCD.Selected == null
       || MCD.Selected.Count < 1) return;
      ProcessContentList(MCD.Selected);
    }
 
    // ^v send Selected list to COC via
    void ProcessContentList(
      IEnumerable<string> files)
    {
      foreach (var fn in files.Where(
        fn => !string.IsNullOrEmpty(fn)
        && File.Exists(fn)))
      {
        Search2 = fn;
        ContentOfCurrent();
      }
    }
 
    // Called by both ME & PB as stated;
    // REM: MCD already arranged it, if;
    // ME calling edited is MCD.Current;
    // Selected  = Files Open in Editor;
    // WholeList = if(ME) Favorites.sme;
    void ContentOfAll()
    {
      //Testing(); // Keep
      if (MCD.WholeList == null
       || MCD.WholeList.Count < 1) return;
      SearchAll = true;
      ProcessContentList(MCD.WholeList);
    }
    /* Keep Test Code */
    //static void Testing()
    //{
    //  
    //  const string
    //      b = "Both",
    //      e = "Editor",
    //      c = "Cancel";
    //  var s = "Error";
 
    //  while (!s.Equals("Cancel"))
    //  {
    //    s = "Blaster";
    //    var d = Choice();
    //    switch (d)
    //    {
    //      case 1: s = b; break;
    //      case 2: s = e; break;
    //      case 4: s = c; break;
    //    }
    //    MessageBox.Show(
    //      string.Format("{0} {1}",
    //        "You Selected: ", s));
    //  }
    //}
#endregion Higher Level: Content-Members
    //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
 
 
#region Higher Level: By-Name Method Group.
 
    // Hit only by M.E. Indexes go 2 P.B. as
    // before, but via FileSystemWatcher,
    // and we'll also Send an ME-2nd
    // instance the ones found in Selected:
    // That will tab to them w/ proven code.
    void FindByName2()
    {
      FindByName1(true);
      CheckList();
      if ( Indexes == null
        || Indexes.Count < 1)
      { MessageBox.Show(MCR.Nada);
        return; }
 
      var opn = new Collection<string>();
      foreach (var file in from file in
        MCD.OpenFiles let idx= MCD.WholeList
        .IndexOf(file)where Indexes.Contains(
        idx) select file) opn.Add(file);
 
      UpdateSelected();
      // ^ SendSelected call now in there.
      if (opn.Count < 1) return;
      SendOpen2Me(opn);
    }
 
    // Sends filenames of Open with a
    // Match to a thread of ME.Process
    static void
      SendOpen2Me(IList<string> opn)
    {
      var fil = Application
        .StartupPath + MCR.EditorApp;
 
      if (opn == null
        || !File.Exists(fil)) return;
 
      opn.Insert(0, fil);
 
      var ary =
        MCT.Collection2Array(opn, 0);
 
      MCT.RunApp(ary);
    }
 
    // Checks for anything Open in ME
    // that's NOT in Managed, & adds it.
    // Append; Don't mess up the order
    // of indices as they're relied upon!
    static void CheckList()
    {
      foreach (var file in MCD.OpenFiles
        .Where(file => !MCD.WholeList
          .Contains(file)))
            MCD.WholeList.Add(file);
    }
#endregion Higher Level: By-Name Members.
    //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
 
 
#endregion Higher Levels use Instrumentals.
    //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
 
 
  }
}

Comments