<commit_before>/*
 * 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; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 2013 Soumyajit De
 */

#include <shogun/lib/common.h>

#ifdef HAVE_COLPACK
#ifdef HAVE_EIGEN3

#include <vector>
#include <string>
#include <cstring>
#include <shogun/lib/SGVector.h>
#include <shogun/lib/SGString.h>
#include <shogun/base/Parameter.h>
#include <shogun/mathematics/eigen3.h>
#include <shogun/mathematics/Random.h>
#include <shogun/mathematics/logdet/SparseMatrixOperator.h>
#include <shogun/mathematics/logdet/ProbingSampler.h>
#include <ColPack/ColPackHeaders.h>

using namespace Eigen;
using namespace ColPack;

namespace shogun
{

CProbingSampler::CProbingSampler() : CTraceSampler()
{
	init();

	SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
}

CProbingSampler::CProbingSampler(
	CSparseMatrixOperator<float64_t>* matrix_operator, int64_t power,
	EOrderingVariant ordering, EColoringVariant coloring)
	: CTraceSampler(matrix_operator->get_dimension())
{
	init();

	m_power=power;
	m_matrix_operator=matrix_operator;

	std::string str_ordering;
	switch(ordering)
	{
	case NATURAL:
		str_ordering="NATURAL";
		break;
	case LARGEST_FIRST:
		str_ordering="LARGEST_FIRST";
		break;
	case DYNAMIC_LARGEST_FIRST:
		str_ordering="DYNAMIC_LARGEST_FIRST";
		break;
	case DISTANCE_TWO_LARGEST_FIRST:
		str_ordering="DISTANCE_TWO_LARGEST_FIRST";
		break;
	case SMALLEST_LAST:
		str_ordering="SMALLEST_LAST";
		break;
	case DISTANCE_TWO_SMALLEST_LAST:
		str_ordering="DISTANCE_TWO_SMALLEST_LAST";
		break;
	case INCIDENCE_DEGREE:
		str_ordering="INCIDENCE_DEGREE";
		break;
	case DISTANCE_TWO_INCIDENCE_DEGREE:
		str_ordering="DISTANCE_TWO_INCIDENCE_DEGREE";
		break;
	case RANDOM:
		str_ordering="RANDOM";
		break;
	}

	std::string str_coloring;
	switch(coloring)
	{
	case DISTANCE_ONE:
		str_coloring="DISTANCE_ONE";
		break;
	case ACYCLIC:
		str_coloring="ACYCLIC";
		break;
	case ACYCLIC_FOR_INDIRECT_RECOVERY:
		str_coloring="ACYCLIC_FOR_INDIRECT_RECOVERY";
		break;
	case STAR:
		str_coloring="STAR";
		break;
	case RESTRICTED_STAR:
		str_coloring="RESTRICTED_STAR";
		break;
	case DISTANCE_TWO:
		str_coloring="DISTANCE_TWO";
		break;
	}

	m_ordering=SGString<char>(index_t(str_ordering.size()));
	m_coloring=SGString<char>(index_t(str_coloring.size()));
	memcpy(m_ordering.string, str_ordering.data(), str_ordering.size());
	memcpy(m_coloring.string, str_coloring.data(), str_coloring.size());

	SG_REF(m_matrix_operator);

	SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
}

void CProbingSampler::init()
{
	m_matrix_operator=NULL;
	m_power=1;

	SG_ADD(&m_probing_vector, "probing_vector", "the probing vector generated"
		" from coloring", MS_NOT_AVAILABLE);

	SG_ADD(&m_power, "matrix-power", "power of the sparse-matrix for coloring",
		MS_NOT_AVAILABLE);

	SG_ADD(&m_ordering, "ordering-variant", "ordering variant for coloring",
		MS_NOT_AVAILABLE);

	SG_ADD(&m_coloring, "coloring-variant", "coloring variant for coloring",
		MS_NOT_AVAILABLE);

	SG_ADD((CSGObject**)&m_matrix_operator, "matrix-operator",
		"the sparse-matrix linear opeator for probing", MS_NOT_AVAILABLE);
}

CProbingSampler::~CProbingSampler()
{
	SG_UNREF(m_matrix_operator);

	SG_GCDEBUG("%s destroyed (%p)\n", this->get_name(), this)
}

SGVector<int32_t> CProbingSampler::get_probing_vector() const
{
	return m_probing_vector;
}

void CProbingSampler::precompute()
{
	// do coloring things here and save the probing vector
	SparsityStructure* sp_str=m_matrix_operator->get_sparsity_structure(m_power);

	GraphColoringInterface* Color
		=new GraphColoringInterface(SRC_MEM_ADOLC, sp_str->m_ptr, sp_str->m_num_rows);

	std::string ordering(m_ordering.string, m_ordering.slen);
	std::string coloring(m_coloring.string, m_coloring.slen);
	Color->Coloring(ordering, coloring);

	std::vector<int32_t> vi_VertexColors;
	Color->GetVertexColors(vi_VertexColors);

	REQUIRE(vi_VertexColors.size()==static_cast<uint32_t>(m_dimension),
		"dimension mismatch, %d vs %d!\n", vi_VertexColors.size(), m_dimension);

	m_probing_vector=SGVector<int32_t>(vi_VertexColors.size());

	for (std::vector<int32_t>::iterator it=vi_VertexColors.begin();
		it!=vi_VertexColors.end(); it++)
	{
		index_t i=static_cast<index_t>(std::distance(vi_VertexColors.begin(), it));
		m_probing_vector[i]=*it;
	}

	Map<VectorXi> probe(m_probing_vector.vector, m_probing_vector.vlen);
	m_num_samples=probe.maxCoeff()+1;

	delete sp_str;
	delete Color;
}

SGVector<float64_t> CProbingSampler::sample(index_t idx) const
{
	SGVector<float64_t> s(m_dimension);
	s.set_const(0.0);
	if (idx>=m_num_samples)
		SG_WARNING("idx should be less than %d\n", m_num_samples)
	else
	{
		for (index_t i=0; i<m_dimension; ++i)
		{
			if (m_probing_vector[i]==idx)
			{
				float64_t x=sg_rand->std_normal_distrib();
				s[i]=(x>0)-(x<0);
			}
		}
	}
	return s;
}

}

#endif // HAVE_EIGEN3
#endif // HAVE_COLPACK
<commit_msg>make index warning an assertion to prevent wrong samples<commit_after>/*
 * 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; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 2013 Soumyajit De
 */

#include <shogun/lib/common.h>

#ifdef HAVE_COLPACK
#ifdef HAVE_EIGEN3

#include <vector>
#include <string>
#include <cstring>
#include <shogun/lib/SGVector.h>
#include <shogun/lib/SGString.h>
#include <shogun/base/Parameter.h>
#include <shogun/mathematics/eigen3.h>
#include <shogun/mathematics/Random.h>
#include <shogun/mathematics/logdet/SparseMatrixOperator.h>
#include <shogun/mathematics/logdet/ProbingSampler.h>
#include <ColPack/ColPackHeaders.h>

using namespace Eigen;
using namespace ColPack;

namespace shogun
{

CProbingSampler::CProbingSampler() : CTraceSampler()
{
	init();

	SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
}

CProbingSampler::CProbingSampler(
	CSparseMatrixOperator<float64_t>* matrix_operator, int64_t power,
	EOrderingVariant ordering, EColoringVariant coloring)
	: CTraceSampler(matrix_operator->get_dimension())
{
	init();

	m_power=power;
	m_matrix_operator=matrix_operator;

	std::string str_ordering;
	switch(ordering)
	{
	case NATURAL:
		str_ordering="NATURAL";
		break;
	case LARGEST_FIRST:
		str_ordering="LARGEST_FIRST";
		break;
	case DYNAMIC_LARGEST_FIRST:
		str_ordering="DYNAMIC_LARGEST_FIRST";
		break;
	case DISTANCE_TWO_LARGEST_FIRST:
		str_ordering="DISTANCE_TWO_LARGEST_FIRST";
		break;
	case SMALLEST_LAST:
		str_ordering="SMALLEST_LAST";
		break;
	case DISTANCE_TWO_SMALLEST_LAST:
		str_ordering="DISTANCE_TWO_SMALLEST_LAST";
		break;
	case INCIDENCE_DEGREE:
		str_ordering="INCIDENCE_DEGREE";
		break;
	case DISTANCE_TWO_INCIDENCE_DEGREE:
		str_ordering="DISTANCE_TWO_INCIDENCE_DEGREE";
		break;
	case RANDOM:
		str_ordering="RANDOM";
		break;
	}

	std::string str_coloring;
	switch(coloring)
	{
	case DISTANCE_ONE:
		str_coloring="DISTANCE_ONE";
		break;
	case ACYCLIC:
		str_coloring="ACYCLIC";
		break;
	case ACYCLIC_FOR_INDIRECT_RECOVERY:
		str_coloring="ACYCLIC_FOR_INDIRECT_RECOVERY";
		break;
	case STAR:
		str_coloring="STAR";
		break;
	case RESTRICTED_STAR:
		str_coloring="RESTRICTED_STAR";
		break;
	case DISTANCE_TWO:
		str_coloring="DISTANCE_TWO";
		break;
	}

	m_ordering=SGString<char>(index_t(str_ordering.size()));
	m_coloring=SGString<char>(index_t(str_coloring.size()));
	memcpy(m_ordering.string, str_ordering.data(), str_ordering.size());
	memcpy(m_coloring.string, str_coloring.data(), str_coloring.size());

	SG_REF(m_matrix_operator);

	SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
}

void CProbingSampler::init()
{
	m_matrix_operator=NULL;
	m_power=1;

	SG_ADD(&m_probing_vector, "probing_vector", "the probing vector generated"
		" from coloring", MS_NOT_AVAILABLE);

	SG_ADD(&m_power, "matrix-power", "power of the sparse-matrix for coloring",
		MS_NOT_AVAILABLE);

	SG_ADD(&m_ordering, "ordering-variant", "ordering variant for coloring",
		MS_NOT_AVAILABLE);

	SG_ADD(&m_coloring, "coloring-variant", "coloring variant for coloring",
		MS_NOT_AVAILABLE);

	SG_ADD((CSGObject**)&m_matrix_operator, "matrix-operator",
		"the sparse-matrix linear opeator for probing", MS_NOT_AVAILABLE);
}

CProbingSampler::~CProbingSampler()
{
	SG_UNREF(m_matrix_operator);

	SG_GCDEBUG("%s destroyed (%p)\n", this->get_name(), this)
}

SGVector<int32_t> CProbingSampler::get_probing_vector() const
{
	return m_probing_vector;
}

void CProbingSampler::precompute()
{
	// do coloring things here and save the probing vector
	SparsityStructure* sp_str=m_matrix_operator->get_sparsity_structure(m_power);

	GraphColoringInterface* Color
		=new GraphColoringInterface(SRC_MEM_ADOLC, sp_str->m_ptr, sp_str->m_num_rows);

	std::string ordering(m_ordering.string, m_ordering.slen);
	std::string coloring(m_coloring.string, m_coloring.slen);
	Color->Coloring(ordering, coloring);

	std::vector<int32_t> vi_VertexColors;
	Color->GetVertexColors(vi_VertexColors);

	REQUIRE(vi_VertexColors.size()==static_cast<uint32_t>(m_dimension),
		"dimension mismatch, %d vs %d!\n", vi_VertexColors.size(), m_dimension);

	m_probing_vector=SGVector<int32_t>(vi_VertexColors.size());

	for (std::vector<int32_t>::iterator it=vi_VertexColors.begin();
		it!=vi_VertexColors.end(); it++)
	{
		index_t i=static_cast<index_t>(std::distance(vi_VertexColors.begin(), it));
		m_probing_vector[i]=*it;
	}

	Map<VectorXi> probe(m_probing_vector.vector, m_probing_vector.vlen);
	m_num_samples=probe.maxCoeff()+1;

	delete sp_str;
	delete Color;
}

SGVector<float64_t> CProbingSampler::sample(index_t idx) const
{
	REQUIRE(idx<m_num_samples, "Given index (%d) must be smaller than "
			"number of samples to draw (%d)\n", idx, m_num_samples);

	SGVector<float64_t> s(m_dimension);
	s.set_const(0.0);

	for (index_t i=0; i<m_dimension; ++i)
	{
		if (m_probing_vector[i]==idx)
		{
			float64_t x=sg_rand->std_normal_distrib();
			s[i]=(x>0)-(x<0);
		}
	}

	return s;
}

}

#endif // HAVE_EIGEN3
#endif // HAVE_COLPACK
<|endoftext|>


<commit_before>/*
 *
 * Copyright 2016 gRPC authors.
 *
 * 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 <stdbool.h>
#include <stdint.h>
#include <string.h>

#include <grpc/grpc.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>

#include "src/core/lib/slice/percent_encoding.h"
#include "test/core/util/memory_counters.h"

bool squelch = true;
bool leak_check = true;

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  struct grpc_memory_counters counters;
  grpc_init();
  grpc_memory_counters_init();
  grpc_slice input = grpc_slice_from_copied_buffer((const char*)data, size);
  grpc_slice output;
  if (grpc_strict_percent_decode_slice(
          input, grpc_url_percent_encoding_unreserved_bytes, &output)) {
    grpc_slice_unref(output);
  }
  if (grpc_strict_percent_decode_slice(
          input, grpc_compatible_percent_encoding_unreserved_bytes, &output)) {
    grpc_slice_unref(output);
  }
  grpc_slice_unref(grpc_permissive_percent_decode_slice(input));
  grpc_slice_unref(input);
  counters = grpc_memory_counters_snapshot();
  grpc_memory_counters_destroy();
  grpc_shutdown();
  GPR_ASSERT(counters.total_size_relative == 0);
  return 0;
}
<commit_msg>Fix percent decode fuzzer<commit_after>/*
 *
 * Copyright 2016 gRPC authors.
 *
 * 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 <stdbool.h>
#include <stdint.h>
#include <string.h>

#include <grpc/grpc.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>

#include "src/core/lib/slice/percent_encoding.h"
#include "test/core/util/memory_counters.h"

bool squelch = true;
bool leak_check = true;

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  grpc_core::testing::LeakDetector leak_detector(true);
  grpc_init();
  grpc_slice input = grpc_slice_from_copied_buffer((const char*)data, size);
  grpc_slice output;
  if (grpc_strict_percent_decode_slice(
          input, grpc_url_percent_encoding_unreserved_bytes, &output)) {
    grpc_slice_unref(output);
  }
  if (grpc_strict_percent_decode_slice(
          input, grpc_compatible_percent_encoding_unreserved_bytes, &output)) {
    grpc_slice_unref(output);
  }
  grpc_slice_unref(grpc_permissive_percent_decode_slice(input));
  grpc_slice_unref(input);
  grpc_shutdown();
  return 0;
}
<|endoftext|>


<commit_before>#include "acmacs-base/range-v3.hh"
#include "acmacs-whocc/sheet.hh"
#include "acmacs-whocc/log.hh"

// ----------------------------------------------------------------------

bool acmacs::sheet::v1::Sheet::matches(const std::regex& re, const cell_t& cell)
{
    return std::visit(
        [&re, &cell]<typename Content>(const Content& arg) {
            if constexpr (std::is_same_v<Content, std::string>)
                return std::regex_search(arg, re);
            else
                return std::regex_search(fmt::format("{}", cell), re); // CDC id is a number in CDC tables, still we want to match
        },
        cell);

} // acmacs::sheet::v1::Sheet::matches

// ----------------------------------------------------------------------

bool acmacs::sheet::v1::Sheet::matches(const std::regex& re, std::smatch& match, const cell_t& cell)
{
    return std::visit(
        [&re, &match]<typename Content>(const Content& arg) {
            if constexpr (std::is_same_v<Content, std::string>)
                return std::regex_search(arg, match, re);
            else
                return false;
        },
        cell);

} // acmacs::sheet::v1::Sheet::matches

// ----------------------------------------------------------------------

size_t acmacs::sheet::v1::Sheet::size(const cell_t& cell) const
{
    return std::visit(
        []<typename Content>(const Content& arg) {
            if constexpr (std::is_same_v<Content, std::string>)
                return arg.size();
            else
                return 0ul;
        },
        cell);

} // acmacs::sheet::v1::Sheet::size

// ----------------------------------------------------------------------

#include "acmacs-base/global-constructors-push.hh"

// \xEF\xBC\x9C -> "<" unicode Fullwidth Less-Than Sign ＜ (NIID)
static const std::regex re_titer{R"(^(<|(?:<|>|\xEF\xBC\x9C)?\s*[1-9][0-9]{0,5}|N[DA]|QNS|\*)$)", acmacs::regex::icase};

#include "acmacs-base/diagnostics-pop.hh"

bool acmacs::sheet::v1::Sheet::maybe_titer(const cell_t& cell) const
{
    return std::visit(
        []<typename Content>(const Content& arg) {
            if constexpr (std::is_same_v<Content, std::string>) {
                // if (!arg.empty() && static_cast<unsigned char>(arg[0]) > 0x7F) {
                //     AD_DEBUG("titer? \"{}\"", arg);
                //     for (auto cc : arg)
                //         AD_DEBUG("titer? 0x{:X}", static_cast<unsigned char>(cc));
                // }
                return std::regex_search(arg, re_titer);
            }
            else if constexpr (std::is_same_v<Content, double> || std::is_same_v<Content, long>)
                return arg > 0;
            else
                return false;
        },
        cell);

} // acmacs::sheet::v1::Sheet::maybe_titer

// ----------------------------------------------------------------------

acmacs::sheet::v1::column_range acmacs::sheet::v1::Sheet::titer_range(nrow_t row) const
{
    range<ncol_t> longest;
    range<ncol_t> current;
    const auto update = [&longest, &current] {
        if (current.valid()) {
            if (!longest.valid() || longest.length() < current.length()) {
                longest = current;
                // AD_DEBUG("longest {}: {}", row, longest);
            }
            current = range<ncol_t>{};
        }
    };

    for (auto col = ncol_t{0}; col < number_of_columns(); ++col) {
        if (maybe_titer(row, col)) {
            if (!current.valid())
                current.first = col;
            current.second = col;
            // AD_DEBUG("titer {}{}: {} --> {}", row, col, cell(row, col), current);
        }
        else
            update();
    }
    update();
    return longest;

} // acmacs::sheet::v1::Sheet::titer_range

// ----------------------------------------------------------------------

std::vector<acmacs::sheet::cell_match_t> acmacs::sheet::v1::Sheet::grep(const std::regex& rex, const cell_addr_t& min, const cell_addr_t& max) const
{
    std::vector<cell_match_t> result;
    for (auto row = min.row; row < max.row; ++row) {
        for (auto col = min.col; col < max.col; ++col) {
            const auto cl = cell(row, col);
            // AD_DEBUG("Sheet::grep {} {} \"{}\"", row, col, cl);
            std::smatch match;
            if (matches(rex, match, cl)) {
                cell_match_t cm{.row = row, .col = col, .matches = std::vector<std::string>(match.size())};
                std::transform(std::cbegin(match), std::cend(match), std::begin(cm.matches), [](const auto& submatch) { return submatch.str(); });
                result.push_back(std::move(cm));
            }
        }
    }
    return result;

} // acmacs::sheet::v1::Sheet::grep

// ----------------------------------------------------------------------
/// Local Variables:
/// eval: (if (fboundp 'eu-rename-buffer) (eu-rename-buffer))
/// End:
<commit_msg>VIDRL parser<commit_after>#include "acmacs-base/range-v3.hh"
#include "acmacs-whocc/sheet.hh"
#include "acmacs-whocc/log.hh"

// ----------------------------------------------------------------------

bool acmacs::sheet::v1::Sheet::matches(const std::regex& re, const cell_t& cell)
{
    return std::visit(
        [&re, &cell]<typename Content>(const Content& arg) {
            if constexpr (std::is_same_v<Content, std::string>)
                return std::regex_search(arg, re);
            else
                return std::regex_search(fmt::format("{}", cell), re); // CDC id is a number in CDC tables, still we want to match
        },
        cell);

} // acmacs::sheet::v1::Sheet::matches

// ----------------------------------------------------------------------

bool acmacs::sheet::v1::Sheet::matches(const std::regex& re, std::smatch& match, const cell_t& cell)
{
    return std::visit(
        [&re, &match]<typename Content>(const Content& arg) {
            if constexpr (std::is_same_v<Content, std::string>)
                return std::regex_search(arg, match, re);
            else
                return false;
        },
        cell);

} // acmacs::sheet::v1::Sheet::matches

// ----------------------------------------------------------------------

size_t acmacs::sheet::v1::Sheet::size(const cell_t& cell) const
{
    return std::visit(
        []<typename Content>(const Content& arg) {
            if constexpr (std::is_same_v<Content, std::string>)
                return arg.size();
            else
                return 0ul;
        },
        cell);

} // acmacs::sheet::v1::Sheet::size

// ----------------------------------------------------------------------

#include "acmacs-base/global-constructors-push.hh"

// \xEF\xBC\x9C -> "<" unicode Fullwidth Less-Than Sign ＜ (NIID)
static const std::regex re_titer{R"(^(<|(?:<|>|\xEF\xBC\x9C)?\s*[1-9][0-9]{0,5}|N[DAT]|QNS|\*)$)", acmacs::regex::icase};

#include "acmacs-base/diagnostics-pop.hh"

bool acmacs::sheet::v1::Sheet::maybe_titer(const cell_t& cell) const
{
    return std::visit(
        []<typename Content>(const Content& arg) {
            if constexpr (std::is_same_v<Content, std::string>) {
                // if (!arg.empty() && static_cast<unsigned char>(arg[0]) > 0x7F) {
                //     AD_DEBUG("titer? \"{}\"", arg);
                //     for (auto cc : arg)
                //         AD_DEBUG("titer? 0x{:X}", static_cast<unsigned char>(cc));
                // }
                return std::regex_search(arg, re_titer);
            }
            else if constexpr (std::is_same_v<Content, double> || std::is_same_v<Content, long>)
                return arg > 0;
            else
                return false;
        },
        cell);

} // acmacs::sheet::v1::Sheet::maybe_titer

// ----------------------------------------------------------------------

acmacs::sheet::v1::column_range acmacs::sheet::v1::Sheet::titer_range(nrow_t row) const
{
    range<ncol_t> longest;
    range<ncol_t> current;
    const auto update = [&longest, &current] {
        if (current.valid()) {
            if (!longest.valid() || longest.length() < current.length()) {
                longest = current;
                // AD_DEBUG("longest {}: {}", row, longest);
            }
            current = range<ncol_t>{};
        }
    };

    for (auto col = ncol_t{0}; col < number_of_columns(); ++col) {
        if (maybe_titer(row, col)) {
            if (!current.valid())
                current.first = col;
            current.second = col;
            // AD_DEBUG("titer {}{}: {} --> {}", row, col, cell(row, col), current);
        }
        else
            update();
    }
    update();
    return longest;

} // acmacs::sheet::v1::Sheet::titer_range

// ----------------------------------------------------------------------

std::vector<acmacs::sheet::cell_match_t> acmacs::sheet::v1::Sheet::grep(const std::regex& rex, const cell_addr_t& min, const cell_addr_t& max) const
{
    std::vector<cell_match_t> result;
    for (auto row = min.row; row < max.row; ++row) {
        for (auto col = min.col; col < max.col; ++col) {
            const auto cl = cell(row, col);
            // AD_DEBUG("Sheet::grep {} {} \"{}\"", row, col, cl);
            std::smatch match;
            if (matches(rex, match, cl)) {
                cell_match_t cm{.row = row, .col = col, .matches = std::vector<std::string>(match.size())};
                std::transform(std::cbegin(match), std::cend(match), std::begin(cm.matches), [](const auto& submatch) { return submatch.str(); });
                result.push_back(std::move(cm));
            }
        }
    }
    return result;

} // acmacs::sheet::v1::Sheet::grep

// ----------------------------------------------------------------------
/// Local Variables:
/// eval: (if (fboundp 'eu-rename-buffer) (eu-rename-buffer))
/// End:
<|endoftext|>


<commit_before>/*=========================================================================

Program:   ORFEO Toolbox
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif

#include <iostream>
#include <iomanip>

#include "otbCommandLineArgumentParser.h"
#include "otbImageFileReader.h"
#include "otbStreamingImageFileWriter.h"
#include "otbMapProjections.h"
#include "otbOrthoRectificationFilter.h"
#include "otbImage.h"
#include "otbMacro.h"

#include "itkExceptionObject.h"
#include "itkMacro.h"
#include "itkTransform.h"

#include "init/ossimInit.h"

template<typename TMapProjection>
int generic_main_carto_geo(TMapProjection* mapProjection, otb::CommandLineArgumentParseResult* parseResult)
{

  try
  {

    typedef TMapProjection MapProjectionType;

    typename MapProjectionType::InputPointType cartoPoint;
    typename MapProjectionType::OutputPointType geoPoint;

    cartoPoint[0]=parseResult->GetParameterDouble("--XCarto");
    cartoPoint[1]=parseResult->GetParameterDouble("--YCarto");

    geoPoint = mapProjection->TransformPoint(cartoPoint);

    if (!parseResult->IsOptionPresent("--OTBTesting"))
    {
      std::cout << std::setprecision(10) << "Cartographic Point  (x , y)  : (" << cartoPoint[0] << "," << cartoPoint[1] << ")" << std::endl;
      std::cout << std::setprecision(10) << "Geographic   Point (Lat,Lon) : (" << geoPoint[1] << "," <<  geoPoint[0] << ")" << std::endl;
    }
    else
    {
      std::string outputTestFileName = parseResult->GetParameterString("--OTBTesting",0);

      ofstream outputTestFile;
      outputTestFile.open(outputTestFileName.c_str());

      outputTestFile << std::setprecision(10) << "Cartographic Point  (x , y)  : (" << cartoPoint[0] << "," << cartoPoint[1] << ")" << std::endl;
      outputTestFile << std::setprecision(10) << "Geographic   Point (Lat,Lon) : (" << geoPoint[1] << "," <<  geoPoint[0] << ")" << std::endl;

      outputTestFile.close();
    }

  }
  catch ( itk::ExceptionObject & err )
  {
    std::cout << "Exception itk::ExceptionObject raised !" << std::endl;
    std::cout << err << std::endl;
    return EXIT_FAILURE;
  }
  catch ( std::bad_alloc & err )
  {
    std::cout << "Exception bad_alloc : "<<(char*)err.what()<< std::endl;
    return EXIT_FAILURE;
  }
  catch ( ... )
  {
    std::cout << "Unknown exception raised !" << std::endl;
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;

}//Fin main()

int main(int argc, char* argv[])
{
  try
  {
    ossimInit::instance()->initialize(argc, argv);

    // Parse command line parameters
    typedef otb::CommandLineArgumentParser ParserType;
    ParserType::Pointer parser = ParserType::New();

    parser->SetProgramDescription("Cartographic to geographic coordinates conversion");
    parser->AddOption("--XCarto","X cartographic value of desired point","-x");
    parser->AddOption("--YCarto","Y cartographic value of desired point","-y");
    parser->AddOptionNParams("--MapProjectionType","Type (UTM/LAMBERT/LAMBERT2/LAMBERT93/SINUS/ECKERT4/TRANSMERCATOR/MOLLWEID/SVY21) and parameters of map projection used","-mapProj");

    typedef otb::CommandLineArgumentParseResult ParserResultType;
    ParserResultType::Pointer  parseResult = ParserResultType::New();

    try
    {
      parser->ParseCommandLine(argc,argv,parseResult);
    }
    catch ( itk::ExceptionObject & err )
    {
      std::string descriptionException = err.GetDescription();
      if (descriptionException.find("ParseCommandLine(): Help Parser") != std::string::npos)
      {
        std::cout << "WARNING : output file pixels are converted in 'unsigned char'" << std::endl;
        return EXIT_SUCCESS;
      }
      if (descriptionException.find("ParseCommandLine(): Version Parser") != std::string::npos)
      {
        return EXIT_SUCCESS;
      }
      return EXIT_FAILURE;
    }

    // Code

    std::string typeMap = parseResult->GetParameterString("--MapProjectionType",0);
    int nbParams = parseResult->GetNumberOfParameters("--MapProjectionType");
    nbParams--;

    if ((typeMap == "UTM")&&(nbParams==2))
    {
      int numZone = parseResult->GetParameterUInt("--MapProjectionType",1);
      char hemisphere = parseResult->GetParameterChar("--MapProjectionType",2);

      typedef otb::UtmInverseProjection UtmProjectionType;
      UtmProjectionType::Pointer utmProjection = UtmProjectionType::New();

      utmProjection->SetZone(numZone);
      utmProjection->SetHemisphere(hemisphere);

      return generic_main_carto_geo<UtmProjectionType>(utmProjection, parseResult);
    }
    else
    {
      std::vector<double> parameters ;

      for (int i=1; i<nbParams+1; i++)
      {
        parameters.push_back(parseResult->GetParameterDouble("--MapProjectionType",i));
      }

      if ((typeMap == "LAMBERT")&&(nbParams==4))
      {
        typedef otb::LambertConformalConicInverseProjection LambertProjectionType;
        LambertProjectionType::Pointer lambertProjection = LambertProjectionType::New();

        lambertProjection->SetParameters(parameters[0],parameters[1],parameters[2],parameters[3]);

        return generic_main_carto_geo<LambertProjectionType>(lambertProjection, parseResult);
      }
      else if ((typeMap == "LAMBERT2")&&(nbParams==0))
      {
        typedef otb::Lambert2EtenduInverseProjection Lambert2ProjectionType;
        Lambert2ProjectionType::Pointer lambert2Projection = Lambert2ProjectionType::New();

        return generic_main_carto_geo<Lambert2ProjectionType>(lambert2Projection, parseResult);
      }
      else if ((typeMap == "LAMBERT93")&&(nbParams==0))
      {
        typedef otb::Lambert93InverseProjection Lambert93ProjectionType;
        Lambert93ProjectionType::Pointer lambert93Projection = Lambert93ProjectionType::New();

        return generic_main_carto_geo<Lambert93ProjectionType>(lambert93Projection, parseResult);
      }
      else if ((typeMap == "SINUS")&&(nbParams==2))
      {
        typedef otb::SinusoidalInverseProjection SinusoidalProjectionType;
        SinusoidalProjectionType::Pointer sinusoidalProjection = SinusoidalProjectionType::New();

        sinusoidalProjection->SetParameters(parameters[0],parameters[1]);

        return generic_main_carto_geo<SinusoidalProjectionType>(sinusoidalProjection, parseResult);
      }
      else if ((typeMap == "ECKERT4")&&(nbParams==2))
      {
        typedef otb::Eckert4InverseProjection Eckert4ProjectionType;
        Eckert4ProjectionType::Pointer eckert4Projection = Eckert4ProjectionType::New();

        eckert4Projection->SetParameters(parameters[0],parameters[1]);

        return generic_main_carto_geo<Eckert4ProjectionType>(eckert4Projection, parseResult);
      }
      else if ((typeMap == "TRANSMERCATOR")&&(nbParams==3))
      {
        typedef otb::TransMercatorInverseProjection TransMercatorProjectionType;
        TransMercatorProjectionType::Pointer transMercatorProjection = TransMercatorProjectionType::New();

        transMercatorProjection->SetParameters(parameters[0],parameters[1],parameters[2]);

        return generic_main_carto_geo<TransMercatorProjectionType>(transMercatorProjection, parseResult);
      }
      else if ((typeMap == "MOLLWEID")&&(nbParams==2))
      {
        typedef otb::MollweidInverseProjection MollweidProjectionType;
        MollweidProjectionType::Pointer mollweidProjection = MollweidProjectionType::New();

        mollweidProjection->SetParameters(parameters[0],parameters[1]);

        return generic_main_carto_geo<MollweidProjectionType>(mollweidProjection, parseResult);
      }
      else if ((typeMap == "SVY21")&&(nbParams==0))
      {
        typedef otb::SVY21InverseProjection SVY21ProjectionType;
        SVY21ProjectionType::Pointer svy21Projection = SVY21ProjectionType::New();

        return generic_main_carto_geo<SVY21ProjectionType>(svy21Projection, parseResult);
      }
      else
      {
        itkGenericExceptionMacro(<< "TypeMap not recognized, choose one with (parameters) : UTM(2), LAMBERT(4), LAMBERT2(0), LAMBERT2(93), SINUS(2), ECKERT4(2), TRANSMERCATOR(3), MOLLWEID(2), SVY21(0)");
      }

    }

  }
  catch ( itk::ExceptionObject & err )
  {
    std::cout << "Exception itk::ExceptionObject raised !" << std::endl;
    std::cout << err << std::endl;
    return EXIT_FAILURE;
  }
  catch ( std::bad_alloc & err )
  {
    std::cout << "Exception bad_alloc : "<<(char*)err.what()<< std::endl;
    return EXIT_FAILURE;
  }
  catch ( ... )
  {
    std::cout << "Unknown exception raised !" << std::endl;
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}

<commit_msg>STYLE: cleaning copyright<commit_after>/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.

    This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE,  See the above copyright notices for more information.

=========================================================================*/

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif

#include <iostream>
#include <iomanip>

#include "otbCommandLineArgumentParser.h"
#include "otbImageFileReader.h"
#include "otbStreamingImageFileWriter.h"
#include "otbMapProjections.h"
#include "otbOrthoRectificationFilter.h"
#include "otbImage.h"
#include "otbMacro.h"

#include "itkExceptionObject.h"
#include "itkMacro.h"
#include "itkTransform.h"

#include "init/ossimInit.h"

template<typename TMapProjection>
int generic_main_carto_geo(TMapProjection* mapProjection, otb::CommandLineArgumentParseResult* parseResult)
{

  try
  {

    typedef TMapProjection MapProjectionType;

    typename MapProjectionType::InputPointType cartoPoint;
    typename MapProjectionType::OutputPointType geoPoint;

    cartoPoint[0]=parseResult->GetParameterDouble("--XCarto");
    cartoPoint[1]=parseResult->GetParameterDouble("--YCarto");

    geoPoint = mapProjection->TransformPoint(cartoPoint);

    if (!parseResult->IsOptionPresent("--OTBTesting"))
    {
      std::cout << std::setprecision(10) << "Cartographic Point  (x , y)  : (" << cartoPoint[0] << "," << cartoPoint[1] << ")" << std::endl;
      std::cout << std::setprecision(10) << "Geographic   Point (Lat,Lon) : (" << geoPoint[1] << "," <<  geoPoint[0] << ")" << std::endl;
    }
    else
    {
      std::string outputTestFileName = parseResult->GetParameterString("--OTBTesting",0);

      ofstream outputTestFile;
      outputTestFile.open(outputTestFileName.c_str());

      outputTestFile << std::setprecision(10) << "Cartographic Point  (x , y)  : (" << cartoPoint[0] << "," << cartoPoint[1] << ")" << std::endl;
      outputTestFile << std::setprecision(10) << "Geographic   Point (Lat,Lon) : (" << geoPoint[1] << "," <<  geoPoint[0] << ")" << std::endl;

      outputTestFile.close();
    }

  }
  catch ( itk::ExceptionObject & err )
  {
    std::cout << "Exception itk::ExceptionObject raised !" << std::endl;
    std::cout << err << std::endl;
    return EXIT_FAILURE;
  }
  catch ( std::bad_alloc & err )
  {
    std::cout << "Exception bad_alloc : "<<(char*)err.what()<< std::endl;
    return EXIT_FAILURE;
  }
  catch ( ... )
  {
    std::cout << "Unknown exception raised !" << std::endl;
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;

}//Fin main()

int main(int argc, char* argv[])
{
  try
  {
    ossimInit::instance()->initialize(argc, argv);

    // Parse command line parameters
    typedef otb::CommandLineArgumentParser ParserType;
    ParserType::Pointer parser = ParserType::New();

    parser->SetProgramDescription("Cartographic to geographic coordinates conversion");
    parser->AddOption("--XCarto","X cartographic value of desired point","-x");
    parser->AddOption("--YCarto","Y cartographic value of desired point","-y");
    parser->AddOptionNParams("--MapProjectionType","Type (UTM/LAMBERT/LAMBERT2/LAMBERT93/SINUS/ECKERT4/TRANSMERCATOR/MOLLWEID/SVY21) and parameters of map projection used","-mapProj");

    typedef otb::CommandLineArgumentParseResult ParserResultType;
    ParserResultType::Pointer  parseResult = ParserResultType::New();

    try
    {
      parser->ParseCommandLine(argc,argv,parseResult);
    }
    catch ( itk::ExceptionObject & err )
    {
      std::string descriptionException = err.GetDescription();
      if (descriptionException.find("ParseCommandLine(): Help Parser") != std::string::npos)
      {
        std::cout << "WARNING : output file pixels are converted in 'unsigned char'" << std::endl;
        return EXIT_SUCCESS;
      }
      if (descriptionException.find("ParseCommandLine(): Version Parser") != std::string::npos)
      {
        return EXIT_SUCCESS;
      }
      return EXIT_FAILURE;
    }

    // Code

    std::string typeMap = parseResult->GetParameterString("--MapProjectionType",0);
    int nbParams = parseResult->GetNumberOfParameters("--MapProjectionType");
    nbParams--;

    if ((typeMap == "UTM")&&(nbParams==2))
    {
      int numZone = parseResult->GetParameterUInt("--MapProjectionType",1);
      char hemisphere = parseResult->GetParameterChar("--MapProjectionType",2);

      typedef otb::UtmInverseProjection UtmProjectionType;
      UtmProjectionType::Pointer utmProjection = UtmProjectionType::New();

      utmProjection->SetZone(numZone);
      utmProjection->SetHemisphere(hemisphere);

      return generic_main_carto_geo<UtmProjectionType>(utmProjection, parseResult);
    }
    else
    {
      std::vector<double> parameters ;

      for (int i=1; i<nbParams+1; i++)
      {
        parameters.push_back(parseResult->GetParameterDouble("--MapProjectionType",i));
      }

      if ((typeMap == "LAMBERT")&&(nbParams==4))
      {
        typedef otb::LambertConformalConicInverseProjection LambertProjectionType;
        LambertProjectionType::Pointer lambertProjection = LambertProjectionType::New();

        lambertProjection->SetParameters(parameters[0],parameters[1],parameters[2],parameters[3]);

        return generic_main_carto_geo<LambertProjectionType>(lambertProjection, parseResult);
      }
      else if ((typeMap == "LAMBERT2")&&(nbParams==0))
      {
        typedef otb::Lambert2EtenduInverseProjection Lambert2ProjectionType;
        Lambert2ProjectionType::Pointer lambert2Projection = Lambert2ProjectionType::New();

        return generic_main_carto_geo<Lambert2ProjectionType>(lambert2Projection, parseResult);
      }
      else if ((typeMap == "LAMBERT93")&&(nbParams==0))
      {
        typedef otb::Lambert93InverseProjection Lambert93ProjectionType;
        Lambert93ProjectionType::Pointer lambert93Projection = Lambert93ProjectionType::New();

        return generic_main_carto_geo<Lambert93ProjectionType>(lambert93Projection, parseResult);
      }
      else if ((typeMap == "SINUS")&&(nbParams==2))
      {
        typedef otb::SinusoidalInverseProjection SinusoidalProjectionType;
        SinusoidalProjectionType::Pointer sinusoidalProjection = SinusoidalProjectionType::New();

        sinusoidalProjection->SetParameters(parameters[0],parameters[1]);

        return generic_main_carto_geo<SinusoidalProjectionType>(sinusoidalProjection, parseResult);
      }
      else if ((typeMap == "ECKERT4")&&(nbParams==2))
      {
        typedef otb::Eckert4InverseProjection Eckert4ProjectionType;
        Eckert4ProjectionType::Pointer eckert4Projection = Eckert4ProjectionType::New();

        eckert4Projection->SetParameters(parameters[0],parameters[1]);

        return generic_main_carto_geo<Eckert4ProjectionType>(eckert4Projection, parseResult);
      }
      else if ((typeMap == "TRANSMERCATOR")&&(nbParams==3))
      {
        typedef otb::TransMercatorInverseProjection TransMercatorProjectionType;
        TransMercatorProjectionType::Pointer transMercatorProjection = TransMercatorProjectionType::New();

        transMercatorProjection->SetParameters(parameters[0],parameters[1],parameters[2]);

        return generic_main_carto_geo<TransMercatorProjectionType>(transMercatorProjection, parseResult);
      }
      else if ((typeMap == "MOLLWEID")&&(nbParams==2))
      {
        typedef otb::MollweidInverseProjection MollweidProjectionType;
        MollweidProjectionType::Pointer mollweidProjection = MollweidProjectionType::New();

        mollweidProjection->SetParameters(parameters[0],parameters[1]);

        return generic_main_carto_geo<MollweidProjectionType>(mollweidProjection, parseResult);
      }
      else if ((typeMap == "SVY21")&&(nbParams==0))
      {
        typedef otb::SVY21InverseProjection SVY21ProjectionType;
        SVY21ProjectionType::Pointer svy21Projection = SVY21ProjectionType::New();

        return generic_main_carto_geo<SVY21ProjectionType>(svy21Projection, parseResult);
      }
      else
      {
        itkGenericExceptionMacro(<< "TypeMap not recognized, choose one with (parameters) : UTM(2), LAMBERT(4), LAMBERT2(0), LAMBERT2(93), SINUS(2), ECKERT4(2), TRANSMERCATOR(3), MOLLWEID(2), SVY21(0)");
      }

    }

  }
  catch ( itk::ExceptionObject & err )
  {
    std::cout << "Exception itk::ExceptionObject raised !" << std::endl;
    std::cout << err << std::endl;
    return EXIT_FAILURE;
  }
  catch ( std::bad_alloc & err )
  {
    std::cout << "Exception bad_alloc : "<<(char*)err.what()<< std::endl;
    return EXIT_FAILURE;
  }
  catch ( ... )
  {
    std::cout << "Unknown exception raised !" << std::endl;
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}

<|endoftext|>


<commit_before>#include <stdbool.h>
#include <stdio.h>
#include <glib.h>
#include "ds3.h"
#include "test.h"
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( put_metadata ) {
    ds3_error* error;
    ds3_bulk_object_list* obj_list;
    uint64_t metadata_count;
    ds3_bulk_response* bulk_response;
    ds3_metadata* metadata_result;
    ds3_metadata_entry* metadata_entry;
    const char* file_name[1] = {"resources/beowulf.txt"};
    ds3_client* client = get_client();
    const char* bucket_name = "metadata_test";
    FILE* file;
    ds3_request* request = ds3_init_put_bucket(bucket_name);

    error = ds3_put_bucket(client, request);
    ds3_free_request(request);
    handle_error(error);

    obj_list = ds3_convert_file_list(file_name, 1);

    request = ds3_init_put_bulk(bucket_name, obj_list);
    error = ds3_bulk(client, request, &bulk_response);
    ds3_free_request(request);

    handle_error(error);

    request = ds3_init_put_object_for_job(bucket_name, "resources/beowulf.txt", 0, obj_list->list[0].length, bulk_response->job_id->value);
    file = fopen(obj_list->list[0].name->value, "r");
    ds3_free_bulk_object_list(obj_list);

    ds3_request_set_metadata(request, "name", "value");

    error = ds3_put_object(client, request, file, ds3_read_from_file);
    ds3_free_request(request);
    fclose(file);
    handle_error(error);
    ds3_free_bulk_response(bulk_response);

    request = ds3_init_head_object(bucket_name, "resources/beowulf.txt");

    error = ds3_head_object(client, request, &metadata_result);
    ds3_free_request(request);
    handle_error(error);
    BOOST_REQUIRE(metadata_result != NULL);

    metadata_count = ds3_metadata_size(metadata_result);
    BOOST_CHECK(metadata_count == 1);

    metadata_entry = ds3_metadata_get_entry(metadata_result, "name");
    BOOST_REQUIRE(metadata_entry != NULL);
    BOOST_CHECK(g_strcmp0(metadata_entry->name->value, "name") == 0);

    ds3_free_metadata_entry(metadata_entry);
    ds3_free_metadata(metadata_result);
    clear_bucket(client, bucket_name);
    free_client(client);
}

BOOST_AUTO_TEST_CASE( put_bucket_metadata ) {
    ds3_error* error;
    ds3_bulk_object_list* obj_list;
    uint64_t metadata_count;
    ds3_bulk_response* bulk_response;
    ds3_metadata* metadata_result;
    ds3_metadata_entry* metadata_entry;
    const char* file_name[1] = {"resources/beowulf.txt"};
    ds3_client* client = get_client();
    const char* bucket_name = "metadata_test";
    FILE* file;
    ds3_request* request = ds3_init_put_bucket(bucket_name);

    error = ds3_put_bucket(client, request);
    ds3_free_request(request);
    handle_error(error);

    obj_list = ds3_convert_file_list(file_name, 1);

    request = ds3_init_put_bulk(bucket_name, obj_list);
    error = ds3_bulk(client, request, &bulk_response);
    ds3_free_request(request);

    handle_error(error);

    request = ds3_init_put_object_for_job(bucket_name, "resources/beowulf.txt", 0, obj_list->list[0].length, bulk_response->job_id->value);
    file = fopen(obj_list->list[0].name->value, "r");
    ds3_free_bulk_object_list(obj_list);

    ds3_request_set_metadata(request, "name", "value");

    error = ds3_put_object(client, request, file, ds3_read_from_file);
    ds3_free_request(request);
    fclose(file);
    handle_error(error);
    ds3_free_bulk_response(bulk_response);

    request = ds3_init_head_bucket(bucket_name);

    error = ds3_head_bucket(client, request, &metadata_result);
    ds3_free_request(request);
    handle_error(error);
    BOOST_REQUIRE(metadata_result != NULL);

    metadata_count = ds3_metadata_size(metadata_result);
    BOOST_CHECK(metadata_count == 1);

    metadata_entry = ds3_metadata_get_entry(metadata_result, "name");
    BOOST_REQUIRE(metadata_entry != NULL);
    BOOST_CHECK(g_strcmp0(metadata_entry->name->value, "name") == 0);

    ds3_free_metadata_entry(metadata_entry);
    ds3_free_metadata(metadata_result);
    clear_bucket(client, bucket_name);
    free_client(client);
}

BOOST_AUTO_TEST_CASE( put_multiple_metadata_items ) {
    ds3_error* error;
    ds3_bulk_object_list* obj_list;
    uint64_t metadata_count;
    ds3_bulk_response* bulk_response;
    ds3_metadata* metadata_result;
    ds3_metadata_entry* metadata_entry;
    const char* file_name[1] = {"resources/beowulf.txt"};
    ds3_client* client = get_client_at_loglvl(DS3_DEBUG);
    const char* bucket_name = "multi_metadata_test";
    FILE* file;
    ds3_request* request = ds3_init_put_bucket(bucket_name);

    error = ds3_put_bucket(client, request);
    ds3_free_request(request);
    handle_error(error);

    obj_list = ds3_convert_file_list(file_name, 1);

    request = ds3_init_put_bulk(bucket_name, obj_list);
    error = ds3_bulk(client, request, &bulk_response);
    ds3_free_request(request);
    handle_error(error);

    request = ds3_init_put_object_for_job(bucket_name, "resources/beowulf.txt", 0, obj_list->list[0].length, bulk_response->job_id->value);
    file = fopen(obj_list->list[0].name->value, "r");
    ds3_free_bulk_object_list(obj_list);

    ds3_request_set_metadata(request, "key", "value2");
    ds3_request_set_metadata(request, "name", "value");

    error = ds3_put_object(client, request, file, ds3_read_from_file);
    ds3_free_request(request);
    fclose(file);
    handle_error(error);
    ds3_free_bulk_response(bulk_response);

    request = ds3_init_head_object(bucket_name, "resources/beowulf.txt");

    error = ds3_head_object(client, request, &metadata_result);
    ds3_free_request(request);
    handle_error(error);
    BOOST_REQUIRE(metadata_result != NULL);

    metadata_count = ds3_metadata_size(metadata_result);
    BOOST_CHECK(metadata_count == 2);

    metadata_entry = ds3_metadata_get_entry(metadata_result, "name");
    BOOST_REQUIRE(metadata_entry != NULL);
    BOOST_CHECK(g_strcmp0(metadata_entry->name->value, "name") == 0);
    BOOST_CHECK(g_strcmp0(metadata_entry->values[0]->value, "value") == 0);
    ds3_free_metadata_entry(metadata_entry);

    metadata_entry = ds3_metadata_get_entry(metadata_result, "key");
    BOOST_REQUIRE(metadata_entry != NULL);
    BOOST_CHECK(g_strcmp0(metadata_entry->name->value, "key") == 0);
    BOOST_CHECK(g_strcmp0(metadata_entry->values[0]->value, "value2") == 0);

    ds3_free_metadata_entry(metadata_entry);
    ds3_free_metadata(metadata_result);
    clear_bucket(client, bucket_name);
    free_client(client);
}

static bool contains_key(const ds3_metadata_keys_result* metadata_keys, const char* key) {
    uint64_t i;
    for (i = 0; i < metadata_keys->num_keys; i++) {
        if (g_strcmp0(key, metadata_keys->keys[i]->value) == 0) {
            return TRUE;
        }
    }
    return FALSE;
}

BOOST_AUTO_TEST_CASE( metadata_keys ) {
    ds3_error* error;
    ds3_bulk_object_list* obj_list;
    uint64_t metadata_count;
    ds3_bulk_response* bulk_response;
    ds3_metadata* metadata_result = NULL;
    ds3_metadata_keys_result* metadata_keys = NULL;

    const char* file_name[1] = {"resources/beowulf.txt"};
    ds3_client* client = get_client_at_loglvl(DS3_DEBUG);
    const char* bucket_name = "key_metadata_test";
    FILE* file;
    ds3_request* request = ds3_init_put_bucket(bucket_name);

    error = ds3_put_bucket(client, request);
    ds3_free_request(request);
    handle_error(error);

    obj_list = ds3_convert_file_list(file_name, 1);

    request = ds3_init_put_bulk(bucket_name, obj_list);
    error = ds3_bulk(client, request, &bulk_response);
    ds3_free_request(request);
    handle_error(error);

    request = ds3_init_put_object_for_job(bucket_name, "resources/beowulf.txt", 0, obj_list->list[0].length, bulk_response->job_id->value);
    file = fopen(obj_list->list[0].name->value, "r");
    ds3_free_bulk_object_list(obj_list);

    ds3_request_set_metadata(request, "key", "value2");
    ds3_request_set_metadata(request, "name", "value");

    error = ds3_put_object(client, request, file, ds3_read_from_file);
    ds3_free_request(request);
    fclose(file);
    handle_error(error);
    ds3_free_bulk_response(bulk_response);

    request = ds3_init_head_object(bucket_name, "resources/beowulf.txt");

    error = ds3_head_object(client, request, &metadata_result);
    ds3_free_request(request);
    handle_error(error);
    BOOST_REQUIRE(metadata_result != NULL);

    metadata_count = ds3_metadata_size(metadata_result);
    BOOST_CHECK(metadata_count == 2);

    metadata_keys = ds3_metadata_keys(metadata_result);
    BOOST_REQUIRE(metadata_keys != NULL);

    BOOST_CHECK(metadata_keys->num_keys == 2);
    BOOST_CHECK(contains_key(metadata_keys, "key"));
    BOOST_CHECK(contains_key(metadata_keys, "name"));

    ds3_free_metadata_keys(metadata_keys);
    ds3_free_metadata(metadata_result);
    clear_bucket(client, bucket_name);
    free_client(client);
}

BOOST_AUTO_TEST_CASE( put_metadata_using_get_object_retrieval ) {
    ds3_error* error;
    ds3_bulk_object_list* obj_list;
    uint64_t metadata_count;
    ds3_bulk_response* bulk_response;
    ds3_metadata* metadata_result;
    ds3_metadata_entry* metadata_entry;
    const char* file_name[1] = {"resources/beowulf.txt"};
    ds3_client* client = get_client();
    const char* bucket_name = "get_object_metadata_test";
    FILE* file;
    ds3_request* request = ds3_init_put_bucket(bucket_name);

    error = ds3_put_bucket(client, request);
    ds3_free_request(request);
    handle_error(error);

    obj_list = ds3_convert_file_list(file_name, 1);

    request = ds3_init_put_bulk(bucket_name, obj_list);
    error = ds3_bulk(client, request, &bulk_response);
    ds3_free_request(request);
    handle_error(error);

    request = ds3_init_put_object_for_job(bucket_name, "resources/beowulf.txt", 0, obj_list->list[0].length, bulk_response->job_id->value);
    file = fopen(obj_list->list[0].name->value, "r");

    ds3_request_set_metadata(request, "name", "value");
    error = ds3_put_object(client, request, file, ds3_read_from_file);
    ds3_free_request(request);
    fclose(file);
    handle_error(error);
    ds3_free_bulk_response(bulk_response);

    request = ds3_init_get_bulk(bucket_name, obj_list, NONE);
    error = ds3_bulk(client, request, &bulk_response);
    ds3_free_request(request);
    ds3_free_bulk_object_list(obj_list);
    handle_error(error);

    request = ds3_init_get_object_for_job(bucket_name, "resources/beowulf.txt", 0, bulk_response->job_id->value);
    file = fopen("/dev/null", "w");
    error = ds3_get_object_with_metadata(client, request, file, ds3_write_to_file, &metadata_result);
    ds3_free_request(request);
    ds3_free_bulk_response(bulk_response);
    fclose(file);
    handle_error(error);
    BOOST_REQUIRE(metadata_result != NULL);

    metadata_count = ds3_metadata_size(metadata_result);
    BOOST_CHECK(metadata_count == 1);

    metadata_entry = ds3_metadata_get_entry(metadata_result, "name");
    BOOST_REQUIRE(metadata_entry != NULL);
    BOOST_CHECK(g_strcmp0(metadata_entry->name->value, "name") == 0);

    ds3_free_metadata_entry(metadata_entry);
    ds3_free_metadata(metadata_result);
    clear_bucket(client, bucket_name);
    free_client(client);
}
<commit_msg>wrote a basic unit test for head_bucket<commit_after>#include <stdbool.h>
#include <stdio.h>
#include <glib.h>
#include "ds3.h"
#include "test.h"
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( put_metadata ) {
    ds3_error* error;
    ds3_bulk_object_list* obj_list;
    uint64_t metadata_count;
    ds3_bulk_response* bulk_response;
    ds3_metadata* metadata_result;
    ds3_metadata_entry* metadata_entry;
    const char* file_name[1] = {"resources/beowulf.txt"};
    ds3_client* client = get_client();
    const char* bucket_name = "metadata_test";
    FILE* file;
    ds3_request* request = ds3_init_put_bucket(bucket_name);

    error = ds3_put_bucket(client, request);
    ds3_free_request(request);
    handle_error(error);

    obj_list = ds3_convert_file_list(file_name, 1);

    request = ds3_init_put_bulk(bucket_name, obj_list);
    error = ds3_bulk(client, request, &bulk_response);
    ds3_free_request(request);

    handle_error(error);

    request = ds3_init_put_object_for_job(bucket_name, "resources/beowulf.txt", 0, obj_list->list[0].length, bulk_response->job_id->value);
    file = fopen(obj_list->list[0].name->value, "r");
    ds3_free_bulk_object_list(obj_list);

    ds3_request_set_metadata(request, "name", "value");

    error = ds3_put_object(client, request, file, ds3_read_from_file);
    ds3_free_request(request);
    fclose(file);
    handle_error(error);
    ds3_free_bulk_response(bulk_response);

    request = ds3_init_head_object(bucket_name, "resources/beowulf.txt");

    error = ds3_head_object(client, request, &metadata_result);
    ds3_free_request(request);
    handle_error(error);
    BOOST_REQUIRE(metadata_result != NULL);

    metadata_count = ds3_metadata_size(metadata_result);
    BOOST_CHECK(metadata_count == 1);

    metadata_entry = ds3_metadata_get_entry(metadata_result, "name");
    BOOST_REQUIRE(metadata_entry != NULL);
    BOOST_CHECK(g_strcmp0(metadata_entry->name->value, "name") == 0);

    ds3_free_metadata_entry(metadata_entry);
    ds3_free_metadata(metadata_result);
    clear_bucket(client, bucket_name);
    free_client(client);
}

BOOST_AUTO_TEST_CASE( head_bucket ) {
    ds3_error* error;
    ds3_client* client = get_client();
    const char* bucket_name = "metadata_test";
    ds3_request* request = ds3_init_put_bucket(bucket_name);

    error = ds3_put_bucket(client, request);
    ds3_free_request(request);
    handle_error(error);

    request = ds3_init_head_bucket(bucket_name);

    error = ds3_head_bucket(client, request);
    ds3_free_request(request);
    handle_error(error);
    //BOOST_REQUIRE(metadata_result != NULL);
    clear_bucket(client, bucket_name);
    free_client(client);
}

BOOST_AUTO_TEST_CASE( put_multiple_metadata_items ) {
    ds3_error* error;
    ds3_bulk_object_list* obj_list;
    uint64_t metadata_count;
    ds3_bulk_response* bulk_response;
    ds3_metadata* metadata_result;
    ds3_metadata_entry* metadata_entry;
    const char* file_name[1] = {"resources/beowulf.txt"};
    ds3_client* client = get_client_at_loglvl(DS3_DEBUG);
    const char* bucket_name = "multi_metadata_test";
    FILE* file;
    ds3_request* request = ds3_init_put_bucket(bucket_name);

    error = ds3_put_bucket(client, request);
    ds3_free_request(request);
    handle_error(error);

    obj_list = ds3_convert_file_list(file_name, 1);

    request = ds3_init_put_bulk(bucket_name, obj_list);
    error = ds3_bulk(client, request, &bulk_response);
    ds3_free_request(request);
    handle_error(error);

    request = ds3_init_put_object_for_job(bucket_name, "resources/beowulf.txt", 0, obj_list->list[0].length, bulk_response->job_id->value);
    file = fopen(obj_list->list[0].name->value, "r");
    ds3_free_bulk_object_list(obj_list);

    ds3_request_set_metadata(request, "key", "value2");
    ds3_request_set_metadata(request, "name", "value");

    error = ds3_put_object(client, request, file, ds3_read_from_file);
    ds3_free_request(request);
    fclose(file);
    handle_error(error);
    ds3_free_bulk_response(bulk_response);

    request = ds3_init_head_object(bucket_name, "resources/beowulf.txt");

    error = ds3_head_object(client, request, &metadata_result);
    ds3_free_request(request);
    handle_error(error);
    BOOST_REQUIRE(metadata_result != NULL);

    metadata_count = ds3_metadata_size(metadata_result);
    BOOST_CHECK(metadata_count == 2);

    metadata_entry = ds3_metadata_get_entry(metadata_result, "name");
    BOOST_REQUIRE(metadata_entry != NULL);
    BOOST_CHECK(g_strcmp0(metadata_entry->name->value, "name") == 0);
    BOOST_CHECK(g_strcmp0(metadata_entry->values[0]->value, "value") == 0);
    ds3_free_metadata_entry(metadata_entry);

    metadata_entry = ds3_metadata_get_entry(metadata_result, "key");
    BOOST_REQUIRE(metadata_entry != NULL);
    BOOST_CHECK(g_strcmp0(metadata_entry->name->value, "key") == 0);
    BOOST_CHECK(g_strcmp0(metadata_entry->values[0]->value, "value2") == 0);

    ds3_free_metadata_entry(metadata_entry);
    ds3_free_metadata(metadata_result);
    clear_bucket(client, bucket_name);
    free_client(client);
}

static bool contains_key(const ds3_metadata_keys_result* metadata_keys, const char* key) {
    uint64_t i;
    for (i = 0; i < metadata_keys->num_keys; i++) {
        if (g_strcmp0(key, metadata_keys->keys[i]->value) == 0) {
            return TRUE;
        }
    }
    return FALSE;
}

BOOST_AUTO_TEST_CASE( metadata_keys ) {
    ds3_error* error;
    ds3_bulk_object_list* obj_list;
    uint64_t metadata_count;
    ds3_bulk_response* bulk_response;
    ds3_metadata* metadata_result = NULL;
    ds3_metadata_keys_result* metadata_keys = NULL;

    const char* file_name[1] = {"resources/beowulf.txt"};
    ds3_client* client = get_client_at_loglvl(DS3_DEBUG);
    const char* bucket_name = "key_metadata_test";
    FILE* file;
    ds3_request* request = ds3_init_put_bucket(bucket_name);

    error = ds3_put_bucket(client, request);
    ds3_free_request(request);
    handle_error(error);

    obj_list = ds3_convert_file_list(file_name, 1);

    request = ds3_init_put_bulk(bucket_name, obj_list);
    error = ds3_bulk(client, request, &bulk_response);
    ds3_free_request(request);
    handle_error(error);

    request = ds3_init_put_object_for_job(bucket_name, "resources/beowulf.txt", 0, obj_list->list[0].length, bulk_response->job_id->value);
    file = fopen(obj_list->list[0].name->value, "r");
    ds3_free_bulk_object_list(obj_list);

    ds3_request_set_metadata(request, "key", "value2");
    ds3_request_set_metadata(request, "name", "value");

    error = ds3_put_object(client, request, file, ds3_read_from_file);
    ds3_free_request(request);
    fclose(file);
    handle_error(error);
    ds3_free_bulk_response(bulk_response);

    request = ds3_init_head_object(bucket_name, "resources/beowulf.txt");

    error = ds3_head_object(client, request, &metadata_result);
    ds3_free_request(request);
    handle_error(error);
    BOOST_REQUIRE(metadata_result != NULL);

    metadata_count = ds3_metadata_size(metadata_result);
    BOOST_CHECK(metadata_count == 2);

    metadata_keys = ds3_metadata_keys(metadata_result);
    BOOST_REQUIRE(metadata_keys != NULL);

    BOOST_CHECK(metadata_keys->num_keys == 2);
    BOOST_CHECK(contains_key(metadata_keys, "key"));
    BOOST_CHECK(contains_key(metadata_keys, "name"));

    ds3_free_metadata_keys(metadata_keys);
    ds3_free_metadata(metadata_result);
    clear_bucket(client, bucket_name);
    free_client(client);
}

BOOST_AUTO_TEST_CASE( put_metadata_using_get_object_retrieval ) {
    ds3_error* error;
    ds3_bulk_object_list* obj_list;
    uint64_t metadata_count;
    ds3_bulk_response* bulk_response;
    ds3_metadata* metadata_result;
    ds3_metadata_entry* metadata_entry;
    const char* file_name[1] = {"resources/beowulf.txt"};
    ds3_client* client = get_client();
    const char* bucket_name = "get_object_metadata_test";
    FILE* file;
    ds3_request* request = ds3_init_put_bucket(bucket_name);

    error = ds3_put_bucket(client, request);
    ds3_free_request(request);
    handle_error(error);

    obj_list = ds3_convert_file_list(file_name, 1);

    request = ds3_init_put_bulk(bucket_name, obj_list);
    error = ds3_bulk(client, request, &bulk_response);
    ds3_free_request(request);
    handle_error(error);

    request = ds3_init_put_object_for_job(bucket_name, "resources/beowulf.txt", 0, obj_list->list[0].length, bulk_response->job_id->value);
    file = fopen(obj_list->list[0].name->value, "r");

    ds3_request_set_metadata(request, "name", "value");
    error = ds3_put_object(client, request, file, ds3_read_from_file);
    ds3_free_request(request);
    fclose(file);
    handle_error(error);
    ds3_free_bulk_response(bulk_response);

    request = ds3_init_get_bulk(bucket_name, obj_list, NONE);
    error = ds3_bulk(client, request, &bulk_response);
    ds3_free_request(request);
    ds3_free_bulk_object_list(obj_list);
    handle_error(error);

    request = ds3_init_get_object_for_job(bucket_name, "resources/beowulf.txt", 0, bulk_response->job_id->value);
    file = fopen("/dev/null", "w");
    error = ds3_get_object_with_metadata(client, request, file, ds3_write_to_file, &metadata_result);
    ds3_free_request(request);
    ds3_free_bulk_response(bulk_response);
    fclose(file);
    handle_error(error);
    BOOST_REQUIRE(metadata_result != NULL);

    metadata_count = ds3_metadata_size(metadata_result);
    BOOST_CHECK(metadata_count == 1);

    metadata_entry = ds3_metadata_get_entry(metadata_result, "name");
    BOOST_REQUIRE(metadata_entry != NULL);
    BOOST_CHECK(g_strcmp0(metadata_entry->name->value, "name") == 0);

    ds3_free_metadata_entry(metadata_entry);
    ds3_free_metadata(metadata_result);
    clear_bucket(client, bucket_name);
    free_client(client);
}
<|endoftext|>


<commit_before>/*
** Copyright 2011 Merethis
**
** This file is part of Centreon Broker.
**
** Centreon Broker 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.
**
** Centreon Broker 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 Centreon Broker. If not, see
** <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include <QDir>
#include <QTime>
#include <stdlib.h>
#include <time.h>
#include "com/centreon/broker/logging/file.hh"
#include "com/centreon/broker/logging/logging.hh"
#include "com/centreon/broker/logging/manager.hh"

using namespace com::centreon::broker;

#define MESSAGE "0123456789abcdefghijklmnopqrstuvwxyz"
#define MESSAGE_COUNT 1000000
#define TEMP_FILE1 "centreon_broker_unit_test1"
#define TEMP_FILE2 "centreon_broker_unit_test2"
#define TEMP_FILE3 "centreon_broker_unit_test3"

/**
 *  Check logging performance (informational).
 *
 *  @return 0.
 */
int main() {
  // Initialization.
  logging::manager::load();

  // Build filenames.
  QString filename1(QDir::tempPath());
  filename1.append("/" TEMP_FILE1);
  QString filename2(QDir::tempPath());
  filename2.append("/" TEMP_FILE2);
  QString filename3(QDir::tempPath());
  filename3.append("/" TEMP_FILE3);

  // Remove old files.
  QFile::remove(filename1);
  QFile::remove(filename2);
  QFile::remove(filename3);

  // Initialize file backend.
  logging::file::with_thread_id(false);
  logging::file::with_timestamp(false);
  logging::file backend1(filename1);
  logging::file backend2(filename2);
  logging::file backend3(filename3);

  // Add backends to logging list.
  logging::manager::instance().log_on(
    backend1,
    logging::debug_type | logging::error_type | logging::info_type,
    logging::high);
  logging::manager::instance().log_on(
    backend2,
    logging::config_type | logging::error_type,
    logging::medium);
  logging::manager::instance().log_on(
    backend3,
    logging::error_type,
    logging::low);

  // Initialize random number generator.
  srand(time(NULL));

  // Timer.
  QTime timer;
  timer.start();

  // Log messages.
  for (unsigned int i = 0; i < MESSAGE_COUNT; ++i) {
    // Logging level.
    logging::level l;
    switch (rand() % 3) {
     case 0:
      l = logging::low;
      break ;
     case 1:
      l = logging::medium;
      break ;
     default:
      l = logging::high;
      break ;
    }

    // Logging type.
    switch (rand() % 4) {
     case 0:
      logging::config(l) << MESSAGE << i;
      break ;
     case 1:
      logging::debug(l) << MESSAGE << i;
      break ;
     case 2:
      logging::error(l) << MESSAGE << i;
      break ;
     default:
      logging::info(l) << MESSAGE << i;
      break ;
    }
  }

  // Performance output.
  int elapsed(timer.elapsed());
  std::cout << MESSAGE_COUNT << " messages in " << elapsed << " ms - "
    << static_cast<double>(MESSAGE_COUNT) / elapsed << " msg/ms"
    << std::endl;

  // Remove temp files.
  QFile::remove(filename1);
  QFile::remove(filename2);
  QFile::remove(filename3);

  return (0);
}
<commit_msg>Adjusting a unit test which took some time to execute.<commit_after>/*
** Copyright 2011 Merethis
**
** This file is part of Centreon Broker.
**
** Centreon Broker 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.
**
** Centreon Broker 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 Centreon Broker. If not, see
** <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include <QDir>
#include <QTime>
#include <stdlib.h>
#include <time.h>
#include "com/centreon/broker/logging/file.hh"
#include "com/centreon/broker/logging/logging.hh"
#include "com/centreon/broker/logging/manager.hh"

using namespace com::centreon::broker;

#define MESSAGE "0123456789abcdefghijklmnopqrstuvwxyz"
#define MESSAGE_COUNT 100000
#define TEMP_FILE1 "centreon_broker_unit_test1"
#define TEMP_FILE2 "centreon_broker_unit_test2"
#define TEMP_FILE3 "centreon_broker_unit_test3"

/**
 *  Check logging performance (informational).
 *
 *  @return 0.
 */
int main() {
  // Initialization.
  logging::manager::load();

  // Build filenames.
  QString filename1(QDir::tempPath());
  filename1.append("/" TEMP_FILE1);
  QString filename2(QDir::tempPath());
  filename2.append("/" TEMP_FILE2);
  QString filename3(QDir::tempPath());
  filename3.append("/" TEMP_FILE3);

  // Remove old files.
  QFile::remove(filename1);
  QFile::remove(filename2);
  QFile::remove(filename3);

  // Initialize file backend.
  logging::file::with_thread_id(false);
  logging::file::with_timestamp(false);
  logging::file backend1(filename1);
  logging::file backend2(filename2);
  logging::file backend3(filename3);

  // Add backends to logging list.
  logging::manager::instance().log_on(
    backend1,
    logging::debug_type | logging::error_type | logging::info_type,
    logging::high);
  logging::manager::instance().log_on(
    backend2,
    logging::config_type | logging::error_type,
    logging::medium);
  logging::manager::instance().log_on(
    backend3,
    logging::error_type,
    logging::low);

  // Initialize random number generator.
  srand(time(NULL));

  // Timer.
  QTime timer;
  timer.start();

  // Log messages.
  for (unsigned int i = 0; i < MESSAGE_COUNT; ++i) {
    // Logging level.
    logging::level l;
    switch (rand() % 3) {
     case 0:
      l = logging::low;
      break ;
     case 1:
      l = logging::medium;
      break ;
     default:
      l = logging::high;
      break ;
    }

    // Logging type.
    switch (rand() % 4) {
     case 0:
      logging::config(l) << MESSAGE << i;
      break ;
     case 1:
      logging::debug(l) << MESSAGE << i;
      break ;
     case 2:
      logging::error(l) << MESSAGE << i;
      break ;
     default:
      logging::info(l) << MESSAGE << i;
      break ;
    }
  }

  // Performance output.
  int elapsed(timer.elapsed());
  std::cout << MESSAGE_COUNT << " messages in " << elapsed << " ms - "
    << static_cast<double>(MESSAGE_COUNT) / elapsed << " msg/ms"
    << std::endl;

  // Remove temp files.
  QFile::remove(filename1);
  QFile::remove(filename2);
  QFile::remove(filename3);

  return (0);
}
<|endoftext|>


<commit_before>#ifndef JARNGREIPR_IO_PDB_READER
#define JARNGREIPR_IO_PDB_READER
#include <jarngreipr/io/PDBAtom.hpp>
#include <jarngreipr/io/PDBResidue.hpp>
#include <jarngreipr/io/PDBChain.hpp>
#include <jarngreipr/util/string.hpp>
#include <stdexcept>
#include <fstream>
#include <cassert>

namespace mjolnir
{

namespace pdb
{
struct different_line : public std::invalid_argument {
    explicit different_line(const std::string& w) : std::invalid_argument(w) {}
    explicit different_line(const char* w)        : std::invalid_argument(w) {}
    ~different_line() override = default;
};

struct invalid_format : public std::runtime_error {
    explicit invalid_format(const std::string& w) : std::runtime_error(w) {}
    explicit invalid_format(const char* w)        : std::runtime_error(w) {}
    ~invalid_format() override = default;
};

struct no_more_atom : public std::runtime_error {
    explicit no_more_atom(const std::string& w) : std::runtime_error(w) {}
    explicit no_more_atom(const char* w)        : std::runtime_error(w) {}
    ~no_more_atom() override = default;
};
}// pdb

template<typename coordT>
PDBAtom<coordT> read_pdb_atom_line(const std::string& line)
{
    try
    {
        const std::string prefix = remove_whitespace(line.substr(0, 6));
        if(prefix != "ATOM" && prefix != "HETATM")
        {
            throw pdb::different_line(
                    "mjolnir::read_pdb_atom: neither ATOM nor HETATM line");
        }
        PDBAtom<coordT> atom;
        atom.prefix       = prefix;
        atom.atom_id      = std::stoi(line.substr(6, 5));
        atom.atom_name    = remove_whitespace(line.substr(12, 4));
        atom.altloc       = line[16];
        atom.residue_name = remove_whitespace(line.substr(17, 3));
        atom.chain_id     = line[21];
        atom.residue_id   = std::stoi(line.substr(22, 4));
        atom.icode        = line[26];
        typename PDBAtom<coordT>::real_type x, y, z;
        x = std::stod(line.substr(30, 8));
        y = std::stod(line.substr(38, 8));
        z = std::stod(line.substr(46, 8));
        atom.position = coordT(x, y, z);

        try{atom.occupancy = std::stod(line.substr(54, 6));}
        catch(std::exception& excpt){atom.occupancy = 0e0;}

        try{atom.temperature_factor = std::stod(line.substr(60, 6));}
        catch(std::exception& excpt){atom.temperature_factor = 0e0;}

        try{atom.element = line.substr(76,2);}
        catch(std::exception& excpt){atom.element = "";}

        try{atom.charge = line.substr(78,2);}
        catch(std::exception& excpt){atom.charge = "";}

        return atom;
    }
    catch(pdb::different_line const&)
    {
        throw;
    }
    catch(...)
    {
        throw pdb::invalid_format(
                "mjolnir::read_pdb_atom_line: reading line"_str + line);
    }
}

template<typename coordT>
std::vector<PDBAtom<coordT>> read_pdb_atoms(std::istream& istrm)
{
    std::vector<PDBAtom<coordT>> atoms;
    while(!istrm.eof())
    {
        std::string line;
        std::getline(istrm, line);
        try
        {
            atoms.push_back(read_pdb_atom_line<coordT>(line));
        }
        catch(pdb::different_line const&)
        {
            // found non-ATOM line. ignore and continue.
            continue;
        }
        catch(pdb::invalid_format const& ivf)
        {
            throw std::runtime_error("mjolnir::io::read_pdb_atoms: "_str +
                "invalid line: \n"_str + line);
        }
    }
    return atoms;
}

template<typename coordT>
std::vector<PDBChain<coordT>> read_pdb_chains(std::istream& istrm)
{
    const std::vector<PDBAtom<coordT>> atoms = read_pdb_atoms<coordT>(istrm);

    std::vector<PDBResidue<coordT>> residues;
    /* split-atoms-into-residues */{
        PDBResidue<coordT> tmp;
        for(const auto& atom : atoms)
        {
            if(tmp.is_in_same_residue(atom))
            {
                tmp.push_back(atom);
            }
            else // make new residue
            {
                assert(false == tmp.empty());

                residues.push_back(tmp);
                tmp.clear();
                tmp.push_back(atom);
            }
        }
        if(false == tmp.empty())
        {
            residues.push_back(std::move(tmp));
        }
    }

    std::vector<PDBChain<coordT>> chains;
    /* split-residues-into-chians */{
        PDBChain<coordT> tmp;
        for(auto&& residue : residues)
        {
            if(tmp.is_in_same_chain(residue))
            {
                tmp.push_back(std::move(residue));
            }
            else // make new chain
            {
                assert(false == tmp.empty());

                chains.push_back(tmp);
                tmp.clear();
                tmp.push_back(residue);
            }
        }
        if(false == tmp.empty())
        {
            chains.push_back(std::move(tmp));
        }
    }
    return chains;
}

template<typename coordT>
std::vector<PDBChain<coordT>> read_pdb_chains(const std::string& fname)
{
    std::ifstream ifs(fname);
    if(!ifs.good())
    {
        throw std::runtime_error(
                "mjolnir::io::read_pdb_chains: file open error: " + fname);
    }
    return read_pdb_chains<coordT>(ifs);
}

template<typename coordT>
std::vector<PDBAtom<coordT>> read_pdb_atoms(const std::string& fname)
{
    std::ifstream ifs(fname);
    if(!ifs.good())
    {
        throw std::runtime_error(
                "mjolnir::io::read_pdb_atoms: file open error: " + fname);
    }
    return read_pdb_atoms<coordT>(ifs);
}

// lazy PDB reader
template<typename coordT>
class PDBReader
{
  public:

    typedef coordT coordinate_type;
    typedef PDBAtom<coordinate_type>    atom_type;
    typedef PDBResidue<coordinate_type> residue_type;
    typedef PDBChain<coordinate_type>   chain_type;
  public:

    explicit PDBReader(const std::string& fname)
        : filename_(fname), ifs_(fname)
    {}
    ~PDBReader() = default;

    atom_type    read_next_atom();
    residue_type read_next_residue();
    chain_type   read_next_chain();

    std::string const& filename() const noexcept {return this->filename_;}

    bool is_eof()  {ifs_.peek(); return ifs_.eof();}
    bool is_good() {return ifs.good();}

  private:

    const std::string filename_;
    std::ifstream     ifs_;
};

template<typename coordT>
PDBAtom<coordT> PDBReader<coordT>::read_next_atom()
{
    while(!this->ifs_.eof())
    {
        std::string line;
        std::getline(this->ifs_, line);
        if(line.size() < 6)
        {
            // cannot detect ATOM or HERATM signeture. ignore the line.
            continue;
        }
        try
        {
            return read_pdb_atom_line<coordT>(line);
        }
        catch(pdb::different_line const&)
        {
            continue;
        }
        catch(pdb::invalid_format const&)
        {
            throw pdb::invalid_format(
                    "mjolnir::io::PDBReader::read_next_atom: "_str +
                    "invalid line found in the file: \n"_str + line);
        }
    }
    throw pdb::no_more_atom("mjolnir::PDBReader::read_next_atom: "_str +
            "no more atoms in file: "_str + this->filename_);
}

template<typename coordT>
PDBResidue<coordT> PDBReader<coordT>::read_next_residue()
{
    residue_type res;
    while(!this->ifs_.eof())
    {
        const auto pos = this->ifs_.tellg();
        atom_type atm;
        try
        {
            atm = this->read_next_atom();
        }
        catch(const pdb::no_more_atom& nma)
        {
            break;
        }

        if(res.is_in_same_residue(atm))
        {
            res.push_back(atm);
        }
        else // different residue
        {
            this->ifs_.seekg(pos); // restore the position
            break;
        }
    }
    if(res.empty())
    {
        throw pdb::no_more_atom("mjolnir::PDBReader::read_next_residue: "_str +
                "no more atoms in file: "_str + this->filename_);
    }
    return res;
}

template<typename coordT>
PDBChain<coordT> PDBReader<coordT>::read_next_chain()
{
    chain_type chn;
    while(!this->ifs_.eof())
    {
        const auto pos = this->ifs_.tellg();
        residue_type res;
        try
        {
            res = this->read_next_residue();
        }
        catch(const pdb::no_more_atom& nma)
        {
            break;
        }

        if(chn.is_in_same_chain(res))
        {
            chn.push_back(res);
        }
        else // different residue
        {
            this->ifs_.seekg(pos); // restore the position
            break;
        }
    }
    if(chn.empty())
    {
        throw pdb::no_more_atom("mjolnir::PDBReader::read_next_chain: "_str +
                "no more atoms in file: "_str + this->filename_);
    }
    return chn;
}

} // mjolnir
#endif /* JARNGREIPR_IO_PDB_READER */
<commit_msg>fix typo & restrict to read ATOM<commit_after>#ifndef JARNGREIPR_IO_PDB_READER
#define JARNGREIPR_IO_PDB_READER
#include <jarngreipr/io/PDBAtom.hpp>
#include <jarngreipr/io/PDBResidue.hpp>
#include <jarngreipr/io/PDBChain.hpp>
#include <jarngreipr/util/string.hpp>
#include <stdexcept>
#include <fstream>
#include <cassert>

namespace mjolnir
{

namespace pdb
{
struct different_line : public std::invalid_argument {
    explicit different_line(const std::string& w) : std::invalid_argument(w) {}
    explicit different_line(const char* w)        : std::invalid_argument(w) {}
    ~different_line() override = default;
};

struct invalid_format : public std::runtime_error {
    explicit invalid_format(const std::string& w) : std::runtime_error(w) {}
    explicit invalid_format(const char* w)        : std::runtime_error(w) {}
    ~invalid_format() override = default;
};

struct no_more_atom : public std::runtime_error {
    explicit no_more_atom(const std::string& w) : std::runtime_error(w) {}
    explicit no_more_atom(const char* w)        : std::runtime_error(w) {}
    ~no_more_atom() override = default;
};
}// pdb

template<typename coordT>
PDBAtom<coordT> read_pdb_atom_line(const std::string& line)
{
    try
    {
        const std::string prefix = remove_whitespace(line.substr(0, 6));
        if(prefix != "ATOM")
        {
            throw pdb::different_line(
                    "mjolnir::read_pdb_atom: neither ATOM nor HETATM line");
        }
        PDBAtom<coordT> atom;
        atom.prefix       = prefix;
        atom.atom_id      = std::stoi(line.substr(6, 5));
        atom.atom_name    = remove_whitespace(line.substr(12, 4));
        atom.altloc       = line[16];
        atom.residue_name = remove_whitespace(line.substr(17, 3));
        atom.chain_id     = line[21];
        atom.residue_id   = std::stoi(line.substr(22, 4));
        atom.icode        = line[26];
        typename PDBAtom<coordT>::real_type x, y, z;
        x = std::stod(line.substr(30, 8));
        y = std::stod(line.substr(38, 8));
        z = std::stod(line.substr(46, 8));
        atom.position = coordT(x, y, z);

        try{atom.occupancy = std::stod(line.substr(54, 6));}
        catch(std::exception& excpt){atom.occupancy = 0e0;}

        try{atom.temperature_factor = std::stod(line.substr(60, 6));}
        catch(std::exception& excpt){atom.temperature_factor = 0e0;}

        try{atom.element = line.substr(76,2);}
        catch(std::exception& excpt){atom.element = "";}

        try{atom.charge = line.substr(78,2);}
        catch(std::exception& excpt){atom.charge = "";}

        return atom;
    }
    catch(pdb::different_line const&)
    {
        throw;
    }
    catch(...)
    {
        throw pdb::invalid_format(
                "mjolnir::read_pdb_atom_line: reading line"_str + line);
    }
}

template<typename coordT>
std::vector<PDBAtom<coordT>> read_pdb_atoms(std::istream& istrm)
{
    std::vector<PDBAtom<coordT>> atoms;
    while(!istrm.eof())
    {
        std::string line;
        std::getline(istrm, line);
        try
        {
            atoms.push_back(read_pdb_atom_line<coordT>(line));
        }
        catch(pdb::different_line const&)
        {
            // found non-ATOM line. ignore and continue.
            continue;
        }
        catch(pdb::invalid_format const& ivf)
        {
            throw std::runtime_error("mjolnir::io::read_pdb_atoms: "_str +
                "invalid line: \n"_str + line);
        }
    }
    return atoms;
}

template<typename coordT>
std::vector<PDBChain<coordT>> read_pdb_chains(std::istream& istrm)
{
    const std::vector<PDBAtom<coordT>> atoms = read_pdb_atoms<coordT>(istrm);

    std::vector<PDBResidue<coordT>> residues;
    /* split-atoms-into-residues */{
        PDBResidue<coordT> tmp;
        for(const auto& atom : atoms)
        {
            if(tmp.is_in_same_residue(atom))
            {
                tmp.push_back(atom);
            }
            else // make new residue
            {
                assert(false == tmp.empty());

                residues.push_back(tmp);
                tmp.clear();
                tmp.push_back(atom);
            }
        }
        if(false == tmp.empty())
        {
            residues.push_back(std::move(tmp));
        }
    }

    std::vector<PDBChain<coordT>> chains;
    /* split-residues-into-chians */{
        PDBChain<coordT> tmp;
        for(auto&& residue : residues)
        {
            if(tmp.is_in_same_chain(residue))
            {
                tmp.push_back(std::move(residue));
            }
            else // make new chain
            {
                assert(false == tmp.empty());

                chains.push_back(tmp);
                tmp.clear();
                tmp.push_back(residue);
            }
        }
        if(false == tmp.empty())
        {
            chains.push_back(std::move(tmp));
        }
    }
    return chains;
}

template<typename coordT>
std::vector<PDBChain<coordT>> read_pdb_chains(const std::string& fname)
{
    std::ifstream ifs(fname);
    if(!ifs.good())
    {
        throw std::runtime_error(
                "mjolnir::io::read_pdb_chains: file open error: " + fname);
    }
    return read_pdb_chains<coordT>(ifs);
}

template<typename coordT>
std::vector<PDBAtom<coordT>> read_pdb_atoms(const std::string& fname)
{
    std::ifstream ifs(fname);
    if(!ifs.good())
    {
        throw std::runtime_error(
                "mjolnir::io::read_pdb_atoms: file open error: " + fname);
    }
    return read_pdb_atoms<coordT>(ifs);
}

// lazy PDB reader
template<typename coordT>
class PDBReader
{
  public:

    typedef coordT coordinate_type;
    typedef PDBAtom<coordinate_type>    atom_type;
    typedef PDBResidue<coordinate_type> residue_type;
    typedef PDBChain<coordinate_type>   chain_type;
  public:

    explicit PDBReader(const std::string& fname)
        : filename_(fname), ifs_(fname)
    {}
    ~PDBReader() = default;

    atom_type    read_next_atom();
    residue_type read_next_residue();
    chain_type   read_next_chain();

    std::string const& filename() const noexcept {return this->filename_;}

    bool is_eof()  {ifs_.peek(); return ifs_.eof();}
    bool is_good() {return ifs_.good();}

  private:

    const std::string filename_;
    std::ifstream     ifs_;
};

template<typename coordT>
PDBAtom<coordT> PDBReader<coordT>::read_next_atom()
{
    while(!this->ifs_.eof())
    {
        std::string line;
        std::getline(this->ifs_, line);
        if(line.size() < 6)
        {
            // cannot detect ATOM or HERATM signeture. ignore the line.
            continue;
        }
        try
        {
            return read_pdb_atom_line<coordT>(line);
        }
        catch(pdb::different_line const&)
        {
            continue;
        }
        catch(pdb::invalid_format const&)
        {
            throw pdb::invalid_format(
                    "mjolnir::io::PDBReader::read_next_atom: "_str +
                    "invalid line found in the file: \n"_str + line);
        }
    }
    throw pdb::no_more_atom("mjolnir::PDBReader::read_next_atom: "_str +
            "no more atoms in file: "_str + this->filename_);
}

template<typename coordT>
PDBResidue<coordT> PDBReader<coordT>::read_next_residue()
{
    residue_type res;
    while(!this->ifs_.eof())
    {
        const auto pos = this->ifs_.tellg();
        atom_type atm;
        try
        {
            atm = this->read_next_atom();
        }
        catch(const pdb::no_more_atom& nma)
        {
            break;
        }

        if(res.is_in_same_residue(atm))
        {
            res.push_back(atm);
        }
        else // different residue
        {
            this->ifs_.seekg(pos); // restore the position
            break;
        }
    }
    if(res.empty())
    {
        throw pdb::no_more_atom("mjolnir::PDBReader::read_next_residue: "_str +
                "no more atoms in file: "_str + this->filename_);
    }
    return res;
}

template<typename coordT>
PDBChain<coordT> PDBReader<coordT>::read_next_chain()
{
    chain_type chn;
    while(!this->ifs_.eof())
    {
        const auto pos = this->ifs_.tellg();
        residue_type res;
        try
        {
            res = this->read_next_residue();
        }
        catch(const pdb::no_more_atom& nma)
        {
            break;
        }

        if(chn.is_in_same_chain(res))
        {
            chn.push_back(res);
        }
        else // different residue
        {
            this->ifs_.seekg(pos); // restore the position
            break;
        }
    }
    if(chn.empty())
    {
        throw pdb::no_more_atom("mjolnir::PDBReader::read_next_chain: "_str +
                "no more atoms in file: "_str + this->filename_);
    }
    return chn;
}

} // mjolnir
#endif /* JARNGREIPR_IO_PDB_READER */
<|endoftext|>