<commit_before>#include "shuf-t.h"
#include "utils.h"
#include <QDebug>

//global variables initialization

qint64 _param_buffer_size = 1024*1024*1024; //1 Gib
uint _param_start_line = 0;
uint _param_end_line = 0;
uint _param_header = 0;
uint _param_output_limit = 0;
bool _param_verbose = true;

QFile source_file;
QFile destination_file;
QString source_string;
QVector< Block > metadata;

bool is_temporary_file = false;
bool remove_trailing_empty_line = true;

uint readMetadata(QForkedTextStream& source_stream, const qint64 source_length)
{
    double progress_step = 0;

    if (_param_end_line > 0)
        progress_step = (double) _param_end_line / 10;  //progress by lines read
    else
        progress_step = (double) source_length   / 10;  //progress by bytes read

    metadata.clear();

    if (_param_end_line > 0)
    {
        metadata.reserve(_param_end_line - std::max(_param_start_line, (uint) 1) + 1);
    }

    double progress = 0;

    // scan file for line offsets
    qint64 pos_start = 0;
    uint lines_processed = 0;

    while (!source_stream.atEnd())
    {
        source_stream.readLine();

        qint64 pos_end = source_stream.getFastPos();
        if (_param_start_line <= ++lines_processed)
        {
            uint strlen = source_stream.getBytesRead();
            metadata.append( Block(pos_start, strlen) );
        }

        pos_start = pos_end;

        double current_progress;
        if (_param_end_line > 0)
           current_progress = lines_processed; // progress by lines read
        else
           current_progress = pos_start;       // progress by bytes read

            while (current_progress - progress > 1e-5)
            {
                print(".");
                progress += progress_step;

            }

            if(_param_end_line > 0 && lines_processed >= _param_end_line) break;

    }

    print("\n");

    if (remove_trailing_empty_line)
        if (metadata.count() > 0 && metadata.last().length == 0)
            metadata.removeLast();

    return 0;
}

//// Fisher–Yates shuffle
//// https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm

void shuffleMetadata()
{
    double blocks_processed = 1; // n-1 loops
    uint n = metadata.count();
    double progress_step = (double) n/10;
    double progress = 0;

    for (uint i = n-1; i > 0; i--)
    {
        uint j = rand() % (i+1);
        if (j != i)
        {
            Block b     = metadata[i];
            metadata[i] = metadata[j];
            metadata[j] = b;
        }

        blocks_processed++;
        while( blocks_processed - progress > 1e-5)
        {
            print(".");
            progress += progress_step;
        }
    }

    print("\n");
}

int writeData(QForkedTextStream& in_stream, QTextStream& out_stream)
{
    int result = -1;
    char * mem = (char*) malloc(_param_buffer_size);
    if(mem == NULL)
    {
        qCritical() << "can't allocate enough memory";
        return result;
    }

    QByteArray buffer;
    buffer = buffer.setRawData(mem, _param_buffer_size);

    in_stream.seek(0);

    QTextStream buffer_stream(&buffer, QIODevice::ReadWrite);

    // copy header from the beginning of input
    while (_param_header--)
        out_stream << in_stream.readLine() + "\n";

    // copy data
    uint total_blocks = metadata.count();
    uint blocks_left;
    if (_param_output_limit > 0)
        blocks_left = std::min(_param_output_limit, total_blocks);
    else
        blocks_left = total_blocks;

    double blocks_processed = 0;
    double progress_step = (double) total_blocks/10;
    double progress = 0;
    result = 0;

    while (result == 0 && blocks_left > 0)
    {
        QVector<Block2Buf> blocks_to_read;
        uint buffer_size_left = _param_buffer_size;

        uint blocks_marked = 0;

        // mark as many blocks as could fit our buffer

        while (blocks_marked < blocks_left)
        {
            Block& next_block = metadata[blocks_marked];
            uint block_len = next_block.length + 1; // +1 for "\n"

            if (block_len < buffer_size_left)
            {   // block still can fit the buffer
                blocks_to_read.append(Block2Buf(next_block.offset, _param_buffer_size-buffer_size_left));
                buffer_size_left -= block_len;

                blocks_marked++;
            } else
            {   // buffer is filled
                break;
            }
        }

        if(blocks_marked == 0)
        {   // nothing was marked
            if (blocks_left > 0)
            {   // but there was data to write
                print("error: block is bigger than buffer"); //need to return -1
                result = -1;
            }
            break;
        } else {
            metadata.remove(0, blocks_marked);
            blocks_left -= blocks_marked;
        }

        // sort blocks by offset toread them from input with one scan
        std::sort(blocks_to_read.begin(), blocks_to_read.end());

        // read blocks from input to buffer
        buffer_stream.seek(0);
        qint64 pos_buffer = 0;
        qint64 pos_input  = 0;
        //QHelperTextStream in_helper(in_stream);

        foreach (Block2Buf block, blocks_to_read)
        {
            if (pos_input != block.offset_read)
            {
                if(!in_stream.seek(block.offset_read)) //always forward
                {
                    result = -1;
                    break;
                }

                pos_input = block.offset_read;
            }

            if (pos_buffer != block.offset_write)
            {
                if (!buffer_stream.seek(block.offset_write)) //any direction
                {
                    result = -1;
                    break;
                }

                pos_buffer = block.offset_write;
            }

            buffer_stream << in_stream.readLine()+"\n";
            qint64 strlen = in_stream.getBytesRead();
            strlen += 1; //for '\n
            pos_buffer += strlen;
            pos_input = in_stream.getFastPos();
        }

        // save buffer to output
        buffer_stream.seek(0);
        pos_buffer = 0;
        qint64 buffer_size_used = _param_buffer_size - buffer_size_left;

        while (pos_buffer < buffer_size_used)
        {
            int read_len =  std::min(QTEXTSTREAM_BUFFERSIZE, (int) (buffer_size_used - pos_buffer));
            out_stream << buffer_stream.read(read_len);
            pos_buffer += read_len;
        }

        // progress by blocks
        blocks_processed = total_blocks - blocks_left;
        while (blocks_processed - progress > 1e-5)
        {
            progress += progress_step;
            print(".");
        }

    }

    print("\n");

    free(mem);

    return result;

}

uint openFileSource(QForkedTextStream& ts, const QString filename)
{
    source_file.close();
    source_file.setFileName(filename);
    int result = -1;
    if (source_file.open(QIODevice::ReadOnly))
    {
        ts.setDevice(&source_file);
        ts.seek(0);
        result = readMetadata(ts, source_file.size()-1);
    }

    return result;
}

QString readStdinToTmpFile()
{
   QString fname;
   QTemporaryFile f;
   f.setAutoRemove(false);
    if (f.open())
    {
        fname = f.fileName();
        QTextStream f_stream(&f);
        QFile f2;
        if (f2.open(stdin, QIODevice::ReadOnly))
        {
            QTextStream f2_stream(&f2);
            while(!f2_stream.atEnd())
            {
                f_stream << f2_stream.readLine() + '\n';
            }
            f2.close();

        } else
        {
            f.close();
            qCritical() << "ERROR: Can't open stdin";
            return QString();
        }

        f.close();
    } else {
        qCritical() << "ERROR: Can't create a temp file " + fname;
        return QString();
    }

   is_temporary_file = true;
   return fname;
}

void closeFileSource()
{
    if (!is_temporary_file)
            source_file.close();
    else
            source_file.remove();
}

uint openFileDestination(QTextStream& ts, const QString filename)
{
    destination_file.close();
    destination_file.setFileName(filename);
    int result = -1;
    if (destination_file.open(QIODevice::WriteOnly|QIODevice::Truncate))
    {
        ts.setDevice(&destination_file);
        ts.seek(0);
    }

    return result;
}

uint openInputRangeSource(QForkedTextStream& ts, uint range_min, uint range_max)
{
    source_string.clear();
    QStringList sl;
    for (uint i = range_min; i <= range_max; i++)
        sl.append(QString::number(i));
    source_string = sl.join("\n");
    ts.setString(&source_string, QIODevice::ReadOnly);
    return readMetadata(ts, source_string.data_ptr()->size);
}

void closeInputRangeSource()
{
    source_string.clear();
}

uint openStdOutDestination(QTextStream& ts)
{
   if (!destination_file.open(stdout, QIODevice::WriteOnly))
       return -1;

   ts.setDevice(&destination_file);
   return 0;
}

void closeFileDestination()
{
    destination_file.close();
}

<commit_msg>empty file fix<commit_after>#include "shuf-t.h"
#include "utils.h"
#include <QDebug>

//global variables initialization

qint64 _param_buffer_size = 1024*1024*1024; //1 Gib
uint _param_start_line = 0;
uint _param_end_line = 0;
uint _param_header = 0;
uint _param_output_limit = 0;
bool _param_verbose = true;

QFile source_file;
QFile destination_file;
QString source_string;
QVector< Block > metadata;

bool is_temporary_file = false;
bool remove_trailing_empty_line = true;

uint readMetadata(QForkedTextStream& source_stream, const qint64 source_length)
{
    double progress_step = 0;

    if (_param_end_line > 0)
        progress_step = (double) _param_end_line / 10;  //progress by lines read
    else
        progress_step = (double) source_length   / 10;  //progress by bytes read

    if (progress_step <= 0) progress_step = 1; //file length 0 or 1

    metadata.clear();

    if (_param_end_line > 0)
    {
        metadata.reserve(_param_end_line - std::max(_param_start_line, (uint) 1) + 1);
    }

    double progress = 0;

    // scan file for line offsets
    qint64 pos_start = 0;
    uint lines_processed = 0;

    while (!source_stream.atEnd())
    {
        source_stream.readLine();

        qint64 pos_end = source_stream.getFastPos();
        if (_param_start_line <= ++lines_processed)
        {
            uint strlen = source_stream.getBytesRead();
            metadata.append( Block(pos_start, strlen) );
        }

        pos_start = pos_end;

        double current_progress;
        if (_param_end_line > 0)
           current_progress = lines_processed; // progress by lines read
        else
           current_progress = pos_start;       // progress by bytes read

            while (current_progress - progress > 1e-5)
            {
                print(".");
                progress += progress_step;

            }

            if(_param_end_line > 0 && lines_processed >= _param_end_line) break;

    }

    print("\n");

    if (remove_trailing_empty_line)
        if (metadata.count() > 0 && metadata.last().length == 0)
            metadata.removeLast();

    return 0;
}

//// Fisher–Yates shuffle
//// https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm

void shuffleMetadata()
{
    double blocks_processed = 1; // n-1 loops
    uint n = metadata.count();
    double progress_step = (double) n/10;
    double progress = 0;

    if (n > 0)
    for (uint i = n-1; i > 0; i--)
    {
        uint j = rand() % (i+1);
        if (j != i)
        {
            Block b     = metadata[i];
            metadata[i] = metadata[j];
            metadata[j] = b;
        }

        blocks_processed++;
        while( blocks_processed - progress > 1e-5)
        {
            print(".");
            progress += progress_step;
        }
    }

    print("\n");
}

int writeData(QForkedTextStream& in_stream, QTextStream& out_stream)
{
    int result = -1;
    char * mem = (char*) malloc(_param_buffer_size);
    if(mem == NULL)
    {
        qCritical() << "can't allocate enough memory";
        return result;
    }

    QByteArray buffer;
    buffer = buffer.setRawData(mem, _param_buffer_size);

    in_stream.seek(0);

    QTextStream buffer_stream(&buffer, QIODevice::ReadWrite);

    // copy header from the beginning of input
    while (_param_header--)
        out_stream << in_stream.readLine() + "\n";

    // copy data
    uint total_blocks = metadata.count();
    uint blocks_left;
    if (_param_output_limit > 0)
        blocks_left = std::min(_param_output_limit, total_blocks);
    else
        blocks_left = total_blocks;

    double blocks_processed = 0;
    double progress_step = (double) total_blocks/10;
    double progress = 0;
    result = 0;

    while (result == 0 && blocks_left > 0)
    {
        QVector<Block2Buf> blocks_to_read;
        uint buffer_size_left = _param_buffer_size;

        uint blocks_marked = 0;

        // mark as many blocks as could fit our buffer

        while (blocks_marked < blocks_left)
        {
            Block& next_block = metadata[blocks_marked];
            uint block_len = next_block.length + 1; // +1 for "\n"

            if (block_len < buffer_size_left)
            {   // block still can fit the buffer
                blocks_to_read.append(Block2Buf(next_block.offset, _param_buffer_size-buffer_size_left));
                buffer_size_left -= block_len;

                blocks_marked++;
            } else
            {   // buffer is filled
                break;
            }
        }

        if(blocks_marked == 0)
        {   // nothing was marked
            if (blocks_left > 0)
            {   // but there was data to write
                print("error: block is bigger than buffer"); //need to return -1
                result = -1;
            }
            break;
        } else {
            metadata.remove(0, blocks_marked);
            blocks_left -= blocks_marked;
        }

        // sort blocks by offset toread them from input with one scan
        std::sort(blocks_to_read.begin(), blocks_to_read.end());

        // read blocks from input to buffer
        buffer_stream.seek(0);
        qint64 pos_buffer = 0;
        qint64 pos_input  = 0;
        //QHelperTextStream in_helper(in_stream);

        foreach (Block2Buf block, blocks_to_read)
        {
            if (pos_input != block.offset_read)
            {
                if(!in_stream.seek(block.offset_read)) //always forward
                {
                    result = -1;
                    break;
                }

                pos_input = block.offset_read;
            }

            if (pos_buffer != block.offset_write)
            {
                if (!buffer_stream.seek(block.offset_write)) //any direction
                {
                    result = -1;
                    break;
                }

                pos_buffer = block.offset_write;
            }

            buffer_stream << in_stream.readLine()+"\n";
            qint64 strlen = in_stream.getBytesRead();
            strlen += 1; //for '\n
            pos_buffer += strlen;
            pos_input = in_stream.getFastPos();
        }

        // save buffer to output
        buffer_stream.seek(0);
        pos_buffer = 0;
        qint64 buffer_size_used = _param_buffer_size - buffer_size_left;

        while (pos_buffer < buffer_size_used)
        {
            int read_len =  std::min(QTEXTSTREAM_BUFFERSIZE, (int) (buffer_size_used - pos_buffer));
            out_stream << buffer_stream.read(read_len);
            pos_buffer += read_len;
        }

        // progress by blocks
        blocks_processed = total_blocks - blocks_left;
        while (blocks_processed - progress > 1e-5)
        {
            progress += progress_step;
            print(".");
        }

    }

    print("\n");

    free(mem);

    return result;

}

uint openFileSource(QForkedTextStream& ts, const QString filename)
{
    source_file.close();
    source_file.setFileName(filename);
    int result = -1;
    if (source_file.open(QIODevice::ReadOnly))
    {
        ts.setDevice(&source_file);
        ts.seek(0);
        result = readMetadata(ts, source_file.size()-1);
    }

    return result;
}

QString readStdinToTmpFile()
{
   QString fname;
   QTemporaryFile f;
   f.setAutoRemove(false);
    if (f.open())
    {
        fname = f.fileName();
        QTextStream f_stream(&f);
        QFile f2;
        if (f2.open(stdin, QIODevice::ReadOnly))
        {
            QTextStream f2_stream(&f2);
            while(!f2_stream.atEnd())
            {
                f_stream << f2_stream.readLine() + '\n';
            }
            f2.close();

        } else
        {
            f.close();
            qCritical() << "ERROR: Can't open stdin";
            return QString();
        }

        f.close();
    } else {
        qCritical() << "ERROR: Can't create a temp file " + fname;
        return QString();
    }

   is_temporary_file = true;
   return fname;
}

void closeFileSource()
{
    if (!is_temporary_file)
            source_file.close();
    else
            source_file.remove();
}

uint openFileDestination(QTextStream& ts, const QString filename)
{
    destination_file.close();
    destination_file.setFileName(filename);
    int result = -1;
    if (destination_file.open(QIODevice::WriteOnly|QIODevice::Truncate))
    {
        ts.setDevice(&destination_file);
        ts.seek(0);
    }

    return result;
}

uint openInputRangeSource(QForkedTextStream& ts, uint range_min, uint range_max)
{
    source_string.clear();
    QStringList sl;
    for (uint i = range_min; i <= range_max; i++)
        sl.append(QString::number(i));
    source_string = sl.join("\n");
    ts.setString(&source_string, QIODevice::ReadOnly);
    return readMetadata(ts, source_string.data_ptr()->size);
}

void closeInputRangeSource()
{
    source_string.clear();
}

uint openStdOutDestination(QTextStream& ts)
{
   if (!destination_file.open(stdout, QIODevice::WriteOnly))
       return -1;

   ts.setDevice(&destination_file);
   return 0;
}

void closeFileDestination()
{
    destination_file.close();
}

<|endoftext|>


<commit_before>/*!
 * Copyright (c) 2017 by Contributors
 * \file main.cpp
 * \brief model-converter main
 * \author HPI-DeepLearning
*/
#include <stdio.h>
#include <libgen.h>
#include <fstream>

#include <mxnet/ndarray.h>

#include "../src/xnor_cpu.h"

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"

using mxnet::op::xnor_cpu::BITS_PER_BINARY_WORD;
using mxnet::op::xnor_cpu::BINARY_WORD;

void convert_to_binary(mxnet::NDArray& array) {
  assert(mshadow::mshadow_sizeof(array.dtype()) == sizeof(BINARY_WORD));
  assert(array.shape().ndim() == 4); // adjust for FC, flatten
  assert(array.shape()[1] % BITS_PER_BINARY_WORD == 0);
  nnvm::TShape binarized_shape(1);
  int size = array.shape().Size();
  binarized_shape[0] = size / BITS_PER_BINARY_WORD;
  mxnet::NDArray temp(binarized_shape, mxnet::Context::CPU(), false, array.dtype());
  mxnet::op::xnor_cpu::get_binary_row((float*) array.data().dptr_, (BINARY_WORD*) temp.data().dptr_, size);
  array = temp;
}

int convert_params_file(const std::string& input_file, const std::string& output_file) {
  std::vector<mxnet::NDArray> data;
  std::vector<std::string> keys;

  std::cout << "loading " << input_file << "..." << std::endl;
  { // loading params file into data and keys
    std::unique_ptr<dmlc::Stream> fi(dmlc::Stream::Create(input_file.c_str(), "r"));
    mxnet::NDArray::Load(fi.get(), &data, &keys);
  }

  const std::string filter_string("qconvolution");
  auto containsSubString = [filter_string](std::string s) {
    return s.find(filter_string) != std::string::npos;}; //@todo: add FC

  auto iter = std::find_if(keys.begin(),
                           keys.end(),
                           containsSubString);

  //Use a while loop, checking whether iter is at the end of myVector
  //Do a find_if starting at the item after iter, std::next(iter)
  while (iter != keys.end())
  {
    std::cout << "|- converting weights " << *iter << "..." << std::endl;
    convert_to_binary(data[iter - keys.begin()]);
    iter = std::find_if(std::next(iter),
                        keys.end(),
                        containsSubString);
  }

  { // saving params back to *_converted
    std::unique_ptr<dmlc::Stream> fo(dmlc::Stream::Create(output_file.c_str(), "w"));
    mxnet::NDArray::Save(fo.get(), data, keys);
  }
  std::cout << "wrote converted params to " << output_file << std::endl;
  return 0;
}

int convert_json_file(const std::string& input_fname, const std::string& output_fname) {
  std::cout << "loading " << input_fname << "..." << std::endl;
  std::string json;
  {
    std::ifstream stream(input_fname);
    if (!stream.is_open()) {
      std::cout << "cant find json file at " + input_fname << std::endl;
      return -1;
    }
    std::stringstream buffer;
    buffer << stream.rdbuf();
    json = buffer.str();
  }

  rapidjson::Document d;
  d.Parse(json.c_str());

  assert(d.HasMember("nodes"));
  rapidjson::Value& nodes = d["nodes"];
  assert(nodes.IsArray());

  for (rapidjson::Value::ValueIterator itr = nodes.Begin(); itr != nodes.End(); ++itr) { // @todo: add FC
    if (!itr->HasMember("op") || !(*itr)["op"].IsString() || std::strcmp((*itr)["op"].GetString(), "QConvolution") != 0) {
      continue;
    }

    assert((*itr).HasMember("attr"));
    rapidjson::Value& op_attributes = (*itr)["attr"];
    op_attributes.AddMember("binarized_weights_only", "True", d.GetAllocator());

    assert((*itr).HasMember("name"));
    std::cout << "|- adjusting attributes for " << (*itr)["name"].GetString() << std::endl;
  }

  rapidjson::StringBuffer buffer;
  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  d.Accept(writer);

  {
    std::ofstream stream(output_fname);
    if (!stream.is_open()) {
      std::cout << "cant find json file at " + output_fname << std::endl;
      return -1;
    }
    std::string output = buffer.GetString();
    stream << output;
    stream.close();
  }

  std::cout << "wrote converted json to " << output_fname << std::endl;

  return 0;
}

int main(int argc, char ** argv){
  if (argc != 2) {
    std::cout << "usage: " + std::string(argv[0]) + " <mxnet *.params file>" << std::endl;
    return -1;
  }

  const std::string params_file(argv[1]);

  const std::string path(dirname(argv[1]));
  const std::string params_file_name(basename(argv[1]));
  std::string base_name = params_file_name;
  base_name.erase(base_name.rfind('-')); // watchout if no '-'
  const std::string output_name(path + "/" + "binarized_" + params_file_name);

  if (convert_params_file(params_file, output_name) != 0) {
    return -1;
  }

  const std::string json_file_name(path + "/"                + base_name + "-symbol.json");
  const std::string json_out_fname(path + "/" + "binarized_" + base_name + "-symbol.json");

  if (convert_json_file(json_file_name, json_out_fname) != 0) {
    return -1;
  }

  return 0;
}<commit_msg>minor improvements on help message and data type of size variable<commit_after>/*!
 * Copyright (c) 2017 by Contributors
 * \file main.cpp
 * \brief model-converter main
 * \author HPI-DeepLearning
*/
#include <stdio.h>
#include <libgen.h>
#include <fstream>

#include <mxnet/ndarray.h>

#include "../src/xnor_cpu.h"

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"

using mxnet::op::xnor_cpu::BITS_PER_BINARY_WORD;
using mxnet::op::xnor_cpu::BINARY_WORD;

void convert_to_binary(mxnet::NDArray& array) {
  assert(mshadow::mshadow_sizeof(array.dtype()) == sizeof(BINARY_WORD));
  assert(array.shape().ndim() == 4); // adjust for FC, flatten
  assert(array.shape()[1] % BITS_PER_BINARY_WORD == 0);
  nnvm::TShape binarized_shape(1);
  size_t size = array.shape().Size();
  binarized_shape[0] = size / BITS_PER_BINARY_WORD;
  mxnet::NDArray temp(binarized_shape, mxnet::Context::CPU(), false, array.dtype());
  mxnet::op::xnor_cpu::get_binary_row((float*) array.data().dptr_, (BINARY_WORD*) temp.data().dptr_, size);
  array = temp;
}

int convert_params_file(const std::string& input_file, const std::string& output_file) {
  std::vector<mxnet::NDArray> data;
  std::vector<std::string> keys;

  std::cout << "loading " << input_file << "..." << std::endl;
  { // loading params file into data and keys
    std::unique_ptr<dmlc::Stream> fi(dmlc::Stream::Create(input_file.c_str(), "r"));
    mxnet::NDArray::Load(fi.get(), &data, &keys);
  }

  const std::string filter_string("qconvolution");
  auto containsSubString = [filter_string](std::string s) {
    return s.find(filter_string) != std::string::npos;}; //@todo: add FC

  auto iter = std::find_if(keys.begin(),
                           keys.end(),
                           containsSubString);

  //Use a while loop, checking whether iter is at the end of myVector
  //Do a find_if starting at the item after iter, std::next(iter)
  while (iter != keys.end())
  {
    std::cout << "|- converting weights " << *iter << "..." << std::endl;
    convert_to_binary(data[iter - keys.begin()]);
    iter = std::find_if(std::next(iter),
                        keys.end(),
                        containsSubString);
  }

  { // saving params back to *_converted
    std::unique_ptr<dmlc::Stream> fo(dmlc::Stream::Create(output_file.c_str(), "w"));
    mxnet::NDArray::Save(fo.get(), data, keys);
  }
  std::cout << "wrote converted params to " << output_file << std::endl;
  return 0;
}

int convert_json_file(const std::string& input_fname, const std::string& output_fname) {
  std::cout << "loading " << input_fname << "..." << std::endl;
  std::string json;
  {
    std::ifstream stream(input_fname);
    if (!stream.is_open()) {
      std::cout << "cant find json file at " + input_fname << std::endl;
      return -1;
    }
    std::stringstream buffer;
    buffer << stream.rdbuf();
    json = buffer.str();
  }

  rapidjson::Document d;
  d.Parse(json.c_str());

  assert(d.HasMember("nodes"));
  rapidjson::Value& nodes = d["nodes"];
  assert(nodes.IsArray());

  for (rapidjson::Value::ValueIterator itr = nodes.Begin(); itr != nodes.End(); ++itr) { // @todo: add FC
    if (!itr->HasMember("op") || !(*itr)["op"].IsString() || std::strcmp((*itr)["op"].GetString(), "QConvolution") != 0) {
      continue;
    }

    assert((*itr).HasMember("attr"));
    rapidjson::Value& op_attributes = (*itr)["attr"];
    op_attributes.AddMember("binarized_weights_only", "True", d.GetAllocator());

    assert((*itr).HasMember("name"));
    std::cout << "|- adjusting attributes for " << (*itr)["name"].GetString() << std::endl;
  }

  rapidjson::StringBuffer buffer;
  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  d.Accept(writer);

  {
    std::ofstream stream(output_fname);
    if (!stream.is_open()) {
      std::cout << "cant find json file at " + output_fname << std::endl;
      return -1;
    }
    std::string output = buffer.GetString();
    stream << output;
    stream.close();
  }

  std::cout << "wrote converted json to " << output_fname << std::endl;

  return 0;
}

int main(int argc, char ** argv){
  if (argc != 2) {
    std::cout << "usage: " + std::string(argv[0]) + " <mxnet *.params file>" << std::endl;
    std::cout << "  will binarize the weights of the Convolutional Layers of your model," << std::endl;
    std::cout << "  pack 32 values into one and save the result with the prefix 'binarized_'" << std::endl;
    return -1;
  }

  const std::string params_file(argv[1]);

  const std::string path(dirname(argv[1]));
  const std::string params_file_name(basename(argv[1]));
  std::string base_name = params_file_name;
  base_name.erase(base_name.rfind('-')); // watchout if no '-'
  const std::string output_name(path + "/" + "binarized_" + params_file_name);

  if (convert_params_file(params_file, output_name) != 0) {
    return -1;
  }

  const std::string json_file_name(path + "/"                + base_name + "-symbol.json");
  const std::string json_out_fname(path + "/" + "binarized_" + base_name + "-symbol.json");

  if (convert_json_file(json_file_name, json_out_fname) != 0) {
    return -1;
  }

  return 0;
}<|endoftext|>


<commit_before>/**
 * Exact Nearest Neigbor by linear scan.
 *
 * Usage: [filename] R data_set_file query_set_file
 */

#include <iostream>

using namespace std;

int main(int argc, char** argv) {
  if (argc < 4) {
    cout << "Usage: " << argv[0] << " R data_set_file query_set_file" << endl;
    exit(1);
  }

  cout << "exact LSH using linear scan!" << endl;

  return 0;
}
<commit_msg>Done with exact NN using linear scan<commit_after>/**
 * Exact Nearest Neigbor by linear scan.
 *
 * Usage: [filename] R data_set_file query_set_file
 */

#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

using HamT = long long; // binary features in hamming space

HamT toHamT(const string& line) {
  HamT num = 0;
  int d = line.length()/2;
  for (int i = 0; i < d; i++) {
    if (line[2*i]-'0' == 1) {
      num += pow(2, d-i-1);
    }
  }
  return num;
}

const string conv_to_string(const HamT h, const int d) {
  string s("");
  HamT num = h;

  for (int i = 0; i < d; i++) {
    char ch;
    if (num % 2 == 0) ch = '0';
    else ch = '1';
    s = ch + s;

    num /= 2;
    if (i < d-1) s = ' ' + s;
  }

  return s;
}

int hamTDistance(const HamT h1, const HamT h2) {
  int dist = 0;
  long long distNum = h1 ^ h2;
  while (distNum != 0) {
    distNum &= distNum-1;
    dist++;
  }
  return dist;
}

int main(int argc, char** argv) {
  if (argc < 4) {
    cout << "Usage: " << argv[0] << " R data_set_file query_set_file" << endl;
    exit(1);
  }

  int R = stoi(argv[1]);
  int d = 0;

  string line;
  ifstream dataset(argv[2]);
  if (!dataset.is_open()) {
    cout << "unable to open data set file: " << argv[2] << endl;
    exit(1);
  }
  vector<HamT> datapoints;
  while (getline(dataset, line)) {
    datapoints.push_back(toHamT(line));
    if (d == 0) {
      for (auto ch : line) {
        if (ch == '0' || ch == '1') d++;
      }
    }
  }
  dataset.close();

  ifstream queryset(argv[3]);
  if (!queryset.is_open()) {
    cout << "unable to open query set file: " << argv[3] << endl;
    exit(1);
  }
  vector<HamT> querypoints;
  while (getline(queryset, line)) {
    querypoints.push_back(toHamT(line));
  }
  queryset.close();

  for (auto qpoint : querypoints) {
    int count = 0;
    cout << "NNs (R=" << R << ") for "
         << conv_to_string(qpoint, d) << " :" << endl;
    for (auto dpoint : datapoints) {
      if (hamTDistance(qpoint, dpoint) <= R) {
        cout << conv_to_string(dpoint, d) << endl;
        count++;
      }
    }
    cout << "Total NNs for " << conv_to_string(qpoint, d)
         << " : " << count << endl;
    // cout << "Total time for R-NN query: " << endl;
  }

  return 0;
}
<|endoftext|>


<commit_before>/// HEADER
#include <utils_param/set_parameter.h>

using namespace param;

SetParameter::SetParameter()
    : Parameter("noname")
{
}

SetParameter::SetParameter(const std::string &name)
    : Parameter(name)
{
}

SetParameter::~SetParameter()
{

}

void SetParameter::setByName(const std::string &name)
{
    for(std::vector< std::pair<std::string, variant> >::iterator it = set_.begin(); it != set_.end(); ++it) {
        if(it->first == name) {
            value_ = it->second;
            return;
        }
    }

    throw std::runtime_error(std::string("no such parameter: ") + name);
}

int SetParameter::noParameters() const
{
    return set_.size();
}

std::string SetParameter::convertToString(const variant &v) const
{
    if(v.type() == typeid(std::string)) {
        return boost::any_cast<std::string> (v);
    }

    std::stringstream ss;
    if(v.type() == typeid(int)) {
        ss << boost::any_cast<int> (v);

    } else if(v.type() == typeid(double)) {
        ss << boost::any_cast<double> (v);

    } else if(v.type() == typeid(bool)) {
        ss << boost::any_cast<bool> (v);

    } else {
        throw std::runtime_error("unsupported type");
    }

    return ss.str();
}

std::string SetParameter::getName(int idx) const
{
    return set_[idx].first;
}

std::string SetParameter::getName() const
{
    std::string str =  convertToString(value_);
    for(std::vector< std::pair<std::string, variant> >::const_iterator it = set_.begin(); it != set_.end(); ++it) {
        if(convertToString(it->second) == str) {
            return it->first;
        }
    }

    throw std::runtime_error("cannot get the name for parameter '" + name() + "'");
}

const std::type_info& SetParameter::type() const
{
    return value_.type();
}

std::string SetParameter::toStringImpl() const
{
    return std::string("[set: ") + convertToString(value_)  + "]";
}

boost::any SetParameter::get_unsafe() const
{
    return value_;
}

void SetParameter::set_unsafe(const boost::any &v)
{
    value_ = v;
}

void SetParameter::setFrom(const Parameter &other)
{
    const SetParameter* range = dynamic_cast<const SetParameter*>(&other);
    if(range) {
        value_ = range->value_;
        parameter_changed(this);
    } else {
        throw std::runtime_error("bad setFrom, invalid types");
    }
}

void SetParameter::write(YAML::Emitter& e) const
{
    e << YAML::BeginMap;
    e << YAML::Key << "name" << YAML::Value << name();
    e << YAML::Key << "type" << YAML::Value << "set";

    if(value_.type() == typeid(int)) {
        e << YAML::Key << "int" << YAML::Value << boost::any_cast<int> (value_);

    } else if(value_.type() == typeid(double)) {
        e << YAML::Key << "double" << YAML::Value << boost::any_cast<double> (value_);

    } else if(value_.type() == typeid(bool)) {
        e << YAML::Key << "bool" << YAML::Value << boost::any_cast<bool> (value_);

    } else if(value_.type() == typeid(std::string)) {
        e << YAML::Key << "string" << YAML::Value << boost::any_cast<std::string> (value_);
    }

    e << YAML::EndMap;
}

namespace {
template <typename T>
T __read(const YAML::Node& n) {
    T v;
    n >> v;
    return v;
}
}

void SetParameter::read(const YAML::Node& n)
{
    if(!n.FindValue("name")) {
        return;
    }

    n["name"] >> name_;

    if(n.FindValue("int")) {
        value_ = __read<int>(n["int"]);

    } else if(n.FindValue("double")) {
        value_ = __read<double>(n["double"]);

    } else if(n.FindValue("bool")) {
        value_ = __read<bool>(n["bool"]);

    } else if(n.FindValue("string")) {
        value_ = __read<std::string>(n["string"]);
    }
}
<commit_msg>callback bug fixed<commit_after>/// HEADER
#include <utils_param/set_parameter.h>

using namespace param;

SetParameter::SetParameter()
    : Parameter("noname")
{
}

SetParameter::SetParameter(const std::string &name)
    : Parameter(name)
{
}

SetParameter::~SetParameter()
{

}

void SetParameter::setByName(const std::string &name)
{
    for(std::vector< std::pair<std::string, variant> >::iterator it = set_.begin(); it != set_.end(); ++it) {
        if(it->first == name) {
            value_ = it->second;
            parameter_changed(this);
            return;
        }
    }

    throw std::runtime_error(std::string("no such parameter: ") + name);
}

int SetParameter::noParameters() const
{
    return set_.size();
}

std::string SetParameter::convertToString(const variant &v) const
{
    if(v.type() == typeid(std::string)) {
        return boost::any_cast<std::string> (v);
    }

    std::stringstream ss;
    if(v.type() == typeid(int)) {
        ss << boost::any_cast<int> (v);

    } else if(v.type() == typeid(double)) {
        ss << boost::any_cast<double> (v);

    } else if(v.type() == typeid(bool)) {
        ss << boost::any_cast<bool> (v);

    } else {
        throw std::runtime_error("unsupported type");
    }

    return ss.str();
}

std::string SetParameter::getName(int idx) const
{
    return set_[idx].first;
}

std::string SetParameter::getName() const
{
    std::string str =  convertToString(value_);
    for(std::vector< std::pair<std::string, variant> >::const_iterator it = set_.begin(); it != set_.end(); ++it) {
        if(convertToString(it->second) == str) {
            return it->first;
        }
    }

    throw std::runtime_error("cannot get the name for parameter '" + name() + "'");
}

const std::type_info& SetParameter::type() const
{
    return value_.type();
}

std::string SetParameter::toStringImpl() const
{
    return std::string("[set: ") + convertToString(value_)  + "]";
}

boost::any SetParameter::get_unsafe() const
{
    return value_;
}

void SetParameter::set_unsafe(const boost::any &v)
{
    value_ = v;
}

void SetParameter::setFrom(const Parameter &other)
{
    const SetParameter* range = dynamic_cast<const SetParameter*>(&other);
    if(range) {
        value_ = range->value_;
        parameter_changed(this);
    } else {
        throw std::runtime_error("bad setFrom, invalid types");
    }
}

void SetParameter::write(YAML::Emitter& e) const
{
    e << YAML::BeginMap;
    e << YAML::Key << "name" << YAML::Value << name();
    e << YAML::Key << "type" << YAML::Value << "set";

    if(value_.type() == typeid(int)) {
        e << YAML::Key << "int" << YAML::Value << boost::any_cast<int> (value_);

    } else if(value_.type() == typeid(double)) {
        e << YAML::Key << "double" << YAML::Value << boost::any_cast<double> (value_);

    } else if(value_.type() == typeid(bool)) {
        e << YAML::Key << "bool" << YAML::Value << boost::any_cast<bool> (value_);

    } else if(value_.type() == typeid(std::string)) {
        e << YAML::Key << "string" << YAML::Value << boost::any_cast<std::string> (value_);
    }

    e << YAML::EndMap;
}

namespace {
template <typename T>
T __read(const YAML::Node& n) {
    T v;
    n >> v;
    return v;
}
}

void SetParameter::read(const YAML::Node& n)
{
    if(!n.FindValue("name")) {
        return;
    }

    n["name"] >> name_;

    if(n.FindValue("int")) {
        value_ = __read<int>(n["int"]);

    } else if(n.FindValue("double")) {
        value_ = __read<double>(n["double"]);

    } else if(n.FindValue("bool")) {
        value_ = __read<bool>(n["bool"]);

    } else if(n.FindValue("string")) {
        value_ = __read<std::string>(n["string"]);
    }
}
<|endoftext|>


<commit_before>//
// Game.cpp
//

#include "pch.h"
#include "Game.h"

using namespace DirectX;

using Microsoft::WRL::ComPtr;

Game::Game()
{
    m_deviceResources = std::make_unique<DX::DeviceResources>();
    m_deviceResources->RegisterDeviceNotify(this);
}

// Initialize the Direct3D resources required to run.
void Game::Initialize(HWND window, int width, int height)
{
    m_deviceResources->SetWindow(window, width, height);

    m_deviceResources->CreateDeviceResources();
    CreateDeviceDependentResources();

    m_deviceResources->CreateWindowSizeDependentResources();
    CreateWindowSizeDependentResources();

    // TODO: Change the timer settings if you want something other than the default variable timestep mode.
    // e.g. for 60 FPS fixed timestep update logic, call:
    /*
    m_timer.SetFixedTimeStep(true);
    m_timer.SetTargetElapsedSeconds(1.0 / 60);
    */
}

// Executes the basic game loop.
void Game::Tick()
{
    m_timer.Tick([&]()
    {
        Update(m_timer);
    });

    Render();
}

// Updates the world.
void Game::Update(DX::StepTimer const& timer)
{
    float elapsedTime = float(timer.GetElapsedSeconds());

    // TODO: Add your game logic here.
    elapsedTime;
}

// Draws the scene.
void Game::Render()
{
    // Don't try to render anything before the first Update.
    if (m_timer.GetFrameCount() == 0)
    {
        return;
    }

    Clear();

    // TODO: Add your rendering code here.

    m_deviceResources->Present();
}

// Helper method to clear the back buffers.
void Game::Clear()
{
    // Clear the views
    auto context = m_deviceResources->GetD3DDeviceContext();
    auto renderTarget = m_deviceResources->GetBackBufferRenderTargetView();
    auto depthStencil = m_deviceResources->GetDepthStencilView();

    context->ClearRenderTargetView(renderTarget, Colors::CornflowerBlue);
    context->ClearDepthStencilView(depthStencil, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
    context->OMSetRenderTargets(1, &renderTarget, depthStencil);

    // Set the viewport.
    auto viewport = m_deviceResources->GetScreenViewport();
    context->RSSetViewports(1, &viewport);
}

// Message handlers
void Game::OnActivated()
{
    // TODO: Game is becoming active window.
}

void Game::OnDeactivated()
{
    // TODO: Game is becoming background window.
}

void Game::OnSuspending()
{
    // TODO: Game is being power-suspended (or minimized).
}

void Game::OnResuming()
{
    m_timer.ResetElapsedTime();

    // TODO: Game is being power-resumed (or returning from minimize).
}

void Game::OnWindowSizeChanged(int width, int height)
{
    if (!m_deviceResources->WindowSizeChanged(width, height))
        return;

    CreateWindowSizeDependentResources();

    // TODO: Game window is being resized.
}

// Properties
void Game::GetDefaultSize(int& width, int& height) const
{
    // TODO: Change to desired default window size (note minimum size is 320x200).
    width = 800;
    height = 600;
}

// These are the resources that depend on the device.
void Game::CreateDeviceDependentResources()
{
    // TODO: Initialize device dependent objects here (independent of window size).
}

// Allocate all memory resources that change on a window SizeChanged event.
void Game::CreateWindowSizeDependentResources()
{
    // TODO: Initialize windows-size dependent objects here.
}

void Game::OnDeviceLost()
{
    // TODO: Add Direct3D resource cleanup here.
}

void Game::OnDeviceRestored()
{
    CreateDeviceDependentResources();

    CreateWindowSizeDependentResources();
}<commit_msg>Fix for 2013 templates<commit_after>//
// Game.cpp
//

#include "pch.h"
#include "Game.h"

using namespace DirectX;

using Microsoft::WRL::ComPtr;

Game::Game()
{
    m_deviceResources = std::make_unique<DX::DeviceResources>();
    m_deviceResources->RegisterDeviceNotify(this);
}

// Initialize the Direct3D resources required to run.
void Game::Initialize(HWND window, int width, int height)
{
    m_deviceResources->SetWindow(window, width, height);

    m_deviceResources->CreateDeviceResources();
    CreateDeviceDependentResources();

    m_deviceResources->CreateWindowSizeDependentResources();
    CreateWindowSizeDependentResources();

    // TODO: Change the timer settings if you want something other than the default variable timestep mode.
    // e.g. for 60 FPS fixed timestep update logic, call:
    /*
    m_timer.SetFixedTimeStep(true);
    m_timer.SetTargetElapsedSeconds(1.0 / 60);
    */
}

// Executes the basic game loop.
void Game::Tick()
{
    m_timer.Tick([&]()
    {
        Update(m_timer);
    });

    Render();
}

// Updates the world.
void Game::Update(DX::StepTimer const& timer)
{
    float elapsedTime = float(timer.GetElapsedSeconds());

    // TODO: Add your game logic here.
    elapsedTime;
}

// Draws the scene.
void Game::Render()
{
    // Don't try to render anything before the first Update.
    if (m_timer.GetFrameCount() == 0)
    {
        return;
    }

    Clear();

    auto context = m_deviceResources->GetD3DDeviceContext();

    // TODO: Add your rendering code here.
    context;

    m_deviceResources->Present();
}

// Helper method to clear the back buffers.
void Game::Clear()
{
    // Clear the views
    auto context = m_deviceResources->GetD3DDeviceContext();
    auto renderTarget = m_deviceResources->GetBackBufferRenderTargetView();
    auto depthStencil = m_deviceResources->GetDepthStencilView();

    context->ClearRenderTargetView(renderTarget, Colors::CornflowerBlue);
    context->ClearDepthStencilView(depthStencil, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
    context->OMSetRenderTargets(1, &renderTarget, depthStencil);

    // Set the viewport.
    auto viewport = m_deviceResources->GetScreenViewport();
    context->RSSetViewports(1, &viewport);
}

// Message handlers
void Game::OnActivated()
{
    // TODO: Game is becoming active window.
}

void Game::OnDeactivated()
{
    // TODO: Game is becoming background window.
}

void Game::OnSuspending()
{
    // TODO: Game is being power-suspended (or minimized).
}

void Game::OnResuming()
{
    m_timer.ResetElapsedTime();

    // TODO: Game is being power-resumed (or returning from minimize).
}

void Game::OnWindowSizeChanged(int width, int height)
{
    if (!m_deviceResources->WindowSizeChanged(width, height))
        return;

    CreateWindowSizeDependentResources();

    // TODO: Game window is being resized.
}

// Properties
void Game::GetDefaultSize(int& width, int& height) const
{
    // TODO: Change to desired default window size (note minimum size is 320x200).
    width = 800;
    height = 600;
}

// These are the resources that depend on the device.
void Game::CreateDeviceDependentResources()
{
    auto device = m_deviceResources->GetD3DDevice();

    // TODO: Initialize device dependent objects here (independent of window size).
    device;
}

// Allocate all memory resources that change on a window SizeChanged event.
void Game::CreateWindowSizeDependentResources()
{
    // TODO: Initialize windows-size dependent objects here.
}

void Game::OnDeviceLost()
{
    // TODO: Add Direct3D resource cleanup here.
}

void Game::OnDeviceRestored()
{
    CreateDeviceDependentResources();

    CreateWindowSizeDependentResources();
}<|endoftext|>


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

#include <iostream>
#include <memory>
#include <string>

// include ROS 1
#include "ros/message.h"
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-parameter"
#endif
#include "ros/ros.h"
#include "std_msgs/String.h"
#ifdef __clang__
# pragma clang diagnostic pop
#endif

// include ROS 2
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

ros::Publisher ros1_pub;

void ros2ChatterCallback(const std_msgs::msg::String::SharedPtr ros2_msg)
{
  printf("  I heard from ROS 2: [%s]\n", ros2_msg->data.c_str());

  std_msgs::String ros1_msg;
  ros1_msg.data = ros2_msg->data;
  printf("  Passing along to ROS 1: [%s]\n", ros1_msg.data.c_str());
  ros1_pub.publish(ros1_msg);
}

rclcpp::Publisher<std_msgs::msg::String>::SharedPtr ros2_pub;

void ros1ChatterCallback(const ros::MessageEvent<std_msgs::String const> & ros1_msg_event)
{
  const boost::shared_ptr<ros::M_string> & connection_header =
    ros1_msg_event.getConnectionHeaderPtr();
  std::string key = "callerid";
  if (connection_header->find(key) != connection_header->end()) {
    if (connection_header->at(key) == "/ros_bridge") {
      printf("    I heard from ROS 1 from myself\n");
      return;
    }
    printf("I heard from ROS 1 from: [%s]\n", connection_header->at(key).c_str());
  }

  const boost::shared_ptr<std_msgs::String const> & ros1_msg = ros1_msg_event.getConstMessage();
  printf("I heard from ROS 1: [%s]\n", ros1_msg->data.c_str());

  auto ros2_msg = std::make_shared<std_msgs::msg::String>();
  ros2_msg->data = ros1_msg->data;
  printf("Passing along to ROS 2: [%s]\n", ros2_msg->data.c_str());
  ros2_pub->publish(ros2_msg);
}

int main(int argc, char * argv[])
{
  // ROS 1 node and publisher
  ros::init(argc, argv, "ros_bridge");
  ros::NodeHandle ros1_node;
  ros1_pub = ros1_node.advertise<std_msgs::String>("chatter", 10);

  // ROS 2 node and publisher
  rclcpp::init(argc, argv);
  auto ros2_node = rclcpp::Node::make_shared("ros_bridge");
  ros2_pub = ros2_node->create_publisher<std_msgs::msg::String>(
    "chatter", rmw_qos_profile_sensor_data);

  // ROS 1 subscriber
  ros::Subscriber ros1_sub = ros1_node.subscribe(
    "chatter", 10, ros1ChatterCallback);

  // ROS 2 subscriber
  auto ros2_sub = ros2_node->create_subscription<std_msgs::msg::String>(
    "chatter", ros2ChatterCallback, rmw_qos_profile_sensor_data, nullptr, true);

  // ROS 1 asynchronous spinner
  ros::AsyncSpinner async_spinner(1);
  async_spinner.start();

  // ROS 2 spinning loop
  rclcpp::executors::SingleThreadedExecutor executor;
  while (ros1_node.ok() && rclcpp::ok()) {
    executor.spin_node_once(ros2_node, std::chrono::milliseconds(1000));
  }

  return 0;
}
<commit_msg>Corrected publish calls with shared_ptr signature (#188)<commit_after>// Copyright 2015 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <iostream>
#include <memory>
#include <string>

// include ROS 1
#include "ros/message.h"
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-parameter"
#endif
#include "ros/ros.h"
#include "std_msgs/String.h"
#ifdef __clang__
# pragma clang diagnostic pop
#endif

// include ROS 2
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

ros::Publisher ros1_pub;

void ros2ChatterCallback(const std_msgs::msg::String::SharedPtr ros2_msg)
{
  printf("  I heard from ROS 2: [%s]\n", ros2_msg->data.c_str());

  std_msgs::String ros1_msg;
  ros1_msg.data = ros2_msg->data;
  printf("  Passing along to ROS 1: [%s]\n", ros1_msg.data.c_str());
  ros1_pub.publish(ros1_msg);
}

rclcpp::Publisher<std_msgs::msg::String>::SharedPtr ros2_pub;

void ros1ChatterCallback(const ros::MessageEvent<std_msgs::String const> & ros1_msg_event)
{
  const boost::shared_ptr<ros::M_string> & connection_header =
    ros1_msg_event.getConnectionHeaderPtr();
  std::string key = "callerid";
  if (connection_header->find(key) != connection_header->end()) {
    if (connection_header->at(key) == "/ros_bridge") {
      printf("    I heard from ROS 1 from myself\n");
      return;
    }
    printf("I heard from ROS 1 from: [%s]\n", connection_header->at(key).c_str());
  }

  const boost::shared_ptr<std_msgs::String const> & ros1_msg = ros1_msg_event.getConstMessage();
  printf("I heard from ROS 1: [%s]\n", ros1_msg->data.c_str());

  std_msgs::msg::String ros2_msg;
  ros2_msg.data = ros1_msg->data;
  printf("Passing along to ROS 2: [%s]\n", ros2_msg.data.c_str());
  ros2_pub->publish(ros2_msg);
}

int main(int argc, char * argv[])
{
  // ROS 1 node and publisher
  ros::init(argc, argv, "ros_bridge");
  ros::NodeHandle ros1_node;
  ros1_pub = ros1_node.advertise<std_msgs::String>("chatter", 10);

  // ROS 2 node and publisher
  rclcpp::init(argc, argv);
  auto ros2_node = rclcpp::Node::make_shared("ros_bridge");
  ros2_pub = ros2_node->create_publisher<std_msgs::msg::String>(
    "chatter", rmw_qos_profile_sensor_data);

  // ROS 1 subscriber
  ros::Subscriber ros1_sub = ros1_node.subscribe(
    "chatter", 10, ros1ChatterCallback);

  // ROS 2 subscriber
  auto ros2_sub = ros2_node->create_subscription<std_msgs::msg::String>(
    "chatter", ros2ChatterCallback, rmw_qos_profile_sensor_data, nullptr, true);

  // ROS 1 asynchronous spinner
  ros::AsyncSpinner async_spinner(1);
  async_spinner.start();

  // ROS 2 spinning loop
  rclcpp::executors::SingleThreadedExecutor executor;
  while (ros1_node.ok() && rclcpp::ok()) {
    executor.spin_node_once(ros2_node, std::chrono::milliseconds(1000));
  }

  return 0;
}
<|endoftext|>


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

/*
Author: Chao Ma (mctt90@gmail.com)
This file is the implementation of the Trainer class.
*/

#include <stdio.h>
#include <vector>

#include "src/solver/trainer.h"
#include "src/data/data_structure.h"

namespace xLearn {

// Standard training
void Trainer::Train() {
  //for in n epoch
  for (int n = 0; n < epoch_; ++n) {
    TIME_START();
    // Return to the head of the data
    train_reader_->Reset();
    if (test_reader_ != NULL) {
      test_reader_->Reset();
    }
    DMatrix* matrix = NULL;
    // Calc grad and update model
    while (train_reader_->Samples(matrix)) {
      loss_->CalcGrad(matrix, model_, updater_);
    }
    // Calc Train loss
    train_reader_->Reset();
    index_t count_sample = 0;
    real_t loss_val = 0.0;
    std::vector<real_t> pred;
    int tmp = 0;
    while (1) {
      tmp = train_reader_->Samples(matrix);
      if (tmp == 0) { break; }
      if (tmp != pred.size()) {
        pred.resize(tmp);
      }
      count_sample += tmp;
      loss_->Predict(matrix, model_, pred);
      loss_val += loss_->Evalute(pred, matrix->Y);
    }
    loss_val /= count_sample;
    printf("  Epoch %d  |  Train loss: %f  |", n, loss_val);
    // Calc Test loss
    if (test_reader_ != NULL) {
      count_sample = 0;
      loss_val = 0;
      tmp = 0;
      while (1) {
        tmp = test_reader_->Samples(matrix);
        if (tmp == 0) { break; }
        if (tmp != pred.size()) {
          pred.resize(tmp);
        }
        count_sample += tmp;
        loss_->Predict(matrix, model_, pred);
        loss_val += loss_->Evalute(pred, matrix->Y);
      }
      loss_val /= count_sample;
      printf("  Test loss: %f  |", loss_val);
    }
    TIME_END();
    printf("  ");
    SHOW_TIME();
    printf("  |\n");
  }
}

// Training using cross-validation
void Trainer::CVTrain() {

}

} // namespace xLearn
<commit_msg>update<commit_after>//------------------------------------------------------------------------------
// Copyright (c) 2016 by contributors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//------------------------------------------------------------------------------

/*
Author: Chao Ma (mctt90@gmail.com)
This file is the implementation of the Trainer class.
*/

#include <stdio.h>
#include <vector>

#include "src/solver/trainer.h"
#include "src/data/data_structure.h"

namespace xLearn {

// Standard training
void Trainer::Train() {
  //for in n epoch
  for (int n = 0; n < epoch_; ++n) {
    TIME_START();
    // Return to the head of the data
    train_reader_->Reset();
    if (test_reader_ != NULL) {
      test_reader_->Reset();
    }
    DMatrix* matrix = NULL;
    // Calc grad and update model
    while (train_reader_->Samples(matrix)) {
      loss_->CalcGrad(matrix, model_, updater_);
    }
    // Calc Train loss
    train_reader_->Reset();
    index_t count_sample = 0;
    real_t loss_val = 0.0;
    std::vector<real_t> pred;
    int tmp = 0;
    while (1) {
      tmp = train_reader_->Samples(matrix);
      if (tmp == 0) { break; }
      if (tmp != pred.size()) {
        pred.resize(tmp);
      }
      count_sample += tmp;
      loss_->Predict(matrix, model_, pred);
      loss_val += loss_->Evalute(pred, matrix->Y);
    }
    loss_val /= count_sample;
    printf("  Epoch %d  |  Train loss: %f  |", n, loss_val);
    // Calc Test loss
    if (test_reader_ != NULL) {
      count_sample = 0;
      loss_val = 0;
      tmp = 0;
      while (1) {
        tmp = test_reader_->Samples(matrix);
        if (tmp == 0) { break; }
        if (tmp != pred.size()) {
          pred.resize(tmp);
        }
        count_sample += tmp;
        loss_->Predict(matrix, model_, pred);
        loss_val += loss_->Evalute(pred, matrix->Y);
      }
      loss_val /= count_sample;
      printf("  Test loss: %f  |", loss_val);
    }
    TIME_END();
    printf("  ");
    SHOW_TIME();
    printf("  |\n");
  }
}

// Training using cross-validation
void Trainer::CVTrain() {
  std::vector<real_t> train_loss(reader_list_.size());
  std::vector<real_t> test_loss(reader_list_.size());
  // Use ith Reader as validation Reader
  for (int i = 0; i < reader_list_.size(); ++i) {
    printf("  Cross-validation: %d/%lu: \n", i+1, reader_list_.size());
    // for n epoch
    for (int n = 0; n < epoch_; ++n) {
      TIME_START();
      // Use the other Reader to train
      for (int j = 0; j < reader_list_.size(); ++j) {
        if (i == j) { continue; }
        train_reader_ = reader_list_[j];
        train_reader_->Reset();
        DMatrix* matrix = NULL;
        while (train_reader_->Samples(matrix)) {
          loss_->CalcGrad(matrix, model_, updater_);
        }
      }
      // Calc train loss
      index_t count_sample = 0;
      real_t loss_val = 0.0;
      std::vector<real_t> pred;
      int tmp = 0;
      for (int j = 0; j < reader_list_.size(); ++j) {
        if (i == j) { continue; }
        train_reader_ = reader_list_[j];
        train_reader_->Reset();
        DMatrix* matrix = NULL;
        while (1) {
          tmp = train_reader_->Samples(matrix);
          if (tmp == 0) { break; }
          if (tmp != pred.size()) {
            pred.resize(tmp);
          }
          count_sample += tmp;
          loss_->Predict(matrix, model_, pred);
          loss_val += loss_->Evalute(pred, matrix->Y);
        }
      }
      loss_val /= count_sample;
      if (n == epoch_-1) {
        train_loss[i] = loss_val;
      }
      printf("    Epoch %d  |  Train loss: %f  |", n, loss_val);
      // Calc Test loss
      count_sample = 0;
      loss_val = 0.0;
      tmp = 0;
      test_reader_ = reader_list_[i];
      test_reader_->Reset();
      DMatrix* matrix = NULL;
      while (1) {
        tmp = test_reader_->Samples(matrix);
        if (tmp == 0) { break; }
        if (tmp != pred.size()) {
          pred.resize(tmp);
        }
        count_sample += tmp;
        loss_->Predict(matrix, model_, pred);
        loss_val += loss_->Evalute(pred, matrix->Y);
      }
      loss_val /= count_sample;
      if (n == epoch_-1) {
        test_loss[i] = loss_val;
      }
      printf("  Test loss: %f  |", loss_val);
      TIME_END();
      printf("  ");
      SHOW_TIME();
      printf("  |\n");
    }
  }
  // average train loss
  real_t aver = 0;
  for (int i = 0; i < train_loss.size(); ++i) {
    aver += train_loss[i];
  }
  aver /= train_loss.size();
  printf("  Average train loss: %f \n", aver);
  // average test loss
  aver = 0;
  for (int i = 0; i < test_loss.size(); ++i) {
    aver += test_loss[i];
  }
  aver /= test_loss.size();
  printf("  Average test loss: %f \n", aver);
}

} // namespace xLearn
<|endoftext|>


<commit_before>//
//  src/macho/file.cc
//  tbd
//
//  Created by inoahdev on 4/24/17.
//  Copyright © 2017 inoahdev. All rights reserved.
//

#include <mach-o/swap.h>

#include <cerrno>
#include <cstdlib>

#include <fcntl.h>
#include <unistd.h>

#include "file.h"

namespace macho {
    file::file(const std::string &path)
    : file_(fopen(path.data(), "r")) {
        auto &file = file_;
        if (!file) {
            fprintf(stderr, "Failed to open mach-o file at path (%s), failing with error (%s)\n", path.data(), strerror(errno));
            exit(1);
        }

        this->validate();
    }

    file::~file() {
        fclose(file_);
    }

    bool file::is_valid_file(const std::string &path) noexcept {
        const auto descriptor = open(path.data(), O_RDONLY);
        if (descriptor == -1) {
            return false;
        }

        uint32_t magic;
        read(descriptor, &magic, sizeof(uint32_t));

        auto result = magic == MH_MAGIC || magic == MH_CIGAM || magic == MH_MAGIC_64 || magic == MH_CIGAM_64 || magic == FAT_MAGIC || magic == FAT_CIGAM || magic == FAT_MAGIC_64 || magic == FAT_CIGAM_64;

        close(descriptor);
        return result;
    }

    bool file::has_library_command(int descriptor, const struct mach_header &header) noexcept {
        auto should_swap = false;

        const auto header_magic_is_64_bit = header.magic == MH_MAGIC_64 || header.magic == MH_CIGAM_64;
        if (header_magic_is_64_bit) {
            lseek(descriptor, sizeof(uint32_t), SEEK_CUR);
        } else {
            const auto header_magic_is_32_bit = header.magic == MH_MAGIC || header.magic == MH_CIGAM;
            if (!header_magic_is_32_bit) {
                return false;
            }
        }

        const auto header_magic_is_big_endian = header.magic == MH_CIGAM_64 || header.magic == MH_CIGAM;
        if (header_magic_is_big_endian) {
            should_swap = true;
        }

        const auto load_commands = new char[header.sizeofcmds];
        read(descriptor, load_commands, header.sizeofcmds);

        auto index = 0;
        auto size_left = header.sizeofcmds;

        const auto &ncmds = header.ncmds;
        for (auto i = 0; i < ncmds; i++) {
            const auto load_cmd = (struct load_command *)&load_commands[index];
            if (should_swap) {
                swap_load_command(load_cmd, NX_LittleEndian);
            }

            auto cmdsize = load_cmd->cmdsize;

            const auto cmdsize_is_too_small = cmdsize < sizeof(struct load_command);
            const auto cmdsize_is_larger_than_load_command_space = cmdsize > size_left;
            const auto cmdsize_takes_up_rest_of_load_command_space = (cmdsize == size_left && i != ncmds - 1);

            if (cmdsize_is_too_small || cmdsize_is_larger_than_load_command_space || cmdsize_takes_up_rest_of_load_command_space) {
                delete[] load_commands;
                return false;
            }

            if (load_cmd->cmd == LC_ID_DYLIB) {
                delete[] load_commands;
                return true;
            }

            index += cmdsize;
        }

        delete[] load_commands;
        return false;
    };

    bool file::is_valid_library(const std::string &path) noexcept {
        const auto descriptor = open(path.data(), O_RDONLY);
        if (descriptor == -1) {
            return false;
        }

        uint32_t magic;
        read(descriptor, &magic, sizeof(uint32_t));

        const auto magic_is_fat_64 = magic == FAT_MAGIC_64 || magic == FAT_CIGAM_64;
        if (magic_is_fat_64) {
            uint32_t nfat_arch;
            read(descriptor, &nfat_arch, sizeof(uint32_t));

            const auto magic_is_64_bit = magic == FAT_CIGAM_64;
            if (magic_is_64_bit) {
                swap_value(nfat_arch);
            }

            const auto architectures = new struct fat_arch_64[nfat_arch];
            const auto architectures_size = sizeof(struct fat_arch_64) * nfat_arch;

            read(descriptor, architectures, architectures_size);

            if (magic_is_64_bit) {
                swap_fat_arch_64(architectures, nfat_arch, NX_LittleEndian);
            }

            for (auto i = 0; i < nfat_arch; i++) {
                const auto &architecture = architectures[i];
                struct mach_header header;

                lseek(descriptor, architecture.offset, SEEK_SET);
                read(descriptor, &header, sizeof(struct mach_header));

                if (!has_library_command(descriptor, header)) {
                    close(descriptor);
                    return false;
                }
            }

            delete[] architectures;
        } else {
            const auto magic_is_fat_32 = magic == FAT_MAGIC || magic == FAT_CIGAM;
            if (magic_is_fat_32) {
                uint32_t nfat_arch;
                read(descriptor, &nfat_arch, sizeof(uint32_t));

                swap_value(nfat_arch);

                const auto architectures = new struct fat_arch[nfat_arch];
                const auto architectures_size = sizeof(struct fat_arch) * nfat_arch;

                read(descriptor, architectures, architectures_size);

                const auto magic_is_big_endian = magic == FAT_CIGAM;
                if (magic_is_big_endian) {
                    swap_fat_arch(architectures, nfat_arch, NX_LittleEndian);
                }

                for (auto i = 0; i < nfat_arch; i++) {
                    const auto &architecture = architectures[i];
                    struct mach_header header;

                    lseek(descriptor, architecture.offset, SEEK_SET);
                    read(descriptor, &header, sizeof(struct mach_header));

                    if (!has_library_command(descriptor, header)) {
                        close(descriptor);
                        return false;
                    }
                }

                delete[] architectures;
            } else {
                const auto magic_is_thin = magic == MH_MAGIC_64 || magic == MH_CIGAM_64 || magic == MH_MAGIC || magic == MH_CIGAM;
                if (magic_is_thin) {
                    struct mach_header header;
                    header.magic = magic;

                    read(descriptor, &header.cputype, sizeof(struct mach_header) - sizeof(uint32_t));
                    if (!has_library_command(descriptor, header)) {
                        close(descriptor);
                        return false;
                    }
                } else {
                    close(descriptor);
                    return false;
                }
            }
        }

        close(descriptor);
        return true;
    }

    void file::validate() {
        auto &containers = containers_;
        auto &file = file_;
        auto &magic = magic_;

        fread(&magic, sizeof(uint32_t), 1, file);

        const auto magic_is_fat = magic == FAT_MAGIC || magic == FAT_CIGAM || magic == FAT_MAGIC_64 || magic == FAT_CIGAM_64;
        if (magic_is_fat) {
            uint32_t nfat_arch;
            fread(&nfat_arch, sizeof(uint32_t), 1, file);

            if (!nfat_arch) {
                fprintf(stderr, "Mach-o file has 0 architectures");
                exit(1);
            }

            auto should_swap = magic == FAT_CIGAM || magic == FAT_CIGAM_64;
            if (should_swap) {
                swap_value(nfat_arch);
            }

            containers.reserve(nfat_arch);

            const auto magic_is_fat_64 = magic == FAT_MAGIC_64 || magic == FAT_CIGAM_64;
            if (magic_is_fat_64) {
                const auto architectures = new struct fat_arch_64[nfat_arch];
                const auto architectures_size = sizeof(struct fat_arch_64) * nfat_arch;

                fread(architectures, architectures_size, 1, file);

                if (should_swap) {
                    swap_fat_arch_64(architectures, nfat_arch, NX_LittleEndian);
                }

                for (auto i = 0; i < nfat_arch; i++) {
                    const auto &architecture = architectures[i];
                    containers.emplace_back(file, architecture.offset, architecture.size);
                }

                delete[] architectures;
            } else {
                const auto architectures = new struct fat_arch[nfat_arch];
                fread(architectures, sizeof(struct fat_arch) * nfat_arch, 1, file);

                if (should_swap) {
                    swap_fat_arch(architectures, nfat_arch, NX_LittleEndian);
                }

                for (auto i = 0; i < nfat_arch; i++) {
                    const auto &architecture = architectures[i];
                    containers.emplace_back(file, architecture.offset, architecture.size);
                }

                delete[] architectures;
            }
        } else {
            const auto magic_is_thin = magic == MH_MAGIC || magic == MH_CIGAM || magic == MH_MAGIC_64 || magic == MH_CIGAM_64;
            if (magic_is_thin) {
                containers.emplace_back(file, 0);
            } else {
                fputs("Mach-o file is invalid, does not have a valid magic-number", stderr);
                exit(1);
            }
        }
    }
}
<commit_msg>Add more variables<commit_after>//
//  src/macho/file.cc
//  tbd
//
//  Created by inoahdev on 4/24/17.
//  Copyright © 2017 inoahdev. All rights reserved.
//

#include <mach-o/swap.h>

#include <cerrno>
#include <cstdlib>

#include <fcntl.h>
#include <unistd.h>

#include "file.h"

namespace macho {
    file::file(const std::string &path)
    : file_(fopen(path.data(), "r")) {
        auto &file = file_;
        if (!file) {
            fprintf(stderr, "Failed to open mach-o file at path (%s), failing with error (%s)\n", path.data(), strerror(errno));
            exit(1);
        }

        this->validate();
    }

    file::~file() {
        fclose(file_);
    }

    bool file::is_valid_file(const std::string &path) noexcept {
        const auto descriptor = open(path.data(), O_RDONLY);
        if (descriptor == -1) {
            return false;
        }

        uint32_t magic;
        read(descriptor, &magic, sizeof(uint32_t));

        auto result = magic == MH_MAGIC || magic == MH_CIGAM || magic == MH_MAGIC_64 || magic == MH_CIGAM_64 || magic == FAT_MAGIC || magic == FAT_CIGAM || magic == FAT_MAGIC_64 || magic == FAT_CIGAM_64;

        close(descriptor);
        return result;
    }

    bool file::has_library_command(int descriptor, const struct mach_header &header) noexcept {
        auto should_swap = false;

        const auto header_magic_is_64_bit = header.magic == MH_MAGIC_64 || header.magic == MH_CIGAM_64;
        if (header_magic_is_64_bit) {
            lseek(descriptor, sizeof(uint32_t), SEEK_CUR);
        } else {
            const auto header_magic_is_32_bit = header.magic == MH_MAGIC || header.magic == MH_CIGAM;
            if (!header_magic_is_32_bit) {
                return false;
            }
        }

        const auto header_magic_is_big_endian = header.magic == MH_CIGAM_64 || header.magic == MH_CIGAM;
        if (header_magic_is_big_endian) {
            should_swap = true;
        }

        const auto load_commands = new char[header.sizeofcmds];
        read(descriptor, load_commands, header.sizeofcmds);

        auto index = 0;
        auto size_left = header.sizeofcmds;

        const auto &ncmds = header.ncmds;
        for (auto i = 0; i < ncmds; i++) {
            const auto load_cmd = (struct load_command *)&load_commands[index];
            if (should_swap) {
                swap_load_command(load_cmd, NX_LittleEndian);
            }

            auto cmdsize = load_cmd->cmdsize;

            const auto cmdsize_is_too_small = cmdsize < sizeof(struct load_command);
            const auto cmdsize_is_larger_than_load_command_space = cmdsize > size_left;
            const auto cmdsize_takes_up_rest_of_load_command_space = (cmdsize == size_left && i != ncmds - 1);

            if (cmdsize_is_too_small || cmdsize_is_larger_than_load_command_space || cmdsize_takes_up_rest_of_load_command_space) {
                delete[] load_commands;
                return false;
            }

            if (load_cmd->cmd == LC_ID_DYLIB) {
                delete[] load_commands;
                return true;
            }

            index += cmdsize;
        }

        delete[] load_commands;
        return false;
    };

    bool file::is_valid_library(const std::string &path) noexcept {
        const auto descriptor = open(path.data(), O_RDONLY);
        if (descriptor == -1) {
            return false;
        }

        uint32_t magic;
        read(descriptor, &magic, sizeof(uint32_t));

        const auto magic_is_fat_64 = magic == FAT_MAGIC_64 || magic == FAT_CIGAM_64;
        if (magic_is_fat_64) {
            uint32_t nfat_arch;
            read(descriptor, &nfat_arch, sizeof(uint32_t));

            const auto magic_is_64_bit = magic == FAT_CIGAM_64;
            if (magic_is_64_bit) {
                swap_value(nfat_arch);
            }

            const auto architectures = new struct fat_arch_64[nfat_arch];
            const auto architectures_size = sizeof(struct fat_arch_64) * nfat_arch;

            read(descriptor, architectures, architectures_size);

            if (magic_is_64_bit) {
                swap_fat_arch_64(architectures, nfat_arch, NX_LittleEndian);
            }

            for (auto i = 0; i < nfat_arch; i++) {
                const auto &architecture = architectures[i];
                struct mach_header header;

                lseek(descriptor, architecture.offset, SEEK_SET);
                read(descriptor, &header, sizeof(struct mach_header));

                if (!has_library_command(descriptor, header)) {
                    close(descriptor);
                    return false;
                }
            }

            delete[] architectures;
        } else {
            const auto magic_is_fat_32 = magic == FAT_MAGIC || magic == FAT_CIGAM;
            if (magic_is_fat_32) {
                uint32_t nfat_arch;
                read(descriptor, &nfat_arch, sizeof(uint32_t));

                swap_value(nfat_arch);

                const auto architectures = new struct fat_arch[nfat_arch];
                const auto architectures_size = sizeof(struct fat_arch) * nfat_arch;

                read(descriptor, architectures, architectures_size);

                const auto magic_is_big_endian = magic == FAT_CIGAM;
                if (magic_is_big_endian) {
                    swap_fat_arch(architectures, nfat_arch, NX_LittleEndian);
                }

                for (auto i = 0; i < nfat_arch; i++) {
                    const auto &architecture = architectures[i];
                    struct mach_header header;

                    lseek(descriptor, architecture.offset, SEEK_SET);
                    read(descriptor, &header, sizeof(struct mach_header));

                    if (!has_library_command(descriptor, header)) {
                        close(descriptor);
                        return false;
                    }
                }

                delete[] architectures;
            } else {
                const auto magic_is_thin = magic == MH_MAGIC_64 || magic == MH_CIGAM_64 || magic == MH_MAGIC || magic == MH_CIGAM;
                if (magic_is_thin) {
                    struct mach_header header;
                    header.magic = magic;

                    read(descriptor, &header.cputype, sizeof(struct mach_header) - sizeof(uint32_t));
                    if (!has_library_command(descriptor, header)) {
                        close(descriptor);
                        return false;
                    }
                } else {
                    close(descriptor);
                    return false;
                }
            }
        }

        close(descriptor);
        return true;
    }

    void file::validate() {
        auto &containers = containers_;
        auto &file = file_;
        auto &magic = magic_;

        fread(&magic, sizeof(uint32_t), 1, file);

        const auto magic_is_fat = magic == FAT_MAGIC || magic == FAT_CIGAM || magic == FAT_MAGIC_64 || magic == FAT_CIGAM_64;
        if (magic_is_fat) {
            uint32_t nfat_arch;
            fread(&nfat_arch, sizeof(uint32_t), 1, file);

            if (!nfat_arch) {
                fprintf(stderr, "Mach-o file has 0 architectures");
                exit(1);
            }

            auto should_swap = magic == FAT_CIGAM || magic == FAT_CIGAM_64;
            if (should_swap) {
                swap_value(nfat_arch);
            }

            containers.reserve(nfat_arch);

            const auto magic_is_fat_64 = magic == FAT_MAGIC_64 || magic == FAT_CIGAM_64;
            if (magic_is_fat_64) {
                const auto architectures = new struct fat_arch_64[nfat_arch];
                const auto architectures_size = sizeof(struct fat_arch_64) * nfat_arch;

                fread(architectures, architectures_size, 1, file);

                if (should_swap) {
                    swap_fat_arch_64(architectures, nfat_arch, NX_LittleEndian);
                }

                for (auto i = 0; i < nfat_arch; i++) {
                    const auto &architecture = architectures[i];
                    containers.emplace_back(file, architecture.offset, architecture.size);
                }

                delete[] architectures;
            } else {
                const auto architectures = new struct fat_arch[nfat_arch];
                const auto architectures_size = sizeof(struct fat_arch_64) * nfat_arch;

                fread(architectures, architectures_size, 1, file);

                if (should_swap) {
                    swap_fat_arch(architectures, nfat_arch, NX_LittleEndian);
                }

                for (auto i = 0; i < nfat_arch; i++) {
                    const auto &architecture = architectures[i];
                    containers.emplace_back(file, architecture.offset, architecture.size);
                }

                delete[] architectures;
            }
        } else {
            const auto magic_is_thin = magic == MH_MAGIC || magic == MH_CIGAM || magic == MH_MAGIC_64 || magic == MH_CIGAM_64;
            if (magic_is_thin) {
                containers.emplace_back(file, 0);
            } else {
                fputs("Mach-o file is invalid, does not have a valid magic-number", stderr);
                exit(1);
            }
        }
    }
}
<|endoftext|>


<commit_before>/*
 * satk - SIM application toolkit
 * Copyright © 2011, Intel Corporation.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Written by - Luc Yriarte <luc.yriarte@linux.intel.com>
 */

#include <QtDBus>
#include "ofonodbustypes.h"
#include "stkofonoutils.h"
#include "stkdefines.h"

QPixmap StkOfonoUtils::findIcon(uchar id)
{ // #### TODO implement
    return TEST_PIXMAP;
}

QString StkOfonoUtils::findIconUrl(uchar id)
{ // #### TODO implement
    return TEST_ICON_URL;
}

QList<StkIf*> StkOfonoUtils::findSimToolkitInterfaces(const QDBusConnection &connection, MgrIf *mgrIf)
{
    QList<StkIf*> simToolkitInterfaces;
    QDBusError dbusError;
    // org.ofono.Manager interface GetModems method
    QDBusPendingReply<OfonoModemList> modemsCall = mgrIf->GetModems();
    modemsCall.waitForFinished();
    if (modemsCall.isError()) {
        dbusError = modemsCall.error();
        qDebug() << "Error:" << dbusError.name() << ":" << dbusError.message();
        return simToolkitInterfaces;
    }
    // loop foreach modem, find the first org.ofono.SimToolkit interface
    // in order to pass it to the SimToolkit main window
    foreach(const OfonoModem &pms, modemsCall.value()) {
        bool foundSimToolkit = false;
        // loop interfaces to find "org.ofono.SimToolkit"
        foreach(const QString &interface, pms.varmap.value("Interfaces").toStringList())
        {
            if (interface == "org.ofono.SimToolkit")
            {
                foundSimToolkit = true;
                break;
            }
        }
        if (foundSimToolkit)
        {
            // Instanciate proxy for org.ofono.SimToolkit interface
            simToolkitInterfaces.append(new StkIf("org.ofono",pms.objpath.path(),connection,NULL));
        }
    }
    return simToolkitInterfaces;
}

int StkOfonoUtils::registerSimToolkitAgent(QDBusConnection &connection, StkAgentService *stkAgentService, StkIf *stkIf)
{
    QDBusError dbusError;
    QDBusObjectPath stkAgentPath(STK_AGENT_PATH);
    if (!connection.registerObject(stkAgentPath.path(),stkAgentService)) {
        dbusError = connection.lastError();
        qDebug() << "registerObject Error:" << dbusError.name() << ":" << dbusError.message();
        return -1;
    }
    // Register SimToolkitAgent with org.ofono.SimToolkit interface RegisterAgent method
    QDBusPendingReply<> stkRegisterCall = stkIf->RegisterAgent(stkAgentPath);
    stkRegisterCall.waitForFinished();
    if (stkRegisterCall.isError()) {
        dbusError = stkRegisterCall.error();
        qDebug() << "RegisterAgent error:" << dbusError.name() << ":" << dbusError.message();
        return -1;
    }
    return 0;
}

int StkOfonoUtils::unRegisterSimToolkitAgent(StkIf *stkIf)
{
    QDBusError dbusError;
    QDBusObjectPath stkAgentPath(STK_AGENT_PATH);
    QDBusPendingReply<> stkRegisterCall = stkIf->UnregisterAgent(stkAgentPath);
    stkRegisterCall.waitForFinished();
    if (stkRegisterCall.isError()) {
        dbusError = stkRegisterCall.error();
        qDebug() << "UnregisterAgent error:" << dbusError.name() << ":" << dbusError.message();
        return -1;
    }
    return 0;
}
<commit_msg>SIM Application Toolkit: cleanup / simplify<commit_after>/*
 * satk - SIM application toolkit
 * Copyright © 2011, Intel Corporation.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Written by - Luc Yriarte <luc.yriarte@linux.intel.com>
 */

#include <QtDBus>
#include "ofonodbustypes.h"
#include "stkofonoutils.h"
#include "stkdefines.h"

QPixmap StkOfonoUtils::findIcon(uchar id)
{ // #### TODO implement
    return TEST_PIXMAP;
}

QString StkOfonoUtils::findIconUrl(uchar id)
{ // #### TODO implement
    return TEST_ICON_URL;
}

QList<StkIf*> StkOfonoUtils::findSimToolkitInterfaces(const QDBusConnection &connection, MgrIf *mgrIf)
{
    QList<StkIf*> simToolkitInterfaces;
    QDBusError dbusError;
    // org.ofono.Manager interface GetModems method
    QDBusPendingReply<OfonoModemList> modemsCall = mgrIf->GetModems();
    modemsCall.waitForFinished();
    if (modemsCall.isError()) {
        dbusError = modemsCall.error();
        qDebug() << "Error:" << dbusError.name() << ":" << dbusError.message();
        return simToolkitInterfaces;
    }
    // loop foreach modem, find all org.ofono.SimToolkit interfaces
    foreach(const OfonoModem &pms, modemsCall.value()) {
        // loop interfaces to find "org.ofono.SimToolkit"
        foreach(const QString &interface, pms.varmap.value("Interfaces").toStringList()) {
            if (interface == "org.ofono.SimToolkit") {
                // Instanciate proxy for org.ofono.SimToolkit interface
                simToolkitInterfaces.append(new StkIf("org.ofono",pms.objpath.path(),connection,NULL));
                break;
            }
        }
    }
    return simToolkitInterfaces;
}

int StkOfonoUtils::registerSimToolkitAgent(QDBusConnection &connection, StkAgentService *stkAgentService, StkIf *stkIf)
{
    QDBusError dbusError;
    QDBusObjectPath stkAgentPath(STK_AGENT_PATH);
    if (!connection.registerObject(stkAgentPath.path(),stkAgentService)) {
        dbusError = connection.lastError();
        qDebug() << "registerObject Error:" << dbusError.name() << ":" << dbusError.message();
        return -1;
    }
    // Register SimToolkitAgent with org.ofono.SimToolkit interface RegisterAgent method
    QDBusPendingReply<> stkRegisterCall = stkIf->RegisterAgent(stkAgentPath);
    stkRegisterCall.waitForFinished();
    if (stkRegisterCall.isError()) {
        dbusError = stkRegisterCall.error();
        qDebug() << "RegisterAgent error:" << dbusError.name() << ":" << dbusError.message();
        return -1;
    }
    return 0;
}

int StkOfonoUtils::unRegisterSimToolkitAgent(StkIf *stkIf)
{
    QDBusError dbusError;
    QDBusObjectPath stkAgentPath(STK_AGENT_PATH);
    QDBusPendingReply<> stkRegisterCall = stkIf->UnregisterAgent(stkAgentPath);
    stkRegisterCall.waitForFinished();
    if (stkRegisterCall.isError()) {
        dbusError = stkRegisterCall.error();
        qDebug() << "UnregisterAgent error:" << dbusError.name() << ":" << dbusError.message();
        return -1;
    }
    return 0;
}
<|endoftext|>