<commit_before>// Copyright 2003-2009 <NAME>, with original implementations and ideas contributed by <NAME>.
// This source comes from the Yield project. It is licensed under the GPLv2 (see COPYING for terms and conditions).

#ifndef _YIELD_MAIN_H_
#define _YIELD_MAIN_H_

#include "yield/platform.h"

#include <algorithm>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#ifdef _WIN32
//#include <vld.h>
#include <windows.h> // For ConsoleCtrlHandler
#else
#ifdef __MACH__
#include <mach-o/dyld.h> // For _NSGetExecutablePath
#endif
#include <signal.h>
#endif

#include "SimpleOpt.h"


namespace YIELD
{
  class Main
  {
  public:
    const char* get_program_name() const { return program_name; }

#ifdef _WIN32
    virtual int main( char* args ) // From WinMain
    {
      std::vector<char*> argvv;

      char argv0[PATH_MAX];
      GetModuleFileNameA( NULL, argv0, PATH_MAX );
      argvv.push_back( argv0 );

      const char *start_args_p = args, *args_p = start_args_p;
      while ( *args_p != 0 )
      {
        while ( *args_p != ' ' && *args_p != 0 ) args_p++;
        size_t arg_len = args_p - start_args_p;
        char* arg = new char[arg_len+1];
        memcpy_s( arg, arg_len, start_args_p, arg_len );
        arg[arg_len] = 0;
        argvv.push_back( arg );
        if ( *args_p != 0 )
        {
          args_p++;
          start_args_p = args_p;
        }
      }

      int ret = main( static_cast<int>( argvv.size() ), &argvv[0] );

      for ( std::vector<char*>::size_type argvv_i = 1; argvv_i < argvv.size(); argvv_i++ )
        delete [] argvv[argvv_i];

      return ret;
    }
#endif

    virtual int main( int argc, char** argv )
    {
      int ret = 0;

      try
      {
        std::vector<CSimpleOpt::SOption> simpleopt_options;
        for ( std::vector<Option>::const_iterator option_i = options.begin(); option_i != options.end(); option_i++ )
        {
          const Option& option = *option_i;
          CSimpleOpt::SOption short_simpleopt_option = { option.get_id(), option.get_short_arg(), option.get_default_values() ? SO_REQ_SEP : SO_NONE };
          simpleopt_options.push_back( short_simpleopt_option );
          if ( option.get_long_arg() )
          {
            CSimpleOpt::SOption long_simpleopt_option = { option.get_id(), option.get_long_arg(), option.get_default_values() ? SO_REQ_SEP : SO_NONE };
            simpleopt_options.push_back( long_simpleopt_option );
          }
        }
        CSimpleOpt::SOption sentinel_simpleopt_option = SO_END_OF_OPTIONS;
        simpleopt_options.push_back( sentinel_simpleopt_option );

        // Make copies of the strings in argv so that SimpleOpt can punch holes in them
        std::vector<char*> argvv;
        for ( int arg_i = 0; arg_i < argc; arg_i++ )
        {
          size_t arg_len = strnlen( argv[arg_i], SIZE_MAX ) + 1;
          char* arg = new char[arg_len];
          memcpy_s( arg, arg_len, argv[arg_i], arg_len );
          argvv.push_back( arg ); 
        }

        // Check for the -h option first; if found print usage information and exit
        // Parsing options may cause side effects, but the program should do nothing if -h is specified
        {
          CSimpleOpt args( argc, &argvv[0], &simpleopt_options[0] );

          while ( args.Next() )
          {
            if ( args.LastError() == SO_SUCCESS && args.OptionId() == 0 )
            {
              printUsage();
              return 0;
            }
          }
        }

        // Now parse options
        {
          CSimpleOpt args( argc, &argvv[0], &simpleopt_options[0] );

          while ( args.Next() )
          {
            if ( args.LastError() == SO_SUCCESS && args.OptionId() > 0 )
              parseOption( args.OptionId(), args.OptionArg() );
          }

          parseFiles( args.FileCount(), args.Files() );
        }

        for ( std::vector<char*>::iterator arg_i = argvv.begin(); arg_i != argvv.end(); arg_i++ )
          delete [] *arg_i;
        argvv.clear();

        // Replace argv[0] with the absolute path to the executable<commit_msg>client: fixed option parsing problem in main.h<commit_after>// Copyright 2003-2009 <NAME>, with original implementations and ideas contributed by <NAME>.
// This source comes from the Yield project. It is licensed under the GPLv2 (see COPYING for terms and conditions).

#ifndef _YIELD_MAIN_H_
#define _YIELD_MAIN_H_

#include "yield/platform.h"

#include <algorithm>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#ifdef _WIN32
//#include <vld.h>
#include <windows.h> // For ConsoleCtrlHandler
#else
#ifdef __MACH__
#include <mach-o/dyld.h> // For _NSGetExecutablePath
#endif
#include <signal.h>
#endif

#include "SimpleOpt.h"


namespace YIELD
{
  class Main
  {
  public:
    const char* get_program_name() const { return program_name; }

#ifdef _WIN32
    virtual int main( char* args ) // From WinMain
    {
      std::vector<char*> argvv;

      char argv0[PATH_MAX];
      GetModuleFileNameA( NULL, argv0, PATH_MAX );
      argvv.push_back( argv0 );

      const char *start_args_p = args, *args_p = start_args_p;
      while ( *args_p != 0 )
      {
        while ( *args_p != ' ' && *args_p != 0 ) args_p++;
        size_t arg_len = args_p - start_args_p;
        char* arg = new char[arg_len+1];
        memcpy_s( arg, arg_len, start_args_p, arg_len );
        arg[arg_len] = 0;
        argvv.push_back( arg );
        if ( *args_p != 0 )
        {
          args_p++;
          start_args_p = args_p;
        }
      }

      int ret = main( static_cast<int>( argvv.size() ), &argvv[0] );

      for ( std::vector<char*>::size_type argvv_i = 1; argvv_i < argvv.size(); argvv_i++ )
        delete [] argvv[argvv_i];

      return ret;
    }
#endif

    virtual int main( int argc, char** argv )
    {
      int ret = 0;

      try
      {
        std::vector<CSimpleOpt::SOption> simpleopt_options;
        for ( std::vector<Option>::const_iterator option_i = options.begin(); option_i != options.end(); option_i++ )
        {
          const Option& option = *option_i;
          CSimpleOpt::SOption short_simpleopt_option = { option.get_id(), option.get_short_arg(), option.get_default_values() ? SO_REQ_SEP : SO_NONE };
          simpleopt_options.push_back( short_simpleopt_option );
          if ( option.get_long_arg() )
          {
            CSimpleOpt::SOption long_simpleopt_option = { option.get_id(), option.get_long_arg(), option.get_default_values() ? SO_REQ_SEP : SO_NONE };
            simpleopt_options.push_back( long_simpleopt_option );
          }
        }
        CSimpleOpt::SOption sentinel_simpleopt_option = SO_END_OF_OPTIONS;
        simpleopt_options.push_back( sentinel_simpleopt_option );

        // Make copies of the strings in argv so that SimpleOpt can punch holes in them
        std::vector<char*> argvv( argc );
        for ( int arg_i = 0; arg_i < argc; arg_i++ )
        {
          size_t arg_len = strnlen( argv[arg_i], SIZE_MAX ) + 1;
          argvv[arg_i] = new char[arg_len];
          memcpy_s( argvv[arg_i], arg_len, argv[arg_i], arg_len );
        }

        // Check for the -h option first; if found print usage information and exit
        // Parsing options may cause side effects, but the program should do nothing if -h is specified
        {
          CSimpleOpt args( argc, &argvv[0], &simpleopt_options[0] );

          while ( args.Next() )
          {
            if ( args.LastError() == SO_SUCCESS && args.OptionId() == 0 )
            {
              printUsage();
              return 0;
            }
          }
        }

        // Restore the underlying argv strings so that SimpleOpt can start fresh on the next pass
        for ( int arg_i = 0; arg_i < argc; arg_i++ )
        {
          size_t arg_len = strnlen( argv[arg_i], SIZE_MAX ) + 1;
          memcpy_s( argvv[arg_i], arg_len, argv[arg_i], arg_len );
        }        

        // Now parse options
        {
          CSimpleOpt args( argc, &argvv[0], &simpleopt_options[0] );

          while ( args.Next() )
          {
            if ( args.LastError() == SO_SUCCESS && args.OptionId() > 0 )
              parseOption( args.OptionId(), args.OptionArg() );
          }

          parseFiles( args.FileCount(), args.Files() );
        }

        for ( std::vector<char*>::iterator arg_i = argvv.begin(); arg_i != argvv.end(); arg_i++ )
          delete [] *arg_i;
        argvv.clear();

        // Replace argv[0] with the absolute path to the executable
{'fraction_non_alphanumeric': 0.12015413358243811, 'fraction_numerical': 0.008173750583839328, 'mean_word_length': 2.3133462282398454, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '104820', 'n_tokens_mistral': 2832, 'n_tokens_neox': 2733, 'n_words': 694}