Advertisement
GlobalAccessSoftware

SearchForm3.cs

Oct 3rd, 2019
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.84 KB | None | 0 0
  1.  
  2. #region Top Part
  3. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  4. // MyCustomLibrary.SearchForm3.cs
  5. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  6. // v5.2.7.12 05-May-2019 Created and Roughed
  7. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  8. // v5.2.7.14 12-May-2019 Great Design Stage.
  9. // v5.2.8.15 15-May-2019 Comin along nicely!
  10. // v5.2.8.16 16-May-2019 EyeOutBug Fixed ++
  11. // v5.2.8.17 18-May Split out from SF1 for
  12. // Buttons & getting ready for After-Dialog!
  13. // v5.2.8.18 26-May-2019 Worked into SF3:
  14. // v5.2.8.19 31-May Mechanisms such as the
  15. // PropertyChanged Events & App Detect plus
  16. // Tie-ins to Both MyEditor & PrintBlaster.
  17. // v5.2.9.19 June 2019, Ready Mods ++
  18. // v5.2.9.20 07-June Integration w/MyEditor!
  19. // v5.3.0.21 10-June Brought MySearchType &
  20. // SearchCom into SearchForm2, so now
  21. // it's MST for the Mutexes AND MySearchType
  22. // and SearchForm Class for what was MSC b4.
  23. // v5.3.0.21 15-June Getting Serious Now!
  24. // v5.3.1.23 25-Jun Coming Together! Rev-1;
  25. // v5.3.1.24 03-Jul FindByName2 Mod + Comm.
  26. // v5.3.1.25 10-July SearchForm Features plus.
  27. // v5.3.3.27 18-Jul-2019 4 way comms.
  28. // v5.3.3.28 21-Jul SmesaSearch.cs rockin it
  29. // -=[ Design Revision 4 ]=- My Custom Type.
  30. // v5.3.4.29 25-Jul-2019 Spelling things out
  31. // Class{in-Class{in-Class}}; Custom Types:
  32. // v5.3.5.30 30-July ReCoded4 WorkList2
  33. // v5.3.5.31 03-Aug ReCoded4 WorkList2
  34. // v5.4.5.32 07-Aug-2019 Search & Replace.
  35. // v5.4.5.33 12-Aug SF4 Split Out & Polish.
  36. // v5.4.5.34 18-Aug Sorting things out.
  37. // v5.4.6.36 29-Aug Work down testwells &
  38. // BreakPoints, Clean and Polish++
  39. // v5.4.7.40 24-Sep-2019 Settling down after
  40. // some evolution during tracing & testing.
  41. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  42.  
  43. using System;
  44. using System.Collections.Generic;
  45. using System.Collections.ObjectModel;
  46. using System.IO;
  47. using System.Linq;
  48. using System.Security;
  49. using System.Threading;
  50. using System.Windows.Forms;
  51.  
  52. // Class Aliases
  53. using MCD = MyCustomLibrary.Dialogs;
  54. using MMF = MyCustomLibrary.MemoryMapper;
  55. using MCT = MyCustomLibrary.MyToolbox;
  56.  
  57. // Property Aliases
  58. using MCR = MyCustomLibrary.Properties.Resources;
  59. using MCS = MyCustomLibrary.Properties.Settings;
  60.  
  61. #endregion Pop Tart
  62.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  63.  
  64.  
  65. namespace MyCustomLibrary
  66. {
  67.   public partial class SearchForm
  68.   {
  69. #region Lower Level / Instrumental Methods
  70.  
  71. #region By Name Method Group
  72.  
  73.     // REM: This Method is called from Loops
  74.     void FindByName1()
  75.     {
  76.       const bool  calledByMe = false;
  77.       FindByName1(calledByMe);
  78.     }
  79.     // This OverLoad is ONLY directly called
  80.     // from  FindByName2(); & the overload
  81.     // above. (True only if ME calling.)
  82.     void FindByName1(bool find2)
  83.     {
  84.       Indexes = new Collection<int>();
  85.       foreach (var file in MCD.WholeList
  86.         .Where(file => !string
  87.           .IsNullOrEmpty(searchBox.Text)
  88.         && !string.IsNullOrEmpty(
  89.           file) && File.Exists(file)))
  90.       {
  91.         if (wordCheckBox.Checked)
  92.           WordUp(file);
  93.         else
  94.           BasketCase(file);
  95.       }
  96.       if ( Indexes != null
  97.         && Indexes.Count > 0
  98.         && !find2) UpdateSelected();
  99.       else if (!find2)
  100.         MessageBox.Show(MCR.Nada);
  101.     }
  102.  
  103.     void UpdateSelected()
  104.     {
  105.       var temp = new Collection<string>();//
  106.       var hole = MCD.WholeList;
  107.       foreach (var index in Indexes
  108.         .Where(index => index >= 0
  109.           && index < hole.Count))
  110.             temp.Add(hole[index]);
  111.       var t = 0;
  112.       if (!MCT.P1Running) t = RunBlaster();
  113.       MCD.Selected = temp;
  114.       if (t < 9)
  115.       {
  116.         #region Design Note:
  117.         // Psuedo Property.Changed   |
  118.         // Event, Triggers call      |
  119.         // to: SendSelected(below);  v
  120.         #endregion Design Note:
  121.         IndexesFound = Indexes;// <-===<<
  122.         Indexes.Clear();
  123.         Validate();
  124.         return;
  125.       }
  126.       if (!MCT.Testing) return;
  127.       #region Permanent TestCode
  128.       MessageBox.Show(string.Format(
  129.         "{0}{1}{2}{3}{4}",
  130.         MCR.TimeOut1,          // 0
  131.         Path.GetFileName(      // 1
  132.           MCT.GetRoot + MCR.BlasterApp),
  133.         MCR.TimeOut2,          // 2
  134.         MCR.UpDate,            // 3
  135.         "\n\nCycles === " + t)); // 4
  136.       #endregion
  137.     }
  138.  
  139.     void SendSelected()
  140.     {
  141.       var ct = IndexesFound.Count;
  142.       if (ct < 1) return;
  143.       var sl = new Collection<string>();
  144.       for (var i = 0; i < ct; ++i)
  145.         sl.Add(MCD.WholeList[
  146.           IndexesFound[i]]);
  147.  
  148.       // uses strings now no nums past here!
  149.       var fn = Application.StartupPath +
  150.         MCR.Selected;
  151.       var args = MCD.GetArgs(fn, 0);
  152.       args[3] = sl;
  153.       if (!MMF.WriteStrings(args))
  154.         MessageBox.Show(MCR.Err10);
  155.     }
  156.  
  157.     static int RunBlaster()
  158.     {
  159.       #region testcode UpdateSelected
  160.       //const string txt =
  161.       //  "Update Selected PB NOT" +
  162.       //  " Running, HIT!";
  163.       //MessageBox.Show(txt);
  164.       #endregion
  165.       var file =
  166.         MCT.GetRoot + MCR.BlasterApp;
  167.       if (!File.Exists(file)) return 9;//\\
  168.       var args = new[]
  169.       { file, MCR.Blaster, MCR.Blaster };
  170.       // breakpoint RunBlaster()
  171.       MCT.RunApp(args);
  172.       var t = 0;
  173.       do Thread.Sleep(333);
  174.       while (!MCT.P1Running && ++t < 9);
  175.       Thread.Sleep(1212);
  176.       return t;
  177.     }
  178.  
  179.     void BasketCase(string file)
  180.     {
  181.       if (caseCheckBox.Checked)
  182.       {
  183.         if (file.Contains(searchBox.Text))//
  184.           Indexes.Add(
  185.             MCD.WholeList.IndexOf(file));
  186.       }
  187.       else // case not checked.
  188.       {
  189.         if (file.ToLower().Contains(
  190.           searchBox.Text.ToLower()))
  191.           Indexes.Add(
  192.             MCD.WholeList.IndexOf(file));
  193.       }
  194.     }
  195.    
  196.     void WordUp(string file)
  197.     {
  198.       var fn = Path.
  199.         GetFileNameWithoutExtension(file);
  200.       if (string.IsNullOrEmpty(fn)) return;
  201.       if (wordCheckBox.Checked)
  202.       {
  203.         if (searchBox.Text.Contains(fn))
  204.           Indexes.Add(
  205.             MCD.WholeList.IndexOf(file));
  206.       }
  207.       else // word not checked.
  208.       {
  209.         if (searchBox.Text.ToLower()
  210.           .Contains(fn.ToLower()))
  211.           Indexes.Add(
  212.             MCD.WholeList.IndexOf(file));
  213.       }
  214.     }
  215. #endregion By Name Method Group
  216.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  217.  
  218.  
  219. #region By Content Method Group
  220.  
  221.     string[] Lines { get; set; }
  222.  
  223.     void ContentOfCurrent(bool solo)
  224.     {
  225.       #region Note: Search4 var is reused
  226.       // it has done it's initial job of
  227.       // bringing in the search string to
  228.       // Load. Once SearchForm is loaded
  229.       // that value becomes searchBox.Text
  230.       // & we can reuse Search2 (4) as the
  231.       // filename to pass in at invokation.
  232.       #endregion
  233.       if (!solo) return;
  234.       Search2 = MCD.Current;
  235.       ContentOfCurrent();
  236.     }
  237.  
  238.     bool ReadActualFile()
  239.     {
  240.       var fn = Search2;
  241.       if (string.IsNullOrEmpty(fn)
  242.         || !File.Exists(fn)) return false;
  243.       try
  244.       {
  245.         var test = File.ReadAllLines(fn);
  246.         if (test.Length < 1) return false;
  247.         Lines = test;
  248.         return true;
  249.       }
  250.       #region Catches
  251.       catch (ArgumentException)
  252.       { return false; }
  253.       catch (PathTooLongException)
  254.       { return false; }
  255.       catch (DirectoryNotFoundException)
  256.       { return false; }
  257.       catch (IOException)
  258.       { return false; }
  259.       catch (UnauthorizedAccessException)
  260.       { return false; }
  261.       catch (NotSupportedException)
  262.       { return false; }
  263.       catch (SecurityException)
  264.       { return false; }
  265.       #endregion Catches
  266.     }
  267.  
  268.     // This is also called from Loops.
  269.     void ContentOfCurrent()
  270.     {
  271.       if ( MCD.Selected == null
  272.         || MCD.Selected.Count < 1
  273.         || !MCD.IsFile(Search2)
  274.         || !ReadActualFile()
  275.         || Lines == null
  276.         || Lines.Length < 1) return;
  277.  
  278.       var  file = Get1Record(Search2);
  279.       if (file == null
  280.         || file.Line == null
  281.         || file.Line.Count < 1
  282.         || string.IsNullOrEmpty(file.Name))
  283.         return;
  284.  
  285.       Info.File.Add(file);
  286.     }
  287.  
  288.     // -=[ Updated for Custom Type ]=-
  289.     // v5.3.5.31 02-Aug-2019 by -JpE-
  290.     Files Get1Record(string fn)
  291.     {
  292.       if (string.IsNullOrEmpty(fn)
  293.         || !File.Exists(fn)) return null;
  294.  
  295.       var  fileN = new Files(
  296.         new Collection<Lines>())
  297.       { Name = fn };
  298.  
  299.       var  lines = GetLines();
  300.       if ( lines!= null
  301.         && lines.Star != null
  302.         && lines.Star.Count > 0
  303.         ) fileN.Line.Add(lines);
  304.  
  305.       return fileN;
  306.     }
  307.  
  308.     Lines GetLines()
  309.     {
  310.       Lines line = null;
  311.       var c = Lines.Length;
  312.       for (var y = 0; y < c; ++y)
  313.       {
  314.         if (wordCheckBox.Checked)
  315.         {
  316.           // breakpoint wordCheckBox.Checked
  317.           if (string.IsNullOrEmpty(
  318.             Lines[y]) || !Lines[y].Equals
  319.             (searchBox.Text)) continue;
  320.         }
  321.         else
  322.           if (string.IsNullOrEmpty(Lines[y])
  323.             || !Lines[y].Contains(
  324.               searchBox.Text)
  325.             ) continue;
  326.  
  327.         var test = FindHits(Lines[y]);
  328.         if (test == null
  329.           ||test.Count < 1
  330.           ) continue;
  331.  
  332.         // Hits Found & Set, So is Y;
  333.         line = new Lines(test)
  334.         { LineNumber = y };
  335.       }
  336.       return line;//s;
  337.     }
  338.  
  339.     // v5.4.5.32 07-Aug-2019
  340.     Collection<int> FindHits(string line)
  341.     {
  342.       var case11 = GetCase();
  343.       var search = searchBox.Text;
  344.  
  345.       var word12 = wordCheckBox.Checked;
  346.       if (word12) search = string.Format(
  347.         " {0} ", search);
  348.  
  349.       if (word12 && !line.Contains(
  350.         search)) return null;
  351.  
  352.       var hits = new Collection<int>();
  353.       var accu = 0;
  354.  
  355.       while (line.Length >=
  356.         searchBox.Text.Length
  357.         && line.Contains(search))
  358.       {
  359.         var xCoord = line.IndexOf(
  360.           search, case11);
  361.         if (xCoord < 0) continue;
  362.  
  363.         // Accumulator: Orig Line Positions.
  364.         hits.Add(accu += xCoord);
  365.         if (search.Length >
  366.           line.Length) break;
  367.  
  368.         line = line.Remove(
  369.           0, xCoord + search.Length);
  370.       }
  371.       return hits;
  372.     }
  373.  
  374.     StringComparison GetCase()
  375.     {
  376.       return caseCheckBox.Checked
  377.         ? StringComparison
  378.         // case sensitive
  379.             .InvariantCulture
  380.         : StringComparison
  381.         // ignore case
  382.             .InvariantCultureIgnoreCase;
  383.     }
  384. #endregion By-Content Method Group
  385.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  386.  
  387.  
  388. #endregion Instrumental Low Level Members
  389.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  390.  
  391.  
  392. #region Higher Level Methods ALL use Instr
  393.  
  394. #region Higher Level: Content Method Group..
  395.  
  396.     void ContentOfSelected()
  397.     {
  398.       if (MCD.Selected == null
  399.        || MCD.Selected.Count < 1) return;
  400.       ProcessContentList(MCD.Selected);
  401.     }
  402.  
  403.     // ^v send Selected list to COC via
  404.     void ProcessContentList(
  405.       IEnumerable<string> files)
  406.     {
  407.       foreach (var fn in files.Where(
  408.         fn => !string.IsNullOrEmpty(fn)
  409.         && File.Exists(fn)))
  410.       {
  411.         Search2 = fn;
  412.         ContentOfCurrent();
  413.       }
  414.     }
  415.  
  416.     // Called by both ME & PB as stated;
  417.     // REM: MCD already arranged it, if;
  418.     // ME calling edited is MCD.Current;
  419.     // Selected  = Files Open in Editor;
  420.     // WholeList = if(ME) Favorites.sme;
  421.     void ContentOfAll()
  422.     {
  423.       //Testing(); // Keep
  424.       if (MCD.WholeList == null
  425.        || MCD.WholeList.Count < 1) return;
  426.       SearchAll = true;
  427.       ProcessContentList(MCD.WholeList);
  428.     }
  429.     /* Keep Test Code */
  430.     //static void Testing()
  431.     //{
  432.     //  
  433.     //  const string
  434.     //      b = "Both",
  435.     //      e = "Editor",
  436.     //      c = "Cancel";
  437.     //  var s = "Error";
  438.  
  439.     //  while (!s.Equals("Cancel"))
  440.     //  {
  441.     //    s = "Blaster";
  442.     //    var d = Choice();
  443.     //    switch (d)
  444.     //    {
  445.     //      case 1: s = b; break;
  446.     //      case 2: s = e; break;
  447.     //      case 4: s = c; break;
  448.     //    }
  449.     //    MessageBox.Show(
  450.     //      string.Format("{0} {1}",
  451.     //        "You Selected: ", s));
  452.     //  }
  453.     //}
  454. #endregion Higher Level: Content-Members
  455.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  456.  
  457.  
  458. #region Higher Level: By-Name Method Group.
  459.  
  460.     // Hit only by M.E. Indexes go 2 P.B. as
  461.     // before, but via FileSystemWatcher,
  462.     // and we'll also Send an ME-2nd
  463.     // instance the ones found in Selected:
  464.     // That will tab to them w/ proven code.
  465.     void FindByName2()
  466.     {
  467.       FindByName1(true);
  468.       CheckList();
  469.       if ( Indexes == null
  470.         || Indexes.Count < 1)
  471.       { MessageBox.Show(MCR.Nada);
  472.         return; }
  473.  
  474.       var opn = new Collection<string>();
  475.       foreach (var file in from file in
  476.         MCD.OpenFiles let idx= MCD.WholeList
  477.         .IndexOf(file)where Indexes.Contains(
  478.         idx) select file) opn.Add(file);
  479.  
  480.       UpdateSelected();
  481.       // ^ SendSelected call now in there.
  482.       if (opn.Count < 1) return;
  483.       SendOpen2Me(opn);
  484.     }
  485.  
  486.     // Sends filenames of Open with a
  487.     // Match to a thread of ME.Process
  488.     static void
  489.       SendOpen2Me(IList<string> opn)
  490.     {
  491.       var fil = Application
  492.         .StartupPath + MCR.EditorApp;
  493.  
  494.       if (opn == null
  495.         || !File.Exists(fil)) return;
  496.  
  497.       opn.Insert(0, fil);
  498.  
  499.       var ary =
  500.         MCT.Collection2Array(opn, 0);
  501.  
  502.       MCT.RunApp(ary);
  503.     }
  504.  
  505.     // Checks for anything Open in ME
  506.     // that's NOT in Managed, & adds it.
  507.     // Append; Don't mess up the order
  508.     // of indices as they're relied upon!
  509.     static void CheckList()
  510.     {
  511.       foreach (var file in MCD.OpenFiles
  512.         .Where(file => !MCD.WholeList
  513.           .Contains(file)))
  514.             MCD.WholeList.Add(file);
  515.     }
  516. #endregion Higher Level: By-Name Members.
  517.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  518.  
  519.  
  520. #endregion Higher Levels use Instrumentals.
  521.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  522.  
  523.  
  524.   }
  525. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement