00001 #include "String_Alloc.h" 00002 00003 #include "ace/OS_NS_string.h" 00004 #include "ace/OS_NS_wchar.h" 00005 #include "ace/OS_Memory.h" 00006 00007 // FUZZ: disable check_for_streams_include 00008 #include "ace/streams.h" 00009 00010 ACE_RCSID (tao, 00011 String_Alloc, 00012 "$Id: String_Alloc.cpp 76935 2007-02-06 19:04:24Z johnnyw $") 00013 00014 TAO_BEGIN_VERSIONED_NAMESPACE_DECL 00015 00016 char * 00017 CORBA::string_dup (const char *str) 00018 { 00019 if (!str) 00020 { 00021 errno = EINVAL; 00022 return 0; 00023 } 00024 00025 size_t const len = ACE_OS::strlen (str); 00026 00027 // This allocates an extra byte for the '\0'; 00028 char * copy = CORBA::string_alloc (static_cast<CORBA::ULong> (len)); 00029 00030 if (copy != 0) 00031 { 00032 // The memcpy() assumes that the destination is a valid buffer. 00033 ACE_OS::memcpy (copy, str, len + 1); 00034 } 00035 00036 return copy; 00037 } 00038 00039 char * 00040 CORBA::string_alloc (CORBA::ULong len) 00041 { 00042 // Allocate 1 + strlen to accomodate the null terminating character. 00043 char *s = 0; 00044 ACE_NEW_RETURN (s, 00045 char[size_t (len + 1)], 00046 0); 00047 00048 s[0]= '\0'; 00049 00050 return s; 00051 } 00052 00053 void 00054 CORBA::string_free (char *str) 00055 { 00056 delete [] str; 00057 } 00058 00059 // **************************************************************** 00060 00061 CORBA::WChar* 00062 CORBA::wstring_dup (const WChar *const str) 00063 { 00064 if (!str) 00065 { 00066 errno = EINVAL; 00067 return 0; 00068 } 00069 00070 CORBA::WChar* retval = 00071 CORBA::wstring_alloc (static_cast <CORBA::ULong> (ACE_OS::strlen (str))); 00072 00073 // The wscpy() below assumes that the destination is a valid buffer. 00074 if (retval == 0) 00075 { 00076 return 0; 00077 } 00078 00079 return ACE_OS::wscpy (retval, str); 00080 } 00081 00082 CORBA::WChar* 00083 CORBA::wstring_alloc (CORBA::ULong len) 00084 { 00085 CORBA::WChar *s = 0; 00086 ACE_NEW_RETURN (s, 00087 CORBA::WChar [(size_t) (len + 1)], 00088 0); 00089 00090 return s; 00091 } 00092 00093 void 00094 CORBA::wstring_free (CORBA::WChar *const str) 00095 { 00096 delete [] str; 00097 } 00098 00099 TAO_END_VERSIONED_NAMESPACE_DECL