| [73a5f7] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2016 Frederik Heber. All rights reserved.
 | 
|---|
 | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
 | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /*
 | 
|---|
 | 24 |  * SaveFragmentResultsAction.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Sep 04, 2016
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | // include headers that implement a archive in simple text format
 | 
|---|
 | 36 | // and before MemDebug due to placement new
 | 
|---|
 | 37 | #include <boost/archive/text_oarchive.hpp>
 | 
|---|
 | 38 | #include <boost/archive/text_iarchive.hpp>
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | #include <iostream>
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 | #include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp"
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 | #include "Actions/FragmentationAction/SaveFragmentResultsAction.hpp"
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 | using namespace MoleCuilder;
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 | // and construct the stuff
 | 
|---|
 | 53 | #include "SaveFragmentResultsAction.def"
 | 
|---|
 | 54 | #include "Action_impl_pre.hpp"
 | 
|---|
 | 55 | /** =========== define the function ====================== */
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 | bool writeFragmentResultsToFile(
 | 
|---|
 | 58 |     const boost::filesystem::path &fragmentresults_file
 | 
|---|
 | 59 |     )
 | 
|---|
 | 60 | {
 | 
|---|
 | 61 |   /// if file is given, parse from file into ResultsContainer
 | 
|---|
 | 62 |   FragmentationResultContainer& container = FragmentationResultContainer::getInstance();
 | 
|---|
 | 63 |   if (!boost::filesystem::exists(fragmentresults_file)) {
 | 
|---|
 | 64 |     LOG(1, "INFO: Saving fragment results to " << fragmentresults_file.string() << ".");
 | 
|---|
 | 65 |     std::ofstream returnstream(fragmentresults_file.string().c_str());
 | 
|---|
 | 66 |     if (returnstream.good()) {
 | 
|---|
 | 67 |       boost::archive::text_oarchive oa(returnstream);
 | 
|---|
 | 68 |       oa << container;
 | 
|---|
 | 69 |       if (returnstream.bad()) { // check if correctly written
 | 
|---|
 | 70 |         LOG(1, "Failed to write to file " << fragmentresults_file.string() << ".");
 | 
|---|
 | 71 |         return false;
 | 
|---|
 | 72 |       } else
 | 
|---|
 | 73 |         returnstream.close();
 | 
|---|
 | 74 |     }
 | 
|---|
 | 75 |   } else {
 | 
|---|
 | 76 |     ELOG(1, "Given file" << fragmentresults_file.string() << " already exists.");
 | 
|---|
 | 77 |     return false;
 | 
|---|
 | 78 |   }
 | 
|---|
 | 79 |   return true;
 | 
|---|
 | 80 | }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 | ActionState::ptr FragmentationSaveFragmentResultsAction::performCall()
 | 
|---|
 | 83 | {
 | 
|---|
 | 84 |   if (!params.fragmentresults_file.get().empty()) {
 | 
|---|
 | 85 |     const boost::filesystem::path &fragmentresults_file = params.fragmentresults_file.get();
 | 
|---|
 | 86 |     if (writeFragmentResultsToFile(fragmentresults_file)) {
 | 
|---|
 | 87 |       STATUS("SUCCESS: Saving fragment results to file "+toString(fragmentresults_file.string())+".");
 | 
|---|
 | 88 |       return Action::success;
 | 
|---|
 | 89 |     } else {
 | 
|---|
 | 90 |       STATUS("FAIL: Failed to write fragment results file.");
 | 
|---|
 | 91 |       return Action::failure;
 | 
|---|
 | 92 |     }
 | 
|---|
 | 93 |   } else {
 | 
|---|
 | 94 |     STATUS("FAIL: Fragment results file name empty.");
 | 
|---|
 | 95 |     return Action::failure;
 | 
|---|
 | 96 |   }
 | 
|---|
 | 97 | }
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 | ActionState::ptr FragmentationSaveFragmentResultsAction::performUndo(ActionState::ptr _state) {
 | 
|---|
 | 100 |   return Action::success;
 | 
|---|
 | 101 | }
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 | ActionState::ptr FragmentationSaveFragmentResultsAction::performRedo(ActionState::ptr _state){
 | 
|---|
 | 104 |   return Action::success;
 | 
|---|
 | 105 | }
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 | bool FragmentationSaveFragmentResultsAction::canUndo() {
 | 
|---|
 | 108 |   return false;
 | 
|---|
 | 109 | }
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 | bool FragmentationSaveFragmentResultsAction::shouldUndo() {
 | 
|---|
 | 112 |   return false;
 | 
|---|
 | 113 | }
 | 
|---|
 | 114 | /** =========== end of function ====================== */
 | 
|---|