1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
using System;
namespace ViperBytes.IO
{
/// <summary>
/// Handles a capacity in bytes
/// </summary>
class Capacity
{
#region statics
/// <summary>
/// The units of byte
/// </summary>
public static string[] Units = new string[] { "Byte", "KiloByte", "MegaByte", "GigaByte", "TeraByte", "PetaByte", "ExaByte", "ZettaByte", "YottaByte" };
/// <summary>
/// Short forms of the units
/// </summary>
public static string[] ShortUnits = new string[] { "Byte", "KByte", "MByte", "GByte", "TByte", "PByte", "EByte", "ZByte", "YByte" };
/// <summary>
/// Shortest forms of units
/// </summary>
public static string[] VeryShortUnits = new string[] { "Byte", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
#endregion
#region fields
/// <summary>
/// Contains the number of bytes
/// </summary>
private decimal bytes;
#endregion
#region properties
/// <summary>
/// Contains the bytes, read only
/// </summary>
public decimal Bytes
{
get
{
return this.bytes;
}
}
#endregion
#region constructor
/// <summary>
/// Constructor
/// </summary>
public Capacity()
{
this.bytes = 0;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="bytes">Number of bytes</param>
public Capacity(decimal bytes)
{
this.bytes = bytes;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="size">Size string in form of a number, followed of a space and the unit</param>
public Capacity(string size)
{
decimal number;
int unit;
// the first is, to get the separator, that is a space char
if (size.Contains(" "))
{
int i = size.LastIndexOf(" ");
try
{
number = Decimal.Parse(size.Substring(0, i));
unit = GetStringUnit(size.Substring(i + 1));
}
catch
{
number = 0;
unit = 0;
}
}
else
{
// no separator found - no unit!
try
{
number = Decimal.Parse(size);
unit = 0;
}
catch
{
number = 0;
unit = 0;
}
}
// set bytes
try
{
this.bytes = Math.Floor(number * (decimal)Math.Pow(1024, unit));
}
catch
{
this.bytes = 0;
}
}
#endregion
#region public members
/// <summary>
/// Returns size in string without unit
/// </summary>
/// <returns>Size in string</returns>
public override string ToString()
{
return ToString(GetOptimalUnit());
}
/// <summary>
/// Returns size in string without unit
/// </summary>
/// <param name="unit">The unit string</param>
/// <returns>Size in string</returns>
public string ToString(string unit)
{
return ToString(GetStringUnit(unit));
}
/// <summary>
/// Returns size in string without unit
/// </summary>
/// <param name="unit">The unit integer from 0 to 9</param>
/// <returns>Size in string</returns>
public string ToString(int unit)
{
return (Bytes / (decimal)Math.Pow(1024, unit)).ToString("n");
}
/// <summary>
/// Returns size in string with unit
/// </summary>
/// <returns>Size in string</returns>
public string ToUnitString()
{
return ToUnitString(GetOptimalUnit());
}
/// <summary>
/// Returns size in string with unit
/// </summary>
/// <param name="unit">The unit string</param>
/// <returns>Size in string</returns>
public string ToUnitString(string unit)
{
return ToUnitString(GetStringUnit(unit));
}
/// <summary>
/// Returns size in string with unit
/// </summary>
/// <param name="unit">The unit integer from 0 to 9</param>
/// <returns>Size in string</returns>
public string ToUnitString(int unit)
{
return (Bytes / (decimal)Math.Pow(1024, unit)).ToString("n") + " " + Units[unit];
}
/// <summary>
/// Returns size in string with short unit
/// </summary>
/// <returns>Size in string</returns>
public string ToShortUnitString()
{
return ToShortUnitString(GetOptimalUnit());
}
/// <summary>
/// Returns size in string with short unit
/// </summary>
/// <param name="unit">The unit string</param>
/// <returns>Size in string</returns>
public string ToShortUnitString(string unit)
{
return ToShortUnitString(GetStringUnit(unit));
}
/// <summary>
/// Returns size in string with short unit
/// </summary>
/// <param name="unit">The unit integer from 0 to 9</param>
/// <returns>Size in string</returns>
public string ToShortUnitString(int unit)
{
return (Bytes / (decimal)Math.Pow(1024, unit)).ToString("n") + " " + ShortUnits[unit];
}
/// <summary>
/// Returns size in string with very short unit
/// </summary>
/// <returns>Size in string</returns>
public string ToVeryShortUnitString()
{
return ToVeryShortUnitString(GetOptimalUnit());
}
/// <summary>
/// Returns size in string with very short unit
/// </summary>
/// <param name="unit">The unit string</param>
/// <returns>Size in string</returns>
public string ToVeryShortUnitString(string unit)
{
return ToVeryShortUnitString(GetStringUnit(unit));
}
/// <summary>
/// Returns size in string with very short unit
/// </summary>
/// <param name="unit">The unit integer from 0 to 9</param>
/// <returns>Size in string</returns>
public string ToVeryShortUnitString(int unit)
{
return (Bytes / (decimal)Math.Pow(1024, unit)).ToString("n") + " " + VeryShortUnits[unit];
}
#endregion
#region privates members
/// <summary>
/// Gets optimal display unit
/// </summary>
/// <returns>The unit from 0 (byte) to 8 (yottabyte)</returns>
private int GetOptimalUnit()
{
int unit = 0;
// get optimal unit
while (true)
{
if (Bytes < (decimal)Math.Pow(1024, unit))
{
if (unit > 0)
unit--;
break;
}
if (unit < Units.Length - 1)
unit++;
else
break;
}
return unit;
}
/// <summary>
/// Gets a unit number from a string containing the unit
/// </summary>
/// <param name="unit">Contains the unit</param>
/// <returns>An integer from 0 to 8</returns>
private int GetStringUnit(string unit)
{
for (int i = 0; i < 9; i++)
{
if ((Units[i] == unit) || (ShortUnits[i] == unit) || (VeryShortUnits[i] == unit))
return i;
}
return 0;
}
#endregion
}
}
|