sbStoragePropertyUnitConverter.cpp
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 // Property Unit Converter for information storage quantities,
26 // ie: byte, kB, MB, GB
27 
29 #include <math.h>
30 #include <stdlib.h>
31 
32 // ctor - register all units
34 {
35  SetStringBundle(NS_LITERAL_STRING("chrome://songbird/locale/songbird.properties"));
37  NS_LITERAL_STRING("b"),
38  NS_LITERAL_STRING("&storage.unit.bytes"),
39  NS_LITERAL_STRING("&storage.unit.bytes.short"),
40  PR_TRUE);
42  NS_LITERAL_STRING("kb"),
43  NS_LITERAL_STRING("&storage.unit.kilobytes"),
44  NS_LITERAL_STRING("&storage.unit.kilobytes.short"));
46  NS_LITERAL_STRING("mb"),
47  NS_LITERAL_STRING("&storage.unit.megabytes"),
48  NS_LITERAL_STRING("&storage.unit.megabytes.short"));
50  NS_LITERAL_STRING("gb"),
51  NS_LITERAL_STRING("&storage.unit.gigabytes"),
52  NS_LITERAL_STRING("&storage.unit.gigabytes.short"));
54  NS_LITERAL_STRING("tb"),
55  NS_LITERAL_STRING("&storage.unit.terabytes"),
56  NS_LITERAL_STRING("&storage.unit.terabytes.short"));
58  NS_LITERAL_STRING("pb"),
59  NS_LITERAL_STRING("&storage.unit.petabytes"),
60  NS_LITERAL_STRING("&storage.unit.petabytes.short"));
62  NS_LITERAL_STRING("eb"),
63  NS_LITERAL_STRING("&storage.unit.exabytes"),
64  NS_LITERAL_STRING("&storage.unit.exabytes.short"));
65 }
66 
67 // dtor
69 {
70 }
71 
72 // convert from the native unit to any of the exposed units
73 NS_IMETHODIMP
75  PRUint32 aUnitID,
76  PRFloat64 &_retVal)
77 {
78  switch (aUnitID) {
79  case STORAGE_UNIT_BYTE:
80  // native unit, nothing to do
81  break;
83  aValue /= 1024.0;
84  break;
86  aValue /= (1024.0*1024.0);
87  break;
89  aValue /= (1024.0*1024.0*1024.0);
90  break;
92  aValue /= (1024.0*1024.0*1024.0*1024.0);
93  break;
95  aValue /= (1024.0*1024.0*1024.0*1024.0*1024.0);
96  break;
98  aValue /= (1024.0*1024.0*1024.0*1024.0*1024.0*1024.0);
99  break;
100  default:
101  return NS_ERROR_INVALID_ARG;
102  }
103  _retVal = aValue;
104  return NS_OK;
105 }
106 
107 // convert from any of the exposed units back to the native unit
108 NS_IMETHODIMP
110  PRUint32 aUnitID,
111  PRFloat64 &_retVal)
112 {
113  switch (aUnitID) {
114  case STORAGE_UNIT_BYTE:
115  // native unit, nothing to do
116  break;
118  aValue *= 1024.0;
119  break;
121  aValue *= (1024.0*1024.0);
122  break;
124  aValue *= (1024.0*1024.0*1024.0);
125  break;
127  aValue *= (1024.0*1024.0*1024.0*1024.0);
128  break;
130  aValue *= (1024.0*1024.0*1024.0*1024.0*1024.0);
131  break;
133  aValue *= (1024.0*1024.0*1024.0*1024.0*1024.0*1024.0);
134  break;
135  default:
136  return NS_ERROR_INVALID_ARG;
137  }
138  _retVal = aValue;
139  return NS_OK;
140 }
141 
143  // get number of digits
144  PRUint32 d;
145  if (aValue == 0) d = 1;
146  else d = (PRUint32)(log10(fabs(aValue)) + 1);
147 
148  // and pick most suitable unit
149  if (d < 4) return STORAGE_UNIT_BYTE;
150  if (d < 7) return STORAGE_UNIT_KILOBYTE;
151  if (d < 10) return STORAGE_UNIT_MEGABYTE;
152  if (d < 13) return STORAGE_UNIT_GIGABYTE;
153  if (d < 16) return STORAGE_UNIT_TERABYTE;
154  if (d < 19) return STORAGE_UNIT_PETABYTE;
155  return STORAGE_UNIT_EXABYTE;
156 }
return NS_OK
NS_IMETHOD ConvertFromUnitToNative(PRFloat64 aValue, PRUint32 aUnitID, PRFloat64 &_retVal)
onPageChanged aValue
Definition: FeedWriter.js:1395
function d(s)
virtual PRInt32 GetAutoUnit(PRFloat64 aValue)
void RegisterUnit(PRUint32 aUnitInternalID, const nsAString &aUnitExternalID, const nsAString &aUnitName, const nsAString &aUnitShortName, PRBool isNative=PR_FALSE)
NS_IMETHOD ConvertFromNativeToUnit(PRFloat64 aValue, PRUint32 aUnitID, PRFloat64 &_retVal)
void SetStringBundle(const nsAString &aStringBundle)