📄 system_traits.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: unixstl/system/system_traits.hpp
*
* Purpose: Contains the system_traits template class, and ANSI and
* Unicode specialisations thereof.
*
* Created: 15th November 2002
* Updated: 12th March 2007
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2002-2007, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file unixstl/system/system_traits.hpp
*
* \brief [C++ only] Definition of the unixstl::system_traits traits
* class
* (\ref group__library__system "System" Library).
*/
#ifndef UNIXSTL_INCL_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS
#define UNIXSTL_INCL_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define UNIXSTL_VER_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS_MAJOR 5
# define UNIXSTL_VER_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS_MINOR 0
# define UNIXSTL_VER_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS_REVISION 2
# define UNIXSTL_VER_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS_EDIT 96
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
# include <unixstl/unixstl.h>
#endif /* !UNIXSTL_INCL_UNIXSTL_H_UNIXSTL */
#ifdef _WIN32
# include <ctype.h>
#endif /* _WIN32 */
#include <errno.h>
#include <fcntl.h>
#ifdef _WIN32
# include <io.h>
# if defined(STLSOFT_COMPILER_IS_INTEL) || \
defined(STLSOFT_COMPILER_IS_MSVC)
# include <direct.h>
# endif /* os && compiler */
#endif /* _WIN32 */
#include <dlfcn.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <sys/types.h>
#include <sys/stat.h>
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _UNIXSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::unixstl */
namespace unixstl
{
# else
/* Define stlsoft::unixstl_project */
namespace stlsoft
{
namespace unixstl_project
{
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_UNIXSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
#ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
/** \brief Traits for accessing the correct system functions for a given
* character type.
*
* \ingroup group__library__system
*
* system_traits is a traits class for determining the correct system
* structures and functions for a given character type.
*
* \param C The character type (e.g. \c char, \c wchar_t)
*/
template <ss_typename_param_k C>
struct system_traits
{
/// \name Types
/// @{
public:
/// \brief The character type
typedef C char_type;
/// \brief The size type
typedef us_size_t size_type;
/// \brief The difference type
typedef us_ptrdiff_t difference_type;
/// \brief The current instantion of the type
typedef system_traits<C> class_type;
/// \brief The (signed) integer type
typedef us_int_t int_type;
/// \brief The Boolean type
typedef us_bool_t bool_type;
/// \brief The type of a handle to a dynamically loaded module
typedef void *module_type;
/// \brief The type of a handle to a kernel object
typedef int handle_type;
/// \brief The type of system error codes
typedef int error_type;
/// @}
/// \name General string handling
/// @{
public:
/// \brief Copies the contents of \c src to \c dest
static char_type *str_copy(char_type *dest, char_type const* src);
/// \brief Copies the contents of \c src to \c dest, up to cch \c characters
static char_type *str_n_copy(char_type *dest, char_type const* src, size_type cch);
/// \brief Appends the contents of \c src to \c dest
static char_type *str_cat(char_type *dest, char_type const* src);
/// \brief Appends the contents of \c src to \c dest, up to cch \c characters
static char_type *str_n_cat(char_type *dest, char_type const* src, size_type cch);
/// \brief Comparies the contents of \c src and \c dest
static int_type str_compare(char_type const* s1, char_type const* s2);
/// \brief Comparies the contents of \c src and \c dest in a case-insensitive fashion
static int_type str_compare_no_case(char_type const* s1, char_type const* s2);
/// \brief Comparies the contents of \c src and \c dest up to \c cch characters
static int_type str_n_compare(char_type const* s1, char_type const* s2, size_type cch);
/// \brief Evaluates the length of \c src
static size_type str_len(char_type const* src);
/// \brief Finds the given character \c ch in \c s
static char_type *str_chr(char_type const* s, char_type ch);
/// \brief Finds the rightmost instance \c ch in \c s
static char_type *str_rchr(char_type const* s, char_type ch);
/// \brief Finds the given substring \c sub in \c s
static char_type *str_str(char_type const* s, char_type const* sub);
/// \brief Finds one of a set of characters in \c s
static char_type *str_pbrk(char_type const* s, char_type const* charSet);
/// \brief Returns a pointer to the end of the string
static char_type *str_end(char_type const* s);
/// @}
/// \name Dynamic Loading
/// @{
public:
/// \brief Loads the given executable module
static module_type load_library(char_type const* name);
/// \brief Closes the given executable module
static bool_type free_library(module_type hModule);
/// \brief Retrieves the given symbol from the library
static void *find_symbol(module_type hModule, char const* symbolName);
/// @}
/// \name Kernel object control
/// @{
public:
/// \brief Closes the given operating system handle
static bool_type close_handle(handle_type h);
/// @}
/// \name Error
/// @{
public:
/// \brief Gives the last error
static error_type get_last_error();
/// \brief Sets the last error
static void set_last_error(error_type er = error_type());
/// @}
/// \name Environment
/// @{
public:
/// \brief Gets an environment variable into the given buffer
///
/// \param name The name of the variable to find
/// \param buffer The buffer in which to write the variable. If this is NULL, then the required length is returned
/// \param cchBuffer The size of the buffer, in characters
static size_type get_environment_variable(char_type const* name, char_type* buffer, size_type cchBuffer);
/// \brief Expands environment strings in \c src into \c buffer, up to a maximum \c cchDest characters
static size_type expand_environment_strings(char_type const* src, char_type* buffer, size_type cchBuffer);
/// @}
};
#else /* ? STLSOFT_DOCUMENTATION_SKIP_SECTION */
template <ss_typename_param_k C>
struct system_traits;
STLSOFT_TEMPLATE_SPECIALISATION
struct system_traits<us_char_a_t>
{
public:
typedef us_char_a_t char_type;
typedef us_size_t size_type;
typedef us_ptrdiff_t difference_type;
typedef system_traits<us_char_a_t> class_type;
typedef us_int_t int_type;
typedef us_bool_t bool_type;
typedef void *module_type;
typedef int handle_type;
typedef int error_type;
public:
static char_type *str_copy(char_type *dest, char_type const* src)
{
return ::strcpy(dest, src);
}
static char_type *str_n_copy(char_type *dest, char_type const* src, size_type cch)
{
return ::strncpy(dest, src, cch);
}
static char_type *str_cat(char_type *dest, char_type const* src)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -