summaryrefslogtreecommitdiff
path: root/ace/OS_NS_stdlib_base.inl
blob: 5ee64c034217e00de5792f686dff5a5f4533db5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* -*- C++ -*- */
// $Id$

// Standard library includes for wide functions declared in ace_wchar.h

#ifndef OS_NS_STDLIB_BASE_INL
#define OS_NS_STDLIB_BASE_INL

#include "OS_NS_string_base.h"

namespace ACE_OS
{

template <typename DCHAR, typename SCHAR> inline
size_t string_copy (DCHAR* dest, const SCHAR* src, size_t len)
{
  // We must have a source and a valid length with a dest
  if ( src == 0 || ( dest != 0 && static_cast<signed int>(len) < 0 ) )
  {
    return -1;
  }
  // If we perform a copy
  if ( dest != 0 )
  {
    if (len != 0)
    {
      DCHAR* d = dest;
      const SCHAR* s = src;
      do
      {
        if ((*d++ = static_cast<DCHAR>(*s++)) == 0)
        {
          // NUL pad the remaining n-1 bytes
          while (--len != 0)
            *d++ = 0;
          break;
        }
      } while (--len != 0);
      return static_cast<size_t>( d - dest );
    }
    return 0;
  }
  // Otherwise just calc length
  return ACE_OS::strlen( src );
}

#if !defined(ACE_LACKS_WCSTOMBS)
template <> inline
size_t string_copy (char* s, const wchar_t* t, size_t len)
{
#if defined(ACE_WIN32)
   // When VC6 support is dropped use wcstombs
  if ( t == 0 )
    return -1;
  int size = ::WideCharToMultiByte(
              CP_OEMCP, 0,
              t, -1,
              s, (s ? len : 0),
              0, 0 );
  // wcstombs returns len NOT size when dest == 0
  return ( s == 0 ? size - 1 : size );
#else
  return ::wcstombs( s, t, len );
#endif
}
#endif /* !ACE_LACKS_WCSTOMBS */

#if !defined(ACE_LACKS_MBSTOWCS)
template <> inline
size_t string_copy (wchar_t* s, const char* t, size_t len)
{
#if defined(ACE_WIN32)
   // When VC6 support is dropped use mbstowcs
  if ( t == 0 )
    return -1;
  int size = ::MultiByteToWideChar(
                CP_OEMCP, 0,
                t, -1,
                s, (s ? len : 0) );
  // mbstowcs returns len NOT size when dest == 0
  return ( s == 0 ? size - 1 : size );
#else
  return ::mbstowcs( s, t, len );
#endif
}
#endif

}

#endif