<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: JoinDesignView.hxx,v $
 *
 *  $Revision: 1.8 $
 *
 *  last change: $Author: obo $ $Date: 2006-07-10 15:28:19 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 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
 *
 ************************************************************************/

#ifndef DBAUI_JOINDESIGNVIEW_HXX
#define DBAUI_JOINDESIGNVIEW_HXX

#ifndef DBAUI_DATAVIEW_HXX
#include "dataview.hxx"
#endif
#ifndef _VECTOR_
#include <vector>
#endif
#ifndef _STRING_HXX
#include <tools/string.hxx>
#endif
#ifndef DBAUI_ENUMTYPES_HXX
#include "QEnumTypes.hxx"
#endif

#include <memory>

namespace dbaui
{
    class OTableConnection;
    class OConnectionLineData;
    class OJoinController;
    class OScrollWindowHelper;
    class OJoinTableView;
    class OTableWindow;

    class OJoinDesignView : public ODataView
    {
    protected:
        OScrollWindowHelper*    m_pScrollWindow;    // contains only the scrollbars
        OJoinTableView*         m_pTableView;       // presents the upper window
        OJoinController*        m_pController;

    public:
        OJoinDesignView(Window* pParent,
                        OJoinController* _pController,
                        const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& );
        virtual ~OJoinDesignView();

        // set the view readonly or not
        virtual void setReadOnly(sal_Bool _bReadOnly);
        // set the statement for representation
        /// late construction
        virtual void Construct();
        virtual void initialize();
        virtual void KeyInput( const KeyEvent& rEvt );

        virtual void SaveTabWinUIConfig(OTableWindow* pWin);
        OJoinController* getController() const { return m_pController; }
        // called when fields are deleted
        // called when a table from tabeview was deleted
        void TableDeleted(const ::rtl::OUString& rAliasName);

        OJoinTableView* getTableView() const { return m_pTableView; }
        void zoomTableView(const Fraction& _rFraction);
        void SaveUIConfig();
    protected:
        // return the Rectangle where I can paint myself
        virtual void resizeDocumentView(Rectangle& rRect);
        DECL_LINK( SplitHdl, void* );
    };
}
#endif // DBAUI_JOINDESIGNVIEW_HXX

<commit_msg>INTEGRATION: CWS dba23a (1.8.118); FILE MERGED 2007/02/26 11:48:13 fs 1.8.118.1: remove unused code Issue number:  #i74804# Submitted by:  jnavrati@openoffice.org Reviewed by:   frank.schoenheit@sun.com<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: JoinDesignView.hxx,v $
 *
 *  $Revision: 1.9 $
 *
 *  last change: $Author: kz $ $Date: 2007-05-10 10:28:13 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 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
 *
 ************************************************************************/

#ifndef DBAUI_JOINDESIGNVIEW_HXX
#define DBAUI_JOINDESIGNVIEW_HXX

#ifndef DBAUI_DATAVIEW_HXX
#include "dataview.hxx"
#endif
#ifndef _VECTOR_
#include <vector>
#endif
#ifndef _STRING_HXX
#include <tools/string.hxx>
#endif
#ifndef DBAUI_ENUMTYPES_HXX
#include "QEnumTypes.hxx"
#endif

#include <memory>

namespace dbaui
{
    class OTableConnection;
    class OConnectionLineData;
    class OJoinController;
    class OScrollWindowHelper;
    class OJoinTableView;
    class OTableWindow;

    class OJoinDesignView : public ODataView
    {
    protected:
        OScrollWindowHelper*    m_pScrollWindow;    // contains only the scrollbars
        OJoinTableView*         m_pTableView;       // presents the upper window
        OJoinController*        m_pController;

    public:
        OJoinDesignView(Window* pParent,
                        OJoinController* _pController,
                        const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& );
        virtual ~OJoinDesignView();

        // set the view readonly or not
        virtual void setReadOnly(sal_Bool _bReadOnly);
        // set the statement for representation
        /// late construction
        virtual void Construct();
        virtual void initialize();
        virtual void KeyInput( const KeyEvent& rEvt );

        virtual void SaveTabWinUIConfig(OTableWindow* pWin);
        OJoinController* getController() const { return m_pController; }
        // called when fields are deleted

        OJoinTableView* getTableView() const { return m_pTableView; }
    protected:
        // return the Rectangle where I can paint myself
        virtual void resizeDocumentView(Rectangle& rRect);
        DECL_LINK( SplitHdl, void* );
    };
}
#endif // DBAUI_JOINDESIGNVIEW_HXX

<|endoftext|>


<commit_before>//---------------------------------------------------------------------------
//    $Id$
//    Version: $Name$
//
//    Copyright (C) 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2012 by the deal.II authors
//
//    This file is subject to QPL and may not be  distributed
//    without copyright and license information. Please refer
//    to the file deal.II/doc/license.html for the  text  and
//    further information on this license.
//
//---------------------------------------------------------------------------

#include <deal.II/base/thread_management.h>
#include <iostream>
#include <cstdlib>

#ifdef DEAL_II_USE_MT_POSIX
#  include <list>
#endif

#ifndef DEAL_II_USE_DIRECT_ERRNO_H
#  include <errno.h>
#else
#  include </usr/include/errno.h>
#endif
#ifndef DEAL_II_MSVC
#  include <sys/errno.h>
#endif

#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif

#ifdef HAVE_SYS_SYSCALL_H
#  include <sys/syscall.h>
#endif

DEAL_II_NAMESPACE_OPEN

namespace Threads
{
  namespace internal
  {
                                     // counter and access mutex for the
                                     // number of threads
    volatile unsigned int n_existing_threads_counter = 1;
    ThreadMutex  n_existing_threads_mutex;

    void register_thread ()
    {
      ThreadMutex::ScopedLock lock (n_existing_threads_mutex);
      ++n_existing_threads_counter;
    }

    void deregister_thread ()
    {
      ThreadMutex::ScopedLock lock (n_existing_threads_mutex);
      --n_existing_threads_counter;
      Assert (n_existing_threads_counter >= 1,
              ExcInternalError());
    }

    void handle_std_exception (const std::exception &exc)
    {
                                       // lock the following context
                                       // to ensure that we don't
                                       // print things over each other
                                       // if we have trouble from
                                       // multiple threads. release
                                       // the lock before calling
                                       // std::abort, though
      static Mutex mutex;
      {
        Mutex::ScopedLock lock(mutex);

        std::cerr << std::endl << std::endl
                  << "---------------------------------------------------------"
                  << std::endl
                  << "In one of the sub-threads of this program, an exception\n"
                  << "was thrown and not caught. Since exceptions do not\n"
                  << "propagate to the main thread, the library has caught it.\n"
                  << "The information carried by this exception is given below.\n"
                  << std::endl
                  << "---------------------------------------------------------"
                  << std::endl;
        std::cerr << "Exception message: " << std::endl
                  << "  " << exc.what() << std::endl
                  << "Exception type: " << std::endl
                  << "  " << typeid(exc).name() << std::endl;
        std::cerr << "Aborting!" << std::endl
                  << "---------------------------------------------------------"
                  << std::endl;
      }

      std::abort ();
    }

    void handle_unknown_exception ()
    {
                                       // lock the following context
                                       // to ensure that we don't
                                       // print things over each other
                                       // if we have trouble from
                                       // multiple threads. release
                                       // the lock before calling
                                       // std::abort, though
      static Mutex mutex;
      {
        Mutex::ScopedLock lock(mutex);

        std::cerr << std::endl << std::endl
                  << "---------------------------------------------------------"
                  << std::endl
                  << "In one of the sub-threads of this program, an exception\n"
                  << "was thrown and not caught. Since exceptions do not\n"
                  << "propagate to the main thread, the library has caught it.\n"
                  << std::endl
                  << "---------------------------------------------------------"
                  << std::endl;
        std::cerr << "Type of exception is unknown, but not std::exception.\n"
                  << "No additional information is available.\n"
                  << "---------------------------------------------------------"
                  << std::endl;
      }
      std::abort ();
    }
  }

  unsigned int n_existing_threads ()
  {
    ThreadMutex::ScopedLock lock (internal::n_existing_threads_mutex);
    return internal::n_existing_threads_counter;
  }

  unsigned int this_thread_id ()
  {
#if HAVE_GETPID
    const pid_t this_id = getpid();
#elif SYS_gettid
    const int this_id = syscall(SYS_gettid);
#else
#  ifdef DEAL_II_MSVC
    const unsigned int this_id = 0;
#  else
    const pid_t this_id = 0;
#  endif
#endif
    return static_cast<unsigned int>(this_id);
  }

#if DEAL_II_USE_MT != 1
  DummyBarrier::DummyBarrier (const unsigned int  count,
                              const char         *,
                              void               *)
  {
    Assert (count == 1, ExcBarrierSizeNotUseful(count));
  }

#else
#  ifdef DEAL_II_USE_MT_POSIX

#ifndef DEAL_II_USE_MT_POSIX_NO_BARRIERS
  PosixThreadBarrier::PosixThreadBarrier (const unsigned int  count,
                                          const char         *,
                                          void               *)
  {
    pthread_barrier_init (&barrier, 0, count);
  }

#else

  PosixThreadBarrier::PosixThreadBarrier (const unsigned int  count,
                                          const char         *,
                                          void               *)
                  : count (count)
  {
                                     // throw an exception unless we
                                     // have the special case that a
                                     // count of 1 is given, since
                                     // then waiting for a barrier is
                                     // a no-op, and we don't need the
                                     // POSIX functionality
    AssertThrow (count == 1,
                 ExcMessage ("Your local POSIX installation does not support\n"
                             "POSIX barriers. You will not be able to use\n"
                             "this class, but the rest of the threading\n"
                             "functionality is available."));
  }
#endif

  PosixThreadBarrier::~PosixThreadBarrier ()
  {
#ifndef DEAL_II_USE_MT_POSIX_NO_BARRIERS
    pthread_barrier_destroy (&barrier);
#else
                                     // unless the barrier is a no-op,
                                     // complain again (how did we get
                                     // here then?)
    if (count != 1)
      std::abort ();
#endif
  }

  int
  PosixThreadBarrier::wait ()
  {
#ifndef DEAL_II_USE_MT_POSIX_NO_BARRIERS
    return pthread_barrier_wait (&barrier);
#else
                                     // in the special case, this
                                     // function is a no-op. otherwise
                                     // complain about the missing
                                     // POSIX functions
    if (count == 1)
      return 0;
    else
      {
        std::abort ();
        return 1;
      };
#endif
  }

#  endif
#endif

  std::vector<std::pair<unsigned int,unsigned int> >
  split_interval (const unsigned int begin,
                  const unsigned int end,
                  const unsigned int n_intervals)
  {
    Assert (end >= begin, ExcInternalError());

    const unsigned int n_elements              = end-begin;
    const unsigned int n_elements_per_interval = n_elements / n_intervals;
    const unsigned int residual                = n_elements % n_intervals;

    std::vector<std::pair<unsigned int,unsigned int> > return_values (n_intervals);

    return_values[0].first = begin;
    for (unsigned int i=0; i<n_intervals; ++i)
      {
        if (i != n_intervals-1)
          {
            return_values[i].second = (return_values[i].first
                                       + n_elements_per_interval);
                                             // distribute residual in
                                             // division equally among
                                             // the first few
                                             // subintervals
            if (i < residual)
              ++return_values[i].second;
            return_values[i+1].first = return_values[i].second;
          }
        else
          return_values[i].second = end;
      };
    return return_values;
  }
}   // end namespace Thread

DEAL_II_NAMESPACE_CLOSE
<commit_msg>Undo r26920 since that appears to have led to numerous testsuite failures.<commit_after>//---------------------------------------------------------------------------
//    $Id$
//    Version: $Name$
//
//    Copyright (C) 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2012 by the deal.II authors
//
//    This file is subject to QPL and may not be  distributed
//    without copyright and license information. Please refer
//    to the file deal.II/doc/license.html for the  text  and
//    further information on this license.
//
//---------------------------------------------------------------------------

#include <deal.II/base/thread_management.h>
#include <iostream>
#include <cstdlib>

#ifdef DEAL_II_USE_MT_POSIX
#  include <list>
#endif

#ifndef DEAL_II_USE_DIRECT_ERRNO_H
#  include <errno.h>
#else
#  include </usr/include/errno.h>
#endif
#ifndef DEAL_II_MSVC
#  include <sys/errno.h>
#endif

#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif

#ifdef HAVE_SYS_SYSCALL_H
#  include <sys/syscall.h>
#endif

DEAL_II_NAMESPACE_OPEN

namespace Threads
{
  namespace internal
  {
                                     // counter and access mutex for the
                                     // number of threads
    volatile unsigned int n_existing_threads_counter = 1;
    ThreadMutex  n_existing_threads_mutex;

    void register_thread ()
    {
      ThreadMutex::ScopedLock lock (n_existing_threads_mutex);
      ++n_existing_threads_counter;
    }

    void deregister_thread ()
    {
      ThreadMutex::ScopedLock lock (n_existing_threads_mutex);
      --n_existing_threads_counter;
      Assert (n_existing_threads_counter >= 1,
              ExcInternalError());
    }

    void handle_std_exception (const std::exception &exc)
    {
                                       // lock the following context
                                       // to ensure that we don't
                                       // print things over each other
                                       // if we have trouble from
                                       // multiple threads. release
                                       // the lock before calling
                                       // std::abort, though
      static Mutex mutex;
      {
        Mutex::ScopedLock lock(mutex);

        std::cerr << std::endl << std::endl
                  << "---------------------------------------------------------"
                  << std::endl
                  << "In one of the sub-threads of this program, an exception\n"
                  << "was thrown and not caught. Since exceptions do not\n"
                  << "propagate to the main thread, the library has caught it.\n"
                  << "The information carried by this exception is given below.\n"
                  << std::endl
                  << "---------------------------------------------------------"
                  << std::endl;
        std::cerr << "Exception message: " << std::endl
                  << "  " << exc.what() << std::endl
                  << "Exception type: " << std::endl
                  << "  " << typeid(exc).name() << std::endl;
        std::cerr << "Aborting!" << std::endl
                  << "---------------------------------------------------------"
                  << std::endl;
      }

      std::abort ();
    }

    void handle_unknown_exception ()
    {
                                       // lock the following context
                                       // to ensure that we don't
                                       // print things over each other
                                       // if we have trouble from
                                       // multiple threads. release
                                       // the lock before calling
                                       // std::abort, though
      static Mutex mutex;
      {
        Mutex::ScopedLock lock(mutex);

        std::cerr << std::endl << std::endl
                  << "---------------------------------------------------------"
                  << std::endl
                  << "In one of the sub-threads of this program, an exception\n"
                  << "was thrown and not caught. Since exceptions do not\n"
                  << "propagate to the main thread, the library has caught it.\n"
                  << std::endl
                  << "---------------------------------------------------------"
                  << std::endl;
        std::cerr << "Type of exception is unknown, but not std::exception.\n"
                  << "No additional information is available.\n"
                  << "---------------------------------------------------------"
                  << std::endl;
      }
      std::abort ();
    }
  }

  unsigned int n_existing_threads ()
  {
    ThreadMutex::ScopedLock lock (internal::n_existing_threads_mutex);
    return internal::n_existing_threads_counter;
  }

  unsigned int this_thread_id ()
  {
#ifdef SYS_gettid
    const int this_id = syscall(SYS_gettid);
#elif HAVE_GETPID
    const pid_t this_id = getpid();
#else
#  ifdef DEAL_II_MSVC
    const unsigned int this_id = 0;
#  else
    const pid_t this_id = 0;
#  endif
#endif
    return static_cast<unsigned int>(this_id);
  }

#if DEAL_II_USE_MT != 1
  DummyBarrier::DummyBarrier (const unsigned int  count,
                              const char         *,
                              void               *)
  {
    Assert (count == 1, ExcBarrierSizeNotUseful(count));
  }

#else
#  ifdef DEAL_II_USE_MT_POSIX

#ifndef DEAL_II_USE_MT_POSIX_NO_BARRIERS
  PosixThreadBarrier::PosixThreadBarrier (const unsigned int  count,
                                          const char         *,
                                          void               *)
  {
    pthread_barrier_init (&barrier, 0, count);
  }

#else

  PosixThreadBarrier::PosixThreadBarrier (const unsigned int  count,
                                          const char         *,
                                          void               *)
                  : count (count)
  {
                                     // throw an exception unless we
                                     // have the special case that a
                                     // count of 1 is given, since
                                     // then waiting for a barrier is
                                     // a no-op, and we don't need the
                                     // POSIX functionality
    AssertThrow (count == 1,
                 ExcMessage ("Your local POSIX installation does not support\n"
                             "POSIX barriers. You will not be able to use\n"
                             "this class, but the rest of the threading\n"
                             "functionality is available."));
  }
#endif

  PosixThreadBarrier::~PosixThreadBarrier ()
  {
#ifndef DEAL_II_USE_MT_POSIX_NO_BARRIERS
    pthread_barrier_destroy (&barrier);
#else
                                     // unless the barrier is a no-op,
                                     // complain again (how did we get
                                     // here then?)
    if (count != 1)
      std::abort ();
#endif
  }

  int
  PosixThreadBarrier::wait ()
  {
#ifndef DEAL_II_USE_MT_POSIX_NO_BARRIERS
    return pthread_barrier_wait (&barrier);
#else
                                     // in the special case, this
                                     // function is a no-op. otherwise
                                     // complain about the missing
                                     // POSIX functions
    if (count == 1)
      return 0;
    else
      {
        std::abort ();
        return 1;
      };
#endif
  }

#  endif
#endif

  std::vector<std::pair<unsigned int,unsigned int> >
  split_interval (const unsigned int begin,
                  const unsigned int end,
                  const unsigned int n_intervals)
  {
    Assert (end >= begin, ExcInternalError());

    const unsigned int n_elements              = end-begin;
    const unsigned int n_elements_per_interval = n_elements / n_intervals;
    const unsigned int residual                = n_elements % n_intervals;

    std::vector<std::pair<unsigned int,unsigned int> > return_values (n_intervals);

    return_values[0].first = begin;
    for (unsigned int i=0; i<n_intervals; ++i)
      {
        if (i != n_intervals-1)
          {
            return_values[i].second = (return_values[i].first
                                       + n_elements_per_interval);
                                             // distribute residual in
                                             // division equally among
                                             // the first few
                                             // subintervals
            if (i < residual)
              ++return_values[i].second;
            return_values[i+1].first = return_values[i].second;
          }
        else
          return_values[i].second = end;
      };
    return return_values;
  }
}   // end namespace Thread

DEAL_II_NAMESPACE_CLOSE
<|endoftext|>


<commit_before>/**
 * @file himan.cpp
 *
 * @brief himan main program
 *
 * @author partio
 *
 */

#include <iostream>
#include "himan_common.h"
#include "plugin_factory.h"
#include "json_parser.h"
#include "himan_plugin.h"
#include "compiled_plugin.h"
#include "auxiliary_plugin.h"
#include "logger_factory.h"
#include <vector>
#include <boost/lexical_cast.hpp>
#include <boost/program_options.hpp>

#define HIMAN_AUXILIARY_INCLUDE

#include "pcuda.h"

#undef HIMAN_AUXILIARY_INCLUDE

using namespace himan;
using namespace std;

void banner();
shared_ptr<configuration> ParseCommandLine(int argc, char** argv);

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

	shared_ptr<configuration> conf;

	try
	{
		conf = ParseCommandLine(argc, argv);
	}
	catch (const std::exception &e)
	{
		cerr << e.what() << endl;
		exit(1);
	}

	unique_ptr<logger> aLogger = unique_ptr<logger> (logger_factory::Instance()->GetLog("himan"));

	/*
	 * Initialize plugin factory before parsing configuration file. This prevents himan from
	 * terminating suddenly with SIGSEGV on RHEL5 environments.
	 *
	 * Also, it may be good to have neons -plugin declared at main level of program. This
	 * goes also for the cache plugin.
	 *
	 * Note that we don't actually do anything with the plugin here.
	 */

	shared_ptr<plugin::auxiliary_plugin> n = dynamic_pointer_cast<plugin::auxiliary_plugin> (plugin_factory::Instance()->Plugin("neons"));
	shared_ptr<plugin::auxiliary_plugin> c = dynamic_pointer_cast<plugin::auxiliary_plugin> (plugin_factory::Instance()->Plugin("cache"));

	std::vector<shared_ptr<plugin_configuration>> plugins;

	try
	{
		plugins = json_parser::Instance()->Parse(conf);
	}
	catch (std::runtime_error& e)
	{
		aLogger->Fatal(e.what());
		exit(1);
	}

	conf.reset(); // we don't need this conf anymore, it was only used as a base for json_parser

	banner();

	vector<shared_ptr<plugin::himan_plugin>> thePlugins = plugin_factory::Instance()->Plugins();

	aLogger->Info("Found " + boost::lexical_cast<string> (thePlugins.size()) + " plugins");

	aLogger->Debug("Processqueue size: " + boost::lexical_cast<string> (plugins.size()));

	for (size_t i = 0; i < plugins.size(); i++)
	{

		shared_ptr<plugin_configuration> pc = plugins[i];

		if (pc->Name() == "precipitation")
		{
			aLogger->Warning("Plugin 'precipitation' is deprecated -- use 'split_sum' instead'");
			pc->Name("split_sum");
		}

		auto aPlugin = dynamic_pointer_cast<plugin::compiled_plugin > (plugin_factory::Instance()->Plugin(pc->Name()));

		if (!aPlugin)
		{
			aLogger->Error("Unable to declare plugin " + pc->Name());
			continue;
		}

		if (pc->StatisticsEnabled())
		{
			pc->StartStatistics();
		}

		aLogger->Info("Calculating " + pc->Name());

		try
		{
			aPlugin->Process(pc);
		}
		catch (const exception& e)
		{
			aLogger->Fatal(string("Caught exception: ") + e.what());
			exit(1);
		}

		if (pc->StatisticsEnabled())
		{
			pc->WriteStatistics();
		}

	}

	return 0;

}

void banner()
{
	cout << endl
			  << "************************************************" << endl
			  << "* By the Power of Grayskull, I Have the Power! *" << endl
			  << "************************************************" << endl << endl;

}

shared_ptr<configuration> ParseCommandLine(int argc, char** argv)
{

	shared_ptr<configuration> conf = make_shared<configuration> ();

	namespace po = boost::program_options;

	po::options_description desc("Allowed options");

	// Can't use required() since it doesn't allow us to use --list-plugins

	string outfileType = "";
	string confFile = "";
	string statisticsLabel = "";
	vector<string> auxFiles;
	short int cudaDeviceId = 0;

	himan::HPDebugState debugState = himan::kDebugMsg;

	int logLevel = 0;
	short int threadCount = -1;

	desc.add_options()
	("help,h", "print out help message")
	("type,t", po::value(&outfileType), "output file type, one of: grib, grib2, netcdf, querydata")
	("version,v", "display version number")
	("configuration-file,f", po::value(&confFile), "configuration file")
	("auxiliary-files,a", po::value<vector<string> > (&auxFiles), "auxiliary (helper) file(s)")
	("threads,j", po::value(&threadCount), "number of started threads")
	("list-plugins,l", "list all defined plugins")
	("debug-level,d", po::value(&logLevel), "set log level: 0(fatal) 1(error) 2(warning) 3(info) 4(debug) 5(trace)")
	("statistics,s", po::value(&statisticsLabel), "record statistics information")
	("no-cuda", "disable all cuda extensions")
	("no-cuda-packing", "disable cuda packing and unpacking of grib data")
	("cuda-device-id", po::value(&cudaDeviceId), "use a specific cuda device (default: 0)")
	("cuda-properties", "print cuda device properties of platform (if any)")
	;

	po::positional_options_description p;
	p.add("auxiliary-files", -1);

	po::variables_map opt;
	po::store(po::command_line_parser(argc, argv)
			  .options(desc)
			  .positional(p)
			  .run(),
			  opt);

	po::notify(opt);

	if (threadCount)
	{
		conf->ThreadCount(threadCount);
	}

	if (auxFiles.size())
	{
		conf->AuxiliaryFiles(auxFiles);
	}

	if (logLevel)
	{
		switch (logLevel)
		{
		case 0:
			debugState = kFatalMsg;
			break;
		case 1:
			debugState = kErrorMsg;
			break;
		case 2:
			debugState = kWarningMsg;
			break;
		case 3:
			debugState = kInfoMsg;
			break;
		case 4:
			debugState = kDebugMsg;
			break;
		case 5:
			debugState = kTraceMsg;
			break;
		}

	}

	logger_factory::Instance()->DebugState(debugState);

	if (opt.count("version"))
	{
		cout << "himan-bin version " << __DATE__ << " " << __TIME__ << endl;
		exit(1);
	}

	shared_ptr<plugin::pcuda> cuda;

#ifndef HAVE_CUDA
	if (opt.count("cuda-properties"))
	{
		cout << "CUDA support turned off at compile time" << endl;
		exit(1);
	}

	conf->UseCuda(false);
	conf->UseCudaForPacking(false);
	conf->CudaDeviceCount(0);

	if (opt.count("cuda-device-id"))
	{
		cerr << "CUDA support turned off at compile time" << endl;
	}

#else
	cuda = dynamic_pointer_cast<plugin::pcuda> (plugin_factory::Instance()->Plugin("pcuda"));

	if (opt.count("cuda-properties"))
	{
		cuda->Capabilities();
		exit(1);
	}

	if (opt.count("no-cuda-packing"))
	{
		conf->UseCudaForPacking(false);
	}

	if (opt.count("no-cuda"))
	{
		conf->UseCuda(false);
		conf->UseCudaForPacking(false);
	}

	conf->CudaDeviceCount(static_cast<short> (cuda->DeviceCount()));

	if (opt.count("cuda-device-id"))
	{
		if (cudaDeviceId >= conf->CudaDeviceCount() || cudaDeviceId < 0)
		{
			cerr << "cuda device id " << cudaDeviceId << " requested, allowed maximum cuda device id is " << conf->CudaDeviceCount()-1 << endl;
			cerr << "cuda mode is disabled" << endl;
			conf->UseCuda(false);
			conf->UseCudaForPacking(false);
		}

		conf->CudaDeviceId(cudaDeviceId);
	}
#endif

	if (!outfileType.empty())
	{
		if (outfileType == "grib")
		{
			conf->OutputFileType(kGRIB1);
		}
		else if (outfileType == "grib2")
		{
			conf->OutputFileType(kGRIB2);
		}
		else if (outfileType == "netcdf")
		{
			conf->OutputFileType(kNetCDF);
		}
		else if (outfileType == "querydata")
		{
			conf->OutputFileType(kQueryData);
		}
		else
		{
			cerr << "Invalid file type: " << outfileType << endl;
			exit(1);
		}
	}

	if (opt.count("help"))
	{
		cout << "usage: himan [ options ]" << endl;
		cout << desc;
		cout << endl << "Examples:" << endl;
		cout << "  himan -f etc/tpot.json" << endl;
		cout << "  himan -f etc/vvmms.json -a file.grib -t querydata" << endl << endl;
		exit(1);
	}

	if (opt.count("list-plugins"))
	{

		vector<shared_ptr<plugin::himan_plugin>> thePlugins = plugin_factory::Instance()->Plugins();

		for (size_t i = 0; i < thePlugins.size(); i++)
		{
			cout << "Plugin '"  << thePlugins[i]->ClassName() << "'" << endl << "\tversion " << thePlugins[i]->Version() << endl;

			switch (thePlugins[i]->PluginClass())
			{

				case kCompiled:
					if (dynamic_pointer_cast<plugin::compiled_plugin> (thePlugins[i])->CudaEnabledCalculation())
					{
						cout << "\tcuda-enabled\n";
					}
					cout << "\ttype compiled (hard-coded) --> " << dynamic_pointer_cast<plugin::compiled_plugin> (thePlugins[i])->Formula() << endl;
					break;

				case kAuxiliary:
					cout << "\ttype aux" << endl;
					break;

				case kInterpreted:
					cout << "\ttype interpreted" << endl;
					break;

				default:
					cout << " has unknown plugin type" << endl;
					exit(1);
			}
		}

		exit(1);
	}

	if (!confFile.empty())
	{
		conf->ConfigurationFile(confFile);
	}
	else
	{
		cerr << "himan: Configuration file not defined" << endl;
		exit(1);
	}

	if (!statisticsLabel.empty())
	{
		conf->StatisticsLabel(statisticsLabel);
	}

	return conf;
}
<commit_msg>Rename kindex to stability for backwards compatibility<commit_after>/**
 * @file himan.cpp
 *
 * @brief himan main program
 *
 * @author partio
 *
 */

#include <iostream>
#include "himan_common.h"
#include "plugin_factory.h"
#include "json_parser.h"
#include "himan_plugin.h"
#include "compiled_plugin.h"
#include "auxiliary_plugin.h"
#include "logger_factory.h"
#include <vector>
#include <boost/lexical_cast.hpp>
#include <boost/program_options.hpp>

#define HIMAN_AUXILIARY_INCLUDE

#include "pcuda.h"

#undef HIMAN_AUXILIARY_INCLUDE

using namespace himan;
using namespace std;

void banner();
shared_ptr<configuration> ParseCommandLine(int argc, char** argv);

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

	shared_ptr<configuration> conf;

	try
	{
		conf = ParseCommandLine(argc, argv);
	}
	catch (const std::exception &e)
	{
		cerr << e.what() << endl;
		exit(1);
	}

	unique_ptr<logger> aLogger = unique_ptr<logger> (logger_factory::Instance()->GetLog("himan"));

	/*
	 * Initialize plugin factory before parsing configuration file. This prevents himan from
	 * terminating suddenly with SIGSEGV on RHEL5 environments.
	 *
	 * Also, it may be good to have neons -plugin declared at main level of program. This
	 * goes also for the cache plugin.
	 *
	 * Note that we don't actually do anything with the plugin here.
	 */

	shared_ptr<plugin::auxiliary_plugin> n = dynamic_pointer_cast<plugin::auxiliary_plugin> (plugin_factory::Instance()->Plugin("neons"));
	shared_ptr<plugin::auxiliary_plugin> c = dynamic_pointer_cast<plugin::auxiliary_plugin> (plugin_factory::Instance()->Plugin("cache"));

	std::vector<shared_ptr<plugin_configuration>> plugins;

	try
	{
		plugins = json_parser::Instance()->Parse(conf);
	}
	catch (std::runtime_error& e)
	{
		aLogger->Fatal(e.what());
		exit(1);
	}

	conf.reset(); // we don't need this conf anymore, it was only used as a base for json_parser

	banner();

	vector<shared_ptr<plugin::himan_plugin>> thePlugins = plugin_factory::Instance()->Plugins();

	aLogger->Info("Found " + boost::lexical_cast<string> (thePlugins.size()) + " plugins");

	aLogger->Debug("Processqueue size: " + boost::lexical_cast<string> (plugins.size()));

	for (size_t i = 0; i < plugins.size(); i++)
	{

		shared_ptr<plugin_configuration> pc = plugins[i];

		if (pc->Name() == "precipitation")
		{
			aLogger->Warning("Plugin 'precipitation' is deprecated -- use 'split_sum' instead'");
			pc->Name("split_sum");
		}
		else if (pc->Name() == "kindex")
		{
			aLogger->Warning("Plugin 'kindex' is deprecated -- use 'stability' instead'");
			pc->Name("stability");
		}

		auto aPlugin = dynamic_pointer_cast<plugin::compiled_plugin > (plugin_factory::Instance()->Plugin(pc->Name()));

		if (!aPlugin)
		{
			aLogger->Error("Unable to declare plugin " + pc->Name());
			continue;
		}

		if (pc->StatisticsEnabled())
		{
			pc->StartStatistics();
		}

		aLogger->Info("Calculating " + pc->Name());

		try
		{
			aPlugin->Process(pc);
		}
		catch (const exception& e)
		{
			aLogger->Fatal(string("Caught exception: ") + e.what());
			exit(1);
		}

		if (pc->StatisticsEnabled())
		{
			pc->WriteStatistics();
		}

	}

	return 0;

}

void banner()
{
	cout << endl
			  << "************************************************" << endl
			  << "* By the Power of Grayskull, I Have the Power! *" << endl
			  << "************************************************" << endl << endl;

}

shared_ptr<configuration> ParseCommandLine(int argc, char** argv)
{

	shared_ptr<configuration> conf = make_shared<configuration> ();

	namespace po = boost::program_options;

	po::options_description desc("Allowed options");

	// Can't use required() since it doesn't allow us to use --list-plugins

	string outfileType = "";
	string confFile = "";
	string statisticsLabel = "";
	vector<string> auxFiles;
	short int cudaDeviceId = 0;

	himan::HPDebugState debugState = himan::kDebugMsg;

	int logLevel = 0;
	short int threadCount = -1;

	desc.add_options()
	("help,h", "print out help message")
	("type,t", po::value(&outfileType), "output file type, one of: grib, grib2, netcdf, querydata")
	("version,v", "display version number")
	("configuration-file,f", po::value(&confFile), "configuration file")
	("auxiliary-files,a", po::value<vector<string> > (&auxFiles), "auxiliary (helper) file(s)")
	("threads,j", po::value(&threadCount), "number of started threads")
	("list-plugins,l", "list all defined plugins")
	("debug-level,d", po::value(&logLevel), "set log level: 0(fatal) 1(error) 2(warning) 3(info) 4(debug) 5(trace)")
	("statistics,s", po::value(&statisticsLabel), "record statistics information")
	("no-cuda", "disable all cuda extensions")
	("no-cuda-packing", "disable cuda packing and unpacking of grib data")
	("cuda-device-id", po::value(&cudaDeviceId), "use a specific cuda device (default: 0)")
	("cuda-properties", "print cuda device properties of platform (if any)")
	;

	po::positional_options_description p;
	p.add("auxiliary-files", -1);

	po::variables_map opt;
	po::store(po::command_line_parser(argc, argv)
			  .options(desc)
			  .positional(p)
			  .run(),
			  opt);

	po::notify(opt);

	if (threadCount)
	{
		conf->ThreadCount(threadCount);
	}

	if (auxFiles.size())
	{
		conf->AuxiliaryFiles(auxFiles);
	}

	if (logLevel)
	{
		switch (logLevel)
		{
		case 0:
			debugState = kFatalMsg;
			break;
		case 1:
			debugState = kErrorMsg;
			break;
		case 2:
			debugState = kWarningMsg;
			break;
		case 3:
			debugState = kInfoMsg;
			break;
		case 4:
			debugState = kDebugMsg;
			break;
		case 5:
			debugState = kTraceMsg;
			break;
		}

	}

	logger_factory::Instance()->DebugState(debugState);

	if (opt.count("version"))
	{
		cout << "himan-bin version " << __DATE__ << " " << __TIME__ << endl;
		exit(1);
	}

	shared_ptr<plugin::pcuda> cuda;

#ifndef HAVE_CUDA
	if (opt.count("cuda-properties"))
	{
		cout << "CUDA support turned off at compile time" << endl;
		exit(1);
	}

	conf->UseCuda(false);
	conf->UseCudaForPacking(false);
	conf->CudaDeviceCount(0);

	if (opt.count("cuda-device-id"))
	{
		cerr << "CUDA support turned off at compile time" << endl;
	}

#else
	cuda = dynamic_pointer_cast<plugin::pcuda> (plugin_factory::Instance()->Plugin("pcuda"));

	if (opt.count("cuda-properties"))
	{
		cuda->Capabilities();
		exit(1);
	}

	if (opt.count("no-cuda-packing"))
	{
		conf->UseCudaForPacking(false);
	}

	if (opt.count("no-cuda"))
	{
		conf->UseCuda(false);
		conf->UseCudaForPacking(false);
	}

	conf->CudaDeviceCount(static_cast<short> (cuda->DeviceCount()));

	if (opt.count("cuda-device-id"))
	{
		if (cudaDeviceId >= conf->CudaDeviceCount() || cudaDeviceId < 0)
		{
			cerr << "cuda device id " << cudaDeviceId << " requested, allowed maximum cuda device id is " << conf->CudaDeviceCount()-1 << endl;
			cerr << "cuda mode is disabled" << endl;
			conf->UseCuda(false);
			conf->UseCudaForPacking(false);
		}

		conf->CudaDeviceId(cudaDeviceId);
	}
#endif

	if (!outfileType.empty())
	{
		if (outfileType == "grib")
		{
			conf->OutputFileType(kGRIB1);
		}
		else if (outfileType == "grib2")
		{
			conf->OutputFileType(kGRIB2);
		}
		else if (outfileType == "netcdf")
		{
			conf->OutputFileType(kNetCDF);
		}
		else if (outfileType == "querydata")
		{
			conf->OutputFileType(kQueryData);
		}
		else
		{
			cerr << "Invalid file type: " << outfileType << endl;
			exit(1);
		}
	}

	if (opt.count("help"))
	{
		cout << "usage: himan [ options ]" << endl;
		cout << desc;
		cout << endl << "Examples:" << endl;
		cout << "  himan -f etc/tpot.json" << endl;
		cout << "  himan -f etc/vvmms.json -a file.grib -t querydata" << endl << endl;
		exit(1);
	}

	if (opt.count("list-plugins"))
	{

		vector<shared_ptr<plugin::himan_plugin>> thePlugins = plugin_factory::Instance()->Plugins();

		for (size_t i = 0; i < thePlugins.size(); i++)
		{
			cout << "Plugin '"  << thePlugins[i]->ClassName() << "'" << endl << "\tversion " << thePlugins[i]->Version() << endl;

			switch (thePlugins[i]->PluginClass())
			{

				case kCompiled:
					if (dynamic_pointer_cast<plugin::compiled_plugin> (thePlugins[i])->CudaEnabledCalculation())
					{
						cout << "\tcuda-enabled\n";
					}
					cout << "\ttype compiled (hard-coded) --> " << dynamic_pointer_cast<plugin::compiled_plugin> (thePlugins[i])->Formula() << endl;
					break;

				case kAuxiliary:
					cout << "\ttype aux" << endl;
					break;

				case kInterpreted:
					cout << "\ttype interpreted" << endl;
					break;

				default:
					cout << " has unknown plugin type" << endl;
					exit(1);
			}
		}

		exit(1);
	}

	if (!confFile.empty())
	{
		conf->ConfigurationFile(confFile);
	}
	else
	{
		cerr << "himan: Configuration file not defined" << endl;
		exit(1);
	}

	if (!statisticsLabel.empty())
	{
		conf->StatisticsLabel(statisticsLabel);
	}

	return conf;
}
<|endoftext|>


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

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "Nebula.h"

using namespace std;

/* ------------------------------------------------------------------------- */
/* GLOBAL VARIABLES                                                          */
/* ------------------------------------------------------------------------- */

static const char * usage =
"\n  oned [-h] [-v] [-f]\n\n"
"SYNOPSIS\n"
"  Starts the OpenNebula daemon\n\n"
"OPTIONS\n"
"\t-h\tprints this help.\n"
"\t-v\tprints OpenNebula version and license\n"
"\t-f\tforeground, do not fork the oned daemon\n";

static const char * susage =
"usage: oned [-h] [-v] [-f]\n";

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

static void print_license()
{
    cout<< "Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs\n\n"
        << Nebula::version() << " is distributed and licensed for use under the"
        << " terms of the\nApache License, Version 2.0 "
        << "(http://www.apache.org/licenses/LICENSE-2.0).\n";
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

static void oned_main()
{
    try
    {
        Nebula& nd  = Nebula::instance();
        nd.start();
    }
    catch (exception &e)
    {
        cerr << e.what() << endl;

        return;
    }
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

int main(int argc, char **argv)
{
    char            opt;
    bool            foreground = false;
    const char *    nl;
    int             fd;
    pid_t           pid,sid;
    string          wd;
    int             rc;

    while((opt = getopt(argc,argv,"vhf")) != -1)
        switch(opt)
        {
            case 'v':
                print_license();
                exit(0);
                break;
            case 'h':
                cout << usage;
                exit(0);
                break;
            case 'f':
                foreground = true;
                break;
            default:
                cerr << susage;
                exit(-1);
                break;
        }

    // ---------------------------------
    //   Check if other oned is running
    // ---------------------------------

    string lockfile;
    string var_location;

    nl = getenv("ONE_LOCATION");

    if (nl == 0) // OpenNebula in root of FSH
    {
    	var_location = "/var/lib/one/";
    	lockfile 	 = "/var/lock/one/one";
    }
    else
    {
    	var_location = nl;
    	var_location += "/var/";

    	lockfile = var_location + ".lock";
    }

    fd = open(lockfile.c_str(), O_CREAT|O_EXCL, 0640);

    if( fd == -1)
    {
        cerr<< "Error: Cannot start oned, opening lock file " << lockfile
            << endl;

        exit(-1);
    }

    close(fd);

    // ----------------------------
    //   Fork & exit main process
    // ----------------------------

    if (foreground == true)
    {
        pid = 0; //Do not fork
    }
    else
    {
        pid = fork();
    }

    switch (pid){
        case -1: // Error
            cerr << "Error: Unable to fork.\n";
            exit(-1);

        case 0: // Child process

            rc  = chdir(var_location.c_str());

            if (rc != 0)
            {
                goto error_chdir;
            }

            if (foreground == false)
            {
                sid = setsid();

                if (sid == -1)
                {
                    goto error_sid;
                }
            }

            oned_main();

            unlink(lockfile.c_str());
            break;

        default: // Parent process
            break;
    }

    return 0;

error_chdir:
    cerr << "Error: cannot change to dir " << wd << "\n";
    unlink(lockfile.c_str());
    exit(-1);

error_sid:
    cerr << "Error: creating new session\n";
    unlink(lockfile.c_str());
    exit(-1);
}
<commit_msg>Fix getopt return type. Fix oned startup in armv6l<commit_after>/* -------------------------------------------------------------------------- */
/* Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs        */
/*                                                                            */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
/* not use this file except in compliance with the License. You may obtain    */
/* a copy of the License at                                                   */
/*                                                                            */
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
/*                                                                            */
/* Unless required by applicable law or agreed to in writing, software        */
/* distributed under the License is distributed on an "AS IS" BASIS,          */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/* See the License for the specific language governing permissions and        */
/* limitations under the License.                                             */
/* -------------------------------------------------------------------------- */

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "Nebula.h"

using namespace std;

/* ------------------------------------------------------------------------- */
/* GLOBAL VARIABLES                                                          */
/* ------------------------------------------------------------------------- */

static const char * usage =
"\n  oned [-h] [-v] [-f]\n\n"
"SYNOPSIS\n"
"  Starts the OpenNebula daemon\n\n"
"OPTIONS\n"
"\t-h\tprints this help.\n"
"\t-v\tprints OpenNebula version and license\n"
"\t-f\tforeground, do not fork the oned daemon\n";

static const char * susage =
"usage: oned [-h] [-v] [-f]\n";

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

static void print_license()
{
    cout<< "Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs\n\n"
        << Nebula::version() << " is distributed and licensed for use under the"
        << " terms of the\nApache License, Version 2.0 "
        << "(http://www.apache.org/licenses/LICENSE-2.0).\n";
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

static void oned_main()
{
    try
    {
        Nebula& nd  = Nebula::instance();
        nd.start();
    }
    catch (exception &e)
    {
        cerr << e.what() << endl;

        return;
    }
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

int main(int argc, char **argv)
{
    int             opt;
    bool            foreground = false;
    const char *    nl;
    int             fd;
    pid_t           pid,sid;
    string          wd;
    int             rc;

    while((opt = getopt(argc,argv,"vhf")) != -1)
        switch(opt)
        {
            case 'v':
                print_license();
                exit(0);
                break;
            case 'h':
                cout << usage;
                exit(0);
                break;
            case 'f':
                foreground = true;
                break;
            default:
                cerr << susage;
                exit(-1);
                break;
        }

    // ---------------------------------
    //   Check if other oned is running
    // ---------------------------------

    string lockfile;
    string var_location;

    nl = getenv("ONE_LOCATION");

    if (nl == 0) // OpenNebula in root of FSH
    {
    	var_location = "/var/lib/one/";
    	lockfile 	 = "/var/lock/one/one";
    }
    else
    {
    	var_location = nl;
    	var_location += "/var/";

    	lockfile = var_location + ".lock";
    }

    fd = open(lockfile.c_str(), O_CREAT|O_EXCL, 0640);

    if( fd == -1)
    {
        cerr<< "Error: Cannot start oned, opening lock file " << lockfile
            << endl;

        exit(-1);
    }

    close(fd);

    // ----------------------------
    //   Fork & exit main process
    // ----------------------------

    if (foreground == true)
    {
        pid = 0; //Do not fork
    }
    else
    {
        pid = fork();
    }

    switch (pid){
        case -1: // Error
            cerr << "Error: Unable to fork.\n";
            exit(-1);

        case 0: // Child process

            rc  = chdir(var_location.c_str());

            if (rc != 0)
            {
                goto error_chdir;
            }

            if (foreground == false)
            {
                sid = setsid();

                if (sid == -1)
                {
                    goto error_sid;
                }
            }

            oned_main();

            unlink(lockfile.c_str());
            break;

        default: // Parent process
            break;
    }

    return 0;

error_chdir:
    cerr << "Error: cannot change to dir " << wd << "\n";
    unlink(lockfile.c_str());
    exit(-1);

error_sid:
    cerr << "Error: creating new session\n";
    unlink(lockfile.c_str());
    exit(-1);
}
<|endoftext|>


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

#include "system_call_table.h"

#include <asm/unistd.h>
#include "sandbox_impl.h"

#if defined(__x86_64__)
#ifndef __NR_set_robust_list
#define __NR_set_robust_list 273
#endif
#ifndef __NR_accept4
#define __NR_accept4         288
#endif
#elif defined(__i386__)
#ifndef __NR_set_robust_list
#define __NR_set_robust_list 311
#endif
#else
#error Unsupported target platform
#endif

namespace playground {

struct SyscallTable *SyscallTable::syscallTable;
unsigned SyscallTable::maxSyscall = 0;

void SyscallTable::initializeSyscallTable() {
  if (syscallTable) {
    return;
  }

#define S(x) reinterpret_cast<void (*)()>(&Sandbox::x)
#define P(x) Sandbox::x
  static const struct policy {
    unsigned syscallNum;
    void     (*handler)();
    bool     (*trustedProcess)(const SyscallRequestInfo* info);
  } default_policy[] = {
#if defined(__NR_accept)
    { __NR_accept,          UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_accept4,         UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_access,          S(sandbox_access),          P(process_access)    },
    { __NR_brk,             UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_clock_gettime,   UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_clone,           S(sandbox_clone),           P(process_clone)     },
    { __NR_close,           UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_dup,             UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_dup2,            UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_epoll_create,    UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_epoll_ctl,       UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_epoll_wait,      UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_exit,            S(sandbox_exit),            P(process_exit)      },
    { __NR_exit_group,      UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_fcntl,           UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_fcntl64)
    { __NR_fcntl64,         UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_fdatasync,       UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_fstat,           UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_fstat64)
    { __NR_fstat64,         UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_futex,           UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_getdents,        UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_getdents64,      UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_getegid,         UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_getegid32)
    { __NR_getegid32,       UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_geteuid,         UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_geteuid32)
    { __NR_geteuid32,       UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_getgid,          UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_getgid32)
    { __NR_getgid32,        UNRESTRICTED_SYSCALL,       NULL                 },
#endif
#if defined(__NR_getpeername)
    { __NR_getpeername,     UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_getpid,          S(sandbox_getpid),          NULL                 },
#if defined(__NR_getsockname)
    { __NR_getsockname,     UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_getsockopt,      S(sandbox_getsockopt),      P(process_getsockopt)},
#endif
    { __NR_gettid,          S(sandbox_gettid),          NULL                 },
    { __NR_gettimeofday,    UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_getuid,          UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_getuid32)
    { __NR_getuid32,        UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_ioctl,           S(sandbox_ioctl),           P(process_ioctl)     },
#if defined(__NR_ipc)
    { __NR_ipc,             S(sandbox_ipc),             P(process_ipc)       },
#endif
#if defined(__NR__llseek)
    { __NR__llseek,         UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_lseek,           UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_lstat,           S(sandbox_lstat),           P(process_stat)      },
#if defined(__NR_lstat64)
    { __NR_lstat64,         S(sandbox_lstat64),         P(process_stat)      },
#endif
    { __NR_madvise,         S(sandbox_madvise),         P(process_madvise)   },
    {
#if defined(__NR_mmap2)
    __NR_mmap2,
#else
    __NR_mmap,
#endif
                            S(sandbox_mmap),            P(process_mmap)      },
    { __NR_mprotect,        S(sandbox_mprotect),        P(process_mprotect)  },
    { __NR_munmap,          S(sandbox_munmap),          P(process_munmap)    },
    { __NR_nanosleep,       UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_open,            S(sandbox_open),            P(process_open)      },
    { __NR_pipe,            UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_poll,            UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_prctl,           S(sandbox_prctl),           P(process_prctl)     },
    { __NR_pread64,         UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_preadv,          UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_pwrite64,        UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_pwritev,         UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_recvfrom)
    { __NR_recvfrom,        S(sandbox_recvfrom),        P(process_recvfrom)  },
    { __NR_recvmsg,         S(sandbox_recvmsg),         P(process_recvmsg)   },
#endif
#if defined(__NR_rt_sigaction)
    { __NR_rt_sigaction,    S(sandbox_rt_sigaction),    P(process_sigaction) },
#endif
#if defined(__NR_rt_sigprocmask)
    { __NR_rt_sigprocmask,  S(sandbox_rt_sigprocmask),  NULL                 },
#endif
#if defined(__NR_sendmsg)
    { __NR_sendmsg,         S(sandbox_sendmsg),         P(process_sendmsg)   },
    { __NR_sendto,          S(sandbox_sendto),          P(process_sendto)    },
#endif
    { __NR_set_robust_list, UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_setsockopt)
    { __NR_setsockopt,      S(sandbox_setsockopt),      P(process_setsockopt)},
#endif
#if defined(__NR_shmat)
    { __NR_shmat,           S(sandbox_shmat),           P(process_shmat)     },
    { __NR_shmctl,          S(sandbox_shmctl),          P(process_shmctl)    },
    { __NR_shmdt,           S(sandbox_shmdt),           P(process_shmdt)     },
    { __NR_shmget,          S(sandbox_shmget),          P(process_shmget)    },
#endif
#if defined(__NR_shutdown)
    { __NR_shutdown,        UNRESTRICTED_SYSCALL,       NULL                 },
#endif
#if defined(__NR_sigaction)
    { __NR_sigaction,       S(sandbox_sigaction),       P(process_sigaction) },
#endif
#if defined(__NR_signal)
    { __NR_signal,          S(sandbox_signal),          P(process_sigaction) },
#endif
#if defined(__NR_sigprocmask)
    { __NR_sigprocmask,     S(sandbox_sigprocmask),     NULL                 },
#endif
#if defined(__NR_socketpair)
    { __NR_socketpair,      UNRESTRICTED_SYSCALL,       NULL                 },
#endif
#if defined(__NR_socketcall)
    { __NR_socketcall,      S(sandbox_socketcall),      P(process_socketcall)},
#endif
    { __NR_stat,            S(sandbox_stat),            P(process_stat)      },
#if defined(__NR_stat64)
    { __NR_stat64,          S(sandbox_stat64),          P(process_stat)      },
#endif
    { __NR_time,            UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_uname,           UNRESTRICTED_SYSCALL,       NULL                 },
  };

  // Find the highest used system call number
  for (const struct policy *policy = default_policy;
       policy-default_policy <
         (int)(sizeof(default_policy)/sizeof(struct policy));
       ++policy) {
    if (policy->syscallNum > maxSyscall) {
      maxSyscall = policy->syscallNum;
    }
  }

  syscallTable = reinterpret_cast<SyscallTable *>(
    mmap(NULL, getSyscallTableSize(),
         PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0));
  if (syscallTable == MAP_FAILED) {
    Sandbox::die("Failed to allocate system call table");
  }
  for (const struct policy *policy = default_policy;
       policy-default_policy <
         (int)(sizeof(default_policy)/sizeof(struct policy));
       ++policy) {
    syscallTable[policy->syscallNum].handler        = policy->handler;
    syscallTable[policy->syscallNum].trustedProcess = policy->trustedProcess;
  }
  protectSyscallTable();
}

size_t SyscallTable::getSyscallTableSize() {
  return ((sizeof(struct SyscallTable) * (maxSyscall + 1)) + 4095) & ~4095;
}

void SyscallTable::protectSyscallTable() {
  if (mprotect(syscallTable, getSyscallTableSize(), PROT_READ) != 0) {
    Sandbox::die("Failed to protect system call table");
  }
}

void SyscallTable::unprotectSyscallTable() {
  if (mprotect(syscallTable, getSyscallTableSize(),
               PROT_READ | PROT_WRITE) != 0) {
    Sandbox::die("Failed to unprotect system call table");
  }
}

} //  namespace
<commit_msg>Allow writev() and readv() system calls<commit_after>// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "system_call_table.h"

#include <asm/unistd.h>
#include "sandbox_impl.h"

#if defined(__x86_64__)
#ifndef __NR_set_robust_list
#define __NR_set_robust_list 273
#endif
#ifndef __NR_accept4
#define __NR_accept4         288
#endif
#elif defined(__i386__)
#ifndef __NR_set_robust_list
#define __NR_set_robust_list 311
#endif
#else
#error Unsupported target platform
#endif

namespace playground {

struct SyscallTable *SyscallTable::syscallTable;
unsigned SyscallTable::maxSyscall = 0;

void SyscallTable::initializeSyscallTable() {
  if (syscallTable) {
    return;
  }

#define S(x) reinterpret_cast<void (*)()>(&Sandbox::x)
#define P(x) Sandbox::x
  static const struct policy {
    unsigned syscallNum;
    void     (*handler)();
    bool     (*trustedProcess)(const SyscallRequestInfo* info);
  } default_policy[] = {
#if defined(__NR_accept)
    { __NR_accept,          UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_accept4,         UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_access,          S(sandbox_access),          P(process_access)    },
    { __NR_brk,             UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_clock_gettime,   UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_clone,           S(sandbox_clone),           P(process_clone)     },
    { __NR_close,           UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_dup,             UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_dup2,            UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_epoll_create,    UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_epoll_ctl,       UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_epoll_wait,      UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_exit,            S(sandbox_exit),            P(process_exit)      },
    { __NR_exit_group,      UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_fcntl,           UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_fcntl64)
    { __NR_fcntl64,         UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_fdatasync,       UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_fstat,           UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_fstat64)
    { __NR_fstat64,         UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_futex,           UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_getdents,        UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_getdents64,      UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_getegid,         UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_getegid32)
    { __NR_getegid32,       UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_geteuid,         UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_geteuid32)
    { __NR_geteuid32,       UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_getgid,          UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_getgid32)
    { __NR_getgid32,        UNRESTRICTED_SYSCALL,       NULL                 },
#endif
#if defined(__NR_getpeername)
    { __NR_getpeername,     UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_getpid,          S(sandbox_getpid),          NULL                 },
#if defined(__NR_getsockname)
    { __NR_getsockname,     UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_getsockopt,      S(sandbox_getsockopt),      P(process_getsockopt)},
#endif
    { __NR_gettid,          S(sandbox_gettid),          NULL                 },
    { __NR_gettimeofday,    UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_getuid,          UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_getuid32)
    { __NR_getuid32,        UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_ioctl,           S(sandbox_ioctl),           P(process_ioctl)     },
#if defined(__NR_ipc)
    { __NR_ipc,             S(sandbox_ipc),             P(process_ipc)       },
#endif
#if defined(__NR__llseek)
    { __NR__llseek,         UNRESTRICTED_SYSCALL,       NULL                 },
#endif
    { __NR_lseek,           UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_lstat,           S(sandbox_lstat),           P(process_stat)      },
#if defined(__NR_lstat64)
    { __NR_lstat64,         S(sandbox_lstat64),         P(process_stat)      },
#endif
    { __NR_madvise,         S(sandbox_madvise),         P(process_madvise)   },
    {
#if defined(__NR_mmap2)
    __NR_mmap2,
#else
    __NR_mmap,
#endif
                            S(sandbox_mmap),            P(process_mmap)      },
    { __NR_mprotect,        S(sandbox_mprotect),        P(process_mprotect)  },
    { __NR_munmap,          S(sandbox_munmap),          P(process_munmap)    },
    { __NR_nanosleep,       UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_open,            S(sandbox_open),            P(process_open)      },
    { __NR_pipe,            UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_poll,            UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_prctl,           S(sandbox_prctl),           P(process_prctl)     },
    { __NR_pread64,         UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_preadv,          UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_pwrite64,        UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_pwritev,         UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_readv,           UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_recvfrom)
    { __NR_recvfrom,        S(sandbox_recvfrom),        P(process_recvfrom)  },
    { __NR_recvmsg,         S(sandbox_recvmsg),         P(process_recvmsg)   },
#endif
#if defined(__NR_rt_sigaction)
    { __NR_rt_sigaction,    S(sandbox_rt_sigaction),    P(process_sigaction) },
#endif
#if defined(__NR_rt_sigprocmask)
    { __NR_rt_sigprocmask,  S(sandbox_rt_sigprocmask),  NULL                 },
#endif
#if defined(__NR_sendmsg)
    { __NR_sendmsg,         S(sandbox_sendmsg),         P(process_sendmsg)   },
    { __NR_sendto,          S(sandbox_sendto),          P(process_sendto)    },
#endif
    { __NR_set_robust_list, UNRESTRICTED_SYSCALL,       NULL                 },
#if defined(__NR_setsockopt)
    { __NR_setsockopt,      S(sandbox_setsockopt),      P(process_setsockopt)},
#endif
#if defined(__NR_shmat)
    { __NR_shmat,           S(sandbox_shmat),           P(process_shmat)     },
    { __NR_shmctl,          S(sandbox_shmctl),          P(process_shmctl)    },
    { __NR_shmdt,           S(sandbox_shmdt),           P(process_shmdt)     },
    { __NR_shmget,          S(sandbox_shmget),          P(process_shmget)    },
#endif
#if defined(__NR_shutdown)
    { __NR_shutdown,        UNRESTRICTED_SYSCALL,       NULL                 },
#endif
#if defined(__NR_sigaction)
    { __NR_sigaction,       S(sandbox_sigaction),       P(process_sigaction) },
#endif
#if defined(__NR_signal)
    { __NR_signal,          S(sandbox_signal),          P(process_sigaction) },
#endif
#if defined(__NR_sigprocmask)
    { __NR_sigprocmask,     S(sandbox_sigprocmask),     NULL                 },
#endif
#if defined(__NR_socketpair)
    { __NR_socketpair,      UNRESTRICTED_SYSCALL,       NULL                 },
#endif
#if defined(__NR_socketcall)
    { __NR_socketcall,      S(sandbox_socketcall),      P(process_socketcall)},
#endif
    { __NR_stat,            S(sandbox_stat),            P(process_stat)      },
#if defined(__NR_stat64)
    { __NR_stat64,          S(sandbox_stat64),          P(process_stat)      },
#endif
    { __NR_time,            UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_uname,           UNRESTRICTED_SYSCALL,       NULL                 },
    { __NR_writev,          UNRESTRICTED_SYSCALL,       NULL                 },
  };

  // Find the highest used system call number
  for (const struct policy *policy = default_policy;
       policy-default_policy <
         (int)(sizeof(default_policy)/sizeof(struct policy));
       ++policy) {
    if (policy->syscallNum > maxSyscall) {
      maxSyscall = policy->syscallNum;
    }
  }

  syscallTable = reinterpret_cast<SyscallTable *>(
    mmap(NULL, getSyscallTableSize(),
         PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0));
  if (syscallTable == MAP_FAILED) {
    Sandbox::die("Failed to allocate system call table");
  }
  for (const struct policy *policy = default_policy;
       policy-default_policy <
         (int)(sizeof(default_policy)/sizeof(struct policy));
       ++policy) {
    syscallTable[policy->syscallNum].handler        = policy->handler;
    syscallTable[policy->syscallNum].trustedProcess = policy->trustedProcess;
  }
  protectSyscallTable();
}

size_t SyscallTable::getSyscallTableSize() {
  return ((sizeof(struct SyscallTable) * (maxSyscall + 1)) + 4095) & ~4095;
}

void SyscallTable::protectSyscallTable() {
  if (mprotect(syscallTable, getSyscallTableSize(), PROT_READ) != 0) {
    Sandbox::die("Failed to protect system call table");
  }
}

void SyscallTable::unprotectSyscallTable() {
  if (mprotect(syscallTable, getSyscallTableSize(),
               PROT_READ | PROT_WRITE) != 0) {
    Sandbox::die("Failed to unprotect system call table");
  }
}

} //  namespace
<|endoftext|>


<commit_before>// Hossein Moein
// September 12, 2017
// Copyright (C) 2018-2019 Hossein Moein
// Distributed under the BSD Software License (see file License)

#include "DateTime.h"
#include "DataFrame.h"
#include <cstdlib>
#include <fstream>
#include <functional>

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

namespace hmdf
{

#define gcc_likely(x)    __builtin_expect(!!(x), 1)
#define gcc_unlikely(x)  __builtin_expect(!!(x), 0)

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

inline static void
_get_token_from_file_  (std::ifstream &file, char delim, char *value) {

    char    c;
    int     count = 0;

    while (file.get (c))
        if (c == delim)
            break;
        else
            value[count++] = c;

    value[count] = 0;
}

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

template<typename T, typename V>
inline static void
_col_vector_push_back_(V &vec,
                       std::ifstream &file,
                       T (*converter)(const char *))  {

    char    value[1024];
    char    c = 0;

    while (file.get(c)) {
        if (c == '\n')  break;
        file.unget();
        _get_token_from_file_(file, ',', value);
        vec.push_back(converter(value));
    }
}

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

template<>
inline void
_col_vector_push_back_<const char *, std::vector<std::string>>(
    std::vector<std::string> &vec,
    std::ifstream &file,
    const char * (*converter)(const char *))  {

    char    value[1024];
    char    c = 0;

    while (file.get(c)) {
        if (c == '\n')  break;
        file.unget();
        _get_token_from_file_(file, ',', value);
        vec.push_back(value);
    }
}

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

template<>
inline void
_col_vector_push_back_<DateTime, std::vector<DateTime>>(
    std::vector<DateTime> &vec,
    std::ifstream &file,
    DateTime (*converter)(const char *))  {

    char    value[1024];
    char    c = 0;

    while (file.get(c)) {
        if (c == '\n')  break;
        file.unget();
        _get_token_from_file_(file, ',', value);

        time_t      t;
        int         n;
        DateTime    dt;

#ifdef _WIN32
        ::sscanf(value, "%lld.%d", &t, &n);
#else
        ::sscanf(value, "%ld.%d", &t, &n);
#endif // _WIN32
        dt.set_time(t, n);
        vec.emplace_back(std::move(dt));
    }
}

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

template<typename T>
struct  _IdxParserFunctor_  {

    void operator()(std::vector<T> &, std::ifstream &file)  {  }
};

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

template<>
struct  _IdxParserFunctor_<double>  {

    inline void operator()(std::vector<double> &vec, std::ifstream &file)  {

        _col_vector_push_back_(vec, file, &::atof);
    }
};

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

template<>
struct  _IdxParserFunctor_<int>  {

    inline void operator()(std::vector<int> &vec, std::ifstream &file)  {

        _col_vector_push_back_(vec, file, &::atoi);
    }
};

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

template<>
struct  _IdxParserFunctor_<long>  {

    inline void operator()(std::vector<long> &vec, std::ifstream &file)  {

        _col_vector_push_back_(vec, file, &::atol);
    }
};

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

template<>
struct  _IdxParserFunctor_<long long>  {

    inline void operator()(std::vector<long long> &vec, std::ifstream &file) {

        _col_vector_push_back_(vec, file, &::atol);
    }
};

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

template<>
struct  _IdxParserFunctor_<unsigned long>  {

    inline void
    operator()(std::vector<unsigned long> &vec, std::ifstream &file)  {

        _col_vector_push_back_(vec, file, &::atoll);
    }
};

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

template<>
struct  _IdxParserFunctor_<unsigned long long>  {

    inline void
    operator()(std::vector<unsigned long long> &vec, std::ifstream &file)  {

        _col_vector_push_back_(vec, file, &::atoll);
    }
};

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

template<>
struct  _IdxParserFunctor_<std::string>  {

    inline void
    operator()(std::vector<std::string> &vec, std::ifstream &file)  {

        auto    converter =
            [](const char *s)-> const char * { return s; };

        _col_vector_push_back_<const char *, std::vector<std::string>>
            (vec, file, converter);
    }
};

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

template<>
struct  _IdxParserFunctor_<DateTime>  {

    inline void
    operator()(std::vector<DateTime> &vec, std::ifstream &file) {

        auto    converter =
            [](const char *)-> DateTime  { return DateTime(); };

        _col_vector_push_back_<DateTime, std::vector<DateTime>>
            (vec, file, converter);
    }
};

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

template<>
struct  _IdxParserFunctor_<bool>  {

    inline void operator()(std::vector<bool> &vec, std::ifstream &file)  {

        _col_vector_push_back_(vec, file, &::atoi);
    }
};

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

template<typename TS, typename  HETERO>
bool DataFrame<TS, HETERO>::read (const char *file_name, io_format iof)  {

    static_assert(std::is_base_of<HeteroVector, HETERO>::value,
                  "Only a StdDataFrame can call read()");

    std::ifstream   file;

    file.open(file_name, std::ios::in);  // Open for reading

    char    col_name[256];
    char    value[32];
    char    type_str[64];
    char    c;

    while (file.get(c)) {
        if (c == '#' || c == '\n' || c == '\0') {
            if (c == '#')
                while (file.get(c))
                    if (c == '\n') break;

            continue;
        }
        file.unget();

        _get_token_from_file_(file, ':', col_name);
        _get_token_from_file_(file, ':', value); // Get the size
        file.get(c);
        if (c != '<')
            throw DataFrameError("DataFrame::read(): ERROR: Expected "
                                 "'<' char to specify column type");
        _get_token_from_file_(file, '>', type_str);
        file.get(c);
        if (c != ':')
            throw DataFrameError("DataFrame::read(): ERROR: Expected "
                                 "':' char to start column values");

        if (! ::strcmp(col_name, "INDEX"))  {
            TSVec   vec;

            vec.reserve(::atoi(value));
            _IdxParserFunctor_<typename TSVec::value_type>()(vec, file);
            load_index(std::forward<TSVec &&>(vec));
        }
        else  {
            if (! ::strcmp(type_str, "double"))  {
                std::vector<double> &vec = create_column<double>(col_name);

                vec.reserve(::atoi(value));
                _col_vector_push_back_(vec, file, ::atof);
            }
            else if (! ::strcmp(type_str, "int"))  {
                std::vector<int> &vec = create_column<int>(col_name);

                _col_vector_push_back_(vec, file, &::atoi);
            }
            else if (! ::strcmp(type_str, "uint"))  {
                std::vector<unsigned int>   &vec =
                    create_column<unsigned int>(col_name);

                vec.reserve(::atoi(value));
                _col_vector_push_back_(vec, file, &::atol);
            }
            else if (! ::strcmp(type_str, "long"))  {
                std::vector<long>   &vec = create_column<long>(col_name);

                vec.reserve(::atoi(value));
                _col_vector_push_back_(vec, file, &::atol);
            }
            else if (! ::strcmp(type_str, "ulong"))  {
                std::vector<unsigned long>  &vec =
                    create_column<unsigned long>(col_name);

                vec.reserve(::atoi(value));
                _col_vector_push_back_(vec, file, &::atoll);
            }
            else if (! ::strcmp(type_str, "string"))  {
                std::vector<std::string>    &vec =
                    create_column<std::string>(col_name);
                auto                        converter =
                    [](const char *s)-> const char * { return s; };

                vec.reserve(::atoi(value));
                _col_vector_push_back_<const char *, std::vector<std::string>>
                    (vec, file, converter);
            }
            else if (! ::strcmp(type_str, "DateTime"))  {
                std::vector<DateTime>   &vec =
                    create_column<DateTime>(col_name);
                auto                    converter =
                    [](const char *)-> DateTime { return DateTime(); };

                vec.reserve(::atoi(value));
                _col_vector_push_back_<DateTime, std::vector<DateTime>>
                    (vec, file, converter);
            }
            else if (! ::strcmp(type_str, "bool"))  {
                std::vector<bool>   &vec = create_column<bool>(col_name);

                vec.reserve(::atoi(value));
                _col_vector_push_back_(vec, file, &::atoi);
            }
            else
                throw DataFrameError ("DataFrame::read(): ERROR: Unknown "
                                      "column type");
        }
    }

    file.close();
    return(true);
}

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

template<typename TS, typename  HETERO>
std::future<bool> DataFrame<TS, HETERO>::
read_async(const char *file_name, io_format iof) {

    return (std::async(std::launch::async,
                       &DataFrame::read,
                       this,
                       file_name,
                       iof));
}

} // namespace hmdf

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

// Local Variables:
// mode:C++
// tab-width:4
// c-basic-offset:4
// End:
<commit_msg>Fixed a MSVC warning<commit_after>// Hossein Moein
// September 12, 2017
// Copyright (C) 2018-2019 Hossein Moein
// Distributed under the BSD Software License (see file License)

#include "DateTime.h"
#include "DataFrame.h"
#include <cstdlib>
#include <fstream>
#include <functional>

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

namespace hmdf
{

#define gcc_likely(x)    __builtin_expect(!!(x), 1)
#define gcc_unlikely(x)  __builtin_expect(!!(x), 0)

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

inline static void
_get_token_from_file_  (std::ifstream &file, char delim, char *value) {

    char    c;
    int     count = 0;

    while (file.get (c))
        if (c == delim)
            break;
        else
            value[count++] = c;

    value[count] = 0;
}

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

template<typename T, typename V>
inline static void
_col_vector_push_back_(V &vec,
                       std::ifstream &file,
                       T (*converter)(const char *))  {

    char    value[1024];
    char    c = 0;

    while (file.get(c)) {
        if (c == '\n')  break;
        file.unget();
        _get_token_from_file_(file, ',', value);
        vec.push_back(converter(value));
    }
}

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

template<>
inline void
_col_vector_push_back_<const char *, std::vector<std::string>>(
    std::vector<std::string> &vec,
    std::ifstream &file,
    const char * (*converter)(const char *))  {

    char    value[1024];
    char    c = 0;

    while (file.get(c)) {
        if (c == '\n')  break;
        file.unget();
        _get_token_from_file_(file, ',', value);
        vec.push_back(value);
    }
}

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

template<>
inline void
_col_vector_push_back_<DateTime, std::vector<DateTime>>(
    std::vector<DateTime> &vec,
    std::ifstream &file,
    DateTime (*converter)(const char *))  {

    char    value[1024];
    char    c = 0;

    while (file.get(c)) {
        if (c == '\n')  break;
        file.unget();
        _get_token_from_file_(file, ',', value);

        time_t      t;
        int         n;
        DateTime    dt;

#ifdef _WIN32
        ::sscanf(value, "%lld.%d", &t, &n);
#else
        ::sscanf(value, "%ld.%d", &t, &n);
#endif // _WIN32
        dt.set_time(t, n);
        vec.emplace_back(std::move(dt));
    }
}

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

template<typename T>
struct  _IdxParserFunctor_  {

    void operator()(std::vector<T> &, std::ifstream &file)  {  }
};

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

template<>
struct  _IdxParserFunctor_<double>  {

    inline void operator()(std::vector<double> &vec, std::ifstream &file)  {

        _col_vector_push_back_(vec, file, &::atof);
    }
};

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

template<>
struct  _IdxParserFunctor_<int>  {

    inline void operator()(std::vector<int> &vec, std::ifstream &file)  {

        _col_vector_push_back_(vec, file, &::atoi);
    }
};

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

template<>
struct  _IdxParserFunctor_<long>  {

    inline void operator()(std::vector<long> &vec, std::ifstream &file)  {

        _col_vector_push_back_(vec, file, &::atol);
    }
};

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

template<>
struct  _IdxParserFunctor_<long long>  {

    inline void operator()(std::vector<long long> &vec, std::ifstream &file) {

        _col_vector_push_back_(vec, file, &::atoll);
    }
};

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

template<>
struct  _IdxParserFunctor_<unsigned long>  {

    inline void
    operator()(std::vector<unsigned long> &vec, std::ifstream &file)  {

        _col_vector_push_back_(vec, file, &::atol);
    }
};

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

template<>
struct  _IdxParserFunctor_<unsigned long long>  {

    inline void
    operator()(std::vector<unsigned long long> &vec, std::ifstream &file)  {

        _col_vector_push_back_(vec, file, &::atoll);
    }
};

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

template<>
struct  _IdxParserFunctor_<std::string>  {

    inline void
    operator()(std::vector<std::string> &vec, std::ifstream &file)  {

        auto    converter =
            [](const char *s)-> const char * { return s; };

        _col_vector_push_back_<const char *, std::vector<std::string>>
            (vec, file, converter);
    }
};

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

template<>
struct  _IdxParserFunctor_<DateTime>  {

    inline void
    operator()(std::vector<DateTime> &vec, std::ifstream &file) {

        auto    converter =
            [](const char *)-> DateTime  { return DateTime(); };

        _col_vector_push_back_<DateTime, std::vector<DateTime>>
            (vec, file, converter);
    }
};

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

template<>
struct  _IdxParserFunctor_<bool>  {

    inline void operator()(std::vector<bool> &vec, std::ifstream &file)  {

        _col_vector_push_back_(vec, file, &::atoi);
    }
};

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

template<typename TS, typename  HETERO>
bool DataFrame<TS, HETERO>::read (const char *file_name, io_format iof)  {

    static_assert(std::is_base_of<HeteroVector, HETERO>::value,
                  "Only a StdDataFrame can call read()");

    std::ifstream   file;

    file.open(file_name, std::ios::in);  // Open for reading

    char    col_name[256];
    char    value[32];
    char    type_str[64];
    char    c;

    while (file.get(c)) {
        if (c == '#' || c == '\n' || c == '\0') {
            if (c == '#')
                while (file.get(c))
                    if (c == '\n') break;

            continue;
        }
        file.unget();

        _get_token_from_file_(file, ':', col_name);
        _get_token_from_file_(file, ':', value); // Get the size
        file.get(c);
        if (c != '<')
            throw DataFrameError("DataFrame::read(): ERROR: Expected "
                                 "'<' char to specify column type");
        _get_token_from_file_(file, '>', type_str);
        file.get(c);
        if (c != ':')
            throw DataFrameError("DataFrame::read(): ERROR: Expected "
                                 "':' char to start column values");

        if (! ::strcmp(col_name, "INDEX"))  {
            TSVec   vec;

            vec.reserve(::atoi(value));
            _IdxParserFunctor_<typename TSVec::value_type>()(vec, file);
            load_index(std::forward<TSVec &&>(vec));
        }
        else  {
            if (! ::strcmp(type_str, "double"))  {
                std::vector<double> &vec = create_column<double>(col_name);

                vec.reserve(::atoi(value));
                _col_vector_push_back_(vec, file, ::atof);
            }
            else if (! ::strcmp(type_str, "int"))  {
                std::vector<int> &vec = create_column<int>(col_name);

                _col_vector_push_back_(vec, file, &::atoi);
            }
            else if (! ::strcmp(type_str, "uint"))  {
                std::vector<unsigned int>   &vec =
                    create_column<unsigned int>(col_name);

                vec.reserve(::atoi(value));
                _col_vector_push_back_(vec, file, &::atol);
            }
            else if (! ::strcmp(type_str, "long"))  {
                std::vector<long>   &vec = create_column<long>(col_name);

                vec.reserve(::atoi(value));
                _col_vector_push_back_(vec, file, &::atol);
            }
            else if (! ::strcmp(type_str, "ulong"))  {
                std::vector<unsigned long>  &vec =
                    create_column<unsigned long>(col_name);

                vec.reserve(::atoi(value));
                _col_vector_push_back_(vec, file, &::atoll);
            }
            else if (! ::strcmp(type_str, "string"))  {
                std::vector<std::string>    &vec =
                    create_column<std::string>(col_name);
                auto                        converter =
                    [](const char *s)-> const char * { return s; };

                vec.reserve(::atoi(value));
                _col_vector_push_back_<const char *, std::vector<std::string>>
                    (vec, file, converter);
            }
            else if (! ::strcmp(type_str, "DateTime"))  {
                std::vector<DateTime>   &vec =
                    create_column<DateTime>(col_name);
                auto                    converter =
                    [](const char *)-> DateTime { return DateTime(); };

                vec.reserve(::atoi(value));
                _col_vector_push_back_<DateTime, std::vector<DateTime>>
                    (vec, file, converter);
            }
            else if (! ::strcmp(type_str, "bool"))  {
                std::vector<bool>   &vec = create_column<bool>(col_name);

                vec.reserve(::atoi(value));
                _col_vector_push_back_(vec, file, &::atoi);
            }
            else
                throw DataFrameError ("DataFrame::read(): ERROR: Unknown "
                                      "column type");
        }
    }

    file.close();
    return(true);
}

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

template<typename TS, typename  HETERO>
std::future<bool> DataFrame<TS, HETERO>::
read_async(const char *file_name, io_format iof) {

    return (std::async(std::launch::async,
                       &DataFrame::read,
                       this,
                       file_name,
                       iof));
}

} // namespace hmdf

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

// Local Variables:
// mode:C++
// tab-width:4
// c-basic-offset:4
// End:
<|endoftext|>


<commit_before>#pragma once

#include <functional>
#include <memory>
#include <vector>

#include "blackhole/logger.hpp"

namespace blackhole {
inline namespace v1 {

class handler_t;
class record_t;
class scoped_t;

}  // namespace v1
}  // namespace blackhole

namespace blackhole {
inline namespace v1 {

class root_logger_t : public logger_t {
public:
    typedef std::function<auto(const record_t&) -> bool> filter_t;

private:
    struct sync_t;
    std::unique_ptr<sync_t> sync;

    struct inner_t;
    std::shared_ptr<inner_t> inner;

public:
    /// Constructs a root level logger with the given handlers.
    ///
    /// \note you can create a logger with no handlers, it'll just drop all messages.
    root_logger_t(std::vector<std::unique_ptr<handler_t>> handlers);

    /// Constructs a root level logger with the given filtering function and handlers.
    ///
    /// \overload
    /// \note you can create a logger with no handlers, it'll just drop all messages.
    root_logger_t(filter_t filter, std::vector<std::unique_ptr<handler_t>> handlers);

    root_logger_t(const root_logger_t& other) = delete;

    /// Constructs a root level logger by consuming another existing logger.
    root_logger_t(root_logger_t&& other) noexcept;

    ~root_logger_t();

    auto operator=(const root_logger_t& other) -> root_logger_t& = delete;
    auto operator=(root_logger_t&& other) noexcept -> root_logger_t&;

    /// Replaces the current logger filter function with the given one.
    ///
    /// Any logging event for which the filter function returns `false` is rejected.
    ///
    /// \warning the function must be thread-safe.
    auto filter(filter_t fn) -> void;

    auto log(severity_t severity, const message_t& message) -> void;
    auto log(severity_t severity, const message_t& message, attribute_pack& pack) -> void;
    auto log(severity_t severity, const lazy_message_t& message, attribute_pack& pack) -> void;

    auto manager() -> scope::manager_t&;

private:
    template<typename F>
    auto consume(severity_t severity, const string_view& pattern, attribute_pack& pack, const F& fn) -> void;
};

}  // namespace v1
}  // namespace blackhole
<commit_msg>refactor(root): dead code elimination<commit_after>#pragma once

#include <functional>
#include <memory>
#include <vector>

#include "blackhole/logger.hpp"

namespace blackhole {
inline namespace v1 {

class handler_t;
class record_t;

}  // namespace v1
}  // namespace blackhole

namespace blackhole {
inline namespace v1 {

class root_logger_t : public logger_t {
public:
    typedef std::function<auto(const record_t&) -> bool> filter_t;

private:
    struct sync_t;
    std::unique_ptr<sync_t> sync;

    struct inner_t;
    std::shared_ptr<inner_t> inner;

public:
    /// Constructs a root level logger with the given handlers.
    ///
    /// \note you can create a logger with no handlers, it'll just drop all messages.
    root_logger_t(std::vector<std::unique_ptr<handler_t>> handlers);

    /// Constructs a root level logger with the given filtering function and handlers.
    ///
    /// \overload
    /// \note you can create a logger with no handlers, it'll just drop all messages.
    root_logger_t(filter_t filter, std::vector<std::unique_ptr<handler_t>> handlers);

    root_logger_t(const root_logger_t& other) = delete;

    /// Constructs a root level logger by consuming another existing logger.
    root_logger_t(root_logger_t&& other) noexcept;

    ~root_logger_t();

    auto operator=(const root_logger_t& other) -> root_logger_t& = delete;
    auto operator=(root_logger_t&& other) noexcept -> root_logger_t&;

    /// Replaces the current logger filter function with the given one.
    ///
    /// Any logging event for which the filter function returns `false` is rejected.
    ///
    /// \warning the function must be thread-safe.
    auto filter(filter_t fn) -> void;

    auto log(severity_t severity, const message_t& message) -> void;
    auto log(severity_t severity, const message_t& message, attribute_pack& pack) -> void;
    auto log(severity_t severity, const lazy_message_t& message, attribute_pack& pack) -> void;

    auto manager() -> scope::manager_t&;

private:
    template<typename F>
    auto consume(severity_t severity, const string_view& pattern, attribute_pack& pack, const F& fn) -> void;
};

}  // namespace v1
}  // namespace blackhole
<|endoftext|>