quickfix_ruby 1.14.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/ext/quickfix/Acceptor.cpp +248 -0
- data/ext/quickfix/Acceptor.h +127 -0
- data/ext/quickfix/Allocator.h +9 -0
- data/ext/quickfix/Application.h +127 -0
- data/ext/quickfix/AtomicCount.h +109 -0
- data/ext/quickfix/DOMDocument.h +74 -0
- data/ext/quickfix/DataDictionary.cpp +618 -0
- data/ext/quickfix/DataDictionary.h +539 -0
- data/ext/quickfix/DataDictionaryProvider.cpp +71 -0
- data/ext/quickfix/DataDictionaryProvider.h +70 -0
- data/ext/quickfix/DatabaseConnectionID.h +105 -0
- data/ext/quickfix/DatabaseConnectionPool.h +91 -0
- data/ext/quickfix/Dictionary.cpp +162 -0
- data/ext/quickfix/Dictionary.h +94 -0
- data/ext/quickfix/Event.h +95 -0
- data/ext/quickfix/Exceptions.h +299 -0
- data/ext/quickfix/Field.h +672 -0
- data/ext/quickfix/FieldConvertors.h +863 -0
- data/ext/quickfix/FieldMap.cpp +238 -0
- data/ext/quickfix/FieldMap.h +244 -0
- data/ext/quickfix/FieldNumbers.h +46 -0
- data/ext/quickfix/FieldTypes.cpp +64 -0
- data/ext/quickfix/FieldTypes.h +698 -0
- data/ext/quickfix/Fields.h +31 -0
- data/ext/quickfix/FileLog.cpp +200 -0
- data/ext/quickfix/FileLog.h +112 -0
- data/ext/quickfix/FileStore.cpp +332 -0
- data/ext/quickfix/FileStore.h +129 -0
- data/ext/quickfix/FixFieldNumbers.h +1537 -0
- data/ext/quickfix/FixFields.h +1538 -0
- data/ext/quickfix/FixValues.h +3281 -0
- data/ext/quickfix/FlexLexer.h +188 -0
- data/ext/quickfix/Group.cpp +64 -0
- data/ext/quickfix/Group.h +73 -0
- data/ext/quickfix/HtmlBuilder.h +186 -0
- data/ext/quickfix/HttpConnection.cpp +739 -0
- data/ext/quickfix/HttpConnection.h +78 -0
- data/ext/quickfix/HttpMessage.cpp +149 -0
- data/ext/quickfix/HttpMessage.h +133 -0
- data/ext/quickfix/HttpParser.cpp +49 -0
- data/ext/quickfix/HttpParser.h +54 -0
- data/ext/quickfix/HttpServer.cpp +169 -0
- data/ext/quickfix/HttpServer.h +78 -0
- data/ext/quickfix/Initiator.cpp +289 -0
- data/ext/quickfix/Initiator.h +149 -0
- data/ext/quickfix/Log.cpp +77 -0
- data/ext/quickfix/Log.h +179 -0
- data/ext/quickfix/Message.cpp +580 -0
- data/ext/quickfix/Message.h +370 -0
- data/ext/quickfix/MessageCracker.h +188 -0
- data/ext/quickfix/MessageSorters.cpp +105 -0
- data/ext/quickfix/MessageSorters.h +156 -0
- data/ext/quickfix/MessageStore.cpp +146 -0
- data/ext/quickfix/MessageStore.h +174 -0
- data/ext/quickfix/Mutex.h +131 -0
- data/ext/quickfix/MySQLConnection.h +194 -0
- data/ext/quickfix/MySQLLog.cpp +275 -0
- data/ext/quickfix/MySQLLog.h +145 -0
- data/ext/quickfix/MySQLStore.cpp +331 -0
- data/ext/quickfix/MySQLStore.h +142 -0
- data/ext/quickfix/NullStore.cpp +54 -0
- data/ext/quickfix/NullStore.h +99 -0
- data/ext/quickfix/OdbcConnection.h +266 -0
- data/ext/quickfix/OdbcLog.cpp +252 -0
- data/ext/quickfix/OdbcLog.h +117 -0
- data/ext/quickfix/OdbcStore.cpp +338 -0
- data/ext/quickfix/OdbcStore.h +113 -0
- data/ext/quickfix/PUGIXML_DOMDocument.cpp +112 -0
- data/ext/quickfix/PUGIXML_DOMDocument.h +81 -0
- data/ext/quickfix/Parser.cpp +103 -0
- data/ext/quickfix/Parser.h +57 -0
- data/ext/quickfix/PostgreSQLConnection.h +176 -0
- data/ext/quickfix/PostgreSQLLog.cpp +276 -0
- data/ext/quickfix/PostgreSQLLog.h +144 -0
- data/ext/quickfix/PostgreSQLStore.cpp +334 -0
- data/ext/quickfix/PostgreSQLStore.h +141 -0
- data/ext/quickfix/Queue.h +75 -0
- data/ext/quickfix/QuickfixRuby.cpp +252066 -0
- data/ext/quickfix/QuickfixRuby.h +34 -0
- data/ext/quickfix/Responder.h +43 -0
- data/ext/quickfix/Session.cpp +1487 -0
- data/ext/quickfix/Session.h +335 -0
- data/ext/quickfix/SessionFactory.cpp +274 -0
- data/ext/quickfix/SessionFactory.h +86 -0
- data/ext/quickfix/SessionID.h +170 -0
- data/ext/quickfix/SessionSettings.cpp +189 -0
- data/ext/quickfix/SessionSettings.h +171 -0
- data/ext/quickfix/SessionState.h +231 -0
- data/ext/quickfix/Settings.cpp +100 -0
- data/ext/quickfix/Settings.h +53 -0
- data/ext/quickfix/SharedArray.h +150 -0
- data/ext/quickfix/SocketAcceptor.cpp +222 -0
- data/ext/quickfix/SocketAcceptor.h +75 -0
- data/ext/quickfix/SocketConnection.cpp +238 -0
- data/ext/quickfix/SocketConnection.h +103 -0
- data/ext/quickfix/SocketConnector.cpp +116 -0
- data/ext/quickfix/SocketConnector.h +67 -0
- data/ext/quickfix/SocketInitiator.cpp +260 -0
- data/ext/quickfix/SocketInitiator.h +81 -0
- data/ext/quickfix/SocketMonitor.cpp +335 -0
- data/ext/quickfix/SocketMonitor.h +111 -0
- data/ext/quickfix/SocketServer.cpp +177 -0
- data/ext/quickfix/SocketServer.h +100 -0
- data/ext/quickfix/ThreadedSocketAcceptor.cpp +258 -0
- data/ext/quickfix/ThreadedSocketAcceptor.h +98 -0
- data/ext/quickfix/ThreadedSocketConnection.cpp +209 -0
- data/ext/quickfix/ThreadedSocketConnection.h +82 -0
- data/ext/quickfix/ThreadedSocketInitiator.cpp +268 -0
- data/ext/quickfix/ThreadedSocketInitiator.h +84 -0
- data/ext/quickfix/TimeRange.cpp +173 -0
- data/ext/quickfix/TimeRange.h +258 -0
- data/ext/quickfix/Utility.cpp +537 -0
- data/ext/quickfix/Utility.h +219 -0
- data/ext/quickfix/Values.h +62 -0
- data/ext/quickfix/config.h +0 -0
- data/ext/quickfix/config_windows.h +0 -0
- data/ext/quickfix/extconf.rb +12 -0
- data/ext/quickfix/index.h +37 -0
- data/ext/quickfix/pugiconfig.hpp +72 -0
- data/ext/quickfix/pugixml.cpp +10765 -0
- data/ext/quickfix/pugixml.hpp +1341 -0
- data/ext/quickfix/strptime.h +7 -0
- data/lib/quickfix40.rb +274 -0
- data/lib/quickfix41.rb +351 -0
- data/lib/quickfix42.rb +1184 -0
- data/lib/quickfix43.rb +3504 -0
- data/lib/quickfix44.rb +10721 -0
- data/lib/quickfix50.rb +13426 -0
- data/lib/quickfix50sp1.rb +15629 -0
- data/lib/quickfix50sp2.rb +17068 -0
- data/lib/quickfix_fields.rb +19853 -0
- data/lib/quickfix_ruby.rb +82 -0
- data/lib/quickfixt11.rb +70 -0
- data/spec/FIX40.xml +862 -0
- data/spec/FIX41.xml +1285 -0
- data/spec/FIX42.xml +2746 -0
- data/spec/FIX43.xml +4229 -0
- data/spec/FIX44.xml +6596 -0
- data/spec/FIX50.xml +8137 -0
- data/spec/FIX50SP1.xml +9496 -0
- data/spec/FIX50SP2.xml +10011 -0
- data/spec/FIXT11.xml +313 -0
- data/test/test_DataDictionaryTestCase.rb +268 -0
- data/test/test_DictionaryTestCase.rb +112 -0
- data/test/test_FieldBaseTestCase.rb +24 -0
- data/test/test_MessageTestCase.rb +368 -0
- data/test/test_SessionSettingsTestCase.rb +41 -0
- metadata +193 -0
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
/****************************************************************************
|
|
2
|
+
** Copyright (c) 2001-2014
|
|
3
|
+
**
|
|
4
|
+
** This file is part of the QuickFIX FIX Engine
|
|
5
|
+
**
|
|
6
|
+
** This file may be distributed under the terms of the quickfixengine.org
|
|
7
|
+
** license as defined by quickfixengine.org and appearing in the file
|
|
8
|
+
** LICENSE included in the packaging of this file.
|
|
9
|
+
**
|
|
10
|
+
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|
11
|
+
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
12
|
+
**
|
|
13
|
+
** See http://www.quickfixengine.org/LICENSE for licensing information.
|
|
14
|
+
**
|
|
15
|
+
** Contact ask@quickfixengine.org if any conditions of this licensing are
|
|
16
|
+
** not clear to you.
|
|
17
|
+
**
|
|
18
|
+
****************************************************************************/
|
|
19
|
+
|
|
20
|
+
#ifdef _MSC_VER
|
|
21
|
+
#include "stdafx.h"
|
|
22
|
+
#else
|
|
23
|
+
#include "config.h"
|
|
24
|
+
#endif
|
|
25
|
+
|
|
26
|
+
#include "Utility.h"
|
|
27
|
+
|
|
28
|
+
#ifdef USING_STREAMS
|
|
29
|
+
#include <stropts.h>
|
|
30
|
+
#include <sys/conf.h>
|
|
31
|
+
#endif
|
|
32
|
+
#include <string.h>
|
|
33
|
+
#include <math.h>
|
|
34
|
+
#include <stdio.h>
|
|
35
|
+
#include <algorithm>
|
|
36
|
+
#include <fstream>
|
|
37
|
+
|
|
38
|
+
namespace FIX
|
|
39
|
+
{
|
|
40
|
+
void string_replace( const std::string& oldValue,
|
|
41
|
+
const std::string& newValue,
|
|
42
|
+
std::string& value )
|
|
43
|
+
{
|
|
44
|
+
for( std::string::size_type pos = value.find(oldValue);
|
|
45
|
+
pos != std::string::npos;
|
|
46
|
+
pos = value.find(oldValue, pos) )
|
|
47
|
+
{
|
|
48
|
+
value.replace( pos, oldValue.size(), newValue );
|
|
49
|
+
pos += newValue.size();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
std::string string_toUpper( const std::string& value )
|
|
54
|
+
{
|
|
55
|
+
std::string copy = value;
|
|
56
|
+
std::transform( copy.begin(), copy.end(), copy.begin(), toupper );
|
|
57
|
+
return copy;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
std::string string_toLower( const std::string& value )
|
|
61
|
+
{
|
|
62
|
+
std::string copy = value;
|
|
63
|
+
std::transform( copy.begin(), copy.end(), copy.begin(), tolower );
|
|
64
|
+
return copy;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
std::string string_strip( const std::string& value )
|
|
68
|
+
{
|
|
69
|
+
if( !value.size() )
|
|
70
|
+
return value;
|
|
71
|
+
|
|
72
|
+
size_t startPos = value.find_first_not_of(" \t\r\n");
|
|
73
|
+
size_t endPos = value.find_last_not_of(" \t\r\n");
|
|
74
|
+
|
|
75
|
+
if( startPos == std::string::npos )
|
|
76
|
+
return value;
|
|
77
|
+
|
|
78
|
+
return std::string( value, startPos, endPos - startPos + 1 );
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
void socket_init()
|
|
82
|
+
{
|
|
83
|
+
#ifdef _MSC_VER
|
|
84
|
+
WORD version = MAKEWORD( 2, 2 );
|
|
85
|
+
WSADATA data;
|
|
86
|
+
WSAStartup( version, &data );
|
|
87
|
+
#else
|
|
88
|
+
struct sigaction sa;
|
|
89
|
+
sa.sa_handler = SIG_IGN;
|
|
90
|
+
sigemptyset( &sa.sa_mask );
|
|
91
|
+
sa.sa_flags = 0;
|
|
92
|
+
sigaction( SIGPIPE, &sa, 0 );
|
|
93
|
+
#endif
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
void socket_term()
|
|
97
|
+
{
|
|
98
|
+
#ifdef _MSC_VER
|
|
99
|
+
WSACleanup();
|
|
100
|
+
#endif
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
int socket_createAcceptor(int port, bool reuse)
|
|
104
|
+
{
|
|
105
|
+
int socket = ::socket( PF_INET, SOCK_STREAM, 0 );
|
|
106
|
+
if ( socket < 0 ) return -1;
|
|
107
|
+
|
|
108
|
+
sockaddr_in address;
|
|
109
|
+
socklen_t socklen;
|
|
110
|
+
|
|
111
|
+
address.sin_family = PF_INET;
|
|
112
|
+
address.sin_port = htons( port );
|
|
113
|
+
address.sin_addr.s_addr = INADDR_ANY;
|
|
114
|
+
socklen = sizeof( address );
|
|
115
|
+
if( reuse )
|
|
116
|
+
socket_setsockopt( socket, SO_REUSEADDR );
|
|
117
|
+
|
|
118
|
+
int result = bind( socket, reinterpret_cast < sockaddr* > ( &address ),
|
|
119
|
+
socklen );
|
|
120
|
+
if ( result < 0 ) return -1;
|
|
121
|
+
result = listen( socket, SOMAXCONN );
|
|
122
|
+
if ( result < 0 ) return -1;
|
|
123
|
+
return socket;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
int socket_createConnector()
|
|
127
|
+
{
|
|
128
|
+
return ::socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
int socket_connect( int socket, const char* address, int port )
|
|
132
|
+
{
|
|
133
|
+
const char* hostname = socket_hostname( address );
|
|
134
|
+
if( hostname == 0 ) return -1;
|
|
135
|
+
|
|
136
|
+
sockaddr_in addr;
|
|
137
|
+
addr.sin_family = PF_INET;
|
|
138
|
+
addr.sin_port = htons( port );
|
|
139
|
+
addr.sin_addr.s_addr = inet_addr( hostname );
|
|
140
|
+
|
|
141
|
+
int result = connect( socket, reinterpret_cast < sockaddr* > ( &addr ),
|
|
142
|
+
sizeof( addr ) );
|
|
143
|
+
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
int socket_accept( int s )
|
|
148
|
+
{
|
|
149
|
+
if ( !socket_isValid( s ) ) return -1;
|
|
150
|
+
return accept( s, 0, 0 );
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
ssize_t socket_send( int s, const char* msg, size_t length )
|
|
154
|
+
{
|
|
155
|
+
return send( s, msg, length, 0 );
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
void socket_close( int s )
|
|
159
|
+
{
|
|
160
|
+
shutdown( s, 2 );
|
|
161
|
+
#ifdef _MSC_VER
|
|
162
|
+
closesocket( s );
|
|
163
|
+
#else
|
|
164
|
+
close( s );
|
|
165
|
+
#endif
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
bool socket_fionread( int s, int& bytes )
|
|
169
|
+
{
|
|
170
|
+
bytes = 0;
|
|
171
|
+
#if defined(_MSC_VER)
|
|
172
|
+
return ::ioctlsocket( s, FIONREAD, &( ( unsigned long& ) bytes ) ) == 0;
|
|
173
|
+
#elif defined(USING_STREAMS)
|
|
174
|
+
return ::ioctl( s, I_NREAD, &bytes ) >= 0;
|
|
175
|
+
#else
|
|
176
|
+
return ::ioctl( s, FIONREAD, &bytes ) == 0;
|
|
177
|
+
#endif
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
bool socket_disconnected( int s )
|
|
181
|
+
{
|
|
182
|
+
char byte;
|
|
183
|
+
return ::recv (s, &byte, sizeof (byte), MSG_PEEK) <= 0;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
int socket_setsockopt( int s, int opt )
|
|
187
|
+
{
|
|
188
|
+
#ifdef _MSC_VER
|
|
189
|
+
BOOL optval = TRUE;
|
|
190
|
+
#else
|
|
191
|
+
int optval = 1;
|
|
192
|
+
#endif
|
|
193
|
+
return socket_setsockopt( s, opt, optval );
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
int socket_setsockopt( int s, int opt, int optval )
|
|
197
|
+
{
|
|
198
|
+
int level = SOL_SOCKET;
|
|
199
|
+
if( opt == TCP_NODELAY )
|
|
200
|
+
level = IPPROTO_TCP;
|
|
201
|
+
|
|
202
|
+
#ifdef _MSC_VER
|
|
203
|
+
return ::setsockopt( s, level, opt,
|
|
204
|
+
( char* ) & optval, sizeof( optval ) );
|
|
205
|
+
#else
|
|
206
|
+
return ::setsockopt( s, level, opt,
|
|
207
|
+
&optval, sizeof( optval ) );
|
|
208
|
+
#endif
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
int socket_getsockopt( int s, int opt, int& optval )
|
|
212
|
+
{
|
|
213
|
+
int level = SOL_SOCKET;
|
|
214
|
+
if( opt == TCP_NODELAY )
|
|
215
|
+
level = IPPROTO_TCP;
|
|
216
|
+
|
|
217
|
+
#ifdef _MSC_VER
|
|
218
|
+
int length = sizeof(int);
|
|
219
|
+
#else
|
|
220
|
+
socklen_t length = sizeof(socklen_t);
|
|
221
|
+
#endif
|
|
222
|
+
|
|
223
|
+
return ::getsockopt( s, level, opt,
|
|
224
|
+
( char* ) & optval, & length );
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
#ifndef _MSC_VER
|
|
228
|
+
int socket_fcntl( int s, int opt, int arg )
|
|
229
|
+
{
|
|
230
|
+
return ::fcntl( s, opt, arg );
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
int socket_getfcntlflag( int s, int arg )
|
|
234
|
+
{
|
|
235
|
+
return socket_fcntl( s, F_GETFL, arg );
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
int socket_setfcntlflag( int s, int arg )
|
|
239
|
+
{
|
|
240
|
+
int oldValue = socket_getfcntlflag( s, arg );
|
|
241
|
+
oldValue |= arg;
|
|
242
|
+
return socket_fcntl( s, F_SETFL, arg );
|
|
243
|
+
}
|
|
244
|
+
#endif
|
|
245
|
+
|
|
246
|
+
void socket_setnonblock( int socket )
|
|
247
|
+
{
|
|
248
|
+
#ifdef _MSC_VER
|
|
249
|
+
u_long opt = 1;
|
|
250
|
+
::ioctlsocket( socket, FIONBIO, &opt );
|
|
251
|
+
#else
|
|
252
|
+
socket_setfcntlflag( socket, O_NONBLOCK );
|
|
253
|
+
#endif
|
|
254
|
+
}
|
|
255
|
+
bool socket_isValid( int socket )
|
|
256
|
+
{
|
|
257
|
+
#ifdef _MSC_VER
|
|
258
|
+
return socket != INVALID_SOCKET;
|
|
259
|
+
#else
|
|
260
|
+
return socket >= 0;
|
|
261
|
+
#endif
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#ifndef _MSC_VER
|
|
265
|
+
bool socket_isBad( int s )
|
|
266
|
+
{
|
|
267
|
+
struct stat buf;
|
|
268
|
+
fstat( s, &buf );
|
|
269
|
+
return errno == EBADF;
|
|
270
|
+
}
|
|
271
|
+
#endif
|
|
272
|
+
|
|
273
|
+
void socket_invalidate( int& socket )
|
|
274
|
+
{
|
|
275
|
+
#ifdef _MSC_VER
|
|
276
|
+
socket = INVALID_SOCKET;
|
|
277
|
+
#else
|
|
278
|
+
socket = -1;
|
|
279
|
+
#endif
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
short socket_hostport( int socket )
|
|
283
|
+
{
|
|
284
|
+
struct sockaddr_in addr;
|
|
285
|
+
socklen_t len = sizeof(addr);
|
|
286
|
+
if( getsockname(socket, (struct sockaddr*)&addr, &len) < 0 )
|
|
287
|
+
return 0;
|
|
288
|
+
|
|
289
|
+
return ntohs( addr.sin_port );
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const char* socket_hostname( int socket )
|
|
293
|
+
{
|
|
294
|
+
struct sockaddr_in addr;
|
|
295
|
+
socklen_t len = sizeof(addr);
|
|
296
|
+
if( getsockname(socket, (struct sockaddr*)&addr, &len) < 0 )
|
|
297
|
+
return 0;
|
|
298
|
+
|
|
299
|
+
return inet_ntoa( addr.sin_addr );
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const char* socket_hostname( const char* name )
|
|
303
|
+
{
|
|
304
|
+
struct hostent* host_ptr = 0;
|
|
305
|
+
struct in_addr** paddr;
|
|
306
|
+
struct in_addr saddr;
|
|
307
|
+
|
|
308
|
+
#if( GETHOSTBYNAME_R_INPUTS_RESULT || GETHOSTBYNAME_R_RETURNS_RESULT )
|
|
309
|
+
hostent host;
|
|
310
|
+
char buf[1024];
|
|
311
|
+
int error;
|
|
312
|
+
#endif
|
|
313
|
+
|
|
314
|
+
saddr.s_addr = inet_addr( name );
|
|
315
|
+
if ( saddr.s_addr != ( unsigned ) - 1 ) return name;
|
|
316
|
+
|
|
317
|
+
#if GETHOSTBYNAME_R_INPUTS_RESULT
|
|
318
|
+
gethostbyname_r( name, &host, buf, sizeof(buf), &host_ptr, &error );
|
|
319
|
+
#elif GETHOSTBYNAME_R_RETURNS_RESULT
|
|
320
|
+
host_ptr = gethostbyname_r( name, &host, buf, sizeof(buf), &error );
|
|
321
|
+
#else
|
|
322
|
+
host_ptr = gethostbyname( name );
|
|
323
|
+
#endif
|
|
324
|
+
|
|
325
|
+
if ( host_ptr == 0 ) return 0;
|
|
326
|
+
|
|
327
|
+
paddr = ( struct in_addr ** ) host_ptr->h_addr_list;
|
|
328
|
+
return inet_ntoa( **paddr );
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const char* socket_peername( int socket )
|
|
332
|
+
{
|
|
333
|
+
struct sockaddr_in addr;
|
|
334
|
+
socklen_t len = sizeof(addr);
|
|
335
|
+
if( getpeername( socket, (struct sockaddr*)&addr, &len ) < 0 )
|
|
336
|
+
return "UNKNOWN";
|
|
337
|
+
char* result = inet_ntoa( addr.sin_addr );
|
|
338
|
+
if( result )
|
|
339
|
+
return result;
|
|
340
|
+
else
|
|
341
|
+
return "UNKNOWN";
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
std::pair<int, int> socket_createpair()
|
|
345
|
+
{
|
|
346
|
+
#ifdef _MSC_VER
|
|
347
|
+
int acceptor = socket_createAcceptor(0, true);
|
|
348
|
+
const char* host = socket_hostname( acceptor );
|
|
349
|
+
short port = socket_hostport( acceptor );
|
|
350
|
+
int client = socket_createConnector();
|
|
351
|
+
socket_connect( client, "localhost", port );
|
|
352
|
+
int server = socket_accept( acceptor );
|
|
353
|
+
socket_close(acceptor);
|
|
354
|
+
return std::pair<int, int>( client, server );
|
|
355
|
+
#else
|
|
356
|
+
int pair[2];
|
|
357
|
+
socketpair( AF_UNIX, SOCK_STREAM, 0, pair );
|
|
358
|
+
return std::pair<int, int>( pair[0], pair[1] );
|
|
359
|
+
#endif
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
tm time_gmtime( const time_t* t )
|
|
363
|
+
{
|
|
364
|
+
#ifdef _MSC_VER
|
|
365
|
+
#if( _MSC_VER >= 1400 )
|
|
366
|
+
tm result;
|
|
367
|
+
gmtime_s( &result, t );
|
|
368
|
+
return result;
|
|
369
|
+
#else
|
|
370
|
+
return *gmtime( t );
|
|
371
|
+
#endif
|
|
372
|
+
#else
|
|
373
|
+
tm result;
|
|
374
|
+
return *gmtime_r( t, &result );
|
|
375
|
+
#endif
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
tm time_localtime( const time_t* t)
|
|
379
|
+
{
|
|
380
|
+
#ifdef _MSC_VER
|
|
381
|
+
#if( _MSC_VER >= 1400 )
|
|
382
|
+
tm result;
|
|
383
|
+
localtime_s( &result, t );
|
|
384
|
+
return result;
|
|
385
|
+
#else
|
|
386
|
+
return *localtime( t );
|
|
387
|
+
#endif
|
|
388
|
+
#else
|
|
389
|
+
tm result;
|
|
390
|
+
return *localtime_r( t, &result );
|
|
391
|
+
#endif
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
bool thread_spawn( THREAD_START_ROUTINE func, void* var, thread_id& thread )
|
|
395
|
+
{
|
|
396
|
+
#ifdef _MSC_VER
|
|
397
|
+
thread_id result = 0;
|
|
398
|
+
unsigned int id = 0;
|
|
399
|
+
result = _beginthreadex( NULL, 0, &func, var, 0, &id );
|
|
400
|
+
if ( result == 0 ) return false;
|
|
401
|
+
#else
|
|
402
|
+
thread_id result = 0;
|
|
403
|
+
if( pthread_create( &result, 0, func, var ) != 0 ) return false;
|
|
404
|
+
#endif
|
|
405
|
+
thread = result;
|
|
406
|
+
return true;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
bool thread_spawn( THREAD_START_ROUTINE func, void* var )
|
|
410
|
+
{
|
|
411
|
+
thread_id thread = 0;
|
|
412
|
+
return thread_spawn( func, var, thread );
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
void thread_join( thread_id thread )
|
|
416
|
+
{
|
|
417
|
+
#ifdef _MSC_VER
|
|
418
|
+
WaitForSingleObject( ( void* ) thread, INFINITE );
|
|
419
|
+
CloseHandle((HANDLE)thread);
|
|
420
|
+
#else
|
|
421
|
+
pthread_join( ( pthread_t ) thread, 0 );
|
|
422
|
+
#endif
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
void thread_detach( thread_id thread )
|
|
426
|
+
{
|
|
427
|
+
#ifdef _MSC_VER
|
|
428
|
+
CloseHandle((HANDLE)thread);
|
|
429
|
+
#else
|
|
430
|
+
pthread_t t = thread;
|
|
431
|
+
pthread_detach( t );
|
|
432
|
+
#endif
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
thread_id thread_self()
|
|
436
|
+
{
|
|
437
|
+
#ifdef _MSC_VER
|
|
438
|
+
return (unsigned)GetCurrentThread();
|
|
439
|
+
#else
|
|
440
|
+
return pthread_self();
|
|
441
|
+
#endif
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
void process_sleep( double s )
|
|
445
|
+
{
|
|
446
|
+
#ifdef _MSC_VER
|
|
447
|
+
Sleep( (long)(s * 1000) );
|
|
448
|
+
#else
|
|
449
|
+
timespec time, remainder;
|
|
450
|
+
double intpart;
|
|
451
|
+
time.tv_nsec = (long)(modf(s, &intpart) * 1e9);
|
|
452
|
+
time.tv_sec = (int)intpart;
|
|
453
|
+
while( nanosleep(&time, &remainder) == -1 )
|
|
454
|
+
time = remainder;
|
|
455
|
+
#endif
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
std::string file_separator()
|
|
459
|
+
{
|
|
460
|
+
#ifdef _MSC_VER
|
|
461
|
+
return "\\";
|
|
462
|
+
#else
|
|
463
|
+
return "/";
|
|
464
|
+
#endif
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
void file_mkdir( const char* path )
|
|
468
|
+
{
|
|
469
|
+
int length = (int)strlen( path );
|
|
470
|
+
std::string createPath = "";
|
|
471
|
+
|
|
472
|
+
for( const char* pos = path; (pos - path) <= length; ++pos )
|
|
473
|
+
{
|
|
474
|
+
createPath += *pos;
|
|
475
|
+
if( *pos == '/' || *pos == '\\' || (pos - path) == length )
|
|
476
|
+
{
|
|
477
|
+
#ifdef _MSC_VER
|
|
478
|
+
_mkdir( createPath.c_str() );
|
|
479
|
+
#else
|
|
480
|
+
// use umask to override rwx for all
|
|
481
|
+
mkdir( createPath.c_str(), 0777 );
|
|
482
|
+
#endif
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
FILE* file_fopen( const char* path, const char* mode )
|
|
488
|
+
{
|
|
489
|
+
#if( _MSC_VER >= 1400 )
|
|
490
|
+
FILE* result = 0;
|
|
491
|
+
fopen_s( &result, path, mode );
|
|
492
|
+
return result;
|
|
493
|
+
#else
|
|
494
|
+
return fopen( path, mode );
|
|
495
|
+
#endif
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
void file_fclose( FILE* file )
|
|
499
|
+
{
|
|
500
|
+
fclose( file );
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
bool file_exists( const char* path )
|
|
504
|
+
{
|
|
505
|
+
std::ifstream stream;
|
|
506
|
+
stream.open( path, std::ios_base::in );
|
|
507
|
+
if( stream.is_open() )
|
|
508
|
+
{
|
|
509
|
+
stream.close();
|
|
510
|
+
return true;
|
|
511
|
+
}
|
|
512
|
+
return false;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
void file_unlink( const char* path )
|
|
516
|
+
{
|
|
517
|
+
#ifdef _MSC_VER
|
|
518
|
+
_unlink( path );
|
|
519
|
+
#else
|
|
520
|
+
unlink( path );
|
|
521
|
+
#endif
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
int file_rename( const char* oldpath, const char* newpath )
|
|
525
|
+
{
|
|
526
|
+
return rename( oldpath, newpath );
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
std::string file_appendpath( const std::string& path, const std::string& file )
|
|
530
|
+
{
|
|
531
|
+
const char last = path[path.size()-1];
|
|
532
|
+
if( last == '/' || last == '\\' )
|
|
533
|
+
return std::string(path) + file;
|
|
534
|
+
else
|
|
535
|
+
return std::string(path) + file_separator() + file;
|
|
536
|
+
}
|
|
537
|
+
}
|