<commit_before>/*=========================================================================
*
*  Copyright Insight Software Consortium
*
*  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.txt
*
*  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 "sitkImageViewer.h"
#include "sitkMacro.h"
#include "sitkImageFileWriter.h"
#include <itkMacro.h>
#include <itksys/SystemTools.hxx>
#include <itksys/Process.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <iterator>
#include <string>
#include <algorithm>
#include <ctype.h>

#ifdef _WIN32
#include <process.h>
#else
#include <unistd.h>
#endif


//
//  Dave's mental notes about ImageJ/Fiji and file formats
//
// ImageJ without plugins doesn't support MetaIO.
// ImageJ displaying a color NifTi image by defaults displays R, G, and B as separate channels.
// To display color NifTi images properly in ImageJ, you need to use the NIFTI_COLOR_MACRO
//
// Fiji does support MetaIO.
// Fiji has the same color NifTi problem as ImageJ.
//
// Invoking Fiji is simpler then ImageJ because the command line flags are the
// same on Windows and Linux.

#define localDebugMacro(x)\
  {                                                                     \
    if (itk::simple::ImageViewer::GetGlobalDefaultDebug())                     \
      {                                                                 \
      std::ostringstream msg;                                           \
      msg << "Debug: In " __FILE__ ", line " << __LINE__ << ": " x      \
          << "\n\n";                                                    \
      ::itk::OutputWindowDisplayDebugText( msg.str().c_str() );         \
      }                                                                 \
  }                                                                     \

#define IMAGEJ_OPEN_MACRO "open(\"%f\"); rename(\"%t\");"
#define NIFTI_COLOR_MACRO " run(\"Make Composite\", \"display=Composite\");"

namespace itk
{
  namespace simple
  {

namespace
{
  // forward declaration of some helper functions
  std::string ReplaceWords( const std::string & command, const std::string & app, const std::string & filename,
                            const std::string & title, bool& fileFlag );
  std::string UnquoteWord( const std::string & word );
  std::vector<std::string> ConvertCommand( const std::string & command, const std::string & app,
                                           const std::string & filename, const std::string & title="" );
  std::string FormatFileName ( const std::string & TempDirectory, const std::string & name,
                               const std::string & extension, const int tagID );
  std::string BuildFullFileName( const std::string & name, const std::string & extension, const int tagID );
#ifdef _WIN32
  std::string DoubleBackslashes( const std::string & word );
#endif

  void ExecuteCommand( const std::vector<std::string> & cmdLine, const unsigned int timeout=500 );
}


int ImageViewer::m_GlobalViewerImageCount=0;
bool ImageViewer::m_GlobalAreDefaultsInitialized=false;
unsigned int ImageViewer::m_GlobalProcessDelay;

std::vector<std::string> ImageViewer::m_GlobalDefaultSearchPath;
std::vector<std::string> ImageViewer::m_GlobalDefaultExecutableNames;

std::string ImageViewer::m_GlobalDefaultViewCommand;

std::string ImageViewer::m_GlobalDefaultApplication;
std::string ImageViewer::m_GlobalDefaultFileExtension;

bool ImageViewer::m_GlobalDefaultDebug=false;

//
// this is an ugly mess
//
void ImageViewer::initializeDefaults()
  {
  if (m_GlobalAreDefaultsInitialized)
    return;

  std::string Extension;
  std::string cmd;

  // check environment variables for user specified strings

  // File Extension
  itksys::SystemTools::GetEnv ( "SITK_SHOW_EXTENSION", Extension );
  if (Extension.length()>0)
    {
    m_GlobalDefaultFileExtension = Extension;
    }
  else
    {
    m_GlobalDefaultFileExtension = ".mha";
    }

  // Show command
  itksys::SystemTools::GetEnv ( "SITK_SHOW_COMMAND", cmd );
  if (cmd.length()>0)
    {
    m_GlobalDefaultViewCommand = cmd;
    }
  else
    {
#if defined(__APPLE__)
    m_GlobalDefaultViewCommand = "open -a %a -n --args -eval \'" IMAGEJ_OPEN_MACRO "\'";
#else
    // Linux & Windows
    m_GlobalDefaultViewCommand = "%a -eval \'" IMAGEJ_OPEN_MACRO "\'";
#endif
    }


  //
  // Build the SearchPath
  //
#ifdef _WIN32

  std::string ProgramFiles;
  std::vector<std::string> win_dirs = { "PROGRAMFILES", "PROGRAMFILES(x86)", "PROGRAMW6432" };

  for (unsigned int i=0; i<win_dirs.size(); i++)
    {<commit_msg>Fix: C++11 style init causing problems with RTools(windows) c++<commit_after>/*=========================================================================
*
*  Copyright Insight Software Consortium
*
*  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.txt
*
*  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 "sitkImageViewer.h"
#include "sitkMacro.h"
#include "sitkImageFileWriter.h"
#include <itkMacro.h>
#include <itksys/SystemTools.hxx>
#include <itksys/Process.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <iterator>
#include <string>
#include <algorithm>
#include <ctype.h>

#ifdef _WIN32
#include <process.h>
#else
#include <unistd.h>
#endif


//
//  Dave's mental notes about ImageJ/Fiji and file formats
//
// ImageJ without plugins doesn't support MetaIO.
// ImageJ displaying a color NifTi image by defaults displays R, G, and B as separate channels.
// To display color NifTi images properly in ImageJ, you need to use the NIFTI_COLOR_MACRO
//
// Fiji does support MetaIO.
// Fiji has the same color NifTi problem as ImageJ.
//
// Invoking Fiji is simpler then ImageJ because the command line flags are the
// same on Windows and Linux.

#define localDebugMacro(x)\
  {                                                                     \
    if (itk::simple::ImageViewer::GetGlobalDefaultDebug())                     \
      {                                                                 \
      std::ostringstream msg;                                           \
      msg << "Debug: In " __FILE__ ", line " << __LINE__ << ": " x      \
          << "\n\n";                                                    \
      ::itk::OutputWindowDisplayDebugText( msg.str().c_str() );         \
      }                                                                 \
  }                                                                     \

#define IMAGEJ_OPEN_MACRO "open(\"%f\"); rename(\"%t\");"
#define NIFTI_COLOR_MACRO " run(\"Make Composite\", \"display=Composite\");"

namespace itk
{
  namespace simple
  {

namespace
{
  // forward declaration of some helper functions
  std::string ReplaceWords( const std::string & command, const std::string & app, const std::string & filename,
                            const std::string & title, bool& fileFlag );
  std::string UnquoteWord( const std::string & word );
  std::vector<std::string> ConvertCommand( const std::string & command, const std::string & app,
                                           const std::string & filename, const std::string & title="" );
  std::string FormatFileName ( const std::string & TempDirectory, const std::string & name,
                               const std::string & extension, const int tagID );
  std::string BuildFullFileName( const std::string & name, const std::string & extension, const int tagID );
#ifdef _WIN32
  std::string DoubleBackslashes( const std::string & word );
#endif

  void ExecuteCommand( const std::vector<std::string> & cmdLine, const unsigned int timeout=500 );
}


int ImageViewer::m_GlobalViewerImageCount=0;
bool ImageViewer::m_GlobalAreDefaultsInitialized=false;
unsigned int ImageViewer::m_GlobalProcessDelay;

std::vector<std::string> ImageViewer::m_GlobalDefaultSearchPath;
std::vector<std::string> ImageViewer::m_GlobalDefaultExecutableNames;

std::string ImageViewer::m_GlobalDefaultViewCommand;

std::string ImageViewer::m_GlobalDefaultApplication;
std::string ImageViewer::m_GlobalDefaultFileExtension;

bool ImageViewer::m_GlobalDefaultDebug=false;

//
// this is an ugly mess
//
void ImageViewer::initializeDefaults()
  {
  if (m_GlobalAreDefaultsInitialized)
    return;

  std::string Extension;
  std::string cmd;

  // check environment variables for user specified strings

  // File Extension
  itksys::SystemTools::GetEnv ( "SITK_SHOW_EXTENSION", Extension );
  if (Extension.length()>0)
    {
    m_GlobalDefaultFileExtension = Extension;
    }
  else
    {
    m_GlobalDefaultFileExtension = ".mha";
    }

  // Show command
  itksys::SystemTools::GetEnv ( "SITK_SHOW_COMMAND", cmd );
  if (cmd.length()>0)
    {
    m_GlobalDefaultViewCommand = cmd;
    }
  else
    {
#if defined(__APPLE__)
    m_GlobalDefaultViewCommand = "open -a %a -n --args -eval \'" IMAGEJ_OPEN_MACRO "\'";
#else
    // Linux & Windows
    m_GlobalDefaultViewCommand = "%a -eval \'" IMAGEJ_OPEN_MACRO "\'";
#endif
    }


  //
  // Build the SearchPath
  //
#ifdef _WIN32

  std::string ProgramFiles;
  std::vector<std::string> win_dirs;
  win_dirs.push_back("PROGRAMFILES");
  win_dirs.push_back("PROGRAMFILES(x86)");
  win_dirs.push_back("PROGRAMW6432");

  for (unsigned int i=0; i<win_dirs.size(); i++)
    {
{'fraction_non_alphanumeric': 0.13823900592175517, 'fraction_numerical': 0.004659741772643433, 'mean_word_length': 2.8283166109253064, 'pattern_counts': {'":': 2, '<': 55, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1339646', 'n_tokens_mistral': 2958, 'n_tokens_neox': 2865, 'n_words': 814}