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 "except.hpp"
#include "../debug.hpp" #include "../debug.hpp"
#include "../types/casts.hpp"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -38,11 +39,15 @@ net::error::error (int _code):
{ CHECK (_code != 0); } { CHECK (_code != 0); }
//-----------------------------------------------------------------------------
std::string std::string
net::error::code_to_string (int code) { net::error::code_to_string (int code) {
#ifdef __WIN32 #ifdef __WIN32
char message[256]; 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); CHECK_HARD (output != 0);
return std::string (message); return std::string (message);
@ -52,6 +57,7 @@ net::error::code_to_string (int code) {
} }
//-----------------------------------------------------------------------------
void void
net::error::throw_code (int code) { net::error::throw_code (int code) {
#ifdef __WIN32 #ifdef __WIN32
@ -78,6 +84,12 @@ net::error::throw_code (int code) {
} }
void
net::error::throw_code (void)
{ throw_code (last_code ()); }
//-----------------------------------------------------------------------------
void void
net::error::try_code (int err) { net::error::try_code (int err) {
if (err == 0) if (err == 0)
@ -92,11 +104,7 @@ net::error::try_code (void)
{ try_code (last_code ()); } { try_code (last_code ()); }
void //-----------------------------------------------------------------------------
net::error::throw_code (void)
{ throw_code (last_code ()); }
int int
net::error::last_code (void) { net::error::last_code (void) {
#ifdef __WIN32 #ifdef __WIN32
@ -107,6 +115,7 @@ net::error::last_code (void) {
} }
//-----------------------------------------------------------------------------
template <int CODE> template <int CODE>
net::error_code<CODE>::error_code (void): net::error_code<CODE>::error_code (void):
net::error (CODE) net::error (CODE)

View File

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

View File

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

View File

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

View File

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