<commit_before>/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
 *  Main authors:
 *     <NAME> <<EMAIL>>
 */

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef __MINIZINC_UTILS_H__
#define __MINIZINC_UTILS_H__

#include <string>
#include <sstream>
#include <ctime>
#include <limits>
#include <iomanip>


using namespace std;

namespace MiniZinc {
  
  inline std::string stoptime(clock_t& start) {
    std::ostringstream oss;
    clock_t now = clock();
    oss << std::setprecision(0) << std::fixed << ((static_cast<double>(now-start) / CLOCKS_PER_SEC) * 1000.0) << " ms";
    start = now;
    return oss.str();
  }

  inline std::string timeDiff(clock_t t2, clock_t t1) {
    std::ostringstream oss;
    oss << std::setprecision(2) << std::fixed << ((static_cast<double>(t2-t1) / CLOCKS_PER_SEC)) << " s";
    return oss.str();
  }

  inline bool beginswith(string s, string t) {
    return s.compare(0, t.length(), t)==0;
  }

  inline void checkIOStatus( bool fOk, string msg )
  {
    if ( !fOk ) {
      std::cerr << "\n  " << msg << strerror(errno) << "." << std::endl;
      exit(EXIT_FAILURE);
    }
  }
  
  template <class T> inline bool assignStr(T*, const string ) { return false; }
  template<> inline bool assignStr(string* pS, const string s ) {
    *pS = s;
    return true;
  }
  
  /// A simple per-cmdline option parser
  class CLOParser {
    int& i;              // current item
    const int argc=0;
    const char* const* argv=0;
    
  public:
    CLOParser( int& ii, const int ac, const char* const* av )
      : i(ii), argc(ac), argv(av) { }
    template <class Value=int>
    inline bool getOption(  const char* names, // space-separated option list
                            Value* pResult=nullptr, // pointer to value storage
                            bool fValueOptional=false // if pResult, for non-string values
                ) {
      if( i>=argc )
        return false;
      assert( argv[i] );
      string arg( argv[i] );
      /// Separate keywords
      string keyword;
      istringstream iss( names );
      while ( iss >> keyword ) {
        if ( 0!=arg.compare( 0, keyword.size(), keyword ) )
          continue;
        /// Process it
        if ( keyword.size() < arg.size() ) {
          if ( 0==pResult )
            continue;
          arg.erase( 0, keyword.size() );
        } else {
          if ( 0==pResult )
            return true;
          i++;
          if( i>=argc )
            return false;
          arg = argv[i];
        }
        assert( pResult );
        if ( assignStr( pResult, arg ) ) 
          return true;
        istringstream iss( arg );<commit_msg>For optional cmdline values, can stop after the end of the list<commit_after>/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
 *  Main authors:
 *     <NAME> <<EMAIL>>
 */

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef __MINIZINC_UTILS_H__
#define __MINIZINC_UTILS_H__

#include <string>
#include <sstream>
#include <ctime>
#include <limits>
#include <iomanip>


using namespace std;

namespace MiniZinc {
  
  inline std::string stoptime(clock_t& start) {
    std::ostringstream oss;
    clock_t now = clock();
    oss << std::setprecision(0) << std::fixed << ((static_cast<double>(now-start) / CLOCKS_PER_SEC) * 1000.0) << " ms";
    start = now;
    return oss.str();
  }

  inline std::string timeDiff(clock_t t2, clock_t t1) {
    std::ostringstream oss;
    oss << std::setprecision(2) << std::fixed << ((static_cast<double>(t2-t1) / CLOCKS_PER_SEC)) << " s";
    return oss.str();
  }

  inline bool beginswith(string s, string t) {
    return s.compare(0, t.length(), t)==0;
  }

  inline void checkIOStatus( bool fOk, string msg )
  {
    if ( !fOk ) {
      std::cerr << "\n  " << msg << strerror(errno) << "." << std::endl;
      exit(EXIT_FAILURE);
    }
  }
  
  template <class T> inline bool assignStr(T*, const string ) { return false; }
  template<> inline bool assignStr(string* pS, const string s ) {
    *pS = s;
    return true;
  }
  
  /// A simple per-cmdline option parser
  class CLOParser {
    int& i;              // current item
    const int argc=0;
    const char* const* argv=0;
    
  public:
    CLOParser( int& ii, const int ac, const char* const* av )
      : i(ii), argc(ac), argv(av) { }
    template <class Value=int>
    inline bool getOption(  const char* names, // space-separated option list
                            Value* pResult=nullptr, // pointer to value storage
                            bool fValueOptional=false // if pResult, for non-string values
                ) {
      if( i>=argc )
        return false;
      assert( argv[i] );
      string arg( argv[i] );
      /// Separate keywords
      string keyword;
      istringstream iss( names );
      while ( iss >> keyword ) {
        if ( 0!=arg.compare( 0, keyword.size(), keyword ) )
          continue;
        /// Process it
        if ( keyword.size() < arg.size() ) {
          if ( 0==pResult )
            continue;
          arg.erase( 0, keyword.size() );
        } else {
          if ( 0==pResult )
            return true;
          i++;
          if( i>=argc )
            return fValueOptional;
          arg = argv[i];
        }
        assert( pResult );
        if ( assignStr( pResult, arg ) ) 
          return true;
        istringstream iss( arg );
{'fraction_non_alphanumeric': 0.13803411288904519, 'fraction_numerical': 0.008791981712678037, 'mean_word_length': 2.146017699115044, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 37, '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': '740643', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1829, 'n_words': 537}