| [c66ae6] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2015 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 |  * AdjacencyMatcher.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Jul 19, 2015
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  *
 | 
|---|
 | 29 |  *
 | 
|---|
 | 30 |  *  This is mostly copy&pasted from AdjacencyMatcher.cpp
 | 
|---|
 | 31 |  */
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | // include config.h
 | 
|---|
 | 34 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 35 | #include <config.h>
 | 
|---|
 | 36 | #endif
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | #include <boost/exception/all.hpp>
 | 
|---|
 | 41 | #include <boost/filesystem/operations.hpp>
 | 
|---|
 | 42 | #include <boost/filesystem/path.hpp>
 | 
|---|
 | 43 | #include <boost/tokenizer.hpp>
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | #include <algorithm>
 | 
|---|
 | 46 | #include <fstream>
 | 
|---|
 | 47 | #include <iostream>
 | 
|---|
 | 48 | #include <iterator>
 | 
|---|
 | 49 | #include <map>
 | 
|---|
 | 50 | #include <set>
 | 
|---|
 | 51 | #include <sstream>
 | 
|---|
 | 52 | #include <string>
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | #include "Adjacency.hpp"
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 | #include "CodePatterns/toString.hpp"
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 | typedef std::set<detail::Adjacency> adjacencies_t;
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 | using namespace detail;
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 | namespace bf = boost::filesystem;
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 | typedef boost::error_info<
 | 
|---|
 | 65 |                 struct tag_IllegalTesselationFileContent_LineNumber,
 | 
|---|
 | 66 |                 int> IllegalTesselationFileContent_LineNumber;
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 | typedef boost::error_info<
 | 
|---|
 | 69 |                 struct tag_IllegalTesselationFileContent_Entries,
 | 
|---|
 | 70 |                 int> IllegalTesselationFileContent_Entries;
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | typedef boost::error_info<
 | 
|---|
 | 73 |                 struct tag_IllegalTesselationFileContent_Line,
 | 
|---|
 | 74 |                 int> IllegalTesselationFileContent_Line;
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 | //!> Exception for illegal lines encountered in adjacencies files
 | 
|---|
 | 77 | struct IllegalTesselationFileContentException:
 | 
|---|
 | 78 |                 virtual boost::exception, virtual std::exception { };
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 | Adjacency::index_t toIndex(const std::string &_element)
 | 
|---|
 | 81 | {
 | 
|---|
 | 82 |         Adjacency::index_t index;
 | 
|---|
 | 83 |         std::stringstream tokenstring(_element);
 | 
|---|
 | 84 |         tokenstring >> index;
 | 
|---|
 | 85 |         return index;
 | 
|---|
 | 86 | }
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 | template <class T>
 | 
|---|
 | 89 | std::ostream& operator<<(
 | 
|---|
 | 90 |                 std::ostream &_ost,
 | 
|---|
 | 91 |                 const std::set<T> &elements)
 | 
|---|
 | 92 | {
 | 
|---|
 | 93 |         for( const T elem : elements)
 | 
|---|
 | 94 |                 _ost << elem << std::endl;
 | 
|---|
 | 95 |         return _ost;
 | 
|---|
 | 96 | }
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 | /** Parses a adjacencies file into a vector of Adjacencys.
 | 
|---|
 | 99 |  *
 | 
|---|
 | 100 |  * \param _filename name of file to parse
 | 
|---|
 | 101 |  * \return adjacencies_t structure filled with adjacency index triples
 | 
|---|
 | 102 |  */
 | 
|---|
 | 103 | const adjacencies_t parseFile(const boost::filesystem::path &_filename)
 | 
|---|
 | 104 | {
 | 
|---|
 | 105 |         adjacencies_t adjacencies;
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 |         std::ifstream infile(_filename.string().c_str());
 | 
|---|
 | 108 |         boost::char_separator<char> sep(" \t");
 | 
|---|
 | 109 | 
 | 
|---|
 | 110 |         typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
 | 
|---|
 | 111 |         std::string line;
 | 
|---|
 | 112 |         while (!infile.eof()) {
 | 
|---|
 | 113 |                 std::getline(infile, line);
 | 
|---|
 | 114 |                 tokenizer tokens(line, sep);
 | 
|---|
 | 115 |                 Adjacency::indices_t indices;
 | 
|---|
 | 116 |                 const int entries = std::distance( tokens.begin(), tokens.end() );
 | 
|---|
 | 117 |                 // check for empty lines
 | 
|---|
 | 118 |                 if (entries == 0)
 | 
|---|
 | 119 |                         continue;
 | 
|---|
 | 120 |                 std::transform(
 | 
|---|
 | 121 |                                 tokens.begin(),
 | 
|---|
 | 122 |                                 tokens.end(),
 | 
|---|
 | 123 |                                 std::back_inserter(indices),
 | 
|---|
 | 124 |                                 toIndex);
 | 
|---|
 | 125 |                 adjacencies.insert(Adjacency(indices));
 | 
|---|
 | 126 |         }
 | 
|---|
 | 127 |         infile.close();
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 |         return adjacencies;
 | 
|---|
 | 130 | }
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 | int main(int argc, char **argv) {
 | 
|---|
 | 133 | 
 | 
|---|
 | 134 |         /// check arguments and give usage
 | 
|---|
 | 135 |         if (argc <= 2) {
 | 
|---|
 | 136 |                 std::cerr << "Usage: " << argv[0] << " <list of files>\n";
 | 
|---|
 | 137 |                 std::cerr << "At least two adjacencies files need to be specified that contain\n";
 | 
|---|
 | 138 |                 std::cerr << "each three indices per line.\n";
 | 
|---|
 | 139 | 
 | 
|---|
 | 140 |                 return 255;
 | 
|---|
 | 141 |         }
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 |         /// read in files and parse into structures
 | 
|---|
 | 144 |         typedef std::multimap<adjacencies_t, std::string> adjacencies_filename_t;
 | 
|---|
 | 145 |         //!> contains all adjacencies parsed from files
 | 
|---|
 | 146 |         adjacencies_filename_t adjacencyfiles;
 | 
|---|
 | 147 |         for (int i=1; i<argc;++i) {
 | 
|---|
 | 148 |                 const bf::path filename(argv[i]);
 | 
|---|
 | 149 |                 if (bf::exists(filename)) {
 | 
|---|
 | 150 |                         try {
 | 
|---|
 | 151 |                                 adjacencies_t adjacencies = parseFile(filename);
 | 
|---|
 | 152 |                                 adjacencyfiles.insert(
 | 
|---|
 | 153 |                                                 std::make_pair(adjacencies, filename.string()) );
 | 
|---|
 | 154 |                         } catch (IllegalTesselationFileContentException &e) {
 | 
|---|
 | 155 |                                 std::cerr << "Illegal line at "
 | 
|---|
 | 156 |                                                 << *boost::get_error_info<IllegalTesselationFileContent_LineNumber>(e)
 | 
|---|
 | 157 |                                                 << " encountered with "
 | 
|---|
 | 158 |                                                 << *boost::get_error_info<IllegalTesselationFileContent_Entries>(e)
 | 
|---|
 | 159 |                                                 << " entries in "
 | 
|---|
 | 160 |                                                 << filename.string()
 | 
|---|
 | 161 |                                                 << ", aborting.\n";
 | 
|---|
 | 162 |                                 return 5;
 | 
|---|
 | 163 |                         }
 | 
|---|
 | 164 |                 } else {
 | 
|---|
 | 165 |                         std::cerr << "The following file does not exist "
 | 
|---|
 | 166 |                                         << filename.string() << ", aborting.\n";
 | 
|---|
 | 167 |                         return 5;
 | 
|---|
 | 168 |                 }
 | 
|---|
 | 169 |         }
 | 
|---|
 | 170 | 
 | 
|---|
 | 171 |         /// make comparison for equivalence
 | 
|---|
 | 172 |         assert( adjacencyfiles.size() >= 2 );
 | 
|---|
 | 173 |         adjacencies_filename_t::const_iterator advancer = adjacencyfiles.begin();
 | 
|---|
 | 174 |         adjacencies_filename_t::const_iterator iter = advancer++;
 | 
|---|
 | 175 | 
 | 
|---|
 | 176 |         bool AllEqual = true;
 | 
|---|
 | 177 |         while( advancer != adjacencyfiles.end() ) {
 | 
|---|
 | 178 | //        std::cout << "Comparing " << *advancer << " with " << *iter << std::endl;
 | 
|---|
 | 179 |                 if (advancer->first == iter->first) {
 | 
|---|
 | 180 |                         std::cout << "MATCH: "
 | 
|---|
 | 181 |                                         << advancer->second << " and " << iter->second
 | 
|---|
 | 182 |                                         << std::endl;
 | 
|---|
 | 183 |                 } else {
 | 
|---|
 | 184 |                         std::cout << advancer->first << " and " << iter->first
 | 
|---|
 | 185 |                                         << " don't match.\n";
 | 
|---|
 | 186 |                         AllEqual = false;
 | 
|---|
 | 187 |                 }
 | 
|---|
 | 188 |                 iter = advancer++;
 | 
|---|
 | 189 |         }
 | 
|---|
 | 190 | 
 | 
|---|
 | 191 |         // exit (convert bool to int)
 | 
|---|
 | 192 |         return (AllEqual ? 0 : 1);
 | 
|---|
 | 193 | }
 | 
|---|