You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
6.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: pantheios/util/system/hostname.h
  3. *
  4. * Purpose: Functions for eliciting host name
  5. *
  6. * Created: 14th April 2008
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://www.pantheios.org/
  10. *
  11. * Copyright (c) 2008-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are
  16. * met:
  17. *
  18. * - Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * - Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the
  24. * names of any contributors may be used to endorse or promote products
  25. * derived from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * ////////////////////////////////////////////////////////////////////// */
  40. /** \file pantheios/util/system/hostname.h
  41. *
  42. * [C, C++] Functions for eliciting host name
  43. */
  44. #ifndef PANTHEIOS_INCL_PANTHEIOS_SYSTEM_H_HOSTNAME
  45. #define PANTHEIOS_INCL_PANTHEIOS_SYSTEM_H_HOSTNAME
  46. /* /////////////////////////////////////////////////////////////////////////
  47. * Version information
  48. */
  49. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  50. # define PANTHEIOS_VER_PANTHEIOS_SYSTEM_H_HOSTNAME_MAJOR 1
  51. # define PANTHEIOS_VER_PANTHEIOS_SYSTEM_H_HOSTNAME_MINOR 2
  52. # define PANTHEIOS_VER_PANTHEIOS_SYSTEM_H_HOSTNAME_REVISION 1
  53. # define PANTHEIOS_VER_PANTHEIOS_SYSTEM_H_HOSTNAME_EDIT 12
  54. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  55. /* /////////////////////////////////////////////////////////////////////////
  56. * Includes
  57. */
  58. #ifndef PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS
  59. # include <pantheios/pantheios.h>
  60. #endif /* !PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS */
  61. #ifndef PANTHEIOS_INCL_PANTHEIOS_QUALITY_H_CONTRACT
  62. # include <pantheios/quality/contract.h>
  63. #endif /* !PANTHEIOS_INCL_PANTHEIOS_QUALITY_H_CONTRACT */
  64. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  65. # include <stlsoft/stlsoft.h>
  66. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  67. #ifdef __cplusplus
  68. # if defined(STLSOFT_COMPILER_IS_MSVC) && \
  69. _MSC_VER < 1300
  70. # include <pantheios/util/memory/auto_buffer_selector.hpp>
  71. # endif /* VC6 */
  72. # ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER
  73. # include <stlsoft/memory/auto_buffer.hpp>
  74. # endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER */
  75. #endif /* __cplusplus */
  76. /* /////////////////////////////////////////////////////////////////////////
  77. * Namespace
  78. */
  79. #if !defined(PANTHEIOS_NO_NAMESPACE)
  80. namespace pantheios
  81. {
  82. #endif /* !PANTHEIOS_NO_NAMESPACE */
  83. /* /////////////////////////////////////////////////////////////////////////
  84. * API
  85. */
  86. /** Retrieves the host system identifier
  87. *
  88. * \ingroup group__utility
  89. *
  90. * \param buffer Pointer to a character buffer to receive a copy of the host
  91. * name. May not be <code>NULL</code> unless <code>cchBuffer</code> is 0
  92. * \param cchBuffer Number of characters available in <code>buffer</code>.
  93. * Must include the space for a terminating nul.
  94. *
  95. * \return The number of characters copied into the destination buffer, or
  96. * an error indication code.
  97. * \retval 0 The host name was unavailable. (For those builds for which
  98. * exceptions are not supported, this will include out-of-memory
  99. * conditions.)
  100. * \retval cchBuffer There was insufficient space to copy the host name into
  101. * the buffer. The host name is <b>not</b> copied into the buffer.
  102. *
  103. * \remarks This function abstracts the underlying system call(s) of the
  104. * supported operating systems. The somewhat awkward semantics - indicating
  105. * exhaustion by returning the requested size rather than the required
  106. * size - is a result of the limitation of the UNIX
  107. * <code>gethostname()</code> function. Since the function is intended to be
  108. * mostly used, via the C++ wrapper, from the pantheios::hostId inserter
  109. * class, the inconvenience is deemed acceptable.
  110. */
  111. PANTHEIOS_CALL(size_t) pantheios_getHostName(pan_char_t* buffer, size_t cchBuffer);
  112. #if !defined(PANTHEIOS_NO_NAMESPACE)
  113. /** Equivalent to \ref pantheios::pantheios_getHostName "pantheios_getHostName()"
  114. *
  115. * \ingroup group__utility
  116. */
  117. inline size_t getHostName(pan_char_t* buffer, size_t cchBuffer)
  118. {
  119. return pantheios_getHostName(buffer, cchBuffer);
  120. }
  121. #endif /* !PANTHEIOS_NO_NAMESPACE */
  122. #ifdef __cplusplus
  123. /** Retrieves the host system identifier
  124. *
  125. * \ingroup group__utility
  126. *
  127. * \param buffer A mutable (non-const) reference to an instance of a
  128. * specialisation of <code>stlsoft::auto_buffer</code> in which the
  129. * elicited host-name will be written, including terminating
  130. * nul-character
  131. */
  132. #if defined(STLSOFT_COMPILER_IS_MSVC) && \
  133. _MSC_VER < 1300
  134. template <typename B>
  135. inline size_t getHostName(B& buffer)
  136. #else /* ? compiler */
  137. template <size_t N, typename A>
  138. # ifdef STLSOFT_AUTO_BUFFER_NEW_FORM
  139. inline size_t getHostName(stlsoft::auto_buffer<pan_char_t, N, A>& buffer)
  140. # else /* ? STLSOFT_AUTO_BUFFER_NEW_FORM */
  141. inline size_t getHostName(stlsoft::auto_buffer<pan_char_t, A, N>& buffer)
  142. # endif /* STLSOFT_AUTO_BUFFER_NEW_FORM */
  143. #endif /* compiler */
  144. {
  145. PANTHEIOS_CONTRACT_ENFORCE_PRECONDITION_PARAMS_API(0 != buffer.size(), "buffer must not be empty");
  146. for(;;)
  147. {
  148. size_t cch = pantheios_getHostName(&buffer[0], buffer.size());
  149. if(buffer.size() == cch)
  150. {
  151. if(!buffer.resize(2 * buffer.size()))
  152. {
  153. return 0;
  154. }
  155. }
  156. else
  157. {
  158. buffer.resize(cch);
  159. break;
  160. }
  161. }
  162. return buffer.size();
  163. }
  164. #endif /* __cplusplus */
  165. /* /////////////////////////////////////////////////////////////////////////
  166. * Namespace
  167. */
  168. #if !defined(PANTHEIOS_NO_NAMESPACE)
  169. } /* namespace pantheios */
  170. #endif /* !PANTHEIOS_NO_NAMESPACE */
  171. /* ////////////////////////////////////////////////////////////////////// */
  172. #endif /* !PANTHEIOS_INCL_PANTHEIOS_SYSTEM_H_HOSTNAME */
  173. /* ///////////////////////////// end of file //////////////////////////// */