<commit_before>// Author: Danilo Piparo, Omar Zapata   16/12/2015

#include <unistd.h>
#include <fcntl.h>
#include <string>
#include "TInterpreter.h"

bool JupyROOTExecutorImpl(const char *code);
bool JupyROOTDeclarerImpl(const char *code);

class JupyROOTExecutorHandler {
   private:
      bool fCapturing=false;
      std::string fStdoutpipe;
      std::string fStderrpipe;
      int fStdout_pipe[2];
      int fStderr_pipe[2];
      int fSaved_stderr;
      int fSaved_stdout;
   public:
      JupyROOTExecutorHandler();
      void Poll();
      void InitCapture();
      void EndCapture();
      void Clear();
      std::string& GetStdout();
      std::string& GetStderr();
   };

#ifndef F_LINUX_SPECIFIC_BASE
#define F_LINUX_SPECIFIC_BASE       1024
#endif
#ifndef F_SETPIPE_SZ
#define F_SETPIPE_SZ    (F_LINUX_SPECIFIC_BASE + 7)
#endif

constexpr long MAX_PIPE_SIZE = 1048575;

JupyROOTExecutorHandler::JupyROOTExecutorHandler() {}

static void PollImpl(FILE *stdStream, int *pipeHandle, std::string &pipeContent)
{
   int buf_read;
   char ch;
   fflush(stdStream);
   while (true) {
      buf_read = read(pipeHandle[0], &ch, 1);
      if (buf_read == 1) {
         pipeContent += ch;
      } else break;
   }
}

void JupyROOTExecutorHandler::Poll()
{
   PollImpl(stdout, fStdout_pipe, fStdoutpipe);
   PollImpl(stderr, fStderr_pipe, fStderrpipe);
}

static void InitCaptureImpl(int &savedStdStream, int *pipeHandle, int FILENO)
{
   savedStdStream = dup(FILENO);
   if (pipe(pipeHandle) != 0) {
      return;
   }
   long flags_stdout = fcntl(pipeHandle[0], F_GETFL);
   flags_stdout |= O_NONBLOCK;
   fcntl(pipeHandle[0], F_SETFL, flags_stdout);
   fcntl(pipeHandle[0], F_SETPIPE_SZ, MAX_PIPE_SIZE);
   dup2(pipeHandle[1], FILENO);
   close(pipeHandle[1]);
}

void JupyROOTExecutorHandler::InitCapture()
{
   if (!fCapturing)  {
      InitCaptureImpl(fSaved_stdout, fStdout_pipe, STDOUT_FILENO);
      InitCaptureImpl(fSaved_stderr, fStderr_pipe, STDERR_FILENO);
      fCapturing = true;
   }
}

void JupyROOTExecutorHandler::EndCapture()
{
   if (fCapturing)  {
      Poll();
      dup2(fSaved_stdout, STDOUT_FILENO);
      dup2(fSaved_stderr, STDERR_FILENO);
      fCapturing = false;
   }
}

void JupyROOTExecutorHandler::Clear()
{
   fStdoutpipe = "";
   fStderrpipe = "";
}

std::string &JupyROOTExecutorHandler::GetStdout()
{
   return fStdoutpipe;
}

std::string &JupyROOTExecutorHandler::GetStderr()
{
   return fStderrpipe;
}

////////////////////////////////////////////////////////////////////////////////
bool JupyROOTExecutorImpl(const char *code)
{
   auto status = false;
   try {
      if (gInterpreter->ProcessLine(code))    {
         status = true;
      }
   }
   catch(...) {
      status = true;
   }
   return status;
}

bool JupyROOTDeclarerImpl(const char *code)
{
   bool status = false;
   try {
      if (gInterpreter->Declare(code)) {
         status = true;
      }
   }
   catch(...) {
      status = true;
   }
   return status;
}

JupyROOTExecutorHandler *JupyROOTExecutorHandler_ptr = nullptr;

extern "C" {

   int JupyROOTExecutor(const char *code)
   {
      return JupyROOTExecutorImpl(code);
   }
   int JupyROOTDeclarer(const char *code)
   {
      return JupyROOTDeclarerImpl(code);
   }

   void JupyROOTExecutorHandler_Clear()
   {
      JupyROOTExecutorHandler_ptr->Clear();
   }

   void JupyROOTExecutorHandler_Ctor()
   {
      JupyROOTExecutorHandler_ptr = new JupyROOTExecutorHandler();
   }

   void JupyROOTExecutorHandler_Poll()
   {
      JupyROOTExecutorHandler_ptr->Poll();
   }

   void JupyROOTExecutorHandler_EndCapture()
   {
      JupyROOTExecutorHandler_ptr->EndCapture();
   }

   void JupyROOTExecutorHandler_InitCapture()
   {
      JupyROOTExecutorHandler_ptr->InitCapture();
   }

   const char *JupyROOTExecutorHandler_GetStdout()
   {
      auto out = JupyROOTExecutorHandler_ptr->GetStdout();
      if (out.empty()) return 0;
      return out.c_str();
   }

   const char *JupyROOTExecutorHandler_GetStderr()
   {
      auto out = JupyROOTExecutorHandler_ptr->GetStderr();
      if (out.empty()) return 0;
      return out.c_str();
   }

   void JupyROOTExecutorHandler_Dtor()
   {
      delete JupyROOTExecutorHandler_ptr;
   }

}
<commit_msg>Remove useless workaround now that the type is specified<commit_after>// Author: Danilo Piparo, Omar Zapata   16/12/2015

#include <unistd.h>
#include <fcntl.h>
#include <string>
#include "TInterpreter.h"

bool JupyROOTExecutorImpl(const char *code);
bool JupyROOTDeclarerImpl(const char *code);

class JupyROOTExecutorHandler {
   private:
      bool fCapturing=false;
      std::string fStdoutpipe;
      std::string fStderrpipe;
      int fStdout_pipe[2];
      int fStderr_pipe[2];
      int fSaved_stderr;
      int fSaved_stdout;
   public:
      JupyROOTExecutorHandler();
      void Poll();
      void InitCapture();
      void EndCapture();
      void Clear();
      std::string& GetStdout();
      std::string& GetStderr();
   };

#ifndef F_LINUX_SPECIFIC_BASE
#define F_LINUX_SPECIFIC_BASE       1024
#endif
#ifndef F_SETPIPE_SZ
#define F_SETPIPE_SZ    (F_LINUX_SPECIFIC_BASE + 7)
#endif

constexpr long MAX_PIPE_SIZE = 1048575;

JupyROOTExecutorHandler::JupyROOTExecutorHandler() {}

static void PollImpl(FILE *stdStream, int *pipeHandle, std::string &pipeContent)
{
   int buf_read;
   char ch;
   fflush(stdStream);
   while (true) {
      buf_read = read(pipeHandle[0], &ch, 1);
      if (buf_read == 1) {
         pipeContent += ch;
      } else break;
   }
}

void JupyROOTExecutorHandler::Poll()
{
   PollImpl(stdout, fStdout_pipe, fStdoutpipe);
   PollImpl(stderr, fStderr_pipe, fStderrpipe);
}

static void InitCaptureImpl(int &savedStdStream, int *pipeHandle, int FILENO)
{
   savedStdStream = dup(FILENO);
   if (pipe(pipeHandle) != 0) {
      return;
   }
   long flags_stdout = fcntl(pipeHandle[0], F_GETFL);
   flags_stdout |= O_NONBLOCK;
   fcntl(pipeHandle[0], F_SETFL, flags_stdout);
   fcntl(pipeHandle[0], F_SETPIPE_SZ, MAX_PIPE_SIZE);
   dup2(pipeHandle[1], FILENO);
   close(pipeHandle[1]);
}

void JupyROOTExecutorHandler::InitCapture()
{
   if (!fCapturing)  {
      InitCaptureImpl(fSaved_stdout, fStdout_pipe, STDOUT_FILENO);
      InitCaptureImpl(fSaved_stderr, fStderr_pipe, STDERR_FILENO);
      fCapturing = true;
   }
}

void JupyROOTExecutorHandler::EndCapture()
{
   if (fCapturing)  {
      Poll();
      dup2(fSaved_stdout, STDOUT_FILENO);
      dup2(fSaved_stderr, STDERR_FILENO);
      fCapturing = false;
   }
}

void JupyROOTExecutorHandler::Clear()
{
   fStdoutpipe = "";
   fStderrpipe = "";
}

std::string &JupyROOTExecutorHandler::GetStdout()
{
   return fStdoutpipe;
}

std::string &JupyROOTExecutorHandler::GetStderr()
{
   return fStderrpipe;
}

////////////////////////////////////////////////////////////////////////////////
bool JupyROOTExecutorImpl(const char *code)
{
   auto status = false;
   try {
      if (gInterpreter->ProcessLine(code))    {
         status = true;
      }
   }
   catch(...) {
      status = true;
   }
   return status;
}

bool JupyROOTDeclarerImpl(const char *code)
{
   bool status = false;
   try {
      if (gInterpreter->Declare(code)) {
         status = true;
      }
   }
   catch(...) {
      status = true;
   }
   return status;
}

JupyROOTExecutorHandler *JupyROOTExecutorHandler_ptr = nullptr;

extern "C" {

   int JupyROOTExecutor(const char *code)
   {
      return JupyROOTExecutorImpl(code);
   }
   int JupyROOTDeclarer(const char *code)
   {
      return JupyROOTDeclarerImpl(code);
   }

   void JupyROOTExecutorHandler_Clear()
   {
      JupyROOTExecutorHandler_ptr->Clear();
   }

   void JupyROOTExecutorHandler_Ctor()
   {
      JupyROOTExecutorHandler_ptr = new JupyROOTExecutorHandler();
   }

   void JupyROOTExecutorHandler_Poll()
   {
      JupyROOTExecutorHandler_ptr->Poll();
   }

   void JupyROOTExecutorHandler_EndCapture()
   {
      JupyROOTExecutorHandler_ptr->EndCapture();
   }

   void JupyROOTExecutorHandler_InitCapture()
   {
      JupyROOTExecutorHandler_ptr->InitCapture();
   }

   const char *JupyROOTExecutorHandler_GetStdout()
   {
      auto out = JupyROOTExecutorHandler_ptr->GetStdout();
      return out.c_str();
   }

   const char *JupyROOTExecutorHandler_GetStderr()
   {
      auto out = JupyROOTExecutorHandler_ptr->GetStderr();
      return out.c_str();
   }

   void JupyROOTExecutorHandler_Dtor()
   {
      delete JupyROOTExecutorHandler_ptr;
   }

}
<|endoftext|>


<commit_before>// Author: Danilo Piparo, Omar Zapata   16/12/2015

#include <unistd.h>
#include <fcntl.h>
#include <string>
#include <iostream>
#include "TInterpreter.h"

bool JupyROOTExecutorImpl(const char *code);
bool JupyROOTDeclarerImpl(const char *code);

class JupyROOTExecutorHandler {
private:
   bool fCapturing = false;
   std::string fStdoutpipe;
   std::string fStderrpipe;
   int fStdout_pipe[2];
   int fStderr_pipe[2];
   int fSaved_stderr;
   int fSaved_stdout;
public:
   JupyROOTExecutorHandler();
   void Poll();
   void InitCapture();
   void EndCapture();
   void Clear();
   std::string &GetStdout();
   std::string &GetStderr();
};

#ifndef F_LINUX_SPECIFIC_BASE
#define F_LINUX_SPECIFIC_BASE       1024
#endif
#ifndef F_SETPIPE_SZ
#define F_SETPIPE_SZ    (F_LINUX_SPECIFIC_BASE + 7)
#endif

constexpr long MAX_PIPE_SIZE = 1048575;

JupyROOTExecutorHandler::JupyROOTExecutorHandler() {}

static void PollImpl(FILE *stdStream, int *pipeHandle, std::string &pipeContent)
{
   int buf_read;
   char ch;
   fflush(stdStream);
   while (true) {
      buf_read = read(pipeHandle[0], &ch, 1);
      if (buf_read == 1) {
         pipeContent += ch;
      } else break;
   }
}

void JupyROOTExecutorHandler::Poll()
{
   PollImpl(stdout, fStdout_pipe, fStdoutpipe);
   PollImpl(stderr, fStderr_pipe, fStderrpipe);
}

static void InitCaptureImpl(int &savedStdStream, int *pipeHandle, int FILENO)
{
   savedStdStream = dup(FILENO);
   if (pipe(pipeHandle) != 0) {
      return;
   }
   long flags_stdout = fcntl(pipeHandle[0], F_GETFL);
   flags_stdout |= O_NONBLOCK;
   fcntl(pipeHandle[0], F_SETFL, flags_stdout);
   fcntl(pipeHandle[0], F_SETPIPE_SZ, MAX_PIPE_SIZE);
   dup2(pipeHandle[1], FILENO);
   close(pipeHandle[1]);
}

void JupyROOTExecutorHandler::InitCapture()
{
   if (!fCapturing)  {
      InitCaptureImpl(fSaved_stdout, fStdout_pipe, STDOUT_FILENO);
      InitCaptureImpl(fSaved_stderr, fStderr_pipe, STDERR_FILENO);
      fCapturing = true;
   }
}

void JupyROOTExecutorHandler::EndCapture()
{
   if (fCapturing)  {
      Poll();
      dup2(fSaved_stdout, STDOUT_FILENO);
      dup2(fSaved_stderr, STDERR_FILENO);
      fCapturing = false;
   }
}

void JupyROOTExecutorHandler::Clear()
{
   fStdoutpipe = "";
   fStderrpipe = "";
}

std::string &JupyROOTExecutorHandler::GetStdout()
{
   return fStdoutpipe;
}

std::string &JupyROOTExecutorHandler::GetStderr()
{
   return fStderrpipe;
}

JupyROOTExecutorHandler *JupyROOTExecutorHandler_ptr = nullptr;

////////////////////////////////////////////////////////////////////////////////
bool JupyROOTExecutorImpl(const char *code)
{
   auto status = false;
   try {
      TInterpreter::EErrorCode err = TInterpreter::kNoError;
      if (gInterpreter->ProcessLine(code, &err)) {
         status = true;
      }

      if (err == TInterpreter::kProcessing) {
         gInterpreter->ProcessLine(".@");
         gInterpreter->ProcessLine("cerr << \"Unbalanced curly braces. This cell was not processed.\" << endl;");
      }
   } catch (...) {
      status = true;
   }

   return status;
}

bool JupyROOTDeclarerImpl(const char *code)
{
   bool status = false;
   try {
      if (gInterpreter->Declare(code)) {
         status = true;
      }
   } catch (...) {
      status = true;
   }
   return status;
}

extern "C" {

   int JupyROOTExecutor(const char *code)
   {
      return JupyROOTExecutorImpl(code);
   }
   int JupyROOTDeclarer(const char *code)
   {
      return JupyROOTDeclarerImpl(code);
   }

   void JupyROOTExecutorHandler_Clear()
   {
      JupyROOTExecutorHandler_ptr->Clear();
   }

   void JupyROOTExecutorHandler_Ctor()
   {
      if (!JupyROOTExecutorHandler_ptr) {
         JupyROOTExecutorHandler_ptr = new JupyROOTExecutorHandler();
      }
   }

   void JupyROOTExecutorHandler_Poll()
   {
      JupyROOTExecutorHandler_ptr->Poll();
   }

   void JupyROOTExecutorHandler_EndCapture()
   {
      JupyROOTExecutorHandler_ptr->EndCapture();
   }

   void JupyROOTExecutorHandler_InitCapture()
   {
      JupyROOTExecutorHandler_ptr->InitCapture();
   }

   const char *JupyROOTExecutorHandler_GetStdout()
   {
      auto out = JupyROOTExecutorHandler_ptr->GetStdout();
      return out.c_str();
   }

   const char *JupyROOTExecutorHandler_GetStderr()
   {
      auto out = JupyROOTExecutorHandler_ptr->GetStderr();
      return out.c_str();
   }

   void JupyROOTExecutorHandler_Dtor()
   {
      if (!JupyROOTExecutorHandler_ptr) return;
      delete JupyROOTExecutorHandler_ptr;
      JupyROOTExecutorHandler_ptr = nullptr;
   }

}
<commit_msg>Fix erroneous type inference usage<commit_after>// Author: Danilo Piparo, Omar Zapata   16/12/2015

#include <unistd.h>
#include <fcntl.h>
#include <string>
#include <iostream>
#include "TInterpreter.h"

bool JupyROOTExecutorImpl(const char *code);
bool JupyROOTDeclarerImpl(const char *code);

class JupyROOTExecutorHandler {
private:
   bool fCapturing = false;
   std::string fStdoutpipe;
   std::string fStderrpipe;
   int fStdout_pipe[2];
   int fStderr_pipe[2];
   int fSaved_stderr;
   int fSaved_stdout;
public:
   JupyROOTExecutorHandler();
   void Poll();
   void InitCapture();
   void EndCapture();
   void Clear();
   std::string &GetStdout();
   std::string &GetStderr();
};

#ifndef F_LINUX_SPECIFIC_BASE
#define F_LINUX_SPECIFIC_BASE       1024
#endif
#ifndef F_SETPIPE_SZ
#define F_SETPIPE_SZ    (F_LINUX_SPECIFIC_BASE + 7)
#endif

constexpr long MAX_PIPE_SIZE = 1048575;

JupyROOTExecutorHandler::JupyROOTExecutorHandler() {}

static void PollImpl(FILE *stdStream, int *pipeHandle, std::string &pipeContent)
{
   int buf_read;
   char ch;
   fflush(stdStream);
   while (true) {
      buf_read = read(pipeHandle[0], &ch, 1);
      if (buf_read == 1) {
         pipeContent += ch;
      } else break;
   }
}

void JupyROOTExecutorHandler::Poll()
{
   PollImpl(stdout, fStdout_pipe, fStdoutpipe);
   PollImpl(stderr, fStderr_pipe, fStderrpipe);
}

static void InitCaptureImpl(int &savedStdStream, int *pipeHandle, int FILENO)
{
   savedStdStream = dup(FILENO);
   if (pipe(pipeHandle) != 0) {
      return;
   }
   long flags_stdout = fcntl(pipeHandle[0], F_GETFL);
   flags_stdout |= O_NONBLOCK;
   fcntl(pipeHandle[0], F_SETFL, flags_stdout);
   fcntl(pipeHandle[0], F_SETPIPE_SZ, MAX_PIPE_SIZE);
   dup2(pipeHandle[1], FILENO);
   close(pipeHandle[1]);
}

void JupyROOTExecutorHandler::InitCapture()
{
   if (!fCapturing)  {
      InitCaptureImpl(fSaved_stdout, fStdout_pipe, STDOUT_FILENO);
      InitCaptureImpl(fSaved_stderr, fStderr_pipe, STDERR_FILENO);
      fCapturing = true;
   }
}

void JupyROOTExecutorHandler::EndCapture()
{
   if (fCapturing)  {
      Poll();
      dup2(fSaved_stdout, STDOUT_FILENO);
      dup2(fSaved_stderr, STDERR_FILENO);
      fCapturing = false;
   }
}

void JupyROOTExecutorHandler::Clear()
{
   fStdoutpipe = "";
   fStderrpipe = "";
}

std::string &JupyROOTExecutorHandler::GetStdout()
{
   return fStdoutpipe;
}

std::string &JupyROOTExecutorHandler::GetStderr()
{
   return fStderrpipe;
}

JupyROOTExecutorHandler *JupyROOTExecutorHandler_ptr = nullptr;

////////////////////////////////////////////////////////////////////////////////
bool JupyROOTExecutorImpl(const char *code)
{
   auto status = false;
   try {
      TInterpreter::EErrorCode err = TInterpreter::kNoError;
      if (gInterpreter->ProcessLine(code, &err)) {
         status = true;
      }

      if (err == TInterpreter::kProcessing) {
         gInterpreter->ProcessLine(".@");
         gInterpreter->ProcessLine("cerr << \"Unbalanced curly braces. This cell was not processed.\" << endl;");
      }
   } catch (...) {
      status = true;
   }

   return status;
}

bool JupyROOTDeclarerImpl(const char *code)
{
   bool status = false;
   try {
      if (gInterpreter->Declare(code)) {
         status = true;
      }
   } catch (...) {
      status = true;
   }
   return status;
}

extern "C" {

   int JupyROOTExecutor(const char *code)
   {
      return JupyROOTExecutorImpl(code);
   }
   int JupyROOTDeclarer(const char *code)
   {
      return JupyROOTDeclarerImpl(code);
   }

   void JupyROOTExecutorHandler_Clear()
   {
      JupyROOTExecutorHandler_ptr->Clear();
   }

   void JupyROOTExecutorHandler_Ctor()
   {
      if (!JupyROOTExecutorHandler_ptr) {
         JupyROOTExecutorHandler_ptr = new JupyROOTExecutorHandler();
      }
   }

   void JupyROOTExecutorHandler_Poll()
   {
      JupyROOTExecutorHandler_ptr->Poll();
   }

   void JupyROOTExecutorHandler_EndCapture()
   {
      JupyROOTExecutorHandler_ptr->EndCapture();
   }

   void JupyROOTExecutorHandler_InitCapture()
   {
      JupyROOTExecutorHandler_ptr->InitCapture();
   }

   const char *JupyROOTExecutorHandler_GetStdout()
   {
      return JupyROOTExecutorHandler_ptr->GetStdout().c_str();
   }

   const char *JupyROOTExecutorHandler_GetStderr()
   {
      return JupyROOTExecutorHandler_ptr->GetStderr().c_str();
   }

   void JupyROOTExecutorHandler_Dtor()
   {
      if (!JupyROOTExecutorHandler_ptr) return;
      delete JupyROOTExecutorHandler_ptr;
      JupyROOTExecutorHandler_ptr = nullptr;
   }

}
<|endoftext|>


<commit_before>#include "Options.hpp"
//#include <stdlib.h>

using namespace std;

string Options::output_fname(const string& suffix) const
{
  return (outfile_prefix.empty() ? msa_file : outfile_prefix) + ".raxml." + suffix;
}

void Options::set_default_outfile(std::string& fname, const std::string& suffix)
{
  if (fname.empty())
    fname = output_fname(suffix);
}

void Options::set_default_outfiles()
{
  set_default_outfile(outfile_names.log, "log");
  set_default_outfile(outfile_names.checkpoint, "ckp");
  set_default_outfile(outfile_names.start_tree, "startTree");
  set_default_outfile(outfile_names.best_tree, "bestTree");
  set_default_outfile(outfile_names.best_model, "bestModel");
  set_default_outfile(outfile_names.ml_trees, "mlTrees");
  set_default_outfile(outfile_names.bootstrap_trees, "bootstraps");
  set_default_outfile(outfile_names.support_tree, "support");
}

bool Options::result_files_exist() const
{
  return sysutil_file_exists(best_tree_file()) || sysutil_file_exists(bootstrap_trees_file()) ||
      sysutil_file_exists(support_tree_file()) || sysutil_file_exists(best_model_file());
}

void Options::remove_result_files() const
{
  if (sysutil_file_exists(best_tree_file()))
    std::remove(best_tree_file().c_str());
  if (sysutil_file_exists(best_model_file()))
    std::remove(best_model_file().c_str());
  if (sysutil_file_exists(bootstrap_trees_file()))
    std::remove(bootstrap_trees_file().c_str());
  if (sysutil_file_exists(support_tree_file()))
    std::remove(support_tree_file().c_str());
}

string Options::simd_arch_name() const
{
  switch(simd_arch)
  {
    case PLL_ATTRIB_ARCH_CPU:
      return "NONE";
      break;
    case PLL_ATTRIB_ARCH_SSE:
      return "SSE3";
      break;
    case PLL_ATTRIB_ARCH_AVX:
      return "AVX";
      break;
    case PLL_ATTRIB_ARCH_AVX2:
      return "AVX2";
      break;
    case PLL_ATTRIB_ARCH_AVX512:
      return "AVX512";
      break;
    default:
      return "UNKNOWN";
  }
}

std::ostream& operator<<(std::ostream& stream, const Options& opts)
{
  stream << "RAxML-NG was called as follows:" << endl << endl << opts.cmdline << endl << endl;

  stream << "Analysis options:" << endl;

  stream << "  run mode: ";
  switch(opts.command)
  {
    case Command::search:
      stream << "ML tree search";
      break;
    case Command::evaluate:
      stream << "Evaluate tree likelihood";
      break;
    case Command::bootstrap:
      stream << "Bootstrapping";
      break;
    case Command::all:
      stream << "ML tree search + bootstrapping";
      break;
    case Command::support:
      stream << "Compute bipartition support";
      break;
    default:
      break;
  }
  stream << endl;

  stream << "  start tree(s): ";
  switch(opts.start_tree)
  {
    case StartingTree::random:
      stream << "random";
      break;
    case StartingTree::parsimony:
      stream << "parsimony";
      break;
    case StartingTree::user:
      stream << "user";
      break;
  }
  if (opts.num_searches > 1)
    stream << " (" << opts.num_searches << ")";
  stream << endl;

  stream << "  random seed: " << opts.random_seed << endl;
  stream << "  tip-inner: " << (opts.use_tip_inner ? "ON" : "OFF") << endl;
  stream << "  pattern compression: " << (opts.use_pattern_compression ? "ON" : "OFF") << endl;
  stream << "  per-rate scalers: " << (opts.use_rate_scalers ? "ON" : "OFF") << endl;
  stream << "  site repeats: " << (opts.use_repeats ? "ON" : "OFF") << endl;

  if (opts.command == Command::search)
  {
    if (opts.spr_radius > 0)
      stream << "  fast spr radius: " << opts.spr_radius << endl;
    else
      stream << "  fast spr radius: AUTO" << endl;

    if (opts.spr_cutoff > 0.)
      stream << "  spr subtree cutoff: " << opts.spr_cutoff << endl;
    else
      stream << "  spr subtree cutoff: OFF" << endl;
  }

  stream << "  branch lengths: ";
  stream << (opts.optimize_brlen ? "ML estimate" : "user-specified") << " (";
  if (opts.brlen_linkage == PLLMOD_TREE_BRLEN_SCALED)
    stream << "proportional";
  else if (opts.brlen_linkage == PLLMOD_TREE_BRLEN_UNLINKED)
    stream << "unlinked";
  else
    stream << "linked";

  stream << ")" << endl;

  stream << "  SIMD kernels: " << opts.simd_arch_name() << endl;

  stream << "  parallelization: ";
  if (opts.num_ranks > 1 && opts.num_threads > 1)
  {
    stream << "hybrid MPI+PTHREADS (" << opts.num_ranks <<  " ranks x " <<
        opts.num_threads <<  " threads)" << endl;
  }
  else if (opts.num_ranks > 1)
    stream <<  "MPI (" << opts.num_ranks << " ranks)" << endl;
  else if (opts.num_threads > 1)
    stream << "PTHREADS (" << opts.num_threads << " threads)" << endl;
  else
    stream << "NONE/sequential" << endl;

  stream << endl;

  return stream;
}

<commit_msg>properly check for command-specific result files<commit_after>#include "Options.hpp"
//#include <stdlib.h>

using namespace std;

string Options::output_fname(const string& suffix) const
{
  return (outfile_prefix.empty() ? msa_file : outfile_prefix) + ".raxml." + suffix;
}

void Options::set_default_outfile(std::string& fname, const std::string& suffix)
{
  if (fname.empty())
    fname = output_fname(suffix);
}

void Options::set_default_outfiles()
{
  set_default_outfile(outfile_names.log, "log");
  set_default_outfile(outfile_names.checkpoint, "ckp");
  set_default_outfile(outfile_names.start_tree, "startTree");
  set_default_outfile(outfile_names.best_tree, "bestTree");
  set_default_outfile(outfile_names.best_model, "bestModel");
  set_default_outfile(outfile_names.ml_trees, "mlTrees");
  set_default_outfile(outfile_names.bootstrap_trees, "bootstraps");
  set_default_outfile(outfile_names.support_tree, "support");
}

bool Options::result_files_exist() const
{
  switch (command)
  {
    case Command::evaluate:
    case Command::search:
      return sysutil_file_exists(best_tree_file()) || sysutil_file_exists(best_model_file());
    case Command::bootstrap:
      return sysutil_file_exists(bootstrap_trees_file());
    case Command::all:
      return sysutil_file_exists(best_tree_file()) || sysutil_file_exists(bootstrap_trees_file()) ||
             sysutil_file_exists(support_tree_file()) || sysutil_file_exists(best_model_file());
    case Command::support:
      return sysutil_file_exists(support_tree_file());
    default:
      return false;
  }
}

void Options::remove_result_files() const
{
  if (sysutil_file_exists(best_tree_file()))
    std::remove(best_tree_file().c_str());
  if (sysutil_file_exists(best_model_file()))
    std::remove(best_model_file().c_str());

  if (command == Command::bootstrap || command == Command::all)
  {
    if (sysutil_file_exists(bootstrap_trees_file()))
      std::remove(bootstrap_trees_file().c_str());
  }
  if (command == Command::support || command == Command::all)
  {
    if (sysutil_file_exists(support_tree_file()))
      std::remove(support_tree_file().c_str());
  }
}

string Options::simd_arch_name() const
{
  switch(simd_arch)
  {
    case PLL_ATTRIB_ARCH_CPU:
      return "NONE";
      break;
    case PLL_ATTRIB_ARCH_SSE:
      return "SSE3";
      break;
    case PLL_ATTRIB_ARCH_AVX:
      return "AVX";
      break;
    case PLL_ATTRIB_ARCH_AVX2:
      return "AVX2";
      break;
    case PLL_ATTRIB_ARCH_AVX512:
      return "AVX512";
      break;
    default:
      return "UNKNOWN";
  }
}

std::ostream& operator<<(std::ostream& stream, const Options& opts)
{
  stream << "RAxML-NG was called as follows:" << endl << endl << opts.cmdline << endl << endl;

  stream << "Analysis options:" << endl;

  stream << "  run mode: ";
  switch(opts.command)
  {
    case Command::search:
      stream << "ML tree search";
      break;
    case Command::evaluate:
      stream << "Evaluate tree likelihood";
      break;
    case Command::bootstrap:
      stream << "Bootstrapping";
      break;
    case Command::all:
      stream << "ML tree search + bootstrapping";
      break;
    case Command::support:
      stream << "Compute bipartition support";
      break;
    default:
      break;
  }
  stream << endl;

  stream << "  start tree(s): ";
  switch(opts.start_tree)
  {
    case StartingTree::random:
      stream << "random";
      break;
    case StartingTree::parsimony:
      stream << "parsimony";
      break;
    case StartingTree::user:
      stream << "user";
      break;
  }
  if (opts.num_searches > 1)
    stream << " (" << opts.num_searches << ")";
  stream << endl;

  stream << "  random seed: " << opts.random_seed << endl;
  stream << "  tip-inner: " << (opts.use_tip_inner ? "ON" : "OFF") << endl;
  stream << "  pattern compression: " << (opts.use_pattern_compression ? "ON" : "OFF") << endl;
  stream << "  per-rate scalers: " << (opts.use_rate_scalers ? "ON" : "OFF") << endl;
  stream << "  site repeats: " << (opts.use_repeats ? "ON" : "OFF") << endl;

  if (opts.command == Command::search)
  {
    if (opts.spr_radius > 0)
      stream << "  fast spr radius: " << opts.spr_radius << endl;
    else
      stream << "  fast spr radius: AUTO" << endl;

    if (opts.spr_cutoff > 0.)
      stream << "  spr subtree cutoff: " << opts.spr_cutoff << endl;
    else
      stream << "  spr subtree cutoff: OFF" << endl;
  }

  stream << "  branch lengths: ";
  stream << (opts.optimize_brlen ? "ML estimate" : "user-specified") << " (";
  if (opts.brlen_linkage == PLLMOD_TREE_BRLEN_SCALED)
    stream << "proportional";
  else if (opts.brlen_linkage == PLLMOD_TREE_BRLEN_UNLINKED)
    stream << "unlinked";
  else
    stream << "linked";

  stream << ")" << endl;

  stream << "  SIMD kernels: " << opts.simd_arch_name() << endl;

  stream << "  parallelization: ";
  if (opts.num_ranks > 1 && opts.num_threads > 1)
  {
    stream << "hybrid MPI+PTHREADS (" << opts.num_ranks <<  " ranks x " <<
        opts.num_threads <<  " threads)" << endl;
  }
  else if (opts.num_ranks > 1)
    stream <<  "MPI (" << opts.num_ranks << " ranks)" << endl;
  else if (opts.num_threads > 1)
    stream << "PTHREADS (" << opts.num_threads << " threads)" << endl;
  else
    stream << "NONE/sequential" << endl;

  stream << endl;

  return stream;
}

<|endoftext|>


<commit_before>/*
  Copyright:        Copyright (C) 2003-2018 SIL International.
  Authors:          mcdurdin
*/
#include <vector>
#include <iterator>
#include <codecvt>
#include <locale>
#include "kmx_processevent.h"
#include "utfcodec.hpp"

using namespace km::kbp;
using namespace kmx;

const km_kbp_cp *km::kbp::kmx::u16chr(const km_kbp_cp *p, km_kbp_cp ch) {
  while (*p) {
    if (*p == ch) return p;
    p++;
  }
  return ch == 0 ? p : NULL;
}

const km_kbp_cp *km::kbp::kmx::u16cpy(km_kbp_cp *dst, const km_kbp_cp *src) {
  km_kbp_cp *o = dst;
  while (*src) {
    *dst++ = *src++;
  }
  *dst = 0;
  return o;
}

const km_kbp_cp *km::kbp::kmx::u16ncpy(km_kbp_cp *dst, const km_kbp_cp *src, size_t max) {
  km_kbp_cp *o = dst;
  while (*src && max > 0) {
    *dst++ = *src++;
    max--;
  }
  while(max > 0) {
    *dst++ = 0;
    max--;
  }
  return o;
}

size_t km::kbp::kmx::u16len(const km_kbp_cp *p) {
  int i = 0;
  while (*p) {
    p++;
    i++;
  }
  return i;
}

int km::kbp::kmx::u16cmp(const km_kbp_cp *p, const km_kbp_cp *q) {
  while (*p && *q) {
    if (*p != *q) return *p - *q;
    p++;
    q++;
  }
  return *p - *q;
}

int km::kbp::kmx::u16icmp(const km_kbp_cp *p, const km_kbp_cp *q) {
  while (*p && *q) {
    if (toupper(*p) != toupper(*q)) return *p - *q;
    p++;
    q++;
  }
  return *p - *q;
}

int km::kbp::kmx::u16ncmp(const km_kbp_cp *p, const km_kbp_cp *q, size_t count) {
  while (*p && *q && count) {
    if (*p != *q) return *p - *q;
    p++;
    q++;
    count--;
  }
  if (count)
    return *p - *q;
  return 0;
}

km_kbp_cp *km::kbp::kmx::u16tok(km_kbp_cp *p, km_kbp_cp ch, km_kbp_cp **ctx) {
  if (!p) {
    p = *ctx;
    if (!p) return NULL;
  }

  km_kbp_cp *q = p;
  while (*q && *q != ch) {
    q++;
  }
  if (*q) {
    *q = 0;
    q++;
    while (*q == ch) q++;
    *ctx = q;
  }
  else {
    *ctx = NULL;
  }
  return p;
}

/*
* int xstrlen( PKMX_BYTE p );
*
* Parameters: p Pointer to string to get length of
*
* Returns:    length of string
*
*   Called by:  various functions
*
* xstrlen calculates the length of a string, ignoring some special chars.
*/
PKMX_WCHAR km::kbp::kmx::incxstr(PKMX_WCHAR p) {

  if (*p == 0)
    return p;
  if (*p != UC_SENTINEL) {
    if (*p >= 0xD800 && *p <= 0xDBFF && *(p + 1) >= 0xDC00 && *(p + 1) <= 0xDFFF)
      return p + 2;
    return p + 1;
  }
  // UC_SENTINEL(FFFF) with UC_SENTINEL_EXTENDEDEND(0x10) == variable length
  if (*(p + 1) == CODE_EXTENDED) {
    p += 2;
    while (*p && *p != UC_SENTINEL_EXTENDEDEND)
      p++;

    if (*p == 0)        return p;
    return p + 1;
  }

  if (*(p + 1) > CODE_LASTCODE || CODE__SIZE[*(p + 1)] == -1) {
    return p + 1;
  }

  int deltaptr = 2 + CODE__SIZE[*(p + 1)];

  // check for \0 between FFFF and next printable character
  for (int i = 0; i < deltaptr; i++) {
    if (*p == 0)
      return p;
    p++;
  }
  return p;
}

PKMX_WCHAR km::kbp::kmx::decxstr(PKMX_WCHAR p, PKMX_WCHAR pStart) {

  if(p <= pStart) {
    return NULL;
  }

  p--;
  if(*p == UC_SENTINEL_EXTENDEDEND)
  {
    int n = 0;
    while(*p != UC_SENTINEL && n < 10) { p--; n++; }

    if(p < pStart) {
      // May be a malformed virtual key
      return pStart;
    }
    return p;
  }

  if(p == pStart) return p; // Don't allow test before pStart

  if(*p >= 0xDC00 && *p <= 0xDFFF && *(p-1) >= 0xD800 && *(p-1) <= 0xDBFF)
  {
    return p-1;
  }

  else if(*(p-1) == UC_SENTINEL) return p-1;
  else
  {
    for (int i = 1; i < 5; i++)
    {
      //   *(p - i) == UC_SENTINEL  &&  next CODE_ right to UC_SENTINEL ( looked up in CODE__SIZE+1) has value i
      if ((*(p - i) == UC_SENTINEL) && (CODE__SIZE[*(p - i + 1)] + 1 == i)) {
        int jump = CODE__SIZE[*(p - i + 1)] + 1;
        return (p - jump);
      }
    }
  }
  return p;
}

int km::kbp::kmx::xstrlen_ignoreifopt(PKMX_WCHAR p)
{
  int i;
  for(i = 0; *p; i++, p=incxstr(p))
  {
    if(*p == UC_SENTINEL && (*(p+1) == CODE_IFOPT || *(p+1) == CODE_IFSYSTEMSTORE)) i--;  // I3432
  }
  return i;
}

int km::kbp::kmx::xstrlen(PKMX_WCHAR p)
{
  int i;
  for(i = 0; *p; i++, p=incxstr(p));
  return i;
}

int km::kbp::kmx::xstrpos(PKMX_WCHAR p1, PKMX_WCHAR p)
{
  int i;
  for(i = 0; p < p1; p = incxstr(p), i++);
  return i;
}

PKMX_WCHAR km::kbp::kmx::xstrchr(PKMX_WCHAR buf, PKMX_WCHAR chr)
{
  for(PKMX_WCHAR q = incxstr(buf); *buf; buf = q, q = incxstr(buf))
    if(!u16ncmp(buf, chr, (intptr_t)(q-buf)))
      return buf;
  return NULL;
}

int km::kbp::kmx::xchrcmp(PKMX_WCHAR ch1, PKMX_WCHAR ch2)
{
  PKMX_WCHAR nch1 = incxstr(ch1);
  if(nch1 == ch1) return *ch2 - *ch1; /* comparing *ch2 to nul */
  return u16ncmp(ch1, ch2, (intptr_t)(nch1-ch1));
}

PKMX_WCHAR km::kbp::kmx::strtowstr(PKMX_CHAR in)
{
  PKMX_WCHAR result;

  auto s = convert<char,char16_t>(in);
  result = new char16_t[s.length() + 1];
  s.copy(result, s.length());
  result[s.length()] = 0;
  return result;
}

PKMX_CHAR km::kbp::kmx::wstrtostr(PKMX_WCHAR in)
{
  PKMX_CHAR result;

  auto s = convert<char16_t,char>(in);
  result = new char[s.length() + 1];
  s.copy(result, s.length());
  result[s.length()] = 0;
  return result;
}
<commit_msg>fix(common/core): decxstr another small layout change<commit_after>/*
  Copyright:        Copyright (C) 2003-2018 SIL International.
  Authors:          mcdurdin
*/
#include <vector>
#include <iterator>
#include <codecvt>
#include <locale>
#include "kmx_processevent.h"
#include "utfcodec.hpp"

using namespace km::kbp;
using namespace kmx;

const km_kbp_cp *km::kbp::kmx::u16chr(const km_kbp_cp *p, km_kbp_cp ch) {
  while (*p) {
    if (*p == ch) return p;
    p++;
  }
  return ch == 0 ? p : NULL;
}

const km_kbp_cp *km::kbp::kmx::u16cpy(km_kbp_cp *dst, const km_kbp_cp *src) {
  km_kbp_cp *o = dst;
  while (*src) {
    *dst++ = *src++;
  }
  *dst = 0;
  return o;
}

const km_kbp_cp *km::kbp::kmx::u16ncpy(km_kbp_cp *dst, const km_kbp_cp *src, size_t max) {
  km_kbp_cp *o = dst;
  while (*src && max > 0) {
    *dst++ = *src++;
    max--;
  }
  while(max > 0) {
    *dst++ = 0;
    max--;
  }
  return o;
}

size_t km::kbp::kmx::u16len(const km_kbp_cp *p) {
  int i = 0;
  while (*p) {
    p++;
    i++;
  }
  return i;
}

int km::kbp::kmx::u16cmp(const km_kbp_cp *p, const km_kbp_cp *q) {
  while (*p && *q) {
    if (*p != *q) return *p - *q;
    p++;
    q++;
  }
  return *p - *q;
}

int km::kbp::kmx::u16icmp(const km_kbp_cp *p, const km_kbp_cp *q) {
  while (*p && *q) {
    if (toupper(*p) != toupper(*q)) return *p - *q;
    p++;
    q++;
  }
  return *p - *q;
}

int km::kbp::kmx::u16ncmp(const km_kbp_cp *p, const km_kbp_cp *q, size_t count) {
  while (*p && *q && count) {
    if (*p != *q) return *p - *q;
    p++;
    q++;
    count--;
  }
  if (count)
    return *p - *q;
  return 0;
}

km_kbp_cp *km::kbp::kmx::u16tok(km_kbp_cp *p, km_kbp_cp ch, km_kbp_cp **ctx) {
  if (!p) {
    p = *ctx;
    if (!p) return NULL;
  }

  km_kbp_cp *q = p;
  while (*q && *q != ch) {
    q++;
  }
  if (*q) {
    *q = 0;
    q++;
    while (*q == ch) q++;
    *ctx = q;
  }
  else {
    *ctx = NULL;
  }
  return p;
}

/*
* int xstrlen( PKMX_BYTE p );
*
* Parameters: p Pointer to string to get length of
*
* Returns:    length of string
*
*   Called by:  various functions
*
* xstrlen calculates the length of a string, ignoring some special chars.
*/
PKMX_WCHAR km::kbp::kmx::incxstr(PKMX_WCHAR p) {

  if (*p == 0)
    return p;
  if (*p != UC_SENTINEL) {
    if (*p >= 0xD800 && *p <= 0xDBFF && *(p + 1) >= 0xDC00 && *(p + 1) <= 0xDFFF)
      return p + 2;
    return p + 1;
  }
  // UC_SENTINEL(FFFF) with UC_SENTINEL_EXTENDEDEND(0x10) == variable length
  if (*(p + 1) == CODE_EXTENDED) {
    p += 2;
    while (*p && *p != UC_SENTINEL_EXTENDEDEND)
      p++;

    if (*p == 0)        return p;
    return p + 1;
  }

  if (*(p + 1) > CODE_LASTCODE || CODE__SIZE[*(p + 1)] == -1) {
    return p + 1;
  }

  int deltaptr = 2 + CODE__SIZE[*(p + 1)];

  // check for \0 between FFFF and next printable character
  for (int i = 0; i < deltaptr; i++) {
    if (*p == 0)
      return p;
    p++;
  }
  return p;
}

PKMX_WCHAR km::kbp::kmx::decxstr(PKMX_WCHAR p, PKMX_WCHAR pStart)
{
  if(p <= pStart) {
    return NULL;
  }

  p--;
  if(*p == UC_SENTINEL_EXTENDEDEND)
  {
    int n = 0;
    while(*p != UC_SENTINEL && n < 10) { p--; n++; }

    if(p < pStart) {
      // May be a malformed virtual key
      return pStart;
    }
    return p;
  }

  if(p == pStart) return p; // Don't allow test before pStart

  if(*p >= 0xDC00 && *p <= 0xDFFF && *(p-1) >= 0xD800 && *(p-1) <= 0xDBFF)
  {
    return p-1;
  }

  else if(*(p-1) == UC_SENTINEL) return p-1;
  else
  {
    for (int i = 1; i < 5; i++)
    {
      //   *(p - i) == UC_SENTINEL  &&  next CODE_ right to UC_SENTINEL ( looked up in CODE__SIZE+1) has value i
      if ((*(p - i) == UC_SENTINEL) && (CODE__SIZE[*(p - i + 1)] + 1 == i)) {
        return (p - (CODE__SIZE[*(p - i + 1)] + 1));
      }
    }
  }
  return p;
}

int km::kbp::kmx::xstrlen_ignoreifopt(PKMX_WCHAR p)
{
  int i;
  for(i = 0; *p; i++, p=incxstr(p))
  {
    if(*p == UC_SENTINEL && (*(p+1) == CODE_IFOPT || *(p+1) == CODE_IFSYSTEMSTORE)) i--;  // I3432
  }
  return i;
}

int km::kbp::kmx::xstrlen(PKMX_WCHAR p)
{
  int i;
  for(i = 0; *p; i++, p=incxstr(p));
  return i;
}

int km::kbp::kmx::xstrpos(PKMX_WCHAR p1, PKMX_WCHAR p)
{
  int i;
  for(i = 0; p < p1; p = incxstr(p), i++);
  return i;
}

PKMX_WCHAR km::kbp::kmx::xstrchr(PKMX_WCHAR buf, PKMX_WCHAR chr)
{
  for(PKMX_WCHAR q = incxstr(buf); *buf; buf = q, q = incxstr(buf))
    if(!u16ncmp(buf, chr, (intptr_t)(q-buf)))
      return buf;
  return NULL;
}

int km::kbp::kmx::xchrcmp(PKMX_WCHAR ch1, PKMX_WCHAR ch2)
{
  PKMX_WCHAR nch1 = incxstr(ch1);
  if(nch1 == ch1) return *ch2 - *ch1; /* comparing *ch2 to nul */
  return u16ncmp(ch1, ch2, (intptr_t)(nch1-ch1));
}

PKMX_WCHAR km::kbp::kmx::strtowstr(PKMX_CHAR in)
{
  PKMX_WCHAR result;

  auto s = convert<char,char16_t>(in);
  result = new char16_t[s.length() + 1];
  s.copy(result, s.length());
  result[s.length()] = 0;
  return result;
}

PKMX_CHAR km::kbp::kmx::wstrtostr(PKMX_WCHAR in)
{
  PKMX_CHAR result;

  auto s = convert<char16_t,char>(in);
  result = new char[s.length() + 1];
  s.copy(result, s.length());
  result[s.length()] = 0;
  return result;
}
<|endoftext|>


<commit_before>/**************************************************************************
 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
 *                                                                        *
 * Author: The ALICE Off-line Project.                                    *
 * Contributors are mentioned in the code where appropriate.              *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/
/* $Id$ */
//_________________________________________________________________________
// Macros analyzing the ESD file
// Use Case :
//          root> .L AnaESD.C++
//          root> ana() --> prints the objects stored in ESD
//
// author  : Yves Schutz (CERN/SUBATECH)
// February 2004
//_________________________________________________________________________
#if !defined(__CINT__) || defined(__MAKECINT__)
#include "TFile.h"
#include "TMath.h"
#include "TH1D.h"
#include "TROOT.h"
#include "TBrowser.h"
#include "AliPHOSGetter.h"
#include "AliPHOSGeometry.h"
#include "Riostream.h"
#include "AliESD.h"
#include "AliESDtrack.h"
#include "AliESDCaloTrack.h"
#include "AliEMCALRecParticle.h"
#include "AliPHOSRecParticle.h"
#include "AliKalmanTrack.h"
#include "AliPHOSGridFile.h"
#endif

Bool_t AnaESD(TString filename) ;
void   Match(AliESDtrack * ct, AliESDtrack * cp, Double_t * dist) ;

//__________________________________________________________________________
Bool_t Ana(const TString type = "per5", const Int_t run = 1, const Int_t nOfEvt = 1)
{
  // Analyzes data from the AliEn data catalog

  // Data Challenge identification
  const TString kYear("2004") ;
  const TString kProd("02") ;
  const TString kVers("V4.01.Rev.00") ;

  // get the LFN file name in the Grid catalogue ;
  AliPHOSGridFile lfn ;
  if (!lfn.IsConnected())
    return kFALSE ;
  lfn.SetPath(kYear, kProd, kVers, type) ;
  lfn.SetRun(run) ;

  //loop over the events
  Int_t nevt, evt = 0 ;
  for (nevt = 0 ; nevt < nOfEvt ; nevt++) {
    evt++ ;
    lfn.SetEvt(evt) ;
    TString fileName = lfn.GetLFN() ;

    if (fileName.IsNull()) {
      nevt-- ;
      continue ;
    }

    printf(">>>>>>>>>>>> Processing %s-%s/%s/%s : run # %d event # %d \n",
	   kYear.Data(), kProd.Data(), kVers.Data(), type.Data(), run, evt) ;
    AnaESD(fileName) ;
  }
  return kTRUE ;
}

//__________________________________________________________________________
Bool_t AnaESD(TString fileName)
{
  // Analyze ESD from file fileName
  // calo track histograms ;

  TFile * out = new TFile("out.root", "RECREATE") ;
  TH1D * hCaloEnergy = 0 ;
  TH1D * heta = 0 ;
  TH1D * hphi = 0 ;
  Double_t dist[3] ;

  AliPHOSGetter * gime = AliPHOSGetter::Instance(fileName) ;

  Int_t nEvent = gime->MaxEvent() ;
  Int_t event ;
  AliESD * esd = 0 ;
  for (event = 0 ; event < nEvent; event++) {
    cout << "AnaESD : Processing event # " << event << endl ;
    esd = gime->ESD(event) ;
    if (!esd)
      return kFALSE ;

    //esd->Print();
    // Calorimeter tracks
    AliESDtrack * ct ;
    Int_t caloindex ;
    for (caloindex = 0 ; caloindex < esd->GetNumberOfTracks() ; caloindex++) {
      // get the tracks and check if it is from PHOS
      ct = esd->GetTrack(caloindex) ;
      if ( !ct->IsPHOS() )
	continue ;
      Double_t energy = ct->GetPHOSsignal() ;
      if(!hCaloEnergy) {
	out->cd() ;
	hCaloEnergy = new TH1D("CaloEnergy", "Energy in calorimeter", 1500, 0., 150.) ;
      }
      hCaloEnergy->Fill(energy) ;
      AliESDtrack * cp ;
      Int_t cpindex ;
      for (cpindex = 0 ; cpindex < esd->GetNumberOfTracks() ; cpindex++) {
	// get the charged tracks from central tracking
	cp = esd->GetTrack(cpindex) ;
	if ( cp->IsPHOS() )
	  continue ;
	Match(ct, cp, dist) ;
	if (!heta && !hphi) {
	  heta = new TH1D("Correta", "neutral-charged correlation in eta" , 100, 0., 360.) ;
	  hphi = new TH1D("Corrphi", "neutral-charged correlation in phi" , 100, 0., 360.) ;
	}
	heta->Fill(dist[1]) ;
	hphi->Fill(dist[2]) ;
      }
    }
  }
  TBrowser * bs = new TBrowser("Root Memory Bowser", gROOT->FindObjectAny("out.root") ) ;
  bs->Show() ;
  out->Write() ;
  return kTRUE ;
}

//__________________________________________________________________________
void Match(AliESDtrack * ct, AliESDtrack * cp, Double_t * dist)
{
  // Calculates the distance (x,z) between  the particle detected by PHOS and
  // the charged particle reconstructed by the global tracking

 //  Int_t phN ;
//   Double_t phZ, phX ;

//   if (ct->IsPHOS()) { // it is a PHOS particle
//     Double_t cpTheta,  cpPhi ;
//     Double_t phTheta,  phPhi ;
//     cpTheta = cpPhi = phTheta = phPhi = 0. ;
//    //    cout << "PHOS particle # " << " pos ("
//     // 	 << pp->GetPos().X() << ", " << pp->GetPos().Y() << ", " << pp->GetPos().Z() << ")" << endl ;

//     AliPHOSGetter * gime = AliPHOSGetter::Instance() ;
//     gime->PHOSGeometry()->ImpactOnEmc(*pp, phN, phZ, phX) ;
//     Double_t xyzAtPHOS[3] ;
//     cp->GetOuterXYZ(xyzAtPHOS) ;
//     if ( (xyzAtPHOS[0] +  xyzAtPHOS[1] + xyzAtPHOS[2]) != 0.) { //it has reached PHOS
//       //the next check are only if we want high quality tracks
//       //       ULong_t status = cp->GetStatus() ;
//       //       if ((status & AliESDtrack::kTRDput)==0)
//       // 	do not continue;
//       //       if ((status & AliESDtrack::kTRDStop)!=0)
//       // 	do not continue;
//       //       cout << "Charged particle # " << " pos ("
//       // 	   << xyzAtPHOS[0] << ", " << xyzAtPHOS[1] << ", " << xyzAtPHOS[2] << ")" <<  endl ;
//       TVector3 poscp(xyzAtPHOS[0], xyzAtPHOS[1], xyzAtPHOS[2]) ;
//       Int_t cpN ;
//       Double_t cpZ,cpX ;
//       gime->PHOSGeometry()->ImpactOnEmc(poscp, cpN, cpZ, cpX) ;
//       if (cpN) {// we are inside the PHOS acceptance
// 	// 	cout << "Charged Matching 1: " << cpN << " " << cpZ << " " << cpX << endl ;
// 	// 	cout << "Charged Matching 2: " << phN << " " << phZ << " " << phX << endl ;
// 	dist[0] = TMath::Sqrt( (cpZ-phZ)*(cpZ-phZ) + (cpX-phX)*(cpX-phX)) ;
//       }
//       phTheta = pp->Theta() ;
//       phPhi   = pp->Phi() ;
//       TParticle tempo ;
//       tempo.SetMomentum(xyzAtPHOS[0], xyzAtPHOS[1], xyzAtPHOS[2], 0.) ;
//       cpTheta = tempo.Theta() ;
//       cpPhi   = tempo.Phi() ;
//       //cout << phTheta << " " << phPhi << " " << endl
//       //cout <<	 cpTheta << " " << cpPhi-phPhi << " " << endl ;
//     }
//     dist[1] = (phTheta - cpTheta)*TMath::RadToDeg() ;
//     dist[2] = (phPhi - cpPhi)*TMath::RadToDeg() ;
//   }

//   if (ep) {
//     //cout << "EMCAL particle # " << endl ;
//   }
}
<commit_msg>updating<commit_after>/**************************************************************************
 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
 *                                                                        *
 * Author: The ALICE Off-line Project.                                    *
 * Contributors are mentioned in the code where appropriate.              *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/
/* $Id$ */
//_________________________________________________________________________
// Macros analyzing the ESD file
// Use Case :
//          root> .L AnaESD.C++
//          root> ana() --> prints the objects stored in ESD
//
// author  : Yves Schutz (CERN/SUBATECH)
// February 2004
//_________________________________________________________________________
#if !defined(__CINT__) || defined(__MAKECINT__)
#include "TFile.h"
#include "TMath.h"
#include "TH1D.h"
#include "TROOT.h"
#include "TBrowser.h"
#include "AliPHOSGetter.h"
#include "AliPHOSGeometry.h"
#include "Riostream.h"
#include "AliESD.h"
#include "AliESDtrack.h"
#include "AliESDCaloTrack.h"
#include "AliEMCALRecParticle.h"
#include "AliPHOSRecParticle.h"
#include "AliKalmanTrack.h"
#include "AliPHOSGridFile.h"
#endif

Bool_t AnaESD(TString filename) ;
void   Match(AliESDtrack * ct, AliESDtrack * cp, Double_t * dist) ;

//__________________________________________________________________________
Bool_t Ana(const TString type = "per5", const Int_t run = 1, const Int_t nOfEvt = 1)
{
  // Analyzes data from the AliEn data catalog

  // Data Challenge identification
  const TString kYear("2004") ;
  const TString kProd("02") ;
  const TString kVers("V4.01.Rev.00") ;

  // get the LFN file name in the Grid catalogue ;
  AliPHOSGridFile lfn ;
  if (!lfn.IsConnected())
    return kFALSE ;
  lfn.SetPath(kYear, kProd, kVers, type) ;
  lfn.SetRun(run) ;

  //loop over the events
  Int_t nevt, evt = 0 ;
  for (nevt = 0 ; nevt < nOfEvt ; nevt++) {
    evt++ ;
    lfn.SetEvt(evt) ;
    TString fileName = lfn.GetLFN() ;

    if (fileName.IsNull()) {
      nevt-- ;
      continue ;
    }

    printf(">>>>>>>>>>>> Processing %s-%s/%s/%s : run # %d event # %d \n",
	   kYear.Data(), kProd.Data(), kVers.Data(), type.Data(), run, evt) ;
    AnaESD(fileName) ;
  }
  return kTRUE ;
}

//__________________________________________________________________________
Bool_t AnaESD(TString fileName)
{
  // Analyze ESD from file fileName
  // calo track histograms ;

  TFile * out = new TFile("AOD.root", "RECREATE") ;
  TH1D * hCaloEnergyA = 0 ;
  TH1D * hCaloEnergyE = 0 ;
  TH1D * hCaloEnergyG = 0 ;
  TH1D * hCaloEnergyP = 0 ;
  TH1D * heta = 0 ;
  TH1D * hphi = 0 ;
  Double_t dist[3] ;

  AliPHOSGetter * gime = AliPHOSGetter::Instance(fileName) ;

  Int_t nEvent = gime->MaxEvent() ;
  Int_t event ;
  AliESD * esd = 0 ;
  for (event = 0 ; event < nEvent; event++) {
    cout << "AnaESD : Processing event # " << event << endl ;
    esd = gime->ESD(event) ;
    if (!esd)
      return kFALSE ;

    //esd->Print();
    // Calorimeter tracks
    AliESDtrack * ct ;
    Int_t caloindex ;
    for (caloindex = 0 ; caloindex < esd->GetNumberOfTracks() ; caloindex++) {
      // get the tracks and check if it is from PHOS
      ct = esd->GetTrack(caloindex) ;
      if ( !ct->IsPHOS() )
	continue ;
      Double_t energy = ct->GetPHOSsignal() ;
      // check if CPV bit is set (see AliPHOSFastRecParticle)
      Double_t type[AliESDtrack::kSPECIES+4] ;
      ct->GetPHOSpid(type) ;

      if ( (type[AliESDtrack::kElectron] == 1.0) || (type[AliESDtrack::kPhoton] == 1.0) ) {
	if(!hCaloEnergyA) {
	  out->cd() ;
	  hCaloEnergyA = new TH1D("CaloEnergyA", "Energy in calorimeter Electron/Photon like", 500, 0., 50.) ;
	}
	hCaloEnergyA->Fill(energy) ;
      }

      if ( type[AliESDtrack::kElectron] == 1.0 ) {
	if(!hCaloEnergyE) {
	  out->cd() ;
	  hCaloEnergyE = new TH1D("CaloEnergyE", "Energy in calorimeter Electron like", 500, 0., 50.) ;
	}
	hCaloEnergyE->Fill(energy) ;
      }

      if ( type[AliESDtrack::kPhoton] == 1.0 ) {
	if(!hCaloEnergyG) {
	  out->cd() ;
	  hCaloEnergyG = new TH1D("CaloEnergyG", "Energy in calorimeter Gamma like", 500, 0., 50.) ;
	}
	hCaloEnergyG->Fill(energy) ;
      }

      if ( type[AliESDtrack::kPi0] == 1.0 ) {
	if(!hCaloEnergyP) {
	  out->cd() ;
	  hCaloEnergyP = new TH1D("CaloEnergyP", "Energy in calorimeter Pi0 like", 500, 0., 100.) ;
	}
	hCaloEnergyP->Fill(energy) ;
      }

      AliESDtrack * cp ;
      Int_t cpindex ;
      for (cpindex = 0 ; cpindex < esd->GetNumberOfTracks() ; cpindex++) {
	// get the charged tracks from central tracking
	cp = esd->GetTrack(cpindex) ;
	if ( cp->IsPHOS() )
	  continue ;
	Match(ct, cp, dist) ;
	if (!heta && !hphi) {
	  heta = new TH1D("Correta", "neutral-charged correlation in eta" , 100, 0., 360.) ;
	  hphi = new TH1D("Corrphi", "neutral-charged correlation in phi" , 100, 0., 360.) ;
	}
	heta->Fill(dist[1]) ;
	hphi->Fill(dist[2]) ;
      }
    }
  }
  TBrowser * bs = new TBrowser("Root Memory Bowser", gROOT->FindObjectAny("AOD.root") ) ;
  bs->Show() ;
  out->Write() ;
  return kTRUE ;
}

//__________________________________________________________________________
void Match(AliESDtrack * ct, AliESDtrack * cp, Double_t * dist)
{
  // Calculates the distance (x,z) between  the particle detected by PHOS and
  // the charged particle reconstructed by the global tracking

 //  Int_t phN ;
//   Double_t phZ, phX ;

//   if (ct->IsPHOS()) { // it is a PHOS particle
//     Double_t cpTheta,  cpPhi ;
//     Double_t phTheta,  phPhi ;
//     cpTheta = cpPhi = phTheta = phPhi = 0. ;
//    //    cout << "PHOS particle # " << " pos ("
//     // 	 << pp->GetPos().X() << ", " << pp->GetPos().Y() << ", " << pp->GetPos().Z() << ")" << endl ;

//     AliPHOSGetter * gime = AliPHOSGetter::Instance() ;
//     gime->PHOSGeometry()->ImpactOnEmc(*pp, phN, phZ, phX) ;
//     Double_t xyzAtPHOS[3] ;
//     cp->GetOuterXYZ(xyzAtPHOS) ;
//     if ( (xyzAtPHOS[0] +  xyzAtPHOS[1] + xyzAtPHOS[2]) != 0.) { //it has reached PHOS
//       //the next check are only if we want high quality tracks
//       //       ULong_t status = cp->GetStatus() ;
//       //       if ((status & AliESDtrack::kTRDput)==0)
//       // 	do not continue;
//       //       if ((status & AliESDtrack::kTRDStop)!=0)
//       // 	do not continue;
//       //       cout << "Charged particle # " << " pos ("
//       // 	   << xyzAtPHOS[0] << ", " << xyzAtPHOS[1] << ", " << xyzAtPHOS[2] << ")" <<  endl ;
//       TVector3 poscp(xyzAtPHOS[0], xyzAtPHOS[1], xyzAtPHOS[2]) ;
//       Int_t cpN ;
//       Double_t cpZ,cpX ;
//       gime->PHOSGeometry()->ImpactOnEmc(poscp, cpN, cpZ, cpX) ;
//       if (cpN) {// we are inside the PHOS acceptance
// 	// 	cout << "Charged Matching 1: " << cpN << " " << cpZ << " " << cpX << endl ;
// 	// 	cout << "Charged Matching 2: " << phN << " " << phZ << " " << phX << endl ;
// 	dist[0] = TMath::Sqrt( (cpZ-phZ)*(cpZ-phZ) + (cpX-phX)*(cpX-phX)) ;
//       }
//       phTheta = pp->Theta() ;
//       phPhi   = pp->Phi() ;
//       TParticle tempo ;
//       tempo.SetMomentum(xyzAtPHOS[0], xyzAtPHOS[1], xyzAtPHOS[2], 0.) ;
//       cpTheta = tempo.Theta() ;
//       cpPhi   = tempo.Phi() ;
//       //cout << phTheta << " " << phPhi << " " << endl
//       //cout <<	 cpTheta << " " << cpPhi-phPhi << " " << endl ;
//     }
//     dist[1] = (phTheta - cpTheta)*TMath::RadToDeg() ;
//     dist[2] = (phPhi - cpPhi)*TMath::RadToDeg() ;
//   }

//   if (ep) {
//     //cout << "EMCAL particle # " << endl ;
//   }
}
<|endoftext|>


<commit_before>/*
Copyright (c) by respective owners including Yahoo!, Microsoft, and
individual contributors. All rights reserved.  Released under a BSD (revised)
license as described in the file LICENSE.
 */
#include <stdint.h>
#include "parse_primitives.h"
#include "v_array.h"
#include "example.h"
#include "simple_label.h"
#include "gd.h"
#include "global_data.h"
#include "memory.h"

int compare_feature(const void* p1, const void* p2) {
  feature* f1 = (feature*) p1;
  feature* f2 = (feature*) p2;
  return (f1->weight_index - f2->weight_index);
}

float collision_cleanup(v_array<feature>& feature_map) {

 int pos = 0;
 float sum_sq = 0.;

 for(uint32_t i = 1;i < feature_map.size();i++) {
    if(feature_map[i].weight_index == feature_map[pos].weight_index)
      feature_map[pos].x += feature_map[i].x;
    else {
      sum_sq += feature_map[pos].x*feature_map[pos].x;
      feature_map[++pos] = feature_map[i];
    }
  }
  sum_sq += feature_map[pos].x*feature_map[pos].x;
  feature_map.end = &(feature_map[pos]);
  feature_map.end++;
  return sum_sq;
}

struct features_and_source
{
  v_array<feature> feature_map; //map to store sparse feature vectors
  uint32_t stride_shift;
  weight* base;
};

void vec_store(features_and_source& p, float fx, float& fw) {
  feature f = {fx, (uint32_t)(&fw - p.base) >> p.stride_shift};
  p.feature_map.push_back(f);
}

audit_data copy_audit_data(audit_data &src) {
  audit_data dst;
  dst.space = (char*)calloc_or_die(strlen(src.space)+1, sizeof(char));
  strcpy(dst.space, src.space);
  dst.feature = (char*)calloc_or_die(strlen(src.feature)+1, sizeof(char));
  strcpy(dst.feature, src.feature);
  dst.weight_index = src.weight_index;
  dst.x = src.x;
  dst.alloced = src.alloced;
  return dst;
}

namespace VW {
void copy_example_label(example* dst, example* src, size_t label_size, void(*copy_label)(void*&,void*)) {
  if (!src->ld) {
    if (dst->ld) free(dst->ld);  // TODO: this should be a delete_label, really
    dst->ld = NULL;
  } else {
    if ((label_size == 0) && (copy_label == NULL)) {
      if (dst->ld) free(dst->ld);  // TODO: this should be a delete_label, really
      dst->ld = NULL;
    } else if (copy_label) {
      copy_label(dst->ld, src->ld);
    } else {
      //dst->ld = (void*)malloc(label_size);
      memcpy(dst->ld, src->ld, label_size);
    }
  }
}

void copy_example_data(bool audit, example* dst, example* src)
{
  //std::cerr << "copy_example_data dst = " << dst << std::endl;
  copy_array(dst->tag, src->tag);
  dst->example_counter = src->example_counter;

  copy_array(dst->indices, src->indices);
  for (size_t i=0; i<256; i++)
    copy_array(dst->atomics[i], src->atomics[i]);
  dst->ft_offset = src->ft_offset;

  if (audit)
    for (size_t i=0; i<256; i++)
      copy_array(dst->audit_features[i], src->audit_features[i], copy_audit_data);

  dst->num_features = src->num_features;
  dst->partial_prediction = src->partial_prediction;
  copy_array(dst->topic_predictions, src->topic_predictions);
  dst->loss = src->loss;
  dst->eta_round = src->eta_round;
  dst->eta_global = src->eta_global;
  dst->example_t = src->example_t;
  memcpy(dst->sum_feat_sq, src->sum_feat_sq, 256 * sizeof(float));
  dst->total_sum_feat_sq = src->total_sum_feat_sq;
  dst->revert_weight = src->revert_weight;
  dst->test_only = src->test_only;
  dst->end_pass = src->end_pass;
  dst->sorted = src->sorted;
  dst->in_use = src->in_use;}

void copy_example_data(bool audit, example* dst, example* src, size_t label_size, void(*copy_label)(void*&,void*)) {
  copy_example_data(audit, dst, src);
  copy_example_label(dst, src, label_size, copy_label);
}

flat_example* flatten_example(vw& all, example *ec)
{
	flat_example* fec = (flat_example*) calloc_or_die(1,sizeof(flat_example));
	fec->ld = (label_data*)calloc_or_die(1, sizeof(label_data));
	memcpy(fec->ld, ec->ld, sizeof(label_data));

	fec->tag_len = ec->tag.size();
	if (fec->tag_len >0)
	{
		fec->tag = ec->tag.begin;
	}

	fec->example_counter = ec->example_counter;
	fec->ft_offset = ec->ft_offset;
	fec->num_features = ec->num_features;

	features_and_source fs;
	fs.base = all.reg.weight_vector;
	fs.stride_shift = all.reg.stride_shift;
	GD::foreach_feature<features_and_source, vec_store>(all, *ec, fs);
	qsort(fs.feature_map.begin, fs.feature_map.size(), sizeof(feature), compare_feature);

	fec->feature_map_len = fs.feature_map.size();
	if (fec->feature_map_len > 0)
	{
		fec->feature_map = fs.feature_map.begin;
	}

	return fec;
}

void free_flatten_example(flat_example* fec)
{  //note: The label memory should be freed by by freeing the original example.
  if (fec)
    {
      if (fec->feature_map_len > 0)
	free(fec->feature_map);
      free(fec->ld);
      free(fec);
    }
}

}

example *alloc_examples(size_t label_size, size_t count=1)
{
  example* ec = (example*)calloc_or_die(count, sizeof(example));
  if (ec == NULL) return NULL;
  for (size_t i=0; i<count; i++) {
    ec[i].ld = calloc_or_die(1, label_size);
    if (ec[i].ld == NULL) {
      for (size_t j=0; j<i; j++) free(ec[j].ld);
      free(ec);
      return NULL;
    }
    ec[i].in_use = true;
    ec[i].ft_offset = 0;
    //  std::cerr << "  alloc_example.indices.begin=" << ec->indices.begin << " end=" << ec->indices.end << " // ld = " << ec->ld << "\t|| me = " << ec << std::endl;
  }
  return ec;
}

void dealloc_example(void(*delete_label)(void*), example&ec)
{
  if (delete_label) {
    delete_label(ec.ld);
  }
  ec.tag.delete_v();

  ec.topic_predictions.delete_v();

  free(ec.ld);
  for (size_t j = 0; j < 256; j++)
    {
      ec.atomics[j].delete_v();

      if (ec.audit_features[j].begin != ec.audit_features[j].end_array)
        {
          for (audit_data* temp = ec.audit_features[j].begin;
               temp != ec.audit_features[j].end; temp++)
            if (temp->alloced) {
              free(temp->space);
              free(temp->feature);
              temp->alloced = false;
            }
	  ec.audit_features[j].delete_v();
        }
    }
  ec.indices.delete_v();
}

<commit_msg>another flatten_example fix<commit_after>/*
Copyright (c) by respective owners including Yahoo!, Microsoft, and
individual contributors. All rights reserved.  Released under a BSD (revised)
license as described in the file LICENSE.
 */
#include <stdint.h>
#include "parse_primitives.h"
#include "v_array.h"
#include "example.h"
#include "simple_label.h"
#include "gd.h"
#include "global_data.h"
#include "memory.h"

int compare_feature(const void* p1, const void* p2) {
  feature* f1 = (feature*) p1;
  feature* f2 = (feature*) p2;
  return (f1->weight_index - f2->weight_index);
}

float collision_cleanup(v_array<feature>& feature_map) {

 int pos = 0;
 float sum_sq = 0.;

 for(uint32_t i = 1;i < feature_map.size();i++) {
    if(feature_map[i].weight_index == feature_map[pos].weight_index)
      feature_map[pos].x += feature_map[i].x;
    else {
      sum_sq += feature_map[pos].x*feature_map[pos].x;
      feature_map[++pos] = feature_map[i];
    }
  }
  sum_sq += feature_map[pos].x*feature_map[pos].x;
  feature_map.end = &(feature_map[pos]);
  feature_map.end++;
  return sum_sq;
}

struct features_and_source
{
  v_array<feature> feature_map; //map to store sparse feature vectors
  uint32_t stride_shift;
  weight* base;
};

void vec_store(features_and_source& p, float fx, float& fw) {
  feature f = {fx, (uint32_t)(&fw - p.base) >> p.stride_shift};
  p.feature_map.push_back(f);
}

audit_data copy_audit_data(audit_data &src) {
  audit_data dst;
  dst.space = (char*)calloc_or_die(strlen(src.space)+1, sizeof(char));
  strcpy(dst.space, src.space);
  dst.feature = (char*)calloc_or_die(strlen(src.feature)+1, sizeof(char));
  strcpy(dst.feature, src.feature);
  dst.weight_index = src.weight_index;
  dst.x = src.x;
  dst.alloced = src.alloced;
  return dst;
}

namespace VW {
void copy_example_label(example* dst, example* src, size_t label_size, void(*copy_label)(void*&,void*)) {
  if (!src->ld) {
    if (dst->ld) free(dst->ld);  // TODO: this should be a delete_label, really
    dst->ld = NULL;
  } else {
    if ((label_size == 0) && (copy_label == NULL)) {
      if (dst->ld) free(dst->ld);  // TODO: this should be a delete_label, really
      dst->ld = NULL;
    } else if (copy_label) {
      copy_label(dst->ld, src->ld);
    } else {
      //dst->ld = (void*)malloc(label_size);
      memcpy(dst->ld, src->ld, label_size);
    }
  }
}

void copy_example_data(bool audit, example* dst, example* src)
{
  //std::cerr << "copy_example_data dst = " << dst << std::endl;
  copy_array(dst->tag, src->tag);
  dst->example_counter = src->example_counter;

  copy_array(dst->indices, src->indices);
  for (size_t i=0; i<256; i++)
    copy_array(dst->atomics[i], src->atomics[i]);
  dst->ft_offset = src->ft_offset;

  if (audit)
    for (size_t i=0; i<256; i++)
      copy_array(dst->audit_features[i], src->audit_features[i], copy_audit_data);

  dst->num_features = src->num_features;
  dst->partial_prediction = src->partial_prediction;
  copy_array(dst->topic_predictions, src->topic_predictions);
  dst->loss = src->loss;
  dst->eta_round = src->eta_round;
  dst->eta_global = src->eta_global;
  dst->example_t = src->example_t;
  memcpy(dst->sum_feat_sq, src->sum_feat_sq, 256 * sizeof(float));
  dst->total_sum_feat_sq = src->total_sum_feat_sq;
  dst->revert_weight = src->revert_weight;
  dst->test_only = src->test_only;
  dst->end_pass = src->end_pass;
  dst->sorted = src->sorted;
  dst->in_use = src->in_use;}

void copy_example_data(bool audit, example* dst, example* src, size_t label_size, void(*copy_label)(void*&,void*)) {
  copy_example_data(audit, dst, src);
  copy_example_label(dst, src, label_size, copy_label);
}

flat_example* flatten_example(vw& all, example *ec)
{
	flat_example* fec = (flat_example*) calloc_or_die(1,sizeof(flat_example));
	fec->ld = (label_data*)calloc_or_die(1, sizeof(label_data));
	memcpy(fec->ld, ec->ld, sizeof(label_data));

	fec->tag_len = ec->tag.size();
	if (fec->tag_len >0)
	  {
	    fec->tag = (char*)calloc_or_die(fec->tag_len+1, sizeof(char));
	    memcpy(fec->tag,ec->tag.begin, fec->tag_len);
	  }

	fec->example_counter = ec->example_counter;
	fec->ft_offset = ec->ft_offset;
	fec->num_features = ec->num_features;

	features_and_source fs;
	fs.base = all.reg.weight_vector;
	fs.stride_shift = all.reg.stride_shift;
	GD::foreach_feature<features_and_source, vec_store>(all, *ec, fs);
	qsort(fs.feature_map.begin, fs.feature_map.size(), sizeof(feature), compare_feature);

	fec->feature_map_len = fs.feature_map.size();
	if (fec->feature_map_len > 0)
	{
		fec->feature_map = fs.feature_map.begin;
	}

	return fec;
}

void free_flatten_example(flat_example* fec)
{  //note: The label memory should be freed by by freeing the original example.
  if (fec)
    {
      if (fec->feature_map_len > 0)
	free(fec->feature_map);
      if (fec->tag_len > 0)
	free(fec->tag);
      free(fec->ld);
      free(fec);
    }
}

}

example *alloc_examples(size_t label_size, size_t count=1)
{
  example* ec = (example*)calloc_or_die(count, sizeof(example));
  if (ec == NULL) return NULL;
  for (size_t i=0; i<count; i++) {
    ec[i].ld = calloc_or_die(1, label_size);
    if (ec[i].ld == NULL) {
      for (size_t j=0; j<i; j++) free(ec[j].ld);
      free(ec);
      return NULL;
    }
    ec[i].in_use = true;
    ec[i].ft_offset = 0;
    //  std::cerr << "  alloc_example.indices.begin=" << ec->indices.begin << " end=" << ec->indices.end << " // ld = " << ec->ld << "\t|| me = " << ec << std::endl;
  }
  return ec;
}

void dealloc_example(void(*delete_label)(void*), example&ec)
{
  if (delete_label) {
    delete_label(ec.ld);
  }
  ec.tag.delete_v();

  ec.topic_predictions.delete_v();

  free(ec.ld);
  for (size_t j = 0; j < 256; j++)
    {
      ec.atomics[j].delete_v();

      if (ec.audit_features[j].begin != ec.audit_features[j].end_array)
        {
          for (audit_data* temp = ec.audit_features[j].begin;
               temp != ec.audit_features[j].end; temp++)
            if (temp->alloced) {
              free(temp->space);
              free(temp->feature);
              temp->alloced = false;
            }
	  ec.audit_features[j].delete_v();
        }
    }
  ec.indices.delete_v();
}

<|endoftext|>


<commit_before>#include "PowersOf2.hpp"

using namespace std;

namespace snarkfront {

size_t sizeBits(const bool& dummy) { return 1; }
size_t sizeBits(const uint32_t& dummy) { return 32; }
size_t sizeBits(const uint64_t& dummy) { return 64; }

vector<int> valueBits(const bool& a) { return vector<int>(1, a); }

template <typename UINT_N>
vector<int> valueBits_internal(const UINT_N& a) {
    vector<int> v;
    v.reserve(sizeBits(a));

    UINT_N b = a;
    for (size_t i = 0; i < sizeBits(b); ++i) {
        v.push_back(b & 0x1);
        b >>= 1;
    }

    return v;
}

vector<int> valueBits(const uint32_t& a) { return valueBits_internal(a); }
vector<int> valueBits(const uint64_t& a) { return valueBits_internal(a); }

size_t countBits(const vector<int>& v) {
    size_t count = 0;

    for (const auto& b : v)
        count += b;

    return count;
}

} // namespace snarkfront
<commit_msg>8-bit unsigned int type<commit_after>#include "PowersOf2.hpp"

using namespace std;

namespace snarkfront {

size_t sizeBits(const bool& dummy) { return 1; }
size_t sizeBits(const uint8_t& dummy) { return 8; }
size_t sizeBits(const uint32_t& dummy) { return 32; }
size_t sizeBits(const uint64_t& dummy) { return 64; }

vector<int> valueBits(const bool& a) { return vector<int>(1, a); }

template <typename UINT_N>
vector<int> valueBits_internal(const UINT_N& a) {
    vector<int> v;
    v.reserve(sizeBits(a));

    UINT_N b = a;
    for (size_t i = 0; i < sizeBits(b); ++i) {
        v.push_back(b & 0x1);
        b >>= 1;
    }

    return v;
}

vector<int> valueBits(const uint8_t& a) { return valueBits_internal(a); }
vector<int> valueBits(const uint32_t& a) { return valueBits_internal(a); }
vector<int> valueBits(const uint64_t& a) { return valueBits_internal(a); }

size_t countBits(const vector<int>& v) {
    size_t count = 0;

    for (const auto& b : v)
        count += b;

    return count;
}

} // namespace snarkfront
<|endoftext|>


<commit_before>
#ifdef _WIN32
#include <windows.h>
#else
#include <alloca.h>
#include <unistd.h>
#include <cerrno>
#include <cstdlib>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstdio>
#include <cstring>
#include <signal.h>
#endif

#include <nstd/Debug.h>
#include <nstd/List.h>
#ifndef _WIN32
#include <nstd/File.h>
#endif
#include <nstd/Process.h>

Process::Process()
{
#ifdef _WIN32
  ASSERT(sizeof(hProcess) >= sizeof(HANDLE));
  hProcess = INVALID_HANDLE_VALUE;
  hStdOutRead = INVALID_HANDLE_VALUE;
  hStdErrRead = INVALID_HANDLE_VALUE;
  hStdInWrite = INVALID_HANDLE_VALUE;
#else
  pid = 0;
#endif
}

Process::~Process()
{
  uint32_t exitCode;
  join(exitCode);
}

bool Process::isRunning() const
{
#ifdef _WIN32
  return hProcess != INVALID_HANDLE_VALUE;
#else
  return pid != 0;
#endif
}

uint32_t Process::start(const String& commandLine)
{
#ifdef _WIN32
  if(hProcess != INVALID_HANDLE_VALUE)
  {
    SetLastError(ERROR_INVALID_HANDLE);
    return 0;
  }

  STARTUPINFO si;
  PROCESS_INFORMATION pi;

  ZeroMemory(&si, sizeof(si));
  si.cb = sizeof(si);
  ZeroMemory(&pi, sizeof(pi));

  String args(commandLine);

  if(!CreateProcess(NULL, (tchar_t*)args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    return 0;

  CloseHandle(pi.hThread);

  ASSERT(pi.hProcess);
  hProcess = pi.hProcess;

  return pi.dwProcessId;

#else
  if(pid)
  {
    errno = EINVAL;
    return false;
  }

  // split commandLine into args
  List<String> command;
  {
    String arg;
    for(const tchar_t* p = commandLine; *p;)
      switch(*p)
      {
      case _T('"'):
        for(++p; *p;)
          switch(*p)
          {
          case _T('"'):
            ++p;
            break;
          case _T('\\'):
            if(p[1] == _T('"'))
            {
              arg.append(_T('"'));
              p += 2;
            }
            break;
          default:
            arg.append(*(p++));
        }
        break;
      case _T(' '):
        command.append(arg);
        arg.clear();
        ++p;
        break;
      default:
        arg.append(*(p++));
      }
    if(!arg.isEmpty())
      command.append(arg);
  }

  // load path env
  List<String> searchPaths;
  {
    char* pathVar = getenv("PATH");
    for(const char* str = pathVar; *str;)
    {
      const char* end = strchr(str, ':');
      if(end)
      {
        if(end > str)
          searchPaths.append(String(str, end - str));
        ++end;
        str = end;
      }
      else
      {
        searchPaths.append(String(str, String::length(str)));
        break;
      }
    }
  }

  // find executable
  String program;
  if(!command.isEmpty())
    program = command.front();
  String programPath = program;
  if(((const char*)programPath)[0] != '/')
    for(List<String>::Iterator i = searchPaths.begin(), end = searchPaths.end(); i != end; ++i)
    {
      String testPath = *i;
      testPath.append('/');
      testPath.append(program);
      if(File::exists(testPath))
      {
        programPath = testPath;
        break;
      }
    }

  // start process
  int r = vfork();
  if(r == -1)
    return 0;
  else if(r != 0) // parent
  {
    pid = r;
    return r;
  }
  else // child
  {
    const char** argv = (const char**)alloca(sizeof(const char*) * (command.size() + 1));
    int i = 0;
    for(List<String>::Iterator j = command.begin(), end = command.end(); j != end; ++j)
      argv[i++] = *j;
    argv[i] = 0;

    const char** envp = (const char**)environ;
    const char* executable = programPath;
    if(execve(executable, (char* const*)argv, (char* const*)envp) == -1)
    {
      fprintf(stderr, "%s: %s\n", executable, strerror(errno));
      _exit(EXIT_FAILURE);
    }
    ASSERT(false); // unreachable
    return 0;
  }

#endif
}

bool_t Process::kill()
{
#ifdef _WIN32
  if(hProcess == INVALID_HANDLE_VALUE)
  {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
  TerminateProcess(hProcess, EXIT_FAILURE);
  if(WaitForSingleObject(hProcess, INFINITE) != WAIT_OBJECT_0)
    return false;
  CloseHandle((HANDLE)hProcess);
  hProcess = INVALID_HANDLE_VALUE;
  if(hStdOutRead != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdOutRead);
    hStdOutRead = INVALID_HANDLE_VALUE;
  }
  if(hStdErrRead != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdErrRead);
    hStdErrRead = INVALID_HANDLE_VALUE;
  }
  if(hStdInWrite != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdInWrite);
    hStdInWrite = INVALID_HANDLE_VALUE;
  }
  return true;
#else
  if(!pid)
  {
    errno = EINVAL;
    return false;
  }
  ::kill((pid_t)pid, SIGKILL);
  int status;
  if(waitpid(pid, &status, 0) != (pid_t)pid)
    return false;
  pid = 0;
  return true;
#endif
}

bool_t Process::join(uint32_t& exitCode)
{
#ifdef _WIN32
  if(hProcess == INVALID_HANDLE_VALUE)
  {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
  DWORD dwExitCode = 0;
  if(!GetExitCodeProcess(hProcess, &dwExitCode))
    return false;
  if(dwExitCode == STILL_ACTIVE)
  {
    if(WaitForSingleObject(hProcess, INFINITE) != WAIT_OBJECT_0)
      return false;
    if(!GetExitCodeProcess(hProcess, &dwExitCode))
      return false;
  }
  CloseHandle((HANDLE)hProcess);
  hProcess = INVALID_HANDLE_VALUE;
  exitCode = (uint32_t)dwExitCode;
  if(hStdOutRead != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdOutRead);
    hStdOutRead = INVALID_HANDLE_VALUE;
  }
  if(hStdErrRead != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdErrRead);
    hStdErrRead = INVALID_HANDLE_VALUE;
  }
  if(hStdInWrite != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdInWrite);
    hStdInWrite = INVALID_HANDLE_VALUE;
  }
  return true;
#else
  if(!pid)
  {
    errno = EINVAL;
    return false;
  }
  int status;
  if(waitpid(pid, &status, 0) != (pid_t)pid)
    return false;
  exitCode = WEXITSTATUS(status);
  pid = 0;
  return true;
#endif
}

uint32_t Process::getCurrentProcessId()
{
#ifdef _WIN32
  return (uint32_t)GetCurrentProcessId();
#else
  return (uint32_t)getpid();
#endif
}

bool_t Process::open(const String& commandLine, uint_t streams)
{
#ifdef _WIN32
  if(hProcess != INVALID_HANDLE_VALUE)
  {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }

  STARTUPINFO si;
  PROCESS_INFORMATION pi;

  ZeroMemory(&si, sizeof(si));
  si.cb = sizeof(si);
  ZeroMemory(&pi, sizeof(pi));

  si.dwFlags = STARTF_USESTDHANDLES;
  si.hStdInput = INVALID_HANDLE_VALUE;
  si.hStdOutput = INVALID_HANDLE_VALUE;
  si.hStdError = INVALID_HANDLE_VALUE;

  SECURITY_ATTRIBUTES sa;
  sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  sa.bInheritHandle = TRUE;
  sa.lpSecurityDescriptor = NULL;

  if(streams & stdoutStream)
  {
   if(!CreatePipe(&hStdOutRead, &si.hStdOutput, &sa, 0) ||
      !SetHandleInformation(hStdOutRead, HANDLE_FLAG_INHERIT, 0))
      goto error;
  }
  if(streams & stderrStream)
  {
   if(!CreatePipe(&hStdErrRead, &si.hStdError, &sa, 0) ||
      !SetHandleInformation(hStdErrRead, HANDLE_FLAG_INHERIT, 0))
      goto error;
  }
  if(streams & stdinStream)
  {
   if(!CreatePipe(&si.hStdInput, &hStdInWrite, &sa, 0) ||
      !SetHandleInformation(hStdInWrite, HANDLE_FLAG_INHERIT, 0))
      goto error;
  }

  {
    String args(commandLine);

    if(!CreateProcess(NULL, (tchar_t*)args, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
      goto error;
  }

  CloseHandle(pi.hThread);

  if(si.hStdOutput != INVALID_HANDLE_VALUE)
    CloseHandle(si.hStdOutput);
  if(si.hStdError != INVALID_HANDLE_VALUE)
    CloseHandle(si.hStdError);
  if(si.hStdInput != INVALID_HANDLE_VALUE)
    CloseHandle(si.hStdInput);

  ASSERT(pi.hProcess);
  hProcess = pi.hProcess;
  return true;
error:
  if(hStdOutRead != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdOutRead);
    hStdOutRead = INVALID_HANDLE_VALUE;
  }
  if(hStdErrRead != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdErrRead);
    hStdErrRead = INVALID_HANDLE_VALUE;
  }
  if(hStdInWrite != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdInWrite);
    hStdInWrite = INVALID_HANDLE_VALUE;
  }
  if(si.hStdOutput != INVALID_HANDLE_VALUE)
    CloseHandle(si.hStdOutput);
  if(si.hStdError != INVALID_HANDLE_VALUE)
    CloseHandle(si.hStdError);
  if(si.hStdInput != INVALID_HANDLE_VALUE)
    CloseHandle(si.hStdInput);
  return false;
#else
  // todo
  return 0;
#endif
}

ssize_t Process::read(void_t* buffer, size_t length)
{
#ifdef _WIN32
  DWORD i;
  if(!ReadFile(hStdOutRead, buffer, length, &i, NULL))
    return -1;
  return i;
#else
  // todo
  return -1;
#endif
}

ssize_t Process::read(void_t* buffer, size_t length, uint_t& streams)
{
#ifdef _WIN32
  HANDLE handles[2];
  DWORD handleCount = 0;
  if(streams & stdoutStream)
    handles[handleCount++] = hStdOutRead;
  if(streams & stderrStream)
    handles[handleCount++] = hStdErrRead;
  DWORD dw = WaitForMultipleObjects(handleCount, handles, FALSE, INFINITE);
  if(dw < WAIT_OBJECT_0 || dw >= WAIT_OBJECT_0 + handleCount)
    return -1;
  DWORD i;
  if(!ReadFile(handles[dw - WAIT_OBJECT_0], buffer, length, &i, NULL))
    return -1;
  return i;
#else
  // todo
  return -1;
#endif
}

ssize_t Process::write(const void_t* buffer, size_t length)
{
#ifdef _WIN32
  // todo
  DWORD i;
  if(!WriteFile(hStdInWrite, buffer, length, &i, NULL))
    return -1;
  return i;
#else
  // todo
  return -1;
#endif
}
<commit_msg>Process: Use execvp to replace a process image<commit_after>
#ifdef _WIN32
#include <windows.h>
#else
#include <alloca.h>
#include <unistd.h>
#include <cerrno>
#include <cstdlib>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstdio>
#include <cstring>
#include <signal.h>
#endif

#include <nstd/Debug.h>
#include <nstd/List.h>
#ifndef _WIN32
#include <nstd/File.h>
#endif
#include <nstd/Process.h>

Process::Process()
{
#ifdef _WIN32
  ASSERT(sizeof(hProcess) >= sizeof(HANDLE));
  hProcess = INVALID_HANDLE_VALUE;
  hStdOutRead = INVALID_HANDLE_VALUE;
  hStdErrRead = INVALID_HANDLE_VALUE;
  hStdInWrite = INVALID_HANDLE_VALUE;
#else
  pid = 0;
#endif
}

Process::~Process()
{
  uint32_t exitCode;
  join(exitCode);
}

bool Process::isRunning() const
{
#ifdef _WIN32
  return hProcess != INVALID_HANDLE_VALUE;
#else
  return pid != 0;
#endif
}

uint32_t Process::start(const String& commandLine)
{
#ifdef _WIN32
  if(hProcess != INVALID_HANDLE_VALUE)
  {
    SetLastError(ERROR_INVALID_HANDLE);
    return 0;
  }

  STARTUPINFO si;
  PROCESS_INFORMATION pi;

  ZeroMemory(&si, sizeof(si));
  si.cb = sizeof(si);
  ZeroMemory(&pi, sizeof(pi));

  String args(commandLine);

  if(!CreateProcess(NULL, (tchar_t*)args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    return 0;

  CloseHandle(pi.hThread);

  ASSERT(pi.hProcess);
  hProcess = pi.hProcess;

  return pi.dwProcessId;

#else
  if(pid)
  {
    errno = EINVAL;
    return false;
  }

  // split commandLine into args
  List<String> command;
  {
    String arg;
    for(const tchar_t* p = commandLine; *p;)
      switch(*p)
      {
      case _T('"'):
        for(++p; *p;)
          switch(*p)
          {
          case _T('"'):
            ++p;
            break;
          case _T('\\'):
            if(p[1] == _T('"'))
            {
              arg.append(_T('"'));
              p += 2;
            }
            break;
          default:
            arg.append(*(p++));
        }
        break;
      case _T(' '):
        command.append(arg);
        arg.clear();
        ++p;
        break;
      default:
        arg.append(*(p++));
      }
    if(!arg.isEmpty())
      command.append(arg);
  }

  // start process
  int r = vfork();
  if(r == -1)
    return 0;
  else if(r != 0) // parent
  {
    pid = r;
    return r;
  }
  else // child
  {
    const char** argv = (const char**)alloca(sizeof(const char*) * (command.size() + 1));
    int i = 0;
    for(List<String>::Iterator j = command.begin(), end = command.end(); j != end; ++j)
      argv[i++] = *j;
    argv[i] = 0;

    const char* executable = i > 0 ? argv[0] : "";
    if(execvp(executable, (char* const*)argv) == -1)
    {
      fprintf(stderr, "%s: %s\n", executable, strerror(errno));
      _exit(EXIT_FAILURE);
    }
    ASSERT(false); // unreachable
    return 0;
  }

#endif
}

bool_t Process::kill()
{
#ifdef _WIN32
  if(hProcess == INVALID_HANDLE_VALUE)
  {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
  TerminateProcess(hProcess, EXIT_FAILURE);
  if(WaitForSingleObject(hProcess, INFINITE) != WAIT_OBJECT_0)
    return false;
  CloseHandle((HANDLE)hProcess);
  hProcess = INVALID_HANDLE_VALUE;
  if(hStdOutRead != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdOutRead);
    hStdOutRead = INVALID_HANDLE_VALUE;
  }
  if(hStdErrRead != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdErrRead);
    hStdErrRead = INVALID_HANDLE_VALUE;
  }
  if(hStdInWrite != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdInWrite);
    hStdInWrite = INVALID_HANDLE_VALUE;
  }
  return true;
#else
  if(!pid)
  {
    errno = EINVAL;
    return false;
  }
  ::kill((pid_t)pid, SIGKILL);
  int status;
  if(waitpid(pid, &status, 0) != (pid_t)pid)
    return false;
  pid = 0;
  return true;
#endif
}

bool_t Process::join(uint32_t& exitCode)
{
#ifdef _WIN32
  if(hProcess == INVALID_HANDLE_VALUE)
  {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }
  DWORD dwExitCode = 0;
  if(!GetExitCodeProcess(hProcess, &dwExitCode))
    return false;
  if(dwExitCode == STILL_ACTIVE)
  {
    if(WaitForSingleObject(hProcess, INFINITE) != WAIT_OBJECT_0)
      return false;
    if(!GetExitCodeProcess(hProcess, &dwExitCode))
      return false;
  }
  CloseHandle((HANDLE)hProcess);
  hProcess = INVALID_HANDLE_VALUE;
  exitCode = (uint32_t)dwExitCode;
  if(hStdOutRead != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdOutRead);
    hStdOutRead = INVALID_HANDLE_VALUE;
  }
  if(hStdErrRead != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdErrRead);
    hStdErrRead = INVALID_HANDLE_VALUE;
  }
  if(hStdInWrite != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdInWrite);
    hStdInWrite = INVALID_HANDLE_VALUE;
  }
  return true;
#else
  if(!pid)
  {
    errno = EINVAL;
    return false;
  }
  int status;
  if(waitpid(pid, &status, 0) != (pid_t)pid)
    return false;
  exitCode = WEXITSTATUS(status);
  pid = 0;
  return true;
#endif
}

uint32_t Process::getCurrentProcessId()
{
#ifdef _WIN32
  return (uint32_t)GetCurrentProcessId();
#else
  return (uint32_t)getpid();
#endif
}

bool_t Process::open(const String& commandLine, uint_t streams)
{
#ifdef _WIN32
  if(hProcess != INVALID_HANDLE_VALUE)
  {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }

  STARTUPINFO si;
  PROCESS_INFORMATION pi;

  ZeroMemory(&si, sizeof(si));
  si.cb = sizeof(si);
  ZeroMemory(&pi, sizeof(pi));

  si.dwFlags = STARTF_USESTDHANDLES;
  si.hStdInput = INVALID_HANDLE_VALUE;
  si.hStdOutput = INVALID_HANDLE_VALUE;
  si.hStdError = INVALID_HANDLE_VALUE;

  SECURITY_ATTRIBUTES sa;
  sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  sa.bInheritHandle = TRUE;
  sa.lpSecurityDescriptor = NULL;

  if(streams & stdoutStream)
  {
   if(!CreatePipe(&hStdOutRead, &si.hStdOutput, &sa, 0) ||
      !SetHandleInformation(hStdOutRead, HANDLE_FLAG_INHERIT, 0))
      goto error;
  }
  if(streams & stderrStream)
  {
   if(!CreatePipe(&hStdErrRead, &si.hStdError, &sa, 0) ||
      !SetHandleInformation(hStdErrRead, HANDLE_FLAG_INHERIT, 0))
      goto error;
  }
  if(streams & stdinStream)
  {
   if(!CreatePipe(&si.hStdInput, &hStdInWrite, &sa, 0) ||
      !SetHandleInformation(hStdInWrite, HANDLE_FLAG_INHERIT, 0))
      goto error;
  }

  {
    String args(commandLine);

    if(!CreateProcess(NULL, (tchar_t*)args, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
      goto error;
  }

  CloseHandle(pi.hThread);

  if(si.hStdOutput != INVALID_HANDLE_VALUE)
    CloseHandle(si.hStdOutput);
  if(si.hStdError != INVALID_HANDLE_VALUE)
    CloseHandle(si.hStdError);
  if(si.hStdInput != INVALID_HANDLE_VALUE)
    CloseHandle(si.hStdInput);

  ASSERT(pi.hProcess);
  hProcess = pi.hProcess;
  return true;
error:
  if(hStdOutRead != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdOutRead);
    hStdOutRead = INVALID_HANDLE_VALUE;
  }
  if(hStdErrRead != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdErrRead);
    hStdErrRead = INVALID_HANDLE_VALUE;
  }
  if(hStdInWrite != INVALID_HANDLE_VALUE)
  {
    CloseHandle(hStdInWrite);
    hStdInWrite = INVALID_HANDLE_VALUE;
  }
  if(si.hStdOutput != INVALID_HANDLE_VALUE)
    CloseHandle(si.hStdOutput);
  if(si.hStdError != INVALID_HANDLE_VALUE)
    CloseHandle(si.hStdError);
  if(si.hStdInput != INVALID_HANDLE_VALUE)
    CloseHandle(si.hStdInput);
  return false;
#else
  // todo
  return 0;
#endif
}

ssize_t Process::read(void_t* buffer, size_t length)
{
#ifdef _WIN32
  DWORD i;
  if(!ReadFile(hStdOutRead, buffer, length, &i, NULL))
    return -1;
  return i;
#else
  // todo
  return -1;
#endif
}

ssize_t Process::read(void_t* buffer, size_t length, uint_t& streams)
{
#ifdef _WIN32
  HANDLE handles[2];
  DWORD handleCount = 0;
  if(streams & stdoutStream)
    handles[handleCount++] = hStdOutRead;
  if(streams & stderrStream)
    handles[handleCount++] = hStdErrRead;
  DWORD dw = WaitForMultipleObjects(handleCount, handles, FALSE, INFINITE);
  if(dw < WAIT_OBJECT_0 || dw >= WAIT_OBJECT_0 + handleCount)
    return -1;
  DWORD i;
  if(!ReadFile(handles[dw - WAIT_OBJECT_0], buffer, length, &i, NULL))
    return -1;
  return i;
#else
  // todo
  return -1;
#endif
}

ssize_t Process::write(const void_t* buffer, size_t length)
{
#ifdef _WIN32
  // todo
  DWORD i;
  if(!WriteFile(hStdInWrite, buffer, length, &i, NULL))
    return -1;
  return i;
#else
  // todo
  return -1;
#endif
}
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: HashMaps.hxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: mtg $ $Date: 2001-10-26 21:41:28 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (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.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): Martin Gallwey (gallwey@sun.com)
 *
 *
 ************************************************************************/
#ifndef _HASHMAPS_HXX
#define _HASHMAPS_HXX

#ifndef _COM_SUN_STAR_CONTAINER_XNAMECONTAINER_HPP_
#include <com/sun/star/container/XNameContainer.hpp>
#endif
#ifndef _COM_SUN_STAR_LANG_XUNOTUNNEl_HPP_
#include <com/sun/star/lang/XUnoTunnel.hpp>
#endif
#ifndef _ZIP_ENTRY_HXX_
#include <ZipEntry.hxx>
#endif
#include <hash_map>
#include <memory>

struct eqFunc
{
    sal_Bool operator()( const rtl::OUString &r1,
                         const rtl::OUString &r2) const
    {
        return r1 == r2;
    }
};

class ZipPackageFolder;
struct ContentInfo;

typedef std::hash_map < rtl::OUString,
                        ZipPackageFolder *,
                        ::rtl::OUStringHash,
                        eqFunc > FolderHash;

typedef std::hash_map < rtl::OUString,
                        std::auto_ptr < ContentInfo >,
                        ::rtl::OUStringHash,
                        eqFunc > ContentHash;

typedef std::hash_map < rtl::OUString,
                        ZipEntry,
                        rtl::OUStringHash,
                        eqFunc > EntryHash;

#endif
<commit_msg>#93877# using auto_ptr's in STL containers is apparently bad, so use ref-counted ORefs instead<commit_after>/*************************************************************************
 *
 *  $RCSfile: HashMaps.hxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: mtg $ $Date: 2001-10-30 13:52:18 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (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.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): Martin Gallwey (gallwey@sun.com)
 *
 *
 ************************************************************************/
#ifndef _HASHMAPS_HXX
#define _HASHMAPS_HXX

#ifndef _ZIP_ENTRY_HXX_
#include <ZipEntry.hxx>
#endif
#ifndef _VOS_REF_HXX_
#include <vos/ref.hxx>
#endif
#include <hash_map>

struct eqFunc
{
    sal_Bool operator()( const rtl::OUString &r1,
                         const rtl::OUString &r2) const
    {
        return r1 == r2;
    }
};

class ZipPackageFolder;
class ContentInfo;

typedef std::hash_map < rtl::OUString,
                        ZipPackageFolder *,
                        ::rtl::OUStringHash,
                        eqFunc > FolderHash;

typedef std::hash_map < rtl::OUString,
                        vos::ORef < ContentInfo >,
                        ::rtl::OUStringHash,
                        eqFunc > ContentHash;

typedef std::hash_map < rtl::OUString,
                        ZipEntry,
                        rtl::OUStringHash,
                        eqFunc > EntryHash;

#endif
<|endoftext|>


<commit_before>#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <cmath>
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include "Base.h"
using namespace std;
//Simple constructor
Processes::Processes()
{
    currCmds.size() = 0;
    string andand == "&&";
    string oror = "||";
}
//Simple destructor
Processes::~Processes()
{
    for(int i = 0; i < currCmds; ++i)
    {
        delete currCmds.at(i);
    }
}
//This is used to add connector objects to the currCmds vector
void Processes::addCmd(Base* nxtCmd)
{
    this->currCmds.push_back(nxtCmd);
}
//This executes all objects within the object's currCmds vector
void Processes::execute()
{
    int temp = 0;
    for(int i = 0; i < currCmds.size(); ++i)
    {
        temp = currCmds.at(i)->execute();
    }
}
//Processes parses the code to split it into separate command lines
//should there be any semicolons present.
void Processes::parse(string input)
{
    vector<string> currCs
    istringstream inSS(input);
    string currString;
    //Main loop for parsing input that contains semicolons
    while(inSS >> currString);
    {
        bool hashtag = false;
        bool semicolon = false;
        currCs.size() = 0;
        currCs.push_back(currString);
        while(!semicolon)
        {
            inSS >> currString;
            //Tests for hashtag/semicolon presence
            if(currString.find("#") != string::npos)
            {
                hashtag = true;
            }
            if(currString.find(";") != string::npos)
            {
                semicolon = true;
            }
            if(!hashtag)
            {
                currCs.push_back(currString);
            }
        }
    }

}<commit_msg>Making changes<commit_after>#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <cmath>
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include "Base.h"
using namespace std;
//Simple constructor
Processes::Processes()
{
    currCmds.size() = 0;
    string andand == "&&";
    string oror = "||";
}
//Simple destructor
Processes::~Processes()
{
    for(int i = 0; i < currCmds; ++i)
    {
        delete currCmds.at(i);
    }
}
//This is used to add connector objects to the currCmds vector
void Processes::addCmd(Base* nxtCmd)
{
    this->currCmds.push_back(nxtCmd);
}
//This executes all objects within the object's currCmds vector
void Processes::execute()
{
    int temp = 0;
    for(int i = 0; i < currCmds.size(); ++i)
    {
        temp = currCmds.at(i)->execute();
    }
}
//Processes parses the code to split it into separate command lines
//should there be any semicolons present.
void Processes::parse(string input)
{
    vector<string> currCs
    istringstream inSS(input);
    string currString;
    //Main loop for parsing input that contains semicolons
    while(input.find(";") == string::npos);
    {
        bool hashtag = false;
        bool semicolon = false;
        currCs.size() = 0;
        while(!semicolon)
        {
            inSS >> currString;
            input.erase(0, currString.size())
            //Tests for hashtag/semicolon presence
            if(currString.find("#") != string::npos)
            {
                hashtag = true;
            }
            if(currString.find(";") != string::npos)
            {
                semicolon = true;
            }
            if((!hashtag) && (!semicolon))
            {
                currCs.push_back(currString);
            }
        }
    }
}<|endoftext|>