diff options
-rw-r--r-- | src/format.cc | 265 | ||||
-rw-r--r-- | src/format.h | 139 | ||||
-rw-r--r-- | src/numerics/amount.cc (renamed from src/amount.cc) | 0 | ||||
-rw-r--r-- | src/numerics/amount.h (renamed from src/amount.h) | 0 | ||||
-rw-r--r-- | src/numerics/balance.h (renamed from src/balance.h) | 0 | ||||
-rw-r--r-- | src/numerics/balpair.h (renamed from src/balpair.h) | 0 | ||||
-rw-r--r-- | src/utility/binary.cc (renamed from src/binary.cc) | 2 | ||||
-rw-r--r-- | src/utility/binary.h (renamed from src/binary.h) | 0 | ||||
-rw-r--r-- | src/utility/pushvar.h (renamed from src/scoped_execute.h) | 74 | ||||
-rw-r--r-- | src/utility/utils.h (renamed from src/utils.h) | 2 | ||||
-rw-r--r-- | src/xmlparse.cc | 506 |
11 files changed, 16 insertions, 972 deletions
diff --git a/src/format.cc b/src/format.cc deleted file mode 100644 index 1e89328c..00000000 --- a/src/format.cc +++ /dev/null @@ -1,265 +0,0 @@ -/* - * Copyright (c) 2003-2007, John Wiegley. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of New Artisans LLC nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "format.h" - -namespace ledger { - -void format_t::parse(const string& fmt) -{ - element_t * current = NULL; - - char buf[1024]; - char * q = buf; - - if (elements.size() > 0) - clear_elements(); - format_string = fmt; - - for (const char * p = fmt.c_str(); *p; p++) { - if (*p != '%' && *p != '\\') { - *q++ = *p; - continue; - } - else if (*p == '\\') { - p++; - switch (*p) { - case 'b': *q++ = '\b'; break; - case 'f': *q++ = '\f'; break; - case 'n': *q++ = '\n'; break; - case 'r': *q++ = '\r'; break; - case 't': *q++ = '\t'; break; - case 'v': *q++ = '\v'; break; - default: - *q++ = *p; - break; - } - continue; - } - else { - assert(*p == '%'); - if (*(p + 1) == '%') { - p++; // %% is the same as \% - *q++ = *p; - continue; - } - } - - current = new element_t; - elements.push_back(current); - - if (q != buf) { - current->kind = element_t::TEXT; - current->chars = new string(buf, q); - q = buf; - - current = new element_t; - elements.push_back(current); - } - - ++p; - if (*p == '-') { - current->align_left = true; - ++p; - } - - if (*p && std::isdigit(*p)) { - int num = *p++ - '0'; - while (*p && std::isdigit(*p)) { - num *= 10; - num += *p++ - '0'; - } - current->min_width = num; - } - - if (*p == '.') { - ++p; - int num = 0; - while (*p && std::isdigit(*p)) { - num *= 10; - num += *p++ - '0'; - } - - current->max_width = num; - if (current->min_width == -1) - current->min_width = current->max_width; - } - - if (current->max_width != -1 && current->min_width != -1 && - current->max_width < current->min_width) - throw_(format_error, "Maximum width is less than the minimum width"); - - switch (*p) { - case '|': - current->kind = element_t::COLUMN; - break; - - case '{': - case '(': { - char open = *p; - char close = *p == '{' ? '}' : ')'; - ++p; - const char * b = p; - int depth = 1; - while (*p) { - if (*p == close && --depth == 0) - break; - else if (*p == open) - ++depth; - p++; - } - if (*p != close) - throw_(format_error, "Missing '" << close << "'"); - - if (open == '{') { - assert(! current->xpath); - current->kind = element_t::XPATH; - current->xpath = new xml::xpath_t(string(b, p)); - } else { - assert(! current->format); - current->kind = element_t::GROUP; - current->format = new format_t(string(b, p)); - } - break; - } - - default: - assert(! current->xpath); - current->kind = element_t::XPATH; - current->xpath = new xml::xpath_t(string(p, p + 1)); - break; - } - } - - if (q != buf) { - current = new element_t; - elements.push_back(current); - - current->kind = element_t::TEXT; - current->chars = new string(buf, q); - } -} - -void format_t::compile(xml::node_t * context) -{ - for (std::list<element_t *>::iterator i = elements.begin(); - i != elements.end(); - i++) - switch ((*i)->kind) { - case element_t::XPATH: - assert((*i)->xpath); - (*i)->xpath->compile(context); - break; - case element_t::GROUP: - assert((*i)->format); - (*i)->format->compile(context); - break; - default: - break; - } -} - -int format_t::element_formatter_t::operator() - (std::ostream& out_str, element_t * elem, xml::node_t * context, - int column) const -{ - if (elem->kind == element_t::COLUMN) { - if (elem->max_width != -1 && elem->max_width < column) { - out_str << '\n'; - column = 0; - } - - if (elem->min_width != -1 && elem->min_width > column) { - out_str << string(elem->min_width - column, ' '); - column = elem->min_width; - } - return column; - } - - std::ostringstream out; - - if (elem->align_left) - out << std::left; - else - out << std::right; - - if (elem->min_width > 0) - out.width(elem->min_width); - - int start_column = column; - - if (elem->kind == element_t::XPATH) - elem->xpath->calc(context).strip_annotations() - .print(out, elem->min_width, elem->max_width); - else if (elem->kind == element_t::GROUP) - column = elem->format->format(out, context, column); - else if (elem->kind == element_t::TEXT) - out << *elem->chars; - else - assert(false); - - string temp = out.str(); - for (string::const_iterator i = temp.begin(); - i != temp.end(); - i++) - if (*i == '\n' || *i == '\r') - column = 0; - else - column++; - - int virtual_width = column - start_column; - - if (elem->min_width != -1 && virtual_width < elem->min_width) { - out_str << temp << string(' ', elem->min_width - virtual_width); - } - else if (elem->max_width != -1 && virtual_width > elem->max_width) { - temp.erase(temp.length() - (virtual_width - elem->max_width)); - out_str << temp; - } - else { - out_str << temp; - } - - return column; -} - -int format_t::format(std::ostream& out, xml::node_t * context, - int column, const element_formatter_t& formatter) const -{ - for (std::list<element_t *>::const_iterator i = elements.begin(); - i != elements.end(); - i++) - column = formatter(out, *i, context, column); - - return column; -} - -} // namespace ledger diff --git a/src/format.h b/src/format.h deleted file mode 100644 index c25c1149..00000000 --- a/src/format.h +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (c) 2003-2007, John Wiegley. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of New Artisans LLC nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef _FORMAT_H -#define _FORMAT_H - -#include "xpath.h" - -namespace ledger { - -class format_t -{ - public: - struct element_t - { - bool align_left; - short min_width; - short max_width; - - enum kind_t { UNKNOWN, TEXT, COLUMN, XPATH, GROUP } kind; - union { - string * chars; - xml::xpath_t * xpath; - format_t * format; - }; - - element_t() - : align_left(false), min_width(-1), max_width(-1), - kind(UNKNOWN), chars(NULL) { - TRACE_CTOR(element_t, ""); - } - - ~element_t() { - TRACE_DTOR(element_t); - - switch (kind) { - case TEXT: - checked_delete(chars); - break; - case XPATH: - checked_delete(xpath); - break; - case GROUP: - checked_delete(format); - break; - default: - assert(! chars); - break; - } - } - - private: - element_t(const element_t& other); - }; - - struct element_formatter_t { - virtual ~element_formatter_t() {} - virtual int operator()(std::ostream& out, element_t * element, - xml::node_t * context, int column) const; - }; - - string format_string; - std::list<element_t *> elements; - - private: - format_t(const format_t&); - - public: - format_t() { - TRACE_CTOR(format_t, ""); - } - format_t(const string& fmt) { - TRACE_CTOR(format_t, "const string&"); - parse(fmt); - } - - void clear_elements() { - for (std::list<element_t *>::iterator i = elements.begin(); - i != elements.end(); - i++) - checked_delete(*i); - elements.clear(); - } - - virtual ~format_t() { - TRACE_DTOR(format_t); - clear_elements(); - } - - void parse(const string& fmt); - - void compile(const string& fmt, xml::node_t * context = NULL) { - parse(fmt); - compile(context); - } - void compile(xml::node_t * context = NULL); - - int format(std::ostream& out, xml::node_t * context = NULL, - int column = 0, const element_formatter_t& formatter = - element_formatter_t()) const; - - operator bool() const { - return ! format_string.empty(); - } -}; - -DECLARE_EXCEPTION(format_error); - -} // namespace ledger - -#endif // _FORMAT_H diff --git a/src/amount.cc b/src/numerics/amount.cc index 4c771cc7..4c771cc7 100644 --- a/src/amount.cc +++ b/src/numerics/amount.cc diff --git a/src/amount.h b/src/numerics/amount.h index f4dbc9bd..f4dbc9bd 100644 --- a/src/amount.h +++ b/src/numerics/amount.h diff --git a/src/balance.h b/src/numerics/balance.h index 1bab4b6b..1bab4b6b 100644 --- a/src/balance.h +++ b/src/numerics/balance.h diff --git a/src/balpair.h b/src/numerics/balpair.h index 13e4857b..13e4857b 100644 --- a/src/balpair.h +++ b/src/numerics/balpair.h diff --git a/src/binary.cc b/src/utility/binary.cc index 9ddbb144..5802c3bd 100644 --- a/src/binary.cc +++ b/src/utility/binary.cc @@ -29,7 +29,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "binary.h" +#include "utils.h" namespace ledger { namespace binary { diff --git a/src/binary.h b/src/utility/binary.h index 74199c89..74199c89 100644 --- a/src/binary.h +++ b/src/utility/binary.h diff --git a/src/scoped_execute.h b/src/utility/pushvar.h index 522bac5a..793c0ca6 100644 --- a/src/scoped_execute.h +++ b/src/utility/pushvar.h @@ -30,7 +30,7 @@ */ /** - * @file scoped_execute.h + * @file scopevar.h * @author John Wiegley * @date Sun May 6 20:10:52 2007 * @@ -105,7 +105,7 @@ * @endcode * * But what if it could be even easier? That is what this file is - * for, to provide a scoped_execute<> class which guarantees execution + * for, to provide a scopevar<> class which guarantees execution * of arbtirary code after a scope has terminated, without having to * resort to custom utility classes. It relies on boost::bind to * declare pending function calls. Here it what the above would look @@ -114,7 +114,7 @@ * @code * void foo(pthread_mutex_t * mutex) { * if (pthread_mutex_lock(mutex) == 0) { - * scoped_execute<void> unlock_mutex + * scopevar<void> unlock_mutex * (boost::bind(pthread_mutex_unlock, mutex)); * try { * // Do work that requires the mutex to be locked @@ -131,7 +131,7 @@ * The single call to boost::bind creates a closure binding that will * be invoked once the containing scope has terminated. * - * Another kind of scoped_execute is useful for setting the values of + * Another kind of scopevar is useful for setting the values of * variables to a predetermined value upon completion of a scope. * Consider this example: * @@ -139,7 +139,7 @@ * bool foo_was_run; * * void foo() { - * scoped_execute<bool&> set_success((_1 = true), foo_was_run); + * scopevar<bool&> set_success((_1 = true), foo_was_run); * // do some code, and make sure foo_was_run is set to true * // once the scope is exited -- however this happens. * } @@ -177,34 +177,31 @@ * } * @endcode * - * Finally, you can stop a scoped_execute or scoped_variable from + * Finally, you can stop a scopevar or scoped_variable from * invoking its completion code by calling the `clear' method on the * object instance. Once `clear' is called, the scoped execution * becomes inert and will do nothing when the enclosing scope is * exited. */ -#ifndef _SCOPED_EXECUTE_H -#define _SCOPED_EXECUTE_H - -#include <boost/noncopyable.hpp> -#include <boost/function.hpp> +#ifndef _SCOPEVAR_H +#define _SCOPEVAR_H template <typename T> -class scoped_variable : public boost::noncopyable +class push_variable : public boost::noncopyable { T& var; T prev; bool enabled; public: - explicit scoped_variable(T& _var) + explicit push_variable(T& _var) : var(_var), prev(var), enabled(true) {} - explicit scoped_variable(T& _var, const T& value) + explicit push_variable(T& _var, const T& value) : var(_var), prev(var), enabled(true) { var = value; } - ~scoped_variable() { + ~push_variable() { if (enabled) var = prev; } @@ -214,49 +211,4 @@ public: } }; -template <typename T> -class scoped_execute : public boost::noncopyable -{ - typedef boost::function<void (T)> function_t; - - function_t code; - T arg; - bool enabled; - -public: - explicit scoped_execute(const function_t& _code, T _arg) - : code(_code), arg(_arg), enabled(true) {} - - ~scoped_execute() { - if (enabled) - code(arg); - } - - void clear() { - enabled = false; - } -}; - -template <> -class scoped_execute<void> : public boost::noncopyable -{ - typedef boost::function<void ()> function_t; - - function_t code; - bool enabled; - -public: - explicit scoped_execute(const function_t& _code) - : code(_code), enabled(true) {} - - ~scoped_execute() { - if (enabled) - code(); - } - - void clear() { - enabled = false; - } -}; - -#endif // _SCOPED_EXECUTE_H +#endif // _SCOPEVAR_H diff --git a/src/utils.h b/src/utility/utils.h index 1420f9ca..9ddedb0e 100644 --- a/src/utils.h +++ b/src/utility/utils.h @@ -507,11 +507,13 @@ inline void throw_unexpected_error(char, char) { * * Date/time support classes * General support for objects with "flags" + * Support for object serialization (binary read/write) * Support for scoped execution and variable restoration */ #include "times.h" #include "flags.h" +#include "binary.h" #include "pushvar.h" /********************************************************************** diff --git a/src/xmlparse.cc b/src/xmlparse.cc deleted file mode 100644 index ff724dfe..00000000 --- a/src/xmlparse.cc +++ /dev/null @@ -1,506 +0,0 @@ -/* - * Copyright (c) 2003-2007, John Wiegley. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of New Artisans LLC nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "xml.h" -#include "journal.h" - -namespace ledger { -namespace xml { - -#if defined(HAVE_EXPAT) || defined(HAVE_XMLPARSE) - -static XML_Parser current_parser; -static unsigned int count; - -static journal_t * curr_journal; -static entry_t * curr_entry; -static commodity_t * curr_comm; -static string comm_flags; - -static transaction_t::state_t curr_state; - -static string data; -static bool ignore; -static string have_error; - -static void startElement(void *userData, const char *name, const char **attrs) -{ - if (ignore) - return; - - if (std::strcmp(name, "entry") == 0) { - assert(! curr_entry); - curr_entry = new entry_t; - curr_state = transaction_t::UNCLEARED; - } - else if (std::strcmp(name, "transaction") == 0) { - assert(curr_entry); - curr_entry->add_transaction(new transaction_t); - if (curr_state != transaction_t::UNCLEARED) - curr_entry->transactions.back()->state = curr_state; - } - else if (std::strcmp(name, "commodity") == 0) { - if (string(attrs[0]) == "flags") - comm_flags = attrs[1]; - } - else if (std::strcmp(name, "total") == 0) { - ignore = true; - } -} - -static void endElement(void *userData, const char *name) -{ - if (ignore) { - if (std::strcmp(name, "total") == 0) - ignore = false; - return; - } - - if (std::strcmp(name, "entry") == 0) { - assert(curr_entry); - if (curr_journal->add_entry(curr_entry)) { - count++; - } else { - account_t * acct = curr_journal->find_account("<Unknown>"); - curr_entry->add_transaction(new transaction_t(acct)); - if (curr_journal->add_entry(curr_entry)) { - count++; - } else { - checked_delete(curr_entry); - have_error = "Entry cannot be balanced"; - } - } - curr_entry = NULL; - } - else if (std::strcmp(name, "en:date") == 0) { - curr_entry->_date = parse_datetime(data); - } - else if (std::strcmp(name, "en:date_eff") == 0) { - curr_entry->_date_eff = parse_datetime(data); - } - else if (std::strcmp(name, "en:code") == 0) { - curr_entry->code = data; - } - else if (std::strcmp(name, "en:cleared") == 0) { - curr_state = transaction_t::CLEARED; - } - else if (std::strcmp(name, "en:pending") == 0) { - curr_state = transaction_t::PENDING; - } - else if (std::strcmp(name, "en:payee") == 0) { - curr_entry->payee = data; - } - else if (std::strcmp(name, "tr:account") == 0) { - curr_entry->transactions.back()->account = curr_journal->find_account(data); - } - else if (std::strcmp(name, "tr:cleared") == 0) { - curr_entry->transactions.back()->state = transaction_t::CLEARED; - } - else if (std::strcmp(name, "tr:pending") == 0) { - curr_entry->transactions.back()->state = transaction_t::PENDING; - } - else if (std::strcmp(name, "tr:virtual") == 0) { - curr_entry->transactions.back()->add_flags(TRANSACTION_VIRTUAL); - } - else if (std::strcmp(name, "tr:generated") == 0) { - curr_entry->transactions.back()->add_flags(TRANSACTION_AUTO); - } - else if (std::strcmp(name, "symbol") == 0) { - assert(! curr_comm); - curr_comm = amount_t::current_pool->find_or_create(data); - assert(curr_comm); - curr_comm->add_flags(COMMODITY_STYLE_SUFFIXED); - if (! comm_flags.empty()) { - for (string::size_type i = 0, l = comm_flags.length(); i < l; i++) { - switch (comm_flags[i]) { - case 'P': curr_comm->drop_flags(COMMODITY_STYLE_SUFFIXED); break; - case 'S': curr_comm->add_flags(COMMODITY_STYLE_SEPARATED); break; - case 'T': curr_comm->add_flags(COMMODITY_STYLE_THOUSANDS); break; - case 'E': curr_comm->add_flags(COMMODITY_STYLE_EUROPEAN); break; - } - } - } - } -#if 0 - // jww (2006-03-02): !!! - else if (std::strcmp(name, "price") == 0) { - assert(curr_comm); - amount_t * price = new amount_t(data); - std::ostringstream symstr; - symstr << curr_comm->symbol << " {" << *price << "}"; - commodity_t * priced_comm = - commodity_t::find_commodity(symstr.str(), true); - priced_comm->price = price; - priced_comm->base = curr_comm; - curr_comm = priced_comm; - } -#endif - else if (std::strcmp(name, "quantity") == 0) { - amount_t temp; - temp.parse(data); - curr_entry->transactions.back()->amount = temp; - - if (curr_comm) { - string::size_type i = data.find('.'); - if (i != string::npos) { - int precision = data.length() - i - 1; - if (precision > curr_comm->precision()) - curr_comm->set_precision(precision); - } - curr_entry->transactions.back()->amount->set_commodity(*curr_comm); - curr_comm = NULL; - } - } - else if (std::strcmp(name, "tr:amount") == 0) { - curr_comm = NULL; - } -} - -static void dataHandler(void *userData, const char *s, int len) -{ - if (! ignore) - data = string(s, len); -} - -bool xml_parser_t::test(std::istream& in) const -{ - char buf[80]; - - in.getline(buf, 79); - if (std::strncmp(buf, "<?xml", 5) != 0) { - in.clear(); - in.seekg(0, std::ios::beg); - return false; - } - - in.getline(buf, 79); - if (! std::strstr(buf, "<ledger")) { - in.clear(); - in.seekg(0, std::ios::beg); - return false; - } - - in.clear(); - in.seekg(0, std::ios::beg); - return true; -} - -unsigned int xml_parser_t::parse(std::istream& in, - journal_t * journal, - account_t * master, - const optional<path>& original) -{ - char buf[BUFSIZ]; - - count = 0; - curr_journal = journal; - curr_entry = NULL; - curr_comm = NULL; - ignore = false; - - XML_Parser parser = XML_ParserCreate(NULL); - current_parser = parser; - - XML_SetElementHandler(parser, startElement, endElement); - XML_SetCharacterDataHandler(parser, dataHandler); - - while (! in.eof()) { - in.getline(buf, BUFSIZ - 1); - std::strcat(buf, "\n"); - bool result; - try { - result = XML_Parse(parser, buf, std::strlen(buf), in.eof()); - } - catch (const std::exception& err) { - //unsigned long line = XML_GetCurrentLineNumber(parser) - offset++; - XML_ParserFree(parser); - throw_(parse_error, err.what()); - } - - if (! have_error.empty()) { - //unsigned long line = XML_GetCurrentLineNumber(parser) - offset++; -#if 0 - // jww (2007-04-26): What is this code doing? - parse_error err(have_error); - std::cerr << "Error: " << err.what() << std::endl; -#endif - have_error = ""; - } - - if (! result) { - //unsigned long line = XML_GetCurrentLineNumber(parser) - offset++; - const char * err = XML_ErrorString(XML_GetErrorCode(parser)); - XML_ParserFree(parser); - throw_(parse_error, err); - } - } - - XML_ParserFree(parser); - - return count; -} - -#endif // defined(HAVE_EXPAT) || defined(HAVE_XMLPARSE) - -#if 0 -void xml_write_amount(std::ostream& out, const amount_t& amount, - const int depth = 0) -{ - for (int i = 0; i < depth; i++) out << ' '; - out << "<amount>\n"; - - commodity_t& c = amount.commodity(); - for (int i = 0; i < depth + 2; i++) out << ' '; - out << "<commodity flags=\""; - if (! (c.flags() & COMMODITY_STYLE_SUFFIXED)) out << 'P'; - if (c.flags() & COMMODITY_STYLE_SEPARATED) out << 'S'; - if (c.flags() & COMMODITY_STYLE_THOUSANDS) out << 'T'; - if (c.flags() & COMMODITY_STYLE_EUROPEAN) out << 'E'; - out << "\">\n"; - for (int i = 0; i < depth + 4; i++) out << ' '; -#if 0 - // jww (2006-03-02): !!! - if (c.price) { - out << "<symbol>" << c.base->symbol << "</symbol>\n"; - for (int i = 0; i < depth + 4; i++) out << ' '; - out << "<price>\n"; - xml_write_amount(out, *c.price, depth + 6); - for (int i = 0; i < depth + 4; i++) out << ' '; - out << "</price>\n"; - } else { - out << "<symbol>" << c.symbol << "</symbol>\n"; - } -#endif - for (int i = 0; i < depth + 2; i++) out << ' '; - out << "</commodity>\n"; - - for (int i = 0; i < depth + 2; i++) out << ' '; - out << "<quantity>"; - out << amount.quantity_string() << "</quantity>\n"; - - for (int i = 0; i < depth; i++) out << ' '; - out << "</amount>\n"; -} - -void xml_write_value(std::ostream& out, const value_t& value, - const int depth = 0) -{ - balance_t * bal = NULL; - - for (int i = 0; i < depth; i++) out << ' '; - out << "<value type=\""; - switch (value.type) { - case value_t::BOOLEAN: out << "boolean"; break; - case value_t::INTEGER: out << "integer"; break; - case value_t::AMOUNT: out << "amount"; break; - case value_t::BALANCE: - case value_t::BALANCE_PAIR: out << "balance"; break; - } - out << "\">\n"; - - switch (value.type) { - case value_t::BOOLEAN: - for (int i = 0; i < depth + 2; i++) out << ' '; - out << "<boolean>" << value.as_boolean() << "</boolean>\n"; - break; - - case value_t::INTEGER: - for (int i = 0; i < depth + 2; i++) out << ' '; - out << "<integer>" << value.as_long() << "</integer>\n"; - break; - - case value_t::AMOUNT: - xml_write_amount(out, value.as_amount(), depth + 2); - break; - - case value_t::BALANCE: - bal = &value.as_balance(); - // fall through... - - case value_t::BALANCE_PAIR: - if (! bal) - bal = &value.as_balance_pair()->quantity; - - for (int i = 0; i < depth + 2; i++) out << ' '; - out << "<balance>\n"; - -#if 0 - // jww (2007-04-30): Change this so that types know how to stream - // themselves to XML on their own. - - for (balance_t::amounts_map::const_iterator i = bal->amounts.begin(); - i != bal->amounts.end(); - i++) - xml_write_amount(out, (*i).second, depth + 4); -#endif - - for (int i = 0; i < depth + 2; i++) out << ' '; - out << "</balance>\n"; - break; - - default: - assert(false); - break; - } - - for (int i = 0; i < depth; i++) out << ' '; - out << "</value>\n"; -} - -void output_xml_string(std::ostream& out, const string& str) -{ - for (const char * s = str.c_str(); *s; s++) { - switch (*s) { - case '<': - out << "<"; - break; - case '>': - out << "&rt;"; - break; - case '&': - out << "&"; - break; - default: - out << *s; - break; - } - } -} - -void format_xml_entries::format_last_entry() -{ - output_stream << " <entry>\n" - << " <en:date>" << last_entry->_date.to_string("%Y/%m/%d") - << "</en:date>\n"; - - if (last_entry->_date_eff) - output_stream << " <en:date_eff>" - << last_entry->_date_eff.to_string("%Y/%m/%d") - << "</en:date_eff>\n"; - - if (! last_entry->code.empty()) { - output_stream << " <en:code>"; - output_xml_string(output_stream, last_entry->code); - output_stream << "</en:code>\n"; - } - - if (! last_entry->payee.empty()) { - output_stream << " <en:payee>"; - output_xml_string(output_stream, last_entry->payee); - output_stream << "</en:payee>\n"; - } - - bool first = true; - for (transactions_list::const_iterator i = last_entry->transactions.begin(); - i != last_entry->transactions.end(); - i++) { - if (transaction_has_xdata(**i) && - transaction_xdata_(**i).dflags & TRANSACTION_TO_DISPLAY) { - if (first) { - output_stream << " <en:transactions>\n"; - first = false; - } - - output_stream << " <transaction>\n"; - - if ((*i)->_date) - output_stream << " <tr:date>" - << (*i)->_date.to_string("%Y/%m/%d") - << "</tr:date>\n"; - - if ((*i)->_date_eff) - output_stream << " <tr:date_eff>" - << (*i)->_date_eff.to_string("%Y/%m/%d") - << "</tr:date_eff>\n"; - - if ((*i)->state == transaction_t::CLEARED) - output_stream << " <tr:cleared/>\n"; - else if ((*i)->state == transaction_t::PENDING) - output_stream << " <tr:pending/>\n"; - - if ((*i)->flags & TRANSACTION_VIRTUAL) - output_stream << " <tr:virtual/>\n"; - if ((*i)->flags & TRANSACTION_AUTO) - output_stream << " <tr:generated/>\n"; - - if ((*i)->account) { - string name = (*i)->account->fullname(); - if (name == "<Total>") - name = "[TOTAL]"; - else if (name == "<Unknown>") - name = "[UNKNOWN]"; - - output_stream << " <tr:account>"; - output_xml_string(output_stream, name); - output_stream << "</tr:account>\n"; - } - - output_stream << " <tr:amount>\n"; - if (transaction_xdata_(**i).dflags & TRANSACTION_COMPOUND) - xml_write_value(output_stream, - transaction_xdata_(**i).value, 10); - else - xml_write_value(output_stream, value_t((*i)->amount), 10); - output_stream << " </tr:amount>\n"; - - if ((*i)->cost) { - output_stream << " <tr:cost>\n"; - xml_write_value(output_stream, value_t(*(*i)->cost), 10); - output_stream << " </tr:cost>\n"; - } - - if (! (*i)->note.empty()) { - output_stream << " <tr:note>"; - output_xml_string(output_stream, (*i)->note); - output_stream << "</tr:note>\n"; - } - - if (show_totals) { - output_stream << " <total>\n"; - xml_write_value(output_stream, transaction_xdata_(**i).total, 10); - output_stream << " </total>\n"; - } - - output_stream << " </transaction>\n"; - - transaction_xdata_(**i).dflags |= TRANSACTION_DISPLAYED; - } - } - - if (! first) - output_stream << " </en:transactions>\n"; - - output_stream << " </entry>\n"; -} -#endif - -} // namespace xml -} // namespace ledger |