PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ZebraFlickr/CommandLine/ByteSizeDecoder.cs

https://bitbucket.org/garethl/zebraflickr
C# | 132 lines | 89 code | 18 blank | 25 comment | 9 complexity | df13ef0cd14a1ef5e837698a52af75f2 MD5 | raw file
  1. #region License
  2. // Copyright (c) 2012 Gareth Lennox (garethl@dwakn.com)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Gareth Lennox nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Collections.Generic;
  30. using ZebraFlickr.CommandLine.Exceptions;
  31. namespace ZebraFlickr.CommandLine
  32. {
  33. public class ByteSizeDecoder : IArgumentDecoder
  34. {
  35. private readonly IDictionary<string, long> _multipliers =
  36. new Dictionary<string, long>(StringComparer.InvariantCultureIgnoreCase)
  37. {
  38. {"k", 1024L},
  39. {"kb", 1024L},
  40. {"m", 1024L*1024},
  41. {"mb", 1024L*1024},
  42. {"g", 1024L*1024*1024},
  43. {"gb", 1024L*1024*1024},
  44. {"t", 1024L*1024*1024*1024},
  45. {"tb", 1024L*1024*1024*1024},
  46. };
  47. private ArgumentMap _map;
  48. #region IArgumentDecoder Members
  49. public void Decode(object item, Parser.Argument argument)
  50. {
  51. var value = argument.Value;
  52. var byteValue = Parse(value);
  53. if (_map.Property.PropertyType == typeof (int))
  54. {
  55. if (byteValue > int.MaxValue)
  56. {
  57. throw new ValidationException("Value cannot be bigger than 2GB");
  58. }
  59. else
  60. {
  61. _map.Property.SetValue(item, (int) byteValue, null);
  62. }
  63. }
  64. else
  65. {
  66. _map.Property.SetValue(item, byteValue, null);
  67. }
  68. }
  69. public void Initialize(ArgumentMap map)
  70. {
  71. _map = map;
  72. }
  73. public bool HasValue
  74. {
  75. get { return true; }
  76. }
  77. #endregion
  78. private long Parse(string value)
  79. {
  80. var digits = new List<char>();
  81. var letters = new List<char>();
  82. var isDigits = true;
  83. foreach (var c in value)
  84. {
  85. if (isDigits && char.IsDigit(c))
  86. {
  87. digits.Add(c);
  88. }
  89. else
  90. {
  91. isDigits = false;
  92. letters.Add(c);
  93. }
  94. }
  95. if (digits.Count == 0)
  96. throw new ValidationException("Missing number");
  97. var number = long.Parse(new string(digits.ToArray()));
  98. var suffix = new string(letters.ToArray());
  99. if (suffix.Length > 0)
  100. {
  101. long multiplier;
  102. if (_multipliers.TryGetValue(suffix, out multiplier))
  103. {
  104. number *= multiplier;
  105. }
  106. else
  107. {
  108. throw new ValidationException(string.Format("Unknown suffix '{0}'", suffix));
  109. }
  110. }
  111. return number;
  112. }
  113. }
  114. }