/*
 * $Id: binary.h,v 1.7 2005/08/26 06:40:32 jason Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <math.h>

#ifdef __CYGWIN__
#include <io.h>
#endif

#if   defined _WIN32  ||  defined __TURBOC__  ||  defined __ZTC__  ||  defined _MSC_VER
# include <io.h>
# include <fcntl.h>
# include <time.h>
# include <sys/types.h>
# include <sys/stat.h>
#elif defined __unix__  ||  defined __linux__
# include <fcntl.h>
# include <unistd.h>
# include <sys/time.h>
# include <sys/ioctl.h>
# include <sys/types.h>
# include <sys/stat.h>
#else
/* .... hier Includes für neues Betriebssystem einfügen (mit vorgestellten: #elif defined) */
# include <fcntl.h>
# include <unistd.h>
# include <sys/ioctl.h>
# include <sys/stat.h>
#endif

#ifndef O_BINARY
# ifdef _O_BINARY
#  define O_BINARY              _O_BINARY
# else
#  define O_BINARY              0
# endif
#endif

#if   defined __BORLANDC__  ||  defined _WIN32
# define FILENO(__fp)          _fileno ((__fp))
#elif defined __CYGWIN__  ||  defined __TURBOC__  ||  defined __unix__  ||  defined __EMX__  ||  defined _MSC_VER
# define FILENO(__fp)          fileno  ((__fp))
#else
# define FILENO(__fp)          fileno  ((__fp))
#endif

/*
 * If we have access to a file via file name, we can open the file with an
 * additional "b" or a O_BINARY within the (f)open function to get a
 * transparent untranslated data stream which is necessary for audio bitstream
 * data and also for PCM data. If we are working with
 * stdin/stdout/FILENO_STDIN/FILENO_STDOUT we can't open the file with this
 * attributes, because the files are already open. So we need a non
 * standardized sequence to switch to this mode (not necessary for Unix).
 * Mostly the sequency is the same for incoming and outgoing streams, but only
 * mostly so we need one for IN and one for OUT.
 * Macros are called with the file pointer and you get back the untransalted file
 * pointer which can be equal or different from the original.
*/

#if   defined __EMX__
# define SETBINARY_FD(__fp)     (_fsetmode ( (__fp), "b" ))
# define SETBINARY_IN(__fp)     (_fsetmode ( (__fp), "b" ))
# define SETBINARY_OUT(__fp)    (_fsetmode ( (__fp), "b" ))
#elif defined __TURBOC__ || defined __BORLANDC__
# define SETBINARY_FD(__fp)     (setmode   ( (__fp),  O_BINARY ))
# define SETBINARY_IN(__fp)     (setmode   ( FILENO ((__fp)),  O_BINARY ))
# define SETBINARY_OUT(__fp)    (setmode   ( FILENO ((__fp)),  O_BINARY ))
#elif defined __CYGWIN__
# define SETBINARY_FD(__fp)     (setmode   ( (__fp), _O_BINARY ))
# define SETBINARY_IN(__fp)     (setmode   ( FILENO ((__fp)), _O_BINARY ))
# define SETBINARY_OUT(__fp)    (setmode   ( FILENO ((__fp)), _O_BINARY ))
#elif defined _WIN32
# define SETBINARY_FD(__fp)     (_setmode  ( (__fp), _O_BINARY ))
# define SETBINARY_IN(__fp)     (_setmode  ( FILENO ((__fp)), _O_BINARY ))
# define SETBINARY_OUT(__fp)    (_setmode  ( FILENO ((__fp)), _O_BINARY ))
#elif defined _MSC_VER
# define SETBINARY_FD(__fp)     (setmode   ( (__fp),  O_BINARY ))
# define SETBINARY_IN(__fp)     (setmode   ( FILENO ((__fp)),  O_BINARY ))
# define SETBINARY_OUT(__fp)    (setmode   ( FILENO ((__fp)),  O_BINARY ))
#elif defined __unix__
# define SETBINARY_FD(__fp)     (void)(__fp)
# define SETBINARY_IN(__fp)     (void)(__fp)
# define SETBINARY_OUT(__fp)    (void)(__fp)
#elif 0
# define SETBINARY_FD(__fp)     (void)(__fp)
# define SETBINARY_IN(__fp)     (freopen   ( NULL, "rb", (__fp) ))
# define SETBINARY_OUT(__fp)    (freopen   ( NULL, "wb", (__fp) ))
#else
# define SETBINARY_FD(__fp)     (void)(__fp)
# define SETBINARY_IN(__fp)     (void)(__fp)
# define SETBINARY_OUT(__fp)    (void)(__fp)
#endif
