00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "CeylanStream.h"
00028
00029
00030 #include "CeylanLogPlug.h"
00031 #include "CeylanOperators.h"
00032
00033
00034 #ifdef CEYLAN_USES_CONFIG_H
00035 #include "CeylanConfig.h"
00036 #endif // CEYLAN_USES_CONFIG_H
00037
00038
00039 #if CEYLAN_ARCH_NINTENDO_DS
00040 #include "CeylanConfigForNintendoDS.h"
00041 #endif // CEYLAN_ARCH_NINTENDO_DS
00042
00043
00044
00045
00046 extern "C"
00047 {
00048
00049
00050 #if CEYLAN_ARCH_NINTENDO_DS
00051
00052 #include "fat.h"
00053
00054 #include <fcntl.h>
00055 #include <unistd.h>
00056
00057 #endif // CEYLAN_ARCH_NINTENDO_DS
00058
00059
00060 #ifdef CEYLAN_USES_ERRNO_H
00061 #include <errno.h>
00062 #endif // CEYLAN_USES_ERRNO_H
00063
00064 #ifdef CEYLAN_USES_UNISTD_H
00065 #include <unistd.h>
00066 #endif // CEYLAN_USES_UNISTD_H
00067
00068 }
00069
00070
00071 #include <cerrno>
00072
00073
00074 using namespace Ceylan::Log ;
00075 using namespace Ceylan::System ;
00076
00077
00078
00079 Stream::Stream( bool blocking ) :
00080 _isBlocking( blocking )
00081 {
00082
00083 }
00084
00085
00086 Stream::~Stream() throw()
00087 {
00088
00089 }
00090
00091
00092
00093 bool Stream::isBlocking() const
00094 {
00095
00096 return _isBlocking ;
00097
00098 }
00099
00100
00101
00102 const std::string Stream::toString( Ceylan::VerbosityLevels level ) const
00103 {
00104
00105 if ( _isBlocking )
00106 return "Blocking Stream" ;
00107 else
00108 return "Non-blocking Stream" ;
00109
00110 }
00111
00112
00113
00114 bool Stream::Close( FileDescriptor & fd )
00115 {
00116
00117 #if CEYLAN_USES_FILE_DESCRIPTORS
00118
00119
00120
00121 if ( fd > 0 )
00122 {
00123
00124 int volatile ret ;
00125
00126 do
00127 {
00128 ret = ::close( fd ) ;
00129 }
00130 while ( ( ret == -1 ) && ( errno == EINTR ) ) ;
00131
00132 if ( ret == -1 )
00133 {
00134
00135 switch( errno )
00136 {
00137
00138 case EBADF:
00139 throw Stream::CloseException( "Ceylan::System::close: "
00140 "file descriptor " + Ceylan::toString( fd )
00141 + " is invalid." ) ;
00142
00143 case EIO:
00144 throw Stream::CloseException( "Ceylan::System::close: "
00145 "an I/O error occurred with file descriptor "
00146 + Ceylan::toString( fd ) + "." ) ;
00147
00148 default:
00149 throw Stream::CloseException( "Ceylan::System::close: "
00150 "unexpected error occurred with file descriptor "
00151 + Ceylan::toString( fd ) + "." ) ;
00152
00153 }
00154 }
00155
00156 fd = 0 ;
00157
00158 return true ;
00159
00160 }
00161
00162 return false ;
00163
00164 #else // CEYLAN_USES_FILE_DESCRIPTORS
00165
00166 throw Stream::CloseException( "Ceylan::System::close: "
00167 "file descriptor feature not available" ) ;
00168
00169 #endif // CEYLAN_USES_FILE_DESCRIPTORS
00170
00171 }
00172
00173
00174
00175 void Stream::setBlocking( bool newStatus )
00176 {
00177
00178
00179 if ( newStatus == false )
00180 throw NonBlockingNotSupportedException(
00181 "Stream::setBlocking: this stream cannot be put in "
00182 "non-blocking mode." ) ;
00183
00184 }
00185
00186