From c82fd98b275cb25122106797af00feabc5015c7f Mon Sep 17 00:00:00 2001 From: David Capello Date: Mon, 3 Jun 2024 11:28:22 -0300 Subject: [PATCH] Add freopen() wrapper --- base/file_handle.cpp | 15 ++++++++++++++- base/file_handle.h | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/base/file_handle.cpp b/base/file_handle.cpp index 363f03807..70ef13032 100644 --- a/base/file_handle.cpp +++ b/base/file_handle.cpp @@ -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. @@ -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); diff --git a/base/file_handle.h b/base/file_handle.h index 31db0a1e9..ef9f3e73f 100644 --- a/base/file_handle.h +++ b/base/file_handle.h @@ -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. @@ -18,6 +18,7 @@ namespace base { using FileHandle = std::shared_ptr; 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);