Tools.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. namespace WinSCP
  5. {
  6. internal static class Tools
  7. {
  8. public static int TimeSpanToMilliseconds(TimeSpan value)
  9. {
  10. if ((value.TotalMilliseconds > int.MaxValue) || (value.TotalMilliseconds < int.MinValue))
  11. {
  12. throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, "Cannot convert {0} to integer", value));
  13. }
  14. return (int)value.TotalMilliseconds;
  15. }
  16. public static TimeSpan MillisecondsToTimeSpan(int value)
  17. {
  18. return TimeSpan.FromMilliseconds(value);
  19. }
  20. public static string ArgumentEscape(string value)
  21. {
  22. int i = 0;
  23. while (i < value.Length)
  24. {
  25. if (value[i] == '"')
  26. {
  27. value = value.Insert(i, "\"");
  28. ++i;
  29. }
  30. ++i;
  31. }
  32. return value;
  33. }
  34. public static void AddRawParameters(
  35. ref string arguments, Dictionary<string, string> parameters, string switchName, bool count)
  36. {
  37. if (parameters.Count > 0)
  38. {
  39. if (!string.IsNullOrEmpty(arguments))
  40. {
  41. arguments += " ";
  42. }
  43. arguments += switchName;
  44. if (count)
  45. {
  46. arguments += string.Format(CultureInfo.InvariantCulture, "[{0}]", parameters.Count);
  47. }
  48. foreach (KeyValuePair<string, string> rawSetting in parameters)
  49. {
  50. arguments += string.Format(CultureInfo.InvariantCulture, " {0}=\"{1}\"", rawSetting.Key, ArgumentEscape(rawSetting.Value));
  51. }
  52. }
  53. }
  54. public static int LengthTo32Bit(long length)
  55. {
  56. if (length < int.MinValue || length > int.MaxValue)
  57. {
  58. throw new OverflowException(string.Format(CultureInfo.CurrentCulture, "Size {0} cannot be represented using 32-bit value", length));
  59. }
  60. return (int)length;
  61. }
  62. }
  63. }