<commit_before>/**
Authors: 
<NAME> <<EMAIL>>

Maintainers: 
Currently maintained by the authors.

This file is part of the MONICA model. 
Copyright (C) 2007-2013, Leibniz Centre for Agricultural Landscape Research (ZALF)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <fstream>
#include <sys/stat.h>

#include "monica-parameters.h"
#include "monica.h"
#include "soilcolumn.h"
#include "soiltemperature.h"
#include "soilmoisture.h"
#include "soilorganic.h"
#include "soiltransport.h"
#include "crop.h"

#include "conversion.h"
#include "climate/climate-common.h"
#include "db/abstract-db-connections.h"
#include "tools/algorithms.h"
#include "tools/read-ini.h"

#include "configuration.h"

#ifdef __unix__
  #include <sys/io.h>
#else
  #include <io.h>
#endif

#include <boost/foreach.hpp>
#include <boost/format.hpp>

/* write cson to string */
extern "C" {
  static int cson_data_dest_string(void * state, void const * src, unsigned int n )
  {
    if( ! state ) return cson_rc.ArgError;
    if( !src || !n ) return 0;
    else
    {
      std::string *str = static_cast<std::string*>(state);
      str->append((char*) src, n);
      return 0;
    }
  }
}

/* TODO: nach Tools:: ? */
struct DMY_
{
  int d, m, y;
  Tools::Date toDate(bool useLeapYears = true) const
  {
    return Tools::Date(d, m, y, useLeapYears);
  }
};

// to read HERMES two digit date format in management files
struct ParseDate_
{
  DMY_ operator()(const std::string & d)
  {
//    ISO Date YYYY-MM-DD
//    if (std::regex_match(d, "(\d{4}\-\d\d\-\d\d)")) {
//      DMY_ r;
//      r.d = atoi(d.substr(8, 2).c_str());
//      r.m = atoi(d.substr(5, 2).c_str());
//      r.y = atoi(d.substr(2, 2).c_str());
//      r.y = r.y <= 76 ? 2000 + r.y : 1900 + r.y;
//      std::cout << d << r.toDate().toString() << std::endl;
//      return r;
//    }

//    ISO Date YYYY-MM-DD
    if (d.length() == 10 &&
        std::count(d.begin(), d.end(), '-') == 2 &&
        d.at(4) == '-'
        && d.at(7) == '-') {
      DMY_ r;
      r.d = atoi(d.substr(8, 2).c_str());
      r.m = atoi(d.substr(5, 2).c_str());
      r.y = atoi(d.substr(2, 2).c_str());
      r.y = r.y <= 76 ? 2000 + r.y : 1900 + r.y;
//      std::cout << d << " -> " << r.toDate().toString() << std::endl;
      return r;
    }

    DMY_ r;
    r.d = atoi(d.substr(0, 2).c_str());
    r.m = atoi(d.substr(2, 2).c_str());
    r.y = atoi(d.substr(4, 2).c_str());
    r.y = r.y <= 76 ? 2000 + r.y : 1900 + r.y;
    return r;
  }
} parseDate_;


const cson_value* Monica::Configuration::metaSim = NULL;<commit_msg>use ISO date in monica-json<commit_after>/**
Authors: 
<NAME> <<EMAIL>>

Maintainers: 
Currently maintained by the authors.

This file is part of the MONICA model. 
Copyright (C) 2007-2013, Leibniz Centre for Agricultural Landscape Research (ZALF)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <fstream>
#include <sys/stat.h>

#include "monica-parameters.h"
#include "monica.h"
#include "soilcolumn.h"
#include "soiltemperature.h"
#include "soilmoisture.h"
#include "soilorganic.h"
#include "soiltransport.h"
#include "crop.h"

#include "conversion.h"
#include "climate/climate-common.h"
#include "db/abstract-db-connections.h"
#include "tools/algorithms.h"
#include "tools/read-ini.h"

#include "configuration.h"

#ifdef __unix__
  #include <sys/io.h>
#else
  #include <io.h>
#endif

#include <boost/foreach.hpp>
#include <boost/format.hpp>

/* write cson to string */
extern "C" {
  static int cson_data_dest_string(void * state, void const * src, unsigned int n )
  {
    if( ! state ) return cson_rc.ArgError;
    if( !src || !n ) return 0;
    else
    {
      std::string *str = static_cast<std::string*>(state);
      str->append((char*) src, n);
      return 0;
    }
  }
}

/* TODO: nach Tools:: ? */
struct DMY_
{
  int d, m, y;
  Tools::Date toDate(bool useLeapYears = true) const
  {
    return Tools::Date(d, m, y, useLeapYears);
  }
};

// copied ParseDate from monica-parameters.cpp
struct ParseDate_
{
  DMY_ operator()(const std::string & d)
  {

    DMY_ r;

    /* ISO Date YYYY-MM-DD */
//    TODO: use regexp if available in new gcc version
    if (d.length() == 10 &&
        std::count(d.begin(), d.end(), '-') == 2 &&
        d.at(4) == '-'
        && d.at(7) == '-') {
      r.d = atoi(d.substr(8, 2).c_str());
      r.m = atoi(d.substr(5, 2).c_str());
      r.y = atoi(d.substr(0, 4).c_str());
    }

    return r;
  }
} parseDate_;


const cson_value* Monica::Configuration::metaSim = NULL;
{'fraction_non_alphanumeric': 0.15588547189819724, 'fraction_numerical': 0.015376458112407211, 'mean_word_length': 3.133674214755296, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 30, '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': '7422588', 'n_tokens_mistral': 2133, 'n_tokens_neox': 2032, 'n_words': 609}