From: Don Armstrong Date: Fri, 11 Apr 2014 23:22:48 +0000 (-0700) Subject: use store_filepos and reliable_tellg X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=6f8372858a1c1f6d1fa38973a076c8a20b32900b;p=mothur.git use store_filepos and reliable_tellg --- diff --git a/deconvolutecommand.cpp b/deconvolutecommand.cpp index bbfa8b6..e98d8c0 100644 --- a/deconvolutecommand.cpp +++ b/deconvolutecommand.cpp @@ -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>::iterator in_fasta_from_hash_map(multimap>& seq_strings, Sequence& seq, ifstream& fasta_file) { - streampos cur_pos = fasta_file.tellg(); const std::hash 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>::iterator if (it_range.first == seq_strings.end()) { return seq_strings.end(); } + pair 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>::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 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& 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(','); diff --git a/deconvolutecommand.h b/deconvolutecommand.h index 7f3628d..62837d0 100644 --- a/deconvolutecommand.h +++ b/deconvolutecommand.h @@ -51,6 +51,10 @@ multimap>::iterator Sequence&, ifstream&); +pair store_filepos(ifstream&); +void restore_filepos(ifstream&, pair&); +streampos reliable_tellg(ifstream&); + string primary_seqname(string); #endif