<commit_before>#ifndef INC_BENCODE_HPP
#define INC_BENCODE_HPP

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <iostream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

// Try to use N4082's any and string_view classes, or fall back to Boost's.
#ifdef __has_include
#  if __has_include(<experimental/any>)
#    include <experimental/any>
#    define BENCODE_ANY_NS std::experimental
#  else
#    include <boost/any.hpp>
#    define BENCODE_ANY_NS boost
#  endif
#  if __has_include(<experimental/string_view>)
#    include <experimental/string_view>
#    define BENCODE_STRING_VIEW std::experimental::string_view
#  else
#    include <boost/utility/string_ref.hpp>
#    define BENCODE_STRING_VIEW boost::string_ref
#  endif
#else
#  include <boost/any.hpp>
#  include <boost/utility/string_ref.hpp>
#  define BENCODE_ANY_NS boost
#  define BENCODE_STRING_VIEW boost::string_ref
#endif

namespace bencode {

  using string = std::string;
  using integer = long long;
  using list = std::vector<BENCODE_ANY_NS::any>;
  using dict = std::map<std::string, BENCODE_ANY_NS::any>;

  using string_view = BENCODE_STRING_VIEW;
  using dict_view = std::map<BENCODE_STRING_VIEW, BENCODE_ANY_NS::any>;

  enum eof_behavior {
    check_eof,
    no_check_eof
  };

  template<typename T>
  BENCODE_ANY_NS::any decode(T &begin, T end);

  namespace detail {
    template<bool View, typename T>
    BENCODE_ANY_NS::any decode_data(T &begin, T end);

    template<typename T>
    integer decode_int_real(T &begin, T end) {
      assert(*begin == 'i');
      ++begin;

      bool positive = true;
      if(*begin == '-') {
        positive = false;
        ++begin;
      }

      // TODO: handle overflow
      integer value = 0;
      for(; begin != end && std::isdigit(*begin); ++begin)
        value = value * 10 + *begin - '0';
      if(begin == end)
        throw std::invalid_argument("unexpected end of string");
      if(*begin != 'e')
        throw std::invalid_argument("expected 'e'");

      ++begin;
      return positive ? value : -value;
    }

    template<bool View, typename T>
    inline integer decode_int(T &begin, T end) {
      return decode_int_real(begin, end);
    }

    template<bool View>
    class str_reader {
    public:
      template<typename Iter, typename Size>
      inline string operator ()(Iter &begin, Iter end, Size len) {
        return call(
          begin, end, len,
          typename std::iterator_traits<Iter>::iterator_category()
        );
      }
    private:
      template<typename Iter, typename Size>
      string call(Iter &begin, Iter end, Size len, std::forward_iterator_tag) {
        if(std::distance(begin, end) < static_cast<std::ptrdiff_t>(len))
          throw std::invalid_argument("unexpected end of string");

        std::string value(len, 0);
        std::copy_n(begin, len, value.begin());
        std::advance(begin, len);
        return value;
      }

      template<typename Iter, typename Size>
      string call(Iter &begin, Iter end, Size len, std::input_iterator_tag) {
        std::string value(len, 0);
        for(Size i = 0; i < len; i++) {
          if(begin == end)
            throw std::invalid_argument("unexpected end of string");
          value[i] = *begin;
          ++begin;
        }
        return value;
      }
    };

    template<>
    class str_reader<true> {
    public:
      template<typename Iter, typename Size>
      string_view operator ()(Iter &begin, Iter end, Size len) {
        if(std::distance(begin, end) < static_cast<std::ptrdiff_t>(len))
          throw std::invalid_argument("unexpected end of string");

        string_view value(begin, len);
        std::advance(begin, len);
        return value;
      }
    };

    template<bool View, typename T>
    typename std::conditional<View, string_view, string>::type
    decode_str(T &begin, T end) {
      assert(std::isdigit(*begin));
      std::size_t len = 0;
      for(; begin != end && std::isdigit(*begin); ++begin)
        len = len * 10 + *begin - '0';

      if(begin == end)
        throw std::invalid_argument("unexpected end of string");
      if(*begin != ':')
        throw std::invalid_argument("expected ':'");
      ++begin;

      return str_reader<View>{}(begin, end, len);
    }

    template<bool View, typename T>
    list decode_list(T &begin, T end) {
      assert(*begin == 'l');
      ++begin;

      list value;
      while(begin != end && *begin != 'e') {
        value.push_back(decode_data<View>(begin, end));
      }

      if(begin == end)
        throw std::invalid_argument("unexpected end of string");

      ++begin;
      return value;
    }

    template<bool View, typename T>
    typename std::conditional<View, dict_view, dict>::type
    decode_dict(T &begin, T end) {
      assert(*begin == 'd');
      ++begin;

      typename std::conditional<View, dict_view, dict>::type value;
      while(begin != end && *begin != 'e') {
        if(!std::isdigit(*begin))
          throw std::invalid_argument("expected string token");

        auto k = decode_str<View>(begin, end);
        auto v = decode_data<View>(begin, end);
        auto result = value.emplace(std::move(k), std::move(v));
        if(!result.second) {
          throw std::invalid_argument(
            "duplicated key in dict: " + std::string(result.first->first)
          );
        }
      }

      if(begin == end)
        throw std::invalid_argument("unexpected end of string");

      ++begin;
      return value;
    }

    template<bool View, typename T>
    BENCODE_ANY_NS::any decode_data(T &begin, T end) {
      if(begin == end)
        return BENCODE_ANY_NS::any();

      if(*begin == 'i')
        return decode_int<View>(begin, end);
      else if(*begin == 'l')
        return decode_list<View>(begin, end);
      else if(*begin == 'd')
        return decode_dict<View>(begin, end);
      else if(std::isdigit(*begin))
        return decode_str<View>(begin, end);

      throw std::invalid_argument("unexpected type");
    }

  }

  template<typename T>
  inline BENCODE_ANY_NS::any decode(T &begin, T end) {
    return detail::decode_data<false>(begin, end);
  }

  template<typename T>
  inline BENCODE_ANY_NS::any decode(const T &begin, T end) {
    T b(begin);
    return detail::decode_data<false>(b, end);
  }

  inline BENCODE_ANY_NS::any decode(const BENCODE_STRING_VIEW &s) {
    return decode(s.begin(), s.end());
  }

  inline BENCODE_ANY_NS::any
  decode(std::istream &s, eof_behavior e = check_eof) {
    std::istreambuf_iterator<char> begin(s), end;
    auto result = decode(begin, end);
    // If we hit EOF, update the parent stream.
    if(e == check_eof && begin == end)
      s.setstate(std::ios_base::eofbit);
    return result;
  }

  template<typename T>
  inline BENCODE_ANY_NS::any decode_view(T &begin, T end) {
    return detail::decode_data<true>(begin, end);
  }

  template<typename T>
  inline BENCODE_ANY_NS::any decode_view(const T &begin, T end) {
    T b(begin);
    return detail::decode_data<true>(b, end);
  }

  inline BENCODE_ANY_NS::any decode_view(const BENCODE_STRING_VIEW &s) {
    return decode_view(s.begin(), s.end());
  }

  class list_encoder {
  public:
    inline list_encoder(std::ostream &os) : os(os) {
      os.put('l');
    }

    template<typename T>
    inline list_encoder & add(T &&value);

    inline void end() {
      os.put('e');
    }
  private:
    std::ostream &os;
  };

  class dict_encoder {
  public:
    inline dict_encoder(std::ostream &os) : os(os) {
      os.put('d');
    }

    template<typename T>
    inline dict_encoder & add(const BENCODE_STRING_VIEW &key, T &&value);

    inline void end() {
      os.put('e');
    }
  private:
    std::ostream &os;
  };

  // TODO: make these use unformatted output?
  inline void encode(std::ostream &os, integer value) {
    os << "i" << value << "e";
  }

  inline void encode(std::ostream &os, const BENCODE_STRING_VIEW &value) {
    os << value.size() << ":" << value;
  }

  template<typename T>
  void encode(std::ostream &os, const std::vector<T> &value) {
    list_encoder e(os);
    for(auto &&i : value)
      e.add(i);
    e.end();
  }

  template<typename T>
  void encode(std::ostream &os, const std::map<std::string, T> &value) {
    dict_encoder e(os);
    for(auto &&i : value)
      e.add(i.first, i.second);
    e.end();
  }

#ifdef BENCODE_HAS_STRING_VIEW
  template<typename T>
  void encode(std::ostream &os, const std::map<BENCODE_STRING_VIEW, T> &value) {
    dict_encoder e(os);
    for(auto &&i : value)
      e.add(i.first, i.second);
    e.end();
  }
#endif

  // Overload for `any`, but only if the passed-in type is already `any`. Don't
  // accept an implicit conversion!
  template<typename T>
  auto encode(std::ostream &os, const T &value) ->
  typename std::enable_if<std::is_same<T, BENCODE_ANY_NS::any>::value>::type {
    using BENCODE_ANY_NS::any_cast;
    auto &type = value.type();

    if(type == typeid(integer))
      encode(os, any_cast<integer>(value));
    else if(type == typeid(string))
      encode(os, any_cast<string>(value));
    else if(type == typeid(list))
      encode(os, any_cast<list>(value));
    else if(type == typeid(dict))
      encode(os, any_cast<dict>(value));
#ifdef BENCODE_HAS_STRING_VIEW
    else if(type == typeid(string_view))
      encode(os, any_cast<string_view>(value));
    else if(type == typeid(dict_view))
      encode(os, any_cast<dict_view>(value));
#endif
    else
      throw std::invalid_argument("unexpected type");
  }

  namespace detail {
    inline void encode_list_items(list_encoder &) {}

    template<typename T, typename ...Rest>
    void encode_list_items(list_encoder &enc, T &&t, Rest &&...rest) {
      enc.add(std::forward<T>(t));
      encode_list_items(enc, std::forward<Rest>(rest)...);
    }

    inline void encode_dict_items(dict_encoder &) {}

    template<typename Key, typename Value, typename ...Rest>
    void encode_dict_items(dict_encoder &enc, Key &&key, Value &&value,
                           Rest &&...rest) {
      enc.add(std::forward<Key>(key), std::forward<Value>(value));
      encode_dict_items(enc, std::forward<Rest>(rest)...);
    }
  }

  template<typename ...T>
  void encode_list(std::ostream &os, T &&...t) {
    list_encoder enc(os);
    detail::encode_list_items(enc, std::forward<T>(t)...);
    enc.end();
  }

  template<typename ...T>
  void encode_dict(std::ostream &os, T &&...t) {
    dict_encoder enc(os);
    detail::encode_dict_items(enc, std::forward<T>(t)...);
    enc.end();
  }

  template<typename T>
  inline list_encoder & list_encoder::add(T &&value) {
    encode(os, std::forward<T>(value));
    return *this;
  }

  template<typename T>
  inline dict_encoder &
  dict_encoder::add(const BENCODE_STRING_VIEW &key, T &&value) {
    encode(os, key);
    encode(os, std::forward<T>(value));
    return *this;
  }

  template<typename T>
  std::string encode(T &&t) {
    std::stringstream ss;
    encode(ss, std::forward<T>(t));
    return ss.str();
  }

}

#endif
<commit_msg>Allow disabling the use of N4480<commit_after>#ifndef INC_BENCODE_HPP
#define INC_BENCODE_HPP

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <iostream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

// Try to use N4480's any and string_view classes, or fall back to Boost's.
#if !defined(BENCODE_NO_STDLIB_EXTS) && defined(__has_include)
#  if __has_include(<experimental/any>)
#    include <experimental/any>
#    define BENCODE_ANY_NS std::experimental
#  else
#    include <boost/any.hpp>
#    define BENCODE_ANY_NS boost
#  endif
#  if __has_include(<experimental/string_view>)
#    include <experimental/string_view>
#    define BENCODE_STRING_VIEW std::experimental::string_view
#  else
#    include <boost/utility/string_ref.hpp>
#    define BENCODE_STRING_VIEW boost::string_ref
#  endif
#else
#  include <boost/any.hpp>
#  include <boost/utility/string_ref.hpp>
#  define BENCODE_ANY_NS boost
#  define BENCODE_STRING_VIEW boost::string_ref
#endif

namespace bencode {

  using string = std::string;
  using integer = long long;
  using list = std::vector<BENCODE_ANY_NS::any>;
  using dict = std::map<std::string, BENCODE_ANY_NS::any>;

  using string_view = BENCODE_STRING_VIEW;
  using dict_view = std::map<BENCODE_STRING_VIEW, BENCODE_ANY_NS::any>;

  enum eof_behavior {
    check_eof,
    no_check_eof
  };

  template<typename T>
  BENCODE_ANY_NS::any decode(T &begin, T end);

  namespace detail {
    template<bool View, typename T>
    BENCODE_ANY_NS::any decode_data(T &begin, T end);

    template<typename T>
    integer decode_int_real(T &begin, T end) {
      assert(*begin == 'i');
      ++begin;

      bool positive = true;
      if(*begin == '-') {
        positive = false;
        ++begin;
      }

      // TODO: handle overflow
      integer value = 0;
      for(; begin != end && std::isdigit(*begin); ++begin)
        value = value * 10 + *begin - '0';
      if(begin == end)
        throw std::invalid_argument("unexpected end of string");
      if(*begin != 'e')
        throw std::invalid_argument("expected 'e'");

      ++begin;
      return positive ? value : -value;
    }

    template<bool View, typename T>
    inline integer decode_int(T &begin, T end) {
      return decode_int_real(begin, end);
    }

    template<bool View>
    class str_reader {
    public:
      template<typename Iter, typename Size>
      inline string operator ()(Iter &begin, Iter end, Size len) {
        return call(
          begin, end, len,
          typename std::iterator_traits<Iter>::iterator_category()
        );
      }
    private:
      template<typename Iter, typename Size>
      string call(Iter &begin, Iter end, Size len, std::forward_iterator_tag) {
        if(std::distance(begin, end) < static_cast<std::ptrdiff_t>(len))
          throw std::invalid_argument("unexpected end of string");

        std::string value(len, 0);
        std::copy_n(begin, len, value.begin());
        std::advance(begin, len);
        return value;
      }

      template<typename Iter, typename Size>
      string call(Iter &begin, Iter end, Size len, std::input_iterator_tag) {
        std::string value(len, 0);
        for(Size i = 0; i < len; i++) {
          if(begin == end)
            throw std::invalid_argument("unexpected end of string");
          value[i] = *begin;
          ++begin;
        }
        return value;
      }
    };

    template<>
    class str_reader<true> {
    public:
      template<typename Iter, typename Size>
      string_view operator ()(Iter &begin, Iter end, Size len) {
        if(std::distance(begin, end) < static_cast<std::ptrdiff_t>(len))
          throw std::invalid_argument("unexpected end of string");

        string_view value(begin, len);
        std::advance(begin, len);
        return value;
      }
    };

    template<bool View, typename T>
    typename std::conditional<View, string_view, string>::type
    decode_str(T &begin, T end) {
      assert(std::isdigit(*begin));
      std::size_t len = 0;
      for(; begin != end && std::isdigit(*begin); ++begin)
        len = len * 10 + *begin - '0';

      if(begin == end)
        throw std::invalid_argument("unexpected end of string");
      if(*begin != ':')
        throw std::invalid_argument("expected ':'");
      ++begin;

      return str_reader<View>{}(begin, end, len);
    }

    template<bool View, typename T>
    list decode_list(T &begin, T end) {
      assert(*begin == 'l');
      ++begin;

      list value;
      while(begin != end && *begin != 'e') {
        value.push_back(decode_data<View>(begin, end));
      }

      if(begin == end)
        throw std::invalid_argument("unexpected end of string");

      ++begin;
      return value;
    }

    template<bool View, typename T>
    typename std::conditional<View, dict_view, dict>::type
    decode_dict(T &begin, T end) {
      assert(*begin == 'd');
      ++begin;

      typename std::conditional<View, dict_view, dict>::type value;
      while(begin != end && *begin != 'e') {
        if(!std::isdigit(*begin))
          throw std::invalid_argument("expected string token");

        auto k = decode_str<View>(begin, end);
        auto v = decode_data<View>(begin, end);
        auto result = value.emplace(std::move(k), std::move(v));
        if(!result.second) {
          throw std::invalid_argument(
            "duplicated key in dict: " + std::string(result.first->first)
          );
        }
      }

      if(begin == end)
        throw std::invalid_argument("unexpected end of string");

      ++begin;
      return value;
    }

    template<bool View, typename T>
    BENCODE_ANY_NS::any decode_data(T &begin, T end) {
      if(begin == end)
        return BENCODE_ANY_NS::any();

      if(*begin == 'i')
        return decode_int<View>(begin, end);
      else if(*begin == 'l')
        return decode_list<View>(begin, end);
      else if(*begin == 'd')
        return decode_dict<View>(begin, end);
      else if(std::isdigit(*begin))
        return decode_str<View>(begin, end);

      throw std::invalid_argument("unexpected type");
    }

  }

  template<typename T>
  inline BENCODE_ANY_NS::any decode(T &begin, T end) {
    return detail::decode_data<false>(begin, end);
  }

  template<typename T>
  inline BENCODE_ANY_NS::any decode(const T &begin, T end) {
    T b(begin);
    return detail::decode_data<false>(b, end);
  }

  inline BENCODE_ANY_NS::any decode(const BENCODE_STRING_VIEW &s) {
    return decode(s.begin(), s.end());
  }

  inline BENCODE_ANY_NS::any
  decode(std::istream &s, eof_behavior e = check_eof) {
    std::istreambuf_iterator<char> begin(s), end;
    auto result = decode(begin, end);
    // If we hit EOF, update the parent stream.
    if(e == check_eof && begin == end)
      s.setstate(std::ios_base::eofbit);
    return result;
  }

  template<typename T>
  inline BENCODE_ANY_NS::any decode_view(T &begin, T end) {
    return detail::decode_data<true>(begin, end);
  }

  template<typename T>
  inline BENCODE_ANY_NS::any decode_view(const T &begin, T end) {
    T b(begin);
    return detail::decode_data<true>(b, end);
  }

  inline BENCODE_ANY_NS::any decode_view(const BENCODE_STRING_VIEW &s) {
    return decode_view(s.begin(), s.end());
  }

  class list_encoder {
  public:
    inline list_encoder(std::ostream &os) : os(os) {
      os.put('l');
    }

    template<typename T>
    inline list_encoder & add(T &&value);

    inline void end() {
      os.put('e');
    }
  private:
    std::ostream &os;
  };

  class dict_encoder {
  public:
    inline dict_encoder(std::ostream &os) : os(os) {
      os.put('d');
    }

    template<typename T>
    inline dict_encoder & add(const BENCODE_STRING_VIEW &key, T &&value);

    inline void end() {
      os.put('e');
    }
  private:
    std::ostream &os;
  };

  // TODO: make these use unformatted output?
  inline void encode(std::ostream &os, integer value) {
    os << "i" << value << "e";
  }

  inline void encode(std::ostream &os, const BENCODE_STRING_VIEW &value) {
    os << value.size() << ":" << value;
  }

  template<typename T>
  void encode(std::ostream &os, const std::vector<T> &value) {
    list_encoder e(os);
    for(auto &&i : value)
      e.add(i);
    e.end();
  }

  template<typename T>
  void encode(std::ostream &os, const std::map<std::string, T> &value) {
    dict_encoder e(os);
    for(auto &&i : value)
      e.add(i.first, i.second);
    e.end();
  }

#ifdef BENCODE_HAS_STRING_VIEW
  template<typename T>
  void encode(std::ostream &os, const std::map<BENCODE_STRING_VIEW, T> &value) {
    dict_encoder e(os);
    for(auto &&i : value)
      e.add(i.first, i.second);
    e.end();
  }
#endif

  // Overload for `any`, but only if the passed-in type is already `any`. Don't
  // accept an implicit conversion!
  template<typename T>
  auto encode(std::ostream &os, const T &value) ->
  typename std::enable_if<std::is_same<T, BENCODE_ANY_NS::any>::value>::type {
    using BENCODE_ANY_NS::any_cast;
    auto &type = value.type();

    if(type == typeid(integer))
      encode(os, any_cast<integer>(value));
    else if(type == typeid(string))
      encode(os, any_cast<string>(value));
    else if(type == typeid(list))
      encode(os, any_cast<list>(value));
    else if(type == typeid(dict))
      encode(os, any_cast<dict>(value));
#ifdef BENCODE_HAS_STRING_VIEW
    else if(type == typeid(string_view))
      encode(os, any_cast<string_view>(value));
    else if(type == typeid(dict_view))
      encode(os, any_cast<dict_view>(value));
#endif
    else
      throw std::invalid_argument("unexpected type");
  }

  namespace detail {
    inline void encode_list_items(list_encoder &) {}

    template<typename T, typename ...Rest>
    void encode_list_items(list_encoder &enc, T &&t, Rest &&...rest) {
      enc.add(std::forward<T>(t));
      encode_list_items(enc, std::forward<Rest>(rest)...);
    }

    inline void encode_dict_items(dict_encoder &) {}

    template<typename Key, typename Value, typename ...Rest>
    void encode_dict_items(dict_encoder &enc, Key &&key, Value &&value,
                           Rest &&...rest) {
      enc.add(std::forward<Key>(key), std::forward<Value>(value));
      encode_dict_items(enc, std::forward<Rest>(rest)...);
    }
  }

  template<typename ...T>
  void encode_list(std::ostream &os, T &&...t) {
    list_encoder enc(os);
    detail::encode_list_items(enc, std::forward<T>(t)...);
    enc.end();
  }

  template<typename ...T>
  void encode_dict(std::ostream &os, T &&...t) {
    dict_encoder enc(os);
    detail::encode_dict_items(enc, std::forward<T>(t)...);
    enc.end();
  }

  template<typename T>
  inline list_encoder & list_encoder::add(T &&value) {
    encode(os, std::forward<T>(value));
    return *this;
  }

  template<typename T>
  inline dict_encoder &
  dict_encoder::add(const BENCODE_STRING_VIEW &key, T &&value) {
    encode(os, key);
    encode(os, std::forward<T>(value));
    return *this;
  }

  template<typename T>
  std::string encode(T &&t) {
    std::stringstream ss;
    encode(ss, std::forward<T>(t));
    return ss.str();
  }

}

#endif
<|endoftext|>


<commit_before>/*-------------------------------------------------------------------------
 *
 *   FILE
 *	pqxx/isolation.hxx
 *
 *   DESCRIPTION
 *      definitions of transaction isolation levels
 *   Policies and traits describing SQL transaction isolation levels
 *   DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/isolation instead.
 *
 * Copyright (c) 2003-2004, Jeroen T. Vermeulen <jtv@xs4all.nl>
 *
 * See COPYING for copyright license.  If you did not receive a file called
 * COPYING with this source code, please notify the distributor of this mistake,
 * or contact the author.
 *
 *-------------------------------------------------------------------------
 */
#include "pqxx/util"

namespace pqxx
{

/// Transaction isolation levels; PostgreSQL doesn't implement all SQL levels
/** The only levels implemented in postgres are read_committed and serializable;
 * SQL also defines read_uncommitted and repeatable_read.  Unless you're bent on
 * using nasty tricks to communicate between ongoing transactions and such, you
 * won't really need isolation levels for anything except performance
 * optimization.  In that case, you can safely emulate read_uncommitted by using
 * read_committed and repeatable_read by using serializable.  In general,
 * serializable is the safest choice.
 */
enum PQXX_LIBEXPORT isolation_level
{
  // read_uncommitted,
  read_committed,
  // repeatable_read,
  serializable
};

/// Traits class to describe an isolation level; primarly for libpqxx's own use
template<isolation_level LEVEL> struct isolation_traits
{
  static isolation_level level() throw () { return LEVEL; }
  static const char *name() throw ();
};

template<> inline const char *isolation_traits<read_committed>::name() throw ()
	{ return "READ COMMITTED"; }
template<> inline const char *isolation_traits<serializable>::name() throw ()
	{ return "SERIALIZABLE"; }

}

<commit_msg>Added missing #include "pqxx/libcompiler.h"<commit_after>/*-------------------------------------------------------------------------
 *
 *   FILE
 *	pqxx/isolation.hxx
 *
 *   DESCRIPTION
 *      definitions of transaction isolation levels
 *   Policies and traits describing SQL transaction isolation levels
 *   DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/isolation instead.
 *
 * Copyright (c) 2003-2005, Jeroen T. Vermeulen <jtv@xs4all.nl>
 *
 * See COPYING for copyright license.  If you did not receive a file called
 * COPYING with this source code, please notify the distributor of this mistake,
 * or contact the author.
 *
 *-------------------------------------------------------------------------
 */
#include "pqxx/libcompiler.h"

#include "pqxx/util"

namespace pqxx
{

/// Transaction isolation levels; PostgreSQL doesn't implement all SQL levels
/** The only levels implemented in postgres are read_committed and serializable;
 * SQL also defines read_uncommitted and repeatable_read.  Unless you're bent on
 * using nasty tricks to communicate between ongoing transactions and such, you
 * won't really need isolation levels for anything except performance
 * optimization.  In that case, you can safely emulate read_uncommitted by using
 * read_committed and repeatable_read by using serializable.  In general,
 * serializable is the safest choice.
 */
enum PQXX_LIBEXPORT isolation_level
{
  // read_uncommitted,
  read_committed,
  // repeatable_read,
  serializable
};

/// Traits class to describe an isolation level; primarly for libpqxx's own use
template<isolation_level LEVEL> struct isolation_traits
{
  static isolation_level level() throw () { return LEVEL; }
  static const char *name() throw ();
};

template<> inline const char *isolation_traits<read_committed>::name() throw ()
	{ return "READ COMMITTED"; }
template<> inline const char *isolation_traits<serializable>::name() throw ()
	{ return "SERIALIZABLE"; }

}

<|endoftext|>


<commit_before>#ifndef CONTROL_HPP
#define CONTROL_HPP

#include <atomic>
#include <iostream>
#include <thread>
#include <GL/gl3w.h>

#include "util.hpp"
#include "model.hpp"
#include "view.hpp"

namespace Control {
	typedef enum : unsigned char {
		e_tid_view=0, ///< The view thread ID, necessarily 0 (master)
		e_tid_model,  ///< The model thread ID
		e_tid_total   ///< The total number of enumerated threads
	} e_tid; ///< The ID of a thread, where 0 is the master

	/// A data structure controlling both model and view
	struct control : virtual Util::task {
		const char *mpath;
		View::view viewer;
		Model::contour<Util::undef_t, GLfloat, GLclampf, GLclampf> model;

		void init(std::atomic_bool &alive) override;
		void poll(std::atomic_bool &alive);
		void run(std::atomic_bool &alive) override;

		/** Constructor; initializes and applies shaders
 		 * @param alive Shared state; false signals shutdown
 		 * @param ply The path to a ply model
 		 * @param vert Path to a GLSL vertex shader
 		 * @param frag Path to a GLSL fragment shader
 		 */
		control(std::atomic_bool &alive, const char *ply,
 			const char *vert, const char *frag);
	};
}

#endif
<commit_msg>Changed the enumerated thread ID's to task ID's (superficial)<commit_after>#ifndef CONTROL_HPP
#define CONTROL_HPP

#include <atomic>
#include <iostream>
#include <thread>
#include <GL/gl3w.h>

#include "util.hpp"
#include "model.hpp"
#include "view.hpp"

namespace Control {
	typedef enum : unsigned char {
		e_tid_view=0, ///< The view task ID, necessarily 0 (master)
		e_tid_model,  ///< The model task ID
		e_tid_total   ///< The total number of enumerated tasks
	} e_tid; ///< The ID of a task

	/// A data structure controlling both model and view
	struct control : virtual Util::task {
		const char *mpath;
		View::view viewer;
		Model::contour<Util::undef_t, GLfloat, GLclampf, GLclampf> model;

		void init(std::atomic_bool &alive) override;
		void poll(std::atomic_bool &alive);
		void run(std::atomic_bool &alive) override;

		/** Constructor; initializes and applies shaders
 		 * @param alive Shared state; false signals shutdown
 		 * @param ply The path to a ply model
 		 * @param vert Path to a GLSL vertex shader
 		 * @param frag Path to a GLSL fragment shader
 		 */
		control(std::atomic_bool &alive, const char *ply,
 			const char *vert, const char *frag);
	};
}

#endif
<|endoftext|>


<commit_before>#include "buffer_cache/semantic_checking.hpp"
#include "semantic_checking.hpp"

/* Buf */

template<class inner_cache_t>
block_id_t scc_buf_t<inner_cache_t>::get_block_id() {
    rassert(inner_buf);
    return inner_buf->get_block_id();
}

template<class inner_cache_t>
bool scc_buf_t<inner_cache_t>::is_dirty() {
    rassert(inner_buf);
    return inner_buf->is_dirty();
}

template<class inner_cache_t>
const void *scc_buf_t<inner_cache_t>::get_data_read() const {
    rassert(inner_buf);
    return inner_buf->get_data_read();
}

template<class inner_cache_t>
void *scc_buf_t<inner_cache_t>::get_data_major_write() {
    has_been_changed = true;
    return inner_buf->get_data_major_write();
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::set_data(void *dest, const void *src, const size_t n) {
    has_been_changed = true;
    inner_buf->set_data(dest, src, n);
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::move_data(void *dest, const void *src, const size_t n) {
    has_been_changed = true;
    inner_buf->move_data(dest, src, n);
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::apply_patch(buf_patch_t *patch) {
    has_been_changed = true;
    inner_buf->apply_patch(patch);
}

template<class inner_cache_t>
patch_counter_t scc_buf_t<inner_cache_t>::get_next_patch_counter() {
    return inner_buf->get_next_patch_counter();
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::mark_deleted(bool write_null) {
    rassert(inner_buf);
    inner_buf->mark_deleted(write_null);
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::touch_recency(repli_timestamp timestamp) {
    rassert(inner_buf);
    // TODO: Why are we not tracking this?
    inner_buf->touch_recency(timestamp);
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::release() {
    rassert(inner_buf);
    if (!snapshotted && !inner_buf->is_deleted()) {
        /* There are two valid use cases for should_load == false:
         1. deletion of buffers
         2. overwriting the value of a buffer
         If we got here, it has to be case 2, so we hope that the buffer has been filled
         with data by now and compute a new crc.
        */
        rassert(should_load || has_been_changed);
        if (!has_been_changed && cache->crc_map.get(inner_buf->get_block_id())) {
            rassert(compute_crc() == cache->crc_map.get(inner_buf->get_block_id()));
        } else {
            cache->crc_map.set(inner_buf->get_block_id(), compute_crc());
        }
    }

    // TODO: We want to track order tokens here.
    //    if (!snapshotted) {
    //        cache->sink_map[inner_buf->get_block_id()].check_out(order token);
    //    }

    inner_buf->release();
    delete this;
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::on_block_available(typename inner_cache_t::buf_t *buf) {
    rassert(!inner_buf);
    rassert(buf);

    inner_buf = buf;
    if (!snapshotted) {
        if (cache->crc_map.get(inner_buf->get_block_id())) {
            rassert(compute_crc() == cache->crc_map.get(inner_buf->get_block_id()));
        } else {
            cache->crc_map.set(inner_buf->get_block_id(), compute_crc());
        }
    }
    if (available_cb) available_cb->on_block_available(this);
}

template<class inner_cache_t>
scc_buf_t<inner_cache_t>::scc_buf_t(scc_cache_t<inner_cache_t> *_cache, bool snapshotted, bool should_load)
    : snapshotted(snapshotted), should_load(should_load), has_been_changed(false), inner_buf(NULL), available_cb(NULL), cache(_cache) { }

/* Transaction */

template<class inner_cache_t>
bool scc_transaction_t<inner_cache_t>::commit(transaction_commit_callback_t *callback) {
    if (inner_transaction->commit(this)) {
        delete this;
        return true;
    } else {
        commit_cb = callback;
        return false;
    }
}

template<class inner_cache_t>
scc_buf_t<inner_cache_t> *scc_transaction_t<inner_cache_t>::acquire(block_id_t block_id, access_t mode,
                   block_available_callback_t *callback, bool should_load) {
    scc_buf_t<inner_cache_t> *buf = new scc_buf_t<inner_cache_t>(this->cache, snapshotted || mode == rwi_read_outdated_ok, should_load);
    buf->cache = this->cache;
    if (!snapshotted) {
        cache->sink_map[block_id].check_out(order_token);
    }

    if (typename inner_cache_t::buf_t *inner_buf = inner_transaction->acquire(block_id, mode, buf, should_load)) {
        buf->inner_buf = inner_buf;
        rassert(block_id == buf->get_block_id());
        if (!snapshotted && should_load) {
            if (cache->crc_map.get(block_id)) {
                rassert(buf->compute_crc() == cache->crc_map.get(block_id));
            } else {
                cache->crc_map.set(block_id, buf->compute_crc());
            }
        }
        return buf;
    } else {
        buf->available_cb = callback;
        return NULL;
    }
}

template<class inner_cache_t>
scc_buf_t<inner_cache_t> *scc_transaction_t<inner_cache_t>::allocate() {
    scc_buf_t<inner_cache_t> *buf = new scc_buf_t<inner_cache_t>(this->cache, snapshotted, true);
    buf->inner_buf = inner_transaction->allocate();
    cache->crc_map.set(buf->inner_buf->get_block_id(), buf->compute_crc());
    return buf;
}

template<class inner_cache_t>
void scc_transaction_t<inner_cache_t>::get_subtree_recencies(block_id_t *block_ids, size_t num_block_ids, repli_timestamp *recencies_out, get_subtree_recencies_callback_t *cb) {
    return inner_transaction->get_subtree_recencies(block_ids, num_block_ids, recencies_out, cb);
}

template<class inner_cache_t>
scc_transaction_t<inner_cache_t>::scc_transaction_t(UNUSED order_token_t _order_token, access_t _access, scc_cache_t<inner_cache_t> *_cache)
    : cache(_cache),
#ifndef NDEBUG
      order_token(_order_token),
#endif
      snapshotted(false),
      access(_access),
      begin_cb(NULL),
      inner_transaction(NULL) { }

template<class inner_cache_t>
void scc_transaction_t<inner_cache_t>::on_txn_begin(typename inner_cache_t::transaction_t *txn) {
    rassert(!inner_transaction);
    inner_transaction = txn;
    if (begin_cb) begin_cb->on_txn_begin(this);
}

template<class inner_cache_t>
void scc_transaction_t<inner_cache_t>::on_txn_commit(UNUSED typename inner_cache_t::transaction_t *txn) {
    if (commit_cb) commit_cb->on_txn_commit(this);
    delete this;
}

/* Cache */

template<class inner_cache_t>
void scc_cache_t<inner_cache_t>::create(
        translator_serializer_t *serializer,
        mirrored_cache_static_config_t *static_config)
{
    inner_cache_t::create(serializer, static_config);
}

template<class inner_cache_t>
scc_cache_t<inner_cache_t>::scc_cache_t(
        translator_serializer_t *serializer,
        mirrored_cache_config_t *dynamic_config)
    : inner_cache(serializer, dynamic_config) {
}

template<class inner_cache_t>
block_size_t scc_cache_t<inner_cache_t>::get_block_size() {
    return inner_cache.get_block_size();
}

template<class inner_cache_t>
scc_transaction_t<inner_cache_t> *scc_cache_t<inner_cache_t>::begin_transaction(order_token_t token, access_t access, int expected_change_count, repli_timestamp recency_timestamp, transaction_begin_callback_t *callback) {
    scc_transaction_t<inner_cache_t> *txn = new scc_transaction_t<inner_cache_t>(token, access, this);
    if (typename inner_cache_t::transaction_t *inner_txn = inner_cache.begin_transaction(token, access, expected_change_count, recency_timestamp, txn)) {
        txn->inner_transaction = inner_txn;
        return txn;
    } else {
        txn->begin_cb = callback;
        return NULL;
    }
}

template<class inner_cache_t>
void scc_cache_t<inner_cache_t>::offer_read_ahead_buf(block_id_t block_id, void *buf, repli_timestamp recency_timestamp) {
    inner_cache.offer_read_ahead_buf(block_id, buf, recency_timestamp);
}

template<class inner_cache_t>
bool scc_cache_t<inner_cache_t>::contains_block(block_id_t block_id) {
    return inner_cache.contains_block(block_id);
}
<commit_msg>Removed silly NDEBUG guards around order_token checking in semantic_checking.tcc.<commit_after>#include "buffer_cache/semantic_checking.hpp"
#include "semantic_checking.hpp"

/* Buf */

template<class inner_cache_t>
block_id_t scc_buf_t<inner_cache_t>::get_block_id() {
    rassert(inner_buf);
    return inner_buf->get_block_id();
}

template<class inner_cache_t>
bool scc_buf_t<inner_cache_t>::is_dirty() {
    rassert(inner_buf);
    return inner_buf->is_dirty();
}

template<class inner_cache_t>
const void *scc_buf_t<inner_cache_t>::get_data_read() const {
    rassert(inner_buf);
    return inner_buf->get_data_read();
}

template<class inner_cache_t>
void *scc_buf_t<inner_cache_t>::get_data_major_write() {
    has_been_changed = true;
    return inner_buf->get_data_major_write();
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::set_data(void *dest, const void *src, const size_t n) {
    has_been_changed = true;
    inner_buf->set_data(dest, src, n);
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::move_data(void *dest, const void *src, const size_t n) {
    has_been_changed = true;
    inner_buf->move_data(dest, src, n);
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::apply_patch(buf_patch_t *patch) {
    has_been_changed = true;
    inner_buf->apply_patch(patch);
}

template<class inner_cache_t>
patch_counter_t scc_buf_t<inner_cache_t>::get_next_patch_counter() {
    return inner_buf->get_next_patch_counter();
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::mark_deleted(bool write_null) {
    rassert(inner_buf);
    inner_buf->mark_deleted(write_null);
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::touch_recency(repli_timestamp timestamp) {
    rassert(inner_buf);
    // TODO: Why are we not tracking this?
    inner_buf->touch_recency(timestamp);
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::release() {
    rassert(inner_buf);
    if (!snapshotted && !inner_buf->is_deleted()) {
        /* There are two valid use cases for should_load == false:
         1. deletion of buffers
         2. overwriting the value of a buffer
         If we got here, it has to be case 2, so we hope that the buffer has been filled
         with data by now and compute a new crc.
        */
        rassert(should_load || has_been_changed);
        if (!has_been_changed && cache->crc_map.get(inner_buf->get_block_id())) {
            rassert(compute_crc() == cache->crc_map.get(inner_buf->get_block_id()));
        } else {
            cache->crc_map.set(inner_buf->get_block_id(), compute_crc());
        }
    }

    // TODO: We want to track order tokens here.
    //    if (!snapshotted) {
    //        cache->sink_map[inner_buf->get_block_id()].check_out(order token);
    //    }

    inner_buf->release();
    delete this;
}

template<class inner_cache_t>
void scc_buf_t<inner_cache_t>::on_block_available(typename inner_cache_t::buf_t *buf) {
    rassert(!inner_buf);
    rassert(buf);

    inner_buf = buf;
    if (!snapshotted) {
        if (cache->crc_map.get(inner_buf->get_block_id())) {
            rassert(compute_crc() == cache->crc_map.get(inner_buf->get_block_id()));
        } else {
            cache->crc_map.set(inner_buf->get_block_id(), compute_crc());
        }
    }
    if (available_cb) available_cb->on_block_available(this);
}

template<class inner_cache_t>
scc_buf_t<inner_cache_t>::scc_buf_t(scc_cache_t<inner_cache_t> *_cache, bool snapshotted, bool should_load)
    : snapshotted(snapshotted), should_load(should_load), has_been_changed(false), inner_buf(NULL), available_cb(NULL), cache(_cache) { }

/* Transaction */

template<class inner_cache_t>
bool scc_transaction_t<inner_cache_t>::commit(transaction_commit_callback_t *callback) {
    if (inner_transaction->commit(this)) {
        delete this;
        return true;
    } else {
        commit_cb = callback;
        return false;
    }
}

template<class inner_cache_t>
scc_buf_t<inner_cache_t> *scc_transaction_t<inner_cache_t>::acquire(block_id_t block_id, access_t mode,
                   block_available_callback_t *callback, bool should_load) {
    scc_buf_t<inner_cache_t> *buf = new scc_buf_t<inner_cache_t>(this->cache, snapshotted || mode == rwi_read_outdated_ok, should_load);
    buf->cache = this->cache;
    if (!snapshotted) {
        cache->sink_map[block_id].check_out(order_token);
    }

    if (typename inner_cache_t::buf_t *inner_buf = inner_transaction->acquire(block_id, mode, buf, should_load)) {
        buf->inner_buf = inner_buf;
        rassert(block_id == buf->get_block_id());
        if (!snapshotted && should_load) {
            if (cache->crc_map.get(block_id)) {
                rassert(buf->compute_crc() == cache->crc_map.get(block_id));
            } else {
                cache->crc_map.set(block_id, buf->compute_crc());
            }
        }
        return buf;
    } else {
        buf->available_cb = callback;
        return NULL;
    }
}

template<class inner_cache_t>
scc_buf_t<inner_cache_t> *scc_transaction_t<inner_cache_t>::allocate() {
    scc_buf_t<inner_cache_t> *buf = new scc_buf_t<inner_cache_t>(this->cache, snapshotted, true);
    buf->inner_buf = inner_transaction->allocate();
    cache->crc_map.set(buf->inner_buf->get_block_id(), buf->compute_crc());
    return buf;
}

template<class inner_cache_t>
void scc_transaction_t<inner_cache_t>::get_subtree_recencies(block_id_t *block_ids, size_t num_block_ids, repli_timestamp *recencies_out, get_subtree_recencies_callback_t *cb) {
    return inner_transaction->get_subtree_recencies(block_ids, num_block_ids, recencies_out, cb);
}

template<class inner_cache_t>
scc_transaction_t<inner_cache_t>::scc_transaction_t(order_token_t _order_token, access_t _access, scc_cache_t<inner_cache_t> *_cache)
    : cache(_cache),
      order_token(_order_token),
      snapshotted(false),
      access(_access),
      begin_cb(NULL),
      inner_transaction(NULL) { }

template<class inner_cache_t>
void scc_transaction_t<inner_cache_t>::on_txn_begin(typename inner_cache_t::transaction_t *txn) {
    rassert(!inner_transaction);
    inner_transaction = txn;
    if (begin_cb) begin_cb->on_txn_begin(this);
}

template<class inner_cache_t>
void scc_transaction_t<inner_cache_t>::on_txn_commit(UNUSED typename inner_cache_t::transaction_t *txn) {
    if (commit_cb) commit_cb->on_txn_commit(this);
    delete this;
}

/* Cache */

template<class inner_cache_t>
void scc_cache_t<inner_cache_t>::create(
        translator_serializer_t *serializer,
        mirrored_cache_static_config_t *static_config)
{
    inner_cache_t::create(serializer, static_config);
}

template<class inner_cache_t>
scc_cache_t<inner_cache_t>::scc_cache_t(
        translator_serializer_t *serializer,
        mirrored_cache_config_t *dynamic_config)
    : inner_cache(serializer, dynamic_config) {
}

template<class inner_cache_t>
block_size_t scc_cache_t<inner_cache_t>::get_block_size() {
    return inner_cache.get_block_size();
}

template<class inner_cache_t>
scc_transaction_t<inner_cache_t> *scc_cache_t<inner_cache_t>::begin_transaction(order_token_t token, access_t access, int expected_change_count, repli_timestamp recency_timestamp, transaction_begin_callback_t *callback) {
    scc_transaction_t<inner_cache_t> *txn = new scc_transaction_t<inner_cache_t>(token, access, this);
    if (typename inner_cache_t::transaction_t *inner_txn = inner_cache.begin_transaction(token, access, expected_change_count, recency_timestamp, txn)) {
        txn->inner_transaction = inner_txn;
        return txn;
    } else {
        txn->begin_cb = callback;
        return NULL;
    }
}

template<class inner_cache_t>
void scc_cache_t<inner_cache_t>::offer_read_ahead_buf(block_id_t block_id, void *buf, repli_timestamp recency_timestamp) {
    inner_cache.offer_read_ahead_buf(block_id, buf, recency_timestamp);
}

template<class inner_cache_t>
bool scc_cache_t<inner_cache_t>::contains_block(block_id_t block_id) {
    return inner_cache.contains_block(block_id);
}
<|endoftext|>


<commit_before>// Author: Rene Brun

#include "TROOT.h"
#include "TKey.h"
#include "TFile.h"
#include "TSystem.h"
#include "TTree.h"

  //Example of script showing how to copy all objects (including directories)
  //from a source file.
  //For each input file, a new directory is created in the current directory
  //with the name of the source file.
  //After execution of:
  // root > .x copyFiles.C
  //the file result.root will contain 4 subdirectories:
  // "tot100.root", "hsimple.root", "hs1.root","hs2.root"

void CopyDir(TDirectory *source) {
   //copy all objects and subdirs of directory source as a subdir of the current directory
   source->ls();
   TDirectory *savdir = gDirectory;
   TDirectory *adir = savdir->mkdir(source->GetName());
   adir->cd();
   //loop on all entries of this directory
   TKey *key;
   TIter nextkey(source->GetListOfKeys());
   while ((key = (TKey*)nextkey())) {
      const char *classname = key->GetClassName();
      TClass *cl = gROOT->GetClass(classname);
      if (!cl) continue;
      if (cl->InheritsFrom("TDirectory")) {
         source->cd(key->GetName());
         TDirectory *subdir = gDirectory;
         adir->cd();
         CopyDir(subdir);
         adir->cd();
      } else if (cl->InheritsFrom("TTree")) {
         TTree *T = (TTree*)source->Get(key->GetName());
         adir->cd();
         TTree *newT = T->CloneTree();
         newT->Write();
      } else {
         source->cd();
         TObject *obj = key->ReadObj();
         adir->cd();
         obj->Write();
         delete obj;
     }
  }
  adir->SaveSelf(kTRUE);
  savdir->cd();
}
void CopyFile(const char *fname) {
   //Copy all objects and subdirs of file fname as a subdir of the current directory
   TDirectory *target = gDirectory;
   TFile *f = TFile::Open(fname);
   if (!f || f->IsZombie()) {
      printf("Cannot copy file: %s\n",fname);
      target->cd();
      return;
   }
   target->cd();
   CopyDir(f);
   delete f;
   target->cd();
}
void copyFiles() {
   //main function copying 4 files as subdirectories of a new file
   TFile *f = new TFile("result.root","recreate");
   CopyFile("tot100.root");
   CopyFile("hsimple.root");
   CopyFile("hs1.root");
   CopyFile("hs2.root");
   f->ls();
   delete f;
}
<commit_msg>Following the recent fix by Philippe, we can use now the option "fast" when calling CloneTree.<commit_after>// Author: Rene Brun

#include "TROOT.h"
#include "TKey.h"
#include "TFile.h"
#include "TSystem.h"
#include "TTree.h"

  //Example of script showing how to copy all objects (including directories)
  //from a source file.
  //For each input file, a new directory is created in the current directory
  //with the name of the source file.
  //After execution of:
  // root > .x copyFiles.C
  //the file result.root will contain 4 subdirectories:
  // "tot100.root", "hsimple.root", "hs1.root","hs2.root"

void CopyDir(TDirectory *source) {
   //copy all objects and subdirs of directory source as a subdir of the current directory
   source->ls();
   TDirectory *savdir = gDirectory;
   TDirectory *adir = savdir->mkdir(source->GetName());
   adir->cd();
   //loop on all entries of this directory
   TKey *key;
   TIter nextkey(source->GetListOfKeys());
   while ((key = (TKey*)nextkey())) {
      const char *classname = key->GetClassName();
      TClass *cl = gROOT->GetClass(classname);
      if (!cl) continue;
      if (cl->InheritsFrom("TDirectory")) {
         source->cd(key->GetName());
         TDirectory *subdir = gDirectory;
         adir->cd();
         CopyDir(subdir);
         adir->cd();
      } else if (cl->InheritsFrom("TTree")) {
         TTree *T = (TTree*)source->Get(key->GetName());
         adir->cd();
         TTree *newT = T->CloneTree(-1,"fast");
         newT->Write();
      } else {
         source->cd();
         TObject *obj = key->ReadObj();
         adir->cd();
         obj->Write();
         delete obj;
     }
  }
  adir->SaveSelf(kTRUE);
  savdir->cd();
}
void CopyFile(const char *fname) {
   //Copy all objects and subdirs of file fname as a subdir of the current directory
   TDirectory *target = gDirectory;
   TFile *f = TFile::Open(fname);
   if (!f || f->IsZombie()) {
      printf("Cannot copy file: %s\n",fname);
      target->cd();
      return;
   }
   target->cd();
   CopyDir(f);
   delete f;
   target->cd();
}
void copyFiles() {
   //main function copying 4 files as subdirectories of a new file
   TFile *f = new TFile("result.root","recreate");
   CopyFile("tot100.root");
   CopyFile("hsimple.root");
   CopyFile("hs1.root");
   CopyFile("hs2.root");
   f->ls();
   delete f;
}
<|endoftext|>


<commit_before>/*
 * Copyright (C) 2003-2007 Funambol, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307  USA
 */

#include "base/util/BasicTime.h"

BasicTime::BasicTime() {
    year = 1970;
    month = 1;
    day = 1;
    weekday = -1;
    hour = 0;
    min = 0;
    sec = 0;
    tzHour = 0;
    tzMin = 0;
}

int BasicTime::set(int yy, int mon, int dd, int wd,
                   int hh, int mm, int ss, int tzh, int tzm)
{
    //  TODO
    return 0;
}

/**
 * Parse the date in RF 822 format
 *
 * Some examples:
 * Date: Fri, 01 Aug 2003 14:04:55 +0800
 * Date: Wed, 30 Jul 2003 13:24:21 -0700
 * Date: 20 Jun 2003 15:42:12 -0500
 *
 * RFC822 date time
 *
 *   date-time   =  [ day "," ] date time        ; dd mm yy
 *                                              ;  hh:mm:ss zzz
 *
 *   day         =  "Mon"  / "Tue" /  "Wed"  / "Thu"
 *               /  "Fri"  / "Sat" /  "Sun"
 *
 *   date        =  1*2DIGIT month 2DIGIT        ; day month year
 *                                               ;  e.g. 20 Jun 82
 *
 *   month       =  "Jan"  /  "Feb" /  "Mar"  /  "Apr"
 *               /  "May"  /  "Jun" /  "Jul"  /  "Aug"
 *               /  "Sep"  /  "Oct" /  "Nov"  /  "Dec"
 *
 *   time        =  hour zone                    ; ANSI and Military
 *
 *   hour        =  2DIGIT ":" 2DIGIT [":" 2DIGIT]
 *                                               ; 00:00:00 - 23:59:59
 *
 *   zone        =  "UT"  / "GMT"                ; Universal Time
 *                                               ; North American : UT
 *               /  "EST" / "EDT"                ;  Eastern:  - 5/ - 4
 *               /  "CST" / "CDT"                ;  Central:  - 6/ - 5
 *               /  "MST" / "MDT"                ;  Mountain: - 7/ - 6
 *               /  "PST" / "PDT"                ;  Pacific:  - 8/ - 7
 *               /  1ALPHA                       ; Military: Z = UT;
 *                                               ;  A:-1; (J not used)
 *                                               ;  M:-12; N:+1; Y:+12
 *               / ( ("+" / "-") 4DIGIT )        ; Local differential
 *                                               ;  hours+min. (HHMM)
**/
int BasicTime::parseRfc822(const char *date)
{

    int ret = 0;
    if (!isADate(date)) {
        return -1;
    }

    const char *days[] = {
        "Sun", "Mon", "Tue", "Wed",
        "Thu", "Fri", "Sat"
    };
    const char *months[] = {
        "Jan", "Feb", "Mar", "Apr",
        "May", "Jun", "Jul", "Aug",
        "Sep", "Oct", "Nov", "Dec"
    };
    char dayOfWeek[6] = "---,";
    char mon[4] = "---";
    char time[10] = "00:00:00";
    char timeZone[20] = "GMT";

    // Wed Feb 01 14:40:45 Europe/Amsterdam 2006
    // do we have day of week?
    const char *pdate = strstr( date, "," );
    if ( pdate == 0 ) {
        ret=sscanf(date, "%d %s %d %s %s",
            &day, mon, &year, time, timeZone);
    }
    else {
        ret=sscanf(date, "%s, %d %s %d %s %s",
            dayOfWeek, &day, mon, &year, time, timeZone);
        if (ret >= 1 && ret < 6) {
            // it can be an error in the format: Mon,12 Feb 2007 09:00:01 +0100
            // the comma is attached to the day
            if (*(pdate + 1) != ' ') {
                ret = sscanf(pdate + 1, "%s,%d %s %d %s %s",
                    &day, mon, &year, time, timeZone);

            }
        }
        // Convert day of week
        for (int i = 0; i < 7; i++) {
            if ( strcmp(days[i], dayOfWeek) == 0 ) {
                weekday = i;
                break;
            }
        }
        // Do not exit on error on weekday, treat it like if it was
        // not present.
    }
    // Trap parsing error
    if(ret == EOF || ret == 0){
        return -1;
    }
    // Check validity
    if (year > 3000 || day < 0 || day > 31){
        *this = BasicTime();
        return -1;
    }

    // Get month
    int i;
    for (i = 0; i < 12; i++) {
        if ( strcmp(months[i], mon) == 0 ) {
            month = i+1;
            break;
        }
    }
    // Trap parsing error
    if (i==13) {
        return -1;
    }

    // Year ---------------------------------
    if (year <= 60) {
        year += 2000;
    } else if (year > 60 && year < 100) {
        year += 1900;
    }

    // hh:mm:ss -------------------------
    // do we have sec?
    if (strlen(time) > 6 && time[5] == ':')
        sscanf(time, "%d:%d:%d", &hour, &min, &sec);
    else
        sscanf(time, "%d:%d", &hour, &min);

    // Timezone ---------------------------------
    if ( strcmp(timeZone, "GMT") != 0 && strcmp(timeZone, "UT") != 0) {
        // is this explicit time?
        if ( timeZone[0] == '+' || timeZone[0]== '-' ) {
            char wcH[4] = "+00";
            char wcM[4] = "00";

            // get hour
            if ( strlen(timeZone) > 3) {
                wcH[0] = timeZone[0];
                wcH[1] = timeZone[1];
                wcH[2] = timeZone[2];
                wcH[3] = '\0';
            }
            // get min
            if ( strlen(timeZone) >= 5)	{
                wcM[0] = timeZone[3];
                wcM[1] = timeZone[4];
                wcM[2] = '\0';
            }
            tzHour = atoi(wcH);
            tzMin = atoi(wcM);
        }
        // otherwise it could be one string with the time
        else if ( strcmp(timeZone, "EDT") == 0) {
            tzHour = -4;
        }
        else if ( strcmp(timeZone, "EST") == 0
            ||  strcmp(timeZone, "CDT") == 0) {
                tzHour = -5;
        }
        else if ( strcmp(timeZone, "CST") == 0
            ||  strcmp(timeZone, "MDT") == 0) {
                tzHour = -6;
        }
        else if ( strcmp(timeZone, "MST") == 0
            ||  strcmp(timeZone, "PDT") == 0 ){
                tzHour = -7;
        }
        else if ( strcmp(timeZone, "PST") == 0) {
            tzHour = -8;
        }
    }

    // clean up
    return 0;
}

/*
* The function return if the argument passed is a date in a format
* we are searching. To decide it the date must contain the month, a space
* and the millennium
*
* Mar 2007, Jun 2007, Mar 07, Mar 98. We search Mar 2, Mar 0...
* If no one of them is found try with the millenium 1XXX
*
*/
bool BasicTime::isADate(const char* date) {
    const char *months[] = {
        "Jan 2", "Feb 2", "Mar 2", "Apr 2",
        "May 2", "Jun 2", "Jul 2", "Aug 2",
        "Sep 2", "Oct 2", "Nov 2", "Dec 2",
        "Jan 1", "Feb 1", "Mar 1", "Apr 1",
        "May 1", "Jun 1", "Jul 1", "Aug 1",
        "Sep 1", "Oct 1", "Nov 1", "Dec 1",
        "Jan 0", "Feb 0", "Mar 0", "Apr 0",
        "May 0", "Jun 0", "Jul 0", "Aug 0",
        "Sep 0", "Oct 0", "Nov 0", "Dec 0",
        "Jan 9", "Feb 9", "Mar 9", "Apr 9",
        "May 9", "Jun 9", "Jul 9", "Aug 9",
        "Sep 9", "Oct 9", "Nov 9", "Dec 9"
    };
    for (int i = 0; i < 48; i++) {
        if (strstr(date, months[i]) != NULL) {
            return true;
        }
    }
    return false;

}

// Date: Fri, 01 Aug 2003 14:04:55 +0800
char *BasicTime::formatRfc822() const {
	const char *days[] = {
        "Sun", "Mon", "Tue", "Wed",
        "Thu", "Fri", "Sat", "Sun"
    };
	const char *months[] = {
        "Jan", "Feb", "Mar", "Apr",
        "May", "Jun", "Jul", "Aug",
        "Sep", "Oct", "Nov", "Dec"
    };
    char *ret = new char[60]; // FIXME: avoid sprintf and static size

    // If the weekday is valid, print it, otherwise ignore it
    if(weekday > 0 && weekday < 7) {
        sprintf(ret, "%s, %d %s %d %02d:%02d:%02d %+03d%02d",
                      days[weekday], day, months[month-1], year,
                      hour, min, sec,
                      tzHour, tzMin);
    }
    else {
        sprintf(ret, "%d %s %d %02d:%02d:%02d %+03d%02d",
                      day, months[month-1], year,
                      hour, min, sec,
                      tzHour, tzMin);
    }

    return ret;
}

ArrayElement *BasicTime::clone() {
    return new BasicTime(*this);
};

BasicTime& BasicTime::operator=(const BasicTime& o) {
    year = o.year;
    month = o.month;
    day = o.day;
    weekday = o.weekday;
    hour = o.hour;
    min = o.min;
    sec = o.sec;
    tzHour = o.tzHour;
    tzMin = o.tzMin;

    return *this;
}

bool BasicTime::operator==(const BasicTime& o) const {
    return (
        year == o.year &&
        month == o.month &&
        day == o.day &&
        weekday == o.weekday &&
        hour == o.hour &&
        min == o.min &&
        sec == o.sec &&
        tzHour == o.tzHour &&
        tzMin == o.tzMin
    );
}

<commit_msg>Fixed parser error<commit_after>/*
 * Copyright (C) 2003-2007 Funambol, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307  USA
 */

#include "base/util/BasicTime.h"

BasicTime::BasicTime() {
    year = 1970;
    month = 1;
    day = 1;
    weekday = -1;
    hour = 0;
    min = 0;
    sec = 0;
    tzHour = 0;
    tzMin = 0;
}

int BasicTime::set(int yy, int mon, int dd, int wd,
                   int hh, int mm, int ss, int tzh, int tzm)
{
    //  TODO
    return 0;
}

/**
 * Parse the date in RF 822 format
 *
 * Some examples:
 * Date: Fri, 01 Aug 2003 14:04:55 +0800
 * Date: Wed, 30 Jul 2003 13:24:21 -0700
 * Date: 20 Jun 2003 15:42:12 -0500
 *
 * RFC822 date time
 *
 *   date-time   =  [ day "," ] date time        ; dd mm yy
 *                                              ;  hh:mm:ss zzz
 *
 *   day         =  "Mon"  / "Tue" /  "Wed"  / "Thu"
 *               /  "Fri"  / "Sat" /  "Sun"
 *
 *   date        =  1*2DIGIT month 2DIGIT        ; day month year
 *                                               ;  e.g. 20 Jun 82
 *
 *   month       =  "Jan"  /  "Feb" /  "Mar"  /  "Apr"
 *               /  "May"  /  "Jun" /  "Jul"  /  "Aug"
 *               /  "Sep"  /  "Oct" /  "Nov"  /  "Dec"
 *
 *   time        =  hour zone                    ; ANSI and Military
 *
 *   hour        =  2DIGIT ":" 2DIGIT [":" 2DIGIT]
 *                                               ; 00:00:00 - 23:59:59
 *
 *   zone        =  "UT"  / "GMT"                ; Universal Time
 *                                               ; North American : UT
 *               /  "EST" / "EDT"                ;  Eastern:  - 5/ - 4
 *               /  "CST" / "CDT"                ;  Central:  - 6/ - 5
 *               /  "MST" / "MDT"                ;  Mountain: - 7/ - 6
 *               /  "PST" / "PDT"                ;  Pacific:  - 8/ - 7
 *               /  1ALPHA                       ; Military: Z = UT;
 *                                               ;  A:-1; (J not used)
 *                                               ;  M:-12; N:+1; Y:+12
 *               / ( ("+" / "-") 4DIGIT )        ; Local differential
 *                                               ;  hours+min. (HHMM)
**/
int BasicTime::parseRfc822(const char *date)
{

    int ret = 0;
    if (!isADate(date)) {
        return -1;
    }

    const char *days[] = {
        "Sun", "Mon", "Tue", "Wed",
        "Thu", "Fri", "Sat"
    };
    const char *months[] = {
        "Jan", "Feb", "Mar", "Apr",
        "May", "Jun", "Jul", "Aug",
        "Sep", "Oct", "Nov", "Dec"
    };
    char dayOfWeek[6] = "---,";
    char mon[4] = "---";
    char time[10] = "00:00:00";
    char timeZone[20] = "GMT";

    // Wed Feb 01 14:40:45 Europe/Amsterdam 2006
    // do we have day of week?
    const char *pdate = strstr( date, "," );
    if ( pdate == 0 ) {
        ret=sscanf(date, "%d %s %d %s %s",
            &day, mon, &year, time, timeZone);
    }
    else {
        ret=sscanf(date, "%s %d %s %d %s %s",
            dayOfWeek, &day, mon, &year, time, timeZone);
        if (ret >= 1 && ret < 6) {
            // it can be an error in the format: Mon,12 Feb 2007 09:00:01 +0100
            // the comma is attached to the day
            if (*(pdate + 1) != ' ') {
                ret = sscanf(pdate + 1, "%s,%d %s %d %s %s",
                    &day, mon, &year, time, timeZone);

            }
        }
        // Convert day of week
        for (int i = 0; i < 7; i++) {
            if ( strncmp(days[i], dayOfWeek, 3) == 0 ) {
                weekday = i;
                break;
            }
        }
        // Do not exit on error on weekday, treat it like if it was
        // not present.
    }
    // Trap parsing error
    if(ret == EOF || ret == 0){
        return -1;
    }
    // Check validity
    if (year > 3000 || day < 0 || day > 31){
        *this = BasicTime();
        return -1;
    }

    // Get month
    int i;
    for (i = 0; i < 12; i++) {
        if ( strcmp(months[i], mon) == 0 ) {
            month = i+1;
            break;
        }
    }
    // Trap parsing error
    if (i==13) {
        return -1;
    }

    // Year ---------------------------------
    if (year <= 60) {
        year += 2000;
    } else if (year > 60 && year < 100) {
        year += 1900;
    }

    // hh:mm:ss -------------------------
    // do we have sec?
    if (strlen(time) > 6 && time[5] == ':')
        sscanf(time, "%d:%d:%d", &hour, &min, &sec);
    else
        sscanf(time, "%d:%d", &hour, &min);

    // Timezone ---------------------------------
    if ( strcmp(timeZone, "GMT") != 0 && strcmp(timeZone, "UT") != 0) {
        // is this explicit time?
        if ( timeZone[0] == '+' || timeZone[0]== '-' ) {
            char wcH[4] = "+00";
            char wcM[4] = "00";

            // get hour
            if ( strlen(timeZone) > 3) {
                wcH[0] = timeZone[0];
                wcH[1] = timeZone[1];
                wcH[2] = timeZone[2];
                wcH[3] = '\0';
            }
            // get min
            if ( strlen(timeZone) >= 5)	{
                wcM[0] = timeZone[3];
                wcM[1] = timeZone[4];
                wcM[2] = '\0';
            }
            tzHour = atoi(wcH);
            tzMin = atoi(wcM);
        }
        // otherwise it could be one string with the time
        else if ( strcmp(timeZone, "EDT") == 0) {
            tzHour = -4;
        }
        else if ( strcmp(timeZone, "EST") == 0
            ||  strcmp(timeZone, "CDT") == 0) {
                tzHour = -5;
        }
        else if ( strcmp(timeZone, "CST") == 0
            ||  strcmp(timeZone, "MDT") == 0) {
                tzHour = -6;
        }
        else if ( strcmp(timeZone, "MST") == 0
            ||  strcmp(timeZone, "PDT") == 0 ){
                tzHour = -7;
        }
        else if ( strcmp(timeZone, "PST") == 0) {
            tzHour = -8;
        }
    }

    // clean up
    return 0;
}

/*
* The function return if the argument passed is a date in a format
* we are searching. To decide it the date must contain the month, a space
* and the millennium
*
* Mar 2007, Jun 2007, Mar 07, Mar 98. We search Mar 2, Mar 0...
* If no one of them is found try with the millenium 1XXX
*
*/
bool BasicTime::isADate(const char* date) {
    const char *months[] = {
        "Jan 2", "Feb 2", "Mar 2", "Apr 2",
        "May 2", "Jun 2", "Jul 2", "Aug 2",
        "Sep 2", "Oct 2", "Nov 2", "Dec 2",
        "Jan 1", "Feb 1", "Mar 1", "Apr 1",
        "May 1", "Jun 1", "Jul 1", "Aug 1",
        "Sep 1", "Oct 1", "Nov 1", "Dec 1",
        "Jan 0", "Feb 0", "Mar 0", "Apr 0",
        "May 0", "Jun 0", "Jul 0", "Aug 0",
        "Sep 0", "Oct 0", "Nov 0", "Dec 0",
        "Jan 9", "Feb 9", "Mar 9", "Apr 9",
        "May 9", "Jun 9", "Jul 9", "Aug 9",
        "Sep 9", "Oct 9", "Nov 9", "Dec 9"
    };
    for (int i = 0; i < 48; i++) {
        if (strstr(date, months[i]) != NULL) {
            return true;
        }
    }
    return false;

}

// Date: Fri, 01 Aug 2003 14:04:55 +0800
char *BasicTime::formatRfc822() const {
	const char *days[] = {
        "Sun", "Mon", "Tue", "Wed",
        "Thu", "Fri", "Sat", "Sun"
    };
	const char *months[] = {
        "Jan", "Feb", "Mar", "Apr",
        "May", "Jun", "Jul", "Aug",
        "Sep", "Oct", "Nov", "Dec"
    };
    char *ret = new char[60]; // FIXME: avoid sprintf and static size

    // If the weekday is valid, print it, otherwise ignore it
    if(weekday > 0 && weekday < 7) {
        sprintf(ret, "%s, %d %s %d %02d:%02d:%02d %+03d%02d",
                      days[weekday], day, months[month-1], year,
                      hour, min, sec,
                      tzHour, tzMin);
    }
    else {
        sprintf(ret, "%d %s %d %02d:%02d:%02d %+03d%02d",
                      day, months[month-1], year,
                      hour, min, sec,
                      tzHour, tzMin);
    }

    return ret;
}

ArrayElement *BasicTime::clone() {
    return new BasicTime(*this);
};

BasicTime& BasicTime::operator=(const BasicTime& o) {
    year = o.year;
    month = o.month;
    day = o.day;
    weekday = o.weekday;
    hour = o.hour;
    min = o.min;
    sec = o.sec;
    tzHour = o.tzHour;
    tzMin = o.tzMin;

    return *this;
}

bool BasicTime::operator==(const BasicTime& o) const {
    return (
        year == o.year &&
        month == o.month &&
        day == o.day &&
        weekday == o.weekday &&
        hour == o.hour &&
        min == o.min &&
        sec == o.sec &&
        tzHour == o.tzHour &&
        tzMin == o.tzMin
    );
}

<|endoftext|>


<commit_before>/* This file is part of the KDE project
 * Copyright (C) 2007 Aaron Seigo <aseigo@kde.org>
 * Copyright (C) 2002 Shane Wright <me@shanewright.co.uk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "rfc822endanalyzer.h"

#include <strigi/fieldtypes.h>
#include <strigi/analysisresult.h>
#include <strigi/streamendanalyzer.h>

Rfc822EndAnalyzer::Rfc822EndAnalyzer( const Rfc822EndAnalyzerFactory* f )
      : m_factory(  f )
{
}

bool Rfc822EndAnalyzer::checkHeader(  const char* header, int32_t headersize ) const
{
    //TODO: how can we know if we have a RFC822 message here?
    //      we just return false for now anyways since we need readLine to be useful =)
    return false;
}

char Rfc822EndAnalyzer::analyze( Strigi::AnalysisResult& idx, Strigi::InputStream* in )
{
    char id_from[] = "From: ";
    char id_to[] = "To: ";
    char id_subject[] = "Subject: ";
    char id_date[] = "Date: ";
    char id_contenttype[] = "Content-Type: ";

    // we need a buffer for lines
    char linebuf[4096];
    linebuf[0] = 0;

    bool foundFrom = false;
    bool foundTo = false;
    bool foundDate = false;
    bool foundSubject = false;
    bool foundContentType = false;

    while ( in->status() == Strigi::Ok ) {
        //TODO: reenable when readline is implemented in StreamBase
        //in->readLine(linebuf, sizeof( linebuf ) );

        if (!foundFrom && memcmp(linebuf, id_from, 6) == 0) {
            idx.addValue( m_factory->field( From ), linebuf + 6 );
            foundFrom = true;
        } else if (!foundTo && memcmp(linebuf, id_to, 4) == 0) {
            idx.addValue( m_factory->field( To ), linebuf + 4 );
            foundTo = true;
        } else if (!foundSubject && memcmp(linebuf, id_subject, 9) == 0) {
            idx.addValue( m_factory->field( Subject ), linebuf + 9 );
            foundSubject = true;
        } else if (!foundDate && memcmp(linebuf, id_date, 6) == 0) {
            idx.addValue( m_factory->field( Date ), linebuf + 6 );
            foundDate = true;
        } else if (!foundContentType &&
                   memcmp(linebuf, id_contenttype, 14) == 0) {
            idx.addValue( m_factory->field( ContentType ), linebuf + 14 );
            foundContentType = true;
        }

        if ( foundFrom && foundTo && foundDate &&
             foundSubject && foundContentType ) {
            return Strigi::Ok;
        }
    };

    return Strigi::Error;
}

const Strigi::RegisteredField* Rfc822EndAnalyzerFactory::field( Rfc822EndAnalyzer::Field f ) const
{
  switch ( f ) {
    case Rfc822EndAnalyzer::From:
      return fromField;
      break;
    case Rfc822EndAnalyzer::To:
      return toField;
      break;
    case Rfc822EndAnalyzer::Subject:
      return subjectField;
      break;
    case Rfc822EndAnalyzer::Date:
      return dateField;
      break;
    case Rfc822EndAnalyzer::ContentType:
    default:
      return contentTypeField;
      break;
  }
}

void Rfc822EndAnalyzerFactory::registerFields( Strigi::FieldRegister& reg )
{
  fromField = reg.registerField("email.from", Strigi::FieldRegister::stringType, 1, 0 );
  toField = reg.registerField("email.to", Strigi::FieldRegister::stringType, 1, 0 );
  subjectField = reg.registerField("email.subject", Strigi::FieldRegister::stringType, 1, 0 );
  dateField = reg.registerField("date", Strigi::FieldRegister::stringType, 1, 0 );
  contentTypeField = reg.registerField("content-type", Strigi::FieldRegister::stringType, 1, 0 );
}

<commit_msg>SVN_SILENT Analyzer field name fix.<commit_after>/* This file is part of the KDE project
 * Copyright (C) 2007 Aaron Seigo <aseigo@kde.org>
 * Copyright (C) 2002 Shane Wright <me@shanewright.co.uk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "rfc822endanalyzer.h"

#include <strigi/fieldtypes.h>
#include <strigi/analysisresult.h>
#include <strigi/streamendanalyzer.h>

Rfc822EndAnalyzer::Rfc822EndAnalyzer( const Rfc822EndAnalyzerFactory* f )
      : m_factory(  f )
{
}

bool Rfc822EndAnalyzer::checkHeader(  const char* header, int32_t headersize ) const
{
    //TODO: how can we know if we have a RFC822 message here?
    //      we just return false for now anyways since we need readLine to be useful =)
    return false;
}

char Rfc822EndAnalyzer::analyze( Strigi::AnalysisResult& idx, Strigi::InputStream* in )
{
    char id_from[] = "From: ";
    char id_to[] = "To: ";
    char id_subject[] = "Subject: ";
    char id_date[] = "Date: ";
    char id_contenttype[] = "Content-Type: ";

    // we need a buffer for lines
    char linebuf[4096];
    linebuf[0] = 0;

    bool foundFrom = false;
    bool foundTo = false;
    bool foundDate = false;
    bool foundSubject = false;
    bool foundContentType = false;

    while ( in->status() == Strigi::Ok ) {
        //TODO: reenable when readline is implemented in StreamBase
        //in->readLine(linebuf, sizeof( linebuf ) );

        if (!foundFrom && memcmp(linebuf, id_from, 6) == 0) {
            idx.addValue( m_factory->field( From ), linebuf + 6 );
            foundFrom = true;
        } else if (!foundTo && memcmp(linebuf, id_to, 4) == 0) {
            idx.addValue( m_factory->field( To ), linebuf + 4 );
            foundTo = true;
        } else if (!foundSubject && memcmp(linebuf, id_subject, 9) == 0) {
            idx.addValue( m_factory->field( Subject ), linebuf + 9 );
            foundSubject = true;
        } else if (!foundDate && memcmp(linebuf, id_date, 6) == 0) {
            idx.addValue( m_factory->field( Date ), linebuf + 6 );
            foundDate = true;
        } else if (!foundContentType &&
                   memcmp(linebuf, id_contenttype, 14) == 0) {
            idx.addValue( m_factory->field( ContentType ), linebuf + 14 );
            foundContentType = true;
        }

        if ( foundFrom && foundTo && foundDate &&
             foundSubject && foundContentType ) {
            return Strigi::Ok;
        }
    };

    return Strigi::Error;
}

const Strigi::RegisteredField* Rfc822EndAnalyzerFactory::field( Rfc822EndAnalyzer::Field f ) const
{
  switch ( f ) {
    case Rfc822EndAnalyzer::From:
      return fromField;
      break;
    case Rfc822EndAnalyzer::To:
      return toField;
      break;
    case Rfc822EndAnalyzer::Subject:
      return subjectField;
      break;
    case Rfc822EndAnalyzer::Date:
      return dateField;
      break;
    case Rfc822EndAnalyzer::ContentType:
    default:
      return contentTypeField;
      break;
  }
}

void Rfc822EndAnalyzerFactory::registerFields( Strigi::FieldRegister& reg )
{
  fromField = reg.registerField("email.from", Strigi::FieldRegister::stringType, 1, 0 );
  toField = reg.registerField("email.to", Strigi::FieldRegister::stringType, 1, 0 );
  subjectField = reg.registerField("message.subject", Strigi::FieldRegister::stringType, 1, 0 );
  dateField = reg.registerField("content.creation_time", Strigi::FieldRegister::stringType, 1, 0 );
  contentTypeField = reg.registerField("email.content_type", Strigi::FieldRegister::stringType, 1, 0 );
}

<|endoftext|>


<commit_before>/*
  Copyright 2008 Google Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#include "runtime.h"
#include <ggadget/sysdeps.h>
#include <ggadget/logger.h>
#include <ggadget/dbus/dbus_result_receiver.h>
#include "hal_strings.h"

namespace ggadget {
namespace framework {
namespace linux_system {

Runtime::Runtime() {
  DBusProxyFactory factory(NULL);
  DBusProxy *proxy = factory.NewSystemProxy(kHalDBusName,
                                            kHalObjectComputer,
                                            kHalInterfaceDevice,
                                            false);
  ASSERT(proxy);
  DBusStringReceiver name_receiver;
  if (!proxy->Call(kHalMethodGetProperty, true, -1,
                   name_receiver.NewSlot(),
                   MESSAGE_TYPE_STRING, kHalPropSystemKernelName,
                   MESSAGE_TYPE_INVALID)) {
    DLOG("Failed to get kernel name.");
    os_name_ = GGL_PLATFORM;
  } else {
    os_name_ = name_receiver.GetValue();
  }
  DBusStringReceiver version_receiver;
  if (!proxy->Call(kHalMethodGetProperty, true, -1,
                   version_receiver.NewSlot(),
                   MESSAGE_TYPE_STRING, kHalPropSystemKernelVersion,
                   MESSAGE_TYPE_INVALID)) {
    DLOG("Failed to get kernel version.");
  } else {
    os_version_ = version_receiver.GetValue();
  }
  delete proxy;
}

Runtime::~Runtime() {
}

} // namespace linux_system
} // namespace framework
} // namespace ggadget
<commit_msg>Stopped using D-Bus to retrieve the osName and osVersion.<commit_after>/*
  Copyright 2008 Google Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#include "runtime.h"
#include <ggadget/sysdeps.h>
#include <ggadget/logger.h>
#include <sys/utsname.h>

namespace ggadget {
namespace framework {
namespace linux_system {

Runtime::Runtime() {
  struct utsname uts;
  if (uname(&uts)) {
    DLOG("Failed to get the system information.");
    os_name_ = GGL_PLATFORM;
  } else {
    os_name_ = uts.sysname;
    os_version_ = uts.release;
  }
}

Runtime::~Runtime() {
}

} // namespace linux_system
} // namespace framework
} // namespace ggadget
<|endoftext|>


<commit_before>//=======================================================================
// Copyright Baptiste Wicht 2015-2016.
// Distributed under the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#ifndef WORD_SPOTTER_SCALING_HPP
#define WORD_SPOTTER_SCALING_HPP

template <typename Features>
void local_linear_feature_scaling(std::vector<Features>& features) {
    const auto width = features.size();

    for (std::size_t f = 0; f < features.back().size(); ++f) {
        double A = features[0][f];
        double B = features[0][f];

        for (std::size_t i = 1; i < width; ++i) {
            A = std::min(A, features[i][f]);
            B = std::max(B, features[i][f]);
        }

        auto scale = [A, B](auto x) { return (x - A) / (B - A); };

        for (std::size_t i = 0; i < width; ++i) {
            features[i][f] = scale(features[i][f]);
        }
    }
}

template <typename Features>
void local_mean_feature_scaling(std::vector<Features>& features) {
    const auto width = features.size();

    for (std::size_t f = 0; f < features.back().size(); ++f) {
        // Compute the mean
        double mean = 0.0;
        for (std::size_t i = 0; i < width; ++i) {
            mean += features[i][f];
        }
        mean /= width;
        //Normalize to zero-mean
        for (std::size_t i = 0; i < width; ++i) {
            features[i][f] -= mean;
        }
        //Compute the variance
        double std = 0.0;
        for (std::size_t i = 0; i < width; ++i) {
            std += features[i][f] * features[i][f];
        }
        std = std::sqrt(std / width);
        //Normalize to unit variance
        if (std != 0.0) {
            for (std::size_t i = 0; i < width; ++i) {
                features[i][f] /= std;
            }
        }
    }
}

template <typename Features>
auto global_mean_scaling(Features& features, config& conf, bool training) {
    if (training) {
        for (std::size_t f = 0; f < features.back().back().size(); ++f) {
            // Compute the mean
            double mean  = 0.0;
            double count = 0;
            for (std::size_t t = 0; t < features.size(); ++t) {
                for (std::size_t i = 0; i < features[t].size(); ++i) {
                    mean += features[t][i][f];
                    ++count;
                }
            }
            mean /= count;

            //Compute the variance
            double std = 0.0;
            count = 0.0;
            for (std::size_t t = 0; t < features.size(); ++t) {
                for (std::size_t i = 0; i < features[t].size(); ++i) {
                    std += (features[t][i][f] - mean) * (features[t][i][f] - mean);
                    ++count;
                }
            }
            std = std::sqrt(std / count);

            conf.scale_a[f] = mean;
            conf.scale_b[f] = std;
        }
    }

    return [](double x, double mean, double std) -> double { return std == 0.0 ? x - mean : (x - mean) / std; };
}

template <typename Features>
auto global_linear_scaling(Features& features, config& conf, bool training) {
    if (training) {
        for (std::size_t f = 0; f < features.back().back().size(); ++f) {
            double A = features[0][0][f];
            double B = features[0][0][f];

            for (std::size_t t = 0; t < features.size(); ++t) {
                for (std::size_t i = 0; i < features[t].size(); ++i) {
                    A = std::min(A, features[t][i][f]);
                    B = std::max(B, features[t][i][f]);
                }
            }

            conf.scale_a[f] = A;
            conf.scale_b[f] = B;
        }
    }

    return [](double x, double A, double B) -> double { return (x - A) / (B - A); };
}

#endif
<commit_msg>Fix type for std::min<commit_after>//=======================================================================
// Copyright Baptiste Wicht 2015-2016.
// Distributed under the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#ifndef WORD_SPOTTER_SCALING_HPP
#define WORD_SPOTTER_SCALING_HPP

template <typename Features>
void local_linear_feature_scaling(std::vector<Features>& features) {
    const auto width = features.size();

    for (std::size_t f = 0; f < features.back().size(); ++f) {
        auto A = features[0][f];
        auto B = features[0][f];

        for (std::size_t i = 1; i < width; ++i) {
            A = std::min(A, features[i][f]);
            B = std::max(B, features[i][f]);
        }

        auto scale = [A, B](auto x) { return (x - A) / (B - A); };

        for (std::size_t i = 0; i < width; ++i) {
            features[i][f] = scale(features[i][f]);
        }
    }
}

template <typename Features>
void local_mean_feature_scaling(std::vector<Features>& features) {
    const auto width = features.size();

    for (std::size_t f = 0; f < features.back().size(); ++f) {
        // Compute the mean
        double mean = 0.0;
        for (std::size_t i = 0; i < width; ++i) {
            mean += features[i][f];
        }
        mean /= width;
        //Normalize to zero-mean
        for (std::size_t i = 0; i < width; ++i) {
            features[i][f] -= mean;
        }
        //Compute the variance
        double std = 0.0;
        for (std::size_t i = 0; i < width; ++i) {
            std += features[i][f] * features[i][f];
        }
        std = std::sqrt(std / width);
        //Normalize to unit variance
        if (std != 0.0) {
            for (std::size_t i = 0; i < width; ++i) {
                features[i][f] /= std;
            }
        }
    }
}

template <typename Features>
auto global_mean_scaling(Features& features, config& conf, bool training) {
    if (training) {
        for (std::size_t f = 0; f < features.back().back().size(); ++f) {
            // Compute the mean
            double mean  = 0.0;
            double count = 0;
            for (std::size_t t = 0; t < features.size(); ++t) {
                for (std::size_t i = 0; i < features[t].size(); ++i) {
                    mean += features[t][i][f];
                    ++count;
                }
            }
            mean /= count;

            //Compute the variance
            double std = 0.0;
            count = 0.0;
            for (std::size_t t = 0; t < features.size(); ++t) {
                for (std::size_t i = 0; i < features[t].size(); ++i) {
                    std += (features[t][i][f] - mean) * (features[t][i][f] - mean);
                    ++count;
                }
            }
            std = std::sqrt(std / count);

            conf.scale_a[f] = mean;
            conf.scale_b[f] = std;
        }
    }

    return [](double x, double mean, double std) -> double { return std == 0.0 ? x - mean : (x - mean) / std; };
}

template <typename Features>
auto global_linear_scaling(Features& features, config& conf, bool training) {
    if (training) {
        for (std::size_t f = 0; f < features.back().back().size(); ++f) {
            double A = features[0][0][f];
            double B = features[0][0][f];

            for (std::size_t t = 0; t < features.size(); ++t) {
                for (std::size_t i = 0; i < features[t].size(); ++i) {
                    A = std::min(A, features[t][i][f]);
                    B = std::max(B, features[t][i][f]);
                }
            }

            conf.scale_a[f] = A;
            conf.scale_b[f] = B;
        }
    }

    return [](double x, double A, double B) -> double { return (x - A) / (B - A); };
}

#endif
<|endoftext|>


<commit_before>////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2017 ArangoDB GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Jan Steemann
////////////////////////////////////////////////////////////////////////////////

#include "RocksDBCollectionExport.h"
#include "Basics/WriteLocker.h"
#include "RocksDBEngine/RocksDBCollection.h"
#include "StorageEngine/EngineSelectorFeature.h"
#include "StorageEngine/PhysicalCollection.h"
#include "StorageEngine/StorageEngine.h"
#include "Transaction/Hints.h"
#include "Transaction/StandaloneContext.h"
#include "Utils/CollectionGuard.h"
#include "Utils/SingleCollectionTransaction.h"
#include "VocBase/LogicalCollection.h"
#include "VocBase/vocbase.h"

using namespace arangodb;

RocksDBCollectionExport::RocksDBCollectionExport(
    TRI_vocbase_t* vocbase, std::string const& name,
    CollectionExport::Restrictions const& restrictions)
    : _collection(nullptr),
      _name(name),
      _resolver(vocbase),
      _restrictions(restrictions) {
  // prevent the collection from being unloaded while the export is ongoing
  // this may throw
  _guard.reset(new arangodb::CollectionGuard(vocbase, _name.c_str(), false));

  _collection = _guard->collection();
  TRI_ASSERT(_collection != nullptr);
}

RocksDBCollectionExport::~RocksDBCollectionExport() {}

void RocksDBCollectionExport::run(size_t limit) {
  SingleCollectionTransaction trx(
      transaction::StandaloneContext::Create(_collection->vocbase()), _name,
      AccessMode::Type::READ);

  // already locked by guard above
  trx.addHint(transaction::Hints::Hint::NO_USAGE_LOCK);
  Result res = trx.begin();

  if (!res.ok()) {
    THROW_ARANGO_EXCEPTION(res);
  }

  _vpack.reserve(limit);

  ManagedDocumentResult mmdr;
  trx.invokeOnAllElements(
      _collection->name(),
      [this, &limit, &trx, &mmdr](DocumentIdentifierToken const& token) {
        if (limit == 0) {
          return false;
        }
        if (_collection->readDocument(&trx, token, mmdr)) {
          _vpack.emplace_back(VPackSlice(mmdr.vpack()));
          --limit;
        }
        return true;
      });

  trx.finish(res.errorNumber());

  // delete guard right now as we're about to return
  // if we would continue holding the guard's collection lock and return,
  // and the export object gets later freed in a different thread, then all
  // would be lost. so we'll release the lock here and rely on the cleanup
  // thread not unloading the collection (as we've acquired a document ditch
  // for the collection already - this will prevent unloading of the
  // collection's
  // datafiles etc.)
  _guard.reset();
}
<commit_msg>unintentionally broke the export<commit_after>////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2017 ArangoDB GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Jan Steemann
////////////////////////////////////////////////////////////////////////////////

#include "RocksDBCollectionExport.h"
#include "Basics/WriteLocker.h"
#include "RocksDBEngine/RocksDBCollection.h"
#include "StorageEngine/EngineSelectorFeature.h"
#include "StorageEngine/PhysicalCollection.h"
#include "StorageEngine/StorageEngine.h"
#include "Transaction/Hints.h"
#include "Transaction/StandaloneContext.h"
#include "Utils/CollectionGuard.h"
#include "Utils/SingleCollectionTransaction.h"
#include "VocBase/LogicalCollection.h"
#include "VocBase/vocbase.h"

using namespace arangodb;

RocksDBCollectionExport::RocksDBCollectionExport(
    TRI_vocbase_t* vocbase, std::string const& name,
    CollectionExport::Restrictions const& restrictions)
    : _collection(nullptr),
      _name(name),
      _resolver(vocbase),
      _restrictions(restrictions) {
  // prevent the collection from being unloaded while the export is ongoing
  // this may throw
  _guard.reset(new arangodb::CollectionGuard(vocbase, _name.c_str(), false));

  _collection = _guard->collection();
  TRI_ASSERT(_collection != nullptr);
}

RocksDBCollectionExport::~RocksDBCollectionExport() {}

void RocksDBCollectionExport::run(size_t limit) {
  SingleCollectionTransaction trx(
      transaction::StandaloneContext::Create(_collection->vocbase()), _name,
      AccessMode::Type::READ);

  // already locked by guard above
  trx.addHint(transaction::Hints::Hint::NO_USAGE_LOCK);
  Result res = trx.begin();

  if (!res.ok()) {
    THROW_ARANGO_EXCEPTION(res);
  }

  size_t maxDocuments = _collection->numberDocuments(&trx);
  if (limit > 0 && limit < maxDocuments) {
    maxDocuments = limit;
  } else {
    limit = maxDocuments;
  }

  _vpack.reserve(maxDocuments);

  ManagedDocumentResult mmdr;
  trx.invokeOnAllElements(
      _collection->name(),
      [this, &limit, &trx, &mmdr](DocumentIdentifierToken const& token) {
        if (limit == 0) {
          return false;
        }
        if (_collection->readDocument(&trx, token, mmdr)) {
          _vpack.emplace_back(VPackSlice(mmdr.vpack()));
          --limit;
        }
        return true;
      });

  trx.finish(res.errorNumber());

  // delete guard right now as we're about to return
  // if we would continue holding the guard's collection lock and return,
  // and the export object gets later freed in a different thread, then all
  // would be lost. so we'll release the lock here and rely on the cleanup
  // thread not unloading the collection (as we've acquired a document ditch
  // for the collection already - this will prevent unloading of the
  // collection's
  // datafiles etc.)
  _guard.reset();
}
<|endoftext|>


<commit_before>#include <vector>

#include "hdf5.h"
#include "hdf5_hl.h"

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/layer.hpp"
#include "caffe/util/io.hpp"
#include "caffe/vision_layers.hpp"

namespace caffe {

template <typename Dtype>
HDF5OutputLayer<Dtype>::HDF5OutputLayer(const LayerParameter& param)
    : Layer<Dtype>(param),
      file_name_(param.hdf5_output_param().file_name()) {
  /* create a HDF5 file */
  file_id_ = H5Fcreate(file_name_.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT,
                       H5P_DEFAULT);
  CHECK_GE(file_id_, 0) << "Failed to open HDF5 file" << file_name_;
}

template <typename Dtype>
HDF5OutputLayer<Dtype>::~HDF5OutputLayer<Dtype>() {
  herr_t status = H5Fclose(file_id_);
  CHECK_GE(status, 0) << "Failed to close HDF5 file " << file_name_;
}

template <typename Dtype>
void HDF5OutputLayer<Dtype>::SaveBlobs() {
  // TODO: no limit on the number of blobs
  LOG(INFO) << "Saving HDF5 file" << file_name_;
  CHECK_EQ(data_blob_.num(), label_blob_.num()) <<
      "data blob and label blob must have the same batch size";
  hdf5_save_nd_dataset(file_id_, HDF5_DATA_DATASET_NAME, data_blob_);
  hdf5_save_nd_dataset(file_id_, HDF5_DATA_LABEL_NAME, label_blob_);
  LOG(INFO) << "Successfully saved " << data_blob_.num() << " rows";
}

template <typename Dtype>
void HDF5OutputLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
  CHECK_GE(bottom.size(), 2);
  CHECK_EQ(bottom[0]->num(), bottom[1]->num());
  data_blob_.Reshape(bottom[0]->num(), bottom[0]->channels(),
                     bottom[0]->height(), bottom[0]->width());
  label_blob_.Reshape(bottom[1]->num(), bottom[1]->channels(),
                     bottom[1]->height(), bottom[1]->width());
  const int data_datum_dim = bottom[0]->count() / bottom[0]->num();
  const int label_datum_dim = bottom[1]->count() / bottom[1]->num();

  for (int i = 0; i < bottom[0]->num(); ++i) {
    caffe_copy(data_datum_dim, &bottom[0]->cpu_data()[i * data_datum_dim],
        &data_blob_.mutable_cpu_data()[i * data_datum_dim]);
    caffe_copy(label_datum_dim, &bottom[1]->cpu_data()[i * label_datum_dim],
        &label_blob_.mutable_cpu_data()[i * label_datum_dim]);
  }
  SaveBlobs();
}

template <typename Dtype>
void HDF5OutputLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  return;
}

#ifdef CPU_ONLY
STUB_GPU(HDF5OutputLayer);
#endif

INSTANTIATE_CLASS(HDF5OutputLayer);
REGISTER_LAYER_CLASS(HDF5_OUTPUT, HDF5OutputLayer);
}  // namespace caffe
<commit_msg>Minor whitespace fix in logging message in HDF5 output layer<commit_after>#include <vector>

#include "hdf5.h"
#include "hdf5_hl.h"

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/layer.hpp"
#include "caffe/util/io.hpp"
#include "caffe/vision_layers.hpp"

namespace caffe {

template <typename Dtype>
HDF5OutputLayer<Dtype>::HDF5OutputLayer(const LayerParameter& param)
    : Layer<Dtype>(param),
      file_name_(param.hdf5_output_param().file_name()) {
  /* create a HDF5 file */
  file_id_ = H5Fcreate(file_name_.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT,
                       H5P_DEFAULT);
  CHECK_GE(file_id_, 0) << "Failed to open HDF5 file" << file_name_;
}

template <typename Dtype>
HDF5OutputLayer<Dtype>::~HDF5OutputLayer<Dtype>() {
  herr_t status = H5Fclose(file_id_);
  CHECK_GE(status, 0) << "Failed to close HDF5 file " << file_name_;
}

template <typename Dtype>
void HDF5OutputLayer<Dtype>::SaveBlobs() {
  // TODO: no limit on the number of blobs
  LOG(INFO) << "Saving HDF5 file " << file_name_;
  CHECK_EQ(data_blob_.num(), label_blob_.num()) <<
      "data blob and label blob must have the same batch size";
  hdf5_save_nd_dataset(file_id_, HDF5_DATA_DATASET_NAME, data_blob_);
  hdf5_save_nd_dataset(file_id_, HDF5_DATA_LABEL_NAME, label_blob_);
  LOG(INFO) << "Successfully saved " << data_blob_.num() << " rows";
}

template <typename Dtype>
void HDF5OutputLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
  CHECK_GE(bottom.size(), 2);
  CHECK_EQ(bottom[0]->num(), bottom[1]->num());
  data_blob_.Reshape(bottom[0]->num(), bottom[0]->channels(),
                     bottom[0]->height(), bottom[0]->width());
  label_blob_.Reshape(bottom[1]->num(), bottom[1]->channels(),
                     bottom[1]->height(), bottom[1]->width());
  const int data_datum_dim = bottom[0]->count() / bottom[0]->num();
  const int label_datum_dim = bottom[1]->count() / bottom[1]->num();

  for (int i = 0; i < bottom[0]->num(); ++i) {
    caffe_copy(data_datum_dim, &bottom[0]->cpu_data()[i * data_datum_dim],
        &data_blob_.mutable_cpu_data()[i * data_datum_dim]);
    caffe_copy(label_datum_dim, &bottom[1]->cpu_data()[i * label_datum_dim],
        &label_blob_.mutable_cpu_data()[i * label_datum_dim]);
  }
  SaveBlobs();
}

template <typename Dtype>
void HDF5OutputLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  return;
}

#ifdef CPU_ONLY
STUB_GPU(HDF5OutputLayer);
#endif

INSTANTIATE_CLASS(HDF5OutputLayer);
REGISTER_LAYER_CLASS(HDF5_OUTPUT, HDF5OutputLayer);
}  // namespace caffe
<|endoftext|>


<commit_before><commit_msg>[routing] Review fixes.<commit_after><|endoftext|>