<commit_before>/*
 * Copyright (C) 2013 by Glenn Hickey (hickey@soe.ucsc.edu)
 *
 * Released under the MIT license, see LICENSE.txt
 */

#include <cassert>
#include <deque>
#include <sstream>
#include <limits>
#include <fstream>
#include <algorithm>
#include "halLodManager.h"
#include "halCLParser.h"

#ifdef ENABLE_UDC
extern "C" {
#include "common.h"
#include "udc.h"
// Dont want this macro ruining std::max
#ifdef max
#undef max
#endif
}
#endif

using namespace std;
using namespace hal;

// default to 5 days for now
const unsigned long LodManager::MaxAgeSec = 432000;

// specify upper limit of lods.
// (MUST MANUALLY KEEP CONSISTENT WITH global MaxLodToken variable in
// hal/lod/halLodInterpolate.py)
const string LodManager::MaxLodToken = "max";

LodManager::LodManager():
    _options(NULL),
    _maxLodLowerBound((hal_size_t)numeric_limits<hal_index_t>::max()) {
  // FIXME: the way options work is weird.
}

LodManager::~LodManager()
{
  for (AlignmentMap::iterator mapIt = _map.begin(); mapIt != _map.end();
       ++mapIt)
  {
    if (mapIt->second.second.get() != NULL)
    {
        const_cast<Alignment*>(mapIt->second.second.get())->close();
    }
  }
}

void LodManager::loadLODFile(const string& lodPath,
                             const CLParser* options)
{
    _options = options;
  _map.clear();

#ifdef ENABLE_UDC
  char* cpath = const_cast<char*>(lodPath.c_str());

  size_t cbufSize = 0;
  char* cbuffer = udcFileReadAll(cpath, NULL, 100000, &cbufSize);
  if (cbuffer == NULL)
  {
    throw hal_exception("Error udc-opening " + lodPath);
  }
  string cbufCpy(cbuffer);
  freeMem(cbuffer);
  stringstream ifile(cbufCpy);
#else
  ifstream ifile(lodPath.c_str());
#endif

  if (!ifile.good())
  {
    stringstream emes;
    emes << "Error opening " << lodPath;
    throw hal_exception(emes.str());
  }

  string lineBuffer;
  hal_size_t minLen;
  string path;
  hal_size_t lineNum = 1;
  bool foundMax = false;
  _maxLodLowerBound = (hal_size_t)numeric_limits<hal_index_t>::max();
  while (ifile.good())
  {
    getline(ifile, lineBuffer);
    stringstream ss(lineBuffer);
    ss >> minLen >> path;
    if (ifile.bad())
    {
        throw hal_exception("Error parsing line " + std::to_string(lineNum) + " of " + lodPath);
    }
    if (lineBuffer.length() == 0)
    {
      continue;
    }
    string fullHalPath;
    if (foundMax == true)
    {
        throw hal_exception("Error on line " + std::to_string(lineNum) + " of " + lodPath
                            + ": Limit token (" + MaxLodToken + ") can only appear on"
                            + " final line.");
    }
    if (path == MaxLodToken)
    {
      foundMax = true;
      _maxLodLowerBound = minLen;
      fullHalPath = MaxLodToken;
    }
    else
    {
      fullHalPath = resolvePath(lodPath, path);
    }
    _map.insert(pair<hal_size_t, PathAlign>(
                  minLen, PathAlign(fullHalPath, AlignmentConstPtr())));
    ++lineNum;
  }

  checkMap(lodPath);
}

void LodManager::loadSingeHALFile(const string& halPath,
                                  const CLParser* options)
{
    _options = options;
  _map.clear();
  _map.insert(pair<hal_size_t, PathAlign>(
                0, PathAlign(halPath, AlignmentConstPtr())));
  _maxLodLowerBound = (hal_size_t)numeric_limits<hal_index_t>::max();
  checkMap(halPath);
}

const Alignment* LodManager::getAlignment(hal_size_t queryLength,
                                           bool needDNA)
{
  assert(_map.size() > 0);
  AlignmentMap::iterator mapIt;
  if (needDNA == true)
  {
    mapIt = _map.begin();
  }
  else
  {
    mapIt = _map.upper_bound(queryLength);
    --mapIt;
  }
  assert(mapIt->first <= queryLength);
  AlignmentConstPtr& alignment = mapIt->second.second;
  if (mapIt->first == _maxLodLowerBound)
  {
      throw hal_exception("Query length " + std::to_string(queryLength) + " above maximum LOD size of "
                          + std::to_string(getMaxQueryLength()));
  }
  if (alignment.get() == NULL)
  {
      alignment = AlignmentConstPtr(openHalAlignment(mapIt->second.first, _options));
      checkAlignment(mapIt->first, mapIt->second.first, alignment.get());
  }
  assert(mapIt->second.second.get() != NULL);
  return alignment.get();
}

bool LodManager::isLod0(hal_size_t queryLength) const
{
  assert(_map.size() > 0);
  AlignmentMap::const_iterator mapIt = _map.upper_bound(queryLength);
  --mapIt;
  return mapIt == _map.begin();
}

string LodManager::resolvePath(const string& lodPath,
                               const string& halPath)
{
  assert(lodPath.empty() == false && halPath.empty() == false);
  if (halPath[0] == '/' || halPath.find(":/") != string::npos)
  {
    return halPath;
  }
  size_t sPos = lodPath.find_last_of('/');
  if (sPos == string::npos)
  {
    return halPath;
  }
  return lodPath.substr(0, sPos + 1) + halPath;
}

void LodManager::checkMap(const string& lodPath)
{
  if (_map.size() == 0)
  {
      throw hal_exception("No entries were found in " + lodPath);
  }
  AlignmentMap::const_iterator mapIt = _map.begin();
  if (mapIt->first != 0)
  {
    throw hal_exception("No alignment with range value 0 found in " + lodPath + ". "
                        + "A record of the form \"0 pathToOriginalHALFile\" must be present");
  }
  if (_maxLodLowerBound == 0)
  {
    throw hal_exception("Maximum LOD query length must be > 0");
  }
}

void LodManager::checkAlignment(hal_size_t minQuery,
                                const string& path,
                                const Alignment* alignment)
{
  if (alignment->getNumGenomes() == 0)
  {
    throw hal_exception("No genomes found in base alignment specified in " + path);
  }

#ifndef NDEBUG
  if (minQuery == 0)
  {
    vector<string> leafNames = alignment->getLeafNamesBelow(
      alignment->getRootName());
    string name = !leafNames.empty() ? leafNames[0] : alignment->getRootName();
    const Genome* genome = alignment->openGenome(name);

    bool seqFound = genome->containsDNAArray();
    alignment->closeGenome(genome);
    if (seqFound == false)
    {
      throw hal_exception("HAL file for highest level of detail (0) in " + path
                          + "must contain DNA sequence information.");
    }
  }
#endif
}
<commit_msg>improved error message<commit_after>/*
 * Copyright (C) 2013 by Glenn Hickey (hickey@soe.ucsc.edu)
 *
 * Released under the MIT license, see LICENSE.txt
 */

#include <cassert>
#include <deque>
#include <sstream>
#include <limits>
#include <fstream>
#include <algorithm>
#include "halLodManager.h"
#include "halCLParser.h"

#ifdef ENABLE_UDC
extern "C" {
#include "common.h"
#include "udc.h"
// Dont want this macro ruining std::max
#ifdef max
#undef max
#endif
}
#endif

using namespace std;
using namespace hal;

// default to 5 days for now
const unsigned long LodManager::MaxAgeSec = 432000;

// specify upper limit of lods.
// (MUST MANUALLY KEEP CONSISTENT WITH global MaxLodToken variable in
// hal/lod/halLodInterpolate.py)
const string LodManager::MaxLodToken = "max";

LodManager::LodManager():
    _options(NULL),
    _maxLodLowerBound((hal_size_t)numeric_limits<hal_index_t>::max()) {
  // FIXME: the way options work is weird.
}

LodManager::~LodManager()
{
  for (AlignmentMap::iterator mapIt = _map.begin(); mapIt != _map.end();
       ++mapIt)
  {
    if (mapIt->second.second.get() != NULL)
    {
        const_cast<Alignment*>(mapIt->second.second.get())->close();
    }
  }
}

void LodManager::loadLODFile(const string& lodPath,
                             const CLParser* options)
{
    _options = options;
  _map.clear();

#ifdef ENABLE_UDC
  char* cpath = const_cast<char*>(lodPath.c_str());

  size_t cbufSize = 0;
  char* cbuffer = udcFileReadAll(cpath, NULL, 100000, &cbufSize);
  if (cbuffer == NULL)
  {
    throw hal_exception("Error udc-opening " + lodPath);
  }
  string cbufCpy(cbuffer);
  freeMem(cbuffer);
  stringstream ifile(cbufCpy);
#else
  ifstream ifile(lodPath.c_str());
#endif

  if (!ifile.good())
  {
    stringstream emes;
    emes << "Error opening " << lodPath;
    throw hal_exception(emes.str());
  }

  string lineBuffer;
  hal_size_t minLen;
  string path;
  hal_size_t lineNum = 1;
  bool foundMax = false;
  _maxLodLowerBound = (hal_size_t)numeric_limits<hal_index_t>::max();
  while (ifile.good())
  {
    getline(ifile, lineBuffer);
    stringstream ss(lineBuffer);
    ss >> minLen >> path;
    if (ifile.bad())
    {
        throw hal_exception("Error parsing line " + std::to_string(lineNum) + " of " + lodPath);
    }
    if (lineBuffer.length() == 0)
    {
      continue;
    }
    string fullHalPath;
    if (foundMax == true)
    {
        throw hal_exception("Error on line " + std::to_string(lineNum) + " of " + lodPath
                            + ": Limit token (" + MaxLodToken + ") can only appear on"
                            + " final line.");
    }
    if (path == MaxLodToken)
    {
      foundMax = true;
      _maxLodLowerBound = minLen;
      fullHalPath = MaxLodToken;
    }
    else
    {
      fullHalPath = resolvePath(lodPath, path);
    }
    _map.insert(pair<hal_size_t, PathAlign>(
                  minLen, PathAlign(fullHalPath, AlignmentConstPtr())));
    ++lineNum;
  }

  checkMap(lodPath);
}

void LodManager::loadSingeHALFile(const string& halPath,
                                  const CLParser* options)
{
    _options = options;
  _map.clear();
  _map.insert(pair<hal_size_t, PathAlign>(
                0, PathAlign(halPath, AlignmentConstPtr())));
  _maxLodLowerBound = (hal_size_t)numeric_limits<hal_index_t>::max();
  checkMap(halPath);
}

const Alignment* LodManager::getAlignment(hal_size_t queryLength,
                                           bool needDNA)
{
  assert(_map.size() > 0);
  AlignmentMap::iterator mapIt;
  if (needDNA == true)
  {
    mapIt = _map.begin();
  }
  else
  {
    mapIt = _map.upper_bound(queryLength);
    --mapIt;
  }
  assert(mapIt->first <= queryLength);
  AlignmentConstPtr& alignment = mapIt->second.second;
  if (mapIt->first == _maxLodLowerBound)
  {
      throw hal_exception("Query length " + std::to_string(queryLength) + " above maximum LOD size of "
                          + std::to_string(getMaxQueryLength()));
  }
  if (alignment.get() == NULL)
  {
      alignment = AlignmentConstPtr(openHalAlignment(mapIt->second.first, _options));
      checkAlignment(mapIt->first, mapIt->second.first, alignment.get());
  }
  assert(mapIt->second.second.get() != NULL);
  return alignment.get();
}

bool LodManager::isLod0(hal_size_t queryLength) const
{
  assert(_map.size() > 0);
  AlignmentMap::const_iterator mapIt = _map.upper_bound(queryLength);
  --mapIt;
  return mapIt == _map.begin();
}

string LodManager::resolvePath(const string& lodPath,
                               const string& halPath)
{
  assert(lodPath.empty() == false && halPath.empty() == false);
  if (halPath[0] == '/' || halPath.find(":/") != string::npos)
  {
    return halPath;
  }
  size_t sPos = lodPath.find_last_of('/');
  if (sPos == string::npos)
  {
    return halPath;
  }
  return lodPath.substr(0, sPos + 1) + halPath;
}

void LodManager::checkMap(const string& lodPath)
{
  if (_map.size() == 0)
  {
      throw hal_exception("No entries were found in " + lodPath);
  }
  AlignmentMap::const_iterator mapIt = _map.begin();
  if (mapIt->first != 0)
  {
    throw hal_exception("No alignment with range value 0 found in " + lodPath + ". "
                        + "A record of the form \"0 pathToOriginalHALFile\" must be present");
  }
  if (_maxLodLowerBound == 0)
  {
    throw hal_exception("Maximum LOD query length must be > 0");
  }
}

void LodManager::checkAlignment(hal_size_t minQuery,
                                const string& path,
                                const Alignment* alignment)
{
  if (alignment->getNumGenomes() == 0)
  {
    throw hal_exception("No genomes found in base alignment specified in " + path);
  }

#ifndef NDEBUG
  if (minQuery == 0)
  {
    vector<string> leafNames = alignment->getLeafNamesBelow(
      alignment->getRootName());
    string name = !leafNames.empty() ? leafNames[0] : alignment->getRootName();
    const Genome* genome = alignment->openGenome(name);

    bool seqFound = genome->containsDNAArray();
    alignment->closeGenome(genome);
    if (seqFound == false)
    {
      throw hal_exception("HAL file for highest level of detail (0) in genome "
                          + name + " (" + path + ") must contain DNA sequence information.");
    }
  }
#endif
}
<|endoftext|>


<commit_before>// remove duplicates from sorted array
// Given a sorted array, remove the duplicates in place such that each element appear only once
// and return the new length.
// Do not allocate extra space for another array, you must do this in place with constant memory.
// For example, Given input array A = [1,1,2],
// Your function should return length = 2, and A is now [1,2].
// 10.29.2016
#include <iostream>
#include <cassert>

using namespace std;

class Solution
{
public:
    int removeDuplicates(int A[], int n)
    {
        if (n == 0 || n == 1) return n;

        int index = 0;

        for (int i = 1; i < n; i++) {
            if (A[i] != A[index])
                A[++index] = A[i];
        }

        return index + 1;
    }
};

void print(int A[], int n)
{
    for (int i = 0; i < n; i++)
        cout << A[i] << " ";
    cout << endl;
}

void test(int A[], int n, int B[], int size)
{
    static int counter = 0;
    cout << "test:" << ++counter << endl;
    print(A, n);
    Solution s;

    int len = s.removeDuplicates(A, n);
    print(A, len);
    assert(len == size);

    for (int i = 0; i < len; i++)
        assert(A[i] == B[i]);
    cout << "OK\n";
}

int main()
{
    {
        int A[3] = {1,1,2};
        int B[2] = {1,2};

        test(A, 3, B, 2);
    }

    {
        int A[] = {};
        int B[] = {};

        test(A, 0, B, 0);
    }

    {
        int A[] = {1};
        int B[] = {1};

        test(A, 1, B, 1);
    }

    {
        int A[] = {1,2,3};
        int B[] = {1,2,3};

        test(A, 3, B, 3);
    }

    return 0;
}
<commit_msg>add more methods<commit_after>// remove duplicates from sorted array
// Given a sorted array, remove the duplicates in place such that each element appear only once
// and return the new length.
// Do not allocate extra space for another array, you must do this in place with constant memory.
// For example, Given input array A = [1,1,2],
// Your function should return length = 2, and A is now [1,2].
// 10.29.2016
#include <iostream>
#include <cassert>
#include <algorithm>

using namespace std;

class Solution
{
public:
    int removeDuplicates(int A[], int n)
    {
        if (n <= 1) return n;

        int index = 0;

        for (int i = 1; i < n; i++) {
            if (A[i] != A[index])
                A[++index] = A[i];
        }

        return index + 1;
    }

    int removeDuplicates1(int A[], int n) {
        return distance(A, unique(A, A + n));
    }

    int removeDuplicates2(int A[], int n) {
        return removeDuplicates(A, A + n, A) - A;
    }

    template<typename InIt, typename OutIt>
    OutIt removeDuplicates(InIt first, InIt last, OutIt output) {
        while (first != last) {
            *output++ = *first;
            first = upper_bound(first, last, *first);
        }
        return output;
    }
};

void print(int A[], int n)
{
    for (int i = 0; i < n; i++)
        cout << A[i] << " ";
    cout << endl;
}

void test(int A[], int n, int B[], int size)
{
    static int counter = 0;
    cout << "test:" << ++counter << endl;
    print(A, n);
    Solution s;

    int len = s.removeDuplicates(A, n);
    print(A, len);
    assert(len == size);

    for (int i = 0; i < len; i++)
        assert(A[i] == B[i]);
    cout << "OK\n";
}

void test1(int A[], int n, int B[], int size)
{
    static int counter = 0;
    cout << "test:" << ++counter << endl;
    print(A, n);
    Solution s;

    int len = s.removeDuplicates1(A, n);
    print(A, len);
    assert(len == size);

    for (int i = 0; i < len; i++)
        assert(A[i] == B[i]);
    cout << "OK\n";
}

void test2(int A[], int n, int B[], int size)
{
    static int counter = 0;
    cout << "test:" << ++counter << endl;
    print(A, n);
    Solution s;

    int len = s.removeDuplicates1(A, n);
    print(A, len);
    assert(len == size);

    for (int i = 0; i < len; i++)
        assert(A[i] == B[i]);
    cout << "OK\n";
}

int main()
{
    {
        int A[3] = {1,1,2};
        int B[2] = {1,2};

        test(A, 3, B, 2);
        test1(A, 3, B, 2);
        test2(A, 3, B, 2);
    }

    {
        int A[] = {};
        int B[] = {};

        test(A, 0, B, 0);
        test1(A, 0, B, 0);
        test2(A, 0, B, 0);
    }

    {
        int A[] = {1};
        int B[] = {1};

        test(A, 1, B, 1);
        test1(A, 1, B, 1);
        test2(A, 1, B, 1);
    }

    {
        int A[] = {1,2,3};
        int B[] = {1,2,3};

        test(A, 3, B, 3);
        test1(A, 3, B, 3);
        test2(A, 3, B, 3);
    }

    return 0;
}
<|endoftext|>


<commit_before>/*
 *     Copyright 2015 Couchbase, 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.
 */

#define LCB_NO_DEPR_CXX_CTORS

#include "config.h"
#include <sys/types.h>
#include <libcouchbase/couchbase.h>
#include <libcouchbase/api3.h>
#include <libcouchbase/n1ql.h>
#include <libcouchbase/vbucket.h>
#include <iostream>
#include <list>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include <fstream>
#include <algorithm> // random_shuffle
#include <stdexcept>
#include <sstream>
#ifndef WIN32
#include <pthread.h>
#include <unistd.h> // isatty()
#endif
#include "common/options.h"
#include "common/histogram.h"

using namespace cbc;
using namespace cliopts;
using std::vector;
using std::string;
using std::cerr;
using std::endl;

#ifndef _WIN32
static bool use_ansi_codes = true;
#else
static bool use_ansi_codes = false;
#endif

static void do_or_die(lcb_error_t rc)
{
    if (rc != LCB_SUCCESS) {
        std::stringstream ss;
        ss << "[" << std::hex << rc << "] " << lcb_strerror(NULL, rc);
        throw std::runtime_error(ss.str());
    }
}

class Metrics {
public:
    Metrics()
    : n_rows(0), n_queries(0), n_errors(0), last_update(time(NULL)), hg(NULL)
    {
        #ifndef _WIN32
        if (pthread_mutex_init(&m_lock, NULL) != 0) {
            abort();
        }
        #endif
        start_time = last_update;
    }

    size_t nerrors() { return n_errors; }

    void update_row(size_t n = 1) { n_rows += n; update_display(); }
    void update_done(size_t n = 1) { n_queries += n; update_display(); }
    void update_error(size_t n = 1) { n_errors += n; update_display(); }

    void update_timings(lcb_U64 duration) {
        if (hg != NULL) {
            hg->record(duration);
        }
    }

#ifndef _WIN32
    bool is_tty() const { return isatty(STDOUT_FILENO); }
    void lock() { pthread_mutex_lock(&m_lock); }
    void unlock() { pthread_mutex_unlock(&m_lock); }
#else
    void lock(){}
    void unlock(){}
    bool is_tty() const { return false; }
#endif
    void prepare_screen()
    {
        if (is_tty() && use_ansi_codes) {
            printf("\n\n\n");
        }
    }

    void prepare_timings()
    {
        if (hg == NULL) {
            hg = new Histogram();
            hg->installStandalone(stdout);
        }
    }

private:
    void update_display()
    {
        time_t now = time(NULL);
        time_t duration = now - last_update;

        if (!duration) {
            return;
        }

        last_update = now;

        const char *prefix;
        const char *final_suffix;

        // Only use "ticker" style updates if we're a TTY and we have no
        // following timings.
        if (use_ansi_codes && is_tty() && hg == NULL) {
            // Move up 3 cursors
            printf("\x1B[2A");
            prefix = "\x1B[K";
            final_suffix = "\r";
        } else {
            // Determine the total number of time
            unsigned total_duration = now - start_time;
            printf("\n"); // Clear line..
            printf("+%us\n", total_duration);
            prefix = "";
            final_suffix = "\n";
        }

        printf("%sQUERIES/SEC: %lu\n", prefix, (unsigned long)(n_queries / duration));
        printf("%sROWS/SEC:    %lu\n", prefix, (unsigned long)(n_rows / duration));
        printf("%sERRORS:      %lu%s", prefix, (unsigned long)n_errors, final_suffix);

        if (hg != NULL) {
            hg->write();
        }
        fflush(stdout);

        n_queries = 0;
        n_rows = 0;
    }

    size_t n_rows;
    size_t n_queries;
    size_t n_errors;
    time_t last_update;
    time_t start_time;
    cbc::Histogram *hg;
#ifndef _WIN32
    pthread_mutex_t m_lock;
#endif
};

Metrics GlobalMetrics;

class Configuration
{
public:
    Configuration() : o_file("queryfile"), o_threads("num-threads"), o_errlog("error-log"), m_errlog(NULL) {
        o_file.mandatory(true);
        o_file.description(
            "Path to a file containing all the queries to execute. "
            "Each line should contain the full query body");
        o_file.abbrev('f');

        o_threads.description("Number of threads to run");
        o_threads.abbrev('t');
        o_threads.setDefault(1);

        o_errlog.description(
            "Path to a file containing failed queries");
        o_errlog.abbrev('e');
        o_errlog.setDefault("");
    }

    ~Configuration() {
        if (m_errlog != NULL) {
            delete m_errlog;
            m_errlog = NULL;
        }
    }
    void addToParser(Parser& parser)
    {
        parser.addOption(o_file);
        parser.addOption(o_threads);
        parser.addOption(o_errlog);
        m_params.addToParser(parser);
    }

    void processOptions()
    {
        std::ifstream ifs(o_file.const_result().c_str());
        if (!ifs.is_open()) {
            int ec_save = errno;
            string errstr(o_file.const_result());
            errstr += ": ";
            errstr += strerror(ec_save);
            throw std::runtime_error(errstr);
        }

        string curline;
        while (std::getline(ifs, curline).good() && !ifs.eof()) {
            if (!curline.empty()) {
                m_queries.push_back(curline);
            }
        }
        std::cerr << "Loaded " << m_queries.size() << " queries "
                  << "from \"" << o_file.const_result() << "\"" << std::endl;
        if (m_params.useTimings()) {
            GlobalMetrics.prepare_timings();
        }

        if (o_errlog.passed()) {
            m_errlog = new std::ofstream(o_errlog.const_result().c_str());
            if (!m_errlog->is_open()) {
                int ec_save = errno;
                string errstr(o_file.const_result());
                errstr += ": ";
                errstr += strerror(ec_save);
                throw std::runtime_error(errstr);
            }
        }
    }

    void set_cropts(lcb_create_st &opts) { m_params.fillCropts(opts); }
    const vector<string>& queries() const { return m_queries; }
    size_t nthreads() { return o_threads.result(); }
    std::ofstream* errlog() { return m_errlog; }

private:
    vector<string> m_queries;
    StringOption o_file;
    UIntOption o_threads;
    ConnParams m_params;
    StringOption o_errlog;
    std::ofstream *m_errlog;
};

extern "C" { static void n1qlcb(lcb_t, int, const lcb_RESPN1QL *resp); }
extern "C" { static void* pthrfunc(void*); }

class ThreadContext;
struct QueryContext {
    lcb_U64 begin;
    bool received; // whether any row was received
    ThreadContext *ctx; // Parent

    QueryContext(ThreadContext *tctx)
    : begin(lcb_nstime()), received(false), ctx(tctx) {}
};

class ThreadContext {
public:
    void run()
    {
        while (!m_cancelled) {
            vector<string>::const_iterator ii = m_queries.begin();
            for (; ii != m_queries.end(); ++ii) {
                run_one_query(*ii);
            }
        }
    }

#ifndef _WIN32
    void start()
    {
        assert(m_thr == NULL);
        m_thr = new pthread_t;
        int rc = pthread_create(m_thr, NULL, pthrfunc, this);
        if (rc != 0) {
            throw std::runtime_error(strerror(rc));
        }
    }

    void join()
    {
        assert(m_thr != NULL);
        void *arg = NULL;
        pthread_join(*m_thr, &arg);
    }

    ~ThreadContext()
    {
        if (m_thr != NULL) {
            join();
            delete m_thr;
            m_thr = NULL;
        }
    }
#else
    void start() { run(); }
    void join() {}
#endif

    void handle_response(const lcb_RESPN1QL *resp, QueryContext *ctx)
    {
        if (!ctx->received) {
            lcb_U64 duration = lcb_nstime() - ctx->begin;
            m_metrics->lock();
            m_metrics->update_timings(duration);
            m_metrics->unlock();

            ctx->received = true;
        }

        if (resp->rflags & LCB_RESP_F_FINAL) {
            if (resp->rc != LCB_SUCCESS) {
                if (m_errlog != NULL) {
                    std::stringstream ss;
                    ss.write(m_cmd.query, m_cmd.nquery);
                    ss << endl;
                    ss.write(resp->row, resp->nrow);
                    log_error(resp->rc, ss.str().c_str(), ss.str().size());
                } else {
                    log_error(resp->rc, NULL, 0);
                }
            }
        } else {
            last_nrow++;
        }
    }

    ThreadContext(lcb_t instance, const vector<string>& initial_queries, std::ofstream *errlog)
    : m_instance(instance), last_nerr(0), last_nrow(0),
      m_metrics(&GlobalMetrics), m_cancelled(false), m_thr(NULL),
      m_errlog(errlog)
    {
        memset(&m_cmd, 0, sizeof m_cmd);
        m_cmd.content_type = "application/json";
        m_cmd.callback = n1qlcb;

        // Shuffle the list
        m_queries = initial_queries;
        std::random_shuffle(m_queries.begin(), m_queries.end());
    }

private:

    void log_error(lcb_error_t err, const char* info, size_t ninfo)
    {
        size_t erridx;
        m_metrics->lock();
        m_metrics->update_error();
        erridx = m_metrics->nerrors();
        m_metrics->unlock();

        if (m_errlog != NULL) {
            std::stringstream ss;
            ss << "[" << erridx << "] 0x" << std::hex << err << ", "
               << lcb_strerror(NULL, err) << endl;
            if (ninfo) {
                ss.write(info, ninfo);
                ss << endl;
            }
            *m_errlog << ss.str();
            m_errlog->flush();
        }
    }

    void run_one_query(const string& txt)
    {
        // Reset counters
        last_nrow = 0;
        last_nerr = 0;

        m_cmd.query = txt.c_str();
        m_cmd.nquery = txt.size();

        // Set up our context
        QueryContext qctx(this);

        lcb_error_t rc = lcb_n1ql_query(m_instance, &qctx, &m_cmd);
        if (rc != LCB_SUCCESS) {
            log_error(rc, txt.c_str(), txt.size());
        } else {
            lcb_wait(m_instance);
            m_metrics->lock();
            m_metrics->update_row(last_nrow);
            m_metrics->update_done(1);
            m_metrics->unlock();
        }
    }

    lcb_t m_instance;
    vector<string> m_queries;
    size_t last_nerr;
    size_t last_nrow;
    lcb_CMDN1QL m_cmd;
    Metrics *m_metrics;
    volatile bool m_cancelled;
    #ifndef _WIN32
    pthread_t *m_thr;
    #else
    void *m_thr;
    #endif
    std::ofstream *m_errlog;
};

static void n1qlcb(lcb_t, int, const lcb_RESPN1QL *resp)
{
    QueryContext *qctx = reinterpret_cast<QueryContext*>(resp->cookie);
    qctx->ctx->handle_response(resp, qctx);
}

static void* pthrfunc(void *arg)
{
    reinterpret_cast<ThreadContext*>(arg)->run();
    return NULL;
}

static bool
instance_has_n1ql(lcb_t instance) {
    // Check that the instance supports N1QL
    lcbvb_CONFIG *vbc;
    do_or_die(lcb_cntl(instance, LCB_CNTL_GET, LCB_CNTL_VBCONFIG, &vbc));

    int sslmode = 0;
    lcbvb_SVCMODE svcmode = LCBVB_SVCMODE_PLAIN;

    do_or_die(lcb_cntl(instance, LCB_CNTL_GET, LCB_CNTL_SSL_MODE, &sslmode));

    if (sslmode & LCB_SSL_ENABLED) {
        svcmode = LCBVB_SVCMODE_SSL;
    }

    int hix = lcbvb_get_randhost(vbc, LCBVB_SVCTYPE_N1QL, svcmode);
    return hix > -1;
}

static void real_main(int argc, char **argv) {
    Configuration config;
    Parser parser;
    config.addToParser(parser);
    parser.parse(argc, argv);

    vector<ThreadContext*> threads;
    vector<lcb_t> instances;

    lcb_create_st cropts = { 0 };
    config.set_cropts(cropts);
    config.processOptions();

    for (size_t ii = 0; ii < config.nthreads(); ii++) {
        lcb_t instance;
        do_or_die(lcb_create(&instance, &cropts));
        do_or_die(lcb_connect(instance));
        lcb_wait(instance);
        do_or_die(lcb_get_bootstrap_status(instance));

        if (ii == 0 && !instance_has_n1ql(instance)) {
            throw std::runtime_error("Cluster does not support N1QL!");
        }

        ThreadContext* cx = new ThreadContext(instance, config.queries(), config.errlog());
        threads.push_back(cx);
        instances.push_back(instance);
    }

    GlobalMetrics.prepare_screen();

    for (size_t ii = 0; ii < threads.size(); ++ii) {
        threads[ii]->start();
    }
    for (size_t ii = 0; ii < threads.size(); ++ii) {
        threads[ii]->join();
    }
    for (size_t ii = 0; ii < instances.size(); ++ii) {
        lcb_destroy(instances[ii]);
    }
}

int main(int argc, char **argv)
{
    try {
        real_main(argc, argv);
        return 0;
    } catch (std::exception& exc) {
        cerr << exc.what() << endl;
        exit(EXIT_FAILURE);
    }
}
<commit_msg>CCBC-907: cbc-n1qlback: do not require trailing empty line for input<commit_after>/*
 *     Copyright 2015 Couchbase, 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.
 */

#define LCB_NO_DEPR_CXX_CTORS

#include "config.h"
#include <sys/types.h>
#include <libcouchbase/couchbase.h>
#include <libcouchbase/api3.h>
#include <libcouchbase/n1ql.h>
#include <libcouchbase/vbucket.h>
#include <iostream>
#include <list>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include <fstream>
#include <algorithm> // random_shuffle
#include <stdexcept>
#include <sstream>
#ifndef WIN32
#include <pthread.h>
#include <unistd.h> // isatty()
#endif
#include "common/options.h"
#include "common/histogram.h"

using namespace cbc;
using namespace cliopts;
using std::vector;
using std::string;
using std::cerr;
using std::endl;

#ifndef _WIN32
static bool use_ansi_codes = true;
#else
static bool use_ansi_codes = false;
#endif

static void do_or_die(lcb_error_t rc)
{
    if (rc != LCB_SUCCESS) {
        std::stringstream ss;
        ss << "[" << std::hex << rc << "] " << lcb_strerror(NULL, rc);
        throw std::runtime_error(ss.str());
    }
}

class Metrics {
public:
    Metrics()
    : n_rows(0), n_queries(0), n_errors(0), last_update(time(NULL)), hg(NULL)
    {
        #ifndef _WIN32
        if (pthread_mutex_init(&m_lock, NULL) != 0) {
            abort();
        }
        #endif
        start_time = last_update;
    }

    size_t nerrors() { return n_errors; }

    void update_row(size_t n = 1) { n_rows += n; update_display(); }
    void update_done(size_t n = 1) { n_queries += n; update_display(); }
    void update_error(size_t n = 1) { n_errors += n; update_display(); }

    void update_timings(lcb_U64 duration) {
        if (hg != NULL) {
            hg->record(duration);
        }
    }

#ifndef _WIN32
    bool is_tty() const { return isatty(STDOUT_FILENO); }
    void lock() { pthread_mutex_lock(&m_lock); }
    void unlock() { pthread_mutex_unlock(&m_lock); }
#else
    void lock(){}
    void unlock(){}
    bool is_tty() const { return false; }
#endif
    void prepare_screen()
    {
        if (is_tty() && use_ansi_codes) {
            printf("\n\n\n");
        }
    }

    void prepare_timings()
    {
        if (hg == NULL) {
            hg = new Histogram();
            hg->installStandalone(stdout);
        }
    }

private:
    void update_display()
    {
        time_t now = time(NULL);
        time_t duration = now - last_update;

        if (!duration) {
            return;
        }

        last_update = now;

        const char *prefix;
        const char *final_suffix;

        // Only use "ticker" style updates if we're a TTY and we have no
        // following timings.
        if (use_ansi_codes && is_tty() && hg == NULL) {
            // Move up 3 cursors
            printf("\x1B[2A");
            prefix = "\x1B[K";
            final_suffix = "\r";
        } else {
            // Determine the total number of time
            unsigned total_duration = now - start_time;
            printf("\n"); // Clear line..
            printf("+%us\n", total_duration);
            prefix = "";
            final_suffix = "\n";
        }

        printf("%sQUERIES/SEC: %lu\n", prefix, (unsigned long)(n_queries / duration));
        printf("%sROWS/SEC:    %lu\n", prefix, (unsigned long)(n_rows / duration));
        printf("%sERRORS:      %lu%s", prefix, (unsigned long)n_errors, final_suffix);

        if (hg != NULL) {
            hg->write();
        }
        fflush(stdout);

        n_queries = 0;
        n_rows = 0;
    }

    size_t n_rows;
    size_t n_queries;
    size_t n_errors;
    time_t last_update;
    time_t start_time;
    cbc::Histogram *hg;
#ifndef _WIN32
    pthread_mutex_t m_lock;
#endif
};

Metrics GlobalMetrics;

class Configuration
{
public:
    Configuration() : o_file("queryfile"), o_threads("num-threads"), o_errlog("error-log"), m_errlog(NULL) {
        o_file.mandatory(true);
        o_file.description(
            "Path to a file containing all the queries to execute. "
            "Each line should contain the full query body");
        o_file.abbrev('f');

        o_threads.description("Number of threads to run");
        o_threads.abbrev('t');
        o_threads.setDefault(1);

        o_errlog.description(
            "Path to a file containing failed queries");
        o_errlog.abbrev('e');
        o_errlog.setDefault("");
    }

    ~Configuration() {
        if (m_errlog != NULL) {
            delete m_errlog;
            m_errlog = NULL;
        }
    }
    void addToParser(Parser& parser)
    {
        parser.addOption(o_file);
        parser.addOption(o_threads);
        parser.addOption(o_errlog);
        m_params.addToParser(parser);
    }

    void processOptions()
    {
        std::ifstream ifs(o_file.const_result().c_str());
        if (!ifs.is_open()) {
            int ec_save = errno;
            string errstr(o_file.const_result());
            errstr += ": ";
            errstr += strerror(ec_save);
            throw std::runtime_error(errstr);
        }

        string curline;
        while (ifs.good()) {
            std::getline(ifs, curline);
            if (!curline.empty()) {
                m_queries.push_back(curline);
            }
        }
        std::cerr << "Loaded " << m_queries.size() << " queries "
                  << "from \"" << o_file.const_result() << "\"" << std::endl;
        if (m_params.useTimings()) {
            GlobalMetrics.prepare_timings();
        }

        if (o_errlog.passed()) {
            m_errlog = new std::ofstream(o_errlog.const_result().c_str());
            if (!m_errlog->is_open()) {
                int ec_save = errno;
                string errstr(o_file.const_result());
                errstr += ": ";
                errstr += strerror(ec_save);
                throw std::runtime_error(errstr);
            }
        }
    }

    void set_cropts(lcb_create_st &opts) { m_params.fillCropts(opts); }
    const vector<string>& queries() const { return m_queries; }
    size_t nthreads() { return o_threads.result(); }
    std::ofstream* errlog() { return m_errlog; }

private:
    vector<string> m_queries;
    StringOption o_file;
    UIntOption o_threads;
    ConnParams m_params;
    StringOption o_errlog;
    std::ofstream *m_errlog;
};

extern "C" { static void n1qlcb(lcb_t, int, const lcb_RESPN1QL *resp); }
extern "C" { static void* pthrfunc(void*); }

class ThreadContext;
struct QueryContext {
    lcb_U64 begin;
    bool received; // whether any row was received
    ThreadContext *ctx; // Parent

    QueryContext(ThreadContext *tctx)
    : begin(lcb_nstime()), received(false), ctx(tctx) {}
};

class ThreadContext {
public:
    void run()
    {
        while (!m_cancelled) {
            vector<string>::const_iterator ii = m_queries.begin();
            for (; ii != m_queries.end(); ++ii) {
                run_one_query(*ii);
            }
        }
    }

#ifndef _WIN32
    void start()
    {
        assert(m_thr == NULL);
        m_thr = new pthread_t;
        int rc = pthread_create(m_thr, NULL, pthrfunc, this);
        if (rc != 0) {
            throw std::runtime_error(strerror(rc));
        }
    }

    void join()
    {
        assert(m_thr != NULL);
        void *arg = NULL;
        pthread_join(*m_thr, &arg);
    }

    ~ThreadContext()
    {
        if (m_thr != NULL) {
            join();
            delete m_thr;
            m_thr = NULL;
        }
    }
#else
    void start() { run(); }
    void join() {}
#endif

    void handle_response(const lcb_RESPN1QL *resp, QueryContext *ctx)
    {
        if (!ctx->received) {
            lcb_U64 duration = lcb_nstime() - ctx->begin;
            m_metrics->lock();
            m_metrics->update_timings(duration);
            m_metrics->unlock();

            ctx->received = true;
        }

        if (resp->rflags & LCB_RESP_F_FINAL) {
            if (resp->rc != LCB_SUCCESS) {
                if (m_errlog != NULL) {
                    std::stringstream ss;
                    ss.write(m_cmd.query, m_cmd.nquery);
                    ss << endl;
                    ss.write(resp->row, resp->nrow);
                    log_error(resp->rc, ss.str().c_str(), ss.str().size());
                } else {
                    log_error(resp->rc, NULL, 0);
                }
            }
        } else {
            last_nrow++;
        }
    }

    ThreadContext(lcb_t instance, const vector<string>& initial_queries, std::ofstream *errlog)
    : m_instance(instance), last_nerr(0), last_nrow(0),
      m_metrics(&GlobalMetrics), m_cancelled(false), m_thr(NULL),
      m_errlog(errlog)
    {
        memset(&m_cmd, 0, sizeof m_cmd);
        m_cmd.content_type = "application/json";
        m_cmd.callback = n1qlcb;

        // Shuffle the list
        m_queries = initial_queries;
        std::random_shuffle(m_queries.begin(), m_queries.end());
    }

private:

    void log_error(lcb_error_t err, const char* info, size_t ninfo)
    {
        size_t erridx;
        m_metrics->lock();
        m_metrics->update_error();
        erridx = m_metrics->nerrors();
        m_metrics->unlock();

        if (m_errlog != NULL) {
            std::stringstream ss;
            ss << "[" << erridx << "] 0x" << std::hex << err << ", "
               << lcb_strerror(NULL, err) << endl;
            if (ninfo) {
                ss.write(info, ninfo);
                ss << endl;
            }
            *m_errlog << ss.str();
            m_errlog->flush();
        }
    }

    void run_one_query(const string& txt)
    {
        // Reset counters
        last_nrow = 0;
        last_nerr = 0;

        m_cmd.query = txt.c_str();
        m_cmd.nquery = txt.size();

        // Set up our context
        QueryContext qctx(this);

        lcb_error_t rc = lcb_n1ql_query(m_instance, &qctx, &m_cmd);
        if (rc != LCB_SUCCESS) {
            log_error(rc, txt.c_str(), txt.size());
        } else {
            lcb_wait(m_instance);
            m_metrics->lock();
            m_metrics->update_row(last_nrow);
            m_metrics->update_done(1);
            m_metrics->unlock();
        }
    }

    lcb_t m_instance;
    vector<string> m_queries;
    size_t last_nerr;
    size_t last_nrow;
    lcb_CMDN1QL m_cmd;
    Metrics *m_metrics;
    volatile bool m_cancelled;
    #ifndef _WIN32
    pthread_t *m_thr;
    #else
    void *m_thr;
    #endif
    std::ofstream *m_errlog;
};

static void n1qlcb(lcb_t, int, const lcb_RESPN1QL *resp)
{
    QueryContext *qctx = reinterpret_cast<QueryContext*>(resp->cookie);
    qctx->ctx->handle_response(resp, qctx);
}

static void* pthrfunc(void *arg)
{
    reinterpret_cast<ThreadContext*>(arg)->run();
    return NULL;
}

static bool
instance_has_n1ql(lcb_t instance) {
    // Check that the instance supports N1QL
    lcbvb_CONFIG *vbc;
    do_or_die(lcb_cntl(instance, LCB_CNTL_GET, LCB_CNTL_VBCONFIG, &vbc));

    int sslmode = 0;
    lcbvb_SVCMODE svcmode = LCBVB_SVCMODE_PLAIN;

    do_or_die(lcb_cntl(instance, LCB_CNTL_GET, LCB_CNTL_SSL_MODE, &sslmode));

    if (sslmode & LCB_SSL_ENABLED) {
        svcmode = LCBVB_SVCMODE_SSL;
    }

    int hix = lcbvb_get_randhost(vbc, LCBVB_SVCTYPE_N1QL, svcmode);
    return hix > -1;
}

static void real_main(int argc, char **argv) {
    Configuration config;
    Parser parser;
    config.addToParser(parser);
    parser.parse(argc, argv);

    vector<ThreadContext*> threads;
    vector<lcb_t> instances;

    lcb_create_st cropts = { 0 };
    config.set_cropts(cropts);
    config.processOptions();

    for (size_t ii = 0; ii < config.nthreads(); ii++) {
        lcb_t instance;
        do_or_die(lcb_create(&instance, &cropts));
        do_or_die(lcb_connect(instance));
        lcb_wait(instance);
        do_or_die(lcb_get_bootstrap_status(instance));

        if (ii == 0 && !instance_has_n1ql(instance)) {
            throw std::runtime_error("Cluster does not support N1QL!");
        }

        ThreadContext* cx = new ThreadContext(instance, config.queries(), config.errlog());
        threads.push_back(cx);
        instances.push_back(instance);
    }

    GlobalMetrics.prepare_screen();

    for (size_t ii = 0; ii < threads.size(); ++ii) {
        threads[ii]->start();
    }
    for (size_t ii = 0; ii < threads.size(); ++ii) {
        threads[ii]->join();
    }
    for (size_t ii = 0; ii < instances.size(); ++ii) {
        lcb_destroy(instances[ii]);
    }
}

int main(int argc, char **argv)
{
    try {
        real_main(argc, argv);
        return 0;
    } catch (std::exception& exc) {
        cerr << exc.what() << endl;
        exit(EXIT_FAILURE);
    }
}
<|endoftext|>


<commit_before>
//#include <google/profiler.h>

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/median.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/variance.hpp>
#include <boost/foreach.hpp>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <getopt.h>
#include <boost/foreach.hpp>
#include <sys/time.h>
#include <unistd.h>

#include "analyze.hpp"
#include "config.h"
#include "constants.hpp"
#include "fragment_model.hpp"
#include "linalg.hpp"
#include "logger.hpp"
#include "sampler.hpp"
#include "summarize.hpp"
#include "transcripts.hpp"

using namespace boost::accumulators;

const char* isolator_logo =
"     _           _       _\n"
"    (_)___  ___ | | __ _| |_ ___  _ __\n"
"    | / __|/ _ \\| |/ _` | __/ _ \\| '__|\n"
"    | \\__ \\ (_) | | (_| | || (_) | |\n"
"    |_|___/\\___/|_|\\__,_|\\__\\___/|_|\n";

static void print_logo()
{
    printf("%s\n     Version: %s\n     Instruction set: %s\n\n",
           isolator_logo, VERSION, LINALG_INSTR_SET);
}

void print_summarize_usage(FILE* fout)
{
    fprintf(fout, "Usage: isolator summarize [options] analyze_output.hdf5\n");
}

void print_summarize_help(FILE* fout)
{
    print_summarize_usage(fout);
    fprintf(fout,
            "\nOptions:\n"
            "-h, --help                Print this help message\n"
            "-v, --verbose             Print a bunch of information useful mainly for debugging.\n"
            "-o, --out=FILE            Output summary to the given file. (default: standard out).\n"
            "-l, --list                List summarization strategies.\n"
            "-s, --strategy            Summarization strategy.\n"
            "-a, --condition_a         First condition for pairwise comparisons.\n"
            "-b, --condition_b         First condition for pairwise comparisons.\n"
            "\n"
            "See 'isolator help summarize' for more.\n");
}

int isolator_summarize(int argc, char* argv[])
{
    Logger::start();

    static struct option long_options[] =
    {
        {"help",        no_argument,       NULL, 'h'},
        {"verbose",     no_argument,       NULL, 'v'},
        {"out",         required_argument, NULL, 'o'},
        {"list",        no_argument,       NULL, 'l'},
        {"strategy",    required_argument, NULL, 's'},
        {"condition_a", required_argument, NULL, 'a'},
        {"condition_b", required_argument, NULL, 'b'},
        {0, 0, 0, 0}
    };

    const char* out_fn = "isolator_summarize.tsv";
    Logger::level logger_level = Logger::INFO;
    unsigned int condition_a = 0, condition_b = 1;

    int opt;
    int opt_idx;

    const char* strategy_names[] = {
        "median_transcript_expression",
        "median_gene_expression",
        "condition_tgroup_mean",
        "experiment_tgroup_sd",
        "tgroup_fold_change",
        NULL
    };
    const char** s; // used in a couple places below

    size_t strategy = 0;

    while (true) {
        opt = getopt_long(argc, argv, "hvo:s:l", long_options, &opt_idx);

        if (opt == -1) break;

        switch (opt) {
            case 'h':
                print_summarize_help(stdout);
                return 0;

            case 'v':
                logger_level = Logger::DEBUG;
                break;

            case 'o':
                out_fn = optarg;
                break;

            case 'l':
                s = strategy_names;
                printf("Available strategies:\n");
                while (*s) {
                    printf("  %s\n", *s);
                    ++s;
                }
                return 0;

            case 's':
                s = strategy_names;
                strategy = 0;
                while (*s) {
                    if (strcmp(*s, optarg) == 0) {
                        break;
                    }
                    ++strategy;
                    ++s;
                }

                if (!*s) {
                    Logger::abort("No such strategy: %s", optarg);
                    return 1;
                }
                break;

            case 'a':
                condition_a = strtoul(optarg, NULL, 10);
                break;

            case 'b':
                condition_b = strtoul(optarg, NULL, 10);
                break;

            case '?':
                fprintf(stderr, "\n");
                print_summarize_help(stderr);
                return 1;

            default:
                abort();
        }
    }

    Logger::info("Outputing %s", strategy_names[strategy]);

    /* no positional argumens */
    if (optind == argc) {
        print_summarize_usage(stdout);
        return 0;
    }

    /* too many */
    else if (optind + 1 > argc) {
        fprintf(stderr, "Too many arguments.\n\n");
        print_summarize_usage(stderr);
        return 1;
    }

    FILE* out_f = stdout;
    if (out_fn) {
        out_f = fopen(out_fn, "w");
        if (out_f == NULL) {
            fprintf(stderr, "Can't open file %s for writing.\n", out_fn);
        }
    }

    const char* in_fn = argv[optind];

    Summarize summarize(in_fn);
    if (strategy == 0) {
        summarize.median_transcript_expression(out_f);
    }
    else if (strategy == 1) {
        summarize.median_gene_expression(out_f);
    }
    else if (strategy == 2) {
        summarize.median_condition_tgroup_expression(out_f);
    }
    else if (strategy == 3) {
        summarize.median_experiment_tgroup_sd(out_f);
    }
    else if (strategy == 4) {
        summarize.tgroup_fold_change(out_f, condition_a, condition_b);
    }

    fclose(out_f);

    Logger::end();
    return EXIT_SUCCESS;
}

void print_analyze_usage(FILE* fout)
{
    fprintf(fout, "Usage: isolator analyze [options] genes.gtf a1.bam[,a2.bam...] [b1.bam[,b2.bam...]]\n");
}

void print_analyze_help(FILE* fout)
{
    print_analyze_usage(fout);
    fprintf(fout,
        "\nOptions:\n"
        "-h, --help                Print this help message\n"
        "-v, --verbose             Print a bunch of information useful mainly for debugging\n"
        "-g, --genomic-seq=FILE    Correct for sequence bias, given the a the sequence\n"
        "                          against which the reads are aligned, in FAST format.\n"
        "-p, --threads=N           number of threads to use.\n"
        "    --no-gc-correction    disable post-hoc GC-content adjustments.\n"
        "-N, --num-samples         generate this many samples (default: 250)\n"
        "-B, --burnin              warmup for this many samples before collecting data (default: 100)\n\n"
        "See 'isolator help teste' for more.\n");
}

int isolator_analyze(int argc, char* argv[])
{
    static struct option long_options[] =
    {
        {"help",             no_argument,       NULL, 'h'},
        {"verbose",          no_argument,       NULL, 'v'},
        {"genomic-seq",      required_argument, NULL, 'g'},
        {"threads",          required_argument, NULL, 'p'},
        {"no-gc-correction", no_argument,       NULL, 0},
        {"num-samples",      required_argument, NULL, 'N'},
        {"burnin",           required_argument, NULL, 'B'},
        {0, 0, 0, 0}
    };

    Logger::level logger_level = Logger::INFO;
    unsigned int burnin = 100;
    unsigned int num_samples = 250;
    bool run_gc_correction = true;
    constants::num_threads = boost::thread::hardware_concurrency();
    const char* fa_fn  = NULL;

    int opt;
    int optidx;
    while (true) {
        opt = getopt_long(argc, argv, "hvg:p:N:", long_options, &optidx);

        if (opt == -1) break;

        switch (opt) {
            case 'h':
                print_analyze_help(stdout);
                return 0;

            case 'v':
                logger_level = Logger::DEBUG;
                break;

            case 'p':
                constants::num_threads = std::max(1, atoi(optarg));
                break;

            case 'g':
                fa_fn = optarg;
                break;

            case 'B':
                burnin = strtoul(optarg, NULL, 10);
                break;

            case 'N':
                num_samples = strtoul(optarg, NULL, 10);
                break;

            case 0:
                if (strcmp(long_options[optidx].name, "no-gc-correction") == 0) {
                    run_gc_correction = false;
                }
                break;

            case '?':
                fprintf(stderr, "\n");
                print_analyze_help(stderr);
                return 1;

            default:
                abort();
        }
    }

    // no positional argumens
    if (optind + 1 >= argc) {
        print_analyze_usage(stdout);
        return 0;
    }

    print_logo();
    Logger::start();
    Logger::set_level(logger_level);

    // read transcripts
    const char* gtf_fn = argv[optind++];
    TranscriptSet ts;
    FILE* gtf_f = fopen(gtf_fn, "rb");
    if (gtf_f == NULL) {
        Logger::abort("Can't open file %s for reading.", gtf_fn);
    }
    ts.read_gtf(gtf_f);
    fclose(gtf_f);

    // initialize
    Analyze analyze(burnin, num_samples, ts, fa_fn, run_gc_correction);
    int condition_num = 1;
    for (; optind < argc; ++optind) {
        char condition_name[100];
        snprintf(condition_name, sizeof(condition_name), "condition_%d", condition_num);

        const char* fn;
        for (fn = strtok(argv[optind], ","); fn; fn = strtok(NULL, ",")) {
            analyze.add_sample(condition_name, fn);
            Logger::info("Adding '%s' to condition '%s'", fn, condition_name);
        }

        ++condition_num;
    }

    analyze.run();

    Logger::info("Finished. Have a nice day!");
    Logger::end();

    return EXIT_SUCCESS;
}

void print_usage(FILE* fout)
{
    fprintf(fout,
            "Usage: isolator <command> [<args>]\n"
            "Isolator version %s\n"
            "Instruction set: %s\n\n"
            "Where <command> is one of:\n"
            "    analyze           Quantify and test for differential expression\n"
            "                      and splicing, among other things.\n"
            "    quantify          Quantify transcript abundance.\n"
            "    summarize         Summarize a sampler run.\n"
            "    report            Generate useful output from an quantify run.\n"
            "    help              Become enlightened.\n",
            VERSION, LINALG_INSTR_SET);
}

int isolator_help(int argc, char* argv[])
{
    if (argc <= 1) {
        print_usage(stdout);
        return EXIT_SUCCESS;
    }

    size_t cmdlen = 9 + strlen(argv[1]) + 1;
    char* cmd = new char [cmdlen];
    snprintf(cmd, cmdlen, "isolator-%s", argv[1]);

    execlp("man", "man", cmd, (char*) NULL);

    // if execlp returned, there was an error
    fprintf(stderr, "No suitable man page viewer found.\n");
    return EXIT_FAILURE;
}

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

    if (argc <= 1) {
        print_usage(stdout);
        return EXIT_SUCCESS;
    }

    ++argv;
    --argc;

    if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) {
        return isolator_help(argc, argv);
    }

    if (strcmp(argv[0], "summarize") == 0) {
        return isolator_summarize(argc, argv);
    }
    else if (strcmp(argv[0], "analyze") == 0) {
        return isolator_analyze(argc, argv);
    }
    else if (strcmp(argv[0], "help") == 0) {
        return isolator_help(argc, argv);
    }

    fprintf(stderr, "Unknown command %s.\n\n", argv[0]);
    return EXIT_FAILURE;
}

<commit_msg>Remove reference to the quantify command.<commit_after>
//#include <google/profiler.h>

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/median.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/variance.hpp>
#include <boost/foreach.hpp>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <getopt.h>
#include <boost/foreach.hpp>
#include <sys/time.h>
#include <unistd.h>

#include "analyze.hpp"
#include "config.h"
#include "constants.hpp"
#include "fragment_model.hpp"
#include "linalg.hpp"
#include "logger.hpp"
#include "sampler.hpp"
#include "summarize.hpp"
#include "transcripts.hpp"

using namespace boost::accumulators;

const char* isolator_logo =
"     _           _       _\n"
"    (_)___  ___ | | __ _| |_ ___  _ __\n"
"    | / __|/ _ \\| |/ _` | __/ _ \\| '__|\n"
"    | \\__ \\ (_) | | (_| | || (_) | |\n"
"    |_|___/\\___/|_|\\__,_|\\__\\___/|_|\n";

static void print_logo()
{
    printf("%s\n     Version: %s\n     Instruction set: %s\n\n",
           isolator_logo, VERSION, LINALG_INSTR_SET);
}

void print_summarize_usage(FILE* fout)
{
    fprintf(fout, "Usage: isolator summarize [options] analyze_output.hdf5\n");
}

void print_summarize_help(FILE* fout)
{
    print_summarize_usage(fout);
    fprintf(fout,
            "\nOptions:\n"
            "-h, --help                Print this help message\n"
            "-v, --verbose             Print a bunch of information useful mainly for debugging.\n"
            "-o, --out=FILE            Output summary to the given file. (default: standard out).\n"
            "-l, --list                List summarization strategies.\n"
            "-s, --strategy            Summarization strategy.\n"
            "-a, --condition_a         First condition for pairwise comparisons.\n"
            "-b, --condition_b         First condition for pairwise comparisons.\n"
            "\n"
            "See 'isolator help summarize' for more.\n");
}

int isolator_summarize(int argc, char* argv[])
{
    Logger::start();

    static struct option long_options[] =
    {
        {"help",        no_argument,       NULL, 'h'},
        {"verbose",     no_argument,       NULL, 'v'},
        {"out",         required_argument, NULL, 'o'},
        {"list",        no_argument,       NULL, 'l'},
        {"strategy",    required_argument, NULL, 's'},
        {"condition_a", required_argument, NULL, 'a'},
        {"condition_b", required_argument, NULL, 'b'},
        {0, 0, 0, 0}
    };

    const char* out_fn = "isolator_summarize.tsv";
    Logger::level logger_level = Logger::INFO;
    unsigned int condition_a = 0, condition_b = 1;

    int opt;
    int opt_idx;

    const char* strategy_names[] = {
        "median_transcript_expression",
        "median_gene_expression",
        "condition_tgroup_mean",
        "experiment_tgroup_sd",
        "tgroup_fold_change",
        NULL
    };
    const char** s; // used in a couple places below

    size_t strategy = 0;

    while (true) {
        opt = getopt_long(argc, argv, "hvo:s:l", long_options, &opt_idx);

        if (opt == -1) break;

        switch (opt) {
            case 'h':
                print_summarize_help(stdout);
                return 0;

            case 'v':
                logger_level = Logger::DEBUG;
                break;

            case 'o':
                out_fn = optarg;
                break;

            case 'l':
                s = strategy_names;
                printf("Available strategies:\n");
                while (*s) {
                    printf("  %s\n", *s);
                    ++s;
                }
                return 0;

            case 's':
                s = strategy_names;
                strategy = 0;
                while (*s) {
                    if (strcmp(*s, optarg) == 0) {
                        break;
                    }
                    ++strategy;
                    ++s;
                }

                if (!*s) {
                    Logger::abort("No such strategy: %s", optarg);
                    return 1;
                }
                break;

            case 'a':
                condition_a = strtoul(optarg, NULL, 10);
                break;

            case 'b':
                condition_b = strtoul(optarg, NULL, 10);
                break;

            case '?':
                fprintf(stderr, "\n");
                print_summarize_help(stderr);
                return 1;

            default:
                abort();
        }
    }

    Logger::info("Outputing %s", strategy_names[strategy]);

    /* no positional argumens */
    if (optind == argc) {
        print_summarize_usage(stdout);
        return 0;
    }

    /* too many */
    else if (optind + 1 > argc) {
        fprintf(stderr, "Too many arguments.\n\n");
        print_summarize_usage(stderr);
        return 1;
    }

    FILE* out_f = stdout;
    if (out_fn) {
        out_f = fopen(out_fn, "w");
        if (out_f == NULL) {
            fprintf(stderr, "Can't open file %s for writing.\n", out_fn);
        }
    }

    const char* in_fn = argv[optind];

    Summarize summarize(in_fn);
    if (strategy == 0) {
        summarize.median_transcript_expression(out_f);
    }
    else if (strategy == 1) {
        summarize.median_gene_expression(out_f);
    }
    else if (strategy == 2) {
        summarize.median_condition_tgroup_expression(out_f);
    }
    else if (strategy == 3) {
        summarize.median_experiment_tgroup_sd(out_f);
    }
    else if (strategy == 4) {
        summarize.tgroup_fold_change(out_f, condition_a, condition_b);
    }

    fclose(out_f);

    Logger::end();
    return EXIT_SUCCESS;
}

void print_analyze_usage(FILE* fout)
{
    fprintf(fout, "Usage: isolator analyze [options] genes.gtf a1.bam[,a2.bam...] [b1.bam[,b2.bam...]]\n");
}

void print_analyze_help(FILE* fout)
{
    print_analyze_usage(fout);
    fprintf(fout,
        "\nOptions:\n"
        "-h, --help                Print this help message\n"
        "-v, --verbose             Print a bunch of information useful mainly for debugging\n"
        "-g, --genomic-seq=FILE    Correct for sequence bias, given the a the sequence\n"
        "                          against which the reads are aligned, in FAST format.\n"
        "-p, --threads=N           number of threads to use.\n"
        "    --no-gc-correction    disable post-hoc GC-content adjustments.\n"
        "-N, --num-samples         generate this many samples (default: 250)\n"
        "-B, --burnin              warmup for this many samples before collecting data (default: 100)\n\n"
        "See 'isolator help teste' for more.\n");
}

int isolator_analyze(int argc, char* argv[])
{
    static struct option long_options[] =
    {
        {"help",             no_argument,       NULL, 'h'},
        {"verbose",          no_argument,       NULL, 'v'},
        {"genomic-seq",      required_argument, NULL, 'g'},
        {"threads",          required_argument, NULL, 'p'},
        {"no-gc-correction", no_argument,       NULL, 0},
        {"num-samples",      required_argument, NULL, 'N'},
        {"burnin",           required_argument, NULL, 'B'},
        {0, 0, 0, 0}
    };

    Logger::level logger_level = Logger::INFO;
    unsigned int burnin = 100;
    unsigned int num_samples = 250;
    bool run_gc_correction = true;
    constants::num_threads = boost::thread::hardware_concurrency();
    const char* fa_fn  = NULL;

    int opt;
    int optidx;
    while (true) {
        opt = getopt_long(argc, argv, "hvg:p:N:", long_options, &optidx);

        if (opt == -1) break;

        switch (opt) {
            case 'h':
                print_analyze_help(stdout);
                return 0;

            case 'v':
                logger_level = Logger::DEBUG;
                break;

            case 'p':
                constants::num_threads = std::max(1, atoi(optarg));
                break;

            case 'g':
                fa_fn = optarg;
                break;

            case 'B':
                burnin = strtoul(optarg, NULL, 10);
                break;

            case 'N':
                num_samples = strtoul(optarg, NULL, 10);
                break;

            case 0:
                if (strcmp(long_options[optidx].name, "no-gc-correction") == 0) {
                    run_gc_correction = false;
                }
                break;

            case '?':
                fprintf(stderr, "\n");
                print_analyze_help(stderr);
                return 1;

            default:
                abort();
        }
    }

    // no positional argumens
    if (optind + 1 >= argc) {
        print_analyze_usage(stdout);
        return 0;
    }

    print_logo();
    Logger::start();
    Logger::set_level(logger_level);

    // read transcripts
    const char* gtf_fn = argv[optind++];
    TranscriptSet ts;
    FILE* gtf_f = fopen(gtf_fn, "rb");
    if (gtf_f == NULL) {
        Logger::abort("Can't open file %s for reading.", gtf_fn);
    }
    ts.read_gtf(gtf_f);
    fclose(gtf_f);

    // initialize
    Analyze analyze(burnin, num_samples, ts, fa_fn, run_gc_correction);
    int condition_num = 1;
    for (; optind < argc; ++optind) {
        char condition_name[100];
        snprintf(condition_name, sizeof(condition_name), "condition_%d", condition_num);

        const char* fn;
        for (fn = strtok(argv[optind], ","); fn; fn = strtok(NULL, ",")) {
            analyze.add_sample(condition_name, fn);
            Logger::info("Adding '%s' to condition '%s'", fn, condition_name);
        }

        ++condition_num;
    }

    analyze.run();

    Logger::info("Finished. Have a nice day!");
    Logger::end();

    return EXIT_SUCCESS;
}

void print_usage(FILE* fout)
{
    fprintf(fout,
            "Usage: isolator <command> [<args>]\n"
            "Isolator version %s\n"
            "Instruction set: %s\n\n"
            "Where <command> is one of:\n"
            "    analyze           Quantify and test for differential expression\n"
            "                      and splicing, among other things.\n"
            "    summarize         Summarize a sampler run.\n"
            "    report            Generate useful output from an analyze run.\n"
            "    help              Become enlightened.\n",
            VERSION, LINALG_INSTR_SET);
}

int isolator_help(int argc, char* argv[])
{
    if (argc <= 1) {
        print_usage(stdout);
        return EXIT_SUCCESS;
    }

    size_t cmdlen = 9 + strlen(argv[1]) + 1;
    char* cmd = new char [cmdlen];
    snprintf(cmd, cmdlen, "isolator-%s", argv[1]);

    execlp("man", "man", cmd, (char*) NULL);

    // if execlp returned, there was an error
    fprintf(stderr, "No suitable man page viewer found.\n");
    return EXIT_FAILURE;
}

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

    if (argc <= 1) {
        print_usage(stdout);
        return EXIT_SUCCESS;
    }

    ++argv;
    --argc;

    if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) {
        return isolator_help(argc, argv);
    }

    if (strcmp(argv[0], "summarize") == 0) {
        return isolator_summarize(argc, argv);
    }
    else if (strcmp(argv[0], "analyze") == 0) {
        return isolator_analyze(argc, argv);
    }
    else if (strcmp(argv[0], "help") == 0) {
        return isolator_help(argc, argv);
    }

    fprintf(stderr, "Unknown command %s.\n\n", argv[0]);
    return EXIT_FAILURE;
}

<|endoftext|>


<commit_before>// This file generated by nlsinfo -H -N FBNLS ../src ../util, on Mon Jun 21 00:15:24 2004

#ifndef FLUXBOX_NLS_HH
#define FLUXBOX_NLS_HH

namespace FBNLS {

enum {
	AlignSet = 1,
	AlignBottomCenter = 1,
	AlignBottomLeft = 2,
	AlignBottomRight = 3,
	AlignHorizontal = 4,
	AlignLeft = 5,
	AlignLeftBottom = 6,
	AlignLeftCenter = 7,
	AlignLeftTop = 8,
	AlignRelative = 9,
	AlignRight = 10,
	AlignRightBottom = 11,
	AlignRightCenter = 12,
	AlignRightTop = 13,
	AlignTopCenter = 14,
	AlignTopLeft = 15,
	AlignTopRight = 16,
	AlignVertical = 17,

	BaseDisplaySet = 2,
	BaseDisplayAborting = 1,
	BaseDisplayShuttingDown = 2,
	BaseDisplaySignalCaught = 3,

	CommonSet = 3,
	CommonAlpha = 1,
	CommonAutoHide = 2,
	CommonCompiled = 3,
	CommonCompiledOptions = 4,
	CommonCompiler = 5,
	CommonCompilerVersion = 6,
	CommonDefaultInitFile = 7,
	CommonDefaultKeyFile = 8,
	CommonDefaultLocalePath = 9,
	CommonDefaultMenuFile = 10,
	CommonDefaultStyle = 11,
	CommonDefaults = 12,
	CommonDisabled = 13,
	CommonError = 14,
	CommonFluxboxVersion = 15,
	CommonMaximizeOver = 16,
	CommonSvnRevision = 17,
	CommonVisible = 18,
    CommonBackgroundWarning = 19,

	ConfigmenuSet = 4,
	ConfigmenuAntiAlias = 1,
	ConfigmenuAutoRaise = 2,
	ConfigmenuClickRaises = 3,
	ConfigmenuClickFocus = 4,
	ConfigmenuDecorateTransient = 5,
	ConfigmenuDesktopWheeling = 6,
	ConfigmenuFocusLast = 7,
	ConfigmenuFocusModel = 8,
	ConfigmenuFocusNew = 9,
	ConfigmenuFullMax = 10,
	ConfigmenuImageDithering = 11,
	ConfigmenuOpaqueMove = 12,
// disabled, obsolete:	ConfigmenuSemiSloppyFocus = 13,
	ConfigmenuMouseFocus = 14,
	ConfigmenuWorkspaceWarping = 15,
	ConfigmenuForcePseudoTrans = 16,
	ConfigmenuMenuAlpha = 17,
	ConfigmenuTransparency = 18,
	ConfigmenuFocusedAlpha = 19,
	ConfigmenuUnfocusedAlpha = 20,
	ConfigmenuTabMenu = 21,
	ConfigmenuTabsInTitlebar = 22,

	EwmhSet = 5,
	EwmhOutOfMemoryClientList = 1,

	FbTkErrorSet = 6,
	FbTkErrorBigHeight = 1,
	FbTkErrorBigWidth = 2,
	FbTkErrorCantFallbackFont = 3,
	FbTkErrorColorAllocation = 4,
	FbTkErrorCreatePixmap = 5,
	FbTkErrorCreateXImage = 6,
	FbTkErrorFailedRead = 7,
	FbTkErrorNoRenderFormat = 8,
	FbTkErrorNoRenderPicture = 9,
	FbTkErrorNoRenderPixmap = 10,
	FbTkErrorNoRenderVisualFormat = 11,
	FbTkErrorOutOfMemory = 12,
	FbTkErrorOutOfMemoryBlue = 13,
	FbTkErrorOutOfMemoryGreen = 14,
	FbTkErrorOutOfMemoryRed = 15,
	FbTkErrorThemeItem = 16,
	FbTkErrorUnsupportedVisual = 17,
	FbTkErrorUsingDefault = 18,

	FluxboxSet = 7,
	FluxboxBadRCFile = 1,
	FluxboxCantLoadGroupFile = 2,
	FluxboxCantLoadRCFile = 3,
	FluxboxCantLoadRCFileTrying = 4,
	FluxboxCantMapWindow = 5,
	FluxboxErrorCreatingDirectory = 6,
	FluxboxErrorNoScreens = 7,
	FluxboxErrorRegexp = 8,
	FluxboxFatalSingleton = 9,
	FluxboxNoDisplay = 10,
	FluxboxWarningLocale = 11,
	FluxboxWarningLocaleModifiers = 12,

	GnomeSet = 8,
	GnomeOutOfMemoryClientList = 1,

	KeysSet = 9,
	KeysBadLine = 1,
	KeysBadMerge = 2,
	KeysInvalidKeyMod = 3,

	MenuSet = 10,
	MenuConfiguration = 1,
	MenuDefaultRootMenu = 2,
	MenuExit = 3,
	MenuIcons = 4,
	MenuLayer = 5,
	MenuOnHead = 6,
	MenuPlacement = 7,
	MenuReconfigure = 8,
	MenuRestart = 9,

	RememberSet = 11,
	RememberDecorations = 1,
	RememberDimensions = 2,
	RememberJumpToWorkspace = 3,
	RememberLayer = 4,
	RememberMenuItemName = 5,
	RememberPosition = 6,
	RememberSaveOnClose = 7,
	RememberShaded = 8,
	RememberSticky = 9,
	RememberUnknown = 10,
	RememberWorkspace = 11,
    RememberHead = 12,

	ScreenSet = 12,
	ScreenAnotherWMRunning = 1,
	ScreenGeometryFormat = 2,
	ScreenManagingScreen = 3,

	SlitSet = 13,
	SlitClientsMenu = 1,
	SlitCycleDown = 2,
	SlitCycleUp = 3,
	SlitDirection = 4,
	SlitLayer = 5,
	SlitOnHead = 6,
	SlitPlacement = 7,
	SlitSlit = 8,
    SlitSaveSlitList = 9,

	ToolbarSet = 14,
	ToolbarEditWkspcName = 1,
	ToolbarIconbarMode = 2,
	ToolbarIconbarModeAllWindows = 3,
	ToolbarIconbarModeIcons = 4,
	ToolbarIconbarModeNone = 5,
	ToolbarIconbarModeWorkspace = 6,
	ToolbarIconbarModeWorkspaceIcons = 7,
	ToolbarLayer = 8,
	ToolbarOnHead = 9,
	ToolbarPlacement = 10,
	ToolbarToolbar = 11,
	ToolbarWidthPercent = 12,
	ToolbarClock24 = 13,
	ToolbarClock12 = 14,
	ToolbarClockEditFormat = 15,
	ToolbarShowIcons = 16,
    ToolbarIconbarModeNoIcons = 17,
    ToolbarIconbarModeWorkspaceNoIcons = 18,

	WindowSet = 15,
	WindowUnnamed = 1,

	WindowmenuSet = 16,
	WindowmenuClose = 1,
	WindowmenuIconify = 2,
	WindowmenuLayer = 3,
	WindowmenuLower = 4,
	WindowmenuMaximize = 5,
	WindowmenuRaise = 6,
	WindowmenuSendTo = 7,
	WindowmenuShade = 8,
	WindowmenuStick = 9,
    WindowmenuKill = 10,

	WorkspaceSet = 17,
	WorkspaceDefaultNameFormat = 1,
	WorkspaceMenuTitle = 2,
	WorkspaceNewWorkspace = 3,
	WorkspaceRemoveLast = 4,

	fbsetrootSet = 18,
	fbsetrootMustSpecify = 1,
	fbsetrootNoPixmapAtoms = 2,
	fbsetrootUsage = 3,

	mainSet = 19,
	mainDISPLAYRequiresArg = 1,
	mainErrorBadAlloc = 2,
	mainErrorBadCast = 3,
	mainErrorOutOfRange = 4,
	mainErrorRuntime = 5,
	mainErrorStandardException = 6,
	mainErrorUnknown = 7,
	mainLOGRequiresArg = 8,
	mainLogFile = 9,
	mainLoggingTo = 10,
	mainRCRequiresArg = 11,
	mainScreenRequiresArg = 12,
	mainUsage = 13,
	mainWarnDisplayEnv = 14,

	LayerSet = 20,
	LayerAboveDock = 1,
	LayerBottom = 2,
	LayerDesktop = 3,
	LayerDock = 4,
	LayerNormal = 5,
	LayerTop = 6,

	dummy_not_used = 0 // just for the end

}; // end enum

}; // end namespace FBNLS

#endif // FLUXBOX_NLS_HH
<commit_msg>missing nls string id, thanks Michael 'buk' Scherer<commit_after>// This file generated by nlsinfo -H -N FBNLS ../src ../util, on Mon Jun 21 00:15:24 2004

#ifndef FLUXBOX_NLS_HH
#define FLUXBOX_NLS_HH

namespace FBNLS {

enum {
	AlignSet = 1,
	AlignBottomCenter = 1,
	AlignBottomLeft = 2,
	AlignBottomRight = 3,
	AlignHorizontal = 4,
	AlignLeft = 5,
	AlignLeftBottom = 6,
	AlignLeftCenter = 7,
	AlignLeftTop = 8,
	AlignRelative = 9,
	AlignRight = 10,
	AlignRightBottom = 11,
	AlignRightCenter = 12,
	AlignRightTop = 13,
	AlignTopCenter = 14,
	AlignTopLeft = 15,
	AlignTopRight = 16,
	AlignVertical = 17,

	BaseDisplaySet = 2,
	BaseDisplayAborting = 1,
	BaseDisplayShuttingDown = 2,
	BaseDisplaySignalCaught = 3,

	CommonSet = 3,
	CommonAlpha = 1,
	CommonAutoHide = 2,
	CommonCompiled = 3,
	CommonCompiledOptions = 4,
	CommonCompiler = 5,
	CommonCompilerVersion = 6,
	CommonDefaultInitFile = 7,
	CommonDefaultKeyFile = 8,
	CommonDefaultLocalePath = 9,
	CommonDefaultMenuFile = 10,
	CommonDefaultStyle = 11,
	CommonDefaults = 12,
	CommonDisabled = 13,
	CommonError = 14,
	CommonFluxboxVersion = 15,
	CommonMaximizeOver = 16,
	CommonSvnRevision = 17,
	CommonVisible = 18,
	CommonBackgroundWarning = 19,

	ConfigmenuSet = 4,
	ConfigmenuAntiAlias = 1,
	ConfigmenuAutoRaise = 2,
	ConfigmenuClickRaises = 3,
	ConfigmenuClickFocus = 4,
	ConfigmenuDecorateTransient = 5,
	ConfigmenuDesktopWheeling = 6,
	ConfigmenuFocusLast = 7,
	ConfigmenuFocusModel = 8,
	ConfigmenuFocusNew = 9,
	ConfigmenuFullMax = 10,
	ConfigmenuImageDithering = 11,
	ConfigmenuOpaqueMove = 12,
// disabled, obsolete:	ConfigmenuSemiSloppyFocus = 13,
	ConfigmenuMouseFocus = 14,
	ConfigmenuWorkspaceWarping = 15,
	ConfigmenuForcePseudoTrans = 16,
	ConfigmenuMenuAlpha = 17,
	ConfigmenuTransparency = 18,
	ConfigmenuFocusedAlpha = 19,
	ConfigmenuUnfocusedAlpha = 20,
	ConfigmenuTabMenu = 21,
	ConfigmenuTabsInTitlebar = 22,
	ConfigmenuExternalTabWidth = 23,

	EwmhSet = 5,
	EwmhOutOfMemoryClientList = 1,

	FbTkErrorSet = 6,
	FbTkErrorBigHeight = 1,
	FbTkErrorBigWidth = 2,
	FbTkErrorCantFallbackFont = 3,
	FbTkErrorColorAllocation = 4,
	FbTkErrorCreatePixmap = 5,
	FbTkErrorCreateXImage = 6,
	FbTkErrorFailedRead = 7,
	FbTkErrorNoRenderFormat = 8,
	FbTkErrorNoRenderPicture = 9,
	FbTkErrorNoRenderPixmap = 10,
	FbTkErrorNoRenderVisualFormat = 11,
	FbTkErrorOutOfMemory = 12,
	FbTkErrorOutOfMemoryBlue = 13,
	FbTkErrorOutOfMemoryGreen = 14,
	FbTkErrorOutOfMemoryRed = 15,
	FbTkErrorThemeItem = 16,
	FbTkErrorUnsupportedVisual = 17,
	FbTkErrorUsingDefault = 18,

	FluxboxSet = 7,
	FluxboxBadRCFile = 1,
	FluxboxCantLoadGroupFile = 2,
	FluxboxCantLoadRCFile = 3,
	FluxboxCantLoadRCFileTrying = 4,
	FluxboxCantMapWindow = 5,
	FluxboxErrorCreatingDirectory = 6,
	FluxboxErrorNoScreens = 7,
	FluxboxErrorRegexp = 8,
	FluxboxFatalSingleton = 9,
	FluxboxNoDisplay = 10,
	FluxboxWarningLocale = 11,
	FluxboxWarningLocaleModifiers = 12,

	GnomeSet = 8,
	GnomeOutOfMemoryClientList = 1,

	KeysSet = 9,
	KeysBadLine = 1,
	KeysBadMerge = 2,
	KeysInvalidKeyMod = 3,

	MenuSet = 10,
	MenuConfiguration = 1,
	MenuDefaultRootMenu = 2,
	MenuExit = 3,
	MenuIcons = 4,
	MenuLayer = 5,
	MenuOnHead = 6,
	MenuPlacement = 7,
	MenuReconfigure = 8,
	MenuRestart = 9,

	RememberSet = 11,
	RememberDecorations = 1,
	RememberDimensions = 2,
	RememberJumpToWorkspace = 3,
	RememberLayer = 4,
	RememberMenuItemName = 5,
	RememberPosition = 6,
	RememberSaveOnClose = 7,
	RememberShaded = 8,
	RememberSticky = 9,
	RememberUnknown = 10,
	RememberWorkspace = 11,
	RememberHead = 12,

	ScreenSet = 12,
	ScreenAnotherWMRunning = 1,
	ScreenGeometryFormat = 2,
	ScreenManagingScreen = 3,

	SlitSet = 13,
	SlitClientsMenu = 1,
	SlitCycleDown = 2,
	SlitCycleUp = 3,
	SlitDirection = 4,
	SlitLayer = 5,
	SlitOnHead = 6,
	SlitPlacement = 7,
	SlitSlit = 8,
	SlitSaveSlitList = 9,

	ToolbarSet = 14,
	ToolbarEditWkspcName = 1,
	ToolbarIconbarMode = 2,
	ToolbarIconbarModeAllWindows = 3,
	ToolbarIconbarModeIcons = 4,
	ToolbarIconbarModeNone = 5,
	ToolbarIconbarModeWorkspace = 6,
	ToolbarIconbarModeWorkspaceIcons = 7,
	ToolbarLayer = 8,
	ToolbarOnHead = 9,
	ToolbarPlacement = 10,
	ToolbarToolbar = 11,
	ToolbarWidthPercent = 12,
	ToolbarClock24 = 13,
	ToolbarClock12 = 14,
	ToolbarClockEditFormat = 15,
	ToolbarShowIcons = 16,
	ToolbarIconbarModeNoIcons = 17,
	ToolbarIconbarModeWorkspaceNoIcons = 18,

	WindowSet = 15,
	WindowUnnamed = 1,

	WindowmenuSet = 16,
	WindowmenuClose = 1,
	WindowmenuIconify = 2,
	WindowmenuLayer = 3,
	WindowmenuLower = 4,
	WindowmenuMaximize = 5,
	WindowmenuRaise = 6,
	WindowmenuSendTo = 7,
	WindowmenuShade = 8,
	WindowmenuStick = 9,
	WindowmenuKill = 10,

	WorkspaceSet = 17,
	WorkspaceDefaultNameFormat = 1,
	WorkspaceMenuTitle = 2,
	WorkspaceNewWorkspace = 3,
	WorkspaceRemoveLast = 4,

	fbsetrootSet = 18,
	fbsetrootMustSpecify = 1,
	fbsetrootNoPixmapAtoms = 2,
	fbsetrootUsage = 3,

	mainSet = 19,
	mainDISPLAYRequiresArg = 1,
	mainErrorBadAlloc = 2,
	mainErrorBadCast = 3,
	mainErrorOutOfRange = 4,
	mainErrorRuntime = 5,
	mainErrorStandardException = 6,
	mainErrorUnknown = 7,
	mainLOGRequiresArg = 8,
	mainLogFile = 9,
	mainLoggingTo = 10,
	mainRCRequiresArg = 11,
	mainScreenRequiresArg = 12,
	mainUsage = 13,
	mainWarnDisplayEnv = 14,

	LayerSet = 20,
	LayerAboveDock = 1,
	LayerBottom = 2,
	LayerDesktop = 3,
	LayerDock = 4,
	LayerNormal = 5,
	LayerTop = 6,

	dummy_not_used = 0 // just for the end

}; // end enum

}; // end namespace FBNLS

#endif // FLUXBOX_NLS_HH
<|endoftext|>


<commit_before>/*Fichier conçu pour la génération de la documentation automatique avec
* Doxygen.
*/
//! \file **********************************************************************
//!
//! \brief Implémentation pour une utilisation facile de l'usb.
//!
//! - Compilateur		: GCC (C++11)
//!
//! \author Maleyrie Antoine : antoine.maleyrie@gmail.com
//! \version 0.3
//! \date 26 Août 2011
//!
//! ****************************************************************************

/*
*	Copyright © 2011-1012 - Antoine Maleyrie.
*/

//_____ I N C L U D E S ________________________________________________________
#include <iostream>
#include "easyusb.h"

//_____ M A C R O S ____________________________________________________________

//_____ I M P L E M E N T A T I O N S __________________________________________

EasyUsb::EasyUsb() : _ctx(nullptr), _device(nullptr)
{
	// On construit un contexte pour utiliser l'usb.
	if(libusb_init(&_ctx) != 0)
	{
		std::cerr << "EasyUsb::EasyUsb libusb initialisation failed." << std::endl;
		libusb_set_debug(_ctx, 3);
	}
}

EasyUsb::~EasyUsb()
{
	disconnect();
	libusb_exit(_ctx);
}

bool EasyUsb::connect(uint16_t vendor_id, uint16_t product_id)
{
	//On déconnecté si il est déjà connecter.
	disconnect();

	//Récupéré le device lier au id.
	_device = libusb_open_device_with_vid_pid(_ctx, vendor_id, product_id);
	//Si erreur ou si le device n'a pas été trouver.
	if(_device == nullptr)
	{
		std::cerr << "EasyUsb::connect The device is not found or error." << std::endl;
		return false;
	}

	//Vérifie si un driver n'est pas déjà charger.
	if(libusb_kernel_driver_active(_device, 0) == 1)
	{
		//On détache le driver.
		libusb_detach_kernel_driver(_device, 0);
	}

	//Reset le périphérie.
	reset();

	return true;
}

void EasyUsb::disconnect(void)
{
	//Si il existe un périphérie.
	if(_device != nullptr)
	{
		//Reset le périphérie.
		reset();

		//Met fin au périphérie.
		libusb_close(_device);
		_device = nullptr;
	}
}

bool EasyUsb::reset(void)
{
	if(libusb_reset_device(_device) != 0)
	{
		std::cerr << "EasyUsb::reset Failed the reset device." << std::endl;
		return false;
	}

	return true;
}

bool EasyUsb::write(unsigned char data[], int length)
{
	//Si il n'existe pas de périphérie.
	if(_device == nullptr)
	{
		std::cerr << "EasyUsb::write No device for write." << std::endl;
		return false;
	}

	//On demande à communiquer.
	if(libusb_claim_interface(_device, 1) != 0)
	{
		//On ce déconnecte si erreur.
		disconnect();
		std::cerr << "EasyUsb::write The claim has failed." << std::endl;
		return false;
	}

	//Pour vérifier le nombre de donner réellement écrite.
	int transferred = 0;

	//Écriture des données.
	if(libusb_bulk_transfer(_device, (2|LIBUSB_ENDPOINT_OUT), data, length, &transferred, 0) != 0)
	{
		std::cerr << "EasyUsb::write Error has write the data." << std::endl;
		return false;
	}

	//On ne veut plus communiquer.
	if(libusb_release_interface(_device, 1) != 0)
	{
		std::cerr << "EasyUsb::write The release has failed." << std::endl;
		return false;
	}

	//Vérifier le nombre de donner réellement écrite.
	if(transferred != length)
	{
		std::cerr << "EasyUsb::write The data numbers write is invalid." << std::endl;
		return false;
	}

	return true;
}

bool EasyUsb::read(unsigned char data[], int sizeData, int * length)
{
	//Si il n'existe pas de périphérie.
	if(_device == nullptr)
	{
		std::cerr << "EasyUsb::read No device for read." << std::endl;
		return false;
	}

	//On demande à communiquer.
	if(libusb_claim_interface(_device, 1) != 0)
	{
		//On ce déconnecte si erreur.
		disconnect();
		std::cerr << "EasyUsb::read The claim has failed." << std::endl;
		return false;
	}

	//Lecture des données.
	if(libusb_bulk_transfer(_device, (1|LIBUSB_ENDPOINT_IN), data, *length, &sizeData, 0) != 0)
	{
		std::cerr << "EasyUsb::read Error has read the data." << std::endl;
		return false;
	}

	//On ne veut plus communiquer.
	if(libusb_release_interface(_device, 1) != 0)
	{
		std::cerr << "EasyUsb::read The release has failed." << std::endl;
		return false;
	}

	return true;
}

<commit_msg>correction for last modification<commit_after>/*Fichier conçu pour la génération de la documentation automatique avec
* Doxygen.
*/
//! \file **********************************************************************
//!
//! \brief Implémentation pour une utilisation facile de l'usb.
//!
//! - Compilateur		: GCC (C++11)
//!
//! \author Maleyrie Antoine : antoine.maleyrie@gmail.com
//! \version 0.3
//! \date 26 Août 2011
//!
//! ****************************************************************************

/*
*	Copyright © 2011-1012 - Antoine Maleyrie.
*/

//_____ I N C L U D E S ________________________________________________________
#include <iostream>
#include "easyusb.h"

//_____ M A C R O S ____________________________________________________________

//_____ I M P L E M E N T A T I O N S __________________________________________

EasyUsb::EasyUsb() : _ctx(nullptr), _device(nullptr)
{
	// On construit un contexte pour utiliser l'usb.
	if(libusb_init(&_ctx) != 0)
	{
		std::cerr << "EasyUsb::EasyUsb libusb initialisation failed." << std::endl;
		libusb_set_debug(_ctx, 3);
	}
}

EasyUsb::~EasyUsb()
{
	disconnect();
	libusb_exit(_ctx);
}

bool EasyUsb::connect(uint16_t vendor_id, uint16_t product_id)
{
	//On déconnecté si il est déjà connecter.
	disconnect();

	//Récupéré le device lier au id.
	_device = libusb_open_device_with_vid_pid(_ctx, vendor_id, product_id);
	//Si erreur ou si le device n'a pas été trouver.
	if(_device == nullptr)
	{
		std::cerr << "EasyUsb::connect The device is not found or error." << std::endl;
		return false;
	}

	//Vérifie si un driver n'est pas déjà charger.
	if(libusb_kernel_driver_active(_device, 0) == 1)
	{
		//On détache le driver.
		libusb_detach_kernel_driver(_device, 0);
	}

	//Reset le périphérie.
	reset();

	return true;
}

void EasyUsb::disconnect(void)
{
	//Si il existe un périphérie.
	if(_device != nullptr)
	{
		//Reset le périphérie.
		reset();

		//Met fin au périphérie.
		libusb_close(_device);
		_device = nullptr;
	}
}

bool EasyUsb::reset(void)
{
	if(libusb_reset_device(_device) != 0)
	{
		std::cerr << "EasyUsb::reset Failed the reset device." << std::endl;
		return false;
	}

	return true;
}

bool EasyUsb::write(unsigned char data[], int length)
{
	//Si il n'existe pas de périphérie.
	if(_device == nullptr)
	{
		std::cerr << "EasyUsb::write No device for write." << std::endl;
		return false;
	}

	//On demande à communiquer.
	if(libusb_claim_interface(_device, 1) != 0)
	{
		//On ce déconnecte si erreur.
		disconnect();
		std::cerr << "EasyUsb::write The claim has failed." << std::endl;
		return false;
	}

	//Pour vérifier le nombre de donner réellement écrite.
	int transferred = 0;

	//Écriture des données.
	if(libusb_bulk_transfer(_device, (2|LIBUSB_ENDPOINT_OUT), data, length, &transferred, 0) != 0)
	{
		std::cerr << "EasyUsb::write Error has write the data." << std::endl;
		return false;
	}

	//On ne veut plus communiquer.
	if(libusb_release_interface(_device, 1) != 0)
	{
		std::cerr << "EasyUsb::write The release has failed." << std::endl;
		return false;
	}

	//Vérifier le nombre de donner réellement écrite.
	if(transferred != length)
	{
		std::cerr << "EasyUsb::write The data numbers write is invalid." << std::endl;
		return false;
	}

	return true;
}

bool EasyUsb::read(unsigned char data[], int sizeData, int * length)
{
	//Si il n'existe pas de périphérie.
	if(_device == nullptr)
	{
		std::cerr << "EasyUsb::read No device for read." << std::endl;
		return false;
	}

	//On demande à communiquer.
	if(libusb_claim_interface(_device, 1) != 0)
	{
		//On ce déconnecte si erreur.
		disconnect();
		std::cerr << "EasyUsb::read The claim has failed." << std::endl;
		return false;
	}

	//Lecture des données.
	if(libusb_bulk_transfer(_device, (1|LIBUSB_ENDPOINT_IN), data, sizeData, length, 0) != 0)
	{
		std::cerr << "EasyUsb::read Error has read the data." << std::endl;
		return false;
	}

	//On ne veut plus communiquer.
	if(libusb_release_interface(_device, 1) != 0)
	{
		std::cerr << "EasyUsb::read The release has failed." << std::endl;
		return false;
	}

	return true;
}

<|endoftext|>