Tools.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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(ref string arguments, Dictionary<string, string> parameters, string switchName)
  35. {
  36. if (parameters.Count > 0)
  37. {
  38. if (!string.IsNullOrEmpty(arguments))
  39. {
  40. arguments += " ";
  41. }
  42. arguments += switchName;
  43. foreach (KeyValuePair<string, string> rawSetting in parameters)
  44. {
  45. arguments += string.Format(CultureInfo.InvariantCulture, " {0}=\"{1}\"", rawSetting.Key, ArgumentEscape(rawSetting.Value));
  46. }
  47. }
  48. }
  49. }
  50. }