/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* This is GNU Go, a Go program. Contact gnugo@gnu.org, or see *
* http://www.gnu.org/software/gnugo/ for more information. *
* Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *
* 2008 and 2009 by the Free Software Foundation. *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation - version 3 or *
* (at your option) any later version. *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License in file COPYING for more details. *
* You should have received a copy of the GNU General Public *
* License along with this program; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02111, USA. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Workaround M$ Windows C library deficiency: it cannot handle
* sockets as stdio streams. So we do this ourselves, more or less.
* Call winsocket_activate(...) with socket handle to use. After this
* fake stream NULL will work over the socket, while other streams
* will (hopefully) keep working as usual.
#define WINSOCKET_H_INTERNAL_INCLUSION
#if USE_WINDOWS_SOCKET_CLUDGE
static int socket_handle
= 0;
static int socket_end_of_file
= 0;
winsocket_activate(int _socket_handle
)
assert(socket_handle
== 0);
socket_handle
= _socket_handle
;
/* Miscellaneous functions. */
winsocket_setbuf(FILE *file
, char *buffer
)
assert(socket_handle
!= 0);
winsocket_fflush(FILE *file
)
assert(socket_handle
!= 0);
winsocket_feof(FILE *file
)
assert(socket_handle
!= 0);
return socket_end_of_file
;
winsocket_fclose(FILE *file
)
assert(socket_handle
!= 0);
winsocket_fread(void *buffer
, size_t size
, size_t num_items
, FILE *file
)
return fread(buffer
, size
, num_items
, file
);
assert(socket_handle
!= 0);
if (recv(socket_handle
, (char *) buffer
, size
* num_items
, 0)
winsocket_fgets(char *buffer
, int size
, FILE *file
)
return fgets(buffer
, size
, file
);
/* FIXME: Optimize if reading char-by-char is too slow. */
for (stored_length
= 0; stored_length
< size
- 1; stored_length
) {
if (recv(socket_handle
, buffer
+ stored_length
, 1, 0) != 1) {
if (buffer
[stored_length
++] == '\n')
buffer
[stored_length
+ 1] = 0;
winsocket_fwrite(const void *buffer
, size_t size
, size_t num_items
,
return fwrite(buffer
, size
, num_items
, file
);
assert(socket_handle
!= 0);
return ((send(socket_handle
, (const char *) buffer
, size
* num_items
, 0)
winsocket_fputc(int character
, FILE *file
)
return fputc(character
, file
);
assert(socket_handle
!= 0);
return (send(socket_handle
, (const char *) &character
, 1, 0) == 1
winsocket_fputs(const char *string
, FILE *file
)
return fputs(string
, file
);
int length
= strlen(string
);
assert(socket_handle
!= 0);
return send(socket_handle
, string
, length
, 0) == length
? length
: EOF
;
winsocket_fprintf(FILE *file
, const char *format_string
, ...)
va_start(arguments
, format_string
);
result
= winsocket_vfprintf(file
, format_string
, arguments
);
winsocket_vfprintf(FILE *file
, const char *format_string
, va_list arguments
)
return vfprintf(file
, format_string
, arguments
);
int length
= _vsnprintf(buffer
, sizeof buffer
, format_string
, arguments
);
assert(socket_handle
!= 0);
return send(socket_handle
, buffer
, length
, 0) == length
? length
: -1;
#endif /* USE_WINDOWS_SOCKET_CLUDGE */