Util.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace TDMS.Common
  6. {
  7. internal static class Util
  8. {
  9. public static TDMSDataType GetTDMSDataType<T>()
  10. {
  11. var dataType = Common.TDMSDataType.String;
  12. var inputType = typeof(T);
  13. if (inputType == typeof(byte))
  14. {
  15. dataType = Common.TDMSDataType.UInt8;
  16. }
  17. else if (inputType == typeof(short))
  18. {
  19. dataType = Common.TDMSDataType.Int16;
  20. }
  21. else if (inputType == typeof(int))
  22. {
  23. dataType = Common.TDMSDataType.Int32;
  24. }
  25. else if (inputType == typeof(float))
  26. {
  27. dataType = Common.TDMSDataType.Float;
  28. }
  29. else if (inputType == typeof(double))
  30. {
  31. dataType = Common.TDMSDataType.Double;
  32. }
  33. else if (inputType == typeof(DateTime))
  34. {
  35. dataType = Common.TDMSDataType.Timestamp;
  36. }
  37. else
  38. {
  39. throw new TDMSErrorException("Unsupported data type.");
  40. }
  41. return dataType;
  42. }
  43. public static bool IsValidPath(string path)
  44. {
  45. // 检查是否为空或只包含空格
  46. if (string.IsNullOrWhiteSpace(path))
  47. return false;
  48. try
  49. {
  50. // 获取路径中的非法字符数组
  51. char[] invalidChars = Path.GetInvalidPathChars();
  52. // 检查路径中是否包含非法字符
  53. foreach (char c in invalidChars)
  54. {
  55. if (path.IndexOf(c) >= 0)
  56. return false;
  57. }
  58. // 尝试获取路径的标准化形式
  59. string normalizedPath = Path.GetFullPath(path);
  60. // 检查路径长度是否超过最大限制
  61. if (normalizedPath.Length > 248 || Path.GetFileName(normalizedPath).Length > 260)
  62. return false;
  63. // 如果以上条件都满足,则认为路径有效
  64. return true;
  65. }
  66. catch
  67. {
  68. // 如果在处理过程中抛出异常,如路径太长等,也视为无效路径
  69. return false;
  70. }
  71. }
  72. public static TDMSDataType ToDataType(this Type type)
  73. {
  74. if(type == typeof(string))
  75. {
  76. return Common.TDMSDataType.String;
  77. }
  78. else if(type == typeof(byte))
  79. {
  80. return Common.TDMSDataType.UInt8;
  81. }
  82. else if(type == typeof(short))
  83. {
  84. return Common.TDMSDataType.Int16;
  85. }
  86. else if(type == typeof(int))
  87. {
  88. return Common.TDMSDataType.Int32;
  89. }
  90. else if(type == typeof(float))
  91. {
  92. return Common.TDMSDataType.Float;
  93. }
  94. else if(type == typeof(double))
  95. {
  96. return Common.TDMSDataType.Double;
  97. }
  98. else if(type == typeof(DateTime))
  99. {
  100. return Common.TDMSDataType.Timestamp;
  101. }
  102. else
  103. {
  104. throw new TDMSErrorException("Unsupported data type.");
  105. }
  106. }
  107. public static DatetimeFactor Factoring(this DateTime[] values)
  108. {
  109. var df = new DatetimeFactor(values.Length);
  110. for (int i = 0; i < values.Length; i++)
  111. {
  112. var t = (DateTime)(object)values[i];
  113. df.Years[i] = (uint)t.Year;
  114. df.Months[i] = (uint)t.Month;
  115. df.Days[i] = (uint)t.Day;
  116. df.Hours[i] = (uint)t.Hour;
  117. df.Minutes[i] = (uint)t.Minute;
  118. df.Seconds[i] = (uint)t.Second;
  119. df.Milliseconds[i] = (double)t.Millisecond;
  120. }
  121. return df;
  122. }
  123. }
  124. /// <summary>
  125. /// 用于存储DateTime数组的各个时间分量
  126. /// </summary>
  127. class DatetimeFactor(int count)
  128. {
  129. public uint[] Years { get; } = new uint[count];
  130. public uint[] Months { get; } = new uint[count];
  131. public uint[] Days { get; } = new uint[count];
  132. public uint[] Hours { get; } = new uint[count];
  133. public uint[] Minutes { get; } = new uint[count];
  134. public uint[] Seconds { get; } = new uint[count];
  135. public double[] Milliseconds { get; } = new double[count];
  136. public uint[] WeekDays { get; } = new uint[count];
  137. public DateTime[] ToDateTimeArray()
  138. {
  139. var result = new DateTime[Years.Length];
  140. for (int i = 0; i < Years.Length; i++)
  141. {
  142. result[i] = new DateTime((int)Years[i], (int)Months[i], (int)Days[i], (int)Hours[i], (int)Minutes[i], (int)Seconds[i], (int)Milliseconds[i]);
  143. }
  144. return result;
  145. }
  146. }
  147. }