diff options
author | John Wiegley <johnw@newartisans.com> | 2004-11-08 06:43:11 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 02:40:47 -0400 |
commit | c9fb11bd60a2170fb896d77ff8d7706f563ad597 (patch) | |
tree | 42bdf09e7d8727ba31d1d8dae9b4eb4b2a605441 | |
parent | fa2ceaed13c031add578ee8eb33da0c9980b9fb1 (diff) | |
download | fork-ledger-c9fb11bd60a2170fb896d77ff8d7706f563ad597.tar.gz fork-ledger-c9fb11bd60a2170fb896d77ff8d7706f563ad597.tar.bz2 fork-ledger-c9fb11bd60a2170fb896d77ff8d7706f563ad597.zip |
updated to version 2.0
-rw-r--r-- | LICENSE | 28 | ||||
-rw-r--r-- | Makefile | 50 | ||||
-rw-r--r-- | Makefile.am | 109 | ||||
-rw-r--r-- | Makefile.lt | 115 | ||||
-rw-r--r-- | NEWS | 177 | ||||
-rw-r--r-- | README | 1241 | ||||
-rwxr-xr-x | acprep | 38 | ||||
-rwxr-xr-x | acprep.lt | 37 | ||||
-rw-r--r-- | amount.cc | 1529 | ||||
-rw-r--r-- | amount.h | 443 | ||||
-rw-r--r-- | balance.cc | 277 | ||||
-rw-r--r-- | balance.h | 893 | ||||
-rw-r--r-- | binary.cc | 753 | ||||
-rw-r--r-- | binary.h | 26 | ||||
-rw-r--r-- | config.cc | 991 | ||||
-rw-r--r-- | config.h | 103 | ||||
-rw-r--r-- | configure.in | 96 | ||||
-rw-r--r-- | configure.lt | 97 | ||||
-rw-r--r-- | datetime.cc | 419 | ||||
-rw-r--r-- | datetime.h | 62 | ||||
-rw-r--r-- | debug.cc | 112 | ||||
-rw-r--r-- | debug.h | 119 | ||||
-rw-r--r-- | derive.cc | 144 | ||||
-rw-r--r-- | derive.h | 14 | ||||
-rw-r--r-- | emacs.py | 53 | ||||
-rw-r--r-- | error.h | 65 | ||||
-rw-r--r-- | format.cc | 842 | ||||
-rw-r--r-- | format.h | 198 | ||||
-rw-r--r-- | gnucash.cc | 258 | ||||
-rw-r--r-- | gnucash.h | 21 | ||||
-rw-r--r-- | journal.cc | 790 | ||||
-rw-r--r-- | journal.h | 385 | ||||
-rw-r--r-- | ledger.cc | 674 | ||||
-rw-r--r-- | ledger.el | 56 | ||||
-rw-r--r-- | ledger.h | 371 | ||||
-rw-r--r-- | ledger.texi | 3723 | ||||
-rw-r--r-- | main.cc | 424 | ||||
-rw-r--r-- | main.py | 479 | ||||
-rw-r--r-- | mask.cc | 49 | ||||
-rw-r--r-- | mask.h | 32 | ||||
-rw-r--r-- | option.cc | 261 | ||||
-rw-r--r-- | option.h | 44 | ||||
-rw-r--r-- | parse.cc | 582 | ||||
-rw-r--r-- | parser.cc | 119 | ||||
-rw-r--r-- | parser.h | 40 | ||||
-rw-r--r-- | py_eval.cc | 152 | ||||
-rw-r--r-- | py_eval.h | 75 | ||||
-rw-r--r-- | pyledger.cc | 9 | ||||
-rw-r--r-- | pyledger.h | 16 | ||||
-rw-r--r-- | qif.cc | 203 | ||||
-rw-r--r-- | qif.h | 21 | ||||
-rw-r--r-- | quotes.cc | 76 | ||||
-rw-r--r-- | quotes.h | 30 | ||||
-rw-r--r-- | reports.cc | 1225 | ||||
-rw-r--r-- | sample.dat | 29 | ||||
-rw-r--r-- | scripts/README | 5 | ||||
-rwxr-xr-x[-rw-r--r--] | scripts/bal | 18 | ||||
-rwxr-xr-x | scripts/bal-huquq | 21 | ||||
-rwxr-xr-x[-rw-r--r--] | scripts/entry | 2 | ||||
-rwxr-xr-x[-rw-r--r--] | scripts/getquote | 8 | ||||
-rw-r--r-- | scripts/mean | 27 | ||||
-rw-r--r-- | scripts/profit | 2 | ||||
-rw-r--r-- | scripts/reg | 14 | ||||
-rwxr-xr-x[-rw-r--r--] | scripts/report | 28 | ||||
-rw-r--r-- | scripts/spending | 8 | ||||
-rwxr-xr-x | scripts/tc | 7 | ||||
-rwxr-xr-x | scripts/ti | 5 | ||||
-rwxr-xr-x | scripts/to | 3 | ||||
-rwxr-xr-x | scripts/trend | 30 | ||||
-rw-r--r-- | scripts/worth | 2 | ||||
-rwxr-xr-x | setup.py | 22 | ||||
-rw-r--r-- | textual.cc | 597 | ||||
-rw-r--r-- | textual.h | 26 | ||||
-rwxr-xr-x | timeclock | 461 | ||||
-rw-r--r-- | timeclock.el | 1362 | ||||
-rw-r--r-- | timing.h | 54 | ||||
-rw-r--r-- | util.h | 37 | ||||
-rw-r--r-- | valexpr.cc | 1037 | ||||
-rw-r--r-- | valexpr.h | 202 | ||||
-rw-r--r-- | value.cc | 1062 | ||||
-rw-r--r-- | value.h | 395 | ||||
-rw-r--r-- | walk.cc | 1099 | ||||
-rw-r--r-- | walk.h | 580 | ||||
-rw-r--r-- | xml.cc | 220 | ||||
-rw-r--r-- | xml.h | 21 |
85 files changed, 21019 insertions, 5511 deletions
diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..272ffb40 --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +Copyright (c) 2003-2004, John Wiegley. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +- Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +- Neither the name of New Artisans LLC nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile deleted file mode 100644 index f61047cc..00000000 --- a/Makefile +++ /dev/null @@ -1,50 +0,0 @@ -CODE = amount.cc ledger.cc parse.cc reports.cc -OBJS = $(patsubst %.cc,%.o,$(CODE)) -#CXX = cc -CXX = g++ -CFLAGS = -Wall -ansi -pedantic -DFLAGS = -O3 -fomit-frame-pointer -#DFLAGS = -g -DDEBUG=1 -INCS = -I/sw/include -LIBS = -L/sw/lib -lgmpxx -lgmp -lpcre - -ifdef GNUCASH -CODE := $(CODE) gnucash.cc -CFLAGS := $(CFLAGS) -DREAD_GNUCASH=1 -INCS := $(INCS) -I/usr/include/xmltok -LIBS := $(LIBS) -lxmlparse -endif - -all: make.deps ledger ledger.info - -install: all - strip ledger - cp ledger $(HOME)/bin - -ledger: $(OBJS) - $(CXX) $(CFLAGS) $(INCS) $(DFLAGS) -o $@ $(OBJS) $(LIBS) - -ledger.info: ledger.texi - makeinfo $< - -ledger.pdf: ledger.texi - texi2pdf $< - -%.o: %.cc - $(CXX) $(CFLAGS) $(INCS) $(DFLAGS) -c -o $@ $< - -clean: - rm -f ledger *.o *.elc *~ .\#* .gdb_history README.texi - rm -f *.aux *.cp *.fn *.ky *.log *.pg *.toc *.tp *.vr - -distclean fullclean: clean - rm -f ledger.info README.html *.pdf *.elc make.deps TAGS - -rebuild: clean deps all - -deps: make.deps - -make.deps: Makefile - cc -M $(INCS) $(CODE) > $@ - -include make.deps diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 00000000..c7cf5deb --- /dev/null +++ b/Makefile.am @@ -0,0 +1,109 @@ +lib_LIBRARIES = libledger.a +libledger_a_CXXFLAGS = +libledger_a_SOURCES = \ + amount.cc \ + balance.cc \ + binary.cc \ + config.cc \ + datetime.cc \ + derive.cc \ + format.cc \ + journal.cc \ + mask.cc \ + option.cc \ + parser.cc \ + qif.cc \ + quotes.cc \ + textual.cc \ + valexpr.cc \ + value.cc \ + walk.cc +if HAVE_XMLPARSE +libledger_a_SOURCES += gnucash.cc xml.cc +endif +if HAVE_BOOST_PYTHON +libledger_a_CXXFLAGS += -DUSE_BOOST_PYTHON=1 +libledger_a_SOURCES += py_eval.cc +endif +if DEBUG +libledger_a_CXXFLAGS += -DDEBUG_LEVEL=4 +libledger_a_SOURCES += debug.cc +endif + +pkginclude_HEADERS = \ + acconf.h \ + amount.h \ + balance.h \ + binary.h \ + config.h \ + datetime.h \ + debug.h \ + derive.h \ + error.h \ + format.h \ + gnucash.h \ + journal.h \ + ledger.h \ + mask.h \ + option.h \ + parser.h \ + py_eval.h \ + pyledger.h \ + qif.h \ + quotes.h \ + textual.h \ + timing.h \ + util.h \ + valexpr.h \ + value.h \ + walk.h \ + xml.h + +###################################################################### + +bin_PROGRAMS = ledger +ledger_CXXFLAGS = +ledger_SOURCES = main.cc +if HAVE_BOOST_PYTHON +ledger_CXXFLAGS += -DUSE_BOOST_PYTHON=1 +ledger_LDADD = $(LIBOBJS) libledger.a -lboost_python -lpython$(PYTHON_VERSION) +else +ledger_LDADD = $(LIBOBJS) libledger.a +endif +if DEBUG +ledger_CXXFLAGS += -DDEBUG_LEVEL=4 +endif + +info_TEXINFOS = ledger.texi + +###################################################################### + +if HAVE_BOOST_PYTHON + +noinst_PROGRAMS = ledger.so + +if HAVE_XMLPARSE +HAVE_XMLPARSE_VALUE = true +else +HAVE_XMLPARSE_VALUE = false +endif + +ledger.so: py_eval.cc libledger.a + CFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS) -L." \ + HAVE_XMLPARSE="$(HAVE_XMLPARSE_VALUE)" \ + python setup.py build --build-lib=. + +install-exec-hook: + CFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS) -L." \ + HAVE_XMLPARSE="$(HAVE_XMLPARSE_VALUE)" \ + python setup.py install --prefix=$(prefix) + +endif + +all-clean: maintainer-clean + rm -fr *~ .*~ .\#* *.html *.info *.pdf *.a *.so *.o *.lo *.la \ + *.elc *.aux *.cp *.fn *.ky *.log *.pg *.toc *.tp *.vr *.pyc \ + .gdb_history gmon.out h out TAGS ledger valexpr .deps \ + build AUTHORS COPYING INSTALL Makefile aclocal.m4 autom4te \ + acconf.h acconf.h.in config.guess config.sub configure \ + depcomp install-sh missing stamp texinfo.tex Makefile.in diff --git a/Makefile.lt b/Makefile.lt new file mode 100644 index 00000000..937b0629 --- /dev/null +++ b/Makefile.lt @@ -0,0 +1,115 @@ +lib_LTLIBRARIES = libledger.la +libledger_la_CXXFLAGS = +libledger_la_SOURCES = \ + amount.cc \ + autoxact.cc \ + balance.cc \ + binary.cc \ + config.cc \ + datetime.cc \ + derive.cc \ + format.cc \ + journal.cc \ + mask.cc \ + option.cc \ + parser.cc \ + qif.cc \ + quotes.cc \ + textual.cc \ + valexpr.cc \ + value.cc \ + walk.cc +if HAVE_XMLPARSE +libledger_la_SOURCES += gnucash.cc xml.cc +endif +if HAVE_BOOST_PYTHON +libledger_la_CXXFLAGS += -DUSE_BOOST_PYTHON=1 +libledger_la_SOURCES += py_eval.cc +libledger_la_LIBADD = $(LIBOBJS) -lboost_python -lpython$(PYTHON_VERSION) +endif +if DEBUG +libledger_la_CXXFLAGS += -DDEBUG_LEVEL=4 +libledger_la_SOURCES += debug.cc +endif +libledger_la_LDFLAGS = -static -version-info 2:0 + +pkginclude_HEADERS = \ + acconf.h \ + amount.h \ + autoxact.h \ + balance.h \ + binary.h \ + config.h \ + datetime.h \ + debug.h \ + derive.h \ + error.h \ + format.h \ + gnucash.h \ + journal.h \ + ledger.h \ + mask.h \ + option.h \ + parser.h \ + py_eval.h \ + pyledger.h \ + qif.h \ + quotes.h \ + textual.h \ + timing.h \ + util.h \ + valexpr.h \ + value.h \ + walk.h \ + xml.h + +###################################################################### + +bin_PROGRAMS = ledger +ledger_CXXFLAGS = +ledger_SOURCES = main.cc +if HAVE_BOOST_PYTHON +ledger_CXXFLAGS += -DUSE_BOOST_PYTHON=1 +ledger_LDADD = $(LIBOBJS) libledger.la -lboost_python -lpython$(PYTHON_VERSION) +else +ledger_LDADD = $(LIBOBJS) libledger.la +endif +if DEBUG +ledger_CXXFLAGS += -DDEBUG_LEVEL=4 +endif +ledger_LDFLAGS = -static + +info_TEXINFOS = ledger.texi + +###################################################################### + +if HAVE_BOOST_PYTHON + +noinst_PROGRAMS = ledger.so + +if HAVE_XMLPARSE +HAVE_XMLPARSE_VALUE = true +else +HAVE_XMLPARSE_VALUE = false +endif + +ledger.so: py_eval.cc libledger.la + CFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS) -L. -L.libs" \ + HAVE_XMLPARSE="$(HAVE_XMLPARSE_VALUE)" \ + python setup.py build --build-lib=. + +install-exec-hook: + CFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS) -L. -L.libs" \ + HAVE_XMLPARSE="$(HAVE_XMLPARSE_VALUE)" \ + python setup.py install --prefix=$(prefix) + +endif + +all-clean: maintainer-clean + rm -fr *~ .*~ .\#* *.html *.info *.pdf *.a *.so *.o *.lo *.la \ + *.elc *.aux *.cp *.fn *.ky *.log *.pg *.toc *.tp *.vr *.pyc \ + .gdb_history gmon.out h out TAGS ledger valexpr .deps \ + .libs build AUTHORS COPYING INSTALL Makefile acconf.h \ + acconf.h.in aclocal.m4 autom4te config.guess config.sub \ + configure depcomp install-sh libtool ltconfig ltmain.sh \ + missing stamp texinfo.tex Makefile.in @@ -1,13 +1,174 @@ -1.7 + Ledger NEWS - Pricing histories are now supported, so that ledger remembers - historical prices of all commodities (if this information is - provided), and can give register reports based on past and present - market values, as well as original cost basis. +* 2.0 -1.6 +This version represents a full rewrite, while preserving much of the +original data format and command-line syntax. There are too many new +options to describe in full, but a quick list: value expressions, +complex date masks, binary caching of ledger data, many new reporting +options, a simple way to specify payee regexps, calculation and +display predicates, and two-way Python integration. Ledger also uses +autoconf now, and builds as a library plus the command-line driver. - Can now parse timeclock files. These are simple timelogs that track +** Differences from 1.7 + +- changes in option syntax: + + -d now specifies the display predicate. To give a date mask similar + to 1.7, use the -p (period) option. + + -P now generates the "by payee" report. To specify a price database + to use, use --price-db. + + -G now generates a net gain report. To print totals in a format + consumable by gnuplot, use -J. + + -l now specifies the calculation predicate. To emulate the old + usage of "-l \$100", use: -d "AT>100". + + -N is gone. Instead of "-N REGEX", use: -d "/REGEX/?T>0:T". + + -F now specifies the report format string. The old meaning of -F + now has little use. + + -S now takes a value expression as the sorting criterion. To get + the old meaning of "-S", use "-S d". + + -n now means "collapse entries in the register report". The get the + old meaning of -n in the balance report, use "-T a". + + -p now specifies the reporting period. You can convert commodities + in a report using value expressions. For example, to display hours + at $10 per hour: + -T "O>={0.01h}?{\$10.00}*O:O" + Or, to reduce totals, so that every $417 becomes 1.0 AU: + -T "O>={\$0.01}?{1.0 AU}*(O/{\$417}):O" + +- The use of "+" and "-" in ledger files to specify permanent regexps + has been removed. + +- The "-from" argument is no longer used by the "entry" command. + Simply remove it. + +** Features new to 2.0 + +- The most significant feature to be added is "value expressions". + They are used in many places to indicate what to display, sorting + order, how to calculate totals, etc. Logic and math operators are + supported, as well as simple functions. See the manual. + +- If the environment variable LEDGER_FILE (or LEDGER) is used, a + binary cache of that ledger is kept in ~/.ledger-cache (or the file + given by LEDGER_CACHE). This greatly speeds up subsequent queries. + Happens only if "-f" or "--file" is not used. + +- New 'xml' report outputs an XML version of what "register" would + have displayed. This can be used to manipulate reported data in a + more scriptable way. + + Ledger can also read as input the output from the "xml" report. If + the "xml" report did not contain balanced entries, they will be + balanced by the "<Unknown>" account. For example: + ledger reg rent + displays the same results as: + ledger xml rent | ledger -f - reg rent + +- Regexps given directly after the command name now apply only to + account names. To match on a payee, use "--" to separate the two + kinds of regexps. For example, to find a payee named "John" within + all Expenses accounts, use: + ledger register expenses -- john + + Note: This command is identical (and internally converted) to: + ledger -l "/expenses/|//john/" register + +- To include entries from another file into a specific account, use: + !account ACCOUNT + !include FILE + !end + +- Register reports now show only matching account transactions. Use + "-r" to see "related accounts" -- the account the transfer came from + or went to (This was the old behavior in 1.x, but led to confusion). + "-r" also works with balance reports, where it will total all the + transactions related to your query. + +- Automated transactions now use value expressions for the predicate. + The new syntax is: + = VALUE-EXPR + TRANSACTIONS... + + Only one VALUE-EXPR is supported (compared to multiple account + regexps before). However, since value expression allow for logic + chaining, there is no loss of functionality. Matching can also be + much more comprehensive. + +- If Boost.Python is installed (libboost_python.a), ledger can support + two-way Python integration. This feature is enabled by passing + --enable-python to the "configure" script before building. Ledger + can then be used as a module (ledger.so), as well as supporting + Python function calls directly from value expressions. See main.py + for an example of driving Ledger from Python. It implements nearly + all the functionality of the C++ driver, main.cc. + + (This feature has yet to mature, and so is being offered as a beta + feature in this release. It is mostly functional, and those curious + are welcome to play with it.) + +- New reporting options: + + "-o FILE" outputs data to FILE. If "-", output goes to stdout (the + default). + + -O shows base commodity values (this is the old behavior) + -B shows basis cost of commodities + -V shows market value of commodities + -g reports gain/loss performance of each register item + -G reports net gain/loss over time + -A reports average transaction value (arithmetic mean) + -D reports each transaction's deviation from the average + + -w uses 132 columns for the register report, rather than 80. Set + the environment variable LEDGER_WIDE for this to be the default. + + "-p INTERVAL" allows for more flexible period reporting, such as: + + monthly + every week + every 3 quarters + weekly from 12/20 + monthly in 2003 + weekly from last month until dec + + "-y DATEFMT" changes the date format used in all reports. The + default is "%Y/%m/%d". + + -Y and -W print yearly and weekly subtotals, just as -M prints + monthly subtotals. + + --dow shows cumulative totals for each day of the week. + + -P reports transactions grouped by payee + + -x reports the payee as the commodity; useful in some cases + + -j and -J replace the previous -G (gnuplot) option. -j reports the + amounts column in a way gnuplot can consume, and -J the totals + column. An example is in "scripts/report". + + "--period-sort EXPR" sorts transactions within a reporting period. + The regular -S option sorts all reported transactions. + +* 1.7 + +- Pricing histories are now supported, so that ledger remembers + historical pricing of all commodities, and can give register reports + based on past and present market values as well as the original cost + basis. See the manual for more details on the new option switches. + +* 1.6 + +- Can now parse timeclock files. These are simple timelogs that track in/out events, which can be maintained using my timeclock tool. By allowing ledger to parse these, it means that reporting can be done on them in the same way as a ledger file (the commodities is "h", @@ -16,4 +177,4 @@ hours into a dollar value via a receivable account, is now trivial. See the docs for more on how to do this. - Began keeping NEWS file. :) +- Began keeping a NEWS file. :) @@ -1,1226 +1,57 @@ -#comment -*-muse-*- -#title Ledger: Command-Line Accounting -<contents> +Welcome to Ledger, a command-line accounting program. -* Introduction +Quick start +=========== -Ledger is an accounting tool with the moxie to exist. It provides no -bells or whistles, and returns the user to the days before user -interfaces were even a twinkling in their father's CRT. +To build Ledger, you will first need these two libraries installed: -What it does do is to offer a double-entry accounting ledger with all -the flexibility and muscle of its modern day cousins, without any of -the fat. Think of it as the Bran Muffin of accounting tools. + gmp GNU multi-precision library + pcre Perl regular expression library -To use it, you need to start keeping a ledger. This is the basis of -all accounting, and if you haven't started yet, now is the time to -learn. The little booklet that comes with your checkbook is a ledger, -so we'll describe double-entry accounting in terms of that. +If you wish to read Gnucash data files, you will also need two XML +libraries, which may or may not be available in a single package (it +depends on your distribution): -A checkbook ledger records debits (subtractions, or withdrawals) and -credits (additions, or deposits) with reference to a single account: -the checking account. Where the money comes from, and where it goes -to, are described in the payee field, where you write the person or -company's name. The ultimate aim of keeping a checkbook ledger is to -know how much money is available to spend. That's really the aim of -all ledgers. + xmlparse + xmltok -What computers add is the ability to walk through these transactions, -and tell you things about your spending habits; to let you devise -budgets and get control over your spending; to squirrel away money -into virtual savings account without having to physically move money -around; etc. As you keep your ledger, you are recording information -about your life and habits, and sometimes that information can start -telling you things you aren't aware of. Such is the aim of all good -accounting tools. +Once you have determined where the headers and libraries for the above +packages are installed, run the script "configure", passing those +paths. If you installed everything under /usr/local, you can probably +just type "./configure". Otherwise, do this: -The next step up from a checkbook ledger, is a ledger that keeps track -of all your accounts, not just checking. In such a ledger, you record -not only who gets paid -- in the case of a debit -- but where the -money came from. In a checkbook ledger, its assumed that all the -money comes from your checking account. But in a general ledger, you -write transaction two-lines: The source account and target account. -*There must always be a debit from at least one account for every -credit made to another account*. This is what is meant by -"double-entry" accounting: the ledger must always balance to zero, -with an equal number of debits and credits. + ./configure CPPFLAGS=-I<INCLUDE-PATH> LDFLAGS=-L<LIBRARY-PATH> -For example, let's say you have a checking account and a brokerage -account, and you can write checks from both of them. Rather than keep -two checkbooks, you decide to use one ledger for both. In this -general ledger you need to record a payment to Pacific Bell for your -monthly phone bill. The cost is $23.00, let's say, and you want to -pay it from your checking account. In the general ledger you need to -say where the money came from, in addition to where it's going to. -The entry might look like this: +If you need to specify multiple include or library paths, then do +this: -<example> -9/29 BAL Pacific Bell $-200.00 $-200.00 - Equity:Opening Balances $200.00 -9/29 BAL Checking $100.00 $100.00 - Equity:Opening Balances $-100.00 -9/29 100 Pacific Bell $23.00 $223.00 - Checking $-23.00 $77.00 -</example> + ./configure CPPFLAGS="-I<PATH1> -I<PATH2>" LDFLAGS="-L<PATH1> -L<PATH2>" -The first line shows a credit (or payment) to Pacific Bell for $23.00. -Because there is no "balance" in a general ledger -- it's always zero --- we write in the total balance of all payments to "Pacific Bell", -which now is $223.00 (previously the balance was $200.00). This is -done by looking at the last entry for "Pacific Bell" in the ledger, -adding $23.00 to that amount, and writing the total in the balance -column. And the money came from "Checking" -- a debit (or withdrawal) -of $23.00 -- which leaves the ending balance in "Checking" at $77.00. -This is a very manual procedure; but that's where computers come in... +Once configure is done running, just type: -The transaction must balance to $0: $23 went to Pacific Bell, $23 came -from Checking. There is nothing left over to be accounted for, since -the money has simply moved from one account to another. This is the -basis of double-entry accounting: that money never pops in or out of -existence; it is always a transaction from one account to another. + make install -Keeping a general ledger is the same as keeping two separate ledgers: -One for Pacific Bell and one for Checking. In that case, each time a -credit is written into one, you write a corresponding debit into the -other. This makes it easier to write in a "running balance", since -you don't have to look back at the last time the account was -referenced -- but it also means having a lot of ledger books, if you -deal with multiple accounts. -Enter the beauty of computerized accounting. The purpose of the -Ledger program is to make general ledger accounting simple, by keeping -track of the balances for you. Your only job is to enter the -credit/debit transactions. If a transaction does not balance, Ledger -will display an error and indicate which transaction is wrong.[1] +Mailing List and IRC +==================== -In summary, there are two aspects of Ledger use: Updating the ledger -data file, and using the Ledger tool to view the summarized result of -your entries. +If you need help on how to use Ledger, or run into problems, you can +just the Ledger mailing list at the following Web address: -And just for the sake of example -- as a starting point for those who -want to dive in head-first -- here are the ledger entries from above, -formatting as the ledger program wishes to see them: + https://lists.sourceforge.net/lists/listinfo/ledger-discuss -<example> -; Set the year for subsequent entries to 2004 -Y 2004 +You can also find help at the #ledger channel on the IRC server +irc.freenode.net. -9/29 Pacific Bell - Payable:Pacific Bell $-200.00 - Equity:Opening Balances -9/29 Checking - Accounts:Checking $100.00 - Equity:Opening Balances +Building Ledger as a Python Module +================================== -9/29 Pacific Bell - Payable:Pacific Bell $23.00 - Accounts:Checking -</example> +If you have Python 2.2 or higher installed, and Boost.Python, then +Ledger can also be built as a Python module, if --enable-python is +passed to the configure script. This means you can interact with your +Ledger data from Python, making it easy to write custom reports. -The account balances and registers in this file, if saved as -=ledger.dat=, could be reported using: - -<example> -$ ledger -f ledger.dat balance -$ ledger -f ledger.dat register checking -$ ledger -f ledger.dat register bell -</example> - -** Building the program - -Ledger is written in ANSI C++, and should compile on any platform. It -depends only on the GNU multiprecision integer library (libgmp), and -the Perl regular expression library (libpcre). It was developed using -GNU make and gcc 3.3. - -To build and install once you have these libraries on your system, -enter these commands: - -<example> -make -cp ledger /usr/local/bin -</example> - -Note that when building GNUmp, make sure to pass the =--enable-cxx= -flag to configure, otherwise it will not build **libgmpxx.a**. And in -case it is not already on your system, **xmlparse.h** is part of the -libxmltok package, and not expat. - -* Keeping a ledger - -The most important part of accounting is keeping a good ledger. If -you have a good ledger, tools can be written to work whatever -mathematically tricks you need to better understand your spending -patterns. Without a good ledger, no tool, however smart, can help -you. - -The Ledger program aims at making ledger entry as simple as possible. -Since it is a command-line tool, it does not provide a user interface -for keeping a ledger. If you like, you may use Gnucash to maintain -your ledger, in which case the Ledger program will read Gnucash's data -files directly. In that case, read the Gnucash manual now, and skip -to the next chapter. - -If you are not using Gnucash, but a text editor to maintain your -ledger, read on. Ledger has been designed to make data entry as -simple as possible, by keeping the ledger format easy, and also by -automagically determining as much information as possible based on the -nature of your entries. - -For example, you do not need to tell Ledger about the accounts you -use. Any time Ledger sees a debit or a credit to an account it knows -nothing about, it will create it. If you use a commodity that is new -to Ledger, it will create that commodity, and determine its display -characteristics (placement of the symbol before or after the amount, -display precision, etc) based on how you used the commodity in the -transaction. - -Here is the Pacific Bell example from above, given as a Ledger -transaction: - -<example> -9/29 (100) Pacific Bell - Expenses:Utilities:Telephone $23.00 - Assets:Checking $-23.00 -</example> - -As you can see, it is very similar to what would be written on paper, -minus the computed balance totals, and adding in account names that -work better with Ledger's scheme of things. In fact, since Ledger is -smart about many things, you don't need to specify the balanced -amount, if it is the same as the first line: - -<example> -9/29 (100) Pacific Bell - Expenses:Utilities:Telephone $23.00 - Assets:Checking -</example> - -For this entry, Ledger will figure out that $-23.00 must come from -"Assets:Checking" in order to balance the entry. - -** Credits and Debits - -Credit and debit are simple enough terms in themselves, but the usages -of the modern world have made them very hard to puzzle out. - -Basically, a credit means you add something to an account, and a debit -means you take away. A debit card is correctly name: From your point -of view, it debits your checking account every time you use it. - -The credit card is strangely named, because you have to look at it -from the merchant's point of view: Every time you use it, it credit's -*his* account right away. This was a giant leap from the days of cash -and checks, when the only other way to supply immediate credit was by -a wire transfer. But a credit card does not credit you anything at -all. In fact, from your point of view, it should be called a -liability card, since it increases your liability to the issuing bank -every time you use it. - -In Ledger, credits and debits are given as they are, which means that -sometimes you will see a minus sign where you don't expect one. For -example, when you get paid, in order to credit your bank account, you -need to debit an income account: - -<example> -9/29 My Employer - Assets:Checking $500.00 - Income:Salary $-500.00 -</example> - -But wait, you say, why is the Income a negative figure? And when you -look at the balance totals for your ledger, you will certainly be -surprised to see Expenses as a positive figure, and Income as a -negative figure. Isn't that the opposite of how it should look? - -It may take getting used to, but to properly use a general ledger you -will need to think in terms of correct debits and credits. Rather -than Ledger "fixing" the minus signs, let's understand why they are -there. - -When you earn money, the money has to come from somewhere. Let's call -that somewhere "society". In order for society to give you an income, -you must take money away from society (debit) in order to put it into -your bank (credit). When you then spend that money, it leaves your -bank account (debit) and goes back to society (credit). This is why -Income will appear negative -- it reflects the money you have drawn -from society -- and why Expenses will be positive -- it is the amount -you've given back. These credits and debits will always cancel each -other out in the end, because you don't have the ability to create new -money: It must always come from somewhere, and in the end must always -leave. This is the beginning of economy, after which the explanation -gets terribly difficult. - -Based on that explanation, here's another way to look at your balance -report: Every negative figure means that that account or person or -place has less money now than when you started your ledger; and every -positive figure means that that account or person or place has more -money now that when you started your ledger. Make sense? - -Also, credit cards will have a negative value, because you are -spending *from* them (debit) in order pay someone else (credit). They -are called credit cards because you are able to instantly credit that -other person, by simply waving a card. - -** Commodities and Currencies - -Ledger makes no assumptions about the commodities you use; it only -requires that you specify a commodity. The commodity may be any -non-numeric string that does not contain a period, comma, forward -slash or at-sign. It may appear before or after the amount, although -it is assumed that symbols appearing before the amount refer to -currencies, while non-joined symbols appearing after the amount refer -to commodities. Here are some valid currency and commodity -specifiers: - -<example> -$20.00 ; currency: twenty US dollars -USD 20 ; currency: the same -40 AAPL ; commodity: 40 shares of Apple stock -MD 60 ; currency: 60 Deutsch Mark -£50 ; currency: 50 British pounds -50e ; currency: 50 Euros (use symbol) -</example> - -Ledger will examine the first use of any commodity to determine how -that commodity should be printed on reports. It pays attention to -whether the name of commodity was separated from the amount, whether -it came before or after, the precision used in specifying the amount, -whether thousand marks were used, etc. This is done so that printing -the commodity looks the same as the way you use it. - -An account may contain multiple commodities, in which case it will -have separate totals for each. For example, if your brokerage account -contains both cash, gold, and several stock quantities, the balance -might look like: - -<example> - $200.00 -100.00 AU - AAPL 40 - BORL 100 - FEQTX 50 Assets:Brokerage -</example> - -This balance report shows how much of each commodity is in your -brokerage account. - -Sometimes, you will want to know the current street value of your -balance, and not the commodity totals. For this to happen, you must -specify what the current price is for each commodity. The price can -be in any commodity, in which case the balance will be computed in -terms of that commodity. The usual way to specify prices is with a -file of price settings, which might look like this: - -<example> -AU=$357.00 -AAPL=$37 -BORL=$19 -FEQTX=$32 -</example> - -Specify the prices file using the =-p= option: - -<example> -ledger -p prices.db balance brokerage -</example> - -Now the balance for your brokerage account will be given in US -dollars, since the prices database has specified conversion factors -from each commodity into dollars: - -<example> -$40880.00 Assets:Brokerage -</example> - -You can convert from any commodity to any other commodity. Let's say -you had $5000 in your checking account, and for whatever reason you -wanted to know many ounces of gold that would buy. If gold is -currently $357 per ounce, then each dollar is worth 1/357 AU: - -<example> -ledger -p "$=0.00280112 AU" balance checking -</example> - -<example> -14.01 AU Assets:Checking -</example> - -$5000 would buy 14 ounces of gold, which becomes the new display -commodity since a conversion factor was provided. - -Commodities conversions can also be chained, up to a depth of 10. -Here is a sample prices database that uses chaining: - -<example> -AAPL=$15 -$=0.00280112 AU -AU=300 Euro -Euro=MD 0.75 -</example> - -This is a roundabout way of reporting AAPL shares in their Deutsch -Mark equivalent. - -*** Commodity price histories - -Whenever a commodity is purchased using a different commodity (such as -a share of common stock using dollars), it establishes a price for -that commodity on that day. It is also possible, by recording price -details in a ledger file, to specify other prices for commodities at -any given time. Such price entries might look like those below: - -<example> -P 2004/06/21 02:17:58 TWCUX $27.76 -P 2004/06/21 02:17:59 AGTHX $25.41 -P 2004/06/21 02:18:00 OPTFX $39.31 -P 2004/06/21 02:18:01 FEQTX $22.49 -P 2004/06/21 02:18:02 AAPL $32.91 -</example> - -By default, ledger will not consider commodity prices when generating -its various reports. It will always report balances in terms of the -commodity total, rather than the current value of those commodities. -To enable pricing reports, three options are possible: - -**-P FILE** :: - With this option, or if the environment variable =PRICE_HIST= is - set, pricing information obtained from the Internet will be kept - in this file. Also, this file will be read after all other ledger - files are read, so that full history information is available for - reports. - -**-T** :: - Report commodity totals only, not their market value or basis cost. - -**-V** :: - Report commodity values in terms of their last known market price. - -**-B** :: - Report commodities in terms of their "basis cost", or what they cost - at time of purchase. Thus, totals in the register and balance - report reflect the total amount spent. - -**-A** :: - Report commodities in terms of their net gain, which is: the market - value minus the cost basis. A balance report using this option - shows very quickly the performance of investments. - -**-Q** :: - When needed (see the =-L= option) pricing quotes are obtained by - calling the script =getquote= (a sample Perl script is provided, but - the interface is kept simple so replacements may be made). - -**-L MINS** :: - When using the =-Q= flag, new quotes are obtained only if current - pricing data is older than MINS minutes. The default is one day, - or 1440 minutes. - -**-p ARG** :: - If a string, such as "COMM=$1.20", the commodity COMM will be - reported only in terms of the conversion factor, which supersedes - all other pricing histories for that commodity. This can be used to - perform arbitrary value substitutions. For example, to report the - value of your dollars in terms of the ounces of gold they would buy, - use: -p "$=0.00280112 AU" (or whatever the current exchange rate - is). - -Note that the =-B=, =-T=, =-V=, and =-A= are mutually exclusive. - -** Accounts and Inventories - -Since Ledger's accounts and commodity system is so flexible, you can -have accounts that don't really exist, and use commodities that no one -else recognizes. For example, let's say you are buying and selling -various items in EverQuest, and want to keep track of them using a -ledger. Just add items of whatever quantity you wish into your -EverQuest account: - -<example> -9/29 Get some stuff at the Inn - Places:Black's Tavern -3 Apples - Places:Black's Tavern -5 Steaks - EverQuest:Inventory -</example> - -Now your EverQuest:Inventory has 3 apples and 5 steaks in it. The -amounts are negative, because you are taking *from* Black's Tavern in -order to credit your Inventory account. Note that you don't have to -use "Places:Black's Tavern" as the source account. You could use -"EverQuest:System" to represent the fact that you acquired them -online. The only purpose for choosing one kind of source account over -another is for generate more informative reports later on. The more -you know, the better analysis you can perform. - -If you later sell some of these items to another player, the entry -would look like: - -<example> -10/2 Strum Brightblade - EverQuest:Inventory -2 Steaks - EverQuest:Inventory 15 Gold -</example> - -Now you've turned 2 steaks into 15 gold, courtesy of your customer, -Strum Brightblade. - -** Understanding Equity - -The most confusing entry in any ledger will be your equity account -- -because starting balances can't come out of nowhere. - -When you first start your ledger, you will likely already have money -in some of your accounts. Let's say there's $100 in your checking -account; then add an entry to your ledger to reflect this amount. -Where will money come from? The answer: your equity. - -<example> -10/2 Opening Balance - Assets:Checking $100.00 - Equity:Opening Balances -</example> - -But what is equity? You may have heard of equity when people talked -about house mortgages, as "the part of the house that you own". -Basically, equity is like the value of something. If you own a car -worth $5000, then you have $5000 in equity in that car. In order to -turn that car (a commodity) into a cash flow, or a credit to your bank -account, you will have to debit the equity by selling it. - -When you start a ledger, you are probably already worth something. -Your net worth is your current equity. By transferring the money in -the ledger from your equity to your bank accounts, you are crediting -the ledger account based on your prior equity value. That is why, -when you look at the balance report, you will see a large negative -number for Equity that never changes: Because that is what you were -worth (what you debited from yourself in order to start the ledger) -before the money started moving around. If the total positive value -of your assets is greater than the absolute value of your starting -equity, it means you are making money. - -Clear as mud? Keep thinking about it. Until you figure it out, put -"-- -Equity" at the end of your balance command, to remove the -confusing figure from the totals. - -** Dealing with cash - -Something that stops many people from keeping a ledger at all is the -insanity of tracking cash expenses. They rarely generate a receipt, -and there are often a lot of small transactions, rather than a few -large ones, as with checks. - -The answer is: don't bother. Move your spending to a debit card, but -in general ignore cash. Once you withdraw it from the ATM, mark it as -already spent to an "Expenses:Cash" category: - -<example> -2004/03/15 ATM - Expenses:Cash $100.00 - Assets:Checking -</example> - -If at some point you make a large cash expense that you want to track, -just "move" the amount of the expense from "Expenses:Cash" into the -target account: - -<example> -2004/03/20 Somebody - Expenses:Food $65.00 - Expenses:Cash -</example> - -This way, you can still track large cash expenses, while ignoring all -of the smaller ones. - -** Archiving previous years - -After a while, your ledger can get to be pretty large. While this -will not slow down the ledger program much -- it's designed to process -ledger files very quickly -- things can start to feel "messy"; and -it's a universal complaint that when finances feel messy, people avoid -them. - -Thus, archiving the data from previous years into their own files can -offer a sense of completion, and freedom from the past. But how to -best accomplish this with the ledger program? There are two commands -that make it very simple: "print", and "equity". - -Let's take an example file, with data ranging from year 2000 until -2004. We want to archive years 2000 and 2001 to their own file, -leaving just 2003 and 2004 in the current file. So, use "print" to -output all the earlier entries to a file called =ledger-old.dat=. -(Keeping in mind that the ending date is not inclusive, which is why -2002 is mentioned in the following command): - -<example> -$ ledger -f ledger.dat -b 2000/1/1 -e 2002/1/1 print \ - > ledger-old.dat -</example> - -To delete older data from the current ledger file, use "print" again, -this time specifying year 2002 as the starting date: - -<example> -$ ledger -f ledger.dat -b 2002/1/1 print > x -$ mv x ledger.dat -</example> - -However, now the current file contains *only* transactions from 2002 -onward, which will not yield accurate present-day balances, because -the net income from previous years is no longer being tallied. To -compensate for this, we must append an equity report for the old -ledger at the beginning of the new one: - -<example> -$ ledger -f ledger-old.dat equity > equity.dat -$ cat equity.dat ledger.dat > x -$ mv x ledger.dat -$ rm equity.dat -</example> - -Now the balances reported from =ledger.dat= are identical to what they -were before the data was split. - -How often should you split your ledger? You never need to, if you -don't want to. Even eighty years of data will not slow down ledger -much -- and that's just using present day hardware! Or, you can keep -the previous and current year in one file, and each year before that -in its own file. It's really up to you, and how you want to organize -your finances. For those who also keep an accurate paper trail, it -might be useful to archive the older years to their own files, then -burn those files to a CD to keep with the paper records -- along with -any electronic statements received during the year. In the arena of -organization, just keep in mind this maxim: Do whatever keeps you -doing it. - -** Virtual transactions - -A virtual transaction is when you, in your mind, see money as moving -to a certain place, when in reality that money has not moved at all. -There are several scenarios in which this type of tracking comes in -handy, and each of them will be discussed in detail. - -To enter a virtual transaction, surround the account name in -parentheses. This form of usage does not need to balance. However, -if you want to ensure the virtual transaction balances with other -virtual transactions in the same entry, use square brackets. For -example: - -<example> -10/2 Paycheck - Assets:Checking $1000.00 - Income:Salary $-1000.00 - (Debt:Alimony) $200.00 -</example> - -In this example, after receiving a paycheck an alimony debt is -increased -- even though no money has moved around yet. - -<example> -10/2 Paycheck - Assets:Checking $1000.00 - Income:Salary $-1000.00 - [Savings:Trip] $200.00 - [Assets:Checking] $-200.00 -</example> - -In this example, $200 has been deducted from checking toward savings -for a trip. It will appear as though the money has been moved from -the account into "Savings:Trip", although no money has actually moved -anywhere. - -When balances are displayed, virtual transactions will be factored in. -To view balances without any virtual balances factored in, using the -"-R" flag, for "Reality". - -Write about: Saving for a Special Occasion; Keeping a Budget; Tracking -Allocated Funds. - -** Automated transactions - -As a Bahá'í, I need to compute Huqúqu'lláh whenever I acquire assets. -The exact details of this are a bit complex, so if you have further -interest, please consult the Web. - -For any fellow Bahá'ís out there who want to track Huqúqu'lláh, the -Ledger tool makes this extremely easy. Just set up the following -automated transaction at the top of your ledger file: - -<example> -; These entries will compute Huqúqu'lláh based on the -; contents of the ledger. - -= ^Income: -= ^Expenses:Rent$ -= ^Expenses:Furnishings -= ^Expenses:Business -= ^Expenses:Taxes -= ^Expenses:Insurance - (Liabilities:Huqúqu'lláh) 0.19 -</example> - -This automated transaction works by looking at each transaction -appearing afterward in the ledger file. If any match the account -regexps, occurring after the equal signs above, 19% of the value of -that transaction is applied to the "Liabilities:Huqúqu'lláh" account. -So if $1000 is earned through Income:Salary, which is seen as a debit -from Income, a debit of $190 is applied to "Liabilities:Huqúqu'lláh"; -if $1000 is spent on Rent -- seen as a credit to the Expense account --- a credit of $190 is applied to Huqúqu'lláh. The ultimate balance -of Huqúqu'lláh reflects how much must be paid to that account in order -to balance it to zero. - -When you're ready to pay, just write a check directly to the account -"Liabilities:Huqúqu'lláh": - -<example> -2003/01/01 (101) Baha'i Huqúqu'lláh Trust - Liabilities:Huqúqu'lláh $1,000.00 - Assets:Checking -</example> - -That's it. To see how much Huqúq is currently owed based on your -ledger entries, use: - -<example> -ledger balance Liabilities:Huqúq -</example> - - -** Using Emacs to Keep Your Ledger - -In the Ledger tarball is an Emacs module, =ledger.el=. This module -makes the process of keeping a text ledger much easier for Emacs -users. I recommend putting this at the top of your ledger file: - -<example> -; -*-ledger-*- -</example> - -And this in your =.emacs= file, after copying =ledger.el= to your -site-lisp directory: - -<example> -(load "ledger") -</example> - -Now when you edit your ledger file, it will be in =ledger-mode=. -=ledger-mode= adds the following commands: - -C-c C-a :: - For quickly adding new entries based on the form of older ones - (see previous section). - -C-c C-c :: - Toggles the "cleared" flag of the transaction under point. - -C-c C-r :: - Reconciles an account by displaying the transactions in another - buffer, where simply hitting the spacebar will toggle the cleared - flag of the transaction in the ledger. It also displays the current - cleared balance for the account in the modeline. - -** Using GnuCash to Keep Your Ledger - -The Ledger tool is fast and simple, but it offers no custom method for -actually editing the ledger. It assumes you know how to use a text -editor, and like doing so. Perhaps an Emacs mode will appear someday -soon to make editing Ledger's data files much easier. - -Until then, you are free to use GnuCash to maintain your ledger, and -the Ledger program for querying and reporting on the contents -of that ledger. It takes a little longer to parse the XML data format -that GnuCash uses, but the end result is identical. - -Then again, why would anyone use a Gnome-centric, 35 megabyte behemoth -to edit their data, and a 65 kilobyte binary to query it... - -** Using timeclock to record billable time - -The timeclock tool makes it easy to track time events, like clocking -into and out of a particular job. These events accumulate in a -timelog file. - -Each in/out event may have an optional description. If the "in" -description is a ledger account name, these in/out pairs may be viewed -as virtual transactions, adding time commodities (hours) to that -account. - -For example, the command-line version of the timeclock tool (which is -written in Python) could be used to begin a timelog file like: - -<example> -$ export TIMELOG=$HOME/.timelog -$ ti ClientOne category -$ sleep 10 -$ to waited for ten seconds -</example> - -The **.timelog** file now contains: - -<example> -i 2004/10/06 15:21:00 ClientOne category -o 2004/10/06 15:21:10 waited for ten seconds -</example> - -Ledger can parse this directly, as if it had seen the following ledger -entry: - -<example> -2004/10/06 category - (ClientOne) 0.00277h -</example> - -In other words, the timelog event pair is seen as adding 0.00277h (ten -seconds) worth of time to the ClientOne account. This would be -considered billable time, which later could be invoiced and credited -to accounts receivable: - -<example> -2004/11/01 (INV#1) ClientOne, Inc. - Receivable:ClientOne $0.10 - ClientOne -0.00277h @ $35.00 -</example> - -The above transaction converts the clocked time into an invoice for -the time spent, at an hourly rate of $35. Once the invoice is paid, -the money is deposited from the receivable account into a checking -account: - -<example> -2004/12/01 ClientOne, Inc. - Assets:Checking $0.10 - Receivable:ClientOne -</example> - -And now the time spent has been turned into hard cash in the checking -account. - -The advantage to using timeclock and invoicing to bill time is that -you will always know, by looking at the balance report, exactly how -much unbilled and unpaid time you've spent working for any particular -client. - -I like to =!include= my timelog at the top of my company's accounting -ledger, with the attached prefix "Billable": - -<example> -; -*-ledger-*- - -; This is the ledger file for my company. But first, include the -; timelog data, entering all of the time events within the umbrella -; account "Billable". - -!include /home/johnw/.timelog Billable - -; Here follows this fiscal year's transactions for the company. - -2004/11/01 (INV#1) ClientOne, Inc. - Receivable:ClientOne $0.10 - Billable:ClientOne -0.00277h @ $35.00 - -2004/12/01 ClientOne, Inc. - Assets:Checking $0.10 - Receivable:ClientOne -</example> - -* Running Ledger - -Now that you have an orderly and well-organized general ledger, it's -time to start generating some orderly and well-organized reports. -This is where the Ledger tool comes in. With it, you can balance your -checkbook, see where your money is going, tell whether you've made a -profit this year, and even compute the present day value of your -retirement accounts. And all with the simplest of interfaces: the -command-line. - -The most often used command will be the "balance" command: - -<example> -export LEDGER=/home/johnw/doc/ledger.dat -ledger balance -</example> - -Here I've set my Ledger environment variable to point to where my -ledger file is hiding. Thereafter, I needn't specify it again. - -The balance command prints out the summarized balances of all my -top-level accounts, excluding sub-accounts. In order to see the -balances for a specific account, just specify a regular expression -after the balance command: - -<example> -ledger balance expenses:food -</example> - -This will show all the money that's been spent on food, since the -beginning of the ledger. For food spending just this month -(September), use: - -<example> -ledger -d sep balance expenses:food -</example> - -Or maybe I want to see all of my assets, in which case the -s (show -sub-accounts) option comes in handy: - -<example> -ledger balance -s -</example> - -To exclude a particular account, use a regular expression with a -leading minus sign. The following will show all expenses, but without -food spending: - -<example> -ledger balance expenses -food -</example> - -If you want to show all accounts but for one account, remember to use -"--" to separate the exclusion pattern from the options list: - -<example> -ledger balance -- -equity -</example> - -** File format - -The ledger file format is quite simple, but supports many options. -These are summarized here. - -The initial character of each line determines what that line means, -and how it should be parsed. The possibilities are: - -NUMBER :: - A line starting with a number denotes a regular ledger entry. It - may be followed by any number of lines that beginning whitespace, to - denote account transactions. The format of the header line is: -<example> -DATE [*] [(CODE)] DESC -</example> - -+ :: - If a line begins with plus, it denotes an inclusion regexp that - will always be considered, as if it had been specified by the user - at the end of their command-line. - -**-** :: - If a line begins with minus, it denotes an exclusion regexp that - will always be considered, as if it had been specified by the user - at the end of their command-line. - -**<verbatim>=</verbatim>** :: - If a line begins with equals, it denotes an automated transaction. - The next item on the line must be a regular expression. Any number - of such lines may appear, with no intervening whitespace. - Following this block of lines can be a list of account transactions - preceded by whitespace. - -!WORD :: - A line beginning with an exclamation mark denotes a command - directive. It must be immediately followed by a word specifying - which directories. At the moment, only =!include= is supported, for - including the content of other ledger files into the current one. - -whitespace :: - A line beginning with whitespace, which is not part of a regular or - automated transaction, is ignored. - -; :: - If a line begins with semicolon it is ignored. This is the - preferred method of entering comments. - -Y NUM :: - If a line begins with a capital Y, it denotes the year to be used - for all subsequent entries that specify a date, whatever their type. - This sets the "default year", which ordinarily is the current year - at the time the program is run. Useful at the beginning of a file - to specify the file's year. - -P DATE SYMBOL PRICE :: - Capital P specifies a historical price for a commodity. Any such - number of entries are allowed. These are usually found in a pricing - history file (see the =-Q= option). - -C SYMBOL PRICE :: - Capital C specifies a conversion price for a commodity. This has - no reference to time, and always takes precedence over any - historical price (even very current prices). - -N SYMBOL :: - Capital N indicates that no implicit price conversions should be - obtained for the given symbol. This means that no quotes will ever - be downloaded for that symbol. Useful for a home currency, such as - the dollar ($). Be aware that these pricing options will set the - default reporting characteristics for a commodity. Thus it is - recommended that pricing options occur only after all regular ledger - entries have been parsed. - -i DATE TIME ACCOUNT [DESC] :: - Lowercase (and capital) i indicate an time-in event. This will - start accumulating hours in the account specified. Usually these - entries are created in a timelog file by the timeclock program, - which is distributed with ledger. There must be two spaces between - the account name, and the optional description, if one is used. - -o DATE TIME ACCOUNT [DESC] :: - Lowercase (and capital) o indicate an time-out event. This will - accumulate hours in the account specified. Usually these entries - are created in a timelog file by the timeclock program, which is - distributed with ledger. There must be two spaces between the - account name, and the optional description, if one is used. - -b, h :: - Entries beginning with lowercase b and h are ignored. These are - special entries used by timeclock, but ignored by ledger. - -** Command summary - -*** balance - -The "balance" command reports the current balance of any account. -This command accepts a list of optional regexps, which will confine -the balance report to only matching accounts. By default, the -balances for all accounts will be printed. If an account contains -multiple types of commodities, each commodity's total is separately -reported. - -*** register - -The "register" command displays all the transactions occurring in a -single account, line by line. The account regexp must be specified as -the only argument to this command. If any regexps occur after the -required account name, the register will contain only those -transactions that match. Very useful for hunting down a particular -transaction. - -The output from "register" is very close to what a typical checkbook, -or single account ledger, would look like. It also shows a running -balance. The final running balance of any register should always be -the same as the current balance of that account. - -*** print - -The "print" command prints out ledger entries just as they appear in -the original ledger. They will be properly formatted, and output in -the most economic form possible. The "print" command also takes a -list of optional regexps, which will cause only those transactions -which match in some way to be printed. - -The "print" command is a handy way to clean up a ledger file whose -formatting has gotten out of hand. - -*** equity - -Equity transactions are used to establish the starting value of an -account. You might think of equity as the "ether" from which initial -balances appear. - -*** price - -This commands displays the last known current price for a given -commodity, using the specified end date for the cutoff (default is the -present moment). It takes a list of regexps, which can match the -commodities used in the ledger file. This command is helpful to -quickly seeing the last current price for a specific commodity, or all -commodities referenced by a ledger. - -*** entry - -The three most laborious tasks of keeping a ledger are: adding new -entries, reconciling accounts, and generating reports. To address the -first of these, there is a sub-command to ledger called "entry". It -works on the principle that 80% of all transactions are variants of -earlier transactions. Here's how it works: - -Let's say you have an old transaction of the following form: - -<example> -2004/03/15 * Viva Italiano - Expenses:Food $12.45 - Expenses:Tips $2.55 - Liabilities:MasterCard $-15.00 -</example> - -Now it's 2004/4/9, and you've just eating at Viva Italiano again. The -exact amounts are different, but the overall form is the same. With -the "entry" command you can type: - -<example> -ledger entry 2004/4/9 viva food 11.00 tips 2.50 -</example> - -This will produce the following output: - -<example> -2004/04/09 Viva Italiano - Expenses:Food $11.00 - Expenses:Tips $2.50 - Liabilities:MasterCard $-13.50 -</example> - -This works by finding a transaction that matches the regexp "viva", -and then assuming that any accounts or amounts you specify will be the -same as that earlier transaction. If Ledger does not succeed in -generating a new entry for you, it will print an error and set the -exit code to 1. - -There is a shell script in the distribution called "entry", which -simplifies the task of adding a new entry to your ledger, and then -launches =vi= to let you confirm that the entry looks appropriate. - -** Option summary - -*** Basic options - -**-v** :: - Display the version of ledger being used. - -**-h** :: - Print out quick help on the various options and commands. - -**-f FILE[<verbatim>=</verbatim>ACCOUNT]** :: - Read ledger entries from FILE. This takes precedence over the - environment variable LEDGER. If "<verbatim>=</verbatim>ACCOUNT" is - appended to the filename, then all of the entries are seen as if the - transactions accounts were prefixed by "ACCOUNT:". There may be - multiple occurrences of the =-f= option. - -**-i FILE** :: - Read in the list of patterns to include/exclude from FILE. - Ordinarily, these are specified as arguments after the command. - -*** Filtering options - -**-b DATE** :: - Only consider entries occuring on or after the given date. - -**-e DATE** :: - Only consider entries occuring before the given date. The date is - not inclusive, so any entries occurring on that date will not be - used. - -**-c** :: - Only consider entries occurring on or before the current date. - -**-d DATE** :: - Only consider entries fitting the given date mask. DATE in this - case may be the name of a month, or a year, or a year and month, - such as "2004/05". It's a shorthand for having to specify -b and -e - together. - -**-C** :: - Only consider entries whose cleared flag has been set. The default - is to consider both. - -**-U** :: - Show only uncleared transactions. The default is to consider both. - -**-l AMT** :: - Limit balance reports to those which are greater than AMT. - -**-N REGEXP** :: - If an account matches REGEXP, only display it in the balance report - if its total is negative. Useful to avoid seeing credit in accounts - where one cannot spend that credit, and it will soon become negative - anyway (such as credit cards). - -**-R** :: - Ignore all virtual transactions, and report only the real balance - for each account. - -*** Output formatting options - -**-n** :: - Do not show subtotals in the balance report, or split transactions - in the register report. - -**-s** :: - If an account has children, show them in the balance report. - -**-S** :: - Sort the ledger after reading it. This may affect "register" and - "print" output. - -**-E** :: - Also show empty accounts in the balance totals report. - -**-F** :: - Print full account names in all cases, such as "Assets:Checking" - instead of just "Checking". Only used current by the "balance" - command. - -**-M** :: - When used with the "register" command, causes only monthly subtotals - to appear. This can be useful for looking at spending patterns. - TODO: Accept an argument which specifies the period to use. - -**-G** :: - Modifies the output generated by -M to be friendly to programs like - Gnuplot. It strips away the commodity label, and outputs only two - columns: the date and the amount. - -*** Commodity reporting options - -**-P FILE** :: - With this option, or if the environment variable =PRICE_HIST= is - set, pricing information obtained from the Internet will be kept - in this file. Also, this file will be read after all other ledger - files are read, so that full history information is available for - reports. - -**-T** :: - Report commodity totals only, not their market value or basis cost. - -**-V** :: - Report commodity values in terms of their last known market price. - -**-B** :: - Report commodities in terms of their "basis cost", or what they cost - at time of purchase. Thus, totals in the register and balance - report reflect the total amount spent. - -**-A** :: - Report commodities in terms of their net gain, which is: the market - value minus the cost basis. A balance report using this option - shows very quickly the performance of investments. - -**-Q** :: - When needed (see the =-L= option) pricing quotes are obtained by - calling the script =getquote= (a sample Perl script is provided, but - the interface is kept simple so replacements may be made). - -**-L MINS** :: - When using the =-Q= flag, new quotes are obtained only if current - pricing data is older than MINS minutes. The default is one day, - or 1440 minutes. - -**-p ARG** :: - If a string, such as "COMM=$1.20", the commodity COMM will be - reported only in terms of the conversion factor, which supersedes - all other pricing histories for that commodity. This can be used to - perform arbitrary value substitutions. For example, to report the - value of your dollars in terms of the ounces of gold they would buy, - use: -p "$=0.00280112 AU" (or whatever the current exchange rate - is). - -** Environment variables - -=LEDGER= :: - A colon-separated list of files to be parsed whenever ledger is run. - Easier than typing =-f= all the time. - -=PRICE_HIST= :: - The ledger file used to hold pricing data. =~/.pricedb= would be a - good choice. - -=PRICE_EXP= :: - The number of minutes before pricing data becomes out-of-date. The - default is one day. Use =-L= to temporarily decrease or increase - the value. - -Footnotes: -[1] In some special cases, it will automatically balance the entry - for you. +This feature is mostly undocumented in version 2.0, although main.py +is offered as a working example. @@ -0,0 +1,38 @@ +#!/bin/sh + +touch AUTHORS COPYING + +aclocal +autoheader +if [ "$1" = "--dist" ]; then + automake -acfi +else + automake -acf +fi +autoconf + +INCDIRS="-I/sw/include -I/usr/include/httpd/xml -I/sw/include/python2.3" +INCDIRS="$INCDIRS -Wno-long-double" +LIBDIRS="-L/sw/lib -L/sw/lib/python2.3/config" + +if [ "$1" = "--debug" ]; then + ./configure CPPFLAGS="$INCDIRS" LDFLAGS="$LIBDIRS" CXXFLAGS="-g" \ + --enable-debug +elif [ "$1" = "--debug-python" ]; then + ./configure CPPFLAGS="$INCDIRS" LDFLAGS="$LIBDIRS" CXXFLAGS="-g" \ + --enable-debug --enable-python +elif [ "$1" = "--opt" ]; then + ./configure CPPFLAGS="$INCDIRS" LDFLAGS="$LIBDIRS" \ + CXXFLAGS="-fomit-frame-pointer -fastf -mcpu=7450 -fPIC" +elif [ "$1" = "--flat-opt" ]; then + ./configure CPPFLAGS="$INCDIRS" LDFLAGS="$LIBDIRS" \ + CXXFLAGS="-fomit-frame-pointer -fastf -mcpu=7450" +elif [ "$1" = "--safe-opt" ]; then + ./configure CPPFLAGS="$INCDIRS" LDFLAGS="$LIBDIRS" \ + CXXFLAGS="-fomit-frame-pointer -fastf -mcpu=7450 -DDEBUG_LEVEL=1" \ + --enable-python +elif [ "$1" = "--perf" ]; then + ./configure CPPFLAGS="$INCDIRS" LDFLAGS="$LIBDIRS" CXXFLAGS="-g -pg" +fi + +rm AUTHORS COPYING diff --git a/acprep.lt b/acprep.lt new file mode 100755 index 00000000..36193dea --- /dev/null +++ b/acprep.lt @@ -0,0 +1,37 @@ +#!/bin/sh + +glibtoolize --automake -f -c +aclocal +autoheader +touch AUTHORS ChangeLog COPYING + +if [ "$1" = "--dist" ]; then + automake -acfi +else + automake -acf +fi + +autoconf + +INCDIRS="-I/sw/include -I/usr/include/httpd/xml -I/sw/include/python2.3" +INCDIRS="$INCDIRS -Wno-long-double" +LIBDIRS="-L/sw/lib -L/sw/lib/python2.3/config" + +if [ "$1" = "--debug" ]; then + ./configure CPPFLAGS="$INCDIRS" LDFLAGS="$LIBDIRS" CXXFLAGS="-g" \ + --enable-debug --enable-python --disable-shared +elif [ "$1" = "--opt" ]; then + ./configure CPPFLAGS="$INCDIRS" LDFLAGS="$LIBDIRS" \ + CXXFLAGS="-fomit-frame-pointer -fastf -mcpu=7450 -fPIC" +elif [ "$1" = "--flat-opt" ]; then + ./configure CPPFLAGS="$INCDIRS" LDFLAGS="$LIBDIRS" \ + CXXFLAGS="-fomit-frame-pointer -fastf -mcpu=7450" --disable-shared +elif [ "$1" = "--safe-opt" ]; then + ./configure CPPFLAGS="$INCDIRS" LDFLAGS="$LIBDIRS" \ + CXXFLAGS="-fomit-frame-pointer -fastf -mcpu=7450 -DDEBUG_LEVEL=1" \ + --enable-python --disable-shared +elif [ "$1" = "--perf" ]; then + ./configure CPPFLAGS="$INCDIRS" LDFLAGS="$LIBDIRS" CXXFLAGS="-g -pg" +fi + +rm AUTHORS COPYING @@ -1,621 +1,1326 @@ -#include "ledger.h" +#include "amount.h" +#include "util.h" +#include <list> #include <sstream> -#include <cstdio> -#include <gmp.h> // GNU multi-precision library +#include <cstdlib> + +#include <gmp.h> namespace ledger { -#define MAX_PRECISION 10 // must be 2 or higher +#define BIGINT_BULK_ALLOC 0x0001 -////////////////////////////////////////////////////////////////////// -// -// The `amount' structure. Every transaction has an associated -// amount, which is represented by this structure. `amount' uses the -// GNU multi-precision library, allowing for arbitrarily large -// amounts. Each amount is a quantity of a certain commodity, with -// an optional price per-unit for that commodity at the time the -// amount was stated. -// -// To create an amount, for example: -// -// amount * cost = create_amount("50.2 MSFT @ $100.50"); -// +class amount_t::bigint_t { + public: + mpz_t val; + unsigned short prec; + unsigned short flags; + unsigned int ref; + unsigned int index; -class gmp_amount : public amount -{ - bool priced; + bigint_t() : prec(0), flags(0), ref(1), index(0) { + mpz_init(val); + } + bigint_t(mpz_t _val) : prec(0), flags(0), ref(1), index(0) { + mpz_init_set(val, _val); + } + bigint_t(const bigint_t& other) + : prec(other.prec), flags(0), ref(1), index(0) { + mpz_init_set(val, other.val); + } + ~bigint_t() { + assert(ref == 0); + mpz_clear(val); + } +}; - mpz_t price; - commodity * price_comm; +unsigned int sizeof_bigint_t() { + return sizeof(amount_t::bigint_t); +} - mpz_t quantity; - commodity * quantity_comm; +#define MPZ(x) ((x)->val) - gmp_amount(const gmp_amount& other) {} - gmp_amount& operator=(const gmp_amount& other) { return *this; } +static mpz_t temp; +static mpz_t divisor; +static amount_t::bigint_t true_value; - public: - gmp_amount() : priced(false), price_comm(NULL), quantity_comm(NULL) { - mpz_init(price); - mpz_init(quantity); - } +commodity_t::updater_t * commodity_t::updater = NULL; +commodities_map commodity_t::commodities; +commodity_t * commodity_t::null_commodity; - virtual ~gmp_amount() { - mpz_clear(price); - mpz_clear(quantity); - } +static struct _init_amounts { + _init_amounts() { + mpz_init(temp); + mpz_init(divisor); - virtual commodity * commdty() const { - return quantity_comm; - } + mpz_set_ui(true_value.val, 1); - virtual void set_commdty(commodity * comm) { - quantity_comm = comm; + commodity_t::updater = NULL; + commodity_t::null_commodity = commodity_t::find_commodity("", true); } - virtual amount * copy() const; - virtual amount * value(const amount *) const; - virtual void set_value(const amount * val); - virtual amount * street(std::time_t * when = NULL, - bool use_history = false, - bool download = false) const; + ~_init_amounts() { + mpz_clear(temp); + mpz_clear(divisor); - virtual bool has_price() const { - return priced; - } - virtual amount * per_item_price() const; + if (commodity_t::updater) { + delete commodity_t::updater; + commodity_t::updater = NULL; + } - virtual bool is_zero() const; - virtual bool is_negative() const; - virtual int compare(const amount * other) const; + for (commodities_map::iterator i = commodity_t::commodities.begin(); + i != commodity_t::commodities.end(); + i++) + delete (*i).second; - virtual void negate() { - mpz_ui_sub(quantity, 0, quantity); - } - virtual void credit(const amount * other); + commodity_t::commodities.clear(); - virtual void parse(const std::string& num); - virtual const std::string as_str(bool full_prec) const; - - friend amount * create_amount(const std::string& value, - const amount * cost); -}; + true_value.ref--; + } +} _init_obj; -amount * create_amount(const std::string& value, const amount * cost) +static void mpz_round(mpz_t out, mpz_t value, int value_prec, int round_prec) { - gmp_amount * a = new gmp_amount(); - a->parse(value); - if (cost) - a->set_value(cost); - return a; -} + // Round `value', with an encoding precision of `value_prec', to a + // rounded value with precision `round_prec'. Result is stored in + // `out'. + + assert(value_prec > round_prec); -static void round(mpz_t out, const mpz_t val, int prec) -{ - mpz_t divisor; mpz_t quotient; mpz_t remainder; - mpz_init(divisor); mpz_init(quotient); mpz_init(remainder); - mpz_ui_pow_ui(divisor, 10, MAX_PRECISION - prec); - mpz_tdiv_qr(quotient, remainder, val, divisor); - mpz_ui_pow_ui(divisor, 10, MAX_PRECISION - prec - 1); + mpz_ui_pow_ui(divisor, 10, value_prec - round_prec); + mpz_tdiv_qr(quotient, remainder, value, divisor); + mpz_divexact_ui(divisor, divisor, 10); mpz_mul_ui(divisor, divisor, 5); if (mpz_sgn(remainder) < 0) { - mpz_ui_sub(divisor, 0, divisor); - + mpz_neg(divisor, divisor); if (mpz_cmp(remainder, divisor) < 0) { - mpz_ui_pow_ui(divisor, 10, MAX_PRECISION - prec); + mpz_ui_pow_ui(divisor, 10, value_prec - round_prec); mpz_add(remainder, divisor, remainder); mpz_ui_sub(remainder, 0, remainder); - mpz_add(out, val, remainder); + mpz_add(out, value, remainder); } else { - mpz_sub(out, val, remainder); + mpz_sub(out, value, remainder); } } else { if (mpz_cmp(remainder, divisor) >= 0) { - mpz_ui_pow_ui(divisor, 10, MAX_PRECISION - prec); + mpz_ui_pow_ui(divisor, 10, value_prec - round_prec); mpz_sub(remainder, divisor, remainder); - mpz_add(out, val, remainder); + mpz_add(out, value, remainder); } else { - mpz_sub(out, val, remainder); + mpz_sub(out, value, remainder); } } - - mpz_clear(divisor); mpz_clear(quotient); mpz_clear(remainder); + + // chop off the rounded bits + mpz_ui_pow_ui(divisor, 10, value_prec - round_prec); + mpz_tdiv_q(out, out, divisor); } -static void multiply(mpz_t out, const mpz_t l, const mpz_t r) +amount_t::amount_t(const bool value) { - mpz_t divisor; + if (value) { + quantity = &true_value; + quantity->ref++; + } else { + quantity = NULL; + } + commodity_ = NULL; +} - mpz_init(divisor); +amount_t::amount_t(const long value) +{ + if (value != 0) { + quantity = new bigint_t; + mpz_set_si(MPZ(quantity), value); + } else { + quantity = NULL; + } + commodity_ = NULL; +} - mpz_mul(out, l, r); +amount_t::amount_t(const unsigned long value) +{ + if (value != 0) { + quantity = new bigint_t; + mpz_set_ui(MPZ(quantity), value); + } else { + quantity = NULL; + } + commodity_ = NULL; +} - // The number is at double-precision right now, so rounding at - // precision 0 effectively means rounding to the ordinary - // precision. - round(out, out, 0); +amount_t::amount_t(const double value) +{ + if (value != 0.0) { + quantity = new bigint_t; + mpz_set_d(MPZ(quantity), value); + } else { + quantity = NULL; + } + commodity_ = NULL; +} - // after multiplying, truncate to the correct precision - mpz_ui_pow_ui(divisor, 10, MAX_PRECISION); - mpz_tdiv_q(out, out, divisor); +void amount_t::_release() +{ + if (--quantity->ref == 0) { + if (! (quantity->flags & BIGINT_BULK_ALLOC)) + delete quantity; + else + quantity->~bigint_t(); + } +} - mpz_clear(divisor); +void amount_t::_init() +{ + if (! quantity) { + quantity = new bigint_t; + } + else if (quantity->ref > 1) { + _release(); + quantity = new bigint_t; + } } -amount * gmp_amount::copy() const +void amount_t::_dup() { - gmp_amount * new_amt = new gmp_amount(); + if (quantity->ref > 1) { + bigint_t * q = new bigint_t(*quantity); + _release(); + quantity = q; + } +} - mpz_set(new_amt->quantity, quantity); - new_amt->quantity_comm = quantity_comm; +void amount_t::_copy(const amount_t& amt) +{ + if (quantity != amt.quantity) { + if (quantity) + _release(); + + // Never maintain a pointer into a bulk allocation pool; such + // pointers are not guaranteed to remain. + if (amt.quantity->flags & BIGINT_BULK_ALLOC) { + quantity = new bigint_t(*amt.quantity); + } else { + quantity = amt.quantity; + quantity->ref++; + } + } + commodity_ = amt.commodity_; +} - return new_amt; +amount_t& amount_t::operator=(const std::string& value) +{ + std::istringstream str(value); + parse(str); + return *this; } -amount * gmp_amount::per_item_price() const +amount_t& amount_t::operator=(const char * value) { - if (! priced) - return NULL; + std::string valstr(value); + std::istringstream str(valstr); + parse(str); + return *this; +} - gmp_amount * new_amt = new gmp_amount(); +// assignment operator +amount_t& amount_t::operator=(const amount_t& amt) +{ + if (this != &amt) { + if (amt.quantity) + _copy(amt); + else if (quantity) + _clear(); + } + return *this; +} - mpz_set(new_amt->quantity, price); - new_amt->quantity_comm = price_comm; +amount_t& amount_t::operator=(const bool value) +{ + if (! value) { + if (quantity) + _clear(); + } else { + commodity_ = NULL; + if (quantity) + _release(); + quantity = &true_value; + quantity->ref++; + } + return *this; +} - return new_amt; +amount_t& amount_t::operator=(const long value) +{ + if (value == 0) { + if (quantity) + _clear(); + } else { + commodity_ = NULL; + _init(); + mpz_set_si(MPZ(quantity), value); + } + return *this; } -amount * gmp_amount::value(const amount * pr) const +amount_t& amount_t::operator=(const unsigned long value) { - if (pr) { - const gmp_amount * p = dynamic_cast<const gmp_amount *>(pr); - assert(p); + if (value == 0) { + if (quantity) + _clear(); + } else { + commodity_ = NULL; + _init(); + mpz_set_ui(MPZ(quantity), value); + } + return *this; +} - gmp_amount * new_amt = new gmp_amount(); +amount_t& amount_t::operator=(const double value) +{ + if (value == 0.0) { + if (quantity) + _clear(); + } else { + commodity_ = NULL; + _init(); + mpz_set_d(MPZ(quantity), value); + } + return *this; +} - multiply(new_amt->quantity, quantity, p->quantity); - // If the price we are multiplying by has no commodity, use the - // commodity of the current amount. - if (p->quantity_comm) - new_amt->quantity_comm = p->quantity_comm; - else - new_amt->quantity_comm = quantity_comm; +void amount_t::_resize(unsigned int prec) +{ + assert(prec < 256); + + if (! quantity || prec == quantity->prec) + return; - if (new_amt->quantity_comm && - new_amt->quantity_comm->precision < MAX_PRECISION) - round(new_amt->quantity, new_amt->quantity, - new_amt->quantity_comm->precision); + _dup(); - return new_amt; + if (prec < quantity->prec) { + mpz_ui_pow_ui(divisor, 10, quantity->prec - prec); + mpz_tdiv_q(MPZ(quantity), MPZ(quantity), divisor); + } else { + mpz_ui_pow_ui(divisor, 10, prec - quantity->prec); + mpz_mul(MPZ(quantity), MPZ(quantity), divisor); } - else if (! priced) { - return copy(); + + quantity->prec = prec; +} + + +amount_t& amount_t::operator+=(const amount_t& amt) +{ + if (! amt.quantity) + return *this; + + if (! quantity) { + _copy(amt); + return *this; } - else { - gmp_amount * new_amt = new gmp_amount(); - multiply(new_amt->quantity, quantity, price); + _dup(); - new_amt->quantity_comm = price_comm; - if (new_amt->quantity_comm->precision < MAX_PRECISION) - round(new_amt->quantity, new_amt->quantity, - new_amt->quantity_comm->precision); + if (commodity_ != amt.commodity_) + throw amount_error("Adding amounts with different commodities"); - return new_amt; + if (quantity->prec == amt.quantity->prec) { + mpz_add(MPZ(quantity), MPZ(quantity), MPZ(amt.quantity)); } + else if (quantity->prec < amt.quantity->prec) { + _resize(amt.quantity->prec); + mpz_add(MPZ(quantity), MPZ(quantity), MPZ(amt.quantity)); + } else { + amount_t temp = amt; + temp._resize(quantity->prec); + mpz_add(MPZ(quantity), MPZ(quantity), MPZ(temp.quantity)); + } + + return *this; } -amount * gmp_amount::street(std::time_t * when, - bool use_history, bool download) const +amount_t& amount_t::operator-=(const amount_t& amt) { - static std::time_t now = std::time(NULL); - if (! when) - when = &now; + if (! amt.quantity) + return *this; + + if (! quantity) { + quantity = new bigint_t(*amt.quantity); + commodity_ = amt.commodity_; + mpz_neg(MPZ(quantity), MPZ(quantity)); + return *this; + } - amount * amt = copy(); + _dup(); - if (! amt->commdty()) - return amt; + if (commodity_ != amt.commodity_) + throw amount_error("Subtracting amounts with different commodities"); - int max = 10; - while (--max >= 0) { - amount * price = amt->commdty()->price(when, use_history, download); - if (! price) - break; + if (quantity->prec == amt.quantity->prec) { + mpz_sub(MPZ(quantity), MPZ(quantity), MPZ(amt.quantity)); + } + else if (quantity->prec < amt.quantity->prec) { + _resize(amt.quantity->prec); + mpz_sub(MPZ(quantity), MPZ(quantity), MPZ(amt.quantity)); + } else { + amount_t temp = amt; + temp._resize(quantity->prec); + mpz_sub(MPZ(quantity), MPZ(quantity), MPZ(temp.quantity)); + } - amount * old = amt; - amt = amt->value(price); + return *this; +} - if (amt->commdty() == old->commdty()) { - delete old; - break; - } +amount_t& amount_t::operator*=(const amount_t& amt) +{ + if (! amt.quantity) + return (*this = amt); + else if (! quantity) + return *this; + + _dup(); - delete old; + mpz_mul(MPZ(quantity), MPZ(quantity), MPZ(amt.quantity)); + quantity->prec += amt.quantity->prec; + + unsigned int comm_prec = commodity().precision; + if (quantity->prec > comm_prec + 6U) { + mpz_round(MPZ(quantity), MPZ(quantity), quantity->prec, comm_prec + 6U); + quantity->prec = comm_prec + 6U; } - return amt; + return *this; } -void gmp_amount::set_value(const amount * val) +amount_t& amount_t::operator/=(const amount_t& amt) { - assert(! priced); // don't specify the pricing twice! + if (! amt.quantity) + throw amount_error("Divide by zero"); + else if (! quantity) + return *this; + + _dup(); + + // Increase the value's precision, to capture fractional parts after + // the divide. + mpz_ui_pow_ui(divisor, 10, amt.quantity->prec + 6); + mpz_mul(MPZ(quantity), MPZ(quantity), divisor); + mpz_tdiv_q(MPZ(quantity), MPZ(quantity), MPZ(amt.quantity)); + quantity->prec += 6; + + unsigned int comm_prec = commodity().precision; + if (quantity->prec > comm_prec + 6U) { + mpz_round(MPZ(quantity), MPZ(quantity), quantity->prec, comm_prec + 6U); + quantity->prec = comm_prec + 6U; + } - const gmp_amount * v = dynamic_cast<const gmp_amount *>(val); - assert(v); + return *this; +} - mpz_t quotient; - mpz_t remainder; - mpz_t addend; +// unary negation +void amount_t::negate() +{ + if (quantity) { + _dup(); + mpz_neg(MPZ(quantity), MPZ(quantity)); + } +} - mpz_init(quotient); - mpz_init(remainder); - mpz_init(addend); +int amount_t::sign() const +{ + return quantity ? mpz_sgn(MPZ(quantity)) : 0; +} - mpz_ui_pow_ui(addend, 10, MAX_PRECISION); +// comparisons between amounts +#define AMOUNT_CMP_AMOUNT(OP) \ +bool amount_t::operator OP(const amount_t& amt) const \ +{ \ + if (! quantity) \ + return ! (amt OP 0); \ + if (! amt.quantity) \ + return *this OP 0; \ + \ + if (commodity() && amt.commodity() && \ + commodity() != amt.commodity()) \ + return false; \ + \ + if (quantity->prec == amt.quantity->prec) { \ + return mpz_cmp(MPZ(quantity), MPZ(amt.quantity)) OP 0; \ + } \ + else if (quantity->prec < amt.quantity->prec) { \ + amount_t temp = *this; \ + temp._resize(amt.quantity->prec); \ + return mpz_cmp(MPZ(temp.quantity), MPZ(amt.quantity)) OP 0; \ + } \ + else { \ + amount_t temp = amt; \ + temp._resize(quantity->prec); \ + return mpz_cmp(MPZ(quantity), MPZ(temp.quantity)) OP 0; \ + } \ +} - mpz_tdiv_qr(quotient, remainder, v->quantity, quantity); - mpz_mul(remainder, remainder, addend); - mpz_tdiv_q(remainder, remainder, quantity); - mpz_mul(quotient, quotient, addend); - mpz_add(quotient, quotient, remainder); - mpz_abs(quotient, quotient); +AMOUNT_CMP_AMOUNT(<) +AMOUNT_CMP_AMOUNT(<=) +AMOUNT_CMP_AMOUNT(>) +AMOUNT_CMP_AMOUNT(>=) +AMOUNT_CMP_AMOUNT(==) - priced = true; - mpz_set(price, quotient); - price_comm = v->quantity_comm; +amount_t::operator bool() const +{ + if (! quantity) + return false; - mpz_clear(quotient); - mpz_clear(remainder); - mpz_clear(addend); + if (quantity->prec <= commodity().precision) { + return mpz_sgn(MPZ(quantity)) != 0; + } else { + mpz_set(temp, MPZ(quantity)); + if (commodity_) + mpz_ui_pow_ui(divisor, 10, quantity->prec - commodity_->precision); + else + mpz_ui_pow_ui(divisor, 10, quantity->prec); + mpz_tdiv_q(temp, temp, divisor); + bool zero = mpz_sgn(temp) == 0; + return ! zero; + } } -bool gmp_amount::is_zero() const +amount_t::operator long() const { - mpz_t copy; - mpz_init_set(copy, quantity); - if (quantity_comm && quantity_comm->precision < MAX_PRECISION) - round(copy, copy, quantity_comm->precision); - bool zero = mpz_sgn(copy) == 0; - mpz_clear(copy); - return zero; + if (! quantity) + return 0; + + mpz_set(temp, MPZ(quantity)); + mpz_ui_pow_ui(divisor, 10, quantity->prec); + mpz_tdiv_q(temp, temp, divisor); + return mpz_get_si(temp); } -bool gmp_amount::is_negative() const +amount_t::operator double() const { - return mpz_sgn(quantity) < 0; + if (! quantity) + return 0.0; + + mpz_t remainder; + mpz_init(remainder); + + mpz_set(temp, MPZ(quantity)); + mpz_ui_pow_ui(divisor, 10, quantity->prec); + mpz_tdiv_qr(temp, remainder, temp, divisor); + + char * quotient_s = mpz_get_str(NULL, 10, temp); + char * remainder_s = mpz_get_str(NULL, 10, remainder); + + std::ostringstream num; + num << quotient_s << '.' << remainder_s; + + std::free(quotient_s); + std::free(remainder_s); + + mpz_clear(remainder); + + return std::atof(num.str().c_str()); } -int gmp_amount::compare(const amount * other) const +amount_t amount_t::value(const std::time_t moment) const { - amount * revalued = copy(); - amount * copied = other->copy(); - revalued->negate(); - copied->credit(revalued); - delete revalued; - int result = 1; - if (copied->is_zero()) - result = 0; - else if (copied->is_negative()) - result = -1; - delete copied; - return result; + if (quantity) { + commodity_t& comm = commodity(); + if (! (comm.flags & COMMODITY_STYLE_NOMARKET)) + if (amount_t amt = comm.value(moment)) + return (amt * *this).round(comm.precision); + } + return *this; +} + +amount_t amount_t::round(unsigned int prec) const +{ + if (! quantity || quantity->prec <= prec) + return *this; + + amount_t temp = *this; + temp._dup(); + + mpz_round(MPZ(temp.quantity), MPZ(temp.quantity), temp.quantity->prec, prec); + temp.quantity->prec = prec; + + return temp; } -static std::string amount_to_str(const commodity * comm, const mpz_t val, - bool full_precision) +std::string amount_t::quantity_string() const { - mpz_t temp; + if (! quantity) + return "0"; + + std::ostringstream out; + mpz_t quotient; mpz_t rquotient; mpz_t remainder; - mpz_t divisor; - - bool negative = false; - - mpz_init_set(temp, val); mpz_init(quotient); mpz_init(rquotient); mpz_init(remainder); - mpz_init(divisor); - if (comm == NULL) - full_precision = true; + bool negative = false; - if (! full_precision && comm->precision < MAX_PRECISION) - round(temp, temp, comm->precision); + // Ensure the value is rounded to the commodity's precision before + // outputting it. NOTE: `rquotient' is used here as a temp variable! - mpz_ui_pow_ui(divisor, 10, MAX_PRECISION); - mpz_tdiv_qr(quotient, remainder, temp, divisor); + commodity_t& comm(commodity()); + unsigned short precision; - if (mpz_sgn(quotient) < 0 || mpz_sgn(remainder) < 0) + if (comm == *commodity_t::null_commodity || + comm.flags & COMMODITY_STYLE_VARIABLE) { + mpz_ui_pow_ui(divisor, 10, quantity->prec); + mpz_tdiv_qr(quotient, remainder, MPZ(quantity), divisor); + precision = quantity->prec; + } + else if (comm.precision < quantity->prec) { + mpz_round(rquotient, MPZ(quantity), quantity->prec, + comm.precision); + mpz_ui_pow_ui(divisor, 10, comm.precision); + mpz_tdiv_qr(quotient, remainder, rquotient, divisor); + precision = comm.precision; + } + else if (comm.precision > quantity->prec) { + mpz_ui_pow_ui(divisor, 10, comm.precision - quantity->prec); + mpz_mul(rquotient, MPZ(quantity), divisor); + mpz_ui_pow_ui(divisor, 10, comm.precision); + mpz_tdiv_qr(quotient, remainder, rquotient, divisor); + precision = comm.precision; + } + else if (quantity->prec) { + mpz_ui_pow_ui(divisor, 10, quantity->prec); + mpz_tdiv_qr(quotient, remainder, MPZ(quantity), divisor); + precision = quantity->prec; + } + else { + mpz_set(quotient, MPZ(quantity)); + mpz_set_ui(remainder, 0); + precision = 0; + } + + if (mpz_sgn(quotient) < 0 || mpz_sgn(remainder) < 0) { negative = true; - mpz_abs(quotient, quotient); - mpz_abs(remainder, remainder); - if (full_precision || comm->precision == MAX_PRECISION) { - mpz_set(rquotient, remainder); + mpz_abs(quotient, quotient); + mpz_abs(remainder, remainder); + } + mpz_set(rquotient, remainder); + + if (negative) + out << "-"; + + if (mpz_sgn(quotient) == 0) { + out << '0'; } else { - assert(MAX_PRECISION - comm->precision > 0); - mpz_ui_pow_ui(divisor, 10, MAX_PRECISION - comm->precision); - mpz_tdiv_qr(rquotient, remainder, remainder, divisor); + char * p = mpz_get_str(NULL, 10, quotient); + out << p; + std::free(p); } - std::ostringstream s; + if (precision) { + out << '.'; + + out.width(precision); + out.fill('0'); - if (comm && comm->prefix) { - s << comm->symbol; - if (comm->separate) - s << " "; + char * p = mpz_get_str(NULL, 10, rquotient); + out << p; + std::free(p); } - if (negative) - s << "-"; + mpz_clear(quotient); + mpz_clear(rquotient); + mpz_clear(remainder); + + return out.str(); +} + +std::ostream& operator<<(std::ostream& _out, const amount_t& amt) +{ + if (! amt.quantity) { + _out << "0"; + return _out; + } + + amount_t base(amt); + if (amt.commodity().larger) { + amount_t last(amt); + while (last.commodity().larger) { + last /= *last.commodity_->larger; + last.commodity_ = last.commodity_->larger->commodity_; + if (ledger::abs(last) < 1) + break; + base = last; + } + } + + std::ostringstream out; - if (mpz_sgn(quotient) == 0) - s << '0'; - else if (! comm || ! comm->thousands) - s << quotient; + mpz_t quotient; + mpz_t rquotient; + mpz_t remainder; + + mpz_init(quotient); + mpz_init(rquotient); + mpz_init(remainder); + + bool negative = false; + + // Ensure the value is rounded to the commodity's precision before + // outputting it. NOTE: `rquotient' is used here as a temp variable! + + commodity_t& comm(base.commodity()); + unsigned short precision; + + if (comm == *commodity_t::null_commodity || + comm.flags & COMMODITY_STYLE_VARIABLE) { + mpz_ui_pow_ui(divisor, 10, base.quantity->prec); + mpz_tdiv_qr(quotient, remainder, MPZ(base.quantity), divisor); + precision = base.quantity->prec; + } + else if (comm.precision < base.quantity->prec) { + mpz_round(rquotient, MPZ(base.quantity), base.quantity->prec, + comm.precision); + mpz_ui_pow_ui(divisor, 10, comm.precision); + mpz_tdiv_qr(quotient, remainder, rquotient, divisor); + precision = comm.precision; + } + else if (comm.precision > base.quantity->prec) { + mpz_ui_pow_ui(divisor, 10, comm.precision - base.quantity->prec); + mpz_mul(rquotient, MPZ(base.quantity), divisor); + mpz_ui_pow_ui(divisor, 10, comm.precision); + mpz_tdiv_qr(quotient, remainder, rquotient, divisor); + precision = comm.precision; + } + else if (base.quantity->prec) { + mpz_ui_pow_ui(divisor, 10, base.quantity->prec); + mpz_tdiv_qr(quotient, remainder, MPZ(base.quantity), divisor); + precision = base.quantity->prec; + } else { - bool printed = false; + mpz_set(quotient, MPZ(base.quantity)); + mpz_set_ui(remainder, 0); + precision = 0; + } - // jww (2003-09-29): use a smarter starting value for `powers' - for (int powers = 27; powers >= 0; powers -= 3) { - mpz_ui_pow_ui(divisor, 10, powers); - mpz_tdiv_q(temp, quotient, divisor); + if (mpz_sgn(quotient) < 0 || mpz_sgn(remainder) < 0) { + negative = true; - if (mpz_sgn(temp) == 0) - continue; + mpz_abs(quotient, quotient); + mpz_abs(remainder, remainder); + } + mpz_set(rquotient, remainder); - mpz_ui_pow_ui(divisor, 10, 3); - mpz_tdiv_r(temp, temp, divisor); + if (mpz_sgn(quotient) == 0 && mpz_sgn(rquotient) == 0) { + _out << "0"; + return _out; + } - if (printed) { - s.width(3); - s.fill('0'); - } - s << temp; + if (! (comm.flags & COMMODITY_STYLE_SUFFIXED)) { + if (comm.quote) + out << "\"" << comm.symbol << "\""; + else + out << comm.symbol; + if (comm.flags & COMMODITY_STYLE_SEPARATED) + out << " "; + } + + if (negative) + out << "-"; + + if (mpz_sgn(quotient) == 0) { + out << '0'; + } + else if (! (comm.flags & COMMODITY_STYLE_THOUSANDS)) { + char * p = mpz_get_str(NULL, 10, quotient); + out << p; + std::free(p); + } + else { + std::list<std::string> strs; + char buf[4]; + for (int powers = 0; true; powers += 3) { if (powers > 0) { - s << (comm && comm->european ? '.' : ','); - printed = true; + mpz_ui_pow_ui(divisor, 10, powers); + mpz_tdiv_q(temp, quotient, divisor); + if (mpz_sgn(temp) == 0) + break; + mpz_tdiv_r_ui(temp, temp, 1000); + } else { + mpz_tdiv_r_ui(temp, quotient, 1000); } + mpz_get_str(buf, 10, temp); + strs.push_back(buf); } - } - s << (comm && comm->european ? ',' : '.'); + bool printed = false; - if (comm && (! full_precision || mpz_sgn(rquotient) == 0)) { - s.width(comm->precision); - s.fill('0'); - s << rquotient; - } else { - char buf[MAX_PRECISION + 1]; - gmp_sprintf(buf, "%Zd", rquotient); + for (std::list<std::string>::reverse_iterator i = strs.rbegin(); + i != strs.rend(); + i++) { + if (printed) { + out << (comm.flags & COMMODITY_STYLE_EUROPEAN ? '.' : ','); + out.width(3); + out.fill('0'); + } + out << *i; - int width = std::strlen(buf); - char * p = buf + (width - 1); + printed = true; + } + } - width = MAX_PRECISION - width; + if (precision) { + out << ((comm.flags & COMMODITY_STYLE_EUROPEAN) ? ',' : '.'); - if (comm) { - while (p >= buf && *p == '0' && - (p - buf) >= (comm->precision - width)) - p--; - *(p + 1) = '\0'; - } + out.width(precision); + out.fill('0'); - s.width(width + std::strlen(buf)); - s.fill('0'); - s << buf; + char * p = mpz_get_str(NULL, 10, rquotient); + out << p; + std::free(p); } - if (comm && ! comm->prefix) { - if (comm->separate) - s << " "; - s << comm->symbol; + if (comm.flags & COMMODITY_STYLE_SUFFIXED) { + if (comm.flags & COMMODITY_STYLE_SEPARATED) + out << " "; + if (comm.quote) + out << "\"" << comm.symbol << "\""; + else + out << comm.symbol; } - mpz_clear(temp); mpz_clear(quotient); mpz_clear(rquotient); mpz_clear(remainder); - mpz_clear(divisor); - return s.str(); + // Things are output to a string first, so that if anyone has + // specified a width or fill for _out, it will be applied to the + // entire amount string, and not just the first part. + + _out << out.str(); + + return _out; } -const std::string gmp_amount::as_str(bool full_prec) const +void parse_quantity(std::istream& in, std::string& value) { - std::ostringstream s; - - s << amount_to_str(quantity_comm, quantity, full_prec); + char buf[256]; + char c = peek_next_nonws(in); + READ_INTO(in, buf, 255, c, + std::isdigit(c) || c == '-' || c == '.' || c == ','); + value = buf; +} - if (priced) { - s << " @ "; - s << amount_to_str(price_comm, price, full_prec); +void parse_commodity(std::istream& in, std::string& symbol) +{ + char buf[256]; + char c = peek_next_nonws(in); + if (c == '"') { + in.get(c); + READ_INTO(in, buf, 255, c, c != '"'); + if (c == '"') + in.get(c); + else + throw amount_error("Quoted commodity symbol lacks closing quote"); + } else { + READ_INTO(in, buf, 255, c, ! std::isspace(c) && ! std::isdigit(c) && + c != '-' && c != '.'); } - return s.str(); + symbol = buf; } -static void parse_number(mpz_t out, const std::string& number, - commodity * comm) +void amount_t::parse(std::istream& in, unsigned short flags) { - const char * num = number.c_str(); + // The possible syntax for an amount is: + // + // [-]NUM[ ]SYM [@ AMOUNT] + // SYM[ ][-]NUM [@ AMOUNT] - if (char * p = std::strchr(num, '/')) { - mpz_t numer; - mpz_t val; + std::string symbol; + std::string quant; + unsigned int comm_flags = COMMODITY_STYLE_DEFAULTS;; - // The number was specified as a numerator over denominator, such - // as 5250/100. This gives us the precision, and avoids any - // nastiness having to do with international numbering formats. + char c = peek_next_nonws(in); + if (std::isdigit(c) || c == '.' || c == '-') { + parse_quantity(in, quant); - std::string numer_str(num, p - num); - mpz_init_set_str(numer, numer_str.c_str(), 10); - mpz_init(val); + char n; + if (! in.eof() && ((n = in.peek()) != '\n')) { + if (std::isspace(n)) + comm_flags |= COMMODITY_STYLE_SEPARATED; - int missing = MAX_PRECISION - (std::strlen(++p) - 1); - assert(missing > 0); - mpz_ui_pow_ui(val, 10, missing); + parse_commodity(in, symbol); - mpz_mul(out, numer, val); + comm_flags |= COMMODITY_STYLE_SUFFIXED; + } + } else { + parse_commodity(in, symbol); - mpz_clear(numer); - mpz_clear(val); + if (std::isspace(in.peek())) + comm_flags |= COMMODITY_STYLE_SEPARATED; + + parse_quantity(in, quant); + } + + if (quant.empty()) + throw amount_error("No quantity specified for amount"); + + _init(); + + std::string::size_type last_comma = quant.rfind(','); + std::string::size_type last_period = quant.rfind('.'); + + if (last_comma != std::string::npos && last_period != std::string::npos) { + comm_flags |= COMMODITY_STYLE_THOUSANDS; + if (last_comma > last_period) { + comm_flags |= COMMODITY_STYLE_EUROPEAN; + quantity->prec = quant.length() - last_comma - 1; + } else { + quantity->prec = quant.length() - last_period - 1; + } + } + else if (last_comma != std::string::npos) { + comm_flags |= COMMODITY_STYLE_EUROPEAN; + quantity->prec = quant.length() - last_comma - 1; + } + else if (last_period != std::string::npos) { + quantity->prec = quant.length() - last_period - 1; } else { - static char buf[256]; + quantity->prec = 0; + } - // The number is specified as the user desires, with the commodity - // telling us how to parse it. + // Create the commodity if has not already been seen, and update the + // precision if something greater was used for the quantity. - std::memset(buf, '0', 255); - std::strncpy(buf, num, std::strlen(num)); + commodity_ = commodity_t::find_commodity(symbol, true); + if (! (flags & AMOUNT_PARSE_NO_MIGRATE)) { + commodity_->flags |= comm_flags; + if (quantity->prec > commodity_->precision) + commodity_->precision = quantity->prec; + } - if (comm && comm->thousands) - while (char * t = std::strchr(buf, comm->european ? '.' : ',')) - do { *t = *(t + 1); } while (*(t++ + 1)); + // Now we have the final number. Remove commas and periods, if + // necessary. - char * t = std::strchr(buf, (comm && comm->european) ? ',' : '.'); - if (! t) - t = buf + std::strlen(num); + if (last_comma != std::string::npos || last_period != std::string::npos) { + int len = quant.length(); + char * buf = new char[len + 1]; + const char * p = quant.c_str(); + char * t = buf; - for (int prec = 0; prec < MAX_PRECISION; prec++) { - *t = *(t + 1); - t++; + while (*p) { + if (*p == ',' || *p == '.') + p++; + *t++ = *p++; } *t = '\0'; - mpz_set_str(out, buf, 10); + mpz_set_str(MPZ(quantity), buf, 10); + delete[] buf; + } else { + mpz_set_str(MPZ(quantity), quant.c_str(), 10); } + + if (! (flags & AMOUNT_PARSE_NO_REDUCE)) + reduce(); } -static commodity * parse_amount(mpz_t out, const char * num, - int matched, int * ovector, int base) +void amount_t::reduce() { - static char buf[256]; + while (commodity_ && commodity_->smaller) { + *this *= *commodity_->smaller; + commodity_ = commodity_->smaller->commodity_; + } +} - bool saw_commodity = false; - bool prefix = false; - bool separate = true; - bool thousands = true; - bool european = false; +void amount_t::parse(const std::string& str, unsigned short flags) +{ + std::istringstream stream(str); + parse(stream, flags); +} - std::string symbol; - int precision, result; +void parse_conversion(const std::string& larger_str, + const std::string& smaller_str) +{ + amount_t larger, smaller; - if (ovector[base * 2] >= 0) { - // A prefix symbol was found - saw_commodity = true; - prefix = true; - separate = ovector[(base + 2) * 2] != ovector[(base + 2) * 2 + 1]; - result = pcre_copy_substring(num, ovector, matched, base + 1, buf, 255); - assert(result >= 0); - symbol = buf; - } + larger.parse(larger_str.c_str(), AMOUNT_PARSE_NO_REDUCE); + smaller.parse(smaller_str.c_str(), AMOUNT_PARSE_NO_REDUCE); - // This is the value, and must be present - assert(ovector[(base + 3) * 2] >= 0); - result = pcre_copy_substring(num, ovector, matched, base + 3, buf, 255); - assert(result >= 0); + larger *= smaller; - // Where "thousands" markers used? Is it a european number? - if (char * p = std::strrchr(buf, ',')) { - if (std::strchr(p, '.')) - thousands = true; - else - european = true; + if (larger.commodity()) { + larger.commodity().smaller = new amount_t(smaller); + larger.commodity().flags = smaller.commodity().flags; } + if (smaller.commodity()) + smaller.commodity().larger = new amount_t(larger); +} - // Determine the precision used - if (char * p = std::strchr(buf, european ? ',' : '.')) - precision = std::strlen(++p); - else if (char * p = std::strchr(buf, '/')) - precision = std::strlen(++p) - 1; - else - precision = 0; - // Parse the actual quantity - std::string value_str = buf; - - if (ovector[(base + 4) * 2] >= 0) { - // A suffix symbol was found - saw_commodity = true; - prefix = false; - separate = ovector[(base + 5) * 2] != ovector[(base + 5) * 2 + 1]; - result = pcre_copy_substring(num, ovector, matched, base + 6, buf, 255); - assert(result >= 0); - symbol = buf; - } - - commodity * comm = NULL; - if (saw_commodity) { - commodities_map_iterator item = - main_ledger->commodities.find(symbol.c_str()); - if (item == main_ledger->commodities.end()) - comm = new commodity(symbol, prefix, separate, thousands, - european, precision); - else - comm = (*item).second; +char * bigints; +char * bigints_next; +unsigned int bigints_index; +unsigned int bigints_count; + +void amount_t::read_quantity(char *& data) +{ + char byte = *data++;; + + if (byte == 0) { + quantity = NULL; } + else if (byte == 1) { + quantity = new((bigint_t *)bigints_next) bigint_t; + bigints_next += sizeof(bigint_t); + quantity->flags |= BIGINT_BULK_ALLOC; + + unsigned short len = *((unsigned short *) data); + data += sizeof(unsigned short); + mpz_import(MPZ(quantity), len / sizeof(short), 1, sizeof(short), + 0, 0, data); + data += len; + + char negative = *data++; + if (negative) + mpz_neg(MPZ(quantity), MPZ(quantity)); + + quantity->prec = *((unsigned short *) data); + data += sizeof(unsigned short); + } else { + unsigned int index = *((unsigned int *) data); + data += sizeof(unsigned int); - parse_number(out, value_str.c_str(), comm); + quantity = (bigint_t *) (bigints + (index - 1) * sizeof(bigint_t)); + quantity->ref++; + } +} - return comm; +static char buf[4096]; + +void amount_t::read_quantity(std::istream& in) +{ + char byte; + in.read(&byte, sizeof(byte)); + + if (byte == 0) { + quantity = NULL; + } + else if (byte == 1) { + quantity = new bigint_t; + + unsigned short len; + in.read((char *)&len, sizeof(len)); + assert(len < 4096); + in.read(buf, len); + mpz_import(MPZ(quantity), len / sizeof(short), 1, sizeof(short), + 0, 0, buf); + + char negative; + in.read(&negative, sizeof(negative)); + if (negative) + mpz_neg(MPZ(quantity), MPZ(quantity)); + + in.read((char *)&quantity->prec, sizeof(quantity->prec)); + } + else { + assert(0); + } } -void gmp_amount::parse(const std::string& number) +void amount_t::write_quantity(std::ostream& out) const { - // Compile the regular expression used for parsing amounts - static pcre * re = NULL; - if (! re) { - const char *error; - int erroffset; - static const std::string amount_re = - "(([^-0-9/., ]+)(\\s*))?([-0-9/.,]+)((\\s*)([^-0-9/., @]+))?"; - const std::string regexp = - "^" + amount_re + "(\\s*@\\s*" + amount_re + ")?$"; - re = pcre_compile(regexp.c_str(), 0, &error, &erroffset, NULL); + char byte; + + if (! quantity) { + byte = 0; + out.write(&byte, sizeof(byte)); + return; } - int ovector[60]; - int matched; + if (quantity->index == 0) { + quantity->index = ++bigints_index; + bigints_count++; - matched = pcre_exec(re, NULL, number.c_str(), number.length(), - 0, 0, ovector, 60); - if (matched > 0) { - quantity_comm = parse_amount(quantity, number.c_str(), matched, - ovector, 1); + byte = 1; + out.write(&byte, sizeof(byte)); - // If the following succeeded, then we have a price - if (ovector[8 * 2] >= 0) { - priced = true; - price_comm = parse_amount(price, number.c_str(), matched, - ovector, 9); + std::size_t size; + mpz_export(buf, &size, 1, sizeof(short), 0, 0, MPZ(quantity)); + unsigned short len = size * sizeof(short); + out.write((char *)&len, sizeof(len)); + if (len) { + assert(len < 4096); + out.write(buf, len); } + + byte = mpz_sgn(MPZ(quantity)) < 0 ? 1 : 0; + out.write(&byte, sizeof(byte)); + + out.write((char *)&quantity->prec, sizeof(quantity->prec)); } else { - std::cerr << "Failed to parse amount: " << number << std::endl; + assert(quantity->ref > 1); + + // Since this value has already been written, we simply write + // out a reference to which one it was. + byte = 2; + out.write(&byte, sizeof(byte)); + out.write((char *)&quantity->index, sizeof(quantity->index)); } } -void gmp_amount::credit(const amount * value) +bool amount_t::valid() const { - const gmp_amount * val = dynamic_cast<const gmp_amount *>(value); - assert(quantity_comm == val->quantity_comm); - mpz_add(quantity, quantity, val->quantity); + if (quantity) { + if (! commodity_) + return false; + + if (quantity->ref == 0) + return false; + } + else if (commodity_) { + return false; + } + + return true; +} + + +void commodity_t::add_price(const std::time_t date, const amount_t& price) +{ + if (! history) + history = new history_t; + + history_map::iterator i = history->prices.find(date); + if (i != history->prices.end()) { + (*i).second = price; + } else { + std::pair<history_map::iterator, bool> result + = history->prices.insert(history_pair(date, price)); + assert(result.second); + } +} + +commodity_t * commodity_t::find_commodity(const std::string& symbol, + bool auto_create) +{ + commodities_map::const_iterator i = commodities.find(symbol); + if (i != commodities.end()) + return (*i).second; + + if (auto_create) { + commodity_t * commodity = new commodity_t(symbol); + add_commodity(commodity); + return commodity; + } + + return NULL; +} + +amount_t commodity_t::value(const std::time_t moment) +{ + std::time_t age = 0; + amount_t price; + + if (! history) + return price; + + for (history_map::reverse_iterator i = history->prices.rbegin(); + i != history->prices.rend(); + i++) + if (moment == 0 || std::difftime(moment, (*i).first) >= 0) { + age = (*i).first; + price = (*i).second; + break; + } + + if (updater) + (*updater)(*this, moment, age, + (history->prices.size() > 0 ? + (*history->prices.rbegin()).first : 0), price); + + return price; } } // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> +#include <Python.h> + +using namespace boost::python; +using namespace ledger; + +void py_parse_1(amount_t& amount, const std::string& str, + unsigned short flags) { + amount.parse(str, flags); +} +void py_parse_2(amount_t& amount, const std::string& str) { + amount.parse(str); +} + +struct commodity_updater_wrap : public commodity_t::updater_t +{ + PyObject * self; + commodity_updater_wrap(PyObject * self_) : self(self_) {} + + virtual void operator()(commodity_t& commodity, + const std::time_t moment, + const std::time_t date, + const std::time_t last, + amount_t& price) { + call_method<void>(self, "__call__", commodity, moment, date, last, price); + } +}; + +commodity_t * py_find_commodity_1(const std::string& symbol) +{ + return commodity_t::find_commodity(symbol); +} + +commodity_t * py_find_commodity_2(const std::string& symbol, bool auto_create) +{ + return commodity_t::find_commodity(symbol, auto_create); +} + +#define EXC_TRANSLATOR(type) \ + void exc_translate_ ## type(const type& err) { \ + PyErr_SetString(PyExc_RuntimeError, err.what()); \ + } + +EXC_TRANSLATOR(amount_error) + +void export_amount() +{ + scope().attr("AMOUNT_PARSE_NO_MIGRATE") = AMOUNT_PARSE_NO_MIGRATE; + scope().attr("AMOUNT_PARSE_NO_REDUCE") = AMOUNT_PARSE_NO_REDUCE; + + class_< amount_t > ("Amount") + .def(init<amount_t>()) + .def(init<std::string>()) + .def(init<char *>()) + .def(init<bool>()) + .def(init<long>()) + .def(init<unsigned long>()) + .def(init<double>()) + + .def("commodity", &amount_t::commodity, + return_value_policy<reference_existing_object>()) + .def("set_commodity", &amount_t::set_commodity) + .def("clear_commodity", &amount_t::clear_commodity) + .def("quantity_string", &amount_t::quantity_string) + + .def(self += self) + .def(self += long()) + .def(self + self) + .def(self + long()) + .def(self -= self) + .def(self -= long()) + .def(self - self) + .def(self - long()) + .def(self *= self) + .def(self *= long()) + .def(self * self) + .def(self * long()) + .def(self /= self) + .def(self /= long()) + .def(self / self) + .def(self / long()) + .def(- self) + + .def(self < self) + .def(self < long()) + .def(self <= self) + .def(self <= long()) + .def(self > self) + .def(self > long()) + .def(self >= self) + .def(self >= long()) + .def(self == self) + .def(self == long()) + .def(self != self) + .def(self != long()) + .def(! self) + + .def(self_ns::int_(self)) + .def(self_ns::float_(self)) + .def(self_ns::str(self)) + .def(abs(self)) + + .def("negate", &amount_t::negate) + .def("parse", py_parse_1) + .def("parse", py_parse_2) + .def("reduce", &amount_t::reduce) + .def("valid", &amount_t::valid) + ; + + class_< commodity_t::updater_t, commodity_updater_wrap, boost::noncopyable > + ("Updater") + ; + + scope().attr("COMMODITY_STYLE_DEFAULTS") = COMMODITY_STYLE_DEFAULTS; + scope().attr("COMMODITY_STYLE_SUFFIXED") = COMMODITY_STYLE_SUFFIXED; + scope().attr("COMMODITY_STYLE_SEPARATED") = COMMODITY_STYLE_SEPARATED; + scope().attr("COMMODITY_STYLE_EUROPEAN") = COMMODITY_STYLE_EUROPEAN; + scope().attr("COMMODITY_STYLE_THOUSANDS") = COMMODITY_STYLE_THOUSANDS; + scope().attr("COMMODITY_STYLE_NOMARKET") = COMMODITY_STYLE_NOMARKET; + scope().attr("COMMODITY_STYLE_VARIABLE") = COMMODITY_STYLE_VARIABLE; + + class_< commodity_t > ("Commodity") + .def(init<std::string, optional<unsigned int, unsigned int> >()) + + .def_readonly("symbol", &commodity_t::symbol) + .def("set_symbol", &commodity_t::set_symbol) + .def_readwrite("name", &commodity_t::name) + .def_readwrite("note", &commodity_t::name) + .def_readwrite("precision", &commodity_t::precision) + .def_readwrite("flags", &commodity_t::flags) + .def_readwrite("ident", &commodity_t::ident) + .def_readwrite("updater", &commodity_t::updater) + .add_property("smaller", + make_getter(&commodity_t::smaller, + return_value_policy<reference_existing_object>())) + .add_property("larger", + make_getter(&commodity_t::larger, + return_value_policy<reference_existing_object>())) + + .def(self_ns::str(self)) + + .def("add_price", &commodity_t::add_price) + .def("remove_price", &commodity_t::remove_price) + .def("value", &commodity_t::value) + + .def("valid", &commodity_t::valid) + ; + + def("add_commodity", &commodity_t::add_commodity); + def("remove_commodity", &commodity_t::remove_commodity); + def("find_commodity", py_find_commodity_1, + return_value_policy<reference_existing_object>()); + def("find_commodity", py_find_commodity_2, + return_value_policy<reference_existing_object>()); + +#define EXC_TRANSLATE(type) \ + register_exception_translator<type>(&exc_translate_ ## type); + + EXC_TRANSLATE(amount_error); +} + +#endif // USE_BOOST_PYTHON diff --git a/amount.h b/amount.h new file mode 100644 index 00000000..9b9dc225 --- /dev/null +++ b/amount.h @@ -0,0 +1,443 @@ +#ifndef _AMOUNT_H +#define _AMOUNT_H + +#include <map> +#include <string> +#include <ctime> +#include <cctype> +#include <iostream> +#include <sstream> +#include <cassert> +#include <exception> + +namespace ledger { + +class commodity_t; + +class amount_t +{ + public: + class bigint_t; + + protected: + void _init(); + void _copy(const amount_t& amt); + void _release(); + void _dup(); + void _resize(unsigned int prec); + + void _clear() { + if (quantity) { + assert(commodity_); + _release(); + quantity = NULL; + commodity_ = NULL; + } else { + assert(! commodity_); + } + } + + bigint_t * quantity; + commodity_t * commodity_; + + public: + // constructors + amount_t() : quantity(NULL), commodity_(NULL) {} + amount_t(const amount_t& amt) : quantity(NULL) { + if (amt.quantity) + _copy(amt); + else + commodity_ = NULL; + } + amount_t(const std::string& value) : quantity(NULL) { + parse(value); + } + amount_t(const char * value) : quantity(NULL) { + parse(value); + } + amount_t(const bool value); + amount_t(const long value); + amount_t(const unsigned long value); + amount_t(const double value); + + // destructor + ~amount_t() { + if (quantity) + _release(); + } + + commodity_t& commodity() const; + void set_commodity(commodity_t& comm) { + commodity_ = &comm; + } + void clear_commodity() { + commodity_ = NULL; + } + + bool null() const { + return ! quantity && ! commodity_; + } + + std::string quantity_string() const; + + // assignment operator + amount_t& operator=(const amount_t& amt); + amount_t& operator=(const std::string& value); + amount_t& operator=(const char * value); + amount_t& operator=(const bool value); + amount_t& operator=(const long value); + amount_t& operator=(const unsigned long value); + amount_t& operator=(const double value); + + // general methods + amount_t round(unsigned int prec) const; + + // in-place arithmetic + amount_t& operator+=(const amount_t& amt); + amount_t& operator-=(const amount_t& amt); + amount_t& operator*=(const amount_t& amt); + amount_t& operator/=(const amount_t& amt); + + template <typename T> + amount_t& operator+=(T value) { + return *this += amount_t(value); + } + template <typename T> + amount_t& operator-=(T value) { + return *this -= amount_t(value); + } + template <typename T> + amount_t& operator*=(T value) { + return *this *= amount_t(value); + } + template <typename T> + amount_t& operator/=(T value) { + return *this /= amount_t(value); + } + + // simple arithmetic + amount_t operator+(const amount_t& amt) const { + amount_t temp = *this; + temp += amt; + return temp; + } + amount_t operator-(const amount_t& amt) const { + amount_t temp = *this; + temp -= amt; + return temp; + } + amount_t operator*(const amount_t& amt) const { + amount_t temp = *this; + temp *= amt; + return temp; + } + amount_t operator/(const amount_t& amt) const { + amount_t temp = *this; + temp /= amt; + return temp; + } + + template <typename T> + amount_t operator+(T value) const { + amount_t temp = *this; + temp += value; + return temp; + } + template <typename T> + amount_t operator-(T value) const { + amount_t temp = *this; + temp -= value; + return temp; + } + template <typename T> + amount_t operator*(T value) const { + amount_t temp = *this; + temp *= value; + return temp; + } + template <typename T> + amount_t operator/(T value) const { + amount_t temp = *this; + temp /= value; + return temp; + } + + // unary negation + void negate(); + amount_t negated() const { + amount_t temp = *this; + temp.negate(); + return temp; + } + amount_t operator-() const { + return negated(); + } + + // test for non-zero (use ! for zero) + operator bool() const; + operator long() const; + operator double() const; + + // comparisons between amounts + bool operator<(const amount_t& amt) const; + bool operator<=(const amount_t& amt) const; + bool operator>(const amount_t& amt) const; + bool operator>=(const amount_t& amt) const; + bool operator==(const amount_t& amt) const; + bool operator!=(const amount_t& amt) const { + if (commodity_ != amt.commodity_) + return true; + return ! (*this == amt); + } + + template <typename T> + void parse_num(T num) { + std::ostringstream temp; + temp << num; + std::istringstream in(temp.str()); + parse(in); + } + + int sign() const; + + // POD comparisons +#define AMOUNT_CMP_INT(OP) \ + template <typename T> \ + bool operator OP (T num) const { \ + if (num == 0) { \ + return sign() OP 0; \ + } else { \ + amount_t amt; \ + amt.parse_num(num); \ + return *this OP amt; \ + } \ + } + + AMOUNT_CMP_INT(<) + AMOUNT_CMP_INT(<=) + AMOUNT_CMP_INT(>) + AMOUNT_CMP_INT(>=) + AMOUNT_CMP_INT(==) + + template <typename T> + bool operator!=(T num) const { + return ! (*this == num); + } + + amount_t value(const std::time_t moment) const; + + void abs() { + if (*this < 0) + negate(); + } + +#define AMOUNT_PARSE_NO_MIGRATE 0x01 +#define AMOUNT_PARSE_NO_REDUCE 0x02 + + void parse(std::istream& in, unsigned short flags = 0); + void parse(const std::string& str, unsigned short flags = 0); + void reduce(); + + void read_quantity(char *& data); + void read_quantity(std::istream& in); + void write_quantity(std::ostream& out) const; + + bool valid() const; + + // Classes that are friends, and help to implement this class + + friend std::ostream& operator<<(std::ostream& out, const amount_t& amt); + friend std::istream& operator>>(std::istream& in, amount_t& amt); + + friend unsigned int sizeof_bigint_t(); + + friend void read_binary_amount(char *& data, amount_t& amt); + friend void write_binary_amount(std::ostream& out, const amount_t& amt); + + // This function is special, and exists only to support a custom + // optimization in binary.cc (which offers a significant enough gain + // to be worth the trouble). + + friend void clean_commodity_history(char * item_pool, + char * item_pool_end); +}; + +unsigned int sizeof_bigint_t(); + +void parse_quantity(std::istream& in, std::string& value); +void parse_commodity(std::istream& in, std::string& symbol); +void parse_conversion(const std::string& larger, + const std::string& smaller); + +inline amount_t abs(const amount_t& amt) { + return amt < 0 ? amt.negated() : amt; +} + +std::ostream& operator<<(std::ostream& out, const amount_t& amt); + +inline std::istream& operator>>(std::istream& in, amount_t& amt) { + amt.parse(in); + return in; +} + + +#define COMMODITY_STYLE_DEFAULTS 0x0000 +#define COMMODITY_STYLE_SUFFIXED 0x0001 +#define COMMODITY_STYLE_SEPARATED 0x0002 +#define COMMODITY_STYLE_EUROPEAN 0x0004 +#define COMMODITY_STYLE_THOUSANDS 0x0008 +#define COMMODITY_STYLE_NOMARKET 0x0010 +#define COMMODITY_STYLE_VARIABLE 0x0020 + +typedef std::map<const std::time_t, amount_t> history_map; +typedef std::pair<const std::time_t, amount_t> history_pair; + +typedef std::map<const std::string, commodity_t *> commodities_map; +typedef std::pair<const std::string, commodity_t *> commodities_pair; + +class commodity_t +{ + public: + class updater_t { + public: + virtual ~updater_t() {} + virtual void operator()(commodity_t& commodity, + const std::time_t moment, + const std::time_t date, + const std::time_t last, + amount_t& price) = 0; + }; + + typedef unsigned long ident_t; + + struct history_t { + history_map prices; + std::time_t last_lookup; + }; + + const std::string symbol; + bool quote; + std::string name; + std::string note; + unsigned short precision; + unsigned short flags; + ident_t ident; + history_t * history; + amount_t * smaller; + amount_t * larger; + + // If set, this global function pointer is called to determine + // whether prices have been updated in the meanwhile. + + static updater_t * updater; + + // This map remembers all commodities that have been defined. + + static commodities_map commodities; + static commodity_t * null_commodity; + + static bool add_commodity(commodity_t * commodity, + const std::string symbol = "") { + // The argument "symbol" is useful for creating a symbol alias to + // an underlying commodity type; it is used by the Gnucash parser + // to link "USD" to "$". + std::pair<commodities_map::iterator, bool> result + = commodities.insert(commodities_pair((symbol.empty() ? + commodity->symbol : symbol), + commodity)); + return result.second; + } + static bool remove_commodity(commodity_t * commodity) { + commodities_map::size_type n = commodities.erase(commodity->symbol); + return n > 0; + } + static commodity_t * find_commodity(const std::string& symbol, + bool auto_create = false); + + // Now the per-object constructor and methods + + commodity_t(const std::string& _symbol = "", + unsigned int _precision = 0, + unsigned int _flags = COMMODITY_STYLE_DEFAULTS) + : precision(_precision), flags(_flags), history(NULL), + smaller(NULL), larger(NULL) { + set_symbol(_symbol); + } + ~commodity_t() { + if (history) delete history; + if (smaller) delete smaller; + if (larger) delete larger; + } + + operator bool() const { + return this != null_commodity; + } + bool operator==(const commodity_t& comm) const { + return this == &comm; + } + bool operator!=(const commodity_t& comm) const { + return this != &comm; + } + + void set_symbol(const std::string& sym) { + *(const_cast<std::string *>(&symbol)) = sym; + quote = false; + for (const char * p = symbol.c_str(); *p; p++) + if (std::isspace(*p) || std::isdigit(*p) || *p == '-' || *p == '.') { + quote = true; + return; + } + + } + + void add_price(const std::time_t date, const amount_t& price); + bool remove_price(const std::time_t date) { + if (history) { + history_map::size_type n = history->prices.erase(date); + return n > 0; + } + return false; + } + + amount_t value(const std::time_t moment = std::time(NULL)); + + bool valid() const { + if (symbol.empty() && this != null_commodity) + return false; + + if (precision > 16) + return false; + + if (flags & ~0x1f) + return false; + + return true; + } +}; + +inline std::ostream& operator<<(std::ostream& out, const commodity_t& comm) { + out << comm.symbol; + return out; +} + +inline commodity_t& amount_t::commodity() const { + if (! commodity_) + return *commodity_t::null_commodity; + else + return *commodity_; +} + +class amount_error : public std::exception { + std::string reason; + public: + amount_error(const std::string& _reason) throw() : reason(_reason) {} + virtual ~amount_error() throw() {} + + virtual const char* what() const throw() { + return reason.c_str(); + } +}; + +} // namespace ledger + +#endif // _AMOUNT_H diff --git a/balance.cc b/balance.cc new file mode 100644 index 00000000..42ed2788 --- /dev/null +++ b/balance.cc @@ -0,0 +1,277 @@ +#include "balance.h" + +#include <deque> + +namespace ledger { + +amount_t balance_t::amount(const commodity_t& commodity) const +{ + if (! commodity) { + if (amounts.size() == 1) { + amounts_map::const_iterator i = amounts.begin(); + return (*i).second; + } + } + else if (amounts.size() > 0) { + amounts_map::const_iterator i = amounts.find(&commodity); + if (i != amounts.end()) + return (*i).second; + } + return amount_t(); +} + +balance_t balance_t::value(const std::time_t moment) const +{ + balance_t temp; + + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + temp += (*i).second.value(moment); + + return temp; +} + +struct compare_amount_commodities { + bool operator()(const amount_t * left, const amount_t * right) const { + return left->commodity().symbol < right->commodity().symbol; + } +}; + +void balance_t::write(std::ostream& out, + const int first_width, + const int latter_width) const +{ + bool first = true; + int lwidth = latter_width; + + if (lwidth == -1) + lwidth = first_width; + + typedef std::deque<const amount_t *> amounts_deque; + amounts_deque sorted; + + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if ((*i).second) + sorted.push_back(&(*i).second); + + std::stable_sort(sorted.begin(), sorted.end(), compare_amount_commodities()); + + for (amounts_deque::const_iterator i = sorted.begin(); + i != sorted.end(); + i++) { + int width; + if (! first) { + out << std::endl; + width = lwidth; + } else { + first = false; + width = first_width; + } + + out.width(width); + out.fill(' '); + out << std::right << **i; + } + + if (first) { + out.width(first_width); + out.fill(' '); + out << std::right << "0"; + } +} + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> + +using namespace boost::python; +using namespace ledger; + +unsigned int balance_len(balance_t& bal) +{ + return bal.amounts.size(); +} + +amount_t balance_getitem(balance_t& bal, int i) +{ + std::size_t len = bal.amounts.size(); + + if (abs(i) >= len) { + PyErr_SetString(PyExc_IndexError, "Index out of range"); + throw_error_already_set(); + } + + int x = i < 0 ? len + i : i; + amounts_map::iterator elem = bal.amounts.begin(); + while (--x >= 0) + elem++; + + return (*elem).second; +} + +unsigned int balance_pair_len(balance_pair_t& bal_pair) +{ + return balance_len(bal_pair.quantity); +} + +amount_t balance_pair_getitem(balance_pair_t& bal_pair, int i) +{ + return balance_getitem(bal_pair.quantity, i); +} + +void export_balance() +{ + class_< balance_t > ("Balance") + .def(init<balance_t>()) + .def(init<amount_t>()) + .def(init<long>()) + .def(init<unsigned long>()) + .def(init<double>()) + + .def(self += self) + .def(self += other<amount_t>()) + .def(self += long()) + .def(self + self) + .def(self + other<amount_t>()) + .def(self + long()) + .def(self -= self) + .def(self -= other<amount_t>()) + .def(self -= long()) + .def(self - self) + .def(self - other<amount_t>()) + .def(self - long()) + .def(self *= self) + .def(self *= other<amount_t>()) + .def(self *= long()) + .def(self * self) + .def(self * other<amount_t>()) + .def(self * long()) + .def(self /= self) + .def(self /= other<amount_t>()) + .def(self /= long()) + .def(self / self) + .def(self / other<amount_t>()) + .def(self / long()) + .def(- self) + + .def(self < self) + .def(self < other<amount_t>()) + .def(self < long()) + .def(self <= self) + .def(self <= other<amount_t>()) + .def(self <= long()) + .def(self > self) + .def(self > other<amount_t>()) + .def(self > long()) + .def(self >= self) + .def(self >= other<amount_t>()) + .def(self >= long()) + .def(self == self) + .def(self == other<amount_t>()) + .def(self == long()) + .def(self != self) + .def(self != other<amount_t>()) + .def(self != long()) + .def(! self) + + .def(abs(self)) + .def(self_ns::str(self)) + + .def("__len__", balance_len) + .def("__getitem__", balance_getitem) + + .def("negate", &balance_t::negate) + .def("amount", &balance_t::amount) + .def("value", &balance_t::value) + .def("write", &balance_t::write) + .def("valid", &balance_t::valid) + ; + + class_< balance_pair_t > ("BalancePair") + .def(init<balance_pair_t>()) + .def(init<balance_t>()) + .def(init<amount_t>()) + .def(init<long>()) + .def(init<unsigned long>()) + .def(init<double>()) + + .def(self += self) + .def(self += other<balance_t>()) + .def(self += other<amount_t>()) + .def(self += long()) + .def(self + self) + .def(self + other<balance_t>()) + .def(self + other<amount_t>()) + .def(self + long()) + .def(self -= self) + .def(self -= other<balance_t>()) + .def(self -= other<amount_t>()) + .def(self -= long()) + .def(self - self) + .def(self - other<balance_t>()) + .def(self - other<amount_t>()) + .def(self - long()) + .def(self *= self) + .def(self *= other<balance_t>()) + .def(self *= other<amount_t>()) + .def(self *= long()) + .def(self * self) + .def(self * other<balance_t>()) + .def(self * other<amount_t>()) + .def(self * long()) + .def(self /= self) + .def(self /= other<balance_t>()) + .def(self /= other<amount_t>()) + .def(self /= long()) + .def(self / self) + .def(self / other<balance_t>()) + .def(self / other<amount_t>()) + .def(self / long()) + .def(- self) + + .def(self < self) + .def(self < other<balance_t>()) + .def(self < other<amount_t>()) + .def(self < long()) + .def(self <= self) + .def(self <= other<balance_t>()) + .def(self <= other<amount_t>()) + .def(self <= long()) + .def(self > self) + .def(self > other<balance_t>()) + .def(self > other<amount_t>()) + .def(self > long()) + .def(self >= self) + .def(self >= other<balance_t>()) + .def(self >= other<amount_t>()) + .def(self >= long()) + .def(self == self) + .def(self == other<balance_t>()) + .def(self == other<amount_t>()) + .def(self == long()) + .def(self != self) + .def(self != other<balance_t>()) + .def(self != other<amount_t>()) + .def(self != long()) + .def(! self) + + .def(abs(self)) + .def(self_ns::str(self)) + + .def("__len__", balance_pair_len) + .def("__getitem__", balance_pair_getitem) + + .def("negate", &balance_pair_t::negate) + .def("amount", &balance_pair_t::amount) + .def("value", &balance_pair_t::value) + .def("write", &balance_pair_t::write) + .def("valid", &balance_pair_t::valid) + ; +} + +#endif // USE_BOOST_PYTHON diff --git a/balance.h b/balance.h new file mode 100644 index 00000000..8a6e4a35 --- /dev/null +++ b/balance.h @@ -0,0 +1,893 @@ +#ifndef _BALANCE_H +#define _BALANCE_H + +#include "amount.h" + +#include <map> +#include <ctime> +#include <iostream> + +namespace ledger { + +typedef std::map<const commodity_t *, amount_t> amounts_map; +typedef std::pair<const commodity_t *, amount_t> amounts_pair; + +class balance_t +{ + public: + amounts_map amounts; + + bool valid() const { + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if (! (*i).second.valid()) + return false; + return true; + } + + // constructors + balance_t() {} + balance_t(const balance_t& bal) { + for (amounts_map::const_iterator i = bal.amounts.begin(); + i != bal.amounts.end(); + i++) + *this += (*i).second; + } + balance_t(const amount_t& amt) { + if (amt) + amounts.insert(amounts_pair(&amt.commodity(), amt)); + } + template <typename T> + balance_t(T value) { + amount_t amt(value); + if (amt) + amounts.insert(amounts_pair(&amt.commodity(), amt)); + } + + // assignment operator + balance_t& operator=(const balance_t& bal) { + if (this != &bal) { + amounts.clear(); + for (amounts_map::const_iterator i = bal.amounts.begin(); + i != bal.amounts.end(); + i++) + *this += (*i).second; + } + return *this; + } + balance_t& operator=(const amount_t& amt) { + amounts.clear(); + *this += amt; + return *this; + } + template <typename T> + balance_t& operator=(T value) { + amounts.clear(); + *this += value; + return *this; + } + + // in-place arithmetic + balance_t& operator+=(const balance_t& bal) { + for (amounts_map::const_iterator i = bal.amounts.begin(); + i != bal.amounts.end(); + i++) + *this += (*i).second; + return *this; + } + balance_t& operator+=(const amount_t& amt) { + amounts_map::iterator i = amounts.find(&amt.commodity()); + if (i != amounts.end()) + (*i).second += amt; + else if (amt) + amounts.insert(amounts_pair(&amt.commodity(), amt)); + return *this; + } + template <typename T> + balance_t& operator+=(T val) { + return *this += amount_t(val); + } + balance_t& operator-=(const balance_t& bal) { + for (amounts_map::const_iterator i = bal.amounts.begin(); + i != bal.amounts.end(); + i++) + *this -= (*i).second; + return *this; + } + balance_t& operator-=(const amount_t& amt) { + amounts_map::iterator i = amounts.find(&amt.commodity()); + if (i != amounts.end()) + (*i).second -= amt; + else if (amt) + amounts.insert(amounts_pair(&amt.commodity(), amt)); + return *this; + } + template <typename T> + balance_t& operator-=(T val) { + return *this -= amount_t(val); + } + + // simple arithmetic + balance_t operator+(const balance_t& bal) const { + balance_t temp = *this; + temp += bal; + return temp; + } + balance_t operator+(const amount_t& amt) const { + balance_t temp = *this; + temp += amt; + return temp; + } + template <typename T> + balance_t operator+(T val) const { + balance_t temp = *this; + temp += val; + return temp; + } + balance_t operator-(const balance_t& bal) const { + balance_t temp = *this; + temp -= bal; + return temp; + } + balance_t operator-(const amount_t& amt) const { + balance_t temp = *this; + temp -= amt; + return temp; + } + template <typename T> + balance_t operator-(T val) const { + balance_t temp = *this; + temp -= val; + return temp; + } + + // multiplication and divide + balance_t& operator*=(const balance_t& bal) { + if (amounts.size() == 1 && bal.amounts.size() == 1) + return *this *= (*bal.amounts.begin()).second; + throw amount_error("It makes no sense to multiply two balances"); + } + balance_t& operator*=(const amount_t& amt) { + // Multiplying by the null commodity causes all amounts to be + // increased by the same factor. + if (amt.commodity().symbol.empty()) { + for (amounts_map::iterator i = amounts.begin(); + i != amounts.end(); + i++) + (*i).second *= amt; + } + else if (amounts.size() == 1) { + (*amounts.begin()).second *= amt; + } + else { + amounts_map::iterator i = amounts.find(&amt.commodity()); + if (i != amounts.end()) + (*i).second *= amt; + } + return *this; + } + template <typename T> + balance_t& operator*=(T val) { + return *this *= amount_t(val); + } + + balance_t& operator/=(const balance_t& bal) { + if (amounts.size() == 1 && bal.amounts.size() == 1) + return *this /= (*bal.amounts.begin()).second; + throw amount_error("It makes no sense to divide two balances"); + } + balance_t& operator/=(const amount_t& amt) { + // Dividing by the null commodity causes all amounts to be + // increased by the same factor. + if (amt.commodity().symbol.empty()) { + for (amounts_map::iterator i = amounts.begin(); + i != amounts.end(); + i++) + (*i).second /= amt; + } + else if (amounts.size() == 1) { + (*amounts.begin()).second /= amt; + } + else { + amounts_map::iterator i = amounts.find(&amt.commodity()); + if (i != amounts.end()) + (*i).second /= amt; + } + return *this; + } + template <typename T> + balance_t& operator/=(T val) { + return *this /= amount_t(val); + } + + // multiplication and divide + balance_t operator*(const balance_t& bal) const { + balance_t temp = *this; + temp *= bal; + return temp; + } + balance_t operator*(const amount_t& amt) const { + balance_t temp = *this; + temp *= amt; + return temp; + } + template <typename T> + balance_t operator*(T val) const { + balance_t temp = *this; + temp *= val; + return temp; + } + balance_t operator/(const balance_t& bal) const { + balance_t temp = *this; + temp /= bal; + return temp; + } + balance_t operator/(const amount_t& amt) const { + balance_t temp = *this; + temp /= amt; + return temp; + } + template <typename T> + balance_t operator/(T val) const { + balance_t temp = *this; + temp /= val; + return temp; + } + + // comparison + bool operator<(const balance_t& bal) const { + for (amounts_map::const_iterator i = bal.amounts.begin(); + i != bal.amounts.end(); + i++) + if (! (amount(*(*i).first) < (*i).second)) + return false; + + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if (! ((*i).second < bal.amount(*(*i).first))) + return false; + + if (bal.amounts.size() == 0 && amounts.size() == 0) + return false; + + return true; + } + bool operator<(const amount_t& amt) const { + return amount(amt.commodity()) < amt; + } + template <typename T> + bool operator<(T val) const { + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if ((*i).second < val) + return true; + return false; + } + + bool operator<=(const balance_t& bal) const { + for (amounts_map::const_iterator i = bal.amounts.begin(); + i != bal.amounts.end(); + i++) + if (! (amount(*(*i).first) <= (*i).second)) + return false; + + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if (! ((*i).second <= bal.amount(*(*i).first))) + return false; + + return true; + } + bool operator<=(const amount_t& amt) const { + return amount(amt.commodity()) <= amt; + } + template <typename T> + bool operator<=(T val) const { + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if ((*i).second <= val) + return true; + return false; + } + + bool operator>(const balance_t& bal) const { + for (amounts_map::const_iterator i = bal.amounts.begin(); + i != bal.amounts.end(); + i++) + if (! (amount(*(*i).first) > (*i).second)) + return false; + + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if (! ((*i).second > bal.amount(*(*i).first))) + return false; + + if (bal.amounts.size() == 0 && amounts.size() == 0) + return false; + + return true; + } + bool operator>(const amount_t& amt) const { + return amount(amt.commodity()) > amt; + } + template <typename T> + bool operator>(T val) const { + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if ((*i).second > val) + return true; + return false; + } + + bool operator>=(const balance_t& bal) const { + for (amounts_map::const_iterator i = bal.amounts.begin(); + i != bal.amounts.end(); + i++) + if (! (amount(*(*i).first) >= (*i).second)) + return false; + + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if (! ((*i).second >= bal.amount(*(*i).first))) + return false; + + return true; + } + bool operator>=(const amount_t& amt) const { + return amount(amt.commodity()) >= amt; + } + template <typename T> + bool operator>=(T val) const { + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if ((*i).second >= val) + return true; + return false; + } + + bool operator==(const balance_t& bal) const { + amounts_map::const_iterator i, j; + for (i = amounts.begin(), j = bal.amounts.begin(); + i != amounts.end() && j != bal.amounts.end(); + i++, j++) { + if (! ((*i).first == (*j).first && + (*i).second == (*j).second)) + return false; + } + return i == amounts.end() && j == bal.amounts.end(); + } + bool operator==(const amount_t& amt) const { + return amounts.size() == 1 && (*amounts.begin()).second == amt; + } + template <typename T> + bool operator==(T val) const { + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if ((*i).second == val) + return true; + return false; + } + + bool operator!=(const balance_t& bal) const { + return ! (*this == bal); + } + bool operator!=(const amount_t& amt) const { + return ! (*this == amt); + } + template <typename T> + bool operator!=(T val) const { + return ! (*this == val); + } + + // unary negation + void negate() { + for (amounts_map::iterator i = amounts.begin(); + i != amounts.end(); + i++) + (*i).second.negate(); + } + balance_t negated() const { + balance_t temp = *this; + temp.negate(); + return temp; + } + balance_t operator-() const { + return negated(); + } + + // conversion operators + operator amount_t() const { + if (amounts.size() == 1) + return (*amounts.begin()).second; + else if (amounts.size() == 0) + return amount_t(); + else + throw amount_error("Cannot convert a balance with " + "multiple commodities to an amount"); + } + operator bool() const { + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if ((*i).second) + return true; + return false; + } + + amount_t amount(const commodity_t& commodity) const; + balance_t value(const std::time_t moment) const; + + void write(std::ostream& out, + const int first_width, + const int latter_width = -1) const; + + void abs() { + for (amounts_map::iterator i = amounts.begin(); + i != amounts.end(); + i++) + (*i).second.abs(); + } + + void round() { + for (amounts_map::iterator i = amounts.begin(); + i != amounts.end(); + i++) + if ((*i).second.commodity()) + (*i).second = (*i).second.round((*i).second.commodity().precision); + } +}; + +inline balance_t abs(const balance_t& bal) { + balance_t temp = bal; + temp.abs(); + return temp; +} + +inline std::ostream& operator<<(std::ostream& out, const balance_t& bal) { + bal.write(out, 12); + return out; +} + + +class balance_pair_t +{ + public: + balance_t quantity; + balance_t * cost; + + // constructors + balance_pair_t() : cost(NULL) {} + balance_pair_t(const balance_pair_t& bal_pair) + : quantity(bal_pair.quantity), cost(NULL) { + if (bal_pair.cost) + cost = new balance_t(*bal_pair.cost); + } + balance_pair_t(const balance_t& _quantity) + : quantity(_quantity), cost(NULL) {} + balance_pair_t(const amount_t& _quantity) + : quantity(_quantity), cost(NULL) {} + template <typename T> + balance_pair_t(T value) : quantity(value), cost(NULL) {} + + // destructor + ~balance_pair_t() { + if (cost) + delete cost; + } + + // assignment operator + balance_pair_t& operator=(const balance_pair_t& bal_pair) { + if (this != &bal_pair) { + if (cost) { + delete cost; + cost = NULL; + } + + quantity = bal_pair.quantity; + if (bal_pair.cost) + cost = new balance_t(*bal_pair.cost); + } + return *this; + } + balance_pair_t& operator=(const balance_t& bal) { + if (cost) { + delete cost; + cost = NULL; + } + quantity = bal; + return *this; + } + balance_pair_t& operator=(const amount_t& amt) { + if (cost) { + delete cost; + cost = NULL; + } + quantity = amt; + return *this; + } + template <typename T> + balance_pair_t& operator=(T value) { + if (cost) { + delete cost; + cost = NULL; + } + quantity = value; + return *this; + } + + // in-place arithmetic + balance_pair_t& operator+=(const balance_pair_t& bal_pair) { + if (bal_pair.cost && ! cost) + cost = new balance_t(quantity); + + quantity += bal_pair.quantity; + + if (cost) + *cost += bal_pair.cost ? *bal_pair.cost : bal_pair.quantity; + + return *this; + } + balance_pair_t& operator+=(const balance_t& bal) { + quantity += bal; + if (cost) + *cost += bal; + return *this; + } + balance_pair_t& operator+=(const amount_t& amt) { + quantity += amt; + if (cost) + *cost += amt; + return *this; + } + template <typename T> + balance_pair_t& operator+=(T val) { + return *this += amount_t(val); + } + + balance_pair_t& operator-=(const balance_pair_t& bal_pair) { + if (bal_pair.cost && ! cost) + cost = new balance_t(quantity); + + quantity -= bal_pair.quantity; + + if (cost) + *cost -= bal_pair.cost ? *bal_pair.cost : bal_pair.quantity; + + return *this; + } + balance_pair_t& operator-=(const balance_t& bal) { + quantity -= bal; + if (cost) + *cost -= bal; + return *this; + } + balance_pair_t& operator-=(const amount_t& amt) { + quantity -= amt; + if (cost) + *cost -= amt; + return *this; + } + template <typename T> + balance_pair_t& operator-=(T val) { + return *this -= amount_t(val); + } + + // simple arithmetic + balance_pair_t operator+(const balance_pair_t& bal_pair) const { + balance_pair_t temp = *this; + temp += bal_pair; + return temp; + } + balance_pair_t operator+(const balance_t& bal) const { + balance_pair_t temp = *this; + temp += bal; + return temp; + } + balance_pair_t operator+(const amount_t& amt) const { + balance_pair_t temp = *this; + temp += amt; + return temp; + } + template <typename T> + balance_pair_t operator+(T val) const { + balance_pair_t temp = *this; + temp += val; + return temp; + } + + balance_pair_t operator-(const balance_pair_t& bal_pair) const { + balance_pair_t temp = *this; + temp -= bal_pair; + return temp; + } + balance_pair_t operator-(const balance_t& bal) const { + balance_pair_t temp = *this; + temp -= bal; + return temp; + } + balance_pair_t operator-(const amount_t& amt) const { + balance_pair_t temp = *this; + temp -= amt; + return temp; + } + template <typename T> + balance_pair_t operator-(T val) const { + balance_pair_t temp = *this; + temp -= val; + return temp; + } + + // multiplication and division + balance_pair_t& operator*=(const balance_pair_t& bal_pair) { + if (bal_pair.cost && ! cost) + cost = new balance_t(quantity); + + quantity *= bal_pair.quantity; + if (cost) + *cost *= bal_pair.cost ? *bal_pair.cost : bal_pair.quantity; + + return *this; + } + balance_pair_t& operator*=(const balance_t& bal) { + quantity *= bal; + if (cost) + *cost *= bal; + return *this; + } + balance_pair_t& operator*=(const amount_t& amt) { + quantity *= amt; + if (cost) + *cost *= amt; + return *this; + } + template <typename T> + balance_pair_t& operator*=(T val) { + return *this *= amount_t(val); + } + + balance_pair_t& operator/=(const balance_pair_t& bal_pair) { + if (bal_pair.cost && ! cost) + cost = new balance_t(quantity); + + quantity /= bal_pair.quantity; + if (cost) + *cost /= bal_pair.cost ? *bal_pair.cost : bal_pair.quantity; + + return *this; + } + balance_pair_t& operator/=(const balance_t& bal) { + quantity /= bal; + if (cost) + *cost /= bal; + return *this; + } + balance_pair_t& operator/=(const amount_t& amt) { + quantity /= amt; + if (cost) + *cost /= amt; + return *this; + } + template <typename T> + balance_pair_t& operator/=(T val) { + return *this /= amount_t(val); + } + + balance_pair_t operator*(const balance_pair_t& bal_pair) const { + balance_pair_t temp = *this; + temp *= bal_pair; + return temp; + } + balance_pair_t operator*(const balance_t& bal) const { + balance_pair_t temp = *this; + temp *= bal; + return temp; + } + balance_pair_t operator*(const amount_t& amt) const { + balance_pair_t temp = *this; + temp *= amt; + return temp; + } + template <typename T> + balance_pair_t operator*(T val) const { + balance_pair_t temp = *this; + temp *= val; + return temp; + } + + balance_pair_t operator/(const balance_pair_t& bal_pair) const { + balance_pair_t temp = *this; + temp /= bal_pair; + return temp; + } + balance_pair_t operator/(const balance_t& bal) const { + balance_pair_t temp = *this; + temp /= bal; + return temp; + } + balance_pair_t operator/(const amount_t& amt) const { + balance_pair_t temp = *this; + temp /= amt; + return temp; + } + template <typename T> + balance_pair_t operator/(T val) const { + balance_pair_t temp = *this; + temp /= val; + return temp; + } + + // comparison + bool operator<(const balance_pair_t& bal_pair) const { + return quantity < bal_pair.quantity; + } + bool operator<(const balance_t& bal) const { + return quantity < bal; + } + bool operator<(const amount_t& amt) const { + return quantity < amt; + } + template <typename T> + bool operator<(T val) const { + return quantity < val; + } + + bool operator<=(const balance_pair_t& bal_pair) const { + return quantity <= bal_pair.quantity; + } + bool operator<=(const balance_t& bal) const { + return quantity <= bal; + } + bool operator<=(const amount_t& amt) const { + return quantity <= amt; + } + template <typename T> + bool operator<=(T val) const { + return quantity <= val; + } + + bool operator>(const balance_pair_t& bal_pair) const { + return quantity > bal_pair.quantity; + } + bool operator>(const balance_t& bal) const { + return quantity > bal; + } + bool operator>(const amount_t& amt) const { + return quantity > amt; + } + template <typename T> + bool operator>(T val) const { + return quantity > val; + } + + bool operator>=(const balance_pair_t& bal_pair) const { + return quantity >= bal_pair.quantity; + } + bool operator>=(const balance_t& bal) const { + return quantity >= bal; + } + bool operator>=(const amount_t& amt) const { + return quantity >= amt; + } + template <typename T> + bool operator>=(T val) const { + return quantity >= val; + } + + bool operator==(const balance_pair_t& bal_pair) const { + return quantity == bal_pair.quantity; + } + bool operator==(const balance_t& bal) const { + return quantity == bal; + } + bool operator==(const amount_t& amt) const { + return quantity == amt; + } + template <typename T> + bool operator==(T val) const { + return quantity == val; + } + + bool operator!=(const balance_pair_t& bal_pair) const { + return ! (*this == bal_pair); + } + bool operator!=(const balance_t& bal) const { + return ! (*this == bal); + } + bool operator!=(const amount_t& amt) const { + return ! (*this == amt); + } + template <typename T> + bool operator!=(T val) const { + return ! (*this == val); + } + + // unary negation + void negate() { + quantity.negate(); + if (cost) + cost->negate(); + } + balance_pair_t negated() const { + balance_pair_t temp = *this; + temp.negate(); + return temp; + } + balance_pair_t operator-() const { + return negated(); + } + + // test for non-zero (use ! for zero) + operator bool() const { + return quantity; + } + operator balance_t() const { + return quantity; + } + operator amount_t() const { + return quantity; + } + + void abs() { + quantity.abs(); + if (cost) + cost->abs(); + } + + amount_t amount(const commodity_t& commodity) const { + return quantity.amount(commodity); + } + balance_t value(const std::time_t moment) const { + return quantity.value(moment); + } + void write(std::ostream& out, + const int first_width, + const int latter_width = -1) const { + quantity.write(out, first_width, latter_width); + } + + balance_pair_t& add(const amount_t& amount, + const amount_t * a_cost = NULL) { + if (a_cost && ! cost) + cost = new balance_t(quantity); + + quantity += amount; + if (cost) + *cost += a_cost ? *a_cost : amount; + + return *this; + } + + bool valid() { + return quantity.valid() && (! cost || cost->valid()); + } + + void round() { + quantity.round(); + } +}; + +inline balance_pair_t abs(const balance_pair_t& bal_pair) { + balance_pair_t temp; + temp.abs(); + return temp; +} + +inline std::ostream& operator<<(std::ostream& out, + const balance_pair_t& bal_pair) { + bal_pair.quantity.write(out, 12); + return out; +} + +} // namespace ledger + +#endif // _BALANCE_H diff --git a/binary.cc b/binary.cc new file mode 100644 index 00000000..944f54b6 --- /dev/null +++ b/binary.cc @@ -0,0 +1,753 @@ +#include "journal.h" +#include "valexpr.h" +#include "binary.h" + +#include <fstream> +#include <ctime> +#include <sys/stat.h> + +#define TIMELOG_SUPPORT 1 + +namespace ledger { + +static unsigned long binary_magic_number = 0xFFEED765; +static unsigned long format_version = 0x00020025; + +static account_t ** accounts; +static account_t ** accounts_next; +static unsigned int account_index; + +static commodity_t ** commodities; +static commodity_t ** commodities_next; +static unsigned int commodity_index; + +extern char * bigints; +extern char * bigints_next; +extern unsigned int bigints_index; +extern unsigned int bigints_count; + +#if DEBUG_LEVEL >= ALPHA +#define read_binary_guard(in, id) { \ + unsigned short guard; \ + in.read((char *)&guard, sizeof(guard)); \ + assert(guard == id); \ +} +#else +#define read_binary_guard(in, id) +#endif + +template <typename T> +inline void read_binary_number(std::istream& in, T& num) { + in.read((char *)&num, sizeof(num)); +} + +template <typename T> +inline T read_binary_number(std::istream& in) { + T num; + read_binary_number(in, num); + return num; +} + +inline void read_binary_string(std::istream& in, std::string& str) +{ + read_binary_guard(in, 0x3001); + + unsigned char len; + read_binary_number(in, 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); + buf[len] = '\0'; + str = buf; + } else { + str = ""; + } + + read_binary_guard(in, 0x3002); +} + +inline std::string read_binary_string(std::istream& in) +{ + std::string temp; + read_binary_string(in, temp); + return temp; +} + +template <typename T> +inline void read_binary_number(char *& data, T& num) { + num = *((T *) data); + data += sizeof(T); +} + +template <typename T> +inline T read_binary_number(char *& data) { + T num; + read_binary_number(data, num); + return num; +} + +inline void read_binary_string(char *& data, std::string& str) +{ +#if DEBUG_LEVEL >= ALPHA + unsigned short guard; + guard = *((unsigned short *) data); + data += sizeof(unsigned short); + assert(guard == 0x3001); +#endif + + unsigned char len = *data++; + if (len == 0xff) { + unsigned short slen = *((unsigned short *) data); + str = std::string(data + sizeof(unsigned short), slen); + data += sizeof(unsigned short) + slen; + } + else if (len) { + str = std::string(data, len); + data += len; + } + else { + str = ""; + } + +#if DEBUG_LEVEL >= ALPHA + guard = *((unsigned short *) data); + data += sizeof(unsigned short); + assert(guard == 0x3002); +#endif +} + +inline std::string read_binary_string(char *& data) +{ + std::string temp; + read_binary_string(data, temp); + return temp; +} + +inline void read_binary_string(char *& data, std::string * str) +{ +#if DEBUG_LEVEL >= ALPHA + unsigned short guard; + guard = *((unsigned short *) data); + data += sizeof(unsigned short); + assert(guard == 0x3001); +#endif + + unsigned char len = *data++; + if (len == 0xff) { + unsigned short slen = *((unsigned short *) data); + new(str) std::string(data + sizeof(unsigned short), slen); + data += sizeof(unsigned short) + slen; + } + else if (len) { + new(str) std::string(data, len); + data += len; + } + else { + new(str) std::string(""); + } + +#if DEBUG_LEVEL >= ALPHA + guard = *((unsigned short *) data); + data += sizeof(unsigned short); + assert(guard == 0x3002); +#endif +} + +inline void read_binary_amount(char *& data, amount_t& amt) +{ + commodity_t::ident_t ident; + read_binary_number(data, ident); + if (ident == 0xffffffff) + amt.commodity_ = NULL; + else + amt.commodity_ = commodities[ident - 1]; + + amt.read_quantity(data); +} + +inline void read_binary_transaction(char *& data, transaction_t * xact) +{ + xact->account = accounts[read_binary_number<account_t::ident_t>(data) - 1]; + + read_binary_amount(data, xact->amount); + + if (*data++ == 1) { + xact->cost = new amount_t; + read_binary_amount(data, *xact->cost); + } else { + xact->cost = NULL; + } + read_binary_number(data, xact->flags); + xact->flags |= TRANSACTION_BULK_ALLOC; + read_binary_string(data, &xact->note); + + xact->data = NULL; +} + +inline void read_binary_entry_base(char *& data, entry_base_t * entry, + transaction_t *& xact_pool) +{ + for (unsigned long i = 0, count = read_binary_number<unsigned long>(data); + i < count; + i++) { + DEBUG_PRINT("ledger.memory.ctors", "ctor transaction_t"); + read_binary_transaction(data, xact_pool); + entry->add_transaction(xact_pool++); + } +} + +inline void read_binary_entry(char *& data, entry_t * entry, + transaction_t *& xact_pool) +{ + read_binary_number(data, entry->date); + read_binary_number(data, entry->state); + read_binary_string(data, &entry->code); + read_binary_string(data, &entry->payee); + read_binary_entry_base(data, entry, xact_pool); +} + +inline void read_binary_auto_entry(char *& data, auto_entry_t * entry, + transaction_t *& xact_pool) +{ + read_binary_string(data, &entry->predicate_string); + entry->predicate = new item_predicate<transaction_t>(entry->predicate_string); + read_binary_entry_base(data, entry, xact_pool); +} + +inline void read_binary_period_entry(char *& data, period_entry_t * entry, + transaction_t *& xact_pool) +{ + read_binary_string(data, &entry->period_string); + std::istringstream stream(entry->period_string); + entry->period.parse(stream); + read_binary_entry_base(data, entry, xact_pool); +} + +inline commodity_t * read_binary_commodity(char *& data) +{ + commodity_t * commodity = new commodity_t; + *commodities_next++ = commodity; + + read_binary_string(data, *(const_cast<std::string *>(&commodity->symbol))); + read_binary_number(data, commodity->quote); + read_binary_string(data, commodity->name); + read_binary_string(data, commodity->note); + read_binary_number(data, commodity->precision); + read_binary_number(data, commodity->flags); + read_binary_number(data, commodity->ident); + + return commodity; +} + +inline void read_binary_commodity_extra(char *& data, + commodity_t::ident_t ident) +{ + commodity_t * commodity = commodities[ident]; + + for (unsigned long i = 0, count = read_binary_number<unsigned long>(data); + i < count; + i++) { + std::time_t when; + read_binary_number(data, when); + amount_t amt; + read_binary_amount(data, amt); + + // Upon insertion, amt will be copied, which will cause the amount + // to be duplicated (and thus not lost when the journal's + // item_pool is deleted. + if (! commodity->history) + commodity->history = new commodity_t::history_t; + commodity->history->prices.insert(history_pair(when, amt)); + } + if (commodity->history) + read_binary_number(data, commodity->history->last_lookup); + + unsigned char flag; + + flag = read_binary_number<unsigned char>(data); + if (flag) { + amount_t amt; + read_binary_amount(data, amt); + commodity->smaller = new amount_t(amt); + } + + flag = read_binary_number<unsigned char>(data); + if (flag) { + amount_t amt; + read_binary_amount(data, amt); + commodity->larger = new amount_t(amt); + } +} + +inline +account_t * read_binary_account(char *& data, account_t * master = NULL) +{ + account_t * acct = new account_t(NULL); + *accounts_next++ = acct; + + acct->ident = read_binary_number<account_t::ident_t>(data); + + account_t::ident_t id; + read_binary_number(data, id); // parent id + if (id == 0xffffffff) + acct->parent = NULL; + else + acct->parent = accounts[id - 1]; + + read_binary_string(data, acct->name); + read_binary_string(data, acct->note); + read_binary_number(data, acct->depth); + + // If all of the subaccounts will be added to a different master + // account, throw away what we've learned about the recorded + // journal's own master account. + + if (master) { + delete acct; + acct = master; + } + + for (account_t::ident_t i = 0, + count = read_binary_number<account_t::ident_t>(data); + i < count; + i++) { + account_t * child = read_binary_account(data); + child->parent = acct; + acct->add_account(child); + } + + return acct; +} + +unsigned int read_binary_journal(std::istream& in, + const std::string& file, + journal_t * journal, + account_t * master) +{ + account_index = + commodity_index = 0; + + // Read in the files that participated in this journal, so that they + // can be checked for changes on reading. + + if (! file.empty()) { + for (unsigned short i = 0, + count = read_binary_number<unsigned short>(in); + i < count; + i++) { + std::string path = read_binary_string(in); + if (i == 0 && path != file) + return 0; + + std::time_t old_mtime; + read_binary_number(in, old_mtime); + struct stat info; + stat(path.c_str(), &info); + if (std::difftime(info.st_mtime, old_mtime) > 0) + return 0; + + journal->sources.push_back(path); + } + } + + // Read all of the data in at once, so that we're just dealing with + // a big data buffer. + + unsigned long data_size = read_binary_number<unsigned long>(in); + + char * data_pool = new char[data_size]; + char * data = data_pool; + in.read(data, data_size); + + // Read in the accounts + + account_t::ident_t a_count = read_binary_number<account_t::ident_t>(data); + accounts = accounts_next = new (account_t *)[a_count]; + journal->master = read_binary_account(data, master); + + // Allocate the memory needed for the entries and transactions in + // one large block, which is then chopped up and custom constructed + // as necessary. + + unsigned long count = read_binary_number<unsigned long>(data); + unsigned long auto_count = read_binary_number<unsigned long>(data); + unsigned long period_count = read_binary_number<unsigned long>(data); + unsigned long xact_count = read_binary_number<unsigned long>(data); + unsigned long bigint_count = read_binary_number<unsigned long>(data); + + std::size_t pool_size = (sizeof(entry_t) * count + + sizeof(transaction_t) * xact_count + + sizeof_bigint_t() * bigint_count); + + char * item_pool = new char[pool_size]; + + entry_t * entry_pool = (entry_t *) item_pool; + transaction_t * xact_pool = (transaction_t *) (item_pool + + sizeof(entry_t) * count); + bigints_index = 0; + bigints = bigints_next = (item_pool + sizeof(entry_t) * count + + sizeof(transaction_t) * xact_count); + + // Read in the commodities + + commodity_t::ident_t c_count = read_binary_number<commodity_t::ident_t>(data); + commodities = commodities_next = new (commodity_t *)[c_count]; + for (commodity_t::ident_t i = 0; i < c_count; i++) { + commodity_t * commodity = read_binary_commodity(data); + std::pair<commodities_map::iterator, bool> result + = commodity_t::commodities.insert(commodities_pair(commodity->symbol, + commodity)); + assert(result.second); + } + + for (commodity_t::ident_t i = 0; i < c_count; i++) + read_binary_commodity_extra(data, i); + + // Read in the entries and transactions + + for (unsigned long i = 0; i < count; i++) { + new(entry_pool) entry_t; + read_binary_entry(data, entry_pool, xact_pool); + journal->entries.push_back(entry_pool++); + } + + for (unsigned long i = 0; i < auto_count; i++) { + auto_entry_t * auto_entry = new auto_entry_t; + read_binary_auto_entry(data, auto_entry, xact_pool); + journal->auto_entries.push_back(auto_entry); + } + + for (unsigned long i = 0; i < period_count; i++) { + period_entry_t * period_entry = new period_entry_t; + read_binary_period_entry(data, period_entry, xact_pool); + journal->period_entries.push_back(period_entry); + } + + // Clean up and return the number of entries read + + journal->item_pool = item_pool; + journal->item_pool_end = item_pool + pool_size; + + delete[] accounts; + delete[] commodities; + delete[] data_pool; + + return count; +} + +bool binary_parser_t::test(std::istream& in) const +{ + if (read_binary_number<unsigned long>(in) == binary_magic_number && + read_binary_number<unsigned long>(in) == format_version) + return true; + + in.seekg(0, std::ios::beg); + return false; +} + +unsigned int binary_parser_t::parse(std::istream& in, + journal_t * journal, + account_t * master, + const std::string * original_file) +{ + return read_binary_journal(in, original_file ? *original_file : "", + journal, master); +} + +#if DEBUG_LEVEL >= ALPHA +#define write_binary_guard(in, id) { \ + unsigned short guard = id; \ + out.write((char *)&guard, sizeof(guard)); \ +} +#else +#define write_binary_guard(in, id) +#endif + +template <typename T> +inline void write_binary_number(std::ostream& out, T num) { + out.write((char *)&num, sizeof(num)); +} + +inline void write_binary_string(std::ostream& out, const std::string& str) +{ + write_binary_guard(out, 0x3001); + + unsigned long len = str.length(); + 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) + out.write(str.c_str(), len); + + write_binary_guard(out, 0x3002); +} + +void write_binary_amount(std::ostream& out, const amount_t& amt) +{ + if (amt.commodity_) + write_binary_number(out, amt.commodity().ident); + else + write_binary_number<commodity_t::ident_t>(out, 0xffffffff); + + amt.write_quantity(out); +} + +void write_binary_transaction(std::ostream& out, transaction_t * xact) +{ + write_binary_number(out, xact->account->ident); + write_binary_amount(out, xact->amount); + if (xact->cost) { + write_binary_number<char>(out, 1); + write_binary_amount(out, *xact->cost); + } else { + write_binary_number<char>(out, 0); + } + write_binary_number(out, xact->flags); + write_binary_string(out, xact->note); +} + +void write_binary_entry_base(std::ostream& out, entry_base_t * entry) +{ + write_binary_number<unsigned long>(out, entry->transactions.size()); + for (transactions_list::const_iterator i = entry->transactions.begin(); + i != entry->transactions.end(); + i++) + write_binary_transaction(out, *i); +} + +void write_binary_entry(std::ostream& out, entry_t * entry) +{ + write_binary_number(out, entry->date); + write_binary_number(out, entry->state); + write_binary_string(out, entry->code); + write_binary_string(out, entry->payee); + write_binary_entry_base(out, entry); +} + +void write_binary_auto_entry(std::ostream& out, auto_entry_t * entry) +{ + write_binary_string(out, entry->predicate_string); + write_binary_entry_base(out, entry); +} + +void write_binary_period_entry(std::ostream& out, period_entry_t * entry) +{ + write_binary_string(out, entry->period_string); + write_binary_entry_base(out, entry); +} + +void write_binary_commodity(std::ostream& out, commodity_t * commodity) +{ + write_binary_string(out, commodity->symbol); + write_binary_number(out, commodity->quote); + write_binary_string(out, commodity->name); + write_binary_string(out, commodity->note); + write_binary_number(out, commodity->precision); + write_binary_number(out, commodity->flags); + commodity->ident = ++commodity_index; + write_binary_number(out, commodity->ident); +} + +void write_binary_commodity_extra(std::ostream& out, commodity_t * commodity) +{ + if (! commodity->history) { + write_binary_number<unsigned long>(out, 0); + } else { + write_binary_number<unsigned long>(out, commodity->history->prices.size()); + for (history_map::const_iterator i = commodity->history->prices.begin(); + i != commodity->history->prices.end(); + i++) { + write_binary_number(out, (*i).first); + write_binary_amount(out, (*i).second); + } + write_binary_number(out, commodity->history->last_lookup); + } + + if (commodity->smaller) { + write_binary_number<unsigned char>(out, 1); + write_binary_amount(out, *commodity->smaller); + } else { + write_binary_number<unsigned char>(out, 0); + } + + if (commodity->larger) { + write_binary_number<unsigned char>(out, 1); + write_binary_amount(out, *commodity->larger); + } else { + write_binary_number<unsigned char>(out, 0); + } +} + +static inline account_t::ident_t count_accounts(account_t * account) +{ + account_t::ident_t count = 1; + + for (accounts_map::iterator i = account->accounts.begin(); + i != account->accounts.end(); + i++) + count += count_accounts((*i).second); + + return count; +} + +void write_binary_account(std::ostream& out, account_t * account) +{ + account->ident = ++account_index; + + write_binary_number(out, account->ident); + if (account->parent) + write_binary_number(out, account->parent->ident); + else + write_binary_number<account_t::ident_t>(out, 0xffffffff); + + write_binary_string(out, account->name); + write_binary_string(out, account->note); + write_binary_number(out, account->depth); + + write_binary_number<account_t::ident_t>(out, account->accounts.size()); + for (accounts_map::iterator i = account->accounts.begin(); + i != account->accounts.end(); + i++) + write_binary_account(out, (*i).second); +} + +void write_binary_journal(std::ostream& out, journal_t * journal, + strings_list * files) +{ + write_binary_number(out, binary_magic_number); + write_binary_number(out, format_version); + + // Write out the files that participated in this journal, so that + // they can be checked for changes on reading. + + if (! files) { + write_binary_number<unsigned short>(out, 0); + } else { + write_binary_number<unsigned short>(out, files->size()); + for (strings_list::const_iterator i = files->begin(); + i != files->end(); + i++) { + write_binary_string(out, *i); + struct stat info; + stat((*i).c_str(), &info); + write_binary_number(out, std::time_t(info.st_mtime)); + } + } + + std::ostream::pos_type data_val = out.tellp(); + write_binary_number<unsigned long>(out, 0); + + // Write out the accounts + + write_binary_number<account_t::ident_t>(out, count_accounts(journal->master)); + write_binary_account(out, journal->master); + + // Write out the number of entries, transactions, and amounts + + write_binary_number<unsigned long>(out, journal->entries.size()); + write_binary_number<unsigned long>(out, journal->auto_entries.size()); + write_binary_number<unsigned long>(out, journal->period_entries.size()); + + std::ostream::pos_type xacts_val = out.tellp(); + write_binary_number<unsigned long>(out, 0); + std::ostream::pos_type bigints_val = out.tellp(); + write_binary_number<unsigned long>(out, 0); + bigints_count = 0; + + // Write out the commodities + + write_binary_number<commodity_t::ident_t> + (out, commodity_t::commodities.size() - 1); + + for (commodities_map::const_iterator i = commodity_t::commodities.begin(); + i != commodity_t::commodities.end(); + i++) + if (! (*i).first.empty()) + write_binary_commodity(out, (*i).second); + + for (commodities_map::const_iterator i = commodity_t::commodities.begin(); + i != commodity_t::commodities.end(); + i++) + if (! (*i).first.empty()) + write_binary_commodity_extra(out, (*i).second); + + // Write out the entries and transactions + + unsigned long xact_count = 0; + + for (entries_list::const_iterator i = journal->entries.begin(); + i != journal->entries.end(); + i++) { + write_binary_entry(out, *i); + xact_count += (*i)->transactions.size(); + } + + for (auto_entries_list::const_iterator i = journal->auto_entries.begin(); + i != journal->auto_entries.end(); + i++) { + write_binary_auto_entry(out, *i); + xact_count += (*i)->transactions.size(); + } + + for (period_entries_list::const_iterator i = journal->period_entries.begin(); + i != journal->period_entries.end(); + i++) { + write_binary_period_entry(out, *i); + xact_count += (*i)->transactions.size(); + } + + // Back-patch the count for amounts + + unsigned long data_size = (((unsigned long) out.tellp()) - + ((unsigned long) data_val) - + sizeof(unsigned long)); + out.seekp(data_val); + write_binary_number<unsigned long>(out, data_size); + out.seekp(xacts_val); + write_binary_number<unsigned long>(out, xact_count); + out.seekp(bigints_val); + write_binary_number<unsigned long>(out, bigints_count); +} + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> + +using namespace boost::python; +using namespace ledger; + +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(binary_parse_overloads, + binary_parser_t::parse, 2, 4) + +void py_write_binary_journal(const std::string& path, journal_t * journal) +{ + std::ofstream out(path.c_str()); + write_binary_journal(out, journal, &journal->sources); +} + +void export_binary() { + class_< binary_parser_t, bases<parser_t> > ("BinaryParser") + .def("test", &binary_parser_t::test) + .def("parse", &binary_parser_t::parse, binary_parse_overloads()) + ; + + def("write_binary_journal", py_write_binary_journal); +} + +#endif // USE_BOOST_PYTHON diff --git a/binary.h b/binary.h new file mode 100644 index 00000000..3fade8fe --- /dev/null +++ b/binary.h @@ -0,0 +1,26 @@ +#ifndef _BINARY_H +#define _BINARY_H + +#include "journal.h" +#include "parser.h" + +namespace ledger { + +class binary_parser_t : public parser_t +{ + public: + virtual bool test(std::istream& in) const; + + virtual unsigned int parse(std::istream& in, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL); +}; + +void write_binary_journal(std::ostream& out, + journal_t * journal, + strings_list * files = NULL); + +} // namespace ledger + +#endif // _BINARY_H diff --git a/config.cc b/config.cc new file mode 100644 index 00000000..5b25d066 --- /dev/null +++ b/config.cc @@ -0,0 +1,991 @@ +#include "config.h" +#include "option.h" +#include "quotes.h" +#include "walk.h" +#ifdef USE_BOOST_PYTHON +#include "py_eval.h" +#endif + +#include <fstream> +#include <ctime> +#include <cstdlib> +#ifdef WIN32 +#include <io.h> +#endif + +namespace ledger { + +config_t config; +std::list<option_t> config_options; + +config_t::config_t() +{ + amount_expr = "a"; + total_expr = "O"; + total_expr_template = "#"; + pricing_leeway = 24 * 3600; + budget_flags = BUDGET_NO_BUDGET; + balance_format = "%20T %2_%-a\n"; + register_format = ("%D %-.20P %-.22A %12.66t %12.80T\n%/" + "%32|%-.22A %12.66t %12.80T\n"); + wide_register_format = ("%D %-.35P %-.38A %22.108t %22.132T\n%/" + "%48|%-.38A %22.108t %22.132T\n"); + plot_amount_format = "%D %(St)\n"; + plot_total_format = "%D %(ST)\n"; + print_format = "\n%D %X%C%P\n %-34A %12o%n\n%/ %-34A %12o%n\n"; + equity_format = "\n%D %X%C%P\n%/ %-34A %12t\n"; +#ifndef USE_BOOST_PYTHON + prices_format = "%[%Y/%m/%d %H:%M:%S %Z] %-10A %12t %12T\n"; +#else + prices_format = ("%[%Y/%m/%d %H:%M:%S %Z] %-8A " + "%10t %10(@vmin(t)) %10(@vmax(t)) %12T\n"); +#endif + + show_collapsed = false; + show_subtotal = false; + show_totals = false; + show_related = false; + show_all_related = false; + show_inverted = false; + show_empty = false; + days_of_the_week = false; + by_payee = false; + comm_as_payee = false; + show_revalued = false; + show_revalued_only = false; + download_quotes = false; + + use_cache = false; + cache_dirty = false; +} + +static void +regexps_to_predicate(config_t& config, const std::string& command, + std::list<std::string>::const_iterator begin, + std::list<std::string>::const_iterator end, + const bool account_regexp = false, + const bool add_account_short_masks = false) +{ + std::string regexps[2]; + + // Treat the remaining command-line arguments as regular + // expressions, used for refining report results. + + for (std::list<std::string>::const_iterator i = begin; + i != end; + i++) + if ((*i)[0] == '-') { + if (! regexps[1].empty()) + regexps[1] += "|"; + regexps[1] += (*i).substr(1); + } + else if ((*i)[0] == '+') { + if (! regexps[0].empty()) + regexps[0] += "|"; + regexps[0] += (*i).substr(1); + } + else { + if (! regexps[0].empty()) + regexps[0] += "|"; + regexps[0] += *i; + } + + for (int i = 0; i < 2; i++) { + if (regexps[i].empty()) + continue; + + if (! config.predicate.empty()) + config.predicate += "&"; + + int add_predicate = 0; // 1 adds /.../, 2 adds ///.../ + if (i == 1) { + config.predicate += "!"; + } + else if (add_account_short_masks) { + if (regexps[i].find(':') != std::string::npos || + regexps[i].find('.') != std::string::npos || + regexps[i].find('*') != std::string::npos || + regexps[i].find('+') != std::string::npos || + regexps[i].find('[') != std::string::npos || + regexps[i].find('(') != std::string::npos) { + config.show_subtotal = true; + add_predicate = 1; + } else { + add_predicate = 2; + } + } + else { + add_predicate = 1; + } + + if (i != 1 && command == "b") { + if (! config.display_predicate.empty()) + config.display_predicate += "&"; + else if (! config.show_empty) + config.display_predicate += "T&"; + + if (add_predicate == 2) + config.display_predicate += "//"; + config.display_predicate += "/(?:"; + config.display_predicate += regexps[i]; + config.display_predicate += ")/"; + } + + if (! account_regexp) + config.predicate += "/"; + config.predicate += "/(?:"; + config.predicate += regexps[i]; + config.predicate += ")/"; + } +} + +void config_t::process_options(const std::string& command, + strings_list::iterator arg, + strings_list::iterator args_end) +{ + // Configure some other options depending on report type + + if (command == "p" || command == "e") { + show_related = + show_all_related = true; + } + else if (command == "E") { + show_subtotal = true; + } + else if (show_related) { + if (command == "r") { + show_inverted = true; + } else { + show_subtotal = true; + show_all_related = true; + } + } + + // Process remaining command-line arguments + + if (command != "e") { + // Treat the remaining command-line arguments as regular + // expressions, used for refining report results. + + std::list<std::string>::iterator i = arg; + for (; i != args_end; i++) + if (*i == "--") + break; + + regexps_to_predicate(*this, command, arg, i, true, + (command == "b" && ! show_subtotal && + display_predicate.empty())); + if (i != args_end) + regexps_to_predicate(*this, command, i, args_end); + } + + // Setup the default value for the display predicate + + if (display_predicate.empty()) { + if (command == "b") { + if (! show_empty) + display_predicate = "T"; + if (! show_subtotal) { + if (! display_predicate.empty()) + display_predicate += "&"; + display_predicate += "l<=1"; + } + } + else if (command == "E") { + display_predicate = "t"; + } + else if (command == "r") { + display_predicate = "a"; + } + } + + DEBUG_PRINT("ledger.config.predicates", "Predicate: " << predicate); + DEBUG_PRINT("ledger.config.predicates", "Display P: " << display_predicate); + + // Setup the values of %t and %T, used in format strings + + try { + ledger::amount_expr.reset(parse_value_expr(amount_expr)); + } + catch (const value_expr_error& err) { + throw error(std::string("In amount expression '") + amount_expr + + "': " + err.what()); + } + + std::string expr = total_expr_template; + for (std::string::size_type i = expr.find('#'); + i != std::string::npos; + i = expr.find('#')) + expr = (std::string(expr, 0, i) + "(" + total_expr + ")" + + std::string(expr, i + 1)); + + DEBUG_PRINT("ledger.config.total_expr", + "Total expression template = " << total_expr_template); + DEBUG_PRINT("ledger.config.total_expr", + "Total expression is now = " << expr); + try { + ledger::total_expr.reset(parse_value_expr(expr)); + } + catch (const value_expr_error& err) { + throw error(std::string("In total expression '") + expr + "': " + + err.what()); + } + + // If downloading is to be supported, configure the updater + + if (! commodity_t::updater && download_quotes) + commodity_t::updater = new quotes_by_script(price_db, pricing_leeway, + cache_dirty); + + if (! date_format.empty()) + format_t::date_format = date_format; +} + +void parse_ledger_data(journal_t * journal, parser_t * cache_parser, + parser_t * text_parser, parser_t * xml_parser) +{ + int entry_count = 0; + + DEBUG_PRINT("ledger.config.cache", "3. use_cache = " << config.use_cache); + + if (! config.init_file.empty() && + access(config.init_file.c_str(), R_OK) != -1) { + if (parse_journal_file(config.init_file, journal) || + journal->auto_entries.size() > 0 || + journal->period_entries.size() > 0) + throw error(std::string("Entries found in initialization file '") + + config.init_file + "'"); + + journal->sources.pop_front(); // remove init file + } + + if (cache_parser && config.use_cache && + ! config.cache_file.empty() && + ! config.data_file.empty()) { + DEBUG_PRINT("ledger.config.cache", "using_cache " << config.cache_file); + config.cache_dirty = true; + if (access(config.cache_file.c_str(), R_OK) != -1) { + std::ifstream stream(config.cache_file.c_str()); + if (cache_parser->test(stream)) { + entry_count += cache_parser->parse(stream, journal, NULL, + &config.data_file); + if (entry_count > 0) + config.cache_dirty = false; + } + } + } + + if (entry_count == 0 && ! config.data_file.empty()) { + account_t * account = NULL; + if (! config.account.empty()) + account = journal->find_account(config.account); + + // Add time commodity conversions, so that timelog's may be parsed + // in terms of seconds, but reported as minutes or hours. + commodity_t * commodity; + + commodity = commodity_t::find_commodity("s", true); + commodity->flags |= COMMODITY_STYLE_NOMARKET; + + parse_conversion("1.0m", "60s"); + parse_conversion("1.0h", "60m"); + + commodity = commodity_t::find_commodity("b", true); + commodity->flags |= COMMODITY_STYLE_NOMARKET; + + parse_conversion("1.00 Kb", "1024 b"); + parse_conversion("1.00 Mb", "1024 Kb"); + parse_conversion("1.00 Gb", "1024 Mb"); + parse_conversion("1.00 Tb", "1024 Gb"); + + std::string price_db; + if (! config.price_db.empty() && + access(config.price_db.c_str(), R_OK) != -1) { + if (parse_journal_file(config.price_db, journal)) + throw error("Entries not allowed in price history file"); + else { + price_db = config.price_db; + journal->sources.pop_back(); + } + } + + DEBUG_PRINT("ledger.config.cache", + "rejected cache, parsing " << config.data_file); + if (config.data_file == "-") { + config.use_cache = false; + journal->sources.push_back("<stdin>"); + if (xml_parser && std::cin.peek() == '<') + entry_count += xml_parser->parse(std::cin, journal, account); + else + entry_count += text_parser->parse(std::cin, journal, account); + } + else if (access(config.data_file.c_str(), R_OK) != -1) { + entry_count += parse_journal_file(config.data_file, journal, account); + if (! price_db.empty()) + journal->sources.push_back(price_db); + } + } + + if (entry_count == 0) + throw error("Please specify ledger file using -f," + " or LEDGER_FILE environment variable."); + + VALIDATE(journal->valid()); +} + +static void show_version(std::ostream& out) +{ + out << "Ledger " << ledger::version << ", the command-line accounting tool"; +#ifdef USE_BOOST_PYTHON + out << " (Python support enabled)"; +#endif + out << "\n\nCopyright (c) 2003-2004, John Wiegley. All rights reserved.\n\n\ +This program is made available under the terms of the BSD Public License.\n\ +See LICENSE file included with the distribution for details and disclaimer.\n"; +} + +void option_full_help(std::ostream& out) +{ + out << "usage: ledger [options] COMMAND [ACCT REGEX]... [-- [PAYEE REGEX]...]\n\n\ +Basic options:\n\ + -H, --full-help display this help text\n\ + -h, --help display summarized help text\n\ + -v, --version show version information\n\ + -f, --file FILE read ledger data from FILE\n\ + -o, --output FILE write output to FILE\n\ + -i, --init-file FILE initialize ledger using FILE (default: ~/.ledgerrc)\n\ + --cache FILE use FILE as a binary cache when --file is not used\n\ + -a, --account NAME use NAME for the default account (useful with QIF)\n\n\ +Report filtering:\n\ + -c, --current show only current and past entries (not future)\n\ + -b, --begin DATE set report begin date\n\ + -e, --end DATE set report end date\n\ + -p, --period STR report using the given period\n\ + --period-sort EXPR sort each report period's entries by EXPR\n\ + -C, --cleared consider only cleared transactions\n\ + -U, --uncleared consider only uncleared transactions\n\ + -R, --real consider only real (non-virtual) transactions\n\ + -L, --actual consider only actual (non-automated) transactions\n\ + -r, --related calculate report using related transactions\n\ + --budget generate budget entries based on FILE\n\ + --add-budget show all transactions plus the budget\n\ + --unbudgeted show only unbudgeted transactions\n\ + --forecast EXPR generate forecast entries while EXPR is true\n\ + -l, --limit EXPR calculate only transactions matching EXPR\n\ + -t, --amount EXPR use EXPR to calculate the displayed amount\n\ + -T, --total EXPR use EXPR to calculate the displayed total\n\n\ +Output customization:\n\ + -n, --collapse register: collapse entries; balance: no grand total\n\ + -s, --subtotal balance: show sub-accounts; other: show subtotals\n\ + -P, --by-payee show summarized totals by payee\n\ + -x, --comm-as-payee set commodity name as the payee, for reporting\n\ + -E, --empty balance: show accounts with zero balance\n\ + -W, --weekly show weekly sub-totals\n\ + -M, --monthly show monthly sub-totals\n\ + -Y, --yearly show yearly sub-totals\n\ + --dow show a days-of-the-week report\n\ + -S, --sort EXPR sort report according to the value expression EXPR\n\ + -w, --wide for the default register report, use 132 columns\n\ + -A, --average report average transaction amount\n\ + -D, --deviation report deviation from the average\n\ + -%, --percentage report balance totals as a percentile of the parent\n\ + --totals in the \"xml\" report, include running total\n\ + -j, --amount-data print only raw amount data (useful for scripting)\n\ + -J, --total-data print only raw total data\n\ + -d, --display EXPR display only transactions matching EXPR\n\ + -y, --date-format STR use STR as the date format (default: %Y/%m/%d)\n\ + -F, --format STR use STR as the format; for each report type, use:\n\ + --balance-format --register-format --print-format\n\ + --plot-amount-format --plot-total-format --equity-format\n\ + --prices-format --wide-register-format\n\ +Commodity reporting:\n\ + --price-db FILE sets the price database to FILE (def: ~/.pricedb)\n\ + -L, --price-exp MINS download quotes only if newer than MINS (def: 1440)\n\ + -Q, --download download price information when needed\n\ + -O, --quantity report commodity totals (this is the default)\n\ + -B, --basis report cost basis of commodities\n\ + -V, --market report last known market value\n\ + -g, --performance report gain/loss for each displayed transaction\n\ + -G, --gain report net gain/loss\n\n"; +#ifdef USE_BOOST_PYTHON + out + << "Python support:\n\ + --import MODULE on startup, import the given Python MODULE\n\ + --import-stdin on start, read Python code from standard input\n\n"; +#endif + out + << "Commands:\n\ + balance [REGEXP]... show balance totals for matching accounts\n\ + register [REGEXP]... show register of matching transactions\n\ + print [REGEXP]... print all matching entries\n\ + xml [REGEXP]... print matching entries in XML format\n\ + equity [REGEXP]... output equity entries for matching accounts\n\ + prices [REGEXP]... display price history for matching commodities\n\ + entry DATE PAYEE AMT output a derived entry, based on the arguments\n"; +} + +void option_help(std::ostream& out) +{ + out << "usage: ledger [options] COMMAND [ACCT REGEX]... [-- [PAYEE REGEX]...]\n\n\ +Use -H to see all the help text on one page, or:\n\ + --help-calc calculation options\n\ + --help-disp display options\n\ + --help-comm commodity options\n"; +#ifdef USE_BOOST_PYTHON + out << " --help-python Python options\n"; +#endif + out << "\nBasic options:\n\ + -h, --help display this help text\n\ + -v, --version show version information\n\ + -f, --file FILE read ledger data from FILE\n\ + -o, --output FILE write output to FILE\n\ + -i, --init-file FILE initialize ledger using FILE (default: ~/.ledgerrc)\n\ + --cache FILE use FILE as a binary cache when --file is not used\n\ + -a, --account NAME use NAME for the default account (useful with QIF)\n\n\ +Commands:\n\ + balance [REGEXP]... show balance totals for matching accounts\n\ + register [REGEXP]... show register of matching transactions\n\ + print [REGEXP]... print all matching entries\n\ + xml [REGEXP]... print matching entries in XML format\n\ + equity [REGEXP]... output equity entries for matching accounts\n\ + prices [REGEXP]... display price history for matching commodities\n\ + entry DATE PAYEE AMT output a derived entry, based on the arguments\n"; +} + +void option_calc_help(std::ostream& out) +{ + out << "Options to control how a report is calculated:\n\ + -c, --current show only current and past entries (not future)\n\ + -b, --begin DATE set report begin date\n\ + -e, --end DATE set report end date\n\ + -p, --period STR report using the given period\n\ + --period-sort EXPR sort each report period's entries by EXPR\n\ + -C, --cleared consider only cleared transactions\n\ + -U, --uncleared consider only uncleared transactions\n\ + -R, --real consider only real (non-virtual) transactions\n\ + -L, --actual consider only actual (non-automated) transactions\n\ + -r, --related calculate report using related transactions\n\ + --budget generate budget entries based on FILE\n\ + --add-budget show all transactions plus the budget\n\ + --unbudgeted show only unbudgeted transactions\n\ + --forecast EXPR generate forecast entries while EXPR is true\n\ + -l, --limit EXPR calculate only transactions matching EXPR\n\ + -t, --amount EXPR use EXPR to calculate the displayed amount\n\ + -T, --total EXPR use EXPR to calculate the displayed total\n"; +} + +void option_disp_help(std::ostream& out) +{ + out << "Output to control how report results are displayed:\n\ + -n, --collapse register: collapse entries; balance: no grand total\n\ + -s, --subtotal balance: show sub-accounts; other: show subtotals\n\ + -P, --by-payee show summarized totals by payee\n\ + -x, --comm-as-payee set commodity name as the payee, for reporting\n\ + -E, --empty balance: show accounts with zero balance\n\ + -W, --weekly show weekly sub-totals\n\ + -M, --monthly show monthly sub-totals\n\ + -Y, --yearly show yearly sub-totals\n\ + --dow show a days-of-the-week report\n\ + -S, --sort EXPR sort report according to the value expression EXPR\n\ + -w, --wide for the default register report, use 132 columns\n\ + -A, --average report average transaction amount\n\ + -D, --deviation report deviation from the average\n\ + -%, --percentage report balance totals as a percentile of the parent\n\ + --totals in the \"xml\" report, include running total\n\ + -j, --amount-data print only raw amount data (useful for scripting)\n\ + -J, --total-data print only raw total data\n\ + -d, --display EXPR display only transactions matching EXPR\n\ + -y, --date-format STR use STR as the date format (default: %Y/%m/%d)\n\ + -F, --format STR use STR as the format; for each report type, use:\n\ + --balance-format --register-format --print-format\n\ + --plot-amount-format --plot-total-format --equity-format\n\ + --prices-format --wide-register-format\n"; +} + +void option_comm_help(std::ostream& out) +{ + out << "Options to control how commodity values are determined:\n\ + --price-db FILE sets the price database to FILE (def: ~/.pricedb)\n\ + -Z, --price-exp MINS download quotes only if newer than MINS (def: 1440)\n\ + -Q, --download download price information when needed\n\ + -O, --quantity report commodity totals (this is the default)\n\ + -B, --basis report cost basis of commodities\n\ + -V, --market report last known market value\n\ + -g, --performance report gain/loss for each displayed transaction\n\ + -G, --gain report net gain/loss\n"; +} + +#ifdef USE_BOOST_PYTHON +void option_python_help(std::ostream& out) +{ + out << "Python support options:\n\ + --import MODULE on startup, import the given Python MODULE\n\ + --import-stdin on start, read Python code from standard input\n\n"; +} +#endif + +////////////////////////////////////////////////////////////////////// +// +// Basic options + +OPT_BEGIN(full_help, "H") { + option_full_help(std::cout); + throw 0; +} OPT_END(full_help); + +OPT_BEGIN(help, "h") { + option_help(std::cout); + throw 0; +} OPT_END(help); + +OPT_BEGIN(help_calc, "") { + option_calc_help(std::cout); + throw 0; +} OPT_END(help_calc); + +OPT_BEGIN(help_disp, "") { + option_disp_help(std::cout); + throw 0; +} OPT_END(help_disp); + +OPT_BEGIN(help_comm, "") { + option_comm_help(std::cout); + throw 0; +} OPT_END(help_comm); + +#ifdef USE_BOOST_PYTHON +OPT_BEGIN(help_python, "") { + option_python_help(std::cout); + throw 0; +} OPT_END(help_python); +#endif + +OPT_BEGIN(version, "v") { + show_version(std::cout); + throw 0; +} OPT_END(version); + +OPT_BEGIN(init_file, "i:") { + config.init_file = optarg; +} OPT_END(init_file); + +OPT_BEGIN(file, "f:") { + if (std::string(optarg) == "-" || access(optarg, R_OK) != -1) + config.data_file = optarg; + else + throw error(std::string("The ledger file '") + optarg + + "' does not exist or is not readable"); +} OPT_END(file); + +OPT_BEGIN(cache, ":") { + config.cache_file = optarg; +} OPT_END(cache); + +OPT_BEGIN(output, "o:") { + if (std::string(optarg) != "-") + config.output_file = optarg; +} OPT_END(output); + +OPT_BEGIN(account, "a:") { + config.account = optarg; +} OPT_END(account); + +////////////////////////////////////////////////////////////////////// +// +// Report filtering + +OPT_BEGIN(begin, "b:") { + if (! config.predicate.empty()) + config.predicate += "&"; + config.predicate += "d>=["; + config.predicate += optarg; + config.predicate += "]"; +} OPT_END(begin); + +OPT_BEGIN(end, "e:") { + if (! config.predicate.empty()) + config.predicate += "&"; + config.predicate += "d<["; + config.predicate += optarg; + config.predicate += "]"; +} OPT_END(end); + +OPT_BEGIN(current, "c") { + if (! config.predicate.empty()) + config.predicate += "&"; + config.predicate += "d<=m"; +} OPT_END(current); + +OPT_BEGIN(cleared, "C") { + if (! config.predicate.empty()) + config.predicate += "&"; + config.predicate += "X"; +} OPT_END(cleared); + +OPT_BEGIN(uncleared, "U") { + if (! config.predicate.empty()) + config.predicate += "&"; + config.predicate += "!X"; +} OPT_END(uncleared); + +OPT_BEGIN(real, "R") { + if (! config.predicate.empty()) + config.predicate += "&"; + config.predicate += "R"; +} OPT_END(real); + +OPT_BEGIN(actual, "L") { + if (! config.predicate.empty()) + config.predicate += "&"; + config.predicate += "L"; +} OPT_END(actual); + +////////////////////////////////////////////////////////////////////// +// +// Output customization + +OPT_BEGIN(format, "F:") { + config.format_string = optarg; +} OPT_END(format); + +OPT_BEGIN(date_format, "y:") { + config.date_format = optarg; +} OPT_END(date_format); + +OPT_BEGIN(balance_format, ":") { + config.balance_format = optarg; +} OPT_END(balance_format); + +OPT_BEGIN(register_format, ":") { + config.register_format = optarg; +} OPT_END(register_format); + +OPT_BEGIN(wide_register_format, ":") { + config.wide_register_format = optarg; +} OPT_END(wide_register_format); + +OPT_BEGIN(plot_amount_format, ":") { + config.plot_amount_format = optarg; +} OPT_END(plot_amount_format); + +OPT_BEGIN(plot_total_format, ":") { + config.plot_total_format = optarg; +} OPT_END(plot_total_format); + +OPT_BEGIN(print_format, ":") { + config.print_format = optarg; +} OPT_END(print_format); + +OPT_BEGIN(equity_format, ":") { + config.equity_format = optarg; +} OPT_END(equity_format); + +OPT_BEGIN(prices_format, ":") { + config.prices_format = optarg; +} OPT_END(prices_format); + +OPT_BEGIN(wide, "w") { + config.register_format = config.wide_register_format; +} OPT_END(wide); + +OPT_BEGIN(empty, "E") { + config.show_empty = true; +} OPT_END(empty); + +OPT_BEGIN(collapse, "n") { + config.show_collapsed = true; +} OPT_END(collapse); + +OPT_BEGIN(subtotal, "s") { + config.show_subtotal = true; +} OPT_END(subtotal); + +OPT_BEGIN(totals, "") { + config.show_totals = true; +} OPT_END(totals); + +OPT_BEGIN(sort, "S:") { + config.sort_string = optarg; +} OPT_END(sort); + +OPT_BEGIN(related, "r") { + config.show_related = true; +} OPT_END(related); + +OPT_BEGIN(period, "p:") { + if (config.report_period.empty()) { + config.report_period = optarg; + } else { + config.report_period += " "; + config.report_period += optarg; + } + + // If the period gives a beginning and/or ending date, make sure to + // modify the calculation predicate (via the --begin and --end + // options) to take this into account. + + char buf[32]; + interval_t interval(config.report_period); + if (interval.begin) { + std::strftime(buf, 31, "%Y/%m/%d", std::gmtime(&interval.begin)); + process_option(config_options, "begin", buf); + } + if (interval.end) { + std::strftime(buf, 31, "%Y/%m/%d", std::gmtime(&interval.end)); + process_option(config_options, "end", buf); + } +} OPT_END(period); + +OPT_BEGIN(period_sort, ":") { + config.report_period_sort = optarg; +} OPT_END(period_sort); + +OPT_BEGIN(weekly, "W") { + if (config.report_period.empty()) + config.report_period = "weekly"; + else + config.report_period = std::string("weekly ") + config.report_period; +} OPT_END(weekly); + +OPT_BEGIN(monthly, "M") { + if (config.report_period.empty()) + config.report_period = "monthly"; + else + config.report_period = std::string("monthly ") + config.report_period; +} OPT_END(monthly); + +OPT_BEGIN(yearly, "Y") { + if (config.report_period.empty()) + config.report_period = "yearly"; + else + config.report_period = std::string("yearly ") + config.report_period; +} OPT_END(yearly); + +OPT_BEGIN(dow, "") { + config.days_of_the_week = true; +} OPT_END(dow); + +OPT_BEGIN(by_payee, "P") { + config.by_payee = true; +} OPT_END(by_payee); + +OPT_BEGIN(comm_as_payee, "x") { + config.comm_as_payee = true; +} OPT_END(comm_as_payee); + +OPT_BEGIN(budget, "") { + config.budget_flags = BUDGET_BUDGETED; +} OPT_END(budget); + +OPT_BEGIN(add_budget, "") { + config.budget_flags = BUDGET_BUDGETED | BUDGET_UNBUDGETED; +} OPT_END(add_budget); + +OPT_BEGIN(unbudgeted, "") { + config.budget_flags = BUDGET_UNBUDGETED; +} OPT_END(unbudgeted); + +OPT_BEGIN(forecast, ":") { + config.forecast_limit = optarg; +} OPT_END(forecast); + +OPT_BEGIN(limit, "l:") { + if (! config.predicate.empty()) + config.predicate += "&"; + config.predicate += "("; + config.predicate += optarg; + config.predicate += ")"; +} OPT_END(limit); + +OPT_BEGIN(display, "d:") { + if (! config.display_predicate.empty()) + config.display_predicate += "&"; + config.display_predicate += "("; + config.display_predicate += optarg; + config.display_predicate += ")"; +} OPT_END(display); + +OPT_BEGIN(amount, "t:") { + config.amount_expr = optarg; +} OPT_END(amount); + +OPT_BEGIN(total, "T:") { + config.total_expr = optarg; +} OPT_END(total); + +OPT_BEGIN(amount_data, "j") { + config.format_string = config.plot_amount_format; +} OPT_END(amount_data); + +OPT_BEGIN(total_data, "J") { + config.format_string = config.plot_total_format; +} OPT_END(total_data); + +////////////////////////////////////////////////////////////////////// +// +// Commodity reporting + +OPT_BEGIN(price_db, ":") { + config.price_db = optarg; +} OPT_END(price_db); + +OPT_BEGIN(price_exp, "Z:") { + config.pricing_leeway = std::atol(optarg) * 60; +} OPT_END(price_exp); + +OPT_BEGIN(download, "Q") { + config.download_quotes = true; +} OPT_END(download); + +OPT_BEGIN(quantity, "O") { + config.amount_expr = "a"; + config.total_expr = "O"; +} OPT_END(quantity); + +OPT_BEGIN(basis, "B") { + config.amount_expr = "b"; + config.total_expr = "B"; +} OPT_END(basis); + +OPT_BEGIN(market, "V") { + config.show_revalued = true; + + config.amount_expr = "v"; + config.total_expr = "V"; +} OPT_END(market); + +OPT_BEGIN(performance, "g") { + config.amount_expr = "P(a,m)-b"; // same as 'g', but priced now + config.total_expr = "P(O,m)-B"; +} OPT_END(performance); + +OPT_BEGIN(gain, "G") { + config.show_revalued = + config.show_revalued_only = true; + + config.amount_expr = "a"; + config.total_expr = "G"; +} OPT_END(gain); + +OPT_BEGIN(average, "A") { + config.total_expr_template = "A#"; +} OPT_END(average); + +OPT_BEGIN(deviation, "D") { + config.total_expr_template = "t-A#"; +} OPT_END(deviation); + +OPT_BEGIN(percentage, "%") { + config.total_expr_template = "^#&{100.0%}*(#/^#)"; +} OPT_END(percentage); + +#ifdef USE_BOOST_PYTHON + +////////////////////////////////////////////////////////////////////// +// +// Python support + +OPT_BEGIN(import, ":") { + python_eval(std::string("import ") + optarg, PY_EVAL_STMT); +} OPT_END(import); + +OPT_BEGIN(import_stdin, "") { + python_eval(std::cin, PY_EVAL_MULTI); +} OPT_END(import_stdin); + +#endif // USE_BOOST_PYTHON + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> +#include <boost/python/detail/api_placeholder.hpp> + +using namespace boost::python; +using namespace ledger; + +void py_process_options(config_t& config, const std::string& command, + list args) +{ + strings_list strs; + + int l = len(args); + for (int i = 0; i < l; i++) + strs.push_back(std::string(extract<char *>(args[i]))); + + config.process_options(command, strs.begin(), strs.end()); +} + +void add_other_option_handlers(const std::list<option_t>& other); + +void py_add_config_option_handlers() +{ + add_other_option_handlers(config_options); +} + +BOOST_PYTHON_FUNCTION_OVERLOADS(parse_ledger_data_overloads, + parse_ledger_data, 1, 2) + +void py_option_help() +{ + option_help(std::cout); +} + +void export_config() +{ + class_< config_t > ("Config") + .def_readwrite("init_file", &config_t::init_file) + .def_readwrite("data_file", &config_t::data_file) + .def_readwrite("cache_file", &config_t::cache_file) + .def_readwrite("price_db", &config_t::price_db) + .def_readwrite("output_file", &config_t::output_file) + .def_readwrite("account", &config_t::account) + .def_readwrite("predicate", &config_t::predicate) + .def_readwrite("display_predicate", &config_t::display_predicate) + .def_readwrite("report_period", &config_t::report_period) + .def_readwrite("report_period_sort", &config_t::report_period_sort) + .def_readwrite("format_string", &config_t::format_string) + .def_readwrite("balance_format", &config_t::balance_format) + .def_readwrite("register_format", &config_t::register_format) + .def_readwrite("wide_register_format", &config_t::wide_register_format) + .def_readwrite("plot_amount_format", &config_t::plot_amount_format) + .def_readwrite("plot_total_format", &config_t::plot_total_format) + .def_readwrite("print_format", &config_t::print_format) + .def_readwrite("equity_format", &config_t::equity_format) + .def_readwrite("prices_format", &config_t::prices_format) + .def_readwrite("date_format", &config_t::date_format) + .def_readwrite("sort_string", &config_t::sort_string) + .def_readwrite("amount_expr", &config_t::amount_expr) + .def_readwrite("total_expr", &config_t::total_expr) + .def_readwrite("total_expr_template", &config_t::total_expr_template) + .def_readwrite("forecast_limit", &config_t::forecast_limit) + .def_readwrite("budget_flags", &config_t::budget_flags) + .def_readwrite("pricing_leeway", &config_t::pricing_leeway) + .def_readwrite("show_collapsed", &config_t::show_collapsed) + .def_readwrite("show_subtotal", &config_t::show_subtotal) + .def_readwrite("show_totals", &config_t::show_totals) + .def_readwrite("show_related", &config_t::show_related) + .def_readwrite("show_all_related", &config_t::show_all_related) + .def_readwrite("show_inverted", &config_t::show_inverted) + .def_readwrite("show_empty", &config_t::show_empty) + .def_readwrite("days_of_the_week", &config_t::days_of_the_week) + .def_readwrite("by_payee", &config_t::by_payee) + .def_readwrite("comm_as_payee", &config_t::comm_as_payee) + .def_readwrite("show_revalued", &config_t::show_revalued) + .def_readwrite("show_revalued_only", &config_t::show_revalued_only) + .def_readwrite("download_quotes", &config_t::download_quotes) + .def_readwrite("use_cache", &config_t::use_cache) + .def_readwrite("cache_dirty", &config_t::cache_dirty) + + .def("process_options", py_process_options) + ; + + scope().attr("config") = ptr(&config); + + def("option_help", py_option_help); + def("parse_ledger_data", parse_ledger_data, parse_ledger_data_overloads()); + def("add_config_option_handlers", py_add_config_option_handlers); +} + +#endif // USE_BOOST_PYTHON diff --git a/config.h b/config.h new file mode 100644 index 00000000..cb0db8b7 --- /dev/null +++ b/config.h @@ -0,0 +1,103 @@ +#ifndef _CONFIG_H +#define _CONFIG_H + +#include "journal.h" +#include "option.h" +#include "valexpr.h" +#include "datetime.h" +#include "format.h" +#include "parser.h" + +#include <iostream> +#include <memory> +#include <list> + +namespace ledger { + +struct config_t +{ + // These options can all be set used text fields. + + strings_list price_settings; + std::string init_file; + std::string data_file; + std::string cache_file; + std::string price_db; + std::string output_file; + std::string account; + std::string predicate; + std::string display_predicate; + std::string report_period; + std::string report_period_sort; + std::string format_string; + std::string balance_format; + std::string register_format; + std::string wide_register_format; + std::string plot_amount_format; + std::string plot_total_format; + std::string print_format; + std::string equity_format; + std::string prices_format; + std::string date_format; + std::string sort_string; + std::string amount_expr; + std::string total_expr; + std::string total_expr_template; + std::string forecast_limit; + unsigned long budget_flags; + unsigned long pricing_leeway; + bool show_collapsed; + bool show_subtotal; + bool show_totals; + bool show_related; + bool show_all_related; + bool show_inverted; + bool show_empty; + bool days_of_the_week; + bool by_payee; + bool comm_as_payee; + bool show_revalued; + bool show_revalued_only; + bool download_quotes; + bool use_cache; + bool cache_dirty; + + config_t(); + config_t(const config_t&) { + assert(0); + } + + void process_options(const std::string& command, + strings_list::iterator arg, + strings_list::iterator args_end); +}; + +extern config_t config; +extern std::list<option_t> config_options; + +void option_help(std::ostream& out); + +// Parse what ledger data can be determined from the config settings +void parse_ledger_data(journal_t * journal, + parser_t * cache_parser = NULL, + parser_t * text_parser = NULL, + parser_t * xml_parser = NULL); + +struct declared_option_handler : public option_handler { + declared_option_handler(const std::string& label, + const std::string& opt_chars) { + add_option_handler(config_options, label, opt_chars, *this); + } +}; + +#define OPT_BEGIN(tag, chars) \ + static struct opt_ ## tag ## _handler \ + : public declared_option_handler { \ + opt_ ## tag ## _handler() : declared_option_handler(#tag, chars) {} \ + virtual void operator()(const char * optarg) + +#define OPT_END(tag) } opt_ ## tag ## _handler_obj + +} // namespace ledger + +#endif // _CONFIG_H diff --git a/configure.in b/configure.in new file mode 100644 index 00000000..4d568016 --- /dev/null +++ b/configure.in @@ -0,0 +1,96 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.59) +AC_INIT(ledger, 2.0, johnw@newartisans.com) +AM_INIT_AUTOMAKE(ledger, 2.0) +AC_CONFIG_SRCDIR([main.cc]) +AC_CONFIG_HEADER([acconf.h]) + +# Checks for programs. +AC_PROG_CXX +AC_PROG_MAKE_SET +AC_PROG_RANLIB + +# Checks for libraries. +AC_CHECK_LIB([gmp], [__gmpz_add], [], + AC_MSG_FAILURE("Could not find gmp (GNU multi-precision) library")) +AC_CHECK_LIB([pcre], [pcre_compile], [], + AC_MSG_FAILURE("Could not find pcre (Perl regular expression) library")) +AC_CHECK_LIB([xmlparse], [XML_ParserCreate], + [AC_DEFINE([HAVE_XMLPARSE], [1], [Support reading Gnucash and XML files]) + AM_CONDITIONAL(HAVE_XMLPARSE, true) + AC_SUBST(LIBS, "-lxmlparse -lxmltok $LIBS")], + [AC_MSG_NOTICE([Could not find xmlparse library: Gnucash and XML support disabled]) + AM_CONDITIONAL(HAVE_XMLPARSE, false)], + [-lxmltok]) + +AC_ARG_ENABLE(python, + [ --enable-python Turn on Python support], + [case "${enableval}" in + yes) python=true ;; + no) python=false ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-python) ;; + esac],[python=false]) +AM_CONDITIONAL(USE_PYTHON, test x$python = xtrue) + +if [test x$python = xtrue ]; then + AM_PATH_PYTHON(2.2,, :) + if [test "$PYTHON" != :]; then + AC_CACHE_CHECK( + [if boost_python is available], + [boost_python_cpplib_avail], + [boost_python_save_libs=$LIBS + LIBS="-lboost_python $LIBS" + AC_LANG_PUSH(C++) + AC_TRY_COMPILE( + [#include <boost/python.hpp> + using namespace boost::python; + class foo {}; + BOOST_PYTHON_MODULE(samp) { + class_< foo > ("foo") ; + }], + [return 0], + [boost_python_cpplib_avail=true], + [boost_python_cpplib_avail=false]) + AC_LANG_POP + LIBS=$boost_python_save_libs]) + AM_CONDITIONAL(HAVE_BOOST_PYTHON, + test x$boost_python_cpplib_avail = xtrue) + else + AM_CONDITIONAL(HAVE_BOOST_PYTHON, false) + fi +else + AM_CONDITIONAL(HAVE_BOOST_PYTHON, false) +fi + +# Check for options +AC_ARG_ENABLE(debug, + [ --enable-debug Turn on debugging], + [case "${enableval}" in + yes) debug=true ;; + no) debug=false ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-debug) ;; + esac],[debug=false]) +AM_CONDITIONAL(DEBUG, test x$debug = xtrue) + +# Checks for header files. +AC_STDC_HEADERS +AC_HAVE_HEADERS(sys/stat.h) + +# Checks for typedefs, structures, and compiler characteristics. +AC_HEADER_STDBOOL +AC_TYPE_SIZE_T +AC_STRUCT_TM + +# Checks for library functions. +#AC_FUNC_ERROR_AT_LINE +AC_HEADER_STDC +#AC_FUNC_MALLOC +#AC_FUNC_MKTIME +#AC_FUNC_STAT +#AC_FUNC_STRFTIME +AC_CHECK_FUNCS([memset strchr strstr access mktime stat strftime strptime]) + +AC_CONFIG_FILES([Makefile]) +AC_OUTPUT diff --git a/configure.lt b/configure.lt new file mode 100644 index 00000000..2eedb0e4 --- /dev/null +++ b/configure.lt @@ -0,0 +1,97 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.59) +AC_INIT(ledger, 2.0, johnw@newartisans.com) +AM_INIT_AUTOMAKE(ledger, 2.0) +AC_CONFIG_SRCDIR([main.cc]) +AC_CONFIG_HEADER([acconf.h]) + +# Checks for programs. +AC_PROG_CXX +AC_PROG_MAKE_SET +AC_PROG_LIBTOOL +AM_PROG_LIBTOOL + +# Checks for libraries. +AC_CHECK_LIB([gmp], [__gmpz_add], [], + AC_MSG_FAILURE("Could not find gmp (GNU multi-precision) library")) +AC_CHECK_LIB([pcre], [pcre_compile], [], + AC_MSG_FAILURE("Could not find pcre (Perl regular expression) library")) +AC_CHECK_LIB([xmlparse], [XML_ParserCreate], + [AC_DEFINE([HAVE_XMLPARSE], [1], [Support reading Gnucash and XML files]) + AM_CONDITIONAL(HAVE_XMLPARSE, true) + AC_SUBST(LIBS, "-lxmlparse -lxmltok $LIBS")], + [AC_MSG_NOTICE([Could not find xmlparse library: Gnucash and XML support disabled]) + AM_CONDITIONAL(HAVE_XMLPARSE, false)], + [-lxmltok]) + +AC_ARG_ENABLE(python, + [ --enable-python Turn on Python support], + [case "${enableval}" in + yes) python=true ;; + no) python=false ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-python) ;; + esac],[python=false]) +AM_CONDITIONAL(USE_PYTHON, test x$python = xtrue) + +if [test x$python = xtrue ]; then + AM_PATH_PYTHON(2.2,, :) + if [test "$PYTHON" != :]; then + AC_CACHE_CHECK( + [if boost_python is available], + [boost_python_cpplib_avail], + [boost_python_save_libs=$LIBS + LIBS="-lboost_python $LIBS" + AC_LANG_PUSH(C++) + AC_TRY_COMPILE( + [#include <boost/python.hpp> + using namespace boost::python; + class foo {}; + BOOST_PYTHON_MODULE(samp) { + class_< foo > ("foo") ; + }], + [return 0], + [boost_python_cpplib_avail=true], + [boost_python_cpplib_avail=false]) + AC_LANG_POP + LIBS=$boost_python_save_libs]) + AM_CONDITIONAL(HAVE_BOOST_PYTHON, + test x$boost_python_cpplib_avail = xtrue) + else + AM_CONDITIONAL(HAVE_BOOST_PYTHON, false) + fi +else + AM_CONDITIONAL(HAVE_BOOST_PYTHON, false) +fi + +# Check for options +AC_ARG_ENABLE(debug, + [ --enable-debug Turn on debugging], + [case "${enableval}" in + yes) debug=true ;; + no) debug=false ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-debug) ;; + esac],[debug=false]) +AM_CONDITIONAL(DEBUG, test x$debug = xtrue) + +# Checks for header files. +AC_STDC_HEADERS +AC_HAVE_HEADERS(sys/stat.h) + +# Checks for typedefs, structures, and compiler characteristics. +AC_HEADER_STDBOOL +AC_TYPE_SIZE_T +AC_STRUCT_TM + +# Checks for library functions. +#AC_FUNC_ERROR_AT_LINE +AC_HEADER_STDC +#AC_FUNC_MALLOC +#AC_FUNC_MKTIME +#AC_FUNC_STAT +#AC_FUNC_STRFTIME +AC_CHECK_FUNCS([memset strchr strstr access mktime stat strftime strptime]) + +AC_CONFIG_FILES([Makefile]) +AC_OUTPUT diff --git a/datetime.cc b/datetime.cc new file mode 100644 index 00000000..be4a3026 --- /dev/null +++ b/datetime.cc @@ -0,0 +1,419 @@ +#include "datetime.h" +#include "error.h" + +#include <ctime> +#include <cctype> + +namespace ledger { + +std::time_t now = std::time(NULL); +int now_year = std::localtime(&now)->tm_year; + +static std::time_t base = -1; +static int base_year = -1; + +static const int month_days[12] = { + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 +}; + +static const char * formats[] = { + "%Y/%m/%d", + "%m/%d", + "%Y.%m.%d", + "%m.%d", + "%Y-%m-%d", + "%m-%d", + "%a", + "%A", + "%b", + "%B", + "%Y", + NULL +}; + +std::time_t interval_t::first(const std::time_t moment) const +{ + std::time_t quant = begin; + + if (moment && std::difftime(moment, quant) > 0) { + // Find an efficient starting point for the upcoming while loop. + // We want a date early enough that the range will be correct, but + // late enough that we don't spend hundreds of thousands of loops + // skipping through time. + + struct std::tm * desc = std::gmtime(&moment); + if (years) + desc->tm_mon = 0; + desc->tm_mday = 1; + desc->tm_hour = 0; + desc->tm_min = 0; + desc->tm_sec = 0; + quant = std::mktime(desc); + + std::time_t temp; + while (std::difftime(moment, temp = increment(quant)) >= 0) { + if (quant == temp) + break; + quant = temp; + } + } + + return quant; +} + +std::time_t interval_t::increment(const std::time_t moment) const +{ + std::time_t then = moment; + + if (years || months) { + struct std::tm * desc = std::gmtime(&then); + + if (years) + desc->tm_year += years; + + if (months) { + desc->tm_mon += months; + + if (desc->tm_mon > 11) { + desc->tm_year++; + desc->tm_mon -= 12; + } + else if (desc->tm_mon < 0) { + desc->tm_year--; + desc->tm_mon += 12; + } + } + + desc->tm_hour = 0; + desc->tm_min = 0; + desc->tm_sec = 0; + + then = std::mktime(desc); + } + + return then + seconds; +} + +static void parse_inclusion_specifier(const std::string& word, + std::time_t * begin, + std::time_t * end) +{ + struct std::tm when; + + if (! parse_date_mask(word.c_str(), &when)) + throw interval_expr_error(std::string("Could not parse date mask: ") + + word); + + when.tm_hour = 0; + when.tm_min = 0; + when.tm_sec = 0; + + bool saw_year = true; + bool saw_mon = true; + + if (when.tm_year == -1) { + when.tm_year = now_year; + saw_year = false; + } + if (when.tm_mon == -1) { + when.tm_mon = 0; + saw_mon = false; + } else { + saw_year = false; // don't increment by year if month used + } + if (when.tm_mday == -1) + when.tm_mday = 1; + + if (begin) { + *begin = std::mktime(&when); + if (end) + *end = interval_t(0, saw_mon ? 1 : 0, saw_year ? 1 : 0).increment(*begin); + } + else if (end) { + *end = std::mktime(&when); + } +} + +static inline +void read_lower_word(std::istream& in, std::string& word) { + in >> word; + for (int i = 0, l = word.length(); i < l; i++) + word[i] = std::tolower(word[i]); +} + +static void parse_date_words(std::istream& in, std::string& word, + std::time_t * begin, std::time_t * end) +{ + std::string type; + bool mon_spec = false; + char buf[32]; + + if (word == "this" || word == "last" || word == "next") { + type = word; + if (! in.eof()) + read_lower_word(in, word); + else + word = "month"; + } else { + type = "this"; + } + + if (word == "month") { + std::strftime(buf, 31, "%B", std::localtime(&now)); + word = buf; + mon_spec = true; + } + else if (word == "year") { + std::strftime(buf, 31, "%Y", std::localtime(&now)); + word = buf; + } + + parse_inclusion_specifier(word, begin, end); + + if (type == "last") { + if (mon_spec) { + if (begin) + *begin = interval_t(0, -1, 0).increment(*begin); + if (end) + *end = interval_t(0, -1, 0).increment(*end); + } else { + if (begin) + *begin = interval_t(0, 0, -1).increment(*begin); + if (end) + *end = interval_t(0, 0, -1).increment(*end); + } + } + else if (type == "next") { + if (mon_spec) { + if (begin) + *begin = interval_t(0, 1, 0).increment(*begin); + if (end) + *end = interval_t(0, 1, 0).increment(*end); + } else { + if (begin) + *begin = interval_t(0, 0, 1).increment(*begin); + if (end) + *end = interval_t(0, 0, 1).increment(*end); + } + } +} + +void interval_t::parse(std::istream& in) +{ + std::string word; + + while (! in.eof()) { + read_lower_word(in, word); + if (word == "every") { + read_lower_word(in, word); + if (std::isdigit(word[0])) { + int quantity = std::atol(word.c_str()); + read_lower_word(in, word); + if (word == "days") + seconds = 86400 * quantity; + else if (word == "weeks") + seconds = 7 * 86400 * quantity; + else if (word == "months") + months = quantity; + else if (word == "quarters") + months = 3 * quantity; + else if (word == "years") + years = quantity; + } + else if (word == "day") + seconds = 86400; + else if (word == "week") + seconds = 7 * 86400; + else if (word == "monthly") + months = 1; + else if (word == "quarter") + months = 3; + else if (word == "year") + years = 1; + } + else if (word == "daily") + seconds = 86400; + else if (word == "weekly") + seconds = 7 * 86400; + else if (word == "biweekly") + seconds = 14 * 86400; + else if (word == "monthly") + months = 1; + else if (word == "bimonthly") + months = 2; + else if (word == "quarterly") + months = 3; + else if (word == "yearly") + years = 1; + else if (word == "this" || word == "last" || word == "next") { + parse_date_words(in, word, &begin, &end); + } + else if (word == "in") { + read_lower_word(in, word); + parse_date_words(in, word, &begin, &end); + } + else if (word == "from" || word == "since") { + read_lower_word(in, word); + parse_date_words(in, word, &begin, NULL); + } + else if (word == "to" || word == "until") { + read_lower_word(in, word); + parse_date_words(in, word, NULL, &end); + } + else { + parse_inclusion_specifier(word, &begin, &end); + } + } +} + +bool parse_date_mask(const char * date_str, struct std::tm * result) +{ + for (const char ** f = formats; *f; f++) { + memset(result, INT_MAX, sizeof(struct std::tm)); + if (strptime(date_str, *f, result)) + return true; + } + return false; +} + +bool parse_date(const char * date_str, std::time_t * result, const int year) +{ + struct std::tm when; + + if (! parse_date_mask(date_str, &when)) + return false; + + when.tm_hour = 0; + when.tm_min = 0; + when.tm_sec = 0; + + if (when.tm_year == -1) + when.tm_year = ((year == -1) ? now_year : (year - 1900)); + + if (when.tm_mon == -1) + when.tm_mon = 0; + + if (when.tm_mday == -1) + when.tm_mday = 1; + + *result = std::mktime(&when); + + return true; +} + +bool quick_parse_date(const char * date_str, std::time_t * result) +{ + int year = -1, month = -1, day, num = 0; + + for (const char * p = date_str; *p; p++) { + if (*p == '/' || *p == '-' || *p == '.') { + if (year == -1) + year = num; + else + month = num; + num = 0; + } + else if (*p < '0' || *p > '9') { + return false; + } + else { + num *= 10; + num += *p - '0'; + } + } + + day = num; + + if (month == -1) { + month = year; + year = now_year + 1900; + } + + if (base == -1 || year != base_year) { + struct std::tm when; + std::memset(&when, 0, sizeof(when)); + + base_year = year == -1 ? now_year + 1900 : year; + when.tm_year = year == -1 ? now_year : year - 1900; + when.tm_mday = 1; + + base = std::mktime(&when); + year = base_year; + } + + *result = base; + + --month; + while (--month >= 0) { + *result += month_days[month] * 24 * 60 * 60; + if (month == 1 && year % 4 == 0 && year != 2000) // february in leap years + *result += 24 * 60 * 60; + } + + if (--day) + *result += day * 24 * 60 * 60; + + return true; +} + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> + +using namespace boost::python; +using namespace ledger; + +unsigned int interval_len(interval_t& interval) +{ + int periods = 1; + std::time_t when = interval.first(); + while (interval.end && when < interval.end) { + when = interval.increment(when); + if (when < interval.end) + periods++; + } + return periods; +} + +std::time_t interval_getitem(interval_t& interval, int i) +{ + static std::time_t last_index = 0; + static std::time_t last_moment = 0; + + if (i == 0) { + last_index = 0; + last_moment = interval.first(); + } + else { + last_moment = interval.increment(last_moment); + if (interval.end && last_moment >= interval.end) { + PyErr_SetString(PyExc_IndexError, "Index out of range"); + throw_error_already_set(); + } + } + return last_moment; +} + +void export_datetime() +{ + class_< interval_t > + ("Interval", init<optional<int, int, int, std::time_t, std::time_t> >()) + .def(init<std::string>()) + .def(! self) + + .def_readwrite("years", &interval_t::years) + .def_readwrite("months", &interval_t::months) + .def_readwrite("seconds", &interval_t::seconds) + .def_readwrite("begin", &interval_t::begin) + .def_readwrite("end", &interval_t::end) + + .def("__len__", interval_len) + .def("__getitem__", interval_getitem) + + .def("increment", &interval_t::increment) + ; +} + +#endif // USE_BOOST_PYTHON diff --git a/datetime.h b/datetime.h new file mode 100644 index 00000000..ea1f3aa1 --- /dev/null +++ b/datetime.h @@ -0,0 +1,62 @@ +#ifndef _DATETIME_H +#define _DATETIME_H + +#include "debug.h" + +#include <ctime> +#include <sstream> + +namespace ledger { + +struct interval_t +{ + unsigned int years; + unsigned int months; + unsigned int seconds; + std::time_t begin; + std::time_t end; + + interval_t(int _seconds = 0, int _months = 0, int _years = 0, + std::time_t _begin = 0, std::time_t _end = 0) + : years(_years), months(_months), seconds(_seconds), + begin(_begin), end(_end) { + DEBUG_PRINT("ledger.memory.ctors", "ctor interval_t"); + } + interval_t(const std::string& desc) + : years(0), months(0), seconds(0), begin(0), end(0){ + DEBUG_PRINT("ledger.memory.ctors", "ctor interval_t"); + std::istringstream stream(desc); + parse(stream); + } +#ifdef DEBUG_ENABLED + ~interval_t() { + DEBUG_PRINT("ledger.memory.dtors", "dtor interval_t"); + } +#endif + + operator bool() const { + return seconds > 0 || months > 0 || years > 0; + } + + void start(const std::time_t moment) { + begin = first(moment); + } + std::time_t first(const std::time_t moment = 0) const; + std::time_t increment(const std::time_t) const; + + void parse(std::istream& in); +}; + +extern std::time_t now; +extern int now_year; + +bool parse_date_mask(const char * date_str, struct std::tm * result); + +bool parse_date(const char * date_str, std::time_t * result, + const int year = -1); + +bool quick_parse_date(const char * date_str, std::time_t * result); + +} // namespace ledger + +#endif // _DATETIME_H diff --git a/debug.cc b/debug.cc new file mode 100644 index 00000000..b7f15120 --- /dev/null +++ b/debug.cc @@ -0,0 +1,112 @@ +#include "debug.h" + +#ifdef DEBUG_ENABLED + +#include <map> +#include <fstream> +#include <cstdlib> +#include <cstring> + +int offset = 0; + +std::map<void *, int> ptrs; + +#define PRINT_INC(x) { \ + char buf[128]; \ + std::sprintf(buf, "%d: %p: %s", ++offset, ptr, x); \ + write(1, buf, std::strlen(buf)); \ +} + +#define PRINT_DEC(x) { \ + char buf[128]; \ + std::sprintf(buf, "%d: %p: %s", --offset, ptr, x); \ + write(1, buf, std::strlen(buf)); \ +} + +void * operator new(std::size_t size) throw (std::bad_alloc) { + void * ptr = std::malloc(size); + if (DEBUG("debug.alloc")) { + PRINT_INC("void * operator new(std::size_t size) throw (std::bad_alloc)\n"); + } + return ptr; +} +void * operator new[](std::size_t size) throw (std::bad_alloc) { + void * ptr = std::malloc(size); + if (DEBUG("debug.alloc")) { + PRINT_INC("void * operator new[](std::size_t) throw (std::bad_alloc)\n"); + } + return ptr; +} +void * operator new(std::size_t size, const std::nothrow_t&) throw() { + void * ptr = std::malloc(size); + if (DEBUG("debug.alloc")) { + PRINT_INC("void * operator new(std::size_t size, const std::nothrow_t&) throw()\n"); + } + return ptr; +} +void * operator new[](std::size_t size, const std::nothrow_t&) throw() { + void * ptr = std::malloc(size); + if (DEBUG("debug.alloc")) { + PRINT_INC("void * operator new[](std::size_t size, const std::nothrow_t&) throw()\n"); + } + return ptr; +} +void operator delete(void * ptr) throw() { + if (DEBUG("debug.alloc")) { + PRINT_DEC("void operator delete(void * ptr) throw()\n"); + } + std::free(ptr); +} +void operator delete[](void * ptr) throw() { + if (DEBUG("debug.alloc")) { + PRINT_DEC("void operator delete[](void * ptr) throw()\n"); + } + std::free(ptr); +} +void operator delete(void * ptr, const std::nothrow_t&) throw() { + if (DEBUG("debug.alloc")) { + PRINT_DEC("void operator delete(void * ptr, const std::nothrow_t&) throw()\n"); + } + std::free(ptr); +} +void operator delete[](void * ptr, const std::nothrow_t&) throw() { + if (DEBUG("debug.alloc")) { + PRINT_DEC("void operator delete[](void * ptr, const std::nothrow_t&) throw()\n"); + } + std::free(ptr); +} + +std::ostream * _debug_stream = &std::cerr; +bool _free_debug_stream = false; + +bool _debug_active(const char * const cls) { + if (char * debug = std::getenv("DEBUG_CLASS")) { + static const char * error; + static int erroffset; + static int ovec[30]; + static pcre * class_regexp = pcre_compile(debug, PCRE_CASELESS, + &error, &erroffset, NULL); + return pcre_exec(class_regexp, NULL, cls, std::strlen(cls), + 0, 0, ovec, 30) >= 0; + } + return false; +} + +static struct init_streams { + init_streams() { + // If debugging is enabled and DEBUG_FILE is set, all debugging + // output goes to that file. + if (const char * p = std::getenv("DEBUG_FILE")) { + _debug_stream = new std::ofstream(p); + _free_debug_stream = true; + } + } + ~init_streams() { + if (_free_debug_stream && _debug_stream) { + delete _debug_stream; + _debug_stream = NULL; + } + } +} _debug_init; + +#endif // DEBUG_ENABLED diff --git a/debug.h b/debug.h new file mode 100644 index 00000000..97a5a667 --- /dev/null +++ b/debug.h @@ -0,0 +1,119 @@ +#ifndef _DEBUG_H +#define _DEBUG_H + +#define DEVELOPER 4 +#define ALPHA 3 +#define BETA 2 +#define RELEASE 1 +#define NO_SEATBELT 0 + +#ifndef DEBUG_LEVEL +#define DEBUG_LEVEL NO_SEATBELT +#endif + +#if DEBUG_LEVEL >= RELEASE +#include <cassert> +#endif + +////////////////////////////////////////////////////////////////////// +// +// General debugging facilities +// +// - In developer level, all checking and debugging facilities are +// active. +// +// - Alpha level does not include performance degrading +// VALIDATE calls. +// +// - Beta level is like Alpha, but does not include debugging +// facilities. +// +// - Release level does not include CONFIRM checks, but does include +// assert calls. +// +// - Running with no seatbelt disables all checking except for normal +// syntax and semantic error checking. + +#if DEBUG_LEVEL >= ALPHA + +#include <pcre.h> +#include <cstring> +#include <new> +#include <iostream> +#include <cstdlib> +#include <ctime> + +#define DEBUG_ENABLED + +extern std::ostream * _debug_stream; +extern bool _free_debug_stream; + +bool _debug_active(const char * const cls); + +#define DEBUG_CLASS(cls) static const char * const _debug_cls = (cls) + +#define DEBUG(cls) (_debug_active(cls)) +#define DEBUG_() DEBUG(_debug_cls) + +#define DEBUG_PRINT(cls, x) \ + if (_debug_stream && _debug_active(cls)) { \ + *_debug_stream << x << std::endl; \ + } +#define DEBUG_PRINT_(x) DEBUG_PRINT(_debug_cls, x) + +#define DEBUG_PRINT_TIME(cls, x) { \ + char buf[32]; \ + std::strftime(buf, 31, "%Y/%m/%d:%H", std::localtime(&x)); \ + DEBUG_PRINT(cls, #x << " is " << buf); \ +} + +#define DEBUG_PRINT_TIME_(x) DEBUG_PRINT_TIME(_debug_cls, x) + +#define CONFIRM(x) assert(x) + +#if DEBUG_LEVEL == DEVELOPER +#define VALIDATE(x) assert(x) +#else +#define VALIDATE(x) +#endif + +void * operator new(std::size_t) throw (std::bad_alloc); +void * operator new[](std::size_t) throw (std::bad_alloc); +void operator delete(void*) throw(); +void operator delete[](void*) throw(); +void * operator new(std::size_t, const std::nothrow_t&) throw(); +void * operator new[](std::size_t, const std::nothrow_t&) throw(); +void operator delete(void*, const std::nothrow_t&) throw(); +void operator delete[](void*, const std::nothrow_t&) throw(); + +#else // DEBUG_LEVEL + +#define DEBUG_CLASS(cls) +#define DEBUG(cls) 0 +#define DEBUG_() 0 +#define DEBUG_PRINT(cls, x) +#define DEBUG_PRINT_(x) +#define DEBUG_PRINT_TIME(cls, x) +#define DEBUG_PRINT_TIME_(x) + +#define VALIDATE(x) + +#if DEBUG_LEVEL == NO_SEATBELT + +#undef assert +#define assert(x) +#define CONFIRM(x) + +#elif DEBUG_LEVEL >= RELEASE + +#define CONFIRM(x) + +#elif DEBUG_LEVEL >= BETA + +#define CONFIRM(x) assert(x) + +#endif + +#endif // DEBUG_LEVEL + +#endif // _DEBUG_H diff --git a/derive.cc b/derive.cc new file mode 100644 index 00000000..26569041 --- /dev/null +++ b/derive.cc @@ -0,0 +1,144 @@ +#include "derive.h" +#include "datetime.h" +#include "error.h" +#include "mask.h" + +namespace ledger { + +entry_t * derive_new_entry(journal_t& journal, + strings_list::iterator i, + strings_list::iterator end) +{ + std::auto_ptr<entry_t> added(new entry_t); + + entry_t * matching = NULL; + + if (! parse_date((*i).c_str(), &added->date)) + throw error("Bad date passed to 'entry'"); + + if (++i == end) + throw error("Too few arguments to 'entry'"); + + mask_t regexp(*i++); + + for (entries_list::reverse_iterator j = journal.entries.rbegin(); + j != journal.entries.rend(); + j++) + if (regexp.match((*j)->payee)) { + matching = *j; + break; + } + + added->payee = matching ? matching->payee : regexp.pattern; + + if (i == end) + throw error("Too few arguments to 'entry'"); + + if ((*i)[0] == '-' || std::isdigit((*i)[0])) { + if (! matching) + throw error("Could not determine the account to draw from"); + + transaction_t * m_xact, * xact, * first; + m_xact = matching->transactions.front(); + + first = xact = new transaction_t(m_xact->account, amount_t(*i++)); + added->add_transaction(xact); + + if (xact->amount.commodity().symbol.empty()) + xact->amount.set_commodity(m_xact->amount.commodity()); + + m_xact = matching->transactions.back(); + + xact = new transaction_t(m_xact->account, - first->amount); + added->add_transaction(xact); + + if (i != end) { + account_t * acct = journal.find_account_re(*i); + if (! acct) + acct = journal.find_account(*i); + if (acct) + added->transactions.back()->account = acct; + } + } else { + while (i != end) { + std::string& re_pat(*i++); + account_t * acct = NULL; + commodity_t * cmdty = NULL; + + if (matching) { + mask_t acct_regex(re_pat); + + for (transactions_list::const_iterator x + = matching->transactions.begin(); + x != matching->transactions.end(); + x++) { + if (acct_regex.match((*x)->account->fullname())) { + acct = (*x)->account; + cmdty = &(*x)->amount.commodity(); + break; + } + } + } + + if (! acct) + acct = journal.find_account_re(re_pat); + if (! acct) + acct = journal.find_account(re_pat); + + if (i == end) { + added->add_transaction(new transaction_t(acct)); + goto done; + } + + transaction_t * xact = new transaction_t(acct, amount_t(*i++)); + if (cmdty && ! xact->amount.commodity()) + xact->amount.set_commodity(*cmdty); + + added->add_transaction(xact); + } + + if (! matching) { + throw error("Could not determine the account to draw from"); + } else { + transaction_t * xact + = new transaction_t(matching->transactions.back()->account); + added->add_transaction(xact); + } + } + + done: + if (! added->finalize() || + ! run_hooks(journal.entry_finalize_hooks, *added)) + throw error("Failed to finalize derived entry (check commodities)"); + + return added.release(); +} + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> +#include <boost/python/detail/api_placeholder.hpp> + +using namespace boost::python; +using namespace ledger; + +entry_t * py_derive_new_entry(journal_t& journal, list args) +{ + strings_list strs; + + int l = len(args); + for (int i = 0; i < l; i++) + strs.push_back(extract<std::string>(args[i])); + + return derive_new_entry(journal, strs.begin(), strs.end()); +} + +void export_derive() +{ + def("derive_new_entry", py_derive_new_entry, + return_value_policy<manage_new_object>()); +} + +#endif // USE_BOOST_PYTHON diff --git a/derive.h b/derive.h new file mode 100644 index 00000000..0df7f231 --- /dev/null +++ b/derive.h @@ -0,0 +1,14 @@ +#ifndef _DERIVE_H +#define _DERIVE_H + +#include "journal.h" + +namespace ledger { + +entry_t * derive_new_entry(journal_t& journal, + strings_list::iterator begin, + strings_list::iterator end); + +} // namespace ledger + +#endif // _DERIVE_H diff --git a/emacs.py b/emacs.py new file mode 100644 index 00000000..3398ec9c --- /dev/null +++ b/emacs.py @@ -0,0 +1,53 @@ +import sys + +from ledger import * + +def emacs_date (seconds): + return "(%d %d %d)" % (seconds / 65536, seconds % 65536, 0) + +class EmacsFormatTransactions (TransactionHandler): + last_entry = None + output = None + + def __init__ (self): + self.last_entry = None + + if config.output_file: + self.output = open (config.output_file, "w") + else: + self.output = sys.stdout + + TransactionHandler.__init__ (self) + + def __del__ (self): + if config.output_file: + self.output.close () + + def flush (self): + self.output.write ("))\n") + self.output.flush () + + def write_entry (self, entry): + self.output.write("%s %s %s %s\n" % + (emacs_date (entry.date), + (entry.state and "t") or "nil", + (entry.code and "\"%s\"" % entry.code) or "nil", + (entry.payee and "\"%s\"" % entry.payee) or "nil")) + + def __call__ (self, xact): + if not transaction_has_xdata (xact) or \ + not transaction_xdata (xact).dflags & TRANSACTION_DISPLAYED: + if self.last_entry is None: + self.output.write("((") + self.write_entry (xact.entry) + elif xact.entry != self.last_entry: + self.output.write(")\n (") + self.write_entry (xact.entry) + else: + self.output.write("\n") + self.output.write(" (\"%s\" \"%s\"%s%s)" % + (xact.account.fullname (), xact.amount, + (xact.cost and " \"%s\"" % xact.cost) or "", + (xact.note and " \"%s\"" % xact.note) or "")) + self.last_entry = xact.entry + transaction_xdata (xact).dflags |= TRANSACTION_DISPLAYED diff --git a/error.h b/error.h new file mode 100644 index 00000000..52af358a --- /dev/null +++ b/error.h @@ -0,0 +1,65 @@ +#ifndef _ERROR_H +#define _ERROR_H + +#include "journal.h" + +#include <exception> +#include <string> +#include <sstream> + +namespace ledger { + +class error : public std::exception { + std::string reason; + public: + error(const std::string& _reason) throw() : reason(_reason) {} + virtual ~error() throw() {} + + virtual const char* what() const throw() { + return reason.c_str(); + } +}; + +class compute_error : public error { + public: + compute_error(const std::string& reason) throw() : error(reason) {} + virtual ~compute_error() throw() {} +}; + +class value_expr_error : public error { + public: + value_expr_error(const std::string& reason) throw() : error(reason) {} + virtual ~value_expr_error() throw() {} +}; + +class interval_expr_error : public error { + public: + interval_expr_error(const std::string& reason) throw() : error(reason) {} + virtual ~interval_expr_error() throw() {} +}; + +class format_error : public error { + public: + format_error(const std::string& reason) throw() : error(reason) {} + virtual ~format_error() throw() {} +}; + +class parse_error : public error { + unsigned int line; + std::string file; + public: + parse_error(const std::string& _file, const unsigned int _line, + const std::string& reason) throw() + : error(reason), line(_line), file(_file) {} + virtual ~parse_error() throw() {} + + virtual const char* what() const throw() { + std::ostringstream msg; + msg << file << ", line " << line << ": " << error::what(); + return msg.str().c_str(); + } +}; + +} // namespace ledger + +#endif // _ERROR_H diff --git a/format.cc b/format.cc new file mode 100644 index 00000000..0357c60b --- /dev/null +++ b/format.cc @@ -0,0 +1,842 @@ +#include "format.h" +#include "error.h" +#ifdef USE_BOOST_PYTHON +#include "py_eval.h" +#endif + +#include <cstdlib> +#include <ctime> + +namespace ledger { + +std::string truncated(const std::string& str, unsigned int width) +{ + char buf[256]; + std::memset(buf, '\0', 255); + assert(width < 256); + std::strncpy(buf, str.c_str(), str.length()); + if (buf[width]) + std::strcpy(&buf[width - 2], ".."); + return buf; +} + +std::string partial_account_name(const account_t& account) +{ + std::string name; + + for (const account_t * acct = &account; + acct && acct->parent; + acct = acct->parent) { + if (account_has_xdata(*acct) && + account_xdata_(*acct).dflags & ACCOUNT_DISPLAYED) + break; + + if (name.empty()) + name = acct->name; + else + name = acct->name + ":" + name; + } + + return name; +} + +std::string format_t::date_format = "%Y/%m/%d"; + +element_t * format_t::parse_elements(const std::string& fmt) +{ + std::auto_ptr<element_t> result; + + element_t * current = NULL; + + char buf[1024]; + char * q = buf; + + for (const char * p = fmt.c_str(); *p; p++) { + if (*p != '%' && *p != '\\') { + *q++ = *p; + continue; + } + + if (! result.get()) { + result.reset(new element_t); + current = result.get(); + } else { + current->next = new element_t; + current = current->next; + } + + if (q != buf) { + current->type = element_t::STRING; + current->chars = std::string(buf, q); + q = buf; + + current->next = new element_t; + current = current->next; + } + + if (*p == '\\') { + p++; + current->type = element_t::STRING; + switch (*p) { + case 'b': current->chars = "\b"; break; + case 'f': current->chars = "\f"; break; + case 'n': current->chars = "\n"; break; + case 'r': current->chars = "\r"; break; + case 't': current->chars = "\t"; break; + case 'v': current->chars = "\v"; break; + } + continue; + } + + ++p; + if (*p == '-') { + current->align_left = true; + ++p; + } + + int num = 0; + while (*p && std::isdigit(*p)) { + num *= 10; + num += *p++ - '0'; + } + current->min_width = num; + + if (*p == '.') { + ++p; + num = 0; + while (*p && std::isdigit(*p)) { + num *= 10; + num += *p++ - '0'; + } + current->max_width = num; + if (current->min_width == 0) + current->min_width = current->max_width; + } + + switch (*p) { + case '%': + current->type = element_t::STRING; + current->chars = "%"; + break; + + case '(': { + ++p; + const char * b = p; + int depth = 1; + while (*p) { + if (*p == ')' && --depth == 0) + break; + else if (*p == '(') + ++depth; + p++; + } + if (*p != ')') + throw format_error("Missing ')'"); + + current->type = element_t::VALUE_EXPR; + try { + current->val_expr = parse_value_expr(std::string(b, p)); + } + catch (value_expr_error& err) { + throw value_expr_error(std::string("In format expression '") + + std::string(b, p) + "': " + err.what()); + } + break; + } + + case '@': { + const char * s = ++p; + while (*p && *p != '(') + p++; + if (*p && *++p != ')') + throw format_error("Missing ')'"); + + current->type = element_t::INTERP_FUNC; + current->chars = std::string(s, (p - s) - 1); + break; + } + + case '[': { + ++p; + const char * b = p; + int depth = 1; + while (*p) { + if (*p == ']' && --depth == 0) + break; + else if (*p == '[') + ++depth; + p++; + } + if (*p != ']') + throw format_error("Missing ']'"); + + current->type = element_t::DATE_STRING; + current->chars = std::string(b, p); + break; + } + + case 'D': + current->type = element_t::DATE_STRING; + current->chars = format_t::date_format; + break; + + case 'X': current->type = element_t::CLEARED; break; + case 'C': current->type = element_t::CODE; break; + case 'P': current->type = element_t::PAYEE; break; + case 'a': current->type = element_t::ACCOUNT_NAME; break; + case 'A': current->type = element_t::ACCOUNT_FULLNAME; break; + case 't': current->type = element_t::AMOUNT; break; + case 'o': current->type = element_t::OPT_AMOUNT; break; + case 'T': current->type = element_t::TOTAL; break; + case 'N': current->type = element_t::NOTE; break; + case 'n': current->type = element_t::OPT_NOTE; break; + case '|': current->type = element_t::SPACER; break; + case '_': current->type = element_t::DEPTH_SPACER; break; + } + } + + if (q != buf) { + if (! result.get()) { + result.reset(new element_t); + current = result.get(); + } else { + current->next = new element_t; + current = current->next; + } + current->type = element_t::STRING; + current->chars = std::string(buf, q); + } + + return result.release(); +} + +void format_t::format(std::ostream& out, const details_t& details) const +{ + for (const element_t * elem = elements; elem; elem = elem->next) { + if (elem->align_left) + out << std::left; + else + out << std::right; + + if (elem->min_width > 0) + out.width(elem->min_width); + + switch (elem->type) { + case element_t::STRING: + out << elem->chars; + break; + + case element_t::AMOUNT: + case element_t::TOTAL: + case element_t::VALUE_EXPR: { + value_expr_t * expr = NULL; + switch (elem->type) { + case element_t::AMOUNT: expr = amount_expr.get(); break; + case element_t::TOTAL: expr = total_expr.get(); break; + case element_t::VALUE_EXPR: expr = elem->val_expr; break; + default: + assert(0); + break; + } + if (! expr) + break; + + value_t value; + balance_t * bal = NULL; + + expr->compute(value, details); + + switch (value.type) { + case value_t::BOOLEAN: + out << (*((bool *) value.data) ? "1" : "0"); + break; + case value_t::INTEGER: + out << *((unsigned int *) value.data); + break; + case value_t::AMOUNT: + out << *((amount_t *) value.data); + break; + case value_t::BALANCE: + bal = (balance_t *) value.data; + // fall through... + + case value_t::BALANCE_PAIR: + if (! bal) + bal = &((balance_pair_t *) value.data)->quantity; + + bal->write(out, elem->min_width, + (elem->max_width > 0 ? elem->max_width : elem->min_width)); + break; + default: + assert(0); + break; + } + break; + } + + case element_t::OPT_AMOUNT: + if (details.xact) { + std::string disp; + bool use_disp = false; + + if (details.xact->cost && details.xact->amount) { + amount_t unit_cost = *details.xact->cost / details.xact->amount; + + commodity_t& comm(unit_cost.commodity()); + bool has_flag = comm.flags & COMMODITY_STYLE_VARIABLE; + if (! has_flag) + unit_cost.commodity().flags |= COMMODITY_STYLE_VARIABLE; + + std::ostringstream stream; + stream << details.xact->amount << " @ " << unit_cost; + disp = stream.str(); + use_disp = true; + + if (! has_flag) + unit_cost.commodity().flags &= ~COMMODITY_STYLE_VARIABLE; + } else { + unsigned int xacts_count = 0; + transaction_t * first = NULL; + transaction_t * last = NULL; + + for (transactions_list::const_iterator i + = details.entry->transactions.begin(); + i != details.entry->transactions.end(); + i++) + if (transaction_has_xdata(**i) && + transaction_xdata_(**i).dflags & TRANSACTION_TO_DISPLAY) { + xacts_count++; + if (! first) + first = *i; + last = *i; + } + + use_disp = (xacts_count == 2 && details.xact == last && + first->amount == - last->amount); + } + + if (! use_disp) + out << details.xact->amount; + else + out << disp; + } + break; + + case element_t::DATE_STRING: + if (details.entry && details.entry->date) { + char buf[256]; + std::strftime(buf, 255, elem->chars.c_str(), + std::localtime(&details.entry->date)); + out << (elem->max_width == 0 ? buf : truncated(buf, elem->max_width)); + } else { + out << " "; + } + break; + + case element_t::CLEARED: + if (details.entry && details.entry->state == entry_t::CLEARED) + out << "* "; + else + out << ""; + break; + + case element_t::CODE: { + std::string temp; + if (details.entry && ! details.entry->code.empty()) { + temp += "("; + temp += details.entry->code; + temp += ") "; + } + out << temp; + break; + } + + case element_t::PAYEE: + if (details.entry) + out << (elem->max_width == 0 ? + details.entry->payee : truncated(details.entry->payee, + elem->max_width)); + break; + + case element_t::OPT_NOTE: + if (details.xact && ! details.xact->note.empty()) + out << " ; "; + // fall through... + + case element_t::NOTE: + if (details.xact) + out << (elem->max_width == 0 ? + details.xact->note : truncated(details.xact->note, + elem->max_width)); + break; + + case element_t::ACCOUNT_NAME: + case element_t::ACCOUNT_FULLNAME: + if (details.account) { + std::string name = (elem->type == element_t::ACCOUNT_FULLNAME ? + details.account->fullname() : + partial_account_name(*details.account)); + + if (details.xact && details.xact->flags & TRANSACTION_VIRTUAL) { + if (elem->max_width > 2) + name = truncated(name, elem->max_width - 2); + + if (details.xact->flags & TRANSACTION_BALANCE) + name = "[" + name + "]"; + else + name = "(" + name + ")"; + } + else if (elem->max_width > 0) + name = truncated(name, elem->max_width); + + out << name; + } else { + out << " "; + } + break; + + case element_t::SPACER: + out << " "; + break; + + case element_t::DEPTH_SPACER: + for (const account_t * acct = details.account; + acct; + acct = acct->parent) + if (account_has_xdata(*acct) && + account_xdata_(*acct).dflags & ACCOUNT_DISPLAYED) { + if (elem->min_width > 0 || elem->max_width > 0) + out.width(elem->min_width > elem->max_width ? + elem->min_width : elem->max_width); + out << " "; + } + break; + + case element_t::INTERP_FUNC: +#ifdef USE_BOOST_PYTHON + try { + object func = python_eval(elem->chars); + out << call<std::string>(func.ptr(), details); + } + catch(const boost::python::error_already_set&) { + PyErr_Print(); + throw format_error(std::string("While calling Python function '") + + elem->chars + "'"); + } +#endif + break; + + default: + assert(0); + break; + } + } +} + +format_transactions::format_transactions(std::ostream& _output_stream, + const std::string& format) + : output_stream(_output_stream), last_entry(NULL) +{ + const char * f = format.c_str(); + if (const char * p = std::strstr(f, "%/")) { + first_line_format.reset(std::string(f, 0, p - f)); + next_lines_format.reset(std::string(p + 2)); + } else { + first_line_format.reset(format); + next_lines_format.reset(format); + } +} + +void format_transactions::operator()(transaction_t& xact) +{ + if (! transaction_has_xdata(xact) || + ! (transaction_xdata_(xact).dflags & TRANSACTION_DISPLAYED)) { + if (last_entry != xact.entry) { + first_line_format.format(output_stream, details_t(xact)); + last_entry = xact.entry; + } else { + next_lines_format.format(output_stream, details_t(xact)); + } + transaction_xdata_(xact).dflags |= TRANSACTION_DISPLAYED; + } +} + +void format_entries::format_last_entry() +{ + bool first = true; + for (transactions_list::const_iterator i = last_entry->transactions.begin(); + i != last_entry->transactions.end(); + i++) { + if (transaction_has_xdata(**i) && + transaction_xdata_(**i).dflags & TRANSACTION_TO_DISPLAY) { + if (first) { + first_line_format.format(output_stream, details_t(**i)); + first = false; + } else { + next_lines_format.format(output_stream, details_t(**i)); + } + transaction_xdata_(**i).dflags |= TRANSACTION_DISPLAYED; + } + } +} + +void format_entries::operator()(transaction_t& xact) +{ + transaction_xdata(xact).dflags |= TRANSACTION_TO_DISPLAY; + + if (last_entry && xact.entry != last_entry) + format_last_entry(); + + last_entry = xact.entry; +} + +void xml_write_amount(std::ostream& out, const amount_t& amount, + const int depth = 0) +{ + for (int i = 0; i < depth; i++) out << ' '; + out << "<amount>\n"; + + commodity_t& c = amount.commodity(); + for (int i = 0; i < depth + 2; i++) out << ' '; + out << "<commodity flags=\""; + if (! (c.flags & COMMODITY_STYLE_SUFFIXED)) out << 'P'; + if (c.flags & COMMODITY_STYLE_SEPARATED) out << 'S'; + if (c.flags & COMMODITY_STYLE_THOUSANDS) out << 'T'; + if (c.flags & COMMODITY_STYLE_EUROPEAN) out << 'E'; + out << "\">" << c.symbol << "</commodity>\n"; + + for (int i = 0; i < depth + 2; i++) out << ' '; + out << "<quantity>"; + out << amount.quantity_string() << "</quantity>\n"; + + for (int i = 0; i < depth; i++) out << ' '; + out << "</amount>\n"; +} + +void xml_write_value(std::ostream& out, const value_t& value, + const int depth = 0) +{ + balance_t * bal = NULL; + + for (int i = 0; i < depth; i++) out << ' '; + out << "<value type=\""; + switch (value.type) { + case value_t::BOOLEAN: out << "boolean"; break; + case value_t::INTEGER: out << "integer"; break; + case value_t::AMOUNT: out << "amount"; break; + case value_t::BALANCE: + case value_t::BALANCE_PAIR: out << "balance"; break; + } + out << "\">\n"; + + switch (value.type) { + case value_t::BOOLEAN: + for (int i = 0; i < depth + 2; i++) out << ' '; + out << "<boolean>" << *((bool *) value.data) << "</boolean>\n"; + break; + + case value_t::INTEGER: + for (int i = 0; i < depth + 2; i++) out << ' '; + out << "<integer>" << *((long *) value.data) << "</integer>\n"; + break; + + case value_t::AMOUNT: + xml_write_amount(out, *((amount_t *) value.data), depth + 2); + break; + + case value_t::BALANCE: + bal = (balance_t *) value.data; + // fall through... + + case value_t::BALANCE_PAIR: + if (! bal) + bal = &((balance_pair_t *) value.data)->quantity; + + for (int i = 0; i < depth + 2; i++) out << ' '; + out << "<balance>\n"; + + for (amounts_map::const_iterator i = bal->amounts.begin(); + i != bal->amounts.end(); + i++) + xml_write_amount(out, (*i).second, depth + 4); + + for (int i = 0; i < depth + 2; i++) out << ' '; + out << "</balance>\n"; + break; + + default: + assert(0); + break; + } + + for (int i = 0; i < depth; i++) out << ' '; + out << "</value>\n"; +} + +std::string xml_string(const std::string& str) +{ + std::string::size_type pos = 0; + std::string::size_type index = str.find('&', pos); + if (index == std::string::npos) + return str; + + std::string temp; + while (index != std::string::npos) { + temp += std::string(str, pos, index) + "&"; + pos = index + 1; + index = str.find('&', pos); + } + + return temp; +} + +void format_xml_entries::format_last_entry() +{ + char buf[256]; + std::strftime(buf, 255, format_t::date_format.c_str(), + std::localtime(&last_entry->date)); + + output_stream << " <entry>\n" + << " <en:date>" << buf << "</en:date>\n"; + + if (last_entry->state == entry_t::CLEARED) + output_stream << " <en:cleared/>\n"; + else if (last_entry->state == entry_t::PENDING) + output_stream << " <en:pending/>\n"; + + if (! last_entry->code.empty()) + output_stream << " <en:code>" << xml_string(last_entry->code) + << "</en:code>\n"; + + if (! last_entry->payee.empty()) + output_stream << " <en:payee>" << xml_string(last_entry->payee) + << "</en:payee>\n"; + + bool first = true; + for (transactions_list::const_iterator i = last_entry->transactions.begin(); + i != last_entry->transactions.end(); + i++) { + if (transaction_has_xdata(**i) && + transaction_xdata_(**i).dflags & TRANSACTION_TO_DISPLAY) { + if (first) { + output_stream << " <en:transactions>\n"; + first = false; + } + + output_stream << " <transaction>\n"; + + if ((*i)->flags & TRANSACTION_VIRTUAL) + output_stream << " <tr:virtual/>\n"; + if ((*i)->flags & TRANSACTION_AUTO) + output_stream << " <tr:generated/>\n"; + + if ((*i)->account) { + std::string name = (*i)->account->fullname(); + if (name == "<Total>") + name = "[TOTAL]"; + else if (name == "<Unknown>") + name = "[UNKNOWN]"; + output_stream << " <tr:account>" << name << "</tr:account>\n"; + } + + output_stream << " <tr:amount>\n"; + if (transaction_xdata_(**i).dflags & TRANSACTION_COMPOSITE) + xml_write_value(output_stream, + transaction_xdata_(**i).composite_amount, 10); + else + xml_write_value(output_stream, value_t((*i)->amount), 10); + output_stream << " </tr:amount>\n"; + + if ((*i)->cost) + output_stream << " <tr:cost>" << *(*i)->cost << "</tr:cost>\n"; + + if (! (*i)->note.empty()) + output_stream << " <tr:note>" << xml_string((*i)->note) + << "</tr:note>\n"; + + if (show_totals) { + output_stream << " <total>\n"; + xml_write_value(output_stream, transaction_xdata_(**i).total, 10); + output_stream << " </total>\n"; + } + + output_stream << " </transaction>\n"; + + transaction_xdata_(**i).dflags |= TRANSACTION_DISPLAYED; + } + } + + if (! first) + output_stream << " </en:transactions>\n"; + + output_stream << " </entry>\n"; +} + +void print_entry(std::ostream& out, const entry_t& entry) +{ + const std::string print_format + = "\n%D %X%C%P\n %-34A %12o\n%/ %-34A %12o\n"; + + format_entries formatter(out, print_format); + walk_transactions(const_cast<transactions_list&>(entry.transactions), + formatter); + formatter.flush(); + + clear_transactions_xdata(); + clear_accounts_xdata(); +} + +bool disp_subaccounts_p(const account_t& account, + const item_predicate<account_t>& disp_pred, + const account_t *& to_show) +{ + bool display = false; + unsigned int counted = 0; + bool matches = disp_pred(account); + value_t acct_total; + bool computed = false; + value_t result; + + to_show = NULL; + + for (accounts_map::const_iterator i = account.accounts.begin(); + i != account.accounts.end(); + i++) { + if (! disp_pred(*(*i).second)) + continue; + + compute_total(result, details_t(*(*i).second)); + if (! computed) { + compute_total(acct_total, details_t(account)); + computed = true; + } + + if ((result != acct_total) || counted > 0) { + display = matches; + break; + } + to_show = (*i).second; + counted++; + } + + return display; +} + +bool display_account(const account_t& account, + const item_predicate<account_t>& disp_pred) +{ + // Never display an account that has already been displayed. + if (account_has_xdata(account) && + account_xdata_(account).dflags & ACCOUNT_DISPLAYED) + return false; + + // At this point, one of two possibilities exists: the account is a + // leaf which matches the predicate restrictions; or it is a parent + // and two or more children must be subtotaled; or it is a parent + // and its child has been hidden by the predicate. So first, + // determine if it is a parent that must be displayed regardless of + // the predicate. + + const account_t * account_to_show = NULL; + if (disp_subaccounts_p(account, disp_pred, account_to_show)) + return true; + + return ! account_to_show && disp_pred(account); +} + +void format_account::operator()(account_t& account) +{ + if (display_account(account, disp_pred)) { + if (! account.parent) { + account_xdata(account).dflags |= ACCOUNT_TO_DISPLAY; + } else { + format.format(output_stream, details_t(account)); + account_xdata(account).dflags |= ACCOUNT_DISPLAYED; + } + } +} + +format_equity::format_equity(std::ostream& _output_stream, + const std::string& _format, + const std::string& display_predicate) + : output_stream(_output_stream), disp_pred(display_predicate) +{ + const char * f = _format.c_str(); + if (const char * p = std::strstr(f, "%/")) { + first_line_format.reset(std::string(f, 0, p - f)); + next_lines_format.reset(std::string(p + 2)); + } else { + first_line_format.reset(_format); + next_lines_format.reset(_format); + } + + entry_t header_entry; + header_entry.payee = "Opening Balances"; + header_entry.date = now; + first_line_format.format(output_stream, details_t(header_entry)); +} + +void format_equity::flush() +{ + account_xdata_t xdata; + xdata.value = total; + xdata.value.negate(); + account_t summary(NULL, "Equity:Opening Balances"); + summary.data = &xdata; + next_lines_format.format(output_stream, details_t(summary)); + output_stream.flush(); +} + +void format_equity::operator()(account_t& account) +{ + if (display_account(account, disp_pred)) { + next_lines_format.format(output_stream, details_t(account)); + if (account_has_xdata(account)) + total += account_xdata_(account).value; + account_xdata(account).dflags |= ACCOUNT_DISPLAYED; + } +} + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> + +using namespace boost::python; +using namespace ledger; + +std::string py_format_1(format_t& format, const details_t& item) +{ + std::ostringstream out; + format.format(out, item); + return out.str(); +} + +template <typename T> +std::string py_format(format_t& format, const T& item) +{ + std::ostringstream out; + format.format(out, details_t(item)); + return out.str(); +} + +void export_format() +{ + class_< format_t > ("Format") + .def(init<std::string>()) + .def("reset", &format_t::reset) + .def("format", py_format_1) + .def("format", py_format<account_t>) + .def("format", py_format<entry_t>) + .def("format", py_format<transaction_t>) + ; + + def("truncated", truncated); +#if 0 + def("partial_account_name", partial_account_name); +#endif + def("display_account", display_account); +} + +#endif // USE_BOOST_PYTHON diff --git a/format.h b/format.h new file mode 100644 index 00000000..37e5e21a --- /dev/null +++ b/format.h @@ -0,0 +1,198 @@ +#ifndef _FORMAT_H +#define _FORMAT_H + +#include "journal.h" +#include "valexpr.h" +#include "walk.h" + +namespace ledger { + +std::string truncated(const std::string& str, unsigned int width); + +std::string partial_account_name(const account_t& account, + const unsigned int start_depth); + +struct element_t +{ + enum kind_t { + STRING, + VALUE_EXPR, + DATE_STRING, + CLEARED, + CODE, + PAYEE, + ACCOUNT_NAME, + ACCOUNT_FULLNAME, + AMOUNT, + OPT_AMOUNT, + TOTAL, + NOTE, + OPT_NOTE, + SPACER, + DEPTH_SPACER, + INTERP_FUNC + }; + + bool align_left; + unsigned int min_width; + unsigned int max_width; + + kind_t type; + std::string chars; + value_expr_t * val_expr; + + struct element_t * next; + + element_t() : align_left(false), min_width(0), max_width(0), + type(STRING), val_expr(NULL), next(NULL) { + DEBUG_PRINT("ledger.memory.ctors", "ctor element_t"); + } + + ~element_t() { + DEBUG_PRINT("ledger.memory.dtors", "dtor element_t"); + if (val_expr) delete val_expr; + if (next) delete next; // recursive, but not too deep + } +}; + +struct format_t +{ + std::string format_string; + element_t * elements; + + static std::string date_format; + + format_t() : elements(NULL) { + DEBUG_PRINT("ledger.memory.ctors", "ctor format_t"); + } + format_t(const std::string& _format) : elements(NULL) { + DEBUG_PRINT("ledger.memory.ctors", "ctor format_t"); + reset(_format); + } + ~format_t() { + DEBUG_PRINT("ledger.memory.dtors", "dtor format_t"); + if (elements) delete elements; + } + + void reset(const std::string& _format) { + if (elements) + delete elements; + elements = parse_elements(_format); + format_string = _format; + } + + static element_t * parse_elements(const std::string& fmt); + + void format(std::ostream& out, const details_t& details) const; +}; + +class format_transactions : public item_handler<transaction_t> +{ + protected: + std::ostream& output_stream; + format_t first_line_format; + format_t next_lines_format; + entry_t * last_entry; + + public: + format_transactions(std::ostream& _output_stream, + const std::string& format); + + virtual void flush() { + output_stream.flush(); + } + virtual void operator()(transaction_t& xact); +}; + +class format_entries : public format_transactions +{ + public: + format_entries(std::ostream& output_stream, const std::string& format) + : format_transactions(output_stream, format) {} + + virtual void format_last_entry(); + + virtual void flush() { + if (last_entry) { + format_last_entry(); + last_entry = NULL; + } + format_transactions::flush(); + } + virtual void operator()(transaction_t& xact); +}; + +class format_xml_entries : public format_entries +{ + bool show_totals; + public: + format_xml_entries(std::ostream& output_stream, + const bool _show_totals = false) + : format_entries(output_stream, ""), show_totals(_show_totals) { + output_stream << "<?xml version=\"1.0\"?>\n<ledger>\n"; + } + virtual ~format_xml_entries() { + output_stream << "</ledger>" << std::endl; + } + + virtual void format_last_entry(); +}; + +void print_entry(std::ostream& out, const entry_t& entry); + +bool disp_subaccounts_p(const account_t& account, + const item_predicate<account_t>& disp_pred, + const account_t *& to_show); + +inline bool disp_subaccounts_p(const account_t& account) { + const account_t * temp; + return disp_subaccounts_p(account, item_predicate<account_t>(NULL), temp); +} + +bool display_account(const account_t& account, + const item_predicate<account_t>& disp_pred); + +class format_account : public item_handler<account_t> +{ + std::ostream& output_stream; + + item_predicate<account_t> disp_pred; + + public: + format_t format; + + format_account(std::ostream& _output_stream, + const std::string& _format, + const std::string& display_predicate = NULL) + : output_stream(_output_stream), format(_format), + disp_pred(display_predicate) {} + + virtual void flush() { + output_stream.flush(); + } + + virtual void operator()(account_t& account); +}; + +class format_equity : public item_handler<account_t> +{ + std::ostream& output_stream; + format_t first_line_format; + format_t next_lines_format; + + item_predicate<account_t> disp_pred; + + mutable value_t total; + + public: + format_equity(std::ostream& _output_stream, + const std::string& _format, + const std::string& display_predicate); + + virtual void flush(); + virtual void operator()(account_t& account); +}; + +} // namespace ledger + +#endif // _REPORT_H @@ -1,24 +1,38 @@ +#include "gnucash.h" +#include "journal.h" +#include "format.h" +#include "error.h" + +#include <iostream> #include <sstream> #include <cstring> -#include "ledger.h" - extern "C" { #include <xmlparse.h> // expat XML parser } namespace ledger { -static account * curr_account; -static std::string curr_account_id; -static entry * curr_entry; -static commodity * entry_comm; -static commodity * curr_comm; -static amount * curr_value; -static std::string curr_quant; -static XML_Parser current_parser; -static accounts_map accounts_by_id; -static bool do_compute; +typedef std::map<const std::string, account_t *> accounts_map; +typedef std::pair<const std::string, account_t *> accounts_pair; + +typedef std::map<account_t *, commodity_t *> account_comm_map; +typedef std::pair<account_t *, commodity_t *> account_comm_pair; + +static journal_t * curr_journal; +static account_t * master_account; +static account_t * curr_account; +static std::string curr_account_id; +static entry_t * curr_entry; +static commodity_t * entry_comm; +static commodity_t * curr_comm; +static amount_t curr_value; +static amount_t curr_quant; +static XML_Parser current_parser; +static accounts_map accounts_by_id; +static account_comm_map account_comms; +static unsigned int count; +static std::string have_error; static enum { NO_ACTION, @@ -43,8 +57,7 @@ static enum { static void startElement(void *userData, const char *name, const char **atts) { if (std::strcmp(name, "gnc:account") == 0) { - assert(! curr_account); - curr_account = new account; + curr_account = new account_t(master_account); } else if (std::strcmp(name, "act:name") == 0) action = ACCOUNT_NAME; @@ -54,7 +67,7 @@ static void startElement(void *userData, const char *name, const char **atts) action = ACCOUNT_PARENT; else if (std::strcmp(name, "gnc:commodity") == 0) { assert(! curr_comm); - curr_comm = new commodity; + curr_comm = new commodity_t(""); } else if (std::strcmp(name, "cmdty:id") == 0) action = COMM_SYM; @@ -64,7 +77,7 @@ static void startElement(void *userData, const char *name, const char **atts) action = COMM_PREC; else if (std::strcmp(name, "gnc:transaction") == 0) { assert(! curr_entry); - curr_entry = new entry(main_ledger); + curr_entry = new entry_t; } else if (std::strcmp(name, "trn:num") == 0) action = ENTRY_NUM; @@ -76,7 +89,7 @@ static void startElement(void *userData, const char *name, const char **atts) action = ENTRY_DESC; else if (std::strcmp(name, "trn:split") == 0) { assert(curr_entry); - curr_entry->xacts.push_back(new transaction()); + curr_entry->add_transaction(new transaction_t(curr_account)); } else if (std::strcmp(name, "split:reconciled-state") == 0) action = XACT_STATE; @@ -92,32 +105,52 @@ static void startElement(void *userData, const char *name, const char **atts) action = XACT_NOTE; } - static void endElement(void *userData, const char *name) { if (std::strcmp(name, "gnc:account") == 0) { assert(curr_account); - if (! curr_account->parent) - main_ledger->accounts.insert(accounts_map_pair(curr_account->name, - curr_account)); - accounts_by_id.insert(accounts_map_pair(curr_account_id, curr_account)); + if (curr_account->parent == master_account) + curr_journal->add_account(curr_account); + accounts_by_id.insert(accounts_pair(curr_account_id, curr_account)); curr_account = NULL; } else if (std::strcmp(name, "gnc:commodity") == 0) { assert(curr_comm); - main_ledger->commodities.insert(commodities_map_pair(curr_comm->symbol, - curr_comm)); + commodity_t::add_commodity(curr_comm); curr_comm = NULL; } else if (std::strcmp(name, "gnc:transaction") == 0) { assert(curr_entry); - assert(curr_entry->validate()); - main_ledger->entries.push_back(curr_entry); + if (! curr_journal->add_entry(curr_entry)) { + print_entry(std::cerr, *curr_entry); + have_error = "The above entry does not balance"; + delete curr_entry; + } else { + count++; + } curr_entry = NULL; } action = NO_ACTION; } + +static amount_t convert_number(const std::string& number) +{ + const char * num = number.c_str(); + + if (char * p = std::strchr(num, '/')) { + std::string numer_str(num, p - num); + std::string denom_str(p + 1); + + amount_t amt(numer_str); + amount_t den(denom_str); + + return amt / den; + } else { + return amount_t(number); + } +} + static void dataHandler(void *userData, const char *s, int len) { switch (action) { @@ -130,21 +163,31 @@ static void dataHandler(void *userData, const char *s, int len) break; case ACCOUNT_PARENT: { - accounts_map_iterator i = accounts_by_id.find(std::string(s, len)); + accounts_map::iterator i = accounts_by_id.find(std::string(s, len)); assert(i != accounts_by_id.end()); curr_account->parent = (*i).second; - (*i).second->children.insert(accounts_map_pair(curr_account->name, - curr_account)); + curr_account->depth = curr_account->parent->depth + 1; + (*i).second->add_account(curr_account); break; } case COMM_SYM: - if (curr_comm) - curr_comm->symbol = std::string(s, len); - else if (curr_account) - curr_account->comm = main_ledger->commodities[std::string(s, len)]; - else if (curr_entry) - entry_comm = main_ledger->commodities[std::string(s, len)]; + if (curr_comm) { + curr_comm->set_symbol(std::string(s, len)); + } + else if (curr_account) { + std::string symbol(s, len); + commodity_t * comm = commodity_t::find_commodity(symbol, true); + if (symbol != "$" && symbol != "USD") + comm->flags |= COMMODITY_STYLE_SEPARATED; + account_comms.insert(account_comm_pair(curr_account, comm)); + } + else if (curr_entry) { + std::string symbol(s, len); + entry_comm = commodity_t::find_commodity(symbol, true); + if (symbol != "$" && symbol != "USD") + entry_comm->flags |= COMMODITY_STYLE_SEPARATED; + } break; case COMM_NAME: @@ -167,57 +210,62 @@ static void dataHandler(void *userData, const char *s, int len) } case ENTRY_DESC: - curr_entry->desc = std::string(s, len); + curr_entry->payee = std::string(s, len); break; case XACT_STATE: - curr_entry->cleared = (*s == 'y' || *s == 'c'); + if (*s == 'y') + curr_entry->state = entry_t::PENDING; + else + curr_entry->state = entry_t::CLEARED; break; - case XACT_VALUE: { + case XACT_VALUE: assert(entry_comm); - std::string value = std::string(s, len) + " " + entry_comm->symbol; - curr_value = create_amount(value.c_str()); + curr_value = convert_number(std::string(s, len)); + curr_value.set_commodity(*entry_comm); break; - } case XACT_QUANTITY: - curr_quant = std::string(s, len); + curr_quant = convert_number(std::string(s, len)); break; case XACT_ACCOUNT: { - accounts_map_iterator i = accounts_by_id.find(std::string(s, len)); - if (i == accounts_by_id.end()) { - std::cerr << "Could not find account " << std::string(s, len) - << std::endl; - std::exit(1); - } + transaction_t * xact = curr_entry->transactions.back(); - transaction * xact = curr_entry->xacts.back(); - xact->acct = (*i).second; + accounts_map::iterator i = accounts_by_id.find(std::string(s, len)); + if (i != accounts_by_id.end()) { + xact->account = (*i).second; + } else { + xact->account = curr_journal->find_account("<Unknown>"); - std::string value = curr_quant + " " + xact->acct->comm->symbol; - - if (curr_value->commdty() == xact->acct->comm) { - // assert: value must be equal to curr_value. - delete curr_value; - curr_value = NULL; + have_error = (std::string("Could not find account ") + + std::string(s, len)); } - xact->cost = create_amount(value.c_str(), curr_value); + amount_t value; + + account_comm_map::iterator ac = account_comms.find(xact->account); + if (ac != account_comms.end()) { + commodity_t * default_commodity = (*ac).second; - if (curr_value) { - delete curr_value; - curr_value = NULL; + curr_quant.set_commodity(*default_commodity); + value = curr_quant.round(default_commodity->precision); + + if (curr_value.commodity() == *default_commodity) + curr_value = value; + } else { + value = curr_quant; } - if (do_compute) - xact->acct->balance.credit(xact->cost); + xact->amount = value; + if (value != curr_value) + xact->cost = new amount_t(curr_value); break; } case XACT_NOTE: - curr_entry->xacts.back()->note = std::string(s, len); + curr_entry->transactions.back()->note = std::string(s, len); break; case NO_ACTION: @@ -231,25 +279,38 @@ static void dataHandler(void *userData, const char *s, int len) } } -book * parse_gnucash(std::istream& in, bool compute_balances) +bool gnucash_parser_t::test(std::istream& in) const { - char buf[BUFSIZ]; + char buf[128]; + in.getline(buf, 127); + in.seekg(0, std::ios::beg); - book * ledger = new book; + return std::strncmp(buf, "<?xml version=\"1.0\"?>", 21) == 0; +} - main_ledger = ledger; - do_compute = compute_balances; - action = NO_ACTION; - curr_account = NULL; - curr_entry = NULL; - curr_value = NULL; - curr_comm = NULL; - entry_comm = NULL; +unsigned int gnucash_parser_t::parse(std::istream& in, + journal_t * journal, + account_t * master, + const std::string * original_file) +{ + char buf[BUFSIZ]; + + count = 0; + action = NO_ACTION; + curr_journal = journal; + master_account = master ? master : journal->master; + curr_account = NULL; + curr_entry = NULL; + curr_comm = NULL; + entry_comm = NULL; // GnuCash uses the USD commodity without defining it, which really - // means to use $. - commodity * usd = new commodity("$", true, false, true, false, 2); - main_ledger->commodities.insert(commodities_map_pair("USD", usd)); + // means $. + commodity_t * usd; + usd = new commodity_t("$", 2, COMMODITY_STYLE_THOUSANDS); + commodity_t::add_commodity(usd, "$"); + usd = new commodity_t("$", 2, COMMODITY_STYLE_THOUSANDS); + commodity_t::add_commodity(usd, "USD"); XML_Parser parser = XML_ParserCreate(NULL); current_parser = parser; @@ -259,23 +320,48 @@ book * parse_gnucash(std::istream& in, bool compute_balances) while (! in.eof()) { in.getline(buf, BUFSIZ - 1); - if (! XML_Parse(parser, buf, std::strlen(buf), in.eof())) { - std::cerr << XML_ErrorString(XML_GetErrorCode(parser)) - << " at line " << XML_GetCurrentLineNumber(parser) - << std::endl; - return NULL; + unsigned long line = XML_GetCurrentLineNumber(parser); + const char * msg = XML_ErrorString(XML_GetErrorCode(parser)); + XML_ParserFree(parser); + throw parse_error(original_file ? *original_file : "<gnucash>", line, + msg); + } + + if (! have_error.empty()) { + unsigned long line = XML_GetCurrentLineNumber(parser); + parse_error err(original_file ? *original_file : "<gnucash>", line, + have_error); + std::cerr << "Error: " << err.what() << std::endl; + have_error = ""; } } + XML_ParserFree(parser); accounts_by_id.clear(); curr_account_id.clear(); - curr_quant.clear(); - - main_ledger->commodities.erase("USD"); - return ledger; + return count; } } // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> + +using namespace boost::python; +using namespace ledger; + +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(gnucash_parse_overloads, + gnucash_parser_t::parse, 2, 4) + +void export_gnucash() { + class_< gnucash_parser_t, bases<parser_t> > ("GnucashParser") + .def("test", &gnucash_parser_t::test) + .def("parse", &gnucash_parser_t::parse, gnucash_parse_overloads()) + ; +} + +#endif // USE_BOOST_PYTHON diff --git a/gnucash.h b/gnucash.h new file mode 100644 index 00000000..6b71fffa --- /dev/null +++ b/gnucash.h @@ -0,0 +1,21 @@ +#ifndef _GNUCASH_H +#define _GNUCASH_H + +#include "parser.h" + +namespace ledger { + +class gnucash_parser_t : public parser_t +{ + public: + virtual bool test(std::istream& in) const; + + virtual unsigned int parse(std::istream& in, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL); +}; + +} // namespace ledger + +#endif // _GNUCASH_H diff --git a/journal.cc b/journal.cc new file mode 100644 index 00000000..5767a377 --- /dev/null +++ b/journal.cc @@ -0,0 +1,790 @@ +#include "journal.h" +#include "datetime.h" +#include "valexpr.h" +#include "mask.h" +#include "error.h" +#ifdef USE_BOOST_PYTHON +#include "py_eval.h" +#endif +#include "acconf.h" + +#include <fstream> + +namespace ledger { + +const std::string version = PACKAGE_VERSION; + +bool transaction_t::valid() const +{ + if (! entry) + return false; + + bool found = false; + for (transactions_list::const_iterator i = entry->transactions.begin(); + i != entry->transactions.end(); + i++) + if (*i == this) { + found = true; + break; + } + if (! found) + return false; + + if (! account) + return false; + + if (! amount.valid()) + return false; + + if (cost && ! cost->valid()) + return false; + + if (flags & ~0x000f) + return false; + + return true; +} + +void entry_base_t::add_transaction(transaction_t * xact) +{ + transactions.push_back(xact); +} + +bool entry_base_t::remove_transaction(transaction_t * xact) +{ + transactions.remove(xact); + return true; +} + +bool entry_base_t::finalize() +{ + // Scan through and compute the total balance for the entry. This + // is used for auto-calculating the value of entries with no cost, + // and the per-unit price of unpriced commodities. + + value_t balance; + + bool no_amounts = true; + for (transactions_list::const_iterator x = transactions.begin(); + x != transactions.end(); + x++) + if (! ((*x)->flags & TRANSACTION_VIRTUAL) || + ((*x)->flags & TRANSACTION_BALANCE)) { + amount_t * p = (*x)->cost ? (*x)->cost : &(*x)->amount; + if (*p) { + if (no_amounts) { + balance = *p; + no_amounts = false; + } else { + balance += *p; + } + } + } + + // If it's a null entry, then let the user have their fun + if (no_amounts) + return true; + + // If one transaction of a two-line transaction is of a different + // commodity than the others, and it has no per-unit price, + // determine its price by dividing the unit count into the value of + // the balance. This is done for the last eligible commodity. + + if (balance.type == value_t::BALANCE && + ((balance_t *) balance.data)->amounts.size() == 2) + for (transactions_list::const_iterator x = transactions.begin(); + x != transactions.end(); + x++) { + if ((*x)->cost || ((*x)->flags & TRANSACTION_VIRTUAL)) + continue; + + for (amounts_map::const_iterator i + = ((balance_t *) balance.data)->amounts.begin(); + i != ((balance_t *) balance.data)->amounts.end(); + i++) + if ((*i).second.commodity() != (*x)->amount.commodity()) { + assert((*x)->amount); + balance -= (*x)->amount; + assert(! (*x)->cost); + (*x)->cost = new amount_t(- (*i).second); + balance += *(*x)->cost; + break; + } + + break; + } + + // Walk through each of the transactions, fixing up any that we + // can, and performing any on-the-fly calculations. + + bool empty_allowed = true; + + for (transactions_list::const_iterator x = transactions.begin(); + x != transactions.end(); + x++) { + if (! (*x)->amount.null() || + (((*x)->flags & TRANSACTION_VIRTUAL) && + ! ((*x)->flags & TRANSACTION_BALANCE))) + continue; + + if (! empty_allowed || ! balance || balance.type != value_t::AMOUNT) + return false; + + empty_allowed = false; + + // If one transaction gives no value at all -- and all the + // rest are of the same commodity -- then its value is the + // inverse of the computed value of the others. + + assert(balance.type == value_t::AMOUNT); + (*x)->amount = *((amount_t *) balance.data); + (*x)->amount.negate(); + + balance = 0L; + } + + return ! balance; +} + +entry_t::entry_t(const entry_t& e) + : entry_base_t(e), date(e.date), state(e.state), code(e.code), payee(e.payee) +{ + DEBUG_PRINT("ledger.memory.ctors", "ctor entry_t"); + + for (transactions_list::const_iterator i = transactions.begin(); + i != transactions.end(); + i++) + (*i)->entry = this; +} + +void entry_t::add_transaction(transaction_t * xact) +{ + xact->entry = this; + entry_base_t::add_transaction(xact); +} + +bool entry_t::valid() const +{ + if (! date) + return false; + + if (state != UNCLEARED && state != CLEARED && state != PENDING) + return false; + + for (transactions_list::const_iterator i = transactions.begin(); + i != transactions.end(); + i++) + if ((*i)->entry != this || ! (*i)->valid()) + return false; + + return true; +} + +auto_entry_t::auto_entry_t(const std::string& _predicate) + : predicate_string(_predicate) +{ + DEBUG_PRINT("ledger.memory.ctors", "ctor auto_entry_t"); + predicate = new item_predicate<transaction_t>(predicate_string); +} + +auto_entry_t::~auto_entry_t() +{ + DEBUG_PRINT("ledger.memory.dtors", "dtor auto_entry_t"); + if (predicate) + delete predicate; +} + +void auto_entry_t::extend_entry(entry_base_t& entry) +{ + transactions_list initial_xacts(entry.transactions.begin(), + entry.transactions.end()); + + for (transactions_list::iterator i = initial_xacts.begin(); + i != initial_xacts.end(); + i++) + if ((*predicate)(**i)) + for (transactions_list::iterator t = transactions.begin(); + t != transactions.end(); + t++) { + amount_t amt; + if ((*t)->amount.commodity().symbol.empty()) + amt = (*i)->amount * (*t)->amount; + else + amt = (*t)->amount; + + account_t * account = (*t)->account; + std::string fullname = account->fullname(); + assert(! fullname.empty()); + + if (fullname == "$account") + account = (*i)->account; +#ifdef USE_BOOST_PYTHON + else if (fullname[0] == '@') { + value_expr_t * expr = parse_value_expr(fullname); + if (expr->kind == value_expr_t::F_INTERP_FUNC) + python_call(expr->constant_s, expr->right, details_t(**i), + *(*t)->account); + } +#endif + + transaction_t * xact + = new transaction_t(account, amt, (*t)->flags | TRANSACTION_AUTO); + entry.add_transaction(xact); + } +} + +account_t::~account_t() +{ + DEBUG_PRINT("ledger.memory.dtors", "dtor account_t"); + //assert(! data); + + for (accounts_map::iterator i = accounts.begin(); + i != accounts.end(); + i++) + delete (*i).second; +} + +account_t * account_t::find_account(const std::string& name, + const bool auto_create) +{ + accounts_map::const_iterator i = accounts.find(name); + if (i != accounts.end()) + return (*i).second; + + char buf[256]; + + std::string::size_type sep = name.find(':'); + assert(sep < 256|| sep == std::string::npos); + + const char * first, * rest; + if (sep == std::string::npos) { + first = name.c_str(); + rest = NULL; + } else { + std::strncpy(buf, name.c_str(), sep); + buf[sep] = '\0'; + + first = buf; + rest = name.c_str() + sep + 1; + } + + account_t * account; + + i = accounts.find(first); + if (i == accounts.end()) { + if (! auto_create) + return NULL; + + account = new account_t(this, first); + + std::pair<accounts_map::iterator, bool> result + = accounts.insert(accounts_pair(first, account)); + assert(result.second); + } else { + account = (*i).second; + } + + if (rest) + account = account->find_account(rest, auto_create); + + return account; +} + +static inline +account_t * find_account_re_(account_t * account, const mask_t& regexp) +{ + if (regexp.match(account->fullname())) + return account; + + for (accounts_map::iterator i = account->accounts.begin(); + i != account->accounts.end(); + i++) + if (account_t * a = find_account_re_((*i).second, regexp)) + return a; + + return NULL; +} + +account_t * journal_t::find_account_re(const std::string& regexp) +{ + return find_account_re_(master, mask_t(regexp)); +} + +std::string account_t::fullname() const +{ + if (! _fullname.empty()) { + return _fullname; + } else { + const account_t * first = this; + std::string fullname = name; + + while (first->parent) { + first = first->parent; + if (! first->name.empty()) + fullname = first->name + ":" + fullname; + } + + _fullname = fullname; + + return fullname; + } +} + +std::ostream& operator<<(std::ostream& out, const account_t& account) +{ + out << account.fullname(); + return out; +} + +bool account_t::valid() const +{ + if (depth > 256) + return false; + + for (accounts_map::const_iterator i = accounts.begin(); + i != accounts.end(); + i++) + if (! (*i).second->valid()) + return false; + + return true; +} + +journal_t::~journal_t() +{ + DEBUG_PRINT("ledger.memory.dtors", "dtor journal_t"); + + assert(master); + delete master; + + // Don't bother unhooking each entry's transactions from the + // accounts they refer to, because all accounts are about to + // be deleted. + for (entries_list::iterator i = entries.begin(); + i != entries.end(); + i++) + if (! item_pool || + ((char *) *i) < item_pool || ((char *) *i) >= item_pool_end) + delete *i; + else + (*i)->~entry_t(); + + for (auto_entries_list::iterator i = auto_entries.begin(); + i != auto_entries.end(); + i++) + if (! item_pool || + ((char *) *i) < item_pool || ((char *) *i) >= item_pool_end) + delete *i; + else + (*i)->~auto_entry_t(); + + for (period_entries_list::iterator i = period_entries.begin(); + i != period_entries.end(); + i++) + if (! item_pool || + ((char *) *i) < item_pool || ((char *) *i) >= item_pool_end) + delete *i; + else + (*i)->~period_entry_t(); + + if (item_pool) + delete[] item_pool; +} + +bool journal_t::add_entry(entry_t * entry) +{ + if (! entry->finalize() || + ! run_hooks(entry_finalize_hooks, *entry)) + return false; + + entries.push_back(entry); + + for (transactions_list::const_iterator i = entry->transactions.begin(); + i != entry->transactions.end(); + i++) + if ((*i)->cost && (*i)->amount) + (*i)->amount.commodity().add_price(entry->date, + *(*i)->cost / (*i)->amount); + + return true; +} + +bool journal_t::remove_entry(entry_t * entry) +{ + bool found = false; + entries_list::iterator i; + for (i = entries.begin(); i != entries.end(); i++) + if (*i == entry) { + found = true; + break; + } + if (! found) + return false; + + entries.erase(i); + + return true; +} + +bool journal_t::valid() const +{ + if (! master->valid()) + return false; + + for (entries_list::const_iterator i = entries.begin(); + i != entries.end(); + i++) + if (! (*i)->valid()) + return false; + + for (commodities_map::const_iterator i = commodity_t::commodities.begin(); + i != commodity_t::commodities.end(); + i++) + if (! (*i).second->valid()) + return false; + + return true; +} + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> +#include <boost/python/exception_translator.hpp> + +using namespace boost::python; +using namespace ledger; + +entry_t& transaction_entry(const transaction_t& xact) +{ + return *xact.entry; +} + +unsigned int transactions_len(entry_base_t& entry) +{ + return entry.transactions.size(); +} + +transaction_t& transactions_getitem(entry_base_t& entry, int i) +{ + static int last_index = 0; + static entry_base_t * last_entry = NULL; + static transactions_list::iterator elem; + + std::size_t len = entry.transactions.size(); + + if (abs(i) >= len) { + PyErr_SetString(PyExc_IndexError, "Index out of range"); + throw_error_already_set(); + } + + if (&entry == last_entry && i == last_index + 1) { + last_index = i; + return **++elem; + } + + int x = i < 0 ? len + i : i; + elem = entry.transactions.begin(); + while (--x >= 0) + elem++; + + last_entry = &entry; + last_index = i; + + return **elem; +} + +unsigned int entries_len(journal_t& journal) +{ + return journal.entries.size(); +} + +entry_t& entries_getitem(journal_t& journal, int i) +{ + static int last_index = 0; + static journal_t * last_journal = NULL; + static entries_list::iterator elem; + + std::size_t len = journal.entries.size(); + + if (abs(i) >= len) { + PyErr_SetString(PyExc_IndexError, "Index out of range"); + throw_error_already_set(); + } + + if (&journal == last_journal && i == last_index + 1) { + last_index = i; + return **++elem; + } + + int x = i < 0 ? len + i : i; + elem = journal.entries.begin(); + while (--x >= 0) + elem++; + + last_journal = &journal; + last_index = i; + + return **elem; +} + +unsigned int accounts_len(account_t& account) +{ + return account.accounts.size(); +} + +account_t& accounts_getitem(account_t& account, int i) +{ + static int last_index = 0; + static account_t * last_account = NULL; + static accounts_map::iterator elem; + + std::size_t len = account.accounts.size(); + + if (abs(i) >= len) { + PyErr_SetString(PyExc_IndexError, "Index out of range"); + throw_error_already_set(); + } + + if (&account == last_account && i == last_index + 1) { + last_index = i; + return *(*++elem).second; + } + + int x = i < 0 ? len + i : i; + elem = account.accounts.begin(); + while (--x >= 0) + elem++; + + last_account = &account; + last_index = i; + + return *(*elem).second; +} + +PyObject * py_account_get_data(account_t& account) +{ + return (PyObject *) account.data; +} + +void py_account_set_data(account_t& account, PyObject * obj) +{ + account.data = obj; +} + +account_t * py_find_account_1(journal_t& journal, const std::string& name) +{ + return journal.find_account(name); +} + +account_t * py_find_account_2(journal_t& journal, const std::string& name, + const bool auto_create) +{ + return journal.find_account(name, auto_create); +} + +bool py_add_entry(journal_t& journal, entry_t * entry) { + return journal.add_entry(new entry_t(*entry)); +} + +void py_add_transaction(entry_base_t& entry, transaction_t * xact) { + return entry.add_transaction(new transaction_t(*xact)); +} + +struct entry_base_wrap : public entry_base_t +{ + PyObject * self; + entry_base_wrap(PyObject * self_) : self(self_) {} + + virtual bool valid() const { + return call_method<bool>(self, "valid"); + } +}; + +struct py_entry_finalizer_t : public entry_finalizer_t { + object pyobj; + py_entry_finalizer_t() {} + py_entry_finalizer_t(object obj) : pyobj(obj) {} + py_entry_finalizer_t(const py_entry_finalizer_t& other) + : pyobj(other.pyobj) {} + virtual bool operator()(entry_t& entry) { + return call<bool>(pyobj.ptr(), entry); + } +}; + +std::list<py_entry_finalizer_t> py_finalizers; + +void py_add_entry_finalizer(journal_t& journal, object x) +{ + py_finalizers.push_back(py_entry_finalizer_t(x)); + journal.add_entry_finalizer(&py_finalizers.back()); +} + +void py_remove_entry_finalizer(journal_t& journal, object x) +{ + for (std::list<py_entry_finalizer_t>::iterator i = py_finalizers.begin(); + i != py_finalizers.end(); + i++) + if ((*i).pyobj == x) { + journal.remove_entry_finalizer(&(*i)); + py_finalizers.erase(i); + return; + } +} + +void py_run_entry_finalizers(journal_t& journal, entry_t& entry) +{ + run_hooks(journal.entry_finalize_hooks, entry); +} + +#define EXC_TRANSLATOR(type) \ + void exc_translate_ ## type(const type& err) { \ + PyErr_SetString(PyExc_RuntimeError, err.what()); \ + } + +EXC_TRANSLATOR(error) +EXC_TRANSLATOR(interval_expr_error) +EXC_TRANSLATOR(format_error) +EXC_TRANSLATOR(parse_error) + +void export_journal() +{ + scope().attr("TRANSACTION_NORMAL") = TRANSACTION_NORMAL; + scope().attr("TRANSACTION_VIRTUAL") = TRANSACTION_VIRTUAL; + scope().attr("TRANSACTION_BALANCE") = TRANSACTION_BALANCE; + scope().attr("TRANSACTION_AUTO") = TRANSACTION_AUTO; + scope().attr("TRANSACTION_BULK_ALLOC") = TRANSACTION_BULK_ALLOC; + + class_< transaction_t > ("Transaction") + .def(init<account_t *, amount_t, optional<unsigned int, std::string> >()) + + .def(self == self) + .def(self != self) + + .add_property("entry", + make_getter(&transaction_t::entry, + return_value_policy<reference_existing_object>())) + .add_property("account", + make_getter(&transaction_t::account, + return_value_policy<reference_existing_object>())) + .def_readwrite("amount", &transaction_t::amount) + .add_property("cost", + make_getter(&transaction_t::cost, + return_internal_reference<1>())) + .def_readwrite("flags", &transaction_t::flags) + .def_readwrite("note", &transaction_t::note) + .def_readwrite("data", &transaction_t::data) + + .def("valid", &transaction_t::valid) + ; + + class_< account_t > + ("Account", + init<optional<account_t *, std::string, std::string> >() + [with_custodian_and_ward<1, 2>()]) + .def(self == self) + .def(self != self) + + .add_property("parent", + make_getter(&account_t::parent, + return_internal_reference<1>())) + .def_readwrite("name", &account_t::name) + .def_readwrite("note", &account_t::note) + .def_readonly("depth", &account_t::depth) + .add_property("data", py_account_get_data, py_account_set_data) + .def_readonly("ident", &account_t::ident) + + .def(self_ns::str(self)) + + .def("fullname", &account_t::fullname) + .def("add_account", &account_t::add_account) + .def("remove_account", &account_t::remove_account) + .def("find_account", &account_t::find_account, + return_value_policy<reference_existing_object>()) + + .def("valid", &account_t::valid) + ; + + class_< journal_t > ("Journal") + .def(self == self) + .def(self != self) + + .add_property("master", + make_getter(&journal_t::master, + return_internal_reference<1>())) + .def_readonly("sources", &journal_t::sources) + + .def("__len__", entries_len) + .def("__getitem__", entries_getitem, return_internal_reference<1>()) + .def("add_account", &journal_t::add_account) + .def("remove_account", &journal_t::remove_account) + .def("find_account", py_find_account_1, return_internal_reference<1>()) + .def("find_account", py_find_account_2, return_internal_reference<1>()) + .def("find_account_re", &journal_t::find_account_re, + return_internal_reference<1>()) + + .def("add_entry", py_add_entry) + .def("remove_entry", &journal_t::remove_entry) + .def("add_entry_finalizer", py_add_entry_finalizer) + .def("remove_entry_finalizer", py_remove_entry_finalizer) + .def("run_entry_finalizers", py_run_entry_finalizers) + + .def("valid", &journal_t::valid) + ; + + class_< entry_base_t, entry_base_wrap, boost::noncopyable > ("EntryBase") + .def("__len__", transactions_len) + .def("__getitem__", transactions_getitem, + return_internal_reference<1>()) + + .def("add_transaction", py_add_transaction) + .def("remove_transaction", &entry_base_t::remove_transaction) + + .def(self == self) + .def(self != self) + + .def("valid", &entry_base_t::valid) + ; + + scope in_entry = class_< entry_t, bases<entry_base_t> > ("Entry") + .def_readwrite("date", &entry_t::date) + .def_readwrite("state", &entry_t::state) + .def_readwrite("code", &entry_t::code) + .def_readwrite("payee", &entry_t::payee) + + .def("valid", &entry_t::valid) + ; + + class_< auto_entry_t, bases<entry_base_t> > ("AutoEntry") + .add_property("predicate", + make_getter(&auto_entry_t::predicate, + return_internal_reference<1>())) + .def_readonly("predicate_string", &auto_entry_t::predicate_string) + + .def("valid", &auto_entry_t::valid) + ; + + class_< period_entry_t, bases<entry_base_t> > ("PeriodEntry") + .def_readonly("period", &period_entry_t::period) + .def_readonly("period_string", &period_entry_t::period_string) + + .def("valid", &period_entry_t::valid) + ; + + enum_< entry_t::state_t > ("State") + .value("UNCLEARED", entry_t::UNCLEARED) + .value("CLEARED", entry_t::CLEARED) + .value("PENDING", entry_t::PENDING) + ; + +#define EXC_TRANSLATE(type) \ + register_exception_translator<type>(&exc_translate_ ## type); + + EXC_TRANSLATE(error); + EXC_TRANSLATE(interval_expr_error); + EXC_TRANSLATE(format_error); + EXC_TRANSLATE(parse_error); +} + +#endif // USE_BOOST_PYTHON diff --git a/journal.h b/journal.h new file mode 100644 index 00000000..89455094 --- /dev/null +++ b/journal.h @@ -0,0 +1,385 @@ +#ifndef _JOURNAL_H +#define _JOURNAL_H + +#include <map> +#include <list> +#include <string> +#include <ctime> +#include <iostream> + +#include "amount.h" +#include "datetime.h" +#include "value.h" +#include "error.h" +#include "debug.h" + +namespace ledger { + +// These flags persist with the object +#define TRANSACTION_NORMAL 0x0000 +#define TRANSACTION_VIRTUAL 0x0001 +#define TRANSACTION_BALANCE 0x0002 +#define TRANSACTION_AUTO 0x0004 +#define TRANSACTION_BULK_ALLOC 0x0008 + +class entry_t; +class account_t; + +class transaction_t +{ + public: + entry_t * entry; + account_t * account; + amount_t amount; + amount_t * cost; + unsigned short flags; + std::string note; + mutable void * data; + + transaction_t(account_t * _account = NULL) + : entry(NULL), account(_account), cost(NULL), + flags(TRANSACTION_NORMAL), data(NULL) { + DEBUG_PRINT("ledger.memory.ctors", "ctor transaction_t"); + } + + transaction_t(account_t * _account, + const amount_t& _amount, + unsigned int _flags = TRANSACTION_NORMAL, + const std::string& _note = "") + : entry(NULL), account(_account), amount(_amount), + cost(NULL), flags(_flags), note(_note), data(NULL) { + DEBUG_PRINT("ledger.memory.ctors", "ctor transaction_t"); + } + + transaction_t(const transaction_t& xact) + : entry(xact.entry), account(xact.account), amount(xact.amount), + cost(xact.cost ? new amount_t(*xact.cost) : NULL), + flags(xact.flags), note(xact.note), data(NULL) { + DEBUG_PRINT("ledger.memory.ctors", "ctor transaction_t"); + } + + ~transaction_t() { + DEBUG_PRINT("ledger.memory.dtors", "dtor transaction_t"); + if (cost) + delete cost; + } + + bool operator==(const transaction_t& xact) { + return this == &xact; + } + bool operator!=(const transaction_t& xact) { + return ! (*this == xact); + } + + bool valid() const; +}; + +typedef std::list<transaction_t *> transactions_list; + +class entry_base_t +{ + public: + transactions_list transactions; + + entry_base_t() { + DEBUG_PRINT("ledger.memory.ctors", "ctor entry_base_t"); + } + entry_base_t(const entry_base_t& e) { + DEBUG_PRINT("ledger.memory.ctors", "ctor entry_base_t"); + for (transactions_list::const_iterator i = e.transactions.begin(); + i != e.transactions.end(); + i++) + transactions.push_back(new transaction_t(**i)); + } + virtual ~entry_base_t() { + DEBUG_PRINT("ledger.memory.dtors", "dtor entry_base_t"); + for (transactions_list::iterator i = transactions.begin(); + i != transactions.end(); + i++) + if (! ((*i)->flags & TRANSACTION_BULK_ALLOC)) + delete *i; + else + (*i)->~transaction_t(); + } + + bool operator==(const entry_base_t& entry) { + return this == &entry; + } + bool operator!=(const entry_base_t& entry) { + return ! (*this == entry); + } + + virtual void add_transaction(transaction_t * xact); + virtual bool remove_transaction(transaction_t * xact); + + virtual bool finalize(); + virtual bool valid() const = 0; +}; + +class entry_t : public entry_base_t +{ + public: + enum state_t { UNCLEARED, CLEARED, PENDING }; + + std::time_t date; + state_t state; + std::string code; + std::string payee; + + entry_t() : date(0), state(UNCLEARED) { + DEBUG_PRINT("ledger.memory.ctors", "ctor entry_t"); + } + entry_t(const entry_t& e); + +#ifdef DEBUG_ENABLED + virtual ~entry_t() { + DEBUG_PRINT("ledger.memory.dtors", "dtor entry_t"); + } +#endif + + virtual void add_transaction(transaction_t * xact); + + virtual bool valid() const; +}; + +struct entry_finalizer_t { + virtual ~entry_finalizer_t() {} + virtual bool operator()(entry_t& entry) = 0; +}; + + +template <typename T> +class item_predicate; + +class auto_entry_t : public entry_base_t +{ +public: + item_predicate<transaction_t> * predicate; + std::string predicate_string; + + auto_entry_t() : predicate(NULL) { + DEBUG_PRINT("ledger.memory.ctors", "ctor auto_entry_t"); + } + auto_entry_t(const std::string& _predicate); + + virtual ~auto_entry_t(); + + virtual void extend_entry(entry_base_t& entry); + virtual bool valid() const { + return true; + } +}; + +class journal_t; +struct auto_entry_finalizer_t : public entry_finalizer_t { + journal_t * journal; + auto_entry_finalizer_t(journal_t * _journal) : journal(_journal) {} + virtual bool operator()(entry_t& entry); +}; + + +class period_entry_t : public entry_base_t +{ + public: + interval_t period; + std::string period_string; + + period_entry_t() { + DEBUG_PRINT("ledger.memory.ctors", "ctor period_entry_t"); + } + period_entry_t(const std::string& _period) + : period(_period), period_string(_period) { + DEBUG_PRINT("ledger.memory.ctors", "ctor period_entry_t"); + } + period_entry_t(const period_entry_t& e) + : entry_base_t(e), period(e.period), period_string(e.period_string) { + DEBUG_PRINT("ledger.memory.ctors", "ctor period_entry_t"); + } + +#ifdef DEBUG_ENABLED + virtual ~period_entry_t() { + DEBUG_PRINT("ledger.memory.dtors", "dtor period_entry_t"); + } +#endif + + virtual bool valid() const { + return period; + } +}; + + +typedef std::map<const std::string, account_t *> accounts_map; +typedef std::pair<const std::string, account_t *> accounts_pair; + +class account_t +{ + public: + typedef unsigned long ident_t; + + account_t * parent; + std::string name; + std::string note; + unsigned short depth; + accounts_map accounts; + + mutable void * data; + mutable ident_t ident; + mutable std::string _fullname; + + account_t(account_t * _parent = NULL, + const std::string& _name = "", + const std::string& _note = "") + : parent(_parent), name(_name), note(_note), + depth(parent ? parent->depth + 1 : 0), data(NULL), ident(0) { + DEBUG_PRINT("ledger.memory.ctors", "ctor account_t"); + } + + ~account_t(); + + bool operator==(const account_t& account) { + return this == &account; + } + bool operator!=(const account_t& account) { + return ! (*this == account); + } + + std::string fullname() const; + + void add_account(account_t * acct) { + accounts.insert(accounts_pair(acct->name, acct)); + } + bool remove_account(account_t * acct) { + accounts_map::size_type n = accounts.erase(acct->name); + return n > 0; + } + + account_t * find_account(const std::string& name, bool auto_create = true); + + operator std::string() const { + return fullname(); + } + + bool valid() const; + + friend class journal_t; +}; + +std::ostream& operator<<(std::ostream& out, const account_t& account); + + +struct func_finalizer_t : public entry_finalizer_t { + typedef bool (*func_t)(entry_t& entry); + func_t func; + func_finalizer_t(func_t _func) : func(_func) {} + func_finalizer_t(const func_finalizer_t& other) : func(other.func) {} + virtual bool operator()(entry_t& entry) { + return func(entry); + } +}; + +template <typename T> +void add_hook(std::list<T>& list, T obj, const bool prepend = false) { + if (prepend) + list.push_front(obj); + else + list.push_back(obj); +} + +template <typename T> +void remove_hook(std::list<T>& list, T obj) { + list.remove(obj); +} + +template <typename T, typename Data> +bool run_hooks(std::list<T>& list, Data& item) { + for (typename std::list<T>::const_iterator i = list.begin(); + i != list.end(); + i++) + if (! (*(*i))(item)) + return false; + return true; +} + + +typedef std::list<entry_t *> entries_list; +typedef std::list<auto_entry_t *> auto_entries_list; +typedef std::list<period_entry_t *> period_entries_list; +typedef std::list<std::string> strings_list; + +class journal_t +{ + public: + account_t * master; + entries_list entries; + strings_list sources; + char * item_pool; + char * item_pool_end; + + auto_entries_list auto_entries; + period_entries_list period_entries; + mutable accounts_map accounts_cache; + + std::list<entry_finalizer_t *> entry_finalize_hooks; + + journal_t() { + DEBUG_PRINT("ledger.memory.ctors", "ctor journal_t"); + master = new account_t(NULL, ""); + item_pool = item_pool_end = NULL; + } + ~journal_t(); + + bool operator==(const journal_t& journal) { + return this == &journal; + } + bool operator!=(const journal_t& journal) { + return ! (*this == journal); + } + + void add_account(account_t * acct) { + master->add_account(acct); + } + bool remove_account(account_t * acct) { + return master->remove_account(acct); + } + + account_t * find_account(const std::string& name, bool auto_create = true) { + accounts_map::iterator c = accounts_cache.find(name); + if (c != accounts_cache.end()) + return (*c).second; + + account_t * account = master->find_account(name, auto_create); + accounts_cache.insert(accounts_pair(name, account)); + return account; + } + account_t * find_account_re(const std::string& regexp); + + bool add_entry(entry_t * entry); + bool remove_entry(entry_t * entry); + + void add_entry_finalizer(entry_finalizer_t * finalizer) { + add_hook<entry_finalizer_t *>(entry_finalize_hooks, finalizer); + } + void remove_entry_finalizer(entry_finalizer_t * finalizer) { + remove_hook<entry_finalizer_t *>(entry_finalize_hooks, finalizer); + } + + bool valid() const; +}; + +inline void extend_entry_base(journal_t * journal, entry_base_t& entry) { + for (auto_entries_list::iterator i = journal->auto_entries.begin(); + i != journal->auto_entries.end(); + i++) + (*i)->extend_entry(entry); +} + +inline bool auto_entry_finalizer_t::operator()(entry_t& entry) { + extend_entry_base(journal, entry); + return true; +} + +extern const std::string version; + +} // namespace ledger + +#endif // _JOURNAL_H diff --git a/ledger.cc b/ledger.cc deleted file mode 100644 index 4985fba6..00000000 --- a/ledger.cc +++ /dev/null @@ -1,674 +0,0 @@ -#include "ledger.h" - -#include <fstream> -#include <unistd.h> - -namespace ledger { - -book * main_ledger; - -extern int linenum; - -commodity::~commodity() -{ - if (conversion) - delete conversion; - - for (price_map::iterator i = history.begin(); - i != history.end(); - i++) - delete (*i).second; -} - -void commodity::set_price(amount * price, std::time_t * when) -{ - assert(price); - if (when) - history.insert(price_map_pair(*when, price)); - else - conversion = price; -} - -amount * commodity::price(std::time_t * when, - bool use_history, bool download) const -{ - if (conversion || ! when || ! use_history) - return conversion; - - std::time_t age; - amount * price = NULL; - - for (price_map::reverse_iterator i = history.rbegin(); - i != history.rend(); - i++) - if (std::difftime(*when, (*i).first) >= 0) { - age = (*i).first; - price = (*i).second; - break; - } - - extern long pricing_leeway; - time_t now = time(NULL); // the time of the query - - if (download && ! sought && - std::difftime(now, *when) < pricing_leeway && - (! price || std::difftime(*when, age) > pricing_leeway)) { - using namespace std; - - // Only consult the Internet once for any commodity - sought = true; - - char buf[256]; - buf[0] = '\0'; - - if (FILE * fp = popen((string("getquote ") + symbol).c_str(), "r")) { - if (feof(fp) || ! fgets(buf, 255, fp)) { - fclose(fp); - return price; - } - fclose(fp); - } - - if (buf[0]) { - char * p = strchr(buf, '\n'); - if (p) *p = '\0'; - - price = create_amount(buf); - const_cast<commodity *>(this)->set_price(price, &now); - - extern string price_db; - if (! price_db.empty()) { - char buf[128]; - strftime(buf, 127, "%Y/%m/%d %H:%M:%S", localtime(&now)); - ofstream database(price_db.c_str(), ios_base::out | ios_base::app); - database << "P " << buf << " " << symbol << " " - << price->as_str() << endl; - } - } - } - - return price; -} - -const std::string transaction::acct_as_str() const -{ - char * begin = NULL; - char * end = NULL; - - if (is_virtual) { - if (must_balance) { - begin = "["; - end = "]"; - } else { - begin = "("; - end = ")"; - } - } - - if (begin) - return std::string(begin) + acct->as_str() + end; - else - return acct->as_str(); -} - -void transaction::print(std::ostream& out, bool display_quantity, - bool display_price) const -{ - out.width(30); - out << std::left << acct_as_str(); - - if (cost && display_quantity) { - out << " "; - out.width(12); - - std::string value = cost->as_str(true); - if (! display_price) { - int index = value.find('@'); - if (index != -1) - value = std::string(value, 0, index - 1); - } - out << std::right << value; - } - - if (! note.empty()) - out << " ; " << note; - - out << std::endl; -} - -void entry::print(std::ostream& out, bool shortcut) const -{ - char buf[32]; - std::strftime(buf, 31, "%Y/%m/%d ", std::localtime(&date)); - out << buf; - - if (cleared) - out << "* "; - if (! code.empty()) - out << '(' << code << ") "; - if (! desc.empty()) - out << desc; - - out << std::endl; - - commodity * comm = NULL; - int size = 0; - - for (std::list<transaction *>::const_iterator x = xacts.begin(); - x != xacts.end(); - x++) { - if ((*x)->is_virtual && ! (*x)->must_balance) - continue; - - if (! comm) - comm = (*x)->cost->commdty(); - else if (comm != (*x)->cost->commdty()) - shortcut = false; - - size++; - } - - if (shortcut && size != 2) - shortcut = false; - - for (std::list<transaction *>::const_iterator x = xacts.begin(); - x != xacts.end(); - x++) { - if ((*x)->is_virtual && ! (*x)->specified) - continue; - - out << " "; - - (*x)->print(out, (! shortcut || x == xacts.begin() || - ((*x)->is_virtual && ! (*x)->must_balance)), - size != 2); - } - - out << std::endl; -} - -bool entry::validate(bool show_unaccounted) const -{ - totals balance; - - for (std::list<transaction *>::const_iterator x = xacts.begin(); - x != xacts.end(); - x++) - if ((*x)->cost && (*x)->must_balance) { - amount * value = (*x)->cost->value(); - balance.credit(value); - delete value; - } - - if (show_unaccounted && ! balance.is_zero()) { - std::cerr << "Unaccounted-for balances are:" << std::endl; - balance.print(std::cerr, 20); - std::cerr << std::endl << std::endl; - } - return balance.is_zero(); // must balance to 0.0 -} - -bool entry::finalize(bool do_compute) -{ - // Scan through and compute the total balance for the entry. This - // is used for auto-calculating the value of entries with no cost, - // and the per-unit price of unpriced commodities. - - totals balance; - - for (std::list<transaction *>::iterator x = xacts.begin(); - x != xacts.end(); - x++) - if ((*x)->cost && ! (*x)->is_virtual) { - amount * value = (*x)->cost->value(); - balance.credit(value); - delete value; - } - - // If one transaction of a two-line transaction is of a different - // commodity than the others, and it has no per-unit price, - // determine its price by dividing the unit count into the value of - // the balance. This is done for the last eligible commodity. - - if (! balance.amounts.empty() && balance.amounts.size() == 2) { - for (std::list<transaction *>::iterator x = xacts.begin(); - x != xacts.end(); - x++) { - if ((*x)->is_virtual || (*x)->cost->has_price()) - continue; - - for (totals::iterator i = balance.amounts.begin(); - i != balance.amounts.end(); - i++) - if ((*i).second->commdty() != (*x)->cost->commdty()) { - (*x)->cost->set_value((*i).second); - assert((*x)->cost->has_price()); - (*x)->cost->commdty()->set_price((*x)->cost->per_item_price(), - &date); - break; - } - - break; - } - } - - // Walk through each of the transactions, fixing up any that we - // can, and performing any on-the-fly calculations. - - bool empty_allowed = true; - - for (std::list<transaction *>::iterator x = xacts.begin(); - x != xacts.end(); - x++) { - if ((*x)->is_virtual || (*x)->cost) - continue; - - if (! empty_allowed || balance.amounts.empty() || - balance.amounts.size() != 1) { - std::cerr << "Error, line " << linenum - << ": Transaction entry is lacking an amount." - << std::endl; - return false; - } - empty_allowed = false; - - // If one transaction gives no value at all -- and all the - // rest are of the same commodity -- then its value is the - // inverse of the computed value of the others. - - totals::iterator i = balance.amounts.begin(); - (*x)->cost = (*i).second->value(); - (*x)->cost->negate(); - - if (do_compute) - (*x)->acct->balance.credit((*x)->cost); - } - - // If automated transactions are being used, walk through the - // current transaction lines and create new transactions for all - // that match. - - for (book::virtual_map_iterator m = ledger->virtual_mapping.begin(); - m != ledger->virtual_mapping.end(); - m++) { - std::list<transaction *> new_xacts; - - for (std::list<transaction *>::iterator x = xacts.begin(); - x != xacts.end(); - x++) { - if ((*x)->is_virtual || - ! ledger::matches(*((*m).first), (*x)->acct->as_str())) - continue; - - for (std::list<transaction *>::iterator i = (*m).second->begin(); - i != (*m).second->end(); - i++) { - transaction * t; - - if ((*i)->cost->commdty()) { - t = new transaction((*i)->acct, (*i)->cost); - } else { - amount * temp = (*x)->cost->value(); - t = new transaction((*i)->acct, temp->value((*i)->cost)); - delete temp; - } - - t->is_virtual = (*i)->is_virtual; - t->must_balance = (*i)->must_balance; - - new_xacts.push_back(t); - } - } - - // Add to the current entry any virtual transactions which were - // created. We have to do this afterward, otherwise the - // iteration above is screwed up if we try adding new - // transactions during the traversal. - - for (std::list<transaction *>::iterator x = new_xacts.begin(); - x != new_xacts.end(); - x++) { - xacts.push_back(*x); - - if (do_compute) - (*x)->acct->balance.credit((*x)->cost); - } - } - - // Compute the balances again, just to make sure it all comes out - // right (i.e., zero for every commodity). - - if (! validate()) { - std::cerr << "Error, line " << (linenum - 1) - << ": Failed to balance the following transaction:" - << std::endl; - validate(true); - return false; - } - - return true; -} - -bool entry::matches(const regexps_list& regexps) const -{ - if (regexps.empty() || (ledger::matches(regexps, code) || - ledger::matches(regexps, desc))) { - return true; - } - else { - bool match = false; - - for (std::list<transaction *>::const_iterator x = xacts.begin(); - x != xacts.end(); - x++) { - if (ledger::matches(regexps, (*x)->acct->as_str()) || - ledger::matches(regexps, (*x)->note)) { - match = true; - break; - } - } - return match; - } -} - -totals::~totals() -{ - for (iterator i = amounts.begin(); i != amounts.end(); i++) - delete (*i).second; -} - -void totals::credit(const amount * val) -{ - iterator i = amounts.find(val->commdty()); - if (i != amounts.end()) - (*i).second->credit(val); -#ifndef DEBUG - else - amounts.insert(pair(val->commdty(), val->copy())); -#else - else { - std::pair<iterator, bool> result = - amounts.insert(pair(val->commdty(), val->copy())); - assert(result.second); - } -#endif -} - -void totals::credit(const totals& other) -{ - for (const_iterator i = other.amounts.begin(); - i != other.amounts.end(); - i++) - credit((*i).second); -} - -void totals::negate() -{ - for (const_iterator i = amounts.begin(); i != amounts.end(); i++) - (*i).second->negate(); -} - -bool totals::is_zero() const -{ - for (const_iterator i = amounts.begin(); i != amounts.end(); i++) - if (! (*i).second->is_zero()) - return false; - return true; -} - -bool totals::is_negative() const -{ - bool all_negative = true; - bool some_negative = false; - for (const_iterator i = amounts.begin(); i != amounts.end(); i++) { - if ((*i).second->is_negative()) - some_negative = true; - else if (! (*i).second->is_zero()) - all_negative = false; - } - return some_negative && all_negative; -} - -void totals::print(std::ostream& out, int width) const -{ - bool first = true; - - for (const_iterator i = amounts.begin(); i != amounts.end(); i++) { - if ((*i).second->is_zero()) - continue; - - if (first) - first = false; - else - out << std::endl; - - out.width(width); - out << std::right << (*i).second->as_str(); - } -} - -totals * totals::value() const -{ - totals * cost_basis = new totals; - - for (const_iterator i = amounts.begin(); i != amounts.end(); i++) { - if ((*i).second->is_zero()) - continue; - - amount * value = (*i).second->value(); - cost_basis->credit(value); - delete value; - } - - return cost_basis; -} - -totals * totals::street(std::time_t * when, bool use_history, - bool download) const -{ - totals * street_balance = new totals; - - for (const_iterator i = amounts.begin(); i != amounts.end(); i++) { - if ((*i).second->is_zero()) - continue; - - amount * street = (*i).second->street(when, use_history, download); - street_balance->credit(street); - delete street; - } - - return street_balance; -} - -account::~account() -{ - for (accounts_map_iterator i = children.begin(); - i != children.end(); - i++) - delete (*i).second; -} - -const std::string account::as_str(const account * stop) const -{ - if (! parent || this == stop) - return name; - else if (stop) - return parent->as_str(stop) + ":" + name; - else if (full_name.empty()) - full_name = parent->as_str() + ":" + name; - - return full_name; -} - -mask::mask(const std::string& pat) : exclude(false) -{ - const char * p = pat.c_str(); - if (*p == '-') { - exclude = true; - p++; - while (std::isspace(*p)) - p++; - } - else if (*p == '+') { - p++; - while (std::isspace(*p)) - p++; - } - pattern = p; - - const char *error; - int erroffset; - regexp = pcre_compile(pattern.c_str(), PCRE_CASELESS, - &error, &erroffset, NULL); - if (! regexp) - std::cerr << "Warning: Failed to compile regexp: " << pattern - << std::endl; -} - -mask::mask(const mask& m) : exclude(m.exclude), pattern(m.pattern) -{ - const char *error; - int erroffset; - regexp = pcre_compile(pattern.c_str(), PCRE_CASELESS, - &error, &erroffset, NULL); - assert(regexp); -} - -bool mask::match(const std::string& str) const -{ - static int ovec[30]; - int result = pcre_exec(regexp, NULL, str.c_str(), str.length(), - 0, 0, ovec, 30); - return result >= 0 && ! exclude; -} - -bool matches(const regexps_list& regexps, const std::string& str, - bool * by_exclusion) -{ - if (regexps.empty()) - return false; - - bool match = false; - bool definite = false; - - for (regexps_list_const_iterator r = regexps.begin(); - r != regexps.end(); - r++) { - static int ovec[30]; - int result = pcre_exec((*r).regexp, NULL, str.c_str(), str.length(), - 0, 0, ovec, 30); - if (result >= 0) { - match = ! (*r).exclude; - definite = true; - } - else if ((*r).exclude) { - if (! match) - match = ! definite; - } - else { - definite = true; - } - } - - if (by_exclusion) - *by_exclusion = match && ! definite && by_exclusion; - - return match; -} - -book::~book() -{ - for (commodities_map_iterator i = commodities.begin(); - i != commodities.end(); - i++) - delete (*i).second; - - for (accounts_map_iterator i = accounts.begin(); - i != accounts.end(); - i++) - delete (*i).second; - - for (virtual_map_iterator i = virtual_mapping.begin(); - i != virtual_mapping.end(); - i++) { - delete (*i).first; - - for (std::list<transaction *>::iterator j = (*i).second->begin(); - j != (*i).second->end(); - j++) { - delete *j; - } - delete (*i).second; - } - - for (entries_list_iterator i = entries.begin(); - i != entries.end(); - i++) - delete *i; -} - -account * book::re_find_account(const std::string& regex) -{ - mask acct_regex(regex); - - for (entries_list_reverse_iterator i = entries.rbegin(); - i != entries.rend(); - i++) - for (std::list<transaction *>::iterator x = (*i)->xacts.begin(); - x != (*i)->xacts.end(); - x++) - if (acct_regex.match((*x)->acct->as_str())) - return (*x)->acct; - - return NULL; -} - -account * book::find_account(const std::string& name, bool create) -{ - accounts_map_iterator i = accounts_cache.find(name); - if (i != accounts_cache.end()) - return (*i).second; - - char * buf = new char[name.length() + 1]; - std::strcpy(buf, name.c_str()); - - account * current = NULL; - for (char * tok = std::strtok(buf, ":"); - tok; - tok = std::strtok(NULL, ":")) { - if (! current) { - accounts_map_iterator i = accounts.find(tok); - if (i == accounts.end()) { - if (! create) { - delete[] buf; - return NULL; - } - current = new account(tok); - accounts.insert(accounts_map_pair(tok, current)); - } else { - current = (*i).second; - } - } else { - accounts_map_iterator i = current->children.find(tok); - if (i == current->children.end()) { - if (! create) { - delete[] buf; - return NULL; - } - current = new account(tok, current); - current->parent->children.insert(accounts_map_pair(tok, current)); - } else { - current = (*i).second; - } - } - } - - delete[] buf; - - if (current) - accounts_cache.insert(accounts_map_pair(name, current)); - - return current; -} - -} // namespace ledger @@ -1,4 +1,4 @@ -;;; ledger.el --- Helper code for using my "ledger" command-line tool +;;; ledger.el --- Helper code for use with the "ledger" command-line tool ;; Copyright (C) 2004 John Wiegley (johnw AT gnu DOT org) @@ -32,7 +32,14 @@ ;;; Commentary: -;; This code is only meaningful if you are using "ledger". +;; To use this module: Load this file, open a ledger data file, and +;; type M-x ledger-mode. Once this is done, you can type: +;; +;; C-c C-a add a new entry, based on previous entries +;; C-c C-r reconcile the entries related to an account +;; +;; In the reconcile buffer, use SPACE to toggle the cleared status of +;; a transaction. (defvar ledger-version "1.1" "The version of ledger.el currently loaded") @@ -46,6 +53,11 @@ :type 'file :group 'ledger) +(defcustom ledger-data-file (getenv "LEDGER") + "Path to the ledger data file." + :type 'file + :group 'ledger) + (defvar bold 'bold) (defvar ledger-font-lock-keywords @@ -88,11 +100,10 @@ (if (time-less-p moment date) (throw 'found t))))))) -(defun ledger-add-entry (entry) +(defun ledger-add-entry (entry-text) (interactive - (list (read-string "Entry: " (format-time-string "%Y/%m/%d ")))) - (let* ((args (mapcar 'shell-quote-argument (split-string entry))) - (date (car args)) + (list (read-string "Entry: " (format-time-string "%Y/%m/")))) + (let* ((date (car (split-string entry-text))) (insert-year t) exit-code) (if (string-match "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)" date) (setq date (encode-time 0 0 0 (string-to-int (match-string 3 date)) @@ -106,14 +117,20 @@ (insert (with-temp-buffer (setq exit-code - (apply 'call-process ledger-binary-path nil t nil - (cons "entry" args))) + (ledger-run-ledger + "entry" + (with-temp-buffer + (insert entry-text) + (goto-char (point-min)) + (while (re-search-forward "\\([&$]\\)" nil t) + (replace-match "\\\\\\1")) + (buffer-string)))) (if (= 0 exit-code) (if insert-year - (buffer-string) - (buffer-substring 5 (point-max))) - (concat (if insert-year entry - (substring entry 5)) "\n\n"))))))) + (buffer-substring 2 (point-max)) + (buffer-substring 7 (point-max))) + (concat (if insert-year entry-text + (substring entry-text 6)) "\n"))) "\n")))) (defun ledger-expand-entry () (interactive) @@ -206,9 +223,7 @@ (defun ledger-update-balance-display () (let ((account ledger-acct)) (with-temp-buffer - (let ((exit-code - (apply 'call-process ledger-binary-path nil t nil - (list "-C" "balance" account)))) + (let ((exit-code (ledger-run-ledger "-C" "balance" account))) (if (/= 0 exit-code) (setq ledger-reconcile-text "Reconcile [ERR]") (goto-char (point-min)) @@ -218,7 +233,7 @@ (concat "Reconcile [" (buffer-substring-no-properties (point-min) (point)) "]")))))) - (redraw-modeline)) + (force-mode-line-update)) (defun ledger-reconcile-toggle () (interactive) @@ -290,6 +305,15 @@ (insert " "))) (forward-line)))) +(defun ledger-run-ledger (&rest args) + "run ledger with supplied arguments" + (let ((command + (mapconcat 'identity + (append (list ledger-binary-path + "-f" ledger-data-file) args) " "))) + (insert (shell-command-to-string command))) + 0) + (provide 'ledger) ;;; ledger.el ends here @@ -1,5 +1,5 @@ #ifndef _LEDGER_H -#define _LEDGER_H "$Revision$" +#define _LEDGER_H ////////////////////////////////////////////////////////////////////// // @@ -7,353 +7,30 @@ // // A command-line tool for general double-entry accounting. // -// Copyright (c) 2003 John Wiegley <johnw@newartisans.com> +// Copyright (c) 2003,2004 John Wiegley <johnw@newartisans.com> // -#include <iostream> -#include <string> -#include <vector> -#include <list> -#include <map> -#include <ctime> - -#ifdef DEBUG -#include <cassert> -#else -#ifdef assert -#undef assert -#endif -#define assert(x) -#endif - -#include <pcre.h> // Perl regular expression library - -namespace ledger { - -class amount; -class commodity -{ - commodity(const commodity&); - - typedef std::map<const std::time_t, amount *> price_map; - typedef std::pair<const std::time_t, amount *> price_map_pair; - - public: - std::string name; - std::string symbol; - - mutable bool sought; - - bool prefix; - bool separate; - bool thousands; - bool european; - int precision; - - protected: - mutable price_map history; // the price history - mutable amount * conversion; // fixed conversion (ignore history) - - public: - explicit commodity() : sought(false), prefix(false), separate(true), - thousands(false), european(false), conversion(NULL) {} - - explicit commodity(const std::string& sym, bool pre = false, - bool sep = true, bool thou = true, - bool euro = false, int prec = 2); - ~commodity(); - - void set_price(amount * price, std::time_t * when = NULL); - amount * price(std::time_t * when = NULL, - bool use_history = false, bool download = false) const; -}; - -typedef std::map<const std::string, commodity *> commodities_map; -typedef commodities_map::iterator commodities_map_iterator; -typedef std::pair<const std::string, commodity *> commodities_map_pair; - - -class amount -{ - public: - virtual ~amount() {} - - virtual commodity * commdty() const = 0; - virtual void set_commdty(commodity *) = 0; - - virtual amount * copy() const = 0; - virtual amount * value(const amount * pr = NULL) const = 0; - virtual void set_value(const amount * pr) = 0; - virtual amount * street(std::time_t * when = NULL, - bool use_history = false, - bool download = false) const = 0; - - virtual bool has_price() const = 0; - virtual amount * per_item_price() const = 0; - - // Comparison - - virtual bool is_zero() const = 0; - virtual bool is_negative() const = 0; - virtual int compare(const amount * other) const = 0; - - // Assignment - - virtual void credit(const amount * other) = 0; - virtual void negate() = 0; - - // String conversion routines - - virtual void parse(const std::string& num) = 0; - virtual const std::string as_str(bool full_prec = false) const = 0; -}; - -extern amount * create_amount(const std::string& value, - const amount * cost = NULL); - -class mask -{ - public: - bool exclude; - std::string pattern; - pcre * regexp; - - explicit mask(const std::string& pattern); - - mask(const mask&); - - ~mask() { - pcre_free(regexp); - } - - bool match(const std::string& str) const; -}; - -typedef std::list<mask> regexps_list; -typedef std::list<mask>::iterator regexps_list_iterator; -typedef std::list<mask>::const_iterator regexps_list_const_iterator; - -class account; -class transaction -{ - transaction(const transaction&); - - public: - account * acct; - amount * cost; - - std::string note; - - bool is_virtual; - bool must_balance; - bool specified; - - explicit transaction(account * _acct = NULL, amount * _cost = NULL) - : acct(_acct), cost(_cost), - is_virtual(false), must_balance(true), specified(false) {} - - ~transaction() { - if (cost) - delete cost; - } - - const std::string acct_as_str() const; - - void print(std::ostream& out, bool display_quantity = true, - bool display_price = true) const; -}; - - -class book; -class entry -{ - entry(const entry&); - - public: - book * ledger; - - std::time_t date; - std::string code; - std::string desc; - - bool cleared; - - std::list<transaction *> xacts; - - explicit entry(book * l) : ledger(l), cleared(false) {} - - // If we're running as a command-line tool, it's cheaper to just - // throw away the heap on exit, than spend time freeing things up - // like a good citizen. - - ~entry() { - for (std::list<transaction *>::iterator i = xacts.begin(); - i != xacts.end(); - i++) { - delete *i; - } - } - - bool matches(const regexps_list& regexps) const; - bool validate(bool show_unaccounted = false) const; - bool finalize(bool do_compute = false); - - void print(std::ostream& out, bool shortcut = true) const; -}; - -struct cmp_entry_date { - bool operator()(const entry * left, const entry * right) { - return std::difftime(left->date, right->date) < 0; - } -}; - -typedef std::vector<entry *> entries_list; -typedef entries_list::iterator entries_list_iterator; -typedef entries_list::reverse_iterator entries_list_reverse_iterator; -typedef entries_list::const_iterator entries_list_const_iterator; - - -class totals -{ - totals(const totals&); - - public: - typedef std::map<commodity *, amount *> map; - typedef map::iterator iterator; - typedef map::const_iterator const_iterator; - typedef std::pair<commodity *, amount *> pair; - - map amounts; - - totals() {} - ~totals(); - - void credit(const amount * val); - void credit(const totals& other); - - void negate(); - - bool is_zero() const; - bool is_negative() const; - - void print(std::ostream& out, int width) const; - - totals * value() const; - totals * street(std::time_t * when = NULL, - bool use_history = false, - bool download = false) const; -}; - - -typedef std::map<const std::string, account *> accounts_map; -typedef accounts_map::iterator accounts_map_iterator; -typedef std::pair<const std::string, account *> accounts_map_pair; - -class account -{ - account(const account&); - - public: - account * parent; - - std::string name; -#ifdef READ_GNUCASH - commodity * comm; // default commodity for this account -#endif - totals balance; // optional, parse-time computed balance - int checked; // 'balance' uses this for speed's sake - accounts_map children; - - mutable std::string full_name; - - explicit account() : parent(NULL), checked(0) {} - - explicit account(const std::string& _name, - struct account * _parent = NULL) - : parent(_parent), name(_name), checked(0) {} - - ~account(); - - const std::string as_str(const account * stop = NULL) const; -}; - - -class book -{ - book(const book&); - - public: - typedef std::map<regexps_list *, - std::list<transaction *> *> virtual_map; - - typedef std::pair<regexps_list *, - std::list<transaction *> *> virtual_map_pair; - - typedef virtual_map::const_iterator virtual_map_iterator; - - commodities_map commodities; - accounts_map accounts; - accounts_map accounts_cache; // maps full names to accounts - virtual_map virtual_mapping; - entries_list entries; - int current_year; - - book() {} - ~book(); - - template<typename Compare> - void sort(Compare comp) { - std::sort(entries.begin(), entries.end(), comp); - } - void print(std::ostream& out, regexps_list& regexps, - bool shortcut) const; - - account * re_find_account(const std::string& regex); - account * find_account(const std::string& name, bool create = true); -}; - -extern book * main_ledger; - -inline commodity::commodity(const std::string& sym, bool pre, bool sep, - bool thou, bool euro, int prec) - : symbol(sym), sought(false), prefix(pre), separate(sep), - thousands(thou), european(euro), precision(prec), conversion(NULL) { -#ifdef DEBUG - std::pair<commodities_map_iterator, bool> result = -#endif - main_ledger->commodities.insert(commodities_map_pair(sym, this)); -#ifdef DEBUG - assert(result.second); -#endif -} - -// Parsing routines - -extern int parse_ledger(book * ledger, std::istream& in, - regexps_list& regexps, - bool compute_balances = false, - const char * acct_prefix = NULL); -#ifdef READ_GNUCASH -extern book * parse_gnucash(std::istream& in, bool compute_balances = false); -#endif - -extern int parse_ledger_file(book * ledger, const std::string& file, - regexps_list& regexps, - bool compute_balances = false, - const char * acct_prefix = NULL); - -extern bool parse_date_mask(const char * date_str, - struct std::tm * result); -extern bool parse_date(const char * date_str, std::time_t * result, - const int year = -1); - -extern void record_regexp(const std::string& pattern, regexps_list& regexps); -extern void read_regexps(const std::string& path, regexps_list& regexps); -extern bool matches(const regexps_list& regexps, const std::string& str, - bool * by_exclusion = NULL); - -extern void parse_price_setting(const std::string& setting); - -} // namespace ledger +#include <amount.h> +#include <balance.h> +#include <value.h> + +#include <journal.h> + +#include <datetime.h> +#include <format.h> +#include <quotes.h> +#include <valexpr.h> +#include <walk.h> +#include <derive.h> +#include <error.h> +#include <option.h> +#include <config.h> + +#include <parser.h> +#include <textual.h> +#include <binary.h> +#include <xml.h> +#include <gnucash.h> +#include <qif.h> #endif // _LEDGER_H diff --git a/ledger.texi b/ledger.texi index bcc1937f..d06549f1 100644 --- a/ledger.texi +++ b/ledger.texi @@ -3,6 +3,37 @@ @setfilename ledger.info @settitle Ledger: Command-Line Accounting +@copying +Copyright (c) 2003-2004, John Wiegley. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +- Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +- Neither the name of New Artisans LLC nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +@end copying + @documentencoding iso-8859-1 @iftex @@ -14,28 +45,32 @@ @author John Wiegley @end titlepage +@contents + +@ifnottex @node Top, Introduction, (dir), (dir) @top Overview -@c Page published by Emacs Muse begins here - +@insertcopying +@end ifnottex @menu -* Introduction:: -* Keeping a ledger:: -* Running Ledger:: +* Introduction:: +* Running Ledger:: +* Keeping a ledger:: +* Extending with Python:: @end menu -@node Introduction, Keeping a ledger, Top, Top +@node Introduction, Running Ledger, Top, Top @chapter Introduction Ledger is an accounting tool with the moxie to exist. It provides no bells or whistles, and returns the user to the days before user interfaces were even a twinkling in their father's CRT. -What it does do is to offer a double-entry accounting ledger with all -the flexibility and muscle of its modern day cousins, without any of -the fat. Think of it as the Bran Muffin of accounting tools. +What it does offer is a double-entry accounting ledger with all the +flexibility and muscle of its modern day cousins, without any of the +fat. Think of it as the Bran Muffin of accounting tools. To use it, you need to start keeping a ledger. This is the basis of all accounting, and if you haven't started yet, now is the time to @@ -61,10 +96,10 @@ accounting tools. The next step up from a checkbook ledger, is a ledger that keeps track of all your accounts, not just checking. In such a ledger, you record -not only who gets paid---in the case of a debit---but where the -money came from. In a checkbook ledger, its assumed that all the -money comes from your checking account. But in a general ledger, you -write transaction two-lines: The source account and target account. +not only who gets paid---in the case of a debit---but where the money +came from. In a checkbook ledger, its assumed that all the money +comes from your checking account. But in a general ledger, you write +transaction two-lines: the source account and target account. @emph{There must always be a debit from at least one account for every credit made to another account}. This is what is meant by ``double-entry'' accounting: the ledger must always balance to zero, @@ -79,23 +114,24 @@ pay it from your checking account. In the general ledger you need to say where the money came from, in addition to where it's going to. The entry might look like this: -@example +@smallexample 9/29 BAL Pacific Bell $-200.00 $-200.00 Equity:Opening Balances $200.00 9/29 BAL Checking $100.00 $100.00 Equity:Opening Balances $-100.00 9/29 100 Pacific Bell $23.00 $223.00 Checking $-23.00 $77.00 -@end example - -The first line shows a credit (or payment) to Pacific Bell for $23.00. -Because there is no ``balance'' in a general ledger---it's always zero---we write in the total balance of all payments to ``Pacific Bell'', -which now is $223.00 (previously the balance was $200.00). This is -done by looking at the last entry for ``Pacific Bell'' in the ledger, -adding $23.00 to that amount, and writing the total in the balance -column. And the money came from ``Checking''---a debit (or withdrawal) -of $23.00---which leaves the ending balance in ``Checking'' at $77.00. -This is a very manual procedure; but that's where computers come in... +@end smallexample + +The first line shows a payment to Pacific Bell for $23.00. Because +there is no ``balance'' in a general ledger---it's always zero---we +write in the total balance of all payments to ``Pacific Bell'', which +now is $223.00 (previously the balance was $200.00). This is done by +looking at the last entry for ``Pacific Bell'' in the ledger, adding +$23.00 to that amount, and writing the total in the balance column. +And the money came from ``Checking''---a withdrawal of $23.00---which +leaves the ending balance in ``Checking'' at $77.00. This is a very +manual procedure; but that's where computers come in... The transaction must balance to $0: $23 went to Pacific Bell, $23 came from Checking. There is nothing left over to be accounted for, since @@ -105,20 +141,20 @@ existence; it is always a transaction from one account to another. Keeping a general ledger is the same as keeping two separate ledgers: One for Pacific Bell and one for Checking. In that case, each time a -credit is written into one, you write a corresponding debit into the -other. This makes it easier to write in a ``running balance'', since -you don't have to look back at the last time the account was +payment is written into one, you write a corresponding withdrawal into +the other. This makes it easier to write in a ``running balance'', +since you don't have to look back at the last time the account was referenced---but it also means having a lot of ledger books, if you deal with multiple accounts. Enter the beauty of computerized accounting. The purpose of the Ledger program is to make general ledger accounting simple, by keeping track of the balances for you. Your only job is to enter the -credit/debit transactions. If a transaction does not balance, Ledger -will display an error and indicate which transaction is wrong.@footnote{In some special cases, it will automatically balance the entry -for you.} +transactions. If a transaction does not balance, Ledger displays an +error and indicates the incorrect transaction.@footnote{In some +special cases, it automatically balances this entry for you.} -In summary, there are two aspects of Ledger use: Updating the ledger +In summary, there are two aspects of Ledger use: updating the ledger data file, and using the Ledger tool to view the summarized result of your entries. @@ -126,25 +162,22 @@ And just for the sake of example---as a starting point for those who want to dive in head-first---here are the ledger entries from above, formatting as the ledger program wishes to see them: -@example -; Set the year for subsequent entries to 2004 -Y 2004 - -9/29 Pacific Bell +@smallexample +2004/09/29 Pacific Bell Payable:Pacific Bell $-200.00 Equity:Opening Balances -9/29 Checking +2004/09/29 Checking Accounts:Checking $100.00 Equity:Opening Balances -9/29 Pacific Bell +2004/09/29 Pacific Bell Payable:Pacific Bell $23.00 Accounts:Checking -@end example +@end smallexample The account balances and registers in this file, if saved as -@samp{ledger.dat}, could be reported using: +@file{ledger.dat}, could be reported using: @example $ ledger -f ledger.dat balance @@ -153,31 +186,2361 @@ $ ledger -f ledger.dat register bell @end example @menu -* Building the program:: +* Building the program:: +* Getting help:: @end menu -@node Building the program, , Introduction, Introduction +@node Building the program, Getting help, Introduction, Introduction @section Building the program Ledger is written in ANSI C++, and should compile on any platform. It -depends only on the GNU multiprecision integer library (libgmp), and -the Perl regular expression library (libpcre). It was developed using -GNU make and gcc 3.3. +depends on the GNU multiprecision integer library (libgmp), and the +Perl regular expression library (libpcre). It was developed using GNU +make and gcc 3.3, on a PowerBook running OS/X. To build and install once you have these libraries on your system, enter these commands: @example -make -cp ledger /usr/local/bin +./configure && make install +@end example + +@node Getting help, , Building the program, Introduction +@section Getting help + +If you need help on how to use Ledger, or run into problems, you can +just the Ledger mailing list at the following Web address: + +@example +https://lists.sourceforge.net/lists/listinfo/ledger-discuss +@end example + +You can also find help at the @samp{#ledger} channel on the IRC server +@samp{irc.freenode.net}. + +@node Running Ledger, Keeping a ledger, Introduction, Top +@chapter Running Ledger + +Ledger has a very simple command-line interface, named---enticing +enough---@command{ledger}. It supports a few reporting commands, and +a large number of options for refining the output from those commands. +The basic syntax of any ledger command is: + +@example +ledger [OPTIONS...] COMMAND [ARGS...] +@end example + +Command options must always precede the command word. After the +command word there may appear any number of arguments. For most +commands, these arguments are regular expressions that cause the +output to relate only to transactions matching those regular +expressions. For the @command{entry} command, the arguments have a +special meaning, described below. + +The regular expressions arguments always match the account name that a +transaction refers to. To match on the payee of the entry instead, +precede the regular expression with @samp{--}. For example, the +following balance command reports account totals for rent, food and +movies, but only those whose payee matches Freddie: + +@example +ledger bal rent food movies -- freddie +@end example + +There are many, many command options available with the +@command{ledger} command, and it takes a while to master them. +However, none of them are required to use the basic reporting +commands. + +@menu +* Usage overview:: +* Commands:: +* Options:: +* Format strings:: +* Value expressions:: +* Period expressions:: +* File format:: +* Some typical queries:: +* Budgeting and forecasting:: +@end menu + +@node Usage overview, Commands, Running Ledger, Running Ledger +@section Usage overview + +Before getting into the details of how to run Ledger, it will be +easier to introduce the features in the context of their typical +usage. To that end, this section presents a series of recipes, +gradually introducing all of the command-line features of Ledger. + +For the purpose of these examples, assume the environment variable +@var{LEDGER} is set to the file @file{sample.dat} (which is included +in the distribution), and that the contents of that file are: + +@smallexample += /^Expenses:Books/ + (Liabilities:Taxes) -0.10 + +~ Monthly + Assets:Bank:Checking $500.00 + Income:Salary + +2004/05/01 * Checking balance + Assets:Bank:Checking $1,000.00 + Equity:Opening Balances + +2004/05/01 * Investment balance + Assets:Brokerage 50 AAPL @ $30.00 + Equity:Opening Balances + +2004/05/14 * Pay day + Assets:Bank:Checking $500.00 + Income:Salary + +2004/05/27 Book Store + Expenses:Books $20.00 + Liabilities:MasterCard + +2004/05/27 (100) Credit card company + Liabilities:MasterCard $20.00 + Assets:Bank:Checking +@end smallexample + +This sample file demonstrates a basic principle of accounting which it +is recommended you follow: Keep all of your accounts under five parent +Assets, Liabilities, Income, Expenses and Equity. It is important to +do so in order to make sense out of the following examples. + +@subsection Checking balances + +Ledger has seven basic commands, but by far the most often used are +@command{balance} and @command{register}. To see a summary balance of +all accounts, use: + +@example +ledger bal +@end example + +@command{bal} is a short-hand for @command{balance}. This command +prints out the summary totals of the five parent accounts used in +@file{sample.dat}: + +@smallexample + $1,480.00 + 50 AAPL Assets + $-2,500.00 Equity + $20.00 Expenses + $-500.00 Income + $-2.00 Liabilities +-------------------- + $-1,502.00 + 50 AAPL +@end smallexample + +None of the child accounts are shown, just the parent account totals. +We can see that in @samp{Assets} there is $1,480.00, and 50 shares of +Apple stock. There is also a negative grand total. Usually the grand +total is zero, which means that all accounts balance@footnote{It is +impossible for accounts not to balance in ledger; it reports an error +if a transaction does not balance}. In this case, since the 50 shares +of Apple stock cost $1,500.00 dollars, then these two amounts balance +each other in the grand total. The extra $2.00 comes from a virtual +transaction being added by the automatic entry at the top of the file. +The entry is virtual because the account name was surrounded by +parentheses in an automatic entry. Automatic entries will be +discussed later, but first let's remove the virtual transaction from +the balance report by using the @option{--real} option: + +@example +ledger --real bal +@end example + +Now the report is: + +@smallexample + $1,480.00 + 50 AAPL Assets + $-2,500.00 Equity + $20.00 Expenses + $-500.00 Income +-------------------- + $-1,500.00 + 50 AAPL +@end smallexample + +Since the liability was a virtual transaction, it has dropped from the +report and we see that final total is balanced. + +But we only know that it balances because @file{sample.dat} is quite +simple, and we happen to know that the 50 shares of Apple stock cost +$1,500.00. We can verify that things really balance by reporting the +Apple shares in terms of their cost, instead of their quantity. To do +this requires the @option{--basis}, or @option{-B}, option: + +@example +ledger --real -B bal @end example -Note that when building GNUmp, make sure to pass the @samp{--enable-cxx} -flag to configure, otherwise it will not build @strong{libgmpxx.a}. And in -case it is not already on your system, @strong{xmlparse.h} is part of the -libxmltok package, and not expat. +This command reports: + +@smallexample + $2,980.00 Assets + $-2,500.00 Equity + $20.00 Expenses + $-500.00 Income +@end smallexample + +With the basis cost option, the grand total has disappeared, as it is +now zero. The confirms that the cost of everything balances to zero, +@emph{which must always be true}. Reporting the real basis cost +should never yield a remainder@footnote{If it ever does, then +generated transactions are involved, which can be removed using +@option{--actual}}. + +@subsubsection Sub-account balances + +The totals reported by the balance command are only the topmost parent +accounts. To see the totals of all child accounts as well, use the +@option{-s} option: + +@example +ledger --real -B -s bal +@end example + +This reports: + +@smallexample + $2,980.00 Assets + $1,480.00 Bank:Checking + $1,500.00 Brokerage + $-2,500.00 Equity:Opening Balances + $20.00 Expenses:Books + $-500.00 Income:Salary +@end smallexample + +This shows that the @samp{Assets} total is made up from two child +account, but that the total for each of the other accounts comes from +one child account. + +Sometimes you may have a lot of children, nested very deeply, but only +want to report the first two levels. This can be done with a display +predicate, using a value expression. In the value expression, +@code{T} represents the reported total, and @code{l} is the display +level for the account: + +@example +ledger --real -B -d "T&l<=2" bal +@end example + +This reports: + +@smallexample + $2,980.00 Assets + $1,480.00 Bank + $1,500.00 Brokerage + $-2,500.00 Equity:Opening Balances + $20.00 Expenses:Books + $-500.00 Income:Salary +@end smallexample + +Instead of reporting @samp{Bank:Checking} as a child of @samp{Assets}, +it report only @samp{Bank}, since that account is a nesting level of +2, while @samp{Checking} is at level 3. + +To review the display predicate used---@code{T&l<=2}---this rather +terse expression means: Display an account only if it has a non-zero +total (@code{T}), and its nesting level is less than or equal to 2 +(@code{l<=2}). + +@subsubsection Specific account balances + +While reporting the totals for all accounts can be useful, most often +you will want to check the balance of a specific account or accounts. +To do this, put one or more account names after the balance command. +Since these names are really regular expressions, you can use partial +names if you wish: + +@example +ledger bal checking +@end example + +Reports: + +@smallexample + $1,480.00 Assets:Bank:Checking +@end smallexample + +Any number of names may be used: + +@example +ledger bal checking broker liab +@end example + +Reports: + +@smallexample + $1,480.00 Assets:Bank:Checking + 50 AAPL Assets:Brokerage + $-2.00 Liabilities +@end smallexample + +In this case no grand total is reported, because you are asking for +specific account balances. + +For those comfortable with regular expressions, any Perl regexp is +allowed: + +@example +ledger bal ^assets.*checking ^liab +@end example + +Reports: + +@smallexample + $1,480.00 Assets:Bank:Checking + $-2.00 Liabilities:Taxes +@end smallexample + +@subsection The register report + +While the @command{balance} command can be very handy for checking +account totals, by far the most powerful of Ledger's reporting tools +is the @command{register} command. In fact, internally both commands +use the same logic, but report the results differently: +@command{balance} shows the summary totals, while @command{register} +reports each transaction and how it contributes to that total. + +Paradoxically, the most basic form of @command{register} is almost +never used, since it displays every transaction: + +@example +ledger reg +@end example + +@command{reg} is a short-hand for @command{register}. This command +reports: + +@smallexample +2004/05/01 Checking balance Assets:Bank:Checking $1,000.00 $1,000.00 + Equity:Opening Balan.. $-1,000.00 0 +2004/05/01 Investment balance Assets:Brokerage 50 AAPL 50 AAPL + Equity:Opening Balan.. $-1,500.00 $-1,500.00 + 50 AAPL +2004/05/14 Pay day Assets:Bank:Checking $500.00 $-1,000.00 + 50 AAPL + Income:Salary $-500.00 $-1,500.00 + 50 AAPL +2004/05/27 Book Store Expenses:Books $20.00 $-1,480.00 + 50 AAPL + Liabilities:MasterCard $-20.00 $-1,500.00 + 50 AAPL + (Liabilities:Taxes) $-2.00 $-1,502.00 + 50 AAPL +2004/05/27 Credit card company Liabilities:MasterCard $20.00 $-1,482.00 + 50 AAPL + Assets:Bank:Checking $-20.00 $-1,502.00 + 50 AAPL +@end smallexample + +This rather verbose output shows every account transaction in +@file{sample.dat}, and how it affects the running total. The final +total is identical to what we saw with the plain @command{balance} +command. To see how things really balance, we can use @samp{--real +-B}, just as we did with @command{balance}: + +@example +ledger --real -B reg +@end example + +Reports: + +@smallexample +2004/05/01 Checking balance Assets:Bank:Checking $1,000.00 $1,000.00 + Equity:Opening Balan.. $-1,000.00 0 +2004/05/01 Investment balance Assets:Brokerage $1,500.00 $1,500.00 + Equity:Opening Balan.. $-1,500.00 0 +2004/05/14 Pay day Assets:Bank:Checking $500.00 $500.00 + Income:Salary $-500.00 0 +2004/05/27 Book Store Expenses:Books $20.00 $20.00 + Liabilities:MasterCard $-20.00 0 +2004/05/27 Credit card company Liabilities:MasterCard $20.00 $20.00 + Assets:Bank:Checking $-20.00 0 +@end smallexample + +Here we see that everything balances to zero in the end, as it must. + +@subsubsection Specific register queries + +The most common use of the register command is to summarize +transactions based on the account(s) they affect. Using +@file{sample.dat} as as example, we could look at all book purchases +using: + +@example +ledger reg books +@end example + +Reports: + +@smallexample +2004/05/29 Book Store Expenses:Books $20.00 $20.00 +@end smallexample + +If a double-dash (@samp{--}) occurs in the list of regular +expressions, any following arguments are matched against payee names, +instead of account names: + +@example +ledger reg ^liab -- credit +@end example + +Reports: + +@smallexample +2004/05/29 Credit card company Liabilities:MasterCard $20.00 $20.00 +@end smallexample + +There are many reporting options for tailoring which transactions are +found, and also how to summarize the various amounts and totals that +result. These are plumbed in greater depth below. + +@subsection Selecting transactions + +Although the easiest way to use the register is to report all the +transactions affecting a set of accounts, it can often result in more +information than you want. To cope with an ever-growing amount of +data, there are several options which can help you pinpoint your +report to exactly the transactions that interest you most. This is +called the ``calculation'' phase of Ledger. All of its related +options are documented under @option{--help-calc}. + +@subsubsection By date + +@c -c, --current show only current and past entries (not future) + +@option{--current}(@option{-c}) displays entries occurring on or +before the current date. Any entry recorded for a future date will be +ignored, as if it had not been seen. This is useful if you happen to +pre-record entries, but still wish to view your balances in terms of +what is available today. + +@c -b, --begin DATE set report begin date +@c -e, --end DATE set report end date + +@option{--begin DATE} (@option{-b DATE}) limits the report to only +those entries occurring on or after @var{DATE}. The running total in +the register will start at zero with the first transaction, even if +there are earlier entries. + +To limit the display only, but still add earlier transactions to the +running total, use the display expression @samp{-d 'd>=[DATE]'}): + +@example +ledger --basis -b may -d 'd>=[5/14]' reg ^assets +@end example + +Reports: + +@smallexample +2004/05/14 Pay day Assets:Bank:Checking $500.00 $3,000.00 +2004/05/27 Credit card company Assets:Bank:Checking $-20.00 $2,980.00 +@end smallexample + +In this example, the displayed transactions start from @samp{5/14}, +but the calculated total starts from the beginning of @samp{may}. + +@option{--end DATE} (@option{-e DATE}) states when reporting should +end, both calculation and display. This ending date is not inclusive, +which means you must always use a date that is at least one day after +the last transaction you want to see. + +The @var{DATE} argument to the @option{-b} and @option{-e} options can +be rather flexible. Assuming the current date to be November 15, +2004, then all of the following are equivalent: + +@example +ledger -b oct bal +ledger -b "this oct" bal +ledger -b 2004/10 bal +ledger -b 10 bal +ledger -b last bal +ledger -b "last month" bal +@end example + +@c -p, --period STR report using the given period +@c --period-sort EXPR sort each report period's entries by EXPR + +To constrain the report to a specific time period, use +@option{--period} (@option{-p}). A time period may have both a +beginning and an end, or neither, as well as a specified interval. +Here are a few examples: + +@example +ledger -p 2004 bal +ledger -p august bal +ledger -p "from aug to oct" bal +ledger -p "daily from 8/1 to 8/15" bal +ledger -p "weekly since august" bal +ledger -p "monthly from feb to oct" bal +ledger -p "quarterly in 2004" bal +ledger -p yearly bal +@end example + +See @ref{Period expressions} for more on syntax. Also, all of the +options @option{-b}, @option{-e} and @option{-p} may be used together, +but whatever information occurs last takes priority. An example of +such usage (in a script, perhaps) would be: + +@example +ledger -b 2004 -e 2005 -p monthly reg ^expenses +@end example + +This command is identical to: + +@example +ledger -p "monthly in 2004" reg ^expenses +@end example + +The transactions within a period may be sorted using +@option{--period-sort}, which takes a value expression. This is +similar to the @option{--sort} option, except that it sorts within +each period entry, rather than sorting all transactions in the report. +See the documentation on @option{--sort} below for more details. + +@subsubsection By status + +By default, all regular transactions are included in each report. To +limit the report to certain kinds of transactions, use one or more of +the following options: + +@table @option +@item -C, --cleared +Consider only cleared transactions. +@item -U, --uncleared +Consider only uncleared transactions. +@item -R, --real +Consider only real (non-virtual) transactions. +@item -L, --actual +Consider only actual (non-automated) transactions. +@end table + +Cleared transactions are indicated by an asterix placed just before +the payee name in a transaction. The meaning of this flag is up to +the user, but typically it means that an entry has been seen on a +financial statement. Uncleared transactions are things like uncashed +checks, credit charges that haven't appeared on a statement yet, etc. + +Real transactions are all non-virtual transactions, where the account +name is not surrounded by parentheses or square brackets. Virtual +transactions are useful for showing a transfer of money that never +really happened, like money set aside for savings without actually +transferring it from the parent account. + +Actual transactions are those not generated, either as part of an +automated entry, or a budget or forecast report. A useful of when you +might like to filter out generated transactions is with a budget: + +@example +ledger --budget --actual reg ^expenses +@end example + +This command outputs all transactions affecting a budgeted account, +but without subtracting the budget amount (because the generated +transactions are suppressed with @option{--actual}). The report shows +how much you actually spent on budgeted items. + +@subsubsection By relationship + +@c -r, --related calculate report using related transactions + +Normally, a register report includes only the transactions that match +the regular expressions specified after the command word. For +example, to report all expenses: + +@example +ledger reg ^expenses +@end example + +This reports: + +@smallexample +2004/05/29 Book Store Expenses:Books $20.00 $20.00 +@end smallexample + +Using @option{--related} (@option{-r}) reports the transactions that +did not match your query, but only in entries that otherwise would +have matched. This has the effect of indicating where money came +from, or when to: + +@example +ledger -r reg ^expenses +@end example + +Reports: + +@smallexample +2004/05/29 Book Store Liabilities:MasterCard $20.00 $20.00 +@end smallexample + +@subsubsection By budget + +@c --budget generate budget entries based on FILE + +There is more information about budgeting and forecasting in +@ref{Budgeting and forecasting}. Basically, if you have any period +entries in your ledger file, you can use these options. A period +entry looks like: + +@example +~ Monthly + Assets:Bank:Checking $500.00 + Income:Salary +@end example + +The difference from a regular entry is that the first line begins with +a tilde (~), and instead of a payee there's a period expression +(@ref{Period expressions}). Otherwise, a period entry is in every +other way the same as a regular entry. + +With such an entry in your ledger file, the @option{--budget} option +will report only transactions that match a budgeted account. Using +@file{sample.dat} from above: + +@example +ledger --budget reg ^income +@end example + +Reports: + +@smallexample +2004/05/01 Budget entry Income:Salary $500.00 $500.00 +2004/05/14 Pay day Income:Salary $-500.00 0 +@end smallexample + +The final total is zero, indicating that the budget matched exactly +for the reported period. Budgeting is most often helpful with period +reporting; for example, to show monthly budget results use +@option{--budget -p monthly}. + +@c --add-budget show all transactions plus the budget +@c --unbudgeted show only unbudgeted transactions + +The @option{--add-budget} option reports all matching transactions in +addition to budget transactions; while @option{--unbudgeted} shows +only those that don't match a budgeted account. To summarize: + +@table @option +@item --budget +Show transactions matching budgeted accounts. +@item --unbudgeted +Show transactions matching unbudgeted accounts. +@item --add-budget +Show both budgeted and unbudgeted transactions together (i.e., add the +generated budget transactions to the regular report). +@end table + +@c --forecast EXPR generate forecast entries while EXPR is true + +A report with the @option{--forecast} option will add budgeted +transactions while the specified value expression is true. For +example: + +@example +ledger --forecast 'd<[2005] reg ^income +@end example + +Reports: + +@smallexample +2004/05/14 Pay day Income:Salary $-500.00 $-500.00 +2004/12/01 Forecast entry Income:Salary $-500.00 $-1,000.00 +2005/01/01 Forecast entry Income:Salary $-500.00 $-1,500.00 +@end smallexample + +The date this report was made was November 5, 2004; the reason the +first forecast entry is in december is that forecast entries are only +added for the future, and they only stop after the value expression +has matched at least once, which is why the January entry appears. A +forecast report can be very useful for determining when money will run +out in an account, or for projecting future cash flow: + +@example +ledger --forecast 'd<[2008]' -p yearly reg ^inc ^exp +@end example + +This reports balances projected income against projected expenses, +showing the resulting total in yearly intervals until 2008. For the +case of @file{sample.dat}, which has no budgeted expenses, the result +of the above command (in November 2004) is: + +@smallexample +2004/01/01 - 2004/12/31 Income:Salary $-1,000.00 $-1,000.00 + Expenses:Books $20.00 $-980.00 +2005/01/01 - 2005/12/31 Income:Salary $-6,000.00 $-6,980.00 +2006/01/01 - 2006/12/31 Income:Salary $-6,000.00 $-12,980.00 +2007/01/01 - 2007/12/31 Income:Salary $-6,000.00 $-18,980.00 +2008/01/01 - 2008/01/01 Income:Salary $-500.00 $-19,480.00 +@end smallexample + +@subsubsection By value expression + +@c -l, --limit EXPR calculate only transactions matching EXPR + +Value expressions can be quite complex, and are treated more fully in +@ref{Value expressions}. They can be used for limiting a report with +@option{--limit} (@option{-l}). The following command report income +since august, but expenses since october: + +@example +ledger -l '(/income/&d>=[aug])|(/expenses/&d>=[oct])' reg +@end example + +The basic form of this value expression is @samp{(A&B)|(A&B)}. The +@samp{A} in each part matches against an account name with +@samp{/name/}, while each @samp{B} part compares the date of the +transaction (@samp{d}) with a specified month. The resulting report +will contain only transactions which match the value expression. + +@c -t, --amount EXPR use EXPR to calculate the displayed amount +@c -T, --total EXPR use EXPR to calculate the displayed total + +Another use of value expressions is to calculate the amount reported +for each line of a register report, or for computing the subtotal of +each account shown in a balance report. This example divides each +transaction amount by two: + +@example +ledger -t 'a/2' reg ^exp +@end example + +The @option{-t} option doesn't affect the running total, only how the +transaction amount is displayed. To change the running total, use +@option{-T}. In that case, you will likely want to use the total +(@samp{O}) instead of the amount (@samp{a}): + +@example +ledger -T 'O/2' reg ^exp +@end example + +@subsection Massaging register output + +Even after filtering down your data to just the transactions you're +interested in, the default reporting method of one transaction per +line is often still too much. To combat this complexity, it is +possible to ask Ledger to report the details to you in many different +forms, summarized in various ways. This is the ``display'' phase of +Ledger, and is documented under @option{--help-disp}. + +@subsubsection Summarizing + +@c -n, --collapse register: collapse entries with multiple transactions + +When multiple transactions relate to a single entry, they are reported +as part of that entry. For example, in the case of @file{sample.dat}: + +@example +ledger reg -- book +@end example + +Reports: + +@smallexample +2004/05/29 Book Store Expenses:Books $20.00 $20.00 + Liabilities:MasterCard $-20.00 0 + (Liabilities:Taxes) $-2.00 $-2.00 +@end smallexample + +All three transactions are part of one entry, and as such the entry +details are printed only once. To report every entry on a single +line, use @option{-n} to collapse entries with multiple transactions: + +@example +ledger -n reg -- book +@end example + +Reports: + +@smallexample +2004/05/29 Book Store <Total> $-2.00 $-2.00 +@end smallexample + +In the balance report, @option{-n} causes the grand total not to be +displayed at the bottom of the report. + +@c -s, --subtotal balance: show sub-accounts; other: show subtotals + +If an account occurs more than once in a report, it is possible to +combine them all and report the total per-account, using @option{-s}. +For example, this command: + +@example +ledger -B reg ^assets +@end example + +Reports: + +@smallexample +2004/05/01 Checking balance Assets:Bank:Checking $1,000.00 $1,000.00 +2004/05/01 Investment balance Assets:Brokerage $1,500.00 $2,500.00 +2004/05/14 Pay day Assets:Bank:Checking $500.00 $3,000.00 +2004/05/27 Credit card company Assets:Bank:Checking $-20.00 $2,980.00 +@end smallexample + +But if the @option{-s} option is added, the result becomes: + +@smallexample +2004/05/01 - 2004/05/29 Assets:Bank:Checking $1,480.00 $1,480.00 + Assets:Brokerage $1,500.00 $2,980.00 +@end smallexample + +When account subtotaling is used, only one entry is printed, and the +date and name reflect the range of the combined transactions. + +@c -P, --by-payee show summarized totals by payee + +With @option{-P}, transactions relating to the same payee are +combined. In this case, the date of the combined entry is that of the +latest transaction. + +@c -x, --comm-as-payee set commodity name as the payee, for reporting + +@option{-x} changes the payee name for each transaction to be the same +as the commodity it uses. This can be especially useful combined with +other options, like @option{-P}. For example: + +@example +ledger -Px reg ^assets +@end example + +Reports: + +@smallexample +2004/05/29 $ Assets:Bank:Checking $1,480.00 $1,480.00 +2004/05/01 AAPL Assets:Brokerage 50 AAPL $1,480.00 + 50 AAPL +@end smallexample + +This reports shows the subtotal for each commodity held, and where it +is located. To see the basis cost, or initial investment, add +@option{-B}. Applied to the example above: + +@smallexample +2004/05/29 $ Assets:Bank:Checking $1,480.00 $1,480.00 +2004/05/01 AAPL Assets:Brokerage $1,500.00 $2,980.00 +@end smallexample + +@c -E, --empty balance: show accounts with zero balance + +The only other options which affect summarized totals is @option{-E}, +which works only in the balance report. In this case, it shows +matching accounts with a zero a balance, which are ordinarily +excluded. This can be useful to see all the accounts involved in a +report, even if some have no total. + +@subsubsection Quick periods + +Although the @option{-p} option (also @option{--period}) is much more +versatile, there are other options to make the most common period +reports easier: + +@table @option +@item -W, --weekly +Show weekly sub-totals. Same as @samp{-p weekly}. +@item -M, --monthly +Show monthly sub-totals. Same as @samp{-p monthly}. +@item -Y, --yearly +Show yearly sub-totals. Same as @samp{-p yearly}. +@end table + +@c --dow show a days-of-the-week report + +There is one kind of period report cannot be done with @option{-p}. +This is the @option{--dow}, or ``days of the week'' report, which +shows summarized totals for each day of the week. The following +examples shows a ``day of the week'' report of income and expenses: + +@example +ledger --dow reg ^inc ^exp +@end example + +Reports: + +@smallexample +2004/05/27 Thursdays Expenses:Books $20.00 $20.00 +2004/05/14 Fridays Income:Salary $-500.00 $-480.00 +@end smallexample + +@subsubsection Ordering and width + +@c -S, --sort EXPR sort report according to the value expression EXPR + +The transactions displayed in a report are shown in the same order as +they appear in the ledger file. To change the order and sort a +report, use the @option{--sort} option. @option{--sort} takes a value +expression to determine the value to sort against, making it possible +to sort according to complex criteria. Here are some simple and +useful examples: + +@example +ledger --sort d reg ^exp # sort by date +ledger --sort t reg ^exp # sort by amount total +ledger --sort -t reg ^exp # reverse sort by amount total +ledger --sort Ut reg ^exp # sort by abs amount total +@end example + +For the balance report, you will want to use @samp{T} instead of +@samp{t}: + +@example +ledger --sort T reg ^exp # sort by amount total +ledger --sort -T reg ^exp # reverse sort by amount total +ledger --sort UT reg ^exp # sort by abs amount total +@end example + +The @option{--sort} options sorts all transactions in a report. If +periods are used (such as @option{--monthly}), this can get somewhat +confusing. In that case, you'll probably want to sort within periods +using @option{--period-sort} instead of @option{--sort}. + +@c -w, --wide for the default register report, use 132 columns + +And if the register seems too cramped, and you have a lot of screen +real estate, you can use @option{-w} to format the report within 132 +acolumns, instead of 80. You are more likely then to see full payee +and account names, as well as properly formatted totals when +long-named commodities are used. + +@subsubsection Averages and percentages + +@c -A, --average report average transaction amount + +To see the running total changed to a running average, use +@option{-A}. The final transaction's total will be the overall +average of all displayed transactions. The works in conjunction with +period reporting, so that you can see your monthly average expenses +with: + +@example +ledger -AM reg ^expenses:food +ledger -AMn reg ^expenses +@end example + +This works in the balance report too: + +@example +ledger -AM bal ^expenses:food +ledger -AMs bal ^expenses +@end example + +@c -D, --deviation report deviation from the average + +The @option{-D} option changes the running average into a deviation +from the running average. This only makes sense in the register +report, however. + +@example +ledger -DM reg ^expenses:food +@end example + +@c -%, --percentage report balance totals as a percentile of the parent + +In the balance report only, @option{-%} changes the reported totals +into a percentage of the parent account. This kind of report is +confusing if negative amounts are involved, and doesn't work at all if +multiple commodities occur in an account's history. It has a somewhat +limited usefulness, therefore, but in certain cases it can be handy, +such as reviewing overall expenses: + +@example +ledger -%s -S T bal ^expenses +@end example + +@subsubsection Reporting total data + +@c --totals in the "xml" report, include running total + +Normally in the @command{xml} report, only transaction amounts are +printed. To include the running total under a @samp{<total>} tag, use +@option{--totals}. This does not affect any other report. + +@c -j, --amount-data print only raw amount data (useful for scripting) +@c -J, --total-data print only raw total data + +In the register report only, the output can be changed with +@option{-j} to show only the date and the amount---without +commodities. This only makes sense if a single commodity appears in +the report, but can be quite useful for scripting, or passing the data +to Gnuplot. To show only the date and running total, use @option{-J}. + +@subsubsection Display by value expression + +@c -d, --display EXPR display only transactions matching EXPR + +With @option{-d} you can decide which transactions (or accounts in the +balance report) are displayed, according to a value expression. The +computed total is not affected, only the display. This can be very +useful for shortening a report without changing the running total: + +@example +ledger -d 'd>=[last month]' reg checking +@end example + +This command shows the checking account's register, beginning from +last month, but with the running total reflecting the entire history +of the account. + +@subsubsection Change report format + +@c -y, --date-format STR use STR as the date format (default: %Y/%m/%d) + +When dates are printed in any report, the default format is +@samp{%Y/%m/%d}, which yields dates of the form @samp{YYYY/mm/dd}. +This can be changed with @option{-y}, whose argument is a +@code{strftime} string---see your system's C library documentation for +the allowable codes. Mostly you will want to use @samp{%Y}, @samp{%m} +and @samp{%d}, in whatever combination is convenient for your locale. + +@c -F, --format STR use STR as the format; for each report type, use: +@c --balance-format --register-format --print-format +@c --plot-amount-format --plot-total-format --equity-format +@c --prices-format --wide-register-format + +To change the format of the entire reported line, use @option{-F}. It +supports quite a large number of options, which are all documented in +@ref{Format strings}. In addition, each specific kind of report +(except for @command{xml}) can be changed using one of the following +options: + +@table @option +@item --balance-format +@command{balance} report. Default: +@smallexample +%20T %2_%-a\n +@end smallexample + +@item --register-format +@command{register} report. Default: +@smallexample +%D %-.20P %-.22A %12.66t %12.80T\n%/%32|%-.22A %12.66t %12.80T\n +@end smallexample + +@item --print-format +@command{print} report. Default: +@smallexample +%D %-.35P %-.38A %22.108t %22.132T\n%/%48|%-.38A %22.108t %22.132T\n +@end smallexample + +@item --plot-amount-format +@command{register} report when @option{-j} (plot amount) is used. Default: +@smallexample +%D %(St)\n +@end smallexample + +@item --plot-total-format +@command{register} report when @option{-J} (plot total) is used. Default: +@smallexample +%D %(ST)\n +@end smallexample + +@item --equity-format +@command{equity} report. Default: +@smallexample +\n%D %X%C%P\n %-34A %12o%n\n%/ %-34A %12o%n\n +@end smallexample + +@item --prices-format +@command{prices} report. Default: +@smallexample +\n%D %X%C%P\n%/ %-34A %12t\n +@end smallexample + +@item --wide-register-format +@command{register} report when @option{-w} (wide) is used. Default: +@smallexample +%[%Y/%m/%d %H:%M:%S %Z] %-10A %12t %12T\n +@end smallexample +@end table + +@subsection Standard queries + +If your ledger file uses the standard top-level accounts: Assets, +Liabilities, Income, Expenses, Equity: then the following queries will +enable you to generate some typical accounting reports from your data. + +Your @emph{net worth} can be determined by balancing assets against +liabilities: + +@example +ledger bal ^assets ^liab +@end example + +By removing long-term investment and loan accounts, you can see your +current net liquidity (or liquid net worth): + +@example +ledger bal ^assets ^liab -retirement -brokerage -loan +@end example + +Balancing expenses against income yields your @emph{cash flow}, or net +profit/loss: + +@example +ledger bal ^exp ^inc +@end example + +In this case, if the number is positive it means you spent more than +you earned during the report period. + +@c ---------------------------------------------------------------------- + +The most often used command is the ``balance'' command: + +@example +export LEDGER=/home/johnw/doc/ledger.dat +ledger balance +@end example + +Here I've set my Ledger environment variable to point to where my +ledger file is hiding. Thereafter, I needn't specify it again. + +@subsection Reporting balance totals + +The balance command prints out the summarized balances of all my +top-level accounts, excluding sub-accounts. In order to see the +balances for a specific account, just specify a regular expression +after the balance command: + +@example +ledger balance expenses:food +@end example + +This will show all the money that's been spent on food, since the +beginning of the ledger. For food spending just this month +(September), use: + +@example +ledger -p sep balance expenses:food +@end example + +Or maybe you want to see all of your assets, in which case the -s +(show sub-accounts) option comes in handy: + +@example +ledger -s balance ^assets +@end example + +To exclude a particular account, use a regular expression with a +leading minus sign. The following will show all expenses, but without +food spending: + +@example +ledger balance expenses -food +@end example + +@subsection Reporting percentages + +There is no built-in way to report transaction amounts or account +balances in terms of percentages + +@node Commands, Options, Usage overview, Running Ledger +@section Commands + +@subsection balance + +The @command{balance} command reports the current balance of all +accounts. It accepts a list of optional regexps, which confine the +balance report to the matching accounts. If an account contains +multiple types of commodities, each commodity's total is reported +separately. + +@subsection register + +The @command{register} command displays all the transactions occurring +in a single account, line by line. The account regexp must be +specified as the only argument to this command. If any regexps occur +after the required account name, the register will contain only those +transactions that match. Very useful for hunting down a particular +transaction. + +The output from @command{register} is very close to what a typical +checkbook, or single-account ledger, would look like. It also shows a +running balance. The final running balance of any register should +always be the same as the current balance of that account. + +If you have Gnuplot installed, you may plot the amount or running +total of any register by using the script @file{report}, which is +included in the Ledger distribution. The only requirement is that you +add either @option{-j} or @option{-J} to your register command, in +order to plot either the amount or total column, respectively. + +@subsection print + +The @command{print} command prints out ledger entries just as they +appear in the original ledger. They will be properly formatted, and +output in the most economic form possible. The ``print'' command also +takes a list of optional regexps, which will cause only those +transactions which match in some way to be printed. + +The @command{print} command can be a handy way to clean up a ledger +file whose formatting has gotten out of hand. + +@subsection xml + +The @command{xml} command outputs results similar to what +@command{print} and @command{register} display, but as an XML form. +This data can then be read in and processed. Use the +@option{--totals} option to include the running total with each +transaction. + +@subsection equity + +The @command{equity} command prints out accounts balances as if they +were entries. This makes it easy to establish the starting balances +for an account, such as when @ref{Archiving previous years}. + +@subsection prices + +The @command{prices} command displays the price history for matching +commodities. The @option{-A} flag is useful with this report, to +display the running average price, or @option{-D} to show each price's +deviation from that average. + +@subsection entry + +The @command{entry} commands simplifies the creation of new entries. +It works on the principle that 80% of all transactions are variants of +earlier transactions. Here's how it works: + +Say you currently have this transaction in your ledger file: + +@smallexample +2004/03/15 * Viva Italiano + Expenses:Food $12.45 + Expenses:Tips $2.55 + Liabilities:MasterCard $-15.00 +@end smallexample + +Now it's @samp{2004/4/9}, and you've just eating at @samp{Viva +Italiano} again. The exact amounts are different, but the overall +form is the same. With the @command{entry} command you can type: + +@example +ledger entry 2004/4/9 viva food 11 tips 2.50 +@end example + +This produces the following output: + +@smallexample +2004/04/09 Viva Italiano + Expenses:Food $11.00 + Expenses:Tips $2.50 + Liabilities:MasterCard $-13.50 +@end smallexample + +It works by finding a past transaction matching the regular expression +@samp{viva}, and assuming that any accounts or amounts specified will +be similar to that earlier transaction. If Ledger does not succeed in +generating a new entry, an error is printed and the exit code is set +to @samp{1}. + +There is a shell script in the distribution's @file{scripts} directory +called @file{entry}, which simplifies the task of adding a new entry +to your ledger. It launches @command{vi} to confirm that the entry +looks appropriate. + +Here are a few more examples of the @command{entry} command, assuming +the above journal entry: + +@example +ledger entry 4/9 viva 11.50 +ledger entry 4/9 viva 11.50 checking # (from `checking') +ledger entry 4/9 viva food 11.50 tips 8 +ledger entry 4/9 viva food 11.50 tips 8 cash +ledger entry 4/9 viva food $11.50 tips $8 cash +ledger entry 4/9 viva dining "DM 11.50" +@end example + +@node Options, Format strings, Commands, Running Ledger +@section Options + +With all of the reports, command-line options are useful to modify the +output generated. These command-line options always occur before the +command word. This is done to distinguish options from exclusive +regular expressions, which also begin with a dash. The basic form for +most commands is: + +@example +ledger [OPTIONS] COMMAND [REGEXPS...] [-- [REGEXPS...]] +@end example + +The @var{OPTIONS} and @var{REGEXPS} expressions are both optional. +You could just use @samp{ledger balance}, without any options---which +prints a summary of all accounts. But for more specific reporting, or +to change the appearance of the output, options are needed. + +@menu +* Basic options:: +* Report filtering:: +* Output customization:: +* Commodity reporting:: +* Environment variables:: +@end menu + +@node Basic options, Report filtering, Options, Options +@subsection Basic options + +These are the most basic command options. Most likely, the user will +want to set them using @ref{Environment variables}, instead of using +actual command-line options: + +@option{--help} (@option{-h}) prints a summary of all the options, and +what they are used for. This can be a handy way to remember which +options do what. This help screen is also printed if ledger is run +without a command. + +@option{--version} (@option{-v}) prints the current version of ledger +and exits. This is useful for sending bug reports, to let the author +know which version of ledger you are using. + +@option{--file FILE} (@option{-f FILE}) reads FILE as a ledger file. +This command may be used multiple times. FILE may also be a list of +file names separated by colons. Typically, the environment variable +@env{LEDGER_FILE} is set, rather than using this command-line option. + +@option{--output FILE} (@option{-o FILE}) redirects output from any +command to @var{FILE}. By default, all output goes to standard +output. + +@option{--init-file FILE} (@option{-i FILE}) causes FILE to be read by +ledger before any other ledger file. This file may not contain any +transactions, but it may contain option settings. To specify options +in the init file, use the same syntax as the command-line. Here's an +example init file: + +@smallexample +--price-db ~/finance/.pricedb + +; ~/.ledgerrc ends here +@end smallexample + +Option settings on the command-line or in the environment always take +precedence over settings in the init file. + +@option{--cache FILE} identifies FILE as the default binary cache +file. That is, if the ledger files to be read are specified using the +environment variable @env{LEDGER_FILE}, then whenever a command is +finished a binary copy will be written to the specified cache, to +speed up the loading time of subsequent queries. This filename can +also be given using the environment variable @env{LEDGER_CACHE}, or by +putting the option into your init file. + +@option{--account NAME} (@option{-a NAME}) specifies the default +account which QIF file transactions are assumed to relate to. + +@node Report filtering, Output customization, Basic options, Options +@subsection Report filtering + +These options change which transactions affect the outcome of a +report, in ways other than just using regular expressions: + +@option{--current}(@option{-c}) displays only entries occurring on or +before the current date. + +@option{--begin DATE} (@option{-b DATE}) constrains the report to +entries on or after @var{DATE}. Only entries after that date will be +calculated, which means that the running total in the balance report +will always start at zero with the first matching entry. (Note: This +is different from using @option{--display} to constrain what is +displayed). + +@option{--end DATE} (@option{-e DATE}) constrains the report so that +entries on or after @var{DATE} are not considered. This ending date +is not inclusive, therefore always use a date that is later than the +last entry you want to see. + +@option{--period STR} (@option{-p STR}) sets the reporting period +to @var{STR}. This will subtotal all matching entries within each +period separately, making it easy to see weekly, monthly, quarterly, +etc., transaction totals. A period string can even specify the +beginning and end of the report range, using simple terms like ``last +june'' or ``next month''. For more using period expressions, see +@ref{Period expressions}. + +@option{--period-sort EXPR} sorts the transactions within each +reporting period using the value expression @var{EXPR}. This is most +often useful when reporting monthly expenses, in order to view the +highest expense categories at the top of each month: + +@example +ledger -M --period-sort -At reg ^Expenses +@end example + +@option{--cleared} (@option{-C}) displays only transactions whose entry +has been marked ``cleared'' (by placing an asterix to the right of the +date). + +@option{--uncleared} (@option{-U}) displays only transactions whose +entry has not been marked ``cleared'' (i.e., if there is no asterix to +the right of the date). + +@option{--real} (@option{-R}) displays only real transactions, not +virtual. (A virtual transaction is indicated by surrounding the +account name with parentheses or brackets; see the section on using +virtual transactions for more information). + +@option{--actual} (@option{-L}) displays only actual transactions, and +not those created due to automated transactions. + +@option{--related} (@option{-r}) displays transactions that are +related to whichever transactions would otherwise have matched the +filtering criteria. In the register report, this shows where money +went to, or the account it came from. In the balance report, it shows +all the accounts affected by entries having a related transaction. +For example, if a file had this entry: + +@smallexample +2004/03/20 Safeway + Expenses:Food $65.00 + Expenses:Cash $20.00 + Assets:Checking $-85.00 +@end smallexample + +And the register command was: + +@example +ledger -r register food +@end example + +The following would be output, showing the transactions related to the +transaction that matched: + +@smallexample +2004/03/20 Safeway Expenses:Cash $-20.00 $-20.00 + Assets:Checking $85.00 $65.00 +@end smallexample + +@option{--budget} is useful for displaying how close your transactions +meet your budget. @option{--add-budget} also shows unbudgeted +transactions, while @option{--unbudgeted} shows only those. +@option{--forecast} is a related option that projects your budget into +the future, showing how it will affect future balances. +@xref{Budgeting and forecasting}. + +@option{--limit EXPR} (@option{-l EXPR}) limits which transactions +take part in the calculations of a report. + +@option{--amount EXPR} (@option{-t EXPR}) changes the value expression +used to calculate the ``value'' column in the @command{register} +report, the amount used to calculate account totals in the +@command{balance} report, and the values printed in the +@command{equity} report. @xref{Value expressions}. + +@option{--total EXPR} (@option{-T EXPR}) sets the value expression +used for the ``totals'' column in the @command{register} and +@command{balance} reports. + +@node Output customization, Commodity reporting, Report filtering, Options +@subsection Output customization + +These options affect only the output, but not which transactions are +used to create it: + +@option{--collapse} (@option{-n}) causes entries in a +@command{register} report with multiple transactions to be collapsed +into a single, subtotaled entry. + +@option{--subtotal} (@option{-s}) causes all entries in a +@command{register} report to be collapsed into a single, subtotaled +entry. + +@option{--by-payee} (@option{-P}) reports subtotals by payee. + +@option{--comm-as-payee} (@option{-x}) changes the payee of every +transaction to be the commodity used in that transaction. This can be +useful when combined with other options, such as @option{-s}. + +@option{--empty} (@option{-E}) includes even empty accounts in the +@command{balance} report. + +@option{--weekly} (@option{-W}) reports transaction totals by the +week. The week begins on whichever day of the week begins the month +containing that transaction. To set a specific begin date, use a +period string, such as @samp{weekly from DATE}. @option{--monthly} +(@option{-M}) reports transaction totals by month; @option{--yearly} +(@option{-Y}) reports transaction totals by year. For more complex +period, using the @option{--period} option described above. + +@option{--dow} reports transactions totals for each day of the week. +This is an easy way to see if weekend spending is more than on +weekdays. + +@option{--sort EXPR} (@option{-S EXPR}) sorts a report by comparing +the values determined using the value expression @var{EXPR}. For +example, using @option{-S -UT} in the balance report will sort account +balances from greatest to least, using the absolute value of the +total. For more on how to use value expressions, see @ref{Value +expressions}. + +@option{--wide} (@option{-w}) causes the default @command{register} +report to assume 132 columns instead of 80. + +@option{--average} (@option{-A}) reports the average transaction +value. + +@option{--deviation} (@option{-D}) reports each transaction's +deviation from the average. It is only meaningful in the +@command{register} and @command{prices} reports. + +@option{--percentage} (@option{-%}) shows account subtotals in the +@command{balance} report as percentages of the parent account. + +@option{--totals} include running total information in the +@command{xml} report. + +@option{--amount-data} (@option{-j}) changes the @command{register} +report so that it output nothing but the date and the value column, +and the latter without commodities. This is only meaningful if the +report uses a single commodity. This data can then be fed to other +programs, which could plot the date, analyze it, etc. + +@option{--total-data} (@option{-J}) changes the @command{register} +report so that it output nothing but the date and totals column, +without commodities. + +@option{--display EXPR} (@option{-d EXPR}) limits which transactions +or accounts or actually displayed in a report. They might still be +calculated, and be part of the running total of a register report, for +example, but they will not be displayed. This is useful for seeing +last month's checking transactions, against a running balance which +includes all transaction values: + +@example +ledger -d "d>=[last month]" reg checking +@end example + +The output from this command is very different from the following, +whose running total includes only transactions from the last month +onward: + +@example +ledger -p "last month" reg checking +@end example + +Which is more useful depends on what you're looking to know: the total +amount for the reporting range (@option{-p}), or simply a display +restricted to the reporting range (using @option{-d}). + +@option{--date-format STR} (@option{-y STR}) changes the basic date +format used by reports. The default uses a date like 2004/08/01, +which represents the default date format of @samp{%Y/%m/%d}. To +change the way dates are printed in general, the easiest way is to put +@option{--date-format FORMAT} in the Ledger initialization file +@file{~/.ledgerrc} (or the file referred to by @env{LEDGER_INIT}). + +@option{--format STR} (@option{-F STR}) sets the reporting format for +whatever report ledger is about to make. @xref{Format strings}. +There are also specific format commands for each report type: + +@itemize +@item @option{--balance-format STR} +@item @option{--register-format STR} +@item @option{--print-format STR} +@item @option{--plot-amount-format STR} (-j @command{register}) +@item @option{--plot-total-format STR} (-J @command{register}) +@item @option{--equity-format STR} +@item @option{--prices-format STR} +@item @option{--wide-register-format STR} (-w @command{register}) +@end itemize + +@node Commodity reporting, Environment variables, Output customization, Options +@subsection Commodity reporting + +These options affect how commodity values are displayed: + +@option{--price-db FILE} (@option{-P FILE}) sets the file that is used +for recording downloaded commodity prices. It is always read on +startup, to determine historical prices. Other settings can be placed +in this file manually, to prevent downloading quotes for a specific, +for example. This is done by adding a line like the following: + +@example +; Don't download quotes for the dollar, or timelog values +N $ +N h +@end example -@node Keeping a ledger, Running Ledger, Introduction, Top +@option{--price-exp MINS} (@option{-L MINS}) sets the expected +freshness of price quotes, in minutes. That is, if the last known +quote for any commodity is older than this value---and if +@option{--download} is being used---then the Internet will be +consulted again for a newer price. Otherwise, the old price is still +considered to be fresh enough. + +@option{--download} (@option{-Q}) causes quotes to be automagically +downloaded, as needed, by running a script named @command{getquote} +and expecting that script to return a value understood by ledger. A +sample implementation of a @command{getquote} script, implemented in +Perl, is provided in the distribution. Downloaded quote price are +then appended to the price database, usually specified using the +environment variable @env{LEDGER_PRICE_DB}. + +There are several different ways that ledger can report the totals it +displays. The most flexible way to adjust them is by using value +expressions, and the @option{-t} and @option{-T} options. However, +there are also several ``default'' reports, which will satisfy most +users basic reporting needs: + +@table @code +@item -O, --quantity +Reports commodity totals (this is the default) + +@item -B, --basis +Reports the cost basis for all transactions. + +@item -V, --market +Reports the last known market value for all commodities. + +@item -g, --performance +Reports the net gain/loss for each transaction in a @command{register} +report. + +@item -G --gain +Reports the net gain/loss for all commodities in the report that have +a price history. +@end table + +@node Environment variables, , Commodity reporting, Options +@subsection Environment variables + +Every option to ledger may be set using an environment variable. If +an option has a long name such @option{--this-option}, setting the +environment variable @env{LEDGER_THIS_OPTION} will have the same +affect as specifying that option on the command-line. Options on the +command-line always take precedence over environment variable +settings, however. + +Note that you may also permanently specify option values by placing +option settings in the file @file{~/.ledgerrc}, for example: + +@example +--cache /tmp/.mycache +@end example + +@node Format strings, Value expressions, Options, Running Ledger +@section Format strings + +Format strings may be used to change the output format of reports. +They are specified by passing a formatting string to the +@option{--format} (@option{-F}) option. Within that string, +constructs are allowed which make it possible to display the various +parts of an account or transaction in custom ways. + +Within a format strings, a substitution is specified using a percent +character (@samp{%}). The basic format of all substitutions is: + +@example +%[-][MIN WIDTH][.MAX WIDTH]EXPR +@end example + +If the optional minus sign (@samp{-}) follows the percent character, +whatever is substituted will be left justified. The default is right +justified. If a minimum width is given next, the substituted text +will be at least that wide, perhaps wider. If a period and a maximum +width is given, the substituted text will never be wider than this, +and will be truncated to fit. Here are some examples: + +@example +%-P An entry's payee, left justified +%20P The same, right justified, at least 20 chars wide +%.20P The same, no more than 20 chars wide +%-.20P Left justified, maximum twenty chars wide +@end example + +The expression following the format constraints can be a single +letter, or an expression enclosed in parentheses or brackets. The +allowable expressions are: + +@table @code +@item % +Inserts a percent sign. + +@item t +Inserts the results of the value expression specified by @option{-t}. +If @option{-t} was not specified, the current report style's value +expression is used. + +@item T +Inserts the results of the value expression specified by @option{-T}. +If @option{-T} was not specified, the current report style's value +expression is used. + +@item | +Inserts a single space. This is useful if a width is specified, for +inserting a certain number of spaces. + +@item _ +Inserts a space for each level of an account's depth. That is, if an +account has two parents, this construct will insert two spaces. If a +minimum width is specified, that much space is inserted for each level +of depth. Thus @samp{%5_}, for an account with four parents, will +insert twenty spaces. + +@item (EXPR) +Inserts the amount resulting from the value expression given in +parentheses. To insert five times the total value of an account, for +example, one could say @samp{%12(5*O)}. Note: It's important to put +the five first in that expression, so that the commodity doesn't get +stripped from the total. + +@item [DATEFMT] +Inserts the result of formatting a transaction's date with a date +format string, exactly like those supported by @code{strftime}. For +example: @samp{%[%Y/%m/%d %H:%M:%S]}. + +@item D +By default, this is the same as @samp{%[%Y/%m%/d]}. The date format +used can be changed at any time with the @option{-y} flag, however. +Using @samp{%D} gives the user more control over the way dates are +output. + +@item X +If a transaction has been cleared, this inserts @samp{*} followed by a +space; otherwise nothing is inserted. + +@item C +Inserts the checking number for an entry, in parentheses, followed by +a space; if none was specified, nothing is inserted. + +@item P +Inserts the payee related to a transaction. + +@item a +Inserts the optimal short name for an account. This is normally used +in balance reports. It prints a parent account's name if that name +has not been printed yet, otherwise it just prints the account's name. + +@item A +Inserts the full name of an account. + +@item o +Inserts the ``optimized'' form of a transaction's amount. This is +used by the print report. In some cases, this inserts nothing; in +others, it inserts the transaction amount and its cost. It's use is +not recommend unless you are modifying the print report. + +@item n +Inserts the note associated with a transaction, preceded by two spaces +and a semi-colon, if it exists. Thus, no none becomes an empty +string, while the note @samp{foo} is substituted as @samp{ ; foo}. + +@item N +Inserts the note associated with a transaction, if one exists. + +@item / +The @samp{%/} construct is special. It separates a format string +between what is printed for the first transaction of an entry, and +what is printed for all subsequent transactions. If not used, the +same format string is used for all transactions. +@end table + +@node Value expressions, Period expressions, Format strings, Running Ledger +@section Value expressions + +Value expressions are an expression language used by Ledger to +calculate values used by the program for many different purposes: + +@enumerate +@item +The values displayed in reports +@item +For predicates (where truth is anything non-zero), to determine which +transactions are calculated (@option{-l}) or displayed (@option{-d}). +@item +For sorting criteria, to yield the sort key. +@item +In the matching criteria used by automated transactions. +@end enumerate + +Value expressions support most simple math and logic operators, in +addition to a set of one letter functions and variables. A function's +argument is whatever follows it. The following is a display predicate +that I use with the @command{balance} command: + +@example +ledger -d /^Liabilities/?T<0:UT>100 balance +@end example + +The effect is that account totals are displayed only if: 1) A +Liabilities account has a total less than zero; or 2) the absolute +value of the account's total exceeds 100 units of whatever commodity +contains. If it contains multiple commodities, only one of them must +exceed 100 units. + +Display predicates are also very handy with register reports, to +constrain which entries are printed. For example, the following +command shows only entries from the beginning of the current month, +while still calculating the running balance based on all entries: + +@example +ledger -d "d>[this month]" register checking +@end example + +This advantage to this command's complexity is that it prints the +running total in terms of all entries in the register. The following, +simpler command is similar, but totals only the displayed +transactions: + +@example +ledger -b "this month" register checking +@end example + +@subsection Variables + +Below are the one letter variables available in any value expression. +For the register and print commands, these variables relate to +individual transactions, and sometimes the account affected by a +transaction. For the balance command, these variables relate to +accounts---often with a subtle difference in meaning. The use of each +variable for both is specified. + +@table @code +@item t +This maps to whatever the user specified with @option{-t}. In a +register report, @option{-t} changes the value column; in a balance +report, it has no meaning by default. If @option{-t} was not +specified, the current report style's value expression is used. + +@item T +This maps to whatever the user specified with @option{-T}. In a +register report, @option{-T} changes the totals column; in a balance +report, this is the value given for each account. If @option{-T} was +not specified, the current report style's value expression is used. + +@item m +This is always the present moment/date. +@end table + +@subsubsection Transaction/account details + +@table @code +@item d +A transaction's date, as the number of seconds past the epoch. This +is always ``today'' for an account. + +@item a +The transaction's amount; the balance of an account, without +considering children. + +@item b +The cost of a transaction; the cost of an account, without its +children. + +@item v +The market value of a transaction, or an account without its children. + +@item g +The net gain (market value minus cost basis), for a transaction or an +account without its children. It is the same as @samp{v-b}. + +@item l +The depth (``level'') of an account. If an account has one parent, +it's depth is one. + +@item n +The index of a transaction, or the count of transactions affecting an +account. + +@item X +1 if a transaction's entry has been cleared, 0 otherwise. + +@item R +1 if a transaction is not virtual, 0 otherwise. + +@item Z +1 if a transaction is not automated, 0 otherwise. +@end table + +@subsubsection Calculated totals + +@table @code +@item O +The total of all transactions seen so far, or the total of an account +and all its children. + +@item N +The total count of transactions affecting an account and all its +children. + +@item B +The total cost of all transactions seen so far; the total cost of an +account and all its children. + +@item V +The market value of all transactions seen so far, or of an account and +all its children. + +@item G +The total net gain (market value minus cost basis), for a series of +transactions, or an account and its children. It is the same as +@samp{V-B}. +@end table + +@subsection Functions + +The available one letter functions are: + +@table @code +@item - +Negates the argument. + +@item U +The absolute (unsigned) value of the argument. + +@item S +Strips the commodity from the argument. + +@item A +The arithmetic mean of the argument; @samp{Ax} is the same as +@samp{x/n}. + +@item P +The present market value of the argument. The syntax @samp{P(x,d)} is +supported, which yields the market value at time @samp{d}. If no date +is given, then the current moment is used. +@end table + +@subsection Operators + +The binary and ternary operators, in order of precedence, are: + +@enumerate +@item @samp{* /} +@item @samp{+ -} +@item @samp{! < > =} +@item @samp{& | ?:} +@end enumerate + +@subsection Complex expressions + +More complicated expressions are possible using: + +@table @code +@item NUM +A plain integer represents a commodity-less amount. + +@item @{AMOUNT@} +An amount in braces can be any kind of amount supported by ledger, +with or without a commodity. Use this for decimal values. + +@item /REGEXP/ +A regular expression that matches against an account's full name. If +a transaction, this will match against the account affected by the +transaction. + +@item //REGEXP/ +A regular expression that matches against an entry's payee name. + +@item ///REGEXP/ +A regular expression that matches against an account's base name. If +a transaction, this will match against the account affected by the +transaction. + +@item (EXPR) +A sub-expression is nested in parenthesis. This can be useful passing +more complicated arguments to functions, or for overriding the natural +precedence order of operators. + +@item [DATE] +Useful specifying a date in plain terms. For example, you could say +@samp{[2004/06/01]}. + +@item @@STR(ARGS,...) +If Python support is compiled in, this calls the Python function +@code{STR}. It is always be passed at least one argument, of type +@var{ledger.Details}, representing the known account, entry, and +transaction details at the time the value expression is computed. +Other value expression arguments may also be passed by the user, all +of type @var{Value}. +@end table + +@node Period expressions, File format, Value expressions, Running Ledger +@section Period expressions + +A period expression indicates a span of time, or a reporting interval, +or both. The full syntax is: + +@example +[INTERVAL] [BEGIN] [END] +@end example + +The optional @var{INTERVAL} part may be any one of: + +@example +every day +every week +every monthly +every quarter +every year +every N days # N is any integer +every N weeks +every N months +every N quarters +every N years +daily +weekly +biweekly +monthly +bimonthly +quarterly +yearly +@end example + +After the interval, a begin time, end time, both or neither may be +specified. As for the begin time, it can be either of: + +@example +from <SPEC> +since <SPEC> +@end example + +The end time can be either of: + +@example +to <SPEC> +until <SPEC> +@end example + +Where @var{SPEC} can be any of: + +@example +2004 +2004/10 +2004/10/1 +10/1 +october +oct +this week # or day, month, quarter, year +next week +last week +@end example + +The beginning and ending can be given at the same time, if it spans a +single period. In that case, just use @var{SPEC} by itself. In that +case, the period @samp{oct}, for example, will cover all the days in +october. The possible forms are: + +@example +<SPEC> +in <SPEC> +@end example + +Here are a few examples of period expressions: + +@example +monthly +monthly in 2004 +weekly from oct +weekly from last month +from sep to oct +from 10/1 to 10/5 +monthly until 2005 +from apr +until nov +last oct +weekly last august +@end example + +@node File format, Some typical queries, Period expressions, Running Ledger +@section File format + +The ledger file format is quite simple, but also very flexible. It +supports many options, though typically the user can ignore most of +them. They are summarized below. + +The initial character of each line determines what the line means, and +how it should be interpreted. Allowable initial characters are: + +@table @code +@item NUMBER +A line beginning with a number denotes an entry. It may be followed +by any number of lines, each beginning with whitespace, to denote the +entry's account transactions. The format of the first line is: + +@example +DATE [*] [(CODE)] DESC +@end example + +If @samp{*} appears after the date, it indicates that entry is +``cleared'', meaning it has been seen a bank statement, or otherwise +verified. If a @samp{CODE} appears in parentheses, it may be used to +indicate a check number, or the type of the transaction. Following +these is the payee, or a description of the transaction. + +@item = +An automated entry. A value expression must appear after the equal +sign. + +After this initial line there should be a set of one or more +transactions, just as if it were normal entry. If the amounts of the +transactions have no commodity, they will be applied as modifiers to +whichever real transaction is matched by the value expression. + +@item ~ +A period entry. A period expression must appear after the tilde. + +After this initial line there should be a set of one or more +transactions, just as if it were normal entry. + +@item ! +A line beginning with an exclamation mark denotes a command directive. +It must be immediately followed by the command word. The supported +commands are: + +@table @samp +@item !include +Include the stated ledger file. + +@item !account +The account name is given is taken to be the parent of all +transactions that follow, until @samp{!end} is seen. + +@item !end +Ends an account block. + +@item !python +If Python support is available, all of the lines following +@samp{!python} will be passed to the Python interpretor. Any +functions defined will be available to later Python blocks, and can be +called from a value expression. The Python code block must be ended +with @samp{!end}. +@end table + +@item ; +A line beginning with a colon indicates a comment, and is ignored. + +@item Y +If a line begins with a capital Y, it denotes the year used for all +subsequent entries that give a date without a year. The year should +appear immediately after the Y, for example: @samp{Y2004}. This is +useful at the beginning of a file, to specify the year for that file. +If all entries specify a year, however, this command has no effect. + +@item P +Specifies a historical price for a commodity. These are usually found +in a pricing history file (see the @option{-Q} option). The syntax +is: +@example +P DATE SYMBOL PRICE +@end example + +@item N SYMBOL +Indicates that pricing information is to be ignored for a given +symbol, nor will quotes ever be downloaded for that symbol. Useful +with a home currency, such as the dollar ($). It is recommended that +these pricing options be set in the price database file, which +defaults to @file{~/.pricedb}. The syntax for this command is: +@example +N SYMBOL +@end example + +@item i, o, b, h +These four relate to timeclock support, which permits ledger to read +timelog files. See the timeclock's documentation for more info on the +syntax of its timelog files. +@end table + +@node Some typical queries, Budgeting and forecasting, File format, Running Ledger +@section Some typical queries + +A query such as the following shows all expenses since last +October, sorted by total: + +@example +ledger -b "last oct" -s -S T bal ^expenses +@end example + +From left to right the options mean: Show entries since October, 2003; +show all sub-accounts; sort by the absolute value of the total; and +report the balance for all expenses. + +@subsection Reporting monthly expenses + +The following query makes it easy to see monthly expenses, with each +month's expenses sorted by the amount: + +@example +ledger -M --period-sort t reg ^expenses +@end example + +Now, you might wonder where the money came from to pay for these +things. To see that report, add @option{-r}, which shows the +``related account'' transactions: + +@example +ledger -M --period-sort t -r reg ^expenses +@end example + +But maybe this prints too much information. You might just want to +see how much you're spending with your MasterCard. That kind of query +requires the use of a display predicate, since the transactions +calculated must match @samp{^expenses}, while the transactions +displayed must match @samp{mastercard}. The command would be: + +@example +ledger -M -r -d /mastercard/ reg ^expenses +@end example + +This query says: Report monthly subtotals; report the ``related +account'' transactions; display only related transactions whose +account matches @samp{mastercard}, and base the calculation on +transactions matching @samp{^expenses}. + +This works just as well for report the overall total, too: + +@example +ledger -s -r -d /mastercard/ reg ^expenses +@end example + +The @option{-s} option subtotals all transactions, just as @option{-M} +subtotaled by the month. The running total in both cases is off, +however, since a display expression is being used. + +@subsection Visualizing with Gnuplot + +If you have @command{Gnuplot} installed, you can graph any of the +above register reports. The script to do this is included in the +ledger distribution, and is named @file{scripts/report}. Install +@file{report} anywhere along your @env{PATH}, and then use +@command{report} instead of @command{ledger} when doing a register +report. The only thing to keep in mind is that you must specify +@option{-j} or @option{-J} to indicate whether Gnuplot should plot the +amount, or the running total. For example, this command plots total +monthly expenses made on your MasterCard. + +@example +report -j -M -r -d /mastercard/ reg ^expenses +@end example + +The @command{report} script is a very simple Bourne shell script, that +passes a set of scripted commands to Gnuplot. Feel free to modify the +script to your liking, since you may prefer histograms to line plots, +for example. + +@subsubsection Typical plots + +Here are some useful plots: + +@smallexample +report -j -M reg ^expenses # monthly expenses +report -J reg checking # checking account balance +report -J reg ^income ^expenses # cash flow report + +# net worth report, ignoring non-$ transactions + +report -J -l "Ua>=@{\$0.01@}" reg ^assets ^liab + +# net worth report starting last February. the use of a display +# predicate (-d) is needed, otherwise the balance will start at +# zero, and thus the y-axis will not reflect the true balance + +report -J -l "Ua>=@{\$0.01@}" -d "d>=[last feb]" reg ^assets ^liab +@end smallexample + +The last report uses both a calculation predicate (@option{-l}) and a +display predicate (@option{-d}). The calculation predicates limits +the report to transactions whose amount is greater than $1 (which can +only happen if the transaction amount is in dollars). The display +predicate limits the entries @emph{displayed} to just those since last +February, even those entries from before then will be computed as part +of the balance. + +@node Budgeting and forecasting, , Some typical queries, Running Ledger +@section Budgeting and forecasting + +@subsection Budgeting + +Keeping a budget allows you to pay closer attention to your income and +expenses, by reporting how far your actual financial activity is from +your expectations. + +To start keeping a budget, put some period entries at the top of your +ledger file. A period entry is almost identical to a regular entry, +except that it begins with a tilde and has a period expression in +place of a payee. For example: + +@smallexample +~ Monthly + Expenses:Rent $500.00 + Expenses:Food $450.00 + Expenses:Auto:Gas $120.00 + Expenses:Insurance $150.00 + Expenses:Phone $125.00 + Expenses:Utilities $100.00 + Expenses:Movies $50.00 + Assets + +~ Yearly + Expenses:Auto:Repair $500.00 + Assets +@end smallexample + +These two period entries give the usual monthly expenses, as well as +one typical yearly expense. For help on finding out what your average +monthly expense is for any category, use a command like: + +@example +ledger -p "this year" -MAs bal ^expenses +@end example + +The reported totals are the current year's average for each account. + +Once these period entries are defined, creating a budget report is as +easy as adding @option{--budget} to the command-line. For example, a +typical monthly expense report would be: + +@example +ledger -M reg ^exp +@end example + +To see the same report balanced against your budget, use: + +@example +ledger --budget -M reg ^exp +@end example + +A budget report includes only those accounts that appear in the +budget. To see all expenses balanced against the budget, use +@option{--add-budget}. You can even see only the unbudgeted expenses +using @option{--unbudgeted}: + +@example +ledger --unbudgeted -M reg ^exp +@end example + +You can also use these flags with the @command{balance} command. + +@subsection Forecasting + +Sometimes it's useful to know what your finances will look like in the +future, such as determining when an account will reach zero. Ledger +makes this easy to do, using the same period entries as are used for +budgeting. An example forecast report can be generated with: + +@example +ledger --forecast "T>@{\$-500.00@}" register ^assets ^liabilities +@end example + +This report continues outputting transactions until the running total +is greater than $-500.00. A final transaction is always output, to +show you what the total afterwards would be. + +Forecasting can also be used with the balance report, but by date +only, and not against the running total: + +@example +ledger --forecast "d<[2010]" bal ^assets ^liabilities +@end example + +@node Keeping a ledger, Extending with Python, Running Ledger, Top @chapter Keeping a ledger The most important part of accounting is keeping a good ledger. If @@ -188,19 +2551,19 @@ you. The Ledger program aims at making ledger entry as simple as possible. Since it is a command-line tool, it does not provide a user interface -for keeping a ledger. If you like, you may use Gnucash to maintain -your ledger, in which case the Ledger program will read Gnucash's data -files directly. In that case, read the Gnucash manual now, and skip +for keeping a ledger. If you like, you may use GnuCash to maintain +your ledger, in which case the Ledger program will read GnuCash's data +files directly. In that case, read the GnuCash manual now, and skip to the next chapter. -If you are not using Gnucash, but a text editor to maintain your +If you are not using GnuCash, but a text editor to maintain your ledger, read on. Ledger has been designed to make data entry as simple as possible, by keeping the ledger format easy, and also by automagically determining as much information as possible based on the nature of your entries. For example, you do not need to tell Ledger about the accounts you -use. Any time Ledger sees a debit or a credit to an account it knows +use. Any time Ledger sees a transaction involving an account it knows nothing about, it will create it. If you use a commodity that is new to Ledger, it will create that commodity, and determine its display characteristics (placement of the symbol before or after the amount, @@ -210,11 +2573,11 @@ transaction. Here is the Pacific Bell example from above, given as a Ledger transaction: -@example -9/29 (100) Pacific Bell - Expenses:Utilities:Telephone $23.00 +@smallexample +9/29 (100) Pacific Bell + Expenses:Utilities:Phone $23.00 Assets:Checking $-23.00 -@end example +@end smallexample As you can see, it is very similar to what would be written on paper, minus the computed balance totals, and adding in account names that @@ -222,94 +2585,295 @@ work better with Ledger's scheme of things. In fact, since Ledger is smart about many things, you don't need to specify the balanced amount, if it is the same as the first line: -@example -9/29 (100) Pacific Bell - Expenses:Utilities:Telephone $23.00 +@smallexample +9/29 (100) Pacific Bell + Expenses:Utilities:Phone $23.00 Assets:Checking -@end example +@end smallexample For this entry, Ledger will figure out that $-23.00 must come from -``Assets:Checking'' in order to balance the entry. +@samp{Assets:Checking} in order to balance the entry. @menu -* Credits and Debits:: -* Commodities and Currencies:: -* Accounts and Inventories:: -* Understanding Equity:: -* Dealing with cash:: -* Archiving previous years:: -* Virtual transactions:: -* Automated transactions:: +* Stating where money goes:: +* Assets and Liabilities:: +* Commodities and Currencies:: +* Accounts and Inventories:: +* Understanding Equity:: +* Dealing with Petty Cash:: +* Archiving previous years:: +* Virtual transactions:: +* Automated transactions:: * Using Emacs to Keep Your Ledger:: * Using GnuCash to Keep Your Ledger:: * Using timeclock to record billable time:: @end menu -@node Credits and Debits, Commodities and Currencies, Keeping a ledger, Keeping a ledger -@section Credits and Debits - -Credit and debit are simple enough terms in themselves, but the usages -of the modern world have made them very hard to puzzle out. +@node Stating where money goes, Assets and Liabilities, Keeping a ledger, Keeping a ledger +@section Stating where money goes -Basically, a credit means you add something to an account, and a debit -means you take away. A debit card is correctly name: From your point -of view, it debits your checking account every time you use it. +Accountants will talk of ``credits'' and ``debits'', but the meaning +is often different from the layman's understanding. To avoid +confusion, Ledger uses only subtractions and additions, although the +underlying intent is the same as standard accounting principles. -The credit card is strangely named, because you have to look at it -from the merchant's point of view: Every time you use it, it credit's -@emph{his} account right away. This was a giant leap from the days of cash -and checks, when the only other way to supply immediate credit was by -a wire transfer. But a credit card does not credit you anything at -all. In fact, from your point of view, it should be called a -liability card, since it increases your liability to the issuing bank -every time you use it. +Recall that every transaction will involve two or more accounts. +Money is transferred from one or more accounts to one or more other +accounts. To record the transaction, an amount is @emph{subtracted} +from the source accounts, and @emph{added} to the target accounts. -In Ledger, credits and debits are given as they are, which means that -sometimes you will see a minus sign where you don't expect one. For -example, when you get paid, in order to credit your bank account, you -need to debit an income account: +In order to write a Ledger entry correctly, you must determine where +the money comes from and where it goes to. For example, when you are +paid a salary, you must add money to your bank account and also +subtract it from an income account: -@example +@smallexample 9/29 My Employer Assets:Checking $500.00 Income:Salary $-500.00 -@end example - -But wait, you say, why is the Income a negative figure? And when you -look at the balance totals for your ledger, you will certainly be -surprised to see Expenses as a positive figure, and Income as a -negative figure. Isn't that the opposite of how it should look? +@end smallexample -It may take getting used to, but to properly use a general ledger you -will need to think in terms of correct debits and credits. Rather -than Ledger ``fixing'' the minus signs, let's understand why they are -there. +Why is the Income a negative figure? When you look at the balance +totals for your ledger, you may be surprised to see that Expenses are +a positive figure, and Income is a negative figure. It may take some +getting used to, but to properly use a general ledger you must think +in terms of how money moves. Rather than Ledger ``fixing'' the minus +signs, let's understand why they are there. When you earn money, the money has to come from somewhere. Let's call -that somewhere ``society''. In order for society to give you an income, -you must take money away from society (debit) in order to put it into -your bank (credit). When you then spend that money, it leaves your -bank account (debit) and goes back to society (credit). This is why -Income will appear negative---it reflects the money you have drawn -from society---and why Expenses will be positive---it is the amount -you've given back. These credits and debits will always cancel each -other out in the end, because you don't have the ability to create new -money: It must always come from somewhere, and in the end must always -leave. This is the beginning of economy, after which the explanation -gets terribly difficult. +that somewhere ``society''. In order for society to give you an +income, you must take money away (withdraw) from society in order to +put it into (make a payment to) your bank. When you then spend that +money, it leaves your bank account (a withdrawal) and goes back to +society (a payment). This is why Income will appear negative---it +reflects the money you have drawn from society---and why Expenses will +be positive---it is the amount you've given back. These additions and +subtractions will always cancel each other out in the end, because you +don't have the ability to create new money: it must always come from +somewhere, and in the end must always leave. This is the beginning of +economy, after which the explanation gets terribly difficult. Based on that explanation, here's another way to look at your balance -report: Every negative figure means that that account or person or +report: every negative figure means that that account or person or place has less money now than when you started your ledger; and every positive figure means that that account or person or place has more money now that when you started your ledger. Make sense? -Also, credit cards will have a negative value, because you are -spending @emph{from} them (debit) in order pay someone else (credit). They -are called credit cards because you are able to instantly credit that -other person, by simply waving a card. +@node Assets and Liabilities, Commodities and Currencies, Stating where money goes, Keeping a ledger +@section Assets and Liabilities + +Assets are money that you have, and Liabilities are money that you +owe. ``Liabilities'' is just a more inclusive name for Debts. + +An Asset is typically increased by transferring money from an Income +account, such as when you get paid. Here is a typical entry: + +@smallexample +2004/09/29 My Employer + Assets:Checking $500.00 + Income:Salary +@end smallexample + +Money, here, comes from an Income account belonging to ``My +Employer'', and is transferred to your checking account. The money is +now yours, which makes it an Asset. + +Liabilities track money owed to others. This can happen when you +borrow money to buy something, or if you owe someone money. Here is +an example of increasing a MasterCard liability by spending money with +it: + +@smallexample +2004/09/30 Restaurant + Expenses:Dining $25.00 + Liabilities:MasterCard +@end smallexample + +The Dining account balance now shows $25 spent on Dining, and a +corresponding $25 owed on the MasterCard---and therefore shown as +$-25.00. The MasterCard liability shows up as negative because it +offsets the value of your assets. -@node Commodities and Currencies, Accounts and Inventories, Credits and Debits, Keeping a ledger +The combined total of your Assets and Liabilities is your net worth. +So to see your current net worth, use this command: + +@example +ledger balance ^assets ^liabilities +@end example + +Relatedly, your Income accounts show up negative, because they +transfer money @emph{from} an account in order to increase your +assets. Your Expenses show up positive because that is where the +money went to. The combined total of Income and Expenses is your cash +flow. A negative cash flow means you are spending more than you make. +To see your current cash flow, use this command: + +@example +ledger balance ^income ^expenses +@end example + +Another common question to ask of your expenses is: How much do I +spend each month on X? Ledger provides a simple way of displaying +monthly totals for any account. Here is an example that summarizes +your monthly automobile expenses: + +@example +ledger -M register expenses:auto +@end example + +This assumes, of course, that you use account names like +@samp{Expenses:Auto:Gas} and @samp{Expenses:Auto:Repair}. + +@subsection Tracking reimbursable expenses + +Sometimes you will want to spend money on behalf of someone else, +which will eventually get repaid. Since the money is still ``yours'', +it is really an asset. And since the expenditure was for someone +else, you don't want it contaminating your Expenses reports. You will +need to keep an account for tracking reimbursements. + +This is fairly easy to do in ledger. When spending the money, spend +it @emph{to} your Assets:Reimbursements, using a different account for +each person or business that you spend money for. For example: + +@smallexample +2004/09/29 Circuit City + Assets:Reimbursements:Company XYZ $100.00 + Liabilities:MasterCard +@end smallexample + +This shows $100.00 spent on a MasterCard at Circuit City, with the +expense was made on behalf of Company XYZ. Later, when Company XYZ +pays the amount back, the money will transfer from that reimbursement +account back to a regular asset account: + +@smallexample +2004/09/29 Company XYZ + Assets:Checking $100.00 + Assets:Reimbursements:Company XYZ +@end smallexample + +This deposits the money owed from Company XYZ into a checking account, +presumably because they paid the amount back with a check. + +But what to do if you run your own business, and you want to keep +track of expenses made on your own behalf, while still tracking +everything in a single ledger file? This is more complex, because you +need to track two separate things: 1) The fact that the money should +be reimbursed to you, and 2) What the expense account was, so that you +can later determine where your company is spending its money. + +This kind of transaction is best handled with mirrored transactions in +two different files, one for your personal accounts, and one for your +company accounts. But keeping them in one file involves the same +kinds of transactions, so those are what is shown here. First, the +personal entry, which shows the need for reimbursement: + +@smallexample +2004/09/29 Circuit City + Assets:Reimbursements:Company XYZ $100.00 + Liabilities:MasterCard +@end smallexample + +This is the same as above, except that you own Company XYZ, and are +keeping track of its expenses in the same ledger file. This entry +should be immediately followed by an equivalent entry, which shows the +kind of expense, and also notes the fact that $100.00 is now payable +to you: + +@smallexample +2004/09/29 Circuit City + Company XYZ:Expenses:Computer:Software $100.00 + Company XYZ:Accounts Payable:Your Name +@end smallexample + +This second entry shows that Company XYZ has just spent $100.00 on +software, and that this $100.00 came from Your Name, which must be +paid back. + +These two entries can also be merged, to make things a little clearer. +Note that all amounts must be specified now: + +@smallexample +2004/09/29 Circuit City + Assets:Reimbursements:Company XYZ $100.00 + Liabilities:MasterCard $-100.00 + Company XYZ:Expenses:Computer:Software $100.00 + Company XYZ:Accounts Payable:Your Name $-100.00 +@end smallexample + +To ``pay back'' the reimbursement, just reverse the order of +everything, except this time drawing the money from a company asset, +paying it to accounts payable, and then drawing it again from the +reimbursement account, and paying it to your personal asset account. +It's easier shown than said: + +@smallexample +2004/10/15 Company XYZ + Assets:Checking $100.00 + Assets:Reimbursements:Company XYZ $-100.00 + Company XYZ:Accounts Payable:Your Name $100.00 + Company XYZ:Assets:Checking $-100.00 +@end smallexample + +And now the reimbursements account is paid off, accounts payable is +paid off, and $100.00 has been effectively transferred from the +company's checking account to your personal checking account. The +money simply ``waited''---in both @samp{Assets:Reimbursements:Company +XYZ}, and @samp{Company XYZ:Accounts Payable:Your Name}---until such +time as it could be paid off. + +The value of tracking expenses from both sides like that is that you +do not contaminate your personal expense report with expenses made on +behalf of others, while at the same time making it possible to +generate accurate reports of your company's expenditures. It is more +verbose than just paying for things with your personal assets, but it +gives you a very accurate information trail. + +The advantage to keep these doubled entries together is that they +always stay in sync. The advantage to keeping them apart is that it +clarifies the transfer's point of view. To keep the transactions in +separate files, just separate the two entries that were joined above. +For example, for both the expense and the pay-back shown above, the +following four entries would be created. Two in your personal ledger +file: + +@smallexample +2004/09/29 Circuit City + Assets:Reimbursements:Company XYZ $100.00 + Liabilities:MasterCard $-100.00 + +2004/10/15 Company XYZ + Assets:Checking $100.00 + Assets:Reimbursements:Company XYZ $-100.00 +@end smallexample + +And two in your company ledger file: + +@smallexample +!account Company XYZ + +2004/09/29 Circuit City + Expenses:Computer:Software $100.00 + Accounts Payable:Your Name $-100.00 + +2004/10/15 Company XYZ + Accounts Payable:Your Name $100.00 + Assets:Checking $-100.00 + +!end +@end smallexample + +(Note: The @samp{!account} above means that all accounts mentioned in +the file are children of that account. In this case it means that all +activity in the file relates to Company XYZ). + +After creating these entries, you will always know that $100.00 was +spent using your MasterCard on behalf of Company XYZ, and that Company +XYZ spent the money on computer software and paid it back about two +weeks later. + +@node Commodities and Currencies, Accounts and Inventories, Assets and Liabilities, Keeping a ledger @section Commodities and Currencies Ledger makes no assumptions about the commodities you use; it only @@ -323,11 +2887,10 @@ specifiers: @example $20.00 ; currency: twenty US dollars -USD 20 ; currency: the same 40 AAPL ; commodity: 40 shares of Apple stock -MD 60 ; currency: 60 Deutsch Mark +60 DM ; currency: 60 Deutsch Mark £50 ; currency: 50 British pounds -50e ; currency: 50 Euros (use symbol) +50 EUR ; currency: 50 Euros (or use appropriate symbol) @end example Ledger will examine the first use of any commodity to determine how @@ -342,13 +2905,13 @@ have separate totals for each. For example, if your brokerage account contains both cash, gold, and several stock quantities, the balance might look like: -@example +@smallexample $200.00 100.00 AU AAPL 40 BORL 100 FEQTX 50 Assets:Brokerage -@end example +@end smallexample This balance report shows how much of each commodity is in your brokerage account. @@ -356,65 +2919,50 @@ brokerage account. Sometimes, you will want to know the current street value of your balance, and not the commodity totals. For this to happen, you must specify what the current price is for each commodity. The price can -be in any commodity, in which case the balance will be computed in -terms of that commodity. The usual way to specify prices is with a -file of price settings, which might look like this: +be any commodity, in which case the balance will be computed in terms +of that commodity. The usual way to specify prices is with a price +history file, which might look like this: -@example -AU=$357.00 -AAPL=$37 -BORL=$19 -FEQTX=$32 -@end example +@smallexample +P 2004/06/21 02:18:01 FEQTX $22.49 +P 2004/06/21 02:18:01 BORL $6.20 +P 2004/06/21 02:18:02 AAPL $32.91 +P 2004/06/21 02:18:02 AU $400.00 +@end smallexample -Specify the prices file using the @samp{-p} option: +Specify the price history to use with the @option{-P} option: @example -ledger -p prices.db balance brokerage +ledger -P prices.db balance brokerage @end example -Now the balance for your brokerage account will be given in US -dollars, since the prices database has specified conversion factors -from each commodity into dollars: +The balance for your brokerage account will be reported in US dollars, +since the prices database uses that currency. -@example +@smallexample $40880.00 Assets:Brokerage -@end example +@end smallexample You can convert from any commodity to any other commodity. Let's say you had $5000 in your checking account, and for whatever reason you -wanted to know many ounces of gold that would buy. If gold is -currently $357 per ounce, then each dollar is worth 1/357 AU: - -@example -ledger -p "$=0.00280112 AU" balance checking -@end example - -@example -14.01 AU Assets:Checking -@end example - -$5000 would buy 14 ounces of gold, which becomes the new display -commodity since a conversion factor was provided. - -Commodities conversions can also be chained, up to a depth of 10. -Here is a sample prices database that uses chaining: +wanted to know many ounces of gold that would buy, in terms of the +current price of gold: @example -AAPL=$15 -$=0.00280112 AU -AU=300 Euro -Euro=MD 0.75 +ledger -T "@{1 AU@}*(O/P@{1 AU@})" balance checking @end example -This is a roundabout way of reporting AAPL shares in their Deutsch -Mark equivalent. +Although the total expression appears complex, it is simply saying +that the reported total should be in multiples of AU units, where the +quantity is the account total divided by the price of one AU. Without +the initial multiplication, the reported total would still use the +dollars commodity, since multiplying or dividing amounts always keeps +the left value's commodity. The result of this command might be: -@menu -* Commodity price histories:: -@end menu +@smallexample +14.01 AU Assets:Checking +@end smallexample -@node Commodity price histories, , Commodities and Currencies, Commodities and Currencies @subsection Commodity price histories Whenever a commodity is purchased using a different commodity (such as @@ -423,65 +2971,18 @@ that commodity on that day. It is also possible, by recording price details in a ledger file, to specify other prices for commodities at any given time. Such price entries might look like those below: -@example +@smallexample P 2004/06/21 02:17:58 TWCUX $27.76 P 2004/06/21 02:17:59 AGTHX $25.41 P 2004/06/21 02:18:00 OPTFX $39.31 P 2004/06/21 02:18:01 FEQTX $22.49 P 2004/06/21 02:18:02 AAPL $32.91 -@end example +@end smallexample By default, ledger will not consider commodity prices when generating its various reports. It will always report balances in terms of the commodity total, rather than the current value of those commodities. -To enable pricing reports, three options are possible: - -@table @strong -@item @strong{-P FILE} -With this option, or if the environment variable @samp{PRICE_HIST} is -set, pricing information obtained from the Internet will be kept -in this file. Also, this file will be read after all other ledger -files are read, so that full history information is available for -reports. - -@item @strong{-T} -Report commodity totals only, not their market value or basis cost. - -@item @strong{-V} -Report commodity values in terms of their last known market price. - -@item @strong{-B} -Report commodities in terms of their ``basis cost'', or what they cost -at time of purchase. Thus, totals in the register and balance -report reflect the total amount spent. - -@item @strong{-A} -Report commodities in terms of their net gain, which is: the market -value minus the cost basis. A balance report using this option -shows very quickly the performance of investments. - -@item @strong{-Q} -When needed (see the @samp{-L} option) pricing quotes are obtained by -calling the script @samp{getquote} (a sample Perl script is provided, but -the interface is kept simple so replacements may be made). - -@item @strong{-L MINS} -When using the @samp{-Q} flag, new quotes are obtained only if current -pricing data is older than MINS minutes. The default is one day, -or 1440 minutes. - -@item @strong{-p ARG} -If a string, such as ``COMM=$1.20'', the commodity COMM will be -reported only in terms of the conversion factor, which supersedes -all other pricing histories for that commodity. This can be used to -perform arbitrary value substitutions. For example, to report the -value of your dollars in terms of the ounces of gold they would buy, -use: -p ``$=0.00280112 AU'' (or whatever the current exchange rate -is). - -@end table - -Note that the @samp{-B}, @samp{-T}, @samp{-V}, and @samp{-A} are mutually exclusive. +To enable pricing reports, use one of the commodity reporting options. @node Accounts and Inventories, Understanding Equity, Commodities and Currencies, Keeping a ledger @section Accounts and Inventories @@ -493,35 +2994,35 @@ various items in EverQuest, and want to keep track of them using a ledger. Just add items of whatever quantity you wish into your EverQuest account: -@example +@smallexample 9/29 Get some stuff at the Inn Places:Black's Tavern -3 Apples Places:Black's Tavern -5 Steaks EverQuest:Inventory -@end example +@end smallexample Now your EverQuest:Inventory has 3 apples and 5 steaks in it. The -amounts are negative, because you are taking @emph{from} Black's Tavern in -order to credit your Inventory account. Note that you don't have to -use ``Places:Black's Tavern'' as the source account. You could use -``EverQuest:System'' to represent the fact that you acquired them -online. The only purpose for choosing one kind of source account over -another is for generate more informative reports later on. The more -you know, the better analysis you can perform. +amounts are negative, because you are taking @emph{from} Black's +Tavern in order to add to your Inventory account. Note that you don't +have to use @samp{Places:Black's Tavern} as the source account. You +could use @samp{EverQuest:System} to represent the fact that you +acquired them online. The only purpose for choosing one kind of +source account over another is for generate more informative reports +later on. The more you know, the better analysis you can perform. If you later sell some of these items to another player, the entry would look like: -@example -10/2 Strum Brightblade +@smallexample +10/2 Sturm Brightblade EverQuest:Inventory -2 Steaks EverQuest:Inventory 15 Gold -@end example +@end smallexample Now you've turned 2 steaks into 15 gold, courtesy of your customer, -Strum Brightblade. +Sturm Brightblade. -@node Understanding Equity, Dealing with cash, Accounts and Inventories, Keeping a ledger +@node Understanding Equity, Dealing with Petty Cash, Accounts and Inventories, Keeping a ledger @section Understanding Equity The most confusing entry in any ledger will be your equity account--- @@ -532,11 +3033,11 @@ in some of your accounts. Let's say there's $100 in your checking account; then add an entry to your ledger to reflect this amount. Where will money come from? The answer: your equity. -@example +@smallexample 10/2 Opening Balance Assets:Checking $100.00 Equity:Opening Balances -@end example +@end smallexample But what is equity? You may have heard of equity when people talked about house mortgages, as ``the part of the house that you own''. @@ -548,50 +3049,50 @@ account, you will have to debit the equity by selling it. When you start a ledger, you are probably already worth something. Your net worth is your current equity. By transferring the money in the ledger from your equity to your bank accounts, you are crediting -the ledger account based on your prior equity value. That is why, -when you look at the balance report, you will see a large negative -number for Equity that never changes: Because that is what you were -worth (what you debited from yourself in order to start the ledger) -before the money started moving around. If the total positive value -of your assets is greater than the absolute value of your starting -equity, it means you are making money. +the ledger account based on your prior equity. That is why, when you +look at the balance report, you will see a large negative number for +Equity that never changes: Because that is what you were worth (what +you debited from yourself in order to start the ledger) before the +money started moving around. If the total positive value of your +assets is greater than the absolute value of your starting equity, it +means you are making money. Clear as mud? Keep thinking about it. Until you figure it out, put -``-- -Equity'' at the end of your balance command, to remove the -confusing figure from the totals. +@samp{-Equity} at the end of your balance command, to remove the +confusing figure from the total. -@node Dealing with cash, Archiving previous years, Understanding Equity, Keeping a ledger -@section Dealing with cash +@node Dealing with Petty Cash, Archiving previous years, Understanding Equity, Keeping a ledger +@section Dealing with Petty Cash Something that stops many people from keeping a ledger at all is the -insanity of tracking cash expenses. They rarely generate a receipt, -and there are often a lot of small transactions, rather than a few -large ones, as with checks. +insanity of tracking small cash expenses. They rarely generate a +receipt, and there are often a lot of small transactions, rather than +a few large ones, as with checks. -The answer is: don't bother. Move your spending to a debit card, but -in general ignore cash. Once you withdraw it from the ATM, mark it as -already spent to an ``Expenses:Cash'' category: +One solution is: don't bother. Move your spending to a debit card, +but in general ignore cash. Once you withdraw it from the ATM, mark +it as already spent to an @samp{Expenses:Cash} category: -@example +@smallexample 2004/03/15 ATM Expenses:Cash $100.00 Assets:Checking -@end example +@end smallexample If at some point you make a large cash expense that you want to track, -just ``move'' the amount of the expense from ``Expenses:Cash'' into the -target account: +just ``move'' the amount of the expense from @samp{Expenses:Cash} into +the target account: -@example +@smallexample 2004/03/20 Somebody Expenses:Food $65.00 Expenses:Cash -@end example +@end smallexample This way, you can still track large cash expenses, while ignoring all of the smaller ones. -@node Archiving previous years, Virtual transactions, Dealing with cash, Keeping a ledger +@node Archiving previous years, Virtual transactions, Dealing with Petty Cash, Keeping a ledger @section Archiving previous years After a while, your ledger can get to be pretty large. While this @@ -603,43 +3104,42 @@ them. Thus, archiving the data from previous years into their own files can offer a sense of completion, and freedom from the past. But how to best accomplish this with the ledger program? There are two commands -that make it very simple: ``print'', and ``equity''. +that make it very simple: @command{print}, and @command{equity}. Let's take an example file, with data ranging from year 2000 until 2004. We want to archive years 2000 and 2001 to their own file, -leaving just 2003 and 2004 in the current file. So, use ``print'' to -output all the earlier entries to a file called @samp{ledger-old.dat}. -(Keeping in mind that the ending date is not inclusive, which is why -2002 is mentioned in the following command): +leaving just 2003 and 2004 in the current file. So, use +@command{print} to output all the earlier entries to a file called +@file{ledger-old.dat}. (Keeping in mind that the ending date is not +inclusive, which is why 2002 is mentioned in this command): -@example -$ ledger -f ledger.dat -b 2000/1/1 -e 2002/1/1 print \ - > ledger-old.dat -@end example +@smallexample +ledger -f ledger.dat -b 2000 -e 2002 print > ledger-old.dat +@end smallexample -To delete older data from the current ledger file, use ``print'' again, -this time specifying year 2002 as the starting date: +To delete older data from the current ledger file, use @command{print} +again, this time specifying year 2002 as the starting date: @example -$ ledger -f ledger.dat -b 2002/1/1 print > x -$ mv x ledger.dat +ledger -f ledger.dat -b 2002 print > x +mv x ledger.dat @end example -However, now the current file contains @emph{only} transactions from 2002 -onward, which will not yield accurate present-day balances, because -the net income from previous years is no longer being tallied. To -compensate for this, we must append an equity report for the old +However, now the current file contains @emph{only} transactions from +2002 onward, which will not yield accurate present-day balances, +because the net income from previous years is no longer being tallied. +To compensate for this, we must append an equity report for the old ledger at the beginning of the new one: @example -$ ledger -f ledger-old.dat equity > equity.dat -$ cat equity.dat ledger.dat > x -$ mv x ledger.dat -$ rm equity.dat +ledger -f ledger-old.dat equity > equity.dat +cat equity.dat ledger.dat > x +mv x ledger.dat +rm equity.dat @end example -Now the balances reported from @samp{ledger.dat} are identical to what they -were before the data was split. +Now the balances reported from @file{ledger.dat} are identical to what +they were before the data was split. How often should you split your ledger? You never need to, if you don't want to. Even eighty years of data will not slow down ledger @@ -667,122 +3167,145 @@ if you want to ensure the virtual transaction balances with other virtual transactions in the same entry, use square brackets. For example: -@example -10/2 Paycheck +@smallexample +10/2 Paycheck Assets:Checking $1000.00 Income:Salary $-1000.00 (Debt:Alimony) $200.00 -@end example +@end smallexample In this example, after receiving a paycheck an alimony debt is increased---even though no money has moved around yet. -@example +@smallexample 10/2 Paycheck Assets:Checking $1000.00 Income:Salary $-1000.00 [Savings:Trip] $200.00 [Assets:Checking] $-200.00 -@end example +@end smallexample In this example, $200 has been deducted from checking toward savings for a trip. It will appear as though the money has been moved from -the account into ``Savings:Trip'', although no money has actually moved -anywhere. +the account into @samp{Savings:Trip}, although no money has actually +moved anywhere. When balances are displayed, virtual transactions will be factored in. To view balances without any virtual balances factored in, using the -``-R'' flag, for ``Reality''. - -Write about: Saving for a Special Occasion; Keeping a Budget; Tracking -Allocated Funds. +@option{-R} flag, for ``reality''. @node Automated transactions, Using Emacs to Keep Your Ledger, Virtual transactions, Keeping a ledger @section Automated transactions As a Bahá'í, I need to compute Huqúqu'lláh whenever I acquire assets. -The exact details of this are a bit complex, so if you have further -interest, please consult the Web. +It is similar to tithing for Jews and Christians, or to Zakát for +Muslims. The exact details of computing Huqúqu'lláh are somewhat +complex, but if you have further interest, please consult the Web. -For any fellow Bahá'ís out there who want to track Huqúqu'lláh, the -Ledger tool makes this extremely easy. Just set up the following +Ledger makes this otherwise difficult law very easy. Just set up an automated transaction at the top of your ledger file: -@example -; These entries will compute Huqúqu'lláh based on the -; contents of the ledger. - -= ^Income: -= ^Expenses:Rent$ -= ^Expenses:Furnishings -= ^Expenses:Business -= ^Expenses:Taxes -= ^Expenses:Insurance - (Liabilities:Huqúqu'lláh) 0.19 -@end example - -This automated transaction works by looking at each transaction -appearing afterward in the ledger file. If any match the account -regexps, occurring after the equal signs above, 19% of the value of -that transaction is applied to the ``Liabilities:Huqúqu'lláh'' account. -So if $1000 is earned through Income:Salary, which is seen as a debit -from Income, a debit of $190 is applied to ``Liabilities:Huqúqu'lláh''; -if $1000 is spent on Rent---seen as a credit to the Expense account---a credit of $190 is applied to Huqúqu'lláh. The ultimate balance -of Huqúqu'lláh reflects how much must be paid to that account in order -to balance it to zero. - -When you're ready to pay, just write a check directly to the account -``Liabilities:Huqúqu'lláh'': - -@example +@smallexample +; This automated entry will compute Huqúqu'lláh based on this +; journal's transactions. Any that match will affect the +; Liabilities:Huququ'llah account by 19% of the value of that +; transaction. + += /^(?:Income:|Expenses:(?:Business|Rent$|Furnishings|Taxes|Insurance))/ + (Liabilities:Huququ'llah) 0.19 +@end smallexample + +This automated transaction works by looking at each transaction in the +ledger file. If any match the given value expression, 19% of the +transaction's value is applied to the @samp{Liabilities:Huququ'llah} +account. So, if $1000 is earned from @samp{Income:Salary}, $190 is +added to @samp{Liabilities:Huqúqu'lláh}; if $1000 is spent on Rent, +$190 is subtracted. The ultimate balance of Huqúqu'lláh reflects how +much is owed in order to fulfill one's obligation to Huqúqu'lláh. +When ready to pay, just write a check to cover the amount shown in +@samp{Liabilities:Huququ'llah}. That entry would look like: + +@smallexample 2003/01/01 (101) Baha'i Huqúqu'lláh Trust - Liabilities:Huqúqu'lláh $1,000.00 + Liabilities:Huququ'llah $1,000.00 Assets:Checking -@end example +@end smallexample That's it. To see how much Huqúq is currently owed based on your ledger entries, use: @example -ledger balance Liabilities:Huqúq +ledger balance Liabilities:Huquq @end example +This works fine, but omits one aspect of the law: that Huquq is only +due once the liability exceeds the value of 19 mithqáls of gold (which +is roughly 2.22 ounces). So what we want is for the liability to +appear in the balance report only when it exceeds the present day +value of 2.22 ounces of gold. This can be accomplished using the +command: + +@smallexample +ledger -Q -t "/Liab.*Huquq/?(a/P@{2.22 AU@}<=@{-1.0@}&a):a" -s bal liab +@end smallexample + +With this command, the current price for gold is downloaded, and the +Huqúqu'lláh is reported only if its value exceeds that of 2.22 ounces +of gold. If you wish the liability to be reflected in the parent +subtotal either way, use this instead: + +@smallexample +ledger -Q -T "/Liab.*Huquq/?(O/P@{2.22 AU@}<=@{-1.0@}&O):O" -s bal liab +@end smallexample + +In some cases, you may wish to refer to the account of whatever +transaction matched your automated entry's value expression. To do +this, use the special account name @samp{$account}: + +@smallexample += /^Some:Long:Account:Name/ + [$account] -0.10 + [Savings] 0.10 +@end smallexample + +This example causes 10% of the matching account's total to be deferred +to the @samp{Savings} account---as a balanced virtual transaction, +which may be excluded from reports by using @option{--real}. @node Using Emacs to Keep Your Ledger, Using GnuCash to Keep Your Ledger, Automated transactions, Keeping a ledger @section Using Emacs to Keep Your Ledger -In the Ledger tarball is an Emacs module, @samp{ledger.el}. This module -makes the process of keeping a text ledger much easier for Emacs -users. I recommend putting this at the top of your ledger file: +In the Ledger tarball is an Emacs module, @file{ledger.el}. This +module makes the process of keeping a text ledger much easier for +Emacs users. I recommend putting this at the top of your ledger file: @example ; -*-ledger-*- @end example -And this in your @samp{.emacs} file, after copying @samp{ledger.el} to your -site-lisp directory: +And this in your @file{.emacs} file, after copying @file{ledger.el} to +your @file{site-lisp} directory: @example (load "ledger") @end example -Now when you edit your ledger file, it will be in @samp{ledger-mode}. -@samp{ledger-mode} adds the following commands: +Now when you edit your ledger file, it will be in +@command{ledger-mode}. @command{ledger-mode} adds these commands: @table @strong @item C-c C-a -For quickly adding new entries based on the form of older ones -(see previous section). +For quickly adding new entries based on the form of older ones (see +previous section). @item C-c C-c Toggles the ``cleared'' flag of the transaction under point. @item C-c C-r Reconciles an account by displaying the transactions in another -buffer, where simply hitting the spacebar will toggle the cleared -flag of the transaction in the ledger. It also displays the current +buffer, where simply hitting the spacebar will toggle the cleared flag +of the transaction in the ledger. It also displays the current cleared balance for the account in the modeline. - @end table @node Using GnuCash to Keep Your Ledger, Using timeclock to record billable time, Using Emacs to Keep Your Ledger, Keeping a ledger @@ -794,12 +3317,12 @@ editor, and like doing so. Perhaps an Emacs mode will appear someday soon to make editing Ledger's data files much easier. Until then, you are free to use GnuCash to maintain your ledger, and -the Ledger program for querying and reporting on the contents -of that ledger. It takes a little longer to parse the XML data format -that GnuCash uses, but the end result is identical. +the Ledger program for querying and reporting on the contents of that +ledger. It takes a little longer to parse the XML data format that +GnuCash uses, but the end result is identical. Then again, why would anyone use a Gnome-centric, 35 megabyte behemoth -to edit their data, and a 65 kilobyte binary to query it... +to edit their data, and a one megabyte binary to query it? @node Using timeclock to record billable time, , Using GnuCash to Keep Your Ledger, Keeping a ledger @section Using timeclock to record billable time @@ -817,48 +3340,47 @@ For example, the command-line version of the timeclock tool (which is written in Python) could be used to begin a timelog file like: @example -$ export TIMELOG=$HOME/.timelog -$ ti ClientOne category -$ sleep 10 -$ to waited for ten seconds +export TIMELOG=$HOME/.timelog +ti ClientOne category +sleep 10 +to waited for ten seconds @end example -The @strong{.timelog} file now contains: +The @file{.timelog} file now contains: -@example +@smallexample i 2004/10/06 15:21:00 ClientOne category o 2004/10/06 15:21:10 waited for ten seconds -@end example +@end smallexample -Ledger can parse this directly, as if it had seen the following ledger -entry: +Ledger parses this directly, as if it had seen the following entry: -@example +@smallexample 2004/10/06 category - (ClientOne) 0.00277h -@end example + (ClientOne) 10s +@end smallexample In other words, the timelog event pair is seen as adding 0.00277h (ten -seconds) worth of time to the ClientOne account. This would be +seconds) worth of time to the @samp{ClientOne} account. This would be considered billable time, which later could be invoiced and credited to accounts receivable: -@example +@smallexample 2004/11/01 (INV#1) ClientOne, Inc. Receivable:ClientOne $0.10 ClientOne -0.00277h @@ $35.00 -@end example +@end smallexample The above transaction converts the clocked time into an invoice for the time spent, at an hourly rate of $35. Once the invoice is paid, the money is deposited from the receivable account into a checking account: -@example +@smallexample 2004/12/01 ClientOne, Inc. Assets:Checking $0.10 Receivable:ClientOne -@end example +@end smallexample And now the time spent has been turned into hard cash in the checking account. @@ -868,17 +3390,19 @@ you will always know, by looking at the balance report, exactly how much unbilled and unpaid time you've spent working for any particular client. -I like to @samp{!include} my timelog at the top of my company's accounting -ledger, with the attached prefix ``Billable'': +I like to @samp{!include} my timelog at the top of my company's +accounting ledger, with the attached prefix @samp{Billable}: -@example +@smallexample ; -*-ledger-*- ; This is the ledger file for my company. But first, include the ; timelog data, entering all of the time events within the umbrella ; account "Billable". -!include /home/johnw/.timelog Billable +!account Billable +!include /home/johnw/.timelog +!end ; Here follows this fiscal year's transactions for the company. @@ -889,460 +3413,31 @@ ledger, with the attached prefix ``Billable'': 2004/12/01 ClientOne, Inc. Assets:Checking $0.10 Receivable:ClientOne -@end example - -@node Running Ledger, , Keeping a ledger, Top -@chapter Running Ledger - -Now that you have an orderly and well-organized general ledger, it's -time to start generating some orderly and well-organized reports. -This is where the Ledger tool comes in. With it, you can balance your -checkbook, see where your money is going, tell whether you've made a -profit this year, and even compute the present day value of your -retirement accounts. And all with the simplest of interfaces: the -command-line. - -The most often used command will be the ``balance'' command: - -@example -export LEDGER=/home/johnw/doc/ledger.dat -ledger balance -@end example - -Here I've set my Ledger environment variable to point to where my -ledger file is hiding. Thereafter, I needn't specify it again. - -The balance command prints out the summarized balances of all my -top-level accounts, excluding sub-accounts. In order to see the -balances for a specific account, just specify a regular expression -after the balance command: - -@example -ledger balance expenses:food -@end example - -This will show all the money that's been spent on food, since the -beginning of the ledger. For food spending just this month -(September), use: - -@example -ledger -d sep balance expenses:food -@end example - -Or maybe I want to see all of my assets, in which case the -s (show -sub-accounts) option comes in handy: - -@example -ledger balance -s -@end example - -To exclude a particular account, use a regular expression with a -leading minus sign. The following will show all expenses, but without -food spending: - -@example -ledger balance expenses -food -@end example - -If you want to show all accounts but for one account, remember to use -``--'' to separate the exclusion pattern from the options list: - -@example -ledger balance -- -equity -@end example - -@menu -* File format:: -* Command summary:: -* Option summary:: -* Environment variables:: -@end menu +@end smallexample -@node File format, Command summary, Running Ledger, Running Ledger -@section File format - -The ledger file format is quite simple, but supports many options. -These are summarized here. - -The initial character of each line determines what that line means, -and how it should be parsed. The possibilities are: - -@table @strong -@item NUMBER -A line starting with a number denotes a regular ledger entry. It -may be followed by any number of lines that beginning whitespace, to -denote account transactions. The format of the header line is: -@example -DATE [*] [(CODE)] DESC -@end example - -@item + -If a line begins with plus, it denotes an inclusion regexp that -will always be considered, as if it had been specified by the user -at the end of their command-line. - -@item @strong{-} -If a line begins with minus, it denotes an exclusion regexp that -will always be considered, as if it had been specified by the user -at the end of their command-line. - -@item @strong{=} -If a line begins with equals, it denotes an automated transaction. -The next item on the line must be a regular expression. Any number -of such lines may appear, with no intervening whitespace. -Following this block of lines can be a list of account transactions -preceded by whitespace. - -@item !WORD -A line beginning with an exclamation mark denotes a command -directive. It must be immediately followed by a word specifying -which directories. At the moment, only @samp{!include} is supported, for -including the content of other ledger files into the current one. - -@item whitespace -A line beginning with whitespace, which is not part of a regular or -automated transaction, is ignored. - -@item ; -If a line begins with semicolon it is ignored. This is the -preferred method of entering comments. - -@item Y NUM -If a line begins with a capital Y, it denotes the year to be used -for all subsequent entries that specify a date, whatever their type. -This sets the ``default year'', which ordinarily is the current year -at the time the program is run. Useful at the beginning of a file -to specify the file's year. - -@item P DATE SYMBOL PRICE -Capital P specifies a historical price for a commodity. Any such -number of entries are allowed. These are usually found in a pricing -history file (see the @samp{-Q} option). - -@item C SYMBOL PRICE -Capital C specifies a conversion price for a commodity. This has -no reference to time, and always takes precedence over any -historical price (even very current prices). - -@item N SYMBOL -Capital N indicates that no implicit price conversions should be -obtained for the given symbol. This means that no quotes will ever -be downloaded for that symbol. Useful for a home currency, such as -the dollar ($). Be aware that these pricing options will set the -default reporting characteristics for a commodity. Thus it is -recommended that pricing options occur only after all regular ledger -entries have been parsed. - -@item i DATE TIME ACCOUNT [DESC] -Lowercase (and capital) i indicate an time-in event. This will -start accumulating hours in the account specified. Usually these -entries are created in a timelog file by the timeclock program, -which is distributed with ledger. There must be two spaces between -the account name, and the optional description, if one is used. - -@item o DATE TIME ACCOUNT [DESC] -Lowercase (and capital) o indicate an time-out event. This will -accumulate hours in the account specified. Usually these entries -are created in a timelog file by the timeclock program, which is -distributed with ledger. There must be two spaces between the -account name, and the optional description, if one is used. - -@item b, h -Entries beginning with lowercase b and h are ignored. These are -special entries used by timeclock, but ignored by ledger. - -@end table - -@node Command summary, Option summary, File format, Running Ledger -@section Command summary - -@menu -* balance:: -* register:: -* print:: -* equity:: -* price:: -* entry:: -@end menu - -@node balance, register, Command summary, Command summary -@subsection balance - -The ``balance'' command reports the current balance of any account. -This command accepts a list of optional regexps, which will confine -the balance report to only matching accounts. By default, the -balances for all accounts will be printed. If an account contains -multiple types of commodities, each commodity's total is separately -reported. - -@node register, print, balance, Command summary -@subsection register +@node Extending with Python, , Keeping a ledger, Top +@chapter Extending with Python -The ``register'' command displays all the transactions occurring in a -single account, line by line. The account regexp must be specified as -the only argument to this command. If any regexps occur after the -required account name, the register will contain only those -transactions that match. Very useful for hunting down a particular -transaction. - -The output from ``register'' is very close to what a typical checkbook, -or single account ledger, would look like. It also shows a running -balance. The final running balance of any register should always be -the same as the current balance of that account. - -@node print, equity, register, Command summary -@subsection print - -The ``print'' command prints out ledger entries just as they appear in -the original ledger. They will be properly formatted, and output in -the most economic form possible. The ``print'' command also takes a -list of optional regexps, which will cause only those transactions -which match in some way to be printed. +Ledger fully supports Python as an extension language. It may be used +in a few different forms, which fall into three basic categories: -The ``print'' command is a handy way to clean up a ledger file whose -formatting has gotten out of hand. - -@node equity, price, print, Command summary -@subsection equity +@enumerate +@item +Defining Python functions to use in value expressions +@item +Using the ledger library as a Python module +@item +Setting up custom initialization using Python +@end enumerate -Equity transactions are used to establish the starting value of an -account. You might think of equity as the ``ether'' from which initial -balances appear. +Note that this feature, while functional, is still under development. +It will not be documented until it has been fully proven, probably in +the next version of ledger. For now, if you wish to make this of this +functionality and are willing to debug problems that come up, pass the +option @samp{--enable-python} to configure, and contact the author via +email. -@node price, entry, equity, Command summary -@subsection price +One example of using Python to create a more complex report is in the +script file @file{scripts/trend}. -This commands displays the last known current price for a given -commodity, using the specified end date for the cutoff (default is the -present moment). It takes a list of regexps, which can match the -commodities used in the ledger file. This command is helpful to -quickly seeing the last current price for a specific commodity, or all -commodities referenced by a ledger. - -@node entry, , price, Command summary -@subsection entry - -The three most laborious tasks of keeping a ledger are: adding new -entries, reconciling accounts, and generating reports. To address the -first of these, there is a sub-command to ledger called ``entry''. It -works on the principle that 80% of all transactions are variants of -earlier transactions. Here's how it works: - -Let's say you have an old transaction of the following form: - -@example -2004/03/15 * Viva Italiano - Expenses:Food $12.45 - Expenses:Tips $2.55 - Liabilities:MasterCard $-15.00 -@end example - -Now it's 2004/4/9, and you've just eating at Viva Italiano again. The -exact amounts are different, but the overall form is the same. With -the ``entry'' command you can type: - -@example -ledger entry 2004/4/9 viva food 11.00 tips 2.50 -@end example - -This will produce the following output: - -@example -2004/04/09 Viva Italiano - Expenses:Food $11.00 - Expenses:Tips $2.50 - Liabilities:MasterCard $-13.50 -@end example - -This works by finding a transaction that matches the regexp ``viva'', -and then assuming that any accounts or amounts you specify will be the -same as that earlier transaction. If Ledger does not succeed in -generating a new entry for you, it will print an error and set the -exit code to 1. - -There is a shell script in the distribution called ``entry'', which -simplifies the task of adding a new entry to your ledger, and then -launches @samp{vi} to let you confirm that the entry looks appropriate. - -@node Option summary, Environment variables, Command summary, Running Ledger -@section Option summary - -@menu -* Basic options:: -* Filtering options:: -* Output formatting options:: -* Commodity reporting options:: -@end menu - -@node Basic options, Filtering options, Option summary, Option summary -@subsection Basic options - -@table @strong -@item @strong{-v} -Display the version of ledger being used. - -@item @strong{-h} -Print out quick help on the various options and commands. - -@item @strong{-f FILE[=ACCOUNT]} -Read ledger entries from FILE. This takes precedence over the -environment variable LEDGER. If ``=ACCOUNT'' is -appended to the filename, then all of the entries are seen as if the -transactions accounts were prefixed by ``ACCOUNT:''. There may be -multiple occurrences of the @samp{-f} option. - -@item @strong{-i FILE} -Read in the list of patterns to include/exclude from FILE. -Ordinarily, these are specified as arguments after the command. - -@end table - -@node Filtering options, Output formatting options, Basic options, Option summary -@subsection Filtering options - -@table @strong -@item @strong{-b DATE} -Only consider entries occuring on or after the given date. - -@item @strong{-e DATE} -Only consider entries occuring before the given date. The date is -not inclusive, so any entries occurring on that date will not be -used. - -@item @strong{-c} -Only consider entries occurring on or before the current date. - -@item @strong{-d DATE} -Only consider entries fitting the given date mask. DATE in this -case may be the name of a month, or a year, or a year and month, -such as ``2004/05''. It's a shorthand for having to specify -b and -e -together. - -@item @strong{-C} -Only consider entries whose cleared flag has been set. The default -is to consider both. - -@item @strong{-U} -Show only uncleared transactions. The default is to consider both. - -@item @strong{-l AMT} -Limit balance reports to those which are greater than AMT. - -@item @strong{-N REGEXP} -If an account matches REGEXP, only display it in the balance report -if its total is negative. Useful to avoid seeing credit in accounts -where one cannot spend that credit, and it will soon become negative -anyway (such as credit cards). - -@item @strong{-R} -Ignore all virtual transactions, and report only the real balance -for each account. - -@end table - -@node Output formatting options, Commodity reporting options, Filtering options, Option summary -@subsection Output formatting options - -@table @strong -@item @strong{-n} -Do not show subtotals in the balance report, or split transactions -in the register report. - -@item @strong{-s} -If an account has children, show them in the balance report. - -@item @strong{-S} -Sort the ledger after reading it. This may affect ``register'' and -``print'' output. - -@item @strong{-E} -Also show empty accounts in the balance totals report. - -@item @strong{-F} -Print full account names in all cases, such as ``Assets:Checking'' -instead of just ``Checking''. Only used current by the ``balance'' -command. - -@item @strong{-M} -When used with the ``register'' command, causes only monthly subtotals -to appear. This can be useful for looking at spending patterns. -TODO: Accept an argument which specifies the period to use. - -@item @strong{-G} -Modifies the output generated by -M to be friendly to programs like -Gnuplot. It strips away the commodity label, and outputs only two -columns: the date and the amount. - -@end table - -@node Commodity reporting options, , Output formatting options, Option summary -@subsection Commodity reporting options - -@table @strong -@item @strong{-P FILE} -With this option, or if the environment variable @samp{PRICE_HIST} is -set, pricing information obtained from the Internet will be kept -in this file. Also, this file will be read after all other ledger -files are read, so that full history information is available for -reports. - -@item @strong{-T} -Report commodity totals only, not their market value or basis cost. - -@item @strong{-V} -Report commodity values in terms of their last known market price. - -@item @strong{-B} -Report commodities in terms of their ``basis cost'', or what they cost -at time of purchase. Thus, totals in the register and balance -report reflect the total amount spent. - -@item @strong{-A} -Report commodities in terms of their net gain, which is: the market -value minus the cost basis. A balance report using this option -shows very quickly the performance of investments. - -@item @strong{-Q} -When needed (see the @samp{-L} option) pricing quotes are obtained by -calling the script @samp{getquote} (a sample Perl script is provided, but -the interface is kept simple so replacements may be made). - -@item @strong{-L MINS} -When using the @samp{-Q} flag, new quotes are obtained only if current -pricing data is older than MINS minutes. The default is one day, -or 1440 minutes. - -@item @strong{-p ARG} -If a string, such as ``COMM=$1.20'', the commodity COMM will be -reported only in terms of the conversion factor, which supersedes -all other pricing histories for that commodity. This can be used to -perform arbitrary value substitutions. For example, to report the -value of your dollars in terms of the ounces of gold they would buy, -use: -p ``$=0.00280112 AU'' (or whatever the current exchange rate -is). - -@end table - -@node Environment variables, , Option summary, Running Ledger -@section Environment variables - -@table @strong -@item @samp{LEDGER} -A colon-separated list of files to be parsed whenever ledger is run. -Easier than typing @samp{-f} all the time. - -@item @samp{PRICE_HIST} -The ledger file used to hold pricing data. @samp{~/.pricedb} would be a -good choice. - -@item @samp{PRICE_EXP} -The number of minutes before pricing data becomes out-of-date. The -default is one day. Use @samp{-L} to temporarily decrease or increase -the value. - -@end table - - -@c Page published by Emacs Muse ends here -@contents @bye diff --git a/main.cc b/main.cc new file mode 100644 index 00000000..c70fcff9 --- /dev/null +++ b/main.cc @@ -0,0 +1,424 @@ +#include <ledger.h> +#include "acconf.h" +#include "debug.h" +#ifdef USE_BOOST_PYTHON +#include "py_eval.h" +#endif + +using namespace ledger; + +#include <iostream> +#include <fstream> +#include <sstream> +#include <memory> +#include <algorithm> +#include <exception> +#include <iterator> +#include <string> +#include <cstdlib> +#include <cstring> +#include <ctime> + +#if !defined(DEBUG_LEVEL) || DEBUG_LEVEL <= RELEASE + +#define auto_ptr bogus_auto_ptr + +// This version of auto_ptr does not delete on deconstruction. +namespace std { + template <typename T> + struct bogus_auto_ptr { + T * ptr; + bogus_auto_ptr() : ptr(NULL) {} + explicit bogus_auto_ptr(T * _ptr) : ptr(_ptr) {} + T& operator*() const throw() { + return *ptr; + } + T * operator->() const throw() { + return ptr; + } + T * get() const throw() { return ptr; } + T * release() throw() { + T * tmp = ptr; + ptr = 0; + return tmp; + } + void reset(T * p = 0) throw() { + if (p != ptr) { + delete ptr; + ptr = p; + } + } + }; +} + +#endif + +item_handler<transaction_t> * +chain_xact_handlers(const std::string& command, + item_handler<transaction_t> * base_formatter, + journal_t * journal, + account_t * master, + std::list<item_handler<transaction_t> *>& ptrs) +{ + item_handler<transaction_t> * formatter = NULL; + + ptrs.push_back(formatter = base_formatter); + + // format_transactions write each transaction received to the + // output stream. + if (! (command == "b" || command == "E")) { + // filter_transactions will only pass through transactions + // matching the `display_predicate'. + if (! config.display_predicate.empty()) + ptrs.push_back(formatter = + new filter_transactions(formatter, + config.display_predicate)); + + // calc_transactions computes the running total. When this + // appears will determine, for example, whether filtered + // transactions are included or excluded from the running total. + ptrs.push_back(formatter = new calc_transactions(formatter)); + + // sort_transactions will sort all the transactions it sees, based + // on the `sort_order' value expression. + if (! config.sort_string.empty()) + ptrs.push_back(formatter = + new sort_transactions(formatter, config.sort_string)); + + // changed_value_transactions adds virtual transactions to the + // list to account for changes in market value of commodities, + // which otherwise would affect the running total unpredictably. + if (config.show_revalued) + ptrs.push_back(formatter = + new changed_value_transactions(formatter, + config.show_revalued_only)); + + // collapse_transactions causes entries with multiple transactions + // to appear as entries with a subtotaled transaction for each + // commodity used. + if (config.show_collapsed) + ptrs.push_back(formatter = new collapse_transactions(formatter)); + } + + // subtotal_transactions combines all the transactions it receives + // into one subtotal entry, which has one transaction for each + // commodity in each account. + // + // period_transactions is like subtotal_transactions, but it + // subtotals according to time periods rather than totalling + // everything. + // + // dow_transactions is like period_transactions, except that it + // reports all the transactions that fall on each subsequent day + // of the week. + if (config.show_subtotal && ! (command == "b" || command == "E")) + ptrs.push_back(formatter = new subtotal_transactions(formatter)); + + if (config.days_of_the_week) + ptrs.push_back(formatter = new dow_transactions(formatter)); + else if (config.by_payee) + ptrs.push_back(formatter = new by_payee_transactions(formatter)); + + if (! config.report_period.empty()) { + ptrs.push_back(formatter = + new interval_transactions(formatter, + config.report_period, + config.report_period_sort)); + ptrs.push_back(formatter = new sort_transactions(formatter, "d")); + } + + // invert_transactions inverts the value of the transactions it + // receives. + if (config.show_inverted) + ptrs.push_back(formatter = new invert_transactions(formatter)); + + // related_transactions will pass along all transactions related + // to the transaction received. If `show_all_related' is true, + // then all the entry's transactions are passed; meaning that if + // one transaction of an entry is to be printed, all the + // transaction for that entry will be printed. + if (config.show_related) + ptrs.push_back(formatter = + new related_transactions(formatter, + config.show_all_related)); + + // This filter_transactions will only pass through transactions + // matching the `predicate'. + if (! config.predicate.empty()) + ptrs.push_back(formatter = new filter_transactions(formatter, + config.predicate)); + + // budget_transactions takes a set of transactions from a data + // file and uses them to generate "budget transactions" which + // balance against the reported transactions. + // + // forecast_transactions is a lot like budget_transactions, except + // that it adds entries only for the future, and does not balance + // them against anything but the future balance. + + if (config.budget_flags) { + budget_transactions * handler + = new budget_transactions(formatter, config.budget_flags); + handler->add_period_entries(journal->period_entries); + ptrs.push_back(formatter = handler); + } + else if (! config.forecast_limit.empty()) { + forecast_transactions * handler + = new forecast_transactions(formatter, config.forecast_limit); + handler->add_period_entries(journal->period_entries); + ptrs.push_back(formatter = handler); + } + + if (config.comm_as_payee) + ptrs.push_back(formatter = new set_comm_as_payee(formatter)); + + return formatter; +} + +int parse_and_report(int argc, char * argv[], char * envp[]) +{ + std::auto_ptr<journal_t> journal(new journal_t); + + // Parse command-line arguments, and those set in the environment + + std::list<std::string> args; + process_arguments(config_options, argc - 1, argv + 1, false, args); + + if (args.empty()) { + option_help(std::cerr); + return 1; + } + strings_list::iterator arg = args.begin(); + + config.use_cache = config.data_file.empty() && config.price_db.empty(); + DEBUG_PRINT("ledger.config.cache", "1. use_cache = " << config.use_cache); + + process_environment(config_options, envp, "LEDGER_"); + +#if 1 + // These are here for backwards compatability, but are deprecated. + + if (const char * p = std::getenv("LEDGER")) + process_option(config_options, "file", p); + if (const char * p = std::getenv("LEDGER_INIT")) + process_option(config_options, "init-file", p); + if (const char * p = std::getenv("PRICE_HIST")) + process_option(config_options, "price-db", p); + if (const char * p = std::getenv("PRICE_EXP")) + process_option(config_options, "price-exp", p); +#endif + + const char * p = std::getenv("HOME"); + std::string home = p ? p : ""; + + if (config.init_file.empty()) + config.init_file = home + "/.ledgerrc"; + if (config.cache_file.empty()) + config.cache_file = home + "/.ledger-cache"; + if (config.price_db.empty()) + config.price_db = home + "/.pricedb"; + + if (config.data_file == config.cache_file) + config.use_cache = false; + DEBUG_PRINT("ledger.config.cache", "2. use_cache = " << config.use_cache); + + // Read the command word, canonicalize it to its one letter form, + // then configure the system based on the kind of report to be + // generated + + std::string command = *arg++; + + if (command == "balance" || command == "bal" || command == "b") + command = "b"; + else if (command == "register" || command == "reg" || command == "r") + command = "r"; + else if (command == "print" || command == "p") + command = "p"; + else if (command == "xml") + command = "X"; + else if (command == "entry") + command = "e"; + else if (command == "equity") + command = "E"; + else if (command == "prices") + command = "P"; + else + throw error(std::string("Unrecognized command '") + command + "'"); + + // Parse initialization files, ledger data, price database, etc. + + std::auto_ptr<binary_parser_t> bin_parser(new binary_parser_t); +#ifdef HAVE_XMLPARSE + std::auto_ptr<xml_parser_t> xml_parser(new xml_parser_t); + std::auto_ptr<gnucash_parser_t> gnucash_parser(new gnucash_parser_t); +#endif + std::auto_ptr<qif_parser_t> qif_parser(new qif_parser_t); + std::auto_ptr<textual_parser_t> text_parser(new textual_parser_t); + + register_parser(bin_parser.get()); +#ifdef HAVE_XMLPARSE + register_parser(xml_parser.get()); + register_parser(gnucash_parser.get()); +#endif + register_parser(qif_parser.get()); + register_parser(text_parser.get()); + + parse_ledger_data(journal.get(), bin_parser.get(), text_parser.get() +#ifdef HAVE_XMLPARSE + , xml_parser.get() +#endif + ); + + // process the command word and its following arguments + + config.process_options(command, arg, args.end()); + + std::auto_ptr<entry_t> new_entry; + if (command == "e") { + new_entry.reset(derive_new_entry(*journal, arg, args.end())); + if (! new_entry.get()) + return 1; + } + + // Configure the output stream + + std::ostream * out = &std::cout; + if (! config.output_file.empty()) + out = new std::ofstream(config.output_file.c_str()); + + // Compile the format strings + + const std::string * format; + if (! config.format_string.empty()) + format = &config.format_string; + else if (command == "b") + format = &config.balance_format; + else if (command == "r") + format = &config.register_format; + else if (command == "E") + format = &config.equity_format; + else if (command == "P") + format = &config.prices_format; + else + format = &config.print_format; + +#ifdef USE_BOOST_PYTHON + + // If Python support is compiled, we can easily report minimum and + // maximum values for each commodity. There is a line in config.cc + // which configures the prices report to call these two functions, + // if Python is available. + + if (command == "P") + python_eval("\ +min_val = 0\n\ +def vmin(d, val):\n\ + global min_val\n\ + if not min_val or val < min_val:\n\ + min_val = val\n\ + return val\n\ + return min_val\n\ +\n\ +max_val = 0\n\ +def vmax(d, val):\n\ + global max_val\n\ + if not max_val or val > max_val:\n\ + max_val = val\n\ + return val\n\ + return max_val\n", PY_EVAL_MULTI); + +#endif // USE_BOOST_PYTHON + + // Walk the entries based on the report type and the options + + item_handler<transaction_t> * formatter; + std::list<item_handler<transaction_t> *> formatter_ptrs; + + if (command == "b" || command == "E") + formatter = new set_account_value; + else if (command == "p" || command == "e") + formatter = new format_entries(*out, *format); + else if (command == "X") + formatter = new format_xml_entries(*out, config.show_totals); + else + formatter = new format_transactions(*out, *format); + + formatter = chain_xact_handlers(command, formatter, journal.get(), + journal->master, formatter_ptrs); + + if (command == "e") + walk_transactions(new_entry->transactions, *formatter); + else if (command == "P") + walk_commodities(commodity_t::commodities, *formatter); + else + walk_entries(journal->entries, *formatter); + + if (command != "P") + formatter->flush(); + + // For the balance and equity reports, output the sum totals. + + if (command == "b") { + format_account acct_formatter(*out, *format, config.display_predicate); + sum_accounts(*journal->master); + walk_accounts(*journal->master, acct_formatter, config.sort_string); + acct_formatter.flush(); + + if (account_has_xdata(*journal->master)) { + account_xdata_t& xdata = account_xdata(*journal->master); + if (! config.show_collapsed && xdata.total) { + *out << "--------------------\n"; + xdata.value = xdata.total; + acct_formatter.format.format(*out, details_t(*journal->master)); + } + } + } + else if (command == "E") { + format_equity acct_formatter(*out, *format, config.display_predicate); + sum_accounts(*journal->master); + walk_accounts(*journal->master, acct_formatter, config.sort_string); + acct_formatter.flush(); + } + +#if DEBUG_LEVEL >= BETA + + clear_transactions_xdata(); + clear_accounts_xdata(); + + if (! config.output_file.empty()) + delete out; + + for (std::list<item_handler<transaction_t> *>::iterator i + = formatter_ptrs.begin(); + i != formatter_ptrs.end(); + i++) + delete *i; + +#endif // DEBUG_LEVEL >= BETA + + // Write out the binary cache, if need be + + if (config.use_cache && config.cache_dirty && + ! config.cache_file.empty()) { + std::ofstream stream(config.cache_file.c_str()); + write_binary_journal(stream, journal.get(), &journal->sources); + } + + return 0; +} + +int main(int argc, char * argv[], char * envp[]) +{ + try { + return parse_and_report(argc, argv, envp); + } + catch (const std::exception& err) { + std::cerr << "Error: " << err.what() << std::endl; + return 1; + } + catch (int& val) { + return val; // this acts like a std::setjmp + } +} + +// main.cc ends here. diff --git a/main.py b/main.py new file mode 100644 index 00000000..7b150821 --- /dev/null +++ b/main.py @@ -0,0 +1,479 @@ +#!/usr/bin/env python + +# Ledger, the command-line accounting tool +# +# Copyright (c) 2003-2004, New Artisans LLC. All rights reserved. +# +# This program is made available under the terms of the BSD Public +# License. See the LICENSE file included with the distribution for +# details and disclaimer. +# +# This script provides a Python front-end to the ledger library, and +# replicates the functionality of the C++ front-end, main.cc. It is +# provided as an example, and as a starting point for creating custom +# front-ends based on the Ledger module. See the documentation for an +# API reference, and how to use this module. + +import os +import sys +import string +import time + +true, false = 1, 0 + +from ledger import * + +# Create the main journal object, into which all entries will be +# recorded. Once done, the 'journal' may be iterated to yield those +# entries, in the same order as which they appeared in the journal +# file. + +journal = Journal () + +# This call registers all of the default command-line options that +# Ledger supports into the option handling mechanism. Skip this call +# if you wish to do all of your own processing -- in which case simply +# modify the 'config' object however you like. + +add_config_option_handlers () + +output_for_emacs = false +def handle_emacs_option (arg): + global output_for_emacs + output_for_emacs = true +add_option_handler ("emacs", "", handle_emacs_option) + +averages = {} +compute_monthly_avg = false + +def get_index (xact): + return time.strftime ("%Y/%m", time.localtime (xact.entry.date)) + +class ComputeMonthlyAvg (TransactionHandler): + def __call__ (self, xact): + global averages + index = get_index (xact) + if not averages.has_key(index): + averages[index] = [Value (), 0] + add_transaction_to (xact, averages[index][0]) + averages[index][1] += 1 + TransactionHandler.__call__ (self, xact) + +def monthly_avg (details): + index = get_index (xact) + return averages[index][0] / averages[index][1] + +def show_monthly_averages (arg): + global compute_monthly_avg + compute_monthly_avg = true + config.report_period = "monthly"; + config.total_expr = "@monthly_avg()" + +add_option_handler ("monthly-avg", "", show_monthly_averages) + +# Process the command-line arguments, test whether caching should be +# enabled, and then process any option settings from the execution +# environment. Some historical environment variable names are also +# supported. + +args = process_arguments (sys.argv[1:]) +config.use_cache = not config.data_file +process_environment (os.environ, "LEDGER_") + +if os.environ.has_key ("LEDGER"): + process_option ("file", os.getenv ("LEDGER")) +if os.environ.has_key ("PRICE_HIST"): + process_option ("price-db", os.getenv ("PRICE_HIST")) +if os.environ.has_key ("PRICE_EXP"): + process_option ("price-exp", os.getenv ("PRICE_EXP")) + +# If no argument remain, then no command word was given. Report the +# default help text and exit. + +if len (args) == 0: + option_help () + sys.exit (0) + +# The command word is in the first argument. Canonicalize it to a +# unique, simple form that the remaining code can use to find out +# which command was specified. + +command = args.pop (0); + +if command == "balance" or command == "bal" or command == "b": + command = "b" +elif command == "register" or command == "reg" or command == "r": + command = "r" +elif command == "print" or command == "p": + command = "p" +elif command == "entry": + command = "e" +elif command == "equity": + command = "E" +elif command == "prices": + command = "P" +else: + print "Unrecognized command:", command + sys.exit (1) + +# Create all the parser objects to be used. They are all registered, +# so that Ledger will try each one in turn whenever it is presented +# with a data file. They are attempted in reverse order to their +# registry. Note that Gnucash parsing is only available if the Ledger +# module was built with such support (which requires the xmlparse C +# library). + +text_parser = TextualParser () +bin_parser = BinaryParser () +qif_parser = QifParser () +gnucash_parser = None +try: + gnucash_parser = GnucashParser () +except: + pass + +register_parser (text_parser) +register_parser (bin_parser) +if gnucash_parser: + register_parser (gnucash_parser) +register_parser (qif_parser) + +# Parse all entries from the user specified locations (found in +# 'config') into the journal object we created. The two parsers given +# as explicit arguments indicate: the parser to be used for standard +# input, and the parser to be used for cache files. + +parse_ledger_data (journal, bin_parser) + +# Now that everything has been correctly parsed (parse_ledger_data +# would have thrown an exception if not), we can take time to further +# process the configuration options. This changes the configuration a +# bit based on previous option settings, the command word, and the +# remaining arguments. + +config.process_options (command, args); + +# If the command is "e", use the method journal.derive_entry to create +# a brand new entry based on the arguments given. + +new_entry = None +if command == "e": + new_entry = derive_new_entry (journal, args) + if new_entry is None: + sys.exit (1) + +# Determine the format string to used, based on the command. + +if config.format_string: + format = config.format_string +elif command == "b": + format = config.balance_format +elif command == "r": + format = config.register_format +elif command == "E": + format = config.equity_format +elif command == "P": + min_val = 0 + def vmin(d, val): + global min_val + if not min_val or val < min_val: + min_val = val + return val + return min_val + + max_val = 0 + def vmax(d, val): + global max_val + if not max_val or val > max_val: + max_val = val + return val + return max_val + + format = config.prices_format +else: + format = config.print_format + +# The following two classes are responsible for outputing transactions +# and accounts to the user. There are corresponding C++ versions to +# these, but they rely on I/O streams, which Boost.Python does not +# provide a conversion layer for. + +class FormatTransactions (TransactionHandler): + last_entry = None + output = None + + def __init__ (self, fmt): + try: + i = string.index (fmt, '%/') + self.formatter = Format (fmt[: i]) + self.nformatter = Format (fmt[i + 2 :]) + except ValueError: + self.formatter = Format (fmt) + self.nformatter = None + + self.last_entry = None + + if config.output_file: + self.output = open (config.output_file, "w") + else: + self.output = sys.stdout + + TransactionHandler.__init__ (self) + + def __del__ (self): + if config.output_file: + self.output.close () + + def flush (self): + self.output.flush () + + def __call__ (self, xact): + if not transaction_has_xdata (xact) or \ + not transaction_xdata (xact).dflags & TRANSACTION_DISPLAYED: + if self.nformatter is not None and \ + self.last_entry is not None and \ + xact.entry == self.last_entry: + self.output.write (self.nformatter.format (xact)) + else: + self.output.write (self.formatter.format (xact)) + self.last_entry = xact.entry + transaction_xdata (xact).dflags |= TRANSACTION_DISPLAYED + +class FormatEntries (FormatTransactions): + def __init__ (self, fmt): + self.last_entry = None + FormatTransactions.__init__(self, fmt) + + def flush (self): + self.format_last_entry () + self.last_entry = None + FormatTransactions.flush (self) + + def format_last_entry (self): + first = true + for x in self.last_entry: + if transaction_has_xdata (x) and \ + transaction_xdata (x).dflags & TRANSACTION_TO_DISPLAY: + if first or self.nformatter is None: + self.output.write (self.formatter.format (x)) + first = false + else: + self.output.write (self.nformatter.format (x)) + transaction_xdata (x).dflags |= TRANSACTION_TO_DISPLAY + + def __call__ (self, xact): + if self.last_entry and self.last_entry != xact.entry: + self.format_last_entry () + + transaction_xdata (xact).dflags |= TRANSACTION_TO_DISPLAY + + self.last_entry = xact.entry; + +class FormatAccounts (AccountHandler): + output = None + + def __init__ (self, fmt, pred): + self.formatter = Format (fmt) + self.predicate = AccountPredicate (pred) + + if config.output_file: + self.output = open (config.output_file, "w") + else: + self.output = sys.stdout + + AccountHandler.__init__ (self) + + def __del__ (self): + if config.output_file: + self.output.close () + + def final (self, account): + if account_has_xdata (account): + xdata = account_xdata (account) + if xdata.dflags & ACCOUNT_TO_DISPLAY: + print "--------------------" + xdata.value = xdata.total + self.output.write (self.formatter.format (account)) + + def flush (self): + self.output.flush () + + def __call__ (self, account): + if display_account (account, self.predicate): + if not account.parent: + account_xdata (account).dflags |= ACCOUNT_TO_DISPLAY + else: + self.output.write (self.formatter.format (account)) + account_xdata (account).dflags |= ACCOUNT_DISPLAYED + +class FormatEquity (AccountHandler): + output = None + + def __init__ (self, fmt, pred): + try: + i = string.index (fmt, '%/') + self.formatter = Format (fmt[: i]) + self.nformatter = Format (fmt[i + 2 :]) + except ValueError: + self.formatter = Format (fmt) + self.nformatter = None + + self.predicate = AccountPredicate (pred) + self.total = Value () + + if config.output_file: + self.output = open (config.output_file, "w") + else: + self.output = sys.stdout + + AccountHandler.__init__ (self) + + header_entry = Entry () + header_entry.payee = "Opening Balances" + header_entry.date = int (time.time ()) + self.output.write (self.formatter.format (header_entry)) + + def __del__ (self): + if config.output_file: + self.output.close () + + def flush (self): + summary = Account (Account (), "Equity:Opening Balances") + account_xdata (summary).value = - self.total + self.output.write (self.nformatter.format (summary)) + self.output.flush () + + def __call__ (self, account): + if display_account (account, self.predicate): + self.output.write (self.nformatter.format (account)) + if account_has_xdata (account): + self.total += account_xdata (account).value + account_xdata (account).dflags |= ACCOUNT_DISPLAYED + +# Set the final transaction handler: for balances and equity reports, +# it will simply add the value of the transaction to the account's +# xdata, which is used a bit later to report those totals. For all +# other reports, the transaction data is sent to the configured output +# location (default is sys.stdout). + +if command == "b" or command == "E": + handler = SetAccountValue () +elif command == "p" or command == "e": + handler = FormatEntries (format) +elif output_for_emacs: + import emacs + handler = emacs.EmacsFormatTransactions () +else: + handler = FormatTransactions (format) + +# Chain transaction filters on top of the base handler. Most of these +# filters customize the output for reporting. None of this is done +# for balance or equity reports, which don't need it. + +if not (command == "b" or command == "E"): + if config.display_predicate: + handler = FilterTransactions (handler, config.display_predicate) + + handler = CalcTransactions (handler) + + if config.sort_string: + handler = SortTransactions (handler, config.sort_string) + + if config.show_revalued: + handler = ChangedValueTransactions (handler, config.show_revalued_only) + + if config.show_collapsed: + handler = CollapseTransactions (handler); + +if config.show_subtotal and not (command == "b" or command == "E"): + handler = SubtotalTransactions (handler) + +if config.days_of_the_week: + handler = DowTransactions (handler) +elif config.by_payee: + handler = ByPayeeTransactions (handler) + +if config.report_period: + handler = IntervalTransactions (handler, config.report_period, + config.report_period_sort) + handler = SortTransactions (handler, "d") + +if compute_monthly_avg: + handler = ComputeMonthlyAvg (handler) + +# The next set of transaction filters are used by all reports. + +if config.show_inverted: + handler = InvertTransactions (handler) + +if config.show_related: + handler = RelatedTransactions (handler, config.show_all_related) + +if config.predicate: + handler = FilterTransactions (handler, config.predicate) + +if config.budget_flags: + handler = BudgetTransactions (handler, config.budget_flags) + handler.add_period_entries (journal) +elif config.forecast_limit: + handler = ForecastTransactions (handler, config.forecast_limit) + handler.add_period_entries (journal) + +if config.comm_as_payee: + handler = SetCommAsPayee (handler) + +# Walk the journal's entries, and pass each entry's transaction to the +# handler chain established above. And although a journal's entries +# can be walked using Python, it is significantly faster to do this +# simple walk in C++, using `walk_entries'. +# +# if command == "e": +# for xact in new_entry: +# handler (xact) +# else: +# for entry in journal: +# for xact in entry: +# handler (xact) + +if command == "e": + walk_transactions (new_entry, handler) +elif command == "P": + walk_commodities (handler) +else: + walk_entries (journal, handler) + +# Flush the handlers, causing them to output whatever data is still +# pending. + +handler.flush () + +# For the balance and equity reports, the account totals now need to +# be displayed. This is different from outputting transactions, in +# that we are now outputting account totals to display a summary of +# the transactions that were just walked. + +if command == "b": + acct_formatter = FormatAccounts (format, config.display_predicate) + sum_accounts (journal.master) + walk_accounts (journal.master, acct_formatter, config.sort_string) + acct_formatter.final (journal.master) + acct_formatter.flush () + +elif command == "E": + acct_formatter = FormatEquity (format, config.display_predicate) + sum_accounts (journal.master) + walk_accounts (journal.master, acct_formatter, config.sort_string) + acct_formatter.flush () + +# If it were important to clean things up, we would have to clear out +# the accumulated xdata at this point: + +#clear_transactions_xdata () +#clear_accounts_xdata () + +# If the cache is being used, and is dirty, update it now. + +if config.use_cache and config.cache_dirty and config.cache_file: + write_binary_journal (config.cache_file, journal); + +# We're done! diff --git a/mask.cc b/mask.cc new file mode 100644 index 00000000..a7352d12 --- /dev/null +++ b/mask.cc @@ -0,0 +1,49 @@ +#include "mask.h" +#include "debug.h" + +#include <pcre.h> + +mask_t::mask_t(const std::string& pat) : exclude(false) +{ + const char * p = pat.c_str(); + if (*p == '-') { + exclude = true; + p++; + while (std::isspace(*p)) + p++; + } + else if (*p == '+') { + p++; + while (std::isspace(*p)) + p++; + } + pattern = p; + + const char *error; + int erroffset; + regexp = pcre_compile(pattern.c_str(), PCRE_CASELESS, + &error, &erroffset, NULL); + if (! regexp) + throw mask_error(std::string("Failed to compile regexp '") + pattern + "'"); +} + +mask_t::mask_t(const mask_t& m) : exclude(m.exclude), pattern(m.pattern) +{ + const char *error; + int erroffset; + regexp = pcre_compile(pattern.c_str(), PCRE_CASELESS, + &error, &erroffset, NULL); + assert(regexp); +} + +mask_t::~mask_t() { + pcre_free((pcre *)regexp); +} + +bool mask_t::match(const std::string& str) const +{ + static int ovec[30]; + int result = pcre_exec((pcre *)regexp, NULL, + str.c_str(), str.length(), 0, 0, ovec, 30); + return result >= 0 && ! exclude; +} @@ -0,0 +1,32 @@ +#ifndef _MASK_H +#define _MASK_H + +#include <string> +#include <exception> + +class mask_t +{ + public: + bool exclude; + std::string pattern; + void * regexp; + + explicit mask_t(const std::string& pattern); + mask_t(const mask_t&); + ~mask_t(); + + bool match(const std::string& str) const; +}; + +class mask_error : public std::exception { + std::string reason; + public: + mask_error(const std::string& _reason) throw() : reason(_reason) {} + virtual ~mask_error() throw() {} + + virtual const char* what() const throw() { + return reason.c_str(); + } +}; + +#endif // _MASK_H diff --git a/option.cc b/option.cc new file mode 100644 index 00000000..d1b7dc82 --- /dev/null +++ b/option.cc @@ -0,0 +1,261 @@ +#include "option.h" +#include "debug.h" + +#include <iostream> +#include <cstdarg> + +#include "util.h" + +void add_option_handler(std::list<option_t>& options, + const std::string& label, + const std::string& opt_chars, + option_handler& option) +{ + option_t opt; + + char buf[128]; + char * p = buf; + for (const char * q = label.c_str(); + *q && p - buf < 128; + q++) + if (*q == '_') + *p++ = '-'; + else + *p++ = *q; + *p = '\0'; + opt.long_opt = buf; + + if (! opt_chars.empty()) { + if (opt_chars[0] != ':') + opt.short_opt = opt_chars[0]; + + if (opt_chars[opt_chars.length() - 1] == ':') + opt.wants_arg = true; + } + + opt.handler = &option; + + options.push_front(opt); +} + +namespace { + inline void process_option(const option_t& opt, + const char * arg = NULL) { + if (! opt.handler->handled) { + (*opt.handler)(arg); + opt.handler->handled = true; + } + } +} + +bool process_option(std::list<option_t>& options, + const std::string& opt, const char * arg) +{ + for (std::list<option_t>::iterator i = options.begin(); + i != options.end(); + i++) + if ((*i).long_opt == opt) { + if (! (*i).handler->handled) { + (*(*i).handler)(arg); + (*i).handler->handled = true; + return true; + } + break; + } + + return false; +} + +void process_arguments(std::list<option_t>& options, + int argc, char ** argv, const bool anywhere, + std::list<std::string>& args) +{ + int index = 0; + for (char ** i = argv; index < argc; i++, index++) { + if ((*i)[0] != '-') { + if (anywhere) { + args.push_back(*i); + continue; + } else { + for (; index < argc; i++, index++) + args.push_back(*i); + break; + } + } + + // --long-option + again: + if ((*i)[1] == '-') { + if ((*i)[2] == '\0') + break; + + for (std::list<option_t>::iterator j = options.begin(); + j != options.end(); + j++) + if ((*j).wants_arg) { + if (const char * p = std::strchr(*i + 2, '=')) { + if ((*j).long_opt == std::string(*i + 2, p - (*i + 2))) { + process_option(*j, p + 1); + goto next; + } + } + else if ((*j).long_opt == *i + 2) { + if (++index >= argc) + throw option_error(std::string("missing option argument for ") + + *i); + process_option(*j, argv[index]); + i++; + goto next; + } + } + else if ((*j).long_opt == *i + 2) { + process_option(*j); + goto next; + } + + throw option_error(std::string("illegal option ") + *i); + } else { + for (std::list<option_t>::iterator j = options.begin(); + j != options.end(); + j++) + if ((*i)[1] == (*j).short_opt) { + if ((*j).wants_arg) { + if (++index >= argc) + throw option_error(std::string("missing argument for option ") + + *i); + process_option(*j, argv[index]); + i++; + goto next; + } else { + process_option(*j); + if ((*i)[2]) { + std::strcpy(*i + 1, *i + 2); + goto again; + } + goto next; + } + } + + throw option_error(std::string("illegal option -- ") + (*i)[1]); + } + + next: + ; + } +} + +void process_environment(std::list<option_t>& options, + char ** envp, const std::string& tag) +{ + const char * tag_p = tag.c_str(); + int tag_len = tag.length(); + + for (char ** p = envp; *p; p++) + if (std::strncmp(*p, tag_p, tag_len) == 0) { + char buf[128]; + char * r = buf; + char * q; + for (q = *p + tag_len; + *q && *q != '=' && r - buf < 128; + q++) + if (*q == '_') + *r++ = '-'; + else + *r++ = std::tolower(*q); + *r = '\0'; + + if (*q == '=') + process_option(options, buf, q + 1); + } +} + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> +#include <boost/python/detail/api_placeholder.hpp> +#include <vector> + +using namespace boost::python; + +struct func_option_wrapper : public option_handler +{ + object self; + func_option_wrapper(object _self) : self(_self) {} + + virtual void operator()(const char * arg) { + call<void>(self.ptr(), arg); + } +}; + +namespace { + std::list<func_option_wrapper> wrappers; + std::list<option_t> options; +} + +void py_add_option_handler(const std::string& long_opt, + const std::string& short_opt, object func) +{ + wrappers.push_back(func_option_wrapper(func)); + add_option_handler(options, long_opt, short_opt, wrappers.back()); +} + +void add_other_option_handlers(const std::list<option_t>& other) +{ + options.insert(options.begin(), other.begin(), other.end()); +} + +bool py_process_option(const std::string& opt, const char * arg) +{ + return process_option(options, opt, arg); +} + +list py_process_arguments(list args, bool anywhere = false) +{ + std::vector<char *> strs; + + int l = len(args); + for (int i = 0; i < l; i++) + strs.push_back(extract<char *>(args[i])); + + std::list<std::string> newargs; + process_arguments(options, strs.size(), &strs.front(), anywhere, newargs); + + list py_newargs; + for (std::list<std::string>::iterator i = newargs.begin(); + i != newargs.end(); + i++) + py_newargs.append(*i); + return py_newargs; +} + +BOOST_PYTHON_FUNCTION_OVERLOADS(py_proc_args_overloads, + py_process_arguments, 1, 2) + +void py_process_environment(object env, const std::string& tag) +{ + std::vector<char *> strs; + std::vector<std::string> storage; + + list items = call_method<list>(env.ptr(), "items"); + int l = len(items); + for (int i = 0; i < l; i++) { + tuple pair = extract<tuple>(items[i]); + std::string s = extract<std::string>(pair[0]); + s += "="; + s += extract<std::string>(pair[1]); + storage.push_back(s); + strs.push_back(const_cast<char *>(storage.back().c_str())); + } + + process_environment(options, &strs.front(), tag); +} + +void export_option() +{ + def("add_option_handler", py_add_option_handler); + def("process_option", py_process_option); + def("process_arguments", py_process_arguments, py_proc_args_overloads()); + def("process_environment", py_process_environment); +} + +#endif // USE_BOOST_PYTHON diff --git a/option.h b/option.h new file mode 100644 index 00000000..2e4d7599 --- /dev/null +++ b/option.h @@ -0,0 +1,44 @@ +#ifndef _OPTION_H +#define _OPTION_H + +#include <list> +#include <string> +#include <exception> + +struct option_handler { + bool handled; + option_handler() : handled(false) {} + virtual void operator()(const char * arg = NULL) = 0; +}; + +struct option_t { + char short_opt; + std::string long_opt; + bool wants_arg; + option_handler * handler; + + option_t() : short_opt(0), wants_arg(false), handler(NULL) {} +}; + +class option_error : public std::exception { + std::string reason; + public: + option_error(const std::string& _reason) throw() : reason(_reason) {} + virtual ~option_error() throw() {} + + virtual const char* what() const throw() { + return reason.c_str(); + } +}; + +void add_option_handler(std::list<option_t>& options, const std::string& label, + const std::string& opt_chars, option_handler& option); +bool process_option(std::list<option_t>& options, + const std::string& opt, const char * arg = NULL); +void process_arguments(std::list<option_t>& options, + int argc, char ** argv, const bool anywhere, + std::list<std::string>& args); +void process_environment(std::list<option_t>& options, + char ** envp, const std::string& tag); + +#endif // _OPTION_H diff --git a/parse.cc b/parse.cc deleted file mode 100644 index e4850973..00000000 --- a/parse.cc +++ /dev/null @@ -1,582 +0,0 @@ -#include "ledger.h" - -#include <fstream> -#include <cstring> -#include <ctime> -#include <cctype> - -#define TIMELOG_SUPPORT 1 - -namespace ledger { - -static inline char * skip_ws(char * ptr) -{ - while (std::isspace(*ptr)) - ptr++; - return ptr; -} - -static inline char * next_element(char * buf, bool variable = false) -{ - for (char * p = buf; *p; p++) { - if (! (*p == ' ' || *p == '\t')) - continue; - - if (! variable) { - *p = '\0'; - return skip_ws(p + 1); - } - else if (*p == '\t') { - *p = '\0'; - return skip_ws(p + 1); - } - else if (*(p + 1) == ' ') { - *p = '\0'; - return skip_ws(p + 2); - } - } - return NULL; -} - -static const char *formats[] = { - "%Y-%m-%d", - "%m-%d", - "%Y/%m/%d", - "%m/%d", - "%Y.%m.%d", - "%m.%d", - "%a", - "%A", - "%b", - "%B", - "%Y", - NULL -}; - -bool parse_date_mask(const char * date_str, struct std::tm * result) -{ - for (const char ** f = formats; *f; f++) { - memset(result, INT_MAX, sizeof(struct std::tm)); - if (strptime(date_str, *f, result)) - return true; - } - return false; -} - -bool parse_date(const char * date_str, std::time_t * result, const int year) -{ - struct std::tm when; - - if (! parse_date_mask(date_str, &when)) - return false; - - static std::time_t now = std::time(NULL); - static struct std::tm * now_tm = std::localtime(&now); - - when.tm_hour = 0; - when.tm_min = 0; - when.tm_sec = 0; - - if (when.tm_year == -1) - when.tm_year = ((year == -1) ? now_tm->tm_year : (year - 1900)); - - if (when.tm_mon == -1) - when.tm_mon = now_tm->tm_mon; - - if (when.tm_mday == -1) - when.tm_mday = now_tm->tm_mday; - - *result = std::mktime(&when); - - return true; -} - -void record_price(const std::string& symbol, amount * price, - std::time_t * date = NULL) -{ - commodity * comm = NULL; - commodities_map_iterator item = main_ledger->commodities.find(symbol); - if (item == main_ledger->commodities.end()) - comm = new commodity(symbol); - else - comm = (*item).second; - - assert(comm); - comm->set_price(price, date); -} - -void parse_price_setting(const std::string& setting) -{ - char buf[128]; - std::strcpy(buf, setting.c_str()); - - assert(setting.length() < 128); - - char * c = buf; - char * p = std::strchr(buf, '='); - if (! p) { - std::cerr << "Warning: Invalid price setting: " << setting << std::endl; - } else { - *p++ = '\0'; - record_price(c, create_amount(p)); - } -} - -#define MAX_LINE 1024 - - int linenum; -static bool do_compute; -static std::string account_prefix; - -transaction * parse_transaction_text(char * line, book * ledger) -{ - transaction * xact = new transaction(); - - // The call to `next_element' will skip past the account name, - // and return a pointer to the beginning of the amount. Once - // we know where the amount is, we can strip off any - // transaction note, and parse it. - - char * p = skip_ws(line); - char * cost_str = next_element(p, true); - char * note_str; - - // If there is no amount given, it is intended as an implicit - // amount; we must use the opposite of the value of the - // preceding transaction. - - if (! cost_str || ! *cost_str || *cost_str == ';') { - if (cost_str && *cost_str) { - while (*cost_str == ';' || std::isspace(*cost_str)) - cost_str++; - xact->note = cost_str; - } - - xact->cost = NULL; - } - else { - note_str = std::strchr(cost_str, ';'); - if (note_str) { - *note_str++ = '\0'; - xact->note = skip_ws(note_str); - } - - for (char * t = cost_str + (std::strlen(cost_str) - 1); - std::isspace(*t); - t--) - *t = '\0'; - - xact->cost = create_amount(cost_str); - } - - if (*p == '[' || *p == '(') { - xact->is_virtual = true; - xact->specified = true; - xact->must_balance = *p == '['; - p++; - - char * e = p + (std::strlen(p) - 1); - assert(*e == ')' || *e == ']'); - *e = '\0'; - } - - std::string name = account_prefix + p; - xact->acct = ledger->find_account(name.c_str()); - - if (do_compute && xact->cost) - xact->acct->balance.credit(xact->cost); - - return xact; -} - -transaction * parse_transaction(std::istream& in, book * ledger) -{ - static char line[MAX_LINE + 1]; - in.getline(line, MAX_LINE); - linenum++; - - return parse_transaction_text(line, ledger); -} - -entry * parse_entry(std::istream& in, book * ledger) -{ - entry * curr = new entry(ledger); - - static char line[MAX_LINE + 1]; - in.getline(line, MAX_LINE); - linenum++; - - // Parse the date - - char * next = next_element(line); - if (! parse_date(line, &curr->date, ledger->current_year)) { - std::cerr << "Error, line " << linenum - << ": Failed to parse date: " << line << std::endl; - return NULL; - } - - // Parse the optional cleared flag: * - - if (*next == '*') { - curr->cleared = true; - next = skip_ws(++next); - } - - // Parse the optional code: (TEXT) - - if (*next == '(') { - if (char * p = std::strchr(next++, ')')) { - *p++ = '\0'; - curr->code = next; - next = skip_ws(p); - } - } - - // Parse the description text - - curr->desc = next; - - // Parse all of the transactions associated with this entry - - while (! in.eof() && (in.peek() == ' ' || in.peek() == '\t')) - if (transaction * xact = parse_transaction(in, ledger)) - curr->xacts.push_back(xact); - - // If there were no transactions, throw away the entry - - if (curr->xacts.empty() || ! curr->finalize(do_compute)) { - delete curr; - return NULL; - } - - return curr; -} - -void parse_automated_transactions(std::istream& in, book * ledger) -{ - static char line[MAX_LINE + 1]; - - regexps_list * masks = NULL; - - while (! in.eof() && in.peek() == '=') { - in.getline(line, MAX_LINE); - linenum++; - - char * p = line + 1; - p = skip_ws(p); - - if (! masks) - masks = new regexps_list; - masks->push_back(mask(p)); - } - - std::list<transaction *> * xacts = NULL; - - while (! in.eof() && (in.peek() == ' ' || in.peek() == '\t')) { - if (transaction * xact = parse_transaction(in, ledger)) { - if (! xacts) - xacts = new std::list<transaction *>; - - if (! xact->cost) { - std::cerr << "Error, line " << (linenum - 1) - << ": All automated transactions must have a value." - << std::endl; - } else { - xacts->push_back(xact); - } - } - } - - if (masks && xacts) - ledger->virtual_mapping.insert(book::virtual_map_pair(masks, xacts)); - else if (masks) - delete masks; - else if (xacts) - delete xacts; -} - -////////////////////////////////////////////////////////////////////// -// -// Ledger parser -// - -#ifdef TIMELOG_SUPPORT -static std::time_t time_in; -static account * last_account; -static std::string last_desc; -#endif - -int parse_ledger(book * ledger, std::istream& in, - regexps_list& regexps, bool compute_balances, - const char * acct_prefix) -{ - static char line[MAX_LINE + 1]; - char c; - int count = 0; - std::string old_account_prefix = account_prefix; - - linenum = 1; - do_compute = compute_balances; - if (acct_prefix) { - account_prefix += acct_prefix; - account_prefix += ":"; - } - - while (! in.eof()) { - switch (in.peek()) { - case -1: // end of file - goto done; - - case '\n': - linenum++; - case '\r': // skip blank lines - in.get(c); - break; - -#ifdef TIMELOG_SUPPORT - case 'i': - case 'I': { - std::string date, time; - - in >> c; - in >> date; - in >> time; - date += " "; - date += time; - - in.getline(line, MAX_LINE); - linenum++; - - char * p = skip_ws(line); - char * n = next_element(p, true); - last_desc = n ? n : ""; - - static struct std::tm when; - if (strptime(date.c_str(), "%Y/%m/%d %H:%M:%S", &when)) { - time_in = std::mktime(&when); - last_account = ledger->find_account(p); - } else { - std::cerr << "Error, line " << (linenum - 1) - << ": Cannot parse timelog entry date." - << std::endl; - last_account = NULL; - } - break; - } - - case 'o': - case 'O': - if (last_account) { - std::string date, time; - - in >> c; - in >> date; - in >> time; - date += " "; - date += time; - - static struct std::tm when; - if (strptime(date.c_str(), "%Y/%m/%d %H:%M:%S", &when)) { - entry * curr = new entry(ledger); - - curr->date = std::mktime(&when); - - double diff = (curr->date - time_in) / 60.0 / 60.0; - char buf[128]; - std::sprintf(buf, "%fh", diff); - - curr->cleared = true; - curr->code = ""; - curr->desc = last_desc; - - std::string xact_line = "("; - xact_line += last_account->as_str(); - xact_line += ") "; - xact_line += buf; - - std::strcpy(buf, xact_line.c_str()); - - if (transaction * xact = parse_transaction_text(buf, ledger)) { - curr->xacts.push_back(xact); - - // Make sure numbers are reported only to 1 decimal place. - commodity * cmdty = xact->cost->commdty(); - cmdty->precision = 1; - } - - ledger->entries.push_back(curr); - count++; - } else { - std::cerr << "Error, line " << (linenum - 1) - << ": Cannot parse timelog entry date." - << std::endl; - } - - last_account = NULL; - } - break; -#endif // TIMELOG_SUPPORT - - case 'P': { // a pricing entry - in >> c; - - std::time_t date; - std::string symbol; - - in >> line; // the date - if (! parse_date(line, &date, ledger->current_year)) { - std::cerr << "Error, line " << linenum - << ": Failed to parse date: " << line << std::endl; - break; - } - - int hour, min, sec; - in >> hour; // the time - in >> c; - in >> min; - in >> c; - in >> sec; - date = std::time_t(((unsigned long) date) + - hour * 3600 + min * 60 + sec); - - in >> symbol; // the commodity - in >> line; // the price - - // Add this pricing entry to the history for the given - // commodity. - record_price(symbol, create_amount(line), &date); - break; - } - - case 'N': { // don't download prices - in >> c; - in >> line; // the symbol - - commodity * comm = NULL; - commodities_map_iterator item = main_ledger->commodities.find(line); - if (item == main_ledger->commodities.end()) - comm = new commodity(line); - else - comm = (*item).second; - - assert(comm); - if (comm) - comm->sought = true; - break; - } - - case 'C': { // a flat conversion - in >> c; - - std::string symbol; - in >> symbol; // the commodity - in >> line; // the price - - // Add this pricing entry to the given commodity - record_price(symbol, create_amount(line)); - break; - } - - case 'Y': // set the current year - in >> c; - in >> ledger->current_year; - break; - -#ifdef TIMELOG_SUPPORT - case 'h': - case 'b': -#endif - case ';': // a comment line - in.getline(line, MAX_LINE); - linenum++; - break; - - case '-': - case '+': // permanent regexps - in.getline(line, MAX_LINE); - linenum++; - - // Add the regexp to whatever masks currently exist - regexps.push_back(mask(line)); - break; - - case '=': // automated transactions - do_compute = false; - parse_automated_transactions(in, ledger); - do_compute = compute_balances; - break; - - case '!': // directive - in >> line; - if (std::string(line) == "!include") { - std::string path; - bool has_prefix = false; - - in >> path; - - if (in.peek() == ' ') { - has_prefix = true; - in.getline(line, MAX_LINE); - } - - int curr_linenum = linenum; - count += parse_ledger_file(ledger, path, regexps, compute_balances, - has_prefix ? skip_ws(line) : NULL); - linenum = curr_linenum; - } - break; - - default: - if (entry * ent = parse_entry(in, ledger)) { - ledger->entries.push_back(ent); - count++; - } - break; - } - } - - done: - account_prefix = old_account_prefix; - - return count; -} - -int parse_ledger_file(book * ledger, const std::string& file, - regexps_list& regexps, bool compute_balances, - const char * acct_prefix) -{ - std::ifstream stream(file.c_str()); - - // Parse the ledger - -#ifdef READ_GNUCASH - char buf[32]; - stream.get(buf, 31); - stream.seekg(0); - - if (std::strncmp(buf, "<?xml version=\"1.0\"?>", 21) == 0) - return parse_gnucash(ledger, stream, compute_balances); - else -#endif - return parse_ledger(ledger, stream, regexps, compute_balances, - acct_prefix); -} - -////////////////////////////////////////////////////////////////////// -// -// Read other kinds of data from files -// - -void read_regexps(const std::string& path, regexps_list& regexps) -{ - std::ifstream file(path.c_str()); - - while (! file.eof()) { - char buf[80]; - file.getline(buf, 79); - if (*buf && ! std::isspace(*buf)) - regexps.push_back(mask(buf)); - } -} - -} // namespace ledger diff --git a/parser.cc b/parser.cc new file mode 100644 index 00000000..ef525138 --- /dev/null +++ b/parser.cc @@ -0,0 +1,119 @@ +#include "parser.h" +#include "journal.h" + +#include <fstream> +#ifdef WIN32 +#include <io.h> +#endif + +namespace ledger { + +typedef std::list<parser_t *> parsers_list; + +static parsers_list parsers; + +bool register_parser(parser_t * parser) +{ + parsers_list::iterator i; + for (i = parsers.begin(); i != parsers.end(); i++) + if (*i == parser) + break; + if (i != parsers.end()) + return false; + + parsers.push_back(parser); + + return true; +} + +bool unregister_parser(parser_t * parser) +{ + parsers_list::iterator i; + for (i = parsers.begin(); i != parsers.end(); i++) + if (*i == parser) + break; + if (i == parsers.end()) + return false; + + parsers.erase(i); + + return true; +} + +unsigned int parse_journal(std::istream& in, + journal_t * journal, + account_t * master, + const std::string * original_file) +{ + if (! master) + master = journal->master; + + for (parsers_list::iterator i = parsers.begin(); + i != parsers.end(); + i++) + if ((*i)->test(in)) + return (*i)->parse(in, journal, master, original_file); + + return 0; +} + +unsigned int parse_journal_file(const std::string& path, + journal_t * journal, + account_t * master, + const std::string * original_file) +{ + journal->sources.push_back(path); + + if (access(path.c_str(), R_OK) == -1) + throw error(std::string("Cannot read file '") + path + "'"); + + if (! original_file) + original_file = &path; + + std::ifstream stream(path.c_str()); + return parse_journal(stream, journal, master, original_file); +} + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> +#include <Python.h> + +using namespace boost::python; +using namespace ledger; + +struct parser_wrap : public parser_t +{ + PyObject * self; + parser_wrap(PyObject * self_) : self(self_) {} + + virtual bool test(std::istream& in) const { + return call_method<bool>(self, "test", in); + } + + virtual unsigned int parse(std::istream& in, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL) { + return call_method<unsigned int>(self, "__call__", in, journal, master, + original_file); + } +}; + +BOOST_PYTHON_FUNCTION_OVERLOADS(parse_journal_overloads, parse_journal, 2, 4) +BOOST_PYTHON_FUNCTION_OVERLOADS(parse_journal_file_overloads, + parse_journal_file, 2, 4) + +void export_parser() { + class_< parser_t, parser_wrap, boost::noncopyable > ("Parser") + ; + + def("register_parser", register_parser); + def("unregister_parser", unregister_parser); + def("parse_journal", parse_journal, parse_journal_overloads()); + def("parse_journal_file", parse_journal_file, parse_journal_file_overloads()); +} + +#endif // USE_BOOST_PYTHON diff --git a/parser.h b/parser.h new file mode 100644 index 00000000..515bf09b --- /dev/null +++ b/parser.h @@ -0,0 +1,40 @@ +#ifndef _PARSER_H +#define _PARSER_H + +#include <iostream> +#include <string> + +namespace ledger { + +class account_t; +class journal_t; + +class parser_t +{ + public: + virtual ~parser_t() {} + + virtual bool test(std::istream& in) const = 0; + + virtual unsigned int parse(std::istream& in, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL) = 0; +}; + +bool register_parser(parser_t * parser); +bool unregister_parser(parser_t * parser); + +unsigned int parse_journal(std::istream& in, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL); + +unsigned int parse_journal_file(const std::string& path, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL); + +} // namespace ledger + +#endif // _PARSER_H diff --git a/py_eval.cc b/py_eval.cc new file mode 100644 index 00000000..2b68de16 --- /dev/null +++ b/py_eval.cc @@ -0,0 +1,152 @@ +#include "py_eval.h" +#include "journal.h" +#include "error.h" +#include "acconf.h" + +#include <sstream> +#include <map> + +namespace { + bool python_initialized = false; + bool module_initialized = false; +} + +void export_amount(); +void export_balance(); +void export_value(); +void export_journal(); +void export_parser(); +void export_textual(); +void export_binary(); +void export_qif(); +#ifdef HAVE_XMLPARSE +void export_gnucash(); +void export_xml(); +#endif +void export_option(); +void export_config(); +void export_walk(); +void export_format(); +void export_valexpr(); +void export_datetime(); +void export_derive(); + +void initialize_ledger_for_python() +{ + export_amount(); + export_balance(); + export_value(); + export_journal(); + export_parser(); + export_textual(); + export_binary(); + export_qif(); +#ifdef READ_GNUCASH + export_gnucash(); +#endif + export_option(); + export_config(); + export_walk(); + export_format(); + export_valexpr(); + export_datetime(); + export_derive(); + + module_initialized = true; +} + +namespace ledger { + +static struct python_main_t +{ + handle<> mmodule; + dict nspace; + python_main_t() + : mmodule(borrowed(PyImport_AddModule("__main__"))), + nspace(handle<>(borrowed(PyModule_GetDict(mmodule.get())))) {} +} + * python_main = NULL; + +struct python_run +{ + object result; + python_run(const std::string& str, int input_mode) + : result(handle<>(borrowed(PyRun_String(str.c_str(), input_mode, + python_main->nspace.ptr(), + python_main->nspace.ptr())))) {} + operator object() { + return result; + } +}; + +static struct cleanup_python { + ~cleanup_python() { + if (python_main) { + delete python_main; + python_main = NULL; + } + if (python_initialized) + Py_Finalize(); + } +} _cleanup; + +void init_python() +{ + if (! module_initialized) { + Py_Initialize(); + python_initialized = true; + detail::init_module("ledger", &initialize_ledger_for_python); + } + python_main = new python_main_t; +} + +object python_eval(std::istream& in, py_eval_mode_t mode) +{ + if (! python_initialized) + init_python(); + + bool first = true; + std::string buffer; + buffer.reserve(4096); + + while (! in.eof()) { + char buf[256]; + in.getline(buf, 255); + if (buf[0] == '!') + break; + if (first) + first = false; + else + buffer += "\n"; + buffer += buf; + } + + try { + int input_mode; + switch (mode) { + case PY_EVAL_EXPR: input_mode = Py_eval_input; break; + case PY_EVAL_STMT: input_mode = Py_single_input; break; + case PY_EVAL_MULTI: input_mode = Py_file_input; break; + } + assert(Py_IsInitialized()); + return python_run(buffer, input_mode); + } + catch(const boost::python::error_already_set&) { + PyErr_Print(); + throw error("Evaluating Python code"); + } +} + +object python_eval(const std::string& str, py_eval_mode_t mode) +{ + std::istringstream stream(str); + return python_eval(stream, mode); +} + +object python_eval(const char * c_str, py_eval_mode_t mode) +{ + std::string str(c_str); + return python_eval(str, mode); +} + +} // namespace ledger diff --git a/py_eval.h b/py_eval.h new file mode 100644 index 00000000..8327f9a4 --- /dev/null +++ b/py_eval.h @@ -0,0 +1,75 @@ +#ifndef _PYTHON_H +#define _PYTHON_H + +#include "valexpr.h" + +#include <string> +#include <iostream> + +#include <boost/python.hpp> + +using namespace boost::python; + +namespace ledger { + +enum py_eval_mode_t { + PY_EVAL_EXPR, + PY_EVAL_STMT, + PY_EVAL_MULTI +}; + +object python_eval(std::istream& in, py_eval_mode_t mode = PY_EVAL_EXPR); +object python_eval(const std::string& str, py_eval_mode_t mode = PY_EVAL_EXPR); +object python_eval(const char * c_str, py_eval_mode_t mode = PY_EVAL_EXPR); + +template <typename T> +bool python_call(const std::string& func_name, value_expr_t * arg_expr, + const details_t& details, T& result) +{ + try { + object func = python_eval(func_name); + if (arg_expr) { + if (arg_expr->right) { + list args; + args.append(details); + for (value_expr_t * arg = arg_expr; arg; arg = arg->right) { + assert(arg->kind == value_expr_t::O_ARG); + value_t value; + arg->left->compute(value, details); + args.append(value); + } + + if (PyObject * val = PyObject_CallObject(func.ptr(), + tuple(args).ptr())) { + result = extract<T>(val)(); + Py_DECREF(val); + } + else if (PyObject * err = PyErr_Occurred()) { + PyErr_Print(); + throw value_expr_error(std::string("While calling Python function '") + + func_name + "'"); + } + else { + return false; + } + } else { + assert(arg_expr->kind == value_expr_t::O_ARG); + value_t value; + arg_expr->left->compute(value, details); + result = call<T>(func.ptr(), details, value); + } + } else { + result = call<T>(func.ptr(), details); + } + return true; + } + catch(const boost::python::error_already_set&) { + PyErr_Print(); + throw value_expr_error(std::string("While calling Python function '") + + func_name + "'"); + } + } + +} // namespace ledger + +#endif // _PYTHON_H diff --git a/pyledger.cc b/pyledger.cc new file mode 100644 index 00000000..dc65a4a3 --- /dev/null +++ b/pyledger.cc @@ -0,0 +1,9 @@ +#include <boost/python.hpp> + +using namespace boost::python; + +void initialize_ledger_for_python(); + +BOOST_PYTHON_MODULE(ledger) { + initialize_ledger_for_python(); +} diff --git a/pyledger.h b/pyledger.h new file mode 100644 index 00000000..9d8cafdf --- /dev/null +++ b/pyledger.h @@ -0,0 +1,16 @@ +#ifndef _PYLEDGER_H +#define _PYLEDGER_H + +////////////////////////////////////////////////////////////////////// +// +// Ledger Accounting Tool (with Python support via Boost.Python) +// +// A command-line tool for general double-entry accounting. +// +// Copyright (c) 2003,2004 John Wiegley <johnw@newartisans.com> +// + +#include <ledger.h> +#include <py_eval.h> + +#endif // _PYLEDGER_H @@ -0,0 +1,203 @@ +#include "journal.h" +#include "qif.h" +#include "datetime.h" +#include "error.h" +#include "util.h" + +#include <cstring> + +namespace ledger { + +#define MAX_LINE 1024 + +static char line[MAX_LINE + 1]; +static std::string path; +static unsigned int linenum; + +static inline char * get_line(std::istream& in) { + in.getline(line, MAX_LINE); + int len = std::strlen(line); + if (line[len - 1] == '\r') + line[len - 1] = '\0'; + linenum++; + return line; +} + +bool qif_parser_t::test(std::istream& in) const +{ + char magic[sizeof(unsigned int) + 1]; + in.read(magic, sizeof(unsigned int)); + magic[sizeof(unsigned int)] = '\0'; + in.seekg(0, std::ios::beg); + + return (std::strcmp(magic, "!Typ") == 0 || + std::strcmp(magic, "\n!Ty") == 0 || + std::strcmp(magic, "\r\n!T") == 0); +} + +unsigned int qif_parser_t::parse(std::istream& in, + journal_t * journal, + account_t * master, + const std::string * original_file) +{ + std::auto_ptr<entry_t> entry; + std::auto_ptr<amount_t> amount; + transaction_t * xact; + unsigned int count = 0; + account_t * misc = NULL; + commodity_t * def_commodity = NULL; + + entry.reset(new entry_t); + xact = new transaction_t(master); + entry->add_transaction(xact); + + path = journal->sources.back(); + linenum = 1; + + while (! in.eof()) { + char c; + in.get(c); + switch (c) { + case ' ': + case '\t': + if (peek_next_nonws(in) != '\n') { + get_line(in); + throw parse_error(path, linenum, "Line begins with whitespace"); + } + // fall through... + + case '\n': + linenum++; + case '\r': // skip blank lines + break; + + case '!': + in >> line; + + // jww (2004-08-19): these types are not supported yet + assert(std::strcmp(line, "Type:Invst") != 0 && + std::strcmp(line, "Account") != 0 && + std::strcmp(line, "Type:Cat") != 0 && + std::strcmp(line, "Type:Class") != 0 && + std::strcmp(line, "Type:Memorized") != 0); + + get_line(in); + break; + + case 'D': + in >> line; + if (! parse_date(line, &entry->date)) + throw parse_error(path, linenum, "Failed to parse date"); + break; + + case 'T': + case '$': + in >> line; + xact->amount.parse(line); + if (! def_commodity) + def_commodity = commodity_t::find_commodity("$", true); + xact->amount.set_commodity(*def_commodity); + if (c == '$') + xact->amount.negate(); + break; + + case 'C': + if (in.peek() == '*') { + in.get(c); + entry->state = entry_t::CLEARED; + } + break; + + case 'N': + if (std::isdigit(in.peek())) { + in >> line; + entry->code = line; + } + break; + + case 'P': + case 'M': + case 'L': + case 'S': + case 'E': { + char b = c; + int len; + c = in.peek(); + if (! std::isspace(c) && c != '\n') { + get_line(in); + + switch (b) { + case 'P': + entry->payee = line; + break; + + case 'S': + xact = new transaction_t(NULL); + entry->add_transaction(xact); + // fall through... + case 'L': + len = std::strlen(line); + if (line[len - 1] == ']') + line[len - 1] = '\0'; + xact->account = journal->find_account(line[0] == '[' ? + line + 1 : line); + break; + + case 'M': + case 'E': + xact->note = line; + break; + } + } + break; + } + + case 'A': + // jww (2004-08-19): these are ignored right now + get_line(in); + break; + + case '^': + if (xact->account == master) { + if (! misc) + misc = journal->find_account("Miscellaneous"); + transaction_t * nxact = new transaction_t(misc); + entry->add_transaction(nxact); + nxact->amount.negate(); + } + + if (journal->add_entry(entry.get())) { + entry.release(); + count++; + } + + entry.reset(new entry_t); + xact = new transaction_t(master); + entry->add_transaction(xact); + break; + } + } + + return count; +} + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> + +using namespace boost::python; +using namespace ledger; + +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(qif_parse_overloads, + qif_parser_t::parse, 2, 4) + +void export_qif() { + class_< qif_parser_t, bases<parser_t> > ("QifParser") + .def("test", &qif_parser_t::test) + .def("parse", &qif_parser_t::parse, qif_parse_overloads()) + ; +} + +#endif // USE_BOOST_PYTHON @@ -0,0 +1,21 @@ +#ifndef _QIF_H +#define _QIF_H + +#include "parser.h" + +namespace ledger { + +class qif_parser_t : public parser_t +{ + public: + virtual bool test(std::istream& in) const; + + virtual unsigned int parse(std::istream& in, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL); +}; + +} // namespace ledger + +#endif // _QIF_H diff --git a/quotes.cc b/quotes.cc new file mode 100644 index 00000000..65dc7316 --- /dev/null +++ b/quotes.cc @@ -0,0 +1,76 @@ +#include "quotes.h" +#include "datetime.h" +#include "error.h" +#include "debug.h" + +#include <fstream> +#include <stdlib.h> + +namespace ledger { + +void quotes_by_script::operator()(commodity_t& commodity, + const std::time_t moment, + const std::time_t date, + const std::time_t last, + amount_t& price) +{ + DEBUG_CLASS("ledger.quotes.download"); + + DEBUG_PRINT_("commodity: " << commodity.symbol); + DEBUG_PRINT_TIME_(now); + DEBUG_PRINT_TIME_(moment); + DEBUG_PRINT_TIME_(date); + DEBUG_PRINT_TIME_(last); + if (commodity.history) + DEBUG_PRINT_TIME_(commodity.history->last_lookup); + DEBUG_PRINT_("pricing_leeway is " << pricing_leeway); + + if ((commodity.history && + std::difftime(now, commodity.history->last_lookup) < pricing_leeway) || + std::difftime(now, last) < pricing_leeway || + (price && std::difftime(moment, date) <= pricing_leeway)) + return; + + using namespace std; + + DEBUG_PRINT_("downloading quote for symbol " << commodity.symbol); + + char buf[256]; + buf[0] = '\0'; + + bool success = true; + + if (FILE * fp = popen((string("getquote \"") + + commodity.symbol + "\"").c_str(), "r")) { + if (feof(fp) || ! fgets(buf, 255, fp)) + success = false; + if (pclose(fp) != 0) + success = false; + } + + if (success && buf[0]) { + char * p = strchr(buf, '\n'); + if (p) *p = '\0'; + + DEBUG_PRINT_("downloaded quote: " << buf); + + price.parse(buf); + commodity.add_price(now, price); + + commodity.history->last_lookup = now; + cache_dirty = true; + + if (price && ! price_db.empty()) { + strftime(buf, 127, "%Y/%m/%d %H:%M:%S", localtime(&now)); + ofstream database(price_db.c_str(), ios_base::out | ios_base::app); + database << "P " << buf << " " << commodity.symbol + << " " << price << endl; + } + } else { + throw error(std::string("Failed to download price for '") + + commodity.symbol + "' (command: \"getquote " + + commodity.symbol + "\")"); + } +} + +} // namespace ledger diff --git a/quotes.h b/quotes.h new file mode 100644 index 00000000..53008aa0 --- /dev/null +++ b/quotes.h @@ -0,0 +1,30 @@ +#ifndef _QUOTES_H +#define _QUOTES_H + +#include "amount.h" + +namespace ledger { + +class quotes_by_script : public commodity_t::updater_t +{ + std::string price_db; + unsigned long pricing_leeway; + bool& cache_dirty; + + public: + quotes_by_script(std::string _price_db, + unsigned long _pricing_leeway, + bool& _cache_dirty) + : price_db(_price_db), pricing_leeway(_pricing_leeway), + cache_dirty(_cache_dirty) {} + + virtual void operator()(commodity_t& commodity, + const std::time_t moment, + const std::time_t date, + const std::time_t last, + amount_t& price); +}; + +} // namespace ledger + +#endif // _QUOTES_H diff --git a/reports.cc b/reports.cc deleted file mode 100644 index 2708c1d0..00000000 --- a/reports.cc +++ /dev/null @@ -1,1225 +0,0 @@ -#include "ledger.h" - -#define LEDGER_VERSION "1.7" - -#include <cstring> -#include <unistd.h> - -namespace ledger { - -static bool cleared_only = false; -static bool uncleared_only = false; -static bool show_virtual = true; -static bool show_children = false; -static bool show_sorted = false; -static bool show_empty = false; -static bool show_subtotals = true; -static bool full_names = false; -static bool print_monthly = false; -static bool gnuplot_safe = false; - -static bool cost_basis = false; -static bool use_history = false; -static bool net_gain = false; -static bool get_quotes = false; - long pricing_leeway = 24 * 3600; - std::string price_db; - -static amount * lower_limit = NULL; - -static mask * negonly_regexp = NULL; - -static std::time_t begin_date; -static bool have_beginning = false; - -static std::time_t end_date; -static bool have_ending = false; - -static struct std::tm date_mask; -static bool have_date_mask = false; - -static bool matches_date_range(entry * ent) -{ - if (have_beginning && difftime(ent->date, begin_date) < 0) - return false; - - if (have_ending && difftime(ent->date, end_date) >= 0) - return false; - - if (have_date_mask) { - struct std::tm * then = std::localtime(&ent->date); - - if (date_mask.tm_mon != -1 && - date_mask.tm_mon != then->tm_mon) - return false; - - if (date_mask.tm_mday != -1 && - date_mask.tm_mday != then->tm_mday) - return false; - -#if 0 - // jww (2003-10-10): This causes only certain days of the week to - // print, even when it was not included in the mask. - if (date_mask.tm_wday != -1 && - date_mask.tm_wday != then->tm_wday) - return false; -#endif - - if (date_mask.tm_year != -1 && - date_mask.tm_year != then->tm_year) - return false; - } - - return true; -} - -static amount * resolve_amount(amount * amt, - std::time_t * when = NULL, - totals * balance = NULL, - bool add_base_value = false, - bool free_memory = false) -{ - amount * value; - bool alloced = true; - - if (! use_history) { - value = amt; - alloced = false; - } - else if (cost_basis) { - value = amt->value(); - } - else if (net_gain) { - value = amt->street(when ? when : (have_ending ? &end_date : NULL), - use_history, get_quotes); - amount * basis = amt->value(); - if (value->commdty() == basis->commdty()) { - basis->negate(); - value->credit(basis); - } else { - // If the commodities do not match, ignore this amount by - // returning a zeroed value. - delete basis; - basis = value->copy(); - basis->negate(); - value->credit(basis); - delete basis; - } - } - else { - value = amt->street(when ? when : (have_ending ? &end_date : NULL), - use_history, get_quotes); - } - - if (balance) { - if (add_base_value) - balance->credit(cost_basis ? value : amt); - else - balance->credit(value); - } - - if (free_memory && alloced) { - delete value; - value = NULL; - } - else if (! free_memory && ! alloced) { - value = value->copy(); - } - - return value; -} - -static inline void print_resolved_balance(std::ostream& out, - std::time_t * when, - totals& balance, - bool added_base_value = false) -{ - if (! added_base_value || ! use_history || cost_basis) { - balance.print(out, 12); - } else { - totals * street = balance.street(when ? when : (have_ending ? - &end_date : NULL), - use_history, get_quotes); - street->print(out, 12); - delete street; - } -} - -////////////////////////////////////////////////////////////////////// -// -// Balance reporting code -// - -static bool satisfies_limit(totals& balance) -{ - bool satisfies = true; - bool invert = false; - - assert(lower_limit); - - if (balance.is_negative()) - invert = true; - else - lower_limit->negate(); - - balance.credit(lower_limit); - if (balance.is_negative()) - satisfies = invert; - else - satisfies = ! invert; - - lower_limit->negate(); - balance.credit(lower_limit); - - if (invert) - lower_limit->negate(); - - return satisfies; -} - -static bool satisfies_limit(amount * balance) -{ - bool satisfies = true; - bool invert = false; - - assert(lower_limit); - - if (balance->is_negative()) - invert = true; - else - lower_limit->negate(); - - balance->credit(lower_limit); - if (balance->is_negative()) - satisfies = invert; - else - satisfies = ! invert; - - lower_limit->negate(); - balance->credit(lower_limit); - - if (invert) - lower_limit->negate(); - - return satisfies; -} - -static void adjust_total(account * acct) -{ - for (accounts_map_iterator i = acct->children.begin(); - i != acct->children.end(); - i++) - adjust_total((*i).second); - - if (acct->checked == 1) { - if (! show_empty && acct->balance.is_zero()) - acct->checked = 2; - else if (lower_limit && ! satisfies_limit(acct->balance)) - acct->checked = 2; - else if (negonly_regexp && negonly_regexp->match(acct->as_str()) && - ! acct->balance.is_negative()) - acct->checked = 2; - - if (acct->checked == 2) { - acct->balance.negate(); - for (account * a = acct->parent; a; a = a->parent) - a->balance.credit(acct->balance); - } - } -} - -static int acct_visible_children(account * acct) -{ - int count = 0; - for (accounts_map_iterator i = acct->children.begin(); - i != acct->children.end(); - i++) { - if ((*i).second->checked == 1) { - if ((*i).second->children.size() == 0) - count++; - else - count += acct_visible_children((*i).second); - } - } - return count; -} - -static void display_total(std::ostream& out, totals& balance, - account * acct, int level, int * headlines) -{ - // If the number of visible children is exactly one, do not print - // the parent account, but just the one child (whose name will - // output with sufficiently qualification). - - if (acct->checked == 1 && acct_visible_children(acct) != 1) { - if (acct->balance.is_zero()) { - out.width(20); - out << " "; - } else { - acct->balance.print(out, 20); - } - - if (level == 0 || full_names || ! show_subtotals) { - if (show_subtotals) { - balance.credit(acct->balance); - (*headlines)++; - } - - out << " " << acct->as_str() << std::endl; - } else { - out << " "; - for (int i = 0; i < level; i++) - out << " "; - - assert(acct->parent); - if (acct_visible_children(acct->parent) == 1) { - /* If the account has no other siblings, instead of printing: - Parent - Child - print: - Parent:Child */ - const account * parent; - for (parent = acct->parent; - parent->parent && acct_visible_children(parent->parent) == 1; - parent = parent->parent) {} - - out << acct->as_str(parent) << std::endl; - } else { - out << acct->name << std::endl; - } - } - - level++; - } - - // Display balances for all child accounts - - for (accounts_map_iterator i = acct->children.begin(); - i != acct->children.end(); - i++) - display_total(out, balance, (*i).second, level, headlines); -} - -void report_balances(std::ostream& out, regexps_list& regexps) -{ - // Walk through all of the ledger entries, computing the account - // totals - - for (entries_list_iterator i = main_ledger->entries.begin(); - i != main_ledger->entries.end(); - i++) { - if ((cleared_only && ! (*i)->cleared) || - (uncleared_only && (*i)->cleared) || ! matches_date_range(*i)) - continue; - - for (std::list<transaction *>::iterator x = (*i)->xacts.begin(); - x != (*i)->xacts.end(); - x++) { - if (! show_virtual && (*x)->is_virtual) - continue; - - for (account * acct = (*x)->acct; - acct; - acct = show_subtotals ? acct->parent : NULL) { - bool by_exclusion = false; - bool match = false; - - if (acct->checked == 0) { - if (regexps.empty()) { - if (! (show_children || ! acct->parent)) - acct->checked = 2; - else - acct->checked = 1; - } else { - match = matches(regexps, acct->as_str(), &by_exclusion); - if (! match) { - acct->checked = 2; - } - else if (by_exclusion) { - if (! (show_children || ! acct->parent)) - acct->checked = 2; - else - acct->checked = 1; - } - else { - acct->checked = 1; - } - } - } - - if (acct->checked == 1) { - resolve_amount((*x)->cost, NULL, &acct->balance, false, true); - } - else if (show_subtotals) { - if (! regexps.empty() && ! match) { - for (account * a = acct->parent; a; a = a->parent) { - if (matches(regexps, a->as_str(), &by_exclusion) && - ! by_exclusion) { - match = true; - break; - } - } - if (! match) break; - } - } - } - } - } - - // Walk through all the top-level accounts, giving the balance - // report for each, and then for each of their children. - - totals balance; - int headlines = 0; - - for (accounts_map_iterator i = main_ledger->accounts.begin(); - i != main_ledger->accounts.end(); - i++) { - adjust_total((*i).second); - display_total(out, balance, (*i).second, 0, &headlines); - } - - // Print the total of all the balances shown - - if (show_subtotals && headlines > 1 && ! balance.is_zero()) { - out << "--------------------" << std::endl; - balance.print(out, 20); - out << std::endl; - } -} - -////////////////////////////////////////////////////////////////////// -// -// Register printing code -// - -static std::string truncated(const std::string& str, int width) -{ - char buf[256]; - memset(buf, '\0', 255); - std::strncpy(buf, str.c_str(), width); - if (buf[width - 1]) - std::strcpy(&buf[width - 3], "..."); - else - buf[width] = '\0'; - return buf; -} - -enum periodicity_t { - PERIOD_NONE, - PERIOD_MONTHLY, - PERIOD_WEEKLY_SUN, - PERIOD_WEEKLY_MON -}; - -static totals * prev_balance = NULL; -static std::time_t prev_date; - -void print_register_transaction(std::ostream& out, entry * ent, - transaction * xact, totals& balance); - -static void report_change_in_asset_value(std::ostream& out, std::time_t date, - account * acct, totals& balance) -{ - totals * prev_street_balance = - prev_balance->street(&prev_date, use_history, get_quotes); - totals * curr_street_balance = - prev_balance->street(&date, use_history, get_quotes); - - delete prev_balance; - prev_balance = NULL; - - prev_street_balance->negate(); - curr_street_balance->credit(*prev_street_balance); - - if (! curr_street_balance->is_zero()) { - for (totals::const_iterator i = curr_street_balance->amounts.begin(); - i != curr_street_balance->amounts.end(); - i++) { - if ((*i).second->is_zero()) - continue; - - entry change(main_ledger); - - change.date = date; - change.cleared = true; - change.desc = "Assets revalued"; - - transaction * trans = new transaction(); - trans->acct = const_cast<account *>(acct); - trans->cost = (*i).second->copy(); - change.xacts.push_back(trans); - - transaction * trans2 = new transaction(); - trans2->acct = main_ledger->find_account("Equity:Asset Gain"); - trans2->cost = (*i).second->copy(); - trans2->cost->negate(); - change.xacts.push_back(trans2); - - balance.credit(trans2->cost); - - print_register_transaction(out, &change, trans, balance); - - delete prev_balance; - prev_balance = NULL; - } - } - - delete prev_street_balance; - delete curr_street_balance; -} - -void print_register_transaction(std::ostream& out, entry * ent, - transaction * xact, totals& balance) -{ - if (prev_balance) - report_change_in_asset_value(out, ent->date, xact->acct, balance); - - char buf[32]; - std::strftime(buf, 31, "%Y/%m/%d ", std::localtime(&ent->date)); - out << buf; - - out.width(20); - if (ent->desc.empty()) - out << " "; - else - out << std::left << truncated(ent->desc, 20); - out << " "; - - // Always display the street value, if prices have been - // specified - - amount * street = resolve_amount(xact->cost, &ent->date, &balance, true); - - // If there are two transactions, use the one which does not - // refer to this account. If there are more than two, print - // "<Splits...>", unless the -s option is being used (show - // children), in which case print all of the splits, like - // gnucash does. - - transaction * xp; - if (ent->xacts.size() == 2) { - if (xact == ent->xacts.front()) - xp = ent->xacts.back(); - else - xp = ent->xacts.front(); - } else { - xp = xact; - } - std::string xact_str = xp->acct_as_str(); - - if (xp == xact && ! show_subtotals) - xact_str = "<Splits...>"; - - out.width(22); - out << std::left << truncated(xact_str, 22) << " "; - - out.width(12); - out << std::right << street->as_str(true); - delete street; - - print_resolved_balance(out, &ent->date, balance, true); - - out << std::endl; - - assert(! prev_balance); - prev_balance = new totals; - prev_balance->credit(balance); - prev_date = ent->date; - - if (! show_children || xp != xact) - return; - - for (std::list<transaction *>::iterator y = ent->xacts.begin(); - y != ent->xacts.end(); - y++) { - if (xact == *y) - continue; - - out << " "; - - out.width(22); - out << std::left << truncated((*y)->acct_as_str(), 22) << " "; - out.width(12); - - street = resolve_amount((*y)->cost, &ent->date); - out << std::right << street->as_str(true) << std::endl; - delete street; - } -} - -void print_register_period(std::ostream& out, std::time_t date, - account * acct, amount& sum, totals& balance) -{ - if (! gnuplot_safe && prev_balance) { - sum.negate(); - balance.credit(&sum); - report_change_in_asset_value(out, date, acct, balance); - sum.negate(); - balance.credit(&sum); - } - - char buf[32]; - std::strftime(buf, 31, "%Y/%m/%d ", std::localtime(&date)); - out << buf; - - if (! gnuplot_safe) { - out.width(20); - std::strftime(buf, 31, "%B", std::localtime(&date)); - out << std::left << truncated(buf, 20); - out << " "; - - out.width(22); - out << std::left << truncated(acct->as_str(), 22) << " "; - } else { - commodity * cmdty = sum.commdty(); - cmdty->symbol = ""; - cmdty->separate = false; - cmdty->thousands = false; - cmdty->european = false; - } - - out.width(12); - out << std::right << sum.as_str(); - - if (! gnuplot_safe) { - print_resolved_balance(out, &date, balance, true); - - assert(! prev_balance); - prev_balance = new totals; - prev_balance->credit(balance); - prev_date = date; - } - - out << std::endl; -} - -void print_register(std::ostream& out, const std::string& acct_name, - regexps_list& regexps, periodicity_t period = PERIOD_NONE) -{ - mask acct_regex(acct_name); - - // Walk through all of the ledger entries, printing their register - // formatted equivalent - - totals balance; - amount * period_sum = NULL; // jww (2004-04-27): should be 'totals' type - std::time_t last_date; - account * last_acct; - int last_mon = -1; - - for (entries_list_iterator i = main_ledger->entries.begin(); - i != main_ledger->entries.end(); - i++) { - if ((cleared_only && ! (*i)->cleared) || - (uncleared_only && (*i)->cleared) || - ! matches_date_range(*i) || ! (*i)->matches(regexps)) - continue; - - int entry_mon = std::localtime(&(*i)->date)->tm_mon; - - if (period_sum && period == PERIOD_MONTHLY && - last_mon != -1 && entry_mon != last_mon) { - assert(last_acct); - print_register_period(out, last_date, last_acct, *period_sum, balance); - delete period_sum; - period_sum = NULL; - } - - for (std::list<transaction *>::iterator x = (*i)->xacts.begin(); - x != (*i)->xacts.end(); - x++) { - if (! acct_regex.match((*x)->acct->as_str()) || - (lower_limit && ! satisfies_limit((*x)->cost))) - continue; - - if (period == PERIOD_NONE) { - print_register_transaction(out, *i, *x, balance); - } else { - amount * street = resolve_amount((*x)->cost, &(*i)->date, &balance, - true); - if (period_sum) { - period_sum->credit(street); - delete street; - } else { - period_sum = street; - } - - last_mon = entry_mon; - } - - last_date = (*i)->date; - last_acct = (*x)->acct; - } - } - - if (period_sum) { - if (last_acct) - print_register_period(out, last_date, last_acct, *period_sum, balance); - delete period_sum; - } - - if (! gnuplot_safe && prev_balance) { - report_change_in_asset_value(out, have_ending ? end_date : std::time(NULL), - last_acct, balance); - } -} - -////////////////////////////////////////////////////////////////////// -// -// Create an Equity file based on a ledger. This is used for -// archiving past years, and starting out a new year with compiled -// balances. -// - -static void equity_entry(account * acct, regexps_list& regexps, - std::ostream& out) -{ - if (! acct->balance.is_zero() && - (regexps.empty() || matches(regexps, acct->as_str()))) { - entry opening(main_ledger); - - opening.date = have_ending ? end_date : std::time(NULL); - opening.cleared = true; - opening.desc = "Opening Balance"; - - for (totals::const_iterator i = acct->balance.amounts.begin(); - i != acct->balance.amounts.end(); - i++) { - // Skip it, if there is a zero balance for the commodity - if ((*i).second->is_zero()) - continue; - - transaction * xact = new transaction(); - xact->acct = const_cast<account *>(acct); - xact->cost = (*i).second->copy(); - opening.xacts.push_back(xact); - - xact = new transaction(); - xact->acct = main_ledger->find_account("Equity:Opening Balances"); - xact->cost = (*i).second->copy(); - xact->cost->negate(); - opening.xacts.push_back(xact); - } - - opening.print(out); - } - - // Display balances for all child accounts - - for (accounts_map_iterator i = acct->children.begin(); - i != acct->children.end(); - i++) - equity_entry((*i).second, regexps, out); -} - -void equity_ledger(std::ostream& out, regexps_list& regexps) -{ - // The account have their current totals already generated as a - // result of parsing. We just have to output those values. - // totals - - for (accounts_map_iterator i = main_ledger->accounts.begin(); - i != main_ledger->accounts.end(); - i++) - equity_entry((*i).second, regexps, out); -} - -////////////////////////////////////////////////////////////////////// -// -// Report on the price of any commodities matching REGEXPS. This can -// be used to see what something was worth at a specific time. -// - -void price_report(std::ostream& out, regexps_list& regexps) -{ - if (! have_ending) { - end_date = std::time(NULL); - have_ending = true; - } - - for (commodities_map_iterator i = main_ledger->commodities.begin(); - i != main_ledger->commodities.end(); - i++) - if (regexps.empty() || matches(regexps, (*i).first)) { - amount * price = (*i).second->price(have_ending ? &end_date : NULL, - use_history, get_quotes); - if (price && ! price->is_zero()) { - out.width(20); - out << std::right << price->as_str() << " " << (*i).first - << std::endl; - } - } -} - -////////////////////////////////////////////////////////////////////// -// -// Add a new entry, using hueristic logic to simplify the entry -// requirements -// - -void add_new_entry(int index, int argc, char **argv) -{ - regexps_list regexps; - entry added(main_ledger); - entry * matching = NULL; - - assert(index < argc); - - if (! parse_date(argv[index++], &added.date)) { - std::cerr << "Error: Bad entry date: " << argv[index - 1] - << std::endl; - std::exit(1); - } - - added.cleared = cleared_only; - - if (index == argc) { - std::cerr << "Error: Too few arguments to 'entry'." << std::endl; - std::exit(1); - } - - regexps.clear(); - regexps.push_back(mask(argv[index++])); - - for (entries_list_reverse_iterator i = main_ledger->entries.rbegin(); - i != main_ledger->entries.rend(); - i++) { - if ((*i)->matches(regexps)) { - matching = *i; - break; - } - } - - added.desc = matching ? matching->desc : regexps.front().pattern; - - if (index == argc) { - std::cerr << "Error: Too few arguments to 'entry'." << std::endl; - std::exit(1); - } - - if (argv[index][0] == '-' || std::isdigit(argv[index][0])) { - if (! matching) { - std::cerr << "Error: Missing account name for non-matching entry." - << std::endl; - std::exit(1); - } - - transaction * m_xact, * xact, * first; - - m_xact = matching->xacts.front(); - - first = xact = new transaction(); - xact->acct = m_xact->acct; - xact->cost = create_amount(argv[index++]); - xact->cost->set_commdty(m_xact->cost->commdty()); - - added.xacts.push_back(xact); - - m_xact = matching->xacts.back(); - - xact = new transaction(); - xact->acct = m_xact->acct; - xact->cost = first->cost->copy(); - xact->cost->negate(); - - added.xacts.push_back(xact); - - if ((index + 1) < argc && std::string(argv[index]) == "-from") - if (account * acct = main_ledger->re_find_account(argv[++index])) - added.xacts.back()->acct = acct; - } else { - while (index < argc && std::string(argv[index]) != "-from") { - transaction * xact = new transaction(); - - mask acct_regex(argv[index++]); - - account * acct = NULL; - commodity * cmdty = NULL; - - if (matching) { - for (std::list<transaction *>::iterator x = matching->xacts.begin(); - x != matching->xacts.end(); - x++) { - if (acct_regex.match((*x)->acct->as_str())) { - acct = (*x)->acct; - cmdty = (*x)->cost->commdty(); - break; - } - } - } - - if (acct) - xact->acct = acct; - else - xact->acct = main_ledger->re_find_account(acct_regex.pattern); - - if (! xact->acct) { - std::cerr << "Error: Could not find account name '" - << acct_regex.pattern << "'." << std::endl; - std::exit(1); - } - - if (index == argc) { - std::cerr << "Error: Too few arguments to 'entry'." << std::endl; - std::exit(1); - } - - xact->cost = create_amount(argv[index++]); - if (! xact->cost->commdty()) - xact->cost->set_commdty(cmdty); - - added.xacts.push_back(xact); - } - - if ((index + 1) < argc && std::string(argv[index]) == "-from") { - if (account * acct = main_ledger->re_find_account(argv[++index])) { - transaction * xact = new transaction(); - xact->acct = acct; - xact->cost = NULL; - - added.xacts.push_back(xact); - } - } else { - transaction * xact = new transaction(); - if (! matching) { - std::cerr << "Error: Could not figure out the account to draw from." - << std::endl; - std::exit(1); - } - xact->acct = matching->xacts.back()->acct; - xact->cost = NULL; - added.xacts.push_back(xact); - } - } - - if (added.finalize()) - added.print(std::cout); -} - -// Print out the entire ledger that was read in. This can be used to -// "wash" ugly ledger files. It's written here, instead of ledger.cc, -// in order to access the static globals above. - -void book::print(std::ostream& out, regexps_list& regexps, - bool shortcut) const -{ - for (entries_list_const_iterator i = entries.begin(); - i != entries.end(); - i++) { - if (! matches_date_range(*i) || ! (*i)->matches(regexps)) - continue; - - (*i)->print(out, shortcut); - } -} - -} // namespace ledger - -using namespace ledger; - -static void show_help(std::ostream& out) -{ - std::cerr - << "usage: ledger [options] COMMAND [options] [REGEXPS]" << std::endl - << std::endl - << "Basic options:" << std::endl - << " -h display this help text" << std::endl - << " -v display version information" << std::endl - << " -f FILE specify pathname of ledger data file" << std::endl - << " -i FILE read list of inclusion regexps from FILE" << std::endl - << std::endl - << "Report filtering:" << std::endl - << " -b DATE specify a beginning date" << std::endl - << " -e DATE specify an ending date" << std::endl - << " -c do not show future entries (same as -e TODAY)" << std::endl - << " -d DATE specify a date mask ('-d mon', for all mondays)" << std::endl - << " -C show only cleared transactions and balances" << std::endl - << " -U show only uncleared transactions and balances" << std::endl - << " -l AMT don't print balance totals whose abs value is <AMT" << std::endl - << " -N REGEX accounts matching REGEXP only display if negative" << std::endl - << " -R do not consider virtual transactions: real only" << std::endl - << std::endl - << "Customizing output:" << std::endl - << " -n do not calculate parent account totals" << std::endl - << " -s show sub-accounts in balance totals" << std::endl - << " -S sort the output of \"print\" by date" << std::endl - << " -E show accounts that total to zero" << std::endl - << " -F print each account's full name" << std::endl - << " -M print register using monthly sub-totals" << std::endl - << " -G use with -M to produce gnuplot-friendly output" << std::endl - << std::endl - << "Commodity prices:" << std::endl - << " -P FILE sets the price database, for reading/writing price info" << std::endl - << " -T report commodity totals, not their market value" << std::endl - << " -V report the market value of commodities" << std::endl - << " -B report cost basis of commodities" << std::endl - << " -Q download new price information (when needed) from the Internet" << std::endl - << " (works by running \"getquote SYMBOL\")" << std::endl - << " -L MINS with -Q, fetch quotes only if data is older than MINS" << std::endl - << " -p STR specifies a direct commodity conversion: COMM=AMOUNT" << std::endl - << std::endl - << "commands:" << std::endl - << " balance show balance totals" << std::endl - << " register display a register for ACCOUNT" << std::endl - << " print print all ledger entries" << std::endl - << " equity generate equity ledger for all entries" << std::endl - << " entry output a newly formed entry, based on arguments" << std::endl - << " price show the last known price for matching commodities" << std::endl; -} - -////////////////////////////////////////////////////////////////////// -// -// Command-line parser and top-level logic. -// - -int main(int argc, char * argv[]) -{ - int index; - std::string prices; - std::string limit; - regexps_list regexps; - - std::vector<std::string> files; - - main_ledger = new book; - - // Initialize some variables based on environment variable settings - - if (char * p = std::getenv("PRICE_HIST")) - price_db = p; - - if (char * p = std::getenv("PRICE_EXP")) - pricing_leeway = std::atol(p) * 60; - - // Parse the command-line options - - int c; - while (-1 != (c = getopt(argc, argv, - "+ABb:Ccd:Ee:Ff:Ghi:L:l:MN:nP:p:QRSsTUVv"))) { - switch (char(c)) { - case 'b': - have_beginning = true; - if (! parse_date(optarg, &begin_date)) { - std::cerr << "Error: Bad begin date: " << optarg << std::endl; - return 1; - } - break; - - case 'e': - have_ending = true; - if (! parse_date(optarg, &end_date)) { - std::cerr << "Error: Bad end date: " << optarg << std::endl; - return 1; - } - break; - - case 'c': - end_date = std::time(NULL); - have_ending = true; - break; - - case 'd': - have_date_mask = true; - if (! parse_date_mask(optarg, &date_mask)) { - std::cerr << "Error: Bad date mask: " << optarg << std::endl; - return 1; - } - break; - - case 'h': show_help(std::cout); break; - case 'f': files.push_back(optarg); break; - case 'C': cleared_only = true; break; - case 'U': uncleared_only = true; break; - case 'R': show_virtual = false; break; - case 's': show_children = true; break; - case 'S': show_sorted = true; break; - case 'E': show_empty = true; break; - case 'n': show_subtotals = false; break; - case 'F': full_names = true; break; - case 'M': print_monthly = true; break; - case 'G': gnuplot_safe = true; break; - - case 'N': - negonly_regexp = new mask(optarg); - break; - - // -i path-to-file-of-regexps - case 'i': - if (access(optarg, R_OK) != -1) - read_regexps(optarg, regexps); - break; - - // -p "COMMODITY=PRICE" - case 'p': - parse_price_setting(optarg); - break; - - case 'P': - price_db = optarg; - break; - - case 'Q': - get_quotes = true; - break; - - case 'V': - use_history = true; - break; - - case 'B': - cost_basis = true; - use_history = true; - break; - - case 'A': - net_gain = true; - use_history = true; - break; - - case 'T': - cost_basis = false; - use_history = false; - break; - - case 'L': - pricing_leeway = std::atol(optarg) * 60; - break; - - case 'l': - lower_limit = create_amount(optarg); - break; - - case 'v': - std::cout - << "Ledger Accouting Tool " LEDGER_VERSION << std::endl - << " Copyright (c) 2003 John Wiegley <johnw@newartisans.com>" - << std::endl << std::endl - << "This program is made available under the terms of the BSD" - << std::endl - << "Public License. See the LICENSE file included with the" - << std::endl - << "distribution for details and disclaimer." << std::endl; - return 0; - } - } - - if (optind == argc) { - show_help(std::cout); - return 1; - } - - index = optind; - - // Read the command word - - const std::string command = argv[index++]; - - int name_index = index; - if (command == "register" || command == "reg") { - if (net_gain) { - std::cerr << ("Reporting the asset gain makes " - "no sense for the register report.") - << std::endl; - return 1; - } - - if (name_index == argc) { - std::cerr << ("Error: Must specify an account name " - "after the 'register' command.") << std::endl; - return 1; - } - index++; - } - - // Compile the list of specified regular expressions, which can be - // specified after the command, or using the '-i FILE' option - - if (command != "entry") - for (; index < argc; index++) - regexps.push_back(mask(argv[index])); - - // A ledger data file must be specified - - int entry_count = 0; - - if (files.empty()) { - if (char * p = std::getenv("LEDGER")) { - for (p = std::strtok(p, ":"); p; p = std::strtok(NULL, ":")) { - char * sep = std::strrchr(p, '='); - if (sep) *sep++ = '\0'; - entry_count += parse_ledger_file(main_ledger, std::string(p), regexps, - command == "equity", sep); - } - } - } else { - for (std::vector<std::string>::iterator i = files.begin(); - i != files.end(); i++) { - char buf[4096]; - char * p = buf; - std::strcpy(p, (*i).c_str()); - char * sep = std::strrchr(p, '='); - if (sep) *sep++ = '\0'; - entry_count += parse_ledger_file(main_ledger, std::string(p), regexps, - command == "equity", sep); - } - } - - if (use_history && ! cost_basis && ! price_db.empty()) - entry_count += parse_ledger_file(main_ledger, price_db, regexps, - command == "equity"); - - if (entry_count == 0) { - std::cerr << ("Please specify ledger file(s) using -f option " - "or LEDGER environment variable.") << std::endl; - return 1; - } - - // Process the command - - if (command == "balance" || command == "bal") { - report_balances(std::cout, regexps); - } - else if (command == "register" || command == "reg") { - if (show_sorted || print_monthly) - main_ledger->sort(cmp_entry_date()); - print_register(std::cout, argv[name_index], regexps, - print_monthly ? PERIOD_MONTHLY : PERIOD_NONE); - } - else if (command == "print") { - if (show_sorted) - main_ledger->sort(cmp_entry_date()); - main_ledger->print(std::cout, regexps, ! full_names); - } - else if (command == "equity") { - equity_ledger(std::cout, regexps); - } - else if (command == "price" || command == "prices") { - price_report(std::cout, regexps); - } - else if (command == "entry") { - add_new_entry(index, argc, argv); - } - else { - std::cerr << "Error: Unrecognized command '" << command << "'." - << std::endl; - return 1; - } - -#ifdef DEBUG - // Ordinarily, deleting the main ledger isn't necessary, since the - // process is about to give back its heap to the OS. - - delete main_ledger; - - if (lower_limit) - delete lower_limit; - - if (negonly_regexp) - delete negonly_regexp; -#endif - - return 0; -} - -// reports.cc ends here. @@ -1,11 +1,26 @@ -2004/05/01 Checking balance - Assets:Checking $500.00 += /^Expenses:Books/ + (Liabilities:Taxes) -0.10 + +~ Monthly + Assets:Bank:Checking $500.00 + Income:Salary + +2004/05/01 * Checking balance + Assets:Bank:Checking $1,000.00 Equity:Opening Balances -2004/05/29 Book Store - Expenses:Books $20.00 - Assets:Checking +2004/05/01 * Investment balance + Assets:Brokerage 50 AAPL @ $30.00 + Equity:Opening Balances -2004/05/29 Restaurant - Expenses:Food $50.00 +2004/05/14 * Pay day + Assets:Bank:Checking $500.00 + Income:Salary + +2004/05/27 Book Store + Expenses:Books $20.00 Liabilities:MasterCard + +2004/05/27 (100) Credit card company + Liabilities:MasterCard $20.00 + Assets:Bank:Checking diff --git a/scripts/README b/scripts/README index 7221f95f..6108afbf 100644 --- a/scripts/README +++ b/scripts/README @@ -1,5 +1,4 @@ -This scripts are provided just in the way of giving ideas. They -probably all need to be modified to suit your particular environment. -Beware! +This scripts are provided just to give some ideas. They probably need +to be modified to better suit your environment. Beware! John diff --git a/scripts/bal b/scripts/bal index 7fdf54ca..be7c3a0a 100644..100755 --- a/scripts/bal +++ b/scripts/bal @@ -1,27 +1,21 @@ #!/bin/sh -switch="" -current="-c" -limit="-l \$50" -negonly="-N ^Liabilities" +switch="-c" +limit="-t (/Liabilities/?a<0:Ua>100)&a" -if [ "$1" = "-C" -o "$1" = "-U" -o "$1" = "-P" ]; then +if [ "$1" = "-C" -o "$1" = "-U" ]; then switch="$1" shift -elif [ "$1" = "-b" -o "$1" = "-e" ]; then - current="$1 $2" +elif [ "$1" = "-b" -o "$1" = "-e" -o "$1" = "-p" ]; then + switch="$1 $2" shift 2 fi accts="$@" if [ -z "$accts" ]; then accts="-Equity -Income -Expenses" - if [ ! "$switch" = "-P" ]; then - accts="$accts -Savings -Retirement" - fi else limit="" - negonly="" fi -ledger $current $limit $negonly -s $switch balance $accts +ledger -VQ $switch $limit -s -S "-AT" balance $accts diff --git a/scripts/bal-huquq b/scripts/bal-huquq new file mode 100755 index 00000000..c17c84a2 --- /dev/null +++ b/scripts/bal-huquq @@ -0,0 +1,21 @@ +#!/bin/sh + +switch="-c" +limit="-t (/Liabilities/?(/Huquq/?a/P{2.22AU}<={-1.0}:a<0):Ua>100)&a" + +if [ "$1" = "-C" -o "$1" = "-U" ]; then + switch="$1" + shift +elif [ "$1" = "-b" -o "$1" = "-e" -o "$1" = "-p" ]; then + switch="$1 $2" + shift 2 +fi + +accts="$@" +if [ -z "$accts" ]; then + accts="-Equity -Income -Expenses" +else + limit="" +fi + +ledger -VQ $switch $limit -s -S "-AT" balance $accts diff --git a/scripts/entry b/scripts/entry index 28daf8c8..cc030d8e 100644..100755 --- a/scripts/entry +++ b/scripts/entry @@ -1,6 +1,6 @@ #!/bin/sh -if [ -z "$LEDGER" -o ! -r $LEDGER ]; then +if [ -z "$LEDGER" -o ! -r "$LEDGER" ]; then echo Please set your LEDGER environment variable. fi diff --git a/scripts/getquote b/scripts/getquote index cf8c8abd..bed561d6 100644..100755 --- a/scripts/getquote +++ b/scripts/getquote @@ -1,16 +1,16 @@ #!/usr/bin/perl -exit 0 if $ARGV[0] eq "\$"; +$timeout = 60; use Finance::Quote; $q = Finance::Quote->new; - -$q->timeout(60); +$q->timeout($timeout); $q->require_labels(qw/price/); %quotes = $q->fetch("nasdaq", $ARGV[0]); - if ($quotes{$ARGV[0], "price"}) { print "\$", $quotes{$ARGV[0], "price"}, "\n"; +} else { + exit 1; } diff --git a/scripts/mean b/scripts/mean deleted file mode 100644 index 3c6f779a..00000000 --- a/scripts/mean +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/perl - -$last = $ARGV[-1]; -splice(@ARGV, -1); - -open(PIPE, "ledger -MG @ARGV register $last |") || die; -@values = (); -while (<PIPE>) { - ($date, $value) = split; - push @values, $value; -} -close(PIPE); - -@values = sort @values; -splice(@values, 0, 1); -splice(@values, -1); - -$value = 0.0; -for $item (@values) { - $value += $item; -} - -if (@values) { - printf("%.2f\n", $value / @values); -} else { - die "There are no values to average!\n"; -} diff --git a/scripts/profit b/scripts/profit deleted file mode 100644 index 26f112d1..00000000 --- a/scripts/profit +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -ledger "$@" balance ^Income$ ^Expenses$ diff --git a/scripts/reg b/scripts/reg deleted file mode 100644 index 75ac364e..00000000 --- a/scripts/reg +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh - -switch="-U" -current="-c" - -if [ "$1" = "-C" -o "$1" = "-U" -o "$1" = "-P" -o "$1" = "-M" ]; then - switch="$1" - shift -elif [ "$1" = "-b" -o "$1" = "-e" ]; then - current="$1 $2" - shift 2 -fi - -ledger $current -s $switch register "$@" diff --git a/scripts/report b/scripts/report index aa42982e..24418cdc 100644..100755 --- a/scripts/report +++ b/scripts/report @@ -1,17 +1,21 @@ #!/bin/sh -dir=$HOME/doc/finance +# This script facilities plotting of a ledger register report. If you +# use OS/X, and have AquaTerm installed, you will probably want to set +# LEDGER_TERM to "aqua". +# +# Examples of use: +# +# report -j -M reg food # plot monthly food costs +# report -J reg checking # plot checking account balance -cd /tmp +if [ -z "$LEDGER_TERM" ]; then + LEDGER_TERM="x11 persist" +fi -ledger -M -G register "$@" > $1 - -gnuplot <<EOF -set terminal png -set output "report.png" -set xdata time -set timefmt "%Y/%m/%d" -plot "$1" using 1:2 with linespoints +(cat <<EOF; ledger "$@") | gnuplot + set terminal $LEDGER_TERM + set xdata time + set timefmt "%Y/%m/%d" + plot "-" using 1:2 with lines EOF - -open report.png diff --git a/scripts/spending b/scripts/spending deleted file mode 100644 index 895c4170..00000000 --- a/scripts/spending +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -ledger "$@" balance \ - Expenses:Food \ - Expenses:Movies \ - Expenses:Auto:Gas \ - Expenses:Tips \ - Expenses:Health \ - Expenses:Supplies diff --git a/scripts/tc b/scripts/tc new file mode 100755 index 00000000..c24be99a --- /dev/null +++ b/scripts/tc @@ -0,0 +1,7 @@ +#!/bin/sh + +timeclock out + +proj="$1" +shift +timeclock in "$proj" "$@" diff --git a/scripts/ti b/scripts/ti new file mode 100755 index 00000000..a7214e65 --- /dev/null +++ b/scripts/ti @@ -0,0 +1,5 @@ +#!/bin/sh + +proj="$1" +shift +timeclock in "$proj" "$@" diff --git a/scripts/to b/scripts/to new file mode 100755 index 00000000..3198db3c --- /dev/null +++ b/scripts/to @@ -0,0 +1,3 @@ +#!/bin/sh + +timeclock out "$@" diff --git a/scripts/trend b/scripts/trend new file mode 100755 index 00000000..3c189c0b --- /dev/null +++ b/scripts/trend @@ -0,0 +1,30 @@ +#!/bin/sh + +# This script requires Python support. +# +# To use, just run "trend" with the accounts to compute the trend for: +# +# trend dining +# +# The trend values are not terribly meaningful, but this gives an +# example of how Python can be used to create more complex reports. + +ledger --import-stdin -T "@rdev()" reg "$@" <<EOF +import ledger + +mean = ledger.parse_value_expr ("AT") +last_mean = None +last_dev = None + +def rdev (details): + global last_mean, last_dev + mval = mean.compute (details) + if last_mean is None: + dev = ledger.Value () + else: + dev = mval - last_mean + dev = (last_dev + dev) / 2 + last_mean = mval + last_dev = dev + return dev +EOF diff --git a/scripts/worth b/scripts/worth deleted file mode 100644 index 58b20440..00000000 --- a/scripts/worth +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -ledger "$@" balance ^Assets$ ^Liabilities$ diff --git a/setup.py b/setup.py new file mode 100755 index 00000000..45f39896 --- /dev/null +++ b/setup.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +from distutils.core import setup, Extension + +import os + +libs = ["ledger", "boost_python", "gmp", "pcre"] + +if os.environ.has_key ("HAVE_XMLPARSE") and\ + os.environ["HAVE_XMLPARSE"] == "true": + libs.extend (["xmlparse", "xmltok"]) + +setup(name = "Ledger", + version = "2.0b", + description = "Ledger Accounting Tool", + author = "John Wiegley", + author_email = "johnw@newartisans.com", + url = "http://www.newartisans.com/johnw/", + ext_modules = [ + Extension("ledger", ["pyledger.cc"], + define_macros = [('PYTHON_MODULE', 1)], + libraries = libs)]) diff --git a/textual.cc b/textual.cc new file mode 100644 index 00000000..783cc5de --- /dev/null +++ b/textual.cc @@ -0,0 +1,597 @@ +#include "journal.h" +#include "textual.h" +#include "datetime.h" +#include "valexpr.h" +#include "error.h" +#include "option.h" +#include "config.h" +#include "timing.h" +#include "util.h" +#ifdef USE_BOOST_PYTHON +#include "py_eval.h" +#endif + +#include <fstream> +#include <sstream> +#include <cstring> +#include <ctime> +#include <cctype> + +#define TIMELOG_SUPPORT 1 + +namespace ledger { + +#define MAX_LINE 1024 + +static std::string path; +static unsigned int linenum; + +#ifdef TIMELOG_SUPPORT +static std::time_t time_in; +static account_t * last_account; +static std::string last_desc; +#endif + +inline char * next_element(char * buf, bool variable = false) +{ + for (char * p = buf; *p; p++) { + if (! (*p == ' ' || *p == '\t')) + continue; + + if (! variable) { + *p = '\0'; + return skip_ws(p + 1); + } + else if (*p == '\t') { + *p = '\0'; + return skip_ws(p + 1); + } + else if (*(p + 1) == ' ') { + *p = '\0'; + return skip_ws(p + 2); + } + } + return NULL; +} + +void parse_amount(const char * text, amount_t& amt, unsigned short flags, + transaction_t& xact) +{ + if (*text != '(') { + amt.parse(text, flags); + } else { + value_expr_t * expr = parse_value_expr(text); + value_t result; + expr->compute(result, details_t(xact)); + switch (result.type) { + case value_t::BOOLEAN: + amt = *((bool *) result.data); + break; + case value_t::INTEGER: + amt = *((long *) result.data); + break; + case value_t::AMOUNT: + amt = *((amount_t *) result.data); + break; + + case value_t::BALANCE: + case value_t::BALANCE_PAIR: + throw parse_error(path, linenum, "Value expression yields a balance"); + break; + } + } +} + +transaction_t * parse_transaction(char * line, account_t * account) +{ + // The account will be determined later... + + std::auto_ptr<transaction_t> xact(new transaction_t(NULL)); + + // The call to `next_element' will skip past the account name, + // and return a pointer to the beginning of the amount. Once + // we know where the amount is, we can strip off any + // transaction note, and parse it. + + char * p = skip_ws(line); + if (char * cost_str = next_element(p, true)) { + cost_str = skip_ws(cost_str); + bool has_amount = *cost_str; + + if (char * note_str = std::strchr(cost_str, ';')) { + if (cost_str == note_str) + has_amount = false; + *note_str++ = '\0'; + xact->note = skip_ws(note_str); + } + + if (has_amount) { + bool per_unit = true; + char * price_str = std::strchr(cost_str, '@'); + if (price_str) { + if (price_str == cost_str) + throw parse_error(path, linenum, "Cost specified without amount"); + + *price_str++ = '\0'; + if (*price_str == '@') { + per_unit = false; + price_str++; + } + } + parse_amount(skip_ws(cost_str), xact->amount, AMOUNT_PARSE_NO_REDUCE, + *xact); + if (price_str) { + xact->cost = new amount_t; + parse_amount(skip_ws(price_str), *xact->cost, AMOUNT_PARSE_NO_MIGRATE, + *xact); + } + + if (price_str && per_unit) { + *xact->cost *= xact->amount; + *xact->cost = xact->cost->round(xact->cost->commodity().precision); + } + xact->amount.reduce(); + } + } + + if (*p == '[' || *p == '(') { + xact->flags |= TRANSACTION_VIRTUAL; + if (*p == '[') + xact->flags |= TRANSACTION_BALANCE; + p++; + + char * e = p + (std::strlen(p) - 1); + assert(*e == ')' || *e == ']'); + *e = '\0'; + } + + xact->account = account->find_account(p); + + return xact.release(); +} + +bool parse_transactions(std::istream& in, + account_t * account, + entry_base_t& entry, + const std::string& kind) +{ + static char line[MAX_LINE + 1]; + bool added = false; + + while (! in.eof() && (in.peek() == ' ' || in.peek() == '\t')) { + in.getline(line, MAX_LINE); + if (in.eof()) + break; + linenum++; + if (line[0] == ' ' || line[0] == '\t' || line[0] == '\r') { + char * p = skip_ws(line); + if (! *p || *p == '\r') + break; + } + if (transaction_t * xact = parse_transaction(line, account)) { + entry.add_transaction(xact); + added = true; + } + } + + return added; +} + +namespace { + TIMER_DEF(entry_finish, "finalizing entry"); + TIMER_DEF(entry_xacts, "parsing transactions"); + TIMER_DEF(entry_details, "parsing entry details"); + TIMER_DEF(entry_date, "parsing entry date"); +} + +entry_t * parse_entry(std::istream& in, char * line, account_t * master, + textual_parser_t& parser) +{ + std::auto_ptr<entry_t> curr(new entry_t); + + // Parse the date + + TIMER_START(entry_date); + + char * next = next_element(line); + + if (! quick_parse_date(line, &curr->date)) + throw parse_error(path, linenum, "Failed to parse date"); + + TIMER_STOP(entry_date); + + // Parse the optional cleared flag: * + + TIMER_START(entry_details); + + if (next && *next == '*') { + curr->state = entry_t::CLEARED; + next = skip_ws(++next); + } + + // Parse the optional code: (TEXT) + + if (next && *next == '(') { + if (char * p = std::strchr(next++, ')')) { + *p++ = '\0'; + curr->code = next; + next = skip_ws(p); + } + } + + // Parse the description text + + curr->payee = next ? next : "<Unspecified payee>"; + + TIMER_STOP(entry_details); + + // Parse all of the transactions associated with this entry + + TIMER_START(entry_xacts); + + while (! in.eof() && (in.peek() == ' ' || in.peek() == '\t')) { + in.getline(line, MAX_LINE); + if (in.eof()) + break; + linenum++; + if (line[0] == ' ' || line[0] == '\t' || line[0] == '\r') { + char * p = skip_ws(line); + if (! *p || *p == '\r') + break; + } + if (transaction_t * xact = parse_transaction(line, master)) + curr->add_transaction(xact); + } + + TIMER_STOP(entry_xacts); + + return curr.release(); +} + +template <typename T> +struct push_var { + T& var; + T prev; + push_var(T& _var) : var(_var), prev(var) {} + ~push_var() { var = prev; } +}; + +static inline void parse_symbol(char *& p, std::string& symbol) +{ + if (*p == '"') { + char * q = std::strchr(p + 1, '"'); + if (! q) + throw parse_error(path, linenum, + "Quoted commodity symbol lacks closing quote"); + symbol = std::string(p + 1, 0, q - p - 1); + p = q + 2; + } else { + char * q = std::strchr(p, ' '); + if (q) { + *q = '\0'; + symbol = std::string(p, 0, q - p); + p = q + 1; + } else { + symbol = p; + p += symbol.length(); + } + } + if (symbol.empty()) + throw parse_error(path, linenum, "Failed to parse commodity"); +} + +unsigned int textual_parser_t::parse(std::istream& in, + journal_t * journal, + account_t * master, + const std::string * original_file) +{ + static bool added_auto_entry_hook = false; + static char line[MAX_LINE + 1]; + char c; + unsigned int count = 0; + unsigned int errors = 0; + + std::list<account_t *> account_stack; + auto_entry_finalizer_t auto_entry_finalizer(journal); + + if (! master) + master = journal->master; + + account_stack.push_front(master); + + path = journal->sources.back(); + linenum = 1; + + while (in.good() && ! in.eof()) { + try { + in.getline(line, MAX_LINE); + if (in.eof()) + break; + linenum++; + + switch (line[0]) { + case '\0': + case '\r': + break; + + case ' ': + case '\t': { + char * p = skip_ws(line); + if (*p && *p != '\r') + throw parse_error(path, linenum - 1, "Line begins with whitespace"); + break; + } + +#ifdef TIMELOG_SUPPORT + case 'i': + case 'I': { + std::string date(line, 2, 19); + + char * p = skip_ws(line + 22); + char * n = next_element(p, true); + last_desc = n ? n : ""; + + struct std::tm when; + if (strptime(date.c_str(), "%Y/%m/%d %H:%M:%S", &when)) { + time_in = std::mktime(&when); + last_account = account_stack.front()->find_account(p); + } else { + last_account = NULL; + throw parse_error(path, linenum, "Cannot parse timelog entry date"); + } + break; + } + + case 'o': + case 'O': + if (last_account) { + std::string date(line, 2, 19); + + char * p = skip_ws(line + 22); + if (last_desc.empty() && *p) + last_desc = p; + + struct std::tm when; + if (strptime(date.c_str(), "%Y/%m/%d %H:%M:%S", &when)) { + std::auto_ptr<entry_t> curr(new entry_t); + curr->date = std::mktime(&when); + curr->state = entry_t::CLEARED; + curr->code = ""; + curr->payee = last_desc; + + double diff = std::difftime(curr->date, time_in); + char buf[32]; + std::sprintf(buf, "%lds", long(diff)); + amount_t amt; + amt.parse(buf); + + transaction_t * xact + = new transaction_t(last_account, amt, TRANSACTION_VIRTUAL); + curr->add_transaction(xact); + + if (! journal->add_entry(curr.get())) + throw parse_error(path, linenum, + "Failed to record 'out' timelog entry"); + else + curr.release(); + + count++; + } else { + throw parse_error(path, linenum, "Cannot parse timelog entry date"); + } + + last_account = NULL; + } + break; +#endif // TIMELOG_SUPPORT + + case 'C': // a set of conversions + if (char * p = std::strchr(line + 1, '=')) { + *p++ = '\0'; + parse_conversion(line + 1, p); + } + break; + + case 'P': { // a pricing entry + std::time_t date; + + char * b = skip_ws(line + 1); + char * p = std::strchr(b, ' '); + if (! p) break; + *p++ = '\0'; + + if (! quick_parse_date(b, &date)) + throw parse_error(path, linenum, "Failed to parse date"); + + int hour, min, sec; + + if (! *p) break; hour = (*p++ - '0') * 10; + if (! *p) break; hour += *p++ - '0'; + p++; if (! *p) break; + if (! *p) break; min = (*p++ - '0') * 10; + if (! *p) break; min += *p++ - '0'; + p++; if (! *p) break; + if (! *p) break; sec = (*p++ - '0') * 10; + if (! *p) break; sec += *p++ - '0'; + p++; if (! *p) break; + + date = std::time_t(((unsigned long) date) + + hour * 3600 + min * 60 + sec); + + std::string symbol; + amount_t price; + + parse_symbol(p, symbol); + price.parse(skip_ws(p)); + + commodity_t * commodity = commodity_t::find_commodity(symbol, true); + commodity->add_price(date, price); + break; + } + + case 'N': { // don't download prices + char * p = skip_ws(line + 1); + std::string symbol; + parse_symbol(p, symbol); + + commodity_t * commodity = commodity_t::find_commodity(symbol, true); + commodity->flags |= COMMODITY_STYLE_NOMARKET; + break; + } + + case 'Y': // set the current year + now_year = std::atoi(skip_ws(line + 1)) - 1900; + break; + +#ifdef TIMELOG_SUPPORT + case 'h': + case 'b': +#endif + case ';': // a comment line + break; + + case '-': { // option setting + char * p = std::strchr(line, ' '); + if (! p) + p = std::strchr(line, '='); + if (p) + *p++ = '\0'; + process_option(config_options, line + 2, p ? skip_ws(p) : NULL); + break; + } + + case '=': { // automated entry + if (! added_auto_entry_hook) { + journal->add_entry_finalizer(&auto_entry_finalizer); + added_auto_entry_hook = true; + } + + auto_entry_t * ae = new auto_entry_t(skip_ws(line + 1)); + if (parse_transactions(in, account_stack.front(), *ae, "automated")) { + if (ae->finalize()) + journal->auto_entries.push_back(ae); + else + throw parse_error(path, linenum, + "Automated entry failed to balance"); + } + break; + } + + case '~': { // period entry + period_entry_t * pe = new period_entry_t(skip_ws(line + 1)); + if (! pe->period) + throw parse_error(path, linenum, + std::string("Parsing time period '") + line + "'"); + + if (parse_transactions(in, account_stack.front(), *pe, "period")) { + if (pe->finalize()) { + extend_entry_base(journal, *pe); + journal->period_entries.push_back(pe); + } else { + throw parse_error(path, linenum, "Period entry failed to balance"); + } + } + break; + } + + case '!': { // directive + char * p = std::strchr(line, ' '); + if (p) + *p++ = '\0'; + std::string word(line + 1); + if (word == "include") { + push_var<unsigned int> save_linenum(linenum); + push_var<std::string> save_path(path); + + path = skip_ws(p); + if (path[0] != '/' && path[0] != '\\') { + std::string::size_type pos = save_path.prev.rfind('/'); + if (pos == std::string::npos) + pos = save_path.prev.rfind('\\'); + if (pos != std::string::npos) + path = std::string(save_path.prev, 0, pos + 1) + path; + } + + DEBUG_PRINT("ledger.textual.include", + "Including path '" << path << "'"); + count += parse_journal_file(path, journal, account_stack.front()); + } + else if (word == "account") { + account_t * acct; + acct = account_stack.front()->find_account(skip_ws(p)); + account_stack.push_front(acct); + } + else if (word == "end") { + account_stack.pop_front(); + } +#ifdef USE_BOOST_PYTHON + else if (word == "python") { + python_eval(in, PY_EVAL_MULTI); + } +#endif + break; + } + + default: { + unsigned int first_line = linenum; + if (entry_t * entry = parse_entry(in, line, account_stack.front(), + *this)) { + if (journal->add_entry(entry)) { + count++; + } else { + delete entry; + throw parse_error(path, first_line, "Entry does not balance"); + } + } else { + throw parse_error(path, first_line, "Failed to parse entry"); + } + break; + } + } + } + catch (const parse_error& err) { + std::cerr << "Error: " << err.what() << std::endl; + errors++; + } + catch (const amount_error& err) { + std::cerr << "Error: " << path << ", line " << (linenum - 1) << ": " + << err.what() << std::endl;; + errors++; + } + catch (const error& err) { + std::cerr << "Error: " << path << ", line " << (linenum - 1) << ": " + << err.what() << std::endl;; + errors++; + } + } + + done: + if (added_auto_entry_hook) + journal->remove_entry_finalizer(&auto_entry_finalizer); + + if (errors > 0) + throw error(std::string("Errors parsing file '") + path + "'"); + + return count; +} + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> + +using namespace boost::python; +using namespace ledger; + +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(textual_parse_overloads, + textual_parser_t::parse, 2, 4) + +void export_textual() { + class_< textual_parser_t, bases<parser_t> > ("TextualParser") + .def("test", &textual_parser_t::test) + .def("parse", &textual_parser_t::parse, textual_parse_overloads()) + ; +} + +#endif // USE_BOOST_PYTHON diff --git a/textual.h b/textual.h new file mode 100644 index 00000000..b2f0726f --- /dev/null +++ b/textual.h @@ -0,0 +1,26 @@ +#ifndef _TEXTUAL_H +#define _TEXTUAL_H + +#include "parser.h" + +namespace ledger { + +class textual_parser_t : public parser_t +{ + public: + virtual bool test(std::istream& in) const { + return true; + } + + virtual unsigned int parse(std::istream& in, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL); +}; + +transaction_t * parse_transaction_text(char * line, account_t * account); +transaction_t * parse_transaction(std::istream& in, account_t * account); + +} // namespace ledger + +#endif // _TEXTUAL_H diff --git a/timeclock b/timeclock new file mode 100755 index 00000000..496c3471 --- /dev/null +++ b/timeclock @@ -0,0 +1,461 @@ +#!/usr/bin/env python + +# timeclock, a time-keeping program based on the Ledger library +# +# Copyright (c) 2003-2004, New Artisans LLC. All rights reserved. +# +# This program is made available under the terms of the BSD Public +# License. See the LICENSE file included with the distribution for +# details and disclaimer. +# +# This program implements a simple timeclock, using the identical +# format as my timeclock.el module for Emacs (which is part of the +# Emacs 21 distribution). This allows you to use either this script +# or that module for creating time events. +# +# Usage is very simple: Set the environment variable TIMELOG to the +# path to your timelog file (if this variable is unset, any events +# created will simply be printed to stdout). Once this variable is +# set: +# +# timeclock in "project" what aspect of the project will I do today +# timeclock out what did I accomplish +# +# The description text is optional, but the project is required when +# clocking in. This project should be a account name, which means it +# can use ":" to separate the project from the task, for example: +# +# timeclock in Client:Meetings at the code review meeting +# +# To generate a balance report of time spent, use "timeclock" with no +# arguments, or "timeclock balance". The options available are the +# same as those used for ledger. + +import os +import sys +import string +import time + +true, false = 1, 0 + +from ledger import * + +home = os.getenv ("HOME") +config.init_file = home + "/.timeclockrc"; +config.cache_file = home + "/.timeclock-cache"; + +# Define some functions for reporting time quantities + +workday = 8 * 60 * 60 # length of a nominal workday + +def secstr (secs): + return "%d:%02d" % (abs (secs) / 60 / 60, (abs (secs) / 60) % 60) + +def daystr (amt): + dy = int (amt) / int (workday) + amt = amt - (dy * workday) + if dy >= 5: + wk = dy / 5 + dy = dy % 5 + if dy: amt = "%sw %sd %s" % (wk, dy, secstr (amt)) + else: amt = "%sw %s" % (wk, secstr (amt)) + else: + if dy: amt = "%sd %s" % (dy, secstr (amt)) + else: amt = secstr (amt) + return amt + +def adaystr (details): + result = "" + for amt in account_xdata(details.account).total: + if amt.commodity ().symbol == "s": + result = daystr (float (amt)) + break + return result + +config.amount_expr = "{1.00h}*(a/{3600.00h})" +config.total_expr = "{1.00h}*(O/{3600.00h})" +config.balance_format = "%20@adaystr() %8T %2_%-a\n"; + +# Help text specific to timeclock + +def show_version (arg): + print """Timeclock, a command-line timekeeping tool + +Copyright (c) 2003-2004, New Artisans LLC. All rights reserved. + +This program is made available under the terms of the BSD Public +License. See the LICENSE file included with the distribution for +details and disclaimer.""" + sys.exit (0) + +def option_help (arg): + print """usage: timeclock [options] COMMAND [ACCT REGEX]... + +Basic options: + -h, --help display this help text + -v, --version show version information + -i, --init FILE initialize ledger by loading FILE (def: ~/.ledgerrc) + --cache FILE use FILE as a binary cache when --file is not used + -f, --file FILE read ledger data from FILE + -o, --output FILE write output to FILE + +Report filtering: + -b, --begin DATE set report begin date + -e, --end DATE set report end date + -c, --current show only current and past entries (not future) + -C, --cleared consider only cleared transactions + -U, --uncleared consider only uncleared transactions + -R, --real consider only real (non-virtual) transactions + -Z, --actual consider only actual (non-automated) transactions + -r, --related calculate report using related transactions + +Output customization: + -F, --format STR use STR as the format; for each report type, use: + --balance-format --register-format + --plot-amount-format --plot-total-format + -y, --date-format STR use STR as the date format (def: %Y/%m/%d) + --wide for the default register report, use 132 columns + -E, --empty balance: show accounts with zero balance + -n, --collapse register: collapse entries with multiple transactions + -s, --subtotal balance: show sub-accounts; register: show subtotals + -S, --sort EXPR sort report according to the value expression EXPR + -p, --period STR report using the given period + --period-sort EXPR sort each report period's entries by EXPR + --dow show a days-of-the-week report + -W, --weekly show weekly sub-totals + -M, --monthly show monthly sub-totals + -Y, --yearly show yearly sub-totals + -l, --limit EXPR calculate only transactions matching EXPR + -d, --display EXPR display only transactions matching EXPR + -t, --amount EXPR use EXPR to calculate the displayed amount + -T, --total EXPR use EXPR to calculate the displayed total + -j, --amount-data print only raw amount data (useful for scripting) + -J, --total-data print only raw total data + +Commodity reporting: + -A, --average report average transaction amount + -D, --deviation report deviation from the average + +Commands: + balance [REGEXP]... show balance totals for matching accounts + register [REGEXP]... show register of matching events""" + sys.exit (0) + +# This call registers all of the default command-line options that +# Ledger supports into the option handling mechanism. Skip this call +# if you wish to do all of your own processing -- in which case simply +# modify the 'config' object however you like. + +add_config_option_handlers () + +add_option_handler ("help", "h", option_help) +add_option_handler ("version", "v", show_version) + +# Process the command-line arguments, test whether caching should be +# enabled, and then process any option settings from the execution +# environment. Some historical environment variable names are also +# supported. + +args = process_arguments (sys.argv[1:]) +config.use_cache = not config.data_file +process_environment (os.environ, "TIMECLOCK_") + +if os.environ.has_key ("TIMELOG"): + process_option ("file", os.getenv ("TIMELOG")) + +# The command word is in the first argument. Canonicalize it to a +# unique, simple form that the remaining code can use to find out +# which command was specified. + +if len (args) == 0: + args = ["balance"] + +command = args.pop (0); + +if command == "balance" or command == "bal" or command == "b": + command = "b" +elif command == "register" or command == "reg" or command == "r": + command = "r" +elif command == "entry": + command = "e" +elif command == "in" or command == "out": + if config.data_file: + log = open (config.data_file, "a") + else: + log = sys.stdout + + if command == "in": + if len (args) == 0: + print "A project name is required when clocking in." + sys.exit (1) + log.write ("i %s %s" % (time.strftime ("%Y/%m/%d %H:%M:%S"), + args.pop (0))) + if len (args) > 0: + log.write (" %s\n" % string.join (args, " ")) + else: + log.write ("o %s" % time.strftime ("%Y/%m/%d %H:%M:%S")) + if len (args) > 0: + log.write (" %s" % string.join (args, " ")) + + log.write ("\n") + log.close () + sys.exit (0) +else: + print "Unrecognized command:", command + sys.exit (1) + +# Create the main journal object, into which all entries will be +# recorded. Once done, the 'journal' may be iterated to yield those +# entries, in the same order as which they appeared in the journal +# file. + +journal = Journal () + +# This parser is intended only for timelog files. + +class Event: + def __init__(self, kind, when, desc): + self.kind = kind + self.when = when + self.desc = desc + +class Interval: + def __init__(self, begin, end): + self.begin = begin + self.end = end + + def length(self): + "Return the length of the interval in seconds." + return self.end.when - self.begin.when + +def parse_timelog(path, journal): + import re + if not os.path.exists (path): + print "Cannot read timelog file '%s'" % path + sys.exit (1) + file = open(path) + history = [] + begin = None + linenum = 0 + for line in file: + linenum += 1 + match = re.match("([iIoO])\s+([0-9/]+\s+[0-9:]+)\s*(.+)", line) + if match: + (kind, when, desc) = match.groups() + when = time.strptime(when, "%Y/%m/%d %H:%M:%S") + when = time.mktime(when) + event = Event(kind, when, desc) + if kind == "i" or kind == "I": + begin = event + else: + if begin.desc: + match = re.match ("(.+?) (.+)", begin.desc) + if match: + acct = match.group (1) + desc = match.group (2) + else: + acct = begin.desc + desc = "" + else: + acct = "Misc" + desc = event.desc + + l = Interval(begin, event).length () + e = Entry () + e.date = int (begin.when) + e.payee = desc + + x = Transaction (journal.find_account (acct), + Amount ("%ss" % l), TRANSACTION_VIRTUAL) + e.add_transaction (x) + + if not journal.add_entry (e): + print "%s, %d: Failed to entry" % (path, linenum) + sys.exit (1) + +parse_timelog (config.data_file, journal) + +# Now that everything has been correctly parsed (parse_ledger_data +# would have thrown an exception if not), we can take time to further +# process the configuration options. This changes the configuration a +# bit based on previous option settings, the command word, and the +# remaining arguments. + +if command == "b" and \ + config.amount_expr == "{1.00h}*(a/{3600.00h})": + config.amount_expr = "a" + +config.process_options (command, args); + +# Determine the format string to used, based on the command. + +if config.format_string: + format = config.format_string +elif command == "b": + format = config.balance_format +elif command == "r": + format = config.register_format +else: + format = config.print_format + +# The following two classes are responsible for outputing transactions +# and accounts to the user. There are corresponding C++ versions to +# these, but they rely on I/O streams, which Boost.Python does not +# provide a conversion layer for. + +class FormatTransactions (TransactionHandler): + last_entry = None + output = None + + def __init__ (self, fmt): + try: + i = string.index (fmt, '%/') + self.formatter = Format (fmt[: i]) + self.nformatter = Format (fmt[i + 2 :]) + except ValueError: + self.formatter = Format (fmt) + self.nformatter = None + + self.last_entry = None + + if config.output_file: + self.output = open (config.output_file, "w") + else: + self.output = sys.stdout + + TransactionHandler.__init__ (self) + + def __del__ (self): + if config.output_file: + self.output.close () + + def flush (self): + self.output.flush () + + def __call__ (self, xact): + if not transaction_has_xdata (xact) or \ + not transaction_xdata (xact).dflags & TRANSACTION_DISPLAYED: + if self.nformatter is not None and \ + self.last_entry is not None and \ + xact.entry == self.last_entry: + self.output.write (self.nformatter.format (xact)) + else: + self.output.write (self.formatter.format (xact)) + self.last_entry = xact.entry + transaction_xdata (xact).dflags |= TRANSACTION_DISPLAYED + +class FormatAccounts (AccountHandler): + output = None + + def __init__ (self, fmt, pred): + self.formatter = Format (fmt) + self.predicate = AccountPredicate (pred) + + if config.output_file: + self.output = open (config.output_file, "w") + else: + self.output = sys.stdout + + AccountHandler.__init__ (self) + + def __del__ (self): + if config.output_file: + self.output.close () + + def final (self, account): + if account_has_xdata (account): + xdata = account_xdata (account) + if xdata.dflags & ACCOUNT_TO_DISPLAY: + print "-------------------- ---------" + xdata.value = xdata.total + self.output.write (self.formatter.format (account)) + + def flush (self): + self.output.flush () + + def __call__ (self, account): + if display_account (account, self.predicate): + if not account.parent: + account_xdata (account).dflags |= ACCOUNT_TO_DISPLAY + else: + self.output.write (self.formatter.format (account)) + account_xdata (account).dflags |= ACCOUNT_DISPLAYED + +# Set the final transaction handler: for balances and equity reports, +# it will simply add the value of the transaction to the account's +# xdata, which is used a bit later to report those totals. For all +# other reports, the transaction data is sent to the configured output +# location (default is sys.stdout). + +if command == "b": + handler = SetAccountValue () +else: + handler = FormatTransactions (format) + +# Chain transaction filters on top of the base handler. Most of these +# filters customize the output for reporting. None of this is done +# for balance or equity reports, which don't need it. + +if command != "b": + if config.display_predicate: + handler = FilterTransactions (handler, config.display_predicate) + + handler = CalcTransactions (handler) + + if config.sort_string: + handler = SortTransactions (handler, config.sort_string) + + if config.show_revalued: + handler = ChangedValueTransactions (handler, config.show_revalued_only) + + if config.show_collapsed: + handler = CollapseTransactions (handler); + +if config.show_subtotal and not (command == "b" or command == "E"): + handler = SubtotalTransactions (handler) + +if config.days_of_the_week: + handler = DowTransactions (handler) +elif config.by_payee: + handler = ByPayeeTransactions (handler) + +if config.report_period: + handler = IntervalTransactions (handler, config.report_period, + config.report_period_sort) + handler = SortTransactions (handler, "d") + +# The next two transaction filters are used by all reports. + +if config.show_inverted: + handler = InvertTransactions (handler) + +if config.show_related: + handler = RelatedTransactions (handler, config.show_all_related) + +if config.predicate: + handler = FilterTransactions (handler, config.predicate) + +if config.comm_as_payee: + handler = SetCommAsPayee (handler) + +# Walk the journal's entries, and pass each entry's transaction to the +# handler chain established above. + +walk_entries (journal, handler) + +# Flush the handlers, causing them to output whatever data is still +# pending. + +handler.flush () + +# For the balance and equity reports, the account totals now need to +# be displayed. This is different from outputting transactions, in +# that we are now outputting account totals to display a summary of +# the transactions that were just walked. + +if command == "b": + acct_formatter = FormatAccounts (format, config.display_predicate) + sum_accounts (journal.master) + walk_accounts (journal.master, acct_formatter, config.sort_string) + acct_formatter.final (journal.master) + acct_formatter.flush () diff --git a/timeclock.el b/timeclock.el new file mode 100644 index 00000000..709ea25f --- /dev/null +++ b/timeclock.el @@ -0,0 +1,1362 @@ +;;; timeclock.el --- mode for keeping track of how much you work + +;; Copyright (C) 1999, 2000, 2001, 2003, 2004 Free Software Foundation, Inc. + +;; Author: John Wiegley <johnw@gnu.org> +;; Created: 25 Mar 1999 +;; Version: 2.6 +;; Keywords: calendar data + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 2, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to the +;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, +;; Boston, MA 02111-1307, USA. + +;;; Commentary: + +;; This mode is for keeping track of time intervals. You can use it +;; for whatever purpose you like, but the typical scenario is to keep +;; track of how much time you spend working on certain projects. +;; +;; Use `timeclock-in' when you start on a project, and `timeclock-out' +;; when you're done. Once you've collected some data, you can use +;; `timeclock-workday-remaining' to see how much time is left to be +;; worked today (where `timeclock-workday' specifies the length of the +;; working day), and `timeclock-when-to-leave' to calculate when you're free. + +;; You'll probably want to bind the timeclock commands to some handy +;; keystrokes. At the moment, C-x t is unused: +;; +;; (require 'timeclock) +;; +;; (define-key ctl-x-map "ti" 'timeclock-in) +;; (define-key ctl-x-map "to" 'timeclock-out) +;; (define-key ctl-x-map "tc" 'timeclock-change) +;; (define-key ctl-x-map "tr" 'timeclock-reread-log) +;; (define-key ctl-x-map "tu" 'timeclock-update-modeline) +;; (define-key ctl-x-map "tw" 'timeclock-when-to-leave-string) + +;; If you want Emacs to display the amount of time "left" to your +;; workday in the modeline, you can either set the value of +;; `timeclock-modeline-display' to t using M-x customize, or you +;; can add this code to your .emacs file: +;; +;; (require 'timeclock) +;; (timeclock-modeline-display) +;; +;; To cancel this modeline display at any time, just call +;; `timeclock-modeline-display' again. + +;; You may also want Emacs to ask you before exiting, if you are +;; currently working on a project. This can be done either by setting +;; `timeclock-ask-before-exiting' to t using M-x customize (this is +;; the default), or by adding the following to your .emacs file: +;; +;; (add-hook 'kill-emacs-query-functions 'timeclock-query-out) + +;; NOTE: If you change your .timelog file without using timeclock's +;; functions, or if you change the value of any of timeclock's +;; customizable variables, you should run the command +;; `timeclock-reread-log'. This will recompute any discrepancies in +;; your average working time, and will make sure that the various +;; display functions return the correct value. + +;;; History: + +;;; Code: + +(defgroup timeclock nil + "Keeping track time of the time that gets spent." + :group 'data) + +;;; User Variables: + +(defcustom timeclock-file (convert-standard-filename "~/.timelog") + "*The file used to store timeclock data in." + :type 'file + :group 'timeclock) + +(defcustom timeclock-workday (* 8 60 60) + "*The length of a work period." + :type 'integer + :group 'timeclock) + +(defcustom timeclock-relative t + "*Whether to maken reported time relative to `timeclock-workday'. +For example, if the length of a normal workday is eight hours, and you +work four hours on Monday, then the amount of time \"remaining\" on +Tuesday is twelve hours -- relative to an averaged work period of +eight hours -- or eight hours, non-relative. So relative time takes +into account any discrepancy of time under-worked or over-worked on +previous days. This only affects the timeclock modeline display." + :type 'boolean + :group 'timeclock) + +(defcustom timeclock-get-project-function 'timeclock-ask-for-project + "*The function used to determine the name of the current project. +When clocking in, and no project is specified, this function will be +called to determine what is the current project to be worked on. +If this variable is nil, no questions will be asked." + :type 'function + :group 'timeclock) + +(defcustom timeclock-get-reason-function 'timeclock-ask-for-reason + "*A function used to determine the reason for clocking out. +When clocking out, and no reason is specified, this function will be +called to determine what is the reason. +If this variable is nil, no questions will be asked." + :type 'function + :group 'timeclock) + +(defcustom timeclock-get-workday-function nil + "*A function used to determine the length of today's workday. +The first time that a user clocks in each day, this function will be +called to determine what is the length of the current workday. If +the return value is nil, or equal to `timeclock-workday', nothing special +will be done. If it is a quantity different from `timeclock-workday', +however, a record will be output to the timelog file to note the fact that +that day has a length that is different from the norm." + :type '(choice (const nil) function) + :group 'timeclock) + +(defcustom timeclock-ask-before-exiting t + "*If non-nil, ask if the user wants to clock out before exiting Emacs. +This variable only has effect if set with \\[customize]." + :set (lambda (symbol value) + (if value + (add-hook 'kill-emacs-query-functions 'timeclock-query-out) + (remove-hook 'kill-emacs-query-functions 'timeclock-query-out)) + (setq timeclock-ask-before-exiting value)) + :type 'boolean + :group 'timeclock) + +(defvar timeclock-update-timer nil + "The timer used to update `timeclock-mode-string'.") + +;; For byte-compiler. +(defvar display-time-hook) +(defvar timeclock-modeline-display) + +(defcustom timeclock-use-display-time t + "*If non-nil, use `display-time-hook' for doing modeline updates. +The advantage of this is that one less timer has to be set running +amok in Emacs' process space. The disadvantage is that it requires +you to have `display-time' running. If you don't want to use +`display-time', but still want the modeline to show how much time is +left, set this variable to nil. Changing the value of this variable +while timeclock information is being displayed in the modeline has no +effect. You should call the function `timeclock-modeline-display' with +a positive argument to force an update." + :set (lambda (symbol value) + (let ((currently-displaying + (and (boundp 'timeclock-modeline-display) + timeclock-modeline-display))) + ;; if we're changing to the state that + ;; `timeclock-modeline-display' is already using, don't + ;; bother toggling it. This happens on the initial loading + ;; of timeclock.el. + (if (and currently-displaying + (or (and value + (boundp 'display-time-hook) + (memq 'timeclock-update-modeline + display-time-hook)) + (and (not value) + timeclock-update-timer))) + (setq currently-displaying nil)) + (and currently-displaying + (set-variable 'timeclock-modeline-display nil)) + (setq timeclock-use-display-time value) + (and currently-displaying + (set-variable 'timeclock-modeline-display t)) + timeclock-use-display-time)) + :type 'boolean + :group 'timeclock + :require 'time) + +(defcustom timeclock-first-in-hook nil + "*A hook run for the first \"in\" event each day. +Note that this hook is run before recording any events. Thus the +value of `timeclock-hours-today', `timeclock-last-event' and the +return value of function `timeclock-last-period' are relative previous +to today." + :type 'hook + :group 'timeclock) + +(defcustom timeclock-load-hook nil + "*Hook that gets run after timeclock has been loaded." + :type 'hook + :group 'timeclock) + +(defcustom timeclock-in-hook nil + "*A hook run every time an \"in\" event is recorded." + :type 'hook + :group 'timeclock) + +(defcustom timeclock-day-over-hook nil + "*A hook that is run when the workday has been completed. +This hook is only run if the current time remaining is being displayed +in the modeline. See the variable `timeclock-modeline-display'." + :type 'hook + :group 'timeclock) + +(defcustom timeclock-out-hook nil + "*A hook run every time an \"out\" event is recorded." + :type 'hook + :group 'timeclock) + +(defcustom timeclock-done-hook nil + "*A hook run every time a project is marked as completed." + :type 'hook + :group 'timeclock) + +(defcustom timeclock-event-hook nil + "*A hook run every time any event is recorded." + :type 'hook + :group 'timeclock) + +(defvar timeclock-last-event nil + "A list containing the last event that was recorded. +The format of this list is (CODE TIME PROJECT).") + +(defvar timeclock-last-event-workday nil + "The number of seconds in the workday of `timeclock-last-event'.") + +;;; Internal Variables: + +(defvar timeclock-discrepancy nil + "A variable containing the time discrepancy before the last event. +Normally, timeclock assumes that you intend to work for +`timeclock-workday' seconds every day. Any days in which you work +more or less than this amount is considered either a positive or +a negative discrepancy. If you work in such a manner that the +discrepancy is always brought back to zero, then you will by +definition have worked an average amount equal to `timeclock-workday' +each day.") + +(defvar timeclock-elapsed nil + "A variable containing the time elapsed for complete periods today. +This value is not accurate enough to be useful by itself. Rather, +call `timeclock-workday-elapsed', to determine how much time has been +worked so far today. Also, if `timeclock-relative' is nil, this value +will be the same as `timeclock-discrepancy'.") ; ? gm + +(defvar timeclock-last-period nil + "Integer representing the number of seconds in the last period. +Note that you shouldn't access this value, but instead should use the +function `timeclock-last-period'.") + +(defvar timeclock-mode-string nil + "The timeclock string (optionally) displayed in the modeline. +The time is bracketed by <> if you are clocked in, otherwise by [].") + +(defvar timeclock-day-over nil + "The date of the last day when notified \"day over\" for.") + +;;; User Functions: + +;;;###autoload +(defun timeclock-modeline-display (&optional arg) + "Toggle display of the amount of time left today in the modeline. +If `timeclock-use-display-time' is non-nil (the default), then +the function `display-time-mode' must be active, and the modeline +will be updated whenever the time display is updated. Otherwise, +the timeclock will use its own sixty second timer to do its +updating. With prefix ARG, turn modeline display on if and only +if ARG is positive. Returns the new status of timeclock modeline +display (non-nil means on)." + (interactive "P") + ;; cf display-time-mode. + (setq timeclock-mode-string "") + (or global-mode-string (setq global-mode-string '(""))) + (let ((on-p (if arg + (> (prefix-numeric-value arg) 0) + (not timeclock-modeline-display)))) + (if on-p + (progn + (or (memq 'timeclock-mode-string global-mode-string) + (setq global-mode-string + (append global-mode-string '(timeclock-mode-string)))) + (unless (memq 'timeclock-update-modeline timeclock-event-hook) + (add-hook 'timeclock-event-hook 'timeclock-update-modeline)) + (when timeclock-update-timer + (cancel-timer timeclock-update-timer) + (setq timeclock-update-timer nil)) + (if (boundp 'display-time-hook) + (remove-hook 'display-time-hook 'timeclock-update-modeline)) + (if timeclock-use-display-time + (progn + ;; Update immediately so there is a visible change + ;; on calling this function. + (if display-time-mode (timeclock-update-modeline) + (message "Activate `display-time-mode' to see \ +timeclock information")) + (add-hook 'display-time-hook 'timeclock-update-modeline)) + (setq timeclock-update-timer + (run-at-time nil 60 'timeclock-update-modeline)))) + (setq global-mode-string + (delq 'timeclock-mode-string global-mode-string)) + (remove-hook 'timeclock-event-hook 'timeclock-update-modeline) + (if (boundp 'display-time-hook) + (remove-hook 'display-time-hook + 'timeclock-update-modeline)) + (when timeclock-update-timer + (cancel-timer timeclock-update-timer) + (setq timeclock-update-timer nil))) + (force-mode-line-update) + (setq timeclock-modeline-display on-p))) + +;; This has to be here so that the function definition of +;; `timeclock-modeline-display' is known to the "set" function. +(defcustom timeclock-modeline-display nil + "Toggle modeline display of time remaining. +You must modify via \\[customize] for this variable to have an effect." + :set (lambda (symbol value) + (setq timeclock-modeline-display + (timeclock-modeline-display (or value 0)))) + :type 'boolean + :group 'timeclock + :require 'timeclock) + +(defsubst timeclock-time-to-date (time) + "Convert the TIME value to a textual date string." + (format-time-string "%Y/%m/%d" time)) + +;;;###autoload +(defun timeclock-in (&optional arg project find-project) + "Clock in, recording the current time moment in the timelog. +With a numeric prefix ARG, record the fact that today has only that +many hours in it to be worked. If arg is a non-numeric prefix arg +\(non-nil, but not a number), 0 is assumed (working on a holiday or +weekend). *If not called interactively, ARG should be the number of +_seconds_ worked today*. This feature only has effect the first time +this function is called within a day. + +PROJECT is the project being clocked into. If PROJECT is nil, and +FIND-PROJECT is non-nil -- or the user calls `timeclock-in' +interactively -- call the function `timeclock-get-project-function' to +discover the name of the project." + (interactive + (list (and current-prefix-arg + (if (numberp current-prefix-arg) + (* current-prefix-arg 60 60) + 0)))) + (if (equal (car timeclock-last-event) "i") + (error "You've already clocked in!") + (unless timeclock-last-event + (timeclock-reread-log)) + ;; Either no log file, or day has rolled over. + (unless (and timeclock-last-event + (equal (timeclock-time-to-date + (cadr timeclock-last-event)) + (timeclock-time-to-date (current-time)))) + (let ((workday (or (and (numberp arg) arg) + (and arg 0) + (and timeclock-get-workday-function + (funcall timeclock-get-workday-function)) + timeclock-workday))) + (run-hooks 'timeclock-first-in-hook) + ;; settle the discrepancy for the new day + (setq timeclock-discrepancy + (- (or timeclock-discrepancy 0) workday)) + (if (not (= workday timeclock-workday)) + (timeclock-log "h" (and (numberp arg) + (number-to-string arg)))))) + (timeclock-log "i" (or project + (and timeclock-get-project-function + (or find-project (interactive-p)) + (funcall timeclock-get-project-function)))) + (run-hooks 'timeclock-in-hook))) + +;;;###autoload +(defun timeclock-out (&optional arg reason find-reason) + "Clock out, recording the current time moment in the timelog. +If a prefix ARG is given, the user has completed the project that was +begun during the last time segment. + +REASON is the user's reason for clocking out. If REASON is nil, and +FIND-REASON is non-nil -- or the user calls `timeclock-out' +interactively -- call the function `timeclock-get-reason-function' to +discover the reason." + (interactive "P") + (or timeclock-last-event + (error "You haven't clocked in!")) + (if (equal (downcase (car timeclock-last-event)) "o") + (error "You've already clocked out!") + (timeclock-log + (if arg "O" "o") + (or reason + (and timeclock-get-reason-function + (or find-reason (interactive-p)) + (funcall timeclock-get-reason-function)))) + (run-hooks 'timeclock-out-hook) + (if arg + (run-hooks 'timeclock-done-hook)))) + +;; Should today-only be removed in favour of timeclock-relative? - gm +(defsubst timeclock-workday-remaining (&optional today-only) + "Return the number of seconds until the workday is complete. +The amount returned is relative to the value of `timeclock-workday'. +If TODAY-ONLY is non-nil, the value returned will be relative only to +the time worked today, and not to past time." + (let ((discrep (timeclock-find-discrep))) + (if discrep + (- (if today-only (cadr discrep) + (car discrep))) + 0.0))) + +;;;###autoload +(defun timeclock-status-string (&optional show-seconds today-only) + "Report the overall timeclock status at the present moment. +If SHOW-SECONDS is non-nil, display second resolution. +If TODAY-ONLY is non-nil, the display will be relative only to time +worked today, ignoring the time worked on previous days." + (interactive "P") + (let ((remainder (timeclock-workday-remaining)) ; today-only? + (last-in (equal (car timeclock-last-event) "i")) + status) + (setq status + (format "Currently %s since %s (%s), %s %s, leave at %s" + (if last-in "IN" "OUT") + (if show-seconds + (format-time-string "%-I:%M:%S %p" + (nth 1 timeclock-last-event)) + (format-time-string "%-I:%M %p" + (nth 1 timeclock-last-event))) + (or (nth 2 timeclock-last-event) + (if last-in "**UNKNOWN**" "workday over")) + (timeclock-seconds-to-string remainder show-seconds t) + (if (> remainder 0) + "remaining" "over") + (timeclock-when-to-leave-string show-seconds today-only))) + (if (interactive-p) + (message status) + status))) + +;;;###autoload +(defun timeclock-change (&optional arg project) + "Change to working on a different project. +This clocks out of the current project, then clocks in on a new one. +With a prefix ARG, consider the previous project as finished at the +time of changeover. PROJECT is the name of the last project you were +working on." + (interactive "P") + (timeclock-out arg) + (timeclock-in nil project (interactive-p))) + +;;;###autoload +(defun timeclock-query-out () + "Ask the user whether to clock out. +This is a useful function for adding to `kill-emacs-query-functions'." + (and (equal (car timeclock-last-event) "i") + (y-or-n-p "You're currently clocking time, clock out? ") + (timeclock-out)) + ;; Unconditionally return t for `kill-emacs-query-functions'. + t) + +;;;###autoload +(defun timeclock-reread-log () + "Re-read the timeclock, to account for external changes. +Returns the new value of `timeclock-discrepancy'." + (interactive) + (setq timeclock-discrepancy nil) + (timeclock-find-discrep) + (if (and timeclock-discrepancy timeclock-modeline-display) + (timeclock-update-modeline)) + timeclock-discrepancy) + +(defun timeclock-seconds-to-string (seconds &optional show-seconds + reverse-leader) + "Convert SECONDS into a compact time string. +If SHOW-SECONDS is non-nil, make the resolution of the return string +include the second count. If REVERSE-LEADER is non-nil, it means to +output a \"+\" if the time value is negative, rather than a \"-\". +This is used when negative time values have an inverted meaning (such +as with time remaining, where negative time really means overtime)." + (if show-seconds + (format "%s%d:%02d:%02d" + (if (< seconds 0) (if reverse-leader "+" "-") "") + (truncate (/ (abs seconds) 60 60)) + (% (truncate (/ (abs seconds) 60)) 60) + (% (truncate (abs seconds)) 60)) + (format "%s%d:%02d" + (if (< seconds 0) (if reverse-leader "+" "-") "") + (truncate (/ (abs seconds) 60 60)) + (% (truncate (/ (abs seconds) 60)) 60)))) + +(defsubst timeclock-currently-in-p () + "Return non-nil if the user is currently clocked in." + (equal (car timeclock-last-event) "i")) + +;;;###autoload +(defun timeclock-workday-remaining-string (&optional show-seconds + today-only) + "Return a string representing the amount of time left today. +Display second resolution if SHOW-SECONDS is non-nil. If TODAY-ONLY +is non-nil, the display will be relative only to time worked today. +See `timeclock-relative' for more information about the meaning of +\"relative to today\"." + (interactive) + (let ((string (timeclock-seconds-to-string + (timeclock-workday-remaining today-only) + show-seconds t))) + (if (interactive-p) + (message string) + string))) + +(defsubst timeclock-workday-elapsed () + "Return the number of seconds worked so far today. +If RELATIVE is non-nil, the amount returned will be relative to past +time worked. The default is to return only the time that has elapsed +so far today." + (let ((discrep (timeclock-find-discrep))) + (if discrep + (nth 2 discrep) + 0.0))) + +;;;###autoload +(defun timeclock-workday-elapsed-string (&optional show-seconds) + "Return a string representing the amount of time worked today. +Display seconds resolution if SHOW-SECONDS is non-nil. If RELATIVE is +non-nil, the amount returned will be relative to past time worked." + (interactive) + (let ((string (timeclock-seconds-to-string (timeclock-workday-elapsed) + show-seconds))) + (if (interactive-p) + (message string) + string))) + +(defsubst timeclock-time-to-seconds (time) + "Convert TIME to a floating point number." + (+ (* (car time) 65536.0) + (cadr time) + (/ (or (car (cdr (cdr time))) 0) 1000000.0))) + +(defsubst timeclock-seconds-to-time (seconds) + "Convert SECONDS (a floating point number) to an Emacs time structure." + (list (floor seconds 65536) + (floor (mod seconds 65536)) + (floor (* (- seconds (ffloor seconds)) 1000000)))) + +;; Should today-only be removed in favour of timeclock-relative? - gm +(defsubst timeclock-when-to-leave (&optional today-only) + "Return a time value representing the end of today's workday. +If TODAY-ONLY is non-nil, the value returned will be relative only to +the time worked today, and not to past time." + (timeclock-seconds-to-time + (- (timeclock-time-to-seconds (current-time)) + (let ((discrep (timeclock-find-discrep))) + (if discrep + (if today-only + (cadr discrep) + (car discrep)) + 0.0))))) + +;;;###autoload +(defun timeclock-when-to-leave-string (&optional show-seconds + today-only) + "Return a string representing the end of today's workday. +This string is relative to the value of `timeclock-workday'. If +SHOW-SECONDS is non-nil, the value printed/returned will include +seconds. If TODAY-ONLY is non-nil, the value returned will be +relative only to the time worked today, and not to past time." + ;; Should today-only be removed in favour of timeclock-relative? - gm + (interactive) + (let* ((then (timeclock-when-to-leave today-only)) + (string + (if show-seconds + (format-time-string "%-I:%M:%S %p" then) + (format-time-string "%-I:%M %p" then)))) + (if (interactive-p) + (message string) + string))) + +;;; Internal Functions: + +(defvar timeclock-project-list nil) +(defvar timeclock-last-project nil) + +(defun timeclock-completing-read (prompt alist &optional default) + "A version of `completing-read' that works on both Emacs and XEmacs." + (if (featurep 'xemacs) + (let ((str (completing-read prompt alist))) + (if (or (null str) (= (length str) 0)) + default + str)) + (completing-read prompt alist nil nil nil nil default))) + +(defun timeclock-ask-for-project () + "Ask the user for the project they are clocking into." + (timeclock-completing-read + (format "Clock into which project (default \"%s\"): " + (or timeclock-last-project + (car timeclock-project-list))) + (mapcar 'list timeclock-project-list) + (or timeclock-last-project + (car timeclock-project-list)))) + +(defvar timeclock-reason-list nil) + +(defun timeclock-ask-for-reason () + "Ask the user for the reason they are clocking out." + (timeclock-completing-read "Reason for clocking out: " + (mapcar 'list timeclock-reason-list))) + +(defun timeclock-update-modeline () + "Update the `timeclock-mode-string' displayed in the modeline. +The value of `timeclock-relative' affects the display as described in +that variable's documentation." + (interactive) + (let ((remainder (timeclock-workday-remaining (not timeclock-relative))) + (last-in (equal (car timeclock-last-event) "i"))) + (when (and (< remainder 0) + (not (and timeclock-day-over + (equal timeclock-day-over + (timeclock-time-to-date + (current-time)))))) + (setq timeclock-day-over + (timeclock-time-to-date (current-time))) + (run-hooks 'timeclock-day-over-hook)) + (setq timeclock-mode-string + (propertize + (format " %c%s%c " + (if last-in ?< ?[) + (timeclock-seconds-to-string remainder nil t) + (if last-in ?> ?])) + 'help-echo "timeclock: time remaining")))) + +(put 'timeclock-mode-string 'risky-local-variable t) + +(defun timeclock-log (code &optional project) + "Log the event CODE to the timeclock log, at the time of call. +If PROJECT is a string, it represents the project which the event is +being logged for. Normally only \"in\" events specify a project." + (with-current-buffer (find-file-noselect timeclock-file) + (goto-char (point-max)) + (if (not (bolp)) + (insert "\n")) + (let ((now (current-time))) + (insert code " " + (format-time-string "%Y/%m/%d %H:%M:%S" now) + (or (and project + (stringp project) + (> (length project) 0) + (concat " " project)) + "") + "\n") + (if (equal (downcase code) "o") + (setq timeclock-last-period + (- (timeclock-time-to-seconds now) + (timeclock-time-to-seconds + (cadr timeclock-last-event))) + timeclock-discrepancy + (+ timeclock-discrepancy + timeclock-last-period))) + (setq timeclock-last-event (list code now project))) + (save-buffer) + (run-hooks 'timeclock-event-hook) + (kill-buffer (current-buffer)))) + +(defvar timeclock-moment-regexp + (concat "\\([bhioO]\\)\\s-+" + "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)\\s-+" + "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)[ \t]*" "\\([^\n]*\\)")) + +(defsubst timeclock-read-moment () + "Read the moment under point from the timelog." + (if (looking-at timeclock-moment-regexp) + (let ((code (match-string 1)) + (year (string-to-number (match-string 2))) + (mon (string-to-number (match-string 3))) + (mday (string-to-number (match-string 4))) + (hour (string-to-number (match-string 5))) + (min (string-to-number (match-string 6))) + (sec (string-to-number (match-string 7))) + (project (match-string 8))) + (list code (encode-time sec min hour mday mon year) project)))) + +(defun timeclock-last-period (&optional moment) + "Return the value of the last event period. +If the last event was a clock-in, the period will be open ended, and +growing every second. Otherwise, it is a fixed amount which has been +recorded to disk. If MOMENT is non-nil, use that as the current time. +This is only provided for coherency when used by +`timeclock-discrepancy'." + (if (equal (car timeclock-last-event) "i") + (- (timeclock-time-to-seconds (or moment (current-time))) + (timeclock-time-to-seconds + (cadr timeclock-last-event))) + timeclock-last-period)) + +(defsubst timeclock-entry-length (entry) + (- (timeclock-time-to-seconds (cadr entry)) + (timeclock-time-to-seconds (car entry)))) + +(defsubst timeclock-entry-begin (entry) + (car entry)) + +(defsubst timeclock-entry-end (entry) + (cadr entry)) + +(defsubst timeclock-entry-project (entry) + (nth 2 entry)) + +(defsubst timeclock-entry-comment (entry) + (nth 3 entry)) + + +(defsubst timeclock-entry-list-length (entry-list) + (let ((length 0)) + (while entry-list + (setq length (+ length (timeclock-entry-length (car entry-list)))) + (setq entry-list (cdr entry-list))) + length)) + +(defsubst timeclock-entry-list-begin (entry-list) + (timeclock-entry-begin (car entry-list))) + +(defsubst timeclock-entry-list-end (entry-list) + (timeclock-entry-end (car (last entry-list)))) + +(defsubst timeclock-entry-list-span (entry-list) + (- (timeclock-time-to-seconds (timeclock-entry-list-end entry-list)) + (timeclock-time-to-seconds (timeclock-entry-list-begin entry-list)))) + +(defsubst timeclock-entry-list-break (entry-list) + (- (timeclock-entry-list-span entry-list) + (timeclock-entry-list-length entry-list))) + +(defsubst timeclock-entry-list-projects (entry-list) + (let (projects) + (while entry-list + (let ((project (timeclock-entry-project (car entry-list)))) + (if projects + (add-to-list 'projects project) + (setq projects (list project)))) + (setq entry-list (cdr entry-list))) + projects)) + + +(defsubst timeclock-day-required (day) + (or (car day) timeclock-workday)) + +(defsubst timeclock-day-length (day) + (timeclock-entry-list-length (cdr day))) + +(defsubst timeclock-day-debt (day) + (- (timeclock-day-required day) + (timeclock-day-length day))) + +(defsubst timeclock-day-begin (day) + (timeclock-entry-list-begin (cdr day))) + +(defsubst timeclock-day-end (day) + (timeclock-entry-list-end (cdr day))) + +(defsubst timeclock-day-span (day) + (timeclock-entry-list-span (cdr day))) + +(defsubst timeclock-day-break (day) + (timeclock-entry-list-break (cdr day))) + +(defsubst timeclock-day-projects (day) + (timeclock-entry-list-projects (cdr day))) + +(defmacro timeclock-day-list-template (func) + `(let ((length 0)) + (while day-list + (setq length (+ length (,(eval func) (car day-list)))) + (setq day-list (cdr day-list))) + length)) + +(defun timeclock-day-list-required (day-list) + (timeclock-day-list-template 'timeclock-day-required)) + +(defun timeclock-day-list-length (day-list) + (timeclock-day-list-template 'timeclock-day-length)) + +(defun timeclock-day-list-debt (day-list) + (timeclock-day-list-template 'timeclock-day-debt)) + +(defsubst timeclock-day-list-begin (day-list) + (timeclock-day-begin (car day-list))) + +(defsubst timeclock-day-list-end (day-list) + (timeclock-day-end (car (last day-list)))) + +(defun timeclock-day-list-span (day-list) + (timeclock-day-list-template 'timeclock-day-span)) + +(defun timeclock-day-list-break (day-list) + (timeclock-day-list-template 'timeclock-day-break)) + +(defun timeclock-day-list-projects (day-list) + (let (projects) + (while day-list + (let ((projs (timeclock-day-projects (car day-list)))) + (while projs + (if projects + (add-to-list 'projects (car projs)) + (setq projects (list (car projs)))) + (setq projs (cdr projs)))) + (setq day-list (cdr day-list))) + projects)) + + +(defsubst timeclock-current-debt (&optional log-data) + (nth 0 (or log-data (timeclock-log-data)))) + +(defsubst timeclock-day-alist (&optional log-data) + (nth 1 (or log-data (timeclock-log-data)))) + +(defun timeclock-day-list (&optional log-data) + (let ((alist (timeclock-day-alist log-data)) + day-list) + (while alist + (setq day-list (cons (cdar alist) day-list) + alist (cdr alist))) + day-list)) + +(defsubst timeclock-project-alist (&optional log-data) + (nth 2 (or log-data (timeclock-log-data)))) + + +(defun timeclock-log-data (&optional recent-only filename) + "Return the contents of the timelog file, in a useful format. +If the optional argument RECENT-ONLY is non-nil, only show the contents +from the last point where the time debt (see below) was set. +If the optional argument FILENAME is non-nil, it is used instead of +the file specified by `timeclock-file.' + +A timelog contains data in the form of a single entry per line. +Each entry has the form: + + CODE YYYY/MM/DD HH:MM:SS [COMMENT] + +CODE is one of: b, h, i, o or O. COMMENT is optional when the code is +i, o or O. The meanings of the codes are: + + b Set the current time balance, or \"time debt\". Useful when + archiving old log data, when a debt must be carried forward. + The COMMENT here is the number of seconds of debt. + + h Set the required working time for the given day. This must + be the first entry for that day. The COMMENT in this case is + the number of hours in this workday. Floating point amounts + are allowed. + + i Clock in. The COMMENT in this case should be the name of the + project worked on. + + o Clock out. COMMENT is unnecessary, but can be used to provide + a description of how the period went, for example. + + O Final clock out. Whatever project was being worked on, it is + now finished. Useful for creating summary reports. + +When this function is called, it will return a data structure with the +following format: + + (DEBT ENTRIES-BY-DAY ENTRIES-BY-PROJECT) + +DEBT is a floating point number representing the number of seconds +\"owed\" before any work was done. For a new file (one without a 'b' +entry), this is always zero. + +The two entries lists have similar formats. They are both alists, +where the CAR is the index, and the CDR is a list of time entries. +For ENTRIES-BY-DAY, the CAR is a textual date string, of the form +YYYY/MM/DD. For ENTRIES-BY-PROJECT, it is the name of the project +worked on, or t for the default project. + +The CDR for ENTRIES-BY-DAY is slightly different than for +ENTRIES-BY-PROJECT. It has the following form: + + (DAY-LENGTH TIME-ENTRIES...) + +For ENTRIES-BY-PROJECT, there is no DAY-LENGTH member. It is simply a +list of TIME-ENTRIES. Note that if DAY-LENGTH is nil, it means +whatever is the default should be used. + +A TIME-ENTRY is a recorded time interval. It has the following format +\(although generally one does not have to manipulate these entries +directly; see below): + + (BEGIN-TIME END-TIME PROJECT [COMMENT] [FINAL-P]) + +Anyway, suffice it to say there are a lot of structures. Typically +the user is expected to manipulate to the day(s) or project(s) that he +or she wants, at which point the following helper functions may be +used: + + timeclock-day-required + timeclock-day-length + timeclock-day-debt + timeclock-day-begin + timeclock-day-end + timeclock-day-span + timeclock-day-break + timeclock-day-projects + + timeclock-day-list-required + timeclock-day-list-length + timeclock-day-list-debt + timeclock-day-list-begin + timeclock-day-list-end + timeclock-day-list-span + timeclock-day-list-break + timeclock-day-list-projects + + timeclock-entry-length + timeclock-entry-begin + timeclock-entry-end + timeclock-entry-project + timeclock-entry-comment + + timeclock-entry-list-length + timeclock-entry-list-begin + timeclock-entry-list-end + timeclock-entry-list-span + timeclock-entry-list-break + timeclock-entry-list-projects + +A few comments should make the use of the above functions obvious: + + `required' is the amount of time that must be spent during a day, or + sequence of days, in order to have no debt. + + `length' is the actual amount of time that was spent. + + `debt' is the difference between required time and length. A + negative debt signifies overtime. + + `begin' is the earliest moment at which work began. + + `end' is the final moment work was done. + + `span' is the difference between begin and end. + + `break' is the difference between span and length. + + `project' is the project that was worked on, and `projects' is a + list of all the projects that were worked on during a given period. + + `comment', where it applies, could mean anything. + +There are a few more functions available, for locating day and entry +lists: + + timeclock-day-alist LOG-DATA + timeclock-project-alist LOG-DATA + timeclock-current-debt LOG-DATA + +See the documentation for the given function if more info is needed." + (let* ((log-data (list 0.0 nil nil)) + (now (current-time)) + (todays-date (timeclock-time-to-date now)) + last-date-limited last-date-seconds last-date + (line 0) last beg day entry event) + (with-temp-buffer + (insert-file-contents (or filename timeclock-file)) + (when recent-only + (goto-char (point-max)) + (unless (re-search-backward "^b\\s-+" nil t) + (goto-char (point-min)))) + (while (or (setq event (timeclock-read-moment)) + (and beg (not last) + (setq last t event (list "o" now)))) + (setq line (1+ line)) + (cond ((equal (car event) "b") + (setcar log-data (string-to-number (nth 2 event)))) + ((equal (car event) "h") + (setq last-date-limited (timeclock-time-to-date (cadr event)) + last-date-seconds (* (string-to-number (nth 2 event)) + 3600.0))) + ((equal (car event) "i") + (if beg + (error "Error in format of timelog file, line %d" line) + (setq beg t)) + (setq entry (list (cadr event) nil + (and (> (length (nth 2 event)) 0) + (nth 2 event)))) + (let ((date (timeclock-time-to-date (cadr event)))) + (if (and last-date + (not (equal date last-date))) + (progn + (setcar (cdr log-data) + (cons (cons last-date day) + (cadr log-data))) + (setq day (list (and last-date-limited + last-date-seconds)))) + (unless day + (setq day (list (and last-date-limited + last-date-seconds))))) + (setq last-date date + last-date-limited nil))) + ((equal (downcase (car event)) "o") + (if (not beg) + (error "Error in format of timelog file, line %d" line) + (setq beg nil)) + (setcar (cdr entry) (cadr event)) + (let ((desc (and (> (length (nth 2 event)) 0) + (nth 2 event)))) + (if desc + (nconc entry (list (nth 2 event)))) + (if (equal (car event) "O") + (nconc entry (if desc + (list t) + (list nil t)))) + (nconc day (list entry)) + (setq desc (nth 2 entry)) + (let ((proj (assoc desc (nth 2 log-data)))) + (if (null proj) + (setcar (cddr log-data) + (cons (cons desc (list entry)) + (car (cddr log-data)))) + (nconc (cdr proj) (list entry))))))) + (forward-line)) + (if day + (setcar (cdr log-data) + (cons (cons last-date day) + (cadr log-data)))) + log-data))) + +(defun timeclock-find-discrep () + "Calculate time discrepancies, in seconds. +The result is a three element list, containing the total time +discrepancy, today's discrepancy, and the time worked today." + ;; This is not implemented in terms of the functions above, because + ;; it's a bit wasteful to read all of that data in, just to throw + ;; away more than 90% of the information afterwards. + ;; + ;; If it were implemented using those functions, it would look + ;; something like this: + ;; (let ((days (timeclock-day-alist (timeclock-log-data))) + ;; (total 0.0)) + ;; (while days + ;; (setq total (+ total (- (timeclock-day-length (cdar days)) + ;; (timeclock-day-required (cdar days)))) + ;; days (cdr days))) + ;; total) + (let* ((now (current-time)) + (todays-date (timeclock-time-to-date now)) + (first t) (accum 0) (elapsed 0) + event beg last-date avg + last-date-limited last-date-seconds) + (unless timeclock-discrepancy + (when (file-readable-p timeclock-file) + (setq timeclock-project-list nil + timeclock-last-project nil + timeclock-reason-list nil + timeclock-elapsed 0) + (with-temp-buffer + (insert-file-contents timeclock-file) + (goto-char (point-max)) + (unless (re-search-backward "^b\\s-+" nil t) + (goto-char (point-min))) + (while (setq event (timeclock-read-moment)) + (cond ((equal (car event) "b") + (setq accum (string-to-number (nth 2 event)))) + ((equal (car event) "h") + (setq last-date-limited + (timeclock-time-to-date (cadr event)) + last-date-seconds + (* (string-to-number (nth 2 event)) 3600.0))) + ((equal (car event) "i") + (when (and (nth 2 event) + (> (length (nth 2 event)) 0)) + (add-to-list 'timeclock-project-list (nth 2 event)) + (setq timeclock-last-project (nth 2 event))) + (let ((date (timeclock-time-to-date (cadr event)))) + (if (if last-date + (not (equal date last-date)) + first) + (setq first nil + accum (- accum (if last-date-limited + last-date-seconds + timeclock-workday)))) + (setq last-date date + last-date-limited nil) + (if beg + (error "Error in format of timelog file!") + (setq beg (timeclock-time-to-seconds (cadr event)))))) + ((equal (downcase (car event)) "o") + (if (and (nth 2 event) + (> (length (nth 2 event)) 0)) + (add-to-list 'timeclock-reason-list (nth 2 event))) + (if (not beg) + (error "Error in format of timelog file!") + (setq timeclock-last-period + (- (timeclock-time-to-seconds (cadr event)) beg) + accum (+ timeclock-last-period accum) + beg nil)) + (if (equal last-date todays-date) + (setq timeclock-elapsed + (+ timeclock-last-period timeclock-elapsed))))) + (setq timeclock-last-event event + timeclock-last-event-workday + (if (equal (timeclock-time-to-date now) last-date-limited) + last-date-seconds + timeclock-workday)) + (forward-line)) + (setq timeclock-discrepancy accum)))) + (unless timeclock-last-event-workday + (setq timeclock-last-event-workday timeclock-workday)) + (setq accum (or timeclock-discrepancy 0) + elapsed (or timeclock-elapsed elapsed)) + (if timeclock-last-event + (if (equal (car timeclock-last-event) "i") + (let ((last-period (timeclock-last-period now))) + (setq accum (+ accum last-period) + elapsed (+ elapsed last-period))) + (if (not (equal (timeclock-time-to-date + (cadr timeclock-last-event)) + (timeclock-time-to-date now))) + (setq accum (- accum timeclock-last-event-workday))))) + (list accum (- elapsed timeclock-last-event-workday) + elapsed))) + +;;; A reporting function that uses timeclock-log-data + +(defun timeclock-day-base (&optional time) + "Given a time within a day, return 0:0:0 within that day. +If optional argument TIME is non-nil, use that instead of the current time." + (let ((decoded (decode-time (or time (current-time))))) + (setcar (nthcdr 0 decoded) 0) + (setcar (nthcdr 1 decoded) 0) + (setcar (nthcdr 2 decoded) 0) + (apply 'encode-time decoded))) + +(defun timeclock-geometric-mean (l) + "Compute the geometric mean of the values in the list L." + (let ((total 0) + (count 0)) + (while l + (setq total (+ total (car l)) + count (1+ count) + l (cdr l))) + (if (> count 0) + (/ total count) + 0))) + +(defun timeclock-generate-report (&optional html-p) + "Generate a summary report based on the current timelog file. +By default, the report is in plain text, but if the optional argument +HTML-P is non-nil, HTML markup is added." + (interactive) + (let ((log (timeclock-log-data)) + (today (timeclock-day-base))) + (if html-p (insert "<p>")) + (insert "Currently ") + (let ((project (nth 2 timeclock-last-event)) + (begin (nth 1 timeclock-last-event)) + done) + (if (timeclock-currently-in-p) + (insert "IN") + (if (or (null project) (= (length project) 0)) + (progn (insert "Done Working Today") + (setq done t)) + (insert "OUT"))) + (unless done + (insert " since " (format-time-string "%Y/%m/%d %-I:%M %p" begin)) + (if html-p + (insert "<br>\n<b>") + (insert "\n*")) + (if (timeclock-currently-in-p) + (insert "Working on ")) + (if html-p + (insert project "</b><br>\n") + (insert project "*\n")) + (let ((proj-data (cdr (assoc project (timeclock-project-alist log)))) + (two-weeks-ago (timeclock-seconds-to-time + (- (timeclock-time-to-seconds today) + (* 2 7 24 60 60)))) + two-week-len today-len) + (while proj-data + (if (not (time-less-p + (timeclock-entry-begin (car proj-data)) today)) + (setq today-len (timeclock-entry-list-length proj-data) + proj-data nil) + (if (and (null two-week-len) + (not (time-less-p + (timeclock-entry-begin (car proj-data)) + two-weeks-ago))) + (setq two-week-len (timeclock-entry-list-length proj-data))) + (setq proj-data (cdr proj-data)))) + (if (null two-week-len) + (setq two-week-len today-len)) + (if html-p (insert "<p>")) + (if today-len + (insert "\nTime spent on this task today: " + (timeclock-seconds-to-string today-len) + ". In the last two weeks: " + (timeclock-seconds-to-string two-week-len)) + (if two-week-len + (insert "\nTime spent on this task in the last two weeks: " + (timeclock-seconds-to-string two-week-len)))) + (if html-p (insert "<br>")) + (insert "\n" + (timeclock-seconds-to-string (timeclock-workday-elapsed)) + " worked today, " + (timeclock-seconds-to-string (timeclock-workday-remaining)) + " remaining, done at " + (timeclock-when-to-leave-string) "\n"))) + (if html-p (insert "<p>")) + (insert "\nThere have been " + (number-to-string + (length (timeclock-day-alist log))) + " days of activity, starting " + (caar (last (timeclock-day-alist log)))) + (if html-p (insert "</p>")) + (when html-p + (insert "<p> +<table> +<td width=\"25\"><br></td><td> +<table border=1 cellpadding=3> +<tr><th><i>Statistics</i></th> + <th>Entire</th> + <th>-30 days</th> + <th>-3 mons</th> + <th>-6 mons</th> + <th>-1 year</th> +</tr>") + (let* ((day-list (timeclock-day-list)) + (thirty-days-ago (timeclock-seconds-to-time + (- (timeclock-time-to-seconds today) + (* 30 24 60 60)))) + (three-months-ago (timeclock-seconds-to-time + (- (timeclock-time-to-seconds today) + (* 90 24 60 60)))) + (six-months-ago (timeclock-seconds-to-time + (- (timeclock-time-to-seconds today) + (* 180 24 60 60)))) + (one-year-ago (timeclock-seconds-to-time + (- (timeclock-time-to-seconds today) + (* 365 24 60 60)))) + (time-in (vector (list t) (list t) (list t) (list t) (list t))) + (time-out (vector (list t) (list t) (list t) (list t) (list t))) + (breaks (vector (list t) (list t) (list t) (list t) (list t))) + (workday (vector (list t) (list t) (list t) (list t) (list t))) + (lengths (vector '(0 0) thirty-days-ago three-months-ago + six-months-ago one-year-ago))) + ;; collect statistics from complete timelog + (while day-list + (let ((i 0) (l 5)) + (while (< i l) + (unless (time-less-p + (timeclock-day-begin (car day-list)) + (aref lengths i)) + (let ((base (timeclock-time-to-seconds + (timeclock-day-base + (timeclock-day-begin (car day-list)))))) + (nconc (aref time-in i) + (list (- (timeclock-time-to-seconds + (timeclock-day-begin (car day-list))) + base))) + (let ((span (timeclock-day-span (car day-list))) + (len (timeclock-day-length (car day-list))) + (req (timeclock-day-required (car day-list)))) + ;; If the day's actual work length is less than + ;; 70% of its span, then likely the exit time + ;; and break amount are not worthwhile adding to + ;; the statistic + (when (and (> span 0) + (> (/ (float len) (float span)) 0.70)) + (nconc (aref time-out i) + (list (- (timeclock-time-to-seconds + (timeclock-day-end (car day-list))) + base))) + (nconc (aref breaks i) (list (- span len)))) + (if req + (setq len (+ len (- timeclock-workday req)))) + (nconc (aref workday i) (list len))))) + (setq i (1+ i)))) + (setq day-list (cdr day-list))) + ;; average statistics + (let ((i 0) (l 5)) + (while (< i l) + (aset time-in i (timeclock-geometric-mean + (cdr (aref time-in i)))) + (aset time-out i (timeclock-geometric-mean + (cdr (aref time-out i)))) + (aset breaks i (timeclock-geometric-mean + (cdr (aref breaks i)))) + (aset workday i (timeclock-geometric-mean + (cdr (aref workday i)))) + (setq i (1+ i)))) + ;; Output the HTML table + (insert "<tr>\n") + (insert "<td align=\"center\">Time in</td>\n") + (let ((i 0) (l 5)) + (while (< i l) + (insert "<td align=\"right\">" + (timeclock-seconds-to-string (aref time-in i)) + "</td>\n") + (setq i (1+ i)))) + (insert "</tr>\n") + + (insert "<tr>\n") + (insert "<td align=\"center\">Time out</td>\n") + (let ((i 0) (l 5)) + (while (< i l) + (insert "<td align=\"right\">" + (timeclock-seconds-to-string (aref time-out i)) + "</td>\n") + (setq i (1+ i)))) + (insert "</tr>\n") + + (insert "<tr>\n") + (insert "<td align=\"center\">Break</td>\n") + (let ((i 0) (l 5)) + (while (< i l) + (insert "<td align=\"right\">" + (timeclock-seconds-to-string (aref breaks i)) + "</td>\n") + (setq i (1+ i)))) + (insert "</tr>\n") + + (insert "<tr>\n") + (insert "<td align=\"center\">Workday</td>\n") + (let ((i 0) (l 5)) + (while (< i l) + (insert "<td align=\"right\">" + (timeclock-seconds-to-string (aref workday i)) + "</td>\n") + (setq i (1+ i)))) + (insert "</tr>\n")) + (insert "<tfoot> +<td colspan=\"6\" align=\"center\"> + <i>These are approximate figures</i></td> +</tfoot> +</table> +</td></table>"))))) + +;;; A helpful little function + +(defun timeclock-visit-timelog () + "Open the file named by `timeclock-file' in another window." + (interactive) + (find-file-other-window timeclock-file)) + +(provide 'timeclock) + +(run-hooks 'timeclock-load-hook) + +;; make sure we know the list of reasons, projects, and have computed +;; the last event and current discrepancy. +(if (file-readable-p timeclock-file) + (timeclock-reread-log)) + +;;; arch-tag: a0be3377-deb6-44ec-b9a2-a7be28436a40 +;;; timeclock.el ends here diff --git a/timing.h b/timing.h new file mode 100644 index 00000000..bc7fdfe2 --- /dev/null +++ b/timing.h @@ -0,0 +1,54 @@ +#ifndef _TIMING_H +#define _TIMING_H + +#include "debug.h" + +#include <ctime> + +namespace ledger { + +class timing_t +{ + public: + std::clock_t begin; + std::clock_t cumulative; + std::string file; + unsigned long line; + std::string symbol; + std::string category; + + timing_t(const std::string& _symbol, const std::string& _category) + : begin(0), cumulative(0), symbol(_symbol), category(_category) {} + + ~timing_t() { + std::string cls = "timing.results."; + cls += symbol; + DEBUG_PRINT(cls.c_str(), file << ":" << line << ": " + << category << " = " + << (double(cumulative) / double(CLOCKS_PER_SEC)) << "s"); + } + + void start(const std::string& _file, unsigned long _line) { + file = _file; + line = _line; + begin = std::clock(); + } + + void stop() { + cumulative += std::clock() - begin; + } +}; + +#ifdef DEBUG_ENABLED +#define TIMER_DEF(sym, cat) static timing_t sym(#sym, cat) +#define TIMER_START(sym) sym.start(__FILE__, __LINE__) +#define TIMER_STOP(sym) sym.stop() +#else +#define TIMER_DEF(sym, cat) +#define TIMER_START(sym) +#define TIMER_STOP(sym) +#endif + +} // namespace ledger + +#endif // _TIMING_H @@ -0,0 +1,37 @@ +#ifndef _UTIL_H +#define _UTIL_H + +inline char * skip_ws(char * ptr) { + while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n') + ptr++; + return ptr; +} + +inline char peek_next_nonws(std::istream& in) { + char c = in.peek(); + while (! in.eof() && std::isspace(c)) { + in.get(c); + c = in.peek(); + } + return c; +} + +#define READ_INTO(str, targ, size, var, cond) { \ + char * _p = targ; \ + var = str.peek(); \ + while (! str.eof() && var != '\n' && (cond) && _p - targ < size) { \ + str.get(var); \ + if (str.eof()) \ + break; \ + if (var == '\\') { \ + str.get(var); \ + if (in.eof()) \ + break; \ + } \ + *_p++ = var; \ + var = str.peek(); \ + } \ + *_p = '\0'; \ +} + +#endif // _UTIL_H diff --git a/valexpr.cc b/valexpr.cc new file mode 100644 index 00000000..ee02dafe --- /dev/null +++ b/valexpr.cc @@ -0,0 +1,1037 @@ +#include "valexpr.h" +#include "walk.h" +#include "error.h" +#include "datetime.h" +#include "debug.h" +#include "util.h" +#ifdef USE_BOOST_PYTHON +#include "py_eval.h" +#endif + +namespace ledger { + +std::auto_ptr<value_expr_t> amount_expr; +std::auto_ptr<value_expr_t> total_expr; + +void value_expr_t::compute(value_t& result, const details_t& details) const +{ + switch (kind) { + case CONSTANT_I: + result = constant_i; + break; + case CONSTANT_T: + result = long(constant_t); + break; + + case CONSTANT_A: + result = constant_a; + break; + + case AMOUNT: + if (details.xact) { + if (transaction_has_xdata(*details.xact) && + transaction_xdata_(*details.xact).dflags & TRANSACTION_COMPOSITE) + result = transaction_xdata_(*details.xact).composite_amount; + else + result = details.xact->amount; + } + else if (details.account && account_has_xdata(*details.account)) { + result = account_xdata(*details.account).value; + } + else { + result = 0L; + } + break; + + case COST: + if (details.xact) { + bool set = false; + if (transaction_has_xdata(*details.xact)) { + transaction_xdata_t& xdata(transaction_xdata_(*details.xact)); + if (xdata.dflags & TRANSACTION_COMPOSITE) { + if (xdata.composite_amount.type == value_t::BALANCE_PAIR && + ((balance_pair_t *) xdata.composite_amount.data)->cost) + result = *((balance_pair_t *) xdata.composite_amount.data)->cost; + else + result = xdata.composite_amount; + set = true; + } + } + + if (! set) { + if (details.xact->cost) + result = *details.xact->cost; + else + result = details.xact->amount; + } + } + else if (details.account && account_has_xdata(*details.account)) { + result = account_xdata(*details.account).value.cost(); + } + else { + result = 0L; + } + break; + + case TOTAL: + if (details.xact && transaction_has_xdata(*details.xact)) + result = transaction_xdata_(*details.xact).total; + else if (details.account && account_has_xdata(*details.account)) + result = account_xdata(*details.account).total; + else + result = 0L; + break; + case COST_TOTAL: + if (details.xact && transaction_has_xdata(*details.xact)) + result = transaction_xdata_(*details.xact).total.cost(); + else if (details.account && account_has_xdata(*details.account)) + result = account_xdata(*details.account).total.cost(); + else + result = 0L; + break; + + case VALUE_EXPR: + if (amount_expr.get()) + amount_expr->compute(result, details); + else + result = 0L; + break; + case TOTAL_EXPR: + if (total_expr.get()) + total_expr->compute(result, details); + else + result = 0L; + break; + + case DATE: + if (details.xact && transaction_has_xdata(*details.xact) && + transaction_xdata_(*details.xact).date) + result = long(transaction_xdata_(*details.xact).date); + else if (details.entry) + result = long(details.entry->date); + else + result = long(now); + break; + + case CLEARED: + if (details.entry) + result = details.entry->state == entry_t::CLEARED; + else + result = false; + break; + + case REAL: + if (details.xact) + result = ! (details.xact->flags & TRANSACTION_VIRTUAL); + else + result = true; + break; + + case ACTUAL: + if (details.xact) + result = ! (details.xact->flags & TRANSACTION_AUTO); + else + result = true; + break; + + case INDEX: + if (details.xact && transaction_has_xdata(*details.xact)) + result = long(transaction_xdata_(*details.xact).index + 1); + else if (details.account && account_has_xdata(*details.account)) + result = long(account_xdata(*details.account).count); + else + result = 0L; + break; + + case COUNT: + if (details.xact && transaction_has_xdata(*details.xact)) + result = long(transaction_xdata_(*details.xact).index + 1); + else if (details.account && account_has_xdata(*details.account)) + result = long(account_xdata(*details.account).total_count); + else + result = 0L; + break; + + case DEPTH: + if (details.account) + result = long(details.account->depth); + else + result = 0L; + break; + + case F_ARITH_MEAN: + if (details.xact && transaction_has_xdata(*details.xact)) { + assert(left); + left->compute(result, details); + result /= amount_t(long(transaction_xdata_(*details.xact).index + 1)); + } + else if (details.account && account_has_xdata(*details.account) && + account_xdata(*details.account).total_count) { + assert(left); + left->compute(result, details); + result /= amount_t(long(account_xdata(*details.account).total_count)); + } + else { + result = 0L; + } + break; + + case F_PARENT: + if (details.account && details.account->parent) + left->compute(result, details_t(*details.account->parent)); + break; + + case F_NEG: + assert(left); + left->compute(result, details); + result.negate(); + break; + + case F_ABS: + assert(left); + left->compute(result, details); + result.abs(); + break; + + case F_STRIP: { + assert(left); + left->compute(result, details); + + balance_t * bal = NULL; + switch (result.type) { + case value_t::BALANCE_PAIR: + bal = &((balance_pair_t *) result.data)->quantity; + // fall through... + + case value_t::BALANCE: + if (! bal) + bal = (balance_t *) result.data; + + if (bal->amounts.size() < 2) { + result.cast(value_t::AMOUNT); + } else { + value_t temp; + for (amounts_map::const_iterator i = bal->amounts.begin(); + i != bal->amounts.end(); + i++) { + amount_t x = (*i).second; + x.clear_commodity(); + temp += x; + } + result = temp; + assert(temp.type == value_t::AMOUNT); + } + // fall through... + + case value_t::AMOUNT: + ((amount_t *) result.data)->clear_commodity(); + break; + + default: + break; + } + break; + } + + case F_PAYEE_MASK: + assert(mask); + if (details.entry) + result = mask->match(details.entry->payee); + else + result = false; + break; + + case F_ACCOUNT_MASK: + assert(mask); + if (details.account) + result = mask->match(details.account->fullname()); + else + result = false; + break; + + case F_SHORT_ACCOUNT_MASK: + assert(mask); + if (details.account) + result = mask->match(details.account->name); + else + result = false; + break; + + case F_VALUE: { + assert(left); + left->compute(result, details); + + std::time_t moment = now; + if (right) { + switch (right->kind) { + case DATE: + if (details.xact && transaction_has_xdata(*details.xact) && + transaction_xdata_(*details.xact).date) + moment = transaction_xdata_(*details.xact).date; + else if (details.entry) + moment = details.entry->date; + break; + case CONSTANT_T: + moment = right->constant_t; + break; + default: + throw compute_error("Invalid date passed to P(value,date)"); + } + } + + result = result.value(moment); + break; + } + + case F_INTERP_FUNC: { +#ifdef USE_BOOST_PYTHON + if (! python_call(constant_s, right, details, result)) + result = 0L; +#else + result = 0L; +#endif + break; + } + + case O_NOT: + left->compute(result, details); + result.negate(); + break; + + case O_QUES: { + assert(left); + assert(right); + assert(right->kind == O_COL); + left->compute(result, details); + if (result) + right->left->compute(result, details); + else + right->right->compute(result, details); + break; + } + + case O_AND: + assert(left); + assert(right); + left->compute(result, details); + if (result) + right->compute(result, details); + break; + + case O_OR: + assert(left); + assert(right); + left->compute(result, details); + if (! result) + right->compute(result, details); + break; + + case O_EQ: + case O_LT: + case O_LTE: + case O_GT: + case O_GTE: { + assert(left); + assert(right); + value_t temp; + left->compute(temp, details); + right->compute(result, details); + switch (kind) { + case O_EQ: result = temp == result; break; + case O_LT: result = temp < result; break; + case O_LTE: result = temp <= result; break; + case O_GT: result = temp > result; break; + case O_GTE: result = temp >= result; break; + default: assert(0); break; + } + break; + } + + case O_ADD: + case O_SUB: + case O_MUL: + case O_DIV: { + assert(left); + assert(right); + value_t temp; + right->compute(temp, details); + left->compute(result, details); + switch (kind) { + case O_ADD: result += temp; break; + case O_SUB: result -= temp; break; + case O_MUL: result *= temp; break; + case O_DIV: result /= temp; break; + default: assert(0); break; + } + break; + } + + case LAST: + default: + assert(0); + break; + } +} + +static inline void unexpected(char c, char wanted = '\0') { + if ((unsigned char) c == 0xff) { + if (wanted) + throw value_expr_error(std::string("Missing '") + wanted + "'"); + else + throw value_expr_error("Unexpected end"); + } else { + if (wanted) + throw value_expr_error(std::string("Invalid char '") + c + + "' (wanted '" + wanted + "')"); + else + throw value_expr_error(std::string("Invalid char '") + c + "'"); + } +} + +value_expr_t * parse_value_term(std::istream& in); + +inline value_expr_t * parse_value_term(const char * p) { + std::istringstream stream(p); + return parse_value_term(stream); +} + +value_expr_t * parse_value_term(std::istream& in) +{ + std::auto_ptr<value_expr_t> node; + + char buf[256]; + char c = peek_next_nonws(in); + if (std::isdigit(c)) { + READ_INTO(in, buf, 255, c, std::isdigit(c)); + + node.reset(new value_expr_t(value_expr_t::CONSTANT_I)); + node->constant_i = std::atol(buf); + return node.release(); + } + else if (c == '{') { + in.get(c); + READ_INTO(in, buf, 255, c, c != '}'); + if (c == '}') + in.get(c); + else + unexpected(c, '}'); + + node.reset(new value_expr_t(value_expr_t::CONSTANT_A)); + node->constant_a.parse(buf); + return node.release(); + } + + in.get(c); + switch (c) { + // Basic terms + case 'm': + node.reset(new value_expr_t(value_expr_t::CONSTANT_T)); + node->constant_t = now; + break; + + case 'a': node.reset(new value_expr_t(value_expr_t::AMOUNT)); break; + case 'b': node.reset(new value_expr_t(value_expr_t::COST)); break; + case 'd': node.reset(new value_expr_t(value_expr_t::DATE)); break; + case 'X': node.reset(new value_expr_t(value_expr_t::CLEARED)); break; + case 'R': node.reset(new value_expr_t(value_expr_t::REAL)); break; + case 'L': node.reset(new value_expr_t(value_expr_t::ACTUAL)); break; + case 'n': node.reset(new value_expr_t(value_expr_t::INDEX)); break; + case 'N': node.reset(new value_expr_t(value_expr_t::COUNT)); break; + case 'l': node.reset(new value_expr_t(value_expr_t::DEPTH)); break; + case 'O': node.reset(new value_expr_t(value_expr_t::TOTAL)); break; + case 'B': node.reset(new value_expr_t(value_expr_t::COST_TOTAL)); break; + + // Relating to format_t + case 't': node.reset(new value_expr_t(value_expr_t::VALUE_EXPR)); break; + case 'T': node.reset(new value_expr_t(value_expr_t::TOTAL_EXPR)); break; + + // Compound terms + case 'v': node.reset(parse_value_expr("P(a,d)")); break; + case 'V': node.reset(parse_value_term("P(O,d)")); break; + case 'g': node.reset(parse_value_expr("v-b")); break; + case 'G': node.reset(parse_value_expr("V-B")); break; + + // Functions + case '^': + node.reset(new value_expr_t(value_expr_t::F_PARENT)); + node->left = parse_value_term(in); + break; + + case '-': + node.reset(new value_expr_t(value_expr_t::F_NEG)); + node->left = parse_value_term(in); + break; + + case 'U': + node.reset(new value_expr_t(value_expr_t::F_ABS)); + node->left = parse_value_term(in); + break; + + case 'S': + node.reset(new value_expr_t(value_expr_t::F_STRIP)); + node->left = parse_value_term(in); + break; + + case 'A': + node.reset(new value_expr_t(value_expr_t::F_ARITH_MEAN)); + node->left = parse_value_term(in); + break; + + case 'P': + node.reset(new value_expr_t(value_expr_t::F_VALUE)); + if (peek_next_nonws(in) == '(') { + in.get(c); + node->left = parse_value_expr(in, true); + if (peek_next_nonws(in) == ',') { + in.get(c); + node->right = parse_value_expr(in, true); + } + in.get(c); + if (c != ')') + unexpected(c, ')'); + } else { + node->left = parse_value_term(in); + } + break; + + // Other + case '/': { + bool payee_mask = false; + bool short_account_mask = false; + + c = peek_next_nonws(in); + if (c == '/') { + in.get(c); + c = in.peek(); + if (c == '/') { + in.get(c); + c = in.peek(); + short_account_mask = true; + } else { + payee_mask = true; + } + } + + READ_INTO(in, buf, 255, c, c != '/'); + if (c != '/') + unexpected(c, '/'); + + in.get(c); + node.reset(new value_expr_t(short_account_mask ? + value_expr_t::F_SHORT_ACCOUNT_MASK : + (payee_mask ? value_expr_t::F_PAYEE_MASK : + value_expr_t::F_ACCOUNT_MASK))); + node->mask = new mask_t(buf); + break; + } + + case '@': { + READ_INTO(in, buf, 255, c, c != '('); + if (c != '(') + unexpected(c, '('); + + node.reset(new value_expr_t(value_expr_t::F_INTERP_FUNC)); + node->constant_s = buf; + + in.get(c); + if (peek_next_nonws(in) == ')') { + in.get(c); + } else { + node->right = new value_expr_t(value_expr_t::O_ARG); + value_expr_t * cur = node->right; + cur->left = parse_value_expr(in, true); + in.get(c); + while (! in.eof() && c == ',') { + cur->right = new value_expr_t(value_expr_t::O_ARG); + cur = cur->right; + cur->left = parse_value_expr(in, true); + in.get(c); + } + if (c != ')') + unexpected(c, ')'); + } + break; + } + + case '(': + node.reset(parse_value_expr(in, true)); + in.get(c); + if (c != ')') + unexpected(c, ')'); + break; + + case '[': { + READ_INTO(in, buf, 255, c, c != ']'); + if (c != ']') + unexpected(c, ']'); + in.get(c); + + node.reset(new value_expr_t(value_expr_t::CONSTANT_T)); + + interval_t timespan(buf); + node->constant_t = timespan.first(); + break; + } + + default: + in.unget(); + break; + } + + return node.release(); +} + +value_expr_t * parse_mul_expr(std::istream& in) +{ + std::auto_ptr<value_expr_t> node(parse_value_term(in)); + + if (node.get() && ! in.eof()) { + char c = peek_next_nonws(in); + while (c == '*' || c == '/') { + in.get(c); + switch (c) { + case '*': { + std::auto_ptr<value_expr_t> prev(node.release()); + node.reset(new value_expr_t(value_expr_t::O_MUL)); + node->left = prev.release(); + node->right = parse_value_term(in); + break; + } + + case '/': { + std::auto_ptr<value_expr_t> prev(node.release()); + node.reset(new value_expr_t(value_expr_t::O_DIV)); + node->left = prev.release(); + node->right = parse_value_term(in); + break; + } + } + c = peek_next_nonws(in); + } + } + + return node.release(); +} + +value_expr_t * parse_add_expr(std::istream& in) +{ + std::auto_ptr<value_expr_t> node(parse_mul_expr(in)); + + if (node.get() && ! in.eof()) { + char c = peek_next_nonws(in); + while (c == '+' || c == '-') { + in.get(c); + switch (c) { + case '+': { + std::auto_ptr<value_expr_t> prev(node.release()); + node.reset(new value_expr_t(value_expr_t::O_ADD)); + node->left = prev.release(); + node->right = parse_mul_expr(in); + break; + } + + case '-': { + std::auto_ptr<value_expr_t> prev(node.release()); + node.reset(new value_expr_t(value_expr_t::O_SUB)); + node->left = prev.release(); + node->right = parse_mul_expr(in); + break; + } + } + c = peek_next_nonws(in); + } + } + + return node.release(); +} + +value_expr_t * parse_logic_expr(std::istream& in) +{ + std::auto_ptr<value_expr_t> node; + + if (peek_next_nonws(in) == '!') { + char c; + in.get(c); + node.reset(new value_expr_t(value_expr_t::O_NOT)); + node->left = parse_logic_expr(in); + return node.release(); + } + + node.reset(parse_add_expr(in)); + + if (node.get() && ! in.eof()) { + char c = peek_next_nonws(in); + if (c == '=' || c == '<' || c == '>') { + in.get(c); + switch (c) { + case '=': { + std::auto_ptr<value_expr_t> prev(node.release()); + node.reset(new value_expr_t(value_expr_t::O_EQ)); + node->left = prev.release(); + node->right = parse_add_expr(in); + break; + } + + case '<': { + std::auto_ptr<value_expr_t> prev(node.release()); + node.reset(new value_expr_t(value_expr_t::O_LT)); + if (peek_next_nonws(in) == '=') { + in.get(c); + node->kind = value_expr_t::O_LTE; + } + node->left = prev.release(); + node->right = parse_add_expr(in); + break; + } + + case '>': { + std::auto_ptr<value_expr_t> prev(node.release()); + node.reset(new value_expr_t(value_expr_t::O_GT)); + if (peek_next_nonws(in) == '=') { + in.get(c); + node->kind = value_expr_t::O_GTE; + } + node->left = prev.release(); + node->right = parse_add_expr(in); + break; + } + + default: + if (! in.eof()) + unexpected(c); + break; + } + } + } + + return node.release(); +} + +value_expr_t * parse_value_expr(std::istream& in, const bool partial) +{ + std::auto_ptr<value_expr_t> node(parse_logic_expr(in)); + + if (node.get() && ! in.eof()) { + char c = peek_next_nonws(in); + while (c == '&' || c == '|' || c == '?') { + in.get(c); + switch (c) { + case '&': { + std::auto_ptr<value_expr_t> prev(node.release()); + node.reset(new value_expr_t(value_expr_t::O_AND)); + node->left = prev.release(); + node->right = parse_logic_expr(in); + break; + } + + case '|': { + std::auto_ptr<value_expr_t> prev(node.release()); + node.reset(new value_expr_t(value_expr_t::O_OR)); + node->left = prev.release(); + node->right = parse_logic_expr(in); + break; + } + + case '?': { + std::auto_ptr<value_expr_t> prev(node.release()); + node.reset(new value_expr_t(value_expr_t::O_QUES)); + node->left = prev.release(); + value_expr_t * choices; + node->right = choices = new value_expr_t(value_expr_t::O_COL); + choices->left = parse_logic_expr(in); + c = peek_next_nonws(in); + if (c != ':') + unexpected(c, ':'); + in.get(c); + choices->right = parse_logic_expr(in); + break; + } + + default: + if (! in.eof()) + unexpected(c); + break; + } + c = peek_next_nonws(in); + } + } + + char c; + if (! node.get()) { + in.get(c); + if (in.eof()) + throw value_expr_error(std::string("Failed to parse value expression")); + else + unexpected(c); + } else if (! partial) { + in.get(c); + if (! in.eof()) + unexpected(c); + else + in.unget(); + } + + return node.release(); +} + +#ifdef DEBUG_ENABLED + +void dump_value_expr(std::ostream& out, const value_expr_t * node) +{ + switch (node->kind) { + case value_expr_t::CONSTANT_I: + out << "UINT[" << node->constant_i << ']'; + break; + case value_expr_t::CONSTANT_T: + out << "DATE/TIME[" << node->constant_t << ']'; + break; + case value_expr_t::CONSTANT_A: + out << "CONST[" << node->constant_a << ']'; + break; + + case value_expr_t::AMOUNT: out << "AMOUNT"; break; + case value_expr_t::COST: out << "COST"; break; + case value_expr_t::DATE: out << "DATE"; break; + case value_expr_t::CLEARED: out << "CLEARED"; break; + case value_expr_t::REAL: out << "REAL"; break; + case value_expr_t::ACTUAL: out << "ACTUAL"; break; + case value_expr_t::INDEX: out << "INDEX"; break; + case value_expr_t::COUNT: out << "COUNT"; break; + case value_expr_t::DEPTH: out << "DEPTH"; break; + case value_expr_t::TOTAL: out << "TOTAL"; break; + case value_expr_t::COST_TOTAL: out << "COST_TOTAL"; break; + + case value_expr_t::F_ARITH_MEAN: + out << "MEAN("; + dump_value_expr(out, node->left); + out << ')'; + break; + + case value_expr_t::F_NEG: + out << "ABS("; + dump_value_expr(out, node->left); + out << ')'; + break; + + case value_expr_t::F_ABS: + out << "ABS("; + dump_value_expr(out, node->left); + out << ')'; + break; + + case value_expr_t::F_STRIP: + out << "STRIP("; + dump_value_expr(out, node->left); + out << ')'; + break; + + case value_expr_t::F_PAYEE_MASK: + assert(node->mask); + out << "P_MASK(" << node->mask->pattern << ')'; + break; + + case value_expr_t::F_ACCOUNT_MASK: + assert(node->mask); + out << "A_MASK(" << node->mask->pattern << ')'; + break; + + case value_expr_t::F_SHORT_ACCOUNT_MASK: + assert(node->mask); + out << "A_SMASK(" << node->mask->pattern << ')'; + break; + + case value_expr_t::F_VALUE: + out << "VALUE("; + dump_value_expr(out, node->left); + if (node->right) { + out << ", "; + dump_value_expr(out, node->right); + } + out << ')'; + break; + + case value_expr_t::F_INTERP_FUNC: + out << "F_INTERP[" << node->constant_s << "]("; + dump_value_expr(out, node->right); + out << ')'; + break; + + case value_expr_t::O_NOT: + out << '!'; + dump_value_expr(out, node->left); + break; + + case value_expr_t::O_ARG: + dump_value_expr(out, node->left); + if (node->right) { + out << ','; + dump_value_expr(out, node->right); + } + break; + + case value_expr_t::O_QUES: + dump_value_expr(out, node->left); + out << '?'; + dump_value_expr(out, node->right->left); + out << ':'; + dump_value_expr(out, node->right->right); + break; + + case value_expr_t::O_AND: + case value_expr_t::O_OR: + out << '('; + dump_value_expr(out, node->left); + switch (node->kind) { + case value_expr_t::O_AND: out << " & "; break; + case value_expr_t::O_OR: out << " | "; break; + default: assert(0); break; + } + dump_value_expr(out, node->right); + out << ')'; + break; + + case value_expr_t::O_EQ: + case value_expr_t::O_LT: + case value_expr_t::O_LTE: + case value_expr_t::O_GT: + case value_expr_t::O_GTE: + out << '('; + dump_value_expr(out, node->left); + switch (node->kind) { + case value_expr_t::O_EQ: out << '='; break; + case value_expr_t::O_LT: out << '<'; break; + case value_expr_t::O_LTE: out << "<="; break; + case value_expr_t::O_GT: out << '>'; break; + case value_expr_t::O_GTE: out << ">="; break; + default: assert(0); break; + } + dump_value_expr(out, node->right); + out << ')'; + break; + + case value_expr_t::O_ADD: + case value_expr_t::O_SUB: + case value_expr_t::O_MUL: + case value_expr_t::O_DIV: + out << '('; + dump_value_expr(out, node->left); + switch (node->kind) { + case value_expr_t::O_ADD: out << '+'; break; + case value_expr_t::O_SUB: out << '-'; break; + case value_expr_t::O_MUL: out << '*'; break; + case value_expr_t::O_DIV: out << '/'; break; + default: assert(0); break; + } + dump_value_expr(out, node->right); + out << ')'; + break; + + case value_expr_t::LAST: + default: + assert(0); + break; + } +} + +#endif // DEBUG_ENABLED + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> + +using namespace boost::python; +using namespace ledger; + +value_t py_compute_1(value_expr_t& value_expr, const details_t& item) +{ + value_t result; + value_expr.compute(result, item); + return result; +} + +template <typename T> +value_t py_compute(value_expr_t& value_expr, const T& item) +{ + value_t result; + value_expr.compute(result, details_t(item)); + return result; +} + +value_expr_t * py_parse_value_expr_1(const std::string& str) +{ + return parse_value_expr(str); +} + +value_expr_t * py_parse_value_expr_2(const std::string& str, const bool partial) +{ + return parse_value_expr(str, partial); +} + +#define EXC_TRANSLATOR(type) \ + void exc_translate_ ## type(const type& err) { \ + PyErr_SetString(PyExc_RuntimeError, err.what()); \ + } + +EXC_TRANSLATOR(value_expr_error) +EXC_TRANSLATOR(compute_error) +EXC_TRANSLATOR(mask_error) + +void export_valexpr() +{ + class_< details_t > ("Details", init<const entry_t&>()) + .def(init<const transaction_t&>()) + .def(init<const account_t&>()) + .add_property("entry", + make_getter(&details_t::entry, + return_value_policy<reference_existing_object>())) + .add_property("xact", + make_getter(&details_t::xact, + return_value_policy<reference_existing_object>())) + .add_property("account", + make_getter(&details_t::account, + return_value_policy<reference_existing_object>())) + ; + + class_< value_expr_t > ("ValueExpr", init<value_expr_t::kind_t>()) + .def("compute", py_compute_1) + .def("compute", py_compute<account_t>) + .def("compute", py_compute<entry_t>) + .def("compute", py_compute<transaction_t>) + ; + + def("parse_value_expr", py_parse_value_expr_1, + return_value_policy<manage_new_object>()); + def("parse_value_expr", py_parse_value_expr_2, + return_value_policy<manage_new_object>()); + + class_< item_predicate<transaction_t> > + ("TransactionPredicate", init<std::string>()) + .def("__call__", &item_predicate<transaction_t>::operator()) + ; + + class_< item_predicate<account_t> > + ("AccountPredicate", init<std::string>()) + .def("__call__", &item_predicate<account_t>::operator()) + ; + +#define EXC_TRANSLATE(type) \ + register_exception_translator<type>(&exc_translate_ ## type); + + EXC_TRANSLATE(value_expr_error); + EXC_TRANSLATE(compute_error); + EXC_TRANSLATE(mask_error); +} + +#endif // USE_BOOST_PYTHON + +#ifdef TEST + +int main(int argc, char *argv[]) +{ + ledger::dump_value_expr(std::cout, ledger::parse_value_expr(argv[1])); + std::cout << std::endl; +} + +#endif // TEST diff --git a/valexpr.h b/valexpr.h new file mode 100644 index 00000000..938ce27d --- /dev/null +++ b/valexpr.h @@ -0,0 +1,202 @@ +#ifndef _VALEXPR_H +#define _VALEXPR_H + +#include "journal.h" +#include "value.h" +#include "error.h" +#include "mask.h" + +#include <memory> + +namespace ledger { + +struct details_t +{ + const entry_t * entry; + const transaction_t * xact; + const account_t * account; + + details_t(const entry_t& _entry) + : entry(&_entry), xact(NULL), account(NULL) { + DEBUG_PRINT("ledger.memory.ctors", "ctor details_t"); + } + details_t(const transaction_t& _xact) + : entry(_xact.entry), xact(&_xact), account(_xact.account) { + DEBUG_PRINT("ledger.memory.ctors", "ctor details_t"); + } + details_t(const account_t& _account) + : entry(NULL), xact(NULL), account(&_account) { + DEBUG_PRINT("ledger.memory.ctors", "ctor details_t"); + } +#ifdef DEBUG_ENABLED + ~details_t() { + DEBUG_PRINT("ledger.memory.dtors", "dtor details_t"); + } +#endif +}; + +struct value_expr_t +{ + enum kind_t { + // Constants + CONSTANT_I, + CONSTANT_T, + CONSTANT_A, + + // Item details + AMOUNT, + COST, + DATE, + CLEARED, + REAL, + ACTUAL, + INDEX, + DEPTH, + + // Item totals + COUNT, + TOTAL, + COST_TOTAL, + + // Relating to format_t + VALUE_EXPR, + TOTAL_EXPR, + + // Functions + F_PARENT, + F_ARITH_MEAN, + F_VALUE, + F_NEG, + F_ABS, + F_STRIP, + F_PAYEE_MASK, + F_ACCOUNT_MASK, + F_SHORT_ACCOUNT_MASK, + F_INTERP_FUNC, + + // Binary operators + O_ADD, + O_SUB, + O_MUL, + O_DIV, + O_EQ, + O_LT, + O_LTE, + O_GT, + O_GTE, + O_NOT, + O_AND, + O_OR, + O_QUES, + O_COL, + O_ARG, + + LAST + }; + + kind_t kind; + value_expr_t * left; + value_expr_t * right; + + union { + std::time_t constant_t; + long constant_i; + }; + std::string constant_s; + amount_t constant_a; + mask_t * mask; + + value_expr_t(const kind_t _kind) + : kind(_kind), left(NULL), right(NULL), mask(NULL) { + DEBUG_PRINT("ledger.memory.ctors", "ctor value_expr_t"); + } + + ~value_expr_t() { + DEBUG_PRINT("ledger.memory.dtors", "dtor value_expr_t"); + if (mask) delete mask; + if (left) delete left; + if (right) delete right; + } + + void compute(value_t& result, const details_t& details) const; +}; + +extern std::auto_ptr<value_expr_t> amount_expr; +extern std::auto_ptr<value_expr_t> total_expr; + +inline void compute_amount(value_t& result, const details_t& details) { + if (amount_expr.get()) + amount_expr->compute(result, details); +} + +inline void compute_total(value_t& result, const details_t& details) { + if (total_expr.get()) + total_expr->compute(result, details); +} + +value_expr_t * parse_value_expr(std::istream& in, + const bool partial = false); + +inline value_expr_t * parse_value_expr(const char * p, + const bool partial = false) { + std::istringstream stream(p); + return parse_value_expr(stream, partial); +} + +inline value_expr_t * parse_value_expr(const std::string& str, + const bool partial = false) { + return parse_value_expr(str.c_str()); +} + +#ifdef DEBUG_ENABLED +void dump_value_expr(std::ostream& out, const value_expr_t * node); +#endif + +////////////////////////////////////////////////////////////////////// + +template <typename T> +class item_predicate +{ + const value_expr_t * predicate; + bool allocated; + + public: + item_predicate(const std::string& _predicate) + : predicate(NULL), allocated(false) { + DEBUG_PRINT("ledger.memory.ctors", "ctor item_predicate<T>"); + if (! _predicate.empty()) { + try { + predicate = parse_value_expr(_predicate); + allocated = true; + } + catch (value_expr_error& err) { + throw value_expr_error(std::string("In predicate '") + + _predicate + "': " + err.what()); + } + } + } + item_predicate(const value_expr_t * _predicate = NULL) + : predicate(_predicate), allocated(false) { + DEBUG_PRINT("ledger.memory.ctors", "ctor item_predicate<T>"); + } + + ~item_predicate() { + DEBUG_PRINT("ledger.memory.dtors", "dtor item_predicate<T>"); + if (predicate && allocated) + delete predicate; + } + + bool operator()(const T& item) const { + if (predicate) { + value_t result; + predicate->compute(result, details_t(item)); + return result; + } else { + return true; + } + } +}; + +} // namespace ledger + +#endif // _VALEXPR_H diff --git a/value.cc b/value.cc new file mode 100644 index 00000000..5c0c0c0b --- /dev/null +++ b/value.cc @@ -0,0 +1,1062 @@ +#include "value.h" + +namespace ledger { + +void value_t::destroy() +{ + switch (type) { + case AMOUNT: + ((amount_t *)data)->~amount_t(); + break; + case BALANCE: + ((balance_t *)data)->~balance_t(); + break; + case BALANCE_PAIR: + ((balance_pair_t *)data)->~balance_pair_t(); + break; + default: + break; + } +} + +value_t& value_t::operator=(const value_t& value) +{ + if (this == &value) + return *this; + + destroy(); + + switch (value.type) { + case BOOLEAN: + *((bool *) data) = *((bool *) value.data); + break; + + case INTEGER: + *((long *) data) = *((long *) value.data); + break; + + case AMOUNT: + new((amount_t *)data) amount_t(*((amount_t *) value.data)); + break; + + case BALANCE: + new((balance_t *)data) balance_t(*((balance_t *) value.data)); + break; + + case BALANCE_PAIR: + new((balance_pair_t *)data) balance_pair_t(*((balance_pair_t *) value.data)); + break; + + default: + assert(0); + break; + } + + type = value.type; + + return *this; +} + +#define DEF_VALUE_ADDSUB_OP(OP) \ +value_t& value_t::operator OP(const value_t& value) \ +{ \ + switch (type) { \ + case BOOLEAN: \ + case INTEGER: \ + cast(INTEGER); \ + switch (value.type) { \ + case BOOLEAN: \ + *((long *) data) OP (*((bool *) value.data) ? 1L : 0L); \ + break; \ + case INTEGER: \ + *((long *) data) OP *((long *) value.data); \ + break; \ + case AMOUNT: \ + cast(AMOUNT); \ + *((amount_t *) data) OP *((amount_t *) value.data); \ + break; \ + case BALANCE: \ + cast(BALANCE); \ + *((balance_t *) data) OP *((balance_t *) value.data); \ + break; \ + case BALANCE_PAIR: \ + cast(BALANCE_PAIR); \ + *((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \ + break; \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case AMOUNT: \ + switch (value.type) { \ + case BOOLEAN: \ + if (*((bool *) value.data) && \ + ((amount_t *) data)->commodity()) { \ + cast(BALANCE); \ + return *this OP value; \ + } \ + *((amount_t *) data) OP (*((bool *) value.data) ? 1L : 0L); \ + break; \ + \ + case INTEGER: \ + if (*((long *) value.data) && \ + ((amount_t *) data)->commodity()) { \ + cast(BALANCE); \ + return *this OP value; \ + } \ + *((amount_t *) data) OP *((long *) value.data); \ + break; \ + \ + case AMOUNT: \ + if (((amount_t *) data)->commodity() != \ + ((amount_t *) value.data)->commodity()) { \ + cast(BALANCE); \ + return *this OP value; \ + } \ + *((amount_t *) data) OP *((amount_t *) value.data); \ + break; \ + \ + case BALANCE: \ + cast(BALANCE); \ + *((balance_t *) data) OP *((balance_t *) value.data); \ + break; \ + \ + case BALANCE_PAIR: \ + cast(BALANCE_PAIR); \ + *((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \ + break; \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case BALANCE: \ + switch (value.type) { \ + case BOOLEAN: \ + *((balance_t *) data) OP (*((bool *) value.data) ? 1L : 0L); \ + break; \ + case INTEGER: \ + *((balance_t *) data) OP *((long *) value.data); \ + break; \ + case AMOUNT: \ + *((balance_t *) data) OP *((amount_t *) value.data); \ + break; \ + case BALANCE: \ + *((balance_t *) data) OP *((balance_t *) value.data); \ + break; \ + case BALANCE_PAIR: \ + cast(BALANCE_PAIR); \ + *((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \ + break; \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case BALANCE_PAIR: \ + switch (value.type) { \ + case BOOLEAN: \ + *((balance_pair_t *) data) OP (*((bool *) value.data) ? 1L : 0L); \ + break; \ + case INTEGER: \ + *((balance_pair_t *) data) OP *((long *) value.data); \ + break; \ + case AMOUNT: \ + *((balance_pair_t *) data) OP *((amount_t *) value.data); \ + break; \ + case BALANCE: \ + *((balance_pair_t *) data) OP *((balance_t *) value.data); \ + break; \ + case BALANCE_PAIR: \ + *((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \ + break; \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + default: \ + assert(0); \ + break; \ + } \ + return *this; \ +} + +DEF_VALUE_ADDSUB_OP(+=) +DEF_VALUE_ADDSUB_OP(-=) + +#define DEF_VALUE_MULDIV_OP(OP) \ +value_t& value_t::operator OP(const value_t& value) \ +{ \ + switch (type) { \ + case BOOLEAN: \ + case INTEGER: \ + cast(INTEGER); \ + switch (value.type) { \ + case BOOLEAN: \ + *((long *) data) OP (*((bool *) value.data) ? 1L : 0L); \ + break; \ + case INTEGER: \ + *((long *) data) OP *((long *) value.data); \ + break; \ + case AMOUNT: \ + cast(AMOUNT); \ + *((amount_t *) data) OP *((amount_t *) value.data); \ + break; \ + case BALANCE: \ + cast(BALANCE); \ + *((balance_t *) data) OP *((balance_t *) value.data); \ + break; \ + case BALANCE_PAIR: \ + cast(BALANCE_PAIR); \ + *((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \ + break; \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case AMOUNT: \ + switch (value.type) { \ + case BOOLEAN: \ + *((amount_t *) data) OP (*((bool *) value.data) ? 1L : 0L); \ + break; \ + case INTEGER: \ + *((amount_t *) data) OP *((long *) value.data); \ + break; \ + case AMOUNT: \ + *((amount_t *) data) OP *((amount_t *) value.data); \ + break; \ + case BALANCE: \ + cast(BALANCE); \ + *((balance_t *) data) OP *((balance_t *) value.data); \ + break; \ + case BALANCE_PAIR: \ + cast(BALANCE_PAIR); \ + *((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \ + break; \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case BALANCE: \ + switch (value.type) { \ + case BOOLEAN: \ + *((balance_t *) data) OP (*((bool *) value.data) ? 1L : 0L); \ + break; \ + case INTEGER: \ + *((balance_t *) data) OP *((long *) value.data); \ + break; \ + case AMOUNT: \ + *((balance_t *) data) OP *((amount_t *) value.data); \ + break; \ + case BALANCE: \ + *((balance_t *) data) OP *((balance_t *) value.data); \ + break; \ + case BALANCE_PAIR: \ + cast(BALANCE_PAIR); \ + *((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \ + break; \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case BALANCE_PAIR: \ + switch (value.type) { \ + case BOOLEAN: \ + *((balance_pair_t *) data) OP (*((bool *) value.data) ? 1L : 0L); \ + break; \ + case INTEGER: \ + *((balance_pair_t *) data) OP *((long *) value.data); \ + break; \ + case AMOUNT: \ + *((balance_pair_t *) data) OP *((amount_t *) value.data); \ + break; \ + case BALANCE: \ + *((balance_pair_t *) data) OP *((balance_t *) value.data); \ + break; \ + case BALANCE_PAIR: \ + *((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \ + break; \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + default: \ + assert(0); \ + break; \ + } \ + return *this; \ +} + +DEF_VALUE_MULDIV_OP(*=) +DEF_VALUE_MULDIV_OP(/=) + +#define DEF_VALUE_CMP_OP(OP) \ +bool value_t::operator OP(const value_t& value) \ +{ \ + switch (type) { \ + case BOOLEAN: \ + switch (value.type) { \ + case BOOLEAN: \ + return *((bool *) data) OP *((bool *) value.data); \ + \ + case INTEGER: \ + return *((bool *) data) OP bool(*((long *) value.data)); \ + \ + case AMOUNT: \ + return *((bool *) data) OP bool(*((amount_t *) value.data)); \ + \ + case BALANCE: \ + return *((bool *) data) OP bool(*((balance_t *) value.data)); \ + \ + case BALANCE_PAIR: \ + return *((bool *) data) OP bool(*((balance_pair_t *) value.data)); \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case INTEGER: \ + switch (value.type) { \ + case BOOLEAN: \ + return (*((long *) data) OP \ + ((long) *((bool *) value.data))); \ + \ + case INTEGER: \ + return (*((long *) data) OP \ + *((long *) value.data)); \ + \ + case AMOUNT: \ + return (amount_t(*((long *) data)) OP \ + *((amount_t *) value.data)); \ + \ + case BALANCE: \ + return (balance_t(*((long *) data)) OP \ + *((balance_t *) value.data)); \ + \ + case BALANCE_PAIR: \ + return (balance_pair_t(*((long *) data)) OP \ + *((balance_pair_t *) value.data)); \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case AMOUNT: \ + switch (value.type) { \ + case BOOLEAN: \ + return *((amount_t *) data) OP amount_t(*((bool *) value.data)); \ + \ + case INTEGER: \ + return (*((amount_t *) data) OP \ + amount_t(*((long *) value.data))); \ + \ + case AMOUNT: \ + return *((amount_t *) data) OP *((amount_t *) value.data); \ + \ + case BALANCE: \ + return (*((amount_t *) data) OP \ + ((balance_t *) value.data)-> \ + amount(((amount_t *) data)->commodity())); \ + \ + case BALANCE_PAIR: \ + return (*((amount_t *) data) OP \ + ((balance_pair_t *) value.data)-> \ + quantity.amount(((amount_t *) data)->commodity())); \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case BALANCE: \ + switch (value.type) { \ + case BOOLEAN: \ + return *((balance_t *) data) OP (long)*((bool *) value.data); \ + \ + case INTEGER: \ + return *((balance_t *) data) OP *((long *) value.data); \ + \ + case AMOUNT: \ + return *((balance_t *) data) OP *((amount_t *) value.data); \ + \ + case BALANCE: \ + return *((balance_t *) data) OP *((balance_t *) value.data); \ + \ + case BALANCE_PAIR: \ + return (*((balance_t *) data) OP \ + ((balance_pair_t *) value.data)->quantity); \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case BALANCE_PAIR: \ + switch (value.type) { \ + case BOOLEAN: \ + return (((balance_pair_t *) data)->quantity OP \ + (long)*((bool *) value.data)); \ + \ + case INTEGER: \ + return (((balance_pair_t *) data)->quantity OP \ + *((long *) value.data)); \ + \ + case AMOUNT: \ + return (((balance_pair_t *) data)->quantity OP \ + *((amount_t *) value.data)); \ + \ + case BALANCE: \ + return (((balance_pair_t *) data)->quantity OP \ + *((balance_t *) value.data)); \ + \ + case BALANCE_PAIR: \ + return (*((balance_pair_t *) data) OP \ + *((balance_pair_t *) value.data)); \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + default: \ + assert(0); \ + break; \ + } \ + return *this; \ +} + +DEF_VALUE_CMP_OP(==) +DEF_VALUE_CMP_OP(<) +DEF_VALUE_CMP_OP(<=) +DEF_VALUE_CMP_OP(>) +DEF_VALUE_CMP_OP(>=) + +template <> +value_t::operator long() const +{ + switch (type) { + case BOOLEAN: + return *((bool *) data) ? 1L : 0L; + case INTEGER: + return *((long *) data); + case AMOUNT: + return *((amount_t *) data); + case BALANCE: + throw value_error("Cannot convert a value balance to a long"); + case BALANCE_PAIR: + throw value_error("Cannot convert a value balance pair to a long"); + + default: + assert(0); + break; + } + assert(0); + return 0; +} + +template <> +value_t::operator double() const +{ + switch (type) { + case BOOLEAN: + return *((bool *) data) ? 1.0 : 0.0; + case INTEGER: + return *((long *) data); + case AMOUNT: + return *((amount_t *) data); + case BALANCE: + throw value_error("Cannot convert a value balance to a double"); + case BALANCE_PAIR: + throw value_error("Cannot convert a value balance pair to a double"); + + default: + assert(0); + break; + } + assert(0); + return 0; +} + +void value_t::cast(type_t cast_type) +{ + switch (type) { + case BOOLEAN: + switch (cast_type) { + case BOOLEAN: + break; + case INTEGER: + *((long *) data) = *((bool *) data); + break; + case AMOUNT: + new((amount_t *)data) amount_t(*((bool *) data)); + break; + case BALANCE: + new((balance_t *)data) balance_t(*((bool *) data)); + break; + case BALANCE_PAIR: + new((balance_pair_t *)data) balance_pair_t(*((bool *) data)); + break; + + default: + assert(0); + break; + } + break; + + case INTEGER: + switch (cast_type) { + case BOOLEAN: + *((bool *) data) = *((long *) data); + break; + case INTEGER: + break; + case AMOUNT: + new((amount_t *)data) amount_t(*((long *) data)); + break; + case BALANCE: + new((balance_t *)data) balance_t(*((long *) data)); + break; + case BALANCE_PAIR: + new((balance_pair_t *)data) balance_pair_t(*((long *) data)); + break; + + default: + assert(0); + break; + } + break; + + case AMOUNT: + switch (cast_type) { + case BOOLEAN: { + bool temp = *((amount_t *) data); + destroy(); + *((bool *)data) = temp; + break; + } + case INTEGER: { + long temp = *((amount_t *) data); + destroy(); + *((long *)data) = temp; + break; + } + case AMOUNT: + break; + case BALANCE: { + amount_t temp = *((amount_t *) data); + destroy(); + new((balance_t *)data) balance_t(temp); + break; + } + case BALANCE_PAIR: { + amount_t temp = *((amount_t *) data); + destroy(); + new((balance_pair_t *)data) balance_pair_t(temp); + break; + } + + default: + assert(0); + break; + } + break; + + case BALANCE: + switch (cast_type) { + case BOOLEAN: { + bool temp = *((balance_t *) data); + destroy(); + *((bool *)data) = temp; + break; + } + case INTEGER: + throw value_error("Cannot convert a balance to an integer"); + case AMOUNT: { + balance_t * temp = (balance_t *) data; + if (temp->amounts.size() == 1) { + amount_t amt = (*temp->amounts.begin()).second; + destroy(); + new((amount_t *)data) amount_t(amt); + } + else if (temp->amounts.size() == 0) { + new((amount_t *)data) amount_t(); + } + else { + throw value_error("Cannot convert a balance with " + "multiple commodities to an amount"); + } + break; + } + case BALANCE: + break; + case BALANCE_PAIR: { + balance_t temp = *((balance_t *) data); + destroy(); + new((balance_pair_t *)data) balance_pair_t(temp); + break; + } + + default: + assert(0); + break; + } + break; + + case BALANCE_PAIR: + switch (cast_type) { + case BOOLEAN: { + bool temp = *((balance_pair_t *) data); + destroy(); + *((bool *)data) = temp; + break; + } + case INTEGER: + throw value_error("Cannot convert a balance pair to an integer"); + + case AMOUNT: { + balance_t * temp = &((balance_pair_t *) data)->quantity; + if (temp->amounts.size() == 1) { + amount_t amt = (*temp->amounts.begin()).second; + destroy(); + new((amount_t *)data) amount_t(amt); + } + else if (temp->amounts.size() == 0) { + new((amount_t *)data) amount_t(); + } + else { + throw value_error("Cannot convert a balance pair with " + "multiple commodities to an amount"); + } + break; + } + case BALANCE: { + balance_t temp = ((balance_pair_t *) data)->quantity; + destroy(); + new((balance_t *)data) balance_t(temp); + break; + } + case BALANCE_PAIR: + break; + + default: + assert(0); + break; + } + break; + + default: + assert(0); + break; + } + type = cast_type; +} + +void value_t::negate() +{ + switch (type) { + case BOOLEAN: + *((bool *) data) = ! *((bool *) data); + break; + case INTEGER: + *((long *) data) = - *((long *) data); + break; + case AMOUNT: + ((amount_t *) data)->negate(); + break; + case BALANCE: + ((balance_t *) data)->negate(); + break; + case BALANCE_PAIR: + ((balance_pair_t *) data)->negate(); + break; + + default: + assert(0); + break; + } +} + +void value_t::abs() +{ + switch (type) { + case BOOLEAN: + break; + case INTEGER: + if (*((long *) data) < 0) + *((long *) data) = - *((long *) data); + break; + case AMOUNT: + ((amount_t *) data)->abs(); + break; + case BALANCE: + ((balance_t *) data)->abs(); + break; + case BALANCE_PAIR: + ((balance_pair_t *) data)->abs(); + break; + + default: + assert(0); + break; + } +} + +value_t value_t::cost() const +{ + switch (type) { + case BOOLEAN: + case INTEGER: + case AMOUNT: + case BALANCE: + return *this; + + case BALANCE_PAIR: + assert(((balance_pair_t *) data)->cost); + if (((balance_pair_t *) data)->cost) + return *(((balance_pair_t *) data)->cost); + else + return ((balance_pair_t *) data)->quantity; + + default: + assert(0); + break; + } + assert(0); + return value_t(); +} + +value_t& value_t::add(const amount_t& amount, const amount_t * cost) +{ + switch (type) { + case BOOLEAN: + case INTEGER: + case AMOUNT: + if (cost) { + cast(BALANCE_PAIR); + return add(amount, cost); + } + else if ((type == AMOUNT && + ((amount_t *) data)->commodity() != amount.commodity()) || + (type != AMOUNT && amount.commodity())) { + cast(BALANCE); + return add(amount, cost); + } + else if (type != AMOUNT) { + cast(AMOUNT); + } + *((amount_t *) data) += amount; + break; + + case BALANCE: + if (cost) { + cast(BALANCE_PAIR); + return add(amount, cost); + } + *((balance_t *) data) += amount; + break; + + case BALANCE_PAIR: + ((balance_pair_t *) data)->add(amount, cost); + break; + + default: + assert(0); + break; + } + + return *this; +} + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> + +using namespace boost::python; +using namespace ledger; + +long balance_len(balance_t& bal); +amount_t balance_getitem(balance_t& bal, int i); +long balance_pair_len(balance_pair_t& bal_pair); +amount_t balance_pair_getitem(balance_pair_t& bal_pair, int i); + +long value_len(value_t& value) +{ + switch (value.type) { + case value_t::BOOLEAN: + case value_t::INTEGER: + case value_t::AMOUNT: + return 1; + + case value_t::BALANCE: + return balance_len(*((balance_t *) value.data)); + + case value_t::BALANCE_PAIR: + return balance_pair_len(*((balance_pair_t *) value.data)); + + default: + assert(0); + break; + } + assert(0); + return 0; +} + +amount_t value_getitem(value_t& value, int i) +{ + std::size_t len = value_len(value); + + if (abs(i) >= len) { + PyErr_SetString(PyExc_IndexError, "Index out of range"); + throw_error_already_set(); + } + + switch (value.type) { + case value_t::BOOLEAN: + case value_t::INTEGER: + return long(value); + + case value_t::AMOUNT: + return *((amount_t *) value.data); + + case value_t::BALANCE: + return balance_getitem(*((balance_t *) value.data), i); + + case value_t::BALANCE_PAIR: + return balance_pair_getitem(*((balance_pair_t *) value.data), i); + + default: + assert(0); + break; + } + assert(0); + return 0L; +} + +double py_to_float(value_t& value) +{ + return double(value); +} + +void export_value() +{ + scope in_value = class_< value_t > ("Value") + .def(init<value_t>()) + .def(init<balance_pair_t>()) + .def(init<balance_t>()) + .def(init<amount_t>()) + .def(init<std::string>()) + .def(init<double>()) + .def(init<long>()) + + .def(self + self) + .def(self + other<balance_pair_t>()) + .def(self + other<balance_t>()) + .def(self + other<amount_t>()) + .def(self + long()) + .def(self + double()) + + .def(other<balance_pair_t>() + self) + .def(other<balance_t>() + self) + .def(other<amount_t>() + self) + .def(long() + self) + .def(double() + self) + + .def(self - self) + .def(self - other<balance_pair_t>()) + .def(self - other<balance_t>()) + .def(self - other<amount_t>()) + .def(self - long()) + .def(self - double()) + + .def(other<balance_pair_t>() - self) + .def(other<balance_t>() - self) + .def(other<amount_t>() - self) + .def(long() - self) + .def(double() - self) + + .def(self * self) + .def(self * other<balance_pair_t>()) + .def(self * other<balance_t>()) + .def(self * other<amount_t>()) + .def(self * long()) + .def(self * double()) + + .def(other<balance_pair_t>() * self) + .def(other<balance_t>() * self) + .def(other<amount_t>() * self) + .def(long() * self) + .def(double() * self) + + .def(self / self) + .def(self / other<balance_pair_t>()) + .def(self / other<balance_t>()) + .def(self / other<amount_t>()) + .def(self / long()) + .def(self / double()) + + .def(other<balance_pair_t>() / self) + .def(other<balance_t>() / self) + .def(other<amount_t>() / self) + .def(long() / self) + .def(double() / self) + + .def(- self) + + .def(self += self) + .def(self += other<balance_pair_t>()) + .def(self += other<balance_t>()) + .def(self += other<amount_t>()) + .def(self += long()) + .def(self += double()) + + .def(self -= self) + .def(self -= other<balance_pair_t>()) + .def(self -= other<balance_t>()) + .def(self -= other<amount_t>()) + .def(self -= long()) + .def(self -= double()) + + .def(self *= self) + .def(self *= other<balance_pair_t>()) + .def(self *= other<balance_t>()) + .def(self *= other<amount_t>()) + .def(self *= long()) + .def(self *= double()) + + .def(self /= self) + .def(self /= other<balance_pair_t>()) + .def(self /= other<balance_t>()) + .def(self /= other<amount_t>()) + .def(self /= long()) + .def(self /= double()) + + .def(self < self) + .def(self < other<balance_pair_t>()) + .def(self < other<balance_t>()) + .def(self < other<amount_t>()) + .def(self < long()) + .def(self < double()) + + .def(other<balance_pair_t>() < self) + .def(other<balance_t>() < self) + .def(other<amount_t>() < self) + .def(long() < self) + .def(double() < self) + + .def(self <= self) + .def(self <= other<balance_pair_t>()) + .def(self <= other<balance_t>()) + .def(self <= other<amount_t>()) + .def(self <= long()) + .def(self <= double()) + + .def(other<balance_pair_t>() <= self) + .def(other<balance_t>() <= self) + .def(other<amount_t>() <= self) + .def(long() <= self) + .def(double() <= self) + + .def(self > self) + .def(self > other<balance_pair_t>()) + .def(self > other<balance_t>()) + .def(self > other<amount_t>()) + .def(self > long()) + .def(self > double()) + + .def(other<balance_pair_t>() > self) + .def(other<balance_t>() > self) + .def(other<amount_t>() > self) + .def(long() > self) + .def(double() > self) + + .def(self >= self) + .def(self >= other<balance_pair_t>()) + .def(self >= other<balance_t>()) + .def(self >= other<amount_t>()) + .def(self >= long()) + .def(self >= double()) + + .def(other<balance_pair_t>() >= self) + .def(other<balance_t>() >= self) + .def(other<amount_t>() >= self) + .def(long() >= self) + .def(double() >= self) + + .def(self == self) + .def(self == other<balance_pair_t>()) + .def(self == other<balance_t>()) + .def(self == other<amount_t>()) + .def(self == long()) + .def(self == double()) + + .def(other<balance_pair_t>() == self) + .def(other<balance_t>() == self) + .def(other<amount_t>() == self) + .def(long() == self) + .def(double() == self) + + .def(self != self) + .def(self != other<balance_pair_t>()) + .def(self != other<balance_t>()) + .def(self != other<amount_t>()) + .def(self != long()) + .def(self != double()) + + .def(other<balance_pair_t>() != self) + .def(other<balance_t>() != self) + .def(other<amount_t>() != self) + .def(long() != self) + .def(double() != self) + + .def(! self) + + .def(self_ns::int_(self)) + .def(self_ns::float_(self)) + .def(self_ns::str(self)) + .def(abs(self)) + + .def_readonly("type", &value_t::type) + + .def("__len__", value_len) + .def("__getitem__", value_getitem) + + .def("cast", &value_t::cast) + .def("negate", &value_t::negate) + .def("cost", &value_t::cost) + .def("add", &value_t::add, return_internal_reference<1>()) + ; + + enum_< value_t::type_t > ("ValueType") + .value("BOOLEAN", value_t::BOOLEAN) + .value("INTEGER", value_t::INTEGER) + .value("AMOUNT", value_t::AMOUNT) + .value("BALANCE", value_t::BALANCE) + .value("BALANCE_PAIR", value_t::BALANCE_PAIR) + ; +} + +#endif // USE_BOOST_PYTHON diff --git a/value.h b/value.h new file mode 100644 index 00000000..d2f893b5 --- /dev/null +++ b/value.h @@ -0,0 +1,395 @@ +#ifndef _VALUE_H +#define _VALUE_H + +#include "amount.h" +#include "balance.h" + +#include <exception> + +namespace ledger { + +class value_error : public std::exception { + std::string reason; + public: + value_error(const std::string& _reason) throw() : reason(_reason) {} + virtual ~value_error() throw() {} + + virtual const char* what() const throw() { + return reason.c_str(); + } +}; + +// The following type is a polymorphous value type used solely for +// performance reasons. The alternative is to compute value +// expressions (valexpr.cc) in terms of the largest data type, +// balance_t. This was found to be prohibitively expensive, especially +// when large logic chains were involved, since many temporary +// allocations would occur for every operator. With value_t, and the +// fact that logic chains only need boolean values to continue, no +// memory allocations need to take place at all. + +class value_t +{ + public: + char data[sizeof(balance_pair_t)]; + + enum type_t { + BOOLEAN, + INTEGER, + AMOUNT, + BALANCE, + BALANCE_PAIR + } type; + + value_t() { + *((long *) data) = 0; + type = INTEGER; + } + + value_t(const value_t& value) : type(INTEGER) { + *this = value; + } + value_t(const bool value) { + *((bool *) data) = value; + type = BOOLEAN; + } + value_t(const long value) { + *((long *) data) = value; + type = INTEGER; + } + value_t(const unsigned long value) { + new((amount_t *) data) amount_t(value); + type = AMOUNT; + } + value_t(const double value) { + new((amount_t *) data) amount_t(value); + type = AMOUNT; + } + value_t(const std::string& value) { + new((amount_t *) data) amount_t(value); + type = AMOUNT; + } + value_t(const char * value) { + new((amount_t *) data) amount_t(value); + type = AMOUNT; + } + value_t(const amount_t& value) { + new((amount_t *)data) amount_t(value); + type = AMOUNT; + } + value_t(const balance_t& value) : type(INTEGER) { + *this = value; + } + value_t(const balance_pair_t& value) : type(INTEGER) { + *this = value; + } + + ~value_t() { + destroy(); + } + + void destroy(); + + value_t& operator=(const value_t& value); + value_t& operator=(const bool value) { + if ((bool *) data != &value) { + destroy(); + *((bool *) data) = value; + type = BOOLEAN; + } + return *this; + } + value_t& operator=(const long value) { + if ((long *) data != &value) { + destroy(); + *((long *) data) = value; + type = INTEGER; + } + return *this; + } + value_t& operator=(const unsigned long value) { + return *this = amount_t(value); + } + value_t& operator=(const double value) { + return *this = amount_t(value); + } + value_t& operator=(const std::string& value) { + return *this = amount_t(value); + } + value_t& operator=(const char * value) { + return *this = amount_t(value); + } + value_t& operator=(const amount_t& value) { + if ((amount_t *) data != &value) { + if (! value) { + return *this = 0L; + } else { + destroy(); + new((amount_t *)data) amount_t(value); + type = AMOUNT; + } + } + return *this; + } + value_t& operator=(const balance_t& value) { + if ((balance_t *) data != &value) { + if (value.amounts.size() == 1) { + return *this = (*value.amounts.begin()).second; + } else { + destroy(); + new((balance_t *)data) balance_t(value); + type = BALANCE; + } + } + return *this; + } + value_t& operator=(const balance_pair_t& value) { + if ((balance_pair_t *) data != &value) { + if (! value.cost) { + return *this = value.quantity; + } else { + destroy(); + new((balance_pair_t *)data) balance_pair_t(value); + type = BALANCE_PAIR; + } + } + return *this; + } + + value_t& operator+=(const value_t& value); + value_t& operator-=(const value_t& value); + value_t& operator*=(const value_t& value); + value_t& operator/=(const value_t& value); + + template <typename T> + value_t& operator+=(const T& value) { + return *this += value_t(value); + } + template <typename T> + value_t& operator-=(const T& value) { + return *this -= value_t(value); + } + template <typename T> + value_t& operator*=(const T& value) { + return *this *= value_t(value); + } + template <typename T> + value_t& operator/=(const T& value) { + return *this /= value_t(value); + } + + value_t operator+(const value_t& value) { + value_t temp(*this); + temp += value; + return temp; + } + value_t operator-(const value_t& value) { + value_t temp(*this); + temp -= value; + return temp; + } + value_t operator*(const value_t& value) { + value_t temp(*this); + temp *= value; + return temp; + } + value_t operator/(const value_t& value) { + value_t temp(*this); + temp /= value; + return temp; + } + + template <typename T> + value_t operator+(const T& value) { + return *this + value_t(value); + } + template <typename T> + value_t operator-(const T& value) { + return *this - value_t(value); + } + template <typename T> + value_t operator*(const T& value) { + return *this * value_t(value); + } + template <typename T> + value_t operator/(const T& value) { + return *this / value_t(value); + } + + bool operator<(const value_t& value); + bool operator<=(const value_t& value); + bool operator>(const value_t& value); + bool operator>=(const value_t& value); + bool operator==(const value_t& value); + bool operator!=(const value_t& value) { + return ! (*this == value); + } + + template <typename T> + bool operator<(const T& value) { + return *this < value_t(value); + } + template <typename T> + bool operator<=(const T& value) { + return *this <= value_t(value); + } + template <typename T> + bool operator>(const T& value) { + return *this > value_t(value); + } + template <typename T> + bool operator>=(const T& value) { + return *this >= value_t(value); + } + template <typename T> + bool operator==(const T& value) { + return *this == value_t(value); + } + template <typename T> + bool operator!=(const T& value) { + return ! (*this == value); + } + + template <typename T> + operator T() const; + + void negate(); + value_t negated() const { + value_t temp = *this; + temp.negate(); + return temp; + } + value_t operator-() const { + return negated(); + } + + void abs(); + void cast(type_t cast_type); + value_t cost() const; + value_t& add(const amount_t& amount, const amount_t * cost = NULL); + + value_t value(const std::time_t moment) const { + switch (type) { + case BOOLEAN: + case INTEGER: + return *this; + case AMOUNT: + return ((amount_t *) data)->value(moment); + case BALANCE: + return ((balance_t *) data)->value(moment); + case BALANCE_PAIR: + return ((balance_pair_t *) data)->quantity.value(moment); + } + } + + void round() { + switch (type) { + case BOOLEAN: + case INTEGER: + break; + case AMOUNT: { + amount_t& amount = *((amount_t *) data); + if (amount.commodity()) + amount = amount.round(amount.commodity().precision); + break; + } + case BALANCE: + ((balance_t *) data)->round(); + break; + case BALANCE_PAIR: + ((balance_pair_t *) data)->round(); + break; + } + } +}; + +#define DEF_VALUE_AUX_OP(OP) \ + inline value_t operator OP(const balance_pair_t& value, \ + const value_t& obj) { \ + return value_t(value) OP obj; \ + } \ + inline value_t operator OP(const balance_t& value, \ + const value_t& obj) { \ + return value_t(value) OP obj; \ + } \ + inline value_t operator OP(const amount_t& value, \ + const value_t& obj) { \ + return value_t(value) OP obj; \ + } \ + template <typename T> \ + inline value_t operator OP(T value, const value_t& obj) { \ + return value_t(value) OP obj; \ + } + +DEF_VALUE_AUX_OP(+) +DEF_VALUE_AUX_OP(-) +DEF_VALUE_AUX_OP(*) +DEF_VALUE_AUX_OP(/) + +DEF_VALUE_AUX_OP(<) +DEF_VALUE_AUX_OP(<=) +DEF_VALUE_AUX_OP(>) +DEF_VALUE_AUX_OP(>=) +DEF_VALUE_AUX_OP(==) +DEF_VALUE_AUX_OP(!=) + +template <typename T> +value_t::operator T() const +{ + switch (type) { + case BOOLEAN: + return *((bool *) data); + case INTEGER: + return *((long *) data); + case AMOUNT: + return *((amount_t *) data); + case BALANCE: + return *((balance_t *) data); + case BALANCE_PAIR: + return *((balance_pair_t *) data); + + default: + assert(0); + break; + } + assert(0); + return 0; +} + +template <> value_t::operator long() const; +template <> value_t::operator double() const; + +inline value_t abs(const value_t& value) { + value_t temp(value); + temp.abs(); + return temp; +} + +inline std::ostream& operator<<(std::ostream& out, const value_t& value) { + switch (value.type) { + case value_t::BOOLEAN: + out << *((bool *) value.data); + break; + case value_t::INTEGER: + out << *((long *) value.data); + break; + case value_t::AMOUNT: + out << *((amount_t *) value.data); + break; + case value_t::BALANCE: + out << *((balance_t *) value.data); + break; + case value_t::BALANCE_PAIR: + out << *((balance_pair_t *) value.data); + break; + + default: + assert(0); + break; + } + return out; +} + +} // namespace ledger + +#endif // _VALUE_H diff --git a/walk.cc b/walk.cc new file mode 100644 index 00000000..d94723c2 --- /dev/null +++ b/walk.cc @@ -0,0 +1,1099 @@ +#include "walk.h" +#include "format.h" +#include "textual.h" +#include "util.h" + +namespace ledger { + +std::list<transaction_xdata_t> transactions_xdata; +std::list<void **> transactions_xdata_ptrs; + +std::list<account_xdata_t> accounts_xdata; +std::list<void **> accounts_xdata_ptrs; + +template <> +bool compare_items<transaction_t>::operator()(const transaction_t * left, + const transaction_t * right) +{ + assert(left); + assert(right); + + transaction_xdata_t& lxdata(transaction_xdata(*left)); + if (! (lxdata.dflags & TRANSACTION_SORT_CALC)) { + sort_order->compute(lxdata.sort_value, details_t(*left)); + lxdata.dflags |= TRANSACTION_SORT_CALC; + } + + transaction_xdata_t& rxdata(transaction_xdata(*right)); + if (! (rxdata.dflags & TRANSACTION_SORT_CALC)) { + sort_order->compute(rxdata.sort_value, details_t(*right)); + rxdata.dflags |= TRANSACTION_SORT_CALC; + } + + return lxdata.sort_value < rxdata.sort_value; +} + +transaction_xdata_t& transaction_xdata(const transaction_t& xact) +{ + if (! xact.data) { + transactions_xdata.push_back(transaction_xdata_t()); + xact.data = &transactions_xdata.back(); + transactions_xdata_ptrs.push_back(&xact.data); + } + return *((transaction_xdata_t *) xact.data); +} + +void add_transaction_to(const transaction_t& xact, value_t& value) +{ + if (transaction_has_xdata(xact) && + transaction_xdata_(xact).dflags & TRANSACTION_COMPOSITE) + value += transaction_xdata_(xact).composite_amount; + else if (xact.cost || value) + value.add(xact.amount, xact.cost); + else + value = xact.amount; +} + +void set_account_value::operator()(transaction_t& xact) +{ + add_transaction_to(xact, account_xdata(*xact.account).value); + account_xdata_(*xact.account).count++; + if (xact.flags & TRANSACTION_VIRTUAL) + account_xdata_(*xact.account).virtuals++; + + item_handler<transaction_t>::operator()(xact); +} + +void sort_transactions::flush() +{ + std::stable_sort(transactions.begin(), transactions.end(), + compare_items<transaction_t>(sort_order)); + + for (transactions_deque::iterator i = transactions.begin(); + i != transactions.end(); + i++) { + transaction_xdata(**i).dflags &= ~TRANSACTION_SORT_CALC; + item_handler<transaction_t>::operator()(**i); + } + + item_handler<transaction_t>::flush(); + + transactions.clear(); +} + +void calc_transactions::operator()(transaction_t& xact) +{ + transaction_xdata_t& xdata(transaction_xdata(xact)); + + if (last_xact && transaction_has_xdata(*last_xact)) { + xdata.total += transaction_xdata_(*last_xact).total; + xdata.index = transaction_xdata_(*last_xact).index + 1; + } else { + xdata.index = 0; + } + + if (! (xdata.dflags & TRANSACTION_NO_TOTAL)) + add_transaction_to(xact, xdata.total); + + item_handler<transaction_t>::operator()(xact); + + last_xact = &xact; +} + +void invert_transactions::operator()(transaction_t& xact) +{ + if (transaction_has_xdata(xact) && + transaction_xdata_(xact).dflags & TRANSACTION_COMPOSITE) { + transaction_xdata_(xact).composite_amount.negate(); + } else { + xact.amount.negate(); + if (xact.cost) + xact.cost->negate(); + } + + item_handler<transaction_t>::operator()(xact); +} + + +static inline +void handle_value(const value_t& value, + account_t * account, + entry_t * entry, + unsigned int flags, + std::list<transaction_t>& temps, + item_handler<transaction_t>& handler, + const std::time_t date = 0) +{ + temps.push_back(transaction_t(account)); + transaction_t& xact(temps.back()); + xact.entry = entry; + xact.flags |= TRANSACTION_BULK_ALLOC; + entry->add_transaction(&xact); + + // If the account for this transaction is all virtual, then report + // the transaction as such. This allows subtotal reports to show + // "(Account)" for accounts that contain only virtual transactions. + + if (account && account_has_xdata(*account)) + if (! (account_xdata_(*account).dflags & ACCOUNT_HAS_NON_VIRTUALS)) { + xact.flags |= TRANSACTION_VIRTUAL; + if (! (account_xdata_(*account).dflags & ACCOUNT_HAS_UNB_VIRTUALS)) + xact.flags |= TRANSACTION_BALANCE; + } + + transaction_xdata_t& xdata(transaction_xdata(xact)); + + switch (value.type) { + case value_t::BOOLEAN: + xact.amount = *((bool *) value.data); + break; + case value_t::INTEGER: + xact.amount = *((long *) value.data); + break; + case value_t::AMOUNT: + xact.amount = *((amount_t *) value.data); + break; + + case value_t::BALANCE: + case value_t::BALANCE_PAIR: + xdata.composite_amount = value; + flags |= TRANSACTION_COMPOSITE; + break; + } + + if (date) + xdata.date = date; + if (flags) + xdata.dflags |= flags; + + handler(xact); +} + +void collapse_transactions::report_subtotal() +{ + assert(count >= 1); + + if (count == 1) { + item_handler<transaction_t>::operator()(*last_xact); + } else { + entry_temps.push_back(entry_t()); + entry_t& entry = entry_temps.back(); + entry.payee = last_entry->payee; + entry.date = last_entry->date; + + handle_value(subtotal, &totals_account, last_entry, 0, xact_temps, + *handler); + } + + last_entry = NULL; + last_xact = NULL; + subtotal = 0L; + count = 0; +} + +void collapse_transactions::operator()(transaction_t& xact) +{ + // If we've reached a new entry, report on the subtotal + // accumulated thus far. + + if (last_entry && last_entry != xact.entry && count > 0) + report_subtotal(); + + add_transaction_to(xact, subtotal); + count++; + + last_entry = xact.entry; + last_xact = &xact; +} + +void related_transactions::flush() +{ + if (transactions.size() > 0) + for (transactions_list::iterator i = transactions.begin(); + i != transactions.end(); + i++) + for (transactions_list::iterator j = (*i)->entry->transactions.begin(); + j != (*i)->entry->transactions.end(); + j++) { + transaction_xdata_t& xdata = transaction_xdata(**j); + if (! (xdata.dflags & TRANSACTION_HANDLED) && + (! (xdata.dflags & TRANSACTION_RECEIVED) ? + ! ((*j)->flags & (TRANSACTION_AUTO | TRANSACTION_VIRTUAL)) : + also_matching)) { + xdata.dflags |= TRANSACTION_HANDLED; + item_handler<transaction_t>::operator()(**j); + } + } + + item_handler<transaction_t>::flush(); +} + +void changed_value_transactions::output_diff(const std::time_t current) +{ + value_t cur_bal; + + transaction_xdata(*last_xact).date = current; + compute_total(cur_bal, details_t(*last_xact)); + cur_bal.round(); + transaction_xdata(*last_xact).date = 0; + + if (value_t diff = cur_bal - last_balance) { + entry_temps.push_back(entry_t()); + entry_t& entry = entry_temps.back(); + entry.payee = "Commodities revalued"; + entry.date = current; + + handle_value(diff, NULL, &entry, TRANSACTION_NO_TOTAL, xact_temps, + *handler); + } +} + +void changed_value_transactions::operator()(transaction_t& xact) +{ + if (last_xact) { + std::time_t moment = 0; + if (transaction_has_xdata(*last_xact)) + moment = transaction_xdata_(*last_xact).date; + if (! moment) + moment = xact.entry->date; + output_diff(moment); + } + + if (changed_values_only) + transaction_xdata(xact).dflags |= TRANSACTION_DISPLAYED; + + item_handler<transaction_t>::operator()(xact); + + compute_total(last_balance, details_t(xact)); + last_balance.round(); + + last_xact = &xact; +} + +void subtotal_transactions::report_subtotal(const char * spec_fmt) +{ + char buf[256]; + + if (! spec_fmt) { + std::string fmt = "- "; + fmt += format_t::date_format; + std::strftime(buf, 255, fmt.c_str(), std::localtime(&finish)); + } else { + std::strftime(buf, 255, spec_fmt, std::localtime(&finish)); + } + + entry_temps.push_back(entry_t()); + entry_t& entry = entry_temps.back(); + entry.payee = buf; + entry.date = start; + + for (values_map::iterator i = values.begin(); + i != values.end(); + i++) + handle_value((*i).second, (*i).first, &entry, 0, xact_temps, + *handler, finish); + + values.clear(); +} + +void subtotal_transactions::operator()(transaction_t& xact) +{ + if (! start || std::difftime(xact.entry->date, start) < 0) + start = xact.entry->date; + if (! finish || std::difftime(xact.entry->date, finish) > 0) + finish = xact.entry->date; + + values_map::iterator i = values.find(xact.account); + if (i == values.end()) { + value_t temp; + add_transaction_to(xact, temp); + values.insert(values_pair(xact.account, temp)); + } else { + add_transaction_to(xact, (*i).second); + } + + // If the account for this transaction is all virtual, mark it as + // such, so that `handle_value' can show "(Account)" for accounts + // that contain only virtual transactions. + + if (! (xact.flags & TRANSACTION_VIRTUAL)) + account_xdata(*xact.account).dflags |= ACCOUNT_HAS_NON_VIRTUALS; + else if (! (xact.flags & TRANSACTION_BALANCE)) + account_xdata(*xact.account).dflags |= ACCOUNT_HAS_UNB_VIRTUALS; +} + +void interval_transactions::report_subtotal(const std::time_t moment) +{ + assert(last_xact); + + start = interval.begin; + if (moment) + finish = moment - 86400; + else + finish = last_xact->entry->date; + + subtotal_transactions::report_subtotal(); + + if (sorter) + subtotal_transactions::flush(); + + last_xact = NULL; +} + +void interval_transactions::operator()(transaction_t& xact) +{ + if ((interval.begin && + std::difftime(xact.entry->date, interval.begin) < 0) || + (interval.end && + std::difftime(xact.entry->date, interval.end) >= 0)) + return; + + if (interval) { + if (! started) { + if (! interval.begin) + interval.start(xact.entry->date); + start = interval.begin; + started = true; + } + + std::time_t quant = interval.increment(interval.begin); + if (std::difftime(xact.entry->date, quant) >= 0) { + if (last_xact) + report_subtotal(quant); + + std::time_t temp; + while (std::difftime(xact.entry->date, + temp = interval.increment(quant)) >= 0) { + if (quant == temp) + break; + quant = temp; + } + start = interval.begin = quant; + } + + subtotal_transactions::operator()(xact); + } else { + item_handler<transaction_t>::operator()(xact); + } + + last_xact = &xact; +} + +by_payee_transactions::~by_payee_transactions() +{ + for (payee_subtotals_map::iterator i = payee_subtotals.begin(); + i != payee_subtotals.end(); + i++) + delete (*i).second; +} + +void by_payee_transactions::flush() +{ + for (payee_subtotals_map::iterator i = payee_subtotals.begin(); + i != payee_subtotals.end(); + i++) + (*i).second->report_subtotal((*i).first.c_str()); + + item_handler<transaction_t>::flush(); + + payee_subtotals.clear(); +} + +void by_payee_transactions::operator()(transaction_t& xact) +{ + payee_subtotals_map::iterator i = payee_subtotals.find(xact.entry->payee); + if (i == payee_subtotals.end()) { + payee_subtotals_pair temp(xact.entry->payee, + new subtotal_transactions(handler)); + std::pair<payee_subtotals_map::iterator, bool> result + = payee_subtotals.insert(temp); + + assert(result.second); + if (! result.second) + return; + i = result.first; + } + + if (std::difftime(xact.entry->date, (*i).second->start) > 0) + (*i).second->start = xact.entry->date; + + (*(*i).second)(xact); +} + +void set_comm_as_payee::operator()(transaction_t& xact) +{ + entry_temps.push_back(*xact.entry); + entry_t& entry = entry_temps.back(); + entry.date = xact.entry->date; + entry.state = xact.entry->state; + entry.code = xact.entry->code; + entry.payee = xact.amount.commodity().symbol; + + xact_temps.push_back(xact); + transaction_t& temp = xact_temps.back(); + temp.entry = &entry; + temp.flags |= TRANSACTION_BULK_ALLOC; + entry.add_transaction(&temp); + + item_handler<transaction_t>::operator()(temp); +} + +void dow_transactions::flush() +{ + for (int i = 0; i < 7; i++) { + start = finish = 0; + for (transactions_list::iterator d = days_of_the_week[i].begin(); + d != days_of_the_week[i].end(); + d++) + subtotal_transactions::operator()(**d); + subtotal_transactions::report_subtotal("%As"); + days_of_the_week[i].clear(); + } + + subtotal_transactions::flush(); +} + +void generate_transactions::add_period_entries + (period_entries_list& period_entries) +{ + for (period_entries_list::iterator i = period_entries.begin(); + i != period_entries.end(); + i++) + for (transactions_list::iterator j = (*i)->transactions.begin(); + j != (*i)->transactions.end(); + j++) + add_transaction((*i)->period, **j); +} + +void generate_transactions::add_transaction(const interval_t& period, + transaction_t& xact) +{ + pending_xacts.push_back(pending_xacts_pair(period, &xact)); +} + +void budget_transactions::report_budget_items(const std::time_t moment) +{ + if (pending_xacts.size() > 0) { + std::list<pending_xacts_list::iterator> to_clear; + + bool reported; + do { + reported = false; + for (pending_xacts_list::iterator i = pending_xacts.begin(); + i != pending_xacts.end(); + i++) { + std::time_t& begin = (*i).first.begin; + if (! begin) { + (*i).first.start(moment); + begin = (*i).first.begin; + } + + if (std::difftime(begin, moment) < 0 && + (! (*i).first.end || std::difftime(begin, (*i).first.end) < 0)) { + transaction_t& xact = *(*i).second; + + DEBUG_PRINT("ledger.walk.budget", "Reporting budget for " + << xact.account->fullname()); + DEBUG_PRINT_TIME("ledger.walk.budget", begin); + DEBUG_PRINT_TIME("ledger.walk.budget", moment); + + entry_temps.push_back(entry_t()); + entry_t& entry = entry_temps.back(); + entry.payee = "Budget entry"; + entry.date = begin; + + xact_temps.push_back(xact); + transaction_t& temp = xact_temps.back(); + temp.entry = &entry; + temp.flags |= TRANSACTION_AUTO; + temp.amount.negate(); + temp.flags |= TRANSACTION_BULK_ALLOC; + entry.add_transaction(&temp); + + begin = (*i).first.increment(begin); + + item_handler<transaction_t>::operator()(temp); + + reported = true; + } + } + } while (reported); + + for (std::list<pending_xacts_list::iterator>::iterator + i = to_clear.begin(); + i != to_clear.end(); + i++) + pending_xacts.erase(*i); + } +} + +void budget_transactions::operator()(transaction_t& xact) +{ + bool xact_in_budget = false; + + for (pending_xacts_list::iterator i = pending_xacts.begin(); + i != pending_xacts.end(); + i++) + for (account_t * acct = xact.account; acct; acct = acct->parent) { + if (acct == (*i).second->account) { + xact_in_budget = true; + // Report the transaction as if it had occurred in the parent + // account. + if (xact.account != acct) + xact.account = acct; + goto handle; + } + } + + handle: + if (xact_in_budget && flags & BUDGET_BUDGETED) { + report_budget_items(xact.entry->date); + item_handler<transaction_t>::operator()(xact); + } + else if (! xact_in_budget && flags & BUDGET_UNBUDGETED) { + item_handler<transaction_t>::operator()(xact); + } +} + +void forecast_transactions::add_transaction(const interval_t& period, + transaction_t& xact) +{ + generate_transactions::add_transaction(period, xact); + + interval_t& i = pending_xacts.back().first; + if (! i.begin) { + i.start(now); + i.begin = i.increment(i.begin); + } else { + while (std::difftime(i.begin, now) < 0) + i.begin = i.increment(i.begin); + } +} + +void forecast_transactions::flush() +{ + transactions_list passed; + std::time_t last = 0; + + while (pending_xacts.size() > 0) { + pending_xacts_list::iterator least = pending_xacts.begin(); + for (pending_xacts_list::iterator i = ++pending_xacts.begin(); + i != pending_xacts.end(); + i++) + if (std::difftime((*i).first.begin, (*least).first.begin) < 0) + least = i; + + std::time_t& begin = (*least).first.begin; + + if ((*least).first.end && + std::difftime(begin, (*least).first.end) >= 0) { + pending_xacts.erase(least); + passed.remove((*least).second); + continue; + } + + transaction_t& xact = *(*least).second; + + entry_temps.push_back(entry_t()); + entry_t& entry = entry_temps.back(); + entry.payee = "Forecast entry"; + entry.date = begin; + + xact_temps.push_back(xact); + transaction_t& temp = xact_temps.back(); + temp.entry = &entry; + temp.flags |= TRANSACTION_AUTO; + temp.flags |= TRANSACTION_BULK_ALLOC; + entry.add_transaction(&temp); + + std::time_t next = (*least).first.increment(begin); + if (std::difftime(next, begin) < 0 || // wraparound + (last && std::difftime(next, last) > 5 * 365 * 24 * 60 * 60)) + break; + begin = next; + + item_handler<transaction_t>::operator()(temp); + + if (transaction_has_xdata(temp) && + transaction_xdata_(temp).dflags & TRANSACTION_MATCHES) { + if (! pred(temp)) + break; + last = temp.entry->date; + passed.clear(); + } else { + bool found = false; + for (transactions_list::iterator i = passed.begin(); + i != passed.end(); + i++) + if (*i == &xact) { + found = true; + break; + } + + if (! found) { + passed.push_back(&xact); + if (passed.size() >= pending_xacts.size()) + break; + } + } + } + + item_handler<transaction_t>::flush(); +} + +void clear_transactions_xdata() +{ + transactions_xdata.clear(); + + for (std::list<void **>::iterator i = transactions_xdata_ptrs.begin(); + i != transactions_xdata_ptrs.end(); + i++) + **i = NULL; +} + +template <> +bool compare_items<account_t>::operator()(const account_t * left, + const account_t * right) +{ + assert(left); + assert(right); + + account_xdata_t& lxdata(account_xdata(*left)); + if (! (lxdata.dflags & ACCOUNT_SORT_CALC)) { + sort_order->compute(lxdata.sort_value, details_t(*left)); + lxdata.dflags |= ACCOUNT_SORT_CALC; + } + + account_xdata_t& rxdata(account_xdata(*right)); + if (! (rxdata.dflags & ACCOUNT_SORT_CALC)) { + sort_order->compute(rxdata.sort_value, details_t(*right)); + rxdata.dflags |= ACCOUNT_SORT_CALC; + } + + return lxdata.sort_value < rxdata.sort_value; +} + +account_xdata_t& account_xdata(const account_t& account) +{ + if (! account.data) { + accounts_xdata.push_back(account_xdata_t()); + account.data = &accounts_xdata.back(); + accounts_xdata_ptrs.push_back(&account.data); + } + return *((account_xdata_t *) account.data); +} + +void sum_accounts(account_t& account) +{ + account_xdata_t& xdata(account_xdata(account)); + + for (accounts_map::iterator i = account.accounts.begin(); + i != account.accounts.end(); + i++) { + sum_accounts(*(*i).second); + + xdata.total += account_xdata_(*(*i).second).total; + xdata.total_count += (account_xdata_(*(*i).second).total_count + + account_xdata_(*(*i).second).count); + } + + value_t result; + compute_amount(result, details_t(account)); + if (result) + xdata.total += result; + xdata.total_count += xdata.count; +} + +void sort_accounts(account_t& account, + const value_expr_t * sort_order, + accounts_deque& accounts) +{ + for (accounts_map::iterator i = account.accounts.begin(); + i != account.accounts.end(); + i++) + accounts.push_back((*i).second); + + std::stable_sort(accounts.begin(), accounts.end(), + compare_items<account_t>(sort_order)); +} + +void walk_accounts(account_t& account, + item_handler<account_t>& handler, + const value_expr_t * sort_order) +{ + handler(account); + + if (sort_order) { + accounts_deque accounts; + sort_accounts(account, sort_order, accounts); + for (accounts_deque::const_iterator i = accounts.begin(); + i != accounts.end(); + i++) { + account_xdata(**i).dflags &= ~ACCOUNT_SORT_CALC; + walk_accounts(**i, handler, sort_order); + } + } else { + for (accounts_map::const_iterator i = account.accounts.begin(); + i != account.accounts.end(); + i++) + walk_accounts(*(*i).second, handler, NULL); + } +} + +void walk_accounts(account_t& account, + item_handler<account_t>& handler, + const std::string& sort_string) +{ + if (! sort_string.empty()) { + std::auto_ptr<value_expr_t> sort_order; + try { + sort_order.reset(parse_value_expr(sort_string)); + } + catch (value_expr_error& err) { + throw error(std::string("In sort string '" + sort_string + "': " + + err.what())); + } + walk_accounts(account, handler, sort_order.get()); + } else { + walk_accounts(account, handler); + } +} + +void clear_accounts_xdata() +{ + accounts_xdata.clear(); + + for (std::list<void **>::iterator i = accounts_xdata_ptrs.begin(); + i != accounts_xdata_ptrs.end(); + i++) + **i = NULL; +} + + +void walk_commodities(commodities_map& commodities, + item_handler<transaction_t>& handler) +{ + std::list<transaction_t> xact_temps; + std::list<entry_t> entry_temps; + std::list<account_t> acct_temps; + + for (commodities_map::iterator i = commodities.begin(); + i != commodities.end(); + i++) { + if ((*i).second->flags & COMMODITY_STYLE_NOMARKET) + continue; + + entry_temps.push_back(entry_t()); + acct_temps.push_back(account_t(NULL, (*i).second->symbol)); + + if ((*i).second->history) + for (history_map::iterator j = (*i).second->history->prices.begin(); + j != (*i).second->history->prices.end(); + j++) { + entry_temps.back().date = (*j).first; + + xact_temps.push_back(transaction_t(&acct_temps.back())); + transaction_t& temp = xact_temps.back(); + temp.entry = &entry_temps.back(); + temp.amount = (*j).second; + temp.flags |= TRANSACTION_BULK_ALLOC; + entry_temps.back().add_transaction(&temp); + + handler(xact_temps.back()); + } + } + + handler.flush(); + + clear_entries_transactions(entry_temps); +} + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> + +using namespace boost::python; +using namespace ledger; + +template <typename T> +struct item_handler_wrap : public item_handler<T> +{ + PyObject * self; + + item_handler_wrap(PyObject * self_) : self(self_) {} + item_handler_wrap(PyObject * self_, const item_handler<T>& _handler) + : item_handler<T>(const_cast<item_handler<T> *>(&_handler)), + self(self_) {} + item_handler_wrap(PyObject * self_, item_handler<T> * _handler) + : item_handler<T>(_handler), self(self_) {} + + virtual void flush() { + call_method<void>(self, "flush"); + } + void default_flush() { + item_handler<T>::flush(); + } + + virtual void operator()(T& item) { + call_method<void>(self, "__call__", ptr(&item)); + } + void default_call(T& item) { + item_handler<T>::operator()(item); + } +}; + +void (subtotal_transactions::*subtotal_transactions_flush)() = + &subtotal_transactions::flush; + +void py_walk_entries(journal_t& journal, + item_handler<transaction_t>& handler) { + walk_entries(journal.entries, handler); +} + +void py_walk_transactions(entry_t& entry, + item_handler<transaction_t>& handler) { + walk_transactions(entry.transactions, handler); +} + +void py_walk_accounts_1(account_t& account, + item_handler<account_t>& handler) { + walk_accounts(account, handler); +} + +void py_walk_accounts_2(account_t& account, + item_handler<account_t>& handler, + const value_expr_t * sort_order) { + walk_accounts(account, handler, sort_order); +} + +void py_walk_accounts_3(account_t& account, + item_handler<account_t>& handler, + const std::string& sort_string) { + walk_accounts(account, handler, sort_string); +} + +void py_walk_commodities(item_handler<transaction_t>& handler) { + walk_commodities(commodity_t::commodities, handler); +} + +void py_add_period_entries(generate_transactions& handler, + journal_t * journal) { + handler.add_period_entries(journal->period_entries); +} + +void export_walk() +{ + typedef item_handler<transaction_t> xact_handler_t; + + scope().attr("TRANSACTION_RECEIVED") = TRANSACTION_RECEIVED; + scope().attr("TRANSACTION_HANDLED") = TRANSACTION_HANDLED; + scope().attr("TRANSACTION_TO_DISPLAY") = TRANSACTION_TO_DISPLAY; + scope().attr("TRANSACTION_DISPLAYED") = TRANSACTION_DISPLAYED; + scope().attr("TRANSACTION_NO_TOTAL") = TRANSACTION_NO_TOTAL; + scope().attr("TRANSACTION_SORT_CALC") = TRANSACTION_SORT_CALC; + scope().attr("TRANSACTION_COMPOSITE") = TRANSACTION_COMPOSITE; + scope().attr("TRANSACTION_MATCHES") = TRANSACTION_MATCHES; + + class_< transaction_xdata_t > ("TransactionXData") + .def_readwrite("total", &transaction_xdata_t::total) + .def_readwrite("sort_value", &transaction_xdata_t::sort_value) + .def_readwrite("composite_amount", &transaction_xdata_t::composite_amount) + .def_readwrite("index", &transaction_xdata_t::index) + .def_readwrite("dflags", &transaction_xdata_t::dflags) + ; + + def("transaction_has_xdata", transaction_has_xdata); + def("transaction_xdata", transaction_xdata, return_internal_reference<1>()); + def("clear_transactions_xdata", clear_transactions_xdata); + def("add_transaction_to", add_transaction_to); + + class_< xact_handler_t, item_handler_wrap<transaction_t> > + ("TransactionHandler") + .def(init<xact_handler_t *>()[with_custodian_and_ward<1, 2>()]) + + .def("flush", &xact_handler_t::flush, + &item_handler_wrap<transaction_t>::default_flush) + .def("__call__", &xact_handler_t::operator(), + &item_handler_wrap<transaction_t>::default_call) + ; + + class_< ignore_transactions, bases<xact_handler_t> > + ("IgnoreTransactions") + .def("flush", &xact_handler_t::flush) + .def("__call__", &ignore_transactions::operator()); + ; + + class_< set_account_value, bases<xact_handler_t> > ("SetAccountValue") + .def(init<xact_handler_t *>()[with_custodian_and_ward<1, 2>()]) + .def("flush", &xact_handler_t::flush) + .def("__call__", &set_account_value::operator()); + ; + + class_< sort_transactions, bases<xact_handler_t> > + ("SortTransactions", init<xact_handler_t *, const value_expr_t *>() + [with_custodian_and_ward<1, 2>()]) + .def(init<xact_handler_t *, const std::string&>() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &sort_transactions::flush) + .def("__call__", &sort_transactions::operator()); + ; + + class_< filter_transactions, bases<xact_handler_t> > + ("FilterTransactions", init<xact_handler_t *, std::string>() + [with_custodian_and_ward<1, 2>()]) + .def(init<xact_handler_t *, const std::string&>() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &xact_handler_t::flush) + .def("__call__", &filter_transactions::operator()); + ; + + class_< calc_transactions, bases<xact_handler_t> > + ("CalcTransactions", init<xact_handler_t *>() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &xact_handler_t::flush) + .def("__call__", &calc_transactions::operator()); + ; + + class_< invert_transactions, bases<xact_handler_t> > + ("InvertTransactions", init<xact_handler_t *>() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &xact_handler_t::flush) + .def("__call__", &invert_transactions::operator()); + ; + + class_< collapse_transactions, bases<xact_handler_t> > + ("CollapseTransactions", init<xact_handler_t *>() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &collapse_transactions::flush) + .def("__call__", &collapse_transactions::operator()); + ; + + class_< related_transactions, bases<xact_handler_t> > + ("RelatedTransactions", init<xact_handler_t *, optional<bool> >() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &xact_handler_t::flush) + .def("__call__", &related_transactions::operator()); + ; + + class_< changed_value_transactions, bases<xact_handler_t> > + ("ChangeValueTransactions", init<xact_handler_t *, bool>() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &changed_value_transactions::flush) + .def("__call__", &changed_value_transactions::operator()); + ; + + class_< subtotal_transactions, bases<xact_handler_t> > + ("SubtotalTransactions", init<xact_handler_t *>() + [with_custodian_and_ward<1, 2>()]) + .def("flush", subtotal_transactions_flush) + .def("__call__", &subtotal_transactions::operator()); + ; + + class_< interval_transactions, bases<xact_handler_t> > + ("IntervalTransactions", + init<xact_handler_t *, interval_t, optional<value_expr_t *> >() + [with_custodian_and_ward<1, 2>()]) + .def(init<xact_handler_t *, const std::string&, + optional<const std::string&> >() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &xact_handler_t::flush) + .def("__call__", &interval_transactions::operator()); + ; + + class_< by_payee_transactions, bases<xact_handler_t> > + ("ByPayeeTransactions", init<xact_handler_t *>() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &by_payee_transactions::flush) + .def("__call__", &by_payee_transactions::operator()); + ; + + class_< set_comm_as_payee, bases<xact_handler_t> > + ("SetCommAsPayee", init<xact_handler_t *>() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &xact_handler_t::flush) + .def("__call__", &xact_handler_t::operator()); + ; + + class_< dow_transactions, bases<xact_handler_t> > + ("DowTransactions", init<xact_handler_t *>() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &dow_transactions::flush) + .def("__call__", &dow_transactions::operator()); + ; + + scope().attr("BUDGET_BUDGETED") = BUDGET_BUDGETED; + scope().attr("BUDGET_UNBUDGETED") = BUDGET_UNBUDGETED; + + class_< generate_transactions, bases<xact_handler_t> > + ("GenerateTransactions", init<xact_handler_t *>() + [with_custodian_and_ward<1, 2>()]) + .def("add_transaction", &generate_transactions::add_transaction) + .def("add_period_entries", py_add_period_entries) + .def("flush", &xact_handler_t::flush) + .def("__call__", &xact_handler_t::operator()); + ; + + class_< budget_transactions, bases<generate_transactions> > + ("BudgetTransactions", + init<xact_handler_t *, unsigned long>() + [with_custodian_and_ward<1, 2>()]) + .def("add_transaction", &generate_transactions::add_transaction) + .def("add_period_entries", py_add_period_entries) + .def("flush", &budget_transactions::flush) + .def("__call__", &xact_handler_t::operator()); + ; + + class_< forecast_transactions, bases<generate_transactions> > + ("ForecastTransactions", + init<xact_handler_t *, std::string>() + [with_custodian_and_ward<1, 2>()]) + .def("add_transaction", &forecast_transactions::add_transaction) + .def("add_period_entries", py_add_period_entries) + .def("flush", &forecast_transactions::flush) + .def("__call__", &xact_handler_t::operator()); + ; + + def("walk_entries", py_walk_entries); + def("walk_transactions", py_walk_transactions); + + typedef item_handler<account_t> account_handler_t; + + scope().attr("ACCOUNT_TO_DISPLAY") = ACCOUNT_TO_DISPLAY; + scope().attr("ACCOUNT_DISPLAYED") = ACCOUNT_DISPLAYED; + scope().attr("ACCOUNT_SORT_CALC") = ACCOUNT_SORT_CALC; + + class_< account_xdata_t > ("AccountXData") + .def_readwrite("value", &account_xdata_t::value) + .def_readwrite("total", &account_xdata_t::total) + .def_readwrite("sort_value", &account_xdata_t::sort_value) + .def_readwrite("count", &account_xdata_t::count) + .def_readwrite("total_count", &account_xdata_t::total_count) + .def_readwrite("virtuals", &account_xdata_t::virtuals) + .def_readwrite("dflags", &account_xdata_t::dflags) + ; + + def("account_has_xdata", account_has_xdata); + def("account_xdata", account_xdata, return_internal_reference<1>()); + def("clear_accounts_xdata", clear_accounts_xdata); + + class_< account_handler_t, item_handler_wrap<account_t> > + ("AccountHandler") + .def(init<account_handler_t *>()[with_custodian_and_ward<1, 2>()]) + + .def("flush", &account_handler_t::flush, + &item_handler_wrap<account_t>::default_flush) + .def("__call__", &account_handler_t::operator(), + &item_handler_wrap<account_t>::default_call) + ; + + def("sum_accounts", sum_accounts); + def("walk_accounts", py_walk_accounts_1); + def("walk_accounts", py_walk_accounts_2); + def("walk_accounts", py_walk_accounts_3); + + def("walk_commodities", py_walk_commodities); +} + +#endif // USE_BOOST_PYTHON @@ -0,0 +1,580 @@ +#ifndef _WALK_H +#define _WALK_H + +#include "journal.h" +#include "balance.h" +#include "valexpr.h" +#include "datetime.h" + +#include <iostream> +#include <fstream> +#include <deque> + +namespace ledger { + +template <typename T> +struct item_handler { + item_handler * handler; + + public: + item_handler() : handler(NULL) { + DEBUG_PRINT("ledger.memory.ctors", "ctor item_handler<T>"); + } + item_handler(item_handler * _handler) : handler(_handler) { + DEBUG_PRINT("ledger.memory.ctors", "ctor item_handler<T>"); + } + + virtual ~item_handler() { + DEBUG_PRINT("ledger.memory.dtors", "dtor item_handler<T>"); + } + virtual void flush() { + if (handler) + handler->flush(); + } + virtual void operator()(T& item) { + if (handler) + (*handler)(item); + } +}; + +template <typename T> +class compare_items { + const value_expr_t * sort_order; + public: + compare_items(const value_expr_t * _sort_order) + : sort_order(_sort_order) { + assert(sort_order); + } + bool operator()(const T * left, const T * right); +}; + +template <typename T> +bool compare_items<T>::operator()(const T * left, const T * right) +{ + assert(left); + assert(right); + + value_t left_result; + value_t right_result; + sort_order->compute(left_result, details_t(*left)); + sort_order->compute(right_result, details_t(*right)); + + return left_result < right_result; +} + +template <> +bool compare_items<transaction_t>::operator()(const transaction_t * left, + const transaction_t * right); +template <> +bool compare_items<account_t>::operator()(const account_t * left, + const account_t * right); + +////////////////////////////////////////////////////////////////////// +// +// Transaction handlers +// + +#define TRANSACTION_RECEIVED 0x0001 +#define TRANSACTION_HANDLED 0x0002 +#define TRANSACTION_TO_DISPLAY 0x0004 +#define TRANSACTION_DISPLAYED 0x0008 +#define TRANSACTION_NO_TOTAL 0x0010 +#define TRANSACTION_SORT_CALC 0x0020 +#define TRANSACTION_COMPOSITE 0x0040 +#define TRANSACTION_MATCHES 0x0080 + +struct transaction_xdata_t +{ + value_t total; + value_t sort_value; + value_t composite_amount; + unsigned int index; + unsigned short dflags; + std::time_t date; + + transaction_xdata_t() : index(0), dflags(0), date(0) {} +}; + +inline bool transaction_has_xdata(const transaction_t& xact) { + return xact.data != NULL; +} + +extern std::list<transaction_xdata_t> transactions_xdata; +extern std::list<void **> transactions_xdata_ptrs; + +inline transaction_xdata_t& transaction_xdata_(const transaction_t& xact) { + return *((transaction_xdata_t *) xact.data); +} + +transaction_xdata_t& transaction_xdata(const transaction_t& xact); +void add_transaction_to(const transaction_t& xact, value_t& value); + +////////////////////////////////////////////////////////////////////// + +inline void walk_transactions(transactions_list::iterator begin, + transactions_list::iterator end, + item_handler<transaction_t>& handler) { + for (transactions_list::iterator i = begin; i != end; i++) + handler(**i); +} + +inline void walk_transactions(transactions_list& list, + item_handler<transaction_t>& handler) { + walk_transactions(list.begin(), list.end(), handler); +} + +inline void walk_entries(entries_list::iterator begin, + entries_list::iterator end, + item_handler<transaction_t>& handler) { + for (entries_list::iterator i = begin; i != end; i++) + walk_transactions((*i)->transactions, handler); +} + +inline void walk_entries(entries_list& list, + item_handler<transaction_t>& handler) { + walk_entries(list.begin(), list.end(), handler); +} + +void clear_transactions_xdata(); + +////////////////////////////////////////////////////////////////////// + +class ignore_transactions : public item_handler<transaction_t> +{ + public: + virtual void operator()(transaction_t& xact) {} +}; + +class set_account_value : public item_handler<transaction_t> +{ + public: + set_account_value(item_handler<transaction_t> * handler = NULL) + : item_handler<transaction_t>(handler) {} + + virtual void operator()(transaction_t& xact); +}; + +class sort_transactions : public item_handler<transaction_t> +{ + typedef std::deque<transaction_t *> transactions_deque; + + transactions_deque transactions; + const value_expr_t * sort_order; + bool allocated; + + public: + sort_transactions(item_handler<transaction_t> * handler, + const value_expr_t * _sort_order) + : item_handler<transaction_t>(handler), + sort_order(_sort_order), allocated(false) {} + + sort_transactions(item_handler<transaction_t> * handler, + const std::string& _sort_order) + : item_handler<transaction_t>(handler), allocated(false) { + try { + sort_order = parse_value_expr(_sort_order); + allocated = true; + } + catch (value_expr_error& err) { + throw value_expr_error(std::string("In sort string '") + _sort_order + + "': " + err.what()); + } + } + + virtual ~sort_transactions() { + assert(sort_order); + if (allocated) + delete sort_order; + } + + virtual void flush(); + virtual void operator()(transaction_t& xact) { + transactions.push_back(&xact); + } +}; + +class filter_transactions : public item_handler<transaction_t> +{ + item_predicate<transaction_t> pred; + + public: + filter_transactions(item_handler<transaction_t> * handler, + const value_expr_t * predicate) + : item_handler<transaction_t>(handler), pred(predicate) {} + + filter_transactions(item_handler<transaction_t> * handler, + const std::string& predicate) + : item_handler<transaction_t>(handler), pred(predicate) {} + + virtual void operator()(transaction_t& xact) { + if (pred(xact)) { + transaction_xdata(xact).dflags |= TRANSACTION_MATCHES; + (*handler)(xact); + } + } +}; + +class calc_transactions : public item_handler<transaction_t> +{ + transaction_t * last_xact; + + public: + calc_transactions(item_handler<transaction_t> * handler) + : item_handler<transaction_t>(handler), last_xact(NULL) {} + + virtual void operator()(transaction_t& xact); +}; + +class invert_transactions : public item_handler<transaction_t> +{ + public: + invert_transactions(item_handler<transaction_t> * handler) + : item_handler<transaction_t>(handler) {} + + virtual void operator()(transaction_t& xact); +}; + +inline void clear_entries_transactions(std::list<entry_t>& entries_list) { + for (std::list<entry_t>::iterator i = entries_list.begin(); + i != entries_list.end(); + i++) + (*i).transactions.clear(); +} + +class collapse_transactions : public item_handler<transaction_t> +{ + value_t subtotal; + unsigned int count; + entry_t * last_entry; + transaction_t * last_xact; + account_t totals_account; + + std::list<entry_t> entry_temps; + std::list<transaction_t> xact_temps; + + public: + collapse_transactions(item_handler<transaction_t> * handler) + : item_handler<transaction_t>(handler), count(0), + last_entry(NULL), last_xact(NULL), + totals_account(NULL, "<Total>") {} + + ~collapse_transactions() { + clear_entries_transactions(entry_temps); + } + + virtual void flush() { + if (subtotal) + report_subtotal(); + item_handler<transaction_t>::flush(); + } + + void report_subtotal(); + + virtual void operator()(transaction_t& xact); +}; + +class related_transactions : public item_handler<transaction_t> +{ + transactions_list transactions; + bool also_matching; + + public: + related_transactions(item_handler<transaction_t> * handler, + const bool _also_matching = false) + : item_handler<transaction_t>(handler), + also_matching(_also_matching) {} + + virtual void flush(); + virtual void operator()(transaction_t& xact) { + transaction_xdata(xact).dflags |= TRANSACTION_RECEIVED; + transactions.push_back(&xact); + } +}; + +class changed_value_transactions : public item_handler<transaction_t> +{ + // This filter requires that calc_transactions be used at some point + // later in the chain. + + bool changed_values_only; + transaction_t * last_xact; + value_t last_balance; + + std::list<entry_t> entry_temps; + std::list<transaction_t> xact_temps; + + public: + changed_value_transactions(item_handler<transaction_t> * handler, + bool _changed_values_only) + : item_handler<transaction_t>(handler), + changed_values_only(_changed_values_only), last_xact(NULL) {} + + ~changed_value_transactions() { + clear_entries_transactions(entry_temps); + } + + virtual void flush() { + if (last_xact) { + output_diff(now); + last_xact = NULL; + } + item_handler<transaction_t>::flush(); + } + + void output_diff(const std::time_t current); + + virtual void operator()(transaction_t& xact); +}; + +class subtotal_transactions : public item_handler<transaction_t> +{ + typedef std::map<account_t *, value_t> values_map; + typedef std::pair<account_t *, value_t> values_pair; + + protected: + values_map values; + + std::list<entry_t> entry_temps; + std::list<transaction_t> xact_temps; + + public: + std::time_t start; + std::time_t finish; + + subtotal_transactions(item_handler<transaction_t> * handler) + : item_handler<transaction_t>(handler), start(0), finish(0) {} +#ifdef DEBUG_ENABLED + subtotal_transactions(const subtotal_transactions&) { + assert(0); + } +#endif + + ~subtotal_transactions() { + clear_entries_transactions(entry_temps); + } + + void report_subtotal(const char * spec_fmt = NULL); + + virtual void flush() { + if (values.size() > 0) + report_subtotal(); + item_handler<transaction_t>::flush(); + } + virtual void operator()(transaction_t& xact); +}; + +class interval_transactions : public subtotal_transactions +{ + interval_t interval; + transaction_t * last_xact; + bool started; + + item_handler<transaction_t> * sorter; + + public: + interval_transactions(item_handler<transaction_t> * _handler, + const interval_t& _interval, + const value_expr_t * sort_order = NULL) + : subtotal_transactions(_handler), interval(_interval), + last_xact(NULL), started(false), sorter(NULL) { + if (sort_order) { + sorter = new sort_transactions(handler, sort_order); + handler = sorter; + } + } + interval_transactions(item_handler<transaction_t> * _handler, + const std::string& _interval, + const std::string& sort_order = "") + : subtotal_transactions(_handler), interval(_interval), + last_xact(NULL), started(false), sorter(NULL) { + if (! sort_order.empty()) { + sorter = new sort_transactions(handler, sort_order); + handler = sorter; + } + } + + virtual ~interval_transactions() { + if (sorter) + delete sorter; + } + + void report_subtotal(const std::time_t moment = 0); + + virtual void flush() { + if (last_xact) + report_subtotal(); + subtotal_transactions::flush(); + } + virtual void operator()(transaction_t& xact); +}; + +class by_payee_transactions : public item_handler<transaction_t> +{ + typedef std::map<std::string, subtotal_transactions *> payee_subtotals_map; + typedef std::pair<std::string, subtotal_transactions *> payee_subtotals_pair; + + payee_subtotals_map payee_subtotals; + + public: + by_payee_transactions(item_handler<transaction_t> * handler) + : item_handler<transaction_t>(handler) {} + + virtual ~by_payee_transactions(); + + virtual void flush(); + virtual void operator()(transaction_t& xact); +}; + +class set_comm_as_payee : public item_handler<transaction_t> +{ + std::list<entry_t> entry_temps; + std::list<transaction_t> xact_temps; + + public: + set_comm_as_payee(item_handler<transaction_t> * handler) + : item_handler<transaction_t>(handler) {} + + ~set_comm_as_payee() { + clear_entries_transactions(entry_temps); + } + + virtual void operator()(transaction_t& xact); +}; + +class dow_transactions : public subtotal_transactions +{ + transactions_list days_of_the_week[7]; + + public: + dow_transactions(item_handler<transaction_t> * handler) + : subtotal_transactions(handler) {} + + virtual void flush(); + virtual void operator()(transaction_t& xact) { + struct std::tm * desc = std::localtime(&xact.entry->date); + days_of_the_week[desc->tm_wday].push_back(&xact); + } +}; + +class generate_transactions : public item_handler<transaction_t> +{ + protected: + typedef std::pair<interval_t, transaction_t *> pending_xacts_pair; + typedef std::list<pending_xacts_pair> pending_xacts_list; + + pending_xacts_list pending_xacts; + std::list<entry_t> entry_temps; + std::list<transaction_t> xact_temps; + + public: + generate_transactions(item_handler<transaction_t> * handler) + : item_handler<transaction_t>(handler) {} + + ~generate_transactions() { + clear_entries_transactions(entry_temps); + } + + void add_period_entries(period_entries_list& period_entries); + + virtual void add_transaction(const interval_t& period, transaction_t& xact); +}; + +#define BUDGET_NO_BUDGET 0x00 +#define BUDGET_BUDGETED 0x01 +#define BUDGET_UNBUDGETED 0x02 + +class budget_transactions : public generate_transactions +{ + unsigned short flags; + + public: + budget_transactions(item_handler<transaction_t> * handler, + unsigned long _flags = BUDGET_BUDGETED) + : generate_transactions(handler), flags(_flags) {} + + void report_budget_items(const std::time_t moment); + + virtual void operator()(transaction_t& xact); +}; + +class forecast_transactions : public generate_transactions +{ + item_predicate<transaction_t> pred; + + public: + forecast_transactions(item_handler<transaction_t> * handler, + const value_expr_t * predicate) + : generate_transactions(handler), pred(predicate) {} + + forecast_transactions(item_handler<transaction_t> * handler, + const std::string& predicate) + : generate_transactions(handler), pred(predicate) {} + + virtual void add_transaction(const interval_t& period, + transaction_t& xact); + virtual void flush(); +}; + + +////////////////////////////////////////////////////////////////////// +// +// Account walking functions +// + +#define ACCOUNT_TO_DISPLAY 0x0001 +#define ACCOUNT_DISPLAYED 0x0002 +#define ACCOUNT_SORT_CALC 0x0004 +#define ACCOUNT_HAS_NON_VIRTUALS 0x0008 +#define ACCOUNT_HAS_UNB_VIRTUALS 0x0010 + +struct account_xdata_t +{ + value_t value; + value_t total; + value_t sort_value; + unsigned int count; // transactions counted toward amount + unsigned int total_count; // transactions counted toward total + unsigned int virtuals; + unsigned short dflags; + + account_xdata_t() : count(0), total_count(0), virtuals(0), dflags(0) {} +}; + +inline bool account_has_xdata(const account_t& account) { + return account.data != NULL; +} + +extern std::list<account_xdata_t> accounts_xdata; +extern std::list<void **> accounts_xdata_ptrs; + +inline account_xdata_t& account_xdata_(const account_t& account) { + return *((account_xdata_t *) account.data); +} + +account_xdata_t& account_xdata(const account_t& account); + +////////////////////////////////////////////////////////////////////// + +void sum_accounts(account_t& account); + +typedef std::deque<account_t *> accounts_deque; + +void sort_accounts(account_t& account, + const value_expr_t * sort_order, + accounts_deque& accounts); +void walk_accounts(account_t& account, + item_handler<account_t>& handler, + const value_expr_t * sort_order = NULL); +void walk_accounts(account_t& account, + item_handler<account_t>& handler, + const std::string& sort_string); +void clear_accounts_xdata(); + +////////////////////////////////////////////////////////////////////// + +void walk_commodities(commodities_map& commodities, + item_handler<transaction_t>& handler); + +} // namespace ledger + +#endif // _WALK_H @@ -0,0 +1,220 @@ +#include "xml.h" +#include "journal.h" +#include "datetime.h" +#include "error.h" + +#include <iostream> +#include <sstream> +#include <cstring> + +extern "C" { +#include <xmlparse.h> // expat XML parser +} + +namespace ledger { + +static XML_Parser current_parser; +static unsigned int count; + +static journal_t * curr_journal; +static entry_t * curr_entry; +static commodity_t * curr_comm; +static std::string comm_flags; + +static std::string data; +static bool ignore; +static std::string have_error; + +static void startElement(void *userData, const char *name, const char **attrs) +{ + if (ignore) + return; + + if (std::strcmp(name, "entry") == 0) { + assert(! curr_entry); + curr_entry = new entry_t; + } + else if (std::strcmp(name, "transaction") == 0) { + assert(curr_entry); + curr_entry->add_transaction(new transaction_t); + } + else if (std::strcmp(name, "commodity") == 0) { + if (std::string(attrs[0]) == "flags") + comm_flags = attrs[1]; + } + else if (std::strcmp(name, "total") == 0) { + ignore = true; + } +} + +static void endElement(void *userData, const char *name) +{ + if (ignore) { + if (std::strcmp(name, "total") == 0) + ignore = false; + return; + } + + if (std::strcmp(name, "entry") == 0) { + assert(curr_entry); + if (curr_journal->add_entry(curr_entry)) { + count++; + } else { + account_t * acct = curr_journal->find_account("<Unknown>"); + curr_entry->add_transaction(new transaction_t(acct)); + if (curr_journal->add_entry(curr_entry)) { + count++; + } else { + delete curr_entry; + have_error = "Entry does not balance"; + } + } + curr_entry = NULL; + } + else if (std::strcmp(name, "en:date") == 0) { + quick_parse_date(data.c_str(), &curr_entry->date); + } + else if (std::strcmp(name, "en:cleared") == 0) { + curr_entry->state = entry_t::CLEARED; + } + else if (std::strcmp(name, "en:pending") == 0) { + curr_entry->state = entry_t::PENDING; + } + else if (std::strcmp(name, "en:code") == 0) { + curr_entry->code = data; + } + else if (std::strcmp(name, "en:payee") == 0) { + curr_entry->payee = data; + } + else if (std::strcmp(name, "tr:account") == 0) { + curr_entry->transactions.back()->account = curr_journal->find_account(data); + } + else if (std::strcmp(name, "commodity") == 0) { + assert(! curr_comm); + curr_comm = commodity_t::find_commodity(data, true); + curr_comm->flags |= COMMODITY_STYLE_SUFFIXED; + if (! comm_flags.empty()) + for (std::string::size_type i = 0, l = comm_flags.length(); i < l; i++) + switch (comm_flags[i]) { + case 'P': curr_comm->flags &= ~COMMODITY_STYLE_SUFFIXED; break; + case 'S': curr_comm->flags |= COMMODITY_STYLE_SEPARATED; break; + case 'T': curr_comm->flags |= COMMODITY_STYLE_THOUSANDS; break; + case 'E': curr_comm->flags |= COMMODITY_STYLE_EUROPEAN; break; + } + } + else if (std::strcmp(name, "quantity") == 0) { + curr_entry->transactions.back()->amount.parse(data); + if (curr_comm) { + std::string::size_type i = data.find('.'); + if (i != std::string::npos) { + int precision = data.length() - i - 1; + if (precision > curr_comm->precision) + curr_comm->precision = precision; + } + curr_entry->transactions.back()->amount.set_commodity(*curr_comm); + curr_comm = NULL; + } + } + else if (std::strcmp(name, "tr:amount") == 0) { + curr_comm = NULL; + } +} + +static void dataHandler(void *userData, const char *s, int len) +{ + if (! ignore) + data = std::string(s, len); +} + +bool xml_parser_t::test(std::istream& in) const +{ + char buf[256]; + + in.getline(buf, 255); + if (std::strncmp(buf, "<?xml version=\"1.0\"?>", 21) != 0) { + in.seekg(0, std::ios::beg); + return false; + } + + in.getline(buf, 255); + if (! std::strstr(buf, "<ledger>")) { + in.seekg(0, std::ios::beg); + return false; + } + + in.seekg(0, std::ios::beg); + return true; +} + +unsigned int xml_parser_t::parse(std::istream& in, + journal_t * journal, + account_t * master, + const std::string * original_file) +{ + char buf[BUFSIZ]; + + count = 0; + curr_journal = journal; + curr_entry = NULL; + curr_comm = NULL; + ignore = false; + + XML_Parser parser = XML_ParserCreate(NULL); + current_parser = parser; + + XML_SetElementHandler(parser, startElement, endElement); + XML_SetCharacterDataHandler(parser, dataHandler); + + while (! in.eof()) { + in.getline(buf, BUFSIZ - 1); + bool result; + try { + result = XML_Parse(parser, buf, std::strlen(buf), in.eof()); + } + catch (const std::exception& err) { + unsigned long line = XML_GetCurrentLineNumber(parser); + XML_ParserFree(parser); + throw parse_error(original_file ? *original_file : "<xml>", line, + err.what()); + } + + if (! have_error.empty()) { + unsigned long line = XML_GetCurrentLineNumber(parser); + parse_error err(original_file ? *original_file : "<xml>", line, + have_error); + std::cerr << "Error: " << err.what() << std::endl; + } + + if (! result) { + unsigned long line = XML_GetCurrentLineNumber(parser); + const char * err = XML_ErrorString(XML_GetErrorCode(parser)); + XML_ParserFree(parser); + throw parse_error(original_file ? *original_file : "<xml>", line, err); + } + } + + XML_ParserFree(parser); + + return count; +} + +} // namespace ledger + +#ifdef USE_BOOST_PYTHON + +#include <boost/python.hpp> + +using namespace boost::python; +using namespace ledger; + +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(xml_parse_overloads, + xml_parser_t::parse, 2, 4) + +void export_xml() { + class_< xml_parser_t, bases<parser_t> > ("XmlParser") + .def("test", &xml_parser_t::test) + .def("parse", &xml_parser_t::parse, xml_parse_overloads()) + ; +} + +#endif // USE_BOOST_PYTHON @@ -0,0 +1,21 @@ +#ifndef _XML_H +#define _XML_H + +#include "parser.h" + +namespace ledger { + +class xml_parser_t : public parser_t +{ + public: + virtual bool test(std::istream& in) const; + + virtual unsigned int parse(std::istream& in, + journal_t * journal, + account_t * master = NULL, + const std::string * original_file = NULL); +}; + +} // namespace ledger + +#endif // _XML_H |