+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
+ }
+}