<commit_before>// Copyright (c) 2014, German Neuroinformatics Node (G-Node)
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted under the terms of the BSD License. See
// LICENSE source in the root of the Project.

#include <boost/python.hpp>
#include <boost/optional.hpp>

#include <nix.hpp>

#include <accessors.hpp>
#include <transmorgify.hpp>

//we want only the newest and freshest API
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

#include <numpy/arrayobject.h>
#include <numpy/ndarrayobject.h>

#include <PyEntity.hpp>

#include <stdexcept>

using namespace nix;
using namespace boost::python;

namespace nixpy {

// Label

void setLabel(DataArray& da, const boost::optional<std::string>& label) {
    if (label)
        da.label(*label);
    else
        da.label(boost::none);
}

// Unit

void setUnit(DataArray& da, const boost::optional<std::string> &unit) {
    if (unit)
        da.unit(*unit);
    else
        da.unit(boost::none);
}

// Expansion origin

void setExpansionOrigin(DataArray& da, const boost::optional<double>& eo) {
    if (eo)
        da.expansionOrigin(*eo);
    else
        da.expansionOrigin(boost::none);
}

// Polynom coefficients

void setPolynomCoefficients(DataArray& da, const std::vector<double>& pc) {
    if (!pc.empty())
        da.polynomCoefficients(pc);
    else
        da.polynomCoefficients(boost::none);
}

// Data

static nix::DataType py_dtype_to_nix_dtype(const PyArray_Descr *dtype)
{
    if (dtype == nullptr) {
        return nix::DataType::Nothing;
    }

    if (dtype->byteorder != '=' && dtype->byteorder != '|') {
        //TODO: Handle case where specified byteorder *is*
        //the native byteorder (via BOOST_BIG_ENDIAN macros)
        return nix::DataType::Nothing;
    }

    switch (dtype->kind) {

    case 'u':
        switch (dtype->elsize) {
        case 1: return nix::DataType::UInt8;
        case 2: return nix::DataType::UInt16;
        case 4: return nix::DataType::UInt32;
        case 8: return nix::DataType::UInt64;
        }
        break;

    case 'i':
        switch (dtype->elsize) {
        case 1: return nix::DataType::Int8;
        case 2: return nix::DataType::Int16;
        case 4: return nix::DataType::Int32;
        case 8: return nix::DataType::Int64;
        }
        break;

    case 'f':
        switch (dtype->elsize) {
        case 4: return nix::DataType::Float;
        case 8: return nix::DataType::Double;
        }
        break;

    default:
        break;
    }
    return nix::DataType::Nothing;
}

static nix::DataType array_desc_as_dtype(PyArrayObject *array) {
    nix::DataType nix_dtype = py_dtype_to_nix_dtype(PyArray_DESCR(array));

    if (nix_dtype == nix::DataType::Nothing) {
        throw std::invalid_argument("Unsupported dtype for data");
    }

    return nix_dtype;
}

static PyArrayObject *make_array(PyObject *data, int requirements) {
    if (! PyArray_Check(data)) {
        throw std::invalid_argument("Data not a NumPy array");
    }

    PyArrayObject *array = reinterpret_cast<PyArrayObject *>(data);

     if (! PyArray_CHKFLAGS(array, requirements)) {
        throw std::invalid_argument("data must be c-contiguous and aligned");
    }

     return array;
}

static NDSize array_shape_as_ndsize(PyArrayObject *array) {
    int array_rank = PyArray_NDIM(array);
    npy_intp *array_shape = PyArray_SHAPE(array);

    nix::NDSize data_shape(array_rank);
    for (int i = 0; i < array_rank; i++) {
        data_shape[i] = array_shape[i];
    }

    return data_shape;
}

static void readData(DataArray& da, PyObject *data) {

    PyArrayObject *array = make_array(data, NPY_ARRAY_CARRAY);

    nix::DataType nix_dtype = array_desc_as_dtype(array);
    nix::NDSize data_shape = array_shape_as_ndsize(array);
    nix::NDSize offset(data_shape.size(), 0);

    da.getData(nix_dtype, PyArray_DATA(array), data_shape, offset);
}

static void writeData(DataArray& da, PyObject *data) {

    PyArrayObject *array = make_array(data, NPY_ARRAY_CARRAY_RO);

    nix::DataType nix_dtype = array_desc_as_dtype(array);
    nix::NDSize data_shape = array_shape_as_ndsize(array);
    nix::NDSize offset(data_shape.size(), 0);

    da.setData(nix_dtype, PyArray_DATA(array), data_shape, offset);
}

static void createData(DataArray& da, const NDSize &shape, PyObject *dtype_obj, PyObject *data) {
    PyArray_Descr* py_dtype = nullptr;

    if (! PyArray_DescrConverter(dtype_obj, &py_dtype)) {
        throw std::invalid_argument("Invalid dtype");
    }

    nix::DataType nix_dtype = py_dtype_to_nix_dtype(py_dtype);
    if (nix_dtype == nix::DataType::Nothing) {
        throw std::invalid_argument("Unsupported dtype");
    }

    da.createData(nix_dtype, shape);

    if (data != Py_None) {
        writeData(da, data);
    }

    Py_DECREF(py_dtype);
}

// Dimensions

PyObject* getDimension(const DataArray& da, size_t index) {
    Dimension dim = da.getDimension(index);
    SetDimension set;
    RangeDimension range;
    SampledDimension sample;
    DimensionType type = dim.dimensionType();

    switch(type) {
        case DimensionType::Set:
            set = dim;
            return incref(object(set).ptr());
        case DimensionType::Range:
            range = dim;
            return incref(object(range).ptr());
        case DimensionType::Sample:
            sample = dim;
            return incref(object(sample).ptr());
        default:
            Py_RETURN_NONE;
    }
}

void PyDataArray::do_export() {

    // For numpy to work
    import_array();

    PyEntityWithSources<base::IDataArray>::do_export("DataArray");
    class_<DataArray, bases<base::EntityWithSources<base::IDataArray>>>("DataArray")
        .add_property("label",
                      OPT_GETTER(std::string, DataArray, label),
                      setLabel,
                      doc::data_array_label)
        .add_property("unit",
                      OPT_GETTER(std::string, DataArray, unit),
                      setUnit,
                      doc::data_array_unit)
        .add_property("expansion_origin",
                      OPT_GETTER(double, DataArray, expansionOrigin),
                      setExpansionOrigin,
                      doc::data_array_expansion_origin)
        .add_property("polynom_coefficients",
                      GETTER(std::vector<double>, DataArray, polynomCoefficients),
                      setPolynomCoefficients,
                      doc::data_array_polynom_coefficients)
        .add_property("data_extent",
                      GETTER(NDSize, DataArray, dataExtent),
                      SETTER(NDSize&, DataArray, dataExtent),
                      doc::data_array_data_extent)
        // Data
        .add_property("data_type", &DataArray::dataType,
                      doc::data_array_data_type)
        .def("has_data", &DataArray::hasData,
                      doc::data_array_has_data)

        .def("_create_data", createData)
        .def("_write_data", writeData)
        .def("_read_data", readData)

        // Dimensions
        .def("create_set_dimension", &DataArray::createSetDimension,
             doc::data_array_create_set_dimension)
        .def("create_sampled_dimension", &DataArray::createSampledDimension,
             doc::data_array_create_sampled_dimension)
        .def("create_reange_dimension", &DataArray::createRangeDimension,
             doc::data_array_create_range_dimension)
        .def("append_set_dimension", &DataArray::appendSetDimension,
             doc::data_array_append_set_dimension)
        .def("append_sampled_dimension", &DataArray::appendSampledDimension,
             doc::data_array_append_sampled_dimension)
        .def("append_range_dimension", &DataArray::appendRangeDimension,
             doc::data_array_append_range_dimension)
        .def("_dimension_count", &DataArray::dimensionCount)
        .def("_delete_dimension_by_pos", &DataArray::deleteDimension)
        .def("_get_dimension_by_pos", getDimension)
        // Other
        .def("__str__", &toStr<DataArray>)
        .def("__repr__", &toStr<DataArray>)
        ;

    to_python_converter<std::vector<DataArray>, vector_transmogrify<DataArray>>();
    vector_transmogrify<DataArray>::register_from_python();

    to_python_converter<boost::optional<DataArray>, option_transmogrify<DataArray>>();
    option_transmogrify<DataArray>::register_from_python();
}

}
<commit_msg>PyDataArray: Add DataType -> numpy.dtype str conversion fn<commit_after>// Copyright (c) 2014, German Neuroinformatics Node (G-Node)
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted under the terms of the BSD License. See
// LICENSE source in the root of the Project.

#include <boost/python.hpp>
#include <boost/optional.hpp>

#include <nix.hpp>

#include <accessors.hpp>
#include <transmorgify.hpp>

//we want only the newest and freshest API
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

#include <numpy/arrayobject.h>
#include <numpy/ndarrayobject.h>

#include <PyEntity.hpp>

#include <stdexcept>

using namespace nix;
using namespace boost::python;

namespace nixpy {

// Label

void setLabel(DataArray& da, const boost::optional<std::string>& label) {
    if (label)
        da.label(*label);
    else
        da.label(boost::none);
}

// Unit

void setUnit(DataArray& da, const boost::optional<std::string> &unit) {
    if (unit)
        da.unit(*unit);
    else
        da.unit(boost::none);
}

// Expansion origin

void setExpansionOrigin(DataArray& da, const boost::optional<double>& eo) {
    if (eo)
        da.expansionOrigin(*eo);
    else
        da.expansionOrigin(boost::none);
}

// Polynom coefficients

void setPolynomCoefficients(DataArray& da, const std::vector<double>& pc) {
    if (!pc.empty())
        da.polynomCoefficients(pc);
    else
        da.polynomCoefficients(boost::none);
}

// Data

static nix::DataType py_dtype_to_nix_dtype(const PyArray_Descr *dtype)
{
    if (dtype == nullptr) {
        return nix::DataType::Nothing;
    }

    if (dtype->byteorder != '=' && dtype->byteorder != '|') {
        //TODO: Handle case where specified byteorder *is*
        //the native byteorder (via BOOST_BIG_ENDIAN macros)
        return nix::DataType::Nothing;
    }

    switch (dtype->kind) {

    case 'u':
        switch (dtype->elsize) {
        case 1: return nix::DataType::UInt8;
        case 2: return nix::DataType::UInt16;
        case 4: return nix::DataType::UInt32;
        case 8: return nix::DataType::UInt64;
        }
        break;

    case 'i':
        switch (dtype->elsize) {
        case 1: return nix::DataType::Int8;
        case 2: return nix::DataType::Int16;
        case 4: return nix::DataType::Int32;
        case 8: return nix::DataType::Int64;
        }
        break;

    case 'f':
        switch (dtype->elsize) {
        case 4: return nix::DataType::Float;
        case 8: return nix::DataType::Double;
        }
        break;

    default:
        break;
    }
    return nix::DataType::Nothing;
}

static std::string nix_dtype_to_py_dtype_str(nix::DataType nix_dtype)
{
    switch (nix_dtype) {
    case nix::DataType::UInt8:  return "<u1";
    case nix::DataType::UInt16: return "<u2";
    case nix::DataType::UInt32: return "<u4";
    case nix::DataType::UInt64: return "<u8";
    case nix::DataType::Int8:   return "<i1";
    case nix::DataType::Int16:  return "<i2";
    case nix::DataType::Int32:  return "<i4";
    case nix::DataType::Int64:  return "<i8";
    case nix::DataType::Float:  return "<f4";
    case nix::DataType::Double: return "<f8";
    default:                    return "";
    }
}

static nix::DataType array_desc_as_dtype(PyArrayObject *array) {
    nix::DataType nix_dtype = py_dtype_to_nix_dtype(PyArray_DESCR(array));

    if (nix_dtype == nix::DataType::Nothing) {
        throw std::invalid_argument("Unsupported dtype for data");
    }

    return nix_dtype;
}

static PyArrayObject *make_array(PyObject *data, int requirements) {
    if (! PyArray_Check(data)) {
        throw std::invalid_argument("Data not a NumPy array");
    }

    PyArrayObject *array = reinterpret_cast<PyArrayObject *>(data);

     if (! PyArray_CHKFLAGS(array, requirements)) {
        throw std::invalid_argument("data must be c-contiguous and aligned");
    }

     return array;
}

static NDSize array_shape_as_ndsize(PyArrayObject *array) {
    int array_rank = PyArray_NDIM(array);
    npy_intp *array_shape = PyArray_SHAPE(array);

    nix::NDSize data_shape(array_rank);
    for (int i = 0; i < array_rank; i++) {
        data_shape[i] = array_shape[i];
    }

    return data_shape;
}

static void readData(DataArray& da, PyObject *data) {

    PyArrayObject *array = make_array(data, NPY_ARRAY_CARRAY);

    nix::DataType nix_dtype = array_desc_as_dtype(array);
    nix::NDSize data_shape = array_shape_as_ndsize(array);
    nix::NDSize offset(data_shape.size(), 0);

    da.getData(nix_dtype, PyArray_DATA(array), data_shape, offset);
}

static void writeData(DataArray& da, PyObject *data) {

    PyArrayObject *array = make_array(data, NPY_ARRAY_CARRAY_RO);

    nix::DataType nix_dtype = array_desc_as_dtype(array);
    nix::NDSize data_shape = array_shape_as_ndsize(array);
    nix::NDSize offset(data_shape.size(), 0);

    da.setData(nix_dtype, PyArray_DATA(array), data_shape, offset);
}

static void createData(DataArray& da, const NDSize &shape, PyObject *dtype_obj, PyObject *data) {
    PyArray_Descr* py_dtype = nullptr;

    if (! PyArray_DescrConverter(dtype_obj, &py_dtype)) {
        throw std::invalid_argument("Invalid dtype");
    }

    nix::DataType nix_dtype = py_dtype_to_nix_dtype(py_dtype);
    if (nix_dtype == nix::DataType::Nothing) {
        throw std::invalid_argument("Unsupported dtype");
    }

    da.createData(nix_dtype, shape);

    if (data != Py_None) {
        writeData(da, data);
    }

    Py_DECREF(py_dtype);
}

// Dimensions

PyObject* getDimension(const DataArray& da, size_t index) {
    Dimension dim = da.getDimension(index);
    SetDimension set;
    RangeDimension range;
    SampledDimension sample;
    DimensionType type = dim.dimensionType();

    switch(type) {
        case DimensionType::Set:
            set = dim;
            return incref(object(set).ptr());
        case DimensionType::Range:
            range = dim;
            return incref(object(range).ptr());
        case DimensionType::Sample:
            sample = dim;
            return incref(object(sample).ptr());
        default:
            Py_RETURN_NONE;
    }
}

void PyDataArray::do_export() {

    // For numpy to work
    import_array();

    PyEntityWithSources<base::IDataArray>::do_export("DataArray");
    class_<DataArray, bases<base::EntityWithSources<base::IDataArray>>>("DataArray")
        .add_property("label",
                      OPT_GETTER(std::string, DataArray, label),
                      setLabel,
                      doc::data_array_label)
        .add_property("unit",
                      OPT_GETTER(std::string, DataArray, unit),
                      setUnit,
                      doc::data_array_unit)
        .add_property("expansion_origin",
                      OPT_GETTER(double, DataArray, expansionOrigin),
                      setExpansionOrigin,
                      doc::data_array_expansion_origin)
        .add_property("polynom_coefficients",
                      GETTER(std::vector<double>, DataArray, polynomCoefficients),
                      setPolynomCoefficients,
                      doc::data_array_polynom_coefficients)
        .add_property("data_extent",
                      GETTER(NDSize, DataArray, dataExtent),
                      SETTER(NDSize&, DataArray, dataExtent),
                      doc::data_array_data_extent)
        // Data
        .add_property("data_type", &DataArray::dataType,
                      doc::data_array_data_type)
        .def("has_data", &DataArray::hasData,
                      doc::data_array_has_data)

        .def("_create_data", createData)
        .def("_write_data", writeData)
        .def("_read_data", readData)

        // Dimensions
        .def("create_set_dimension", &DataArray::createSetDimension,
             doc::data_array_create_set_dimension)
        .def("create_sampled_dimension", &DataArray::createSampledDimension,
             doc::data_array_create_sampled_dimension)
        .def("create_reange_dimension", &DataArray::createRangeDimension,
             doc::data_array_create_range_dimension)
        .def("append_set_dimension", &DataArray::appendSetDimension,
             doc::data_array_append_set_dimension)
        .def("append_sampled_dimension", &DataArray::appendSampledDimension,
             doc::data_array_append_sampled_dimension)
        .def("append_range_dimension", &DataArray::appendRangeDimension,
             doc::data_array_append_range_dimension)
        .def("_dimension_count", &DataArray::dimensionCount)
        .def("_delete_dimension_by_pos", &DataArray::deleteDimension)
        .def("_get_dimension_by_pos", getDimension)
        // Other
        .def("__str__", &toStr<DataArray>)
        .def("__repr__", &toStr<DataArray>)
        ;

    to_python_converter<std::vector<DataArray>, vector_transmogrify<DataArray>>();
    vector_transmogrify<DataArray>::register_from_python();

    to_python_converter<boost::optional<DataArray>, option_transmogrify<DataArray>>();
    option_transmogrify<DataArray>::register_from_python();
}

}
<|endoftext|>


<commit_before>#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <set>

#include "shingle.h"
#include "word_idx.h"
#include "parser.h"

#ifdef _OPENMP
#include <omp.h>
#endif

using namespace std;

const int WINDOW_SIZE = 300;

string output_filename_for_thread() {
    stringstream str;
    str << "resemblance";
#ifdef _OPENMP
    str << "." << omp_get_thread_num();
#endif
    str << ".out";
    return str.str();
}

int main(int argc, char *argv[]) {

    const float min_resemblance = argc==1 ? 0 : atof(argv[1]);

    //run_tests();

    // lookup from shingles to idx
    WordIdx word_idx;

    // list of shingles
    vector<Shingle*> shingles;

    // parse stdin
    Parser parser;
    parser.parse(shingles, word_idx);

    // build bit representation
    for (vector<Shingle*>::iterator iter=shingles.begin(); iter!=shingles.end(); ++iter)
        (*iter)->build_bit_representation(word_idx.max_idx());

    // convert from list back to array
    const int number_lines = shingles.size();
    Shingle **shingles_array = new Shingle*[number_lines];
    for (int i=0;i<number_lines;i++)
        shingles_array[i] = shingles[i];

    #pragma omp parallel num_threads(4)
    {

        ofstream file(output_filename_for_thread().c_str());

        for(int i=0;i<number_lines;i++) {

            int window_start = i+1;
            int window_finish = window_start + WINDOW_SIZE;
            if (window_finish > number_lines) window_finish = number_lines;

            // compare each pair output high resemblances
            #pragma omp for
            for (int j=window_start; j<window_finish; j++) {
                float resemblance = shingles_array[i]->resemblance_to(*shingles_array[j]);
                if (resemblance > min_resemblance)
                    file << i << " " << j << " " << resemblance << endl;
            }
        }

        file.close();
    }
}
<commit_msg>move omp for pragma to outer loop and increase schedule size<commit_after>#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <set>

#include "shingle.h"
#include "word_idx.h"
#include "parser.h"

#ifdef _OPENMP
#include <omp.h>
#endif

using namespace std;

const int WINDOW_SIZE = 300;

string output_filename_for_thread() {
    stringstream str;
    str << "resemblance";
#ifdef _OPENMP
    str << "." << omp_get_thread_num();
#endif
    str << ".out";
    return str.str();
}

int main(int argc, char *argv[]) {

    const float min_resemblance = argc==1 ? 0 : atof(argv[1]);

    //run_tests();

    // lookup from shingles to idx
    WordIdx word_idx;

    // list of shingles
    vector<Shingle*> shingles;

    // parse stdin
    Parser parser;
    parser.parse(shingles, word_idx);

    // build bit representation
    for (vector<Shingle*>::iterator iter=shingles.begin(); iter!=shingles.end(); ++iter)
        (*iter)->build_bit_representation(word_idx.max_idx());

    // convert from list back to array
    const int number_lines = shingles.size();
    Shingle **shingles_array = new Shingle*[number_lines];
    for (int i=0;i<number_lines;i++)
        shingles_array[i] = shingles[i];

    #pragma omp parallel num_threads(4)
    {

        ofstream file(output_filename_for_thread().c_str());

        #pragma omp for schedule(dynamic, 1000)
        for(int i=0;i<number_lines;i++) {

            int window_start = i+1;
            int window_finish = window_start + WINDOW_SIZE;
            if (window_finish > number_lines) window_finish = number_lines;

            // compare each pair output high resemblances
            for (int j=window_start; j<window_finish; j++) {
                float resemblance = shingles_array[i]->resemblance_to(*shingles_array[j]);
                if (resemblance > min_resemblance)
                    file << i << " " << j << " " << resemblance << endl;
            }
        }

        file.close();
    }
}
<|endoftext|>


<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
*/
/*
 * encoder.hpp
 *

#ifndef KAFKA_ENCODER_HPP_
#define KAFKA_ENCODER_HPP_

#include <boost/foreach.hpp>
#include "encoder_helper.hpp"

namespace kafkaconnect {

template <typename List>
void encode(std::ostream& stream, const std::string& topic, const uint32_t partition, const List& messages)
{
	// Pre-calculate size of message set
	uint32_t messageset_size = 0;
	BOOST_FOREACH(const std::string& message, messages)
	{
		messageset_size += message_format_header_size + message.length();
	}

	// Packet format is ... packet size (4 bytes)
	encoder_helper::raw(stream, htonl(2 + 2 + topic.size() + 4 + 4 + messageset_size));

	// ... magic number (2 bytes)
	encoder_helper::raw(stream, htons(kafka_format_version));

	// ... topic string size (2 bytes) & topic string
	encoder_helper::raw(stream, htons(topic.size()));
	stream << topic;

	// ... partition (4 bytes)
	encoder_helper::raw(stream, htonl(partition));

	// ... message set size (4 bytes) and message set
	encoder_helper::raw(stream, htonl(messageset_size));
	BOOST_FOREACH(const std::string& message, messages)
	{
		encoder_helper::message(stream, message);
	}
}

}

#endif /* KAFKA_ENCODER_HPP_ */
<commit_msg>Niek Sanders - KAFKA-284 fixed compilation issue for cpp client<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
*/
/*
 * encoder.hpp
 */

#ifndef KAFKA_ENCODER_HPP_
#define KAFKA_ENCODER_HPP_

#include <boost/foreach.hpp>
#include "encoder_helper.hpp"

namespace kafkaconnect {

template <typename List>
void encode(std::ostream& stream, const std::string& topic, const uint32_t partition, const List& messages)
{
	// Pre-calculate size of message set
	uint32_t messageset_size = 0;
	BOOST_FOREACH(const std::string& message, messages)
	{
		messageset_size += message_format_header_size + message.length();
	}

	// Packet format is ... packet size (4 bytes)
	encoder_helper::raw(stream, htonl(2 + 2 + topic.size() + 4 + 4 + messageset_size));

	// ... magic number (2 bytes)
	encoder_helper::raw(stream, htons(kafka_format_version));

	// ... topic string size (2 bytes) & topic string
	encoder_helper::raw(stream, htons(topic.size()));
	stream << topic;

	// ... partition (4 bytes)
	encoder_helper::raw(stream, htonl(partition));

	// ... message set size (4 bytes) and message set
	encoder_helper::raw(stream, htonl(messageset_size));
	BOOST_FOREACH(const std::string& message, messages)
	{
		encoder_helper::message(stream, message);
	}
}

}

#endif /* KAFKA_ENCODER_HPP_ */
<|endoftext|>


<commit_before>//
// Weasel Program, written in "gone-full-retard" C++14!
//
//  https://en.wikipedia.org/wiki/Weasel_program
//
// The Weasel program is a thought experiment and a very rough simulation of
// evolution, by starting with a random string of characters and gradually
// "evolving" them into the text "methinks it is like a weasel".
//
// The program can be easily modified to generate text that satisfies other
// criteria, e.g. being a palindrome, thus not choosing one particular output.
//
// The starting text is random and is mutated randomly (mutation) and it is
// then scored for suitability (natural selection), resulting in a non-random
// output (evolution).
//

#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <locale>
#include <string>
#include <random>
#include <stdexcept>
#include <string>
#include <vector>

namespace
{

///
/// @brief Global target string.
/// @todo Doesn't need to be in unnamed namespace: const's have static linkage.
///
const std::string methinks("methinks it is like a weasel");

///
/// @brief Program modes.
/// @note Uncomment the one you want used, comment all the others.
/// @todo These should be strongly-typed `enum`s used together with a
///  "static if" mechanism, such as `if constexpr` in C++17.
///
#define MODE_WEASEL
//#define MODE_PALINDROME

///
/// @brief Gives the suitability score of a candidate string.
/// @details A perfect score equals `0` (zero); the higher the worse.
/// @todo Use `if constexpr` instead of `#ifdef` once C++17 becomes available.
/// @note This function was designed to be general such that it can be modified
///  easily to score things other than resemblance to the "weasel" text, while
///  retaining the interface and usage: for example, it could score how good
///  a palindrome the `candidate` string is (as already implemented).
/// @param [in] candidate           Candidate string.
/// @returns Suitability score.
/// @retval 0                       Perfect score.
///
std::size_t score(const std::string &candidate)
try
{
#if defined MODE_WEASEL
    if (candidate.length() != methinks.length())
        return std::numeric_limits<std::size_t>::max();

    std::size_t r(methinks.length());

    for (std::size_t i(0); i < methinks.length(); ++i)
        if (candidate.at(i) == methinks.at(i))
            --r;

    return r;
#elif defined MODE_PALINDROME
    auto            ib  (candidate.cbegin());
    const auto      ie  (candidate.cend());
    auto            rib (candidate.crbegin());
    std::size_t     r   (candidate.length());

    for (; ib != ie; ++ib, ++rib)
        if (*ib == *rib)
            --r;

    return r;
#endif
}
catch (const std::out_of_range &e)
{
    std::cerr << "`std::out_of_range` exception in `" << __func__ << "`: ";
    std::cerr << e.what() << std::endl;
    throw;
}
catch (...)
{
    std::cerr << "unknown exception in `" << __func__ << '`' << std::endl;
    throw;
}

} // unnamed namespace

///
/// @brief Enters the program.
/// @returns Whether or not the operation was successful.
/// @retval EXIT_SUCCESS            Operation success.
/// @retval EXIT_FAILURE            Operation failure.
///
int main()
try
{
    // Number of Candidate strings in a Generation
    constexpr std::size_t num_cagen(100);

    std::size_t calen(0); // Candidate string Length

#if defined MODE_WEASEL
    calen = methinks.length();
#elif defined MODE_PALINDROME
    calen = 50;
#endif

    //
    // create a candidate string
    //

    // Pseudo-Random Number Generator
    std::mt19937 prng(
        std::chrono::system_clock::now().time_since_epoch().count());

    // Random Character
    std::uniform_int_distribution<char> rand_char(
        std::numeric_limits<char>::min(),
        std::numeric_limits<char>::max());

    // Random chance to Mutate (0 == no, 1 == yes)
    std::discrete_distribution<unsigned long int> rand_mutate({
        95.0,       // 0
         5.0});     // 1

    // Generate random Printable Character
    std::function<char (void)> gen_print_char =
        [&prng, &rand_char]() -> char
        {
            char c;

            do
            {
                c = rand_char(prng);
            }
            while (!std::isprint(c, std::locale::classic()));

            return c;
        };

    std::string candidate(calen, char(0)); // Candidate string

    std::generate(candidate.begin(), candidate.end(), gen_print_char);

    // the current Candidates Generation
    std::vector<std::string> cagen(num_cagen, candidate);

    // Value passed to `std::setw()` to prettify printing
    constexpr std::size_t val_setw(10);

    std::cout << std::setw(val_setw) << "FIRST: " << candidate << '\n';

    std::size_t kgen(1); // Generation counter

    while (score(candidate) != 0)
    {
        for (std::string &ca: cagen)
            for (char &c: ca)
                if (rand_mutate(prng) != 0)
                    c = gen_print_char();

        for (const std::string &ca: cagen)
            if (score(ca) < score(candidate))
            {
                candidate = ca;
                std::cout << std::setw(val_setw) << std::to_string(kgen) + ": ";
                std::cout << candidate << '\n';
            }

        std::fill(cagen.begin(), cagen.end(), candidate);
        ++kgen;
    }

    std::cout << std::setw(val_setw) << "LAST: " << candidate << std::endl;
    return EXIT_SUCCESS;
}
catch (const std::out_of_range &e)
{
    std::cerr << "`std::out_of_range` exception in `" << __func__ << "`: ";
    std::cerr << e.what() << std::endl;
    return EXIT_FAILURE;
}
catch (...)
{
    std::cerr << "unknown exception in `" << __func__ << '`' << std::endl;
    return EXIT_FAILURE;
}
<commit_msg>Update weasel.cpp<commit_after>//
// Weasel Program, written in "gone-full-retard" C++14!
//
//  https://en.wikipedia.org/wiki/Weasel_program
//
// The Weasel program is a thought experiment and a very rough simulation of
// evolution, by starting with a random string of characters and gradually
// "evolving" them into the text "methinks it is like a weasel".
//
// The program can be easily modified to generate text that satisfies other
// criteria, e.g. being a palindrome, thus not choosing one particular output.
//
// The starting text is random and is mutated randomly (mutation) and it is
// then scored for suitability (natural selection), resulting in a non-random
// output (evolution).
//

#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <locale>
#include <random>
#include <stdexcept>
#include <string>
#include <vector>

namespace
{

///
/// @brief Global target string.
/// @todo Doesn't need to be in unnamed namespace: const's have static linkage.
///
const std::string methinks("methinks it is like a weasel");

///
/// @brief Program modes.
/// @note Uncomment the one you want used, comment all the others.
/// @todo These should be strongly-typed `enum`s used together with a
///  "static if" mechanism, such as `if constexpr` in C++17.
///
#define MODE_WEASEL
//#define MODE_PALINDROME

///
/// @brief Gives the suitability score of a candidate string.
/// @details A perfect score equals `0` (zero); the higher the worse.
/// @todo Use `if constexpr` instead of `#ifdef` once C++17 becomes available.
/// @note This function was designed to be general such that it can be modified
///  easily to score things other than resemblance to the "weasel" text, while
///  retaining the interface and usage: for example, it could score how good
///  a palindrome the `candidate` string is (as already implemented).
/// @param [in] candidate           Candidate string.
/// @returns Suitability score.
/// @retval 0                       Perfect score.
///
std::size_t score(const std::string &candidate)
try
{
#if defined MODE_WEASEL
    if (candidate.length() != methinks.length())
        return std::numeric_limits<std::size_t>::max();

    std::size_t r(methinks.length());

    for (std::size_t i(0); i < methinks.length(); ++i)
        if (candidate.at(i) == methinks.at(i))
            --r;

    return r;
#elif defined MODE_PALINDROME
    auto            ib  (candidate.cbegin());
    const auto      ie  (candidate.cend());
    auto            rib (candidate.crbegin());
    std::size_t     r   (candidate.length());

    for (; ib != ie; ++ib, ++rib)
        if (*ib == *rib)
            --r;

    return r;
#endif
}
catch (const std::out_of_range &e)
{
    std::cerr << "`std::out_of_range` exception in `" << __func__ << "`: ";
    std::cerr << e.what() << std::endl;
    throw;
}
catch (...)
{
    std::cerr << "unknown exception in `" << __func__ << '`' << std::endl;
    throw;
}

} // unnamed namespace

///
/// @brief Enters the program.
/// @returns Whether or not the operation was successful.
/// @retval EXIT_SUCCESS            Operation success.
/// @retval EXIT_FAILURE            Operation failure.
///
int main()
try
{
    // Number of Candidate strings in a Generation
    constexpr std::size_t num_cagen(100);

    std::size_t calen(0); // Candidate string Length

#if defined MODE_WEASEL
    calen = methinks.length();
#elif defined MODE_PALINDROME
    calen = 50;
#endif

    //
    // create a candidate string
    //

    // Pseudo-Random Number Generator
    std::mt19937 prng(
        std::chrono::system_clock::now().time_since_epoch().count());

    // Random Character
    std::uniform_int_distribution<char> rand_char(
        std::numeric_limits<char>::min(),
        std::numeric_limits<char>::max());

    // Random chance to Mutate (0 == no, 1 == yes)
    std::discrete_distribution<unsigned long int> rand_mutate({
        95.0,       // 0
         5.0});     // 1

    // Generate random Printable Character
    std::function<char (void)> gen_print_char =
        [&prng, &rand_char]() -> char
        {
            char c;

            do
            {
                c = rand_char(prng);
            }
            while (!std::isprint(c, std::locale::classic()));

            return c;
        };

    std::string candidate(calen, char(0)); // Candidate string

    std::generate(candidate.begin(), candidate.end(), gen_print_char);

    // the current Candidates Generation
    std::vector<std::string> cagen(num_cagen, candidate);

    // Value passed to `std::setw()` to prettify printing
    constexpr std::size_t val_setw(10);

    std::cout << std::setw(val_setw) << "FIRST: " << candidate << '\n';

    std::size_t kgen(1); // Generation counter

    while (score(candidate) != 0)
    {
        for (std::string &ca: cagen)
            for (char &c: ca)
                if (rand_mutate(prng) != 0)
                    c = gen_print_char();

        for (const std::string &ca: cagen)
            if (score(ca) < score(candidate))
            {
                candidate = ca;
                std::cout << std::setw(val_setw) << std::to_string(kgen) + ": ";
                std::cout << candidate << '\n';
            }

        std::fill(cagen.begin(), cagen.end(), candidate);
        ++kgen;
    }

    std::cout << std::setw(val_setw) << "LAST: " << candidate << std::endl;
    return EXIT_SUCCESS;
}
catch (const std::out_of_range &e)
{
    std::cerr << "`std::out_of_range` exception in `" << __func__ << "`: ";
    std::cerr << e.what() << std::endl;
    return EXIT_FAILURE;
}
catch (...)
{
    std::cerr << "unknown exception in `" << __func__ << '`' << std::endl;
    return EXIT_FAILURE;
}
<|endoftext|>


<commit_before>#include "SRE/Texture.hpp"

#include "SRE/impl/GL.hpp"
#include <stdio.h>
#include <SDL_surface.h>

#include <SDL_image.h>
#include <iostream>
#include <vector>
#include <fstream>
#include <memory>

namespace {
	static std::vector<char> readAllBytes(char const* filename)
	{
		using namespace std;
		ifstream ifs(filename, std::ios::binary | std::ios::ate);
		ifstream::pos_type pos = ifs.tellg();
		if (pos<0) {
			std::cerr << "Cannot read " << filename << std::endl;
			return std::vector<char>();
		}
		std::vector<char>  result((size_t)pos);

		ifs.seekg(0, ios::beg);
		ifs.read(&result[0], pos);

		return result;
	}
}

namespace SRE {
	Texture* Texture::whiteTexture = nullptr;
	Texture* Texture::sphereTexture = nullptr;

	Texture::Texture(const char *data, int width, int height, uint32_t format, bool generateMipmaps)
		: width{ width }, height{ height }, generateMipmap{ generateMipmaps } {
		glGenTextures(1, &textureId);

		GLenum target = GL_TEXTURE_2D;
		GLint mipmapLevel = 0;
		GLint internalFormat = GL_RGBA;
		GLint border = 0;
		GLenum type = GL_UNSIGNED_BYTE;
		glBindTexture(target, textureId);
		glTexImage2D(target, mipmapLevel, internalFormat, width, height, border, format, type, data);
		updateTextureSampler();
		if (generateMipmaps) {
			glGenerateMipmap(GL_TEXTURE_2D);
		}
	}

	GLenum getFormat(SDL_Surface *image) {
		SDL_PixelFormat *format = image->format;
		auto pixelFormat = format->format;
		bool isBGR = format->Rshift == 16;

#ifdef EMSCRIPTEN
        GLenum RGB = GL_RGB;
        GLenum RGBA = GL_RGBA;
#else
        GLenum RGB = isBGR ? GL_BGR : GL_RGB;
        GLenum RGBA = isBGR ? GL_BGRA : GL_RGBA;
#endif
		const bool alpha = SDL_ISPIXELFORMAT_ALPHA(pixelFormat);
		if (alpha) {
			return RGBA;
		}
		else {
			if (format->BytesPerPixel == 4) {
				return RGBA;
			}
			else if (format->BytesPerPixel == 3) {
				return RGB;
			}
			else {
				SDL_SetError("Unknown image format. Only PNG-24 and JPEG is supported.");
				return 0;
			}
		}
	}

	int invert_image(int width, int height, void *image_pixels) {
		auto temp_row = std::unique_ptr<char>(new char[width]);
		if (temp_row.get() == nullptr) {
			SDL_SetError("Not enough memory for image inversion");
			return -1;
		}
		//if height is odd, don't need to swap middle row
		int height_div_2 = height / 2;
		for (int index = 0; index < height_div_2; index++) {
			//uses string.h
			memcpy((Uint8 *)temp_row.get(),
				(Uint8 *)(image_pixels)+
				width * index,
				width);

			memcpy(
				(Uint8 *)(image_pixels)+
				width * index,
				(Uint8 *)(image_pixels)+
				width * (height - index - 1),
				width);
			memcpy(
				(Uint8 *)(image_pixels)+
				width * (height - index - 1),
				temp_row.get(),
				width);
		}
		return 0;
	}

	bool isPowerOfTwo(unsigned int x) {
		return ((x != 0) && !(x & (x - 1)));
	}

	Texture* Texture::createFromFile(const char *pngOrJpeg, bool generateMipmaps) {
		auto data = readAllBytes(pngOrJpeg);
		return createFromMem(data.data(), data.size(), generateMipmaps);
	}

	Texture *Texture::createFromMem(const char *pngOrJpeg, int size, bool generateMipmaps) {
		static bool initialized = false;
		if (!initialized) {
			initialized = true;
			int flags = IMG_INIT_PNG | IMG_INIT_JPG;
			int initted = IMG_Init(flags);
			if ((initted & flags) != flags) {
				std::cerr << "IMG_Init: Failed to init required jpg and png support!" << std::endl;
				std::cerr << "IMG_Init: " << IMG_GetError() << std::endl;
				// handle error
			}
		}

		SDL_RWops *source = SDL_RWFromConstMem(pngOrJpeg, size);
		SDL_Surface *res_texture = IMG_Load_RW(source, 1);
		if (res_texture == NULL) {
			std::cerr << "IMG_Load: " << SDL_GetError() << std::endl;
			return nullptr;
		}
		int w = res_texture->w;
		int h = res_texture->h;
		auto format = getFormat(res_texture);

		int bytesPerPixel = format == GL_BGR || format == GL_RGB ? 3 : 4;
		char *pixels = static_cast<char *>(res_texture->pixels);
		invert_image(w*bytesPerPixel, h, pixels);
		if (generateMipmaps && (!isPowerOfTwo((unsigned int)w) || !isPowerOfTwo((unsigned int)h))) {
			std::cerr << "Ignore mipmaps for textures not power of two" << std::endl;
			generateMipmaps = false;
		}
		Texture *res = new Texture(pixels, w, h, format, generateMipmaps);
		SDL_FreeSurface(res_texture);
		return res;
	}

	Texture *Texture::createFromRGBAMem(const char *data, int width, int height, bool generateMipmaps) {
		return new Texture(data, width, height, GL_RGBA, generateMipmaps);
	}

	// returns true if texture sampling should be filtered (bi-linear or tri-linear sampling) otherwise use point sampling.
	bool Texture::isFilterSampling() {
		return filterSampling;
	}

	// if true texture sampling is filtered (bi-linear or tri-linear sampling) otherwise use point sampling.
	void Texture::setFilterSampling(bool enable) {
		filterSampling = enable;
		updateTextureSampler();
	}

	bool Texture::isWrapTextureCoordinates() {
		return filterSampling;
	}

	void Texture::setWrapTextureCoordinates(bool enable) {
		wrapTextureCoordinates = enable;
		updateTextureSampler();
	}

	int Texture::getWidth() {
		return width;
	}

	int Texture::getHeight() {
		return height;
	}

	void Texture::updateTextureSampler() {
		glBindTexture(GL_TEXTURE_2D, textureId);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapTextureCoordinates ? GL_REPEAT : GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapTextureCoordinates ? GL_REPEAT : GL_CLAMP_TO_EDGE);
		GLuint minification;
		GLuint magnification;
		if (!filterSampling) {
			minification = GL_NEAREST;
			magnification = GL_NEAREST;
		}
		else if (generateMipmap) {
			minification = GL_LINEAR_MIPMAP_LINEAR;
			magnification = GL_LINEAR;
		}
		else {
			minification = GL_LINEAR;
			magnification = GL_LINEAR;
		}
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magnification);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minification);
	}

	Texture *Texture::getWhiteTexture() {
		if (whiteTexture != nullptr) {
			return whiteTexture;
		}
		char one = (char)0xff;
		std::vector<char> data(2 * 2 * 4, one);
		whiteTexture = createFromRGBAMem(data.data(), 2, 2, true);
		return whiteTexture;
	}

	Texture *Texture::getSphereTexture() {
		if (sphereTexture != nullptr) {
			return sphereTexture;
		}
		int size = 128;
		char one = (char)0xff;
		std::vector<char> data(size*size * 4, one);
		for (int x = 0; x<size; x++) {
			for (int y = 0; y<size; y++) {
				float distToCenter = glm::clamp(1.0f - 2.0f*glm::length(glm::vec2((x + 0.5f) / size, (y + 0.5f) / size) - glm::vec2(0.5f, 0.5f)), 0.0f, 1.0f);
				data[x*size * 4 + y * 4 + 0] = (char)(255 * distToCenter);
				data[x*size * 4 + y * 4 + 1] = (char)(255 * distToCenter);
				data[x*size * 4 + y * 4 + 2] = (char)(255 * distToCenter);
				data[x*size * 4 + y * 4 + 3] = (char)255;
			}
		}
		sphereTexture = createFromRGBAMem(data.data(), size, size, true);
		return sphereTexture;
	}
}
<commit_msg>0.2.8 Add support for Emscripten<commit_after>#include "SRE/Texture.hpp"

#include "SRE/impl/GL.hpp"
#include <stdio.h>
#include <SDL_surface.h>

#include <SDL_image.h>
#include <iostream>
#include <vector>
#include <fstream>
#include <memory>

namespace {
	static std::vector<char> readAllBytes(char const* filename)
	{
		using namespace std;
		ifstream ifs(filename, std::ios::binary | std::ios::ate);
		ifstream::pos_type pos = ifs.tellg();
		if (pos<0) {
			std::cerr << "Cannot read " << filename << std::endl;
			return std::vector<char>();
		}
		std::vector<char>  result((size_t)pos);

		ifs.seekg(0, ios::beg);
		ifs.read(&result[0], pos);

		return result;
	}
}

namespace SRE {
	Texture* Texture::whiteTexture = nullptr;
	Texture* Texture::sphereTexture = nullptr;

	Texture::Texture(const char *data, int width, int height, uint32_t format, bool generateMipmaps)
		: width{ width }, height{ height }, generateMipmap{ generateMipmaps } {
		glGenTextures(1, &textureId);

		GLenum target = GL_TEXTURE_2D;
		GLint mipmapLevel = 0;
		GLint internalFormat = GL_RGBA;
		GLint border = 0;
		GLenum type = GL_UNSIGNED_BYTE;
		glBindTexture(target, textureId);
		glTexImage2D(target, mipmapLevel, internalFormat, width, height, border, format, type, data);
		updateTextureSampler();
		if (generateMipmaps) {
			glGenerateMipmap(GL_TEXTURE_2D);
		}
	}

	GLenum getFormat(SDL_Surface *image) {
		SDL_PixelFormat *format = image->format;
		auto pixelFormat = format->format;
		bool isBGR = format->Rshift == 16;

#ifdef EMSCRIPTEN
        GLenum RGB = GL_RGB;
        GLenum RGBA = GL_RGBA;
#else
        GLenum RGB = isBGR ? GL_BGR : GL_RGB;
        GLenum RGBA = isBGR ? GL_BGRA : GL_RGBA;
#endif
		const bool alpha = SDL_ISPIXELFORMAT_ALPHA(pixelFormat);
		if (alpha) {
			return RGBA;
		}
		else {
			if (format->BytesPerPixel == 4) {
				return RGBA;
			}
			else if (format->BytesPerPixel == 3) {
				return RGB;
			}
			else {
				SDL_SetError("Unknown image format. Only PNG-24 and JPEG is supported.");
				return 0;
			}
		}
	}

	int invert_image(int width, int height, void *image_pixels) {
		auto temp_row = std::unique_ptr<char>(new char[width]);
		if (temp_row.get() == nullptr) {
			SDL_SetError("Not enough memory for image inversion");
			return -1;
		}
		//if height is odd, don't need to swap middle row
		int height_div_2 = height / 2;
		for (int index = 0; index < height_div_2; index++) {
			//uses string.h
			memcpy((Uint8 *)temp_row.get(),
				(Uint8 *)(image_pixels)+
				width * index,
				width);

			memcpy(
				(Uint8 *)(image_pixels)+
				width * index,
				(Uint8 *)(image_pixels)+
				width * (height - index - 1),
				width);
			memcpy(
				(Uint8 *)(image_pixels)+
				width * (height - index - 1),
				temp_row.get(),
				width);
		}
		return 0;
	}

	bool isPowerOfTwo(unsigned int x) {
		return ((x != 0) && !(x & (x - 1)));
	}

	Texture* Texture::createFromFile(const char *pngOrJpeg, bool generateMipmaps) {
		auto data = readAllBytes(pngOrJpeg);
		return createFromMem(data.data(), data.size(), generateMipmaps);
	}

	Texture *Texture::createFromMem(const char *pngOrJpeg, int size, bool generateMipmaps) {
		static bool initialized = false;
		if (!initialized) {
			initialized = true;
			int flags = IMG_INIT_PNG | IMG_INIT_JPG;
			int initted = IMG_Init(flags);
			if ((initted & flags) != flags) {
				std::cerr << "IMG_Init: Failed to init required jpg and png support!" << std::endl;
				std::cerr << "IMG_Init: " << IMG_GetError() << std::endl;
				// handle error
			}
		}

		SDL_RWops *source = SDL_RWFromConstMem(pngOrJpeg, size);
		SDL_Surface *res_texture = IMG_Load_RW(source, 1);
		if (res_texture == NULL) {
			std::cerr << "IMG_Load: " << SDL_GetError() << std::endl;
			return nullptr;
		}
		int w = res_texture->w;
		int h = res_texture->h;
		auto format = getFormat(res_texture);

		int bytesPerPixel =
#ifndef EMSCRIPTEN
				format == GL_BGR ||
#endif
						format == GL_RGB ? 3 : 4;
		char *pixels = static_cast<char *>(res_texture->pixels);
		invert_image(w*bytesPerPixel, h, pixels);
		if (generateMipmaps && (!isPowerOfTwo((unsigned int)w) || !isPowerOfTwo((unsigned int)h))) {
			std::cerr << "Ignore mipmaps for textures not power of two" << std::endl;
			generateMipmaps = false;
		}
		Texture *res = new Texture(pixels, w, h, format, generateMipmaps);
		SDL_FreeSurface(res_texture);
		return res;
	}

	Texture *Texture::createFromRGBAMem(const char *data, int width, int height, bool generateMipmaps) {
		return new Texture(data, width, height, GL_RGBA, generateMipmaps);
	}

	// returns true if texture sampling should be filtered (bi-linear or tri-linear sampling) otherwise use point sampling.
	bool Texture::isFilterSampling() {
		return filterSampling;
	}

	// if true texture sampling is filtered (bi-linear or tri-linear sampling) otherwise use point sampling.
	void Texture::setFilterSampling(bool enable) {
		filterSampling = enable;
		updateTextureSampler();
	}

	bool Texture::isWrapTextureCoordinates() {
		return filterSampling;
	}

	void Texture::setWrapTextureCoordinates(bool enable) {
		wrapTextureCoordinates = enable;
		updateTextureSampler();
	}

	int Texture::getWidth() {
		return width;
	}

	int Texture::getHeight() {
		return height;
	}

	void Texture::updateTextureSampler() {
		glBindTexture(GL_TEXTURE_2D, textureId);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapTextureCoordinates ? GL_REPEAT : GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapTextureCoordinates ? GL_REPEAT : GL_CLAMP_TO_EDGE);
		GLuint minification;
		GLuint magnification;
		if (!filterSampling) {
			minification = GL_NEAREST;
			magnification = GL_NEAREST;
		}
		else if (generateMipmap) {
			minification = GL_LINEAR_MIPMAP_LINEAR;
			magnification = GL_LINEAR;
		}
		else {
			minification = GL_LINEAR;
			magnification = GL_LINEAR;
		}
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magnification);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minification);
	}

	Texture *Texture::getWhiteTexture() {
		if (whiteTexture != nullptr) {
			return whiteTexture;
		}
		char one = (char)0xff;
		std::vector<char> data(2 * 2 * 4, one);
		whiteTexture = createFromRGBAMem(data.data(), 2, 2, true);
		return whiteTexture;
	}

	Texture *Texture::getSphereTexture() {
		if (sphereTexture != nullptr) {
			return sphereTexture;
		}
		int size = 128;
		char one = (char)0xff;
		std::vector<char> data(size*size * 4, one);
		for (int x = 0; x<size; x++) {
			for (int y = 0; y<size; y++) {
				float distToCenter = glm::clamp(1.0f - 2.0f*glm::length(glm::vec2((x + 0.5f) / size, (y + 0.5f) / size) - glm::vec2(0.5f, 0.5f)), 0.0f, 1.0f);
				data[x*size * 4 + y * 4 + 0] = (char)(255 * distToCenter);
				data[x*size * 4 + y * 4 + 1] = (char)(255 * distToCenter);
				data[x*size * 4 + y * 4 + 2] = (char)(255 * distToCenter);
				data[x*size * 4 + y * 4 + 3] = (char)255;
			}
		}
		sphereTexture = createFromRGBAMem(data.data(), size, size, true);
		return sphereTexture;
	}
}
<|endoftext|>


<commit_before>// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "xwalk/application/common/application_data.h"

#include "base/base64.h"
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/stl_util.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "base/version.h"
#include "xwalk/application/common/application_manifest_constants.h"
#include "xwalk/application/common/id_util.h"
#include "xwalk/application/common/constants.h"
#include "xwalk/application/common/manifest.h"
#include "xwalk/application/common/manifest_handler.h"
#include "xwalk/application/common/manifest_handlers/main_document_handler.h"
#include "xwalk/application/common/manifest_handlers/permissions_handler.h"
#include "xwalk/application/common/permission_policy_manager.h"
#include "content/public/common/url_constants.h"
#include "url/url_util.h"
#include "ui/base/l10n/l10n_util.h"

namespace keys = xwalk::application_manifest_keys;
namespace widget_keys = xwalk::application_widget_keys;
namespace errors = xwalk::application_manifest_errors;

namespace xwalk {
namespace application {

// static
scoped_refptr<ApplicationData> ApplicationData::Create(
    const base::FilePath& path,
    Manifest::SourceType source_type,
    const base::DictionaryValue& manifest_data,
    const std::string& explicit_id,
    std::string* error_message) {
  DCHECK(error_message);
  base::string16 error;
  scoped_ptr<xwalk::application::Manifest> manifest(
      new xwalk::application::Manifest(source_type,
                 scoped_ptr<base::DictionaryValue>(manifest_data.DeepCopy())));

  if (!InitApplicationID(manifest.get(), path, explicit_id, &error)) {
    *error_message = base::UTF16ToUTF8(error);
    return NULL;
  }

  std::vector<InstallWarning> install_warnings;
  if (!manifest->ValidateManifest(error_message, &install_warnings)) {
    return NULL;
  }

  scoped_refptr<ApplicationData> application = new ApplicationData(path,
                                                           manifest.Pass());
  application->install_warnings_.swap(install_warnings);

  if (!application->Init(&error)) {
    *error_message = base::UTF16ToUTF8(error);
    return NULL;
  }

  return application;
}

// static
bool ApplicationData::IsIDValid(const std::string& id) {
  std::string temp = StringToLowerASCII(id);

#if defined(OS_TIZEN)
  // An ID with 10 characters is most likely a legacy Tizen ID.
  if (temp.size() == kLegacyTizenIdSize) {
    for (size_t i = 0; i < kLegacyTizenIdSize; ++i) {
      const char c = temp[i];
      const bool valid = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z');
      if (!valid)
        return false;
    }

    return true;
  }
#endif

  // Verify that the id is legal.
  if (temp.size() != (kIdSize * 2))
    return false;

  // We only support lowercase IDs, because IDs can be used as URL components
  // (where GURL will lowercase it).
  for (size_t i = 0; i < temp.size(); ++i)
    if (temp[i] < 'a' || temp[i] > 'p')
      return false;

  return true;
}

// static
GURL ApplicationData::GetBaseURLFromApplicationId(
    const std::string& application_id) {
  return GURL(std::string(xwalk::application::kApplicationScheme) +
              content::kStandardSchemeSeparator + application_id + "/");
}

ApplicationData::ManifestData* ApplicationData::GetManifestData(
        const std::string& key) const {
  DCHECK(finished_parsing_manifest_ || thread_checker_.CalledOnValidThread());
  ManifestDataMap::const_iterator iter = manifest_data_.find(key);
  if (iter != manifest_data_.end())
    return iter->second.get();
  return NULL;
}

void ApplicationData::SetManifestData(const std::string& key,
                                      ApplicationData::ManifestData* data) {
  DCHECK(!finished_parsing_manifest_ && thread_checker_.CalledOnValidThread());
  manifest_data_[key] = linked_ptr<ManifestData>(data);
}

Manifest::SourceType ApplicationData::GetSourceType() const {
  return manifest_->GetSourceType();
}

const std::string& ApplicationData::ID() const {
  return manifest_->GetApplicationID();
}

const std::string ApplicationData::VersionString() const {
  return Version()->GetString();
}

bool ApplicationData::IsPlatformApp() const {
  return manifest_->IsPackaged();
}

bool ApplicationData::IsHostedApp() const {
  return GetManifest()->IsHosted();
}

Manifest::PackageType ApplicationData::GetPackageType() const {
  return manifest_->GetPackageType();
}

bool ApplicationData::HasMainDocument() const {
  MainDocumentInfo* main_info = ToMainDocumentInfo(
      GetManifestData(application_manifest_keys::kAppMainKey));

  return main_info != NULL;
}

// static
bool ApplicationData::InitApplicationID(xwalk::application::Manifest* manifest,
                                const base::FilePath& path,
                                const std::string& explicit_id,
                                base::string16* error) {
  std::string application_id;
#if defined(OS_TIZEN)
  if (manifest->HasKey(keys::kTizenAppIdKey)) {
    if (!manifest->GetString(keys::kTizenAppIdKey, &application_id)) {
      NOTREACHED() << "Could not get Tizen application key";
      return false;
    }
  }

  if (!application_id.empty()) {
    manifest->SetApplicationID(application_id);
    return true;
  }
#endif

  if (!explicit_id.empty()) {
    manifest->SetApplicationID(explicit_id);
    return true;
  }

  application_id = GenerateIdForPath(path);
  if (application_id.empty()) {
    NOTREACHED() << "Could not create ID from path.";
    return false;
  }
  manifest->SetApplicationID(application_id);
  return true;
}

ApplicationData::ApplicationData(const base::FilePath& path,
                     scoped_ptr<xwalk::application::Manifest> manifest)
    : manifest_version_(0),
      is_dirty_(false),
      manifest_(manifest.release()),
      finished_parsing_manifest_(false) {
  DCHECK(path.empty() || path.IsAbsolute());
  path_ = path;
}

ApplicationData::~ApplicationData() {
}

// static
GURL ApplicationData::GetResourceURL(const GURL& application_url,
                               const std::string& relative_path) {
  DCHECK(application_url.SchemeIs(xwalk::application::kApplicationScheme));
  DCHECK_EQ("/", application_url.path());

  std::string path = relative_path;

  // If the relative path starts with "/", it is "absolute" relative to the
  // application base directory, but application_url is already specified to
  // refer to that base directory, so strip the leading "/" if present.
  if (relative_path.size() > 0 && relative_path[0] == '/')
    path = relative_path.substr(1);

  GURL ret_val = GURL(application_url.spec() + path);
  DCHECK(StartsWithASCII(ret_val.spec(), application_url.spec(), false));

  return ret_val;
}

Manifest::Type ApplicationData::GetType() const {
  return manifest_->GetType();
}

bool ApplicationData::Init(base::string16* error) {
  DCHECK(error);

  if (!LoadName(error))
    return false;
  if (!LoadVersion(error))
      return false;
  if (!LoadDescription(error))
      return false;
  if (!LoadManifestVersion(error))
    return false;

  application_url_ = ApplicationData::GetBaseURLFromApplicationId(ID());

  ManifestHandlerRegistry* registry =
      ManifestHandlerRegistry::GetInstance(GetPackageType());
  if (!registry->ParseAppManifest(this, error))
    return false;

  finished_parsing_manifest_ = true;
  return true;
}

bool ApplicationData::LoadName(base::string16* error) {
  DCHECK(error);
  base::string16 localized_name;
  std::string name_key(GetNameKey(GetPackageType()));

  if (!manifest_->GetString(name_key, &localized_name) &&
      manifest_->IsXPKPackaged()) {
    *error = base::ASCIIToUTF16(errors::kInvalidName);
    return false;
  }
  non_localized_name_ = base::UTF16ToUTF8(localized_name);
  base::i18n::AdjustStringForLocaleDirection(&localized_name);
  name_ = base::UTF16ToUTF8(localized_name);
  return true;
}

bool ApplicationData::LoadVersion(base::string16* error) {
  DCHECK(error);
  std::string version_str;
  std::string version_key(GetVersionKey(GetPackageType()));

  if (!manifest_->GetString(version_key, &version_str) &&
      manifest_->IsXPKPackaged()) {
    *error = base::ASCIIToUTF16(errors::kInvalidVersion);
    return false;
  }
  version_.reset(new base::Version(version_str));
  if (manifest_->IsXPKPackaged() &&
      (!version_->IsValid() || version_->components().size() > 4)) {
    *error = base::ASCIIToUTF16(errors::kInvalidVersion);
    return false;
  }
  return true;
}

bool ApplicationData::LoadDescription(base::string16* error) {
  DCHECK(error);
  if (manifest_->HasKey(keys::kDescriptionKey) &&
      !manifest_->GetString(keys::kDescriptionKey, &description_) &&
      manifest_->IsXPKPackaged()) {
    *error = base::ASCIIToUTF16(errors::kInvalidDescription);
    return false;
  }
  return true;
}

bool ApplicationData::LoadManifestVersion(base::string16* error) {
  DCHECK(error);
  // Get the original value out of the dictionary so that we can validate it
  // more strictly.
  if (manifest_->value()->HasKey(keys::kManifestVersionKey)) {
    int manifest_version = 1;
    if (!manifest_->GetInteger(keys::kManifestVersionKey, &manifest_version) ||
        manifest_version < 1) {
      if (manifest_->IsXPKPackaged()) {
        *error = base::ASCIIToUTF16(errors::kInvalidManifestVersion);
        return false;
      }
    }
  }

  manifest_version_ = manifest_->GetManifestVersion();
  return true;
}

void ApplicationData::SetEvents(const std::set<std::string>& events) {
  events_ = events;
  is_dirty_ = true;
}

const std::set<std::string>& ApplicationData::GetEvents() const {
  return events_;
}

StoredPermission ApplicationData::GetPermission(
    std::string& permission_name) const {
  StoredPermissionMap::const_iterator iter =
      permission_map_.find(permission_name);
  if (iter == permission_map_.end())
    return UNDEFINED_STORED_PERM;
  return iter->second;
}

bool ApplicationData::SetPermission(const std::string& permission_name,
                                    StoredPermission perm) {
  if (perm != UNDEFINED_STORED_PERM) {
    permission_map_[permission_name] = perm;
    is_dirty_ = true;
    return true;
  }
  return false;
}

void ApplicationData::ClearPermissions() {
  permission_map_.clear();
}

PermissionSet ApplicationData::GetManifestPermissions() const {
  PermissionSet permissions;
  if (manifest_->value()->HasKey(keys::kPermissionsKey)) {
    const PermissionsInfo* perm_info = static_cast<PermissionsInfo*>(
                           GetManifestData(keys::kPermissionsKey));
    permissions = perm_info->GetAPIPermissions();
  }
  return permissions;
}

}   // namespace application
}   // namespace xwalk
<commit_msg>[Tizen] [Runtime] Fix the issue that widget apps fails to install without version attribute.<commit_after>// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "xwalk/application/common/application_data.h"

#include "base/base64.h"
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/stl_util.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "base/version.h"
#include "xwalk/application/common/application_manifest_constants.h"
#include "xwalk/application/common/id_util.h"
#include "xwalk/application/common/constants.h"
#include "xwalk/application/common/manifest.h"
#include "xwalk/application/common/manifest_handler.h"
#include "xwalk/application/common/manifest_handlers/main_document_handler.h"
#include "xwalk/application/common/manifest_handlers/permissions_handler.h"
#include "xwalk/application/common/permission_policy_manager.h"
#include "content/public/common/url_constants.h"
#include "url/url_util.h"
#include "ui/base/l10n/l10n_util.h"

namespace keys = xwalk::application_manifest_keys;
namespace widget_keys = xwalk::application_widget_keys;
namespace errors = xwalk::application_manifest_errors;

namespace xwalk {
namespace application {

// static
scoped_refptr<ApplicationData> ApplicationData::Create(
    const base::FilePath& path,
    Manifest::SourceType source_type,
    const base::DictionaryValue& manifest_data,
    const std::string& explicit_id,
    std::string* error_message) {
  DCHECK(error_message);
  base::string16 error;
  scoped_ptr<xwalk::application::Manifest> manifest(
      new xwalk::application::Manifest(source_type,
                 scoped_ptr<base::DictionaryValue>(manifest_data.DeepCopy())));

  if (!InitApplicationID(manifest.get(), path, explicit_id, &error)) {
    *error_message = base::UTF16ToUTF8(error);
    return NULL;
  }

  std::vector<InstallWarning> install_warnings;
  if (!manifest->ValidateManifest(error_message, &install_warnings)) {
    return NULL;
  }

  scoped_refptr<ApplicationData> application = new ApplicationData(path,
                                                           manifest.Pass());
  application->install_warnings_.swap(install_warnings);

  if (!application->Init(&error)) {
    *error_message = base::UTF16ToUTF8(error);
    return NULL;
  }

  return application;
}

// static
bool ApplicationData::IsIDValid(const std::string& id) {
  std::string temp = StringToLowerASCII(id);

#if defined(OS_TIZEN)
  // An ID with 10 characters is most likely a legacy Tizen ID.
  if (temp.size() == kLegacyTizenIdSize) {
    for (size_t i = 0; i < kLegacyTizenIdSize; ++i) {
      const char c = temp[i];
      const bool valid = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z');
      if (!valid)
        return false;
    }

    return true;
  }
#endif

  // Verify that the id is legal.
  if (temp.size() != (kIdSize * 2))
    return false;

  // We only support lowercase IDs, because IDs can be used as URL components
  // (where GURL will lowercase it).
  for (size_t i = 0; i < temp.size(); ++i)
    if (temp[i] < 'a' || temp[i] > 'p')
      return false;

  return true;
}

// static
GURL ApplicationData::GetBaseURLFromApplicationId(
    const std::string& application_id) {
  return GURL(std::string(xwalk::application::kApplicationScheme) +
              content::kStandardSchemeSeparator + application_id + "/");
}

ApplicationData::ManifestData* ApplicationData::GetManifestData(
        const std::string& key) const {
  DCHECK(finished_parsing_manifest_ || thread_checker_.CalledOnValidThread());
  ManifestDataMap::const_iterator iter = manifest_data_.find(key);
  if (iter != manifest_data_.end())
    return iter->second.get();
  return NULL;
}

void ApplicationData::SetManifestData(const std::string& key,
                                      ApplicationData::ManifestData* data) {
  DCHECK(!finished_parsing_manifest_ && thread_checker_.CalledOnValidThread());
  manifest_data_[key] = linked_ptr<ManifestData>(data);
}

Manifest::SourceType ApplicationData::GetSourceType() const {
  return manifest_->GetSourceType();
}

const std::string& ApplicationData::ID() const {
  return manifest_->GetApplicationID();
}

const std::string ApplicationData::VersionString() const {
  if (!version_->components().empty())
    return Version()->GetString();

  return "";
}

bool ApplicationData::IsPlatformApp() const {
  return manifest_->IsPackaged();
}

bool ApplicationData::IsHostedApp() const {
  return GetManifest()->IsHosted();
}

Manifest::PackageType ApplicationData::GetPackageType() const {
  return manifest_->GetPackageType();
}

bool ApplicationData::HasMainDocument() const {
  MainDocumentInfo* main_info = ToMainDocumentInfo(
      GetManifestData(application_manifest_keys::kAppMainKey));

  return main_info != NULL;
}

// static
bool ApplicationData::InitApplicationID(xwalk::application::Manifest* manifest,
                                const base::FilePath& path,
                                const std::string& explicit_id,
                                base::string16* error) {
  std::string application_id;
#if defined(OS_TIZEN)
  if (manifest->HasKey(keys::kTizenAppIdKey)) {
    if (!manifest->GetString(keys::kTizenAppIdKey, &application_id)) {
      NOTREACHED() << "Could not get Tizen application key";
      return false;
    }
  }

  if (!application_id.empty()) {
    manifest->SetApplicationID(application_id);
    return true;
  }
#endif

  if (!explicit_id.empty()) {
    manifest->SetApplicationID(explicit_id);
    return true;
  }

  application_id = GenerateIdForPath(path);
  if (application_id.empty()) {
    NOTREACHED() << "Could not create ID from path.";
    return false;
  }
  manifest->SetApplicationID(application_id);
  return true;
}

ApplicationData::ApplicationData(const base::FilePath& path,
                     scoped_ptr<xwalk::application::Manifest> manifest)
    : manifest_version_(0),
      is_dirty_(false),
      manifest_(manifest.release()),
      finished_parsing_manifest_(false) {
  DCHECK(path.empty() || path.IsAbsolute());
  path_ = path;
}

ApplicationData::~ApplicationData() {
}

// static
GURL ApplicationData::GetResourceURL(const GURL& application_url,
                               const std::string& relative_path) {
  DCHECK(application_url.SchemeIs(xwalk::application::kApplicationScheme));
  DCHECK_EQ("/", application_url.path());

  std::string path = relative_path;

  // If the relative path starts with "/", it is "absolute" relative to the
  // application base directory, but application_url is already specified to
  // refer to that base directory, so strip the leading "/" if present.
  if (relative_path.size() > 0 && relative_path[0] == '/')
    path = relative_path.substr(1);

  GURL ret_val = GURL(application_url.spec() + path);
  DCHECK(StartsWithASCII(ret_val.spec(), application_url.spec(), false));

  return ret_val;
}

Manifest::Type ApplicationData::GetType() const {
  return manifest_->GetType();
}

bool ApplicationData::Init(base::string16* error) {
  DCHECK(error);

  if (!LoadName(error))
    return false;
  if (!LoadVersion(error))
      return false;
  if (!LoadDescription(error))
      return false;
  if (!LoadManifestVersion(error))
    return false;

  application_url_ = ApplicationData::GetBaseURLFromApplicationId(ID());

  ManifestHandlerRegistry* registry =
      ManifestHandlerRegistry::GetInstance(GetPackageType());
  if (!registry->ParseAppManifest(this, error))
    return false;

  finished_parsing_manifest_ = true;
  return true;
}

bool ApplicationData::LoadName(base::string16* error) {
  DCHECK(error);
  base::string16 localized_name;
  std::string name_key(GetNameKey(GetPackageType()));

  if (!manifest_->GetString(name_key, &localized_name) &&
      manifest_->IsXPKPackaged()) {
    *error = base::ASCIIToUTF16(errors::kInvalidName);
    return false;
  }
  non_localized_name_ = base::UTF16ToUTF8(localized_name);
  base::i18n::AdjustStringForLocaleDirection(&localized_name);
  name_ = base::UTF16ToUTF8(localized_name);
  return true;
}

bool ApplicationData::LoadVersion(base::string16* error) {
  DCHECK(error);
  std::string version_str;
  std::string version_key(GetVersionKey(GetPackageType()));

  if (!manifest_->GetString(version_key, &version_str) &&
      manifest_->IsXPKPackaged()) {
    *error = base::ASCIIToUTF16(errors::kInvalidVersion);
    return false;
  }
  version_.reset(new base::Version(version_str));
  if (manifest_->IsXPKPackaged() &&
      (!version_->IsValid() || version_->components().size() > 4)) {
    *error = base::ASCIIToUTF16(errors::kInvalidVersion);
    return false;
  }
  return true;
}

bool ApplicationData::LoadDescription(base::string16* error) {
  DCHECK(error);
  if (manifest_->HasKey(keys::kDescriptionKey) &&
      !manifest_->GetString(keys::kDescriptionKey, &description_) &&
      manifest_->IsXPKPackaged()) {
    *error = base::ASCIIToUTF16(errors::kInvalidDescription);
    return false;
  }
  return true;
}

bool ApplicationData::LoadManifestVersion(base::string16* error) {
  DCHECK(error);
  // Get the original value out of the dictionary so that we can validate it
  // more strictly.
  if (manifest_->value()->HasKey(keys::kManifestVersionKey)) {
    int manifest_version = 1;
    if (!manifest_->GetInteger(keys::kManifestVersionKey, &manifest_version) ||
        manifest_version < 1) {
      if (manifest_->IsXPKPackaged()) {
        *error = base::ASCIIToUTF16(errors::kInvalidManifestVersion);
        return false;
      }
    }
  }

  manifest_version_ = manifest_->GetManifestVersion();
  return true;
}

void ApplicationData::SetEvents(const std::set<std::string>& events) {
  events_ = events;
  is_dirty_ = true;
}

const std::set<std::string>& ApplicationData::GetEvents() const {
  return events_;
}

StoredPermission ApplicationData::GetPermission(
    std::string& permission_name) const {
  StoredPermissionMap::const_iterator iter =
      permission_map_.find(permission_name);
  if (iter == permission_map_.end())
    return UNDEFINED_STORED_PERM;
  return iter->second;
}

bool ApplicationData::SetPermission(const std::string& permission_name,
                                    StoredPermission perm) {
  if (perm != UNDEFINED_STORED_PERM) {
    permission_map_[permission_name] = perm;
    is_dirty_ = true;
    return true;
  }
  return false;
}

void ApplicationData::ClearPermissions() {
  permission_map_.clear();
}

PermissionSet ApplicationData::GetManifestPermissions() const {
  PermissionSet permissions;
  if (manifest_->value()->HasKey(keys::kPermissionsKey)) {
    const PermissionsInfo* perm_info = static_cast<PermissionsInfo*>(
                           GetManifestData(keys::kPermissionsKey));
    permissions = perm_info->GetAPIPermissions();
  }
  return permissions;
}

}   // namespace application
}   // namespace xwalk
<|endoftext|>


<commit_before>#include "event_manager.hh"

#include <unistd.h>

namespace Kakoune
{

FDWatcher::FDWatcher(int fd, Callback callback)
    : m_fd{fd}, m_callback{std::move(callback)}
{
    EventManager::instance().m_fd_watchers.insert(this);
}

FDWatcher::~FDWatcher()
{
    EventManager::instance().m_fd_watchers.erase(this);
}

void FDWatcher::run(EventMode mode)
{
    m_callback(*this, mode);
}

void FDWatcher::close_fd()
{
    close(m_fd);
    m_fd = -1;
}

Timer::Timer(TimePoint date, Callback callback, EventMode mode)
    : m_date{date}, m_callback{std::move(callback)}, m_mode(mode)
{
    if (EventManager::has_instance())
        EventManager::instance().m_timers.insert(this);
}

Timer::~Timer()
{
    if (EventManager::has_instance())
        EventManager::instance().m_timers.erase(this);
}

void Timer::run(EventMode mode)
{
    if (mode == m_mode)
    {
        m_date = TimePoint::max();
        m_callback(*this);
    }
    else // try again a little later
        m_date = Clock::now() + std::chrono::milliseconds{10};
}

EventManager::EventManager()
{
    FD_ZERO(&m_forced_fd);
}

EventManager::~EventManager()
{
    kak_assert(m_fd_watchers.empty());
    kak_assert(m_timers.empty());
}

void EventManager::handle_next_events(EventMode mode)
{
    int max_fd = 0;
    fd_set rfds;
    FD_ZERO(&rfds);
    for (auto& watcher : m_fd_watchers)
    {
        const int fd = watcher->fd();
        if (fd != -1)
        {
            max_fd = std::max(fd, max_fd);
            FD_SET(fd, &rfds);
        }
    }

    TimePoint next_timer = TimePoint::max();
    for (auto& timer : m_timers)
    {
        if (timer->next_date() <= next_timer)
            next_timer = timer->next_date();
    }
    using namespace std::chrono;
    auto timeout = duration_cast<microseconds>(next_timer - Clock::now()).count();

    constexpr auto us = 1000000000ll;
    timeval tv{ (long)(timeout / us), (long)(timeout % us) };
    int res = select(max_fd + 1, &rfds, nullptr, nullptr, &tv);

    // copy forced fds *after* poll, so that signal handlers can write to
    // m_forced_fd, interupt poll, and directly be serviced.
    fd_set forced = m_forced_fd;
    FD_ZERO(&m_forced_fd);

    for (int fd = 0; fd < max_fd + 1; ++fd)
    {
        if ((res > 0 and FD_ISSET(fd, &rfds)) or FD_ISSET(fd, &forced))
        {
            auto it = find_if(m_fd_watchers,
                              [fd](const FDWatcher* w){return w->fd() == fd; });
            if (it != m_fd_watchers.end())
                (*it)->run(mode);
        }
    }

    TimePoint now = Clock::now();
    for (auto& timer : m_timers)
    {
        if (timer->next_date() <= now)
            timer->run(mode);
    }
}

void EventManager::force_signal(int fd)
{
    FD_SET(fd, &m_forced_fd);
}

}
<commit_msg>Fix timeval field types<commit_after>#include "event_manager.hh"

#include <unistd.h>

namespace Kakoune
{

FDWatcher::FDWatcher(int fd, Callback callback)
    : m_fd{fd}, m_callback{std::move(callback)}
{
    EventManager::instance().m_fd_watchers.insert(this);
}

FDWatcher::~FDWatcher()
{
    EventManager::instance().m_fd_watchers.erase(this);
}

void FDWatcher::run(EventMode mode)
{
    m_callback(*this, mode);
}

void FDWatcher::close_fd()
{
    close(m_fd);
    m_fd = -1;
}

Timer::Timer(TimePoint date, Callback callback, EventMode mode)
    : m_date{date}, m_callback{std::move(callback)}, m_mode(mode)
{
    if (EventManager::has_instance())
        EventManager::instance().m_timers.insert(this);
}

Timer::~Timer()
{
    if (EventManager::has_instance())
        EventManager::instance().m_timers.erase(this);
}

void Timer::run(EventMode mode)
{
    if (mode == m_mode)
    {
        m_date = TimePoint::max();
        m_callback(*this);
    }
    else // try again a little later
        m_date = Clock::now() + std::chrono::milliseconds{10};
}

EventManager::EventManager()
{
    FD_ZERO(&m_forced_fd);
}

EventManager::~EventManager()
{
    kak_assert(m_fd_watchers.empty());
    kak_assert(m_timers.empty());
}

void EventManager::handle_next_events(EventMode mode)
{
    int max_fd = 0;
    fd_set rfds;
    FD_ZERO(&rfds);
    for (auto& watcher : m_fd_watchers)
    {
        const int fd = watcher->fd();
        if (fd != -1)
        {
            max_fd = std::max(fd, max_fd);
            FD_SET(fd, &rfds);
        }
    }

    TimePoint next_timer = TimePoint::max();
    for (auto& timer : m_timers)
    {
        if (timer->next_date() <= next_timer)
            next_timer = timer->next_date();
    }
    using namespace std::chrono;
    auto timeout = duration_cast<microseconds>(next_timer - Clock::now()).count();

    constexpr auto us = 1000000000ll;
    timeval tv{ (time_t)(timeout / us), (suseconds_t)(timeout % us) };
    int res = select(max_fd + 1, &rfds, nullptr, nullptr, &tv);

    // copy forced fds *after* poll, so that signal handlers can write to
    // m_forced_fd, interupt poll, and directly be serviced.
    fd_set forced = m_forced_fd;
    FD_ZERO(&m_forced_fd);

    for (int fd = 0; fd < max_fd + 1; ++fd)
    {
        if ((res > 0 and FD_ISSET(fd, &rfds)) or FD_ISSET(fd, &forced))
        {
            auto it = find_if(m_fd_watchers,
                              [fd](const FDWatcher* w){return w->fd() == fd; });
            if (it != m_fd_watchers.end())
                (*it)->run(mode);
        }
    }

    TimePoint now = Clock::now();
    for (auto& timer : m_timers)
    {
        if (timer->next_date() <= now)
            timer->run(mode);
    }
}

void EventManager::force_signal(int fd)
{
    FD_SET(fd, &m_forced_fd);
}

}
<|endoftext|>


<commit_before>/*
 *   Copyright (C) 2014 Pelagicore AB
 *   All rights reserved.
 */
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>

#include "config.h"
#include "controllerinterface.h"

ControllerInterface::ControllerInterface(const std::string &gatewayDir):
    m_connected(false),
    m_socketPath(gatewayDir + "/ipc_socket"),
    m_running(false),
    m_listenSocket(0),
    m_connectionSocket(0),
    m_highestFd(0),
    m_timeout(Config::instance()->controllerConnectionTimeout())
{
}

ControllerInterface::~ControllerInterface()
{
    // Close both the sockets...
    if (m_connectionSocket) {
        if (close(m_connectionSocket) == -1) {
            log_error() << "close:" << strerror(errno);
        }
    }

    if (m_listenSocket) {
        if (close(m_listenSocket) == -1) {
            log_error() << "close listen:" << strerror(errno);
        }
    }

    // ...and unlink the file
    if (unlink(m_socketPath.c_str()) == -1) {
        log_error() << "unlink:" << strerror(errno);
    }
}

bool ControllerInterface::initialize()
{
    if ((m_listenSocket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
        log_error() << "socket:" << strerror(errno);
        return false;
    }

    struct sockaddr_un local;
    local.sun_family = AF_UNIX;
    strcpy(local.sun_path, m_socketPath.c_str());
    unlink(local.sun_path);

    int len = strlen(local.sun_path) * sizeof(local.sun_family);
    if (bind(m_listenSocket, (struct sockaddr *)&local, len) == -1) {
        log_error() << "bind:" << strerror(errno);
        return false;
    }

    if (listen(m_listenSocket, 1) == -1) {
        log_error() << "listen:" << strerror(errno);
        return false;
    }

    m_selectTimeout.tv_sec = m_timeout;
    m_selectTimeout.tv_usec = 0;

    if (!isControllerConnected()) {
        return false;
    }

    return true;
}

bool ControllerInterface::isControllerConnected()
{
    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(m_listenSocket, &readfds);

    log_debug() << "Waiting for Controller, timeout value is " << m_timeout << " seconds...";

    m_highestFd = m_listenSocket;

    struct timeval tv;
    tv.tv_sec = m_timeout;
    tv.tv_usec = 0;
    int ret = select(m_highestFd + 1, &readfds, 0, 0, &tv);
    if (ret == -1) {
        log_error() << "select:" << strerror(errno);
        return false;
    } else if (ret == 0) {
        log_error() << "select reached timeout";
        return false;
    }

    if (FD_ISSET(m_listenSocket, &readfds)) {
        socklen_t t = sizeof(m_remote);
        m_connectionSocket = accept(m_listenSocket, (struct sockaddr *)&m_remote, &t);
        if (m_connectionSocket == -1) {
            log_error() << "accept:" << strerror(errno);
            // TODO: We need to propagate failure here
            return false; // Disconnects the signal handler
        }

        log_debug() << "Accepted connection from Controller";
        m_connected = true;

        if (m_connectionSocket > m_highestFd) {
            m_highestFd = m_connectionSocket;
        }
        // We are connected, i.e. the Controller is there, now we should
        // be prepared to receive messages.
        sigc::slot<bool> checkForMessageSlot = sigc::mem_fun(this, &ControllerInterface::checkForMessage);
        sigc::connection conn = Glib::signal_timeout().connect(checkForMessageSlot, 10);
    }

    return true;
}

bool ControllerInterface::checkForMessage()
{
    char str[1024];
    memset(str, '\0', sizeof(char) * 1024);

    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 0;

    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(m_connectionSocket, &readfds);
    select(m_highestFd + 1, &readfds, 0, 0, &tv);

    if (FD_ISSET(m_connectionSocket, &readfds)) {
        int received = recv(m_connectionSocket, str, 100, 0);
        if (received == -1) {
            log_error() << "recv:" << strerror(errno);
            // TODO: Do we need to propagate failure here?
            // TODO: Should we continue waiting for messages after this?
            return false; // Disconnects the signal handler
        } else if (received == 0) {
            // Controller has shut down, there will be no more messages
            return false; // Disconnects the signal handler
        }

        log_debug() << "Pelagicontain received: " << str;
    }

    return true; // Continue to let the signal handler call this method
}

bool ControllerInterface::startApp()
{
    bool result = false;

    if (canSend()) {
        char msg[] = {'1', '\0'};

        log_debug("Sending: \"%s\"", msg);

        int ret = send(m_connectionSocket, msg, sizeof(msg), 0);
        if (ret == -1) {
            log_error() << "send:" << strerror(errno);
        } else {
            m_running = true;
            result = true;
        }
    }

    return result;
}

bool ControllerInterface::shutdown()
{
    bool result = false;

    if (canSend()) {
        char msg[] = {'2', '\0'};

        log_debug("Sending: \"%s\"", msg);

        int ret = send(m_connectionSocket, msg, sizeof(msg), 0);
        if (ret == -1) {
            log_error() << "send:" << strerror(errno);
        } else {
            m_running = false;
            result = true;
        }
    }

    return result;
}

bool ControllerInterface::setEnvironmentVariable(const std::string &variable,
                                                 const std::string &value)
{
    bool result = false;

    if (canSend()) {
        std::string command = "3 " + variable + " " + value;
        char msg[command.size() + 1];

        memcpy(msg, command.c_str(), command.size());
        msg[command.size()] = '\0';

        log_debug("Sending: \"%s\"", msg);

        int ret = send(m_connectionSocket, msg, strlen(msg) + 1, 0);
        if (ret == -1) {
            log_error() << "send:" << strerror(errno);
        } else {
            result = true;
        }
    }

    return result;
}

bool ControllerInterface::systemCall(const std::string &cmd)
{
    bool result = false;

    if (canSend()) {
        std::string command = "4 " + cmd;
        char msg[command.size() + 1];

        memcpy(msg, command.c_str(), command.size());
        msg[command.size()] = '\0';

        log_debug("Sending: \"%s\"", msg);

        int ret = send(m_connectionSocket, msg, strlen(msg) + 1, 0);
        if (ret == -1) {
            log_error() << "send:" << strerror(errno);
        } else {
            result = true;
        }
    }

    return result;
}

bool ControllerInterface::hasBeenStarted() const
{
    return m_running;
}

bool ControllerInterface::canSend()
{
    if (!m_connected) {
        log_error() << "No connection to Controller";
        return false;
    }

    fd_set sendfds;
    FD_ZERO(&sendfds);
    FD_SET(m_connectionSocket, &sendfds);

    int ret = select(m_highestFd + 1, 0, &sendfds, 0, &m_selectTimeout);
    if (ret == -1) {
        log_error() << "select:" << strerror(errno);
        return false;
    } else if (ret == 0) {
        log_error() << "select reached timeout";
        return false;
    }

    return FD_ISSET(m_connectionSocket, &sendfds) ? true : false;
}
<commit_msg>controllerinterface.cpp: fix type in calculation<commit_after>/*
 *   Copyright (C) 2014 Pelagicore AB
 *   All rights reserved.
 */
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>

#include "config.h"
#include "controllerinterface.h"

ControllerInterface::ControllerInterface(const std::string &gatewayDir):
    m_connected(false),
    m_socketPath(gatewayDir + "/ipc_socket"),
    m_running(false),
    m_listenSocket(0),
    m_connectionSocket(0),
    m_highestFd(0),
    m_timeout(Config::instance()->controllerConnectionTimeout())
{
}

ControllerInterface::~ControllerInterface()
{
    // Close both the sockets...
    if (m_connectionSocket) {
        if (close(m_connectionSocket) == -1) {
            log_error() << "close:" << strerror(errno);
        }
    }

    if (m_listenSocket) {
        if (close(m_listenSocket) == -1) {
            log_error() << "close listen:" << strerror(errno);
        }
    }

    // ...and unlink the file
    if (unlink(m_socketPath.c_str()) == -1) {
        log_error() << "unlink:" << strerror(errno);
    }
}

bool ControllerInterface::initialize()
{
    if ((m_listenSocket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
        log_error() << "socket:" << strerror(errno);
        return false;
    }

    struct sockaddr_un local;
    local.sun_family = AF_UNIX;
    strcpy(local.sun_path, m_socketPath.c_str());
    unlink(local.sun_path);

    int len = strlen(local.sun_path) + sizeof(local.sun_family);
    if (bind(m_listenSocket, (struct sockaddr *)&local, len) == -1) {
        log_error() << "bind:" << strerror(errno);
        return false;
    }

    if (listen(m_listenSocket, 1) == -1) {
        log_error() << "listen:" << strerror(errno);
        return false;
    }

    m_selectTimeout.tv_sec = m_timeout;
    m_selectTimeout.tv_usec = 0;

    if (!isControllerConnected()) {
        return false;
    }

    return true;
}

bool ControllerInterface::isControllerConnected()
{
    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(m_listenSocket, &readfds);

    log_debug() << "Waiting for Controller, timeout value is " << m_timeout << " seconds...";

    m_highestFd = m_listenSocket;

    struct timeval tv;
    tv.tv_sec = m_timeout;
    tv.tv_usec = 0;
    int ret = select(m_highestFd + 1, &readfds, 0, 0, &tv);
    if (ret == -1) {
        log_error() << "select:" << strerror(errno);
        return false;
    } else if (ret == 0) {
        log_error() << "select reached timeout";
        return false;
    }

    if (FD_ISSET(m_listenSocket, &readfds)) {
        socklen_t t = sizeof(m_remote);
        m_connectionSocket = accept(m_listenSocket, (struct sockaddr *)&m_remote, &t);
        if (m_connectionSocket == -1) {
            log_error() << "accept:" << strerror(errno);
            // TODO: We need to propagate failure here
            return false; // Disconnects the signal handler
        }

        log_debug() << "Accepted connection from Controller";
        m_connected = true;

        if (m_connectionSocket > m_highestFd) {
            m_highestFd = m_connectionSocket;
        }
        // We are connected, i.e. the Controller is there, now we should
        // be prepared to receive messages.
        sigc::slot<bool> checkForMessageSlot = sigc::mem_fun(this, &ControllerInterface::checkForMessage);
        sigc::connection conn = Glib::signal_timeout().connect(checkForMessageSlot, 10);
    }

    return true;
}

bool ControllerInterface::checkForMessage()
{
    char str[1024];
    memset(str, '\0', sizeof(char) * 1024);

    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 0;

    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(m_connectionSocket, &readfds);
    select(m_highestFd + 1, &readfds, 0, 0, &tv);

    if (FD_ISSET(m_connectionSocket, &readfds)) {
        int received = recv(m_connectionSocket, str, 100, 0);
        if (received == -1) {
            log_error() << "recv:" << strerror(errno);
            // TODO: Do we need to propagate failure here?
            // TODO: Should we continue waiting for messages after this?
            return false; // Disconnects the signal handler
        } else if (received == 0) {
            // Controller has shut down, there will be no more messages
            return false; // Disconnects the signal handler
        }

        log_debug() << "Pelagicontain received: " << str;
    }

    return true; // Continue to let the signal handler call this method
}

bool ControllerInterface::startApp()
{
    bool result = false;

    if (canSend()) {
        char msg[] = {'1', '\0'};

        log_debug("Sending: \"%s\"", msg);

        int ret = send(m_connectionSocket, msg, sizeof(msg), 0);
        if (ret == -1) {
            log_error() << "send:" << strerror(errno);
        } else {
            m_running = true;
            result = true;
        }
    }

    return result;
}

bool ControllerInterface::shutdown()
{
    bool result = false;

    if (canSend()) {
        char msg[] = {'2', '\0'};

        log_debug("Sending: \"%s\"", msg);

        int ret = send(m_connectionSocket, msg, sizeof(msg), 0);
        if (ret == -1) {
            log_error() << "send:" << strerror(errno);
        } else {
            m_running = false;
            result = true;
        }
    }

    return result;
}

bool ControllerInterface::setEnvironmentVariable(const std::string &variable,
                                                 const std::string &value)
{
    bool result = false;

    if (canSend()) {
        std::string command = "3 " + variable + " " + value;
        char msg[command.size() + 1];

        memcpy(msg, command.c_str(), command.size());
        msg[command.size()] = '\0';

        log_debug("Sending: \"%s\"", msg);

        int ret = send(m_connectionSocket, msg, strlen(msg) + 1, 0);
        if (ret == -1) {
            log_error() << "send:" << strerror(errno);
        } else {
            result = true;
        }
    }

    return result;
}

bool ControllerInterface::systemCall(const std::string &cmd)
{
    bool result = false;

    if (canSend()) {
        std::string command = "4 " + cmd;
        char msg[command.size() + 1];

        memcpy(msg, command.c_str(), command.size());
        msg[command.size()] = '\0';

        log_debug("Sending: \"%s\"", msg);

        int ret = send(m_connectionSocket, msg, strlen(msg) + 1, 0);
        if (ret == -1) {
            log_error() << "send:" << strerror(errno);
        } else {
            result = true;
        }
    }

    return result;
}

bool ControllerInterface::hasBeenStarted() const
{
    return m_running;
}

bool ControllerInterface::canSend()
{
    if (!m_connected) {
        log_error() << "No connection to Controller";
        return false;
    }

    fd_set sendfds;
    FD_ZERO(&sendfds);
    FD_SET(m_connectionSocket, &sendfds);

    int ret = select(m_highestFd + 1, 0, &sendfds, 0, &m_selectTimeout);
    if (ret == -1) {
        log_error() << "select:" << strerror(errno);
        return false;
    } else if (ret == 0) {
        log_error() << "select reached timeout";
        return false;
    }

    return FD_ISSET(m_connectionSocket, &sendfds) ? true : false;
}
<|endoftext|>


<commit_before>#include "photoeffects.hpp"
#include <iostream>

using namespace cv;
using namespace std;

const char *ORIGINAL_IMAGE="Original image";
const char *FILM_GRAIN_IMAGE="Film Grain image";
const char *helper =
"./filmGrain_sample <img> <value of grain>\n\
\t<img> - file name contained the processed image\n\
\t<value of grain> - degree graininess\n\
";
int processArguments(int argc, char **argv, Mat &img, int &grainValue);

int main(int argc, char** argv)
{

    Mat src;
    int grainValue;
    if (processArguments(argc, argv, src, grainValue) != 0)
    {
        cout << helper << endl;
        return 1;
    }
    namedWindow(ORIGINAL_IMAGE, CV_WINDOW_AUTOSIZE);
    imshow(ORIGINAL_IMAGE, src);
    Mat dst;
    filmGrain(src, dst, grainValue, RNG(0));
    imshow(FILM_GRAIN_IMAGE, dst);
    cout << "Press any key to EXIT"<<endl;
    waitKey(0);
    return 0;
}

int processArguments(int argc, char **argv, Mat &img, int &grainValue)
{
    if (argc < 3)
    {
        return 1;
    }
    img = imread(argv[1], CV_LOAD_IMAGE_COLOR);
    grainValue = atoi(argv[2]);
    return 0;
}
<commit_msg>some modification<commit_after>#include "photoeffects.hpp"
#include <iostream>

using namespace cv;
using namespace std;

const char *ORIGINAL_IMAGE="Original image";
const char *FILM_GRAIN_IMAGE="Film Grain image";
const char *helper =
"./filmGrain_sample <img> <value of grain>\n\
\t<img> - file name contained the processed image\n\
\t<value of grain> - degree graininess\n\
";
int processArguments(int argc, char **argv, Mat &img, int &grainValue);

int main(int argc, char** argv)
{

    Mat src;
    int grainValue;
    if (processArguments(argc, argv, src, grainValue) != 0)
    {
        cout << helper << endl;
        return 1;
    }
    namedWindow(ORIGINAL_IMAGE, CV_WINDOW_AUTOSIZE);
    imshow(ORIGINAL_IMAGE, src);
    Mat dst;
    RNG rng=RNG(0);
    filmGrain(src, dst, grainValue, rng);
    imshow(FILM_GRAIN_IMAGE, dst);
    cout << "Press any key to EXIT"<<endl;
    waitKey(0);
    return 0;
}

int processArguments(int argc, char **argv, Mat &img, int &grainValue)
{
    if (argc < 3)
    {
        return 1;
    }
    img = imread(argv[1], CV_LOAD_IMAGE_COLOR);
    grainValue = atoi(argv[2]);
    return 0;
}
<|endoftext|>