<commit_before>/***************************************************************************
                          cryptographyplugin.cpp  -  description
                             -------------------
    begin                : jeu nov 14 2002
    copyright            : (C) 2002 by Olivier Goffart
    email                : ogoffart@tiscalinet.be
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <qstylesheet.h>

#include <kdebug.h>
#include <kaction.h>
#include <klocale.h>
#include <kgenericfactory.h>

#include "kopete.h"
#include "kopetemessage.h"
#include "kopetemetacontact.h"
#include "kopetemessagemanager.h"

#include "cryptographyplugin.h"
#include "cryptographypreferences.h"
#include "cryptographyselectuserkey.h"

#include "kgpginterface.h"

K_EXPORT_COMPONENT_FACTORY( kopete_cryptography, KGenericFactory<CryptographyPlugin> );

CryptographyPlugin::CryptographyPlugin( QObject *parent, const char *name,
	const QStringList &/*args*/ )
: KopetePlugin( parent, name ) ,
		m_cachedPass()
{
	if( !pluginStatic_ )
		pluginStatic_=this;

	//TODO: found a pixmap
	m_prefs = new CryptographyPreferences ( "kgpg", this );

	connect( kopeteapp, SIGNAL(aboutToDisplay(KopeteMessage&)),
		 SLOT(slotIncomingMessage(KopeteMessage&)) );
	connect( kopeteapp, SIGNAL(aboutToSend(KopeteMessage&)),
		 SLOT(slotOutgoingMessage(KopeteMessage&)) );

	m_collection=0l;
	m_currentMetaContact=0L;

}

CryptographyPlugin::~CryptographyPlugin()
{
	pluginStatic_ = 0L;
	delete m_collection;
}

CryptographyPlugin* CryptographyPlugin::plugin()
{
	return pluginStatic_ ;
}

CryptographyPlugin* CryptographyPlugin::pluginStatic_ = 0L;

QCString CryptographyPlugin::cachedPass()
{
	return pluginStatic_->m_cachedPass;
}

void CryptographyPlugin::setCachedPass(const QCString& p)
{
	pluginStatic_->m_cachedPass=p;
}

bool CryptographyPlugin::serialize( KopeteMetaContact *metaContact,
			  QStringList &strList  ) const
{
	if ( !m_keyMap.contains( metaContact ) )
		return false;

	strList<<  m_keyMap[ metaContact ] ;
	return true;
}

void CryptographyPlugin::deserialize( KopeteMetaContact *metaContact, const QStringList& data )
{
	m_keyMap[ metaContact ] = data.first();
}

KActionCollection *CryptographyPlugin::customContextMenuActions(KopeteMetaContact *m)
{
	delete m_collection;

	m_collection = new KActionCollection(this);

	KAction *action=new KAction( i18n("&Select Cryptography public key"), "kgpg", 0, this, SLOT (slotSelectContactKey()), m_collection);

	m_collection->insert(action);
	m_currentMetaContact=m;
	return m_collection;
}

/*KActionCollection *CryptographyPlugin::customChatActions(KopeteMessageManager *KMM)
{
	delete m_actionCollection;

	m_actionCollection = new KActionCollection(this);
	KAction *actionTranslate = new KAction( i18n ("Translate"), 0,
		this, SLOT( slotTranslateChat() ), m_actionCollection, "actionTranslate" );
	m_actionCollection->insert( actionTranslate );

	m_currentMessageManager=KMM;
	return m_actionCollection;
}*/

void CryptographyPlugin::slotIncomingMessage( KopeteMessage& msg )
{
	QString body=msg.plainBody();
	if(!body.startsWith("-----BEGIN PGP MESSAGE----"))
		return;

	if(msg.direction() != KopeteMessage::Inbound)
	{
		kdDebug() << "CryptographyPlugin::slotIncommingMessage: inbound messages" <<endl;
		if(m_cachedMessages.contains(body))
		{
			msg.setBody("

"+i18n("Outgoing Encrypted Message")+"

"+QStyleSheet::escape(m_cachedMessages[body])+"

"
				,KopeteMessage::RichText);
			m_cachedMessages.remove(body);
		}
		//if there are too messages in cache, clear the cache
		if(m_cachedMessages.count()>10)
			m_cachedMessages.clear();
		return;
	}

	body=KgpgInterface::KgpgDecryptText(body, m_prefs->privateKey());

	if(!body.isEmpty())
	{
		body="

"+i18n("Incomming Encrypted Message")+"

"+QStyleSheet::escape(body)+"

";
		msg.setBody(body,KopeteMessage::RichText);
	}

}

void CryptographyPlugin::slotOutgoingMessage( KopeteMessage& msg )
{
	if(msg.direction() != KopeteMessage::Outbound)
		return;

	QString key;

	QPtrList<KopeteContact> contactlist = msg.to();
	for (KopeteContact *c=contactlist.first(); c; c = contactlist.next())
	{
		if(!m_keyMap.contains(c->metaContact()))
		{
			kdDebug() << "CryptographyPlugin::slotOutgoingMessage: no key selected for one contact" <<endl;
			return;
		}
		if(!key.isNull())
			key+=" ";
		key+=m_keyMap[c->metaContact()];
	}

	if(key.isEmpty())
	{
		kdDebug() << "CryptographyPlugin::slotOutgoingMessage: empty key" <<endl;
		return;
	}

   QString original=msg.plainBody();

	/* Code From KGPG */

  //////////////////              encode from editor
  QString encryptOptions="";

  //if (utrust==true)
		encryptOptions+=" --always-trust ";
  //if (arm==true)
		encryptOptions+=" --armor ";

 /* if (pubcryptography==true)
   {
      if (gpgversion<120) encryptOptions+=" --compress-algo 1 --cipher-algo cast5 ";
      else encryptOptions+=" --cryptography6 ";
    }*/

// if (selec==NULL) {KMessageBox::sorry(0,i18n("You have not choosen an encryption key..."));return;}

	QString resultat=KgpgInterface::KgpgEncryptText(original,key,encryptOptions);
	if (resultat!="")
	{
		msg.setBody(resultat,KopeteMessage::PlainText);
		m_cachedMessages.insert(resultat,original);
	}
	else
		kdDebug() << "CryptographyPlugin::slotOutgoingMessage: empty result" <<endl;

}

void CryptographyPlugin::slotSelectContactKey()
{
	QString key;
	if(m_keyMap.contains(m_currentMetaContact))
		key=m_keyMap[m_currentMetaContact];
	CryptographySelectUserKey *opts=new CryptographySelectUserKey(key,m_currentMetaContact);
	opts->exec();
	if (opts->result()==true)
	{
		key=opts->publicKey();
		if(key.isEmpty())
			m_keyMap.remove(m_currentMetaContact);
		else
			m_keyMap[m_currentMetaContact]=key;
	}
	delete opts;
}

#include "cryptographyplugin.moc"

<commit_msg>english can be hard ;)<commit_after>/***************************************************************************
                          cryptographyplugin.cpp  -  description
                             -------------------
    begin                : jeu nov 14 2002
    copyright            : (C) 2002 by Olivier Goffart
    email                : ogoffart@tiscalinet.be
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <qstylesheet.h>

#include <kdebug.h>
#include <kaction.h>
#include <klocale.h>
#include <kgenericfactory.h>

#include "kopete.h"
#include "kopetemessage.h"
#include "kopetemetacontact.h"
#include "kopetemessagemanager.h"

#include "cryptographyplugin.h"
#include "cryptographypreferences.h"
#include "cryptographyselectuserkey.h"

#include "kgpginterface.h"

K_EXPORT_COMPONENT_FACTORY( kopete_cryptography, KGenericFactory<CryptographyPlugin> );

CryptographyPlugin::CryptographyPlugin( QObject *parent, const char *name,
	const QStringList &/*args*/ )
: KopetePlugin( parent, name ) ,
		m_cachedPass()
{
	if( !pluginStatic_ )
		pluginStatic_=this;

	//TODO: found a pixmap
	m_prefs = new CryptographyPreferences ( "kgpg", this );

	connect( kopeteapp, SIGNAL(aboutToDisplay(KopeteMessage&)),
		 SLOT(slotIncomingMessage(KopeteMessage&)) );
	connect( kopeteapp, SIGNAL(aboutToSend(KopeteMessage&)),
		 SLOT(slotOutgoingMessage(KopeteMessage&)) );

	m_collection=0l;
	m_currentMetaContact=0L;

}

CryptographyPlugin::~CryptographyPlugin()
{
	pluginStatic_ = 0L;
	delete m_collection;
}

CryptographyPlugin* CryptographyPlugin::plugin()
{
	return pluginStatic_ ;
}

CryptographyPlugin* CryptographyPlugin::pluginStatic_ = 0L;

QCString CryptographyPlugin::cachedPass()
{
	return pluginStatic_->m_cachedPass;
}

void CryptographyPlugin::setCachedPass(const QCString& p)
{
	pluginStatic_->m_cachedPass=p;
}

bool CryptographyPlugin::serialize( KopeteMetaContact *metaContact,
			  QStringList &strList  ) const
{
	if ( !m_keyMap.contains( metaContact ) )
		return false;

	strList<<  m_keyMap[ metaContact ] ;
	return true;
}

void CryptographyPlugin::deserialize( KopeteMetaContact *metaContact, const QStringList& data )
{
	m_keyMap[ metaContact ] = data.first();
}

KActionCollection *CryptographyPlugin::customContextMenuActions(KopeteMetaContact *m)
{
	delete m_collection;

	m_collection = new KActionCollection(this);

	KAction *action=new KAction( i18n("&Select Cryptography public key"), "kgpg", 0, this, SLOT (slotSelectContactKey()), m_collection);

	m_collection->insert(action);
	m_currentMetaContact=m;
	return m_collection;
}

/*KActionCollection *CryptographyPlugin::customChatActions(KopeteMessageManager *KMM)
{
	delete m_actionCollection;

	m_actionCollection = new KActionCollection(this);
	KAction *actionTranslate = new KAction( i18n ("Translate"), 0,
		this, SLOT( slotTranslateChat() ), m_actionCollection, "actionTranslate" );
	m_actionCollection->insert( actionTranslate );

	m_currentMessageManager=KMM;
	return m_actionCollection;
}*/

void CryptographyPlugin::slotIncomingMessage( KopeteMessage& msg )
{
	QString body=msg.plainBody();
	if(!body.startsWith("-----BEGIN PGP MESSAGE----"))
		return;

	if(msg.direction() != KopeteMessage::Inbound)
	{
		kdDebug() << "CryptographyPlugin::slotIncomingMessage: inbound messages" <<endl;
		if(m_cachedMessages.contains(body))
		{
			msg.setBody("

"+i18n("Outgoing Encrypted Message")+"

"+QStyleSheet::escape(m_cachedMessages[body])+"

"
				,KopeteMessage::RichText);
			m_cachedMessages.remove(body);
		}
		//if there are too messages in cache, clear the cache
		if(m_cachedMessages.count()>10)
			m_cachedMessages.clear();
		return;
	}

	body=KgpgInterface::KgpgDecryptText(body, m_prefs->privateKey());

	if(!body.isEmpty())
	{
		body="

"+i18n("Incoming Encrypted Message")+"

"+QStyleSheet::escape(body)+"

";
		msg.setBody(body,KopeteMessage::RichText);
	}

}

void CryptographyPlugin::slotOutgoingMessage( KopeteMessage& msg )
{
	if(msg.direction() != KopeteMessage::Outbound)
		return;

	QString key;

	QPtrList<KopeteContact> contactlist = msg.to();
	for (KopeteContact *c=contactlist.first(); c; c = contactlist.next())
	{
		if(!m_keyMap.contains(c->metaContact()))
		{
			kdDebug() << "CryptographyPlugin::slotOutgoingMessage: no key selected for one contact" <<endl;
			return;
		}
		if(!key.isNull())
			key+=" ";
		key+=m_keyMap[c->metaContact()];
	}

	if(key.isEmpty())
	{
		kdDebug() << "CryptographyPlugin::slotOutgoingMessage: empty key" <<endl;
		return;
	}

   QString original=msg.plainBody();

	/* Code From KGPG */

  //////////////////              encode from editor
  QString encryptOptions="";

  //if (utrust==true)
		encryptOptions+=" --always-trust ";
  //if (arm==true)
		encryptOptions+=" --armor ";

 /* if (pubcryptography==true)
   {
      if (gpgversion<120) encryptOptions+=" --compress-algo 1 --cipher-algo cast5 ";
      else encryptOptions+=" --cryptography6 ";
    }*/

// if (selec==NULL) {KMessageBox::sorry(0,i18n("You have not choosen an encryption key..."));return;}

	QString resultat=KgpgInterface::KgpgEncryptText(original,key,encryptOptions);
	if (resultat!="")
	{
		msg.setBody(resultat,KopeteMessage::PlainText);
		m_cachedMessages.insert(resultat,original);
	}
	else
		kdDebug() << "CryptographyPlugin::slotOutgoingMessage: empty result" <<endl;

}

void CryptographyPlugin::slotSelectContactKey()
{
	QString key;
	if(m_keyMap.contains(m_currentMetaContact))
		key=m_keyMap[m_currentMetaContact];
	CryptographySelectUserKey *opts=new CryptographySelectUserKey(key,m_currentMetaContact);
	opts->exec();
	if (opts->result()==true)
	{
		key=opts->publicKey();
		if(key.isEmpty())
			m_keyMap.remove(m_currentMetaContact);
		else
			m_keyMap[m_currentMetaContact]=key;
	}
	delete opts;
}

#include "cryptographyplugin.moc"

<|endoftext|>


<commit_before>#include <eosio/trace_api/store_provider.hpp>

#include <fc/variant_object.hpp>
#include <fc/crypto/base64.hpp>

namespace {
      static constexpr uint32_t _current_version = 1;
      static constexpr const char* _trace_prefix = "trace_";
      static constexpr const char* _trace_index_prefix = "trace_index_";
      static constexpr const char* _trace_ext = ".log";
      static constexpr uint _max_filename_size = std::char_traits<char>::length(_trace_index_prefix) + 10 + 1 + 10 + std::char_traits<char>::length(_trace_ext) + 1; // "trace_index_" + 10-digits + '-' + 10-digits + ".log" + null-char
}

namespace eosio::trace_api {
   namespace bfs = boost::filesystem;
   store_provider::store_provider(const bfs::path& slice_dir, uint32_t width)
   : _slice_directory(slice_dir, width) {
   }

   void store_provider::append(const block_trace_v0& bt) {
      fc::cfile trace;
      fc::cfile index;
      find_or_create_slice_pair(bt.number, true, trace, index);
      // storing as static_variant to allow adding other data types to the trace file in the future
      const uint64_t offset = append_store(data_log_entry { bt }, trace);

      auto be = metadata_log_entry { block_entry_v0 { .id = bt.id, .number = bt.number, .offset = offset }};
      append_store(be, index);
   }

   void store_provider::append_lib(uint32_t lib) {
      fc::cfile index;
      const uint32_t slice_number = _slice_directory.slice_number(lib);
      _slice_directory.find_or_create_index_slice(slice_number, true, index);
      auto le = metadata_log_entry { lib_entry_v0 { .lib = lib }};
      append_store(le, index);
   }

   get_block_t store_provider::get_block(uint32_t block_height, const yield_function& yield) {
      std::optional<uint64_t> trace_offset;
      bool irreversible = false;
      uint64_t offset = scan_metadata_log_from(block_height, 0, [&block_height, &trace_offset, &irreversible](const metadata_log_entry& e) -> bool {
         if (e.contains<block_entry_v0>()) {
            const auto& block = e.get<block_entry_v0>();
            if (block.number == block_height) {
               trace_offset = block.offset;
            }
         } else if (e.contains<lib_entry_v0>()) {
            auto lib = e.get<lib_entry_v0>().lib;
            if (lib >= block_height) {
               irreversible = true;
               return false;
            }
         }
         return true;
      }, yield);
      if (!trace_offset) {
         return get_block_t{};
      }
      std::optional<data_log_entry> entry = read_data_log(block_height, *trace_offset);
      if (!entry) {
         return get_block_t{};
      }
      const auto bt = entry->get<block_trace_v0>();
      return std::make_tuple( bt, irreversible );
   }

   void store_provider::find_or_create_slice_pair(uint32_t block_height, bool append, fc::cfile& trace, fc::cfile& index) {
      const uint32_t slice_number = _slice_directory.slice_number(block_height);
      const bool trace_found = _slice_directory.find_or_create_trace_slice(slice_number, append, trace);
      const bool index_found = _slice_directory.find_or_create_index_slice(slice_number, append, index);
      if (trace_found != index_found) {
         const std::string trace_status = trace_found ? "existing" : "new";
         const std::string index_status = index_found ? "existing" : "new";
         elog("Trace file is ${ts}, but it's metadata file is ${is}. This means the files are not consistent.", ("ts", trace_status)("is", index_status));
      }
   }

   slice_directory::slice_directory(const bfs::path& slice_dir, uint32_t width)
   : _slice_dir(slice_dir), _width(width) {
      if (!exists(_slice_dir))
         throw path_does_not_exist("The provided path does not exist: " + _slice_dir.generic_string());
   }

   bool slice_directory::find_or_create_index_slice(uint32_t slice_number, bool append, fc::cfile& index_file) const {
      const bool found = find_index_slice(slice_number, append, index_file);
      if( !found ) {
         create_new_index_slice_file(index_file);
      }
      return found;
   }

   bool slice_directory::find_index_slice(uint32_t slice_number, bool append, fc::cfile& index_file) const {
      if( !find_slice(_trace_index_prefix, slice_number, index_file) ) {
         return false;
      }

      validate_existing_index_slice_file(index_file, append);
      return true;
   }

   void slice_directory::create_new_index_slice_file(fc::cfile& index_file) const {
      index_file.open(fc::cfile::create_or_update_rw_mode);
      index_header h { .version = _current_version };
      append_store(h, index_file);
   }

   void slice_directory::validate_existing_index_slice_file(fc::cfile& index_file, bool append) const {
      const auto header = extract_store<index_header>(index_file);
      if (header.version != _current_version) {
         throw old_slice_version("Old slice file with version: " + std::to_string(header.version) +
                                 " is in directory, only supporting version: " + std::to_string(_current_version));
      }
      if( append ) {
         index_file.seek_end(0);
      }
   }

   bool slice_directory::find_or_create_trace_slice(uint32_t slice_number, bool append, fc::cfile& trace_file) const {
      const bool found = find_trace_slice(slice_number, append, trace_file);

      if( !found ) {
         trace_file.open(fc::cfile::create_or_update_rw_mode);
      }
      else if( append ) {
         trace_file.seek_end(0);
      }

      return found;
   }

   bool slice_directory::find_trace_slice(uint32_t slice_number, bool append, fc::cfile& trace_file) const {
      const bool found = find_slice(_trace_prefix, slice_number, trace_file);

      if( !found ) {
         return false;
      }

      if( append ) {
         trace_file.seek_end(0);
      }
      return true;
   }

   bool slice_directory::find_slice(const char* slice_prefix, uint32_t slice_number, fc::cfile& slice_file) const {
      char filename[_max_filename_size] = {};
      const uint32_t slice_start = slice_number * _width;
      const int size_written = snprintf(filename, _max_filename_size, "%s%010d-%010d%s", slice_prefix, slice_start, (slice_start + _width), _trace_ext);
      // assert that _max_filename_size is correct
      if ( size_written >= _max_filename_size ) {
         const std::string max_size_str = std::to_string(_max_filename_size - 1); // dropping null character from size
         const std::string size_written_str = std::to_string(size_written);
         throw std::runtime_error("Could not write the complete filename.  Anticipated the max filename characters to be: " +
            max_size_str + " or less, but wrote: " + size_written_str + " characters.  This is likely because the file "
            "format was changed and the code was not updated accordingly. Filename created: " + filename);
      }
      const path slice_path = _slice_dir / filename;
      slice_file.set_file_path(slice_path);
      if( !exists(slice_path)) {
         return false;
      }

      slice_file.open(fc::cfile::create_or_update_rw_mode);
      slice_file.seek(0);
      return true;
   }
}
<commit_msg>Changed to creating the slice diretories if it doesn't exist.<commit_after>#include <eosio/trace_api/store_provider.hpp>

#include <fc/variant_object.hpp>
#include <fc/crypto/base64.hpp>

namespace {
      static constexpr uint32_t _current_version = 1;
      static constexpr const char* _trace_prefix = "trace_";
      static constexpr const char* _trace_index_prefix = "trace_index_";
      static constexpr const char* _trace_ext = ".log";
      static constexpr uint _max_filename_size = std::char_traits<char>::length(_trace_index_prefix) + 10 + 1 + 10 + std::char_traits<char>::length(_trace_ext) + 1; // "trace_index_" + 10-digits + '-' + 10-digits + ".log" + null-char
}

namespace eosio::trace_api {
   namespace bfs = boost::filesystem;
   store_provider::store_provider(const bfs::path& slice_dir, uint32_t width)
   : _slice_directory(slice_dir, width) {
   }

   void store_provider::append(const block_trace_v0& bt) {
      fc::cfile trace;
      fc::cfile index;
      find_or_create_slice_pair(bt.number, true, trace, index);
      // storing as static_variant to allow adding other data types to the trace file in the future
      const uint64_t offset = append_store(data_log_entry { bt }, trace);

      auto be = metadata_log_entry { block_entry_v0 { .id = bt.id, .number = bt.number, .offset = offset }};
      append_store(be, index);
   }

   void store_provider::append_lib(uint32_t lib) {
      fc::cfile index;
      const uint32_t slice_number = _slice_directory.slice_number(lib);
      _slice_directory.find_or_create_index_slice(slice_number, true, index);
      auto le = metadata_log_entry { lib_entry_v0 { .lib = lib }};
      append_store(le, index);
   }

   get_block_t store_provider::get_block(uint32_t block_height, const yield_function& yield) {
      std::optional<uint64_t> trace_offset;
      bool irreversible = false;
      uint64_t offset = scan_metadata_log_from(block_height, 0, [&block_height, &trace_offset, &irreversible](const metadata_log_entry& e) -> bool {
         if (e.contains<block_entry_v0>()) {
            const auto& block = e.get<block_entry_v0>();
            if (block.number == block_height) {
               trace_offset = block.offset;
            }
         } else if (e.contains<lib_entry_v0>()) {
            auto lib = e.get<lib_entry_v0>().lib;
            if (lib >= block_height) {
               irreversible = true;
               return false;
            }
         }
         return true;
      }, yield);
      if (!trace_offset) {
         return get_block_t{};
      }
      std::optional<data_log_entry> entry = read_data_log(block_height, *trace_offset);
      if (!entry) {
         return get_block_t{};
      }
      const auto bt = entry->get<block_trace_v0>();
      return std::make_tuple( bt, irreversible );
   }

   void store_provider::find_or_create_slice_pair(uint32_t block_height, bool append, fc::cfile& trace, fc::cfile& index) {
      const uint32_t slice_number = _slice_directory.slice_number(block_height);
      const bool trace_found = _slice_directory.find_or_create_trace_slice(slice_number, append, trace);
      const bool index_found = _slice_directory.find_or_create_index_slice(slice_number, append, index);
      if (trace_found != index_found) {
         const std::string trace_status = trace_found ? "existing" : "new";
         const std::string index_status = index_found ? "existing" : "new";
         elog("Trace file is ${ts}, but it's metadata file is ${is}. This means the files are not consistent.", ("ts", trace_status)("is", index_status));
      }
   }

   slice_directory::slice_directory(const bfs::path& slice_dir, uint32_t width)
   : _slice_dir(slice_dir), _width(width) {
      if (!exists(_slice_dir)) {
         bfs::create_directories(slice_dir);
      }
   }

   bool slice_directory::find_or_create_index_slice(uint32_t slice_number, bool append, fc::cfile& index_file) const {
      const bool found = find_index_slice(slice_number, append, index_file);
      if( !found ) {
         create_new_index_slice_file(index_file);
      }
      return found;
   }

   bool slice_directory::find_index_slice(uint32_t slice_number, bool append, fc::cfile& index_file) const {
      if( !find_slice(_trace_index_prefix, slice_number, index_file) ) {
         return false;
      }

      validate_existing_index_slice_file(index_file, append);
      return true;
   }

   void slice_directory::create_new_index_slice_file(fc::cfile& index_file) const {
      index_file.open(fc::cfile::create_or_update_rw_mode);
      index_header h { .version = _current_version };
      append_store(h, index_file);
   }

   void slice_directory::validate_existing_index_slice_file(fc::cfile& index_file, bool append) const {
      const auto header = extract_store<index_header>(index_file);
      if (header.version != _current_version) {
         throw old_slice_version("Old slice file with version: " + std::to_string(header.version) +
                                 " is in directory, only supporting version: " + std::to_string(_current_version));
      }
      if( append ) {
         index_file.seek_end(0);
      }
   }

   bool slice_directory::find_or_create_trace_slice(uint32_t slice_number, bool append, fc::cfile& trace_file) const {
      const bool found = find_trace_slice(slice_number, append, trace_file);

      if( !found ) {
         trace_file.open(fc::cfile::create_or_update_rw_mode);
      }
      else if( append ) {
         trace_file.seek_end(0);
      }

      return found;
   }

   bool slice_directory::find_trace_slice(uint32_t slice_number, bool append, fc::cfile& trace_file) const {
      const bool found = find_slice(_trace_prefix, slice_number, trace_file);

      if( !found ) {
         return false;
      }

      if( append ) {
         trace_file.seek_end(0);
      }
      return true;
   }

   bool slice_directory::find_slice(const char* slice_prefix, uint32_t slice_number, fc::cfile& slice_file) const {
      char filename[_max_filename_size] = {};
      const uint32_t slice_start = slice_number * _width;
      const int size_written = snprintf(filename, _max_filename_size, "%s%010d-%010d%s", slice_prefix, slice_start, (slice_start + _width), _trace_ext);
      // assert that _max_filename_size is correct
      if ( size_written >= _max_filename_size ) {
         const std::string max_size_str = std::to_string(_max_filename_size - 1); // dropping null character from size
         const std::string size_written_str = std::to_string(size_written);
         throw std::runtime_error("Could not write the complete filename.  Anticipated the max filename characters to be: " +
            max_size_str + " or less, but wrote: " + size_written_str + " characters.  This is likely because the file "
            "format was changed and the code was not updated accordingly. Filename created: " + filename);
      }
      const path slice_path = _slice_dir / filename;
      slice_file.set_file_path(slice_path);
      if( !exists(slice_path)) {
         return false;
      }

      slice_file.open(fc::cfile::create_or_update_rw_mode);
      slice_file.seek(0);
      return true;
   }
}
<|endoftext|>


<commit_before>/* Copyright (c) 2014 Quanta Research Cambridge, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/time.h>

#include "P4TopIndication.h"
#include "P4TopRequest.h"
#include "GeneratedTypes.h"
#include "utils.h"

#ifndef le32
#define le32    int32_t
#endif

#ifndef u32
#define u32     u_int32_t
#endif

#ifndef u16
#define u16     u_int16_t
#endif

#ifndef s32
#define s32     int32_t
#endif

#define DATA_WIDTH 128

struct pcap_file_header {
    u32 magic;
    u16 version_major;
    u16 version_minor;
    s32 thiszone; /* gmt to local correction */
    u32 sigfigs;  /* accuracy of timL1 cache bytes userspaceestamps */
    u32 snaplen;  /* max length saved portion of each pkt */
    u32 linktype; /* data link type (LINKTYPE_*) */
} __attribute__((packed));

struct pcap_pkthdr_ts {
    le32 hts_sec;
    le32 hts_usec;
}  __attribute__((packed));

struct pcap_pkthdr {
    struct  pcap_pkthdr_ts ts;  /* time stamp */
    le32 caplen;              /* length of portion present */
    le32 length;                  /* length this packet (off wire) */
}  __attribute__((packed));

void mem_copy(const void *buff, int length);

static P4TopRequestProxy *device = 0;

class P4TopIndication : public P4TopIndicationWrapper
{
public:
    virtual void sonic_read_version_resp(uint32_t a) {
        fprintf(stderr, "version %d\n", a);
    }
    virtual void cam_search_result(uint32_t a) {
        fprintf(stderr, "cam search %d\n", a);
    }
    virtual void read_setram_result(uint64_t a) {
        fprintf(stderr, "setram %lx\n", a);
    }
    virtual void match_table_resp(uint32_t a) {
    	fprintf(stderr, "match table");
    }
    virtual void matchTableResponse(uint32_t key, uint32_t value) {
        fprintf(stderr, "\nkey = %u  value = %u\n", key, value);
    }

    P4TopIndication(unsigned int id) : P4TopIndicationWrapper(id) {}
};

void mem_copy(const void *buff, int packet_size) {

    int i, sop, eop;
    uint64_t data[2];
    int numBeats;

    numBeats = packet_size / 8; // 16 bytes per beat for 128-bit datawidth;
    if (packet_size % 8) numBeats++;
    PRINT_INFO("nBeats=%d, packetSize=%d\n", numBeats, packet_size);
    for (i=0; i<numBeats; i++) {
        data[i%2] = *(static_cast<const uint64_t *>(buff) + i);
        sop = (i/2 == 0);
        eop = (i/2 == (numBeats-1)/2);
        if (i%2) {
            device->writePacketData(data, sop, eop);
            PRINT_INFO("%016lx %016lx %d %d\n", data[1], data[0], sop, eop);
        }

        // last beat, padding with zero
        if ((numBeats%2!=0) && (i==numBeats-1)) {
            sop = (i/2 == 0) ? 1 : 0;
            eop = 1;
            data[1] = 0;
            device->writePacketData(data, sop, eop);
            PRINT_INFO("%016lx %016lx %d %d\n", data[1], data[0], sop, eop);
        }
    }
}

/**
 * Send packet on quick_tx device
 * @param qtx       pointer to a quick_tx structure
 * @param buffer   full packet data starting at the ETH frame
 * @param length  length of packet (must be over 0)
 * @return           length of packet if it was successfully queued, QTX_E_EXIT if a critical error occurred
 *                  and close needs to be called
 */
static inline int quick_tx_send_packet(const void* buffer, int length) {
    assert(buffer);
    assert(length > 0);

#ifdef EXTRA_DEBUG
    printf("[quick_tx] Copying data from %p buffer, length = %d\n",
                (buffer, length);
#endif
    mem_copy(buffer, length);

    return length;
}

bool read_pcap_file(char* filename, void** buffer, long *length) {
    FILE *infile;
    long length_read;

    infile = fopen(filename, "r");
    if(infile == NULL) {
            printf("File does not exist!\n");
            return false;
        }

    fseek(infile, 0L, SEEK_END);
    *length = ftell(infile);
    fseek(infile, 0L, SEEK_SET);
    *buffer = (char*)calloc(*length, sizeof(char));

    /* memory error */
    if(*buffer == NULL) {
            printf("Could not allocate %ld bytes of memory!\n", *length);
            return false;
        }

    length_read = fread(*buffer, sizeof(char), *length, infile);
    *length = length_read;
    fclose(infile);

    return true;
}

void test_setram(P4TopRequestProxy *device) {
    device->writeSetRam(0x11, 0xff);
    device->readSetRam(0x11);
}

void test_bcam(P4TopRequestProxy *device) {
    device->camInsert(0x302, 0x24);
    //device->camSearch(0x24);
    device->camInsert(0x303, 0x24);
}

int main(int argc, char **argv)
{
    void *buffer;
    long length;
    struct pcap_pkthdr* pcap_hdr;
    int i;
    int loops = 1;

    P4TopIndication echoIndication(IfcNames_P4TopIndicationH2S);
    device = new P4TopRequestProxy(IfcNames_P4TopRequestS2H);

    device->sonic_read_version();

//    test_setram(device);
    test_bcam(device);

//    device->matchTableRequest(10, 15, 1);
//    device->matchTableRequest(10, 0, 0);

    while(1) sleep(1);

    fprintf(stderr, "Attempts to read pcap file %s\n", argv[1]);
    if (!read_pcap_file(argv[1], &buffer, &length)) {
        perror("Failed to read file!");
        exit(-1);
    }

    for (i = 0; i < loops; i++) {
        void* offset = static_cast<char *>(buffer) + sizeof(struct pcap_file_header);

        while(offset < static_cast<char *>(buffer) + length) {
            pcap_hdr = (struct pcap_pkthdr*) offset;
            offset = static_cast<char *>(offset) + sizeof(struct pcap_pkthdr);

            if ((quick_tx_send_packet((const void*)offset, pcap_hdr->caplen)) < 0) {
                printf("An error occurred while trying to send a packet\n");
                exit(-1);
            }

            offset = static_cast<char *>(offset) + pcap_hdr->caplen;
        }
    }

    return 0;
}
<commit_msg>added more robust tests that tests all ops<commit_after>/* Copyright (c) 2014 Quanta Research Cambridge, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/time.h>

#include "P4TopIndication.h"
#include "P4TopRequest.h"
#include "GeneratedTypes.h"
#include "utils.h"

#ifndef le32
#define le32    int32_t
#endif

#ifndef u32
#define u32     u_int32_t
#endif

#ifndef u16
#define u16     u_int16_t
#endif

#ifndef s32
#define s32     int32_t
#endif

#define DATA_WIDTH 128

struct pcap_file_header {
    u32 magic;
    u16 version_major;
    u16 version_minor;
    s32 thiszone; /* gmt to local correction */
    u32 sigfigs;  /* accuracy of timL1 cache bytes userspaceestamps */
    u32 snaplen;  /* max length saved portion of each pkt */
    u32 linktype; /* data link type (LINKTYPE_*) */
} __attribute__((packed));

struct pcap_pkthdr_ts {
    le32 hts_sec;
    le32 hts_usec;
}  __attribute__((packed));

struct pcap_pkthdr {
    struct  pcap_pkthdr_ts ts;  /* time stamp */
    le32 caplen;              /* length of portion present */
    le32 length;                  /* length this packet (off wire) */
}  __attribute__((packed));

void mem_copy(const void *buff, int length);

static P4TopRequestProxy *device = 0;

class P4TopIndication : public P4TopIndicationWrapper
{
public:
    virtual void sonic_read_version_resp(uint32_t a) {
        fprintf(stderr, "version %d\n", a);
    }
    virtual void cam_search_result(uint32_t a) {
        fprintf(stderr, "cam search %d\n", a);
    }
    virtual void read_setram_result(uint64_t a) {
        fprintf(stderr, "setram %lx\n", a);
    }
    virtual void match_table_resp(uint32_t a) {
    	fprintf(stderr, "match table");
    }
    virtual void matchTableResponse(uint32_t key, uint32_t value) {
        fprintf(stderr, "GET : key = %u  value = %u\n", key, value);
    }

    P4TopIndication(unsigned int id) : P4TopIndicationWrapper(id) {}
};

void mem_copy(const void *buff, int packet_size) {

    int i, sop, eop;
    uint64_t data[2];
    int numBeats;

    numBeats = packet_size / 8; // 16 bytes per beat for 128-bit datawidth;
    if (packet_size % 8) numBeats++;
    PRINT_INFO("nBeats=%d, packetSize=%d\n", numBeats, packet_size);
    for (i=0; i<numBeats; i++) {
        data[i%2] = *(static_cast<const uint64_t *>(buff) + i);
        sop = (i/2 == 0);
        eop = (i/2 == (numBeats-1)/2);
        if (i%2) {
            device->writePacketData(data, sop, eop);
            PRINT_INFO("%016lx %016lx %d %d\n", data[1], data[0], sop, eop);
        }

        // last beat, padding with zero
        if ((numBeats%2!=0) && (i==numBeats-1)) {
            sop = (i/2 == 0) ? 1 : 0;
            eop = 1;
            data[1] = 0;
            device->writePacketData(data, sop, eop);
            PRINT_INFO("%016lx %016lx %d %d\n", data[1], data[0], sop, eop);
        }
    }
}

/**
 * Send packet on quick_tx device
 * @param qtx       pointer to a quick_tx structure
 * @param buffer   full packet data starting at the ETH frame
 * @param length  length of packet (must be over 0)
 * @return           length of packet if it was successfully queued, QTX_E_EXIT if a critical error occurred
 *                  and close needs to be called
 */
static inline int quick_tx_send_packet(const void* buffer, int length) {
    assert(buffer);
    assert(length > 0);

#ifdef EXTRA_DEBUG
    printf("[quick_tx] Copying data from %p buffer, length = %d\n",
                (buffer, length);
#endif
    mem_copy(buffer, length);

    return length;
}

bool read_pcap_file(char* filename, void** buffer, long *length) {
    FILE *infile;
    long length_read;

    infile = fopen(filename, "r");
    if(infile == NULL) {
            printf("File does not exist!\n");
            return false;
        }

    fseek(infile, 0L, SEEK_END);
    *length = ftell(infile);
    fseek(infile, 0L, SEEK_SET);
    *buffer = (char*)calloc(*length, sizeof(char));

    /* memory error */
    if(*buffer == NULL) {
            printf("Could not allocate %ld bytes of memory!\n", *length);
            return false;
        }

    length_read = fread(*buffer, sizeof(char), *length, infile);
    *length = length_read;
    fclose(infile);

    return true;
}

void test_setram(P4TopRequestProxy *device) {
    device->writeSetRam(0x11, 0xff);
    device->readSetRam(0x11);
}

void test_bcam(P4TopRequestProxy *device) {
    device->camInsert(0x302, 0x24);
    //device->camSearch(0x24);
    device->camInsert(0x303, 0x24);
}

int main(int argc, char **argv)
{
    void *buffer;
    long length;
    struct pcap_pkthdr* pcap_hdr;
    int i;
    int loops = 1;

    P4TopIndication echoIndication(IfcNames_P4TopIndicationH2S);
    device = new P4TopRequestProxy(IfcNames_P4TopRequestS2H);

    device->sonic_read_version();

//    test_setram(device);
    test_bcam(device);

//    device->matchTableRequest(10, 15, 1);
//    device->matchTableRequest(10, 0, 0);

    device->matchTableRequest(10, 15, 1); //PUT(10,15)
    device->matchTableRequest(10, 0, 0);  //GET(10) should print k=10 v=15
    device->matchTableRequest(10, 20, 2); //UPDATE(10,20)
    device->matchTableRequest(10, 0, 0);  //GET(10) should print k=10 v=20
    device->matchTableRequest(10, 0, 3);  //REMOVE(10)
    device->matchTableRequest(10, 0, 0);  //GET(10) should not print anything
    device->matchTableRequest(10, 30, 2); //UPDATE(10,30) should not update
    device->matchTableRequest(10, 0, 0);  //GET(10) should not print anything
    device->matchTableRequest(10, 45, 1); //PUT(10,45)
    device->matchTableRequest(10, 0, 0);  //GET(10) should print k=10 v=45

    while(1) sleep(1);

    fprintf(stderr, "Attempts to read pcap file %s\n", argv[1]);
    if (!read_pcap_file(argv[1], &buffer, &length)) {
        perror("Failed to read file!");
        exit(-1);
    }

    for (i = 0; i < loops; i++) {
        void* offset = static_cast<char *>(buffer) + sizeof(struct pcap_file_header);

        while(offset < static_cast<char *>(buffer) + length) {
            pcap_hdr = (struct pcap_pkthdr*) offset;
            offset = static_cast<char *>(offset) + sizeof(struct pcap_pkthdr);

            if ((quick_tx_send_packet((const void*)offset, pcap_hdr->caplen)) < 0) {
                printf("An error occurred while trying to send a packet\n");
                exit(-1);
            }

            offset = static_cast<char *>(offset) + pcap_hdr->caplen;
        }
    }

    return 0;
}
<|endoftext|>


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

#ifndef _OSL_TIME_H_
#include <osl/time.h>
#endif
#ifndef _OSL_THREAD_H_
#include <osl/thread.h>
#endif

#ifndef _RTL_LOGFILE_H_
#include <rtl/logfile.h>
#endif
#ifndef _RTL_STRING_HXX_
#include <rtl/string.hxx>
#endif

namespace rtl
{
/**
@descr  The intended use for class Logfile is to write time stamp information
        for profiling purposes.

        Profiling output should only be generated for a special product version of OpenOffice
        which is compiled with a defined preprocessor symbol 'PROFILE'.
        Therefore we have provided a set of macros that uses the class Logfile only if
        this symbol is defined.  If the macros are not sufficient, i.e. you need more
        then three arguments for a printf style message, then you have to insert an
        #ifdef PROFILE/#endif brace yourself.

        Additionally the environment variable RTL_LOGFILE has to be defined in order to generate
        logging information. If the variable is not empty, it creates a file with the name
        $(RTL_LOGFILE)_$(PID).log, where $(PID) is the process id of the running process.
        It can be used as a run time switch for enabling or disabling the logging.
        Note that this variable is evaluated only once at the first attempt to write a message.

        The class LogFile collects runtime data within its constructor and destructor. It can be
        used for timing whole functions.
        If you want to write timing data without context you can use the RTL_LOGFILE_TRACE-macros
        which are defined inside <rtl/logfile.h>.

        The class LogFile should not be used directly, instead use the RTL_LOGFILE_CONTEXT/
        RTL_LOGFILE_TRACE-macros.

        Macro usage:
        ------------
        RTL_LOGFILE_CONTEXT( instance, name );
        This macro creates an instance of class LogFile with the name "instance" and writes the current time,
        thread id and "name" to the log file.

        Example: RTL_LOGFILE_CONTEXT( aLog, "Timing for foo-method" );

        RTL_LOGFILE_CONTEXT_TRACE( instance, mesage );
        RTL_LOGFILE_CONTEXT_TRACEn( instance, frmt, arg1, .., arg3 );
        These macros can be used to log information in a "instance" context. The "instance" object
        is used to log message informations. All macros with "frmt" uses printf notation to log timing infos.

        Example: RTL_LOGFILE_CONTEXT_TRACE( aLog, "Now we call an expensive function" );
                 RTL_LOGFIlE_CONTEXT_TRACE1( aLog, "Config entries read: %u", (unsigned short)i );

        RTL_LOGFILE_TRACE( string );
        RTL_LOGFILE_TRACEn( frmt, arg1, .., arg3 );
        These macros can be used to log information outside a context. The macro directly calls
        rtl_logfile_trace to write the info to the log file. All macros with "frmt" uses printf
        notation to log timing infos.

        Example: RTL_LOGFILE_TRACE( "Timing for loading a file" );
                 RTL_LOGFILE_TRACE1( aLog, "Timing for loading file: %s", aFileName );

        The lines written to the log file consist of the following space separated elements:
        1.  The time relative to the start of the global timer in milliseconds.  The times is
            started typically for the first logged line.
        2.  Thread id.  It's absolut value is probably of less interest than providing a way to
            distinguish different threads.
        3.  a.  An opening or closing curly brace indicating the start or end of a scope.
                4a. Function name or general scope identifier.
            b.  A vertical line indicating an arbitrary message.
                4b optional function name or general scope identifier.
                5b A colon followed by a space and a free form message terminated by a newline.
*/
    class Logfile
    {
    public:
        inline Logfile( const sal_Char *name );
        inline ~Logfile();
        inline const sal_Char *getName();
    private:
        ::rtl::OString m_sName;
    };

    inline Logfile::Logfile( const sal_Char *name )
        : m_sName( name )
    {
        rtl_logfile_trace( "%06lu %lu { %s\n",
                           osl_getGlobalTimer(),
                           osl_getThreadIdentifier( 0 ),
                           name );
    }

    inline Logfile::~Logfile()
    {
        rtl_logfile_trace( "%06lu %lu } %s\n",
                           osl_getGlobalTimer(),
                           osl_getThreadIdentifier(0),
                           m_sName.pData->buffer );
    }

    inline const sal_Char * Logfile::getName()
    {
        return m_sName.getStr();
    }
}

#ifdef PROFILE
#define RTL_LOGFILE_CONTEXT( instance, name ) ::rtl::Logfile instance( name )
#define RTL_LOGFILE_CONTEXT_TRACE( instance, message ) \
        rtl_logfile_trace( "%06lu %lu | %s : %s\n", \
                           osl_getGlobalTimer(),  \
                           osl_getThreadIdentifier( 0 ), \
                           instance.getName(), \
                           message )
#define RTL_LOGFILE_CONTEXT_TRACE1( instance , frmt, arg1 ) \
        rtl_logfile_trace( "%06lu %lu | %s : ", \
                           osl_getGlobalTimer(),  \
                           osl_getThreadIdentifier( 0 ), \
                           instance.getName() ); \
        rtl_logfile_trace( frmt , arg1 ); \
        rtl_logfile_trace( "\n" )
#define RTL_LOGFILE_CONTEXT_TRACE2( instance , frmt, arg1 , arg2 ) \
        rtl_logfile_trace( "%06lu %lu | %s : ", \
                           osl_getGlobalTimer(),  \
                           osl_getThreadIdentifier( 0 ), \
                           instance.getName() ); \
        rtl_logfile_trace( frmt , arg1 , arg2 ); \
        rtl_logfile_trace( "\n" )
#define RTL_LOGFILE_CONTEXT_TRACE3( instance , frmt, arg1 , arg2 , arg3 ) \
        rtl_logfile_trace( "%06lu %lu | %s : ", \
                           osl_getGlobalTimer(),  \
                           osl_getThreadIdentifier( 0 ), \
                           instance.getName() ); \
        rtl_logfile_trace( frmt , arg1 , arg2 , arg3 ); \
        rtl_logfile_trace( "\n" )

#else
#define RTL_LOGFILE_CONTEXT( instance,name )  ((void)0)
#define RTL_LOGFILE_CONTEXT_TRACE( instance, message )  ((void)0)
#define RTL_LOGFILE_CONTEXT_TRACE1( instance, frmt, arg1 ) ((void)0)
#define RTL_LOGFILE_CONTEXT_TRACE2( instance, frmt, arg1, arg2 ) ((void)0)
#define RTL_LOGFILE_CONTEXT_TRACE3( instance, frmt, arg1, arg2 , arg3 ) ((void)0)
#endif

#endif
<commit_msg>#88409# changed guard define from PROFILE to TIMELOG<commit_after>/*************************************************************************
 *
 *  $RCSfile: logfile.hxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: cd $ $Date: 2001-07-09 12:51:33 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2001 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/
#ifndef _RTL_LOGFILE_HXX_
#define _RTL_LOGFILE_HXX_

#ifndef _OSL_TIME_H_
#include <osl/time.h>
#endif
#ifndef _OSL_THREAD_H_
#include <osl/thread.h>
#endif

#ifndef _RTL_LOGFILE_H_
#include <rtl/logfile.h>
#endif
#ifndef _RTL_STRING_HXX_
#include <rtl/string.hxx>
#endif

namespace rtl
{
/**
@descr  The intended use for class Logfile is to write time stamp information
        for profiling purposes.

        Profiling output should only be generated for a special product version of OpenOffice
        which is compiled with a defined preprocessor symbol 'TIMELOG'.
        Therefore we have provided a set of macros that uses the class Logfile only if
        this symbol is defined.  If the macros are not sufficient, i.e. you need more
        then three arguments for a printf style message, then you have to insert an
        #ifdef TIMELOG/#endif brace yourself.

        Additionally the environment variable RTL_LOGFILE has to be defined in order to generate
        logging information. If the variable is not empty, it creates a file with the name
        $(RTL_LOGFILE)_$(PID).log, where $(PID) is the process id of the running process.
        It can be used as a run time switch for enabling or disabling the logging.
        Note that this variable is evaluated only once at the first attempt to write a message.

        The class LogFile collects runtime data within its constructor and destructor. It can be
        used for timing whole functions.
        If you want to write timing data without context you can use the RTL_LOGFILE_TRACE-macros
        which are defined inside <rtl/logfile.h>.

        The class LogFile should not be used directly, instead use the RTL_LOGFILE_CONTEXT/
        RTL_LOGFILE_TRACE-macros.

        Macro usage:
        ------------
        RTL_LOGFILE_CONTEXT( instance, name );
        This macro creates an instance of class LogFile with the name "instance" and writes the current time,
        thread id and "name" to the log file.

        Example: RTL_LOGFILE_CONTEXT( aLog, "Timing for foo-method" );

        RTL_LOGFILE_CONTEXT_TRACE( instance, mesage );
        RTL_LOGFILE_CONTEXT_TRACEn( instance, frmt, arg1, .., arg3 );
        These macros can be used to log information in a "instance" context. The "instance" object
        is used to log message informations. All macros with "frmt" uses printf notation to log timing infos.

        Example: RTL_LOGFILE_CONTEXT_TRACE( aLog, "Now we call an expensive function" );
                 RTL_LOGFIlE_CONTEXT_TRACE1( aLog, "Config entries read: %u", (unsigned short)i );

        RTL_LOGFILE_TRACE( string );
        RTL_LOGFILE_TRACEn( frmt, arg1, .., arg3 );
        These macros can be used to log information outside a context. The macro directly calls
        rtl_logfile_trace to write the info to the log file. All macros with "frmt" uses printf
        notation to log timing infos.

        Example: RTL_LOGFILE_TRACE( "Timing for loading a file" );
                 RTL_LOGFILE_TRACE1( aLog, "Timing for loading file: %s", aFileName );

        The lines written to the log file consist of the following space separated elements:
        1.  The time relative to the start of the global timer in milliseconds.  The times is
            started typically for the first logged line.
        2.  Thread id.  It's absolut value is probably of less interest than providing a way to
            distinguish different threads.
        3.  a.  An opening or closing curly brace indicating the start or end of a scope.
                4a. Function name or general scope identifier.
            b.  A vertical line indicating an arbitrary message.
                4b optional function name or general scope identifier.
                5b A colon followed by a space and a free form message terminated by a newline.
*/
    class Logfile
    {
    public:
        inline Logfile( const sal_Char *name );
        inline ~Logfile();
        inline const sal_Char *getName();
    private:
        ::rtl::OString m_sName;
    };

    inline Logfile::Logfile( const sal_Char *name )
        : m_sName( name )
    {
        rtl_logfile_trace( "%06lu %lu { %s\n",
                           osl_getGlobalTimer(),
                           osl_getThreadIdentifier( 0 ),
                           name );
    }

    inline Logfile::~Logfile()
    {
        rtl_logfile_trace( "%06lu %lu } %s\n",
                           osl_getGlobalTimer(),
                           osl_getThreadIdentifier(0),
                           m_sName.pData->buffer );
    }

    inline const sal_Char * Logfile::getName()
    {
        return m_sName.getStr();
    }
}

#ifdef TIMELOG
#define RTL_LOGFILE_CONTEXT( instance, name ) ::rtl::Logfile instance( name )
#define RTL_LOGFILE_CONTEXT_TRACE( instance, message ) \
        rtl_logfile_trace( "%06lu %lu | %s : %s\n", \
                           osl_getGlobalTimer(),  \
                           osl_getThreadIdentifier( 0 ), \
                           instance.getName(), \
                           message )
#define RTL_LOGFILE_CONTEXT_TRACE1( instance , frmt, arg1 ) \
        rtl_logfile_trace( "%06lu %lu | %s : ", \
                           osl_getGlobalTimer(),  \
                           osl_getThreadIdentifier( 0 ), \
                           instance.getName() ); \
        rtl_logfile_trace( frmt , arg1 ); \
        rtl_logfile_trace( "\n" )
#define RTL_LOGFILE_CONTEXT_TRACE2( instance , frmt, arg1 , arg2 ) \
        rtl_logfile_trace( "%06lu %lu | %s : ", \
                           osl_getGlobalTimer(),  \
                           osl_getThreadIdentifier( 0 ), \
                           instance.getName() ); \
        rtl_logfile_trace( frmt , arg1 , arg2 ); \
        rtl_logfile_trace( "\n" )
#define RTL_LOGFILE_CONTEXT_TRACE3( instance , frmt, arg1 , arg2 , arg3 ) \
        rtl_logfile_trace( "%06lu %lu | %s : ", \
                           osl_getGlobalTimer(),  \
                           osl_getThreadIdentifier( 0 ), \
                           instance.getName() ); \
        rtl_logfile_trace( frmt , arg1 , arg2 , arg3 ); \
        rtl_logfile_trace( "\n" )

#else
#define RTL_LOGFILE_CONTEXT( instance,name )  ((void)0)
#define RTL_LOGFILE_CONTEXT_TRACE( instance, message )  ((void)0)
#define RTL_LOGFILE_CONTEXT_TRACE1( instance, frmt, arg1 ) ((void)0)
#define RTL_LOGFILE_CONTEXT_TRACE2( instance, frmt, arg1, arg2 ) ((void)0)
#define RTL_LOGFILE_CONTEXT_TRACE3( instance, frmt, arg1, arg2 , arg3 ) ((void)0)
#endif

#endif
<|endoftext|>


<commit_before><commit_msg>better way to check for empty strings<commit_after><|endoftext|>


<commit_before>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center,
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

#include "MiniAppManager.h"
#include <mitkBaseDataIOFactory.h>
#include <metaCommand.h>
#include "ctkCommandLineParser.h"
#include <usAny.h>
#include <mitkIOUtil.h>
#include <boost/lexical_cast.hpp>
#include <mitkCoreObjectFactory.h>
#include <mitkPlanarFigure.h>
#include <mitkPlanarFigureComposite.h>
#include <mitkFiberBundle.h>

#define _USE_MATH_DEFINES
#include <math.h>

/*!
\brief Join multiple tractograms
*/
int FiberJoin(int argc, char* argv[])
  {
    mitkCommandLineParser parser;

    parser.setTitle("Fiber Join");
    parser.setCategory("Fiber Tracking and Processing Methods");
    parser.setContributor("MIC");
    parser.setDescription("Join multiple tractograms");

    parser.setArgumentPrefix("--", "-");
    parser.addArgument("input", "i", mitkCommandLineParser::StringList, "Input:", "input tractograms (.fib, vtk file format)", us::Any(), false);
    parser.addArgument("out", "o", mitkCommandLineParser::String, "Output:", "output tractogram", us::Any(), false);

    map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
    if (parsedArgs.size()==0)
        return EXIT_FAILURE;

    mitkCommandLineParser::StringContainerType inFibs = us::any_cast<mitkCommandLineParser::StringContainerType>(parsedArgs["input"]);
    string outFib = us::any_cast<string>(parsedArgs["out"]);

    if (inFibs.size()<=1)
    {
        std::cout << "More than one input tractogram required!";
        return EXIT_FAILURE;
    }

    try
    {
        mitk::FiberBundle::Pointer result = dynamic_cast<mitk::FiberBundle*>(mitk::IOUtil::LoadDataNode(inFibs.at(0))->GetData());
        for (int i=1; i<inFibs.size(); i++)
        {
            try
            {
                mitk::FiberBundle::Pointer inputTractogram = dynamic_cast<mitk::FiberBundle*>(mitk::IOUtil::LoadDataNode(inFibs.at(i))->GetData());
                result = result->AddBundle(inputTractogram);
            }
            catch(...){ std::cout << "could not load: " << inFibs.at(i); }
        }
        mitk::IOUtil::SaveBaseData(result, outFib);
    }
    catch (itk::ExceptionObject e)
    {
        std::cout << e;
        return EXIT_FAILURE;
    }
    catch (std::exception e)
    {
        std::cout << e.what();
        return EXIT_FAILURE;
    }
    catch (...)
    {
        std::cout << "ERROR!?!";
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}
RegisterDiffusionMiniApp(FiberJoin);
<commit_msg>fixed fiber join cmdapp<commit_after>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center,
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

#include <mitkIOUtil.h>
#include <metaCommand.h>
#include "mitkCommandLineParser.h"
#include <usAny.h>
#include <mitkIOUtil.h>
#include <boost/lexical_cast.hpp>
#include <mitkCoreObjectFactory.h>
#include <mitkPlanarFigure.h>
#include <mitkPlanarFigureComposite.h>
#include <mitkFiberBundle.h>

#define _USE_MATH_DEFINES
#include <math.h>

mitk::FiberBundle::Pointer LoadFib(std::string filename)
{
    std::vector<mitk::BaseData::Pointer> fibInfile = mitk::IOUtil::Load(filename);
    if( fibInfile.empty() )
        std::cout << "File " << filename << " could not be read!";
    mitk::BaseData::Pointer baseData = fibInfile.at(0);
    return dynamic_cast<mitk::FiberBundle*>(baseData.GetPointer());
}

/*!
\brief Join multiple tractograms
*/
int main(int argc, char* argv[])
{
  mitkCommandLineParser parser;

  parser.setTitle("Fiber Join");
  parser.setCategory("Fiber Tracking and Processing Methods");
  parser.setContributor("MIC");
  parser.setDescription("Join multiple tractograms");

  parser.setArgumentPrefix("--", "-");
  parser.addArgument("input", "i", mitkCommandLineParser::StringList, "Input:", "input tractograms", us::Any(), false);
  parser.addArgument("out", "o", mitkCommandLineParser::String, "Output:", "output tractogram", us::Any(), false);

  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
  if (parsedArgs.size()==0)
      return EXIT_FAILURE;

  mitkCommandLineParser::StringContainerType inFibs = us::any_cast<mitkCommandLineParser::StringContainerType>(parsedArgs["input"]);
  std::string outFib = us::any_cast<std::string>(parsedArgs["out"]);

  if (inFibs.size()<=1)
  {
      std::cout << "More than one input tractogram required!";
      return EXIT_FAILURE;
  }

  try
  {
      mitk::FiberBundle::Pointer result = LoadFib(inFibs.at(0));
      for (int i=1; i<inFibs.size(); i++)
      {
          try
          {
              mitk::FiberBundle::Pointer inputTractogram = LoadFib(inFibs.at(i));
              result = result->AddBundle(inputTractogram);
          }
          catch(...){ std::cout << "could not load: " << inFibs.at(i); }
      }
      mitk::IOUtil::SaveBaseData(result, outFib);
  }
  catch (itk::ExceptionObject e)
  {
      std::cout << e;
      return EXIT_FAILURE;
  }
  catch (std::exception e)
  {
      std::cout << e.what();
      return EXIT_FAILURE;
  }
  catch (...)
  {
      std::cout << "ERROR!?!";
      return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}
<|endoftext|>


<commit_before>#ifndef _SNARKFRONT_ENUM_OPS_HPP_
#define _SNARKFRONT_ENUM_OPS_HPP_

#include <cstdint>

#include <snarkfront/BitwiseINT.hpp>
#include <snarkfront/PowersOf2.hpp>

namespace snarkfront {

// logical and arithmetic
enum class LogicalOps { AND, OR, XOR, SAME, CMPLMNT };
enum class ScalarOps { ADD, SUB, MUL };
enum class BitwiseOps { AND, OR, XOR, SAME, CMPLMNT, ADDMOD, SHL, SHR, ROTL, ROTR };

// comparison
enum class EqualityCmp { EQ, NEQ };
enum class ScalarCmp { EQ, NEQ, LT, LE, GT, GE };

// number of operator input arguments
template <typename ENUM_OPS> std::size_t opArgc(const ENUM_OPS op);

// returns true for shift and rotate
bool isPermute(const BitwiseOps op);

// EQ --> SAME
// NEQ --> XOR
LogicalOps eqToLogical(const EqualityCmp op);
LogicalOps eqToLogical(const ScalarCmp op);

// evaluate logical operations
template <typename T>
T evalOp(const LogicalOps op, const T& x, const T& y)
{
    switch (op) {
    case (LogicalOps::AND) : return x && y;
    case (LogicalOps::OR) : return x || y;
    case (LogicalOps::XOR) : return x != y;
    case (LogicalOps::SAME) : return x == y;
    case (LogicalOps::CMPLMNT) : return ! x;
    }
}

// evaluate scalar arithmetic operations
template <typename T>
T evalOp(const ScalarOps op, const T& x, const T& y)
{
    switch (op) {
    case (ScalarOps::ADD) : return x + y;
    case (ScalarOps::SUB) : return x - y;
    case (ScalarOps::MUL) : return x * y;
    }
}

// evaluate bitwise word operations
template <typename T>
T evalOp(const BitwiseOps op, const T& x, const T& y)
{
    typedef BitwiseINT<T> B;

    switch (op) {
    case (BitwiseOps::AND) : return B::AND(x, y);
    case (BitwiseOps::OR) : return B::OR(x, y);
    case (BitwiseOps::XOR) : return B::XOR(x, y);
    case (BitwiseOps::SAME) : return B::CMPLMNT(B::XOR(x, y));
    case (BitwiseOps::CMPLMNT) : return B::CMPLMNT(x);
    case (BitwiseOps::ADDMOD) : return B::ADDMOD(x, y);
    case (BitwiseOps::SHL) : return B::SHL(x, y);
    case (BitwiseOps::SHR) : return B::SHR(x, y);
    case (BitwiseOps::ROTL) : return B::ROTL(x, y);
    case (BitwiseOps::ROTR) : return B::ROTR(x, y);
    }
}

// evaluate equality comparison operations
template <typename T>
bool evalOp(const EqualityCmp op, const T& x, const T& y)
{
    switch (op) {
    case (EqualityCmp::EQ) : return x == y;
    case (EqualityCmp::NEQ) : return x != y;
    }
}

// evaluate scalar comparsion operations
template <typename T>
bool evalOp(const ScalarCmp op, const T& x, const T& y)
{
    switch (op) {
    case (ScalarCmp::EQ) : return x == y;
    case (ScalarCmp::NEQ) : return x != y;
    case (ScalarCmp::LT) : return x < y;
    case (ScalarCmp::LE) : return (x < y) || (x == y);
    case (ScalarCmp::GT) : return y < x;
    case (ScalarCmp::GE) : return (y < x) || (x == y);
    }
}

} // namespace snarkfront

#endif
<commit_msg>finite field type<commit_after>#ifndef _SNARKFRONT_ENUM_OPS_HPP_
#define _SNARKFRONT_ENUM_OPS_HPP_

#include <cstdint>

#include <snarklib/FpModel.hpp>

#include <snarkfront/BitwiseINT.hpp>

namespace snarkfront {

// logical and arithmetic
enum class LogicalOps { AND, OR, XOR, SAME, CMPLMNT };
enum class ScalarOps { ADD, SUB, MUL };
enum class FieldOps { ADD, SUB, MUL, INV };
enum class BitwiseOps { AND, OR, XOR, SAME, CMPLMNT, ADDMOD, SHL, SHR, ROTL, ROTR };

// comparison
enum class EqualityCmp { EQ, NEQ };
enum class ScalarCmp { EQ, NEQ, LT, LE, GT, GE };

// number of operator input arguments
template <typename ENUM_OPS> std::size_t opArgc(const ENUM_OPS op);

// returns true for shift and rotate
bool isPermute(const BitwiseOps op);

// EQ --> SAME
// NEQ --> XOR
LogicalOps eqToLogical(const EqualityCmp op);
LogicalOps eqToLogical(const ScalarCmp op);

// evaluate logical operations
template <typename T>
T evalOp(const LogicalOps op, const T& x, const T& y)
{
    switch (op) {
    case (LogicalOps::AND) : return x && y;
    case (LogicalOps::OR) : return x || y;
    case (LogicalOps::XOR) : return x != y;
    case (LogicalOps::SAME) : return x == y;
    case (LogicalOps::CMPLMNT) : return ! x;
    }
}

// evaluate scalar arithmetic operations
template <typename T>
T evalOp(const ScalarOps op, const T& x, const T& y)
{
    switch (op) {
    case (ScalarOps::ADD) : return x + y;
    case (ScalarOps::SUB) : return x - y;
    case (ScalarOps::MUL) : return x * y;
    }
}

// evaluate finite scalar field arithmetic operations
template <typename T>
T evalOp(const FieldOps op, const T& x, const T& y)
{
    switch (op) {
    case (FieldOps::ADD) : return x + y;
    case (FieldOps::SUB) : return x - y;
    case (FieldOps::MUL) : return x * y;
    case (FieldOps::INV) : return snarklib::inverse(x);
    }
}

// evaluate bitwise word operations
template <typename T>
T evalOp(const BitwiseOps op, const T& x, const T& y)
{
    typedef BitwiseINT<T> B;

    switch (op) {
    case (BitwiseOps::AND) : return B::AND(x, y);
    case (BitwiseOps::OR) : return B::OR(x, y);
    case (BitwiseOps::XOR) : return B::XOR(x, y);
    case (BitwiseOps::SAME) : return B::CMPLMNT(B::XOR(x, y));
    case (BitwiseOps::CMPLMNT) : return B::CMPLMNT(x);
    case (BitwiseOps::ADDMOD) : return B::ADDMOD(x, y);
    case (BitwiseOps::SHL) : return B::SHL(x, y);
    case (BitwiseOps::SHR) : return B::SHR(x, y);
    case (BitwiseOps::ROTL) : return B::ROTL(x, y);
    case (BitwiseOps::ROTR) : return B::ROTR(x, y);
    }
}

// evaluate equality comparison operations
template <typename T>
bool evalOp(const EqualityCmp op, const T& x, const T& y)
{
    switch (op) {
    case (EqualityCmp::EQ) : return x == y;
    case (EqualityCmp::NEQ) : return x != y;
    }
}

// evaluate scalar comparsion operations
template <typename T>
bool evalOp(const ScalarCmp op, const T& x, const T& y)
{
    switch (op) {
    case (ScalarCmp::EQ) : return x == y;
    case (ScalarCmp::NEQ) : return x != y;
    case (ScalarCmp::LT) : return x < y;
    case (ScalarCmp::LE) : return (x < y) || (x == y);
    case (ScalarCmp::GT) : return y < x;
    case (ScalarCmp::GE) : return (y < x) || (x == y);
    }
}

} // namespace snarkfront

#endif
<|endoftext|>


<commit_before>//
// This file is part of Easylogging++ samples
//
// Demonstration of multithreaded application using pthread
//
// Compile this program using (if using gcc-c++ or intel or clang++):
//     [icpc | g++ | clang++] ./pthread.cpp -o bin/./pthread.cpp.bin -D_ELPP_THREAD_SAFE -std=c++0x -pthread -Wall -Wextra
//
// Revision: 1.1
// @author mkhan3189
//

#include <pthread.h>
#include "easylogging++.h"

_INITIALIZE_EASYLOGGINGPP

struct Args {
  const char* thrId;
  el::Logger* logger;
}args;

void* write2(void* args){
  struct Args* a = (struct Args*)args;

  char* threadId = (char*)a->thrId;
  el::Logger* logger = (el::Logger*)a->logger;

  LOG(INFO) << "Writing from different function using macro [Thread #" << threadId << "]";

  logger->info("Info log from [Thread #%v]", threadId);
  logger->info("Info log");
  logger->verbose(2, "Verbose test [Thread #%v]", threadId);
  logger->verbose(2, "Verbose test");
  return NULL;
}

void *write(void* thrId){
  char* threadId = (char*)thrId;
  // Following line will be logged with every thread
  LOG(INFO) << "This standard log is written by [Thread #" << threadId << "]";

  // Following line will be logged with every thread only when --v=2 argument
  // is provided, i.e, ./bin/multithread_test.cpp.bin --v=2
  VLOG(2) << "This is verbose level 2 logging from [Thread #" << threadId << "]";

  // Following line will be logged only once from second running thread (which every runs second into
  // this line because of interval 2)
  LOG_EVERY_N(2, WARNING) << "This will be logged only once from thread who every reaches this line first. Currently running from [Thread #" << threadId << "]";

  for (int i = 1; i <= 10; ++i) {
     VLOG_IF(true, 2) << "Verbose condition [Thread #" << threadId << "]";
     VLOG_EVERY_N(2, 3) << "Verbose level 3 log every 4th time. This is at " << i << " from [Thread #" << threadId << "]";
  }

  // Following line will be logged once with every thread because of interval 1
  LOG_EVERY_N(1, INFO) << "This interval log will be logged with every thread, this one is from [Thread #" << threadId << "]";

  LOG_IF(strcmp(threadId, "2") == 0, INFO) << "This log is only for thread 2 and is ran by [Thread #" << threadId << "]";

  // Register 5 vague loggers
  for (int i = 1; i <= 5; ++i) {
     std::stringstream ss;
     ss << "logger" << i;
     el::Logger* logger = easyloggingpp::Loggers::getLogger(ss.str());
     LOG(INFO) << "Registered logger [" << *logger << "] [Thread #" << threadId << "]";
  }
  CLOG(INFO, "logger1") << "Logging using new logger [Thread #" << threadId << "]";
  CLOG(INFO, "no-logger") << "THIS SHOULD SAY LOGGER NOT REGISTERED YET [Thread #" << threadId << "]"; // << -- NOTE THIS!

  el::Logger* logger = el::Loggers::getLogger("default");
  logger->info("Info log from [Thread #%v]", threadId);

  // Check for log counters positions
  for (int i = 1; i <= 50; ++i) {
     LOG_EVERY_N(2, INFO) << "Counter pos: " << ELPP_COUNTER_POS << " [Thread #" << threadId << "]";
  }
  LOG_EVERY_N(2, INFO) << "Counter pos: " << ELPP_COUNTER_POS << " [Thread #" << threadId << "]";
  return NULL;
}

int main(int argc, char** argv)
{
     _START_EASYLOGGINGPP(argc, argv);
     pthread_t thread1, thread2, thread3, thread4;

    /* Create independent threads each of which will execute function */
     pthread_create( &thread1, NULL, write, (void*)"1");
     pthread_create( &thread2, NULL, write, (void*)"2");
     pthread_create( &thread3, NULL, write, (void*)"3");

     el::Logger* logger = el::Loggers::getLogger("default");
     args.thrId = "4";
     args.logger = logger;
     pthread_create( &thread4, NULL, write2, (void*)&args);

     pthread_join(thread1, NULL);
     pthread_join(thread2, NULL);
     pthread_join(thread3, NULL);
     pthread_join(thread4, NULL);

    exit(0);
}
<commit_msg>pthread sample update<commit_after>//
// This file is part of Easylogging++ samples
//
// Demonstration of multithreaded application using pthread
//
// Compile this program using (if using gcc-c++ or intel or clang++):
//     [icpc | g++ | clang++] ./pthread.cpp -o bin/./pthread.cpp.bin -D_ELPP_THREAD_SAFE -std=c++0x -pthread -Wall -Wextra
//
// Revision: 1.1
// @author mkhan3189
//

#include <pthread.h>
#include "easylogging++.h"

_INITIALIZE_EASYLOGGINGPP

struct Args {
  const char* thrId;
  el::Logger* logger;
}args;

void* write2(void* args){
  struct Args* a = (struct Args*)args;

  char* threadId = (char*)a->thrId;
  el::Logger* logger = (el::Logger*)a->logger;

  LOG(INFO) << "Writing from different function using macro [Thread #" << threadId << "]";

  logger->info("Info log from [Thread #%v]", threadId);
  logger->info("Info log");
  logger->verbose(2, "Verbose test [Thread #%v]", threadId);
  logger->verbose(2, "Verbose test");
  return NULL;
}

void *write(void* thrId){
  char* threadId = (char*)thrId;
  // Following line will be logged with every thread
  LOG(INFO) << "This standard log is written by [Thread #" << threadId << "]";

  // Following line will be logged with every thread only when --v=2 argument
  // is provided, i.e, ./bin/multithread_test.cpp.bin --v=2
  VLOG(2) << "This is verbose level 2 logging from [Thread #" << threadId << "]";

  // Following line will be logged only once from second running thread (which every runs second into
  // this line because of interval 2)
  LOG_EVERY_N(2, WARNING) << "This will be logged only once from thread who every reaches this line first. Currently running from [Thread #" << threadId << "]";

  for (int i = 1; i <= 10; ++i) {
     VLOG_IF(true, 2) << "Verbose condition [Thread #" << threadId << "]";
     VLOG_EVERY_N(2, 3) << "Verbose level 3 log every 4th time. This is at " << i << " from [Thread #" << threadId << "]";
  }

  // Following line will be logged once with every thread because of interval 1
  LOG_EVERY_N(1, INFO) << "This interval log will be logged with every thread, this one is from [Thread #" << threadId << "]";

  LOG_IF(strcmp(threadId, "2") == 0, INFO) << "This log is only for thread 2 and is ran by [Thread #" << threadId << "]";

  // Register 5 vague loggers
  for (int i = 1; i <= 5; ++i) {
     std::stringstream ss;
     ss << "logger" << i;
     el::Logger* logger = easyloggingpp::Loggers::getLogger(ss.str());
     LOG(INFO) << "Registered logger [" << *logger << "] [Thread #" << threadId << "]";
  }
  CLOG(INFO, "logger1") << "Logging using new logger [Thread #" << threadId << "]";
  CLOG(INFO, "no-logger") << "THIS SHOULD SAY LOGGER NOT REGISTERED YET [Thread #" << threadId << "]"; // << -- NOTE THIS!

  el::Logger* logger = el::Loggers::getLogger("default");
  logger->info("Info log from [Thread #%v]", threadId);

  // Check for log counters positions
  for (int i = 1; i <= 50; ++i) {
     LOG_EVERY_N(2, INFO) << "Counter pos: " << ELPP_COUNTER_POS << " [Thread #" << threadId << "]";
  }
  LOG_EVERY_N(2, INFO) << "Counter pos: " << ELPP_COUNTER_POS << " [Thread #" << threadId << "]";
  return NULL;
}

// If you wish you can define your own way to get thread ID
const char* getThreadId_CustomVersion(void) {
    return "MyThreadId";
}

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

     // Your thread ID specification
     el::CustomFormatSpecifier myThreadIdSpecifier("%mythreadId", getThreadId_CustomVersion);
     el::Helpers::installCustomFormatSpecifier(myThreadIdSpecifier);

     // Note your %mythreadId or built-in, both are logged
     el::Loggers::reconfigureAllLoggers(el::ConfigurationType::Format, "%datetime %level (%thread | %mythreadId) [%logger] [%func] [%loc] %msg");
     el::Loggers::reconfigureAllLoggers(el::Level::Verbose, el::ConfigurationType::Format, "%datetime %level-%vlevel (%thread | %mythreadId) [%logger] [%func] [%loc] %msg");

     pthread_t thread1, thread2, thread3, thread4;

    /* Create independent threads each of which will execute function */
     pthread_create( &thread1, NULL, write, (void*)"1");
     pthread_create( &thread2, NULL, write, (void*)"2");
     pthread_create( &thread3, NULL, write, (void*)"3");

     el::Logger* logger = el::Loggers::getLogger("default");
     args.thrId = "4";
     args.logger = logger;
     pthread_create( &thread4, NULL, write2, (void*)&args);

     pthread_join(thread1, NULL);
     pthread_join(thread2, NULL);
     pthread_join(thread3, NULL);
     pthread_join(thread4, NULL);

    exit(0);
}
<|endoftext|>


<commit_before>// Example of bar charts
// Author: Rene Brun
void hbars()
{
   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   dir.ReplaceAll("hbars.C","../tree/cernbuild.C");
   dir.ReplaceAll("/./","/");
   if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
   TFile *f = (TFile*)gROOT->ProcessLineFast("cernbuild(1)");
   if (!f) return;
   TTree *T = (TTree*)f->Get("T");
   T->SetFillColor(45);
   TCanvas *c1 = new TCanvas("c1","histograms with bars",10,10,800,900);
   c1->SetFillColor(42);
   c1->Divide(1,2);

   //horizontal bar chart
   c1->cd(1); gPad->SetGrid(); gPad->SetLogx(); gPad->SetFrameFillColor(33);
   T->Draw("Nation","","hbar2");

   //vertical bar chart
   c1->cd(2); gPad->SetGrid(); gPad->SetFrameFillColor(33);
   T->Draw("Division>>hDiv","","goff");
   TH1F *hDiv   = (TH1F*)gDirectory->Get("hDiv");
   hDiv->SetStats(0);
   TH1F *hDivFR = (TH1F*)hDiv->Clone("hDivFR");
   T->Draw("Division>>hDivFR","Nation==\"FR\"","goff");
   hDiv->SetBarWidth(0.45);
   hDiv->SetBarOffset(0.1);
   hDiv->SetFillColor(49);
   TH1 *h1 = hDiv->DrawCopy("bar2");
   hDivFR->SetBarWidth(0.4);
   hDivFR->SetBarOffset(0.55);
   hDivFR->SetFillColor(50);
   TH1 *h2 = hDivFR->DrawCopy("bar2,same");

   TLegend *legend = new TLegend(0.55,0.65,0.76,0.82);
   legend->AddEntry(h1,"All nations","f");
   legend->AddEntry(h2,"French only","f");
   legend->Draw();

   c1->cd();
   delete f;
}
<commit_msg>- Returns the TCanvas in order to be used by the documentation.<commit_after>// Example of bar charts
// Author: Rene Brun
{
   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   dir.ReplaceAll("hbars.C","../tree/cernbuild.C");
   dir.ReplaceAll("/./","/");
   if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
   TFile *f = (TFile*)gROOT->ProcessLineFast("cernbuild(1)");
   if (!f) return;
   TTree *T = (TTree*)f->Get("T");
   T->SetFillColor(45);
   TCanvas *c1 = new TCanvas("c1","histograms with bars",700,800);
   c1->SetFillColor(42);
   c1->Divide(1,2);

   //horizontal bar chart
   c1->cd(1); gPad->SetGrid(); gPad->SetLogx(); gPad->SetFrameFillColor(33);
   T->Draw("Nation","","hbar2");

   //vertical bar chart
   c1->cd(2); gPad->SetGrid(); gPad->SetFrameFillColor(33);
   T->Draw("Division>>hDiv","","goff");
   TH1F *hDiv   = (TH1F*)gDirectory->Get("hDiv");
   hDiv->SetStats(0);
   TH1F *hDivFR = (TH1F*)hDiv->Clone("hDivFR");
   T->Draw("Division>>hDivFR","Nation==\"FR\"","goff");
   hDiv->SetBarWidth(0.45);
   hDiv->SetBarOffset(0.1);
   hDiv->SetFillColor(49);
   TH1 *h1 = hDiv->DrawCopy("bar2");
   hDivFR->SetBarWidth(0.4);
   hDivFR->SetBarOffset(0.55);
   hDivFR->SetFillColor(50);
   TH1 *h2 = hDivFR->DrawCopy("bar2,same");

   TLegend *legend = new TLegend(0.55,0.65,0.76,0.82);
   legend->AddEntry(h1,"All nations","f");
   legend->AddEntry(h2,"French only","f");
   legend->Draw();

   c1->cd();
   delete f;
   return c1;
}
<|endoftext|>


<commit_before>#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <cmath>
#include <sstream>
#include <string>
#include <vector>

#include "factorencoder.hh"

int read_words(const char* fname,
                 std::map<std::string, long> &words)
{
    std::ifstream vocabfile(fname);
    if (!vocabfile) return -1;

    std::string line, word;
    long count;
    while (getline(vocabfile, line)) {
        std::stringstream ss(line);
        ss >> count;
        ss >> word;
        words[word] = count;
    }
    vocabfile.close();

    return words.size();
}

void resegment_words(const std::map<std::string, long> &words,
                        const std::map<std::string, double> &vocab,
                        std::map<std::string, double> &new_freqs,
                        const int maxlen)
{
    for(std::map<std::string, long>::const_iterator iter = words.begin(); iter != words.end(); ++iter) {

        std::vector<std::string> best_path;
        viterbi(vocab, maxlen, iter->first, best_path);

        if (best_path.size() == 0) {
            std::cerr << "warning, no segmentation for word: " << iter->first << std::endl;
            exit(0);
        }

        // Update statistics
        for (int i=0; i<best_path.size(); i++)
            new_freqs[best_path[i]] += double(iter->second);
    }
}

void resegment_words_w_diff(const std::map<std::string, long> &words,
                                const std::map<std::string, double> &vocab,
                                std::map<std::string, double> &new_freqs,
                                std::map<std::string, std::map<std::string, double> > &diffs,
                                const int maxlen)
{
    std::map<std::string, double> hypo_vocab = vocab;
    for (std::map<std::string, long>::const_iterator iter = words.begin(); iter != words.end(); ++iter) {

        std::vector<std::string> best_path;
        viterbi(vocab, maxlen, iter->first, best_path);

        if (best_path.size() == 0) {
            std::cerr << "warning, no segmentation for word: " << iter->first << std::endl;
            exit(0);
        }

        // Update statistics
        for (int i=0; i<best_path.size(); i++) {
            new_freqs[best_path[i]] += double(iter->second);
        }

        // Hypothesize what the segmentation would be if some subword didn't exist
        for (std::map<std::string, std::map<std::string, double> >::iterator iter = diffs.begin(); iter != diffs.end(); ++iter) {
            for (int i=0; i<best_path.size(); i++) {
                if (best_path[i] == iter->first) {
                    double stored_value = hypo_vocab.at(best_path[i]);
                    hypo_vocab.erase(best_path[i]);
                    std::vector<std::string> hypo_path;
                    viterbi(hypo_vocab, maxlen, iter->first, hypo_path);
                    for (int ib=0; ib<best_path.size(); ib++)
                        diffs[iter->first][best_path[ib]] -= 1;
                    for (int ih=0; ih<hypo_path.size(); ih++)
                        diffs[iter->first][hypo_path[ih]] += 1;
                    hypo_vocab[best_path[i]] = stored_value;
                    break;
                }
            }
        }
    }
}

double get_sum(const std::map<std::string, double> &vocab)
{
    double total = 0.0;
    for(std::map<std::string, double>::const_iterator iter = vocab.begin(); iter != vocab.end(); ++iter) {
        total += iter->second;
    }
    return total;
}

double get_cost(const std::map<std::string, double> &vocab,
                  double densum)
{
    double total = 0.0;
    densum = log2(densum);
    for(std::map<std::string, double>::const_iterator iter = vocab.begin(); iter != vocab.end(); ++iter) {
        total += iter->second * (log2(iter->second)-densum);
    }
    return total;
}

void freqs_to_logprobs(std::map<std::string, double> &vocab,
                          double densum)
{
    for(std::map<std::string, double>::iterator iter = vocab.begin(); iter != vocab.end(); ++iter)
        vocab[iter->first] = (log2(iter->second)-densum);
}

void cutoff(std::map<std::string, double> &vocab,
             double limit)
{
    for(std::map<std::string, double>::iterator iter = vocab.begin(); iter != vocab.end(); ++iter)
        if (vocab[iter->first] <= limit) vocab.erase(iter->first);
}

bool ascending_sort(std::pair<std::string, double> i,std::pair<std::string, double> j) { return (i.second < j.second); }

void sort_vocab(std::map<std::string, double> &vocab,
                  std::vector<std::pair<std::string, double> > &sorted_vocab)
{
    sorted_vocab.clear();
    for (std::map<std::string,double>::const_iterator it = vocab.begin(); it != vocab.end(); it++) {
        std::pair<std::string, double> curr_pair(it->first, it->second);
        sorted_vocab.push_back(curr_pair);
    }
    std::sort(sorted_vocab.begin(), sorted_vocab.end(), ascending_sort);
}

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

    if (argc != 3) {
        std::cerr << "usage: " << argv[0] << " <vocabulary> <words>" << std::endl;
        exit(0);
    }

    int maxlen;
    std::map<std::string, double> vocab;
    std::map<std::string, double> new_morph_freqs;
    std::map<std::string, long> words;

    std::cerr << "Reading vocabulary " << argv[1] << std::endl;
    int retval = read_vocab(argv[1], vocab, maxlen);
    if (retval < 0) {
        std::cerr << "something went wrong reading vocabulary" << std::endl;
        exit(0);
    }
    std::cerr << "\t" << "size: " << vocab.size() << std::endl;
    std::cerr << "\t" << "maximum string length: " << maxlen << std::endl;

    std::cerr << "Reading word list" << std::endl;

    read_words(argv[2], words);

    std::cerr << "\t\t\t" << "vocabulary size: " << vocab.size() << std::endl;

    std::cerr << "Iterating" << std::endl;
    const int n_cutoff_iters = 2;
    int cutoffs[n_cutoff_iters] = { 0, 50 };
    for (int i=0; i<n_cutoff_iters; i++) {
        resegment_words(words, vocab, new_morph_freqs, maxlen);
        double densum = get_sum(new_morph_freqs);
        double cost = get_cost(new_morph_freqs, densum);
        std::cerr << "cost: " << cost << std::endl;
        vocab.swap(new_morph_freqs);
        new_morph_freqs.clear();

        cutoff(vocab, cutoffs[i]);
        std::cerr << "\tcutoff: " << cutoffs[i] << "\t" << "vocabulary size: " << vocab.size() << std::endl;
        densum = get_sum(vocab);
        freqs_to_logprobs(vocab, densum);
    }

    std::vector<std::pair<std::string, double> > sorted_vocab;
    resegment_words(words, vocab, new_morph_freqs, maxlen);
    sort_vocab(new_morph_freqs, sorted_vocab);
    std::map<std::string, std::map<std::string, double> > diffs;
    for (int i=0; i<100; i++) {
        std::pair<std::string, double> &subword = sorted_vocab[i];
//        std::cout << subword.first << "\t" << subword.second << std::endl;
        std::map<std::string, double> emptymap;
        diffs[subword.first] = emptymap;
    }

    resegment_words_w_diff(words, vocab, new_morph_freqs, diffs, maxlen);
    double densum = get_sum(new_morph_freqs);
    double cost = get_cost(new_morph_freqs, densum);
    std::cerr << "cost: " << cost << std::endl;
    for (std::map<std::string, std::map<std::string, double> >::iterator iter = diffs.begin(); iter != diffs.end(); ++iter) {
        for (std::map<std::string, double>::iterator iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2)
            new_morph_freqs[iter2->first] += iter2->second;

        double hypo_densum = get_sum(new_morph_freqs);
        double hypo_cost = get_cost(new_morph_freqs, hypo_densum);
        std::cerr << "word: " << iter->first << "\t" << "change in cost after removal: " << hypo_cost-cost << std::endl;

        for (std::map<std::string, double>::iterator iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2)
            new_morph_freqs[iter2->first] -= iter2->second;
    }

    exit(1);
}

<commit_msg>fixed/improved the loop for collecting hypothesis diff counts.<commit_after>#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <cmath>
#include <sstream>
#include <string>
#include <vector>

#include "factorencoder.hh"

int read_words(const char* fname,
                 std::map<std::string, long> &words)
{
    std::ifstream vocabfile(fname);
    if (!vocabfile) return -1;

    std::string line, word;
    long count;
    while (getline(vocabfile, line)) {
        std::stringstream ss(line);
        ss >> count;
        ss >> word;
        words[word] = count;
    }
    vocabfile.close();

    return words.size();
}

void resegment_words(const std::map<std::string, long> &words,
                        const std::map<std::string, double> &vocab,
                        std::map<std::string, double> &new_freqs,
                        const int maxlen)
{
    for(std::map<std::string, long>::const_iterator iter = words.begin(); iter != words.end(); ++iter) {

        std::vector<std::string> best_path;
        viterbi(vocab, maxlen, iter->first, best_path);

        if (best_path.size() == 0) {
            std::cerr << "warning, no segmentation for word: " << iter->first << std::endl;
            exit(0);
        }

        // Update statistics
        for (int i=0; i<best_path.size(); i++)
            new_freqs[best_path[i]] += double(iter->second);
    }
}

void resegment_words_w_diff(const std::map<std::string, long> &words,
                                const std::map<std::string, double> &vocab,
                                std::map<std::string, double> &new_freqs,
                                std::map<std::string, std::map<std::string, double> > &diffs,
                                const int maxlen)
{
    std::map<std::string, double> hypo_vocab = vocab;
    for (std::map<std::string, long>::const_iterator iter = words.begin(); iter != words.end(); ++iter) {

        std::vector<std::string> best_path;
        viterbi(vocab, maxlen, iter->first, best_path);

        if (best_path.size() == 0) {
            std::cerr << "warning, no segmentation for word: " << iter->first << std::endl;
            exit(0);
        }

        // Update statistics
        std::map<std::string, double> best_path_types;
        for (int i=0; i<best_path.size(); i++) {
            new_freqs[best_path[i]] += double(iter->second);
            best_path_types[best_path[i]] = 0.0;
        }

        // Hypothesize what the segmentation would be if some subword didn't exist
        for (std::map<std::string, double>::iterator iter = best_path_types.begin(); iter != best_path_types.end(); ++iter) {
            if (diffs.find(iter->first) != diffs.end()) {
                double stored_value = hypo_vocab.at(iter->first);
                hypo_vocab.erase(iter->first);
                std::vector<std::string> hypo_path;
                viterbi(hypo_vocab, maxlen, iter->first, hypo_path);
                for (int ib=0; ib<best_path.size(); ib++)
                    diffs[iter->first][best_path[ib]] -= 1.0;
                for (int ih=0; ih<hypo_path.size(); ih++)
                    diffs[iter->first][hypo_path[ih]] += 1.0;
                hypo_vocab[iter->first] = stored_value;
            }
        }
    }
}

double get_sum(const std::map<std::string, double> &vocab)
{
    double total = 0.0;
    for(std::map<std::string, double>::const_iterator iter = vocab.begin(); iter != vocab.end(); ++iter) {
        total += iter->second;
    }
    return total;
}

double get_cost(const std::map<std::string, double> &vocab,
                  double densum)
{
    double total = 0.0;
    densum = log2(densum);
    for(std::map<std::string, double>::const_iterator iter = vocab.begin(); iter != vocab.end(); ++iter) {
        total += iter->second * (log2(iter->second)-densum);
    }
    return total;
}

void freqs_to_logprobs(std::map<std::string, double> &vocab,
                          double densum)
{
    for(std::map<std::string, double>::iterator iter = vocab.begin(); iter != vocab.end(); ++iter)
        vocab[iter->first] = (log2(iter->second)-densum);
}

void cutoff(std::map<std::string, double> &vocab,
             double limit)
{
    for(std::map<std::string, double>::iterator iter = vocab.begin(); iter != vocab.end(); ++iter)
        if (vocab[iter->first] <= limit) vocab.erase(iter->first);
}

bool ascending_sort(std::pair<std::string, double> i,std::pair<std::string, double> j) { return (i.second < j.second); }

void sort_vocab(std::map<std::string, double> &vocab,
                  std::vector<std::pair<std::string, double> > &sorted_vocab)
{
    sorted_vocab.clear();
    for (std::map<std::string,double>::const_iterator it = vocab.begin(); it != vocab.end(); it++) {
        std::pair<std::string, double> curr_pair(it->first, it->second);
        sorted_vocab.push_back(curr_pair);
    }
    std::sort(sorted_vocab.begin(), sorted_vocab.end(), ascending_sort);
}

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

    if (argc != 3) {
        std::cerr << "usage: " << argv[0] << " <vocabulary> <words>" << std::endl;
        exit(0);
    }

    int maxlen;
    std::map<std::string, double> vocab;
    std::map<std::string, double> new_morph_freqs;
    std::map<std::string, long> words;

    std::cerr << "Reading vocabulary " << argv[1] << std::endl;
    int retval = read_vocab(argv[1], vocab, maxlen);
    if (retval < 0) {
        std::cerr << "something went wrong reading vocabulary" << std::endl;
        exit(0);
    }
    std::cerr << "\t" << "size: " << vocab.size() << std::endl;
    std::cerr << "\t" << "maximum string length: " << maxlen << std::endl;

    std::cerr << "Reading word list" << std::endl;

    read_words(argv[2], words);

    std::cerr << "\t\t\t" << "vocabulary size: " << vocab.size() << std::endl;

    std::cerr << "Iterating" << std::endl;
    const int n_cutoff_iters = 2;
    int cutoffs[n_cutoff_iters] = { 0, 50 };
    for (int i=0; i<n_cutoff_iters; i++) {
        resegment_words(words, vocab, new_morph_freqs, maxlen);
        double densum = get_sum(new_morph_freqs);
        double cost = get_cost(new_morph_freqs, densum);
        std::cerr << "cost: " << cost << std::endl;
        vocab.swap(new_morph_freqs);
        new_morph_freqs.clear();

        cutoff(vocab, cutoffs[i]);
        std::cerr << "\tcutoff: " << cutoffs[i] << "\t" << "vocabulary size: " << vocab.size() << std::endl;
        densum = get_sum(vocab);
        freqs_to_logprobs(vocab, densum);
    }

    std::vector<std::pair<std::string, double> > sorted_vocab;
    resegment_words(words, vocab, new_morph_freqs, maxlen);
    sort_vocab(new_morph_freqs, sorted_vocab);
    std::map<std::string, std::map<std::string, double> > diffs;
    for (int i=0; i<sorted_vocab.size(); i++) {
//    for (int i=0; i<1000; i++) {
    std::pair<std::string, double> &subword = sorted_vocab[i];
//        std::cout << subword.first << "\t" << subword.second << std::endl;
        std::map<std::string, double> emptymap;
        diffs[subword.first] = emptymap;
    }

    new_morph_freqs.clear();
    resegment_words_w_diff(words, vocab, new_morph_freqs, diffs, maxlen);
    double densum = get_sum(new_morph_freqs);
    double cost = get_cost(new_morph_freqs, densum);
    std::cerr << "cost: " << cost << std::endl;
    for (std::map<std::string, std::map<std::string, double> >::iterator iter = diffs.begin(); iter != diffs.end(); ++iter) {
        for (std::map<std::string, double>::iterator iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2)
            new_morph_freqs[iter2->first] += iter2->second;

        double hypo_densum = get_sum(new_morph_freqs);
        double hypo_cost = get_cost(new_morph_freqs, hypo_densum);
        std::cout << "word: " << iter->first << "\t" << "change in cost after removal: " << hypo_cost-cost << std::endl;

        for (std::map<std::string, double>::iterator iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2)
            new_morph_freqs[iter2->first] -= iter2->second;
    }

    exit(1);
}

<|endoftext|>