aboutsummaryrefslogtreecommitdiff
path: root/src/include/taler_amount_lib.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/include/taler_amount_lib.h')
-rw-r--r--src/include/taler_amount_lib.h173
1 files changed, 173 insertions, 0 deletions
diff --git a/src/include/taler_amount_lib.h b/src/include/taler_amount_lib.h
new file mode 100644
index 000000000..50b34ff15
--- /dev/null
+++ b/src/include/taler_amount_lib.h
@@ -0,0 +1,173 @@
1/*
2 This file is part of TALER
3 (C) 2014, 2015 Christian Grothoff (and other contributing authors)
4
5 TALER is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
8
9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
15*/
16/**
17 * @file include/taler_amount_lib.h
18 * @brief amount-representation utility functions
19 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
20 */
21#ifndef TALER_AMOUNT_LIB_H
22#define TALER_AMOUNT_LIB_H
23
24
25/**
26 * Number of characters (plus 1 for 0-termination) we use to
27 * represent currency names (i.e. EUR, USD, etc.). We use
28 * 8 for alignment (!).
29 */
30#define TALER_CURRENCY_LEN 8
31
32
33GNUNET_NETWORK_STRUCT_BEGIN
34
35
36/**
37 * Amount, encoded for network transmission.
38 */
39struct TALER_AmountNBO
40{
41 /**
42 * Value in the main currency, in NBO.
43 */
44 uint32_t value;
45
46 /**
47 * Additinal fractional value, in NBO.
48 */
49 uint32_t fraction;
50
51 /**
52 * Type of the currency being represented.
53 */
54 char currency[TALER_CURRENCY_LEN];
55};
56
57GNUNET_NETWORK_STRUCT_END
58
59
60/**
61 * Representation of monetary value in a given currency.
62 */
63struct TALER_Amount
64{
65 /**
66 * Value (numerator of fraction)
67 */
68 uint32_t value;
69
70 /**
71 * Fraction (denominator of fraction)
72 */
73 uint32_t fraction;
74
75 /**
76 * Currency string, left adjusted and padded with zeros.
77 */
78 char currency[TALER_CURRENCY_LEN];
79};
80
81
82/**
83 * Parse denomination description, in the format "T : V : F".
84 *
85 * @param str denomination description
86 * @param denom denomination to write the result to
87 * @return #GNUNET_OK if the string is a valid denomination specification,
88 * #GNUNET_SYSERR if it is invalid.
89 */
90int
91TALER_string_to_amount (const char *str,
92 struct TALER_Amount *denom);
93
94
95/**
96 * Convert amount from host to network representation.
97 *
98 * @param d amount in host representation
99 * @return amount in network representation
100 */
101struct TALER_AmountNBO
102TALER_amount_hton (struct TALER_Amount d);
103
104
105/**
106 * Convert amount from network to host representation.
107 *
108 * @param d amount in network representation
109 * @return amount in host representation
110 */
111struct TALER_Amount
112TALER_amount_ntoh (struct TALER_AmountNBO dn);
113
114
115/**
116 * Compare the value/fraction of two amounts. Does not compare the currency,
117 * i.e. comparing amounts with the same value and fraction but different
118 * currency would return 0.
119 *
120 * @param a1 first amount
121 * @param a2 second amount
122 * @return result of the comparison
123 */
124int
125TALER_amount_cmp (struct TALER_Amount a1,
126 struct TALER_Amount a2);
127
128
129/**
130 * Perform saturating subtraction of amounts.
131 *
132 * @param a1 amount to subtract from
133 * @param a2 amount to subtract
134 * @return (a1-a2) or 0 if a2>=a1
135 */
136struct TALER_Amount
137TALER_amount_subtract (struct TALER_Amount a1,
138 struct TALER_Amount a2);
139
140
141/**
142 * Perform saturating addition of amounts
143 *
144 * @param a1 first amount to add
145 * @param a2 second amount to add
146 * @return sum of a1 and a2
147 */
148struct TALER_Amount
149TALER_amount_add (struct TALER_Amount a1,
150 struct TALER_Amount a2);
151
152
153/**
154 * Normalize the given amount.
155 *
156 * @param amout amount to normalize
157 * @return normalized amount
158 */
159struct TALER_Amount
160TALER_amount_normalize (struct TALER_Amount amount);
161
162
163/**
164 * Convert amount to string.
165 *
166 * @param amount amount to convert to string
167 * @return freshly allocated string representation
168 */
169char *
170TALER_amount_to_string (struct TALER_Amount amount);
171
172
173#endif