Advertisement
GlobalAccessSoftware

MemoryMapper2.cs Class Update.

Mar 23rd, 2019
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.93 KB | None | 0 0
  1.  
  2. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  3. // MemoryMapper2.cs 15-Oct-18/\\//\\//\\//
  4. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  5. // Split from MM v4.8.2.80 15-Oct\//\\//\\//
  6. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  7. // Copied in and altered to read/write
  8. // downward towards zero from the offset
  9. // instead of upward from offset to EoF.
  10. // v4.8.2.81 Designed & Written, Evo in
  11. // Testing & RetroSpection. TestsWell!
  12. // v4.9.3.87 14-Nov-2018 Kernals Splitout.
  13. // Favorites and Removals Modifications
  14. // ReDesigned with Separate flow to do
  15. // Removals from PrintBlaster, with MyEditor
  16. // running &&&, not-running it appends them
  17. // to removals.sme and MyEditor then checks
  18. // the file next run & handles any backlog.
  19. // v4.9.4.88 19-Nov-2018 -JpE-//\\//\\//\\//
  20. // v5.0.6.00 21-Feb-19 List<T> to Collection<T>
  21. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  22.  
  23. using System;
  24. using System.Collections.Generic;
  25. using System.Collections.ObjectModel;
  26. using System.IO;
  27. using System.IO.MemoryMappedFiles;
  28. using System.Linq;
  29. using System.Text;
  30. using System.Threading;
  31. using System.Windows.Forms;
  32.  
  33. using MCR =
  34.   MyCustomLibrary.Properties.Resources;
  35.  
  36. namespace MyCustomLibrary
  37. {
  38.   public static partial class MemoryMapper
  39.   {
  40. #region Kernals for MemoryMapper.cs methods
  41.  
  42. #region Read/Write Bytes Kernals
  43.  
  44. #region Read Bytes Kernal
  45.  
  46.     private static Collection<byte>
  47.       ReadBytesKernal(string path,
  48.         string name, long size, bool read,
  49.           long offs, long view, bool shred)
  50.     {
  51.       var j = size < 1;
  52.       var z = File.Exists(path);
  53.       if (j && z) size = GetSize(path);
  54.       if (j &&!z) size = 1;
  55.       var bytes = new Collection<byte>();
  56.       using (var m = MemoryMappedFile.CreateFromFile(
  57.         path, FileMode.OpenOrCreate, name, size))
  58.       { if (!read) return bytes;// <-= Created Only.
  59.         var position = offs;
  60.         using (var reader =
  61.           m.CreateViewAccessor(offs, view,
  62.           // REM: View itself now ^ offset. ^
  63.           MemoryMappedFileAccess.ReadWrite))
  64.         { if (view == 0) view = size;
  65.           while (position <= view
  66.             && position < size - offs)
  67.           { byte flag;
  68.             //  vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  69.             try{flag=reader.ReadByte(position++);}
  70.             catch (ArgumentException) {  break;  }
  71.             //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  72.             bytes.Add(flag); }
  73.           if (shred) { var used = position;
  74.             position = 0;
  75.             while(position <= used
  76.               &&  position <  (size - offs)
  77.               &&  position <= view)
  78.             { reader.Write(position, (ushort) 0);
  79.               position += 2; } } } }
  80.       return bytes;
  81.     }
  82.  
  83.     private static long GetSize(string path)
  84.     {
  85.       var s = new FileInfo(path);
  86.       return Convert.ToInt64(s.Length);
  87.     }
  88. #endregion Read Bytes Kernal
  89.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  90.  
  91.  
  92. #region Write Bytes Kernal
  93.  
  94.     private static bool WriteBytesKernal(
  95.       string path, string name, long size,
  96.       long offset, long view, object arg)
  97.     {
  98.       var bytes = GetBytes(arg);
  99.       if (bytes == null
  100.         || bytes.Count < 1) return false;
  101.       if(size<1)size=offset+bytes.Count;
  102.       using (var t = MemoryMappedFile
  103.         .CreateFromFile(path, FileMode
  104.           .OpenOrCreate, name, size))
  105.       { using (var writer =
  106.           t.CreateViewAccessor(offset, view,
  107.             MemoryMappedFileAccess.Write))
  108.         { foreach (var bite in bytes)
  109.           { byte  data;
  110.             try {data = Convert.ToByte(bite);}
  111.             catch (FormatException){continue;}
  112.             catch (InvalidCastException)
  113.             { continue; }
  114.             //\\  vvvvvvvvvvvvvvvvvvvvvvvvvvvv
  115.             try{writer.Write(offset++, data);}
  116.             //\\  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  117.             catch(ArgumentException)
  118.             { return false; }
  119.           } } }
  120.       return true;
  121.     }
  122.  
  123.     private static Collection<byte>
  124.       GetBytes(object arg)
  125.     {
  126.       Collection<byte> bites;
  127.       try{bites = arg as Collection<byte>; }
  128.       catch (InvalidCastException)
  129.       {try{bites= (Collection<byte>) arg; }
  130.         catch (InvalidCastException)
  131.         { bites = new Collection<byte>{1}; } }
  132.       return bites;
  133.     }
  134. #endregion Write Bytes Kernal
  135.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  136.  
  137.  
  138. #endregion Read/Write Bytes Kernals
  139.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  140.  
  141.  
  142. #region Read/Write Long Kernals
  143.  
  144. #region Read Long Kernals
  145.  
  146.     private static Collection<long>
  147.       ReadInt64Kernal(long s, string h,
  148.       string t, bool r,
  149.       long o, long v, bool b)
  150.     {
  151.       var c = new Collection<long>();
  152.       var z = s == 0;
  153.       var w = File.Exists(h);
  154.       if (z && w) s = GetSize(h);
  155.       if (z &&!w) s = 8L;
  156.       using ( var f =
  157.         MemoryMappedFile.CreateFromFile(
  158.         h, FileMode.OpenOrCreate, t, s))
  159.       { if (!r) return c;// Create File Only.
  160.         using (var m = f.
  161.           CreateViewAccessor(o, v,
  162.           MemoryMappedFileAccess.ReadWrite))
  163.         { var y = 0L  + o;// position+offset.
  164.           if (v == 0) v = s;
  165.           #region View^ = itself now Offset.
  166.           // rem view is offset, so it's all
  167.           // relative inside the 2nd using,
  168.           // starts at pos-0 & ends at view.
  169.           // (The v variable here.) period^.
  170.           #endregion
  171.           while (y <  v-7 && y < s-o-7)
  172.           { long n;
  173.             //\\//\\ vvvvvvvvvvvvvvvvv
  174.             try{ n = m.ReadInt64(y); }// <-=
  175.             //\\//\\ ^^^^^^^^^^^^^^^^^
  176.             catch(ArgumentException){break;}
  177.             y += 8L; c.Add(n); }
  178.           if (b) { var u = y; y = 0;
  179.             while (y <= u // Blank bytes used.
  180.               &&   y <  s-o && y <= v)
  181.             { m.Write(y, (ushort) 0);
  182.               y += 2; } } } }
  183.       return c;
  184.     }
  185. #endregion Read Long Kernal
  186.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  187.  
  188.  
  189. #region Write Long Kernal
  190.  
  191.     private static bool WriteLongKernal(
  192.       object arg, long s, string h,
  193.       string n, long o, long v)
  194.     {
  195.       Collection<long> l;
  196.       var j = CalcSize64(arg, out l);
  197.       if (s < 1 || s < j) s = j;
  198.       if (s < 1 || l.Count < 1) return false;
  199.       var p = 0L; // (v)iew will be offset.
  200.       using (var fi =
  201.         MemoryMappedFile.CreateFromFile(
  202.           h, FileMode.OpenOrCreate, n, s))
  203.       { using (var wi = fi.CreateViewAccessor(
  204.           o, v, MemoryMappedFileAccess.Write))
  205.         { var xx = 0;
  206.           if (v == 0) v = s-o;
  207.           while (p <  v-7 && p < s-o-7)
  208.           { if (xx > l.Count-1) break;
  209.             var i  = l[xx++];
  210.             try { wi.Write(p, i); }// <-==<<
  211.             catch(ArgumentException){break;}
  212.              p += 8L; } } }
  213.       // long is 8 ^ bytes each.
  214.       return p == j;
  215.     }
  216.  
  217.     private static long CalcSize64(
  218.       object arg, out Collection<long> l)
  219.     {
  220.       var size = 0L;
  221.       l = new Collection<long>();
  222.       try { l = arg as Collection<long>;
  223.         if (l != null){ size = l.Count; } }
  224.       catch(InvalidCastException){return 0L;}
  225.       // Basically returns a ref to l.
  226.       if (l != null) size *= 8;// 8 bytes ea.
  227.       else size = 0L;
  228.       return size;
  229.     }
  230. #endregion Write Long Kernals
  231.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  232.  
  233.  
  234. #endregion Read Write Long Kernals
  235.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  236.  
  237.  
  238. #region Read/Write Strings Kernals
  239.  
  240. #region Read Strings Kernal
  241.  
  242.     private static Collection<string>
  243.       ReadStringsKernal(string path,
  244.         long size, long offs, string name,
  245.         bool read, long view, bool  shred)
  246.     {
  247.       var pos = 0L;
  248.       var input = new Collection<string>();
  249.       var t = File.Exists(path);
  250.       var s = size == 0L;
  251.       if (s && t)  size = GetSize(path);
  252.       if (s && !t) size = offs + 2048L;
  253.       using (var mmf = MemoryMappedFile
  254.         .CreateFromFile(path, FileMode
  255.           .OpenOrCreate, name, size))
  256.       { if (!read) return null;
  257.         using (var rdr =
  258.           mmf.CreateViewAccessor(offs, view,
  259.           MemoryMappedFileAccess.ReadWrite))
  260.         { // ^ REM ^ Accessor itself is now offset.
  261.           if (view == 0) view = size - offs;
  262.           while (pos <= view && pos < size - offs)
  263.           { var  len = rdr.ReadUInt16(pos);
  264.             if ( len < 1) break; // That's it!
  265.             var buff = new byte[len];
  266.             rdr.ReadArray(pos+=2,buff,0,len);
  267.             pos += len; // reposition rdr.
  268.             var str  = Encoding
  269.               .Unicode.GetString(buff);
  270.             if (string.IsNullOrEmpty(str)) break;
  271.             input.Add(str); }
  272.           if (shred) // Shred only bytes used.
  273.           { var used = pos; pos = 0;
  274.             while (pos <= used
  275.               &&   pos <  size - offs
  276.               &&   pos <= view)
  277.             { rdr.Write(pos, (ushort) 0);
  278.               pos += 2;
  279.             }
  280.           }
  281.         }
  282.       }
  283.       return input;
  284.     }
  285. #endregion Read Strings Kernal
  286.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  287.  
  288.  
  289. #region Write Strings Kernal
  290.  
  291.     private static bool WriteStringsKernal(
  292.       string file, string name, long size, long offs,
  293.       long view, IEnumerable<string> strings)
  294.     {
  295.       var pos = 0L;
  296.       var is1 = false;
  297.       try
  298.       { using (var f = MemoryMappedFile
  299.           .CreateFromFile(file,
  300.           FileMode.OpenOrCreate, name, size))
  301.         { using (var w = f.CreateViewAccessor(
  302.             offs, view,
  303.             MemoryMappedFileAccess.Write))
  304.           { // ^v View is already offset.
  305.             if (view == 0) view = size - offs;
  306.             foreach (var str in strings)
  307.             { if (string.IsNullOrEmpty(str)
  308.                 || str.Length > view - pos
  309.                 || str.Length > size - offs - pos
  310.                 || str.Length > 1073741824)
  311.                   continue;
  312.               // 1g Max w/byte ^ arrays.
  313.               var  buf =
  314.                 Encoding.Unicode.GetBytes(str);
  315.               long len = buf.Length;
  316.               if (len < 1) break;
  317.               is1 = true;
  318.               w.Write(pos, (ushort) len);
  319.               w.WriteArray(
  320.                 pos += 2, buf, 0, (int) len);
  321.               pos += len;
  322.             }
  323.           }
  324.         }
  325.       }
  326.       catch (ArgumentOutOfRangeException)
  327.       { return false; }
  328.       return is1;
  329.     }
  330. #endregion Write Strings Kernal
  331.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  332.  
  333.  
  334. #endregion Read/Write Strings Kernals
  335.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  336.  
  337.  
  338. #endregion Kernals
  339.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  340.  
  341.  
  342. #region Reversed Clones
  343.  
  344.     private static readonly Mutex
  345.       Mux1 = new Mutex(false, "Mux1");
  346.  
  347.     private static readonly Mutex
  348.       Mux2 = new Mutex(false, "Mux2");
  349.  
  350.     private static readonly Mutex
  351.       Mux3 = new Mutex(false, "Mux3");
  352.  
  353.  
  354. #region Reverse Read/Write List<bytes>
  355.  
  356.     // Hacked to reverse order and
  357.     // read downward from offset towards 0L
  358.     // or first zero value whichever comes first.
  359.     // v4.8.2.81 20-Oct-2018
  360.     public static Collection<byte>
  361.       ReverseReadBytes(object[] args)
  362.     {
  363.       Mux1.WaitOne();
  364.       const string t = "Reverse Read Bytes";
  365.       long size, offs, lnth;
  366.       // Number of Bytes ^ to read.
  367.       if (args == null || args.Length < 6)
  368.         throw new ArgumentException(t);
  369.       bool read, dele; string path, name;
  370.       try // now unbox args.
  371.       { path =args[0].ToString();//[0]);
  372.         size = Convert.ToInt64(args[1]);
  373.         offs = Convert.ToInt64(args[2]);
  374.         lnth = Convert.ToInt64(args[3]);// <-= NOTE
  375.         read=Convert.ToBoolean(args[4]);
  376.         name=args[5].ToString(); //[5]);
  377.         dele=Convert.ToBoolean(args[6]); }
  378.       catch (InvalidCastException)
  379.       { throw new InvalidCastException(t); }
  380.       catch (FormatException)
  381.       { throw new FormatException(t); }
  382.       #region Memory Mapping
  383.       var j = size < 1;
  384.       var z = File.Exists(path);
  385.       if (j && z) size = GetSize(path);
  386.       if (j &&!z || size < 1 || offs == 0) size = 1;
  387.       var bytes = new Collection<byte>();
  388.       using (var m = MemoryMappedFile.CreateFromFile(
  389.         path, FileMode.OpenOrCreate, name, size))
  390.       { if (!read) return bytes;// <-= Created Only.
  391.         using (var reader = m.CreateViewAccessor(
  392.           0L, offs+1, MemoryMappedFileAccess.Read))
  393.         { for (var i = 0; i < lnth; ++i)
  394.           { byte flag;
  395.               try{flag=reader.ReadByte(offs--);}
  396.             catch (ArgumentException){ break; }
  397.             if (offs < 0) break;
  398.             bytes.Add(flag);
  399.           } } }
  400.       if (dele && File.Exists(path))
  401.         File.Delete(path);
  402.       else if (File.Exists(path))
  403.         File.SetLastAccessTime(path, DateTime.Now);
  404.       #endregion
  405.       Mux1.ReleaseMutex();
  406.       return bytes;
  407.     }
  408.  
  409.     public static bool
  410.       ReverseWriteBytes(object[] args)
  411.     {
  412.       Mux1.WaitOne();
  413.       if (args == null || !args.Any()) throw new
  414.         ArgumentException("Reverse Write Bytes");
  415.       string path, name; long size, offset;
  416.       try { path = args[0].ToString(); }
  417.       catch (FormatException)
  418.       { path = Convert.ToString(args[0]); }
  419.       try { size =(Convert.ToInt64(args[1]));}
  420.       catch (InvalidCastException){size= 1;}
  421.       try { offset =Convert.ToInt64(args[2]);}
  422.       catch (FormatException)
  423.       { Mux1.ReleaseMutex(); return false; }
  424.       catch(InvalidCastException)
  425.       { Mux1.ReleaseMutex(); return false; }
  426.       try { name = args[5].ToString(); }
  427.       catch(InvalidCastException){name="mux";}
  428.       catch(FormatException) { name = "mux"; }
  429.       var bytes = GetBytes(args[3]);
  430.       if (bytes == null || bytes.Count < 1)
  431.       { Mux1.ReleaseMutex(); return false; }
  432.       #region Memory Mapping
  433.       // Altered to write downward instead of upward.
  434.       if (size < 1) size = bytes.Count;
  435.       if (size < 1 || offset +1 > size || offset < 0)
  436.       { Mux1.ReleaseMutex(); return false; }
  437.       using (var t = MemoryMappedFile
  438.         .CreateFromFile(path, FileMode
  439.           .OpenOrCreate, name, size))
  440.       { using (var writer = t.CreateViewAccessor(
  441.         0L, offset+1, MemoryMappedFileAccess.Write))
  442.         { foreach (var bite in bytes){ byte  data;
  443.             try { data = Convert.ToByte(bite);}
  444.             catch (FormatException) { continue; }
  445.             catch (InvalidCastException) { continue; }
  446.               try { writer.Write(offset--, data); }
  447.             catch(ArgumentException)
  448.             { Mux1.ReleaseMutex(); return false; }
  449.             if (offset < 0) break; } } }
  450.       var time = DateTime.Now;
  451.       File.SetLastWriteTime( path, time);
  452.       File.SetLastAccessTime(path, time);
  453.       #endregion
  454.       Mux1.ReleaseMutex();
  455.       return true;
  456.     }
  457. #endregion
  458.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  459.  
  460.      
  461.     // todo: Develop the below only as needed.
  462.  
  463.  
  464. #region Reverse Read/Write List<Int64>
  465.  
  466.     public static Collection<byte>
  467.       ReverseReadInt64(object[] args)
  468.     {
  469.       Mux2.WaitOne();
  470.       // todos as needed.
  471.  
  472.  
  473.       // pre testscode
  474.       if (args == null
  475.         || args.Length <1) return null;
  476.       var str = (from string s in args where
  477.         !string.IsNullOrEmpty(s) select s)
  478.           .Aggregate("args are:\n------",
  479.             (current, s) =>
  480.               current + (s + "\n"));
  481.       MessageBox.Show(str);
  482.  
  483.  
  484.       Mux2.ReleaseMutex();
  485.       return new Collection<byte>{1};// <-= testscode
  486.     }
  487.  
  488.     public static bool
  489.       ReverseWriteInt64(object[] args)
  490.     {
  491.       Mux2.WaitOne();
  492.       // todos as needed.
  493.  
  494.  
  495.       // pre testscode
  496.       if (args == null
  497.         || args.Length <1) return false;
  498.       var str = (from string s in args where
  499.         !string.IsNullOrEmpty(s) select s)
  500.         .Aggregate("args are:\n------",
  501.         (current, s) => current + (s + "\n"));
  502.       MessageBox.Show(str);
  503.  
  504.  
  505.       Mux2.ReleaseMutex();
  506.       return true;
  507.     }
  508. #endregion
  509.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  510.  
  511.  
  512. #region Reverse Read/Write Collection<string>
  513.  
  514.     public static Collection<string>
  515.       ReverseReadStrings(object[] args)
  516.     {
  517.       Mux3.WaitOne();
  518.       // todos as needed.
  519.  
  520.  
  521.       // pre testscode
  522.       if (args == null
  523.         || args.Length <1) return null;
  524.       var str = (from string s in args where
  525.         !string.IsNullOrEmpty(s) select s)
  526.         .Aggregate("args are:\n------",
  527.         (current, s) => current + (s + "\n"));
  528.       MessageBox.Show(str);
  529.  
  530.  
  531.       Mux3.ReleaseMutex();
  532.       return new Collection<string>{"fubar"};
  533.     }
  534.  
  535.     public static bool
  536.       ReverseWriteStrings(object[] args)
  537.     {
  538.       Mux3.WaitOne();
  539.  
  540.  
  541.       // pre testscode
  542.       if (args == null
  543.         || args.Length <1) return false;
  544.       var test = args[0].ToString();
  545.       // todos as needed.
  546.       MessageBox.Show(test);
  547.  
  548.  
  549.       Mux3.ReleaseMutex();
  550.       return true;
  551.     }
  552. #endregion
  553.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  554.  
  555.  
  556. #endregion Reversed Clones
  557.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  558.  
  559.  
  560.   }
  561. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement