summaryrefslogtreecommitdiff
path: root/amount.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2004-08-12 20:41:29 -0400
committerJohn Wiegley <johnw@newartisans.com>2004-08-12 20:41:29 -0400
commit0f2ed1f5e36c8e76bc4fc6c406e7d921f794df6a (patch)
treeddb68580e76e96ff8a0723b399559b8e57d64438 /amount.cc
parent71e8d506573fc61271e10fc3d02eea9a5eb8ba01 (diff)
downloadfork-ledger-0f2ed1f5e36c8e76bc4fc6c406e7d921f794df6a.tar.gz
fork-ledger-0f2ed1f5e36c8e76bc4fc6c406e7d921f794df6a.tar.bz2
fork-ledger-0f2ed1f5e36c8e76bc4fc6c406e7d921f794df6a.zip
write amounts out to the binary file in binary format
Diffstat (limited to 'amount.cc')
-rw-r--r--amount.cc22
1 files changed, 18 insertions, 4 deletions
diff --git a/amount.cc b/amount.cc
index 9a88db38..f5e7b0ad 100644
--- a/amount.cc
+++ b/amount.cc
@@ -646,8 +646,7 @@ void amount_t::parse(std::istream& in)
unsigned int flags = COMMODITY_STYLE_DEFAULTS;;
unsigned int precision = MAX_PRECISION;
- if (! quantity)
- _init();
+ INIT();
char c = peek_next_nonws(in);
if (std::isdigit(c) || c == '.' || c == '-') {
@@ -732,14 +731,26 @@ void amount_t::parse(std::istream& in)
delete[] buf;
}
+// If necessary, amounts may be recorded in a binary file textually.
+// This offers little advantage, and requires binary<->decimal
+// conversion each time the file is saved or loaded.
+//
+//#define WRITE_AMOUNTS_TEXTUALLY
+
static char buf[4096];
void amount_t::write_quantity(std::ostream& out) const
{
unsigned short len;
if (quantity) {
+#ifdef WRITE_AMOUNTS_TEXTUALLY
mpz_get_str(buf, 10, MPZ(quantity));
len = std::strlen(buf);
+#else
+ std::size_t size;
+ mpz_export(buf, &size, 1, sizeof(int), 0, 0, MPZ(quantity));
+ len = size * sizeof(int);
+#endif
assert(len);
out.write((char *)&len, sizeof(len));
out.write(buf, len);
@@ -755,10 +766,13 @@ void amount_t::read_quantity(std::istream& in)
in.read((char *)&len, sizeof(len));
if (len) {
in.read(buf, len);
+ INIT();
+#ifdef WRITE_AMOUNTS_TEXTUALLY
buf[len] = '\0';
- if (! quantity)
- _init();
mpz_set_str(MPZ(quantity), buf, 10);
+#else
+ mpz_import(MPZ(quantity), len / sizeof(int), 1, sizeof(int), 0, 0, buf);
+#endif
} else {
if (quantity)
_clear();