<commit_before>/*
 *            Copyright 2009-2018 The VOTCA Development Team
 *                       (http://www.votca.org)
 *
 *      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 <iostream>
#include <fstream>
#include <stdexcept>
#include <votca/xtp/votca_config.h>

#include <string>
#include <typeinfo>
#include <vector>
#include <boost/filesystem.hpp>
#include <type_traits>
#include <votca/tools/vec.h>
#include <votca/xtp/checkpoint_utils.h>
#include <votca/xtp/checkpointwriter.h>
#include <votca/xtp/checkpointreader.h>
#include <votca/xtp/checkpoint.h>
namespace votca {
namespace xtp {

using namespace checkpoint_utils;
namespace bfs = boost::filesystem;

std::ostream& operator<<(std::ostream& s, CheckpointAccessLevel l){

    switch(l){
    case CheckpointAccessLevel::READ:
        s << "read"; break;
    case CheckpointAccessLevel::EDIT:
        s << "edit"; break;
    case CheckpointAccessLevel::CREATE:
        s << "create"; break;
    }

    return s;
}

bool FileExists(const std::string& fileName){
    return bfs::exists(fileName);
}

void Backup(const std::string& fileName, int num){
    std::string backupName = fileName+"."+std::to_string(num);

    if (FileExists(backupName)){
        Backup(fileName, num+1);
    } else {
        bfs::path src(fileName);
        bfs::path dst(backupName);
        bfs::rename(src, dst);
    }
}

void Backup(const std::string& fileName){
    Backup(fileName, 1);
}

CheckpointFile::CheckpointFile(std::string fN):
    CheckpointFile(fN, CheckpointAccessLevel::EDIT) {};

CheckpointFile::CheckpointFile(std::string fN, CheckpointAccessLevel access)
    : _fileName(fN), _accessLevel(access){

    try {
        H5::Exception::dontPrint();

        switch (_accessLevel){
        case CheckpointAccessLevel::READ:
            _fileHandle = H5::H5File(_fileName, H5F_ACC_RDONLY);
            break;
        case CheckpointAccessLevel::CREATE:
            if (FileExists(_fileName)) Backup(_fileName);
            _fileHandle = H5::H5File(_fileName, H5F_ACC_TRUNC);
            break;
        case CheckpointAccessLevel::EDIT:
            if (!FileExists(_fileName))
                _fileHandle = H5::H5File(_fileName, H5F_ACC_TRUNC);
            else
                _fileHandle = H5::H5File(_fileName, H5F_ACC_RDWR);

        }

    } catch (H5::Exception& error) {
        std::stringstream message;
        message << "Could not access file " << _fileName;
        message << " with permission to " << _accessLevel << "." << std::endl;

        throw std::runtime_error(message.str());
    }
};

std::string CheckpointFile::getFileName() { return _fileName; };

H5::H5File CheckpointFile::getHandle() { return _fileHandle; };

CheckpointWriter CheckpointFile::getWriter(const std::string _path){
    if (_accessLevel == CheckpointAccessLevel::READ){
        throw std::runtime_error("Checkpoint file opened as read only.");
    }

    try{
        return CheckpointWriter(_fileHandle.createGroup(_path), _path);
    } catch(H5::Exception& error) {<commit_msg>Remove checkpoint file automatic backup feature<commit_after>/*
 *            Copyright 2009-2018 The VOTCA Development Team
 *                       (http://www.votca.org)
 *
 *      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 <iostream>
#include <fstream>
#include <stdexcept>
#include <votca/xtp/votca_config.h>

#include <string>
#include <typeinfo>
#include <vector>
#include <boost/filesystem.hpp>
#include <type_traits>
#include <votca/tools/vec.h>
#include <votca/xtp/checkpoint_utils.h>
#include <votca/xtp/checkpointwriter.h>
#include <votca/xtp/checkpointreader.h>
#include <votca/xtp/checkpoint.h>
namespace votca {
namespace xtp {

using namespace checkpoint_utils;
namespace bfs = boost::filesystem;

std::ostream& operator<<(std::ostream& s, CheckpointAccessLevel l){

    switch(l){
    case CheckpointAccessLevel::READ:
        s << "read"; break;
    case CheckpointAccessLevel::EDIT:
        s << "edit"; break;
    case CheckpointAccessLevel::CREATE:
        s << "create"; break;
    }

    return s;
}

bool FileExists(const std::string& fileName){
    return bfs::exists(fileName);
}

CheckpointFile::CheckpointFile(std::string fN):
    CheckpointFile(fN, CheckpointAccessLevel::EDIT) {};

CheckpointFile::CheckpointFile(std::string fN, CheckpointAccessLevel access)
    : _fileName(fN), _accessLevel(access){

    try {
        H5::Exception::dontPrint();

        switch (_accessLevel){
        case CheckpointAccessLevel::READ:
            _fileHandle = H5::H5File(_fileName, H5F_ACC_RDONLY);
            break;
        case CheckpointAccessLevel::CREATE:
            _fileHandle = H5::H5File(_fileName, H5F_ACC_TRUNC);
            break;
        case CheckpointAccessLevel::EDIT:
            if (!FileExists(_fileName))
                _fileHandle = H5::H5File(_fileName, H5F_ACC_TRUNC);
            else
                _fileHandle = H5::H5File(_fileName, H5F_ACC_RDWR);

        }

    } catch (H5::Exception& error) {
        std::stringstream message;
        message << "Could not access file " << _fileName;
        message << " with permission to " << _accessLevel << "." << std::endl;

        throw std::runtime_error(message.str());
    }
};

std::string CheckpointFile::getFileName() { return _fileName; };

H5::H5File CheckpointFile::getHandle() { return _fileHandle; };

CheckpointWriter CheckpointFile::getWriter(const std::string _path){
    if (_accessLevel == CheckpointAccessLevel::READ){
        throw std::runtime_error("Checkpoint file opened as read only.");
    }

    try{
        return CheckpointWriter(_fileHandle.createGroup(_path), _path);
    } catch(H5::Exception& error) {
{'fraction_non_alphanumeric': 0.11913250148544266, 'fraction_numerical': 0.008912655971479501, 'mean_word_length': 3.4035317200784827, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1746976', 'n_tokens_mistral': 2050, 'n_tokens_neox': 2027, 'n_words': 508}