Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize fiopen #2095

Merged
merged 4 commits into from
Mar 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions stl/src/fiopen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ FILE* _Xfsopen(_In_z_ const wchar_t* filename, _In_ int mode, _In_ int prot) {

template <class CharT>
FILE* _Xfiopen(const CharT* filename, ios_base::openmode mode, int prot) {
static const int valid[] = {
static const ios_base::openmode valid[] = {
// valid combinations of open flags
ios_base::in,
ios_base::out,
Expand All @@ -41,7 +41,6 @@ FILE* _Xfiopen(const CharT* filename, ios_base::openmode mode, int prot) {
ios_base::in | ios_base::out | ios_base::binary,
ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary,
ios_base::in | ios_base::out | ios_base::app | ios_base::binary,
0,
};

FILE* fp = nullptr;
Expand All @@ -58,13 +57,12 @@ FILE* _Xfiopen(const CharT* filename, ios_base::openmode mode, int prot) {

mode &= ~(ios_base::ate | ios_base::_Nocreate | ios_base::_Noreplace);

// look for a valid mode
int n = 0;
while (valid[n] != 0 && valid[n] != mode) { // look for a valid mode
++n;
}

if (valid[n] == 0) {
return nullptr; // no valid mode
while (valid[n] != mode) {
if (++n == static_cast<int>(_STD size(valid))) {
return nullptr; // no valid mode
}
AreaZR marked this conversation as resolved.
Show resolved Hide resolved
}

if (norepflag && (mode & (ios_base::out | ios_base::app))
Expand All @@ -81,12 +79,12 @@ FILE* _Xfiopen(const CharT* filename, ios_base::openmode mode, int prot) {
return nullptr; // open failed
AreaZR marked this conversation as resolved.
Show resolved Hide resolved
}

if (!atendflag || fseek(fp, 0, SEEK_END) == 0) {
return fp; // no need to seek to end, or seek succeeded
if (atendflag && fseek(fp, 0, SEEK_END) != 0) {
fclose(fp); // can't position at end
return nullptr;
}

fclose(fp); // can't position at end
return nullptr;
return fp; // no need to seek to end, or seek succeeded
}

_CRTIMP2_PURE FILE* __CLRCALL_PURE_OR_CDECL _Fiopen(
Expand Down