ShakerStringInfo.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using System.Text;
  5. namespace ShakerFile
  6. {
  7. public class ShakerStringInfo
  8. {
  9. public string ShakerName = string.Empty;
  10. public string ShakerSN = string.Empty;
  11. public byte[] TestConfig = new byte[0];
  12. public string ChannelNames = string.Empty;
  13. public void Read(IntPtr file)
  14. {
  15. int count = 0;
  16. int len = 0;
  17. WinFile.ReadFile(file, ref Unsafe.As<int, byte>(ref len), 4, out _, 0);
  18. count += 4;
  19. if (len > 0)
  20. {
  21. byte[] temp = new byte[len];
  22. WinFile.ReadFile(file, ref temp[0], (uint)len, out _, 0);
  23. ShakerName = Encoding.Default.GetString(temp);
  24. }
  25. count += len;
  26. len = 0;
  27. WinFile.ReadFile(file, ref Unsafe.As<int, byte>(ref len), 4, out _, 0);
  28. count += 4;
  29. if (len > 0)
  30. {
  31. byte[] temp = new byte[len];
  32. WinFile.ReadFile(file, ref temp[0], (uint)len, out _, 0);
  33. ShakerSN = Encoding.Default.GetString(temp);
  34. }
  35. count += len;
  36. len = 0;
  37. WinFile.ReadFile(file, ref Unsafe.As<int, byte>(ref len), 4, out _, 0); if (len > 0)
  38. {
  39. TestConfig = new byte[len];
  40. WinFile.ReadFile(file, ref TestConfig[0], (uint)len, out _, 0);
  41. }
  42. count += len;
  43. len = 0;
  44. WinFile.ReadFile(file, ref Unsafe.As<int, byte>(ref len), 4, out _, 0);
  45. if (len > 0)
  46. {
  47. byte[] temp = new byte[len];
  48. WinFile.ReadFile(file, ref temp[0], (uint)len, out _, 0);
  49. ChannelNames = Encoding.Default.GetString(temp);
  50. }
  51. count += len;
  52. }
  53. public void Write(IntPtr file, ref int count)
  54. {
  55. List<byte[]> bytes = new List<byte[]>();
  56. bytes.Add(Encoding.Default.GetBytes(ShakerName));
  57. bytes.Add(Encoding.Default.GetBytes(ShakerSN));
  58. bytes.Add(TestConfig);
  59. bytes.Add(Encoding.Default.GetBytes(ChannelNames));
  60. count = 0;
  61. for (int i = 0; i < bytes.Count; i++)
  62. {
  63. int len = bytes[i].Length;
  64. WinFile.WriteFile(file, ref Unsafe.As<int, byte>(ref len), 4, out _, 0);
  65. count += 4;
  66. if (len > 0)
  67. {
  68. WinFile.WriteFile(file, ref bytes[i][0], (uint)len, out _, 0);
  69. }
  70. count += len;
  71. }
  72. }
  73. }
  74. }