sbFraction.h
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-2009 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 #ifndef SBFRACTION_H_
26 #define SBFRACTION_H_
27 
28 #include <nsStringAPI.h>
29 
36 {
37 public:
41  sbFraction() : mNumerator(0),
42  mDenominator(1) {}
43 
49  sbFraction(PRUint32 aNumerator, PRUint32 aDenominator = 1) :
50  mNumerator(aNumerator),
51  mDenominator(aDenominator) {}
52 
56  sbFraction(PRUint32 aWholeNumber,
57  PRUint32 aNumerator,
58  PRUint32 aDenominator) :
59  mNumerator(aNumerator + aDenominator * aWholeNumber),
60  mDenominator(aDenominator) {}
61 
69  bool IsEqual(sbFraction const & aOther) const
70  {
71  return ((mNumerator == aOther.mNumerator &&
72  mDenominator == aOther.mDenominator) ||
73  (mNumerator * aOther.mDenominator ==
74  mDenominator * aOther.mNumerator));
75  }
76 
83  bool IsLessThan(sbFraction const & aOther) const
84  {
85  return *this < aOther;
86  }
87 
91  operator double() const
92  {
93  return static_cast<double>(mNumerator) / static_cast<double>(mDenominator);
94  }
95 
99  PRBool operator > (const sbFraction & aFraction)
100  {
101  return (PRInt64)mNumerator * aFraction.mDenominator >
102  (PRInt64)aFraction.mNumerator * mDenominator;
103  }
104 
108  PRBool operator < (const sbFraction & aFraction)
109  {
110  return (PRInt64)mNumerator * aFraction.mDenominator <
111  (PRInt64)aFraction.mNumerator * mDenominator;
112  }
113 
117  PRBool operator >= (const sbFraction & aFraction)
118  {
119  return (PRInt64)mNumerator * aFraction.mDenominator >=
120  (PRInt64)aFraction.mNumerator * mDenominator;
121  }
122 
126  PRBool operator <= (const sbFraction & aFraction)
127  {
128  return (PRInt64)mNumerator * aFraction.mDenominator <=
129  (PRInt64)aFraction.mNumerator * mDenominator;
130  }
131 
136  PRUint32 Numerator() const
137  {
138  return mNumerator;
139  }
140 
144  PRUint32 Denominator() const
145  {
146  return mDenominator;
147  }
148 
149  void GetProperFraction(PRUint32& aWhole,
150  PRUint32& aNumerator,
151  PRUint32& aDenominator) const
152  {
153  aWhole = mNumerator / mDenominator;
154  aNumerator = mNumerator - (aWhole * aDenominator);
155  aDenominator = mDenominator;
156  }
157 private:
158  PRUint32 mNumerator;
159  PRUint32 mDenominator;
160 };
161 
165 inline
166 bool operator ==(sbFraction const & aLeft, sbFraction const & aRight)
167 {
168  return aLeft.IsEqual(aRight);
169 }
170 
174 inline
175 bool operator !=(sbFraction const & aLeft, sbFraction const & aRight)
176 {
177  return !aLeft.IsEqual(aRight);
178 }
179 
184 inline
185 bool operator <(sbFraction const & aLeft, sbFraction const & aRight)
186 {
187  return aLeft.IsLessThan(aRight);
188 }
189 
194 inline
195 bool operator >(sbFraction const & aLeft, sbFraction const & aRight)
196 {
197  return aLeft != aRight && !aLeft.IsLessThan(aRight);
198 }
199 
204 inline
205 bool operator >=(sbFraction const & aLeft, sbFraction const & aRight)
206 {
207  return aLeft == aRight || aLeft > aRight;
208 }
209 
214 inline
215 bool operator <=(sbFraction const & aLeft, sbFraction const & aRight)
216 {
217  return aLeft == aRight || aLeft < aRight;
218 }
219 
223 inline
224 nsString sbFractionToString(sbFraction const & aFraction)
225 {
226  nsString result;
227 
228  result.AppendInt(aFraction.Numerator(), 10);
229  if (aFraction.Denominator() > 1) {
230  result.Append(NS_LITERAL_STRING("/"));
231  result.AppendInt(aFraction.Denominator(), 10);
232  }
233 
234  return result;
235 }
236 
237 inline
238 nsresult sbFractionParsePart(nsAString const & aString,
239  PRInt32 aStart,
240  PRInt32 aLength,
241  PRUint32 * aValue)
242 {
243  nsresult rv;
244 
245  nsDependentSubstring part(aString, aStart, aLength);
246  PRInt32 temp = part.ToInteger(&rv, 10);
247  NS_ENSURE_SUCCESS(rv, rv);
248  NS_ENSURE_TRUE(temp >= 0, NS_ERROR_FAILURE);
249 
250  *aValue = temp;
251 
252  return NS_OK;
253 }
254 
258 inline
259 nsresult sbFractionFromString(nsAString const & aString,
260  sbFraction & aFraction)
261 {
262  nsresult rv;
263 
264  PRUint32 whole = 0;
265  PRUint32 numerator = 0;
266  PRUint32 denominator = 1;
267 
268  PRInt32 const space = aString.Find(NS_LITERAL_STRING(" "));
269  PRInt32 const slash = aString.Find(NS_LITERAL_STRING("/"));
270 
271  // Whole number
272  if (space == -1) {
273  if (slash == -1) {
274  numerator = aString.ToInteger(&rv, 10);
275  aFraction = sbFraction(numerator);
276  }
277  else {
278  rv = sbFractionParsePart(aString, 0, slash, &numerator);
279  NS_ENSURE_SUCCESS(rv, rv);
280  rv = sbFractionParsePart(aString,
281  slash + 1,
282  aString.Length() - slash - 1,
283  &denominator);
284  NS_ENSURE_SUCCESS(rv, rv);
285 
286  aFraction = sbFraction(numerator, denominator);
287  }
288  return NS_OK;
289  }
290  // Bad format, no slash
291  if (slash == -1) {
292  return NS_ERROR_FAILURE;
293  }
294 
295  rv = sbFractionParsePart(aString, 0, space, &whole);
296  NS_ENSURE_SUCCESS(rv, rv);
297 
298  rv = sbFractionParsePart(aString, space + 1, slash - space - 1, &numerator);
299  NS_ENSURE_SUCCESS(rv, rv);
300 
301  rv = sbFractionParsePart(aString,
302  slash + 1,
303  aString.Length() - slash - 1,
304  &denominator);
305  NS_ENSURE_SUCCESS(rv, rv);
306 
307  aFraction = sbFraction(whole, numerator, denominator);
308 
309  return NS_OK;
310 }
311 inline
312 nsresult sbFractionFromString(nsACString const & aString,
313  sbFraction & aFraction)
314 {
315  return sbFractionFromString(NS_ConvertASCIItoUTF16(aString), aFraction);
316 }
317 inline
318 nsresult sbFractionFromString(const char* aString,
319  sbFraction & aFraction)
320 {
321  return sbFractionFromString(NS_ConvertASCIItoUTF16(nsDependentCString(aString)),
322  aFraction);
323 }
324 
325 #endif /* SBFRACTION_H_ */
bool operator==(sbFraction const &aLeft, sbFraction const &aRight)
Definition: sbFraction.h:166
bool IsLessThan(sbFraction const &aOther) const
Definition: sbFraction.h:83
return NS_OK
PRBool operator<=(const sbFraction &aFraction)
Definition: sbFraction.h:126
onPageChanged aValue
Definition: FeedWriter.js:1395
nsresult sbFractionFromString(nsAString const &aString, sbFraction &aFraction)
Definition: sbFraction.h:259
bool operator>(sbFraction const &aLeft, sbFraction const &aRight)
Definition: sbFraction.h:195
PRBool operator>(const sbFraction &aFraction)
Definition: sbFraction.h:99
PRUint32 Numerator() const
Definition: sbFraction.h:136
restoreDimensions aLeft
PRBool operator>=(const sbFraction &aFraction)
Definition: sbFraction.h:117
bool operator<(sbFraction const &aLeft, sbFraction const &aRight)
Definition: sbFraction.h:185
bool operator<=(sbFraction const &aLeft, sbFraction const &aRight)
Definition: sbFraction.h:215
PRBool operator<(const sbFraction &aFraction)
Definition: sbFraction.h:108
bool operator!=(sbFraction const &aLeft, sbFraction const &aRight)
Definition: sbFraction.h:175
PRUint32 Denominator() const
Definition: sbFraction.h:144
void GetProperFraction(PRUint32 &aWhole, PRUint32 &aNumerator, PRUint32 &aDenominator) const
Definition: sbFraction.h:149
sbFraction(PRUint32 aWholeNumber, PRUint32 aNumerator, PRUint32 aDenominator)
Definition: sbFraction.h:56
bool IsEqual(sbFraction const &aOther) const
Definition: sbFraction.h:69
sbFraction(PRUint32 aNumerator, PRUint32 aDenominator=1)
Definition: sbFraction.h:49
nsString sbFractionToString(sbFraction const &aFraction)
Definition: sbFraction.h:224
nsresult sbFractionParsePart(nsAString const &aString, PRInt32 aStart, PRInt32 aLength, PRUint32 *aValue)
Definition: sbFraction.h:238
bool operator>=(sbFraction const &aLeft, sbFraction const &aRight)
Definition: sbFraction.h:205