more binary format changes

This commit is contained in:
John Wiegley 2004-08-16 22:18:07 -04:00
parent 7189b181ef
commit 3a44545540
4 changed files with 68 additions and 54 deletions

View file

@ -171,13 +171,13 @@ inline std::istream& operator>>(std::istream& in, amount_t& amt) {
std::ostream& operator<<(std::ostream& out, const amount_t& amt); std::ostream& operator<<(std::ostream& out, const amount_t& amt);
#define COMMODITY_STYLE_DEFAULTS 0x00 #define COMMODITY_STYLE_DEFAULTS 0x0000
#define COMMODITY_STYLE_SUFFIXED 0x01 #define COMMODITY_STYLE_SUFFIXED 0x0001
#define COMMODITY_STYLE_SEPARATED 0x02 #define COMMODITY_STYLE_SEPARATED 0x0002
#define COMMODITY_STYLE_EUROPEAN 0x04 #define COMMODITY_STYLE_EUROPEAN 0x0004
#define COMMODITY_STYLE_THOUSANDS 0x08 #define COMMODITY_STYLE_THOUSANDS 0x0008
#define COMMODITY_STYLE_CONSULTED 0x10 #define COMMODITY_STYLE_CONSULTED 0x0010
#define COMMODITY_STYLE_NOMARKET 0x20 #define COMMODITY_STYLE_NOMARKET 0x0020
typedef std::map<const std::time_t, amount_t> history_map; typedef std::map<const std::time_t, amount_t> history_map;
typedef std::pair<const std::time_t, amount_t> history_pair; typedef std::pair<const std::time_t, amount_t> history_pair;
@ -197,16 +197,16 @@ class commodity_t
const std::time_t moment) = 0; const std::time_t moment) = 0;
}; };
typedef unsigned short ident_t; typedef unsigned long ident_t;
std::string symbol; std::string symbol;
std::string name; std::string name;
std::string note; std::string note;
unsigned int precision; unsigned short precision;
unsigned int flags; unsigned short flags;
history_map history; history_map history;
amount_t conversion; amount_t conversion;
ident_t ident; ident_t ident;
// If set, this global function pointer is called to determine // If set, this global function pointer is called to determine
// whether prices have been updated in the meanwhile. // whether prices have been updated in the meanwhile.

View file

@ -14,8 +14,6 @@
namespace ledger { namespace ledger {
static char buf[4096];
unsigned long binary_magic_number = 0xFFEED765; unsigned long binary_magic_number = 0xFFEED765;
static unsigned long format_version = 0x0002000a; static unsigned long format_version = 0x0002000a;
@ -53,10 +51,22 @@ inline void read_binary_string(std::istream& in, std::string& str)
unsigned char len; unsigned char len;
read_binary_number(in, len); read_binary_number(in, len);
if (len) { if (len == 0xff) {
unsigned short slen;
read_binary_number(in, slen);
char * buf = new char[slen + 1];
in.read(buf, slen);
buf[slen] = '\0';
str = buf;
delete[] buf;
}
else if (len) {
char buf[256];
in.read(buf, len); in.read(buf, len);
buf[len] = '\0'; buf[len] = '\0';
str = buf; str = buf;
} else {
str = "";
} }
read_binary_guard(in, 0x3002); read_binary_guard(in, 0x3002);
@ -64,20 +74,9 @@ inline void read_binary_string(std::istream& in, std::string& str)
inline std::string read_binary_string(std::istream& in) inline std::string read_binary_string(std::istream& in)
{ {
read_binary_guard(in, 0x3001); std::string temp;
read_binary_string(in, temp);
unsigned char len; return temp;
read_binary_number(in, len);
if (len) {
in.read(buf, len);
buf[len] = '\0';
read_binary_guard(in, 0x3002);
return buf;
}
read_binary_guard(in, 0x3002);
return "";
} }
void read_binary_amount(std::istream& in, amount_t& amt) void read_binary_amount(std::istream& in, amount_t& amt)
@ -271,8 +270,15 @@ inline void write_binary_string(std::ostream& out, const std::string& str)
{ {
write_binary_guard(out, 0x3001); write_binary_guard(out, 0x3001);
unsigned char len = str.length(); unsigned long len = str.length();
write_binary_number(out, len); if (len > 255) {
assert(len < 65536);
write_binary_number<unsigned char>(out, 0xff);
write_binary_number<unsigned short>(out, len);
} else {
write_binary_number<unsigned char>(out, len);
}
if (len) if (len)
out.write(str.c_str(), len); out.write(str.c_str(), len);

View file

@ -27,14 +27,16 @@
namespace ledger { namespace ledger {
#define TRANSACTION_NORMAL 0x00 // These flags persist with the object
#define TRANSACTION_VIRTUAL 0x01 #define TRANSACTION_NORMAL 0x0000
#define TRANSACTION_BALANCE 0x02 #define TRANSACTION_VIRTUAL 0x0001
#define TRANSACTION_AUTO 0x04 #define TRANSACTION_BALANCE 0x0002
#define TRANSACTION_AUTO 0x0004
#define TRANSACTION_HANDLED 0x10 // These flags are only used during formatting, and are not saved
#define TRANSACTION_DISPLAYED 0x20 #define TRANSACTION_HANDLED 0x0001
#define TRANSACTION_NO_TOTAL 0x40 #define TRANSACTION_DISPLAYED 0x0002
#define TRANSACTION_NO_TOTAL 0x0004
class entry_t; class entry_t;
class account_t; class account_t;
@ -46,12 +48,12 @@ class transaction_t
account_t * account; account_t * account;
amount_t amount; amount_t amount;
amount_t cost; amount_t cost;
unsigned int flags; unsigned short flags;
std::string note; std::string note;
mutable balance_pair_t total; mutable balance_pair_t total;
mutable unsigned int index; mutable unsigned int index;
mutable unsigned int dflags; mutable unsigned short dflags;
transaction_t(entry_t * _entry, account_t * _account) transaction_t(entry_t * _entry, account_t * _account)
: entry(_entry), account(_account), flags(TRANSACTION_NORMAL), : entry(_entry), account(_account), flags(TRANSACTION_NORMAL),
@ -110,12 +112,12 @@ typedef std::pair<const std::string, account_t *> accounts_pair;
class account_t class account_t
{ {
public: public:
typedef unsigned short ident_t; typedef unsigned long ident_t;
account_t * parent; account_t * parent;
std::string name; std::string name;
std::string note; std::string note;
unsigned char depth; unsigned short depth;
accounts_map accounts; accounts_map accounts;
transactions_list transactions; transactions_list transactions;
@ -174,10 +176,11 @@ typedef std::list<std::string> strings_list;
class journal_t class journal_t
{ {
public: public:
account_t * master; account_t * master;
entries_list entries; entries_list entries;
mutable accounts_map accounts_cache; strings_list sources;
std::list<std::string> sources;
mutable accounts_map accounts_cache;
journal_t() { journal_t() {
master = new account_t(NULL, ""); master = new account_t(NULL, "");

13
main.cc
View file

@ -50,7 +50,7 @@ int main(int argc, char * argv[], char * envp[])
TIMER_STOP(process_args); TIMER_STOP(process_args);
const bool use_cache = config->files.empty(); bool use_cache = config->files.empty();
// Process options from the environment // Process options from the environment
@ -91,6 +91,7 @@ int main(int argc, char * argv[], char * envp[])
if (entry_count == 0 || exceptions.size() > 0) { if (entry_count == 0 || exceptions.size() > 0) {
journal.reset(new journal_t); journal.reset(new journal_t);
entry_count = 0; entry_count = 0;
cache_dirty = true;
} else { } else {
cache_dirty = false; cache_dirty = false;
} }
@ -100,7 +101,13 @@ int main(int argc, char * argv[], char * envp[])
for (strings_list::iterator i = config->files.begin(); for (strings_list::iterator i = config->files.begin();
i != config->files.end(); i != config->files.end();
i++) i++)
entry_count += parse_journal_file(*i, journal.get()); if (*i == "-") {
use_cache = false;
entry_count += parse_textual_journal(std::cin, journal.get(),
journal->master);
} else {
entry_count += parse_journal_file(*i, journal.get());
}
if (! config->price_db.empty()) if (! config->price_db.empty())
if (parse_journal_file(config->price_db, journal.get())) if (parse_journal_file(config->price_db, journal.get()))
@ -452,8 +459,6 @@ int main(int argc, char * argv[], char * envp[])
// feeding each transaction that matches `predicate' to the chain. // feeding each transaction that matches `predicate' to the chain.
walk_entries(journal->entries, *formatter.get()); walk_entries(journal->entries, *formatter.get());
formatter->flush();
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
// The transaction display flags (dflags) are not recorded in the // The transaction display flags (dflags) are not recorded in the
// binary cache, and only need to be cleared if the transactions // binary cache, and only need to be cleared if the transactions