#ifndef __utils_h #define __utils_h #include #include typedef std::basic_stringstream stringstream; #if defined(__BORLANDC__) || defined(__WIN32) #include #include #else #include #endif #include #include #include #if defined(__cplusplus) || defined(c_plusplus) #if defined(__BORLANDC__) || defined(__WIN32) inline void usleep( int s ){ Sleep( s/1000 ); return; } #else inline void usleep( int s ){ timeval tout; tout.tv_sec = s/1000000; tout.tv_usec = s%1000000; select( 0,0,0,0,&tout ); return; } #endif #if defined(__i386__) inline unsigned uswap( unsigned u ){ return u; } inline unsigned short usswap( unsigned short us ){ return us; } #else inline unsigned uswap( unsigned u ) { unsigned char c[4]; c[0] = u & 0xff; c[1] = (u >> 8) & 0xff; c[2] = (u >> 16) & 0xff; c[3] = (u >> 24) & 0xff; return *(unsigned *)c; } inline unsigned short usswap( unsigned short us ) { unsigned char c[2]; c[0] = us & 0xff; c[1] = (us >> 8) & 0xff; return *(unsigned short *)c; } #endif // =========================================================================== // // tracestream allows to accumulate messages into a buffer for later output // Applications can test if there are any new messages using tellp() to return // the current stream pointer position. class tracestream: public stringstream { public: enum { maxBufferLength = 512 }; tracestream():stringstream(){ str(""); } tracestream( const tracestream &tr ):stringstream(){ str(""); } operator void*(){ std::cerr << tellp() << std::endl; return ((tellp() > 0) ? 0 : this); } bool operator !(){ return ((tellp() <= 0) ? false : true); } #if defined(__BORLANDC__) || defined(__CYGWIN__) || defined(__WIN32) tracestream &scan( const char *, ... ); tracestream &form( const char *, ... ); #endif }; // tracestream output operator terminates buffer outputs contents and // resets pointer to start of buffer. Do nothing if no errors. inline std::ostream &operator<<(std::ostream &out, tracestream &tr) { if ( tr.tellp() > 0 ) { out << tr.str(); tr.str(""); } return out; } inline std::ostream &operator<<(tracestream &out, tracestream &tr) { if ( tr.tellp() > 0 ) { out << tr.str(); tr.str(""); } return out; } // =========================================================================== #if defined(__BORLANDC__) || defined(__CYGWIN__) || defined(__WIN32) inline tracestream &tracestream::scan( const char *fmt, ... ) { va_list ap; char buf[maxBufferLength+1]; getline( buf, maxBufferLength ); va_start( ap,fmt ); vsscanf( buf, fmt, ap ); va_end( ap ); return *this; } inline tracestream &tracestream::form( const char *fmt, ... ) { va_list ap; char buf[maxBufferLength+1]; va_start( ap,fmt ); int rc = vsprintf( buf, fmt, ap ); va_end( ap ); if ( rc != EOF ) write( buf, rc ); return *this; } #endif #else void usleep( int s ); #endif int systemf( const char *, ... ); int mkfifo( const char *, unsigned ); #endif