diff options
Diffstat (limited to 'src/utility')
-rw-r--r-- | src/utility/binary.cc | 157 | ||||
-rw-r--r-- | src/utility/binary.h | 269 | ||||
-rw-r--r-- | src/utility/context.h | 142 | ||||
-rw-r--r-- | src/utility/flags.h | 103 | ||||
-rw-r--r-- | src/utility/mask.cc | 55 | ||||
-rw-r--r-- | src/utility/mask.h | 55 | ||||
-rw-r--r-- | src/utility/pushvar.h | 214 | ||||
-rw-r--r-- | src/utility/system.hh | 161 | ||||
-rw-r--r-- | src/utility/times.cc | 80 | ||||
-rw-r--r-- | src/utility/times.h | 123 | ||||
-rw-r--r-- | src/utility/utils.cc | 720 | ||||
-rw-r--r-- | src/utility/utils.h | 546 |
12 files changed, 0 insertions, 2625 deletions
diff --git a/src/utility/binary.cc b/src/utility/binary.cc deleted file mode 100644 index 52d5f196..00000000 --- a/src/utility/binary.cc +++ /dev/null @@ -1,157 +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 "utils.h" - -namespace ledger { -namespace binary { - -void read_bool(std::istream& in, bool& num) -{ - read_guard(in, 0x2005); - unsigned char val; - in.read(reinterpret_cast<char *>(&val), sizeof(val)); - num = val == 1; - read_guard(in, 0x2006); -} - -void read_bool(const char *& data, bool& num) -{ - read_guard(data, 0x2005); - const unsigned char val = *reinterpret_cast<const unsigned char *>(data); - data += sizeof(unsigned char); - num = val == 1; - read_guard(data, 0x2006); -} - -void read_string(std::istream& in, string& str) -{ - read_guard(in, 0x3001); - - unsigned char len; - read_number_nocheck(in, len); - if (len == 0xff) { - unsigned short slen; - read_number_nocheck(in, slen); - char * buf = new char[slen + 1]; - in.read(buf, slen); - buf[slen] = '\0'; - str = buf; - checked_array_delete(buf); - } - else if (len) { - char buf[256]; - in.read(buf, len); - buf[len] = '\0'; - str = buf; - } else { - str = ""; - } - - read_guard(in, 0x3002); -} - -void read_string(const char *& data, string& str) -{ - read_guard(data, 0x3001); - - unsigned char len; - read_number_nocheck(data, len); - if (len == 0xff) { - unsigned short slen; - read_number_nocheck(data, slen); - str = string(data, slen); - data += slen; - } - else if (len) { - str = string(data, len); - data += len; - } - else { - str = ""; - } - - read_guard(data, 0x3002); -} - -void read_string(const char *& data, string * str) -{ - read_guard(data, 0x3001); - - unsigned char len; - read_number_nocheck(data, len); - if (len == 0xff) { - unsigned short slen; - read_number_nocheck(data, slen); - new(str) string(data, slen); - data += slen; - } - else if (len) { - new(str) string(data, len); - data += len; - } - else { - new(str) string(""); - } - - read_guard(data, 0x3002); -} - - -void write_bool(std::ostream& out, bool num) -{ - write_guard(out, 0x2005); - unsigned char val = num ? 1 : 0; - out.write(reinterpret_cast<char *>(&val), sizeof(val)); - write_guard(out, 0x2006); -} - -void write_string(std::ostream& out, const string& str) -{ - write_guard(out, 0x3001); - - unsigned long len = str.length(); - if (len > 255) { - assert(len < 65536); - write_number_nocheck<unsigned char>(out, 0xff); - write_number_nocheck<unsigned short>(out, len); - } else { - write_number_nocheck<unsigned char>(out, len); - } - - if (len) - out.write(str.c_str(), len); - - write_guard(out, 0x3002); -} - -} // namespace binary -} // namespace ledger diff --git a/src/utility/binary.h b/src/utility/binary.h deleted file mode 100644 index 864c6ea2..00000000 --- a/src/utility/binary.h +++ /dev/null @@ -1,269 +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 BINARY_H -#define BINARY_H - -namespace ledger { -namespace binary { - -template <typename T> -inline void read_number_nocheck(std::istream& in, T& num) { - in.read(reinterpret_cast<char *>(&num), sizeof(num)); -} - -template <typename T> -inline void read_number_nocheck(const char *& data, T& num) { - num = *reinterpret_cast<const T *>(data); - data += sizeof(T); -} - -template <typename T> -inline T read_number_nocheck(std::istream& in) { - T num; - read_number_nocheck(in, num); - return num; -} - -template <typename T> -inline T read_number_nocheck(const char *& data) { - T num; - read_number_nocheck(data, num); - return num; -} - -#if DEBUG_LEVEL >= ALPHA -#define read_guard(in, id) \ - if (read_number_nocheck<unsigned short>(in) != id) \ - assert(false); -#else -#define read_guard(in, id) -#endif - -template <typename T> -inline void read_number(std::istream& in, T& num) { - read_guard(in, 0x2003); - in.read(reinterpret_cast<char *>(&num), sizeof(num)); - read_guard(in, 0x2004); -} - -template <typename T> -inline void read_number(const char *& data, T& num) { - read_guard(data, 0x2003); - num = *reinterpret_cast<const T *>(data); - data += sizeof(T); - read_guard(data, 0x2004); -} - -template <typename T> -inline T read_number(std::istream& in) { - T num; - read_number(in, num); - return num; -} - -template <typename T> -inline T read_number(const char *& data) { - T num; - read_number(data, num); - return num; -} - -void read_bool(std::istream& in, bool& num); -void read_bool(const char *& data, bool& num); - -inline bool read_bool(std::istream& in) { - bool num; - read_bool(in, num); - return num; -} - -inline bool read_bool(const char *& data) { - bool num; - read_bool(data, num); - return num; -} - -template <typename T> -void read_long(std::istream& in, T& num) -{ - read_guard(in, 0x2001); - - unsigned char len; - read_number_nocheck(in, len); - - num = 0; - unsigned char temp; - if (len > 3) { - read_number_nocheck(in, temp); - num |= static_cast<unsigned long>(temp) << 24; - } - if (len > 2) { - read_number_nocheck(in, temp); - num |= static_cast<unsigned long>(temp) << 16; - } - if (len > 1) { - read_number_nocheck(in, temp); - num |= static_cast<unsigned long>(temp) << 8; - } - - read_number_nocheck(in, temp); - num |= static_cast<unsigned long>(temp); - - read_guard(in, 0x2002); -} - -template <typename T> -void read_long(const char *& data, T& num) -{ - read_guard(data, 0x2001); - - unsigned char len; - read_number_nocheck(data, len); - - num = 0; - unsigned char temp; - if (len > 3) { - read_number_nocheck(data, temp); - num |= static_cast<unsigned long>(temp) << 24; - } - if (len > 2) { - read_number_nocheck(data, temp); - num |= static_cast<unsigned long>(temp) << 16; - } - if (len > 1) { - read_number_nocheck(data, temp); - num |= static_cast<unsigned long>(temp) << 8; - } - - read_number_nocheck(data, temp); - num |= static_cast<unsigned long>(temp); - - read_guard(data, 0x2002); -} - -template <typename T> -inline T read_long(std::istream& in) { - T num; - read_long(in, num); - return num; -} - -template <typename T> -inline T read_long(const char *& data) { - T num; - read_long(data, num); - return num; -} - -void read_string(std::istream& in, string& str); -void read_string(const char *& data, string& str); -void read_string(const char *& data, string * str); - -inline string read_string(std::istream& in) { - string temp; - read_string(in, temp); - return temp; -} - -inline string read_string(const char *& data) { - string temp; - read_string(data, temp); - return temp; -} - - -template <typename T> -inline void write_number_nocheck(std::ostream& out, T num) { - out.write(reinterpret_cast<char *>(&num), sizeof(num)); -} - -#if DEBUG_LEVEL >= ALPHA -#define write_guard(out, id) \ - write_number_nocheck<unsigned short>(out, id) -#else -#define write_guard(in, id) -#endif - -template <typename T> -inline void write_number(std::ostream& out, T num) { - write_guard(out, 0x2003); - out.write(reinterpret_cast<char *>(&num), sizeof(num)); - write_guard(out, 0x2004); -} - -void write_bool(std::ostream& out, bool num); - -template <typename T> -void write_long(std::ostream& out, T num) -{ - write_guard(out, 0x2001); - - unsigned char len = 4; - if (static_cast<unsigned long>(num) < 0x00000100UL) - len = 1; - else if (static_cast<unsigned long>(num) < 0x00010000UL) - len = 2; - else if (static_cast<unsigned long>(num) < 0x01000000UL) - len = 3; - write_number_nocheck<unsigned char>(out, len); - - unsigned char temp; - if (len > 3) { - temp = (static_cast<unsigned long>(num) & 0xFF000000UL) >> 24; - write_number_nocheck(out, temp); - } - if (len > 2) { - temp = (static_cast<unsigned long>(num) & 0x00FF0000UL) >> 16; - write_number_nocheck(out, temp); - } - if (len > 1) { - temp = (static_cast<unsigned long>(num) & 0x0000FF00UL) >> 8; - write_number_nocheck(out, temp); - } - - temp = (static_cast<unsigned long>(num) & 0x000000FFUL); - write_number_nocheck(out, temp); - - write_guard(out, 0x2002); -} - -void write_string(std::ostream& out, const string& str); - -template <typename T> -inline void write_object(std::ostream& out, const T& journal) { - assert(false); -} - -} // namespace binary -} // namespace ledger - -#endif // BINARY_H diff --git a/src/utility/context.h b/src/utility/context.h deleted file mode 100644 index 411e6821..00000000 --- a/src/utility/context.h +++ /dev/null @@ -1,142 +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 _CONTEXT_H -#define _CONTEXT_H - -namespace ledger { - -class context -{ -public: - string description; // ex: 'While parsing file "%R" at line %L' - - context() throw() {} - explicit context(const string& _description) throw() - : description(_description) {} - - virtual ~context() {} -}; - -class file_context : public context -{ -public: - path pathname; // ex: ledger.dat - - uint_least32_t linenum_beg; // ex: 1010 - uint_least32_t linenum_end; // ex: 1010 - uint_least32_t position_beg; - uint_least32_t position_end; - - optional<uint_least32_t> colnum_beg; // ex: 8 - optional<uint_least32_t> colnum_end; // ex: 8 - - explicit file_context(const path& _pathname, - const uint_least32_t _linenum_beg, - const uint_least32_t _linenum_end, - const uint_least32_t _position_beg, - const uint_least32_t _position_end) throw() - : context(""), - pathname(_pathname), - linenum_beg(_linenum_beg), - linenum_end(_linenum_end), - position_beg(_position_beg), - position_end(_position_end) {} -}; - -class string_context : public context -{ -public: - string text; // ex: (The multi-line text of an entry) - - optional<uint_least32_t> linenum_beg_off; // ex: 2 - optional<uint_least32_t> linenum_end_off; // ex: 2 - optional<uint_least32_t> colnum_beg_off; // ex: 8 - optional<uint_least32_t> colnum_end_off; // ex: 8 -}; - -#if 0 - -class file_context : public error_context -{ - protected: - string file; - unsigned long line; - public: - file_context(const string& _file, unsigned long _line, - const string& _desc = "") throw() - : error_context(_desc), file(_file), line(_line) {} - virtual ~file_context() throw() {} - - virtual void describe(std::ostream& out) const throw() { - if (! desc.empty()) - out << desc << " "; - - out << "\"" << file << "\", line " << line << ": "; - } -}; - -class line_context : public error_context { - public: - string line; - long pos; - - line_context(const string& _line, long _pos, - const string& _desc = "") throw() - : error_context(_desc), line(_line), pos(_pos) {} - virtual ~line_context() throw() {} - - virtual void describe(std::ostream& out) const throw() { - if (! desc.empty()) - out << desc << std::endl; - - out << " " << line << std::endl << " "; - long idx = pos < 0 ? line.length() - 1 : pos; - for (int i = 0; i < idx; i++) - out << " "; - out << "^" << std::endl; - } -}; - -#endif - -extern ptr_list<context> context_stack; - -#define PUSH_CONTEXT() try { -#define POP_CONTEXT(ctxt) \ - } catch (...) { \ - context_stack.push_front(new ctxt); \ - throw; \ - } - -} // namespace ledger - -#endif // _CONTEXT_H diff --git a/src/utility/flags.h b/src/utility/flags.h deleted file mode 100644 index 5ae8b60f..00000000 --- a/src/utility/flags.h +++ /dev/null @@ -1,103 +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 _FLAGS_H -#define _FLAGS_H - -template <typename T = boost::uint_least8_t> -class supports_flags -{ -public: - typedef T flags_t; - -protected: - flags_t flags_; - -public: - supports_flags() : flags_(0) {} - supports_flags(const flags_t arg) : flags_(arg) {} - - flags_t flags() const { - return flags_; - } - bool has_flags(const flags_t arg) const { - return flags_ & arg; - } - - void set_flags(const flags_t arg) { - flags_ = arg; - } - void clear_flags() { - flags_ = 0; - } - void add_flags(const flags_t arg) { - flags_ |= arg; - } - void drop_flags(const flags_t arg) { - flags_ &= ~arg; - } -}; - -template <typename T = boost::uint_least8_t> -class delegates_flags : public boost::noncopyable -{ -public: - typedef T flags_t; - -protected: - supports_flags<T>& flags_; - -public: - delegates_flags() : flags_() {} - delegates_flags(supports_flags<T>& arg) : flags_(arg) {} - - flags_t flags() const { - return flags_.flags(); - } - bool has_flags(const flags_t arg) const { - return flags_.has_flags(arg); - } - - void set_flags(const flags_t arg) { - flags_.set_flags(arg); - } - void clear_flags() { - flags_.clear_flags(); - } - void add_flags(const flags_t arg) { - flags_.add_flags(arg); - } - void drop_flags(const flags_t arg) { - flags_.drop_flags(arg); - } -}; - -#endif // _FLAGS_H diff --git a/src/utility/mask.cc b/src/utility/mask.cc deleted file mode 100644 index 959df8ea..00000000 --- a/src/utility/mask.cc +++ /dev/null @@ -1,55 +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 "mask.h" - -namespace ledger { - -mask_t::mask_t(const 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++; - } - - expr.assign(p); -} - -} // namespace ledger diff --git a/src/utility/mask.h b/src/utility/mask.h deleted file mode 100644 index daae014f..00000000 --- a/src/utility/mask.h +++ /dev/null @@ -1,55 +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 _MASK_H -#define _MASK_H - -#include "utils.h" - -namespace ledger { - -class mask_t -{ - public: - bool exclude; - boost::regex expr; - - explicit mask_t(const string& pattern); - mask_t(const mask_t& m) : exclude(m.exclude), expr(m.expr) {} - - bool match(const string& str) const { - return boost::regex_match(str, expr) && ! exclude; - } -}; - -} // namespace ledger - -#endif // _MASK_H diff --git a/src/utility/pushvar.h b/src/utility/pushvar.h deleted file mode 100644 index 1a9bdcbc..00000000 --- a/src/utility/pushvar.h +++ /dev/null @@ -1,214 +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. - */ - -/** - * @file scopevar.h - * @author John Wiegley - * @date Sun May 6 20:10:52 2007 - * - * @brief Adds a facility to C++ for handling "scoped executions". - * - * There are sometimes cases where you would like to guarantee that - * something happens at the end of a scope, such as calling a function - * to close a resource for you. - * - * The common idiom for this has become to write a helper class whose - * destructor will call that function. Of course, it must then be - * passed information from the calling scope to hold onto as state - * information, since the code within the class itself has no access - * to its points of use. - * - * This type of solution is cumbersome enough that it's sometimes - * avoided. Take calling pthread_mutex_unlock(pthread_mutex_t *) for - * example. A typical snippet of safe C++ code might look like this: - * - * @code - * void foo(pthread_mutex_t * mutex) { - * if (pthread_mutex_lock(mutex) == 0) { - * try { - * // Do work that requires the mutex to be locked; then... - * pthread_mutex_unlock(mutex); - * } - * catch (std::logic_error& exc) { - * // This is an exception we actually handle, and then exit - * pthread_mutex_unlock(mutex); - * } - * catch (...) { - * // These are exceptions we do not handle, but still the - * // mutex must be unlocked - * pthread_mutex_unlock(mutex); - * throw; - * } - * } - * } - * @endcode - * - * The alternative to this, as mentioned above, is to create a helper - * class named pthread_scoped_lock, which might look like this: - * - * @code - * class pthread_scoped_lock : public boost::noncopyable { - * pthread_mutex_t * mutex; - * public: - * explicit pthread_scoped_lock(pthread_mutex_t * locked_mutex) - * : mutex(locked_mutex) {} - * ~pthread_scoped_lock() { - * pthread_mutex_unlock(mutex); - * } - * }; - * @endcode - * - * Although this helper class is just as much work as writing the code - * above, it only needs to be written once. Now the access code can - * look like this: - * - * @code - * void foo(pthread_mutex_t * mutex) { - * if (pthread_mutex_lock(mutex) == 0) { - * pthread_scoped_lock(mutex); - * try { - * // Do work that requires the mutex to be locked - * } - * catch (std::logic_error& exc) { - * // This is an exception we actually handle, and then exit - * } - * } - * } - * @endcode - * - * But what if it could be even easier? That is what this file is - * 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 - * like: - * - * @code - * void foo(pthread_mutex_t * mutex) { - * if (pthread_mutex_lock(mutex) == 0) { - * scopevar<void> unlock_mutex - * (boost::bind(pthread_mutex_unlock, mutex)); - * try { - * // Do work that requires the mutex to be locked - * } - * catch (std::logic_error& exc) { - * // This is an exception we actually handle, and then exit - * } - * } - * } - * @endcode - * - * The advantage here is that no helper class ever needs to created, - * and hence no bugs from such helper classes can creep into the code. - * The single call to boost::bind creates a closure binding that will - * be invoked once the containing scope has terminated. - * - * Another kind of scopevar is useful for setting the values of - * variables to a predetermined value upon completion of a scope. - * Consider this example: - * - * @code - * bool foo_was_run; - * - * void foo() { - * 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. - * } - * @endcode - * - * In this case, the Boost.Lambda library is used to create an - * anonymous functor whose job is to set the global variable - * `foo_was_run' to a predetermined value. - * - * Lastly, there is another helper class, `scoped_variable' whose job - * is solely to return variables to the value they had at the moment - * the scoped_variable class was instantiated. For example, let's say - * you have a `bar' variable that you want to work on, but you want to - * guarantee that its value is restored upon exiting the scope. This - * can be useful in recursion, for "pushing" and "popping" variable - * values during execution, for example: - * - * @code - * std::string bar = "Hello"; - * void foo() { - * scoped_variable<std::string> restore_bar(bar); - * bar = "Goodbye"; - * // do work with the changed bar; it gets restored upon exit - * } - * @endcode - * - * As a shortcut, you can specify the new value for the pushed - * variable as a second constructor argument: - * - * @code - * std::string bar = "Hello"; - * void foo() { - * scoped_variable<std::string> restore_bar(bar, "Goodbye"); - * // do work with the changed bar; it gets restored upon exit - * } - * @endcode - * - * 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 _SCOPEVAR_H -#define _SCOPEVAR_H - -template <typename T> -class push_variable : public boost::noncopyable -{ - T& var; - T prev; - bool enabled; - -public: - explicit push_variable(T& _var) - : var(_var), prev(var), enabled(true) {} - explicit push_variable(T& _var, const T& value) - : var(_var), prev(var), enabled(true) { - var = value; - } - ~push_variable() { - if (enabled) - var = prev; - } - - void clear() { - enabled = false; - } -}; - -#endif // _SCOPEVAR_H diff --git a/src/utility/system.hh b/src/utility/system.hh deleted file mode 100644 index 92fc5874..00000000 --- a/src/utility/system.hh +++ /dev/null @@ -1,161 +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 _SYSTEM_HH -#define _SYSTEM_HH - -/** - * @file system.hh - * @author John Wiegley - * @date Mon Apr 23 03:43:05 2007 - * - * @brief All system headers needed by Ledger. - * - * These are collected here so that a pre-compiled header can be made. - * None of these header files (with the exception of acconf.h, when - * configure is re-run) are expected to change. - */ - -#include "acconf.h" - -#if defined(__GNUG__) && __GNUG__ < 3 -#define _XOPEN_SOURCE -#endif - -#include <algorithm> -#include <exception> -#include <stdexcept> -#include <iostream> -#include <streambuf> -#include <iomanip> -#include <fstream> -#include <sstream> -#include <iterator> -#include <list> -#include <map> -#include <memory> -#include <new> -#include <stack> -#include <string> -#include <vector> - -#if defined(__GNUG__) && __GNUG__ < 3 -namespace std { - inline ostream & right (ostream & i) { - i.setf(i.right, i.adjustfield); - return i; - } - inline ostream & left (ostream & i) { - i.setf(i.left, i.adjustfield); - return i; - } -} -#endif - -#include <cassert> -#include <cctype> -#include <cstdarg> -#include <cstdio> -#include <cstdlib> -#include <cstring> -#include <ctime> - -#if defined __FreeBSD__ && __FreeBSD__ <= 4 -// FreeBSD has a broken isspace macro, so don't use it -#undef isspace(c) -#endif - -#include <sys/stat.h> - -#ifdef WIN32 -#include <io.h> -#else -#include <unistd.h> -#endif - -#if defined(HAVE_GETPWUID) || defined(HAVE_GETPWNAM) -#include <pwd.h> -#endif - -#if defined(HAVE_NL_LANGINFO) -#include <langinfo.h> -#endif - -#include <gmp.h> - -#define HAVE_GDTOA 1 -#ifdef HAVE_GDTOA -#include "gdtoa.h" -#endif - -extern "C" { -#if defined(HAVE_EXPAT) -#include <expat.h> // expat XML parser -#elif defined(HAVE_XMLPARSE) -#include <xmlparse.h> // expat XML parser -#endif -} - -#if defined(HAVE_LIBOFX) -#include <libofx.h> -#endif - -#include <boost/algorithm/string/classification.hpp> -#include <boost/algorithm/string/predicate.hpp> -#include <boost/any.hpp> -#include <boost/cast.hpp> -#include <boost/current_function.hpp> -#include <boost/date_time/posix_time/posix_time.hpp> -#include <boost/filesystem/convenience.hpp> -#include <boost/filesystem/exception.hpp> -#include <boost/filesystem/fstream.hpp> -#include <boost/filesystem/operations.hpp> -#include <boost/filesystem/path.hpp> -#include <boost/foreach.hpp> -#include <boost/function.hpp> -#include <boost/intrusive_ptr.hpp> -#include <boost/lambda/bind.hpp> -#include <boost/lambda/lambda.hpp> -#include <boost/lexical_cast.hpp> -#include <boost/multi_index/hashed_index.hpp> -#include <boost/multi_index/key_extractors.hpp> -#include <boost/multi_index/ordered_index.hpp> -#include <boost/multi_index/random_access_index.hpp> -#include <boost/multi_index/sequenced_index.hpp> -#include <boost/multi_index_container.hpp> -#include <boost/operators.hpp> -#include <boost/optional.hpp> -#include <boost/ptr_container/ptr_list.hpp> -#include <boost/ptr_container/ptr_vector.hpp> -#include <boost/regex.hpp> -#include <boost/variant.hpp> - -#endif // _SYSTEM_HH diff --git a/src/utility/times.cc b/src/utility/times.cc deleted file mode 100644 index fc6f2f1b..00000000 --- a/src/utility/times.cc +++ /dev/null @@ -1,80 +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 "utils.h" - -namespace ledger { - -#ifdef BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK -const ptime time_now = boost::posix_time::microsec_clock::universal_time(); -#else -const ptime time_now = boost::posix_time::second_clock::universal_time(); -#endif -const date date_now = boost::gregorian::day_clock::universal_day(); - -#ifdef SUPPORT_DATE_AND_TIME -const moment_t& now(time_now); -#else -const moment_t& now(date_now); -#endif - -bool day_before_month = false; -static bool day_before_month_initialized = false; - -moment_t parse_datetime(const char * str) -{ - if (! day_before_month_initialized) { -#ifdef HAVE_NL_LANGINFO - const char * d_fmt = nl_langinfo(D_FMT); - if (d_fmt && std::strlen(d_fmt) > 1 && d_fmt[1] == 'd') - day_before_month = true; - day_before_month_initialized = true; -#endif - } -#if 0 - return parse_abs_datetime(in); -#else - int year = ((str[0] - '0') * 1000 + - (str[1] - '0') * 100 + - (str[2] - '0') * 10 + - (str[3] - '0')); - - int mon = ((str[5] - '0') * 10 + - (str[6] - '0')); - - int day = ((str[8] - '0') * 10 + - (str[9] - '0')); - - return moment_t(boost::gregorian::date(year, mon, day)); -#endif -} - -} // namespace ledger diff --git a/src/utility/times.h b/src/utility/times.h deleted file mode 100644 index 949d8031..00000000 --- a/src/utility/times.h +++ /dev/null @@ -1,123 +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 _TIMES_H -#define _TIMES_H - -namespace ledger { - -#define SUPPORT_DATE_AND_TIME 1 -#ifdef SUPPORT_DATE_AND_TIME - -typedef boost::posix_time::ptime moment_t; -typedef moment_t::time_duration_type duration_t; - -inline bool is_valid_moment(const moment_t& moment) { - return ! moment.is_not_a_date_time(); -} - -#else // SUPPORT_DATE_AND_TIME - -typedef boost::gregorian::date moment_t; -typedef boost::gregorian::date_duration duration_t; - -inline bool is_valid_moment(const moment_t& moment) { - return ! moment.is_not_a_date(); -} - -#endif // SUPPORT_DATE_AND_TIME - -extern const moment_t& now; - -DECLARE_EXCEPTION(datetime_error); - -class interval_t -{ -public: - interval_t() {} - interval_t(const string&) {} - - operator bool() const { - return false; - } - - void start(const moment_t&) {} - moment_t next() const { return moment_t(); } - - void parse(std::istream&) {} -}; - -#if 0 -inline moment_t ptime_local_to_utc(const moment_t& when) { - struct std::tm tm_gmt = to_tm(when); - return boost::posix_time::from_time_t(std::mktime(&tm_gmt)); -} - -// jww (2007-04-18): I need to make a general parsing function -// instead, and then make these into private methods. -inline moment_t ptime_from_local_date_string(const string& date_string) { - return ptime_local_to_utc(moment_t(boost::gregorian::from_string(date_string), - time_duration())); -} - -inline moment_t ptime_from_local_time_string(const string& time_string) { - return ptime_local_to_utc(boost::posix_time::time_from_string(time_string)); -} -#endif - -moment_t parse_datetime(const char * str); - -inline moment_t parse_datetime(const string& str) { - return parse_datetime(str.c_str()); -} - -extern const ptime time_now; -extern const date date_now; -extern bool day_before_month; - -#if 0 -struct intorchar -{ - int ival; - string sval; - - intorchar() : ival(-1) {} - intorchar(int val) : ival(val) {} - intorchar(const string& val) : ival(-1), sval(val) {} - intorchar(const intorchar& o) : ival(o.ival), sval(o.sval) {} -}; - -ledger::moment_t parse_abs_datetime(std::istream& input); -#endif - -} // namespace ledger - -#endif // _TIMES_H diff --git a/src/utility/utils.cc b/src/utility/utils.cc deleted file mode 100644 index 7ddeb677..00000000 --- a/src/utility/utils.cc +++ /dev/null @@ -1,720 +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 "utils.h" - -/********************************************************************** - * - * Assertions - */ - -#if defined(ASSERTS_ON) - -namespace ledger { - -DECLARE_EXCEPTION(assertion_failed); - -void debug_assert(const string& reason, - const string& func, - const string& file, - unsigned long line) -{ - std::ostringstream buf; - buf << "Assertion failed in \"" << file << "\", line " << line - << ": " << func << ": " << reason; - throw assertion_failed(buf.str()); -} - -} // namespace ledger - -#endif - -/********************************************************************** - * - * Verification (basically, very slow asserts) - */ - -#if defined(VERIFY_ON) - -namespace ledger { - -bool verify_enabled = false; - -typedef std::pair<std::string, std::size_t> allocation_pair; -typedef std::map<void *, allocation_pair> live_memory_map; -typedef std::multimap<void *, allocation_pair> live_objects_map; - -typedef std::pair<unsigned int, std::size_t> count_size_pair; -typedef std::map<std::string, count_size_pair> object_count_map; - -static live_memory_map * live_memory = NULL; -static object_count_map * live_memory_count = NULL; -static object_count_map * total_memory_count = NULL; - -static bool memory_tracing_active = false; - -static live_objects_map * live_objects = NULL; -static object_count_map * live_object_count = NULL; -static object_count_map * total_object_count = NULL; -static object_count_map * total_ctor_count = NULL; - -void initialize_memory_tracing() -{ - memory_tracing_active = false; - - live_memory = new live_memory_map; - live_memory_count = new object_count_map; - total_memory_count = new object_count_map; - - live_objects = new live_objects_map; - live_object_count = new object_count_map; - total_object_count = new object_count_map; - total_ctor_count = new object_count_map; - - memory_tracing_active = true; -} - -void shutdown_memory_tracing() -{ - memory_tracing_active = false; - - if (live_objects) { - IF_DEBUG("memory.counts") - report_memory(std::cerr, true); - else - IF_DEBUG("memory.counts.live") - report_memory(std::cerr); - else if (live_objects->size() > 0) - report_memory(std::cerr); - } - - checked_delete(live_memory); live_memory = NULL; - checked_delete(live_memory_count); live_memory_count = NULL; - checked_delete(total_memory_count); total_memory_count = NULL; - - checked_delete(live_objects); live_objects = NULL; - checked_delete(live_object_count); live_object_count = NULL; - checked_delete(total_object_count); total_object_count = NULL; - checked_delete(total_ctor_count); total_ctor_count = NULL; -} - -inline void add_to_count_map(object_count_map& the_map, - const char * name, std::size_t size) -{ - object_count_map::iterator k = the_map.find(name); - if (k != the_map.end()) { - (*k).second.first++; - (*k).second.second += size; - } else { - std::pair<object_count_map::iterator, bool> result = - the_map.insert(object_count_map::value_type(name, count_size_pair(1, size))); - VERIFY(result.second); - } -} - -std::size_t current_memory_size() -{ - std::size_t memory_size = 0; - - for (object_count_map::const_iterator i = live_memory_count->begin(); - i != live_memory_count->end(); - i++) - memory_size += (*i).second.second; - - return memory_size; -} - -static void trace_new_func(void * ptr, const char * which, std::size_t size) -{ - memory_tracing_active = false; - - if (! live_memory) return; - - live_memory->insert - (live_memory_map::value_type(ptr, allocation_pair(which, size))); - - add_to_count_map(*live_memory_count, which, size); - add_to_count_map(*total_memory_count, which, size); - add_to_count_map(*total_memory_count, "__ALL__", size); - - memory_tracing_active = true; -} - -static void trace_delete_func(void * ptr, const char * which) -{ - memory_tracing_active = false; - - if (! live_memory) return; - - // Ignore deletions of memory not tracked, since it's possible that - // a user (like boost) allocated a block of memory before memory - // tracking began, and then deleted it before memory tracking ended. - // If it really is a double-delete, the malloc library on OS/X will - // notify me. - - live_memory_map::iterator i = live_memory->find(ptr); - if (i == live_memory->end()) - return; - - std::size_t size = (*i).second.second; - VERIFY((*i).second.first == which); - - live_memory->erase(i); - - object_count_map::iterator j = live_memory_count->find(which); - VERIFY(j != live_memory_count->end()); - - (*j).second.second -= size; - if (--(*j).second.first == 0) - live_memory_count->erase(j); - - memory_tracing_active = true; -} - -} // namespace ledger - -void * operator new(std::size_t size) throw (std::bad_alloc) { - void * ptr = std::malloc(size); - if (DO_VERIFY() && ledger::memory_tracing_active) - ledger::trace_new_func(ptr, "new", size); - return ptr; -} -void * operator new(std::size_t size, const std::nothrow_t&) throw() { - void * ptr = std::malloc(size); - if (DO_VERIFY() && ledger::memory_tracing_active) - ledger::trace_new_func(ptr, "new", size); - return ptr; -} -void * operator new[](std::size_t size) throw (std::bad_alloc) { - void * ptr = std::malloc(size); - if (DO_VERIFY() && ledger::memory_tracing_active) - ledger::trace_new_func(ptr, "new[]", size); - return ptr; -} -void * operator new[](std::size_t size, const std::nothrow_t&) throw() { - void * ptr = std::malloc(size); - if (DO_VERIFY() && ledger::memory_tracing_active) - ledger::trace_new_func(ptr, "new[]", size); - return ptr; -} -void operator delete(void * ptr) throw() { - if (DO_VERIFY() && ledger::memory_tracing_active) - ledger::trace_delete_func(ptr, "new"); - std::free(ptr); -} -void operator delete(void * ptr, const std::nothrow_t&) throw() { - if (DO_VERIFY() && ledger::memory_tracing_active) - ledger::trace_delete_func(ptr, "new"); - std::free(ptr); -} -void operator delete[](void * ptr) throw() { - if (DO_VERIFY() && ledger::memory_tracing_active) - ledger::trace_delete_func(ptr, "new[]"); - std::free(ptr); -} -void operator delete[](void * ptr, const std::nothrow_t&) throw() { - if (DO_VERIFY() && ledger::memory_tracing_active) - ledger::trace_delete_func(ptr, "new[]"); - std::free(ptr); -} - -namespace ledger { - -inline void report_count_map(std::ostream& out, object_count_map& the_map) -{ - for (object_count_map::iterator i = the_map.begin(); - i != the_map.end(); - i++) - out << " " << std::right << std::setw(12) << (*i).second.first - << " " << std::right << std::setw(12) << (*i).second.second - << " " << std::left << (*i).first - << std::endl; -} - -std::size_t current_objects_size() -{ - std::size_t objects_size = 0; - - for (object_count_map::const_iterator i = live_object_count->begin(); - i != live_object_count->end(); - i++) - objects_size += (*i).second.second; - - return objects_size; -} - -void trace_ctor_func(void * ptr, const char * cls_name, const char * args, - std::size_t cls_size) -{ - memory_tracing_active = false; - - if (! live_objects) return; - - static char name[1024]; - std::strcpy(name, cls_name); - std::strcat(name, "("); - std::strcat(name, args); - std::strcat(name, ")"); - - DEBUG("memory.debug", "TRACE_CTOR " << ptr << " " << name); - - live_objects->insert - (live_objects_map::value_type(ptr, allocation_pair(cls_name, cls_size))); - - add_to_count_map(*live_object_count, cls_name, cls_size); - add_to_count_map(*total_object_count, cls_name, cls_size); - add_to_count_map(*total_object_count, "__ALL__", cls_size); - add_to_count_map(*total_ctor_count, name, cls_size); - - memory_tracing_active = true; -} - -void trace_dtor_func(void * ptr, const char * cls_name, std::size_t cls_size) -{ - memory_tracing_active = false; - - if (! live_objects) return; - - DEBUG("memory.debug", "TRACE_DTOR " << ptr << " " << cls_name); - - live_objects_map::iterator i = live_objects->find(ptr); - VERIFY(i != live_objects->end()); - - int ptr_count = live_objects->count(ptr); - for (int x = 0; x < ptr_count; x++, i++) { - if ((*i).second.first == cls_name) { - live_objects->erase(i); - break; - } - } - - object_count_map::iterator k = live_object_count->find(cls_name); - VERIFY(k != live_object_count->end()); - - (*k).second.second -= cls_size; - if (--(*k).second.first == 0) - live_object_count->erase(k); - - memory_tracing_active = true; -} - -void report_memory(std::ostream& out, bool report_all) -{ - if (! live_memory) return; - - if (live_memory_count->size() > 0) { - out << "NOTE: There may be memory held by Boost " - << "and libstdc++ after ledger::shutdown()" << std::endl; - out << "Live memory count:" << std::endl; - report_count_map(out, *live_memory_count); - } - - if (live_memory->size() > 0) { - out << "Live memory:" << std::endl; - - for (live_memory_map::const_iterator i = live_memory->begin(); - i != live_memory->end(); - i++) - out << " " << std::right << std::setw(7) << (*i).first - << " " << std::right << std::setw(7) << (*i).second.second - << " " << std::left << (*i).second.first - << std::endl; - } - - if (report_all && total_memory_count->size() > 0) { - out << "Total memory counts:" << std::endl; - report_count_map(out, *total_memory_count); - } - - if (live_object_count->size() > 0) { - out << "Live object count:" << std::endl; - report_count_map(out, *live_object_count); - } - - if (live_objects->size() > 0) { - out << "Live objects:" << std::endl; - - for (live_objects_map::const_iterator i = live_objects->begin(); - i != live_objects->end(); - i++) - out << " " << std::right << std::setw(7) << (*i).first - << " " << std::right << std::setw(7) << (*i).second.second - << " " << std::left << (*i).second.first - << std::endl; - } - - if (report_all) { - if (total_object_count->size() > 0) { - out << "Total object counts:" << std::endl; - report_count_map(out, *total_object_count); - } - - if (total_ctor_count->size() > 0) { - out << "Total constructor counts:" << std::endl; - report_count_map(out, *total_ctor_count); - } - } -} - - -string::string() : std::string() { - TRACE_CTOR(string, ""); -} -string::string(const string& str) : std::string(str) { - TRACE_CTOR(string, "const string&"); -} -string::string(const std::string& str) : std::string(str) { - TRACE_CTOR(string, "const std::string&"); -} -string::string(const int len, char x) : std::string(len, x) { - TRACE_CTOR(string, "const int, char"); -} -string::string(const char * str) : std::string(str) { - TRACE_CTOR(string, "const char *"); -} -string::string(const char * str, const char * end) : std::string(str, end) { - TRACE_CTOR(string, "const char *, const char *"); -} -string::string(const string& str, int x) : std::string(str, x) { - TRACE_CTOR(string, "const string&, int"); -} -string::string(const string& str, int x, int y) : std::string(str, x, y) { - TRACE_CTOR(string, "const string&, int, int"); -} -string::string(const char * str, int x) : std::string(str, x) { - TRACE_CTOR(string, "const char *, int"); -} -string::string(const char * str, int x, int y) : std::string(str, x, y) { - TRACE_CTOR(string, "const char *, int, int"); -} -string::~string() { - TRACE_DTOR(string); -} - -} // namespace ledger - -#endif // VERIFY_ON - -/********************************************************************** - * - * Logging - */ - -#if defined(LOGGING_ON) - -namespace ledger { - -log_level_t _log_level = LOG_WARN; -std::ostream * _log_stream = &std::cerr; -std::ostringstream _log_buffer; - -#if defined(TRACING_ON) -unsigned int _trace_level; -#endif - -#ifdef BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK -#define CURRENT_TIME() boost::posix_time::microsec_clock::universal_time() -#else -#define CURRENT_TIME() boost::posix_time::second_clock::universal_time() -#endif - -static inline void stream_memory_size(std::ostream& out, std::size_t size) -{ - if (size < 1024) - out << size << 'b'; - else if (size < (1024 * 1024)) - out << (double(size) / 1024.0) << 'K'; - else if (size < (1024 * 1024 * 1024)) - out << (double(size) / (1024.0 * 1024.0)) << 'M'; - else - out << (double(size) / (1024.0 * 1024.0 * 1024.0)) << 'G'; -} - -static bool logger_has_run = false; -static ptime logger_start; - -bool logger_func(log_level_t level) -{ - unsigned long appender = 0; - - if (! logger_has_run) { - logger_has_run = true; - logger_start = CURRENT_TIME(); - - IF_VERIFY() - *_log_stream << " TIME OBJSZ MEMSZ" << std::endl; - - appender = (logger_start - now).total_milliseconds(); - } - - *_log_stream << std::right << std::setw(5) - << (CURRENT_TIME() - logger_start).total_milliseconds(); - - IF_VERIFY() { - *_log_stream << std::right << std::setw(6) << std::setprecision(3); - stream_memory_size(*_log_stream, current_objects_size()); - *_log_stream << std::right << std::setw(6) << std::setprecision(3); - stream_memory_size(*_log_stream, current_memory_size()); - } - - *_log_stream << " " << std::left << std::setw(7); - - switch (level) { - case LOG_CRIT: *_log_stream << "[CRIT]"; break; - case LOG_FATAL: *_log_stream << "[FATAL]"; break; - case LOG_ASSERT: *_log_stream << "[ASSRT]"; break; - case LOG_ERROR: *_log_stream << "[ERROR]"; break; - case LOG_VERIFY: *_log_stream << "[VERFY]"; break; - case LOG_WARN: *_log_stream << "[WARN]"; break; - case LOG_INFO: *_log_stream << "[INFO]"; break; - case LOG_EXCEPT: *_log_stream << "[EXCPT]"; break; - case LOG_DEBUG: *_log_stream << "[DEBUG]"; break; - case LOG_TRACE: *_log_stream << "[TRACE]"; break; - - case LOG_OFF: - case LOG_ALL: - assert(false); - break; - } - - *_log_stream << ' ' << _log_buffer.str(); - - if (appender) - *_log_stream << " (" << appender << "ms startup)"; - - *_log_stream << std::endl; - - _log_buffer.str(""); - - return true; -} - -} // namespace ledger - -#if defined(DEBUG_ON) - -namespace ledger { - -optional<std::string> _log_category; - -} // namespace ledger - -#endif // DEBUG_ON -#endif // LOGGING_ON - -/********************************************************************** - * - * Timers (allows log entries to specify cumulative time spent) - */ - -#if defined(LOGGING_ON) && defined(TIMERS_ON) - -namespace ledger { - -struct timer_t { - log_level_t level; - ptime begin; - time_duration spent; - std::string description; - bool active; - - timer_t(log_level_t _level, std::string _description) - : level(_level), begin(CURRENT_TIME()), - spent(time_duration(0, 0, 0, 0)), - description(_description), active(true) {} -}; - -typedef std::map<std::string, timer_t> timer_map; - -static timer_map timers; - -void start_timer(const char * name, log_level_t lvl) -{ -#if defined(VERIFY_ON) - memory_tracing_active = false; -#endif - - timer_map::iterator i = timers.find(name); - if (i == timers.end()) { - timers.insert(timer_map::value_type(name, timer_t(lvl, _log_buffer.str()))); - } else { - assert((*i).second.description == _log_buffer.str()); - (*i).second.begin = CURRENT_TIME(); - (*i).second.active = true; - } - _log_buffer.str(""); - -#if defined(VERIFY_ON) - memory_tracing_active = true; -#endif -} - -void stop_timer(const char * name) -{ -#if defined(VERIFY_ON) - memory_tracing_active = false; -#endif - - timer_map::iterator i = timers.find(name); - assert(i != timers.end()); - - (*i).second.spent += CURRENT_TIME() - (*i).second.begin; - (*i).second.active = false; - -#if defined(VERIFY_ON) - memory_tracing_active = true; -#endif -} - -void finish_timer(const char * name) -{ -#if defined(VERIFY_ON) - memory_tracing_active = false; -#endif - - timer_map::iterator i = timers.find(name); - if (i == timers.end()) - return; - - time_duration spent = (*i).second.spent; - if ((*i).second.active) { - spent = CURRENT_TIME() - (*i).second.begin; - (*i).second.active = false; - } - - _log_buffer << (*i).second.description << ' '; - - bool need_paren = - (*i).second.description[(*i).second.description.size() - 1] != ':'; - - if (need_paren) - _log_buffer << '('; - - _log_buffer << spent.total_milliseconds() << "ms"; - - if (need_paren) - _log_buffer << ')'; - - logger_func((*i).second.level); - - timers.erase(i); - -#if defined(VERIFY_ON) - memory_tracing_active = true; -#endif -} - -} // namespace ledger - -#endif // LOGGING_ON && TIMERS_ON - -/********************************************************************** - * - * Exception handling - */ - -namespace ledger { - -std::ostringstream _exc_buffer; -ptr_list<context> context_stack; - -} // namespace ledger - -/********************************************************************** - * - * General utility functions - */ - -namespace ledger { - -path expand_path(const path& pathname) -{ - if (pathname.empty()) - return pathname; - -#if 1 - return pathname; -#else - // jww (2007-04-30): I need to port this code to use - // boost::filesystem::path - const char * pfx = NULL; - string::size_type pos = pathname.find_first_of('/'); - - if (pathname.length() == 1 || pos == 1) { - pfx = std::getenv("HOME"); -#ifdef HAVE_GETPWUID - if (! pfx) { - // Punt. We're trying to expand ~/, but HOME isn't set - struct passwd * pw = getpwuid(getuid()); - if (pw) - pfx = pw->pw_dir; - } -#endif - } -#ifdef HAVE_GETPWNAM - else { - string user(pathname, 1, pos == string::npos ? - string::npos : pos - 1); - struct passwd * pw = getpwnam(user.c_str()); - if (pw) - pfx = pw->pw_dir; - } -#endif - - // if we failed to find an expansion, return the path unchanged. - - if (! pfx) - return pathname; - - string result(pfx); - - if (pos == string::npos) - return result; - - if (result.length() == 0 || result[result.length() - 1] != '/') - result += '/'; - - result += pathname.substr(pos + 1); - - return result; -#endif -} - -path resolve_path(const path& pathname) -{ - path temp = pathname; - if (temp.string()[0] == '~') - temp = expand_path(temp); - temp.normalize(); - return temp; -} - -} // namespace ledger diff --git a/src/utility/utils.h b/src/utility/utils.h deleted file mode 100644 index b78e716d..00000000 --- a/src/utility/utils.h +++ /dev/null @@ -1,546 +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. - */ - -/** - * @file utils.h - * @author John Wiegley - * @date Sun May 6 21:20:00 2007 - * - * @brief This file contains general utility facilities used by Ledger. - * - * Ledger has need of the following utility code, which this file - * provides or includes in: - * - * - system headers - * - asserts - * - verification (basically, "heavy asserts") - * - tracing code - * - debug logging code - * - timing code - * - current error context - * - exception framework - * - date/time type - * - supports_flags<> for objects that use flags - * - push_variable<> for restoring variable values - */ - -#ifndef _UTILS_H -#define _UTILS_H - -#if defined(DEBUG_MODE) -#define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE 1 -#define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING 1 -#endif - -#include <system.hh> - -/********************************************************************** - * - * Default values - */ - -#if defined(DEBUG_MODE) -#define VERIFY_ON 1 -#define TRACING_ON 1 -#define DEBUG_ON 1 -#define TIMERS_ON 1 -#elif defined(NDEBUG) -#define NO_ASSERTS 1 -#define NO_LOGGING 1 -#else -#define VERIFY_ON 1 // compiled in, use --verify to enable -#define TRACING_ON 1 // use --trace X to enable -#define TIMERS_ON 1 -#endif - -/********************************************************************** - * - * Forward declarations - */ - -namespace ledger { - using namespace boost; - -#if defined(VERIFY_ON) - class string; -#else - typedef std::string string; -#endif - - typedef posix_time::ptime ptime; - typedef ptime::time_duration_type time_duration; - typedef gregorian::date date; - typedef gregorian::date_duration date_duration; - typedef posix_time::seconds seconds; - - typedef boost::filesystem::path path; - typedef boost::filesystem::ifstream ifstream; - typedef boost::filesystem::ofstream ofstream; - typedef boost::filesystem::filesystem_error filesystem_error; -} - -/********************************************************************** - * - * Assertions - */ - -#ifdef assert -#undef assert -#endif - -#if ! defined(NO_ASSERTS) -#define ASSERTS_ON 1 -#endif -#if defined(ASSERTS_ON) - -namespace ledger { - void debug_assert(const string& reason, const string& func, - const string& file, unsigned long line); -} - -#define assert(x) \ - ((x) ? ((void)0) : debug_assert(#x, BOOST_CURRENT_FUNCTION, \ - __FILE__, __LINE__)) - -#else // ! ASSERTS_ON - -#define assert(x) - -#endif // ASSERTS_ON - -/********************************************************************** - * - * Verification (basically, very slow asserts) - */ - -#if defined(VERIFY_ON) - -namespace ledger { - -extern bool verify_enabled; - -#define VERIFY(x) (ledger::verify_enabled ? assert(x) : ((void)0)) -#define DO_VERIFY() ledger::verify_enabled - -void initialize_memory_tracing(); -void shutdown_memory_tracing(); - -std::size_t current_memory_size(); -std::size_t current_objects_size(); - -void trace_ctor_func(void * ptr, const char * cls_name, const char * args, - std::size_t cls_size); -void trace_dtor_func(void * ptr, const char * cls_name, std::size_t cls_size); - -#define TRACE_CTOR(cls, args) \ - (DO_VERIFY() ? trace_ctor_func(this, #cls, args, sizeof(cls)) : ((void)0)) -#define TRACE_DTOR(cls) \ - (DO_VERIFY() ? trace_dtor_func(this, #cls, sizeof(cls)) : ((void)0)) - -void report_memory(std::ostream& out, bool report_all = false); - -/** - * This string type is a wrapper around std::string that allows us to - * trace constructor and destructor calls. - */ -class string : public std::string -{ -public: - string(); - string(const string& str); - string(const std::string& str); - string(const int len, char x); - string(const char * str); - string(const char * str, const char * end); - string(const string& str, int x); - string(const string& str, int x, int y); - string(const char * str, int x); - string(const char * str, int x, int y); - ~string(); -}; - -inline string operator+(const string& __lhs, const string& __rhs) -{ - string __str(__lhs); - __str.append(__rhs); - return __str; -} - -string operator+(const char* __lhs, const string& __rhs); -string operator+(char __lhs, const string& __rhs); - -inline string operator+(const string& __lhs, const char* __rhs) -{ - string __str(__lhs); - __str.append(__rhs); - return __str; -} - -inline string operator+(const string& __lhs, char __rhs) -{ - typedef string __string_type; - typedef string::size_type __size_type; - __string_type __str(__lhs); - __str.append(__size_type(1), __rhs); - return __str; -} - -inline bool operator==(const string& __lhs, const string& __rhs) -{ return __lhs.compare(__rhs) == 0; } - -inline bool operator==(const char* __lhs, const string& __rhs) -{ return __rhs.compare(__lhs) == 0; } - -inline bool operator==(const string& __lhs, const char* __rhs) -{ return __lhs.compare(__rhs) == 0; } - -inline bool operator!=(const string& __lhs, const string& __rhs) -{ return __rhs.compare(__lhs) != 0; } - -inline bool operator!=(const char* __lhs, const string& __rhs) -{ return __rhs.compare(__lhs) != 0; } - -inline bool operator!=(const string& __lhs, const char* __rhs) -{ return __lhs.compare(__rhs) != 0; } - -} // namespace ledger - -#else // ! VERIFY_ON - -#define VERIFY(x) -#define DO_VERIFY() true -#define TRACE_CTOR(cls, args) -#define TRACE_DTOR(cls) - -#endif // VERIFY_ON - -#define IF_VERIFY() if (DO_VERIFY()) - -/********************************************************************** - * - * Logging - */ - -#if ! defined(NO_LOGGING) -#define LOGGING_ON 1 -#endif -#if defined(LOGGING_ON) - -namespace ledger { - -enum log_level_t { - LOG_OFF = 0, - LOG_CRIT, - LOG_FATAL, - LOG_ASSERT, - LOG_ERROR, - LOG_VERIFY, - LOG_WARN, - LOG_INFO, - LOG_EXCEPT, - LOG_DEBUG, - LOG_TRACE, - LOG_ALL -}; - -extern log_level_t _log_level; -extern std::ostream * _log_stream; -extern std::ostringstream _log_buffer; - -bool logger_func(log_level_t level); - -#define LOGGER(cat) \ - static const char * const _this_category = cat - -#if defined(TRACING_ON) - -extern unsigned int _trace_level; - -#define SHOW_TRACE(lvl) \ - (ledger::_log_level >= ledger::LOG_TRACE && lvl <= ledger::_trace_level) -#define TRACE(lvl, msg) \ - (SHOW_TRACE(lvl) ? \ - ((ledger::_log_buffer << msg), \ - ledger::logger_func(ledger::LOG_TRACE)) : false) - -#else // TRACING_ON - -#define SHOW_TRACE(lvl) false -#define TRACE(lvl, msg) - -#endif // TRACING_ON - -#if defined(DEBUG_ON) - -extern optional<std::string> _log_category; - -inline bool category_matches(const char * cat) { - return _log_category && starts_with(cat, *_log_category); -} - -#define SHOW_DEBUG(cat) \ - (ledger::_log_level >= ledger::LOG_DEBUG && ledger::category_matches(cat)) -#define SHOW_DEBUG_() SHOW_DEBUG(_this_category) - -#define DEBUG(cat, msg) \ - (SHOW_DEBUG(cat) ? \ - ((ledger::_log_buffer << msg), \ - ledger::logger_func(ledger::LOG_DEBUG)) : false) -#define DEBUG_(msg) DEBUG(_this_category, msg) - -#else // DEBUG_ON - -#define SHOW_DEBUG(cat) false -#define SHOW_DEBUG_() false -#define DEBUG(cat, msg) -#define DEBUG_(msg) - -#endif // DEBUG_ON - -#define LOG_MACRO(level, msg) \ - (ledger::_log_level >= level ? \ - ((ledger::_log_buffer << msg), ledger::logger_func(level)) : false) - -#define SHOW_INFO() (ledger::_log_level >= ledger::LOG_INFO) -#define SHOW_WARN() (ledger::_log_level >= ledger::LOG_WARN) -#define SHOW_ERROR() (ledger::_log_level >= ledger::LOG_ERROR) -#define SHOW_FATAL() (ledger::_log_level >= ledger::LOG_FATAL) -#define SHOW_CRITICAL() (ledger::_log_level >= ledger::LOG_CRIT) - -#define INFO(msg) LOG_MACRO(ledger::LOG_INFO, msg) -#define WARN(msg) LOG_MACRO(ledger::LOG_WARN, msg) -#define ERROR(msg) LOG_MACRO(ledger::LOG_ERROR, msg) -#define FATAL(msg) LOG_MACRO(ledger::LOG_FATAL, msg) -#define CRITICAL(msg) LOG_MACRO(ledger::LOG_CRIT, msg) -#define EXCEPTION(msg) LOG_MACRO(ledger::LOG_EXCEPT, msg) - -} // namespace ledger - -#else // ! LOGGING_ON - -#define LOGGER(cat) - -#define SHOW_TRACE(lvl) false -#define SHOW_DEBUG(cat) false -#define SHOW_DEBUG_() false -#define SHOW_INFO() false -#define SHOW_WARN() false -#define SHOW_ERROR() false -#define SHOW_FATAL() false -#define SHOW_CRITICAL() false - -#define TRACE(lvl, msg) -#define DEBUG(cat, msg) -#define DEBUG_(msg) -#define INFO(msg) -#define WARN(msg) -#define ERROR(msg) -#define FATAL(msg) -#define CRITICAL(msg) - -#endif // LOGGING_ON - -#define IF_TRACE(lvl) if (SHOW_TRACE(lvl)) -#define IF_DEBUG(cat) if (SHOW_DEBUG(cat)) -#define IF_DEBUG_() if (SHOW_DEBUG_()) -#define IF_INFO() if (SHOW_INFO()) -#define IF_WARN() if (SHOW_WARN()) -#define IF_ERROR() if (SHOW_ERROR()) -#define IF_FATAL() if (SHOW_FATAL()) -#define IF_CRITICAL() if (SHOW_CRITICAL()) - -/********************************************************************** - * - * Timers (allows log entries to specify cumulative time spent) - */ - -#if defined(LOGGING_ON) && defined(TIMERS_ON) - -namespace ledger { - -void start_timer(const char * name, log_level_t lvl); -void stop_timer(const char * name); -void finish_timer(const char * name); - -#if defined(TRACING_ON) -#define TRACE_START(name, lvl, msg) \ - (SHOW_TRACE(lvl) ? \ - ((ledger::_log_buffer << msg), \ - ledger::start_timer(#name, ledger::LOG_TRACE)) : ((void)0)) -#define TRACE_STOP(name, lvl) \ - (SHOW_TRACE(lvl) ? ledger::stop_timer(#name) : ((void)0)) -#define TRACE_FINISH(name, lvl) \ - (SHOW_TRACE(lvl) ? ledger::finish_timer(#name) : ((void)0)) -#else -#define TRACE_START(name, lvl, msg) -#define TRACE_STOP(name) -#define TRACE_FINISH(name) -#endif - -#if defined(DEBUG_ON) -#define DEBUG_START(name, cat, msg) \ - (SHOW_DEBUG(cat) ? \ - ((ledger::_log_buffer << msg), \ - ledger::start_timer(#name, ledger::LOG_DEBUG)) : ((void)0)) -#define DEBUG_START_(name, msg) \ - DEBUG_START_(name, _this_category, msg) -#define DEBUG_STOP(name, cat) \ - (SHOW_DEBUG(cat) ? ledger::stop_timer(#name) : ((void)0)) -#define DEBUG_STOP_(name) \ - DEBUG_STOP_(name, _this_category) -#define DEBUG_FINISH(name, cat) \ - (SHOW_DEBUG(cat) ? ledger::finish_timer(#name) : ((void)0)) -#define DEBUG_FINISH_(name) \ - DEBUG_FINISH_(name, _this_category) -#else -#define DEBUG_START(name, cat, msg) -#define DEBUG_START_(name, msg) -#define DEBUG_STOP(name) -#define DEBUG_FINISH(name) -#endif - -#define INFO_START(name, msg) \ - (SHOW_INFO() ? \ - ((ledger::_log_buffer << msg), \ - ledger::start_timer(#name, ledger::LOG_INFO)) : ((void)0)) -#define INFO_STOP(name) \ - (SHOW_INFO() ? stop_timer(#name) : ((void)0)) -#define INFO_FINISH(name) \ - (SHOW_INFO() ? finish_timer(#name) : ((void)0)) - -} // namespace ledger - -#else // ! (LOGGING_ON && TIMERS_ON) - -#define TRACE_START(lvl, msg, name) -#define TRACE_STOP(name) -#define TRACE_FINISH(name) - -#define DEBUG_START(name, msg) -#define DEBUG_START_(name, cat, msg) -#define DEBUG_STOP(name) -#define DEBUG_FINISH(name) - -#define INFO_START(name, msg) -#define INFO_STOP(name) -#define INFO_FINISH(name) - -#endif // TIMERS_ON - -/********************************************************************** - * - * Exception handling - */ - -#include "context.h" - -namespace ledger { - -#define DECLARE_EXCEPTION(name) \ - class name : public std::logic_error { \ - public: \ - name(const string& why) throw() : std::logic_error(why) {} \ - } - -extern std::ostringstream _exc_buffer; - -template <typename T> -inline void throw_func(const std::string& message) { - _exc_buffer.str(""); - throw T(message); -} - -#define throw_(cls, msg) \ - ((_exc_buffer << msg), throw_func<cls>(_exc_buffer.str())) - -#if 0 -inline void throw_unexpected_error(char c, char wanted) { - if (c == -1) { - if (wanted) - throw new error(string("Missing '") + wanted + "'"); - else - throw new error("Unexpected end of input"); - } else { - if (wanted) - throw new error(string("Invalid char '") + c + - "' (wanted '" + wanted + "')"); - else - throw new error(string("Invalid char '") + c + "'"); - } -} -#else -inline void throw_unexpected_error(char, char) { -} -#endif - -} // namespace ledger - -/********************************************************************** - * - * 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" - -/********************************************************************** - * - * General utility functions - */ - -#define foreach BOOST_FOREACH - -namespace ledger { - -template <typename T, typename U> -inline T& downcast(U& object) { - return *polymorphic_downcast<T *>(&object); -} - -path resolve_path(const path& pathname); - -#ifdef HAVE_REALPATH -extern "C" char * realpath(const char *, char resolved_path[]); -#endif - -inline const string& either_or(const string& first, - const string& second) { - return first.empty() ? second : first; -} - -} // namespace ledger - -#endif // _UTILS_H |