<commit_before>// -----------------------------------------------------------------------------
// Copyright 2011 <NAME> (<EMAIL>)
//
// 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.
// -----------------------------------------------------------------------------


// Project includes
#include "GtpEngine.h"

// Fuego
#include <fuego/FuegoMainUtil.h>


@interface GtpEngine(Private)
/// @name Initialization and deallocation
//@{
- (id) initWithPipes:(NSArray*)pipes;
- (void) dealloc;
//@}
@end


@implementation GtpEngine

// -----------------------------------------------------------------------------
/// @brief xxx
// -----------------------------------------------------------------------------
+ (GtpEngine*) engineWithInputPipe:(NSString*)inputPipe outputPipe:(NSString*)outputPipe
{
  // Create copies so that the objects can be safely used by the thread when
  // it starts
  NSArray* pipes = [NSArray arrayWithObjects:[inputPipe copy], [outputPipe copy], nil];
  return [[[GtpEngine alloc] initWithPipes:pipes] autorelease];
}

// -----------------------------------------------------------------------------
/// @brief Initializes a GtpEngine object.
///
/// @note This is the designated initializer of GtpEngine.
// -----------------------------------------------------------------------------
- (id) initWithPipes:(NSArray*)pipes
{
  // Call designated initializer of superclass (NSObject)
  self = [super init];
  if (! self)
    return nil;

  // Create and start the thread
  m_thread = [[NSThread alloc] initWithTarget:self selector:@selector(mainLoop:) object:pipes];
  [m_thread start];

  // TODO: Clients that work with self returned here will invoke methods in
  // the context of the main thread. Find a clever/elegant solution so that
  // there is an automatic context switch. We don't want clients to worry about
  // calling NSObject's performSelector:onThread:withObject:waitUntilDone.
  return self;
}

// -----------------------------------------------------------------------------
/// @brief Deallocates memory allocated by this GtpEngine object.
// -----------------------------------------------------------------------------
- (void) dealloc
{
  // TODO implement stuff
  [m_thread release];
  [super dealloc];
}

// -----------------------------------------------------------------------------
/// @brief xxx
// -----------------------------------------------------------------------------
- (void) mainLoop:(NSArray*)pipes
{
  // Create an autorelease pool as the very first thing in this thread
  NSAutoreleasePool* mainPool = [[NSAutoreleasePool alloc] init];

  // Pipe to read commands from the GTP client
  NSString* inputPipePath = [pipes objectAtIndex:0];
  const char* pchInputPipePath = [inputPipePath cStringUsingEncoding:[NSString defaultCStringEncoding]];

  // Stream to write responses to the GTP client
  NSString* outputPipePath = [pipes objectAtIndex:1];
  const char* pchOutputPipePath = [outputPipePath cStringUsingEncoding:[NSString defaultCStringEncoding]];

  NSString* bookFile = [[NSBundle mainBundle] pathForResource:@"book" ofType:@"dat"];

  char programName[255];
  char inputPipeParameterName[255];
  char inputPipeParameterValue[255];
  char outputPipeParameterName[255];
  char outputPipeParameterValue[255];
  // TODO: fix this hack!!!
  sprintf(programName, "%s", [bookFile cStringUsingEncoding:[NSString defaultCStringEncoding]]);
  sprintf(inputPipeParameterName, "--input-pipe");
  sprintf(inputPipeParameterValue, "%s", pchInputPipePath);
  sprintf(outputPipeParameterName, "--output-pipe");
  sprintf(outputPipeParameterValue, "%s", pchOutputPipePath);
  int argc = 5;
  char* argv[argc];
  argv[0] = programName;
  argv[1] = inputPipeParameterName;
  argv[2] = inputPipeParameterValue;
  argv[3] = outputPipeParameterName;
  argv[4] = outputPipeParameterValue;
  argv[5] = noBookParameterName;

  try
  {
    // No need to create an autorelease pool, no Objective-C stuff is happening
    // in here...
    FuegoMainUtil::FuegoMain(argc, argv);
  }
  catch(std::exception& e)
  {
  }
  catch(...)<commit_msg>fixed compiler error from last commit<commit_after>// -----------------------------------------------------------------------------
// Copyright 2011 <NAME> (<EMAIL>)
//
// 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.
// -----------------------------------------------------------------------------


// Project includes
#include "GtpEngine.h"

// Fuego
#include <fuego/FuegoMainUtil.h>


@interface GtpEngine(Private)
/// @name Initialization and deallocation
//@{
- (id) initWithPipes:(NSArray*)pipes;
- (void) dealloc;
//@}
@end


@implementation GtpEngine

// -----------------------------------------------------------------------------
/// @brief xxx
// -----------------------------------------------------------------------------
+ (GtpEngine*) engineWithInputPipe:(NSString*)inputPipe outputPipe:(NSString*)outputPipe
{
  // Create copies so that the objects can be safely used by the thread when
  // it starts
  NSArray* pipes = [NSArray arrayWithObjects:[inputPipe copy], [outputPipe copy], nil];
  return [[[GtpEngine alloc] initWithPipes:pipes] autorelease];
}

// -----------------------------------------------------------------------------
/// @brief Initializes a GtpEngine object.
///
/// @note This is the designated initializer of GtpEngine.
// -----------------------------------------------------------------------------
- (id) initWithPipes:(NSArray*)pipes
{
  // Call designated initializer of superclass (NSObject)
  self = [super init];
  if (! self)
    return nil;

  // Create and start the thread
  m_thread = [[NSThread alloc] initWithTarget:self selector:@selector(mainLoop:) object:pipes];
  [m_thread start];

  // TODO: Clients that work with self returned here will invoke methods in
  // the context of the main thread. Find a clever/elegant solution so that
  // there is an automatic context switch. We don't want clients to worry about
  // calling NSObject's performSelector:onThread:withObject:waitUntilDone.
  return self;
}

// -----------------------------------------------------------------------------
/// @brief Deallocates memory allocated by this GtpEngine object.
// -----------------------------------------------------------------------------
- (void) dealloc
{
  // TODO implement stuff
  [m_thread release];
  [super dealloc];
}

// -----------------------------------------------------------------------------
/// @brief xxx
// -----------------------------------------------------------------------------
- (void) mainLoop:(NSArray*)pipes
{
  // Create an autorelease pool as the very first thing in this thread
  NSAutoreleasePool* mainPool = [[NSAutoreleasePool alloc] init];

  // Pipe to read commands from the GTP client
  NSString* inputPipePath = [pipes objectAtIndex:0];
  const char* pchInputPipePath = [inputPipePath cStringUsingEncoding:[NSString defaultCStringEncoding]];

  // Stream to write responses to the GTP client
  NSString* outputPipePath = [pipes objectAtIndex:1];
  const char* pchOutputPipePath = [outputPipePath cStringUsingEncoding:[NSString defaultCStringEncoding]];

  NSString* bookFile = [[NSBundle mainBundle] pathForResource:@"book" ofType:@"dat"];

  char programName[255];
  char inputPipeParameterName[255];
  char inputPipeParameterValue[255];
  char outputPipeParameterName[255];
  char outputPipeParameterValue[255];
  // TODO: fix this hack!!!
  sprintf(programName, "%s", [bookFile cStringUsingEncoding:[NSString defaultCStringEncoding]]);
  sprintf(inputPipeParameterName, "--input-pipe");
  sprintf(inputPipeParameterValue, "%s", pchInputPipePath);
  sprintf(outputPipeParameterName, "--output-pipe");
  sprintf(outputPipeParameterValue, "%s", pchOutputPipePath);
  int argc = 5;
  char* argv[argc];
  argv[0] = programName;
  argv[1] = inputPipeParameterName;
  argv[2] = inputPipeParameterValue;
  argv[3] = outputPipeParameterName;
  argv[4] = outputPipeParameterValue;

  try
  {
    // No need to create an autorelease pool, no Objective-C stuff is happening
    // in here...
    FuegoMainUtil::FuegoMain(argc, argv);
  }
  catch(std::exception& e)
  {
  }
  catch(...)
{'fraction_non_alphanumeric': 0.26502344857672594, 'fraction_numerical': 0.006870978296433635, 'mean_word_length': 2.031404958677686, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5355400', 'n_tokens_mistral': 2420, 'n_tokens_neox': 2295, 'n_words': 734}