nds2-client - ClientDeveloper  0.16.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nds_os.h
Go to the documentation of this file.
1 /* -*- tab-width:8 c-basic-offset:4 indent-tabs-mode:nil -*- */
2 /* set vi: ts=8:softtabstop=4,shiftwidth=4,expandtab */
3 
4 #ifndef NDS_OS_H
5 #define NDS_OS_H
6 
7 #include <stdio.h>
8 
9 #include "daqc.h"
10 #include "daqc_private.h"
11 #ifdef __cplusplus
12 #include <stdint.h>
13 #include <limits>
14 #include <stdexcept>
15 #ifndef INT32_MAX
16 #define INT32_MAX ( std::numeric_limits< int32_t >::max( ) )
17 #endif /* INT32_MAX */
18 #endif /* __cplusplus */
19 
20 /*
21  * If you desire not to use the Windows _s functions, then add the following
22  * lines to the source file:
23  *
24  * #if _WIN32
25  * #define _CRT_SECURE_NO_WARNINGS 1
26  * #endif
27  *
28  */
30 void socket_io_default( nds_socket_type Socket );
31 
32 void pstrerror_r( char* StrErrBuf, size_t StrErrBufLen );
33 
34 #ifdef __cplusplus
35 template < typename DEST, typename SOURCE >
36 inline DEST
37 check_int_overflow( SOURCE Source )
38 {
39  if ( sizeof( DEST ) < sizeof( SOURCE ) )
40  {
41  switch ( sizeof( DEST ) )
42  {
43  case 4:
44  if ( Source > INT32_MAX )
45  {
46  throw std::overflow_error(
47  "Rounding error resulting from integer truncation" );
48  }
49  }
50  }
51  return static_cast< DEST >( Source );
52 }
53 #endif /* __cplusplus */
54 
55 #if WIN32 || WIN64
56 /* ----------------------------------------------------------------------
57  * Windows System
58  * ----------------------------------------------------------------------
59  */
60 
61 #define DLL_EXPORT __declspec( dllexport )
62 
63 #include <io.h>
64 
65 typedef size_t ssize_t;
66 typedef int socklen_t;
67 
68 #if __cplusplus
69 
70 /* modes for access are not defined by io.h */
71 #define F_OK ( (int)0 )
72 #define W_OK ( (int)2 )
73 
74 static inline int
75 close( nds_socket_type s )
76 {
77  return closesocket( s );
78 }
79 static inline int
80 dup( int fd )
81 {
82  return _dup( fd );
83 }
84 static inline FILE*
85 fdopen( int fd, const char* mode )
86 {
87  return _fdopen( fd, mode );
88 }
89 static inline ssize_t
90 read( nds_socket_type s, void* buf, size_t len )
91 {
92  return recv(
93  s, reinterpret_cast< char* >( buf ), static_cast< int >( len ), 0 );
94 }
95 static inline void
96 sleep( int seconds )
97 {
98  Sleep( seconds * 1000 );
99 }
100 static inline void
101 usleep( int microseconds )
102 {
103  Sleep( microseconds / 1000 );
104 }
105 static inline ssize_t
106 write( nds_socket_type s, const void* buf, size_t count )
107 {
108  return send( s,
109  reinterpret_cast< const char* >( buf ),
110  static_cast< int >( count ),
111  0 );
112 }
113 
114 static inline errno_t
115 strerror_r( int errnum, char* buf, size_t buflen )
116 {
117  return strerror_s( buf, buflen, errnum );
118 }
119 
120 #define perror( message ) nds_perror( message )
121 
122 #else /* __cplusplus */
123 
124 #define close( fd ) closesocket( fd )
125 #define dup( oldfd ) _dup( oldfd )
126 #define fdopen( path, mode ) _fdopen( path, mode )
127 #define read( fd, buf, len ) recv( fd, buf, len, 0 )
128 #define sleep( Seconds ) Sleep( Seconds * 1000 )
129 #define usleep( MicroSeconds ) Sleep( MicroSeconds / 1000 )
130 #define write( fd, buf, count ) send( fd, buf, count, 0 )
131 #define perror( message ) nds_perror( message )
132 #define unlink( filename ) _unlink( filename )
133 
134 #define strerror_r( a, b, c ) strerror_s( b, c, a )
135 #endif /* __cplusplus */
136 
137 #define access( path, mode ) _access( path, mode )
138 #define strdup( str ) _strdup( str )
139 
140 #else /* _WIN32 || _WIN64 */
141 /* ----------------------------------------------------------------------
142  * Non-Windows System
143  * ----------------------------------------------------------------------
144  */
145 #define DLL_EXPORT
146 #define strncpy_s( dest, dest_len, src, count ) strncpy( dest, src, count )
147 
148 #define strcpy_s( dest, dest_len, src ) strcpy( dest, src )
149 #endif /* _WIN32 || _WIN64 */
150 
151 #endif /* NDS_OS_H */
errnum
Definition: nds_errno.hh:7
void pstrerror_r(char *StrErrBuf, size_t StrErrBufLen)
Definition: daqc_internal.c:712
void closesocket(socket_type s)
Definition: socket.hh:95
::socklen_t socklen_t
Definition: socket.hh:81
void socket_io_non_blocking(nds_socket_type Socket)
Definition: nds_os.c:15
int nds_socket_type
Definition: daqc_private.h:12
void socket_io_default(nds_socket_type Socket)
Definition: nds_os.c:24