00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00049 #ifndef UNIXSTL_INCL_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS
00050 #define UNIXSTL_INCL_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS
00051 
00052 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00053 # define UNIXSTL_VER_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS_MAJOR     5
00054 # define UNIXSTL_VER_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS_MINOR     2
00055 # define UNIXSTL_VER_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS_REVISION  1
00056 # define UNIXSTL_VER_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS_EDIT      107
00057 #endif 
00058 
00059 
00060 
00061 
00062 
00063 #ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
00064 # include <unixstl/unixstl.h>
00065 #endif 
00066 #ifndef STLSOFT_INCL_STLSOFT_INTERNAL_H_SAFESTR
00067 # include <stlsoft/internal/safestr.h>
00068 #endif 
00069 
00070 #if defined(_WIN32) || \
00071     defined(_WIN64)
00072 # include <ctype.h>
00073 #endif 
00074 #ifndef STLSOFT_INCL_H_ERRNO
00075 # define STLSOFT_INCL_H_ERRNO
00076 # include <errno.h>
00077 #endif 
00078 #ifndef STLSOFT_INCL_H_FCNTL
00079 # define STLSOFT_INCL_H_FCNTL
00080 # include <fcntl.h>
00081 #endif 
00082 #if defined(_WIN32) || \
00083     defined(_WIN64)
00084 # include <io.h>
00085 # if defined(STLSOFT_COMPILER_IS_INTEL) || \
00086      defined(STLSOFT_COMPILER_IS_MSVC)
00087 #  include <direct.h>
00088 # endif 
00089 #endif 
00090 #ifndef STLSOFT_INCL_H_DLFCN
00091 # define STLSOFT_INCL_H_DLFCN
00092 # include <dlfcn.h>
00093 #endif 
00094 #ifndef STLSOFT_INCL_H_LIMITS
00095 # define STLSOFT_INCL_H_LIMITS
00096 # include <limits.h>
00097 #endif 
00098 #ifndef STLSOFT_INCL_H_STDIO
00099 # define STLSOFT_INCL_H_STDIO
00100 # include <stdio.h>
00101 #endif 
00102 #ifndef STLSOFT_INCL_H_STDLIB
00103 # define STLSOFT_INCL_H_STDLIB
00104 # include <stdlib.h>
00105 #endif 
00106 #ifndef STLSOFT_INCL_H_STRING
00107 # define STLSOFT_INCL_H_STRING
00108 # include <string.h>
00109 #endif 
00110 #ifndef STLSOFT_INCL_H_UNISTD
00111 # define STLSOFT_INCL_H_UNISTD
00112 # include <unistd.h>
00113 #endif 
00114 #ifndef STLSOFT_INCL_H_WCHAR
00115 # define STLSOFT_INCL_H_WCHAR
00116 # include <wchar.h>
00117 #endif 
00118 #ifndef STLSOFT_INCL_SYS_H_TYPES
00119 # define STLSOFT_INCL_SYS_H_TYPES
00120 # include <sys/types.h>
00121 #endif 
00122 #ifndef STLSOFT_INCL_SYS_H_STAT
00123 # define STLSOFT_INCL_SYS_H_STAT
00124 # include <sys/stat.h>
00125 #endif 
00126 
00127 
00128 
00129 
00130 
00131 #ifndef _UNIXSTL_NO_NAMESPACE
00132 # if defined(_STLSOFT_NO_NAMESPACE) || \
00133      defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00134 
00135 namespace unixstl
00136 {
00137 # else
00138 
00139 
00140 namespace stlsoft
00141 {
00142 
00143 namespace unixstl_project
00144 {
00145 
00146 # endif 
00147 #endif 
00148 
00149 
00150 
00151 
00152 
00153 #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
00154 
00164 template <ss_typename_param_k C>
00165 struct system_traits
00166 {
00169 public:
00171     typedef C                                       char_type;
00173     typedef us_size_t                               size_type;
00175     typedef us_ptrdiff_t                            difference_type;
00177     typedef system_traits<C>                        class_type;
00179     typedef us_int_t                                int_type;
00181     typedef us_bool_t                               bool_type;
00183     typedef void*                                   module_type;
00185     typedef int                                     handle_type;
00187     typedef int                                     error_type;
00189 
00192 public:
00194     static char_type*   char_copy(char_type* dest, char_type const* src, size_type n);
00195 #if !defined(STLSOFT_USING_SAFE_STR_FUNCTIONS) || \
00196     defined(_CRT_SECURE_NO_DEPRECATE)
00198     static char_type*   str_copy(char_type* dest, char_type const* src);
00200     static char_type*   str_n_copy(char_type* dest, char_type const* src, size_type cch);
00202     static char_type*   str_cat(char_type* dest, char_type const* src);
00204     static char_type*   str_n_cat(char_type* dest, char_type const* src, size_type cch);
00205 #endif 
00207     static int_type     str_compare(char_type const* s1, char_type const* s2);
00209     static int_type     str_compare_no_case(char_type const* s1, char_type const* s2);
00211     static int_type     str_n_compare(char_type const* s1, char_type const* s2, size_type cch);
00213     static size_type    str_len(char_type const* src);
00215     static char_type*   str_chr(char_type const* s, char_type ch);
00217     static char_type*   str_rchr(char_type const* s, char_type ch);
00219     static char_type*   str_str(char_type const* s, char_type const* sub);
00221     static char_type*   str_pbrk(char_type const* s, char_type const* charSet);
00223     static char_type*   str_end(char_type const* s);
00227     static char_type*   str_set(char_type* s, size_type n, char_type c);
00229 
00232 public:
00234     static module_type  load_library(char_type const* name);
00236     static bool_type    free_library(module_type hModule);
00238     static void*        find_symbol(module_type hModule, char const* symbolName);
00240 
00243 public:
00245     static bool_type    close_handle(handle_type h);
00247 
00250 public:
00252     static error_type   get_last_error();
00254     static void         set_last_error(error_type er = error_type());
00256 
00259 public:
00265     static size_type    get_environment_variable(char_type const* name, char_type* buffer, size_type cchBuffer);
00267     static size_type    expand_environment_strings(char_type const* src, char_type* buffer, size_type cchBuffer);
00269 };
00270 
00271 #else 
00272 
00273 template <ss_typename_param_k C>
00274 struct system_traits;
00275 
00276 STLSOFT_TEMPLATE_SPECIALISATION
00277 struct system_traits<us_char_a_t>
00278 {
00279 public:
00280     typedef us_char_a_t                 char_type;
00281     typedef us_size_t                   size_type;
00282     typedef us_ptrdiff_t                difference_type;
00283     typedef system_traits<us_char_a_t>  class_type;
00284     typedef us_int_t                    int_type;
00285     typedef us_bool_t                   bool_type;
00286     typedef void*                       module_type;
00287     typedef int                         handle_type;
00288     typedef int                         error_type;
00289 
00290 public:
00291     static char_type* char_copy(char_type* dest, char_type const* src, size_type n)
00292     {
00293         return static_cast<char_type*>(::memcpy(dest, src, sizeof(char_type) * n));
00294     }
00295 
00296 #if !defined(STLSOFT_USING_SAFE_STR_FUNCTIONS) || \
00297     defined(_CRT_SECURE_NO_DEPRECATE)
00298     static char_type* str_copy(char_type* dest, char_type const* src)
00299     {
00300         return ::strcpy(dest, src);
00301     }
00302 
00303     static char_type* str_n_copy(char_type* dest, char_type const* src, size_type cch)
00304     {
00305         return ::strncpy(dest, src, cch);
00306     }
00307 
00308     static char_type* str_cat(char_type* dest, char_type const* src)
00309     {
00310         return ::strcat(dest, src);
00311     }
00312 
00313     static char_type* str_n_cat(char_type* dest, char_type const* src, size_type cch)
00314     {
00315         return ::strncat(dest, src, cch);
00316     }
00317 #endif 
00318 
00319     static int_type str_compare(char_type const* s1, char_type const* s2)
00320     {
00321         return ::strcmp(s1, s2);
00322     }
00323 
00324     static int_type str_compare_no_case(char_type const* s1, char_type const* s2);
00325 
00326     static int_type str_n_compare(char_type const* s1, char_type const* s2, size_type cch)
00327     {
00328         return ::strncmp(s1, s2, cch);
00329     }
00330 
00331     static size_type str_len(char_type const* src)
00332     {
00333         return static_cast<size_type>(::strlen(src));
00334     }
00335 
00336     static char_type* str_chr(char_type const* s, char_type ch)
00337     {
00338         return const_cast<char_type*>(::strchr(s, ch));
00339     }
00340 
00341     static char_type* str_rchr(char_type const* s, char_type ch)
00342     {
00343         return const_cast<char_type*>(::strrchr(s, ch));
00344     }
00345 
00346     static char_type* str_str(char_type const* s, char_type const* sub)
00347     {
00348         return const_cast<char_type*>(::strstr(s, sub));
00349     }
00350 
00351     static char_type* str_pbrk(char_type const* s, char_type const* charSet)
00352     {
00353         return const_cast<char_type*>(::strpbrk(s, charSet));
00354     }
00355 
00356     static char_type* str_end(char_type const* s)
00357     {
00358         UNIXSTL_ASSERT(NULL != s);
00359 
00360         for(; *s != '\0'; ++s)
00361         {}
00362 
00363         return const_cast<char_type*>(s);
00364     }
00365 
00366     static char_type* str_set(char_type* s, size_type n, char_type c)
00367     {
00368         UNIXSTL_ASSERT(NULL != s || 0u == n);
00369 
00370         for(; 0u != n; --n, ++s)
00371         {
00372             *s = c;
00373         }
00374 
00375         return s;
00376     }
00377 
00378 public:
00379     static module_type load_library(char_type const* name)
00380     {
00381         return ::dlopen(name, RTLD_NOW);
00382     }
00383 
00384     static bool_type free_library(module_type hModule)
00385     {
00386         return 0 == ::dlclose(hModule);
00387     }
00388 
00389     static void* find_symbol(module_type hModule, char const* symbolName)
00390     {
00391         return ::dlsym(hModule, symbolName);
00392     }
00393 
00394 public:
00395     static bool_type close_handle(handle_type h)
00396     {
00397 #if defined(_WIN32) && \
00398     (   defined(STLSOFT_COMPILER_IS_MSVC) || \
00399         defined(STLSOFT_COMPILER_IS_INTEL))
00400         return 0 == ::_close(h);
00401 #else 
00402         return 0 == ::close(h);
00403 #endif 
00404     }
00405 
00406 public:
00407     static error_type get_last_error()
00408     {
00409         return errno;
00410     }
00411 
00412     static void set_last_error(error_type er = error_type())
00413     {
00414         errno = er;
00415     }
00416 
00417 public:
00418 #if (   defined(STLSOFT_COMPILER_IS_MSVC) || \
00419         defined(STLSOFT_COMPILER_IS_INTEL)) && \
00420     defined(STLSOFT_USING_SAFE_STR_FUNCTIONS)
00421 # if _MSC_VER >= 1200
00422 #  pragma warning(push)
00423 # endif 
00424 # pragma warning(disable : 4996)
00425 #endif 
00426 
00427     static size_type get_environment_variable(char_type const* name, char_type* buffer, size_type cchBuffer)
00428     {
00429         char const* var = ::getenv(name);
00430 
00431         if(NULL == var)
00432         {
00433             return 0;
00434         }
00435         else
00436         {
00437             us_size_t var_len = str_len(var);
00438 
00439             if(NULL == buffer)
00440             {
00441                 return var_len;
00442             }
00443             else
00444             {
00445                 size_type writtenLen = (var_len < cchBuffer) ? var_len : cchBuffer;
00446 
00447                 char_copy(buffer, var, writtenLen);
00448                 if(writtenLen < cchBuffer)
00449                 {
00450                     buffer[writtenLen] = '\0';
00451                 }
00452 
00453                 return (var_len < cchBuffer) ? var_len : cchBuffer;
00454             }
00455         }
00456     }
00457 
00458 #if (   defined(STLSOFT_COMPILER_IS_MSVC) || \
00459         defined(STLSOFT_COMPILER_IS_INTEL)) && \
00460     defined(STLSOFT_USING_SAFE_STR_FUNCTIONS)
00461 # if _MSC_VER >= 1200
00462 #  pragma warning(pop)
00463 # else 
00464 #  pragma warning(default : 4996)
00465 # endif 
00466 #endif 
00467 
00468     static size_type expand_environment_strings(char_type const* src, char_type* buffer, size_type cchBuffer);
00469 };
00470 
00471 STLSOFT_TEMPLATE_SPECIALISATION
00472 struct system_traits<us_char_w_t>
00473 {
00474 public:
00475     typedef us_char_w_t                 char_type;
00476     typedef us_size_t                   size_type;
00477     typedef us_ptrdiff_t                difference_type;
00478     typedef system_traits<us_char_a_t>  class_type;
00479     typedef us_int_t                    int_type;
00480     typedef us_bool_t                   bool_type;
00481     typedef void*                       module_type;
00482     typedef int                         handle_type;
00483     typedef int                         error_type;
00484 
00485 public:
00486     static char_type* char_copy(char_type* dest, char_type const* src, size_type n)
00487     {
00488         return static_cast<char_type*>(::memcpy(dest, src, sizeof(char_type) * n));
00489     }
00490 
00491 #if !defined(STLSOFT_USING_SAFE_STR_FUNCTIONS) || \
00492     defined(_CRT_SECURE_NO_DEPRECATE)
00493     static char_type* str_copy(char_type* dest, char_type const* src)
00494     {
00495         return ::wcscpy(dest, src);
00496     }
00497 
00498     static char_type* str_n_copy(char_type* dest, char_type const* src, size_type cch)
00499     {
00500         return ::wcsncpy(dest, src, cch);
00501     }
00502 
00503     static char_type* str_cat(char_type* dest, char_type const* src)
00504     {
00505         return ::wcscat(dest, src);
00506     }
00507 
00508     static char_type* str_n_cat(char_type* dest, char_type const* src, size_type cch)
00509     {
00510         return ::wcsncat(dest, src, cch);
00511     }
00512 #endif 
00513 
00514     static int_type str_compare(char_type const* s1, char_type const* s2)
00515     {
00516         return ::wcscmp(s1, s2);
00517     }
00518 
00519     static int_type str_compare_no_case(char_type const* s1, char_type const* s2);
00520 
00521     static int_type str_n_compare(char_type const* s1, char_type const* s2, size_type cch)
00522     {
00523         return ::wcsncmp(s1, s2, cch);
00524     }
00525 
00526     static size_type str_len(char_type const* src)
00527     {
00528         return static_cast<size_type>(::wcslen(src));
00529     }
00530 
00531     static char_type* str_chr(char_type const* s, char_type ch)
00532     {
00533         return const_cast<char_type*>(::wcschr(s, ch));
00534     }
00535 
00536     static char_type* str_rchr(char_type const* s, char_type ch)
00537     {
00538         return const_cast<char_type*>(::wcsrchr(s, ch));
00539     }
00540 
00541     static char_type* str_str(char_type const* s, char_type const* sub)
00542     {
00543         return const_cast<char_type*>(::wcsstr(s, sub));
00544     }
00545 
00546     static char_type* str_pbrk(char_type const* s, char_type const* charSet)
00547     {
00548         return const_cast<char_type*>(::wcspbrk(s, charSet));
00549     }
00550 
00551     static char_type* str_end(char_type const* s)
00552     {
00553         UNIXSTL_ASSERT(NULL != s);
00554 
00555         for(; *s != L'\0'; ++s)
00556         {}
00557 
00558         return const_cast<char_type*>(s);
00559     }
00560 
00561     static char_type* str_set(char_type* s, size_type n, char_type c)
00562     {
00563         UNIXSTL_ASSERT(NULL != s || 0u == n);
00564 
00565         for(; 0u != n; --n, ++s)
00566         {
00567             *s = c;
00568         }
00569 
00570         return s;
00571     }
00572 
00573 public:
00574     static module_type load_library(char_type const* name);
00575 
00576     static bool_type free_library(module_type hModule)
00577     {
00578         return 0 == ::dlclose(hModule);
00579     }
00580 
00581     static void* find_symbol(module_type hModule, char const* symbolName)
00582     {
00583         return ::dlsym(hModule, symbolName);
00584     }
00585 
00586 public:
00587     static bool_type close_handle(handle_type h)
00588     {
00589 #if defined(_WIN32) && \
00590     defined(STLSOFT_COMPILER_IS_MSVC)
00591         return 0 == ::_close(h);
00592 #else 
00593         return 0 == ::close(h);
00594 #endif 
00595     }
00596 
00597 public:
00598     static error_type get_last_error()
00599     {
00600         return errno;
00601     }
00602 
00603     static void set_last_error(error_type er = error_type())
00604     {
00605         errno = er;
00606     }
00607 
00608 public:
00609     static size_type get_environment_variable(char_type const* name, char_type* buffer, size_type cchBuffer);
00610     static size_type expand_environment_strings(char_type const* src, char_type* buffer, size_type cchBuffer);
00611 };
00612 
00613 #endif 
00614 
00615 
00616 
00617 
00618 
00619 #ifdef STLSOFT_UNITTEST
00620 # include "./unittest/system_traits_unittest_.h"
00621 #endif 
00622 
00623 
00624 
00625 #ifndef _UNIXSTL_NO_NAMESPACE
00626 # if defined(_STLSOFT_NO_NAMESPACE) || \
00627      defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00628 } 
00629 # else
00630 } 
00631 } 
00632 # endif 
00633 #endif 
00634 
00635 
00636 
00637 #endif 
00638 
00639