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

#include "TimeLayer.h"
#include "Config.h"
#include "Hash.h"
#include "Layer.h"
#include "LonLatToXYTransformation.h"
#include "State.h"
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include <ctpp2/CDT.hpp>
#include <fmt/time.h>
#include <gis/Box.h>
#include <spine/Exception.h>
#include <spine/Json.h>
// TODO:
#include <boost/timer/timer.hpp>

namespace SmartMet
{
namespace Plugin
{
namespace Dali
{
// ----------------------------------------------------------------------
/*!
 * \brief Initialize from JSON
 */
// ----------------------------------------------------------------------

void TimeLayer::init(const Json::Value& theJson,
                     const State& theState,
                     const Config& theConfig,
                     const Properties& theProperties)
{
  try
  {
    // Initialize this variable only once!
    now = boost::posix_time::second_clock::universal_time();

    if (!theJson.isObject())
      throw Spine::Exception(BCP, "Time-layer JSON is not a JSON object");

    Layer::init(theJson, theState, theConfig, theProperties);

    // Extract member values

    Json::Value nulljson;

    auto json = theJson.get("timezone", nulljson);
    if (!json.isNull())
      timezone = json.asString();

    json = theJson.get("prefix", nulljson);
    if (!json.isNull())
      prefix = json.asString();

    json = theJson.get("suffix", nulljson);
    if (!json.isNull())
      suffix = json.asString();

    json = theJson.get("timestamp", nulljson);
    if (!json.isNull())
    {
      if (json.isArray())
      {
        for (unsigned int i = 0; i < json.size(); i++)
          timestamp.push_back(json[i].asString());
      }
      else
        timestamp.push_back(json.asString());
    }

    json = theJson.get("formatter", nulljson);
    if (!json.isNull())
    {
      formatter = json.asString();
      if (formatter != "boost" && formatter != "strftime" && formatter != "fmt")
        throw Spine::Exception(BCP, "Unknown time formatter '" + formatter + "'");
    }

    json = theJson.get("format", nulljson);
    if (!json.isNull())
    {
      if (json.isArray())
      {
        for (unsigned int i = 0; i < json.size(); i++)
          format.push_back(json[i].asString());
      }
      else
        format.push_back(json.asString());
    }

    auto longitudeJson = theJson.get("longitude", nulljson);
    auto latitudeJson = theJson.get("latitude", nulljson);
    if (!longitudeJson.isNull() && !latitudeJson.isNull())
    {
      double longitude = longitudeJson.asDouble();
      double latitude = latitudeJson.asDouble();
      double xCoord = 0;
      double yCoord = 0;
      LonLatToXYTransformation transformation(projection);
      transformation.transform(longitude, latitude, xCoord, yCoord);
      x = xCoord;
      y = yCoord;
    }
    else
    {
      json = theJson.get("x", nulljson);
      if (!json.isNull())
        x = json.asInt();

      json = theJson.get("y", nulljson);
      if (!json.isNull())
        y = json.asInt();
    }
  }
  catch (...)
  {
    throw Spine::Exception::Trace(BCP, "Operation failed!");
  }
}

// ----------------------------------------------------------------------
/*!
 * \brief Generate the layer details into the template hash
 */
// ----------------------------------------------------------------------

void TimeLayer::generate(CTPP::CDT& theGlobals, CTPP::CDT& theLayersCdt, State& theState)
{
  try
  {
    if (!validLayer(theState))
      return;

    // Time execution

    std::string report = "TimeLayer::generate finished in %t sec CPU, %w sec real\n";
    boost::movelib::unique_ptr<boost::timer::auto_cpu_timer> timer;
    if (theState.useTimer())
      timer = boost::movelib::make_unique<boost::timer::auto_cpu_timer>(2, report);

    // Establish the data
    auto q = getModel(theState);

    // Establish the valid time

    auto valid_time = getValidTime();

    // Update the globals

    if (css)
    {
      std::string name = theState.getCustomer() + "/" + *css;
      theGlobals["css"][name] = theState.getStyle(*css);
    }

    // Generate a <text>cdata<text> layer

    CTPP::CDT text_cdt(CTPP::CDT::HASH_VAL);
    text_cdt["start"] = "<text";
    text_cdt["end"] = "</text>";

    // Add attributes to the text tag
    theState.addAttributes(theGlobals, text_cdt, attributes);

    // Override X and Y coordinates if so requested

    if (x || y)
    {
      projection.update(q);
      const auto& box = projection.getBox();

      if (x)
        text_cdt["attributes"]["x"] = ((*x) >= 0 ? (*x) : box.width() + (*x));
      if (y)
        text_cdt["attributes"]["y"] = ((*y) >= 0 ? (*y) : box.height() + (*y));
    }

    // Establish the text to be added

    auto tz =
        theState.getGeoEngine().getTimeZones().time_zone_from_string(timezone ? *timezone : "UTC");

    // Set defaults and validate input.

    if (timestamp.empty())
      timestamp.push_back("validtime");

    if (format.empty())
      format.push_back("%Y-%m-%d %H:%M");

    if (timestamp.size() != format.size())
      throw Spine::Exception(BCP,
                             "TimeLayer timestamp and format arrays should be of the same size");

    // Create the output
    std::ostringstream msg;
    msg << prefix;

    for (auto i = 0ul; i < timestamp.size(); i++)
    {
      auto name = timestamp[i];
      auto fmt = format[i];

      if (name.empty())
        throw Spine::Exception(BCP, "TimeLayer timestamp setting cannot be an empty string");
      if (fmt.empty())
        throw Spine::Exception(BCP, "TimeLayer format setting cannot be an empty string");

      boost::optional<boost::local_time::local_date_time> loctime;
      boost::optional<boost::posix_time::time_duration> duration;

      if (name == "validtime")
      {
        loctime = boost::local_time::local_date_time(valid_time, tz);
      }
      else if (name == "origintime")
      {
        if (!q)
          throw Spine::Exception(BCP, "Origintime not avaible for TimeLayer");
        loctime = boost::local_time::local_date_time(q->originTime(), tz);
      }
      else if (name == "wallclock" || name == "now")
      {
        loctime = boost::local_time::local_date_time(now, tz);
      }
      else if (name == "starttime")
      {
        auto tmp = boost::local_time::local_date_time(valid_time, tz);
        if (interval_start)
          tmp -= boost::posix_time::minutes(*interval_start);
        loctime = tmp;
      }
      else if (name == "endtime")
      {
        auto tmp = boost::local_time::local_date_time(valid_time, tz);
        if (interval_end)
          tmp += boost::posix_time::minutes(*interval_end);
        loctime = tmp;
      }
      else if (name == "leadtime")
      {
        if (!q)
          throw Spine::Exception(BCP, "Origintime not avaible for TimeLayer");
        duration = valid_time - q->originTime();
      }
      else if (name == "leadhour")
      {
        if (!q)
          throw Spine::Exception(BCP, "Origintime not avaible for TimeLayer");
        boost::posix_time::ptime ot = q->originTime();
        duration =
            valid_time - ot + ot.time_of_day() - boost::posix_time::hours(ot.time_of_day().hours());
      }
      else if (name == "time_offset")
      {
        duration = boost::posix_time::minutes(time_offset ? *time_offset : 0);
      }
      else if (name == "interval_start")
      {
        duration = boost::posix_time::minutes(interval_start ? *interval_start : 0);
      }
      else if (name == "interval_end")
      {
        duration = boost::posix_time::minutes(interval_end ? *interval_end : 0);
      }
      else
      {
        loctime = boost::local_time::local_date_time(valid_time, tz) +
                  Fmi::TimeParser::parse_duration(name);
      }

      // durations are always formatted with boost
      if (!loctime || formatter == "boost")
      {
        try
        {
          auto* facet = new boost::posix_time::time_facet(fmt.c_str());
          msg.imbue(std::locale(msg.getloc(), facet));

          if (loctime)
            msg << loctime->local_time();
          else
            msg << *duration;
        }
        catch (...)
        {
          throw Spine::Exception::Trace(BCP, "Failed to format time with Boost")
              .addParameter("format", "'" + fmt + "'");
        }
      }
      else if (formatter == "strftime")
      {
        auto timeinfo = to_tm(loctime->local_time());
        char buffer[100];
        if (!strftime(buffer, 100, fmt.c_str(), &timeinfo))
        {
          throw Spine::Exception(BCP, "Failed to format a non-empty time string with strftime")
              .addParameter("format", "'" + fmt + "'");
        }
        msg << buffer;
      }
      else if (formatter == "fmt")
      {
        try
        {
          auto timeinfo = to_tm(loctime->local_time());
          msg << fmt::format(fmt, timeinfo);
        }
        catch (...)
        {
          throw Spine::Exception::Trace(BCP, "Failed to format time with fmt")
              .addParameter("format", "'" + fmt + "'");
        }
      }

      else
        throw Spine::Exception(BCP, "Unknown TimeLayer time formatter '" + formatter + "'");
    }
    msg << suffix;

    text_cdt["cdata"] = msg.str();
    theLayersCdt.PushBack(text_cdt);
  }
  catch (...)
  {
    throw Spine::Exception::Trace(BCP, "Operation failed!");
  }
}

// ----------------------------------------------------------------------
/*!
 * \brief Hash value
 */
// ----------------------------------------------------------------------

std::size_t TimeLayer::hash_value(const State& theState) const
{
  try
  {
    auto hash = Layer::hash_value(theState);
    boost::hash_combine(hash, Engine::Querydata::hash_value(getModel(theState)));
    boost::hash_combine(hash, Dali::hash_value(timezone));
    boost::hash_combine(hash, Dali::hash_value(prefix));
    boost::hash_combine(hash, Dali::hash_value(suffix));
    boost::hash_combine(hash, Dali::hash_value(timestamp));
    boost::hash_combine(hash, Dali::hash_value(format));
    boost::hash_combine(hash, Dali::hash_value(formatter));
    boost::hash_combine(hash, Dali::hash_value(x));
    boost::hash_combine(hash, Dali::hash_value(y));
    boost::hash_combine(hash, Dali::hash_value(longitude));
    boost::hash_combine(hash, Dali::hash_value(latitude));

    // TODO: The timestamp to be drawn may depend on the wall clock - must include it
    // in the hash. A better solution would be to format the timestamp and
    // then calculate the hash.

    for (const auto& name : timestamp)
    {
      if (name == "wallclock" || name == "now")
        boost::hash_combine(hash, Dali::hash_value(now));
    }

    return hash;
  }
  catch (...)
  {
    throw Spine::Exception::Trace(BCP, "Operation failed!");
  }
}

}  // namespace Dali
}  // namespace Plugin
}  // namespace SmartMet
<commit_msg>do not implicitly decay an array into a pointer<commit_after>//======================================================================

#include "TimeLayer.h"
#include "Config.h"
#include "Hash.h"
#include "Layer.h"
#include "LonLatToXYTransformation.h"
#include "State.h"
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include <ctpp2/CDT.hpp>
#include <fmt/time.h>
#include <gis/Box.h>
#include <spine/Exception.h>
#include <spine/Json.h>
// TODO:
#include <boost/timer/timer.hpp>

namespace SmartMet
{
namespace Plugin
{
namespace Dali
{
// ----------------------------------------------------------------------
/*!
 * \brief Initialize from JSON
 */
// ----------------------------------------------------------------------

void TimeLayer::init(const Json::Value& theJson,
                     const State& theState,
                     const Config& theConfig,
                     const Properties& theProperties)
{
  try
  {
    // Initialize this variable only once!
    now = boost::posix_time::second_clock::universal_time();

    if (!theJson.isObject())
      throw Spine::Exception(BCP, "Time-layer JSON is not a JSON object");

    Layer::init(theJson, theState, theConfig, theProperties);

    // Extract member values

    Json::Value nulljson;

    auto json = theJson.get("timezone", nulljson);
    if (!json.isNull())
      timezone = json.asString();

    json = theJson.get("prefix", nulljson);
    if (!json.isNull())
      prefix = json.asString();

    json = theJson.get("suffix", nulljson);
    if (!json.isNull())
      suffix = json.asString();

    json = theJson.get("timestamp", nulljson);
    if (!json.isNull())
    {
      if (json.isArray())
      {
        for (unsigned int i = 0; i < json.size(); i++)
          timestamp.push_back(json[i].asString());
      }
      else
        timestamp.push_back(json.asString());
    }

    json = theJson.get("formatter", nulljson);
    if (!json.isNull())
    {
      formatter = json.asString();
      if (formatter != "boost" && formatter != "strftime" && formatter != "fmt")
        throw Spine::Exception(BCP, "Unknown time formatter '" + formatter + "'");
    }

    json = theJson.get("format", nulljson);
    if (!json.isNull())
    {
      if (json.isArray())
      {
        for (unsigned int i = 0; i < json.size(); i++)
          format.push_back(json[i].asString());
      }
      else
        format.push_back(json.asString());
    }

    auto longitudeJson = theJson.get("longitude", nulljson);
    auto latitudeJson = theJson.get("latitude", nulljson);
    if (!longitudeJson.isNull() && !latitudeJson.isNull())
    {
      double longitude = longitudeJson.asDouble();
      double latitude = latitudeJson.asDouble();
      double xCoord = 0;
      double yCoord = 0;
      LonLatToXYTransformation transformation(projection);
      transformation.transform(longitude, latitude, xCoord, yCoord);
      x = xCoord;
      y = yCoord;
    }
    else
    {
      json = theJson.get("x", nulljson);
      if (!json.isNull())
        x = json.asInt();

      json = theJson.get("y", nulljson);
      if (!json.isNull())
        y = json.asInt();
    }
  }
  catch (...)
  {
    throw Spine::Exception::Trace(BCP, "Operation failed!");
  }
}

// ----------------------------------------------------------------------
/*!
 * \brief Generate the layer details into the template hash
 */
// ----------------------------------------------------------------------

void TimeLayer::generate(CTPP::CDT& theGlobals, CTPP::CDT& theLayersCdt, State& theState)
{
  try
  {
    if (!validLayer(theState))
      return;

    // Time execution

    std::string report = "TimeLayer::generate finished in %t sec CPU, %w sec real\n";
    boost::movelib::unique_ptr<boost::timer::auto_cpu_timer> timer;
    if (theState.useTimer())
      timer = boost::movelib::make_unique<boost::timer::auto_cpu_timer>(2, report);

    // Establish the data
    auto q = getModel(theState);

    // Establish the valid time

    auto valid_time = getValidTime();

    // Update the globals

    if (css)
    {
      std::string name = theState.getCustomer() + "/" + *css;
      theGlobals["css"][name] = theState.getStyle(*css);
    }

    // Generate a <text>cdata<text> layer

    CTPP::CDT text_cdt(CTPP::CDT::HASH_VAL);
    text_cdt["start"] = "<text";
    text_cdt["end"] = "</text>";

    // Add attributes to the text tag
    theState.addAttributes(theGlobals, text_cdt, attributes);

    // Override X and Y coordinates if so requested

    if (x || y)
    {
      projection.update(q);
      const auto& box = projection.getBox();

      if (x)
        text_cdt["attributes"]["x"] = ((*x) >= 0 ? (*x) : box.width() + (*x));
      if (y)
        text_cdt["attributes"]["y"] = ((*y) >= 0 ? (*y) : box.height() + (*y));
    }

    // Establish the text to be added

    auto tz =
        theState.getGeoEngine().getTimeZones().time_zone_from_string(timezone ? *timezone : "UTC");

    // Set defaults and validate input.

    if (timestamp.empty())
      timestamp.push_back("validtime");

    if (format.empty())
      format.push_back("%Y-%m-%d %H:%M");

    if (timestamp.size() != format.size())
      throw Spine::Exception(BCP,
                             "TimeLayer timestamp and format arrays should be of the same size");

    // Create the output
    std::ostringstream msg;
    msg << prefix;

    for (auto i = 0ul; i < timestamp.size(); i++)
    {
      auto name = timestamp[i];
      auto fmt = format[i];

      if (name.empty())
        throw Spine::Exception(BCP, "TimeLayer timestamp setting cannot be an empty string");
      if (fmt.empty())
        throw Spine::Exception(BCP, "TimeLayer format setting cannot be an empty string");

      boost::optional<boost::local_time::local_date_time> loctime;
      boost::optional<boost::posix_time::time_duration> duration;

      if (name == "validtime")
      {
        loctime = boost::local_time::local_date_time(valid_time, tz);
      }
      else if (name == "origintime")
      {
        if (!q)
          throw Spine::Exception(BCP, "Origintime not avaible for TimeLayer");
        loctime = boost::local_time::local_date_time(q->originTime(), tz);
      }
      else if (name == "wallclock" || name == "now")
      {
        loctime = boost::local_time::local_date_time(now, tz);
      }
      else if (name == "starttime")
      {
        auto tmp = boost::local_time::local_date_time(valid_time, tz);
        if (interval_start)
          tmp -= boost::posix_time::minutes(*interval_start);
        loctime = tmp;
      }
      else if (name == "endtime")
      {
        auto tmp = boost::local_time::local_date_time(valid_time, tz);
        if (interval_end)
          tmp += boost::posix_time::minutes(*interval_end);
        loctime = tmp;
      }
      else if (name == "leadtime")
      {
        if (!q)
          throw Spine::Exception(BCP, "Origintime not avaible for TimeLayer");
        duration = valid_time - q->originTime();
      }
      else if (name == "leadhour")
      {
        if (!q)
          throw Spine::Exception(BCP, "Origintime not avaible for TimeLayer");
        boost::posix_time::ptime ot = q->originTime();
        duration =
            valid_time - ot + ot.time_of_day() - boost::posix_time::hours(ot.time_of_day().hours());
      }
      else if (name == "time_offset")
      {
        duration = boost::posix_time::minutes(time_offset ? *time_offset : 0);
      }
      else if (name == "interval_start")
      {
        duration = boost::posix_time::minutes(interval_start ? *interval_start : 0);
      }
      else if (name == "interval_end")
      {
        duration = boost::posix_time::minutes(interval_end ? *interval_end : 0);
      }
      else
      {
        loctime = boost::local_time::local_date_time(valid_time, tz) +
                  Fmi::TimeParser::parse_duration(name);
      }

      // durations are always formatted with boost
      if (!loctime || formatter == "boost")
      {
        try
        {
          auto* facet = new boost::posix_time::time_facet(fmt.c_str());
          msg.imbue(std::locale(msg.getloc(), facet));

          if (loctime)
            msg << loctime->local_time();
          else
            msg << *duration;
        }
        catch (...)
        {
          throw Spine::Exception::Trace(BCP, "Failed to format time with Boost")
              .addParameter("format", "'" + fmt + "'");
        }
      }
      else if (formatter == "strftime")
      {
        auto timeinfo = to_tm(loctime->local_time());
        char buffer[100];
        if (!strftime(static_cast<char*>(buffer), 100, fmt.c_str(), &timeinfo))
        {
          throw Spine::Exception(BCP, "Failed to format a non-empty time string with strftime")
              .addParameter("format", "'" + fmt + "'");
        }
        msg << static_cast<char*>(buffer);
      }
      else if (formatter == "fmt")
      {
        try
        {
          auto timeinfo = to_tm(loctime->local_time());
          msg << fmt::format(fmt, timeinfo);
        }
        catch (...)
        {
          throw Spine::Exception::Trace(BCP, "Failed to format time with fmt")
              .addParameter("format", "'" + fmt + "'");
        }
      }

      else
        throw Spine::Exception(BCP, "Unknown TimeLayer time formatter '" + formatter + "'");
    }
    msg << suffix;

    text_cdt["cdata"] = msg.str();
    theLayersCdt.PushBack(text_cdt);
  }
  catch (...)
  {
    throw Spine::Exception::Trace(BCP, "Operation failed!");
  }
}

// ----------------------------------------------------------------------
/*!
 * \brief Hash value
 */
// ----------------------------------------------------------------------

std::size_t TimeLayer::hash_value(const State& theState) const
{
  try
  {
    auto hash = Layer::hash_value(theState);
    boost::hash_combine(hash, Engine::Querydata::hash_value(getModel(theState)));
    boost::hash_combine(hash, Dali::hash_value(timezone));
    boost::hash_combine(hash, Dali::hash_value(prefix));
    boost::hash_combine(hash, Dali::hash_value(suffix));
    boost::hash_combine(hash, Dali::hash_value(timestamp));
    boost::hash_combine(hash, Dali::hash_value(format));
    boost::hash_combine(hash, Dali::hash_value(formatter));
    boost::hash_combine(hash, Dali::hash_value(x));
    boost::hash_combine(hash, Dali::hash_value(y));
    boost::hash_combine(hash, Dali::hash_value(longitude));
    boost::hash_combine(hash, Dali::hash_value(latitude));

    // TODO: The timestamp to be drawn may depend on the wall clock - must include it
    // in the hash. A better solution would be to format the timestamp and
    // then calculate the hash.

    for (const auto& name : timestamp)
    {
      if (name == "wallclock" || name == "now")
        boost::hash_combine(hash, Dali::hash_value(now));
    }

    return hash;
  }
  catch (...)
  {
    throw Spine::Exception::Trace(BCP, "Operation failed!");
  }
}

}  // namespace Dali
}  // namespace Plugin
}  // namespace SmartMet
<|endoftext|>


<commit_before>/*
  Copyright(C) 2014 Naoya Murakami <naoya@createfield.com>

  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.

  This file includes the original word2vec's code. https://code.google.com/p/word2vec/
  The following is the header of the file:

    Copyright 2013 Google Inc. 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 <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <stdexcept>
#include <vector>

#include <math.h>
#include <malloc.h>

#include <unicode/normlzr.h>
#include <re2/re2.h>
#include <google/gflags.h>

DEFINE_string(file_path, "", "Input binary file learned by word2vec");
DEFINE_string(input, "", "Input expression string");
DEFINE_int32(output, 1, "1:word,discatnce 2:word 3:split by commma 4:split by tab");
DEFINE_int32(offset, 0, "offset");
DEFINE_int32(limit, -1, "limit -1:all");
DEFINE_double(threshold, 0, "threshold (ex. --threshold 0.75)");
DEFINE_bool(no_normalize, false, "Don't use NFKC normalize");
DEFINE_string(term_filter, "", "Filter by regular expresion pattern to term");
DEFINE_string(output_filter, "", "Cut by regular expresion pattern to term");
DEFINE_bool(server, false, "Server mode");
DEFINE_string(ip, "", "IP address");
DEFINE_string(port, "", "Port number");
DEFINE_bool(h, false, "Help message");

using namespace std;

const long long max_size = 2000;         // max length of strings
const long long N = 40;                  // number of closest words that will be shown
const long long max_w = 50;              // max length of vocabulary entries

long long words, size = 0;
float *M = NULL;
char *vocab = NULL;

string
normalize(const string str)
{
  icu::UnicodeString src;
  icu::UnicodeString dest;
  src = str.c_str();
  UErrorCode status;
  status = U_ZERO_ERROR;
  Normalizer::normalize(src, UNORM_NFKC, 0, dest, status);
  if(U_FAILURE(status)) {
    throw runtime_error("Unicode normalization failed");
  }
  string result;
  dest.toUTF8String(result);
  transform(result.begin(), result.end(), result.begin(), ::tolower);
  return result;
}

bool
word2vec_load(const char *file_name)
{
  FILE *f;
  float len;
  long long a, b;
  char ch;

  f = fopen(file_name, "rb");
  if (f == NULL) {
    printf("Input file not found : %s\n", file_name);
    return false;
  }

  fscanf(f, "%lld", &words);
  fscanf(f, "%lld", &size);
  vocab = (char *)malloc((long long)words * max_w * sizeof(char));
  M = (float *)malloc((long long)words * (long long)size * sizeof(float));
  if (M == NULL) {
    printf("Cannot allocate memory: %lld MB %lld %lld",
           (long long)words * size * sizeof(float) / 1048576, words, size);
    return false;
  }
  for (b = 0; b < words; b++) {
    fscanf(f, "%s%c", &vocab[b * max_w], &ch);
    for (a = 0; a < size; a++) fread(&M[a + b * size], sizeof(float), 1, f);
    len = 0;
    for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
    len = sqrt(len);
    for (a = 0; a < size; a++) M[a + b * size] /= len;
  }
  fclose(f);

  return true;
}

vector<string>
split(const string &str, char delim){
  vector<string> res;
  size_t current = 0, found;
  while((found = str.find_first_of(delim, current)) != string::npos){
    res.push_back(string(str, current, found - current));
    current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

int
calc(string str, int output, int offset, int limit, double threshold,
     bool no_normalize, const char *term_filter, const char *output_filter){
  char st1[max_size];
  char bestw[N][max_size];
  char st[100][max_size];
  char op[100];
  float dist, len, bestd[N], vec[max_size];
  long long a, b, c, d, cn, bi[100];

  for (a = 0; a < N; a++) bestd[a] = 0;
  for (a = 0; a < N; a++) bestw[a][0] = 0;

  st1[0] = 0;

  for(unsigned int i = 0; i < 100; i++){
    st[i][0] = 0;
    op[i] = '+';
  }

  if(!no_normalize){
    str = normalize(str);
  }

  vector<string> result_array;
  result_array = split(str, ' ');
  string result;

  int op_row = 1;
  for(unsigned int i = 0; i < result_array.size(); ++i )
  {
    if(result_array[i] == "+"){
      op[op_row] = '+';
      op_row++;
    }
    else if(result_array[i] == "-"){
      op[op_row] = '-';
      op_row++;
    } else if(result_array[i] == "*"){
      op[op_row] = '*';
      op_row++;
    } else if(result_array[i] == "/"){
      op[op_row] = '/';
      op_row++;
    } else {
      result += result_array[i];
      if ( i < result_array.size() - 1) {
        result += " ";
      }
    }
  }
  strcat(st1, result.c_str());

  cn = 0;
  b = 0;
  c = 0;
  while (1) {
    st[cn][b] = st1[c];
    b++;
    c++;
    st[cn][b] = 0;
    if (st1[c] == 0) break;
    if (st1[c] == ' ') {
      cn++;
      b = 0;
      c++;
    }
  }
  cn++;

  for (a = 0; a < cn; a++) {
    for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
    if (b == words) b = 0;
    bi[a] = b;
    if (output == 1) {
      printf("\nWord: %s  Position in vocabulary: %lld\n", st[a], bi[a]);
    }
    if (b == 0) {
      if (output == 1 || output == 2) {
        printf("Out of dictionary word!\n");
      }
      break;
    }
  }
  if (b == 0) return -1;
  if(cn == 1) {
    for (a = 0; a < size; a++) vec[a] = 0;
    for (b = 0; b < cn; b++) {
      if (bi[b] == -1) continue;
      for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
    }
  } else {
    for (a = 0; a < size; a++) {
      for (b = 0; b < cn; b++) {
        if(op[b] == '-') {
          vec[a] -= M[a + bi[b] * size];
        } else if(op[b] == '/') {
          vec[a] /= M[a + bi[b] * size];
        } else if(op[b] == '*') {
          vec[a] *= M[a + bi[b] * size];
        } else {
          vec[a] += M[a + bi[b] * size];
        }
      }
    }
  }

  len = 0;
  for (a = 0; a < size; a++) len += vec[a] * vec[a];
  len = sqrt(len);
  for (a = 0; a < size; a++) vec[a] /= len;
  for (a = 0; a < N; a++) bestd[a] = 0;
  for (a = 0; a < N; a++) bestw[a][0] = 0;
  for (c = 0; c < words; c++) {
    unsigned int equal = 0;
    for (b = 0; b < cn; b++) {
      if (c == bi[b]) {
        equal = 1;
        break;
      }
    }
    if (equal) continue;
    a = 0;
    for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
    if (a == 1) continue;
    dist = 0;
    for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
    for (a = 0; a < N; a++) {
      if (dist > bestd[a]) {
        for (d = N - 1; d > a; d--) {
          bestd[d] = bestd[d - 1];
          strcpy(bestw[d], bestw[d - 1]);
        }
        bestd[a] = dist;
        strcpy(bestw[a], &vocab[c * max_w]);
        break;
      }
    }
  }

  if (threshold > 0 || term_filter != NULL) {
    for (a = 0; a < N; a++) {
      if (threshold > 0 && bestd[a] < threshold) {
        bestd[a] = 0;
      }
      if (term_filter != NULL) {
        string s = bestw[a];
        string t = term_filter;
        if ( RE2::FullMatch(s, t) ) {
          bestd[a] = 0;
        }
      }
    }
  }

  unsigned int max;
  if (offset + limit > N) {
    max = N;
  } else {
    max = offset + limit;
  }

  for (a = offset; a < max; a++) {
    if (output_filter != NULL) {
      string s = bestw[a];
      re2::RE2::GlobalReplace(&s, output_filter, "");
      strcpy(bestw[a],s.c_str());
    }
    if ( a == 0 ) {
        switch(output) {
          case 3 :
            printf("%s", str.c_str());
            printf(",");
            break;
          case 4 :
            printf("%s", str.c_str());
            printf("\t");
            // for Groonga Groongaのシノニムは自身を含める必要あり。
            if(cn == 1) {
              printf("%s", str.c_str());
              printf("\t");
            }
            break;
        }
    }
    if (strlen(bestw[a]) > 0 && bestd[a] != 0) {
      if ( a < max - 1){
        switch(output) {
          case 1 :
            printf("%f\t%s\n", bestd[a], bestw[a]);
            break;
          case 2 :
            printf("%s\n", bestw[a]);
            break;
          case 3 :
            printf("%s", bestw[a]);
            printf(",");
            break;
          case 4 :
            printf("%s", bestw[a]);
            printf("\t");
            break;
        }
      } else {
        switch(output) {
          case 1 :
            printf("%f\t%s\n", bestd[a], bestw[a]);
            break;
          case 2 :
            printf("%s\n", bestw[a]);
            break;
          case 3 :
            printf("%s", bestw[a]);
            break;
          case 4 :
            printf("%s", bestw[a]);
            break;
        }
      }
    } else {
      if (max < N) {
        max++;
      }
    }
  }
  printf("\n");

  return 0;
}

int
main(int argc, char **argv) {
  const char *file_name = "/var/lib/word2vec/learn.bin";
  string str;

  google::ParseCommandLineFlags(&argc, &argv, true);

  if(FLAGS_h == true){
    printf(" --file_path : Input binary file learned by word2vec\n");
    printf(" --iuput : Input expression string\n");
    printf(" --output : 1:word,discatnce 2:word 3:split by commma 4:split by tab\n");
    printf(" --offset : offset\n");
    printf(" --limit : limit -1:all\n");
    printf(" --threshold : threshold (ex. --threshold 0.75)\n");
    printf(" --no_normalize : Don't use NFKC normalize\n");
    printf(" --term_filter : Filter by regular expresion pattern to term\n");
    printf(" --output_filter : Cut by regular expresion pattern to term\n");
    printf(" --server : Server mode\n");
    printf(" --ip : IP address\n");
    printf(" --port : Port number\n");
    exit(0);
  }

  if (FLAGS_file_path != "") {
    file_name = FLAGS_file_path.c_str();
  }

  if(word2vec_load(file_name) == false){
    printf("Load file failed\n");
    return -1;
  }

  int limit;
  if(FLAGS_limit == -1){
    limit = (int)N;
  } else {
    limit = FLAGS_limit;
  }
  if(FLAGS_input == ""){
    if(FLAGS_output == 1){
      printf("> ");
    }
    while(getline(cin, str)){
      if(str == "EXIT"){
        break;
      }
      calc(str, FLAGS_output, FLAGS_offset, limit, FLAGS_threshold,
           FLAGS_no_normalize, FLAGS_term_filter.c_str(),
           FLAGS_output_filter.c_str());
      if(FLAGS_output == 1){
        printf("> ");
      }
    }
  } else {
    if(FLAGS_output == 1){
      printf("> ");
    }
    ifstream ifs(FLAGS_input.c_str());
    if(ifs.fail()) {
      cerr << "File do not exist.\n";
      exit(0);
    }
    while(getline(ifs, str)){
      calc(str, FLAGS_output, FLAGS_offset, limit, FLAGS_threshold,
           FLAGS_no_normalize, FLAGS_term_filter.c_str(),
           FLAGS_output_filter.c_str());
      if(FLAGS_output == 1){
        printf("> ");
      }
    }
  }

  return 0;
}
<commit_msg>replace fullwidth space to halfwidth space<commit_after>/*
  Copyright(C) 2014 Naoya Murakami <naoya@createfield.com>

  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.

  This file includes the original word2vec's code. https://code.google.com/p/word2vec/
  The following is the header of the file:

    Copyright 2013 Google Inc. 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 <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <stdexcept>
#include <vector>

#include <math.h>
#include <malloc.h>

#include <unicode/normlzr.h>
#include <re2/re2.h>
#include <google/gflags.h>

DEFINE_string(file_path, "", "Input binary file learned by word2vec");
DEFINE_string(input, "", "Input expression string");
DEFINE_int32(output, 1, "1:word,discatnce 2:word 3:split by commma 4:split by tab");
DEFINE_int32(offset, 0, "offset");
DEFINE_int32(limit, -1, "limit -1:all");
DEFINE_double(threshold, 0, "threshold (ex. --threshold 0.75)");
DEFINE_bool(no_normalize, false, "Don't use NFKC normalize");
DEFINE_string(term_filter, "", "Filter by regular expresion pattern to term");
DEFINE_string(output_filter, "", "Cut by regular expresion pattern to term");
DEFINE_bool(server, false, "Server mode");
DEFINE_string(ip, "", "IP address");
DEFINE_string(port, "", "Port number");
DEFINE_bool(h, false, "Help message");

using namespace std;

const long long max_size = 2000;         // max length of strings
const long long N = 40;                  // number of closest words that will be shown
const long long max_w = 50;              // max length of vocabulary entries

long long words, size = 0;
float *M = NULL;
char *vocab = NULL;

string
normalize(const string str)
{
  icu::UnicodeString src;
  icu::UnicodeString dest;
  src = str.c_str();
  UErrorCode status;
  status = U_ZERO_ERROR;
  Normalizer::normalize(src, UNORM_NFKC, 0, dest, status);
  if(U_FAILURE(status)) {
    throw runtime_error("Unicode normalization failed");
  }
  string result;
  dest.toUTF8String(result);
  transform(result.begin(), result.end(), result.begin(), ::tolower);
  return result;
}

bool
word2vec_load(const char *file_name)
{
  FILE *f;
  float len;
  long long a, b;
  char ch;

  f = fopen(file_name, "rb");
  if (f == NULL) {
    printf("Input file not found : %s\n", file_name);
    return false;
  }

  fscanf(f, "%lld", &words);
  fscanf(f, "%lld", &size);
  vocab = (char *)malloc((long long)words * max_w * sizeof(char));
  M = (float *)malloc((long long)words * (long long)size * sizeof(float));
  if (M == NULL) {
    printf("Cannot allocate memory: %lld MB %lld %lld",
           (long long)words * size * sizeof(float) / 1048576, words, size);
    return false;
  }
  for (b = 0; b < words; b++) {
    fscanf(f, "%s%c", &vocab[b * max_w], &ch);
    for (a = 0; a < size; a++) fread(&M[a + b * size], sizeof(float), 1, f);
    len = 0;
    for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
    len = sqrt(len);
    for (a = 0; a < size; a++) M[a + b * size] /= len;
  }
  fclose(f);

  return true;
}

vector<string>
split(const string &str, char delim){
  vector<string> res;
  size_t current = 0, found;
  while((found = str.find_first_of(delim, current)) != string::npos){
    res.push_back(string(str, current, found - current));
    current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

int
calc(string str, int output, int offset, int limit, double threshold,
     bool no_normalize, const char *term_filter, const char *output_filter){
  char st1[max_size];
  char bestw[N][max_size];
  char st[100][max_size];
  char op[100];
  float dist, len, bestd[N], vec[max_size];
  long long a, b, c, d, cn, bi[100];

  for (a = 0; a < N; a++) bestd[a] = 0;
  for (a = 0; a < N; a++) bestw[a][0] = 0;

  st1[0] = 0;

  for(unsigned int i = 0; i < 100; i++){
    st[i][0] = 0;
    op[i] = '+';
  }

  if(!no_normalize){
    str = normalize(str);
  }
  re2::RE2::GlobalReplace(&str, "　", " ");

  vector<string> result_array;
  result_array = split(str, ' ');
  string result;

  int op_row = 1;
  for(unsigned int i = 0; i < result_array.size(); ++i )
  {
    if(result_array[i] == "+"){
      op[op_row] = '+';
      op_row++;
    }
    else if(result_array[i] == "-"){
      op[op_row] = '-';
      op_row++;
    } else if(result_array[i] == "*"){
      op[op_row] = '*';
      op_row++;
    } else if(result_array[i] == "/"){
      op[op_row] = '/';
      op_row++;
    } else {
      result += result_array[i];
      if ( i < result_array.size() - 1) {
        result += " ";
      }
    }
  }
  strcat(st1, result.c_str());

  cn = 0;
  b = 0;
  c = 0;
  while (1) {
    st[cn][b] = st1[c];
    b++;
    c++;
    st[cn][b] = 0;
    if (st1[c] == 0) break;
    if (st1[c] == ' ') {
      cn++;
      b = 0;
      c++;
    }
  }
  cn++;

  for (a = 0; a < cn; a++) {
    for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
    if (b == words) b = 0;
    bi[a] = b;
    if (output == 1) {
      printf("\nWord: %s  Position in vocabulary: %lld\n", st[a], bi[a]);
    }
    if (b == 0) {
      if (output == 1 || output == 2) {
        printf("Out of dictionary word!\n");
      }
      break;
    }
  }
  if (b == 0) return -1;
  if(cn == 1) {
    for (a = 0; a < size; a++) vec[a] = 0;
    for (b = 0; b < cn; b++) {
      if (bi[b] == -1) continue;
      for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
    }
  } else {
    for (a = 0; a < size; a++) {
      for (b = 0; b < cn; b++) {
        if(op[b] == '-') {
          vec[a] -= M[a + bi[b] * size];
        } else if(op[b] == '/') {
          vec[a] /= M[a + bi[b] * size];
        } else if(op[b] == '*') {
          vec[a] *= M[a + bi[b] * size];
        } else {
          vec[a] += M[a + bi[b] * size];
        }
      }
    }
  }

  len = 0;
  for (a = 0; a < size; a++) len += vec[a] * vec[a];
  len = sqrt(len);
  for (a = 0; a < size; a++) vec[a] /= len;
  for (a = 0; a < N; a++) bestd[a] = 0;
  for (a = 0; a < N; a++) bestw[a][0] = 0;
  for (c = 0; c < words; c++) {
    unsigned int equal = 0;
    for (b = 0; b < cn; b++) {
      if (c == bi[b]) {
        equal = 1;
        break;
      }
    }
    if (equal) continue;
    a = 0;
    for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
    if (a == 1) continue;
    dist = 0;
    for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
    for (a = 0; a < N; a++) {
      if (dist > bestd[a]) {
        for (d = N - 1; d > a; d--) {
          bestd[d] = bestd[d - 1];
          strcpy(bestw[d], bestw[d - 1]);
        }
        bestd[a] = dist;
        strcpy(bestw[a], &vocab[c * max_w]);
        break;
      }
    }
  }

  if (threshold > 0 || term_filter != NULL) {
    for (a = 0; a < N; a++) {
      if (threshold > 0 && bestd[a] < threshold) {
        bestd[a] = 0;
      }
      if (term_filter != NULL) {
        string s = bestw[a];
        string t = term_filter;
        if ( RE2::FullMatch(s, t) ) {
          bestd[a] = 0;
        }
      }
    }
  }

  unsigned int max;
  if (offset + limit > N) {
    max = N;
  } else {
    max = offset + limit;
  }

  for (a = offset; a < max; a++) {
    if (output_filter != NULL) {
      string s = bestw[a];
      re2::RE2::GlobalReplace(&s, output_filter, "");
      strcpy(bestw[a],s.c_str());
    }
    if ( a == 0 ) {
        switch(output) {
          case 3 :
            printf("%s", str.c_str());
            printf(",");
            break;
          case 4 :
            printf("%s", str.c_str());
            printf("\t");
            // for Groonga Groongaのシノニムは自身を含める必要あり。
            if(cn == 1) {
              printf("%s", str.c_str());
              printf("\t");
            }
            break;
        }
    }
    if (strlen(bestw[a]) > 0 && bestd[a] != 0) {
      if ( a < max - 1){
        switch(output) {
          case 1 :
            printf("%f\t%s\n", bestd[a], bestw[a]);
            break;
          case 2 :
            printf("%s\n", bestw[a]);
            break;
          case 3 :
            printf("%s", bestw[a]);
            printf(",");
            break;
          case 4 :
            printf("%s", bestw[a]);
            printf("\t");
            break;
        }
      } else {
        switch(output) {
          case 1 :
            printf("%f\t%s\n", bestd[a], bestw[a]);
            break;
          case 2 :
            printf("%s\n", bestw[a]);
            break;
          case 3 :
            printf("%s", bestw[a]);
            break;
          case 4 :
            printf("%s", bestw[a]);
            break;
        }
      }
    } else {
      if (max < N) {
        max++;
      }
    }
  }
  printf("\n");

  return 0;
}

int
main(int argc, char **argv) {
  const char *file_name = "/var/lib/word2vec/learn.bin";
  string str;

  google::ParseCommandLineFlags(&argc, &argv, true);

  if(FLAGS_h == true){
    printf(" --file_path : Input binary file learned by word2vec\n");
    printf(" --iuput : Input expression string\n");
    printf(" --output : 1:word,discatnce 2:word 3:split by commma 4:split by tab\n");
    printf(" --offset : offset\n");
    printf(" --limit : limit -1:all\n");
    printf(" --threshold : threshold (ex. --threshold 0.75)\n");
    printf(" --no_normalize : Don't use NFKC normalize\n");
    printf(" --term_filter : Filter by regular expresion pattern to term\n");
    printf(" --output_filter : Cut by regular expresion pattern to term\n");
    printf(" --server : Server mode\n");
    printf(" --ip : IP address\n");
    printf(" --port : Port number\n");
    exit(0);
  }

  if (FLAGS_file_path != "") {
    file_name = FLAGS_file_path.c_str();
  }

  if(word2vec_load(file_name) == false){
    printf("Load file failed\n");
    return -1;
  }

  int limit;
  if(FLAGS_limit == -1){
    limit = (int)N;
  } else {
    limit = FLAGS_limit;
  }
  if(FLAGS_input == ""){
    if(FLAGS_output == 1){
      printf("> ");
    }
    while(getline(cin, str)){
      if(str == "EXIT"){
        break;
      }
      calc(str, FLAGS_output, FLAGS_offset, limit, FLAGS_threshold,
           FLAGS_no_normalize, FLAGS_term_filter.c_str(),
           FLAGS_output_filter.c_str());
      if(FLAGS_output == 1){
        printf("> ");
      }
    }
  } else {
    if(FLAGS_output == 1){
      printf("> ");
    }
    ifstream ifs(FLAGS_input.c_str());
    if(ifs.fail()) {
      cerr << "File do not exist.\n";
      exit(0);
    }
    while(getline(ifs, str)){
      calc(str, FLAGS_output, FLAGS_offset, limit, FLAGS_threshold,
           FLAGS_no_normalize, FLAGS_term_filter.c_str(),
           FLAGS_output_filter.c_str());
      if(FLAGS_output == 1){
        printf("> ");
      }
    }
  }

  return 0;
}
<|endoftext|>


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

#include <psprint/helper.hxx>
#include <tools/string.hxx>
#include <osl/file.hxx>

const ::rtl::OUString& psp::getPrinterPath()
{
    static const char* pEnv = getenv( "SAL_PSPRINT" );
    static ::rtl::OUString aPath( pEnv ? pEnv : "", pEnv ? strlen( pEnv ) : 0, gsl_getSystemTextEncoding() );
    return aPath;
}

bool psp::convertPfbToPfa( ::osl::File& rInFile, ::osl::File& rOutFile )
{
    static unsigned char hexDigits[] =
        {
            '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
        };

    bool bSuccess = true;
    bool bEof = false;
    unsigned char buffer[256];
    sal_uInt64 nRead;
    sal_uInt64 nOrgPos = 0;
    rInFile.getPos( nOrgPos );

    while( bSuccess && ! bEof )
    {
        // read leading bytes
        bEof = ! rInFile.read( buffer, 6, nRead ) && nRead == 6 ? false : true;
        int nType = buffer[ 1 ];
        int nBytesToRead = buffer[2] | buffer[3] << 8 | buffer[4] << 16 | buffer[5] << 24;
        if( buffer[0] != 0x80 ) // test for pfb m_agic number
        {
            // this migt be a pfa font already
            sal_uInt64 nWrite = 0;
            if( ! rInFile.read( buffer+6, 9, nRead ) && nRead == 9 &&
                ( ! strncmp( (char*)buffer, "%!FontType1-", 12 ) ||
                  ! strncmp( (char*)buffer, "%!PS-AdobeFont-", 15 ) ) )
            {
                if( rOutFile.write( buffer, 15, nWrite ) || nWrite != 15 )
                    bSuccess = false;
                while( bSuccess &&
                       ! rInFile.read( buffer, sizeof( buffer ), nRead ) &&
                       nRead != 0 )
                {
                    if( rOutFile.write( buffer, nRead, nWrite ) ||
                        nWrite != nRead )
                        bSuccess = false;
                }
                bEof = true;
            }
            else
                bSuccess = false;
        }
        else if( nType == 1 || nType == 2 )
        {
            unsigned char* pBuffer = new unsigned char[ nBytesToRead+1 ];

            if( ! rInFile.read( pBuffer, nBytesToRead, nRead ) && nRead == nBytesToRead )
            {
                if( nType == 1 )
                {
                    // ascii data, convert dos lineends( \r\n ) and
                    // m_ac lineends( \r ) to \n
                    unsigned char * pWriteBuffer = new unsigned char[ nBytesToRead ];
                    int nBytesToWrite = 0;
                    for( int i = 0; i < nBytesToRead; i++ )
                    {
                        if( pBuffer[i] != '\r' )
                            pWriteBuffer[ nBytesToWrite++ ] = pBuffer[i];
                        else if( pBuffer[ i+1 ] == '\n' )
                        {
                            i++;
                            pWriteBuffer[ nBytesToWrite++ ] = '\n';
                        }
                        else
                            pWriteBuffer[ nBytesToWrite++ ] = '\n';
                    }
                    if( rOutFile.write( pWriteBuffer, nBytesToWrite, nRead ) || nRead != nBytesToWrite )
                        bSuccess = false;

                    delete pWriteBuffer;
                }
                else
                {
                    // binary data
                    int nBuffer = 0;
                    for( int i = 0; i < nBytesToRead && bSuccess; i++ )
                    {
                        buffer[ nBuffer++ ] = hexDigits[ pBuffer[ i ] >> 4 ];
                        buffer[ nBuffer++ ] = hexDigits[ pBuffer[ i ] & 15 ];
                        if( nBuffer >= 80 )
                        {
                            buffer[ nBuffer++ ] = '\n';
                            if( rOutFile.write( buffer, nBuffer, nRead ) || nRead != nBuffer )
                                bSuccess = false;
                            nBuffer = 0;
                        }
                    }
                    if( nBuffer > 0 && bSuccess )
                    {
                        buffer[ nBuffer++ ] = '\n';
                        if( rOutFile.write( buffer, nBuffer, nRead ) || nRead != nBuffer )
                            bSuccess = false;
                    }
                }
            }
            else
                bSuccess = false;

            delete pBuffer;
        }
        else if( nType == 3 )
            bEof = true;
        else
            bSuccess = false;
    }

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

#include <psprint/helper.hxx>
#include <tools/string.hxx>
#include <osl/file.hxx>
#include <unotools/configmgr.hxx>

const ::rtl::OUString& psp::getPrinterPath()
{
    static ::rtl::OUString aPath;

    if( ! aPath.getLength() )
    {
        ::com::sun::star::uno::Any aPathEntry;
        ::rtl::OUString aEntry;

        aPathEntry = ::utl::ConfigManager::GetDirectConfigProperty( ::utl::ConfigManager::OFFICEINSTALL );
        aPathEntry >>= aPath;
        aPath += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/share/psprint:" ) );
        aPathEntry = ::utl::ConfigManager::GetDirectConfigProperty( ::utl::ConfigManager::INSTALLPATH );
        aPathEntry >>= aEntry;
        aPath += aEntry;
        aPath += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/psprint" ) );

        const char* pEnv = getenv( "SAL_PSPRINT" );
        if( pEnv && *pEnv )
        {
            aPath += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":" ) );
            aEntry = ::rtl::OUString( pEnv ? pEnv : "", pEnv ? strlen( pEnv ) : 0, gsl_getSystemTextEncoding() );
            aPath += aEntry;
        }
#ifdef DEBUG
        fprintf( stderr, "initalizing printer path to \"%s\"\n", ::rtl::OUStringToOString( aPath, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
#endif
    }
    return aPath;
}

bool psp::convertPfbToPfa( ::osl::File& rInFile, ::osl::File& rOutFile )
{
    static unsigned char hexDigits[] =
        {
            '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
        };

    bool bSuccess = true;
    bool bEof = false;
    unsigned char buffer[256];
    sal_uInt64 nRead;
    sal_uInt64 nOrgPos = 0;
    rInFile.getPos( nOrgPos );

    while( bSuccess && ! bEof )
    {
        // read leading bytes
        bEof = ! rInFile.read( buffer, 6, nRead ) && nRead == 6 ? false : true;
        int nType = buffer[ 1 ];
        int nBytesToRead = buffer[2] | buffer[3] << 8 | buffer[4] << 16 | buffer[5] << 24;
        if( buffer[0] != 0x80 ) // test for pfb m_agic number
        {
            // this migt be a pfa font already
            sal_uInt64 nWrite = 0;
            if( ! rInFile.read( buffer+6, 9, nRead ) && nRead == 9 &&
                ( ! strncmp( (char*)buffer, "%!FontType1-", 12 ) ||
                  ! strncmp( (char*)buffer, "%!PS-AdobeFont-", 15 ) ) )
            {
                if( rOutFile.write( buffer, 15, nWrite ) || nWrite != 15 )
                    bSuccess = false;
                while( bSuccess &&
                       ! rInFile.read( buffer, sizeof( buffer ), nRead ) &&
                       nRead != 0 )
                {
                    if( rOutFile.write( buffer, nRead, nWrite ) ||
                        nWrite != nRead )
                        bSuccess = false;
                }
                bEof = true;
            }
            else
                bSuccess = false;
        }
        else if( nType == 1 || nType == 2 )
        {
            unsigned char* pBuffer = new unsigned char[ nBytesToRead+1 ];

            if( ! rInFile.read( pBuffer, nBytesToRead, nRead ) && nRead == nBytesToRead )
            {
                if( nType == 1 )
                {
                    // ascii data, convert dos lineends( \r\n ) and
                    // m_ac lineends( \r ) to \n
                    unsigned char * pWriteBuffer = new unsigned char[ nBytesToRead ];
                    int nBytesToWrite = 0;
                    for( int i = 0; i < nBytesToRead; i++ )
                    {
                        if( pBuffer[i] != '\r' )
                            pWriteBuffer[ nBytesToWrite++ ] = pBuffer[i];
                        else if( pBuffer[ i+1 ] == '\n' )
                        {
                            i++;
                            pWriteBuffer[ nBytesToWrite++ ] = '\n';
                        }
                        else
                            pWriteBuffer[ nBytesToWrite++ ] = '\n';
                    }
                    if( rOutFile.write( pWriteBuffer, nBytesToWrite, nRead ) || nRead != nBytesToWrite )
                        bSuccess = false;

                    delete pWriteBuffer;
                }
                else
                {
                    // binary data
                    int nBuffer = 0;
                    for( int i = 0; i < nBytesToRead && bSuccess; i++ )
                    {
                        buffer[ nBuffer++ ] = hexDigits[ pBuffer[ i ] >> 4 ];
                        buffer[ nBuffer++ ] = hexDigits[ pBuffer[ i ] & 15 ];
                        if( nBuffer >= 80 )
                        {
                            buffer[ nBuffer++ ] = '\n';
                            if( rOutFile.write( buffer, nBuffer, nRead ) || nRead != nBuffer )
                                bSuccess = false;
                            nBuffer = 0;
                        }
                    }
                    if( nBuffer > 0 && bSuccess )
                    {
                        buffer[ nBuffer++ ] = '\n';
                        if( rOutFile.write( buffer, nBuffer, nRead ) || nRead != nBuffer )
                            bSuccess = false;
                    }
                }
            }
            else
                bSuccess = false;

            delete pBuffer;
        }
        else if( nType == 3 )
            bEof = true;
        else
            bSuccess = false;
    }

    return bSuccess;
}
<|endoftext|>


<commit_before>#include <Rcpp.h>
#include <chrono>
#include <string>
#include <unordered_map>
#include "zmq.hpp"

typedef std::chrono::high_resolution_clock Time;
typedef std::chrono::milliseconds ms;
extern Rcpp::Function R_serialize;
extern Rcpp::Function R_unserialize;
int pending_interrupt();

class ZeroMQ {
public:
    ZeroMQ(int threads=1) : ctx(threads), sockets() {}

    std::string listen(Rcpp::CharacterVector addrs, std::string socket_type="ZMQ_REP",
            std::string sid="default") {
        auto sock = zmq::socket_t(ctx, str2socket(socket_type));
        int i;
        for (i=0; i<addrs.length(); i++) {
            auto addr = Rcpp::as<std::string>(addrs[i]);
            try {
                sock.bind(addr);
            } catch(zmq::error_t const &e) {
                if (errno != EADDRINUSE)
                    Rf_error(e.what());
            }
            char option_value[1024];
            size_t option_value_len = sizeof(option_value);
            sock.getsockopt(ZMQ_LAST_ENDPOINT, option_value, &option_value_len);
            sockets.emplace(sid, std::move(sock));
            return std::string(option_value);
        }
        Rf_error("Could not bind port after ", i, " tries");
    }
    void connect(std::string address, std::string socket_type="ZMQ_REQ", std::string sid="default") {
        auto sock = zmq::socket_t(ctx, str2socket(socket_type));
        sock.connect(address);
        sockets.emplace(sid, std::move(sock));
    }
    void disconnect(std::string sid="default") {
        sockets.erase(sid);
    }

    void send(SEXP data, std::string sid="default", bool dont_wait=false, bool send_more=false) {
        auto & socket = find_socket(sid);
        auto flags = zmq::send_flags::none;
        if (dont_wait)
            flags = flags | zmq::send_flags::dontwait;
        if (send_more)
            flags = flags | zmq::send_flags::sndmore;

        if (TYPEOF(data) != RAWSXP)
            data = R_serialize(data, R_NilValue);

        zmq::message_t message(Rf_xlength(data));
        memcpy(message.data(), RAW(data), Rf_xlength(data));
        socket.send(message, flags);
    }
    SEXP receive(std::string sid="default", bool dont_wait=false, bool unserialize=true) {
        auto message = rcv_msg(sid, dont_wait);
        SEXP ans = Rf_allocVector(RAWSXP, message.size());
        memcpy(RAW(ans), message.data(), message.size());
        if (unserialize)
            return R_unserialize(ans);
        else
            return ans;
    }
    Rcpp::IntegerVector poll(Rcpp::CharacterVector sids, int timeout=-1) {
        auto nsock = sids.length();
        auto pitems = std::vector<zmq::pollitem_t>(nsock);
        for (int i = 0; i < nsock; i++) {
            auto socket_id = Rcpp::as<std::string>(sids[i]);
            pitems[i].socket = find_socket(socket_id);
            pitems[i].events = ZMQ_POLLIN; // | ZMQ_POLLOUT; // ssh_proxy XREP/XREQ has 2200
        }

        int rc = -1;
        auto start = Time::now();
        do {
            try {
                rc = zmq::poll(pitems, timeout);
            } catch(zmq::error_t const &e) {
                if (errno != EINTR || pending_interrupt())
                    Rf_error(e.what());
                if (timeout != -1) {
                    ms dt = std::chrono::duration_cast<ms>(Time::now() - start);
                    timeout = timeout - dt.count();
                    if (timeout <= 0)
                        break;
                }
            }
        } while(rc < 0);

        auto result = Rcpp::IntegerVector(nsock);
        for (int i = 0; i < nsock; i++)
            result[i] = pitems[i].revents;
        return result;
    }

private:
    zmq::context_t ctx;
    std::unordered_map<std::string, zmq::socket_t> sockets;

    int str2socket(std::string str) {
        if (str == "ZMQ_REP") {
            return ZMQ_REP;
        } else if (str == "ZMQ_REQ") {
            return ZMQ_REQ;
        } else if (str == "ZMQ_XREP") {
            return ZMQ_XREP;
        } else if (str == "ZMQ_XREQ") {
            return ZMQ_XREQ;
        } else {
            Rcpp::exception(("Invalid socket type: " + str).c_str());
        }
        return -1;
    }

    zmq::socket_t & find_socket(std::string socket_id) {
        auto socket_iter = sockets.find(socket_id);
        if (socket_iter == sockets.end())
            Rf_error("Trying to access non-existing socket: ", socket_id.c_str());
        return socket_iter->second;
    }

    zmq::message_t rcv_msg(std::string sid="default", bool dont_wait=false) {
        auto flags = zmq::recv_flags::none;
        if (dont_wait)
            flags = flags | zmq::recv_flags::dontwait;

        zmq::message_t message;
        auto & socket = find_socket(sid);
        socket.recv(message, flags);
        return message;
    }
};

RCPP_MODULE(zmq) {
    using namespace Rcpp;
    class_<ZeroMQ>("ZeroMQ_raw")
        .constructor() // .constructor<int>() SIGABRT
        .method("listen", &ZeroMQ::listen)
        .method("connect", &ZeroMQ::connect)
        .method("disconnect", &ZeroMQ::disconnect)
        .method("send", &ZeroMQ::send)
        .method("receive", &ZeroMQ::receive)
        .method("poll", &ZeroMQ::poll)
    ;
}
<commit_msg>disable copy<commit_after>#include <Rcpp.h>
#include <chrono>
#include <string>
#include <unordered_map>
#include "zmq.hpp"

typedef std::chrono::high_resolution_clock Time;
typedef std::chrono::milliseconds ms;
extern Rcpp::Function R_serialize;
extern Rcpp::Function R_unserialize;
int pending_interrupt();

class ZeroMQ {
public:
    ZeroMQ(int threads=1) : ctx(threads), sockets() {}
    ZeroMQ(const ZeroMQ &) = delete;
    ZeroMQ & operator=(ZeroMQ const &) = delete;

    std::string listen(Rcpp::CharacterVector addrs, std::string socket_type="ZMQ_REP",
            std::string sid="default") {
        auto sock = zmq::socket_t(ctx, str2socket(socket_type));
        int i;
        for (i=0; i<addrs.length(); i++) {
            auto addr = Rcpp::as<std::string>(addrs[i]);
            try {
                sock.bind(addr);
            } catch(zmq::error_t const &e) {
                if (errno != EADDRINUSE)
                    Rf_error(e.what());
            }
            char option_value[1024];
            size_t option_value_len = sizeof(option_value);
            sock.getsockopt(ZMQ_LAST_ENDPOINT, option_value, &option_value_len);
            sockets.emplace(sid, std::move(sock));
            return std::string(option_value);
        }
        Rf_error("Could not bind port after ", i, " tries");
    }
    void connect(std::string address, std::string socket_type="ZMQ_REQ", std::string sid="default") {
        auto sock = zmq::socket_t(ctx, str2socket(socket_type));
        sock.connect(address);
        sockets.emplace(sid, std::move(sock));
    }
    void disconnect(std::string sid="default") {
        sockets.erase(sid);
    }

    void send(SEXP data, std::string sid="default", bool dont_wait=false, bool send_more=false) {
        auto & socket = find_socket(sid);
        auto flags = zmq::send_flags::none;
        if (dont_wait)
            flags = flags | zmq::send_flags::dontwait;
        if (send_more)
            flags = flags | zmq::send_flags::sndmore;

        if (TYPEOF(data) != RAWSXP)
            data = R_serialize(data, R_NilValue);

        zmq::message_t message(Rf_xlength(data));
        memcpy(message.data(), RAW(data), Rf_xlength(data));
        socket.send(message, flags);
    }
    SEXP receive(std::string sid="default", bool dont_wait=false, bool unserialize=true) {
        auto message = rcv_msg(sid, dont_wait);
        SEXP ans = Rf_allocVector(RAWSXP, message.size());
        memcpy(RAW(ans), message.data(), message.size());
        if (unserialize)
            return R_unserialize(ans);
        else
            return ans;
    }
    Rcpp::IntegerVector poll(Rcpp::CharacterVector sids, int timeout=-1) {
        auto nsock = sids.length();
        auto pitems = std::vector<zmq::pollitem_t>(nsock);
        for (int i = 0; i < nsock; i++) {
            auto socket_id = Rcpp::as<std::string>(sids[i]);
            pitems[i].socket = find_socket(socket_id);
            pitems[i].events = ZMQ_POLLIN; // | ZMQ_POLLOUT; // ssh_proxy XREP/XREQ has 2200
        }

        int rc = -1;
        auto start = Time::now();
        do {
            try {
                rc = zmq::poll(pitems, timeout);
            } catch(zmq::error_t const &e) {
                if (errno != EINTR || pending_interrupt())
                    Rf_error(e.what());
                if (timeout != -1) {
                    ms dt = std::chrono::duration_cast<ms>(Time::now() - start);
                    timeout = timeout - dt.count();
                    if (timeout <= 0)
                        break;
                }
            }
        } while(rc < 0);

        auto result = Rcpp::IntegerVector(nsock);
        for (int i = 0; i < nsock; i++)
            result[i] = pitems[i].revents;
        return result;
    }

private:
    zmq::context_t ctx;
    std::unordered_map<std::string, zmq::socket_t> sockets;

    int str2socket(std::string str) {
        if (str == "ZMQ_REP") {
            return ZMQ_REP;
        } else if (str == "ZMQ_REQ") {
            return ZMQ_REQ;
        } else if (str == "ZMQ_XREP") {
            return ZMQ_XREP;
        } else if (str == "ZMQ_XREQ") {
            return ZMQ_XREQ;
        } else {
            Rcpp::exception(("Invalid socket type: " + str).c_str());
        }
        return -1;
    }

    zmq::socket_t & find_socket(std::string socket_id) {
        auto socket_iter = sockets.find(socket_id);
        if (socket_iter == sockets.end())
            Rf_error("Trying to access non-existing socket: ", socket_id.c_str());
        return socket_iter->second;
    }

    zmq::message_t rcv_msg(std::string sid="default", bool dont_wait=false) {
        auto flags = zmq::recv_flags::none;
        if (dont_wait)
            flags = flags | zmq::recv_flags::dontwait;

        zmq::message_t message;
        auto & socket = find_socket(sid);
        socket.recv(message, flags);
        return message;
    }
};

RCPP_MODULE(zmq) {
    using namespace Rcpp;
    class_<ZeroMQ>("ZeroMQ_raw")
        .constructor() // .constructor<int>() SIGABRT
        .method("listen", &ZeroMQ::listen)
        .method("connect", &ZeroMQ::connect)
        .method("disconnect", &ZeroMQ::disconnect)
        .method("send", &ZeroMQ::send)
        .method("receive", &ZeroMQ::receive)
        .method("poll", &ZeroMQ::poll)
    ;
}
<|endoftext|>


<commit_before>/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2006 Torus Knot Software Ltd
Also see acknowledgements in Readme.html

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

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt.

You may alternatively use this source under the terms of a specific version of
the OGRE Unrestricted License provided you have obtained such a license from
Torus Knot Software Ltd.
-----------------------------------------------------------------------------
*/
#include "OgreStableHeaders.h"

#include "OgreException.h"
#include "OgreLogManager.h"
#include "OgreRoot.h"
#include "OgreStringConverter.h"

#include "OgreGLRenderSystem.h"

#include "OgreGLXRenderTexture.h"
#include "OgreGLXContext.h"
#include "OgreGLXUtils.h"
#include "OgreGLXGLSupport.h"
#include <iostream>

namespace Ogre
{
	//-------------------------------------------------------------------------------------------------//
	GLXPBuffer::GLXPBuffer(GLXGLSupport* glsupport, PixelComponentType format, size_t width, size_t height):
		mGLSupport(glsupport), GLPBuffer(format, width, height), mContext(0)
	{
		Display *glDisplay = mGLSupport->getGLDisplay();
		::GLXDrawable glxDrawable = 0;
		::GLXFBConfig fbConfig = 0;

		bool isFloat = false;
		int bits = 0;

		switch (mFormat)
		{
		case PCT_BYTE:
			bits = 8;
			break;

		case PCT_SHORT:
			bits = 16;
			break;

		case PCT_FLOAT16:
			bits = 16;
			break;

		case PCT_FLOAT32:
			bits = 32;
			break;

		default:
			break;
		}

		int renderAttrib = GLX_RENDER_TYPE;
		int renderValue  = GLX_RGBA_BIT;

		if (mFormat == PCT_FLOAT16 || mFormat == PCT_FLOAT32)
		{
			if (GLXEW_NV_float_buffer)
			{
				renderAttrib = GLX_FLOAT_COMPONENTS_NV;
				renderValue  = GL_TRUE;
			}

			if (GLXEW_ATI_pixel_format_float)
			{
				renderAttrib = GLX_RENDER_TYPE;
				renderValue  = GLX_RGBA_FLOAT_ATI_BIT;
			}

			if (GLXEW_ARB_fbconfig_float)
			{
				renderAttrib = GLX_RENDER_TYPE;
				renderValue  = GLX_RGBA_FLOAT_BIT;
			}

			if (renderAttrib == GLX_RENDER_TYPE && renderValue == GLX_RGBA_BIT)
			{
				OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "No support for Floating point PBuffers",  "GLRenderTexture::createPBuffer");
			}
		}

		int minAttribs[] = {
			GLX_DRAWABLE_TYPE, GLX_PBUFFER,
			renderAttrib,	  renderValue,
			GLX_DOUBLEBUFFER,  0,
			None
		};

		int maxAttribs[] = {
			GLX_RED_SIZE,	  bits,
			GLX_GREEN_SIZE,	bits,
			GLX_BLUE_SIZE,	 bits,
			GLX_ALPHA_SIZE,	bits,
			GLX_STENCIL_SIZE,  INT_MAX,
			None
		};

		int pBufferAttribs[] = {
			GLX_PBUFFER_WIDTH,	  mWidth,
			GLX_PBUFFER_HEIGHT,	 mHeight,
			GLX_PRESERVED_CONTENTS, GL_TRUE,
			None
		};

		fbConfig = mGLSupport->selectFBConfig(minAttribs, maxAttribs);

		glxDrawable = glXCreatePbuffer(glDisplay, fbConfig, pBufferAttribs);

		if (! fbConfig || ! glxDrawable)
		{
			OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to create Pbuffer", "GLXPBuffer::GLXPBuffer");
		}

		GLint fbConfigID;
		GLuint iWidth, iHeight;

		glXGetFBConfigAttrib(glDisplay, fbConfig, GLX_FBCONFIG_ID, &fbConfigID);
		glXQueryDrawable(glDisplay, glxDrawable, GLX_WIDTH, &iWidth);
		glXQueryDrawable(glDisplay, glxDrawable, GLX_HEIGHT, &iHeight);

		mWidth = iWidth;
		mHeight = iHeight;
		LogManager::getSingleton().logMessage(LML_NORMAL, "GLXPBuffer::create used final dimensions " + StringConverter::toString(mWidth) + " x " + StringConverter::toString(mHeight));
		LogManager::getSingleton().logMessage("GLXPBuffer::create used FBConfigID " + StringConverter::toString(fbConfigID));

		mContext = new GLXContext(mGLSupport, fbConfig, glxDrawable);
	}

	//-------------------------------------------------------------------------------------------------//
	GLXPBuffer::~GLXPBuffer()
	{
		glXDestroyPbuffer(mGLSupport->getGLDisplay(), mContext->mDrawable);

		delete mContext;

		LogManager::getSingleton().logMessage(LML_NORMAL, "GLXPBuffer::PBuffer destroyed");
	}

	//-------------------------------------------------------------------------------------------------//
	GLContext *GLXPBuffer::getContext()
	{
		return mContext;
	}
}
<commit_msg>Adding climits include to GLXRenderTexture, supposedly needed on some distros<commit_after>/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2006 Torus Knot Software Ltd
Also see acknowledgements in Readme.html

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

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt.

You may alternatively use this source under the terms of a specific version of
the OGRE Unrestricted License provided you have obtained such a license from
Torus Knot Software Ltd.
-----------------------------------------------------------------------------
*/
#include "OgreStableHeaders.h"

#include "OgreException.h"
#include "OgreLogManager.h"
#include "OgreRoot.h"
#include "OgreStringConverter.h"

#include "OgreGLRenderSystem.h"

#include "OgreGLXRenderTexture.h"
#include "OgreGLXContext.h"
#include "OgreGLXUtils.h"
#include "OgreGLXGLSupport.h"
#include <iostream>
#include <climits>

namespace Ogre
{
	//-------------------------------------------------------------------------------------------------//
	GLXPBuffer::GLXPBuffer(GLXGLSupport* glsupport, PixelComponentType format, size_t width, size_t height):
		mGLSupport(glsupport), GLPBuffer(format, width, height), mContext(0)
	{
		Display *glDisplay = mGLSupport->getGLDisplay();
		::GLXDrawable glxDrawable = 0;
		::GLXFBConfig fbConfig = 0;

		bool isFloat = false;
		int bits = 0;

		switch (mFormat)
		{
		case PCT_BYTE:
			bits = 8;
			break;

		case PCT_SHORT:
			bits = 16;
			break;

		case PCT_FLOAT16:
			bits = 16;
			break;

		case PCT_FLOAT32:
			bits = 32;
			break;

		default:
			break;
		}

		int renderAttrib = GLX_RENDER_TYPE;
		int renderValue  = GLX_RGBA_BIT;

		if (mFormat == PCT_FLOAT16 || mFormat == PCT_FLOAT32)
		{
			if (GLXEW_NV_float_buffer)
			{
				renderAttrib = GLX_FLOAT_COMPONENTS_NV;
				renderValue  = GL_TRUE;
			}

			if (GLXEW_ATI_pixel_format_float)
			{
				renderAttrib = GLX_RENDER_TYPE;
				renderValue  = GLX_RGBA_FLOAT_ATI_BIT;
			}

			if (GLXEW_ARB_fbconfig_float)
			{
				renderAttrib = GLX_RENDER_TYPE;
				renderValue  = GLX_RGBA_FLOAT_BIT;
			}

			if (renderAttrib == GLX_RENDER_TYPE && renderValue == GLX_RGBA_BIT)
			{
				OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "No support for Floating point PBuffers",  "GLRenderTexture::createPBuffer");
			}
		}

		int minAttribs[] = {
			GLX_DRAWABLE_TYPE, GLX_PBUFFER,
			renderAttrib,	  renderValue,
			GLX_DOUBLEBUFFER,  0,
			None
		};

		int maxAttribs[] = {
			GLX_RED_SIZE,	  bits,
			GLX_GREEN_SIZE,	bits,
			GLX_BLUE_SIZE,	 bits,
			GLX_ALPHA_SIZE,	bits,
			GLX_STENCIL_SIZE,  INT_MAX,
			None
		};

		int pBufferAttribs[] = {
			GLX_PBUFFER_WIDTH,	  mWidth,
			GLX_PBUFFER_HEIGHT,	 mHeight,
			GLX_PRESERVED_CONTENTS, GL_TRUE,
			None
		};

		fbConfig = mGLSupport->selectFBConfig(minAttribs, maxAttribs);

		glxDrawable = glXCreatePbuffer(glDisplay, fbConfig, pBufferAttribs);

		if (! fbConfig || ! glxDrawable)
		{
			OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to create Pbuffer", "GLXPBuffer::GLXPBuffer");
		}

		GLint fbConfigID;
		GLuint iWidth, iHeight;

		glXGetFBConfigAttrib(glDisplay, fbConfig, GLX_FBCONFIG_ID, &fbConfigID);
		glXQueryDrawable(glDisplay, glxDrawable, GLX_WIDTH, &iWidth);
		glXQueryDrawable(glDisplay, glxDrawable, GLX_HEIGHT, &iHeight);

		mWidth = iWidth;
		mHeight = iHeight;
		LogManager::getSingleton().logMessage(LML_NORMAL, "GLXPBuffer::create used final dimensions " + StringConverter::toString(mWidth) + " x " + StringConverter::toString(mHeight));
		LogManager::getSingleton().logMessage("GLXPBuffer::create used FBConfigID " + StringConverter::toString(fbConfigID));

		mContext = new GLXContext(mGLSupport, fbConfig, glxDrawable);
	}

	//-------------------------------------------------------------------------------------------------//
	GLXPBuffer::~GLXPBuffer()
	{
		glXDestroyPbuffer(mGLSupport->getGLDisplay(), mContext->mDrawable);

		delete mContext;

		LogManager::getSingleton().logMessage(LML_NORMAL, "GLXPBuffer::PBuffer destroyed");
	}

	//-------------------------------------------------------------------------------------------------//
	GLContext *GLXPBuffer::getContext()
	{
		return mContext;
	}
}
<|endoftext|>


<commit_before>#include "RenderingSystem.h"
#include "Engine.h"
#include "AutoBinder.hpp"

using namespace Core;
using namespace Device;
using namespace Rendering;
using namespace Rendering::Camera;
using namespace Rendering::Shader;
using namespace Rendering::Renderer;
using namespace Rendering::RenderState;
using namespace Rendering::GI;
using namespace Rendering::Manager;
using namespace Rendering::Light;
using namespace Rendering::Shadow;
using namespace Rendering::Material;

void RenderingSystem::InitializeRenderer(Engine& engine, const RenderSetting&& param)
{
	Object& object		= engine.GetObjectManager().Add(param.mainCamName);
	MainCamera& maincam	= engine.GetComponentSystem().SetMainCamera(object.GetObjectID());

	maincam.Initialize(engine.GetDirectX(), _shaderManager, param.renderRect);
	engine.AddRootObject(object);

	_shadowRenderer.Initialize(engine.GetDirectX(), param.shadowMapResolution, param.shadowMapResolution, param.shadowMapResolution);
	_mainRenderer.Initialize(engine.GetDirectX(), _shaderManager, maincam, param.giParam);

	_defaultShaders.Initialize(engine.GetDirectX(), _shaderManager);
	_backBufferMaker.Initialize(engine.GetDirectX(), _shaderManager);
}

void RenderingSystem::Initialize(Engine& engine)
{
	_materialManager.Initialize(engine.GetDirectX());
	_postProcessing.Initialize(engine.GetDirectX(), _shaderManager, engine.GetComponentSystem().GetMainCamera());
}

void RenderingSystem::Update(Engine& engine, float dt)
{
	_postProcessing.SetElapsedTime(dt);
}

void RenderingSystem::Render(Engine& engine, float dt)
{
	auto& dx = engine.GetDirectX();
	_materialManager.UpdateConstBuffer(dx);

	const auto& compoSys		= engine.GetComponentSystem();
	const auto& shadowMgr		= compoSys.GetManager_Direct<ShadowManager>();
	const auto cullParam		= engine.GetCullingParam();
	const auto meshRenderParam	= GetMeshRenderParam();
	_shadowRenderer.RenderShadowMap<SpotLightShadow>(dx, shadowMgr, _materialManager, cullParam, meshRenderParam);
	_shadowRenderer.RenderShadowMap<PointLightShadow>(dx, shadowMgr, _materialManager, cullParam, meshRenderParam);
	_shadowRenderer.RenderShadowMap<DirectionalLightShadow>(dx, shadowMgr, _materialManager, cullParam, meshRenderParam);

	const auto& lightMgr			= compoSys.GetManager_Direct<LightManager>();
	const auto& mainCamera			= compoSys.GetMainCamera();
	SkyBoxMaterial* skyboxMaterial	= _materialManager.Find<SkyBoxMaterial>(mainCamera.GetSkyBoxMaterialID());
	_mainRenderer.UpdateCB(dx, mainCamera, lightMgr);
	_mainRenderer.Render(dx, MainRenderer::Param{mainCamera, meshRenderParam, _materialManager, lightMgr, ShadowSystem{shadowMgr, _shadowRenderer}, std::move(cullParam), skyboxMaterial});

	_postProcessing.SetElapsedTime(dt);
	_postProcessing.UpdateCB(dx);
	_postProcessing.Render(dx, _mainRenderer, mainCamera);

	AutoBinderSampler<PixelShader> sampler(dx, SamplerStateBindIndex(0), SamplerState::Point);
	_backBufferMaker.Render(dx, dx.GetBackBufferRT(), _mainRenderer.GetResultMap().GetTexture2D());

	dx.GetSwapChain()->Present(0, 0);

	_shadowRenderer.ClearDirty();
}

void RenderingSystem::Destroy(Engine& engine)
{

}<commit_msg>RenderingSystem - 줄 정리<commit_after>#include "RenderingSystem.h"
#include "Engine.h"
#include "AutoBinder.hpp"

using namespace Core;
using namespace Device;
using namespace Rendering;
using namespace Rendering::Camera;
using namespace Rendering::Shader;
using namespace Rendering::Renderer;
using namespace Rendering::RenderState;
using namespace Rendering::GI;
using namespace Rendering::Manager;
using namespace Rendering::Light;
using namespace Rendering::Shadow;
using namespace Rendering::Material;

void RenderingSystem::InitializeRenderer(Engine& engine, const RenderSetting&& param)
{
	Object& object		= engine.GetObjectManager().Add(param.mainCamName);
	MainCamera& maincam	= engine.GetComponentSystem().SetMainCamera(object.GetObjectID());

	maincam.Initialize(engine.GetDirectX(), _shaderManager, param.renderRect);
	engine.AddRootObject(object);

	_shadowRenderer.Initialize(engine.GetDirectX(), param.shadowMapResolution, param.shadowMapResolution, param.shadowMapResolution);
	_mainRenderer.Initialize(engine.GetDirectX(), _shaderManager, maincam, param.giParam);

	_defaultShaders.Initialize(engine.GetDirectX(), _shaderManager);
	_backBufferMaker.Initialize(engine.GetDirectX(), _shaderManager);
}

void RenderingSystem::Initialize(Engine& engine)
{
	_materialManager.Initialize(engine.GetDirectX());
	_postProcessing.Initialize(engine.GetDirectX(), _shaderManager, engine.GetComponentSystem().GetMainCamera());
}

void RenderingSystem::Update(Engine& engine, float dt)
{
	_postProcessing.SetElapsedTime(dt);
}

void RenderingSystem::Render(Engine& engine, float dt)
{
	auto& dx = engine.GetDirectX();
	_materialManager.UpdateConstBuffer(dx);

	const auto& compoSys		= engine.GetComponentSystem();
	const auto& shadowMgr		= compoSys.GetManager_Direct<ShadowManager>();
	const auto cullParam		= engine.GetCullingParam();
	const auto meshRenderParam	= GetMeshRenderParam();
	_shadowRenderer.RenderShadowMap<SpotLightShadow>(dx, shadowMgr, _materialManager, cullParam, meshRenderParam);
	_shadowRenderer.RenderShadowMap<PointLightShadow>(dx, shadowMgr, _materialManager, cullParam, meshRenderParam);
	_shadowRenderer.RenderShadowMap<DirectionalLightShadow>(dx, shadowMgr, _materialManager, cullParam, meshRenderParam);

	const auto& lightMgr					= compoSys.GetManager_Direct<LightManager>();
	const auto& mainCamera					= compoSys.GetMainCamera();
	const SkyBoxMaterial* skyboxMaterial	= _materialManager.Find<SkyBoxMaterial>(mainCamera.GetSkyBoxMaterialID());
	_mainRenderer.UpdateCB(dx, mainCamera, lightMgr);
	_mainRenderer.Render(dx, MainRenderer::Param{mainCamera, meshRenderParam, _materialManager, lightMgr, ShadowSystem{shadowMgr, _shadowRenderer}, std::move(cullParam), skyboxMaterial});

	_postProcessing.SetElapsedTime(dt);
	_postProcessing.UpdateCB(dx);
	_postProcessing.Render(dx, _mainRenderer, mainCamera);

	AutoBinderSampler<PixelShader> sampler(dx, SamplerStateBindIndex(0), SamplerState::Point);
	_backBufferMaker.Render(dx, dx.GetBackBufferRT(), _mainRenderer.GetResultMap().GetTexture2D());

	dx.GetSwapChain()->Present(0, 0);

	_shadowRenderer.ClearDirty();
}

void RenderingSystem::Destroy(Engine& engine)
{

}<|endoftext|>


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

Program:   Medical Imaging & Interaction Toolkit
Module:    $RCSfile$
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

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

#include "mitkNativeRenderWindowInteractor.h"
#include "mitkVtkRenderWindow.h"

#include <vtkRenderWindowInteractor.h>

mitk::NativeRenderWindowInteractor::NativeRenderWindowInteractor() : m_MitkRenderWindow(NULL), m_NativeVtkRenderWindowInteractor(NULL)
{
  m_NativeVtkRenderWindowInteractor = vtkRenderWindowInteractor::New();
}

mitk::NativeRenderWindowInteractor::~NativeRenderWindowInteractor()
{

}

void mitk::NativeRenderWindowInteractor::SetMitkRenderWindow(mitk::RenderWindow* renderwindow)
{
  m_MitkRenderWindow = renderwindow;
  if(m_MitkRenderWindow != NULL)
    m_NativeVtkRenderWindowInteractor->SetRenderWindow(m_MitkRenderWindow->GetVtkRenderWindow());
}

void mitk::NativeRenderWindowInteractor::Start()
{
  if(m_MitkRenderWindow != NULL)
    m_NativeVtkRenderWindowInteractor->Start();
}
<commit_msg>FIX: Memory leak<commit_after>/*=========================================================================

Program:   Medical Imaging & Interaction Toolkit
Module:    $RCSfile$
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

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

#include "mitkNativeRenderWindowInteractor.h"
#include "mitkVtkRenderWindow.h"

#include <vtkRenderWindowInteractor.h>

mitk::NativeRenderWindowInteractor::NativeRenderWindowInteractor() : m_MitkRenderWindow(NULL), m_NativeVtkRenderWindowInteractor(NULL)
{
  m_NativeVtkRenderWindowInteractor = vtkRenderWindowInteractor::New();
}

mitk::NativeRenderWindowInteractor::~NativeRenderWindowInteractor()
{
  m_NativeVtkRenderWindowInteractor->Delete();
}

void mitk::NativeRenderWindowInteractor::SetMitkRenderWindow(mitk::RenderWindow* renderwindow)
{
  m_MitkRenderWindow = renderwindow;
  if(m_MitkRenderWindow != NULL)
    m_NativeVtkRenderWindowInteractor->SetRenderWindow(m_MitkRenderWindow->GetVtkRenderWindow());
}

void mitk::NativeRenderWindowInteractor::Start()
{
  if(m_MitkRenderWindow != NULL)
    m_NativeVtkRenderWindowInteractor->Start();
}
<|endoftext|>


<commit_before>/*
 * test_cyclecontrol.cpp
 *
 *  Created on: Nov 25, 2015
 *      Author: jschwan
 */

// boost
#include <boost/test/unit_test.hpp>

#include <iostream>
#include <chrono>
#include <iomanip>
#include <ctime>
#include <unistd.h>

#include <threading/cyclecontrol.hpp>

using namespace fc;

BOOST_AUTO_TEST_SUITE(test_cyclecontrol)

BOOST_AUTO_TEST_CASE( test_cyclecontrol_task_not_finished_in_time) {
	fc::thread::cycle_control thread_manager;
	std::atomic<bool> terminate_thread(false);
	auto tick_cycle = fc::thread::periodic_task([&terminate_thread]()
	{
		while(!terminate_thread)
			usleep(100);
	}, fc::thread::cycle_control::fast_tick);
	thread_manager.add_task(tick_cycle);
	thread_manager.start();

	bool exception_thrown=false;
	for (int i=0; i<300;++i)
	{
		if(auto except_ptr=thread_manager.last_exception()){
			BOOST_CHECK_THROW(std::rethrow_exception(except_ptr), std::runtime_error);
			exception_thrown=true;
			terminate_thread=true;
			break;
		}
		usleep(10000);
	}

	BOOST_CHECK(exception_thrown);
}

BOOST_AUTO_TEST_SUITE_END()

<commit_msg>Fix cppcheck false positive (unused var)<commit_after>/*
 * test_cyclecontrol.cpp
 *
 *  Created on: Nov 25, 2015
 *      Author: jschwan
 */

// boost
#include <boost/test/unit_test.hpp>

#include <iostream>
#include <chrono>
#include <iomanip>
#include <ctime>
#include <unistd.h>

#include <threading/cyclecontrol.hpp>

using namespace fc;

BOOST_AUTO_TEST_SUITE(test_cyclecontrol)

BOOST_AUTO_TEST_CASE( test_cyclecontrol_task_not_finished_in_time)
{
	fc::thread::cycle_control thread_manager;
	std::atomic<bool> terminate_thread(false);
	auto tick_cycle = fc::thread::periodic_task([&terminate_thread]()
	{
		while(!terminate_thread)
			usleep(100);
	}, fc::thread::cycle_control::fast_tick);
	thread_manager.add_task(tick_cycle);
	thread_manager.start();

	bool exception_thrown = false;
	for (int i = 0; i < 300; ++i)
	{
		if (auto except_ptr = thread_manager.last_exception())
		{
			BOOST_CHECK_THROW(std::rethrow_exception(except_ptr), std::runtime_error);
			exception_thrown = true;
			terminate_thread = true;
			break;
		}
		usleep(10000);
	}

	BOOST_CHECK(exception_thrown);
	BOOST_CHECK(terminate_thread);
}

BOOST_AUTO_TEST_SUITE_END()

<|endoftext|>