From c3f7c85f8aff9cf294a3dc26adb4933bc2f83a21 Mon Sep 17 00:00:00 2001 From: Thad House Date: Sun, 27 Aug 2017 21:35:34 -0700 Subject: [PATCH] Adds wpi GetHostname function (#25) --- src/dev/native/cpp/main.cpp | 3 + src/main/native/cpp/support/hostname.cpp | 64 ++++++++++++++++++++++ src/main/native/include/support/hostname.h | 24 ++++++++ src/test/native/cpp/hostname.cpp | 22 ++++++++ 4 files changed, 113 insertions(+) create mode 100644 src/main/native/cpp/support/hostname.cpp create mode 100644 src/main/native/include/support/hostname.h create mode 100644 src/test/native/cpp/hostname.cpp diff --git a/src/dev/native/cpp/main.cpp b/src/dev/native/cpp/main.cpp index 06257fdea6..41b2126618 100644 --- a/src/dev/native/cpp/main.cpp +++ b/src/dev/native/cpp/main.cpp @@ -1,6 +1,9 @@ #include "llvm/StringRef.h" +#include "llvm/SmallVector.h" #include +#include "support/hostname.h" + int main() { llvm::StringRef v1("Hello"); std::cout << v1.lower() << std::endl; diff --git a/src/main/native/cpp/support/hostname.cpp b/src/main/native/cpp/support/hostname.cpp new file mode 100644 index 0000000000..a6830d9583 --- /dev/null +++ b/src/main/native/cpp/support/hostname.cpp @@ -0,0 +1,64 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2017. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "support/hostname.h" + +#ifdef _WIN32 +#include +#pragma comment(lib, "Ws2_32.lib") +#else +#include +#endif + +#include +#include "llvm/SmallVector.h" +#include "llvm/StringRef.h" + +#ifdef _WIN32 +struct WSAHelper { + WSAHelper() { + WSAData wsaData; + WORD wVersionRequested = MAKEWORD(2, 2); + WSAStartup(wVersionRequested, &wsaData); + } + ~WSAHelper() { WSACleanup(); } +}; + +static WSAHelper& GetWSAHelper() { + static WSAHelper helper; + return helper; +} +#endif + +namespace wpi { +static bool GetHostnameImpl(char* name, size_t name_len) { +#ifdef _WIN32 + GetWSAHelper(); +#endif + if (::gethostname(name, name_len) != 0) return false; + name[name_len - 1] = '\0'; // Per POSIX, may not be null terminated if too long + return true; +} + +std::string GetHostname() { + char name[256]; + if (!GetHostnameImpl(name, sizeof(name))) return ""; + return name; +} + +llvm::StringRef GetHostname(llvm::SmallVectorImpl& name) { + // Use a tmp array to not require the SmallVector to be too large. + char tmpName[256]; + if (!GetHostnameImpl(tmpName, sizeof(tmpName))) { + return llvm::StringRef{}; + } + name.clear(); + name.append(tmpName, tmpName + std::strlen(tmpName) + 1); + + return llvm::StringRef{name.data(), name.size(), true}; +} +} // namespace wpi diff --git a/src/main/native/include/support/hostname.h b/src/main/native/include/support/hostname.h new file mode 100644 index 0000000000..0460900c54 --- /dev/null +++ b/src/main/native/include/support/hostname.h @@ -0,0 +1,24 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2017. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#ifndef HOSTNAME_H_ +#define HOSTNAME_H_ + +#include +#include "llvm/StringRef.h" + +namespace llvm { + template + class SmallVectorImpl; + } + +namespace wpi { +std::string GetHostname(); +llvm::StringRef GetHostname(llvm::SmallVectorImpl& name); +} // namespace wpi + +#endif // HOSTNAME_H_ diff --git a/src/test/native/cpp/hostname.cpp b/src/test/native/cpp/hostname.cpp new file mode 100644 index 0000000000..915819fc57 --- /dev/null +++ b/src/test/native/cpp/hostname.cpp @@ -0,0 +1,22 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2017. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "support/hostname.h" +#include "llvm/SmallString.h" +#include "llvm/SmallVector.h" + +#include "gtest/gtest.h" + +namespace wpi { +TEST(HostNameTest, HostNameNotEmpty) { + ASSERT_NE(wpi::GetHostname(), ""); +} +TEST(HostNameTest, HostNameNotEmptySmallVector) { + llvm::SmallVector name; + ASSERT_NE(wpi::GetHostname(name), ""); +} +} // namespace wpi