aboutsummaryrefslogtreecommitdiff
path: root/src/util/amount.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/amount.c')
-rw-r--r--src/util/amount.c42
1 files changed, 33 insertions, 9 deletions
diff --git a/src/util/amount.c b/src/util/amount.c
index bb5bf0d5b..65fac78e3 100644
--- a/src/util/amount.c
+++ b/src/util/amount.c
@@ -19,6 +19,12 @@
19 * @author Sree Harsha Totakura <sreeharsha@totakura.in> 19 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
20 * @author Florian Dold 20 * @author Florian Dold
21 * @author Benedikt Mueller 21 * @author Benedikt Mueller
22 *
23 * TODO:
24 * - the way this library currently deals with underflow/overflow
25 * is insufficient; just going for UINT32_MAX on overflow
26 * will not do; similar issues for incompatible currencies;
27 * we need some more explicit logic to say 'bogus value',
22 */ 28 */
23#include "platform.h" 29#include "platform.h"
24#include "taler_util.h" 30#include "taler_util.h"
@@ -169,7 +175,8 @@ TALER_amount_ntoh (struct TALER_AmountNBO dn)
169 * @return result of the comparison 175 * @return result of the comparison
170 */ 176 */
171int 177int
172TALER_amount_cmp (struct TALER_Amount a1, struct TALER_Amount a2) 178TALER_amount_cmp (struct TALER_Amount a1,
179 struct TALER_Amount a2)
173{ 180{
174 a1 = TALER_amount_normalize (a1); 181 a1 = TALER_amount_normalize (a1);
175 a2 = TALER_amount_normalize (a2); 182 a2 = TALER_amount_normalize (a2);
@@ -195,7 +202,8 @@ TALER_amount_cmp (struct TALER_Amount a1, struct TALER_Amount a2)
195 * @return (a1-a2) or 0 if a2>=a1 202 * @return (a1-a2) or 0 if a2>=a1
196 */ 203 */
197struct TALER_Amount 204struct TALER_Amount
198TALER_amount_subtract (struct TALER_Amount a1, struct TALER_Amount a2) 205TALER_amount_subtract (struct TALER_Amount a1,
206 struct TALER_Amount a2)
199{ 207{
200 a1 = TALER_amount_normalize (a1); 208 a1 = TALER_amount_normalize (a1);
201 a2 = TALER_amount_normalize (a2); 209 a2 = TALER_amount_normalize (a2);
@@ -233,7 +241,8 @@ TALER_amount_subtract (struct TALER_Amount a1, struct TALER_Amount a2)
233 * @return sum of a1 and a2 241 * @return sum of a1 and a2
234 */ 242 */
235struct TALER_Amount 243struct TALER_Amount
236TALER_amount_add (struct TALER_Amount a1, struct TALER_Amount a2) 244TALER_amount_add (struct TALER_Amount a1,
245 struct TALER_Amount a2)
237{ 246{
238 a1 = TALER_amount_normalize (a1); 247 a1 = TALER_amount_normalize (a1);
239 a2 = TALER_amount_normalize (a2); 248 a2 = TALER_amount_normalize (a2);
@@ -243,17 +252,25 @@ TALER_amount_add (struct TALER_Amount a1, struct TALER_Amount a2)
243 252
244 if (0 == a1.currency[0]) 253 if (0 == a1.currency[0])
245 { 254 {
246 memcpy (a2.currency, a1.currency, TALER_CURRENCY_LEN); 255 memcpy (a2.currency,
256 a1.currency,
257 TALER_CURRENCY_LEN);
247 } 258 }
248 259
249 if (0 == a2.currency[0]) 260 if (0 == a2.currency[0])
250 { 261 {
251 memcpy (a1.currency, a2.currency, TALER_CURRENCY_LEN); 262 memcpy (a1.currency,
263 a2.currency,
264 TALER_CURRENCY_LEN);
252 } 265 }
253 266
254 if (0 != a1.currency[0] && 0 != memcmp (a1.currency, a2.currency, TALER_CURRENCY_LEN)) 267 if ( (0 != a1.currency[0]) &&
268 (0 != memcmp (a1.currency,
269 a2.currency,
270 TALER_CURRENCY_LEN)) )
255 { 271 {
256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "adding mismatching currencies\n"); 272 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
273 "adding mismatching currencies\n");
257 } 274 }
258 275
259 if (a1.value < a2.value) 276 if (a1.value < a2.value)
@@ -312,11 +329,18 @@ TALER_amount_to_string (struct TALER_Amount amount)
312 n = (n * 10) % (AMOUNT_FRAC_BASE); 329 n = (n * 10) % (AMOUNT_FRAC_BASE);
313 } 330 }
314 tail[i] = 0; 331 tail[i] = 0;
315 len = GNUNET_asprintf (&result, "%s:%lu.%s", curr, (unsigned long) amount.value, tail); 332 len = GNUNET_asprintf (&result,
333 "%s:%lu.%s",
334 curr,
335 (unsigned long) amount.value,
336 tail);
316 } 337 }
317 else 338 else
318 { 339 {
319 len = GNUNET_asprintf (&result, "%s:%lu", curr, (unsigned long) amount.value); 340 len = GNUNET_asprintf (&result,
341 "%s:%lu",
342 curr,
343 (unsigned long) amount.value);
320 } 344 }
321 GNUNET_assert (len > 0); 345 GNUNET_assert (len > 0);
322 return result; 346 return result;