<commit_before>#include "Session.h"

#include <fstream>
#include <string>
#include <iomanip>

#include <SmurffCpp/Version.h>

#include <SmurffCpp/Utils/omp_util.h>
#include <SmurffCpp/Utils/Distribution.h>
#include <SmurffCpp/Utils/MatrixUtils.h>
#include <SmurffCpp/Utils/counters.h>
#include <SmurffCpp/Utils/Error.h>

#include <SmurffCpp/DataMatrices/DataCreator.h>
#include <SmurffCpp/Priors/PriorFactory.h>

#include <SmurffCpp/result.h>
#include <SmurffCpp/StatusItem.h>

using namespace smurff;

void Session::setRestoreFromRootPath(std::string rootPath)
{
   // open root file
   m_rootFile = std::make_shared<RootFile>(rootPath);

   //restore config
   m_rootFile->restoreConfig(m_config);

   m_config.validate();

   //base functionality
   setFromBase();
}

void Session::setRestoreFromConfig(const Config& cfg, std::string rootPath)
{
   cfg.validate();

   // assign config
   m_config = cfg;

   // open root file
   m_rootFile = std::make_shared<RootFile>(rootPath);

   //base functionality
   setFromBase();
}

void Session::setCreateFromConfig(const Config& cfg)
{
   cfg.validate();

   // assign config
   m_config = cfg;

   if (m_config.getSaveFreq() || m_config.getCheckpointFreq())
   {

       // create root file
       m_rootFile = std::make_shared<RootFile>(m_config.getSavePrefix(), m_config.getSaveExtension());

       //save config
       m_rootFile->saveConfig(m_config);

       //flush record about options.ini
       m_rootFile->flushLast();
   }

    //base functionality
    setFromBase();
}

void Session::setFromBase()
{
   std::shared_ptr<Session> this_session = shared_from_this();

   // initialize pred

   if (m_config.getClassify())
      m_pred->setThreshold(m_config.getThreshold());

   if (m_config.getTest())
      m_pred->set(m_config.getTest());

   // initialize data

   data_ptr = m_config.getTrain()->create(std::make_shared<DataCreator>(this_session));

   // initialize priors

   std::shared_ptr<IPriorFactory> priorFactory = this->create_prior_factory();
   for (std::size_t i = 0; i < m_config.getPriorTypes().size(); i++)
      this->addPrior(priorFactory->create_prior(this_session, i));
}

void Session::init()
{
   //init omp
   threads::init(m_config.getVerbose(), m_config.getNumThreads());

   //initialize random generator
   initRng();

   //initialize train matrix (centring and noise model)
   data().init();

   //initialize model (samples)
   model().init(m_config.getNumLatent(), data().dim(), m_config.getModelInitType());

   //initialize priors
   for(auto &p : m_priors)
      p->init();

   //write header to status file
   if (m_config.getCsvStatus().size())
   {
      std::ofstream f(m_config.getCsvStatus(), std::ofstream::out);
      THROWERROR_ASSERT_MSG(f, "Could not open status csv file: " + m_config.getCsvStatus());
      f << StatusItem::getCsvHeader() << std::endl;
   }

   //write info to console
   if (m_config.getVerbose())
      info(std::cout, "");

   //restore session (model, priors)
   bool resume = restore(m_iter);

   //print session status to console
   if (m_config.getVerbose())
   {
      printStatus(std::cout, resume);
   }

   is_init = true;
}

void Session::run()
{
   init();
   while (step());
}

bool Session::step()
{
   THROWERROR_ASSERT_MSG(is_init, "Session::init() needs to be called before ::step()")

   // go to the next iteration
   m_iter++;

   bool isStep = m_iter < m_config.getBurnin() + m_config.getNSamples();

   if (isStep)
   {
      //init omp
      threads::enable();

      THROWERROR_ASSERT(is_init);

      auto starti = tick();
      BaseSession::step();
      auto endi = tick();

      //WARNING: update is an expensive operation because of sort (when calculating AUC)
      m_pred->update(m_model, m_iter < m_config.getBurnin());

      m_secs_per_iter = endi - starti;

      printStatus(std::cout);

      save(m_iter);

      threads::disable();
   }

   return isStep;
}

std::ostream& Session::info(std::ostream &os, std::string indent)
{
   os << indent << name << " {\n";

   BaseSession::info(os, indent);

   os << indent << "  Version: " << smurff::SMURFF_VERSION << "\n" ;
   os << indent << "  Iterations: " << m_config.getBurnin() << " burnin + " << m_config.getNSamples() << " samples\n";

   if (m_config.getSaveFreq() != 0 || m_config.getCheckpointFreq() != 0)
   {
      if (m_config.getSaveFreq() > 0)
      {
          os << indent << "  Save model: every " << m_config.getSaveFreq() << " iteration\n";
      }
      else if (m_config.getSaveFreq() < 0)
      {
          os << indent << "  Save model after last iteration\n";
      }

      if (m_config.getCheckpointFreq() > 0)
      {
          os << indent << "  Checkpoint state: every " << m_config.getCheckpointFreq() << " seconds\n";
      }

      os << indent << "  Save prefix: " << m_config.getSavePrefix() << "\n";
      os << indent << "  Save extension: " << m_config.getSaveExtension() << "\n";
   }
   else
   {
      os << indent << "  Save model: never\n";
   }

   os << indent << "}\n";
   return os;
}

void Session::save(int iteration)
{
   //do not save if 'never save' mode is selected
   if (!m_config.getSaveFreq() &&
       !m_config.getCheckpointFreq() &&
       !m_config.getCsvStatus().size())
      return;

   std::int32_t isample = iteration - m_config.getBurnin() + 1;

   //save if checkpoint threshold overdue
   if (m_config.getCheckpointFreq() && (tick() - m_lastCheckpointTime) >= m_config.getCheckpointFreq())
   {
      std::int32_t icheckpoint = iteration + 1;

      //save this iteration
      std::shared_ptr<StepFile> stepFile = m_rootFile->createCheckpointStepFile(icheckpoint);
      saveInternal(stepFile);

      //remove previous iteration if required (initial m_lastCheckpointIter is -1 which means that it does not exist)
      if (m_lastCheckpointIter >= 0)
      {
         std::int32_t icheckpointPrev = m_lastCheckpointIter + 1;

         //remove previous iteration
         m_rootFile->removeCheckpointStepFile(icheckpointPrev);

         //flush last item in a root file
         m_rootFile->flushLast();
      }

      //upddate counters
      m_lastCheckpointTime = tick();
      m_lastCheckpointIter = iteration;
   }

   //save model during sampling stage
   if (m_config.getSaveFreq() && isample > 0)
   {
      //save_freq > 0: check modulo - do not save if not a save iteration
      if (m_config.getSaveFreq() > 0 && (isample % m_config.getSaveFreq()) != 0)
      {
          // don't save
      }
      //save_freq < 0: save last iter - do not save if (final model) mode is selected and not a final iteration
      else if (m_config.getSaveFreq() < 0 && isample < m_config.getNSamples())
      {
          // don't save
      }
      else
      {
          //do save this iteration
          std::shared_ptr<StepFile> stepFile = m_rootFile->createSampleStepFile(isample);
          saveInternal(stepFile);
      }
   }
}

void Session::saveInternal(std::shared_ptr<StepFile> stepFile)
{
   if (m_config.getVerbose())
   {
      std::cout << "-- Saving model, predictions,... into '" << stepFile->getStepFileName() << "'." << std::endl;
   }

   BaseSession::save(stepFile);

   //flush last item in a root file
   m_rootFile->flushLast();
}

bool Session::restore(int& iteration)
{
   std::shared_ptr<StepFile> stepFile = nullptr;
   if (m_rootFile)
   {
       stepFile = m_rootFile->openLastStepFile();
   }

   if (!stepFile)
   {
      //if there is nothing to restore - start from initial iteration
      iteration = -1;

      //to keep track at what time we last checkpointed
      m_lastCheckpointTime = tick();
      m_lastCheckpointIter = -1;
      return false;
   }
   else
   {
      if (m_config.getVerbose())
      {
         std::cout << "-- Restoring model, predictions,... from '" << stepFile->getStepFileName() << "'." << std::endl;
      }

      BaseSession::restore(stepFile);

      //restore last iteration index
      if (stepFile->getCheckpoint())
      {
         iteration = stepFile->getIsample() - 1; //restore original state

         //to keep track at what time we last checkpointed
         m_lastCheckpointTime = tick();
         m_lastCheckpointIter = iteration;
      }
      else
      {
         iteration = stepFile->getIsample() + m_config.getBurnin() - 1; //restore original state

         //to keep track at what time we last checkpointed
         m_lastCheckpointTime = tick();
         m_lastCheckpointIter = iteration;
      }

      return true;
   }
}

std::shared_ptr<StatusItem> Session::getStatus() const
{
    std::shared_ptr<StatusItem> ret = std::make_shared<StatusItem>();

    if (m_iter < 0)
    {
        ret->phase = "Initial";
        ret->iter = m_iter + 1;
        ret->phase_iter = 0;
    }
    else if (m_iter < m_config.getBurnin())
    {
        ret->phase = "Burnin";
        ret->iter = m_iter + 1;
        ret->phase_iter = m_config.getBurnin();
    }
    else
    {
        ret->phase = "Sample";
        ret->iter = m_iter - m_config.getBurnin() + 1;
        ret->phase_iter = m_config.getNSamples();
    }

    for (int i = 0; i < (int)model().nmodes(); ++i)
    {
        ret->model_norms.push_back(model().U(i).norm());
    }

    ret->train_rmse = data().train_rmse(model());

    ret->rmse_avg = m_pred->rmse_avg;
    ret->rmse_1sample = m_pred->rmse_1sample;

    ret->auc_avg = m_pred->auc_avg;
    ret->auc_1sample = m_pred->auc_1sample;

    ret->elapsed_iter = m_secs_per_iter;
    ret->nnz_per_sec = (double)(data().nnz()) / m_secs_per_iter;
    ret->samples_per_sec = (double)(model().nsamples()) / m_secs_per_iter;

    return ret;
}

void Session::printStatus(std::ostream& output, bool resume)
{
   if(!m_config.getVerbose() &&
      !m_config.getCsvStatus().size())
      return;

   auto status_item = getStatus();

   std::string resumeString = resume ? "Continue from " : std::string();

   if (m_config.getVerbose() > 0)
   {
       if (m_iter < 0)
       {
           output << " ====== Initial phase ====== " << std::endl;
       }
       else if (m_iter < m_config.getBurnin() && m_iter == 0)
       {
           output << " ====== Sampling (burning phase) ====== " << std::endl;
       }
       else if (m_iter == m_config.getBurnin())
       {
           output << " ====== Burn-in complete, averaging samples ====== " << std::endl;
       }

       output << resumeString
           << status_item->phase
           << " "
           << std::setfill(' ') << std::setw(3) << status_item->iter
           << "/"
           << std::setfill(' ') << std::setw(3) << status_item->phase_iter
           << ": RMSE: "
           << std::fixed << std::setprecision(4) << status_item->rmse_avg
           << " (1samp: "
           << std::fixed << std::setprecision(4) << status_item->rmse_1sample
           << ")";

       if (m_config.getClassify())
       {
           output << " AUC:"
               << std::fixed << std::setprecision(4) << status_item->auc_avg
               << " (1samp: "
               << std::fixed << std::setprecision(4) << status_item->auc_1sample
               << ")"
               << std::endl;
       }

       output << "  U:[";
       for(double n: status_item->model_norms)
       {
           output << std::scientific << std::setprecision(2) << n << ", ";
       }
       output << "] [took: "
           << std::fixed << std::setprecision(1) << status_item->elapsed_iter
           << "s]"
           << std::endl;

       if (m_config.getVerbose() > 1)
       {
           output << std::fixed << std::setprecision(4) << "  RMSE train: " << status_item->train_rmse << std::endl;
           output << "  Priors:" << std::endl;

           for(const auto &p : m_priors)
               p->status(output, "     ");

           output << "  Model:" << std::endl;
           model().status(output, "    ");
           output << "  Noise:" << std::endl;
           data().status(output, "    ");
       }

       if (m_config.getVerbose() > 2)
       {
           output << "  Compute Performance: " << status_item->samples_per_sec << " samples/sec, " << status_item->nnz_per_sec << " nnz/sec" << std::endl;
       }
   }

   if (m_config.getCsvStatus().size())
   {
      std::ofstream f(m_config.getCsvStatus(), std::ofstream::out | std::ofstream::app);
      THROWERROR_ASSERT_MSG(f, "Could not open status csv file: " + m_config.getCsvStatus());
      f << status_item->asCsvString() << std::endl;
   }
}

std::string StatusItem::getCsvHeader()
{
   return "phase;iter;phase_len;rmse_avg;rmse_1samp;train_rmse;auc_avg;auc_1samp;elapsed";
}

std::string StatusItem::asCsvString() const
{
    char ret[1024];
    snprintf(ret, 1024, "%s;%d;%d;%.4f;%.4f;%.4f;%.4f;:%.4f;%0.1f",
                  phase.c_str(), iter, phase_iter, rmse_avg, rmse_1sample, train_rmse,
                  auc_1sample, auc_avg, elapsed_iter);

    return ret;
}

void Session::initRng()
{
   //init random generator
   if (m_config.getRandomSeedSet())
      init_bmrng(m_config.getRandomSeed());
   else
      init_bmrng();
}

std::shared_ptr<IPriorFactory> Session::create_prior_factory() const
{
   return std::make_shared<PriorFactory>();
}
<commit_msg>Print how long it took to save<commit_after>#include "Session.h"

#include <fstream>
#include <string>
#include <iomanip>

#include <SmurffCpp/Version.h>

#include <SmurffCpp/Utils/omp_util.h>
#include <SmurffCpp/Utils/Distribution.h>
#include <SmurffCpp/Utils/MatrixUtils.h>
#include <SmurffCpp/Utils/counters.h>
#include <SmurffCpp/Utils/Error.h>

#include <SmurffCpp/DataMatrices/DataCreator.h>
#include <SmurffCpp/Priors/PriorFactory.h>

#include <SmurffCpp/result.h>
#include <SmurffCpp/StatusItem.h>

using namespace smurff;

void Session::setRestoreFromRootPath(std::string rootPath)
{
   // open root file
   m_rootFile = std::make_shared<RootFile>(rootPath);

   //restore config
   m_rootFile->restoreConfig(m_config);

   m_config.validate();

   //base functionality
   setFromBase();
}

void Session::setRestoreFromConfig(const Config& cfg, std::string rootPath)
{
   cfg.validate();

   // assign config
   m_config = cfg;

   // open root file
   m_rootFile = std::make_shared<RootFile>(rootPath);

   //base functionality
   setFromBase();
}

void Session::setCreateFromConfig(const Config& cfg)
{
   cfg.validate();

   // assign config
   m_config = cfg;

   if (m_config.getSaveFreq() || m_config.getCheckpointFreq())
   {

       // create root file
       m_rootFile = std::make_shared<RootFile>(m_config.getSavePrefix(), m_config.getSaveExtension());

       //save config
       m_rootFile->saveConfig(m_config);

       //flush record about options.ini
       m_rootFile->flushLast();
   }

    //base functionality
    setFromBase();
}

void Session::setFromBase()
{
   std::shared_ptr<Session> this_session = shared_from_this();

   // initialize pred

   if (m_config.getClassify())
      m_pred->setThreshold(m_config.getThreshold());

   if (m_config.getTest())
      m_pred->set(m_config.getTest());

   // initialize data

   data_ptr = m_config.getTrain()->create(std::make_shared<DataCreator>(this_session));

   // initialize priors

   std::shared_ptr<IPriorFactory> priorFactory = this->create_prior_factory();
   for (std::size_t i = 0; i < m_config.getPriorTypes().size(); i++)
      this->addPrior(priorFactory->create_prior(this_session, i));
}

void Session::init()
{
   //init omp
   threads::init(m_config.getVerbose(), m_config.getNumThreads());

   //initialize random generator
   initRng();

   //initialize train matrix (centring and noise model)
   data().init();

   //initialize model (samples)
   model().init(m_config.getNumLatent(), data().dim(), m_config.getModelInitType());

   //initialize priors
   for(auto &p : m_priors)
      p->init();

   //write header to status file
   if (m_config.getCsvStatus().size())
   {
      std::ofstream f(m_config.getCsvStatus(), std::ofstream::out);
      THROWERROR_ASSERT_MSG(f, "Could not open status csv file: " + m_config.getCsvStatus());
      f << StatusItem::getCsvHeader() << std::endl;
   }

   //write info to console
   if (m_config.getVerbose())
      info(std::cout, "");

   //restore session (model, priors)
   bool resume = restore(m_iter);

   //print session status to console
   if (m_config.getVerbose())
   {
      printStatus(std::cout, resume);
   }

   is_init = true;
}

void Session::run()
{
   init();
   while (step());
}

bool Session::step()
{
   THROWERROR_ASSERT_MSG(is_init, "Session::init() needs to be called before ::step()")

   // go to the next iteration
   m_iter++;

   bool isStep = m_iter < m_config.getBurnin() + m_config.getNSamples();

   if (isStep)
   {
      //init omp
      threads::enable();

      THROWERROR_ASSERT(is_init);

      auto starti = tick();
      BaseSession::step();
      auto endi = tick();

      //WARNING: update is an expensive operation because of sort (when calculating AUC)
      m_pred->update(m_model, m_iter < m_config.getBurnin());

      m_secs_per_iter = endi - starti;

      printStatus(std::cout);

      save(m_iter);

      threads::disable();
   }

   return isStep;
}

std::ostream& Session::info(std::ostream &os, std::string indent)
{
   os << indent << name << " {\n";

   BaseSession::info(os, indent);

   os << indent << "  Version: " << smurff::SMURFF_VERSION << "\n" ;
   os << indent << "  Iterations: " << m_config.getBurnin() << " burnin + " << m_config.getNSamples() << " samples\n";

   if (m_config.getSaveFreq() != 0 || m_config.getCheckpointFreq() != 0)
   {
      if (m_config.getSaveFreq() > 0)
      {
          os << indent << "  Save model: every " << m_config.getSaveFreq() << " iteration\n";
      }
      else if (m_config.getSaveFreq() < 0)
      {
          os << indent << "  Save model after last iteration\n";
      }

      if (m_config.getCheckpointFreq() > 0)
      {
          os << indent << "  Checkpoint state: every " << m_config.getCheckpointFreq() << " seconds\n";
      }

      os << indent << "  Save prefix: " << m_config.getSavePrefix() << "\n";
      os << indent << "  Save extension: " << m_config.getSaveExtension() << "\n";
   }
   else
   {
      os << indent << "  Save model: never\n";
   }

   os << indent << "}\n";
   return os;
}

void Session::save(int iteration)
{
   //do not save if 'never save' mode is selected
   if (!m_config.getSaveFreq() &&
       !m_config.getCheckpointFreq() &&
       !m_config.getCsvStatus().size())
      return;

   std::int32_t isample = iteration - m_config.getBurnin() + 1;

   //save if checkpoint threshold overdue
   if (m_config.getCheckpointFreq() && (tick() - m_lastCheckpointTime) >= m_config.getCheckpointFreq())
   {
      std::int32_t icheckpoint = iteration + 1;

      //save this iteration
      std::shared_ptr<StepFile> stepFile = m_rootFile->createCheckpointStepFile(icheckpoint);
      saveInternal(stepFile);

      //remove previous iteration if required (initial m_lastCheckpointIter is -1 which means that it does not exist)
      if (m_lastCheckpointIter >= 0)
      {
         std::int32_t icheckpointPrev = m_lastCheckpointIter + 1;

         //remove previous iteration
         m_rootFile->removeCheckpointStepFile(icheckpointPrev);

         //flush last item in a root file
         m_rootFile->flushLast();
      }

      //upddate counters
      m_lastCheckpointTime = tick();
      m_lastCheckpointIter = iteration;
   }

   //save model during sampling stage
   if (m_config.getSaveFreq() && isample > 0)
   {
      //save_freq > 0: check modulo - do not save if not a save iteration
      if (m_config.getSaveFreq() > 0 && (isample % m_config.getSaveFreq()) != 0)
      {
          // don't save
      }
      //save_freq < 0: save last iter - do not save if (final model) mode is selected and not a final iteration
      else if (m_config.getSaveFreq() < 0 && isample < m_config.getNSamples())
      {
          // don't save
      }
      else
      {
          //do save this iteration
          std::shared_ptr<StepFile> stepFile = m_rootFile->createSampleStepFile(isample);
          saveInternal(stepFile);
      }
   }
}

void Session::saveInternal(std::shared_ptr<StepFile> stepFile)
{
   if (m_config.getVerbose())
   {
      std::cout << "-- Saving model, predictions,... into '" << stepFile->getStepFileName() << "'." << std::endl;
   }
   double start = tick();

   BaseSession::save(stepFile);

   //flush last item in a root file
   m_rootFile->flushLast();

   double stop = tick();
   if (m_config.getVerbose())
   {
      std::cout << "-- Done saving model. Took " << stop - start << " seconds." << std::endl;
   }
}

bool Session::restore(int& iteration)
{
   std::shared_ptr<StepFile> stepFile = nullptr;
   if (m_rootFile)
   {
       stepFile = m_rootFile->openLastStepFile();
   }

   if (!stepFile)
   {
      //if there is nothing to restore - start from initial iteration
      iteration = -1;

      //to keep track at what time we last checkpointed
      m_lastCheckpointTime = tick();
      m_lastCheckpointIter = -1;
      return false;
   }
   else
   {
      if (m_config.getVerbose())
      {
         std::cout << "-- Restoring model, predictions,... from '" << stepFile->getStepFileName() << "'." << std::endl;
      }

      BaseSession::restore(stepFile);

      //restore last iteration index
      if (stepFile->getCheckpoint())
      {
         iteration = stepFile->getIsample() - 1; //restore original state

         //to keep track at what time we last checkpointed
         m_lastCheckpointTime = tick();
         m_lastCheckpointIter = iteration;
      }
      else
      {
         iteration = stepFile->getIsample() + m_config.getBurnin() - 1; //restore original state

         //to keep track at what time we last checkpointed
         m_lastCheckpointTime = tick();
         m_lastCheckpointIter = iteration;
      }

      return true;
   }
}

std::shared_ptr<StatusItem> Session::getStatus() const
{
    std::shared_ptr<StatusItem> ret = std::make_shared<StatusItem>();

    if (m_iter < 0)
    {
        ret->phase = "Initial";
        ret->iter = m_iter + 1;
        ret->phase_iter = 0;
    }
    else if (m_iter < m_config.getBurnin())
    {
        ret->phase = "Burnin";
        ret->iter = m_iter + 1;
        ret->phase_iter = m_config.getBurnin();
    }
    else
    {
        ret->phase = "Sample";
        ret->iter = m_iter - m_config.getBurnin() + 1;
        ret->phase_iter = m_config.getNSamples();
    }

    for (int i = 0; i < (int)model().nmodes(); ++i)
    {
        ret->model_norms.push_back(model().U(i).norm());
    }

    ret->train_rmse = data().train_rmse(model());

    ret->rmse_avg = m_pred->rmse_avg;
    ret->rmse_1sample = m_pred->rmse_1sample;

    ret->auc_avg = m_pred->auc_avg;
    ret->auc_1sample = m_pred->auc_1sample;

    ret->elapsed_iter = m_secs_per_iter;
    ret->nnz_per_sec = (double)(data().nnz()) / m_secs_per_iter;
    ret->samples_per_sec = (double)(model().nsamples()) / m_secs_per_iter;

    return ret;
}

void Session::printStatus(std::ostream& output, bool resume)
{
   if(!m_config.getVerbose() &&
      !m_config.getCsvStatus().size())
      return;

   auto status_item = getStatus();

   std::string resumeString = resume ? "Continue from " : std::string();

   if (m_config.getVerbose() > 0)
   {
       if (m_iter < 0)
       {
           output << " ====== Initial phase ====== " << std::endl;
       }
       else if (m_iter < m_config.getBurnin() && m_iter == 0)
       {
           output << " ====== Sampling (burning phase) ====== " << std::endl;
       }
       else if (m_iter == m_config.getBurnin())
       {
           output << " ====== Burn-in complete, averaging samples ====== " << std::endl;
       }

       output << resumeString
           << status_item->phase
           << " "
           << std::setfill(' ') << std::setw(3) << status_item->iter
           << "/"
           << std::setfill(' ') << std::setw(3) << status_item->phase_iter
           << ": RMSE: "
           << std::fixed << std::setprecision(4) << status_item->rmse_avg
           << " (1samp: "
           << std::fixed << std::setprecision(4) << status_item->rmse_1sample
           << ")";

       if (m_config.getClassify())
       {
           output << " AUC:"
               << std::fixed << std::setprecision(4) << status_item->auc_avg
               << " (1samp: "
               << std::fixed << std::setprecision(4) << status_item->auc_1sample
               << ")"
               << std::endl;
       }

       output << "  U:[";
       for(double n: status_item->model_norms)
       {
           output << std::scientific << std::setprecision(2) << n << ", ";
       }
       output << "] [took: "
           << std::fixed << std::setprecision(1) << status_item->elapsed_iter
           << "s]"
           << std::endl;

       if (m_config.getVerbose() > 1)
       {
           output << std::fixed << std::setprecision(4) << "  RMSE train: " << status_item->train_rmse << std::endl;
           output << "  Priors:" << std::endl;

           for(const auto &p : m_priors)
               p->status(output, "     ");

           output << "  Model:" << std::endl;
           model().status(output, "    ");
           output << "  Noise:" << std::endl;
           data().status(output, "    ");
       }

       if (m_config.getVerbose() > 2)
       {
           output << "  Compute Performance: " << status_item->samples_per_sec << " samples/sec, " << status_item->nnz_per_sec << " nnz/sec" << std::endl;
       }
   }

   if (m_config.getCsvStatus().size())
   {
      std::ofstream f(m_config.getCsvStatus(), std::ofstream::out | std::ofstream::app);
      THROWERROR_ASSERT_MSG(f, "Could not open status csv file: " + m_config.getCsvStatus());
      f << status_item->asCsvString() << std::endl;
   }
}

std::string StatusItem::getCsvHeader()
{
   return "phase;iter;phase_len;rmse_avg;rmse_1samp;train_rmse;auc_avg;auc_1samp;elapsed";
}

std::string StatusItem::asCsvString() const
{
    char ret[1024];
    snprintf(ret, 1024, "%s;%d;%d;%.4f;%.4f;%.4f;%.4f;:%.4f;%0.1f",
                  phase.c_str(), iter, phase_iter, rmse_avg, rmse_1sample, train_rmse,
                  auc_1sample, auc_avg, elapsed_iter);

    return ret;
}

void Session::initRng()
{
   //init random generator
   if (m_config.getRandomSeedSet())
      init_bmrng(m_config.getRandomSeed());
   else
      init_bmrng();
}

std::shared_ptr<IPriorFactory> Session::create_prior_factory() const
{
   return std::make_shared<PriorFactory>();
}
<|endoftext|>


<commit_before>// This file is part of the VroomJs library.
//
// Author:
//     Federico Di Gregorio <fog@initd.org>
//
// Copyright © 2013 Federico Di Gregorio <fog@initd.org>
//
// 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 "vroomjs.h"

using namespace v8;

extern "C" jsvalue jsvalue_alloc_array(const int32_t length);

static Handle<Value> managed_prop_get(Local<String> name, const AccessorInfo& info)
{
    Local<Object> self = info.Holder();
    Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
    ManagedRef* ref = (ManagedRef*)wrap->Value();
    return ref->GetPropertyValue(name);
}

static Handle<Value> managed_prop_set(Local<String> name, Local<Value> value, const AccessorInfo& info)
{
    Local<Object> self = info.Holder();
    Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
    ManagedRef* ref = (ManagedRef*)wrap->Value();
    return ref->SetPropertyValue(name, value);
}

static Handle<Value> managed_call(const Arguments& args)
{
    Local<Object> self = args.Holder();
    Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
    ManagedRef* ref = (ManagedRef*)wrap->Value();
    return ref->Invoke(args);
}

JsEngine* JsEngine::New()
{
    JsEngine* engine = new JsEngine();
    if (engine != NULL) {
        engine->isolate_ = Isolate::New();
        Locker locker(engine->isolate_);
        Isolate::Scope isolate_scope(engine->isolate_);
        engine->context_ = new Persistent<Context>(Context::New());

        (*(engine->context_))->Enter();

        // Setup the template we'll use for all managed object references.
        HandleScope scope;
        Handle<ObjectTemplate> o = ObjectTemplate::New();
        o->SetInternalFieldCount(1);
        o->SetNamedPropertyHandler(managed_prop_get, managed_prop_set);
        o->SetCallAsFunctionHandler(managed_call);
        Persistent<ObjectTemplate> p = Persistent<ObjectTemplate>::New(o);
        engine->managed_template_ = new Persistent<ObjectTemplate>(p);
    }

    return engine;
}

void JsEngine::Dispose()
{
    {
        Locker locker(isolate_);
        Isolate::Scope isolate_scope(isolate_);
        managed_template_->Dispose();
        delete managed_template_;
        context_->Dispose();
        delete context_;
    }

    isolate_->Dispose();
}

jsvalue JsEngine::Execute(const uint16_t* str)
{
    jsvalue v;

    Locker locker(isolate_);
    Isolate::Scope isolate_scope(isolate_);
    (*context_)->Enter();

    HandleScope scope;
    TryCatch trycatch;

    Handle<String> source = String::New(str);
    Handle<Script> script = Script::Compile(source);
    if (!script.IsEmpty()) {
        Local<Value> result = script->Run();
        if (result.IsEmpty())
            v = ErrorFromV8(trycatch);
        else
            v = AnyFromV8(result);
    }
    else {
        v = ErrorFromV8(trycatch);
    }

    (*context_)->Exit();

    return v;
}

jsvalue JsEngine::SetVariable(const uint16_t* name, jsvalue value)
{
    Locker locker(isolate_);
    Isolate::Scope isolate_scope(isolate_);
    (*context_)->Enter();

    HandleScope scope;

    Handle<Value> v = AnyToV8(value);

    if ((*context_)->Global()->Set(String::New(name), v) == false) {
        // TODO: Return an error if set failed.
    }

    (*context_)->Exit();

    return AnyFromV8(Null());
}

jsvalue JsEngine::GetVariable(const uint16_t* name)
{
    jsvalue v;

    Locker locker(isolate_);
    Isolate::Scope isolate_scope(isolate_);
    (*context_)->Enter();

    HandleScope scope;
    TryCatch trycatch;

    Local<Value> value = (*context_)->Global()->Get(String::New(name));
    if (!value.IsEmpty()) {
        v = AnyFromV8(value);
    }
    else {
        v = ErrorFromV8(trycatch);
    }

    (*context_)->Exit();

    return v;
}

jsvalue JsEngine::GetPropertyValue(Persistent<Object>* obj, const uint16_t* name)
{
    jsvalue v;

    Locker locker(isolate_);
    Isolate::Scope isolate_scope(isolate_);
    (*context_)->Enter();

    HandleScope scope;
    TryCatch trycatch;

    Local<Value> value = (*obj)->Get(String::New(name));
    if (!value.IsEmpty()) {
        v = AnyFromV8(value);
    }
    else {
        v = ErrorFromV8(trycatch);
    }

    (*context_)->Exit();

    return v;
}

jsvalue JsEngine::SetPropertyValue(Persistent<Object>* obj, const uint16_t* name, jsvalue value)
{
    Locker locker(isolate_);
    Isolate::Scope isolate_scope(isolate_);
    (*context_)->Enter();

    HandleScope scope;

    Handle<Value> v = AnyToV8(value);

    if ((*obj)->Set(String::New(name), v) == false) {
        // TODO: Return an error if set failed.
    }

    (*context_)->Exit();

    return AnyFromV8(Null());
}

jsvalue JsEngine::InvokeProperty(Persistent<Object>* obj, const uint16_t* name, jsvalue args)
{
    jsvalue v;

    Locker locker(isolate_);
    Isolate::Scope isolate_scope(isolate_);
    (*context_)->Enter();

    HandleScope scope;
    TryCatch trycatch;

    Local<Value> prop = (*obj)->Get(String::New(name));
    if (prop.IsEmpty() || !prop->IsFunction()) {
        v = StringFromV8(String::New("property not found or isn't a function"));
        v.type = JSVALUE_TYPE_ERROR;
    }
    else {
        Local<Value> argv[args.length];
        ArrayToV8Args(args, argv);
        // TODO: Check ArrayToV8Args return value (but right now can't fail, right?)
        Local<Function> func = Local<Function>::Cast(prop);
        Local<Value> value = func->Call(*obj, args.length, argv);
        if (!value.IsEmpty()) {
            v = AnyFromV8(value);
        }
        else {
            v = ErrorFromV8(trycatch);
        }
    }

    (*context_)->Exit();

    return v;
}

jsvalue JsEngine::ErrorFromV8(TryCatch& trycatch)
{
    jsvalue v;

    Local<Value> exception = trycatch.Exception();

    v.type = JSVALUE_TYPE_UNKNOWN_ERROR;
    v.value.str = 0;
    v.length = 0;

    // If this is a managed exception we need to place its ID inside the jsvalue
    // and set the type JSVALUE_TYPE_MANAGED_ERROR to make sure the CLR side will
    // throw on it. Else we just wrap and return the exception Object. Note that
    // this is far from perfect because we ignore both the Message object and the
    // stack stack trace. If the exception is not an object (but just a string,
    // for example) we convert it with toString() and return that as an Exception.
    // TODO: return a composite/special object with stack trace information.

    if (exception->IsObject()) {
        Local<Object> obj = Local<Object>::Cast(exception);
        if (obj->InternalFieldCount() == 1) {
            ManagedRef* ref = (ManagedRef*)obj->GetPointerFromInternalField(0);
            v.type = JSVALUE_TYPE_MANAGED_ERROR;
            v.length = ref->Id();
        }
        else  {
            v = WrappedFromV8(obj);
            v.type = JSVALUE_TYPE_WRAPPED_ERROR;
        }
    }
    else if (!exception.IsEmpty()) {
        v = StringFromV8(exception);
        v.type = JSVALUE_TYPE_ERROR;
    }

    return v;
}

jsvalue JsEngine::StringFromV8(Handle<Value> value)
{
    jsvalue v;

    Local<String> s = value->ToString();
    v.length = s->Length();
    v.value.str = new uint16_t[v.length+1];
    if (v.value.str != NULL) {
        s->Write(v.value.str);
        v.type = JSVALUE_TYPE_STRING;
    }

    return v;
}

jsvalue JsEngine::WrappedFromV8(Handle<Object> obj)
{
    jsvalue v;

    v.type = JSVALUE_TYPE_WRAPPED;
    v.length = 0;
    v.value.ptr = new Persistent<Object>(Persistent<Object>::New(obj));

    return v;
}

jsvalue JsEngine::ManagedFromV8(Handle<Object> obj)
{
    jsvalue v;

    ManagedRef* ref = (ManagedRef*)obj->GetPointerFromInternalField(0);
    v.type = JSVALUE_TYPE_MANAGED;
    v.length = ref->Id();
    v.value.str = 0;

    return v;
}

jsvalue JsEngine::AnyFromV8(Handle<Value> value)
{
    jsvalue v;

    // Initialize to a generic error.
    v.type = JSVALUE_TYPE_UNKNOWN_ERROR;
    v.length = 0;
    v.value.str = 0;

    if (value->IsNull() || value->IsUndefined()) {
        v.type = JSVALUE_TYPE_NULL;
    }
    else if (value->IsBoolean()) {
        v.type = JSVALUE_TYPE_BOOLEAN;
        v.value.i32 = value->BooleanValue() ? 1 : 0;
    }
    else if (value->IsInt32()) {
        v.type = JSVALUE_TYPE_INTEGER;
        v.value.i32 = value->Int32Value();
    }
    else if (value->IsUint32()) {
        v.type = JSVALUE_TYPE_INDEX;
        v.value.i64 = value->Uint32Value();
    }
    else if (value->IsNumber()) {
        v.type = JSVALUE_TYPE_NUMBER;
        v.value.num = value->NumberValue();
    }
    else if (value->IsString()) {
        v = StringFromV8(value);
    }
    else if (value->IsDate()) {
        v.type = JSVALUE_TYPE_DATE;
        v.value.num = value->NumberValue();
    }
    else if (value->IsArray()) {
        Handle<Array> object = Handle<Array>::Cast(value->ToObject());
        v.length = object->Length();
        jsvalue* array = new jsvalue[v.length];
        if (array != NULL) {
            for(int i = 0; i < v.length; i++) {
                array[i] = AnyFromV8(object->Get(i));
            }
            v.type = JSVALUE_TYPE_ARRAY;
            v.value.arr = array;
        }
    }
    else if (value->IsFunction()) {

    }
    else if (value->IsObject()) {
        Handle<Object> obj = Handle<Object>::Cast(value);
        if (obj->InternalFieldCount() == 1)
            v = ManagedFromV8(obj);
        else
            v = WrappedFromV8(obj);
    }

    return v;
}

Handle<Value> JsEngine::AnyToV8(jsvalue v)
{
    if (v.type == JSVALUE_TYPE_NULL) {
        return Null();
    }
    if (v.type == JSVALUE_TYPE_BOOLEAN) {
        return Boolean::New(v.value.i32);
    }
    if (v.type == JSVALUE_TYPE_INTEGER) {
        return Int32::New(v.value.i32);
    }
    if (v.type == JSVALUE_TYPE_NUMBER) {
        return Number::New(v.value.num);
    }
    if (v.type == JSVALUE_TYPE_STRING) {
        return String::New(v.value.str);
    }
    if (v.type == JSVALUE_TYPE_DATE) {
        return Date::New(v.value.num);
    }

    // This is an ID to a managed object that lives inside the JsEngine keep-alive
    // cache. We just wrap it and the pointer to the engine inside an External. A
    // managed error is still a CLR object so it is wrapped exactly as a normal
    // managed object.

    if (v.type == JSVALUE_TYPE_MANAGED || v.type == JSVALUE_TYPE_MANAGED_ERROR) {
        ManagedRef* ref = new ManagedRef(this, v.length);
        Local<Object> obj = (*(managed_template_))->NewInstance();
        obj->SetInternalField(0, External::New(ref));
        return obj;
    }

    return Null();
}

int32_t JsEngine::ArrayToV8Args(jsvalue value, Handle<Value> preallocatedArgs[])
{
    if (value.type != JSVALUE_TYPE_ARRAY)
        return -1;

    for (int i=0 ; i < value.length ; i++) {
        preallocatedArgs[i] = AnyToV8(value.value.arr[i]);
    }

    return value.length;
}

jsvalue JsEngine::ArrayFromArguments(const Arguments& args)
{
    jsvalue v = jsvalue_alloc_array(args.Length());

    for (int i=0 ; i < v.length ; i++) {
        v.value.arr[i] = AnyFromV8(args[i]);
    }

    return v;
}<commit_msg>[libvroomjs] Added callback to dispose of managed references<commit_after>// This file is part of the VroomJs library.
//
// Author:
//     Federico Di Gregorio <fog@initd.org>
//
// Copyright © 2013 Federico Di Gregorio <fog@initd.org>
//
// 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 "vroomjs.h"

using namespace v8;

extern "C" jsvalue jsvalue_alloc_array(const int32_t length);

static Handle<Value> managed_prop_get(Local<String> name, const AccessorInfo& info)
{
    Local<Object> self = info.Holder();
    Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
    ManagedRef* ref = (ManagedRef*)wrap->Value();
    return ref->GetPropertyValue(name);
}

static Handle<Value> managed_prop_set(Local<String> name, Local<Value> value, const AccessorInfo& info)
{
    Local<Object> self = info.Holder();
    Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
    ManagedRef* ref = (ManagedRef*)wrap->Value();
    return ref->SetPropertyValue(name, value);
}

static Handle<Value> managed_call(const Arguments& args)
{
    Local<Object> self = args.Holder();
    Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
    ManagedRef* ref = (ManagedRef*)wrap->Value();
    return ref->Invoke(args);
}

static void managed_destroy(Persistent<Value> object, void* parameter)
{
    Persistent<Object> self = Persistent<Object>::Cast(object);
    Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
    delete (ManagedRef*)wrap->Value();
    object.Dispose();
}

JsEngine* JsEngine::New()
{
    JsEngine* engine = new JsEngine();
    if (engine != NULL) {
        engine->isolate_ = Isolate::New();
        Locker locker(engine->isolate_);
        Isolate::Scope isolate_scope(engine->isolate_);
        engine->context_ = new Persistent<Context>(Context::New());

        (*(engine->context_))->Enter();

        // Setup the template we'll use for all managed object references.
        HandleScope scope;
        Handle<ObjectTemplate> o = ObjectTemplate::New();
        o->SetInternalFieldCount(1);
        o->SetNamedPropertyHandler(managed_prop_get, managed_prop_set);
        o->SetCallAsFunctionHandler(managed_call);
        Persistent<ObjectTemplate> p = Persistent<ObjectTemplate>::New(o);
        engine->managed_template_ = new Persistent<ObjectTemplate>(p);
    }

    return engine;
}

void JsEngine::Dispose()
{
    {
        Locker locker(isolate_);
        Isolate::Scope isolate_scope(isolate_);
        managed_template_->Dispose();
        delete managed_template_;
        context_->Dispose();
        delete context_;
    }

    isolate_->Dispose();
}

jsvalue JsEngine::Execute(const uint16_t* str)
{
    jsvalue v;

    Locker locker(isolate_);
    Isolate::Scope isolate_scope(isolate_);
    (*context_)->Enter();

    HandleScope scope;
    TryCatch trycatch;

    Handle<String> source = String::New(str);
    Handle<Script> script = Script::Compile(source);
    if (!script.IsEmpty()) {
        Local<Value> result = script->Run();
        if (result.IsEmpty())
            v = ErrorFromV8(trycatch);
        else
            v = AnyFromV8(result);
    }
    else {
        v = ErrorFromV8(trycatch);
    }

    (*context_)->Exit();

    return v;
}

jsvalue JsEngine::SetVariable(const uint16_t* name, jsvalue value)
{
    Locker locker(isolate_);
    Isolate::Scope isolate_scope(isolate_);
    (*context_)->Enter();

    HandleScope scope;

    Handle<Value> v = AnyToV8(value);

    if ((*context_)->Global()->Set(String::New(name), v) == false) {
        // TODO: Return an error if set failed.
    }

    (*context_)->Exit();

    return AnyFromV8(Null());
}

jsvalue JsEngine::GetVariable(const uint16_t* name)
{
    jsvalue v;

    Locker locker(isolate_);
    Isolate::Scope isolate_scope(isolate_);
    (*context_)->Enter();

    HandleScope scope;
    TryCatch trycatch;

    Local<Value> value = (*context_)->Global()->Get(String::New(name));
    if (!value.IsEmpty()) {
        v = AnyFromV8(value);
    }
    else {
        v = ErrorFromV8(trycatch);
    }

    (*context_)->Exit();

    return v;
}

jsvalue JsEngine::GetPropertyValue(Persistent<Object>* obj, const uint16_t* name)
{
    jsvalue v;

    Locker locker(isolate_);
    Isolate::Scope isolate_scope(isolate_);
    (*context_)->Enter();

    HandleScope scope;
    TryCatch trycatch;

    Local<Value> value = (*obj)->Get(String::New(name));
    if (!value.IsEmpty()) {
        v = AnyFromV8(value);
    }
    else {
        v = ErrorFromV8(trycatch);
    }

    (*context_)->Exit();

    return v;
}

jsvalue JsEngine::SetPropertyValue(Persistent<Object>* obj, const uint16_t* name, jsvalue value)
{
    Locker locker(isolate_);
    Isolate::Scope isolate_scope(isolate_);
    (*context_)->Enter();

    HandleScope scope;

    Handle<Value> v = AnyToV8(value);

    if ((*obj)->Set(String::New(name), v) == false) {
        // TODO: Return an error if set failed.
    }

    (*context_)->Exit();

    return AnyFromV8(Null());
}

jsvalue JsEngine::InvokeProperty(Persistent<Object>* obj, const uint16_t* name, jsvalue args)
{
    jsvalue v;

    Locker locker(isolate_);
    Isolate::Scope isolate_scope(isolate_);
    (*context_)->Enter();

    HandleScope scope;
    TryCatch trycatch;

    Local<Value> prop = (*obj)->Get(String::New(name));
    if (prop.IsEmpty() || !prop->IsFunction()) {
        v = StringFromV8(String::New("property not found or isn't a function"));
        v.type = JSVALUE_TYPE_ERROR;
    }
    else {
        Local<Value> argv[args.length];
        ArrayToV8Args(args, argv);
        // TODO: Check ArrayToV8Args return value (but right now can't fail, right?)
        Local<Function> func = Local<Function>::Cast(prop);
        Local<Value> value = func->Call(*obj, args.length, argv);
        if (!value.IsEmpty()) {
            v = AnyFromV8(value);
        }
        else {
            v = ErrorFromV8(trycatch);
        }
    }

    (*context_)->Exit();

    return v;
}

jsvalue JsEngine::ErrorFromV8(TryCatch& trycatch)
{
    jsvalue v;

    Local<Value> exception = trycatch.Exception();

    v.type = JSVALUE_TYPE_UNKNOWN_ERROR;
    v.value.str = 0;
    v.length = 0;

    // If this is a managed exception we need to place its ID inside the jsvalue
    // and set the type JSVALUE_TYPE_MANAGED_ERROR to make sure the CLR side will
    // throw on it. Else we just wrap and return the exception Object. Note that
    // this is far from perfect because we ignore both the Message object and the
    // stack stack trace. If the exception is not an object (but just a string,
    // for example) we convert it with toString() and return that as an Exception.
    // TODO: return a composite/special object with stack trace information.

    if (exception->IsObject()) {
        Local<Object> obj = Local<Object>::Cast(exception);
        if (obj->InternalFieldCount() == 1) {
            ManagedRef* ref = (ManagedRef*)obj->GetPointerFromInternalField(0);
            v.type = JSVALUE_TYPE_MANAGED_ERROR;
            v.length = ref->Id();
        }
        else  {
            v = WrappedFromV8(obj);
            v.type = JSVALUE_TYPE_WRAPPED_ERROR;
        }
    }
    else if (!exception.IsEmpty()) {
        v = StringFromV8(exception);
        v.type = JSVALUE_TYPE_ERROR;
    }

    return v;
}

jsvalue JsEngine::StringFromV8(Handle<Value> value)
{
    jsvalue v;

    Local<String> s = value->ToString();
    v.length = s->Length();
    v.value.str = new uint16_t[v.length+1];
    if (v.value.str != NULL) {
        s->Write(v.value.str);
        v.type = JSVALUE_TYPE_STRING;
    }

    return v;
}

jsvalue JsEngine::WrappedFromV8(Handle<Object> obj)
{
    jsvalue v;

    v.type = JSVALUE_TYPE_WRAPPED;
    v.length = 0;
    v.value.ptr = new Persistent<Object>(Persistent<Object>::New(obj));

    return v;
}

jsvalue JsEngine::ManagedFromV8(Handle<Object> obj)
{
    jsvalue v;

    ManagedRef* ref = (ManagedRef*)obj->GetPointerFromInternalField(0);
    v.type = JSVALUE_TYPE_MANAGED;
    v.length = ref->Id();
    v.value.str = 0;

    return v;
}

jsvalue JsEngine::AnyFromV8(Handle<Value> value)
{
    jsvalue v;

    // Initialize to a generic error.
    v.type = JSVALUE_TYPE_UNKNOWN_ERROR;
    v.length = 0;
    v.value.str = 0;

    if (value->IsNull() || value->IsUndefined()) {
        v.type = JSVALUE_TYPE_NULL;
    }
    else if (value->IsBoolean()) {
        v.type = JSVALUE_TYPE_BOOLEAN;
        v.value.i32 = value->BooleanValue() ? 1 : 0;
    }
    else if (value->IsInt32()) {
        v.type = JSVALUE_TYPE_INTEGER;
        v.value.i32 = value->Int32Value();
    }
    else if (value->IsUint32()) {
        v.type = JSVALUE_TYPE_INDEX;
        v.value.i64 = value->Uint32Value();
    }
    else if (value->IsNumber()) {
        v.type = JSVALUE_TYPE_NUMBER;
        v.value.num = value->NumberValue();
    }
    else if (value->IsString()) {
        v = StringFromV8(value);
    }
    else if (value->IsDate()) {
        v.type = JSVALUE_TYPE_DATE;
        v.value.num = value->NumberValue();
    }
    else if (value->IsArray()) {
        Handle<Array> object = Handle<Array>::Cast(value->ToObject());
        v.length = object->Length();
        jsvalue* array = new jsvalue[v.length];
        if (array != NULL) {
            for(int i = 0; i < v.length; i++) {
                array[i] = AnyFromV8(object->Get(i));
            }
            v.type = JSVALUE_TYPE_ARRAY;
            v.value.arr = array;
        }
    }
    else if (value->IsFunction()) {
        // TODO: how do we represent this on the CLR side? Delegate?
    }
    else if (value->IsObject()) {
        Handle<Object> obj = Handle<Object>::Cast(value);
        if (obj->InternalFieldCount() ==     1)
            v = ManagedFromV8(obj);
        else
            v = WrappedFromV8(obj);
    }

    return v;
}

Handle<Value> JsEngine::AnyToV8(jsvalue v)
{
    if (v.type == JSVALUE_TYPE_NULL) {
        return Null();
    }
    if (v.type == JSVALUE_TYPE_BOOLEAN) {
        return Boolean::New(v.value.i32);
    }
    if (v.type == JSVALUE_TYPE_INTEGER) {
        return Int32::New(v.value.i32);
    }
    if (v.type == JSVALUE_TYPE_NUMBER) {
        return Number::New(v.value.num);
    }
    if (v.type == JSVALUE_TYPE_STRING) {
        return String::New(v.value.str);
    }
    if (v.type == JSVALUE_TYPE_DATE) {
        return Date::New(v.value.num);
    }

    // This is an ID to a managed object that lives inside the JsEngine keep-alive
    // cache. We just wrap it and the pointer to the engine inside an External. A
    // managed error is still a CLR object so it is wrapped exactly as a normal
    // managed object.

    if (v.type == JSVALUE_TYPE_MANAGED || v.type == JSVALUE_TYPE_MANAGED_ERROR) {
        ManagedRef* ref = new ManagedRef(this, v.length);
        Persistent<Object> obj = Persistent<Object>::New((*(managed_template_))->NewInstance());
        obj->SetInternalField(0, External::New(ref));
        obj.MakeWeak(NULL, managed_destroy);
        return obj;
    }

    return Null();
}

int32_t JsEngine::ArrayToV8Args(jsvalue value, Handle<Value> preallocatedArgs[])
{
    if (value.type != JSVALUE_TYPE_ARRAY)
        return -1;

    for (int i=0 ; i < value.length ; i++) {
        preallocatedArgs[i] = AnyToV8(value.value.arr[i]);
    }

    return value.length;
}

jsvalue JsEngine::ArrayFromArguments(const Arguments& args)
{
    jsvalue v = jsvalue_alloc_array(args.Length());

    for (int i=0 ; i < v.length ; i++) {
        v.value.arr[i] = AnyFromV8(args[i]);
    }

    return v;
}<|endoftext|>


<commit_before>#ifndef HEADERLIB_HDR_MATH_HPP
#define HEADERLIB_HDR_MATH_HPP
/*
 * math.hpp
 * Common mathematical constructs
 *
 *  Created on: 04.04.2016
 *      Author: andreas
 */
#include "hdr/core.hpp"
namespace hdr::math {
using ::hdr::std::Apply;
using ::hdr::std::False;
using ::hdr::std::True;
using ::hdr::std::flip;
using ::hdr::lambda::_0;
using ::hdr::lambda::_1;
using ::hdr::lambda::Lambda;
using ::hdr::lambda::IApply;
/**	Utility to convert integers to types
 */
template<unsigned u> using Unsigned = Value<u>;
template<signed s> 	 using Signed 	= Value<s>;
template<bool b> 		 using Bool 		= Value<b>;

template<typename _Tp, _Tp a, _Tp b>
struct Plus<Value<a>, Value<b>> { using type = Value<a+b>; };
template<bool a, bool b>
struct Plus<Bool<a>, Bool<b>>   { using type = Bool<a || b>; };

template<typename _Tp, _Tp a, _Tp b>
struct Mult<Value<a>, Value<b>> { using type = Value<a*b>; };
template<bool a, bool b>
struct Mult<Bool<a>, Bool<b>>   { using type = Bool<a && b>; };

template<typename _Tp, _Tp a, _Tp b>
struct Minus<Value<a>, Value<b>> { using type = Value<a-b>; };
template<bool a, bool b>
struct Minus<Bool<a>, Bool<b>>;

template<typename _Tp, _Tp a, _Tp b>
struct Div<Value<a>, Value<b>> { using type = Value<a/b>; };
template<typename _Tp, _Tp a>
struct Div<Value<a>, Value<0>>;
template<bool a, bool b>
struct Div<Bool<a>, Bool<b>>;

template<typename _Tp, _Tp a, _Tp b>
struct Compare<Value<a>, Value<b>> { using type = Value<(a < b)>; };

/// Boolean manipulators
using not_     = Lambda<_0, False, True>;
using and_     = Lambda<_0, _1,    False>;
using or_      = Lambda<_0, True,  _1>;
using xor_     = Lambda<_0, IApply<not_, _1>, _1>;

template<typename Smaller>
struct TotalOrder {
	using smaller = Smaller;
	using greater = Apply<flip, smaller>;
	using unequal = Lambda<or_,  IApply<compare, _0, _1>, IApply<compare, _1, _0>>;
	using equal   = Lambda<not_, IApply<unequal, _0, _1>>;
};
using natural_order = TotalOrder<compare>;

} // hdrstd::math
#endif //HEADERLIB_HDR_MATH_HPP
<commit_msg>Fix bug in math comparison<commit_after>#ifndef HEADERLIB_HDR_MATH_HPP
#define HEADERLIB_HDR_MATH_HPP
/*
 * math.hpp
 * Common mathematical constructs
 *
 *  Created on: 04.04.2016
 *      Author: andreas
 */
#include "hdr/core.hpp"
namespace hdr::math {
using ::hdr::std::Apply;
using ::hdr::std::False;
using ::hdr::std::True;
using ::hdr::std::flip;
using ::hdr::lambda::_0;
using ::hdr::lambda::_1;
using ::hdr::lambda::Lambda;
using ::hdr::lambda::IApply;
/**	Utility to convert integers to types
 */
template<unsigned u> using Unsigned = Value<u>;
template<signed s> 	 using Signed 	= Value<s>;
template<bool b> 		 using Bool 		= Value<b>;

template<typename _Tp, _Tp a, _Tp b>
struct Plus<Value<a>, Value<b>> { using type = Value<a+b>; };
template<bool a, bool b>
struct Plus<Bool<a>, Bool<b>>   { using type = Bool<a || b>; };

template<typename _Tp, _Tp a, _Tp b>
struct Mult<Value<a>, Value<b>> { using type = Value<a*b>; };
template<bool a, bool b>
struct Mult<Bool<a>, Bool<b>>   { using type = Bool<a && b>; };

template<typename _Tp, _Tp a, _Tp b>
struct Minus<Value<a>, Value<b>> { using type = Value<a-b>; };
template<bool a, bool b>
struct Minus<Bool<a>, Bool<b>>;

template<typename _Tp, _Tp a, _Tp b>
struct Div<Value<a>, Value<b>> { using type = Value<a/b>; };
template<typename _Tp, _Tp a>
struct Div<Value<a>, Value<0>>;
template<bool a, bool b>
struct Div<Bool<a>, Bool<b>>;

template<typename _Tp, _Tp a, _Tp b>
struct Compare<Value<a>, Value<b>> { using type = ::hdr::std::FromStdBool<(a < b)>; };

/// Boolean manipulators
using not_     = Lambda<_0, False, True>;
using and_     = Lambda<_0, _1,    False>;
using or_      = Lambda<_0, True,  _1>;
using xor_     = Lambda<_0, IApply<not_, _1>, _1>;

template<typename Smaller>
struct TotalOrder {
	using smaller = Smaller;
	using greater = Apply<flip, smaller>;
	using unequal = Lambda<or_,  IApply<compare, _0, _1>, IApply<compare, _1, _0>>;
	using equal   = Lambda<not_, IApply<unequal, _0, _1>>;
};
using natural_order = TotalOrder<compare>;

} // hdrstd::math
#endif //HEADERLIB_HDR_MATH_HPP
<|endoftext|>


<commit_before>/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/pjrt/mlir_to_hlo.h"

#include <utility>

#include "mlir/Dialect/StandardOps/IR/Ops.h"  // from @llvm-project
#include "mlir/IR/BuiltinOps.h"  // from @llvm-project
#include "mlir/Parser.h"  // from @llvm-project
#include "mlir/Pass/Pass.h"  // from @llvm-project
#include "mlir/Pass/PassManager.h"  // from @llvm-project
#include "mlir/Transforms/Passes.h"  // from @llvm-project
#include "tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.h"
#include "tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/IR/hlo_ops.h"
#include "tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/transforms/passes.h"
#include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h"
#include "tensorflow/compiler/mlir/xla/mlir_hlo_to_hlo.h"

namespace xla {

Status MlirToXlaComputation(mlir::ModuleOp module,
                            XlaComputation& xla_computation,
                            bool use_tuple_args, bool return_tuple) {
  mlir::StatusScopedDiagnosticHandler diagnostic_handler(module->getContext());
  {
    mlir::PassManager pm(module->getContext());
    pm.addNestedPass<mlir::FuncOp>(mlir::mhlo::createChloLegalizeToHloPass(
        /*legalize_broadcasts=*/true, /*expand_compositions=*/true));
    pm.addNestedPass<mlir::FuncOp>(mlir::createCanonicalizerPass());
    // In order to export to XLA, we must sink constants to control flow
    // regions, since XLA uses functional control flow.
    pm.addNestedPass<mlir::FuncOp>(
        mlir::mhlo::createSinkConstantsToControlFlowPass());
    if (failed(pm.run(module))) {
      VLOG(1) << "MHLO->HLO lowering passes failed.";
      module->dump();
      return diagnostic_handler.ConsumeStatus();
    }

    VLOG(5) << "MHLO module after lowering, before HLO import ";
    if (VLOG_IS_ON(5)) {
      module->dump();
    }
  }

  HloProto proto;
  TF_RETURN_IF_ERROR(
      ConvertMlirHloToHlo(module, &proto, use_tuple_args, return_tuple));

  xla_computation = XlaComputation(std::move(*proto.mutable_hlo_module()));
  return Status::OK();
}

StatusOr<mlir::OwningModuleRef> ParseMlirModuleString(
    absl::string_view mlir_module_str, mlir::MLIRContext& context) {
  mlir::OwningModuleRef module;
  context.loadDialect<mlir::StandardOpsDialect>();
  context.loadDialect<mlir::mhlo::MhloDialect>();
  context.loadDialect<mlir::chlo::HloClientDialect>();
  mlir::StatusScopedDiagnosticHandler diagnostic_handler(&context);
  module = mlir::parseSourceString(
      llvm::StringRef(mlir_module_str.data(), mlir_module_str.size()),
      &context);
  if (!module) {
    return diagnostic_handler.ConsumeStatus();
  }
  if (failed(module->verify())) {
    VLOG(1) << "MLIR verification failed.";
    module->dump();
    return diagnostic_handler.ConsumeStatus();
  }
  return std::move(module);
}

Status ParseMlirModuleStringAndConvertToXlaComputation(
    absl::string_view mlir_module_str, XlaComputation& xla_computation,
    bool use_tuple_args, bool return_tuple) {
  mlir::MLIRContext context;
  TF_ASSIGN_OR_RETURN(mlir::OwningModuleRef module,
                      xla::ParseMlirModuleString(mlir_module_str, context));
  return xla::MlirToXlaComputation(*module, xla_computation, use_tuple_args,
                                   return_tuple);
}

}  // namespace xla
<commit_msg>[JAX] Include JAX operator type in MHLO locations.<commit_after>/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/pjrt/mlir_to_hlo.h"

#include <utility>

#include "mlir/Dialect/StandardOps/IR/Ops.h"  // from @llvm-project
#include "mlir/IR/BuiltinOps.h"  // from @llvm-project
#include "mlir/Parser.h"  // from @llvm-project
#include "mlir/Pass/Pass.h"  // from @llvm-project
#include "mlir/Pass/PassManager.h"  // from @llvm-project
#include "mlir/Transforms/Passes.h"  // from @llvm-project
#include "tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.h"
#include "tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/IR/hlo_ops.h"
#include "tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/transforms/passes.h"
#include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h"
#include "tensorflow/compiler/mlir/xla/mlir_hlo_to_hlo.h"

namespace xla {

Status MlirToXlaComputation(mlir::ModuleOp module,
                            XlaComputation& xla_computation,
                            bool use_tuple_args, bool return_tuple) {
  mlir::StatusScopedDiagnosticHandler diagnostic_handler(module->getContext());
  {
    mlir::PassManager pm(module->getContext());
    pm.addNestedPass<mlir::FuncOp>(mlir::mhlo::createChloLegalizeToHloPass(
        /*legalize_broadcasts=*/true, /*expand_compositions=*/true));
    pm.addNestedPass<mlir::FuncOp>(mlir::createCanonicalizerPass());
    // In order to export to XLA, we must sink constants to control flow
    // regions, since XLA uses functional control flow.
    pm.addNestedPass<mlir::FuncOp>(
        mlir::mhlo::createSinkConstantsToControlFlowPass());
    if (failed(pm.run(module))) {
      VLOG(1) << "MHLO->HLO lowering passes failed.";
      module->dump();
      return diagnostic_handler.ConsumeStatus();
    }

    VLOG(5) << "MHLO module after lowering, before HLO import ";
    if (VLOG_IS_ON(5)) {
      module->dump();
    }
  }

  HloProto proto;
  mlir::MlirToHloConversionOptions options;
  // We don't want the conversion to muck with our operator names.
  options.legalize_node_names = false;
  TF_RETURN_IF_ERROR(
      ConvertMlirHloToHlo(module, &proto, use_tuple_args, return_tuple,
                          /*shape_representation_fn=*/nullptr, options));

  xla_computation = XlaComputation(std::move(*proto.mutable_hlo_module()));
  return Status::OK();
}

StatusOr<mlir::OwningModuleRef> ParseMlirModuleString(
    absl::string_view mlir_module_str, mlir::MLIRContext& context) {
  mlir::OwningModuleRef module;
  context.loadDialect<mlir::StandardOpsDialect>();
  context.loadDialect<mlir::mhlo::MhloDialect>();
  context.loadDialect<mlir::chlo::HloClientDialect>();
  mlir::StatusScopedDiagnosticHandler diagnostic_handler(&context);
  module = mlir::parseSourceString(
      llvm::StringRef(mlir_module_str.data(), mlir_module_str.size()),
      &context);
  if (!module) {
    return diagnostic_handler.ConsumeStatus();
  }
  if (failed(module->verify())) {
    VLOG(1) << "MLIR verification failed.";
    module->dump();
    return diagnostic_handler.ConsumeStatus();
  }
  return std::move(module);
}

Status ParseMlirModuleStringAndConvertToXlaComputation(
    absl::string_view mlir_module_str, XlaComputation& xla_computation,
    bool use_tuple_args, bool return_tuple) {
  mlir::MLIRContext context;
  TF_ASSIGN_OR_RETURN(mlir::OwningModuleRef module,
                      xla::ParseMlirModuleString(mlir_module_str, context));
  return xla::MlirToXlaComputation(*module, xla_computation, use_tuple_args,
                                   return_tuple);
}

}  // namespace xla
<|endoftext|>


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

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <list>
#include <string>
#include <utility>

#include "../fpdfsdk/include/fpdf_dataavail.h"
#include "../fpdfsdk/include/fpdf_ext.h"
#include "../fpdfsdk/include/fpdfformfill.h"
#include "../fpdfsdk/include/fpdftext.h"
#include "../fpdfsdk/include/fpdfview.h"
#include "v8/include/v8.h"

#ifdef _WIN32
  #define snprintf _snprintf
#endif

enum OutputFormat {
  OUTPUT_NONE,
  OUTPUT_PPM,
#ifdef _WIN32
  OUTPUT_BMP,
  OUTPUT_EMF,
#endif
};

static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
                     int stride, int width, int height) {
  const char* buffer = reinterpret_cast<const char*>(buffer_void);

  if (stride < 0 || width < 0 || height < 0)
    return;
  if (height > 0 && width > INT_MAX / height)
    return;
  int out_len = width * height;
  if (out_len > INT_MAX / 3)
    return;
  out_len *= 3;

  char filename[256];
  snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
  FILE* fp = fopen(filename, "wb");
  if (!fp)
    return;
  fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
  // Source data is B, G, R, unused.
  // Dest data is R, G, B.
  char* result = new char[out_len];
  if (result) {
    for (int h = 0; h < height; ++h) {
      const char* src_line = buffer + (stride * h);
      char* dest_line = result + (width * h * 3);
      for (int w = 0; w < width; ++w) {
        // R
        dest_line[w * 3] = src_line[(w * 4) + 2];
        // G
        dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
        // B
        dest_line[(w * 3) + 2] = src_line[w * 4];
      }
    }
    fwrite(result, out_len, 1, fp);
    delete [] result;
  }
  fclose(fp);
}

#ifdef _WIN32
static void WriteBmp(const char* pdf_name, int num, const void* buffer,
                     int stride, int width, int height) {
  if (stride < 0 || width < 0 || height < 0)
    return;
  if (height > 0 && width > INT_MAX / height)
    return;
  int out_len = stride * height;
  if (out_len > INT_MAX / 3)
    return;

  char filename[256];
  snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
  FILE* fp = fopen(filename, "wb");
  if (!fp)
    return;

  BITMAPINFO bmi = {0};
  bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
  bmi.bmiHeader.biWidth = width;
  bmi.bmiHeader.biHeight = -height;  // top-down image
  bmi.bmiHeader.biPlanes = 1;
  bmi.bmiHeader.biBitCount = 32;
  bmi.bmiHeader.biCompression = BI_RGB;
  bmi.bmiHeader.biSizeImage = 0;

  BITMAPFILEHEADER file_header = {0};
  file_header.bfType = 0x4d42;
  file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
  file_header.bfOffBits = file_header.bfSize - out_len;

  fwrite(&file_header, sizeof(file_header), 1, fp);
  fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
  fwrite(buffer, out_len, 1, fp);
  fclose(fp);
}

void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
  int width = static_cast<int>(FPDF_GetPageWidth(page));
  int height = static_cast<int>(FPDF_GetPageHeight(page));

  char filename[256];
  snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);

  HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL);

  HRGN rgn = CreateRectRgn(0, 0, width, height);
  SelectClipRgn(dc, rgn);
  DeleteObject(rgn);

  SelectObject(dc, GetStockObject(NULL_PEN));
  SelectObject(dc, GetStockObject(WHITE_BRUSH));
  // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
  Rectangle(dc, 0, 0, width + 1, height + 1);

  FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
                  FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);

  DeleteEnhMetaFile(CloseEnhMetaFile(dc));
}
#endif

int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) {
  printf("Form_Alert called.\n");
  return 0;
}

void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
  std::string feature = "Unknown";
  switch (type) {
    case FPDF_UNSP_DOC_XFAFORM:
      feature = "XFA";
      break;
    case FPDF_UNSP_DOC_PORTABLECOLLECTION:
      feature = "Portfolios_Packages";
      break;
    case FPDF_UNSP_DOC_ATTACHMENT:
    case FPDF_UNSP_ANNOT_ATTACHMENT:
      feature = "Attachment";
      break;
    case FPDF_UNSP_DOC_SECURITY:
      feature = "Rights_Management";
      break;
    case FPDF_UNSP_DOC_SHAREDREVIEW:
      feature = "Shared_Review";
      break;
    case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
    case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
    case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
      feature = "Shared_Form";
      break;
    case FPDF_UNSP_ANNOT_3DANNOT:
      feature = "3D";
      break;
    case FPDF_UNSP_ANNOT_MOVIE:
      feature = "Movie";
      break;
    case FPDF_UNSP_ANNOT_SOUND:
      feature = "Sound";
      break;
    case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
    case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
      feature = "Screen";
      break;
    case FPDF_UNSP_ANNOT_SIG:
      feature = "Digital_Signature";
      break;
  }
  printf("Unsupported feature: %s.\n", feature.c_str());
}

bool ParseCommandLine(int argc, const char* argv[], OutputFormat* output_format,
                      std::list<const char*>* files) {
  *output_format = OUTPUT_NONE;
  files->clear();

  int cur_arg = 1;
  for (; cur_arg < argc; ++cur_arg) {
    if (strcmp(argv[cur_arg], "--ppm") == 0)
      *output_format = OUTPUT_PPM;
#ifdef _WIN32
    else if (strcmp(argv[cur_arg], "--emf") == 0)
      *output_format = OUTPUT_EMF;
    else if (strcmp(argv[cur_arg], "--bmp") == 0)
      *output_format = OUTPUT_BMP;
#endif
    else
      break;
  }

  if (cur_arg > 2)  // Multiple options.
    return false;

  if (cur_arg >= argc)  // No input files.
    return false;

  for (int i = cur_arg; i < argc; i++)
    files->push_back(argv[i]);

  return true;
}

class TestLoader {
 public:
  TestLoader(const char* pBuf, size_t len);

  const char* m_pBuf;
  size_t m_Len;
};

TestLoader::TestLoader(const char* pBuf, size_t len)
    : m_pBuf(pBuf), m_Len(len) {
}

int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
              unsigned long size) {
  TestLoader* pLoader = (TestLoader*) param;
  if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
  memcpy(pBuf, pLoader->m_pBuf + pos, size);
  return 1;
}

bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
  return true;
}

void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
}

void RenderPdf(const char* name, const char* pBuf, size_t len,
               OutputFormat format) {
  printf("Rendering PDF file %s.\n", name);

  IPDF_JSPLATFORM platform_callbacks;
  memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
  platform_callbacks.version = 1;
  platform_callbacks.app_alert = Form_Alert;

  FPDF_FORMFILLINFO form_callbacks;
  memset(&form_callbacks, '\0', sizeof(form_callbacks));
  form_callbacks.version = 1;
  form_callbacks.m_pJsPlatform = &platform_callbacks;

  TestLoader loader(pBuf, len);

  FPDF_FILEACCESS file_access;
  memset(&file_access, '\0', sizeof(file_access));
  file_access.m_FileLen = static_cast<unsigned long>(len);
  file_access.m_GetBlock = Get_Block;
  file_access.m_Param = &loader;

  FX_FILEAVAIL file_avail;
  memset(&file_avail, '\0', sizeof(file_avail));
  file_avail.version = 1;
  file_avail.IsDataAvail = Is_Data_Avail;

  FX_DOWNLOADHINTS hints;
  memset(&hints, '\0', sizeof(hints));
  hints.version = 1;
  hints.AddSegment = Add_Segment;

  FPDF_DOCUMENT doc;
  FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);

  (void) FPDFAvail_IsDocAvail(pdf_avail, &hints);

  if (!FPDFAvail_IsLinearized(pdf_avail)) {
    printf("Non-linearized path...\n");
    doc = FPDF_LoadCustomDocument(&file_access, NULL);
  } else {
    printf("Linearized path...\n");
    doc = FPDFAvail_GetDocument(pdf_avail, NULL);
  }

  (void) FPDF_GetDocPermissions(doc);
  (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);

  FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnviroument(doc, &form_callbacks);
  FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
  FPDF_SetFormFieldHighlightAlpha(form, 100);

  int first_page = FPDFAvail_GetFirstPageNum(doc);
  (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);

  int page_count = FPDF_GetPageCount(doc);
  for (int i = 0; i < page_count; ++i) {
    (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
  }

  FORM_DoDocumentJSAction(form);
  FORM_DoDocumentOpenAction(form);

  size_t rendered_pages = 0;
  size_t bad_pages = 0;
  for (int i = 0; i < page_count; ++i) {
    FPDF_PAGE page = FPDF_LoadPage(doc, i);
    if (!page) {
        bad_pages ++;
        continue;
    }
    FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
    FORM_OnAfterLoadPage(page, form);
    FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);

    int width = static_cast<int>(FPDF_GetPageWidth(page));
    int height = static_cast<int>(FPDF_GetPageHeight(page));
    FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
    FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);

    FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
    rendered_pages ++;

    FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
    int stride = FPDFBitmap_GetStride(bitmap);
    const char* buffer =
        reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));

    switch (format) {
#ifdef _WIN32
      case OUTPUT_BMP:
        WriteBmp(name, i, buffer, stride, width, height);
        break;

      case OUTPUT_EMF:
        WriteEmf(page, name, i);
        break;
#endif
      case OUTPUT_PPM:
        WritePpm(name, i, buffer, stride, width, height);
        break;
      default:
        break;
    }

    FPDFBitmap_Destroy(bitmap);

    FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
    FORM_OnBeforeClosePage(page, form);
    FPDFText_ClosePage(text_page);
    FPDF_ClosePage(page);
  }

  FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
  FPDFDOC_ExitFormFillEnviroument(form);
  FPDF_CloseDocument(doc);
  FPDFAvail_Destroy(pdf_avail);

  printf("Loaded, parsed and rendered %d pages.\n", rendered_pages);
  printf("Skipped %d bad pages.\n", bad_pages);
}

int main(int argc, const char* argv[]) {
  v8::V8::InitializeICU();
  OutputFormat format = OUTPUT_NONE;
  std::list<const char*> files;
  if (!ParseCommandLine(argc, argv, &format, &files)) {
    printf("Usage: pdfium_test [OPTION] [FILE]...\n");
    printf("--ppm    write page images <pdf-name>.<page-number>.ppm\n");
#ifdef _WIN32
    printf("--bmp    write page images <pdf-name>.<page-number>.bmp\n");
    printf("--emf    write page meta files <pdf-name>.<page-number>.emf\n");
#endif
    return 1;
  }

  FPDF_InitLibrary(NULL);

  UNSUPPORT_INFO unsuppored_info;
  memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
  unsuppored_info.version = 1;
  unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler;

  FSDK_SetUnSpObjProcessHandler(&unsuppored_info);

  while (!files.empty()) {
    const char* filename = files.front();
    files.pop_front();
    FILE* file = fopen(filename, "rb");
    if (!file) {
      fprintf(stderr, "Failed to open: %s\n", filename);
      continue;
    }
    (void) fseek(file, 0, SEEK_END);
    size_t len = ftell(file);
    (void) fseek(file, 0, SEEK_SET);
    char* pBuf = (char*) malloc(len);
    size_t ret = fread(pBuf, 1, len, file);
    (void) fclose(file);
    if (ret != len) {
      fprintf(stderr, "Failed to read: %s\n", filename);
    } else {
      RenderPdf(filename, pBuf, len, format);
    }
    free(pBuf);
  }

  FPDF_DestroyLibrary();

  return 0;
}
<commit_msg>Fix compile on mac: format string mismatch error.<commit_after>// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <list>
#include <string>
#include <utility>

#include "../fpdfsdk/include/fpdf_dataavail.h"
#include "../fpdfsdk/include/fpdf_ext.h"
#include "../fpdfsdk/include/fpdfformfill.h"
#include "../fpdfsdk/include/fpdftext.h"
#include "../fpdfsdk/include/fpdfview.h"
#include "v8/include/v8.h"

#ifdef _WIN32
  #define snprintf _snprintf
#endif

enum OutputFormat {
  OUTPUT_NONE,
  OUTPUT_PPM,
#ifdef _WIN32
  OUTPUT_BMP,
  OUTPUT_EMF,
#endif
};

static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
                     int stride, int width, int height) {
  const char* buffer = reinterpret_cast<const char*>(buffer_void);

  if (stride < 0 || width < 0 || height < 0)
    return;
  if (height > 0 && width > INT_MAX / height)
    return;
  int out_len = width * height;
  if (out_len > INT_MAX / 3)
    return;
  out_len *= 3;

  char filename[256];
  snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
  FILE* fp = fopen(filename, "wb");
  if (!fp)
    return;
  fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
  // Source data is B, G, R, unused.
  // Dest data is R, G, B.
  char* result = new char[out_len];
  if (result) {
    for (int h = 0; h < height; ++h) {
      const char* src_line = buffer + (stride * h);
      char* dest_line = result + (width * h * 3);
      for (int w = 0; w < width; ++w) {
        // R
        dest_line[w * 3] = src_line[(w * 4) + 2];
        // G
        dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
        // B
        dest_line[(w * 3) + 2] = src_line[w * 4];
      }
    }
    fwrite(result, out_len, 1, fp);
    delete [] result;
  }
  fclose(fp);
}

#ifdef _WIN32
static void WriteBmp(const char* pdf_name, int num, const void* buffer,
                     int stride, int width, int height) {
  if (stride < 0 || width < 0 || height < 0)
    return;
  if (height > 0 && width > INT_MAX / height)
    return;
  int out_len = stride * height;
  if (out_len > INT_MAX / 3)
    return;

  char filename[256];
  snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
  FILE* fp = fopen(filename, "wb");
  if (!fp)
    return;

  BITMAPINFO bmi = {0};
  bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
  bmi.bmiHeader.biWidth = width;
  bmi.bmiHeader.biHeight = -height;  // top-down image
  bmi.bmiHeader.biPlanes = 1;
  bmi.bmiHeader.biBitCount = 32;
  bmi.bmiHeader.biCompression = BI_RGB;
  bmi.bmiHeader.biSizeImage = 0;

  BITMAPFILEHEADER file_header = {0};
  file_header.bfType = 0x4d42;
  file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
  file_header.bfOffBits = file_header.bfSize - out_len;

  fwrite(&file_header, sizeof(file_header), 1, fp);
  fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
  fwrite(buffer, out_len, 1, fp);
  fclose(fp);
}

void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
  int width = static_cast<int>(FPDF_GetPageWidth(page));
  int height = static_cast<int>(FPDF_GetPageHeight(page));

  char filename[256];
  snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);

  HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL);

  HRGN rgn = CreateRectRgn(0, 0, width, height);
  SelectClipRgn(dc, rgn);
  DeleteObject(rgn);

  SelectObject(dc, GetStockObject(NULL_PEN));
  SelectObject(dc, GetStockObject(WHITE_BRUSH));
  // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
  Rectangle(dc, 0, 0, width + 1, height + 1);

  FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
                  FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);

  DeleteEnhMetaFile(CloseEnhMetaFile(dc));
}
#endif

int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) {
  printf("Form_Alert called.\n");
  return 0;
}

void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
  std::string feature = "Unknown";
  switch (type) {
    case FPDF_UNSP_DOC_XFAFORM:
      feature = "XFA";
      break;
    case FPDF_UNSP_DOC_PORTABLECOLLECTION:
      feature = "Portfolios_Packages";
      break;
    case FPDF_UNSP_DOC_ATTACHMENT:
    case FPDF_UNSP_ANNOT_ATTACHMENT:
      feature = "Attachment";
      break;
    case FPDF_UNSP_DOC_SECURITY:
      feature = "Rights_Management";
      break;
    case FPDF_UNSP_DOC_SHAREDREVIEW:
      feature = "Shared_Review";
      break;
    case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
    case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
    case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
      feature = "Shared_Form";
      break;
    case FPDF_UNSP_ANNOT_3DANNOT:
      feature = "3D";
      break;
    case FPDF_UNSP_ANNOT_MOVIE:
      feature = "Movie";
      break;
    case FPDF_UNSP_ANNOT_SOUND:
      feature = "Sound";
      break;
    case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
    case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
      feature = "Screen";
      break;
    case FPDF_UNSP_ANNOT_SIG:
      feature = "Digital_Signature";
      break;
  }
  printf("Unsupported feature: %s.\n", feature.c_str());
}

bool ParseCommandLine(int argc, const char* argv[], OutputFormat* output_format,
                      std::list<const char*>* files) {
  *output_format = OUTPUT_NONE;
  files->clear();

  int cur_arg = 1;
  for (; cur_arg < argc; ++cur_arg) {
    if (strcmp(argv[cur_arg], "--ppm") == 0)
      *output_format = OUTPUT_PPM;
#ifdef _WIN32
    else if (strcmp(argv[cur_arg], "--emf") == 0)
      *output_format = OUTPUT_EMF;
    else if (strcmp(argv[cur_arg], "--bmp") == 0)
      *output_format = OUTPUT_BMP;
#endif
    else
      break;
  }

  if (cur_arg > 2)  // Multiple options.
    return false;

  if (cur_arg >= argc)  // No input files.
    return false;

  for (int i = cur_arg; i < argc; i++)
    files->push_back(argv[i]);

  return true;
}

class TestLoader {
 public:
  TestLoader(const char* pBuf, size_t len);

  const char* m_pBuf;
  size_t m_Len;
};

TestLoader::TestLoader(const char* pBuf, size_t len)
    : m_pBuf(pBuf), m_Len(len) {
}

int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
              unsigned long size) {
  TestLoader* pLoader = (TestLoader*) param;
  if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
  memcpy(pBuf, pLoader->m_pBuf + pos, size);
  return 1;
}

bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
  return true;
}

void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
}

void RenderPdf(const char* name, const char* pBuf, size_t len,
               OutputFormat format) {
  printf("Rendering PDF file %s.\n", name);

  IPDF_JSPLATFORM platform_callbacks;
  memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
  platform_callbacks.version = 1;
  platform_callbacks.app_alert = Form_Alert;

  FPDF_FORMFILLINFO form_callbacks;
  memset(&form_callbacks, '\0', sizeof(form_callbacks));
  form_callbacks.version = 1;
  form_callbacks.m_pJsPlatform = &platform_callbacks;

  TestLoader loader(pBuf, len);

  FPDF_FILEACCESS file_access;
  memset(&file_access, '\0', sizeof(file_access));
  file_access.m_FileLen = static_cast<unsigned long>(len);
  file_access.m_GetBlock = Get_Block;
  file_access.m_Param = &loader;

  FX_FILEAVAIL file_avail;
  memset(&file_avail, '\0', sizeof(file_avail));
  file_avail.version = 1;
  file_avail.IsDataAvail = Is_Data_Avail;

  FX_DOWNLOADHINTS hints;
  memset(&hints, '\0', sizeof(hints));
  hints.version = 1;
  hints.AddSegment = Add_Segment;

  FPDF_DOCUMENT doc;
  FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);

  (void) FPDFAvail_IsDocAvail(pdf_avail, &hints);

  if (!FPDFAvail_IsLinearized(pdf_avail)) {
    printf("Non-linearized path...\n");
    doc = FPDF_LoadCustomDocument(&file_access, NULL);
  } else {
    printf("Linearized path...\n");
    doc = FPDFAvail_GetDocument(pdf_avail, NULL);
  }

  (void) FPDF_GetDocPermissions(doc);
  (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);

  FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnviroument(doc, &form_callbacks);
  FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
  FPDF_SetFormFieldHighlightAlpha(form, 100);

  int first_page = FPDFAvail_GetFirstPageNum(doc);
  (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);

  int page_count = FPDF_GetPageCount(doc);
  for (int i = 0; i < page_count; ++i) {
    (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
  }

  FORM_DoDocumentJSAction(form);
  FORM_DoDocumentOpenAction(form);

  size_t rendered_pages = 0;
  size_t bad_pages = 0;
  for (int i = 0; i < page_count; ++i) {
    FPDF_PAGE page = FPDF_LoadPage(doc, i);
    if (!page) {
        bad_pages ++;
        continue;
    }
    FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
    FORM_OnAfterLoadPage(page, form);
    FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);

    int width = static_cast<int>(FPDF_GetPageWidth(page));
    int height = static_cast<int>(FPDF_GetPageHeight(page));
    FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
    FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);

    FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
    rendered_pages ++;

    FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
    int stride = FPDFBitmap_GetStride(bitmap);
    const char* buffer =
        reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));

    switch (format) {
#ifdef _WIN32
      case OUTPUT_BMP:
        WriteBmp(name, i, buffer, stride, width, height);
        break;

      case OUTPUT_EMF:
        WriteEmf(page, name, i);
        break;
#endif
      case OUTPUT_PPM:
        WritePpm(name, i, buffer, stride, width, height);
        break;
      default:
        break;
    }

    FPDFBitmap_Destroy(bitmap);

    FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
    FORM_OnBeforeClosePage(page, form);
    FPDFText_ClosePage(text_page);
    FPDF_ClosePage(page);
  }

  FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
  FPDFDOC_ExitFormFillEnviroument(form);
  FPDF_CloseDocument(doc);
  FPDFAvail_Destroy(pdf_avail);

  printf("Loaded, parsed and rendered %zu pages.\n", rendered_pages);
  printf("Skipped %zu bad pages.\n", bad_pages);
}

int main(int argc, const char* argv[]) {
  v8::V8::InitializeICU();
  OutputFormat format = OUTPUT_NONE;
  std::list<const char*> files;
  if (!ParseCommandLine(argc, argv, &format, &files)) {
    printf("Usage: pdfium_test [OPTION] [FILE]...\n");
    printf("--ppm    write page images <pdf-name>.<page-number>.ppm\n");
#ifdef _WIN32
    printf("--bmp    write page images <pdf-name>.<page-number>.bmp\n");
    printf("--emf    write page meta files <pdf-name>.<page-number>.emf\n");
#endif
    return 1;
  }

  FPDF_InitLibrary(NULL);

  UNSUPPORT_INFO unsuppored_info;
  memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
  unsuppored_info.version = 1;
  unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler;

  FSDK_SetUnSpObjProcessHandler(&unsuppored_info);

  while (!files.empty()) {
    const char* filename = files.front();
    files.pop_front();
    FILE* file = fopen(filename, "rb");
    if (!file) {
      fprintf(stderr, "Failed to open: %s\n", filename);
      continue;
    }
    (void) fseek(file, 0, SEEK_END);
    size_t len = ftell(file);
    (void) fseek(file, 0, SEEK_SET);
    char* pBuf = (char*) malloc(len);
    size_t ret = fread(pBuf, 1, len, file);
    (void) fclose(file);
    if (ret != len) {
      fprintf(stderr, "Failed to read: %s\n", filename);
    } else {
      RenderPdf(filename, pBuf, len, format);
    }
    free(pBuf);
  }

  FPDF_DestroyLibrary();

  return 0;
}
<|endoftext|>


<commit_before>#include "ExportFolderPropertiesOperation.h"
#include "MAPIFolders.h"

struct ID
{
	WORD replid;
	BYTE globcnt[6];
};

ExportFolderPropertiesOperation::ExportFolderPropertiesOperation(tstring *pstrBasePath, tstring *pstrMailbox, UserArgs::ActionScope nScope, Log *log, tstring *pstrPropTags, Log *exportFile)
	:OperationBase(pstrBasePath, pstrMailbox, nScope, log)
{
	this->exportFile = exportFile;
	this->pstrPropTags = pstrPropTags;
}

ExportFolderPropertiesOperation::~ExportFolderPropertiesOperation()
{
}

HRESULT ExportFolderPropertiesOperation::Initialize(void)
{
	HRESULT hr = S_OK;
	std::vector<tstring> splitPropStrings;

	CORg(OperationBase::Initialize());

	splitPropStrings = Split(this->pstrPropTags->c_str(), (_T(',')));
	lpPropsToExport = NULL;
	CORg(MAPIAllocateBuffer(CbNewSPropTagArray(splitPropStrings.size()),
		(LPVOID*)&lpPropsToExport));
	this->lpPropsToExport->cValues = splitPropStrings.size();
	for (INT x = 0; x < this->lpPropsToExport->cValues; x++)
	{
		ULONG ptag = std::wcstoul(splitPropStrings.at(x).c_str(), NULL, 16);
		if (ptag == 0xffffffff)
		{
			hr = E_FAIL;
			*pLog << "Failed to parse property tag: " << splitPropStrings.at(x);
			goto Error;
		}
		else
		{
			lpPropsToExport->aulPropTag[x] = ptag;
		}
	}

	*exportFile << _T("Folder Path,");
	for (INT x = 0; x < this->lpPropsToExport->cValues; x++)
	{
		*exportFile << std::hex << this->lpPropsToExport->aulPropTag[x];
		if (x + 1 < this->lpPropsToExport->cValues)
		{
			*exportFile << _T(",");
		}
	}

	*exportFile << "\n";

Error:
	return hr;
}

void ExportFolderPropertiesOperation::ProcessFolder(LPMAPIFOLDER folder, tstring folderPath)
{
	HRESULT hr = S_OK;

	*pLog << "Exporting properties for folder: " << folderPath.c_str() << "\n";

	ULONG cCount = 0;
	SPropValue *rgprops = NULL;
	CORg(folder->GetProps(lpPropsToExport, MAPI_UNICODE, &cCount, &rgprops));
	*exportFile << folderPath.c_str() << _T(",");
	for (ULONG y = 0; y < cCount; y++)
	{
		SPropValue thisProp = rgprops[y];
		if (PROP_TYPE(thisProp.ulPropTag) == PT_STRING8)
		{
			*exportFile << thisProp.Value.lpszA;
		}
		else if (PROP_TYPE(thisProp.ulPropTag) == PT_UNICODE)
		{
			*exportFile << thisProp.Value.lpszW;
		}
		else if (PROP_TYPE(thisProp.ulPropTag) == PT_I8)
		{
			if (thisProp.ulPropTag == 0x67480014)
			{
				ID* pid = (ID*)&thisProp.Value.li.QuadPart;
				ULONG ul = 0;
				for (int i = 0; i < 6; ++i)
				{
					ul <<= 8;
					ul += pid->globcnt[i];
				}

				*exportFile << std::hex << pid->replid << _T("-") << ul;

			}
			else
			{
				*exportFile << std::hex << thisProp.Value.li.QuadPart;
			}
		}

		if (y + 1 < cCount)
		{
			*exportFile << _T(",");
		}
	}

	*exportFile << "\n";

	MAPIFreeBuffer(rgprops);

Cleanup:
	return;

Error:
	goto Cleanup;
}<commit_msg>- Updated ExportFolderProperties to support PT_LONG, PT_BOOLEAN, PT_BINARY - Changed it to output I8 in base 10 when it's not a FID<commit_after>#include "ExportFolderPropertiesOperation.h"
#include "MAPIFolders.h"

struct ID
{
	WORD replid;
	BYTE globcnt[6];
};

ExportFolderPropertiesOperation::ExportFolderPropertiesOperation(tstring *pstrBasePath, tstring *pstrMailbox, UserArgs::ActionScope nScope, Log *log, tstring *pstrPropTags, Log *exportFile)
	:OperationBase(pstrBasePath, pstrMailbox, nScope, log)
{
	this->exportFile = exportFile;
	this->pstrPropTags = pstrPropTags;
}

ExportFolderPropertiesOperation::~ExportFolderPropertiesOperation()
{
}

HRESULT ExportFolderPropertiesOperation::Initialize(void)
{
	HRESULT hr = S_OK;
	std::vector<tstring> splitPropStrings;

	CORg(OperationBase::Initialize());

	splitPropStrings = Split(this->pstrPropTags->c_str(), (_T(',')));
	lpPropsToExport = NULL;
	CORg(MAPIAllocateBuffer(CbNewSPropTagArray(splitPropStrings.size()),
		(LPVOID*)&lpPropsToExport));
	this->lpPropsToExport->cValues = splitPropStrings.size();
	for (INT x = 0; x < this->lpPropsToExport->cValues; x++)
	{
		ULONG ptag = std::wcstoul(splitPropStrings.at(x).c_str(), NULL, 16);
		if (ptag == 0xffffffff)
		{
			hr = E_FAIL;
			*pLog << "Failed to parse property tag: " << splitPropStrings.at(x);
			goto Error;
		}
		else
		{
			lpPropsToExport->aulPropTag[x] = ptag;
		}
	}

	*exportFile << _T("Folder Path,");
	for (INT x = 0; x < this->lpPropsToExport->cValues; x++)
	{
		*exportFile << std::hex << this->lpPropsToExport->aulPropTag[x];
		if (x + 1 < this->lpPropsToExport->cValues)
		{
			*exportFile << _T(",");
		}
	}

	*exportFile << "\n";

Error:
	return hr;
}

void ExportFolderPropertiesOperation::ProcessFolder(LPMAPIFOLDER folder, tstring folderPath)
{
	HRESULT hr = S_OK;

	*pLog << "Exporting properties for folder: " << folderPath.c_str() << "\n";

	ULONG cCount = 0;
	SPropValue *rgprops = NULL;
	CORg(folder->GetProps(lpPropsToExport, MAPI_UNICODE, &cCount, &rgprops));
	*exportFile << folderPath.c_str() << _T(",");
	for (ULONG y = 0; y < cCount; y++)
	{
		SPropValue thisProp = rgprops[y];
		if (PROP_TYPE(thisProp.ulPropTag) == PT_STRING8)
		{
			*exportFile << thisProp.Value.lpszA;
		}
		else if (PROP_TYPE(thisProp.ulPropTag) == PT_UNICODE)
		{
			*exportFile << thisProp.Value.lpszW;
		}
		else if (PROP_TYPE(thisProp.ulPropTag) == PT_I8)
		{
			if (thisProp.ulPropTag == 0x67480014)
			{
				ID* pid = (ID*)&thisProp.Value.li.QuadPart;
				ULONG ul = 0;
				for (int i = 0; i < 6; ++i)
				{
					ul <<= 8;
					ul += pid->globcnt[i];
				}

				*exportFile << std::hex << pid->replid << _T("-") << ul;

			}
			else
			{
				*exportFile << thisProp.Value.li.QuadPart;
			}
		}
		else if (PROP_TYPE(thisProp.ulPropTag) == PT_LONG)
		{
			*exportFile << thisProp.Value.l;
		}
		else if (PROP_TYPE(thisProp.ulPropTag) == PT_BOOLEAN)
		{
			if (thisProp.Value.b)
			{
				*exportFile << "True";
			}
			else
			{
				*exportFile << "False";
			}
		}
		else if (PROP_TYPE(thisProp.ulPropTag) == PT_BINARY)
		{
			for (ULONG i = 0; i < thisProp.Value.bin.cb; i++)
			{
				BYTE oneByte = thisProp.Value.bin.lpb[i];
				if (oneByte < 0x10)
				{
					*exportFile << _T("0");
				}

				*exportFile << std::hex << oneByte;
			}
		}
		else
		{
			*exportFile << _T("MAPIFolders cannot export this property type");
		}

		if (y + 1 < cCount)
		{
			*exportFile << _T(",");
		}
	}

	*exportFile << "\n";

	MAPIFreeBuffer(rgprops);

Cleanup:
	return;

Error:
	goto Cleanup;
}<|endoftext|>