line_reader.h
Go to the documentation of this file.
00001 // Copyright (c) 2009-2010  INRIA Sophia-Antipolis (France).
00002 // All rights reserved.
00003 //
00004 //This file is part of ESBTL.
00005 //
00006 //ESBTL is free software: you can redistribute it and/or modify
00007 //it under the terms of the GNU General Public License as published by
00008 //the Free Software Foundation, either version 3 of the License, or
00009 //(at your option) any later version.
00010 //
00011 //ESBTL is distributed in the hope that it will be useful,
00012 //but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //GNU General Public License for more details.
00015 //
00016 //You should have received a copy of the GNU General Public License
00017 //along with ESBTL.  If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 //
00020 //Additional permission under GNU GPL version 3 section 7
00021 //
00022 //If you modify this Library, or any covered work, by linking or
00023 //combining it with CGAL (or a modified version of that library), the
00024 //licensors of this Library grant you additional permission to convey
00025 //the resulting work. Corresponding Source for a non-source form of
00026 //such a combination shall include the source code for the parts of CGAL
00027 //used as well as that of the covered work. 
00028 //
00029 //
00030 //
00031 // Author(s)     :  Sébastien Loriot
00032 
00033 
00034 
00035 #ifndef ESBTL_LINE_READER_H
00036 #define ESBTL_LINE_READER_H
00037 
00038 #include <ESBTL/internal/compressed_ifstream.h>
00039 
00040 namespace ESBTL{
00041   
00049 template <class Line_format,class Line_selector,class Builder>
00050 class Line_reader{
00051   Line_selector& line_selector;
00052   Builder& builder;
00053 
00054   template <class Occupancy_handler,class Ifstream>
00055   bool read_stream(Ifstream& input,Occupancy_handler occupancy,char default_altloc=char(' ')){
00056     int nblines=0;
00057 
00058     while ( ! input.eof() ){
00059       std::string line;
00060       getline(input,line);
00061       if (line.empty()) continue;
00062       
00063       
00064       Line_format line_format(line);
00065       
00066       int system_index=line_selector.keep(line_format,line,occupancy);
00067       
00068       if (system_index!=DISCARD){
00069         //TODO warning Put inside selector: we could think of a different policy
00070         char altloc=line_format.get_alternate_location(line);
00071         if (altloc!=' '){
00072           if (default_altloc==' ')
00073             default_altloc=altloc;
00074           else
00075             if (altloc!=default_altloc) continue;
00076         }
00077         
00078         builder.interpret_line(line_format,line,system_index);
00079         ++nblines;        
00080       }
00081     }
00082     
00083     nblines+=occupancy.finalize(builder);
00084     builder.create_systems(default_altloc);
00085     
00086     #ifndef NDEBUG
00087     std::cout << "(ESBTL-DEBUG) Lines read " << nblines << std::endl;    
00088     #endif
00089     return true;
00090   }
00091   
00092 public:
00094   Line_reader(Line_selector& line_selector, Builder& builder):line_selector(line_selector),builder(builder){}
00095   
00096   
00097   //template parameter Occupancy_handler tells what to do with atoms with occupancy !=1 (when no altloc present)
00098   //TODO : think of the same think for altloc: when should have that the sum of the occupancy is 1 when considering these atoms!!!!!!
00099   //       This would be also a way to handle differently altloc and to avoid the update at the end to be sure all altloc selected are the same 
00100   //occupancy is not const for the moment since it may be used as internal line storage
00114   template <Reading_mode mode,class Occupancy_handler>
00115   bool read(const std::string& filename,Occupancy_handler occupancy,char default_altloc=' '){
00116     //binary flags prevent from interpretation of some symbols \r\n for example
00117     std::ifstream input( filename.c_str(),std::ios_base::binary );
00118 
00119     if (! input){
00120       std::cerr << "Problem while trying to open file " << filename << ".\nPlease check that the file exists and that you have the right to read it."  << std::endl;
00121       return false;   
00122     }
00123     
00124     internal::Ifstream_compress<mode> stream;
00125     return read_stream(stream.get(input),occupancy,default_altloc);
00126   }
00127 
00128   template <class Occupancy_handler>
00129   bool read(const std::string& filename,Occupancy_handler occupancy,char default_altloc=' '){
00130     return read<ASCII>(filename,occupancy,default_altloc);
00131   }
00132   
00133   
00134 };
00135 
00139 template<Reading_mode mode,class Line_selector,class Builder,class Occupancy_handler>
00140 inline
00141 bool read_a_pdb_file(const std::string& filename,Line_selector& sel,Builder& builder,const Occupancy_handler& occupancy,char altloc=' '){
00142   return Line_reader<PDB::Line_format<>,Line_selector,Builder>(sel,builder).template read<mode>(filename,occupancy,altloc);
00143 }
00144 
00145 template<class Line_selector,class Builder,class Occupancy_handler>
00146 inline
00147 bool read_a_pdb_file(const std::string& filename,Line_selector& sel,Builder& builder,const Occupancy_handler& occupancy,char altloc=' '){
00148   return read_a_pdb_file<ASCII>(filename,sel,builder,occupancy,altloc);
00149 }
00150 
00151 
00152 
00153 } //namespace ESBTL
00154 
00155 #endif // ESBTL_LINE_READER_H