diff options
-rw-r--r-- | LICENSE | 29 | ||||
-rw-r--r-- | Makefile | 50 | ||||
-rw-r--r-- | Makefile.am | 106 | ||||
-rw-r--r-- | NEWS | 129 | ||||
-rw-r--r-- | NOTES | 22 | ||||
-rw-r--r-- | README | 1242 | ||||
-rwxr-xr-x | acprep | 37 | ||||
-rw-r--r-- | amount.cc | 1332 | ||||
-rw-r--r-- | amount.h | 417 | ||||
-rw-r--r-- | autoxact.cc | 30 | ||||
-rw-r--r-- | autoxact.h | 102 | ||||
-rw-r--r-- | balance.cc | 277 | ||||
-rw-r--r-- | balance.h | 869 | ||||
-rw-r--r-- | binary.cc | 631 | ||||
-rw-r--r-- | binary.h | 28 | ||||
-rw-r--r-- | config.cc | 685 | ||||
-rw-r--r-- | config.h | 94 | ||||
-rw-r--r-- | configure.in | 98 | ||||
-rw-r--r-- | datetime.cc | 389 | ||||
-rw-r--r-- | datetime.h | 59 | ||||
-rw-r--r-- | debug.cc | 112 | ||||
-rw-r--r-- | debug.h | 119 | ||||
-rw-r--r-- | derive.cc | 147 | ||||
-rw-r--r-- | derive.h | 14 | ||||
-rw-r--r-- | error.h | 65 | ||||
-rw-r--r-- | format.cc | 606 | ||||
-rw-r--r-- | format.h | 182 | ||||
-rw-r--r-- | gnucash.cc | 205 | ||||
-rw-r--r-- | gnucash.h | 21 | ||||
-rw-r--r-- | journal.cc | 750 | ||||
-rw-r--r-- | journal.h | 282 | ||||
-rw-r--r-- | ledger.cc | 674 | ||||
-rw-r--r-- | ledger.el | 54 | ||||
-rw-r--r-- | ledger.h | 371 | ||||
-rw-r--r-- | ledger.texi | 2078 | ||||
-rw-r--r-- | main.cc | 361 | ||||
-rwxr-xr-x | main.py | 398 | ||||
-rw-r--r-- | option.cc | 257 | ||||
-rw-r--r-- | option.h | 44 | ||||
-rw-r--r-- | parse.cc | 582 | ||||
-rw-r--r-- | parser.cc | 117 | ||||
-rw-r--r-- | parser.h | 40 | ||||
-rw-r--r-- | pyledger.cc | 9 | ||||
-rw-r--r-- | pysample.dat | 36 | ||||
-rw-r--r-- | python.cc | 81 | ||||
-rw-r--r-- | python.h | 58 | ||||
-rw-r--r-- | qif.cc | 201 | ||||
-rw-r--r-- | qif.h | 21 | ||||
-rw-r--r-- | quotes.cc | 74 | ||||
-rw-r--r-- | quotes.h | 30 | ||||
-rw-r--r-- | reports.cc | 1225 | ||||
-rw-r--r--[-rwxr-xr-x] | scripts/bal | 18 | ||||
-rwxr-xr-x | scripts/confirm.py | 40 | ||||
-rw-r--r--[-rwxr-xr-x] | scripts/entry | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | scripts/getquote | 6 | ||||
-rw-r--r--[-rwxr-xr-x] | scripts/mean | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | scripts/profit | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | scripts/reg | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | scripts/report | 8 | ||||
-rw-r--r--[-rwxr-xr-x] | scripts/spending | 0 | ||||
-rwxr-xr-x | scripts/test | 14 | ||||
-rw-r--r--[-rwxr-xr-x] | scripts/worth | 0 | ||||
-rwxr-xr-x | setup.py | 22 | ||||
-rw-r--r-- | textual.cc | 550 | ||||
-rw-r--r-- | textual.h | 23 | ||||
-rw-r--r-- | timing.h | 54 | ||||
-rw-r--r-- | util.h | 37 | ||||
-rw-r--r-- | valexpr.cc | 1051 | ||||
-rw-r--r-- | valexpr.h | 186 | ||||
-rw-r--r-- | value.cc | 787 | ||||
-rw-r--r-- | value.h | 383 | ||||
-rw-r--r-- | walk.cc | 662 | ||||
-rw-r--r-- | walk.h | 485 |
73 files changed, 5255 insertions, 14911 deletions
diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 9f97f8cb..00000000 --- a/LICENSE +++ /dev/null @@ -1,29 +0,0 @@ -Copyright (c) 2003-2004, New Artisans LLC -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 new file mode 100644 index 00000000..6ff88b8b --- /dev/null +++ b/Makefile @@ -0,0 +1,50 @@ +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/usr/local/include +LIBS = -L/usr/local/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 deleted file mode 100644 index 019fe80b..00000000 --- a/Makefile.am +++ /dev/null @@ -1,106 +0,0 @@ -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 \ - option.cc \ - parser.cc \ - qif.cc \ - quotes.cc \ - textual.cc \ - valexpr.cc \ - value.cc \ - walk.cc -if READ_GNUCASH -libledger_la_SOURCES += gnucash.cc -endif -if HAVE_BOOST_PYTHON -libledger_la_CXXFLAGS += -DUSE_BOOST_PYTHON=1 -libledger_la_SOURCES += python.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 = -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 \ - option.h \ - parser.h \ - python.h \ - qif.h \ - quotes.h \ - textual.h \ - timing.h \ - util.h \ - valexpr.h \ - value.h \ - walk.h - -###################################################################### - -bin_PROGRAMS = ledger -ledger_CXXFLAGS = $(global_CXXFLAGS) -ledger_SOURCES = main.cc -if HAVE_BOOST_PYTHON -ledger_LDADD = $(LIBOBJS) libledger.la -lboost_python -lpython$(PYTHON_VERSION) -else -ledger_LDADD = $(LIBOBJS) libledger.la -endif - -info_TEXINFOS = ledger.texi - -###################################################################### - -if HAVE_BOOST_PYTHON - -noinst_PROGRAMS = ledger.so - -if READ_GNUCASH -READ_GNUCASH_VALUE = true -else -READ_GNUCASH_VALUE = false -endif - -ledger.so: python.cc libledger.la - CFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS) -L. -L.libs" \ - READ_GNUCASH="$(READ_GNUCASH_VALUE)" \ - python setup.py build --build-lib=. - -install-exec-hook: - CFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS) -L. -L.libs" \ - READ_GNUCASH="$(READ_GNUCASH_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 \ - .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,126 +1,13 @@ - Ledger NEWS +1.7 -* 2.0 + 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. -- The code was completely rewritten. +1.6 -- The most significant feature added are "value expressions". They - are used in several places to indicate what to display, sorting - order, output format, etc. Logic and math operators are supported, - as well as a few functions. See manual. - -- If the environment variable LEDGER (or LEDGER_FILE) is used, a - binary cache of that ledger is now kept in ~/.ledger (or - LEDGER_CACHE), to speed up later queries of the same data. This - happens only when "-f" or "--file" is not used. - -- New options: - - "-o FILE" outputs data to FILE. If "-", output goes to stdout. - - -j and -J options replace the previous -G (gnuplot) option. -j - reports the values column in a way gnuplot can consume, and -J, the - totals column. An example is in scripts/report. - - "-y DATEFMT" changes the date format used for register 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 INTERVAL" allows for more flexible interval reporting. The - sublanguage used will likely mature over time, but for now it - supports expression like: - - monthly - every week - every 3 quarters - weekly from 12/20 - monthly in 2003 - - -O shows base values (this is the default, and old, behavior) - -B shows basis cost of commodities - -V shows the market value of commodities - -G reports net gain/loss - -A reports average value (arithmetic mean) - -D reports deviation from the average - -X reports the trend (average rate of change) - -Z reports the trend, with older values affecting the trend less - -- Regexps given directly after the command name now apply to account - names only. To search 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: The above command is identical (and internally converted) to: - - ledger -l "/expenses/|//john/" register - -- To include entries from a file into a specific account, use: - - !account ACCOUNT - !include FILE - !end - - Entries specified within an !account block will affect only that - account. - -- Register reports now show only the account affected by default. Use - "-r" to see "related accounts" -- the account the transfer came from - or went to. (This was the old behavior in 1.x, but leads to - confusing reports when viewing reports within subaccounts.) The - "-r" option also works with balance reports, where it shows all - amounts related to your query. - -- Automated transactions now use value expressions as their predicate. - The new syntax is: - - = VALUE-EXPR - TRANSACTIONS... - - Only one VALUE-EXPR is supported, compared to the multiple account - regexps supported before. However, since value expression allow for - logic chaining, there is no loss in functionality. Also, by using a - VALUE-EXPR as a predicate, matching may be much more comprehensive - and selective. - -- The use of "+" and "-" in ledger files (to specify permanent - regexps) has been removed. - -- The "-from" argument is no longer needed with the "entry" command. - It can simply be removed, with no other changes. - -- -l now takes a value expression as the "calculation predicate". - To mimic the old behavior of "-l \$100", use: -d "T&AT>{\$100}" - -- The -S flag now takes a value expression as the sorting criteria. - To sort on the amount, use -S T (balance) or -S t (register). - -- If Boost.Python is installed, ledger supports full, 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. - - Note that this feature has yet to mature, and so is not being - supported in this release. It is mostly functional, however, and - those with a desire to tinker are welcome to play with it. - -* 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 README for more details on the new option switches. - -* 1.6 - -- Can now parse timeclock files. These are simple timelogs that track + 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", @@ -129,4 +16,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 NEWS file. :) @@ -0,0 +1,22 @@ +book + account + commodity * + +account + account * + +entry + transaction * + +transaction + account + amount + amount (price per item) + +amount + commodity * + +commodity + history * + +history @@ -1,42 +1,1226 @@ -Welcome to Ledger, a command-line accounting program. +#comment -*-muse-*- +#title Ledger: Command-Line Accounting -Quick start -=========== +<contents> -To build Ledger, you will first need these two libraries installed: +* Introduction - gmp GNU multi-precision library - pcre Perl regular expression library +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. -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): +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. - xmlparse - xmltok +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. -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: +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. - ./configure CPPFLAGS=-I<INCLUDE-PATH> LDFLAGS=-L<LIBRARY-PATH> +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. -If you need to specify multiple include or library paths, then 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<PATH1> -I<PATH2>" LDFLAGS="-L<PATH1> -L<PATH2>" +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: -Once configure is done running, just type: +<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> - make install +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... -Building Ledger as a Python Module -================================== +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. -If you have Python 2.2 or higher installed, and Boost.Python, then -Ledger will also be built as a Python module. This means you can -interact with your Ledger data from Python, making it easier to write -very custom reporting code. See the developer documentation for -information on Ledger's interfaces, and for some example reports -written in Python. +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] + +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. + +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 + Payable:Pacific Bell $-200.00 + Equity:Opening Balances + +9/29 Checking + Accounts:Checking $100.00 + Equity:Opening Balances + +9/29 Pacific Bell + Payable:Pacific Bell $23.00 + Accounts:Checking +</example> + +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. diff --git a/acprep b/acprep deleted file mode 100755 index 36193dea..00000000 --- a/acprep +++ /dev/null @@ -1,37 +0,0 @@ -#!/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,1119 +1,621 @@ -#include "amount.h" -#include "util.h" +#include "ledger.h" #include <sstream> -#include <deque> - -#include "gmp.h" +#include <cstdio> +#include <gmp.h> // GNU multi-precision library namespace ledger { -#define BIGINT_BULK_ALLOC 0x0001 +#define MAX_PRECISION 10 // must be 2 or higher -class amount_t::bigint_t { - public: - mpz_t val; - unsigned short prec; - unsigned short flags; - unsigned int ref; - unsigned int index; +////////////////////////////////////////////////////////////////////// +// +// 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"); +// - 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); - } -}; +class gmp_amount : public amount +{ + bool priced; -unsigned int sizeof_bigint_t() { - return sizeof(amount_t::bigint_t); -} + mpz_t price; + commodity * price_comm; -#define MPZ(x) ((x)->val) + mpz_t quantity; + commodity * quantity_comm; -static mpz_t temp; -static mpz_t divisor; -static amount_t::bigint_t true_value; + gmp_amount(const gmp_amount& other) {} + gmp_amount& operator=(const gmp_amount& other) { return *this; } -commodity_t::updater_t * commodity_t::updater = NULL; -commodities_map commodity_t::commodities; -commodity_t * commodity_t::null_commodity; + public: + gmp_amount() : priced(false), price_comm(NULL), quantity_comm(NULL) { + mpz_init(price); + mpz_init(quantity); + } -static struct _init_amounts { - _init_amounts(); - ~_init_amounts(); -} _init_obj; + virtual ~gmp_amount() { + mpz_clear(price); + mpz_clear(quantity); + } -_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::~_init_amounts() -{ - mpz_clear(divisor); - mpz_clear(temp); + virtual bool has_price() const { + return priced; + } + virtual amount * per_item_price() const; + + virtual bool is_zero() const; + virtual bool is_negative() const; + virtual int compare(const amount * other) const; - if (commodity_t::updater) { - delete commodity_t::updater; - commodity_t::updater = NULL; + virtual void negate() { + mpz_ui_sub(quantity, 0, quantity); } + virtual void credit(const amount * other); - for (commodities_map::iterator i = commodity_t::commodities.begin(); - i != commodity_t::commodities.end(); - i++) - delete (*i).second; + virtual void parse(const std::string& num); + virtual const std::string as_str(bool full_prec) const; - commodity_t::commodities.clear(); + friend amount * create_amount(const std::string& value, + const amount * cost); +}; - true_value.ref--; +amount * create_amount(const std::string& value, const amount * cost) +{ + gmp_amount * a = new gmp_amount(); + a->parse(value); + if (cost) + a->set_value(cost); + return a; } -static void mpz_round(mpz_t out, mpz_t value, int value_prec, int round_prec) +static void round(mpz_t out, const mpz_t val, int prec) { - // 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); - + mpz_t divisor; mpz_t quotient; mpz_t remainder; + mpz_init(divisor); mpz_init(quotient); mpz_init(remainder); - mpz_ui_pow_ui(divisor, 10, value_prec - round_prec); - mpz_tdiv_qr(quotient, remainder, value, divisor); - mpz_divexact_ui(divisor, divisor, 10); + 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_mul_ui(divisor, divisor, 5); if (mpz_sgn(remainder) < 0) { - mpz_neg(divisor, divisor); + mpz_ui_sub(divisor, 0, divisor); + if (mpz_cmp(remainder, divisor) < 0) { - mpz_ui_pow_ui(divisor, 10, value_prec - round_prec); + mpz_ui_pow_ui(divisor, 10, MAX_PRECISION - prec); mpz_add(remainder, divisor, remainder); mpz_ui_sub(remainder, 0, remainder); - mpz_add(out, value, remainder); + mpz_add(out, val, remainder); } else { - mpz_sub(out, value, remainder); + mpz_sub(out, val, remainder); } } else { if (mpz_cmp(remainder, divisor) >= 0) { - mpz_ui_pow_ui(divisor, 10, value_prec - round_prec); + mpz_ui_pow_ui(divisor, 10, MAX_PRECISION - prec); mpz_sub(remainder, divisor, remainder); - mpz_add(out, value, remainder); + mpz_add(out, val, remainder); } else { - mpz_sub(out, value, remainder); + mpz_sub(out, val, 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); } -amount_t::amount_t(const bool value) +static void multiply(mpz_t out, const mpz_t l, const mpz_t r) { - if (value) { - quantity = &true_value; - quantity->ref++; - } else { - quantity = NULL; - } - commodity_ = NULL; -} + mpz_t divisor; -amount_t::amount_t(const int value) -{ - if (value != 0) { - quantity = new bigint_t; - mpz_set_si(MPZ(quantity), value); - } else { - quantity = NULL; - } - commodity_ = NULL; -} + mpz_init(divisor); -amount_t::amount_t(const unsigned int value) -{ - if (value != 0) { - quantity = new bigint_t; - mpz_set_ui(MPZ(quantity), value); - } else { - quantity = NULL; - } - commodity_ = NULL; -} + mpz_mul(out, l, r); -amount_t::amount_t(const double value) -{ - if (value != 0.0) { - quantity = new bigint_t; - mpz_set_d(MPZ(quantity), value); - // jww (2004-08-20): How do I calculate this? - } 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); -void amount_t::_release() -{ - if (--quantity->ref == 0) { - if (! (quantity->flags & BIGINT_BULK_ALLOC)) - delete quantity; - else - quantity->~bigint_t(); - } -} + // after multiplying, truncate to the correct precision + mpz_ui_pow_ui(divisor, 10, MAX_PRECISION); + mpz_tdiv_q(out, out, divisor); -void amount_t::_init() -{ - if (! quantity) { - quantity = new bigint_t; - } - else if (quantity->ref > 1) { - _release(); - quantity = new bigint_t; - } + mpz_clear(divisor); } -void amount_t::_dup() +amount * gmp_amount::copy() const { - if (quantity->ref > 1) { - bigint_t * q = new bigint_t(*quantity); - _release(); - quantity = q; - } -} + gmp_amount * new_amt = new gmp_amount(); -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_; -} + mpz_set(new_amt->quantity, quantity); + new_amt->quantity_comm = quantity_comm; -amount_t& amount_t::operator=(const std::string& value) -{ - std::istringstream str(value); - parse(str); - return *this; + return new_amt; } -amount_t& amount_t::operator=(const char * value) +amount * gmp_amount::per_item_price() const { - std::string valstr(value); - std::istringstream str(valstr); - parse(str); - return *this; -} + if (! priced) + return NULL; -// 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; -} + gmp_amount * new_amt = new gmp_amount(); -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; -} + mpz_set(new_amt->quantity, price); + new_amt->quantity_comm = price_comm; -amount_t& amount_t::operator=(const int value) -{ - if (value == 0) { - if (quantity) - _clear(); - } else { - commodity_ = NULL; - _init(); - mpz_set_si(MPZ(quantity), value); - } - return *this; + return new_amt; } -amount_t& amount_t::operator=(const unsigned int value) +amount * gmp_amount::value(const amount * pr) const { - if (value == 0) { - if (quantity) - _clear(); - } else { - commodity_ = NULL; - _init(); - mpz_set_ui(MPZ(quantity), value); - } - return *this; -} - -amount_t& amount_t::operator=(const double value) -{ - if (value == 0.0) { - if (quantity) - _clear(); - } else { - commodity_ = NULL; - _init(); - // jww (2004-08-20): How do I calculate precision? - mpz_set_d(MPZ(quantity), value); - } - return *this; -} + if (pr) { + const gmp_amount * p = dynamic_cast<const gmp_amount *>(pr); + assert(p); + gmp_amount * new_amt = new gmp_amount(); -void amount_t::_resize(unsigned int prec) -{ - assert(prec < 256); + multiply(new_amt->quantity, quantity, p->quantity); - if (! quantity || prec == quantity->prec) - return; + // 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; - _dup(); + if (new_amt->quantity_comm && + new_amt->quantity_comm->precision < MAX_PRECISION) + round(new_amt->quantity, new_amt->quantity, + new_amt->quantity_comm->precision); - 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); + return new_amt; } - - quantity->prec = prec; -} - - -amount_t& amount_t::operator+=(const amount_t& amt) -{ - if (! amt.quantity) - return *this; - - if (! quantity) { - _copy(amt); - return *this; + else if (! priced) { + return copy(); } + else { + gmp_amount * new_amt = new gmp_amount(); - _dup(); + multiply(new_amt->quantity, quantity, price); - if (commodity_ != amt.commodity_) - throw amount_error("Adding amounts with different commodities"); + 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 (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 new_amt; } - - return *this; } -amount_t& amount_t::operator-=(const amount_t& amt) +amount * gmp_amount::street(std::time_t * when, + bool use_history, bool download) const { - if (! amt.quantity) - return *this; - - if (! quantity) { - quantity = new bigint_t(*amt.quantity); - commodity_ = amt.commodity_; - mpz_neg(MPZ(quantity), MPZ(quantity)); - return *this; - } - - _dup(); - - if (commodity_ != amt.commodity_) - throw amount_error("Subtracting amounts with different commodities"); + static std::time_t now = std::time(NULL); + if (! when) + when = &now; - 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 * amt = copy(); - return *this; -} + if (! amt->commdty()) + return amt; -amount_t& amount_t::operator*=(const amount_t& amt) -{ - if (! amt.quantity || ! quantity) - return *this; + int max = 10; + while (--max >= 0) { + amount * price = amt->commdty()->price(when, use_history, download); + if (! price) + break; - _dup(); + amount * old = amt; + amt = amt->value(price); - mpz_mul(MPZ(quantity), MPZ(quantity), MPZ(amt.quantity)); - quantity->prec += amt.quantity->prec; + if (amt->commdty() == old->commdty()) { + delete old; + break; + } - 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; + delete old; } - return *this; + return amt; } -amount_t& amount_t::operator/=(const amount_t& amt) +void gmp_amount::set_value(const amount * val) { - if (! quantity) - return *this; - if (! amt.quantity) - throw amount_error("Divide by zero"); - - _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; - } + assert(! priced); // don't specify the pricing twice! - return *this; -} + const gmp_amount * v = dynamic_cast<const gmp_amount *>(val); + assert(v); -// unary negation -void amount_t::negate() -{ - if (quantity) { - _dup(); - mpz_neg(MPZ(quantity), MPZ(quantity)); - } -} + mpz_t quotient; + mpz_t remainder; + mpz_t addend; -int amount_t::sign() const -{ - return quantity ? mpz_sgn(MPZ(quantity)) : 0; -} + mpz_init(quotient); + mpz_init(remainder); + mpz_init(addend); -// comparisons between amounts -#define AMOUNT_CMP_AMOUNT(OP) \ -bool amount_t::operator OP(const amount_t& amt) const \ -{ \ - if (! quantity) \ - return amt > 0; \ - if (! amt.quantity) \ - return *this < 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_ui_pow_ui(addend, 10, MAX_PRECISION); -AMOUNT_CMP_AMOUNT(<) -AMOUNT_CMP_AMOUNT(<=) -AMOUNT_CMP_AMOUNT(>) -AMOUNT_CMP_AMOUNT(>=) -AMOUNT_CMP_AMOUNT(==) + 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_t::operator bool() const -{ - if (! quantity) - return false; + priced = true; + mpz_set(price, quotient); + price_comm = v->quantity_comm; - 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; - } + mpz_clear(quotient); + mpz_clear(remainder); + mpz_clear(addend); } -amount_t amount_t::value(const std::time_t moment) const +bool gmp_amount::is_zero() const { - if (quantity && ! (commodity().flags & COMMODITY_STYLE_NOMARKET)) - if (amount_t amt = commodity().value(moment)) - return (amt * *this).round(commodity().precision); - - return *this; + 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; } -amount_t amount_t::round(unsigned int prec) const +bool gmp_amount::is_negative() 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; + return mpz_sgn(quantity) < 0; } -std::ostream& operator<<(std::ostream& _out, const amount_t& amt) +int gmp_amount::compare(const amount * other) const { - if (! amt.quantity) - return _out; - - std::ostringstream out; + 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; +} +static std::string amount_to_str(const commodity * comm, const mpz_t val, + bool full_precision) +{ + mpz_t temp; 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); - bool negative = false; + if (comm == NULL) + full_precision = true; - // Ensure the value is rounded to the commodity's precision before - // outputting it. NOTE: `rquotient' is used here as a temp variable! + if (! full_precision && comm->precision < MAX_PRECISION) + round(temp, temp, comm->precision); - commodity_t& commodity(amt.commodity()); - unsigned short precision; + mpz_ui_pow_ui(divisor, 10, MAX_PRECISION); + mpz_tdiv_qr(quotient, remainder, temp, divisor); - if (commodity == *commodity_t::null_commodity) { - mpz_ui_pow_ui(divisor, 10, amt.quantity->prec); - mpz_tdiv_qr(quotient, remainder, MPZ(amt.quantity), divisor); - precision = amt.quantity->prec; - } - else if (commodity.precision < amt.quantity->prec) { - mpz_round(rquotient, MPZ(amt.quantity), amt.quantity->prec, - commodity.precision); - mpz_ui_pow_ui(divisor, 10, commodity.precision); - mpz_tdiv_qr(quotient, remainder, rquotient, divisor); - precision = commodity.precision; - } - else if (commodity.precision > amt.quantity->prec) { - mpz_ui_pow_ui(divisor, 10, commodity.precision - amt.quantity->prec); - mpz_mul(rquotient, MPZ(amt.quantity), divisor); - mpz_ui_pow_ui(divisor, 10, commodity.precision); - mpz_tdiv_qr(quotient, remainder, rquotient, divisor); - precision = commodity.precision; - } - else if (amt.quantity->prec) { - mpz_ui_pow_ui(divisor, 10, amt.quantity->prec); - mpz_tdiv_qr(quotient, remainder, MPZ(amt.quantity), divisor); - precision = amt.quantity->prec; - } - else { - mpz_set(quotient, MPZ(amt.quantity)); - mpz_set_ui(remainder, 0); - precision = 0; - } - - if (mpz_sgn(quotient) < 0 || mpz_sgn(remainder) < 0) { + if (mpz_sgn(quotient) < 0 || mpz_sgn(remainder) < 0) negative = true; + mpz_abs(quotient, quotient); + mpz_abs(remainder, remainder); - mpz_abs(quotient, quotient); - mpz_abs(remainder, remainder); + if (full_precision || comm->precision == MAX_PRECISION) { + mpz_set(rquotient, remainder); + } else { + assert(MAX_PRECISION - comm->precision > 0); + mpz_ui_pow_ui(divisor, 10, MAX_PRECISION - comm->precision); + mpz_tdiv_qr(rquotient, remainder, remainder, divisor); } - mpz_set(rquotient, remainder); - if (! (commodity.flags & COMMODITY_STYLE_SUFFIXED)) { - if (commodity.quote) - out << "\"" << commodity.symbol << "\""; - else - out << commodity.symbol; - if (commodity.flags & COMMODITY_STYLE_SEPARATED) - out << " "; + std::ostringstream s; + + if (comm && comm->prefix) { + s << comm->symbol; + if (comm->separate) + s << " "; } if (negative) - out << "-"; + s << "-"; - if (mpz_sgn(quotient) == 0) { - out << '0'; - } - else if (! (commodity.flags & COMMODITY_STYLE_THOUSANDS)) { - char * p = mpz_get_str(NULL, 10, quotient); - out << p; - std::free(p); - } + if (mpz_sgn(quotient) == 0) + s << '0'; + else if (! comm || ! comm->thousands) + s << quotient; else { - std::deque<std::string> strs; - char buf[4]; + bool printed = false; - for (int powers = 0; true; powers += 3) { - if (powers > 0) { - 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); - } + // 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); - bool printed = false; + if (mpz_sgn(temp) == 0) + continue; + + mpz_ui_pow_ui(divisor, 10, 3); + mpz_tdiv_r(temp, temp, divisor); - for (std::deque<std::string>::reverse_iterator i = strs.rbegin(); - i != strs.rend(); - i++) { if (printed) { - out << (commodity.flags & COMMODITY_STYLE_EUROPEAN ? '.' : ','); - out.width(3); - out.fill('0'); + s.width(3); + s.fill('0'); } - out << *i; + s << temp; - printed = true; + if (powers > 0) { + s << (comm && comm->european ? '.' : ','); + printed = true; + } } } - if (precision) { - out << ((commodity.flags & COMMODITY_STYLE_EUROPEAN) ? ',' : '.'); + s << (comm && comm->european ? ',' : '.'); + + 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); + + int width = std::strlen(buf); + char * p = buf + (width - 1); - out.width(precision); - out.fill('0'); + width = MAX_PRECISION - width; - char * p = mpz_get_str(NULL, 10, rquotient); - out << p; - std::free(p); + if (comm) { + while (p >= buf && *p == '0' && + (p - buf) >= (comm->precision - width)) + p--; + *(p + 1) = '\0'; + } + + s.width(width + std::strlen(buf)); + s.fill('0'); + s << buf; } - if (commodity.flags & COMMODITY_STYLE_SUFFIXED) { - if (commodity.flags & COMMODITY_STYLE_SEPARATED) - out << " "; - if (commodity.quote) - out << "\"" << commodity.symbol << "\""; - else - out << commodity.symbol; + if (comm && ! comm->prefix) { + if (comm->separate) + s << " "; + s << comm->symbol; } + mpz_clear(temp); mpz_clear(quotient); mpz_clear(rquotient); mpz_clear(remainder); + mpz_clear(divisor); - // 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; + return s.str(); } -void parse_quantity(std::istream& in, std::string& value) +const std::string gmp_amount::as_str(bool full_prec) const { - char buf[256]; - char c = peek_next_nonws(in); - READ_INTO(in, buf, 255, c, - std::isdigit(c) || c == '-' || c == '.' || c == ','); - value = buf; -} + std::ostringstream s; -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 != '.'); + s << amount_to_str(quantity_comm, quantity, full_prec); + + if (priced) { + s << " @ "; + s << amount_to_str(price_comm, price, full_prec); } - symbol = buf; + return s.str(); } -void amount_t::parse(std::istream& in) +static void parse_number(mpz_t out, const std::string& number, + commodity * comm) { - // The possible syntax for an amount is: - // - // [-]NUM[ ]SYM [@ AMOUNT] - // SYM[ ][-]NUM [@ AMOUNT] + const char * num = number.c_str(); - std::string symbol; - std::string quant; - unsigned int flags = COMMODITY_STYLE_DEFAULTS;; + if (char * p = std::strchr(num, '/')) { + mpz_t numer; + mpz_t val; - char c = peek_next_nonws(in); - if (std::isdigit(c) || c == '.' || c == '-') { - parse_quantity(in, quant); + // 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 n; - if (! in.eof() && ((n = in.peek()) != '\n')) { - if (std::isspace(n)) - flags |= COMMODITY_STYLE_SEPARATED; - - parse_commodity(in, symbol); - - flags |= COMMODITY_STYLE_SUFFIXED; - } - } else { - parse_commodity(in, symbol); - - if (std::isspace(in.peek())) - flags |= COMMODITY_STYLE_SEPARATED; - - parse_quantity(in, quant); - } - - if (quant.empty()) - throw amount_error("No quantity specified for amount"); + std::string numer_str(num, p - num); + mpz_init_set_str(numer, numer_str.c_str(), 10); + mpz_init(val); - _init(); + int missing = MAX_PRECISION - (std::strlen(++p) - 1); + assert(missing > 0); + mpz_ui_pow_ui(val, 10, missing); - std::string::size_type last_comma = quant.rfind(','); - std::string::size_type last_period = quant.rfind('.'); + mpz_mul(out, numer, val); - if (last_comma != std::string::npos && last_period != std::string::npos) { - flags |= COMMODITY_STYLE_THOUSANDS; - if (last_comma > last_period) { - 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) { - 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; + mpz_clear(numer); + mpz_clear(val); } else { - quantity->prec = 0; - } + static char buf[256]; - // Create the commodity if has not already been seen, and update the - // precision if something greater was used for the quantity. + // The number is specified as the user desires, with the commodity + // telling us how to parse it. - commodity_ = commodity_t::find_commodity(symbol, true); - commodity_->flags |= flags; - if (quantity->prec > commodity_->precision) - commodity_->precision = quantity->prec; + std::memset(buf, '0', 255); + std::strncpy(buf, num, std::strlen(num)); - // Now we have the final number. Remove commas and periods, if - // necessary. + if (comm && comm->thousands) + while (char * t = std::strchr(buf, comm->european ? '.' : ',')) + do { *t = *(t + 1); } while (*(t++ + 1)); - if (last_comma != std::string::npos || last_period != std::string::npos) { - int len = quant.length(); - char * buf = new char[len + 1]; + char * t = std::strchr(buf, (comm && comm->european) ? ',' : '.'); + if (! t) + t = buf + std::strlen(num); - const char * p = quant.c_str(); - char * t = buf; - - while (*p) { - if (*p == ',' || *p == '.') - p++; - *t++ = *p++; + for (int prec = 0; prec < MAX_PRECISION; prec++) { + *t = *(t + 1); + t++; } *t = '\0'; - mpz_set_str(MPZ(quantity), buf, 10); - - delete[] buf; - } else { - mpz_set_str(MPZ(quantity), quant.c_str(), 10); + mpz_set_str(out, buf, 10); } } -void amount_t::parse(const std::string& str) +static commodity * parse_amount(mpz_t out, const char * num, + int matched, int * ovector, int base) { - std::istringstream stream(str); - parse(stream); -} + static char buf[256]; + bool saw_commodity = false; + bool prefix = false; + bool separate = true; + bool thousands = true; + bool european = false; -char * bigints; -char * bigints_next; -unsigned int bigints_index; -unsigned int bigints_count; + std::string symbol; + int precision, result; -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); - - quantity = (bigint_t *) (bigints + (index - 1) * sizeof(bigint_t)); - quantity->ref++; + 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; } -} -static char buf[4096]; - -void amount_t::read_quantity(std::istream& in) -{ - static std::deque<bigint_t *> _bigints; + // 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); - char byte; - in.read(&byte, sizeof(byte)); - - if (byte == 0) { - quantity = NULL; - } - else if (byte == 1) { - quantity = new bigint_t; - _bigints.push_back(quantity); - - 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 { - unsigned int index; - in.read((char *)&index, sizeof(index)); - quantity = _bigints[index - 1]; - quantity->ref++; + // Where "thousands" markers used? Is it a european number? + if (char * p = std::strrchr(buf, ',')) { + if (std::strchr(p, '.')) + thousands = true; + else + european = true; } -} -void amount_t::write_quantity(std::ostream& out) const -{ - char byte; + // 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; - if (! quantity) { - byte = 0; - out.write(&byte, sizeof(byte)); - return; + // 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; } - if (quantity->index == 0) { - quantity->index = ++bigints_index; - bigints_count++; + parse_number(out, value_str.c_str(), comm); - byte = 1; - out.write(&byte, sizeof(byte)); - - 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 { - 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)); - } + return comm; } -bool amount_t::valid() const +void gmp_amount::parse(const std::string& number) { - if (quantity) { - if (! commodity_) - return false; - - if (quantity->ref == 0) - return false; - } - else if (commodity_) { - return false; + // 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); } - return true; -} + int ovector[60]; + int matched; + 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); -void commodity_t::add_price(const std::time_t date, const amount_t& price) -{ - history_map::iterator i = history.find(date); - if (i != history.end()) { - (*i).second = price; + // 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); + } } else { - std::pair<history_map::iterator, bool> result - = history.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; + std::cerr << "Failed to parse amount: " << number << std::endl; } - - return NULL; } -amount_t commodity_t::value(const std::time_t moment) +void gmp_amount::credit(const amount * value) { - std::time_t age = 0; - amount_t price; - - for (history_map::reverse_iterator i = history.rbegin(); - i != history.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.size() > 0 ? - (*history.rbegin()).first : 0), price); - return price; + const gmp_amount * val = dynamic_cast<const gmp_amount *>(value); + assert(quantity_comm == val->quantity_comm); + mpz_add(quantity, quantity, val->quantity); } } // namespace ledger - -#ifdef USE_BOOST_PYTHON - -#include <boost/python.hpp> -#include <Python.h> - -using namespace boost::python; -using namespace ledger; - -void (amount_t::*parse_1)(std::istream& in) = &amount_t::parse; -void (amount_t::*parse_2)(const std::string& str) = &amount_t::parse; - -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); -} - -void export_amount() -{ - class_< amount_t > ("Amount") - .def(init<amount_t>()) - .def(init<std::string>()) - .def(init<char *>()) - .def(init<bool>()) - .def(init<int>()) - .def(init<unsigned int>()) - .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(self += self) - .def(self += int()) - .def(self + self) - .def(self + int()) - .def(self -= self) - .def(self -= int()) - .def(self - self) - .def(self - int()) - .def(self *= self) - .def(self *= int()) - .def(self * self) - .def(self * int()) - .def(self /= self) - .def(self /= int()) - .def(self / self) - .def(self / int()) - .def(- self) - - .def(self < self) - .def(self < int()) - .def(self <= self) - .def(self <= int()) - .def(self > self) - .def(self > int()) - .def(self >= self) - .def(self >= int()) - .def(self == self) - .def(self == int()) - .def(self != self) - .def(self != int()) - .def(! self) - - .def(abs(self)) - .def(self_ns::str(self)) - - .def("negate", &amount_t::negate) - .def("parse", parse_1) - .def("parse", parse_2) - .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; - - 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("last_lookup", &commodity_t::last_lookup) - .def_readwrite("conversion", &commodity_t::conversion) - .def_readwrite("ident", &commodity_t::ident) - .def_readwrite("updater", &commodity_t::updater) - - .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>()); -} - -#endif // USE_BOOST_PYTHON diff --git a/amount.h b/amount.h deleted file mode 100644 index 0761c068..00000000 --- a/amount.h +++ /dev/null @@ -1,417 +0,0 @@ -#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 int value); - amount_t(const unsigned int 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; - } - - // 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 int value); - amount_t& operator=(const unsigned int 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; - - // 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(); - } - - void parse(std::istream& in); - void parse(const std::string& str); - - 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); - -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 - -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; - - const std::string symbol; - bool quote; - std::string name; - std::string note; - unsigned short precision; - unsigned short flags; - history_map history; - std::time_t last_lookup; - amount_t conversion; - ident_t ident; - - // 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 void 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)); - assert(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), last_lookup(0) { - set_symbol(_symbol); - } - - 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) { - history_map::size_type n = history.erase(date); - return n > 0; - } - - 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; - - if (! conversion.valid()) - 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/autoxact.cc b/autoxact.cc deleted file mode 100644 index bed69e2e..00000000 --- a/autoxact.cc +++ /dev/null @@ -1,30 +0,0 @@ -#include "autoxact.h" - -namespace ledger { - -void automated_transaction_t::extend_entry(entry_t& entry) -{ - transactions_deque initial_xacts(entry.transactions.begin(), - entry.transactions.end()); - - for (transactions_deque::iterator i = initial_xacts.begin(); - i != initial_xacts.end(); - i++) - if (predicate(**i)) - for (transactions_deque::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; - - transaction_t * xact - = new transaction_t((*t)->account, amt, - (*t)->flags | TRANSACTION_AUTO); - entry.add_transaction(xact); - } -} - -} // namespace ledger diff --git a/autoxact.h b/autoxact.h deleted file mode 100644 index c85890c6..00000000 --- a/autoxact.h +++ /dev/null @@ -1,102 +0,0 @@ -#ifndef _AUTOXACT_H -#define _AUTOXACT_H - -#include "journal.h" -#include "valexpr.h" - -#include <deque> - -namespace ledger { - -typedef std::deque<transaction_t *> transactions_deque; - -class automated_transaction_t -{ -public: - item_predicate<transaction_t> predicate; - transactions_deque transactions; - - automated_transaction_t(const std::string& _predicate, - transactions_deque& _transactions) - : predicate(_predicate) { - DEBUG_PRINT("ledger.memory.ctors", "ctor automated_transaction_t"); - transactions.insert(transactions.begin(), - _transactions.begin(), _transactions.end()); - // Take over ownership of the pointers - _transactions.clear(); - } - - ~automated_transaction_t() { - DEBUG_PRINT("ledger.memory.dtors", "dtor automated_transaction_t"); - for (transactions_deque::iterator i = transactions.begin(); - i != transactions.end(); - i++) - delete *i; - } - - void extend_entry(entry_t& entry); -}; - - -typedef std::deque<automated_transaction_t *> automated_transactions_deque; - -class automated_transactions_t -{ -#ifdef DEBUG_ENABLED - automated_transactions_t(const automated_transactions_t&); -#endif - -public: - automated_transactions_deque automated_transactions; - -#ifdef DEBUG_ENABLED - automated_transactions_t() { - DEBUG_PRINT("ledger.memory.ctors", "ctor automated_transactions_t"); - } -#endif - ~automated_transactions_t() { - DEBUG_PRINT("ledger.memory.dtors", "dtor automated_transactions_t"); - for (automated_transactions_deque::iterator i - = automated_transactions.begin(); - i != automated_transactions.end(); - i++) - delete *i; - } - - void extend_entry(entry_t& entry) { - for (automated_transactions_deque::iterator i - = automated_transactions.begin(); - i != automated_transactions.end(); - i++) - (*i)->extend_entry(entry); - } - - void add_automated_transaction(automated_transaction_t * auto_xact) { - automated_transactions.push_back(auto_xact); - } - bool remove_automated_transaction(automated_transaction_t * auto_xact) { - for (automated_transactions_deque::iterator i - = automated_transactions.begin(); - i != automated_transactions.end(); - i++) { - if (*i == auto_xact) { - automated_transactions.erase(i); - return true; - } - } - return false; - } -}; - -struct autoxact_finalizer_t : public entry_finalizer_t { - automated_transactions_t auto_xacts; - virtual bool operator()(entry_t& entry) { - if (! auto_xacts.automated_transactions.empty()) - auto_xacts.extend_entry(entry); - return true; - } -}; - -} // namespace ledger - -#endif // _AUTOXACT_H diff --git a/balance.cc b/balance.cc deleted file mode 100644 index 0f75f92b..00000000 --- a/balance.cc +++ /dev/null @@ -1,277 +0,0 @@ -#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<int>()) - .def(init<unsigned int>()) - .def(init<double>()) - - .def(self += self) - .def(self += other<amount_t>()) - .def(self += int()) - .def(self + self) - .def(self + other<amount_t>()) - .def(self + int()) - .def(self -= self) - .def(self -= other<amount_t>()) - .def(self -= int()) - .def(self - self) - .def(self - other<amount_t>()) - .def(self - int()) - .def(self *= self) - .def(self *= other<amount_t>()) - .def(self *= int()) - .def(self * self) - .def(self * other<amount_t>()) - .def(self * int()) - .def(self /= self) - .def(self /= other<amount_t>()) - .def(self /= int()) - .def(self / self) - .def(self / other<amount_t>()) - .def(self / int()) - .def(- self) - - .def(self < self) - .def(self < other<amount_t>()) - .def(self < int()) - .def(self <= self) - .def(self <= other<amount_t>()) - .def(self <= int()) - .def(self > self) - .def(self > other<amount_t>()) - .def(self > int()) - .def(self >= self) - .def(self >= other<amount_t>()) - .def(self >= int()) - .def(self == self) - .def(self == other<amount_t>()) - .def(self == int()) - .def(self != self) - .def(self != other<amount_t>()) - .def(self != int()) - .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<int>()) - .def(init<unsigned int>()) - .def(init<double>()) - - .def(self += self) - .def(self += other<balance_t>()) - .def(self += other<amount_t>()) - .def(self += int()) - .def(self + self) - .def(self + other<balance_t>()) - .def(self + other<amount_t>()) - .def(self + int()) - .def(self -= self) - .def(self -= other<balance_t>()) - .def(self -= other<amount_t>()) - .def(self -= int()) - .def(self - self) - .def(self - other<balance_t>()) - .def(self - other<amount_t>()) - .def(self - int()) - .def(self *= self) - .def(self *= other<balance_t>()) - .def(self *= other<amount_t>()) - .def(self *= int()) - .def(self * self) - .def(self * other<balance_t>()) - .def(self * other<amount_t>()) - .def(self * int()) - .def(self /= self) - .def(self /= other<balance_t>()) - .def(self /= other<amount_t>()) - .def(self /= int()) - .def(self / self) - .def(self / other<balance_t>()) - .def(self / other<amount_t>()) - .def(self / int()) - .def(- self) - - .def(self < self) - .def(self < other<balance_t>()) - .def(self < other<amount_t>()) - .def(self < int()) - .def(self <= self) - .def(self <= other<balance_t>()) - .def(self <= other<amount_t>()) - .def(self <= int()) - .def(self > self) - .def(self > other<balance_t>()) - .def(self > other<amount_t>()) - .def(self > int()) - .def(self >= self) - .def(self >= other<balance_t>()) - .def(self >= other<amount_t>()) - .def(self >= int()) - .def(self == self) - .def(self == other<balance_t>()) - .def(self == other<amount_t>()) - .def(self == int()) - .def(self != self) - .def(self != other<balance_t>()) - .def(self != other<amount_t>()) - .def(self != int()) - .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 deleted file mode 100644 index 5b4a0d2a..00000000 --- a/balance.h +++ /dev/null @@ -1,869 +0,0 @@ -#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) { - 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) { - // 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 { - 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) { - 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) { - // 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 { - 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 - 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(); - } -}; - -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 { - if (quantity.amounts.size() == 1) - return (*quantity.amounts.begin()).second; - else - throw amount_error("Cannot convert a balance pair with " - "multiple commodities to an amount"); - } - - 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); - } - - bool valid() { - return quantity.valid() && (! cost || cost->valid()); - } -}; - -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 deleted file mode 100644 index 1c33df7f..00000000 --- a/binary.cc +++ /dev/null @@ -1,631 +0,0 @@ -#include "journal.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 = 0x00020020; - -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(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); - - new(&entry->transactions) transactions_list; - - for (unsigned long i = 0, count = read_binary_number<unsigned long>(data); - i < count; - i++) { - read_binary_transaction(data, xact_pool); - entry->add_transaction(xact_pool++); - } -} - -inline commodity_t * read_binary_commodity(char *& data) -{ - commodity_t * commodity = new commodity_t; - *commodities_next++ = commodity; - - commodity->ident = read_binary_number<commodity_t::ident_t>(data); - - 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); - - 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); - commodity->history.insert(history_pair(when, amt)); - } - - read_binary_number(data, commodity->last_lookup); - read_binary_amount(data, commodity->conversion); - - return commodity; -} - -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 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); - } - - // Read in the entries and transactions - - for (unsigned long i = 0; i < count; i++) { - read_binary_entry(data, entry_pool, xact_pool); - journal->entries.push_back(entry_pool++); - } - - // 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); - 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(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_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_commodity(std::ostream& out, commodity_t * commodity) -{ - commodity->ident = ++commodity_index; - - write_binary_number(out, commodity->ident); - 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); - - write_binary_number<unsigned long>(out, commodity->history.size()); - for (history_map::const_iterator i = commodity->history.begin(); - i != commodity->history.end(); - i++) { - write_binary_number(out, (*i).first); - write_binary_amount(out, (*i).second); - } - - write_binary_number(out, commodity->last_lookup); - write_binary_amount(out, commodity->conversion); -} - -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()); - - 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); - - // 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(); - } - - // 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 deleted file mode 100644 index d57888a5..00000000 --- a/binary.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef _BINARY_H -#define _BINARY_H - -#include "journal.h" -#include "parser.h" - -#include <deque> - -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 deleted file mode 100644 index c1bf427b..00000000 --- a/config.cc +++ /dev/null @@ -1,685 +0,0 @@ -#include "config.h" -#include "option.h" -#include "quotes.h" - -#include <fstream> -#include <stdlib.h> - -namespace ledger { - -config_t config; -std::list<option_t> config_options; - -config_t::config_t() -{ - if (const char * p = std::getenv("HOME")) - init_file = cache_file = price_db = p; - - init_file += "/.ledgerrc"; - cache_file += "/.ledger"; - price_db += "/.pricedb"; - - amount_expr = "a"; - total_expr = "O"; - pricing_leeway = 24 * 3600; - balance_format = "%20T %2_%-n\n"; - register_format = ("%D %-.20P %-.22N %12.66t %12.80T\n%/" - "%32|%-.22N %12.66t %12.80T\n"); - plot_amount_format = "%D %(St)\n"; - plot_total_format = "%D %(ST)\n"; - print_format = "\n%D %X%C%P\n %-34N %12o\n%/ %-34N %12o\n"; - equity_format = "\n%D %X%C%P\n%/ %-34N %12t\n"; - prices_format = "%[%Y/%m/%d %H:%M:%S %Z] %-10N %12t %12T\n"; - - show_collapsed = false; - show_subtotal = false; - show_related = false; - show_all_related = false; - show_inverted = false; - show_empty = false; - days_of_the_week = 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, - 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 += "&"; - - if (i == 1) { - config.predicate += "!"; - } - else if (add_account_short_masks) { - if (regexps[i].find(':') != std::string::npos) { - config.show_subtotal = true; - } else { - if (! config.display_predicate.empty()) - config.display_predicate += "&"; - else if (! config.show_empty) - config.display_predicate += "T&"; - - 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, arg, i, true, - (command == "b" && ! show_subtotal && - display_predicate.empty())); - if (i != args_end) - regexps_to_predicate(*this, 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"; - } - } - - // Setup the values of %t and %T, used in format strings - - try { - if (! format_t::amount_expr) - format_t::amount_expr = parse_value_expr(amount_expr); - } - catch (const value_expr_error& err) { - throw error(std::string("In amount expression (-t): ") + err.what()); - } - - try { - if (! format_t::total_expr) - format_t::total_expr = parse_value_expr(total_expr); - } - catch (const value_expr_error& err) { - throw error(std::string("In total expression (-T): ") + 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 * text_parser, - parser_t * cache_parser) -{ - int entry_count = 0; - - if (! config.init_file.empty() && - access(config.init_file.c_str(), R_OK) != -1) { - if (parse_journal_file(config.init_file, journal)) - throw error("Entries not allowed in initialization file"); - journal->sources.pop_front(); // remove init file - } - - if (cache_parser && config.use_cache && - ! config.cache_file.empty() && - ! config.data_file.empty()) { - 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); - - if (config.data_file == "-") { - config.use_cache = false; - entry_count += parse_journal(std::cin, journal, account); - } else { - entry_count += parse_journal_file(config.data_file, journal, account); - } - - 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"); - } - - for (strings_list::iterator i = config.price_settings.begin(); - i != config.price_settings.end(); - i++) { - std::string conversion = "C "; - conversion += *i; - int i = conversion.find('='); - if (i != -1) { - conversion[i] = ' '; - std::istringstream stream(conversion); - text_parser->parse(stream, journal, journal->master); - } - } - - 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\n\n" - << "Copyright (c) 2003-2004, New Artisans LLC. All rights reserved.\n\n" - << "This program is made available under the terms of the BSD Public\n" - << "License. See the LICENSE file included with the distribution for\n" - << "details and disclaimer.\n"; -} - -void option_help(std::ostream& out) -{ - out - << "usage: ledger [options] COMMAND [ACCT REGEX]... [-- [PAYEE REGEX]...]\n\n\ -Basic options:\n\ - -h, --help display this help text\n\ - -v, --version show version information\n\ - -i, --init FILE initialize ledger by loading FILE (def: ~/.ledgerrc)\n\ - --cache FILE use FILE as a binary cache when --file is not used\n\ - -f, --file FILE read ledger data from FILE\n\ - -o, --output FILE write output to FILE\n\ - -z, --set-price CONV specify a commodity conversion: \"COMM=AMOUNT\"\n\ - -a, --account NAME use NAME as default account (useful with QIF files)\n\n\ -Report filtering:\n\ - -b, --begin-date DATE set report begin date\n\ - -e, --end-date DATE set report end date\n\ - -c, --current show only current and past entries (not future)\n\ - -C, --cleared consider only cleared transactions\n\ - -U, --uncleared consider only uncleared transactions\n\ - -R, --real consider only non-virtual transactions\n\ - -r, --related calculate report using related transactions\n\n\ -Output customization:\n\ - -F, --format STR use STR as the format; for each report type, use:\n\ - --balance-format --equity-format\n\ - --register-format --plot-amount-format\n\ - --print-format --plot-total-format\n\ - --prices-format\n\ - -y, --date-format STR use STR as the date format (def: %Y/%m/%d)\n\ - -E, --empty balance: show accounts with zero balance\n\ - -n, --collapse register: collapse entries with multiple transactions\n\ - -s, --subtotal balance: show sub-accounts; register: show subtotals\n\ - -S, --sort EXPR sort report according to the value expression EXPR\n\ - -p, --interval STR report by interval (period), based on STR\n\ - --dow show a days-of-the-week report\n\ - -W, --weekly show weekly sub-totals\n\ - -M, --monthly show monthly sub-totals\n\ - -Y, --yearly show yearly sub-totals\n\ - -l, --limit EXPR calculate only transactions matching EXPR\n\ - -d, --display EXPR display only transactions matching EXPR\n\ - -t, --amount-expr EXPR set the amount expression for all report types\n\ - -T, --total-expr EXPR set the total expression for all report types\n\ - -j, --amount-data print only raw amount data (useful for scripting)\n\ - -J, --total-data print only raw total data\n\n\ -Commodity reporting:\n\ - -P, --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 commodity cost basis\n\ - -V, --market report commodity market value\n\ - -G, --gain report commodity gain/loss\n\ - -A, --average report average transaction amount\n\ - -D, --deviation report deviation from the average\n\ - -X, --trend report average deviation from the average\n\ - -Z, --weighted-trend same as trend, but older values are less significant\n\ - (-D, -X and -Z make little sense in balance reports)\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\ - 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"; -} - -////////////////////////////////////////////////////////////////////// -// -// Basic options - -OPT_BEGIN(help, "h") { - option_help(std::cout); - throw 0; -} OPT_END(help); - -OPT_BEGIN(version, "v") { - show_version(std::cout); - throw 0; -} OPT_END(version); - -OPT_BEGIN(init, "i:") { - config.init_file = optarg; -} OPT_END(init); - -OPT_BEGIN(file, "f:") { - if (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(set_price, "z:") { - if (std::strchr(optarg, '=')) - config.price_settings.push_back(optarg); - else - throw error(std::string("Invalid price setting: ") + optarg); -} OPT_END(set_price); - -OPT_BEGIN(account, "a:") { - config.account = optarg; -} OPT_END(account); - -////////////////////////////////////////////////////////////////////// -// -// Report filtering - -OPT_BEGIN(begin_date, "b:") { - if (! config.predicate.empty()) - config.predicate += "&"; - config.predicate += "d>=["; - config.predicate += optarg; - config.predicate += "]"; -} OPT_END(begin_date); - -OPT_BEGIN(end_date, "e:") { - if (! config.predicate.empty()) - config.predicate += "&"; - config.predicate += "d<["; - config.predicate += optarg; - config.predicate += "]"; -} OPT_END(end_date); - -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); - -////////////////////////////////////////////////////////////////////// -// -// 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(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(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(sort, "S:") { - config.sort_string = optarg; -} OPT_END(sort); - -OPT_BEGIN(related, "r") { - config.show_related = true; -} OPT_END(related); - -OPT_BEGIN(interval, "p:") { - config.report_interval = optarg; -} OPT_END(interval); - -OPT_BEGIN(weekly, "W") { - config.report_interval = "weekly"; -} OPT_END(weekly); - -OPT_BEGIN(dow, "") { - config.days_of_the_week = true; -} OPT_END(dow); - -OPT_BEGIN(monthly, "M") { - config.report_interval = "monthly"; -} OPT_END(monthly); - -OPT_BEGIN(yearly, "Y") { - config.report_interval = "yearly"; -} OPT_END(yearly); - -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_expr, "t:") { - config.amount_expr = optarg; -} OPT_END(amount_expr); - -OPT_BEGIN(total_expr, "T:") { - config.total_expr = optarg; -} OPT_END(total_expr); - -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, "P:") { - config.price_db = optarg; -} OPT_END(price_db); - -OPT_BEGIN(price_exp, "L:") { - 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 = "c"; - config.total_expr = "C"; -} OPT_END(basis); - -OPT_BEGIN(market, "V") { - config.show_revalued = true; - - config.amount_expr = "v"; - config.total_expr = "V"; -} OPT_END(market); - -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 = std::string("M(") + config.total_expr + ")"; -} OPT_END(average); - -OPT_BEGIN(deviation, "D") { - config.total_expr = std::string("t-M(") + config.total_expr + ")"; -} OPT_END(deviation); - -OPT_BEGIN(trend, "X") { - config.total_expr = std::string("M(t-M(") + config.total_expr + "))"; -} OPT_END(trend); - -OPT_BEGIN(weighted_trend, "Z") { - config.total_expr = (std::string("M(t-(M(") + config.total_expr + - ")/(1+(((m-d)/(30*86400))<0?0:((m-d)/(30*86400))))))"); -} OPT_END(weighted_trend); - -} // 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, 2, 3) - -void py_option_help() -{ - option_help(std::cout); -} - -void export_config() -{ - class_< config_t > ("Config") - .def_readwrite("price_settings", &config_t::price_settings) - .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_interval", &config_t::report_interval) - .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("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("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_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("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 deleted file mode 100644 index 5e1391f4..00000000 --- a/config.h +++ /dev/null @@ -1,94 +0,0 @@ -#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_interval; - std::string format_string; - std::string balance_format; - std::string 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; - unsigned long pricing_leeway; - bool show_collapsed; - bool show_subtotal; - bool show_related; - bool show_all_related; - bool show_inverted; - bool show_empty; - bool days_of_the_week; - 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 * text_parser, - parser_t * cache_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 deleted file mode 100644 index 7788528a..00000000 --- a/configure.in +++ /dev/null @@ -1,98 +0,0 @@ -# -*- Autoconf -*- -# Process this file with autoconf to produce a configure script. - -AC_PREREQ(2.59) -AC_INIT(ledger, 2.0b, johnw@netartisans.com) -AM_INIT_AUTOMAKE(ledger, 2.0b) -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 -#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([READ_GNUCASH], [1], [Support reading gnucash files]) - AM_CONDITIONAL(READ_GNUCASH, true) - AC_SUBST(LIBS, "-lxmlparse -lxmltok $LIBS")], - [AC_MSG_NOTICE([Could not find xmlparse library: Gnucash support disabled]) - AM_CONDITIONAL(READ_GNUCASH, 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]) - -AC_CONFIG_FILES([Makefile]) -AC_OUTPUT diff --git a/datetime.cc b/datetime.cc deleted file mode 100644 index 70b36279..00000000 --- a/datetime.cc +++ /dev/null @@ -1,389 +0,0 @@ -#include "datetime.h" -#include "error.h" - -#include <ctime> - -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) -{ - std::time_t quant = begin; - - if (moment && std::difftime(moment, quant) > 0) { - if (! seconds) { - struct std::tm * desc = std::localtime(&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; -#if DEBUG_LEVEL >= RELEASE - int cutoff = 10000; -#endif - while (std::difftime(moment, temp = increment(quant)) > 0) { - if (quant == temp) - break; - quant = temp; -#if DEBUG_LEVEL >= RELEASE - assert(--cutoff > 0); -#endif - } - } - - return quant; -} - -std::time_t interval_t::increment(const std::time_t moment) -{ - std::time_t then = moment; - - if (years || months) { - struct std::tm * desc = std::localtime(&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; - } - } - - 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; - } - 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); -} - -void interval_t::parse(std::istream& in) -{ - std::string word; - - while (! in.eof()) { - in >> word; - if (word == "every") { - in >> word; - if (std::isdigit(word[0])) { - int quantity = std::atol(word.c_str()); - 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") { - std::string type = word; - bool mon_spec = false; - char buf[32]; - - if (! in.eof()) - in >> word; - else - word = "month"; - - 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) { - begin = interval_t(0, -1, 0).increment(begin); - end = interval_t(0, -1, 0).increment(end); - } else { - begin = interval_t(0, 0, -1).increment(begin); - end = interval_t(0, 0, -1).increment(end); - } - } - else if (type == "next") { - if (mon_spec) { - begin = interval_t(0, 1, 0).increment(begin); - end = interval_t(0, 1, 0).increment(end); - } else { - begin = interval_t(0, 0, 1).increment(begin); - end = interval_t(0, 0, 1).increment(end); - } - } - } - else if (word == "in") { - in >> word; - parse_inclusion_specifier(word, &begin, &end); - } - else if (word == "from") { - in >> word; - if (! parse_date(word.c_str(), &begin)) - throw interval_expr_error("Could not parse 'from' date"); - if (! in.eof()) - in >> word; - } - else if (word == "to") { - in >> word; - if (! parse_date(word.c_str(), &end)) - throw interval_expr_error("Could not parse 'to' date"); - } - 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(char * date_str, std::time_t * result) -{ - int year = -1, month = -1, day, num = 0; - - for (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 = -1; - } - - 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); - } - - *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 deleted file mode 100644 index 41c97fe1..00000000 --- a/datetime.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef _DATETIME_H -#define _DATETIME_H - -#include "journal.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; - } - - std::time_t first(const std::time_t moment = 0); - std::time_t increment(const std::time_t); - - 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(char * date_str, std::time_t * result); - -} // namespace ledger - -#endif // _DATETIME_H diff --git a/debug.cc b/debug.cc deleted file mode 100644 index b7f15120..00000000 --- a/debug.cc +++ /dev/null @@ -1,112 +0,0 @@ -#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 deleted file mode 100644 index 7d3bb668..00000000 --- a/debug.h +++ /dev/null @@ -1,119 +0,0 @@ -#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[16]; \ - std::strftime(buf, 15, "%Y/%m/%d", 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 deleted file mode 100644 index f1c874c7..00000000 --- a/derive.cc +++ /dev/null @@ -1,147 +0,0 @@ -#include "derive.h" -#include "datetime.h" -#include "valexpr.h" -#include "error.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->transactions.size() > 0) - cmdty = &acct->transactions.back()->amount.commodity(); - } - - 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 (! 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 (! 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 deleted file mode 100644 index 0df7f231..00000000 --- a/derive.h +++ /dev/null @@ -1,14 +0,0 @@ -#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/error.h b/error.h deleted file mode 100644 index 52af358a..00000000 --- a/error.h +++ /dev/null @@ -1,65 +0,0 @@ -#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 deleted file mode 100644 index 875b5177..00000000 --- a/format.cc +++ /dev/null @@ -1,606 +0,0 @@ -#include "format.h" -#include "error.h" - -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"; -value_expr_t * format_t::amount_expr = NULL; -value_expr_t * format_t::total_expr = NULL; - -static struct _init_format { - ~_init_format(); -} _init_obj; - -_init_format::~_init_format() -{ - if (format_t::amount_expr) - delete format_t::amount_expr; - if (format_t::total_expr) - delete format_t::total_expr; -} - -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 '[': { - ++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 'n': current->type = element_t::ACCOUNT_NAME; break; - case 'N': current->type = element_t::ACCOUNT_FULLNAME; break; - case 'o': current->type = element_t::OPT_AMOUNT; break; - case 't': current->type = element_t::AMOUNT; break; - case 'T': current->type = element_t::TOTAL; 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; break; - case element_t::TOTAL: expr = total_expr; break; - case element_t::VALUE_EXPR: expr = elem->val_expr; break; - default: - assert(0); - break; - } - if (! expr) - break; - value_t value; - 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: - ((balance_t *) value.data)->write(out, elem->min_width, - (elem->max_width > 0 ? - elem->max_width : elem->min_width)); - break; - case value_t::BALANCE_PAIR: - ((balance_pair_t *) value.data)->quantity.write(out, elem->min_width, - (elem->max_width > 0 ? - elem->max_width : elem->min_width)); - break; - default: - assert(0); - break; - } - break; - } - - case element_t::DATE_STRING: - if (details.entry && details.entry->date != -1) { - 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::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::OPT_AMOUNT: - if (details.xact) { - std::string disp; - bool use_disp = false; - - if (details.xact->cost) { - amount_t unit_cost = *details.xact->cost / details.xact->amount; - std::ostringstream stream; - stream << details.xact->amount << " @ " << unit_cost; - disp = stream.str(); - use_disp = true; - } 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; - - // jww (2004-07-31): this should be handled differently - if (! details.xact->note.empty()) - out << " ; " << details.xact->note; - } - 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; - - 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) -{ - if (last_entry && xact.entry != last_entry) - format_last_entry(); - - transaction_xdata(xact).dflags |= TRANSACTION_TO_DISPLAY; - - last_entry = xact.entry; -} - -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; - - format_t::compute_total(result, details_t(*(*i).second)); - if (! computed) { - format_t::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 = std::time(NULL); - 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 deleted file mode 100644 index 5f6518fd..00000000 --- a/format.h +++ /dev/null @@ -1,182 +0,0 @@ -#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, - OPT_AMOUNT, - AMOUNT, - TOTAL, - SPACER, - DEPTH_SPACER - }; - - 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) {} - - ~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; - static value_expr_t * amount_expr; - static value_expr_t * total_expr; - - format_t() : elements(NULL) {} - format_t(const std::string& _format) : elements(NULL) { - reset(_format); - } - ~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; - - static void compute_amount(value_t& result, const details_t& details) { - if (amount_expr) - amount_expr->compute(result, details); - } - - static void compute_total(value_t& result, const details_t& details) { - if (total_expr) - total_expr->compute(result, details); - } -}; - -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) {} - - 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); -}; - -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,36 +1,24 @@ -#include "gnucash.h" -#include "journal.h" -#include "error.h" - -#include <iostream> #include <sstream> #include <cstring> +#include "ledger.h" + extern "C" { #include <xmlparse.h> // expat XML parser } namespace ledger { -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 * curr_account; -static commodity_t * curr_account_comm; -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 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; static enum { NO_ACTION, @@ -56,7 +44,7 @@ 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_t(curr_account); + curr_account = new account; } else if (std::strcmp(name, "act:name") == 0) action = ACCOUNT_NAME; @@ -66,7 +54,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_t(""); + curr_comm = new commodity; } else if (std::strcmp(name, "cmdty:id") == 0) action = COMM_SYM; @@ -76,7 +64,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_t; + curr_entry = new entry(main_ledger); } else if (std::strcmp(name, "trn:num") == 0) action = ENTRY_NUM; @@ -88,7 +76,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->add_transaction(new transaction_t(curr_account)); + curr_entry->xacts.push_back(new transaction()); } else if (std::strcmp(name, "split:reconciled-state") == 0) action = XACT_STATE; @@ -104,47 +92,32 @@ 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) - curr_journal->add_account(curr_account); - accounts_by_id.insert(accounts_pair(curr_account_id, curr_account)); + main_ledger->accounts.insert(accounts_map_pair(curr_account->name, + curr_account)); + accounts_by_id.insert(accounts_map_pair(curr_account_id, curr_account)); curr_account = NULL; } else if (std::strcmp(name, "gnc:commodity") == 0) { assert(curr_comm); - commodity_t::add_commodity(curr_comm); + main_ledger->commodities.insert(commodities_map_pair(curr_comm->symbol, + curr_comm)); curr_comm = NULL; } else if (std::strcmp(name, "gnc:transaction") == 0) { assert(curr_entry); - if (! curr_journal->add_entry(curr_entry)) - assert(0); + assert(curr_entry->validate()); + main_ledger->entries.push_back(curr_entry); 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) { @@ -157,20 +130,21 @@ 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->add_account(curr_account); + (*i).second->children.insert(accounts_map_pair(curr_account->name, + curr_account)); break; } case COMM_SYM: if (curr_comm) - curr_comm->set_symbol(std::string(s, len)); + curr_comm->symbol = std::string(s, len); else if (curr_account) - curr_account_comm = commodity_t::find_commodity(std::string(s, len)); + curr_account->comm = main_ledger->commodities[std::string(s, len)]; else if (curr_entry) - entry_comm = commodity_t::find_commodity(std::string(s, len)); + entry_comm = main_ledger->commodities[std::string(s, len)]; break; case COMM_NAME: @@ -193,55 +167,57 @@ static void dataHandler(void *userData, const char *s, int len) } case ENTRY_DESC: - curr_entry->payee = std::string(s, len); + curr_entry->desc = std::string(s, len); break; case XACT_STATE: - if (*s == 'y') - curr_entry->state = entry_t::PENDING; - else - curr_entry->state = entry_t::CLEARED; + curr_entry->cleared = (*s == 'y' || *s == 'c'); break; - case XACT_VALUE: + case XACT_VALUE: { assert(entry_comm); - curr_value = convert_number(std::string(s, len) + " " + - entry_comm->symbol); + std::string value = std::string(s, len) + " " + entry_comm->symbol; + curr_value = create_amount(value.c_str()); break; + } case XACT_QUANTITY: - curr_quant = convert_number(std::string(s, len)); + curr_quant = 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()) - throw error(std::string("Could not find account ") + std::string(s, len)); + 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(); - xact->account = (*i).second; + transaction * xact = curr_entry->xacts.back(); + xact->acct = (*i).second; - account_comm_map::iterator ac = account_comms.find(xact->account); - if (ac == account_comms.end()) - throw error(std::string("Could not find account ") + - std::string(*(xact->account))); + std::string value = curr_quant + " " + xact->acct->comm->symbol; - commodity_t * default_commodity = (*ac).second; + if (curr_value->commdty() == xact->acct->comm) { + // assert: value must be equal to curr_value. + delete curr_value; + curr_value = NULL; + } - curr_quant.set_commodity(*default_commodity); - amount_t value = curr_quant.round(default_commodity->precision); + xact->cost = create_amount(value.c_str(), curr_value); - if (curr_value.commodity() == *default_commodity) - curr_value = value; + if (curr_value) { + delete curr_value; + curr_value = NULL; + } - xact->amount = value; - if (value != curr_value) - xact->cost = new amount_t(curr_value); + if (do_compute) + xact->acct->balance.credit(xact->cost); break; } case XACT_NOTE: - curr_entry->transactions.back()->note = std::string(s, len); + curr_entry->xacts.back()->note = std::string(s, len); break; case NO_ACTION: @@ -255,35 +231,25 @@ static void dataHandler(void *userData, const char *s, int len) } } -bool gnucash_parser_t::test(std::istream& in) const -{ - char buf[23]; - in.get(buf, 22); - in.seekg(0); - - return std::strncmp(buf, "<?xml version=\"1.0\"?>", 21) == 0; -} - -unsigned int gnucash_parser_t::parse(std::istream& in, - journal_t * journal, - account_t * master, - const std::string * original_file) +book * parse_gnucash(std::istream& in, bool compute_balances) { char buf[BUFSIZ]; - count = 0; + book * ledger = new book; + + main_ledger = ledger; + do_compute = compute_balances; action = NO_ACTION; - curr_journal = journal; curr_account = NULL; curr_entry = NULL; + curr_value = NULL; curr_comm = NULL; entry_comm = NULL; // GnuCash uses the USD commodity without defining it, which really - // means $. - commodity_t * usd = new commodity_t("$", 2, COMMODITY_STYLE_THOUSANDS); - commodity_t::add_commodity(usd); - commodity_t::add_commodity(usd, "USD"); + // means to use $. + commodity * usd = new commodity("$", true, false, true, false, 2); + main_ledger->commodities.insert(commodities_map_pair("USD", usd)); XML_Parser parser = XML_ParserCreate(NULL); current_parser = parser; @@ -293,36 +259,23 @@ unsigned int gnucash_parser_t::parse(std::istream& in, while (! in.eof()) { in.getline(buf, BUFSIZ - 1); - if (! XML_Parse(parser, buf, std::strlen(buf), in.eof())) - throw parse_error(original_file ? *original_file : "<gnucash>", - XML_GetCurrentLineNumber(parser), - XML_ErrorString(XML_GetErrorCode(parser))); + + 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; + } } XML_ParserFree(parser); accounts_by_id.clear(); curr_account_id.clear(); + curr_quant.clear(); - return count; -} + main_ledger->commodities.erase("USD"); -} // 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()) - ; + return ledger; } -#endif // USE_BOOST_PYTHON +} // namespace ledger diff --git a/gnucash.h b/gnucash.h deleted file mode 100644 index 6b71fffa..00000000 --- a/gnucash.h +++ /dev/null @@ -1,21 +0,0 @@ -#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 deleted file mode 100644 index 2dc2576d..00000000 --- a/journal.cc +++ /dev/null @@ -1,750 +0,0 @@ -#include "journal.h" -#include "valexpr.h" -#include "datetime.h" -#include "error.h" -#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; - - found = false; - for (transactions_list::const_iterator i = account->transactions.begin(); - i != account->transactions.end(); - i++) - if (*i == this) { - found = true; - break; - } - if (! found) - return false; - - if (! amount.valid()) - return false; - - if (cost && ! cost->valid()) - return false; - - if (flags & ~0x000f) - return false; - - return true; -} - -balance_pair_t& add_transaction_to(const transaction_t& xact, - balance_pair_t& bal_pair) -{ - if (xact.cost && ! bal_pair.cost) - bal_pair.cost = new balance_t(bal_pair.quantity); - - bal_pair.quantity += xact.amount; - - if (bal_pair.cost) - *bal_pair.cost += xact.cost ? *xact.cost : xact.amount; - - return bal_pair; -} - -value_t& add_transaction_to(const transaction_t& xact, value_t& value) -{ - switch (value.type) { - case value_t::BOOLEAN: - case value_t::INTEGER: - value.cast(value_t::AMOUNT); - - case value_t::AMOUNT: - if (xact.cost) { - value.cast(value_t::BALANCE_PAIR); - return add_transaction_to(xact, value); - } - else if (((amount_t *) value.data)->commodity() != - xact.amount.commodity()) { - value.cast(value_t::BALANCE); - return add_transaction_to(xact, value); - } - *((amount_t *) value.data) += xact.amount; - break; - - case value_t::BALANCE: - if (xact.cost) { - value.cast(value_t::BALANCE_PAIR); - return add_transaction_to(xact, value); - } - *((balance_t *) value.data) += xact.amount; - break; - - case value_t::BALANCE_PAIR: - add_transaction_to(xact, *((balance_pair_t *) value.data)); - break; - - default: - assert(0); - break; - } - - return value; -} - -void entry_t::add_transaction(transaction_t * xact) -{ - xact->entry = this; - transactions.push_back(xact); - xact->account->add_transaction(xact); -} - -bool entry_t::remove_transaction(transaction_t * xact) -{ - transactions.remove(xact); - xact->account->transactions.remove(xact); - return true; -} - -bool finalize_entry(entry_t& entry) -{ - // 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 = entry.transactions.begin(); - x != entry.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 = entry.transactions.begin(); - x != entry.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 = entry.transactions.begin(); - x != entry.transactions.end(); - x++) { - if ((*x)->amount || ((*x)->flags & TRANSACTION_VIRTUAL)) - 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. - - (*x)->amount = *((amount_t *) balance.data); - (*x)->amount.negate(); - - balance = 0U; - } - - return ! balance; -} - -bool entry_t::valid() const -{ - if (! date || date == -1) - 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; -} - -account_t::~account_t() -{ - DEBUG_PRINT("ledger.memory.ctors", "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)); -} - -bool account_t::remove_transaction(transaction_t * xact) -{ - transactions.remove(xact); - return true; -} - -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 (name.find('-') != std::string::npos) - return false; - - if (depth > 16) - return false; - - for (transactions_list::const_iterator i = transactions.begin(); - i != transactions.end(); - i++) - if ((*i)->account != this) - 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"); - - 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(); - - if (item_pool) - delete[] item_pool; -} - -bool journal_t::add_entry(entry_t * entry) -{ - if (! run_hooks(entry_finalize_hooks, *entry)) { - delete entry; - return false; - } - - entries.push_back(entry); - - for (transactions_list::const_iterator i = entry->transactions.begin(); - i != entry->transactions.end(); - i++) { - (*i)->account->add_transaction(*i); - if ((*i)->cost) - (*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); - - for (transactions_list::const_iterator i - = entry->transactions.begin(); - i != entry->transactions.end(); - i++) { - (*i)->account->remove_transaction(*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_t& entry) -{ - return entry.transactions.size(); -} - -transaction_t& transactions_getitem(entry_t& entry, int i) -{ - static int last_index = 0; - static entry_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; -} - -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); -} - -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; -} - -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(compute_error) -EXC_TRANSLATOR(value_expr_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>())) - .def_readwrite("account", &transaction_t::account) - .def_readwrite("amount", &transaction_t::amount) - .def_readwrite("cost", &transaction_t::cost) - .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> >()) - .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) - .def_readonly("transactions", &account_t::transactions) - .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("add_transaction", &account_t::add_transaction) - .def("remove_transaction", &account_t::remove_transaction) - - .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", &journal_t::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) - ; - - scope in_entry = class_< entry_t > ("Entry") - .def(self == self) - .def(self != self) - - .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("__len__", transactions_len) - .def("__getitem__", transactions_getitem, - return_internal_reference<1>()) - - .def("add_transaction", &entry_t::add_transaction) - .def("remove_transaction", &entry_t::remove_transaction) - - .def("valid", &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(compute_error); - EXC_TRANSLATE(value_expr_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 deleted file mode 100644 index 0e645064..00000000 --- a/journal.h +++ /dev/null @@ -1,282 +0,0 @@ -#ifndef _JOURNAL_H -#define _JOURNAL_H - -#include <map> -#include <list> -#include <string> -#include <ctime> -#include <iostream> - -#include "amount.h" -#include "value.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) { - } - - 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) { - } - - ~transaction_t() { - //assert(! data); - 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; -}; - -balance_pair_t& add_transaction_to(const transaction_t& xact, - balance_pair_t& bal_pair); -value_t& add_transaction_to(const transaction_t& xact, value_t& value); - -typedef std::list<transaction_t *> transactions_list; - -class entry_t -{ - public: - enum state_t { - UNCLEARED, CLEARED, PENDING - }; - - std::time_t date; - state_t state; - std::string code; - std::string payee; - - transactions_list transactions; - - entry_t() : date(-1), state(UNCLEARED) {} - ~entry_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_t& entry) { - return this == &entry; - } - bool operator!=(const entry_t& entry) { - return ! (*this == entry); - } - - void add_transaction(transaction_t * xact); - bool remove_transaction(transaction_t * xact); - - bool valid() const; -}; - -bool finalize_entry(entry_t& entry); - - -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; - transactions_list transactions; - 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) {} - - ~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(); - } - - // These functions should only be called from journal_t::add_entry - // and journal_t::remove_entry; or from the various parsers. - void add_transaction(transaction_t * xact) { - transactions.push_back(xact); - } - bool remove_transaction(transaction_t * xact); - - friend class journal_t; - - bool valid() const; -}; - -std::ostream& operator<<(std::ostream& out, const account_t& account); - - -struct entry_finalizer_t { - virtual ~entry_finalizer_t() {} - virtual bool operator()(entry_t& entry) = 0; -}; - -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<std::string> strings_list; - -class journal_t -{ - public: - account_t * master; - entries_list entries; - strings_list sources; - char * item_pool; - char * item_pool_end; - - mutable accounts_map accounts_cache; - - func_finalizer_t default_finalizer; - std::list<entry_finalizer_t *> entry_finalize_hooks; - - journal_t() : default_finalizer(finalize_entry) { - master = new account_t(NULL, ""); - item_pool = item_pool_end = NULL; - add_entry_finalizer(&default_finalizer); - } - ~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; -}; - -extern const std::string version; - -} // namespace ledger - -#endif // _JOURNAL_H diff --git a/ledger.cc b/ledger.cc new file mode 100644 index 00000000..4985fba6 --- /dev/null +++ b/ledger.cc @@ -0,0 +1,674 @@ +#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 use with the "ledger" command-line tool +;;; ledger.el --- Helper code for using my "ledger" command-line tool ;; Copyright (C) 2004 John Wiegley (johnw AT gnu DOT org) @@ -32,14 +32,7 @@ ;;; Commentary: -;; 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. +;; This code is only meaningful if you are using "ledger". (defvar ledger-version "1.1" "The version of ledger.el currently loaded") @@ -53,11 +46,6 @@ :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 @@ -100,10 +88,11 @@ (if (time-less-p moment date) (throw 'found t))))))) -(defun ledger-add-entry (entry-text) +(defun ledger-add-entry (entry) (interactive - (list (read-string "Entry: " (format-time-string "%Y/%m/")))) - (let* ((date (car (split-string entry-text))) + (list (read-string "Entry: " (format-time-string "%Y/%m/%d ")))) + (let* ((args (mapcar 'shell-quote-argument (split-string entry))) + (date (car args)) (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)) @@ -116,16 +105,15 @@ (save-excursion (insert (with-temp-buffer - (setq exit-code (ledger-run-ledger "entry" entry-text)) + (setq exit-code + (apply 'call-process ledger-binary-path nil t nil + (cons "entry" args))) (if (= 0 exit-code) - (progn - (goto-char (point-min)) - (delete-char 1) - (if insert-year - (buffer-string) - (buffer-substring 5 (point-max)))) - (concat (if insert-year entry-text - (substring entry-text 5)) "\n"))) "\n")))) + (if insert-year + (buffer-string) + (buffer-substring 5 (point-max))) + (concat (if insert-year entry + (substring entry 5)) "\n\n"))))))) (defun ledger-expand-entry () (interactive) @@ -218,7 +206,9 @@ (defun ledger-update-balance-display () (let ((account ledger-acct)) (with-temp-buffer - (let ((exit-code (ledger-run-ledger "-C" "balance" account))) + (let ((exit-code + (apply 'call-process ledger-binary-path nil t nil + (list "-C" "balance" account)))) (if (/= 0 exit-code) (setq ledger-reconcile-text "Reconcile [ERR]") (goto-char (point-min)) @@ -228,7 +218,7 @@ (concat "Reconcile [" (buffer-substring-no-properties (point-min) (point)) "]")))))) - (force-mode-line-update)) + (redraw-modeline)) (defun ledger-reconcile-toggle () (interactive) @@ -300,14 +290,6 @@ (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 +#define _LEDGER_H "$Revision$" ////////////////////////////////////////////////////////////////////// // @@ -7,30 +7,353 @@ // // A command-line tool for general double-entry accounting. // -// Copyright (c) 2003,2004 John Wiegley <johnw@newartisans.com> +// Copyright (c) 2003 John Wiegley <johnw@newartisans.com> // -#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 <autoxact.h> -#include <binary.h> -#include <gnucash.h> -#include <qif.h> +#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 #endif // _LEDGER_H diff --git a/ledger.texi b/ledger.texi index 3d5acd26..bcc1937f 100644 --- a/ledger.texi +++ b/ledger.texi @@ -16,24 +16,26 @@ @node Top, Introduction, (dir), (dir) @top Overview +@c Page published by Emacs Muse begins here + + @menu -* Introduction:: -* Running Ledger:: -* Keeping a ledger:: -* Extending with Python:: +* Introduction:: +* Keeping a ledger:: +* Running Ledger:: @end menu -@node Introduction, Running Ledger, Top, Top +@node Introduction, Keeping a 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 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. +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. 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 @@ -59,10 +61,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, @@ -86,15 +88,14 @@ The entry might look like this: Checking $-23.00 $77.00 @end example -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 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... 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 @@ -104,20 +105,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 -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 +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 -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.} +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.} -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,21 +127,24 @@ want to dive in head-first---here are the ledger entries from above, formatting as the ledger program wishes to see them: @example -2004/09/29 Pacific Bell +; Set the year for subsequent entries to 2004 +Y 2004 + +9/29 Pacific Bell Payable:Pacific Bell $-200.00 Equity:Opening Balances -2004/09/29 Checking +9/29 Checking Accounts:Checking $100.00 Equity:Opening Balances -2004/09/29 Pacific Bell +9/29 Pacific Bell Payable:Pacific Bell $23.00 Accounts:Checking @end example The account balances and registers in this file, if saved as -@file{ledger.dat}, could be reported using: +@samp{ledger.dat}, could be reported using: @example $ ledger -f ledger.dat balance @@ -149,1053 +153,31 @@ $ ledger -f ledger.dat register bell @end example @menu -* Building the program:: +* Building the program:: @end menu @node Building the program, , Introduction, Introduction @section Building the program Ledger is written in ANSI C++, and should compile on any platform. It -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. +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 -./configure && make install -@end example - -@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 will take a while to master them. -However, none of them are required to use the basic reporting -commands. - -@menu -* Commands:: -* Options:: -* Format strings:: -* Value expressions:: -* Interval expressions:: -* Plotting register data:: -* Typical queries:: -* File format:: -@end menu - -@node Commands, Options, Running Ledger, 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 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{-X} to show the price -trend. - -@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: - -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 +make +cp ledger /usr/local/bin @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 @command{vi} to let you confirm that the entry looks -appropriate. - -Here are a few more examples of the entry command, assuming the above -journal entry: - -@example -# Pay $11.00 to first transaction account (food) -ledger entry 4/9 viva 11.00 - -# Pay $11.00 to food from checking -ledger entry 4/9 viva 11.00 checking - -# Pay $11.00 to food, $8.00 to tips -ledger entry 4/9 viva food 11.00 tips 8 - -# Same as the last, but draw from cash -ledger entry 4/9 viva food 11.00 tips 8 cash - -# Same as the last, but force the commodity -ledger entry 4/9 viva food $11.00 tips $8 cash - -# Pay DM 11.00 to dining, no matter what the entry said -ledger entry 4/9 viva dining "DM 11.00" -@end example - -@node Options, Format strings, Commands, Running Ledger -@section Options - -With all of the reports, command-line options are useful for modifying -the output generated. You will often find yourselves adding option -flags to the command-line to modify those commands. - -Command-line options always occur before the command word. This is -done to distinguish them from exclusive matching expressions, which -also begin with a desh. The basic form for most commands is: - -@example -ledger [OPTIONS] COMMAND [REGEXPS...] [-- [REGEXPS...]] -@end example - -The @var{OPTIONS} and @var{REGEXPS} expressions are optional. You -could use just ``ledger balance'' without any options, which would -print the summarized total for 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{--init 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: - -@example ---price-db ~/finance/.pricedb -@end example - -Option settings on the command-line or in the environment always take -precedence over settings in the init file. - -@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 -@var{LEDGER_FILE} is set, rather than using this command-line option. - -@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 @var{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 @var{LEDGER_CACHE}, or by -putting the option into your init file. - -@option{--output FILE} (@option{-o FILE}) redirects output from any -command to @var{FILE}. By default, all output goes to standard -output. - -@option{--set-price CONV} (@option{-z CONV}) specifies a forced -commodity conversion. If a setting like @samp{COMM=$1.20} is used, -the commodity @var{COMM} is reported in terms of the conversion -factor. This can be useful for performing arbitrary value -substitutions. For example, to report dollars in terms of the ounces -of gold they would buy, use: @samp{-z "$=0.00280112 AU"} (or whatever -the current exchange rate is). - -@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{--begin-date 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 DATE} (@option{-e DATE}) contrains 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{--current}(@option{-c}) displays only entries occurring on or -before the current date. - -@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{--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: - -@example -2004/03/20 Safeway - Expenses:Food $65.00 - Expenses:Cash $20.00 - Assets:Checking -@end example - -And if the register command was: - -@example -ledger -r register food -@end example - -This would be the output, to show the transactions related to the -transaction that matched. - -@example -2004/03/20 Safeway Expenses:Cash $-20.00 $-20.00 - Assets:Checking $85.00 $65.00 -@end example - -@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{--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 -@option{--date-format FORMAT} to the initialize file @file{~/.ledgerc} -(or the file referred to by @var{LEDGER_INIT}). - -@option{--format STR} (@option{-F STR}) sets the reporting format for -whatever report ledger is about to make. @xref{Format strings}. - -You can also set the default format for a given report type, usually -in the initialization file @file{~/.ledgerrc}, by using one of the -following options: - -@table @code -@item --balance-format STR -Sets the default format for the @command{balance} report. - -@item --equity-format STR -Sets the default format for the @command{equity} report. - -@item --register-format STR -Sets the default format for the @command{register} report. - -@item --print-format STR -Sets the default format for the @command{print} report. - -@item --prices-format STR -Sets the default format for the @command{prices} report. - -@item --plot-value-format STR -Sets the default format for the @command{register} report, when -@option{-j} is being used. - -@item --plot-total-format STR -Sets the default format for the @command{register} report, when -@option{-J} is being used. -@end table - -@option{--empty} (@option{-E}) includes even empty accounts in the -@command{balance} report. - -@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{--sort EXPR} (@option{-S EXPR}) sorts a report by comparing -the values determined using the value expression @var{EXPR}. For -example, using @option{-S -AT} 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{--interval STR} (@option{-p STR}) sets the reporting interval -to @var{STR}. This will subtotal all matching entries within each -interval separately, making it easy to see weekly, monthly, quarterly, -etc., transaction totals. An interval string can even specify the -beginning and end of the report range, using simple terms like ``last -june'' or ``next month''. For more using interval expressions, see -@ref{Interval expressions}. - -@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{--weekly} (@option{-W}) reports transaction totals by the -week. The week begins on whichever day a transaction is first found, -so to start each week on a Sunday, specifying a beginning date for the -report that falls on a Sunday. @option{--monthly} (@option{-M}) -reports transaction totals by month; @option{--yearly} (@option{-Y}) -reports transaction totals by year. For more complex interval, using -the @option{--interval} option described above. - -@option{--limit EXPR} (@option{-l EXPR}) limits which transactions -take part in the calculations of a report. - -@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{--value EXPR} (@option{-t EXPR}) changes the value expression -used to calculate the ``value'' column in the @command{register} -report, and the totals in the @command{equity} report. @xref{Value -expressions}. - -@option{--total EXPR} (@option{-T EXPR}) sets the value expression -used by the ``total'' column in the @command{register} report, and -also the @command{balance} report. - -@option{--value-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. - -@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 - -@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 @var{LEDGER_PRICE_DB}. - -@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. - -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 --quantity -(@option{-O}) reports commodity totals (this is the default) - -@item --basis -(@option{-B}) reports the cost basis for all transactions. - -@item --market -(@option{-V}) reports the last known market value for all commodities. - -@item --gain -(@option{-G}) reports the net gain/loss for all commodity with a price -history. - -@item --average -(@option{-A}) reports the average transaction value. - -@item --deviation -(@option{-D}) reports each transaction's deviation from the average. -This is only meaningful in the @command{register} report. - -@item --trend -(@option{-X}) reports the basic trend of all transaction values. This -can indicate whether spending is on the increase, or decrease, based -on the way the trend values change in the totals column of the -@command{register} report. - -@item --weighted-trend -(@option{-Z}) reports the trend, like @option{--trend}, but factors in -the passage of time. Older transactions do not swing the trend -numbers as much as recent and future transactions. Thus, recent heavy -spending will indicate a higher trend, than if time were not -considered. -@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 @var{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: +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. -@example ---cache /tmp/.mycache -@end example - -@node Format strings, Value expressions, Options, Running Ledger -@section Format strings - -Format strings can 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 subtitution 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 subtituted will be left justified. The default is right -justified. If a mininum 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 n -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, oherwise it just prints the account's name. - -@item N -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 tranaction amount and its cost. It's use is -not recommend unless you are modifying the print report. - -@item / -The @samp{%/} construct is special. It separates a format string -between what should be printed for the first transaction in an entry, -and what should be printed for all subsequent transactions. If not -used, the same format string is applied to all transactions. -@end table - -Here are default format strings for the balance and print reports, -just to give an example: - -@example -%20T %2_%-n\n -\n%D %X%C%P\n %-34N %12o\n%/ %-34N %12o\n -@end example - -@node Value expressions, Interval expressions, Format strings, Running Ledger -@section Value expressions - -A value expression is a language used by ledger wherever a value is -involved. Some examples are: - -@enumerate -@item Values displayed in reports -@item Predicates, or which transactions get calculated/displayed -@item Sorting criteria, or how transactions are sorted -@item 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:AT>100 balance -@end example - -The effect is that account totals are displayed only if: 1) A -Liabitilies 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>[$(date +%Y/%m/01)]" 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 transactions -displayed: - -@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 c -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-c}. - -@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. -@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 C -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-C}. -@end table - -@subsection Functions - -The available one letter functions are: - -@table @code -@item - -Negates the argument. - -@item A -The absolute value of the argument. - -@item S -Strips the commodity from the argument. - -@item M -The median of the argument; @samp{Mx} 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}. -@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]}. -@end table - -@node Interval expressions, Plotting register data, Value expressions, Running Ledger -@section Interval expressions - -@node Plotting register data, Typical queries, Interval expressions, Running Ledger -@section Plotting register data - -@node Typical queries, File format, Plotting register data, Running Ledger -@section Typical queries - -Once you have an orderly and well-organized general ledger, the next -step is to generate orderly and well-organized reports. This is where -the Ledger command-line 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 compute the present 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 -p 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 -s balance -@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 - -A query like the following will show the largest expense categories -since a given date: - -@example -ledger -b 2003/10 -s -S -AT -d "T>1000" bal ^expenses -internet -@end example - -From left to right the options used mean: Show entries since October, -2003; showa all sub-accounts; sort in verse order by the absolute -value of the total; display only accounts with a total value greater -than 1000 units (of whichever commodities the account contains); and -report the balance for all expenses, except for those matching -``internet''. - -@node File format, , Typical queries, 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 can 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 transaction. 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 line beginning with an exclamation mark denotes a command directive. -It must be immediately followed by the command word. The supported -commands are: - -@table @strong -@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 (whitespace) -A line beginning with whitespace, which is not part of a regular or -automated transaction, is also 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 C -Specifies a conversion price for a commodity. This has no reference -to time, and takes precedence over all historical prices (even the -current prices). Syntax is: -@example -C 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 options 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 Keeping a ledger, Extending with Python, Running Ledger, Top +@node Keeping a ledger, Running Ledger, Introduction, Top @chapter Keeping a ledger The most important part of accounting is keeping a good ledger. If @@ -1206,19 +188,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 transaction involving an account it knows +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, @@ -1250,39 +232,42 @@ For this entry, Ledger will figure out that $-23.00 must come from ``Assets:Checking'' in order to balance the entry. @menu -* 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:: -* Differences with accounting conventions:: +* Credits and Debits:: +* Commodities and Currencies:: +* Accounts and Inventories:: +* Understanding Equity:: +* Dealing with 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 Stating where money goes, Assets and Liabilities, Keeping a ledger, Keeping a ledger -@section Stating where money goes +@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. -Accountants will talk of `credits' and `debits', but their meaning is -often different from the layman's definitions. To avoid this semantic -overloading, we will refer to subtractions and additions. -@xref{Differences with accounting conventions}, for more on how to -reconcile the two systems. +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. -Recall that every transaction will involve two or more accounts. -Money is transferred from one group of accounts to another group. To -record the transaction, @emph{subtract} an amount from the source -accounts, and @emph{add} the same amount to the target accounts. +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. -In order to write the Ledger entry correctly, you must determine where -the money comes from, and where it goes to. For example, when you are -paid, in order to add to your bank account, you must subtract from an -income account: +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 @@ -1296,265 +281,35 @@ 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 money flows. Rather than Ledger -``fixing'' the minus signs, let's understand why they are there. +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 (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. +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 +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? -@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: - -@example -2004/09/29 My Employer - Assets:Checking $500.00 - Income:Salary -@end example - -Money, here, is coming from an Income account belonging to ``My -Employer'', and is being transferred to an account that belongs to you. -The money is now yours, which makes it an asset. - -Liability accounts track money you owe to others. They come into play -whenever you borrow money to buy something, or if you owe someone -money. The usual way a liability is changed is by expending money, -thus transferring it to an Expenses account. For example: - -@example -2004/09/30 Restaurant - Expenses:Dining $25.00 - Liabilities:MasterCard -@end example - -Your account balance will now show $25 spent on Dining, and a -corresponding $25 owed on your MasterCard. The MasterCard liability -will show up as negative, since it offsets the value of your assets. -@emph{The combined total of your Assets and Liabilities is your net -worth}. To see your current net worth, use this command: - -@example -$ ledger balance ^assets ^liabilities -@end example - -Relatedly, your Income accounts will show up negative, because they -transfer money @emph{from} an account in order to increase your -assets. Your Expenses accounts will show up positive, because that is -where the money went. The combined total your Income and Expenses is -your cash flow. A negative cash flow means that you are spending more -cash than you make. To see your current cash flow, use this command: - -@example -$ ledger balance ^income ^expenses -@end example - -Often, it is only important to view your income and expenses when -asking questions like, ``Where did my money go? Am I spending too -much on X? Am I making enough to cover my expenses?'' But most of -the time, you will usually want to ask other questions like, ``Is -there enough money in my checking account to cover my next credit card -bill?'' For these reasons, I recommend creating a script that removes -Income, Expenses, and Equity by default from your basic balance -report. The provided script ``bal'' does this for you, as well as -making it easier to run the balance command: - -@example -$ bal -@end example - -To use this script, it must be copied from the @file{scripts} -directory in the ledger distribution, to a directory along your -@var{PATH}. Also, you must set the environment variable @var{LEDGER} -to point to your main ledger file. - -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 -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}. - -@menu -* Tracking reimbursable expenses:: -@end menu - -@node Tracking reimbursable expenses, , Assets and Liabilities, Assets and Liabilities -@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: - -@example -2004/09/29 Circuit City - Assets:Reimbursements:Company XYZ $100.00 - Liabilities:MasterCard -@end example - -This shows that you spent $100.00 on your MasterCard at Circuit City, -but that the expense was made on behalf of Company XYZ. Later, when -Company XYZ pays you back, you will transfer the money from your -reimbursement account to a regular asset account: - -@example -2004/09/29 Company XYZ - Assets:Checking $100.00 - Assets:Reimbursements:Company XYZ -@end example - -This deposits the money owed from Company XYZ into your checking -account, presumably because they paid you 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: - -@example -2004/09/29 Circuit City - Assets:Reimbursements:Company XYZ $100.00 - Liabilities:MasterCard -@end example - -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: - -@example -2004/09/29 Circuit City - Company XYZ:Expenses:Computer:Software $100.00 - Company XYZ:Accounts Payable:Your Name -@end example - -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: - -@example -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 example - -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: - -@example -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 example - -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: - -@example -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 example - -And two in your company ledger file: - -@example -!account Company XYZ - -2004/09/29 Circuit City - Expenses:Computer:Software $100.00 - Accounts Payable:Your Name $-100.00 +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. -2004/10/15 Company XYZ - Accounts Payable:Your Name $100.00 - Assets:Checking $-100.00 - -!end -@end example - -(Note: The @ above command means that all accounts mentioned in the -file are children of the specified account. In this case it means -that all activity in 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 +@node Commodities and Currencies, Accounts and Inventories, Credits and Debits, Keeping a ledger @section Commodities and Currencies Ledger makes no assumptions about the commodities you use; it only @@ -1568,10 +323,11 @@ specifiers: @example $20.00 ; currency: twenty US dollars +USD 20 ; currency: the same 40 AAPL ; commodity: 40 shares of Apple stock -60 DM ; currency: 60 Deutsch Mark +MD 60 ; currency: 60 Deutsch Mark £50 ; currency: 50 British pounds -50e ; currency: 50 Euros (use appropriate symbol) +50e ; currency: 50 Euros (use symbol) @end example Ledger will examine the first use of any commodity to determine how @@ -1611,7 +367,7 @@ BORL=$19 FEQTX=$32 @end example -Specify the prices file using the @option{-p} option: +Specify the prices file using the @samp{-p} option: @example ledger -p prices.db balance brokerage @@ -1655,7 +411,7 @@ This is a roundabout way of reporting AAPL shares in their Deutsch Mark equivalent. @menu -* Commodity price histories:: +* Commodity price histories:: @end menu @node Commodity price histories, , Commodities and Currencies, Commodities and Currencies @@ -1678,16 +434,17 @@ P 2004/06/21 02:18:02 AAPL $32.91 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, several options are possible: +To enable pricing reports, three options are possible: -@table @code +@table @strong @item @strong{-P FILE} -With this option, or if the environment variable @var{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. +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{-O} +@item @strong{-T} Report commodity totals only, not their market value or basis cost. @item @strong{-V} @@ -1698,19 +455,18 @@ 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{-G} +@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 @option{-L} option) pricing quotes are obtained by -calling the script @command{getquote} (a sample Perl script is -provided, but the interface is kept simple so replacements may be -made). +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 @option{-Q} flag, new quotes are obtained only if current +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. @@ -1725,7 +481,7 @@ is). @end table -Note that the @option{-B}, @option{-O}, @option{-V}, and @option{-G} are mutually exclusive. +Note that the @samp{-B}, @samp{-T}, @samp{-V}, and @samp{-A} are mutually exclusive. @node Accounts and Inventories, Understanding Equity, Commodities and Currencies, Keeping a ledger @section Accounts and Inventories @@ -1746,7 +502,7 @@ EverQuest account: 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 add to your Inventory account. Note that you don't have to +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 @@ -1765,7 +521,7 @@ would look like: Now you've turned 2 steaks into 15 gold, courtesy of your customer, Strum Brightblade. -@node Understanding Equity, Dealing with Petty Cash, Accounts and Inventories, Keeping a ledger +@node Understanding Equity, Dealing with cash, Accounts and Inventories, Keeping a ledger @section Understanding Equity The most confusing entry in any ledger will be your equity account--- @@ -1801,20 +557,20 @@ 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 -``@samp{-- -Equity}'' at the end of your balance command, to remove the +``-- -Equity'' at the end of your balance command, to remove the confusing figure from the totals. -@node Dealing with Petty Cash, Archiving previous years, Understanding Equity, Keeping a ledger -@section Dealing with Petty Cash +@node Dealing with cash, Archiving previous years, Understanding Equity, Keeping a ledger +@section Dealing with cash Something that stops many people from keeping a ledger at all is the -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. +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. -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 ``Expenses:Cash'' category: +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 @@ -1835,7 +591,7 @@ target account: This way, you can still track large cash expenses, while ignoring all of the smaller ones. -@node Archiving previous years, Virtual transactions, Dealing with Petty Cash, Keeping a ledger +@node Archiving previous years, Virtual transactions, Dealing with cash, Keeping a ledger @section Archiving previous years After a while, your ledger can get to be pretty large. While this @@ -1852,7 +608,7 @@ 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 @file{ledger-old.dat}. +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): @@ -1882,8 +638,8 @@ $ mv x ledger.dat $ rm equity.dat @end example -Now the balances reported from @file{ledger.dat} are identical to what -they were before the data was split. +Now the balances reported from @samp{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 @@ -1941,17 +697,16 @@ To view balances without any virtual balances factored in, using the Write about: Saving for a Special Occasion; Keeping a Budget; Tracking Allocated Funds. -@node Automated transactions, Differences with accounting conventions, Virtual transactions, Keeping a ledger +@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 as I acquire assets. It is -similar to tithing for Jews and Christians, and to Zakát for Muslims. -The exact details of computing Huqúqu'lláh are a bit complex, but if -you have further interest, please consult the Web. +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. -Ledger makes this otherwise difficult calculation quite easy. Just -set up the following automated transaction at the top of your ledger -file: +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 @@ -1969,13 +724,12 @@ file: 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. +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'': @@ -1994,69 +748,10 @@ ledger balance Liabilities:Huqúq @end example -@node Differences with accounting conventions, Using Emacs to Keep Your Ledger, Automated transactions, Keeping a ledger -@section Differences with accounting conventions -If you are an accountant, or you are familiar with accounting -terminology, then you might be tearing your hair out after reading the -above. Please don't! - -Ledger is intended to make people comfortable with their finances; to -help them better control the flow of their money. Contemporary -accounting practices, on the other hand, often seem counter-intuitive -and confusing to the layman. To make Ledger more accessible, it -avoids the use of standard accounting conventions and terminology. -However, Ledger is flexible enough that you may interpret what is -happening however you wish. - -Most probably, the following section will confuse you, and you should -skip it if you've managed to understand everything so far. However, -if you intend to communicate your accounting practices to a -professional accountant, the following explanations may be useful. - -@table @strong -@item The entity -The individual or organisation under consideration: the someone or -something on whose behalf you are accounting. Probably you. - -@item Assets -Future economic benefits controlled by the entity as a result of a -past transaction or event. - -@item Liabilities -Future sacrifices of economic benefits that the entity is obliged to -make as a result of a past transaction or event. - -@end table - -The format of the data files used by Ledger is more akin to a general -journal than a ledger. In an accounting ledger, transactions are -grouped by account. In a general journal, transactions are commonly -listed in chronological order. - -Often ``cash'' is used to refer to a liquid savings account at a bank, -rather than the physical notes and coins you may withdraw. - -In general, an ``addition'' in Ledger is an accounting debit, and a -``subtraction'' in Ledger is an accounting credit. The following table -shows the ``normal'' balances for the different types of accounts. -Accountants avoid using negative balances where possible, instead -prefering a positive amount in ``credit'' balance. - -@multitable @columnfractions 0.2 0.2 0.2 0.2 0.2 -@item System @tab Asset @tab Liability @tab Income @tab Expense -@item @strong{Accounting} @tab debit @tab credit @tab credit @tab debit -@item @strong{Ledger} @tab positive @tab negative @tab negative @tab positive -@end multitable - -That's correct: accountants call an addition to their cash a debit! -However, from the bank's perspective it is a credit: the accountant's -cash is a liability for the bank. Consequently, payments to the -account will show up as credits on his bank statement. - -@node Using Emacs to Keep Your Ledger, Using GnuCash to Keep Your Ledger, Differences with accounting conventions, Keeping a ledger +@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, @file{ledger.el}. This module +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: @@ -2064,15 +759,15 @@ users. I recommend putting this at the top of your ledger file: ; -*-ledger-*- @end example -And this in your @file{.emacs} file, after copying @file{ledger.el} to your +And this in your @samp{.emacs} file, after copying @samp{ledger.el} to your site-lisp directory: @example (load "ledger") @end example -Now when you edit your ledger file, it will be in @command{ledger-mode}. -@command{ledger-mode} adds the following commands: +Now when you edit your ledger file, it will be in @samp{ledger-mode}. +@samp{ledger-mode} adds the following commands: @table @strong @item C-c C-a @@ -2196,31 +891,458 @@ ledger, with the attached prefix ``Billable'': Receivable:ClientOne @end example -@node Extending with Python, , Keeping a ledger, Top -@chapter Extending with Python +@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 + +@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 + +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. + +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 + +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. + +@node price, entry, equity, Command summary +@subsection 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. + +@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. -Ledger fully supports Python as an extension language. It may be used -in a few different forms, which fall into three basic categories: +@item @strong{-U} +Show only uncleared transactions. The default is to consider both. -@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 +@item @strong{-l AMT} +Limit balance reports to those which are greater than AMT. -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. +@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 -A quick example of how to use Python can be found by generating the -@command{prices} report using @file{pysample.dat}. See that file -itself for more details. +@c Page published by Emacs Muse ends here @contents @bye diff --git a/main.cc b/main.cc deleted file mode 100644 index 94440eaf..00000000 --- a/main.cc +++ /dev/null @@ -1,361 +0,0 @@ -#include <ledger.h> -#include "acconf.h" - -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_formatters(const std::string& command, - item_handler<transaction_t> * base_formatter, - 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, config.show_inverted)); - - // 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. - // - // interval_transactions is like subtotal_transactions, but it - // subtotals according to time intervals rather than totalling - // everything. - // - // dow_transactions is like interval_transactions, except that it - // reports all the transactions that fall on each subsequent day - // of the week. - if (config.days_of_the_week) - ptrs.push_back(formatter = new dow_transactions(formatter)); - else if (config.show_subtotal) - ptrs.push_back(formatter = new subtotal_transactions(formatter)); - - if (! config.report_interval.empty()) { - ptrs.push_back(formatter = - new interval_transactions(formatter, - config.report_interval)); - ptrs.push_back(formatter = new sort_transactions(formatter, "d")); - } - } - - // 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)); - - return formatter; -} - -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)); - - for (history_map::iterator j = (*i).second->history.begin(); - j != (*i).second->history.end(); - j++) { - entry_temps.back().date = (*j).first; - - xact_temps.push_back(transaction_t(&acct_temps.back())); - xact_temps.back().entry = &entry_temps.back(); - xact_temps.back().amount = (*j).second; - - handler(xact_temps.back()); - } - } -} - -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(); - - 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("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 - - if (config.data_file == config.cache_file) - config.use_cache = false; - - // 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 == "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 READ_GNUCASH - 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 READ_GNUCASH - register_parser(gnucash_parser.get()); -#endif - register_parser(qif_parser.get()); - register_parser(text_parser.get()); - - parse_ledger_data(journal.get(), text_parser.get(), bin_parser.get()); - - // 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; - - // 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") - formatter = new format_entries(*out, *format); - else - formatter = new format_transactions(*out, *format); - - formatter = chain_formatters(command, formatter, 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); - - 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 (xdata.dflags & ACCOUNT_TO_DISPLAY) { - *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 - - // 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 deleted file mode 100755 index b5f52db0..00000000 --- a/main.py +++ /dev/null @@ -1,398 +0,0 @@ -#!/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 () - -# 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.environ["LEDGER"]) -if os.environ.has_key ("PRICE_HIST"): - process_option ("price-db", os.environ["PRICE_HIST"]) -if os.environ.has_key ("PRICE_EXP"): - process_option ("price-exp", os.environ["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" -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, text_parser, 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 -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": - handler = FormatEntries(format) -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, config.show_inverted) - - 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.days_of_the_week: - handler = DowTransactions(handler) - elif config.show_subtotal: - handler = SubtotalTransactions(handler) - - if config.report_interval: - handler = IntervalTransactions(handler, config.report_interval) - handler = SortTransactions(handler, "d") - -# The next two transaction filters are used by all reports. - -if config.show_related: - handler = RelatedTransactions(handler, config.show_all_related) - -if config.predicate: - handler = FilterTransactions(handler, config.predicate) - -# 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 1: - if command == "e": - walk_transactions (new_entry, handler) - else: - walk_entries (journal, handler) -else: - if command == "e": - for xact in new_entry: - handler (xact) - else: - for entry in journal: - for xact in entry: - handler (xact) - -# 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 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! - -clear_transactions_xdata () -clear_accounts_xdata () diff --git a/option.cc b/option.cc deleted file mode 100644 index b04865bc..00000000 --- a/option.cc +++ /dev/null @@ -1,257 +0,0 @@ -#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) -{ - DEBUG_PRINT("ledger.memory.ctors", "ctor option_handler"); - - 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_back(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) { - 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) { - 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 deleted file mode 100644 index 2e4d7599..00000000 --- a/option.h +++ /dev/null @@ -1,44 +0,0 @@ -#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 new file mode 100644 index 00000000..e4850973 --- /dev/null +++ b/parse.cc @@ -0,0 +1,582 @@ +#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 deleted file mode 100644 index bbedaa7d..00000000 --- a/parser.cc +++ /dev/null @@ -1,117 +0,0 @@ -#include "parser.h" -#include "journal.h" - -#include <fstream> -#include <deque> - -namespace ledger { - -typedef std::deque<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) - return 0; - - 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 deleted file mode 100644 index 515bf09b..00000000 --- a/parser.h +++ /dev/null @@ -1,40 +0,0 @@ -#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/pyledger.cc b/pyledger.cc deleted file mode 100644 index dc65a4a3..00000000 --- a/pyledger.cc +++ /dev/null @@ -1,9 +0,0 @@ -#include <boost/python.hpp> - -using namespace boost::python; - -void initialize_ledger_for_python(); - -BOOST_PYTHON_MODULE(ledger) { - initialize_ledger_for_python(); -} diff --git a/pysample.dat b/pysample.dat deleted file mode 100644 index feac582e..00000000 --- a/pysample.dat +++ /dev/null @@ -1,36 +0,0 @@ -!python -# These functions maintain the minimum and maximum of the values -# passed to them. This is used to maintain the current min/max in the -# prices report. - -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 -!end - -; Change the 'prices' report format to show min/max values ---prices-format %[%Y/%m/%d %H:%M:%S %Z] %-8N %10t %10('vmin'a) %10('vmax'a) %12T\n - -2004/05/01 Checking balance - Assets:Checking $500.00 - Equity:Opening Balances - -2004/05/29 Book Store - Expenses:Books $20.00 - Assets:Checking - -2004/05/29 Restaurant - Expenses:Food $50.00 - Liabilities:MasterCard diff --git a/python.cc b/python.cc deleted file mode 100644 index 78f98d5d..00000000 --- a/python.cc +++ /dev/null @@ -1,81 +0,0 @@ -#include "python.h" -#include "journal.h" -#include "acconf.h" - -#include <boost/python.hpp> - -using namespace boost::python; - -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 READ_GNUCASH -void export_gnucash(); -#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 { - -python_support * python_interpretor = NULL; - -static struct cleanup_python { - ~cleanup_python() { - if (python_initialized) - Py_Finalize(); - if (python_interpretor) - delete python_interpretor; - } -} _cleanup; - -void init_python() -{ - if (! module_initialized) { - Py_Initialize(); - python_initialized = true; - detail::init_module("ledger", &initialize_ledger_for_python); - } - - python_interpretor = new python_support; -} - -} // namespace ledger diff --git a/python.h b/python.h deleted file mode 100644 index c560170b..00000000 --- a/python.h +++ /dev/null @@ -1,58 +0,0 @@ -#ifndef _PYTHON_H -#define _PYTHON_H - -#include "error.h" - -#include <boost/python.hpp> - -using namespace boost::python; - -namespace ledger { - -struct python_support -{ - handle<> main_module; - dict main_namespace; - - python_support() - : main_module(borrowed(PyImport_AddModule("__main__"))), - main_namespace(handle<>(borrowed(PyModule_GetDict(main_module.get())))) - {} - ~python_support() { - } -}; - -extern python_support * python_interpretor; - -void init_python(); - -inline void python_eval(std::istream& in) -{ - if (! python_interpretor) - init_python(); - - std::string buffer; - buffer.reserve(4096); - while (! in.eof()) { - char buf[256]; - in.getline(buf, 255); - if (buf[0] == '!') - break; - buffer += buf; - buffer += "\n"; - } - - try { - handle<>(borrowed(PyRun_String(buffer.c_str(), Py_file_input, - python_interpretor->main_namespace.ptr(), - python_interpretor->main_namespace.ptr()))); - } - catch(const boost::python::error_already_set&) { - PyErr_Print(); - throw error("Evaluating Python code"); - } -} - -} // namespace ledger - -#endif // _PYTHON_H diff --git a/qif.cc b/qif.cc deleted file mode 100644 index c8b8aa22..00000000 --- a/qif.cc +++ /dev/null @@ -1,201 +0,0 @@ -#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); - - 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.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 @@ -1,21 +0,0 @@ -#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 deleted file mode 100644 index 84f1dffc..00000000 --- a/quotes.cc +++ /dev/null @@ -1,74 +0,0 @@ -#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); - DEBUG_PRINT_TIME_(commodity.last_lookup); - DEBUG_PRINT_("pricing_leeway is " << pricing_leeway); - - if (std::difftime(now, commodity.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.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 deleted file mode 100644 index 53008aa0..00000000 --- a/quotes.h +++ /dev/null @@ -1,30 +0,0 @@ -#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 new file mode 100644 index 00000000..2708c1d0 --- /dev/null +++ b/reports.cc @@ -0,0 +1,1225 @@ +#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. diff --git a/scripts/bal b/scripts/bal index ca79b8ad..7fdf54ca 100755..100644 --- a/scripts/bal +++ b/scripts/bal @@ -1,23 +1,27 @@ #!/bin/sh -switch="-c" -limit="-d /^Liabilities/?T<0:(T&AT>{\$100.00})" +switch="" +current="-c" +limit="-l \$50" +negonly="-N ^Liabilities" -if [ "$1" = "-C" -o "$1" = "-U" ]; then +if [ "$1" = "-C" -o "$1" = "-U" -o "$1" = "-P" ]; then switch="$1" shift elif [ "$1" = "-b" -o "$1" = "-e" ]; then - switch="$1 $2" + current="$1 $2" shift 2 fi accts="$@" if [ -z "$accts" ]; then accts="-Equity -Income -Expenses" - accts="$accts -^Retirement" - accts="$accts -Deposits" + if [ ! "$switch" = "-P" ]; then + accts="$accts -Savings -Retirement" + fi else limit="" + negonly="" fi -ledger $switch $limit -s -S "-AT" -V -Q balance $accts +ledger $current $limit $negonly -s $switch balance $accts diff --git a/scripts/confirm.py b/scripts/confirm.py deleted file mode 100755 index 1132ca0c..00000000 --- a/scripts/confirm.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/python - -# This script confirms what ledger tells you. - -import sys -import os -import re - -def clean(num): - return float(re.sub("(\s+|\$|,)","",num)) - -running_total = 0.0 -index = 1 -last_line = "" - -for line in os.popen("../ledger %s reg %s" % (sys.argv[1], sys.argv[2])): - value = clean(line[55:67]) - total = clean(line[68:]) - - running_total += value - if abs(running_total - total) > 0.001: - print "! discrepancy of %.2f (%.2f - %.2f) at line %d:" % \ - (running_total - total, running_total, total, index) - print line, - running_total = total - - index += 1 - last_line = line - -balance_total = 0.0 - -for line in os.popen("../ledger %s bal %s" % (sys.argv[1], sys.argv[2])): - balance_total = clean(line[:20]) - -if abs(balance_total - running_total) > 0.001: - print - print "! discrepancy of %.2f (%.2f - %.2f) between register and balance" % \ - (balance_total - running_total, balance_total, running_total) - print last_line, - print line, diff --git a/scripts/entry b/scripts/entry index 28daf8c8..28daf8c8 100755..100644 --- a/scripts/entry +++ b/scripts/entry diff --git a/scripts/getquote b/scripts/getquote index 8daf6633..cf8c8abd 100755..100644 --- a/scripts/getquote +++ b/scripts/getquote @@ -1,14 +1,16 @@ #!/usr/bin/perl +exit 0 if $ARGV[0] eq "\$"; + use Finance::Quote; $q = Finance::Quote->new; + $q->timeout(60); $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 index 3c6f779a..3c6f779a 100755..100644 --- a/scripts/mean +++ b/scripts/mean diff --git a/scripts/profit b/scripts/profit index 26f112d1..26f112d1 100755..100644 --- a/scripts/profit +++ b/scripts/profit diff --git a/scripts/reg b/scripts/reg index 75ac364e..75ac364e 100755..100644 --- a/scripts/reg +++ b/scripts/reg diff --git a/scripts/report b/scripts/report index c5dbe171..aa42982e 100755..100644 --- a/scripts/report +++ b/scripts/report @@ -1,15 +1,17 @@ #!/bin/sh +dir=$HOME/doc/finance + cd /tmp -ledger "$@" > report + +ledger -M -G register "$@" > $1 gnuplot <<EOF set terminal png set output "report.png" set xdata time set timefmt "%Y/%m/%d" -plot "report" using 1:2 with linespoints +plot "$1" using 1:2 with linespoints EOF -rm -f report open report.png diff --git a/scripts/spending b/scripts/spending index 895c4170..895c4170 100755..100644 --- a/scripts/spending +++ b/scripts/spending diff --git a/scripts/test b/scripts/test deleted file mode 100755 index 09b0bb03..00000000 --- a/scripts/test +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh - -for test in \ - "-O nrl:checking" \ - "-B 401" \ - "-V 401" \ - "-G 401" \ - "-B retire" \ - "-V retire" \ - "-G retire" -do - echo testing: $test - python confirm.py $test -done diff --git a/scripts/worth b/scripts/worth index 58b20440..58b20440 100755..100644 --- a/scripts/worth +++ b/scripts/worth diff --git a/setup.py b/setup.py deleted file mode 100755 index 874c8cca..00000000 --- a/setup.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env python - -from distutils.core import setup, Extension - -import os - -libs = ["ledger", "boost_python", "gmp", "pcre"] - -if os.environ.has_key ("READ_GNUCASH") and\ - os.environ["READ_GNUCASH"] == "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 deleted file mode 100644 index aea860e2..00000000 --- a/textual.cc +++ /dev/null @@ -1,550 +0,0 @@ -#include "journal.h" -#include "textual.h" -#include "datetime.h" -#include "autoxact.h" -#include "valexpr.h" -#include "error.h" -#include "option.h" -#include "config.h" -#include "timing.h" -#include "util.h" -#ifdef USE_BOOST_PYTHON -#include "python.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; -} - -transaction_t * parse_transaction_text(char * line, account_t * account, - entry_t * entry) -{ - // 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++; - } - xact->cost = new amount_t; - xact->cost->parse(price_str); - } - - xact->amount.parse(cost_str); - - if (price_str && per_unit) - *xact->cost *= xact->amount; - } - } - - 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(); -} - -transaction_t * parse_transaction(std::istream& in, account_t * account, - entry_t * entry) -{ - static char line[MAX_LINE + 1]; - in.getline(line, MAX_LINE); - linenum++; - - // Skip a possible blank line - if (*skip_ws(line) == '\0') - return NULL; - - return parse_transaction_text(line, account, entry); -} - -void parse_automated_transactions(std::istream& in, - account_t * account, - automated_transactions_t& auto_xacts) -{ - static char line[MAX_LINE + 1]; - in.getline(line, MAX_LINE); - linenum++; - - transactions_deque xacts; - - while (! in.eof() && (in.peek() == ' ' || in.peek() == '\t')) - if (transaction_t * xact = parse_transaction(in, account, NULL)) { - if (! xact->amount) - throw parse_error(path, linenum, - "All automated transactions must have values"); - else - xacts.push_back(xact); - } - - if (! xacts.empty()) - auto_xacts. - add_automated_transaction(new automated_transaction_t(line + 1, xacts)); -} - -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, account_t * master, - textual_parser_t& parser) -{ - std::auto_ptr<entry_t> curr(new entry_t); - - static char line[MAX_LINE + 1]; - in.getline(line, MAX_LINE); - linenum++; - - // 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 == '*') { - curr->state = entry_t::CLEARED; - 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->payee = next; - - 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')) - if (transaction_t * xact = parse_transaction(in, master, curr.get())) - 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; } -}; - -unsigned int textual_parser_t::parse(std::istream& in, - journal_t * journal, - account_t * master, - const std::string * original_file) -{ - static bool added_autoxact_hook = false; - static char line[MAX_LINE + 1]; - char c; - unsigned int count = 0; - unsigned int errors = 0; - commodity_t * time_commodity = NULL; - - std::deque<account_t *> account_stack; - autoxact_finalizer_t autoxact_finalizer; - - if (! master) - master = journal->master; - - account_stack.push_front(master); - - path = journal->sources.back(); - linenum = 1; - - while (! in.eof()) { - try { - switch (in.peek()) { - case -1: // end of file - goto done; - - case ' ': - case '\t': - if (peek_next_nonws(in) != '\n') { - in.getline(line, MAX_LINE); - linenum++; - throw parse_error(path, linenum - 1, "Line begins with whitespace"); - } - // fall through... - - 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 : ""; - - 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, time; - - in >> c; - in >> date; - in >> time; - date += " "; - date += time; - - in.getline(line, MAX_LINE); - linenum++; - - 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) / 60.0 / 60.0; - char buf[32]; - std::sprintf(buf, "%fh", diff); - amount_t amt; - amt.parse(buf); - time_commodity = &amt.commodity(); - - transaction_t * xact - = new transaction_t(last_account, amt, TRANSACTION_VIRTUAL); - curr->add_transaction(xact); - - if (! journal->add_entry(curr.release())) - throw parse_error(path, linenum, - "Failed to record 'out' timelog entry"); - - count++; - } else { - throw parse_error(path, linenum, "Cannot parse timelog entry date"); - } - - last_account = NULL; - } else { - in.getline(line, MAX_LINE); - linenum++; - } - break; -#endif // TIMELOG_SUPPORT - - case 'P': { // a pricing entry - in >> c; - - std::time_t date; - std::string symbol; - - in >> line; // the date - if (! quick_parse_date(line, &date)) - throw parse_error(path, linenum, "Failed to parse date"); - - 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); - - parse_commodity(in, symbol); - - in.getline(line, MAX_LINE); - linenum++; - - amount_t price; - price.parse(skip_ws(line)); - - commodity_t * commodity = commodity_t::find_commodity(symbol, true); - commodity->add_price(date, price); - break; - } - - case 'N': { // don't download prices - std::string symbol; - - in >> c; - parse_commodity(in, symbol); - - commodity_t * commodity = commodity_t::find_commodity(symbol, true); - commodity->flags |= COMMODITY_STYLE_NOMARKET; - break; - } - - case 'C': { // a flat conversion - in >> c; - - std::string symbol; - amount_t price; - - parse_commodity(in, symbol); - - in.getline(line, MAX_LINE); - linenum++; - price.parse(skip_ws(line)); - - commodity_t * commodity = commodity_t::find_commodity(symbol, true); - commodity->conversion = price; - break; - } - - case 'Y': // set the current year - in >> c; - in >> now_year; - now_year -= 1900; - break; - -#ifdef TIMELOG_SUPPORT - case 'h': - case 'b': -#endif - case ';': // a comment line - in.getline(line, MAX_LINE); - linenum++; - break; - - case '-': { // option setting - std::string opt; - in >> c >> c; - in >> opt; - in.getline(line, MAX_LINE); - linenum++; - char * p = skip_ws(line); - process_option(config_options, opt, *p == '\n' ? NULL : p); - break; - } - - case '=': // automated transactions - if (! added_autoxact_hook) { - journal->add_entry_finalizer(&autoxact_finalizer); - added_autoxact_hook = true; - } - parse_automated_transactions(in, account_stack.front(), - autoxact_finalizer.auto_xacts); - break; - - case '!': { // directive - std::string word; - in.get(c); - in >> word; - if (word == "include") { - in.getline(line, MAX_LINE); - linenum++; - - push_var<unsigned int> save_linenum(linenum); - push_var<std::string> save_path(path); - - count += parse_journal_file(skip_ws(line), journal, - account_stack.front()); - } - else if (word == "account") { - in.getline(line, MAX_LINE); - linenum++; - - account_t * acct; - acct = account_stack.front()->find_account(skip_ws(line)); - account_stack.push_front(acct); - } - else if (word == "end") { - account_stack.pop_front(); - } -#ifdef USE_BOOST_PYTHON - else if (word == "python") { - in.getline(line, MAX_LINE); - python_eval(in); - } -#endif - break; - } - - default: { - unsigned int first_line = linenum; - if (entry_t * entry = parse_entry(in, account_stack.front(), *this)) { - if (journal->add_entry(entry)) - count++; - else - 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_autoxact_hook) - journal->remove_entry_finalizer(&autoxact_finalizer); - - if (time_commodity) { - time_commodity->precision = 2; - time_commodity->flags |= COMMODITY_STYLE_NOMARKET; - } - - 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 deleted file mode 100644 index 5fe17dd8..00000000 --- a/textual.h +++ /dev/null @@ -1,23 +0,0 @@ -#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); -}; - -} // namespace ledger - -#endif // _TEXTUAL_H diff --git a/timing.h b/timing.h deleted file mode 100644 index bc7fdfe2..00000000 --- a/timing.h +++ /dev/null @@ -1,54 +0,0 @@ -#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 diff --git a/util.h b/util.h deleted file mode 100644 index 9fe115cc..00000000 --- a/util.h +++ /dev/null @@ -1,37 +0,0 @@ -#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() && (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 deleted file mode 100644 index dce9dfd9..00000000 --- a/valexpr.cc +++ /dev/null @@ -1,1051 +0,0 @@ -#include "valexpr.h" -#include "walk.h" -#include "format.h" -#include "error.h" -#include "datetime.h" -#include "debug.h" -#include "util.h" -#ifdef USE_BOOST_PYTHON -#include "python.h" -#endif - -#include <pcre.h> - -namespace ledger { - -mask_t::mask_t(const std::string& pat) : exclude(false) -{ - DEBUG_PRINT("ledger.memory.ctors", "ctor mask_t"); - - 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; - - DEBUG_PRINT("valexpr.mask.parse", "pattern = '" << pattern << "'"); - - 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_t::mask_t(const mask_t& m) : exclude(m.exclude), pattern(m.pattern) -{ - DEBUG_PRINT("ledger.memory.ctors", "ctor mask_t"); - - const char *error; - int erroffset; - regexp = pcre_compile(pattern.c_str(), PCRE_CASELESS, - &error, &erroffset, NULL); - assert(regexp); -} - -mask_t::~mask_t() { - DEBUG_PRINT("ledger.memory.dtors", "dtor 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; -} - - -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 = (unsigned int) constant_t; - break; - - case CONSTANT_A: - result = constant_a; - break; - - case AMOUNT: - if (details.xact) - result = details.xact->amount; - else if (details.account && account_has_xdata(*details.account)) - result = account_xdata(*details.account).value; - break; - - case COST: - if (details.xact) { - 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(); - } - 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; - 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(); - break; - - case VALUE_EXPR: - assert(format_t::amount_expr); - format_t::amount_expr->compute(result, details); - break; - case TOTAL_EXPR: - assert(format_t::total_expr); - format_t::total_expr->compute(result, details); - break; - - case DATE: - if (details.entry) - result = (unsigned int) details.entry->date; - else - result = (unsigned int) now; - break; - - case CLEARED: - if (details.entry) { - result = details.entry->state == entry_t::CLEARED; - } - else if (details.account) { - bool all_clear = true; - for (transactions_list::const_iterator i - = details.account->transactions.begin(); - i != details.account->transactions.end(); - i++) - if ((*i)->entry->state != entry_t::CLEARED) { - all_clear = false; - break; - } - result = all_clear; - } - break; - - case REAL: - if (details.xact) { - result = ! (details.xact->flags & TRANSACTION_VIRTUAL); - } - else if (details.account) { - bool all_real = true; - for (transactions_list::const_iterator i - = details.account->transactions.begin(); - i != details.account->transactions.end(); - i++) - if ((*i)->flags & TRANSACTION_VIRTUAL) { - all_real = false; - break; - } - result = all_real; - } - break; - - case INDEX: - if (details.xact && transaction_has_xdata(*details.xact)) - result = transaction_xdata(*details.xact).index + 1; - else if (details.account && account_has_xdata(*details.account)) - result = account_xdata(*details.account).subcount; - break; - - case COUNT: - if (details.xact && transaction_has_xdata(*details.xact)) - result = transaction_xdata(*details.xact).index + 1; - else if (details.account && account_has_xdata(*details.account)) - result = account_xdata(*details.account).count; - break; - - case DEPTH: - if (details.account) - result = (unsigned int) details.account->depth; - else - result = 0U; - break; - - case F_ARITH_MEAN: - if (details.xact && transaction_has_xdata(*details.xact)) { - assert(left); - left->compute(result, details); - result /= amount_t(transaction_xdata(*details.xact).index + 1); - } - else if (details.account && account_has_xdata(*details.account) && - account_xdata(*details.account).count) { - assert(left); - left->compute(result, details); - result /= amount_t(account_xdata(*details.account).count); - } - 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); - if (result.type == value_t::BALANCE || - result.type == value_t::BALANCE_PAIR) { - // jww (2004-08-17): do something smarter here? - result.cast(value_t::AMOUNT); - } - if (result.type == value_t::AMOUNT) - ((amount_t *) result.data)->clear_commodity(); - break; - } - - case F_PAYEE_MASK: - assert(mask); - if (details.entry) - result = mask->match(details.entry->payee); - break; - - case F_ACCOUNT_MASK: - assert(mask); - if (details.account) - result = mask->match(details.account->fullname()); - break; - - case F_SHORT_ACCOUNT_MASK: - assert(mask); - if (details.account) - result = mask->match(details.account->name); - break; - - case F_VALUE: { - assert(left); - left->compute(result, details); - - std::time_t moment = now; - if (right) { - switch (right->kind) { - case DATE: - 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)"); - } - } - - switch (result.type) { - case value_t::BOOLEAN: - case value_t::INTEGER: - break; - case value_t::AMOUNT: - result = ((amount_t *)result.data)->value(moment); - break; - case value_t::BALANCE: - result = ((balance_t *)result.data)->value(moment); - break; - case value_t::BALANCE_PAIR: - result = ((balance_pair_t *)result.data)->quantity.value(moment); - break; - } - break; - } - - case F_INTERP_FUNC: { -#ifdef USE_BOOST_PYTHON - if (! python_interpretor) - init_python(); - - try { - object func = python_interpretor->main_namespace[constant_s]; - if (right) { - if (right->right) { - list args; - args.append(details); - for (value_expr_t * arg = right; arg; arg = arg->right) { - assert(arg->kind == O_ARG); - arg->left->compute(result, details); - args.append(result); - } - - PyObject * val = PyObject_CallObject(func.ptr(), tuple(args).ptr()); - if (val) { - result = extract<value_t>(val)(); - Py_DECREF(val); - } - else if (PyObject * err = PyErr_Occurred()) { - PyErr_Print(); - throw value_expr_error(std::string("While calling Python function '") + - constant_s + "'"); - } - else { - result = 0; - } - } else { - assert(right->kind == O_ARG); - right->left->compute(result, details); - result = call<value_t>(func.ptr(), details, result); - } - } else { - result = call<value_t>(func.ptr(), details); - } - } - catch(const boost::python::error_already_set&) { - PyErr_Print(); - throw value_expr_error(std::string("While calling Python function '") + - constant_s + "'"); - } -#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 (c == -1) { - 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 'c': 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 '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 'C': 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-c")); break; - case 'G': node.reset(parse_value_expr("V-C")); break; - - // Functions - case '-': - node.reset(new value_expr_t(value_expr_t::F_NEG)); - node->left = parse_value_term(in); - break; - - case 'A': - 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 'M': - 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, '\''); - - in.get(c); - node.reset(new value_expr_t(value_expr_t::F_INTERP_FUNC)); - node->constant_s = buf; - in.get(c); - if (! in.eof()) { - if (c == '(') { - 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, ')'); - } else { - in.unget(); - node->right = new value_expr_t(value_expr_t::O_ARG); - node->right->left = parse_value_term(in); - } - } - 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::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); -} - -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()) - ; -} - -#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 deleted file mode 100644 index 061c354d..00000000 --- a/valexpr.h +++ /dev/null @@ -1,186 +0,0 @@ -#ifndef _EXPR_H -#define _EXPR_H - -#include "journal.h" -#include "value.h" -#include "error.h" - -namespace ledger { - -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; -}; - -////////////////////////////////////////////////////////////////////// - -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) {} - details_t(const transaction_t& _xact) - : entry(_xact.entry), xact(&_xact), account(_xact.account) {} - details_t(const account_t& _account) - : entry(NULL), xact(NULL), account(&_account) {} -}; - -struct value_expr_t -{ - enum kind_t { - // Constants - CONSTANT_I, - CONSTANT_T, - CONSTANT_A, - - // Item details - AMOUNT, - COST, - DATE, - CLEARED, - REAL, - INDEX, - DEPTH, - - // Item totals - COUNT, - TOTAL, - COST_TOTAL, - - // Relating to format_t - VALUE_EXPR, - TOTAL_EXPR, - - // Functions - 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; - unsigned int 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; -}; - -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; - - public: - item_predicate(const std::string& _predicate) : predicate(NULL) { - DEBUG_PRINT("ledger.memory.ctors", "ctor item_predicate<T>"); - if (! _predicate.empty()) { - try { - predicate = parse_value_expr(_predicate); - } - catch (value_expr_error& err) { - throw value_expr_error(std::string("In predicate '") + - _predicate + "': " + err.what()); - } - } - } - item_predicate(const value_expr_t * _predicate) - : predicate(_predicate) { - DEBUG_PRINT("ledger.memory.ctors", "ctor item_predicate<T>"); - } - - ~item_predicate() { - DEBUG_PRINT("ledger.memory.dtors", "dtor item_predicate<T>"); - if (predicate) - 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 report - -#endif // _REPORT_H diff --git a/value.cc b/value.cc deleted file mode 100644 index 431dca19..00000000 --- a/value.cc +++ /dev/null @@ -1,787 +0,0 @@ -#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: - *((unsigned int *) data) = *((unsigned int *) 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_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: \ - *((unsigned int *) data) OP (*((bool *) value.data) ? 1U : 0U); \ - break; \ - \ - case INTEGER: \ - *((unsigned int *) data) OP *((unsigned int *) 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) ? 1U : 0U); \ - break; \ - \ - case INTEGER: \ - *((amount_t *) data) OP *((unsigned int *) 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) ? 1U : 0U); \ - break; \ - \ - case INTEGER: \ - *((balance_t *) data) OP *((unsigned int *) 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) ? 1U : 0U); \ - break; \ - \ - case INTEGER: \ - *((balance_pair_t *) data) OP *((unsigned int *) 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_OP(+=) -DEF_VALUE_OP(-=) -DEF_VALUE_OP(*=) -DEF_VALUE_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(*((unsigned int *) 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 (*((unsigned int *) data) OP \ - ((unsigned int) *((bool *) value.data))); \ - \ - case INTEGER: \ - return (*((unsigned int *) data) OP \ - *((unsigned int *) value.data)); \ - \ - case AMOUNT: \ - return (amount_t(*((unsigned int *) data)) OP \ - *((amount_t *) value.data)); \ - \ - case BALANCE: \ - return (balance_t(*((unsigned int *) data)) OP \ - *((balance_t *) value.data)); \ - \ - case BALANCE_PAIR: \ - return (balance_pair_t(*((unsigned int *) 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(*((unsigned int *) 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 (unsigned int)*((bool *) value.data); \ - \ - case INTEGER: \ - return *((balance_t *) data) OP *((unsigned int *) 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 \ - (unsigned int)*((bool *) value.data)); \ - \ - case INTEGER: \ - return (((balance_pair_t *) data)->quantity OP \ - *((unsigned int *) 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(>=) - -void value_t::cast(type_t cast_type) -{ - switch (type) { - case BOOLEAN: - switch (cast_type) { - case BOOLEAN: - break; - case INTEGER: - *((unsigned int *) 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) = *((unsigned int *) data); - break; - case INTEGER: - break; - case AMOUNT: - new((amount_t *)data) amount_t(*((unsigned int *) data)); - break; - case BALANCE: - new((balance_t *)data) balance_t(*((unsigned int *) data)); - break; - case BALANCE_PAIR: - new((balance_pair_t *)data) balance_pair_t(*((unsigned int *) 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: { - unsigned int temp = *((amount_t *) data); - destroy(); - *((unsigned int *)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 { - 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 { - 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: - *((unsigned int *) data) = - *((unsigned int *) 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 (*((unsigned int *) data) < 0) - *((unsigned int *) data) = - *((unsigned int *) 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(); -} - -} // namespace ledger - -#ifdef USE_BOOST_PYTHON - -#include <boost/python.hpp> - -using namespace boost::python; -using namespace ledger; - -unsigned int balance_len(balance_t& bal); -amount_t balance_getitem(balance_t& bal, int i); -unsigned int balance_pair_len(balance_pair_t& bal_pair); -amount_t balance_pair_getitem(balance_pair_t& bal_pair, int i); - -unsigned int 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: - case value_t::AMOUNT: - return value; - - 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 0; -} - -void export_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<unsigned int>()) - .def(init<bool>()) - - .def(self + self) - .def(self + other<balance_pair_t>()) - .def(self + other<balance_t>()) - .def(self + other<amount_t>()) - .def(self + int()) - - .def(self - self) - .def(self - other<balance_pair_t>()) - .def(self - other<balance_t>()) - .def(self - other<amount_t>()) - .def(self - int()) - - .def(self * self) - .def(self * other<balance_pair_t>()) - .def(self * other<balance_t>()) - .def(self * other<amount_t>()) - .def(self * int()) - - .def(self / self) - .def(self / other<balance_pair_t>()) - .def(self / other<balance_t>()) - .def(self / other<amount_t>()) - .def(self / int()) - - .def(other<balance_pair_t>() + self) - .def(other<balance_t>() + self) - .def(other<amount_t>() + self) - .def(int() + self) - - .def(other<balance_pair_t>() - self) - .def(other<balance_t>() - self) - .def(other<amount_t>() - self) - .def(int() - self) - - .def(other<balance_pair_t>() * self) - .def(other<balance_t>() * self) - .def(other<amount_t>() * self) - .def(int() * self) - - .def(other<balance_pair_t>() / self) - .def(other<balance_t>() / self) - .def(other<amount_t>() / self) - .def(int() / self) - - .def(- self) - - .def(self += self) - .def(self += other<balance_pair_t>()) - .def(self += other<balance_t>()) - .def(self += other<amount_t>()) - .def(self += int()) - - .def(self -= self) - .def(self -= other<balance_pair_t>()) - .def(self -= other<balance_t>()) - .def(self -= other<amount_t>()) - .def(self -= int()) - - .def(self *= self) - .def(self *= other<balance_pair_t>()) - .def(self *= other<balance_t>()) - .def(self *= other<amount_t>()) - .def(self *= int()) - - .def(self /= self) - .def(self /= other<balance_pair_t>()) - .def(self /= other<balance_t>()) - .def(self /= other<amount_t>()) - .def(self /= int()) - - .def(self < self) - .def(self < other<balance_pair_t>()) - .def(self < other<balance_t>()) - .def(self < other<amount_t>()) - .def(self < int()) - - .def(self <= self) - .def(self <= other<balance_pair_t>()) - .def(self <= other<balance_t>()) - .def(self <= other<amount_t>()) - .def(self <= int()) - - .def(self > self) - .def(self > other<balance_pair_t>()) - .def(self > other<balance_t>()) - .def(self > other<amount_t>()) - .def(self > int()) - - .def(self >= self) - .def(self >= other<balance_pair_t>()) - .def(self >= other<balance_t>()) - .def(self >= other<amount_t>()) - .def(self >= int()) - - .def(self == self) - .def(self == other<balance_pair_t>()) - .def(self == other<balance_t>()) - .def(self == other<amount_t>()) - .def(self == int()) - - .def(self != self) - .def(self != other<balance_pair_t>()) - .def(self != other<balance_t>()) - .def(self != other<amount_t>()) - .def(self != int()) - - .def(! self) - .def(int_(self)) - - .def(abs(self)) - .def(self_ns::str(self)) - - .def("__len__", value_len) - .def("__getitem__", value_getitem) - - .def("cast", &value_t::cast) - .def("negate", &value_t::negate) - .def("cost", &value_t::cost) - ; -} - -#endif // USE_BOOST_PYTHON diff --git a/value.h b/value.h deleted file mode 100644 index 61e20eaa..00000000 --- a/value.h +++ /dev/null @@ -1,383 +0,0 @@ -#ifndef _VALUE_H -#define _VALUE_H - -#include "amount.h" -#include "balance.h" - -#include <exception> - -namespace ledger { - -// 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() { - *((unsigned int *) 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 unsigned int value) { - *((unsigned int *) data) = value; - type = INTEGER; - } - value_t(const int value) { - *((unsigned int *) data) = (unsigned int) value; - type = INTEGER; - } - 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 unsigned int value) { - if ((unsigned int *) data != &value) { - destroy(); - *((unsigned int *) data) = value; - type = INTEGER; - } - return *this; - } - value_t& operator=(const int value) { - return *this = (unsigned int) 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 = 0U; - } 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 value_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; -}; - -inline value_t operator+(const balance_pair_t& value, const value_t& obj) { - return value_t(value) + obj; -} -inline value_t operator+(const balance_t& value, const value_t& obj) { - return value_t(value) + obj; -} -inline value_t operator+(const amount_t& value, const value_t& obj) { - return value_t(value) + obj; -} -template <typename T> -inline value_t operator+(T value, const value_t& obj) { - return value_t(value) + obj; -} - -inline value_t operator-(const balance_pair_t& value, const value_t& obj) { - return value_t(value) - obj; -} -inline value_t operator-(const balance_t& value, const value_t& obj) { - return value_t(value) - obj; -} -inline value_t operator-(const amount_t& value, const value_t& obj) { - return value_t(value) - obj; -} -template <typename T> -inline value_t operator-(T value, const value_t& obj) { - return value_t(value) - obj; -} - -inline value_t operator*(const balance_pair_t& value, const value_t& obj) { - return value_t(value) * obj; -} -inline value_t operator*(const balance_t& value, const value_t& obj) { - return value_t(value) * obj; -} -inline value_t operator*(const amount_t& value, const value_t& obj) { - return value_t(value) * obj; -} -template <typename T> -inline value_t operator*(T value, const value_t& obj) { - return value_t(value) * obj; -} - -inline value_t operator/(const balance_pair_t& value, const value_t& obj) { - return value_t(value) / obj; -} -inline value_t operator/(const balance_t& value, const value_t& obj) { - return value_t(value) / obj; -} -inline value_t operator/(const amount_t& value, const value_t& obj) { - return value_t(value) / obj; -} -template <typename T> -inline value_t operator/(T value, const value_t& obj) { - return value_t(value) / obj; -} - -template <typename T> -value_t::operator T() const -{ - switch (type) { - case BOOLEAN: - return *((bool *) data); - case INTEGER: - return *((unsigned int *) 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; -} - -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 << *((unsigned int *) 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; -} - -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(); - } -}; - -} // namespace ledger - -#endif // _VALUE_H diff --git a/walk.cc b/walk.cc deleted file mode 100644 index 2683aa2d..00000000 --- a/walk.cc +++ /dev/null @@ -1,662 +0,0 @@ -#include "walk.h" -#include "format.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; -} - -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; - (*handler)(**i); - } - - transactions.clear(); - - item_handler<transaction_t>::flush(); -} - -void calc_transactions::operator()(transaction_t& xact) -{ - if (last_xact && transaction_has_xdata(*last_xact)) { - transaction_xdata(xact).total += transaction_xdata(*last_xact).total; - transaction_xdata(xact).index = transaction_xdata(*last_xact).index + 1; - } else { - transaction_xdata(xact).index = 0; - } - - if (inverted) { - xact.amount.negate(); - if (xact.cost) - xact.cost->negate(); - } - - if (! (transaction_xdata(xact).dflags & TRANSACTION_NO_TOTAL)) - add_transaction_to(xact, transaction_xdata(xact).total); - - (*handler)(xact); - - if (inverted) { - xact.amount.negate(); - if (xact.cost) - xact.cost->negate(); - } - - last_xact = &xact; -} - - -static 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) -{ - balance_t * bal = NULL; - - switch (value.type) { - case value_t::BOOLEAN: - case value_t::INTEGER: - case value_t::AMOUNT: { - temps.push_back(transaction_t(account)); - transaction_t& xact = temps.back(); - - xact.entry = entry; - switch (value.type) { - case value_t::BOOLEAN: - xact.amount = *((bool *) value.data); - break; - case value_t::INTEGER: - xact.amount = *((unsigned int *) value.data); - break; - case value_t::AMOUNT: - xact.amount = *((amount_t *) value.data); - break; - default: - assert(0); - break; - } - - if (flags) - transaction_xdata(xact).dflags |= flags; - - (*handler)(xact); - break; - } - - case value_t::BALANCE_PAIR: - bal = &((balance_pair_t *) value.data)->quantity; - // fall through... - - case value_t::BALANCE: - if (! bal) - bal = (balance_t *) value.data; - - for (amounts_map::const_iterator i = bal->amounts.begin(); - i != bal->amounts.end(); - i++) { - temps.push_back(transaction_t(account)); - transaction_t& xact = temps.back(); - - xact.entry = entry; - xact.amount = (*i).second; - - if (flags) - transaction_xdata(xact).dflags |= flags; - - (*handler)(xact); - } - break; - - default: - assert(0); - break; - } -} - -static inline void determine_amount(value_t& result, balance_pair_t& bal_pair) -{ - account_xdata_t xdata; - xdata.value = bal_pair; - account_t temp_account; - temp_account.data = &xdata; - format_t::compute_amount(result, details_t(temp_account)); -} - -void collapse_transactions::report_cumulative_subtotal() -{ - if (count == 1) { - (*handler)(*last_xact); - } else { - assert(count > 1); - - value_t result; - determine_amount(result, subtotal); - handle_value(result, &totals_account, last_entry, 0, xact_temps, handler); - } - - last_entry = NULL; - last_xact = NULL; - subtotal = 0; - count = 0; -} - -void changed_value_transactions::output_diff(const std::time_t current) -{ - value_t prev_bal; - value_t cur_bal; - std::time_t prev_date = last_xact->entry->date; - - format_t::compute_total(prev_bal, details_t(*last_xact)); - - last_xact->entry->date = current; - format_t::compute_total(cur_bal, details_t(*last_xact)); - last_xact->entry->date = prev_date; - - cur_bal -= prev_bal; - if (cur_bal) { - entry_temps.push_back(entry_t()); - entry_t& entry = entry_temps.back(); - - entry.payee = "Commodities revalued"; - entry.date = current; - - handle_value(cur_bal, NULL, &entry, TRANSACTION_NO_TOTAL, xact_temps, - handler); - } -} - -void changed_value_transactions::operator()(transaction_t& xact) -{ - if (last_xact) - output_diff(xact.entry->date); - - if (changed_values_only) - transaction_xdata(xact).dflags |= TRANSACTION_DISPLAYED; - - (*handler)(xact); - - 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; - - // Make sure the end date is inclusive - if (start != finish) - finish -= 86400; - - 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; - - value_t result; - - for (balances_map::iterator i = balances.begin(); - i != balances.end(); - i++) { - entry.date = finish; - value_t result; - determine_amount(result, (*i).second); - entry.date = start; - - handle_value(result, (*i).first, &entry, 0, xact_temps, handler); - } - - balances.clear(); -} - -void subtotal_transactions::operator()(transaction_t& xact) -{ - if (balances.size() == 0) { - start = finish = xact.entry->date; - } else { - if (std::difftime(xact.entry->date, start) < 0) - start = xact.entry->date; - if (std::difftime(xact.entry->date, finish) > 0) - finish = xact.entry->date; - } - - balances_map::iterator i = balances.find(xact.account); - if (i == balances.end()) { - balance_pair_t temp; - add_transaction_to(xact, temp); - balances.insert(balances_pair(xact.account, temp)); - } else { - add_transaction_to(xact, (*i).second); - } -} - -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) { - std::time_t quant = interval.increment(interval.begin); - if (std::difftime(xact.entry->date, quant) >= 0) { - if (last_xact) { - start = interval.begin; - finish = quant; - report_subtotal(); - } - - if (! interval.seconds) { - struct std::tm * desc = std::localtime(&xact.entry->date); - if (interval.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; -#if DEBUG_LEVEL >= RELEASE - int cutoff = 10000; -#endif - while (std::difftime(xact.entry->date, - temp = interval.increment(quant)) > 0) { - if (quant == temp) - break; - quant = temp; -#if DEBUG_LEVEL >= RELEASE - assert(--cutoff > 0); -#endif - } - interval.begin = quant; - } - - subtotal_transactions::operator()(xact); - } else { - (*handler)(xact); - } - - last_xact = &xact; -} - -void dow_transactions::flush() -{ - for (int i = 0; i < 7; i++) { - 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 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; -} - -void sum_accounts(account_t& account) -{ - for (accounts_map::iterator i = account.accounts.begin(); - i != account.accounts.end(); - i++) { - sum_accounts(*(*i).second); - account_xdata(account).total += account_xdata(*(*i).second).total; - account_xdata(account).count += (account_xdata(*(*i).second).count + - account_xdata(*(*i).second).subcount); - } - account_xdata(account).total += account_xdata(account).value; - account_xdata(account).count += account_xdata(account).subcount; -} - -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; -} - -} // 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_) {} - - 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 export_walk() -{ - typedef item_handler<transaction_t> xact_handler_t; - - 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; - - class_< transaction_xdata_t > ("TransactionXData") - .def_readwrite("total", &transaction_xdata_t::total) - .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); - - class_< xact_handler_t, item_handler_wrap<transaction_t> > - ("TransactionHandler") - .def(init<xact_handler_t *>()) - - .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 *, optional<bool> >() - [with_custodian_and_ward<1, 2>()]) - .def("flush", &xact_handler_t::flush) - .def("__call__", &calc_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_< 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>() - [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__", &interval_transactions::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()); - ; - - 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()); - ; - - 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; - - class_< account_xdata_t > ("AccountXData") - .def_readwrite("value", &account_xdata_t::value) - .def_readwrite("total", &account_xdata_t::total) - .def_readwrite("count", &account_xdata_t::count) - .def_readwrite("subcount", &account_xdata_t::subcount) - .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 *>()) - - .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); -} - -#endif // USE_BOOST_PYTHON diff --git a/walk.h b/walk.h deleted file mode 100644 index 0ac9e133..00000000 --- a/walk.h +++ /dev/null @@ -1,485 +0,0 @@ -#ifndef _WALK_H -#define _WALK_H - -#include "journal.h" -#include "balance.h" -#include "valexpr.h" -#include "datetime.h" - -#include <iostream> -#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_HANDLED 0x0001 -#define TRANSACTION_TO_DISPLAY 0x0002 -#define TRANSACTION_DISPLAYED 0x0004 -#define TRANSACTION_NO_TOTAL 0x0008 -#define TRANSACTION_SORT_CALC 0x0010 - -struct transaction_xdata_t -{ - value_t total; - value_t sort_value; - unsigned int index; - unsigned short dflags; - - transaction_xdata_t() : index(0), dflags(0) { - DEBUG_PRINT("ledger.memory.ctors", "ctor transaction_xdata_t"); - } -#ifdef DEBUG_ENABLED - ~transaction_xdata_t() { - DEBUG_PRINT("ledger.memory.dtors", "dtor transaction_xdata_t"); - } -#endif -}; - -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) { - 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); -} - -////////////////////////////////////////////////////////////////////// - -typedef std::deque<transaction_t *> transactions_deque; -typedef std::deque<entry_t *> entries_deque; - -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_transactions(transactions_deque::iterator begin, - transactions_deque::iterator end, - item_handler<transaction_t>& handler) { - for (transactions_deque::iterator i = begin; i != end; i++) - handler(**i); -} - -inline void walk_transactions(transactions_deque& deque, - item_handler<transaction_t>& handler) { - walk_transactions(deque.begin(), deque.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> -{ - 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(true) { - try { - sort_order = parse_value_expr(_sort_order); - } - 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> -{ - const item_predicate<transaction_t> * pred; - bool allocated; - - public: - filter_transactions(item_handler<transaction_t> * handler, - const item_predicate<transaction_t> * predicate) - : item_handler<transaction_t>(handler), - pred(predicate), allocated(false) {} - - filter_transactions(item_handler<transaction_t> * handler, - const std::string& predicate) - : item_handler<transaction_t>(handler), - pred(new item_predicate<transaction_t>(predicate)), - allocated(true) {} - - virtual ~filter_transactions() { - if (allocated) - delete pred; - } - - virtual void operator()(transaction_t& xact) { - if ((*pred)(xact)) - (*handler)(xact); - } -}; - -class calc_transactions : public item_handler<transaction_t> -{ - transaction_t * last_xact; - const bool inverted; - - public: - calc_transactions(item_handler<transaction_t> * handler, - const bool _inverted = false) - : item_handler<transaction_t>(handler), - last_xact(NULL), inverted(_inverted) {} - - virtual void operator()(transaction_t& xact); -}; - -class collapse_transactions : public item_handler<transaction_t> -{ - balance_pair_t subtotal; - unsigned int count; - entry_t * last_entry; - transaction_t * last_xact; - account_t totals_account; - - 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>") {} - - virtual void flush() { - if (subtotal) - report_cumulative_subtotal(); - item_handler<transaction_t>::flush(); - } - - void report_cumulative_subtotal(); - - virtual void 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_cumulative_subtotal(); - - add_transaction_to(xact, subtotal); - count++; - - last_entry = xact.entry; - last_xact = &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; - 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) {} - - virtual void flush() { - if (last_xact) { - output_diff(std::time(NULL)); - 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 *, balance_pair_t> balances_map; - typedef std::pair<account_t *, balance_pair_t> balances_pair; - - protected: - std::time_t start; - std::time_t finish; - balances_map balances; - std::list<entry_t> entry_temps; - std::list<transaction_t> xact_temps; - - public: - subtotal_transactions(item_handler<transaction_t> * handler) - : item_handler<transaction_t>(handler) {} - - void report_subtotal(const char * spec_fmt = NULL); - - virtual void flush() { - if (balances.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; - - public: - interval_transactions(item_handler<transaction_t> * handler, - const interval_t& _interval) - : subtotal_transactions(handler), interval(_interval), - last_xact(NULL) {} - - interval_transactions(item_handler<transaction_t> * handler, - const std::string& _interval) - : subtotal_transactions(handler), interval(_interval), - last_xact(NULL) {} - - virtual ~interval_transactions() { - start = interval.begin; - finish = interval.increment(interval.begin); - } - - 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 related_transactions : public item_handler<transaction_t> -{ - 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 operator()(transaction_t& xact) { - for (transactions_list::iterator i = xact.entry->transactions.begin(); - i != xact.entry->transactions.end(); - i++) - if ((! transaction_has_xdata(**i) || - ! (transaction_xdata(**i).dflags & TRANSACTION_HANDLED)) && - (*i == &xact ? also_matching : - ! ((*i)->flags & TRANSACTION_AUTO))) { - transaction_xdata(**i).dflags |= TRANSACTION_HANDLED; - (*handler)(**i); - } - } -}; - - -////////////////////////////////////////////////////////////////////// -// -// Account walking functions -// - -#define ACCOUNT_TO_DISPLAY 0x1 -#define ACCOUNT_DISPLAYED 0x2 -#define ACCOUNT_SORT_CALC 0x4 - -struct account_xdata_t -{ - value_t value; - value_t total; - value_t sort_value; - unsigned int count; // transactions counted toward total - unsigned int subcount; - unsigned short dflags; - - account_xdata_t() : count(0), subcount(0), dflags(0) { - DEBUG_PRINT("ledger.memory.ctors", "ctor account_xdata_t"); - } -#ifdef DEBUG_ENABLED - ~account_xdata_t() { - DEBUG_PRINT("ledger.memory.dtors", "dtor account_xdata_t"); - } -#endif -}; - -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) { - 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); -} - -inline void set_account_value::operator()(transaction_t& xact) { - add_transaction_to(xact, account_xdata(*xact.account).value); - account_xdata(*xact.account).subcount++; - - if (handler) - (*handler)(xact); -} - -////////////////////////////////////////////////////////////////////// - -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(); - -} // namespace ledger - -#endif // _WALK_H |