2
// FileSizeQueryValue.cs
5
// Gabriel Burt <gburt@novell.com>
6
// Aaron Bockover <abockover@novell.com>
8
// Copyright (C) 2007-2008 Novell, Inc.
10
// Permission is hereby granted, free of charge, to any person obtaining
11
// a copy of this software and associated documentation files (the
12
// "Software"), to deal in the Software without restriction, including
13
// without limitation the rights to use, copy, modify, merge, publish,
14
// distribute, sublicense, and/or sell copies of the Software, and to
15
// permit persons to whom the Software is furnished to do so, subject to
16
// the following conditions:
18
// The above copyright notice and this permission notice shall be
19
// included in all copies or substantial portions of the Software.
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38
public enum FileSizeFactor : long {
47
public class FileSizeQueryValue : IntegerQueryValue
49
private FileSizeFactor factor = FileSizeFactor.None;
50
public FileSizeFactor Factor {
51
get { return factor; }
54
public FileSizeQueryValue ()
58
public FileSizeQueryValue (long bytes)
65
public double FactoredValue {
66
get { return (double)value / (double)factor; }
69
public override void ParseUserQuery (string input)
71
if (input.Length > 1 && (input[input.Length - 1] == 'b' || input[input.Length - 1] == 'B')) {
72
input = input.Substring (0, input.Length - 1);
76
IsEmpty = !Double.TryParse (input, out double_value);
78
if (IsEmpty && input.Length > 1) {
79
IsEmpty = !Double.TryParse (input.Substring (0, input.Length - 1), out double_value);
83
switch (input[input.Length - 1]) {
84
case 'k': case 'K': factor = FileSizeFactor.KB; break;
85
case 'm': case 'M': factor = FileSizeFactor.MB; break;
86
case 'g': case 'G': factor = FileSizeFactor.GB; break;
87
case 't': case 'T': factor = FileSizeFactor.TB; break;
88
case 'p': case 'P': factor = FileSizeFactor.PB; break;
89
default : factor = FileSizeFactor.None; break;
91
value = (long)((double)factor * double_value);
95
public override void ParseXml (XmlElement node)
97
base.ParseUserQuery (node.InnerText);
98
if (node.HasAttribute ("factor")) {
99
this.factor = (FileSizeFactor) Enum.Parse (typeof(FileSizeFactor), node.GetAttribute ("factor"));
105
public override void AppendXml (XmlElement node)
107
base.AppendXml (node);
108
node.SetAttribute ("factor", factor.ToString ());
111
public void SetValue (double value, FileSizeFactor factor)
113
this.value = (long)(value * (double)factor);
114
this.factor = factor;
118
protected void DetermineFactor ()
120
if (!IsEmpty && value != 0) {
121
foreach (FileSizeFactor factor in Enum.GetValues (typeof(FileSizeFactor))) {
122
if (value >= (double)factor) {
123
this.factor = factor;
129
public override string ToUserQuery ()
131
return ToUserQuery (false);
134
public string ToUserQuery (bool always_decimal)
136
if (factor != FileSizeFactor.None) {
137
return String.Format ("{0} {1}",
140
: StringUtil.DoubleToTenthsPrecision (((double)IntValue / (double)factor), always_decimal),
144
return base.ToUserQuery ();