summaryrefslogtreecommitdiff
path: root/TAO/tao/String_Alloc.cpp
blob: 5e03a6710eda3d8b0bb06ef92dfcf8da38f3f0a0 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// -*- C++ -*-
#include "tao/String_Alloc.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_wchar.h"
#include "ace/OS_Memory.h"
#include <cstring>

#ifndef TAO_NO_SHARED_NULL_CORBA_STRING
  static char null_char[]= "";
  static CORBA::WChar null_wchar[]=
# if defined(ACE_HAS_WCHAR) || defined(ACE_HAS_XPG4_MULTIBYTE_CHAR)
    L"";
# else
    { 0 };
# endif
#endif /* TAO_NO_SHARED_NULL_CORBA_STRING */

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

char *
CORBA::string_dup (const char *str)
{
  if (!str)
    {
      errno = EINVAL;
      return nullptr;
    }

#ifndef TAO_NO_SHARED_NULL_CORBA_STRING
  if (!*str)
    return null_char;
#endif /* TAO_NO_SHARED_NULL_CORBA_STRING */

  size_t const len = std::strlen (str);

  // This allocates an extra byte for the '\0';
  char * copy = CORBA::string_alloc (static_cast<CORBA::ULong> (len));
  if (copy)
    {
      // The memcpy() assumes that the destination is a valid buffer.
      ACE_OS::memcpy (copy, str, len + 1);
    }

  return copy;
}

char *
CORBA::string_alloc (CORBA::ULong len)
{
  // Allocate 1 + strlen to accomodate the null terminating character.
  char *s = nullptr;
  ACE_NEW_RETURN (s,
                  char[size_t (len + 1)],
                  nullptr);
  s[0]= '\0';

  return s;
}

void
CORBA::string_free (char *str)
{
#ifndef TAO_NO_SHARED_NULL_CORBA_STRING
  if (null_char != str)
#endif /* TAO_NO_SHARED_NULL_CORBA_STRING */
  delete [] str;
}

// ****************************************************************

CORBA::WChar*
CORBA::wstring_dup (const WChar *const str)
{
  if (!str)
    {
      errno = EINVAL;
      return nullptr;
    }

#ifndef TAO_NO_SHARED_NULL_CORBA_STRING
  if (!*str)
    return null_wchar;
#endif /* TAO_NO_SHARED_NULL_CORBA_STRING */

  CORBA::WChar* retval =
    CORBA::wstring_alloc (static_cast <CORBA::ULong> (ACE_OS::strlen (str)));
  if (retval == nullptr)
    {
      // The wscpy() below assumes that the destination is a valid buffer.
      return nullptr;
    }

  return ACE_OS::wscpy (retval, str);
}

CORBA::WChar*
CORBA::wstring_alloc (CORBA::ULong len)
{
  CORBA::WChar *s = nullptr;
  ACE_NEW_RETURN (s,
                  CORBA::WChar [(size_t) (len + 1)],
                  nullptr);
  return s;
}

void
CORBA::wstring_free (CORBA::WChar *const str)
{
#ifndef TAO_NO_SHARED_NULL_CORBA_STRING
  if (null_wchar != str)
#endif /* TAO_NO_SHARED_NULL_CORBA_STRING */
  delete [] str;
}

TAO_END_VERSIONED_NAMESPACE_DECL