Skip to content

Commit 9b9f6c5

Browse files
committed
Factorize repeated fseek/ftell calls
1 parent 2739c3c commit 9b9f6c5

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

libcxx/include/fstream

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ private:
362362
bool __read_mode();
363363
void __write_mode();
364364

365+
static int __fseek(FILE* __file, pos_type __offset, int __whence);
366+
static pos_type __ftell(FILE* __file);
367+
365368
_LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&);
366369

367370
// There are multiple (__)open function, they use different C-API open
@@ -936,38 +939,44 @@ basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
936939
default:
937940
return pos_type(off_type(-1));
938941
}
939-
# if defined(_LIBCPP_MSVCRT_LIKE)
940-
if (_fseeki64(__file_, __width > 0 ? __width * __off : 0, __whence))
942+
if (__fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
941943
return pos_type(off_type(-1));
942-
pos_type __r = _ftelli64(__file_);
944+
pos_type __r = __ftell(__file_);
945+
__r.state(__st_);
946+
return __r;
947+
}
948+
949+
template <class _CharT, class _Traits>
950+
int
951+
basic_filebuf<_CharT, _Traits>::__fseek(FILE* __file, pos_type __offset, int __whence) {
952+
# if defined(_LIBCPP_MSVCRT_LIKE)
953+
return _fseeki64(__file, __offset, __whence);
943954
# elif !_LIBCPP_HAS_OFF_T_FUNCTIONS
944-
if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
945-
return pos_type(off_type(-1));
946-
pos_type __r = ftell(__file_);
955+
return fseek(__file, __offset, __whence);
947956
# else
948-
if (::fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
949-
return pos_type(off_type(-1));
950-
pos_type __r = ftello(__file_);
957+
return ::fseeko(__file, __offset, __whence);
951958
# endif
952-
__r.state(__st_);
953-
return __r;
954959
}
955960

956961
template <class _CharT, class _Traits>
957962
typename basic_filebuf<_CharT, _Traits>::pos_type
958-
basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) {
959-
if (__file_ == nullptr || sync())
960-
return pos_type(off_type(-1));
963+
basic_filebuf<_CharT, _Traits>::__ftell(FILE* __file) {
961964
# if defined(_LIBCPP_MSVCRT_LIKE)
962-
if (_fseeki64(__file_, __sp, SEEK_SET))
963-
return pos_type(off_type(-1));
965+
return _ftelli64(__file);
964966
# elif !_LIBCPP_HAS_OFF_T_FUNCTIONS
965-
if (fseek(__file_, __sp, SEEK_SET))
966-
return pos_type(off_type(-1));
967+
return ftell(__file);
967968
# else
968-
if (::fseeko(__file_, __sp, SEEK_SET))
969-
return pos_type(off_type(-1));
969+
return ftello(__file);
970970
# endif
971+
}
972+
973+
template <class _CharT, class _Traits>
974+
typename basic_filebuf<_CharT, _Traits>::pos_type
975+
basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) {
976+
if (__file_ == nullptr || sync())
977+
return pos_type(off_type(-1));
978+
if (__fseek(__file_, __sp, SEEK_SET))
979+
return pos_type(off_type(-1));
971980
__st_ = __sp.state();
972981
return __sp;
973982
}
@@ -1014,13 +1023,8 @@ int basic_filebuf<_CharT, _Traits>::sync() {
10141023
}
10151024
}
10161025
}
1017-
# if !_LIBCPP_HAS_OFF_T_FUNCTIONS
1018-
if (fseek(__file_, -__c, SEEK_CUR))
1026+
if (__fseek(__file_, -__c, SEEK_CUR))
10191027
return -1;
1020-
# else
1021-
if (::fseeko(__file_, -__c, SEEK_CUR))
1022-
return -1;
1023-
# endif
10241028
if (__update_st)
10251029
__st_ = __state;
10261030
__extbufnext_ = __extbufend_ = __extbuf_;

0 commit comments

Comments
 (0)