<commit_before>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * 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 "debugcmd_client.h"

#include <fstream>
#include <sstream>
#include <csignal>
#include <zorba/zorba.h>
#ifdef WIN32
#include <windows.h>
#endif

using namespace zorba;

#ifdef SIGINT /* not all system have SIGINT */
void suspend( int aSignum )
{
  ZorbaDebuggerClient * lClient = CommandLineEventHandler::getClient();
  if( lClient != 0 && lClient->isQueryRunning() )
  {
    lClient->suspend();
  } else {
    //if the query is not running, we call the default signal handling
    signal( SIGINT, SIG_DFL ); 
    raise( SIGINT );
  }
}
#endif

ZorbaDebuggerClient * CommandLineEventHandler::theClient = 0;

ZorbaDebuggerClient * CommandLineEventHandler::getClient()
{
  return theClient;
}

CommandLineEventHandler::CommandLineEventHandler( std::string aFileName,
                                                  std::auto_ptr<std::istream> &aQueryFile,
                                                  std::istream & anInput,
                                                  std::ostream & anOutput,
                                                  ZorbaDebuggerClient * aClient )
 : theFileName( aFileName ),
   theQueryFile( aQueryFile ),
   theLocation(0),
   theOutput( anOutput ),
   theInput( anInput )
{
  theClient = aClient;
}

void CommandLineEventHandler::update_location()
{
  theLocation.reset( theClient->getLocation() );
}

void CommandLineEventHandler::listMore()
{
  update_location();
  int start = theLocation->getLineBegin() - 4;
  if ( start <= 0 )
  {
    list( 1, 9 );
  } else {
    list( start, theLocation->getLineBegin() + 4 );
  }
}

void CommandLineEventHandler::list()
{
  update_location();
  int start = theLocation->getLineBegin() - 2;
  if ( start <= 0 )
  {
    list( 1, 5 );
  } else {
    list( start, theLocation->getLineBegin() + 2 );
  }
}

void CommandLineEventHandler::list( unsigned int aLineNo )
{
  list( aLineNo, aLineNo );
}

void CommandLineEventHandler::list( unsigned int aBegin, unsigned int anEnd, bool listAll )
{
  update_location();
  std::string lLine;
  unsigned int lLineNo = 0;
  std::string::iterator lIterator;
  unsigned int i = 0;
  std::ifstream * lFile = dynamic_cast< std::ifstream * >( theQueryFile.get() );
  if ( lFile != 0 )
  {
    if( theLocation->getFileName() == "" )
    {
      theQueryFile.reset( new std::ifstream( theFileName.c_str() ) );
    } else {
      
      theQueryFile.reset( new std::ifstream( theLocation->getFileName().c_str() ) );
    }
  } else {
    theQueryFile->clear();
    theQueryFile->seekg( 0, std::ios::beg );
  }
  theOutput << std::endl; 
  while(theQueryFile->good() && !theQueryFile->eof())
  {
    lLineNo++;
    std::getline( *theQueryFile, lLine, '\n');
    if ( (lLineNo >= aBegin && lLineNo <= anEnd) || listAll )
    {
      if ( lLineNo >= theLocation->getLineBegin() && lLineNo <= theLocation->getLineEnd() )
      {
#ifdef WIN32
        HANDLE lConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_SCREEN_BUFFER_INFO lConsoleInfo;
        GetConsoleScreenBufferInfo(lConsole, &lConsoleInfo);
        const int saved_configuration = lConsoleInfo.wAttributes;
        SetConsoleTextAttribute(lConsole, 15+0*16); 
        theOutput << lLineNo << '\t';
        SetConsoleTextAttribute(lConsole, saved_configuration);
        for(unsigned int j=1; j <= lLine.length(); j++)
        {
          if( j >= theLocation->getColumnBegin() && j <= theLocation->getColumnEnd() )
          {
            SetConsoleTextAttribute(lConsole, 15+0*16); 
            theOutput << lLine.at(j-1);
            SetConsoleTextAttribute(lConsole, saved_configuration);
          } else {
            SetConsoleTextAttribute(lConsole, 15+0*16); 
            theOutput << lLine.at(j-1);
            SetConsoleTextAttribute(lConsole, saved_configuration);
          }
        }
#else
        theOutput << "\033[1m" << lLineNo << '\t' << "\033[0m"; 
        for(unsigned int j=1; j <= lLine.length(); j++)
        {
          if((lLineNo==theLocation->getLineBegin() && j >= theLocation->getColumnBegin()) ||
             (lLineNo==theLocation->getLineEnd() && j <= theLocation->getColumnEnd()) ||
             (lLineNo > theLocation->getLineBegin() && lLineNo < theLocation->getLineEnd())
            )
          {
            theOutput << "\033[1m" << lLine.at(j-1) << "\033[0m";
          } else {
            theOutput << lLine.at(j-1);
          }
        }
#endif
        theOutput << std::endl;<commit_msg>Fix debugger highlighting for windows.<commit_after>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * 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 "debugcmd_client.h"

#include <fstream>
#include <sstream>
#include <csignal>
#include <zorba/zorba.h>
#ifdef WIN32
#include <windows.h>
#endif

using namespace zorba;

#ifdef SIGINT /* not all system have SIGINT */
void suspend( int aSignum )
{
  ZorbaDebuggerClient * lClient = CommandLineEventHandler::getClient();
  if( lClient != 0 && lClient->isQueryRunning() )
  {
    lClient->suspend();
  } else {
    //if the query is not running, we call the default signal handling
    signal( SIGINT, SIG_DFL ); 
    raise( SIGINT );
  }
}
#endif

ZorbaDebuggerClient * CommandLineEventHandler::theClient = 0;

ZorbaDebuggerClient * CommandLineEventHandler::getClient()
{
  return theClient;
}

CommandLineEventHandler::CommandLineEventHandler( std::string aFileName,
                                                  std::auto_ptr<std::istream> &aQueryFile,
                                                  std::istream & anInput,
                                                  std::ostream & anOutput,
                                                  ZorbaDebuggerClient * aClient )
 : theFileName( aFileName ),
   theQueryFile( aQueryFile ),
   theLocation(0),
   theOutput( anOutput ),
   theInput( anInput )
{
  theClient = aClient;
}

void CommandLineEventHandler::update_location()
{
  theLocation.reset( theClient->getLocation() );
}

void CommandLineEventHandler::listMore()
{
  update_location();
  int start = theLocation->getLineBegin() - 4;
  if ( start <= 0 )
  {
    list( 1, 9 );
  } else {
    list( start, theLocation->getLineBegin() + 4 );
  }
}

void CommandLineEventHandler::list()
{
  update_location();
  int start = theLocation->getLineBegin() - 2;
  if ( start <= 0 )
  {
    list( 1, 5 );
  } else {
    list( start, theLocation->getLineBegin() + 2 );
  }
}

void CommandLineEventHandler::list( unsigned int aLineNo )
{
  list( aLineNo, aLineNo );
}

void CommandLineEventHandler::list( unsigned int aBegin, unsigned int anEnd, bool listAll )
{
  update_location();
  std::string lLine;
  unsigned int lLineNo = 0;
  std::string::iterator lIterator;
  unsigned int i = 0;
  std::ifstream * lFile = dynamic_cast< std::ifstream * >( theQueryFile.get() );
  if ( lFile != 0 )
  {
    if( theLocation->getFileName() == "" )
    {
      theQueryFile.reset( new std::ifstream( theFileName.c_str() ) );
    } else {
      
      theQueryFile.reset( new std::ifstream( theLocation->getFileName().c_str() ) );
    }
  } else {
    theQueryFile->clear();
    theQueryFile->seekg( 0, std::ios::beg );
  }
  theOutput << std::endl; 
  while(theQueryFile->good() && !theQueryFile->eof())
  {
    lLineNo++;
    std::getline( *theQueryFile, lLine, '\n');
    if ( (lLineNo >= aBegin && lLineNo <= anEnd) || listAll )
    {
      if ( lLineNo >= theLocation->getLineBegin() && lLineNo <= theLocation->getLineEnd() )
      {
#ifdef WIN32
        HANDLE lConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_SCREEN_BUFFER_INFO lConsoleInfo;
        GetConsoleScreenBufferInfo(lConsole, &lConsoleInfo);
        const int saved_configuration = lConsoleInfo.wAttributes;
        SetConsoleTextAttribute(lConsole, 15+0*16); 
        theOutput << lLineNo << '\t';
        SetConsoleTextAttribute(lConsole, saved_configuration);
        for(unsigned int j=1; j <= lLine.length(); j++)
        {
          if((lLineNo==theLocation->getLineBegin() && j >= theLocation->getColumnBegin()) ||
             (lLineNo==theLocation->getLineEnd() && j <= theLocation->getColumnEnd()) ||
             (lLineNo > theLocation->getLineBegin() && lLineNo < theLocation->getLineEnd())
            )
          {
            SetConsoleTextAttribute(lConsole, 15+0*16); 
            theOutput << lLine.at(j-1);
            SetConsoleTextAttribute(lConsole, saved_configuration);
          } else {
            SetConsoleTextAttribute(lConsole, 15+0*16); 
            theOutput << lLine.at(j-1);
            SetConsoleTextAttribute(lConsole, saved_configuration);
          }
        }
#else
        theOutput << "\033[1m" << lLineNo << '\t' << "\033[0m"; 
        for(unsigned int j=1; j <= lLine.length(); j++)
        {
          if((lLineNo==theLocation->getLineBegin() && j >= theLocation->getColumnBegin()) ||
             (lLineNo==theLocation->getLineEnd() && j <= theLocation->getColumnEnd()) ||
             (lLineNo > theLocation->getLineBegin() && lLineNo < theLocation->getLineEnd())
            )
          {
            theOutput << "\033[1m" << lLine.at(j-1) << "\033[0m";
          } else {
            theOutput << lLine.at(j-1);
          }
        }
#endif
        theOutput << std::endl;
{'fraction_non_alphanumeric': 0.13106325184322856, 'fraction_numerical': 0.013581684128831975, 'mean_word_length': 2.7337921043100324, 'pattern_counts': {'":': 0, '<': 92, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6778687', 'n_tokens_mistral': 3165, 'n_tokens_neox': 3084, 'n_words': 728}