00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
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         
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   
00098   
00099   
00100   
00114   template <Reading_mode mode,class Occupancy_handler>
00115   bool read(const std::string& filename,Occupancy_handler occupancy,char default_altloc=' '){
00116     
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 } 
00154 
00155 #endif // ESBTL_LINE_READER_H