Commit b21526cb authored by Davis King's avatar Davis King

Changed a few things so that the library works in cygwin.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402432
parent 4e042751
...@@ -54,7 +54,7 @@ add_library(dlib all/source.cpp ) ...@@ -54,7 +54,7 @@ add_library(dlib all/source.cpp )
IF (NOT DLIB_ISO_CPP_ONLY) IF (NOT DLIB_ISO_CPP_ONLY)
# we want to link to the right stuff depending on our platform. # we want to link to the right stuff depending on our platform.
IF (WIN32) ############################################################################### IF (WIN32 AND NOT CYGWIN) ###############################################################################
if (DLIB_NO_GUI_SUPPORT) if (DLIB_NO_GUI_SUPPORT)
set (dlib_needed_libraries ws2_32) set (dlib_needed_libraries ws2_32)
else() else()
......
...@@ -42,10 +42,19 @@ ...@@ -42,10 +42,19 @@
#pragma option -w-8026 #pragma option -w-8026
#endif #endif
#include <string> // for the exceptions
#ifdef __CYGWIN__
namespace std
{
typedef std::basic_string<wchar_t> wstring;
}
#endif
#include <algorithm> // for std::swap #include <algorithm> // for std::swap
#include <new> // for std::bad_alloc #include <new> // for std::bad_alloc
#include <string> // for the exceptions
#include <cstdlib> #include <cstdlib>
#include "platform.h" #include "platform.h"
#include "assert.h" #include "assert.h"
......
...@@ -80,6 +80,9 @@ namespace dlib ...@@ -80,6 +80,9 @@ namespace dlib
// ----------------- // -----------------
// cygwin currently doesn't support the getaddrinfo stuff
#ifndef __CYGWIN__
int int
hostname_to_ip ( hostname_to_ip (
const std::string& hostname, const std::string& hostname,
...@@ -143,6 +146,7 @@ namespace dlib ...@@ -143,6 +146,7 @@ namespace dlib
return 0; return 0;
} }
// ----------------- // -----------------
int int
...@@ -185,6 +189,109 @@ namespace dlib ...@@ -185,6 +189,109 @@ namespace dlib
} }
return 0; return 0;
} }
#else
int
hostname_to_ip (
const std::string& hostname,
std::string& ip,
int n
)
{
// ensure that WSAStartup has been called and WSACleanup will eventually
// be called when program ends
sockets_startup();
try
{
// if no hostname was given then return error
if ( hostname.empty())
return OTHER_ERROR;
hostent* address;
address = gethostbyname(hostname.c_str());
if (address == 0)
{
return OTHER_ERROR;
}
// find the nth address
in_addr* addr = reinterpret_cast<in_addr*>(address->h_addr_list[0]);
for (int i = 1; i <= n; ++i)
{
addr = reinterpret_cast<in_addr*>(address->h_addr_list[i]);
// if there is no nth address then return error
if (addr == 0)
return OTHER_ERROR;
}
char* resolved_ip = inet_ntoa(*addr);
// check if inet_ntoa returned an error
if (resolved_ip == NULL)
{
return OTHER_ERROR;
}
ip.assign(resolved_ip);
}
catch(...)
{
return OTHER_ERROR;
}
return 0;
}
// -----------------
int
ip_to_hostname (
const std::string& ip,
std::string& hostname
)
{
// ensure that WSAStartup has been called and WSACleanup will eventually
// be called when program ends
sockets_startup();
try
{
// if no ip was given then return error
if (ip.empty())
return OTHER_ERROR;
hostent* address;
unsigned long ipnum = inet_addr(ip.c_str());
// if inet_addr coudln't convert ip then return an error
if (ipnum == INADDR_NONE)
{
return OTHER_ERROR;
}
address = gethostbyaddr(reinterpret_cast<char*>(&ipnum),4,AF_INET);
// check if gethostbyaddr returned an error
if (address == 0)
{
return OTHER_ERROR;
}
hostname.assign(address->h_name);
}
catch (...)
{
return OTHER_ERROR;
}
return 0;
}
#endif // __CYGWIN__
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
# setting this makes CMake allow normal looking IF ELSE statements # setting this makes CMake allow normal looking IF ELSE statements
SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true) SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
cmake_minimum_required(VERSION 2.4)
# This variable contains a list of all the tests we are building # This variable contains a list of all the tests we are building
# into the regression test suite. # into the regression test suite.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment