Skip to content

Commit

Permalink
Add freopen() wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Jun 3, 2024
1 parent 4a2e2bb commit c82fd98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 14 additions & 1 deletion base/file_handle.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF Base Library
// Copyright (c) 2020-2021 Igara Studio S.A.
// Copyright (c) 2020-2024 Igara Studio S.A.
// Copyright (c) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -55,6 +55,19 @@ FILE* open_file_raw(const string& filename, const string& mode)
#endif
}

FILE* reopen_file_raw(const string& filename, const string& mode, FILE* stream)
{
#if LAF_WINDOWS
return _wfreopen((!filename.empty() ? from_utf8(filename).c_str(): nullptr),
from_utf8(mode).c_str(),
stream);
#else
return freopen((!filename.empty() ? filename.c_str(): nullptr),
mode.c_str(),
stream);
#endif
}

FileHandle open_file(const string& filename, const string& mode)
{
return FileHandle(open_file_raw(filename, mode), fclose_if_valid);
Expand Down
3 changes: 2 additions & 1 deletion base/file_handle.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF Base Library
// Copyright (c) 2020 Igara Studio S.A.
// Copyright (c) 2020-2024 Igara Studio S.A.
// Copyright (c) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
Expand All @@ -18,6 +18,7 @@ namespace base {
using FileHandle = std::shared_ptr<FILE>;

FILE* open_file_raw(const std::string& filename, const std::string& mode);
FILE* reopen_file_raw(const std::string& filename, const std::string& mode, FILE* stream);
FileHandle open_file(const std::string& filename, const std::string& mode);
FileHandle open_file_with_exception(const std::string& filename, const std::string& mode);
FileHandle open_file_with_exception_sync_on_close(const std::string& filename, const std::string& mode);
Expand Down

0 comments on commit c82fd98

Please sign in to comment.