]> git.donarmstrong.com Git - mothur.git/commitdiff
use store_filepos and reliable_tellg
authorDon Armstrong <don@donarmstrong.com>
Fri, 11 Apr 2014 23:22:48 +0000 (16:22 -0700)
committerDon Armstrong <don@donarmstrong.com>
Fri, 11 Apr 2014 23:22:48 +0000 (16:22 -0700)
deconvolutecommand.cpp
deconvolutecommand.h

index bbfa8b6e3eaccc76f350a3b4433cff3500c561b4..e98d8c0e2d2505eb0a27e4af098f3fe428c14eab 100644 (file)
@@ -238,7 +238,7 @@ int DeconvoluteCommand::execute() {
                while (!in.eof()) {
                        
                        if (m->control_pressed) { in.close(); outFasta.close(); m->mothurRemove(outFastaFile); return 0; }
-                       streampos seq_start = in.tellg();
+                       streampos seq_start = reliable_tellg(in);
                        Sequence seq(in);
                        
                        if (seq.getName() != "") {
@@ -357,7 +357,6 @@ multimap<size_t,std::pair<std::streampos,string>>::iterator
   in_fasta_from_hash_map(multimap<size_t,std::pair<std::streampos,string>>& seq_strings,
                             Sequence& seq,
                             ifstream& fasta_file) {
-  streampos cur_pos = fasta_file.tellg();
   const std::hash<std::string> hash_fn;
   auto it_range = seq_strings.equal_range(hash_fn(seq.getAligned()));
   // If the hash of the aligned sequence is not in the map, it can't
@@ -365,6 +364,7 @@ multimap<size_t,std::pair<std::streampos,string>>::iterator
   if (it_range.first == seq_strings.end()) {
     return seq_strings.end();
   }
+  pair<bool,streampos> file_pos = store_filepos(fasta_file);
   // If it is, it might be a hash collision. Read the file to
   // determine if that's the case
   for (auto it = it_range.first; it != it_range.second; it++) {
@@ -373,14 +373,42 @@ multimap<size_t,std::pair<std::streampos,string>>::iterator
     // read in sequence
     Sequence old_seq(fasta_file);
     if (old_seq.getAligned() == seq.getAligned()) {
-      fasta_file.seekg(cur_pos);
+      restore_filepos(fasta_file,file_pos);
       return it;
     }
   }
-  fasta_file.seekg(cur_pos);
+  restore_filepos(fasta_file,file_pos);
   return seq_strings.end();
 }
 
+streampos reliable_tellg(ifstream& file) {
+  bool was_eof = file.eof();
+  if (was_eof) { // we're at EOF: clear the bit so tellg works properly
+    file.clear(file.rdstate() & ~ios::eofbit);
+  }
+  streampos pos = file.tellg();
+  if (was_eof) { // we're at EOF: reset eofbit
+    file.clear(file.rdstate() | ios::eofbit);
+  }
+  return pos;
+}
+
+
+pair<bool,streampos> store_filepos(ifstream& file) {
+  bool was_eof = file.eof();
+  if (was_eof) { // we're at EOF: clear the bit so tellg works properly
+    file.clear(file.rdstate() & ~ios::eofbit);
+  }
+  streampos cur_pos = file.tellg();
+  return make_pair(was_eof,cur_pos);
+}
+
+void restore_filepos(ifstream& file, pair<bool,streampos>& pos_info) {
+  file.seekg(pos_info.second);
+  if (pos_info.first) { // at eof?
+    file.clear(file.rdstate() | ios::eofbit); // restore the eof bit if it was set
+  }
+}
 
 string primary_seqname(string name) {
   int pos = name.find_first_of(',');
index 7f3628dfd14e662e30ea6d97e3de7ae1769dfcc0..62837d0e66e9f27259c902351fa87464629dae40 100644 (file)
@@ -51,6 +51,10 @@ multimap<size_t,std::pair<std::streampos,string>>::iterator
                             Sequence&,
                             ifstream&);
 
+pair<bool,streampos> store_filepos(ifstream&);
+void restore_filepos(ifstream&, pair<bool,streampos>&);
+streampos reliable_tellg(ifstream&);
+
 string primary_seqname(string);
 
 #endif