From: <sv...@op...> - 2024-10-31 14:20:15
|
Author: manx Date: Thu Oct 31 15:20:03 2024 New Revision: 22039 URL: https://source.openmpt.org/browse/openmpt/?op=revision&rev=22039 Log: [Imp] openmpt123: Use setmode() on DOS. Modified: trunk/OpenMPT/openmpt123/openmpt123_stdio.hpp Modified: trunk/OpenMPT/openmpt123/openmpt123_stdio.hpp ============================================================================== --- trunk/OpenMPT/openmpt123/openmpt123_stdio.hpp Thu Oct 31 15:05:43 2024 (r22038) +++ trunk/OpenMPT/openmpt123/openmpt123_stdio.hpp Thu Oct 31 15:20:03 2024 (r22039) @@ -20,7 +20,10 @@ #include <cstdio> -#if MPT_OS_WINDOWS +#if MPT_OS_DJGPP +#include <fcntl.h> +#include <io.h> +#elif MPT_OS_WINDOWS #include <fcntl.h> #include <io.h> #endif @@ -33,7 +36,47 @@ binary, }; -#if MPT_OS_WINDOWS +#if MPT_OS_DJGPP + +class FILE_mode_guard { +private: + FILE * file; + int old_mode; +public: + FILE_mode_guard( FILE * file, FILE_mode new_mode ) + : file(file) + , old_mode(-1) + { + switch (new_mode) { + case FILE_mode::text: + fflush( file ); + old_mode = setmode( fileno( file ), O_TEXT ); + if ( old_mode == -1 ) { + throw exception( MPT_USTRING("failed to set TEXT mode on file descriptor") ); + } + break; + case FILE_mode::binary: + fflush( file ); + old_mode = setmode( fileno( file ), O_BINARY ); + if ( old_mode == -1 ) { + throw exception( MPT_USTRING("failed to set binary mode on file descriptor") ); + } + break; + } + } + FILE_mode_guard( const FILE_mode_guard & ) = delete; + FILE_mode_guard( FILE_mode_guard && ) = default; + FILE_mode_guard & operator=( const FILE_mode_guard & ) = delete; + FILE_mode_guard & operator=( FILE_mode_guard && ) = default; + ~FILE_mode_guard() { + if ( old_mode != -1 ) { + fflush( file ); + old_mode = setmode( fileno( file ), old_mode ); + } + } +}; + +#elif MPT_OS_WINDOWS class FILE_mode_guard { private: |