sbBitratePropertyUnitConverter.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 // Property Unit Converter for bitrate values, ie: bps, kbps, Mbps
28 
30 #include <math.h>
31 #include <stdlib.h>
32 
33 // ctor - register all units
35 {
36  SetStringBundle(NS_LITERAL_STRING("chrome://songbird/locale/songbird.properties"));
38  NS_LITERAL_STRING("bps"),
39  NS_LITERAL_STRING("&bitrate.unit.bitspersecond"),
40  NS_LITERAL_STRING("&bitrate.unit.bitspersecond.short"));
42  NS_LITERAL_STRING("kbps"),
43  NS_LITERAL_STRING("&bitrate.unit.kilobitspersecond"),
44  NS_LITERAL_STRING("&bitrate.unit.kilobitspersecond.short"),
45  PR_TRUE);
47  NS_LITERAL_STRING("mbps"),
48  NS_LITERAL_STRING("&bitrate.unit.megabitspersecond"),
49  NS_LITERAL_STRING("&bitrate.unit.megabitspersecond.short"));
50 }
51 
52 // dtor
54 {
55 }
56 
57 // convert from the native unit to any of the exposed units
58 NS_IMETHODIMP
59 sbBitratePropertyUnitConverter::ConvertFromNativeToUnit(PRFloat64 aValue, PRUint32 aUnitID, PRFloat64 &_retVal)
60 {
61  switch (aUnitID) {
62  case BITRATE_UNIT_BPS:
63  aValue *= 1000.0;
64  break;
65  case BITRATE_UNIT_KBPS:
66  // native unit, nothing to do
67  break;
68  case BITRATE_UNIT_MBPS:
69  aValue /= 1000.0;
70  break;
71  default:
72  return NS_ERROR_INVALID_ARG;
73  }
74  _retVal = aValue;
75  return NS_OK;
76 }
77 
78 // convert from any of the exposed units back to the native unit
79 NS_IMETHODIMP
80 sbBitratePropertyUnitConverter::ConvertFromUnitToNative(PRFloat64 aValue, PRUint32 aUnitID, PRFloat64 &_retVal)
81 {
82  switch (aUnitID) {
83  case BITRATE_UNIT_BPS:
84  aValue /= 1000.0;
85  break;
86  case BITRATE_UNIT_KBPS:
87  // native unit, nothing to do
88  break;
89  case BITRATE_UNIT_MBPS:
90  aValue *= 1000.0;
91  break;
92  default:
93  return NS_ERROR_INVALID_ARG;
94  }
95  _retVal = aValue;
96  return NS_OK;
97 }
98 
100  // get number of digits
101  PRUint32 d;
102  if (aValue == 0) d = 1;
103  else d = (PRUint32)(log10(fabs(aValue)) + 1);
104 
105  // and pick most suitable unit
106  if (d <= 1) return BITRATE_UNIT_BPS;
107  if (d < 4) return BITRATE_UNIT_KBPS;
108  return BITRATE_UNIT_MBPS;
109 }
return NS_OK
onPageChanged aValue
Definition: FeedWriter.js:1395
NS_IMETHOD ConvertFromNativeToUnit(PRFloat64 aValue, PRUint32 aUnitID, PRFloat64 &_retVal)
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 ConvertFromUnitToNative(PRFloat64 aValue, PRUint32 aUnitID, PRFloat64 &_retVal)
void SetStringBundle(const nsAString &aStringBundle)