Fix type warnings

This commit is contained in:
Danny Robson 2013-08-05 16:41:20 +10:00
parent 9ff59f8130
commit 798ea55ec9
5 changed files with 37 additions and 18 deletions

View File

@ -20,6 +20,7 @@
#include "except.hpp"
#include "../debug.hpp"
#include "../types/casts.hpp"
//-----------------------------------------------------------------------------
@ -38,11 +39,15 @@ net::error::error (int _code):
{ CHECK (_code != 0); }
//-----------------------------------------------------------------------------
std::string
net::error::code_to_string (int code) {
#ifdef __WIN32
char message[256];
int output = FormatMessage (0, NULL, code, 0, message, sizeof (message), NULL);
// It should be fine to signcast the code here as Windows guarantees all
// error messages are positive but appears to use int for compatibility
DWORD output = FormatMessage (0, NULL, sign_cast<unsigned> (code), 0, message, sizeof (message), NULL);
CHECK_HARD (output != 0);
return std::string (message);
@ -52,6 +57,7 @@ net::error::code_to_string (int code) {
}
//-----------------------------------------------------------------------------
void
net::error::throw_code (int code) {
#ifdef __WIN32
@ -78,6 +84,12 @@ net::error::throw_code (int code) {
}
void
net::error::throw_code (void)
{ throw_code (last_code ()); }
//-----------------------------------------------------------------------------
void
net::error::try_code (int err) {
if (err == 0)
@ -92,11 +104,7 @@ net::error::try_code (void)
{ try_code (last_code ()); }
void
net::error::throw_code (void)
{ throw_code (last_code ()); }
//-----------------------------------------------------------------------------
int
net::error::last_code (void) {
#ifdef __WIN32
@ -107,6 +115,7 @@ net::error::last_code (void) {
}
//-----------------------------------------------------------------------------
template <int CODE>
net::error_code<CODE>::error_code (void):
net::error (CODE)

View File

@ -25,7 +25,9 @@
#include "../log.hpp"
#include "except.hpp"
#if !defined(HAVE_WINSOCK2_H)
#if defined(HAVE_WINSOCK2_H)
#include <winsock2.h>
#else
#include <sys/socket.h>
#endif
@ -211,8 +213,8 @@ net::socket<D, type::STREAM>::listen (const address_type &_addr, unsigned int _b
template <domain D>
typename net::socket<D, type::STREAM>::socket_ptr
net::socket<D, type::STREAM>::accept (void) {
int newfd = ::accept (this->m_fd, NULL, 0);
if (newfd < 0)
socket_t newfd = ::accept (this->m_fd, NULL, 0);
if (newfd == INVALID_SOCKET)
net::error::throw_code ();
return socket_ptr(new socket<D, type::STREAM> (newfd));

View File

@ -41,6 +41,7 @@ namespace net {
typedef SOCKET socket_t;
#else
typedef int socket_t;
const socket_t INVALID_SOCKET = -1;
#endif
/// Defines the protocol family, or communication domain of a socket (see `man socket').

View File

@ -21,6 +21,8 @@
#include <cstring>
#include "types/casts.hpp"
// TODO: Horribly inefficient, but God help you if you're relying on this
// being efficient in the first place.
bool
@ -32,23 +34,24 @@ strbegins (const char *restrict str,
#if !defined(HAVE_STRNDUP)
#include <cstdlib>
// Written by Niels Möller <nisse@lysator.liu.se>
// Placed in the public domain
char *restrict
char *
strndup (const char *restrict s, size_t size)
{
char *r;
char *end = (char *)memchr(s, 0, size);
char *end = static_cast<char *> (memchr (s, 0, size));
if (end)
// Length + 1
size = end - s + 1;
size = sign_cast<size_t> (end - s) + 1u;
char * r = malloc(size);
char * r = static_cast<char *> (malloc (size));
if (size) {
memcpy(r, s, size-1);
memcpy (r, s, size-1);
r[size-1] = '\0';
}
return r;

View File

@ -26,8 +26,12 @@ strbegins(const char *restrict str,
#if !defined(HAVE_STRNDUP)
char *restrict
strndup (const char *restrict s, size_t size);
#include <cstddef>
extern "C" {
char *
strndup (const char *restrict s, size_t size);
}
#endif
#endif // __UTIL_STRING_HPP