Advertisement
GlobalAccessSoftware

Search and Replace Module Alpha Part-1

Oct 3rd, 2019
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.19 KB | None | 0 0
  1.  
  2. #region Top Part
  3. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  4. // Smesa > SearchReplace.cs from EditMenu
  5. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  6. // v5.2.8.19 31-May Search Actions Design.
  7. // v5.2.9.19 June 2019, Ready Mods ++ Search
  8. // v5.2.9.20 07-June Integration w/MyEditor!
  9. // v5.3.0.21 10-June Brought MySearchType &
  10. // SearchCom into SearchForm Class.
  11. // v5.3.0.21 15-June Getting Serious Now!
  12. // v5.3.0.22 22-Jun-2019 Almost Beyond PB
  13. // Search groups and back to Smesa side but
  14. // it's difficult to Debug w/ all the IPC
  15. // and auxiliary Apps interacting beyond
  16. // my Debugger's Focus.
  17. // v5.3.1.23 25-Jun Coming Together! Rev-1;
  18. // v5.3.1.24 02-Jul-2019 Search Integration.
  19. // v5.3.1.25 10-July SearchForm Features plus.
  20. // MajDesRev3:
  21. // v5.3.3.27 15-July-2019 (start MDR3)
  22. // v5.3.3.27 20-July-2019 4 way comms.
  23. // v5.3.3.28 21-July SmesaSearch.cs rockin'
  24. // -=[ Design Revision 4 ]=- My Custom Type.
  25. // v5.3.4.29 25-Jul-2019 Spelling things out
  26. // Class{in-Class{in-Class}}; Custom Types:
  27. // v5.3.5.30 30-July ReCoded4 WorkList2
  28. // v5.3.5.31 03-Aug ReCoded4 WorkList2
  29. // v5.4.5.32 07-Aug-2019 Search & Replace.
  30. // v5.4.5.34 18-Aug Cleanup & Move into SS2
  31. // v5.4.6.36 29-Aug Work down testwells &
  32. // BreakPoints, Clean and Polish++ 7-Sept.
  33. // v5.4.6.37 08-Sep-2019 Special Methods.
  34. // v5.4.6.38 19-Sep-2019 Tiny Details
  35. // v5.4.7.39 25-Sep ButtonClick Relays Begin
  36. // v5.4.7.40 01-Oct-2019 Heavy Hacking S&R.
  37. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  38.  
  39. using System;
  40. using System.Collections.ObjectModel;
  41. using System.IO;
  42. using System.Threading;
  43. using System.Windows.Forms;
  44.  
  45. // Class Aliases
  46. using MyCustomLibrary;
  47. using MCD = MyCustomLibrary.Dialogs;
  48. using MMF = MyCustomLibrary.MemoryMapper;
  49. using MCT = MyCustomLibrary.MyToolbox;
  50. using MSF = MyCustomLibrary.SearchForm;
  51.  
  52. // Property Aliases
  53. using SMR = MyEditor.Properties.Resources;
  54. using SMS = MyEditor.Properties.Settings;
  55.  
  56. #endregion Pop Tart
  57.  
  58.  
  59. namespace MyEditor
  60. {
  61.   public partial class Smesa
  62.   {
  63. #region File Loading Messages
  64.  
  65.     void Special()
  66.     {
  67.       const bool reset = false;
  68.       Special(reset);
  69.     }
  70.  
  71.     void Special(bool reset)
  72.     {
  73.       if (reset)
  74.       {
  75.         Text = SMR.AddedText;
  76.         ArrowCursor();
  77.       }
  78.       else
  79.       {
  80.         WaitCursor();
  81.         Special(SMS.Default
  82.           .FileNames.Count);
  83.       }
  84.     }
  85.  
  86.     void Special(int ct)
  87.     {
  88.       Text += SMR.WAIT;
  89.       if (ct > 12) Text =
  90.         string.Format(
  91.           "{0} {1} Files.",
  92.           SMR.Loading, ct);
  93.       Show();
  94.       WaitCursor();
  95.     }
  96. #endregion File Loading Messages
  97.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  98.  
  99.    
  100. #region MyEditor Calling Search
  101.  
  102.     void SearchClickedEventHandler(
  103.         object sender, EventArgs e)
  104.     {
  105.       var was  = MCT.Skip;
  106.       MCT.Skip = true;
  107.       foreach (var i in CheckForSave())
  108.         SaveCurrentFile(i);
  109.       MCT.Skip = was;
  110.  
  111.       var tab = TabControl.SelectedIndex;
  112.       var cof = new Collection<string>
  113.       { SMS.Default.FileNames[tab] };
  114.       foreach (var fnm in SMS.Default
  115.         .FileNames) cof.Add(fnm);
  116.       var rtb = TabControl.TabPages[tab]
  117.         .Controls[0] as RichTextBox;
  118.       if (rtb == null
  119.         ||cof.Count < 1) return;
  120.       var cst = new Collection<string>
  121.       { SMS.Default.FileNames[tab] };
  122.       MCD.Search(GetSearchString1(
  123.         rtb.SelectedText), cof, cst);
  124.     }
  125.  
  126.     // Gets Either default or trims text.
  127.     // Truncates the string at 256 chars.
  128.     string GetSearchString1(string text)
  129.     {
  130.       if (!string.IsNullOrEmpty(text))
  131.       {
  132.         var txt = text;
  133.         var spacer = new[] {' '};
  134.         txt = txt.TrimEnd(spacer);
  135.         if (!string.IsNullOrEmpty(txt))
  136.           text = GetSearchString2(txt);
  137.       }
  138.       return text;
  139.     }
  140.  
  141.     string GetSearchString2(string txt)
  142.     {
  143.       var texter = txt;
  144.       var tester = SMR.SearchText;
  145.       var charer = tester.IndexOf(
  146.         "text", StringComparison
  147.           .OrdinalIgnoreCase);
  148.  
  149.       if (charer >= 0 &&
  150.           charer < tester.Length
  151.           - txt.Length
  152.         ) texter = GetSearchString3(
  153.             txt, tester, charer);
  154.  
  155.       return texter;
  156.     }
  157.  
  158.     string GetSearchString3(string text,
  159.       string tester, int charer)
  160.     {
  161.       if (!string.IsNullOrEmpty(text))
  162.       {
  163.         var txt = text;
  164.         tester = tester.Insert(
  165.           charer + "text".Length,
  166.             string.Format(// ^ fixedwell!
  167.               ": \"{0}\"\r\n", txt));
  168.         if (!string.IsNullOrEmpty(txt)
  169.           && YesNoDialog(tester,
  170.             SMR.SearchTitle, 1)
  171.           ) text = GetSearchString4(txt);
  172.       }
  173.       return text;
  174.     }
  175.  
  176.     static string
  177.       GetSearchString4(string text)
  178.     {
  179.       var spacer = new[] {' '};
  180.       var txt = text.TrimEnd(spacer);
  181.       while ( txt.Length > 256)
  182.         txt = txt.Remove(256);
  183.       if (!string.IsNullOrEmpty(
  184.         txt)) text = txt;
  185.       // if & ONLY if!^^^
  186.       return text;
  187.     }
  188. #endregion MyEditor Calling Search
  189.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  190.  
  191.  
  192. #region Incoming Messages from SearchForm
  193.      
  194. #region WorkList2 Custom DataType desRev4
  195.  
  196.     FileSystemWatcher MatchesWatcher
  197.     { get; set; }
  198.  
  199.     WorkList2 WorkList
  200.     { get; set; }
  201.  
  202.  
  203.     void SetupMatchesWatcher()
  204.     {
  205.       MatchesWatcher.NotifyFilter =
  206.         NotifyFilters.FileName;
  207.       MatchesWatcher
  208.         .InternalBufferSize  = 32768;
  209.       MatchesWatcher
  210.         .EnableRaisingEvents = false;
  211.       MatchesWatcher.Path    =
  212.         Application.StartupPath;
  213.       MatchesWatcher.Filter  =
  214.         Path.GetFileName(SMR.Matches);
  215.       MatchesWatcher.Created +=
  216.         MatchesEventHandler;
  217.       MatchesWatcher
  218.         .EnableRaisingEvents = true;
  219.     }
  220.  
  221.     // RedNum = 1; >>====>
  222.     // // Collection of Lines.
  223.     // return IList<Lines> =
  224.     //   GetLines(rtb.Text);
  225.     //
  226.     // consider: Just do one file at a time on the fly for fresh Lines and Starts, Each FindButtonClicked (Which is called all button).
  227.  
  228.     void MatchesEventHandler(
  229.       object sender,
  230.         FileSystemEventArgs e)
  231.     {
  232.       Thread.CurrentThread.Name =
  233.         "MatchesReceived";
  234.       if (RichTextBox1.InvokeRequired)
  235.         try { Invoke(new Invoker1(
  236.           SetupNewWorkList)); }
  237.         catch (ObjectDisposedException)
  238.         {} // breakpoint MatchesEventHandler
  239.       else
  240.         SetupNewWorkList();
  241.     }
  242.  
  243.     // REM: Back on U/I Thread Here.
  244.     void SetupNewWorkList()
  245.     {
  246.       using (new Mutex(true, "SmesaMutex"))
  247.       {
  248.         Thread.CurrentThread.Name =
  249.           "SetupWorkList";
  250.         var  m = ReadMatches();
  251.         if ( m == null
  252.           || m.File == null
  253.           || m.File.Count < 1)
  254.         {
  255.           WorkList = null;
  256.           Tracker  = null;
  257.           if (!MCT.Skip)
  258.             MessageBox.Show(SMR.Work);
  259.           return;
  260.         }
  261.         WorkList = m;
  262.         FindFirst();
  263.       }
  264.     }
  265.  
  266.     // This  returns  m ^ above;
  267.     // Reads the Matches.sme MMF and puts
  268.     // the converted data back together as
  269.     // new WorkList2.File[] Collection.
  270.     // v5.3.5.31 03-July-2019
  271.     static WorkList2 ReadMatches()
  272.     {
  273.       var f = MCT.GetRoot + SMR.Matches;
  274.  
  275.       if (string.IsNullOrEmpty(f)
  276.         || !File.Exists(f)) return null;
  277.  
  278.       var t = MMF.ReadStrings(
  279.         MCD.GetArgs(f,0));
  280.  
  281.       var bools = t[0].Split(',');
  282.       if (bools.Length < 2) return null;
  283.  
  284.       var txt = t[1].Split(',');
  285.       if (txt.Length < 2) return null;
  286.  
  287.       return txt.Length < 2 ? null :
  288.         GetFileData(bools, txt, t);
  289.     }
  290. #endregion WorkList2 Custom DataType usages
  291.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  292.  
  293.  
  294. #region Remote Control Members
  295.  
  296.     FileSystemWatcher
  297.       RemoteControl  { get; set; }
  298.  
  299.     void SetupRemoteControl()
  300.     {
  301.       RemoteControl.NotifyFilter =
  302.         NotifyFilters.FileName;
  303.       RemoteControl
  304.         .InternalBufferSize = 65536;
  305.       RemoteControl
  306.         .EnableRaisingEvents = false;
  307.       RemoteControl.Path =
  308.         Application.StartupPath;
  309.       RemoteControl.Filter =
  310.         Path.GetFileName(SMR.Remote);
  311.       RemoteControl.Created +=
  312.         RemoteEventHandler;
  313.       RemoteControl
  314.         .EnableRaisingEvents = true;
  315.     }
  316.  
  317.     // Buttons Clicked in SearchForm3 obj,
  318.     // after original entry point above.
  319.     // Meaning, on subsequent Event calls.
  320.     void RemoteEventHandler(
  321.       object sender,
  322.       FileSystemEventArgs e)
  323.     {
  324.       Thread.CurrentThread.Name =
  325.         "Remote Handler";
  326.       if (!TabControl.InvokeRequired)
  327.         RemoteEventHandler();
  328.  
  329. #region Tiny Bubbles Coming Out of Invoked Circuitry!
  330. //An unhandled exception of type 'System
  331. //.InvalidOperationException' occurred in
  332. // System.Windows.Forms.dll
  333.  
  334. //Additional information: This property has
  335. // already been set and cannot be modified.
  336. #endregion
  337.       else
  338. //        try { // fixwell REH
  339.           Invoke(new Invoker1(
  340.             RemoteEventHandler));
  341. //        }
  342. #region Catch Clause
  343. //        catch (InvalidOperationException)
  344. //        {
  345. //          const string txt =
  346. //            "Invalid Operation Exception in\r\n" +
  347. //            "Remote Event Handler. Return from\r\n" +
  348. //            "Invokation did not Exit Properly\r\n" +
  349. //            "or That Object was pre-disposed.";
  350. //          if (MCT.Testing)
  351. //            MessageBox.Show(txt);
  352. //        }
  353. #endregion
  354.     }
  355.  
  356.     // Flow is back on the UI/Thread Now.
  357.     void RemoteEventHandler()
  358.     {
  359.       Thread.CurrentThread.Name =
  360.         "Remote Event Handler";
  361.  
  362.       Enabled = true;
  363.  
  364.       // testwell Handle msgs from SF3!
  365.       // Byte Messages from SearchForm
  366.       var button = ReadRemoteByte();
  367.       if (button < 1 || button > 4) return;
  368.  
  369.       if (button.Equals(1))
  370.         FindNextClicked();
  371.  
  372.       if (button.Equals(2))
  373.         ReplaceClicked();
  374.  
  375.       if (button.Equals(3))
  376.         ReplaceAllClicked();
  377.  
  378.       if (button > 3)
  379.         CancelSearchClicked();
  380.       else SearchFormHandoff(1);
  381.       // signals ^ SF2.LetsGo(); (CTS*)
  382.       // *Clear To Send signal*
  383.     }
  384.  
  385.     // Read byte from RemoteControl
  386.     // MMF & return it. v5.3.3.28
  387.     static byte ReadRemoteByte()
  388.     {
  389.       byte byteNum = 0;
  390.  
  391.       var fn = Application
  392.         .StartupPath + SMR.Remote;
  393.       var from = MMF.ReadBytes(
  394.         MCD.GetArgs(fn, 0));
  395.  
  396.       if (from != null
  397.         && from.Count > 0)
  398.           byteNum = from[0];
  399.  
  400.       return byteNum;
  401.     }
  402. #endregion Remote Control Members
  403.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  404.  
  405.  
  406. #endregion Incoming Messages from Search3
  407.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  408.  
  409.  
  410. #region Outgoing Messages back-to SearchForm
  411.  
  412.     // Alerts SF that ME is done with that 1
  413.     // Made ReUsable v5.3.1.25 4 use w/Done.
  414.     // byte 0 = empty,
  415.     // byte 1 = "Go!";
  416.     // byte 2 = "Done!"; // <-=
  417.     // byte 3 = "Cancel";
  418.     // Don't confuse with Search to...
  419.     // (Matches.sme or Remote.sme)
  420.     // Editor; this is Editor to Search comm
  421.     // (SearchTask.sme)
  422.     void SearchFormHandoff(byte task)
  423.     {
  424.       // testwell SearchFormHandoff
  425.       //Enabled = true; // non-static holder.
  426.  
  427.       Enabled = task > 1;
  428.       // if 1, disable ME ^, else enable.
  429.      
  430.       // develop blocking mechanism or System Wide Mutex maybe?
  431.  
  432.       var fn = Application
  433.         .StartupPath + SMR.Search;
  434.  
  435.       if (!MMF.WriteBytes(
  436.         MCD.GetArgs(fn, task)))
  437.           MessageBox.Show(SMR.Err5);
  438.     }
  439. #endregion Outgoing Messages to SearchForm
  440.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  441.  
  442.  
  443. #region Button Click Relays from SearchForm:
  444.  
  445.     P3D GetTrackMax()
  446.     {
  447.       if (WorkList == null) return null;
  448.  
  449.       if (WorkList.File == null
  450.        || WorkList.File.Count < 1
  451.         ) return null;
  452.  
  453.       var f = WorkList.File.Count -1;
  454.       if (WorkList.File[f].Line == null
  455.        || WorkList.File[f].Line.Count < 1
  456.         ) return null;
  457.  
  458.       var l = WorkList.File[f]
  459.         .Line.Count -1;
  460.       if (WorkList.File[f]
  461.         .Line[l].Star == null
  462.        || WorkList.File[f]
  463.         .Line[l].Star.Count < 1
  464.         ) return null;
  465.  
  466.       var c = WorkList.File[f]
  467.         .Line[l].Star.Count -1;
  468.  
  469.       return new P3D
  470.       { Star = c, Line = l, File = f };
  471.     }
  472.  
  473.     /*^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v*/
  474.     // Custom Data Type P3D Doc:
  475.     //^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v
  476.     // C is WhereFound The Position in Line[Y]
  477.     // L is Line Number in current FileName[Z]
  478.     // F is File Number in current WorkList[N]
  479.     /*^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v*/
  480.     P3D Tracker { get; set; }
  481.     P3D TrackTo { get; set; }
  482.  
  483.  
  484.     // Uses WorkList2 Find & highlight'
  485.     // the first pair + Length
  486.     // of searchBox.Text, etc. Called
  487.     // right after MyWorkList is assembled.
  488.     // This Method & most subs testswell now!
  489.     void FindFirst()
  490.     {
  491.       TrackTo = GetTrackMax(); // ABOVE ^^
  492.       Tracker = new P3D // full reset.
  493.       { File  = 0, Line = 0, Star = 0 };
  494.  
  495.       // No FindNext, ^ it's new data!
  496.       SetSelectedText();
  497.     }
  498.  
  499.     // Comes in Set to this.Next Zero ^v
  500.     // Based Record indice. Finds Next
  501.     // after this & sets to it when
  502.     // finished with Current. v5.4.5.33.3
  503.     void FindNextClicked()//byte 1;
  504.     {
  505.       // breakpoint FindNextClicked\dev
  506.       if (Tracker.Equals(TrackTo))
  507.       {
  508.         // Completed Successfully!
  509.         CancelSearchClicked();
  510.         MessageBox.Show(SMR.Done);
  511.         SearchFormHandoff(2);
  512.         //signals SF2.All ^ Done();
  513.         return;
  514.       }
  515.       FindNextMatch();
  516.      
  517.       // testcode FindNextClicked
  518.       const string tx =
  519.         "Find Next Clicked Hit! = ";
  520.       MessageBox.Show(tx);
  521.     }
  522.  
  523.     // Incoming ^v Messages from SearchForm.
  524.     void ReplaceClicked()//byte 2;
  525.     {
  526.       // testwell ReplaceClicked
  527.       ReplaceSelection();
  528.       FindNextClicked();
  529.  
  530.       // testcode ReplaceClicked
  531.       const string t =
  532.         "Replace Clicked Hit!";
  533.       MessageBox.Show(t);
  534.     }
  535.  
  536.     // Uses the rtb.Text.Replace();
  537.     // instead of rtb.Lines and such.
  538.     // Much easier to write too & FAST!
  539.     // v5.4.7.40 01-Oct-2019 by -JpE-
  540.     void ReplaceAllClicked()//byte 3;
  541.     {
  542.       // testwell Replace All
  543.       foreach (var f in WorkList.File)
  544.       {
  545.         var num = GetTabNum(f.Name);
  546.         if (num < 0 || !SMS.Default
  547.           .FileNames.Contains(f.Name)
  548.           ) continue;
  549.  
  550.         var rtb = GetRtB(num);
  551.         TabControl.SelectedIndex = num;
  552.  
  553.         rtb.Text = rtb.Text.Replace(
  554.           WorkList.FindThis,
  555.           WorkList.Change2);
  556.       }
  557.     }
  558.  
  559.     void CancelSearchClicked() // byte 4;
  560.     {
  561.       // breakpoint CancelSearchClicked();
  562.       #region release mode testcode
  563.       //const string t = // (SF just closed)
  564.       //  "Cancel Search Event " +
  565.       //  "from Search Form,\n" +
  566.       //  " Handled in My Editor!\n\n";
  567.       //MessageBox.Show(t);
  568.       #endregion
  569.       WorkList = null;
  570.       Tracker  = null; // null or
  571.  
  572.  
  573.       SearchFormHandoff(3);
  574.     }
  575. #endregion Button Click Relays from Search
  576.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  577.  
  578.  
  579. #region Island Vista Parking Only.
  580.  
  581. #endregion Island Vista Parking.
  582.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  583.  
  584.  
  585.   }
  586. }
  587.  
  588. // 100% By -JpE-
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement