diff options
author | irfan <irfan@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1998-05-18 21:54:25 +0000 |
---|---|---|
committer | irfan <irfan@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1998-05-18 21:54:25 +0000 |
commit | eb91ee2d4a8115858376ed2d5aaad65b045743d4 (patch) | |
tree | 5490ae8b79b7a44077e026502b0efc39a999bcca | |
parent | b67e22b92f71397caf1c0e0bb11673700c011bf5 (diff) | |
download | ATCD-eb91ee2d4a8115858376ed2d5aaad65b045743d4.tar.gz |
*** empty log message ***
-rw-r--r-- | ChangeLog-98b | 11 | ||||
-rw-r--r-- | TAO/ChangeLog-98c | 6 | ||||
-rw-r--r-- | TAO/tao/POA.IDL | 19 | ||||
-rw-r--r-- | TAO/tao/POA.cpp | 16 | ||||
-rw-r--r-- | TAO/tests/POA/Explicit_Activation/server.cpp | 27 | ||||
-rw-r--r-- | TAO/tests/POA/Generic_Servant/server.dsp | 15 | ||||
-rw-r--r-- | ace/ACE.cpp | 26 | ||||
-rw-r--r-- | ace/ACE.h | 7 | ||||
-rw-r--r-- | ace/OS.h | 40 | ||||
-rw-r--r-- | ace/OS.i | 97 | ||||
-rw-r--r-- | ace/SString.cpp | 228 | ||||
-rw-r--r-- | ace/SString.h | 13 | ||||
-rw-r--r-- | ace/SString.i | 155 | ||||
-rw-r--r-- | tests/SString_Test.cpp | 18 |
14 files changed, 445 insertions, 233 deletions
diff --git a/ChangeLog-98b b/ChangeLog-98b index 579c66b5b56..fca15a4c12d 100644 --- a/ChangeLog-98b +++ b/ChangeLog-98b @@ -1,3 +1,14 @@ +Mon May 18 16:37:36 1998 Irfan Pyarali <irfan@cs.wustl.edu> + + * ace/SString.h (ACE_CString): Added the ability to hold on to + user supplied character strings without having to make a copy. + + * ace/OS.h (ACE_OS): Added implementations for strrchr, strnchr, + strnstr, and strstr. + + * ace/ACE.cpp (hash_pjw): Added new versions of these that work + with user specified string length. + Mon May 18 12:24:40 1998 Douglas C. Schmidt <schmidt@flamenco.cs.wustl.edu> * ace/config-*.h: Commented out most of the diff --git a/TAO/ChangeLog-98c b/TAO/ChangeLog-98c index 9a1aba0fe30..db27f8d0d56 100644 --- a/TAO/ChangeLog-98c +++ b/TAO/ChangeLog-98c @@ -1,3 +1,9 @@ +Mon May 18 16:46:46 1998 Irfan Pyarali <irfan@cs.wustl.edu> + + * tao/POA.cpp (parse_key): Changed the processing of IORs so that + POA names are not copied. Only a pointer is kept to the correct + place in the IOR. + Mon May 18 12:34:57 1998 Darrell Brunsch <brunsch@cs.wustl.edu> * tao/Connect.cpp: Debug messages now check diff --git a/TAO/tao/POA.IDL b/TAO/tao/POA.IDL index 1bd73a63cba..e127b83eb02 100644 --- a/TAO/tao/POA.IDL +++ b/TAO/tao/POA.IDL @@ -22,8 +22,12 @@ // IDL module CORBA { - // Basic Policy definition + typedef string Identifier; + typedef string RepositoryId; + interface Current{}; + + // Basic Policy definition typedef unsigned long PolicyType; interface Policy @@ -35,16 +39,11 @@ module CORBA typedef sequence <Policy> PolicyList; - interface Current{}; - // .... }; module PortableServer { - typedef string Identifier; - typedef string RepositoryId; - // forward reference interface POA; @@ -188,14 +187,14 @@ module PortableServer Servant preinvoke( in ObjectId oid, in POA adapter, - in Identifier operation, + in CORBA::Identifier operation, out Cookie the_cookie ) raises (ForwardRequest); void postinvoke( in ObjectId oid, in POA adapter, - in Identifier operation, + in CORBA::Identifier operation, in Cookie the_cookie, in Servant the_servant ); }; @@ -324,12 +323,12 @@ module PortableServer // ************************************************** Object create_reference ( - in RepositoryId intf ) + in CORBA::RepositoryId intf ) raises (WrongPolicy); Object create_reference_with_id ( in ObjectId oid, - in RepositoryId intf ) + in CORBA::RepositoryId intf ) raises (WrongPolicy); diff --git a/TAO/tao/POA.cpp b/TAO/tao/POA.cpp index 14325094ddf..6d7d89ee1e1 100644 --- a/TAO/tao/POA.cpp +++ b/TAO/tao/POA.cpp @@ -1130,7 +1130,8 @@ TAO_POA::find_POA (const char *adapter_name, CORBA::Boolean activate_it, CORBA::Environment &env) { - TAO_POA::String name (adapter_name); + // Don't copy string + TAO_POA::String name (adapter_name, 0, 0); TAO_POA *result = this->find_POA (name, activate_it, @@ -2726,7 +2727,8 @@ TAO_POA::parse_key (const TAO_ObjectKey &key, starting_at = TAO_POA::object_key_type_length () + TAO_Creation_Time::creation_time_length (); int how_many = last_token_position - starting_at; poa_name.set ((const char *) &key[starting_at], - how_many); + how_many, + 0); // Take the substring from (last_token_position + separator_length) // to length for the objectId @@ -2918,15 +2920,17 @@ TAO_POA::parse_poa_name (const TAO_POA::String &adapter_name, // If found, take the substring from 0 to token_position int starting_at = 0; int how_many = token_position - starting_at; - topmost_poa_name = adapter_name.substr (starting_at, - how_many); + topmost_poa_name.set (&adapter_name[starting_at], + how_many, + 0); // Take the substring from (token_position + separator_length) // to length starting_at = token_position + TAO_POA::name_separator_length (); how_many = adapter_name.length () - starting_at; - tail_poa_name = adapter_name.substr (starting_at, - how_many); + tail_poa_name.set (&adapter_name[starting_at], + how_many, + 0); } } diff --git a/TAO/tests/POA/Explicit_Activation/server.cpp b/TAO/tests/POA/Explicit_Activation/server.cpp index 696173acb5e..432f077ac67 100644 --- a/TAO/tests/POA/Explicit_Activation/server.cpp +++ b/TAO/tests/POA/Explicit_Activation/server.cpp @@ -93,7 +93,20 @@ main (int argc, char **argv) return -1; } - // Creation of firstPOA is over. Destroy the Policy objects. + // Create the secondPOA under the firstPOA. + name = "secondPOA"; + PortableServer::POA_var second_poa = + first_poa->create_POA (name.c_str (), + poa_manager.in (), + policies, + env); + if (env.exception () != 0) + { + env.print_exception ("PortableServer::POA::create_POA"); + return -1; + } + + // Creation of POAs is over. Destroy the Policy objects. for (CORBA::ULong i = 0; i < policies.length () && env.exception () == 0; ++i) @@ -177,8 +190,8 @@ main (int argc, char **argv) third_oid[5] = (CORBA::Octet) '\0'; CORBA::Object_var third_foo = - first_poa->create_reference_with_id (third_oid.in (), - "IDL:Foo:1.0", env); + second_poa->create_reference_with_id (third_oid.in (), + "IDL:Foo:1.0", env); if (env.exception () != 0) { @@ -219,10 +232,10 @@ main (int argc, char **argv) cout << third_ior.in () << endl; // Activate thirdPOA using its ObjectID. - MyFooServant third_foo_impl (first_poa.in (), 29); - first_poa->activate_object_with_id (third_oid.in (), - &third_foo_impl, - env); + MyFooServant third_foo_impl (second_poa.in (), 29); + second_poa->activate_object_with_id (third_oid.in (), + &third_foo_impl, + env); if (env.exception () != 0) { env.print_exception ("PortableServer::POA::activate_object_with_id"); diff --git a/TAO/tests/POA/Generic_Servant/server.dsp b/TAO/tests/POA/Generic_Servant/server.dsp index 78b16995046..abb9706bd3b 100644 --- a/TAO/tests/POA/Generic_Servant/server.dsp +++ b/TAO/tests/POA/Generic_Servant/server.dsp @@ -54,6 +54,13 @@ LINK32=link.exe # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /dll /machine:I386
# ADD LINK32 ace.lib tao.lib /nologo /subsystem:windows /dll /machine:I386 /libpath:"..\..\..\tao" /libpath:"..\..\..\..\ace"
# SUBTRACT LINK32 /pdb:none
+# Begin Special Build Tool
+SOURCE=$(InputPath)
+PostBuild_Desc=Copying DLL to ..\Explicit_Activation and\
+ ..\On_Demand_Activation
+PostBuild_Cmds=copy server.dll ..\Explicit_Activation copy server.dll\
+ ..\On_Demand_Activation
+# End Special Build Tool
!ELSEIF "$(CFG)" == "server - Win32 Debug"
@@ -84,9 +91,9 @@ LINK32=link.exe # Begin Special Build Tool
SOURCE=$(InputPath)
PostBuild_Desc=Copying DLL to ..\Explicit_Activation and\
- ..\On_Demand_Activation
+ ..\On_Demand_Activation
PostBuild_Cmds=copy server.dll ..\Explicit_Activation copy server.dll\
- ..\On_Demand_Activation
+ ..\On_Demand_Activation
# End Special Build Tool
!ENDIF
@@ -107,7 +114,7 @@ InputName=Foo BuildCmds= \
..\..\..\tao_idl\tao_idl -Wb,export_macro=GENERIC_SERVANT_Export\
- -Wb,export_include=generic_servant_export.h $(InputName).idl
+ -Wb,export_include=generic_servant_export.h $(InputName).idl
"$(InputName)C.cpp" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
$(BuildCmds)
@@ -136,7 +143,7 @@ InputName=Foo BuildCmds= \
..\..\..\tao_idl\tao_idl -Wb,export_macro=GENERIC_SERVANT_Export\
- -Wb,export_include=generic_servant_export.h $(InputName).idl
+ -Wb,export_include=generic_servant_export.h $(InputName).idl
"$(InputName)C.cpp" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
$(BuildCmds)
diff --git a/ace/ACE.cpp b/ace/ACE.cpp index f3310f4e1ab..9849395f268 100644 --- a/ace/ACE.cpp +++ b/ace/ACE.cpp @@ -314,13 +314,14 @@ ACE::execname (const wchar_t *old_name) #endif /* ACE_HAS_UNICODE */ u_long -ACE::hash_pjw (const char *str) +ACE::hash_pjw (const char *str, size_t len) { u_long hash = 0; - for (const char *temp = str; *temp != 0; temp++) + for (size_t i = 0; i < len; i++) { - hash = (hash << 4) + (*temp * 13); + const char temp = str[i]; + hash = (hash << 4) + (temp * 13); u_long g = hash & 0xf0000000; @@ -335,13 +336,20 @@ ACE::hash_pjw (const char *str) } u_long -ACE::hash_pjw (const ACE_USHORT16 *str) +ACE::hash_pjw (const char *str) +{ + return ACE::hash_pjw (str, ACE_OS::strlen (str)); +} + +u_long +ACE::hash_pjw (const ACE_USHORT16 *str, size_t len) { u_long hash = 0; - for (const ACE_USHORT16 *temp = str; *temp != 0; temp++) + for (size_t i = 0; i < len; i++) { - hash = (hash << 4) + (*temp * 13); + const ACE_USHORT16 temp = str[i]; + hash = (hash << 4) + (temp * 13); u_long g = hash & 0xf0000000; @@ -355,6 +363,12 @@ ACE::hash_pjw (const ACE_USHORT16 *str) return hash; } +u_long +ACE::hash_pjw (const ACE_USHORT16 *str) +{ + return ACE::hash_pjw (str, ACE_OS::strlen (str)); +} + // The CRC routine was taken from the FreeBSD implementation of cksum, // that falls under the following license: /*- diff --git a/ace/ACE.h b/ace/ACE.h index f1ec2cca833..de334da7b91 100644 --- a/ace/ACE.h +++ b/ace/ACE.h @@ -506,10 +506,17 @@ public: static u_long hash_pjw (const char *str); // Computes the hash value of <str> using the ``Hash PJW'' routine. + static u_long hash_pjw (const char *str, size_t len); + // Computes the hash value of <str> using the ``Hash PJW'' routine. + static u_long hash_pjw (const ACE_USHORT16 *str); // Computes the hash value of <str> using the ``Hash PJW'' routine // (works for UNICODE strings). + static u_long hash_pjw (const ACE_USHORT16 *str, size_t len); + // Computes the hash value of <str> using the ``Hash PJW'' routine + // (works for UNICODE strings). + static u_long crc32 (const char* str); // Computes the ISO 8802-3 standard 32 bits CRC for the string // (not for a file). @@ -4734,12 +4734,18 @@ public: const char *t); static char *strchr (char *s, int c); - static char *strrchr (char *s, - int c); static const char *strchr (const char *s, int c); + static char *strrchr (char *s, + int c); static const char *strrchr (const char *s, int c); + static char *strnchr (char *s, + int c, + size_t len); + static const char *strnchr (const char *s, + int c, + size_t len); static int strcmp (const char *s, const char *t); static int strncmp (const char *s, @@ -4759,6 +4765,12 @@ public: const char *t); static const char *strstr (const char *s, const char *t); + static char *strnstr (char *s, + const char *t, + size_t len); + static const char *strnstr (const char *s, + const char *t, + size_t len); static char *strdup (const char *s); static size_t strlen (const char *s); static char *strncpy (char *s, @@ -4801,10 +4813,20 @@ public: static wint_t to_lower (wint_t c); static wchar_t *strcat (wchar_t *s, const wchar_t *t); - static wchar_t *strchr (const wchar_t *s, + static wchar_t *strchr (wchar_t *s, wint_t c); - static wchar_t *strrchr (const wchar_t *s, + static const wchar_t *strchr (const wchar_t *s, + wint_t c); + static wchar_t *strrchr (wchar_t *s, wint_t c); + static const wchar_t *strrchr (const wchar_t *s, + wint_t c); + static wchar_t *strnchr (wchar_t *s, + wint_t c, + size_t len); + static const wchar_t *strnchr (const wchar_t *s, + wint_t c, + size_t len); static int strcmp (const wchar_t *s, const wchar_t *t); static int strncmp (const wchar_t *s, @@ -4831,8 +4853,16 @@ public: static int ace_isspace (wchar_t c); # if defined (ACE_WIN32) - static wchar_t *strstr (const wchar_t *s, + static wchar_t *strstr (wchar_t *s, const wchar_t *t); + static const wchar_t *strstr (const wchar_t *s, + const wchar_t *t); + static wchar_t *strnstr (wchar_t *s, + const wchar_t *t, + size_t len); + static const wchar_t *strnstr (const wchar_t *s, + const wchar_t *t, + size_t len); static wchar_t *strdup (const wchar_t *s); static int sprintf (wchar_t *buf, const wchar_t *format, @@ -1121,7 +1121,7 @@ ACE_INLINE size_t ACE_OS::strcspn (const char *s, const char *reject) { #if !defined (ACE_HAS_WINCE) - // ACE_TRACE ("ACE_OS::strstr"); + // ACE_TRACE ("ACE_OS::strcspn"); return ::strcspn (s, reject); #else const char *scan; @@ -1146,7 +1146,7 @@ ACE_INLINE size_t ACE_OS::strspn (const char *s, const char *t) { #if !defined (ACE_HAS_WINCE) - // ACE_TRACE ("ACE_OS::strstr"); + // ACE_TRACE ("ACE_OS::strspn"); return ::strspn (s, t); #else ACE_UNUSED_ARG (s); @@ -1190,6 +1190,22 @@ ACE_OS::strchr (const char *s, int c) } ACE_INLINE const char * +ACE_OS::strnchr (const char *s, int c, size_t len) +{ + for (size_t i = 0; i < len; i++) + if (s[i] == c) + return s + i; + + return 0; +} + +ACE_INLINE char * +ACE_OS::strnchr (char *s, int c, size_t len) +{ + return (char *) ACE_OS::strnchr ((const char *) s, c, len); +} + +ACE_INLINE const char * ACE_OS::strstr (const char *s, const char *t) { // ACE_TRACE ("ACE_OS::strstr"); @@ -1203,6 +1219,23 @@ ACE_OS::strstr (char *s, const char *t) return ::strstr (s, t); } +ACE_INLINE const char * +ACE_OS::strnstr (const char *s, const char *t, size_t len) +{ + size_t t_len = ::strlen (t); + for (size_t i = 0; i < len - t_len; i++) + if (::memcmp (s + i, t, t_len) == 0) + return s + i; + + return 0; +} + +ACE_INLINE char * +ACE_OS::strnstr (char *s, const char *t, size_t len) +{ + return (char *) ACE_OS::strnstr ((const char *) s, t, len); +} + ACE_INLINE char * ACE_OS::strrchr (char *s, int c) { @@ -8533,15 +8566,45 @@ ACE_OS::strcat (wchar_t *s, const wchar_t *t) return ::wcscat (s, t); } -ACE_INLINE wchar_t * +ACE_INLINE const wchar_t * ACE_OS::strchr (const wchar_t *s, wint_t c) { // ACE_TRACE ("ACE_OS::strchr"); + return (const wchar_t *) ::wcschr (s, c); +} + +ACE_INLINE const wchar_t * +ACE_OS::strnchr (const wchar_t *s, wint_t c, size_t len) +{ + for (size_t i = 0; i < len; i++) + if (s[i] == c) + return s + i; + + return 0; +} + +ACE_INLINE const wchar_t * +ACE_OS::strrchr (const wchar_t *s, wint_t c) +{ + // ACE_TRACE ("ACE_OS::strrchr"); + return (const wchar_t *) ::wcsrchr (s, c); +} + +ACE_INLINE wchar_t * +ACE_OS::strchr (wchar_t *s, wint_t c) +{ + // ACE_TRACE ("ACE_OS::strchr"); return ::wcschr (s, c); } ACE_INLINE wchar_t * -ACE_OS::strrchr (const wchar_t *s, wint_t c) +ACE_OS::strnchr (wchar_t *s, wint_t c, size_t len) +{ + return (wchar_t *) ACE_OS::strnchr ((const wchar_t *) s, c, len); +} + +ACE_INLINE wchar_t * +ACE_OS::strrchr (wchar_t *s, wint_t c) { // ACE_TRACE ("ACE_OS::strrchr"); return ::wcsrchr (s, c); @@ -8663,13 +8726,37 @@ ACE_OS::ace_isspace (wchar_t c) #if defined (ACE_WIN32) -ACE_INLINE wchar_t * +ACE_INLINE const wchar_t * ACE_OS::strstr (const wchar_t *s, const wchar_t *t) { // ACE_TRACE ("ACE_OS::strstr"); + return (const wchar_t *) ::wcsstr (s, t); +} + +ACE_INLINE wchar_t * +ACE_OS::strstr (wchar_t *s, const wchar_t *t) +{ + // ACE_TRACE ("ACE_OS::strstr"); return ::wcsstr (s, t); } +ACE_INLINE const wchar_t * +ACE_OS::strnstr (const wchar_t *s, const wchar_t *t, size_t len) +{ + size_t t_len = ACE_OS::strlen (t); + for (size_t i = 0; i < len - t_len; i++) + if (::memcmp (s + i, t, t_len * sizeof (wchar_t)) == 0) + return s + i; + + return 0; +} + +ACE_INLINE wchar_t * +ACE_OS::strnstr (wchar_t *s, const wchar_t *t, size_t len) +{ + return (wchar_t *) ACE_OS::strnstr ((const wchar_t *) s, t, len); +} + ACE_INLINE wchar_t * ACE_OS::strdup (const wchar_t *s) { diff --git a/ace/SString.cpp b/ace/SString.cpp index ab5848bbb5b..4633ced672e 100644 --- a/ace/SString.cpp +++ b/ace/SString.cpp @@ -206,7 +206,8 @@ const int ACE_WString::npos = -1; ostream & operator<< (ostream &os, const ACE_CString &cs) { - os << cs.fast_rep (); + for (size_t i = 0; i < cs.length (); i++) + os << cs[i]; return os; } @@ -218,127 +219,29 @@ operator<< (ostream &os, const ACE_SString &ss) } #endif /* !ACE_HAS_WINCE */ -ACE_WString -operator+ (const ACE_WString &s, const ACE_WString &t) -{ - ACE_WString temp (s); - temp += t; - return temp; -} - -ACE_CString -operator+ (const ACE_CString &s, const ACE_CString &t) -{ - ACE_CString temp (s); - temp += t; - return temp; -} - -// Copy constructor. - -ACE_CString::ACE_CString (const ACE_CString &s) - : allocator_ (s.allocator_), - len_ (s.len_) -{ - ACE_TRACE ("ACE_CString::ACE_CString"); - - if (this->allocator_ == 0) - this->allocator_ = ACE_Allocator::instance (); - - if (s.fast_rep ()[0] == '\0') - this->rep_ = &ACE_CString::NULL_CString_; - else - { - this->rep_ = (char *) this->allocator_->malloc (s.len_ + 1); - ACE_OS::memcpy ((void *) this->rep_, (const void *) s.rep_, this->len_); - this->rep_[this->len_] = '\0'; - } -} - -void -ACE_CString::dump (void) const -{ - ACE_TRACE ("ACE_CString::dump"); -} - -ACE_CString::~ACE_CString (void) -{ - ACE_TRACE ("ACE_CString::~ACE_CString"); - - if (this->rep_ != &ACE_CString::NULL_CString_) - this->allocator_->free (this->rep_); -} - -// Default constructor. - -ACE_CString::ACE_CString (ACE_Allocator *alloc) - : allocator_ (alloc), - len_ (0), - rep_ (&ACE_CString::NULL_CString_) -{ - ACE_TRACE ("ACE_CString::ACE_CString"); - - if (this->allocator_ == 0) - this->allocator_ = ACE_Allocator::instance (); -} - -// Constructor that actually copies memory. - -ACE_CString::ACE_CString (const char *s, ACE_Allocator *alloc) - : allocator_ (alloc) -{ - ACE_TRACE ("ACE_CString::ACE_CString"); - - if (this->allocator_ == 0) - this->allocator_ = ACE_Allocator::instance (); - - if (s == 0 || s[0] == 0') - { - this->len_ = 0; - this->rep_ = &ACE_CString::NULL_CString_; - } - else - { - this->len_ = ACE_OS::strlen (s); - this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1); - ACE_OS::memcpy (this->rep_, s, this->len_); - this->rep_[this->len_] = '\0'; - } -} - -ACE_CString::ACE_CString (char c, ACE_Allocator *alloc) - : allocator_ (alloc) -{ - ACE_TRACE ("ACE_CString::ACE_CString"); - - if (this->allocator_ == 0) - this->allocator_ = ACE_Allocator::instance (); - - this->len_ = 1; - this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1); - this->rep_[0] = c; - this->rep_[this->len_] = '\0'; -} - // Constructor that copies <s> into dynamically allocated memory. // Probable loss of data. Please use with care. -ACE_CString::ACE_CString (const ACE_USHORT16 *s, ACE_Allocator *alloc) - : allocator_ (alloc) +ACE_CString::ACE_CString (const ACE_USHORT16 *s, + ACE_Allocator *alloc) + : allocator_ (alloc ? alloc : ACE_Allocator::instance ()), + len_ (0), + rep_ (0), + release_ (0) { ACE_TRACE ("ACE_CString::ACE_CString"); - if (this->allocator_ == 0) - this->allocator_ = ACE_Allocator::instance (); - if (s == 0 || s[0] == (ACE_USHORT16) '\0') { + this->release_ = 0; this->len_ = 0; this->rep_ = &ACE_CString::NULL_CString_; } else { - this->len_ = ACE_WString::wstrlen (s); + this->release_ = 1; + + this->len_ = ACE_WString::wstrlen (s); this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1); // Copy the ACE_USHORT16 * string byte-by-byte into the char * @@ -349,81 +252,53 @@ ACE_CString::ACE_CString (const ACE_USHORT16 *s, ACE_Allocator *alloc) } } -// Constructor that actually copies memory. - -ACE_CString::ACE_CString (const char *s, - size_t len, - ACE_Allocator *alloc) - : allocator_ (alloc) -{ - ACE_TRACE ("ACE_CString::ACE_CString"); - - if (this->allocator_ == 0) - this->allocator_ = ACE_Allocator::instance (); - - if (s == 0 || s[0] == '\0') - { - this->len_ = 0; - this->rep_ = &ACE_CString::NULL_CString_; - } - else - { - this->len_ = len; - this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1); - ACE_OS::memcpy (this->rep_, s, len); - this->rep_[len] = '\0'; // Make sure to NUL terminate this! - } -} - -// Assignment operator (does copy memory). - -ACE_CString & -ACE_CString::operator= (const ACE_CString &s) -{ - ACE_TRACE ("ACE_CString::operator="); - // Check for identify. - - if (this != &s) - this->set (s.rep_, s.len_); - - return *this; -} - void -ACE_CString::set (const char *s) -{ - this->set (s, ACE_OS::strlen (s)); -} - -void -ACE_CString::set (const char *s, size_t len) +ACE_CString::set (const char *s, + size_t len, + int release) { // // Free memory if necessary // // Going from memory to no memory - if (s[0] == '\0' && - this->rep_ != &ACE_CString::NULL_CString_) + if ((!release || s == 0 || s[0] == '\0') && + this->release_) this->allocator_->free (this->rep_); // Going from memory to more memory else if (this->len_ < len && - this->rep_ != &ACE_CString::NULL_CString_) + this->release_) this->allocator_->free (this->rep_); + + // Figure out future ownership + if (!release || s == 0 || s[0] == '\0') + this->release_ = 0; + else + this->release_ = 1; // - // Allocate memory if necessary + // Allocate memory if owner and necessary // - + // len is greather than 0, so must allocate space for it. - if (this->len_ < len) + if (this->release_ && this->len_ < len) this->rep_ = (char *) this->allocator_->malloc (len + 1); + // set new length this->len_ = len; - if (s[0] == '\0') + // If no string or null string is specified by the user + if (s == 0 || s[0] == '\0') this->rep_ = &ACE_CString::NULL_CString_; + + // If we don't own the string + else if (!this->release_) + { + this->rep_ = (char *) s; + } + + // We own the string else { ACE_OS::memcpy (this->rep_, s, len); @@ -441,18 +316,18 @@ ACE_CString::substring (size_t offset, size_t count = length; // case 1. empty string - if (len_ == 0) + if (this->len_ == 0) return nil; // case 2. start pos l - if (offset >= len_) + if (offset >= this->len_) return nil; // get all remaining bytes if (length == -1) - count = len_ - offset; + count = this->len_ - offset; - return ACE_CString (&rep_[offset], count, this->allocator_); + return ACE_CString (&this->rep_[offset], count, this->allocator_); } // Concat operator (does copy memory). @@ -469,21 +344,20 @@ ACE_CString::operator+= (const ACE_CString &s) this->len_ += s.len_; char *t = 0; - if (oldlen < newlen) - { - // Allocate memory for the new string. - ACE_ALLOCATOR_RETURN (t, - (char *) this->allocator_->malloc (newlen), - *this); - // Copy memory from old string into new string. - ACE_OS::memcpy (t, this->rep_, oldlen); - } + // Allocate memory for the new string. + ACE_ALLOCATOR_RETURN (t, + (char *) this->allocator_->malloc (newlen), + *this); + // Copy memory from old string into new string. + ACE_OS::memcpy (t, this->rep_, oldlen); ACE_OS::memcpy (t + oldlen, s.rep_, s.len_); t[this->len_] = '\0'; - if (this->rep_ != &ACE_CString::NULL_CString_) + if (this->release_) this->allocator_->free (this->rep_); + + this->release_ = 1; this->rep_ = t; } diff --git a/ace/SString.h b/ace/SString.h index ea49879328d..72f1d2fa8ba 100644 --- a/ace/SString.h +++ b/ace/SString.h @@ -51,10 +51,10 @@ public: ACE_CString (ACE_Allocator *alloc = 0); // Default constructor. - ACE_CString (const char *s, ACE_Allocator *alloc = 0); + ACE_CString (const char *s, ACE_Allocator *alloc = 0, int release = 1); // Constructor that copies <s> into dynamically allocated memory. - ACE_CString (const char *s, size_t len, ACE_Allocator *alloc = 0); + ACE_CString (const char *s, size_t len, ACE_Allocator *alloc = 0, int release = 1); // Constructor that copies <len> chars of <s> into dynamically // allocated memory (will NUL terminate the result). @@ -71,7 +71,7 @@ public: ~ACE_CString (void); // Deletes the memory... - char operator[] (size_t index) const; + const char &operator[] (size_t index) const; // Return the <index'th> character in the string (doesn't perform // bounds checking). @@ -82,10 +82,10 @@ public: ACE_CString &operator= (const ACE_CString &); // Assignment operator (does copy memory). - void set (const char *s); + void set (const char *s, int release = 1); // Copy <s> - void set (const char *s, size_t len); + void set (const char *s, size_t len, int release = 1); // Copy <len> bytes of <s> (will NUL terminate the result) ACE_CString substring (size_t offset, ssize_t length = -1) const; @@ -161,6 +161,9 @@ private: char *rep_; // Pointer to data. + int release_; + // Flag that indicates if we own the memory + static char NULL_CString_; // Represents the "NULL" string to simplify the internal logic. }; diff --git a/ace/SString.i b/ace/SString.i index 4625beaecbb..894e489edb2 100644 --- a/ace/SString.i +++ b/ace/SString.i @@ -1,6 +1,144 @@ /* -*- C++ -*- */ // $Id$ +#include "ace/Malloc.h" + +ACE_INLINE ACE_WString +operator+ (const ACE_WString &s, const ACE_WString &t) +{ + ACE_WString temp (s); + temp += t; + return temp; +} + +ACE_INLINE ACE_CString +operator+ (const ACE_CString &s, const ACE_CString &t) +{ + ACE_CString temp (s); + temp += t; + return temp; +} + +// Default constructor. + +ACE_INLINE +ACE_CString::ACE_CString (ACE_Allocator *alloc) + : allocator_ (alloc ? alloc : ACE_Allocator::instance ()), + len_ (0), + rep_ (0), + release_ (0) +{ + ACE_TRACE ("ACE_CString::ACE_CString"); + + this->set (0, 0, 0); +} + +// Constructor that actually copies memory. + +ACE_INLINE +ACE_CString::ACE_CString (const char *s, + ACE_Allocator *alloc, + int release) + : allocator_ (alloc ? alloc : ACE_Allocator::instance ()), + len_ (0), + rep_ (0), + release_ (0) +{ + ACE_TRACE ("ACE_CString::ACE_CString"); + + size_t length; + if (s != 0) + length = ACE_OS::strlen (s); + else + length = 0; + + this->set (s, length, release); +} + +ACE_INLINE +ACE_CString::ACE_CString (char c, + ACE_Allocator *alloc) + : allocator_ (alloc ? alloc : ACE_Allocator::instance ()), + len_ (0), + rep_ (0), + release_ (0) +{ + ACE_TRACE ("ACE_CString::ACE_CString"); + + this->set (&c, 1, 1); +} + +// Constructor that actually copies memory. + +ACE_INLINE +ACE_CString::ACE_CString (const char *s, + size_t len, + ACE_Allocator *alloc, + int release) + : allocator_ (alloc ? alloc : ACE_Allocator::instance ()), + len_ (0), + rep_ (0), + release_ (0) +{ + ACE_TRACE ("ACE_CString::ACE_CString"); + + this->set (s, len, release); +} + +// Copy constructor. + +ACE_INLINE +ACE_CString::ACE_CString (const ACE_CString &s) + : allocator_ (s.allocator_ ? s.allocator_ : ACE_Allocator::instance ()), + len_ (0), + rep_ (0), + release_ (0) +{ + ACE_TRACE ("ACE_CString::ACE_CString"); + + this->set (s.rep_, s.len_, 1); +} + +ACE_INLINE +ACE_CString::~ACE_CString (void) +{ + ACE_TRACE ("ACE_CString::~ACE_CString"); + + this->set (0, 0, 0); +} + +ACE_INLINE void +ACE_CString::dump (void) const +{ + ACE_TRACE ("ACE_CString::dump"); +} + +// Assignment operator (does copy memory). + +ACE_INLINE ACE_CString & +ACE_CString::operator= (const ACE_CString &s) +{ + ACE_TRACE ("ACE_CString::operator="); + + // Check for identify. + if (this != &s) + this->set (s.rep_, s.len_, 1); + + return *this; +} + +ACE_INLINE void +ACE_CString::set (const char *s, int release) +{ + size_t length; + if (s != 0) + length = ACE_OS::strlen (s); + else + length = 0; + + this->set (s, length, release); +} + ACE_INLINE size_t ACE_CString::length (void) const { @@ -17,7 +155,7 @@ ACE_CString::substr (size_t offset, // Return the <index'th> character in the string. -ACE_INLINE char +ACE_INLINE const char & ACE_CString::operator[] (size_t index) const { ACE_TRACE ("ACE_CString::operator[]"); @@ -42,7 +180,8 @@ ACE_CString::rep (void) const char *new_string; ACE_NEW_RETURN (new_string, char[this->len_ + 1], 0); - ACE_OS::strcpy (new_string, this->rep_); + ACE_OS::strncpy (new_string, this->rep_, this->len_); + new_string[this->len_] = '\0'; return new_string; } @@ -67,7 +206,7 @@ ACE_CString::operator== (const ACE_CString &s) const ACE_TRACE ("ACE_CString::operator=="); return this->len_ == s.len_ - && ACE_OS::strcmp (this->rep_, s.rep_) == 0; + && ACE_OS::strncmp (this->rep_, s.rep_, this->len_) == 0; } // Comparison operator. @@ -83,14 +222,14 @@ ACE_INLINE int ACE_CString::compare (const ACE_CString &s) const { ACE_TRACE ("ACE_CString::compare"); - return ACE_OS::strcmp (this->rep_, s.rep_); + return ACE_OS::strncmp (this->rep_, s.rep_, this->len_); } ACE_INLINE int ACE_CString::find (const char *s, int pos) const { char *substr = this->rep_ + pos; - char *pointer = ACE_OS::strstr (substr, s); + char *pointer = ACE_OS::strnstr (substr, s, this->len_ - pos); if (pointer == 0) return ACE_CString::npos; else @@ -101,7 +240,7 @@ ACE_INLINE int ACE_CString::find (char c, int pos) const { char *substr = this->rep_ + pos; - char *pointer = ACE_OS::strchr (substr, c); + char *pointer = ACE_OS::strnchr (substr, c, this->len_ - pos); if (pointer == 0) return ACE_CString::npos; else @@ -138,7 +277,7 @@ ACE_CString::rfind (char c, int pos) const ACE_INLINE u_long ACE_CString::hash (void) const { - return ACE::hash_pjw (this->rep_); + return ACE::hash_pjw (this->rep_, this->len_); } ACE_INLINE @@ -220,7 +359,7 @@ ACE_SString::operator!= (const ACE_SString &s) const ACE_INLINE int ACE_SString::compare (const ACE_SString &s) const { - ACE_TRACE ("ACE_CString::compare"); + ACE_TRACE ("ACE_SString::compare"); return ACE_OS::strcmp (this->rep_, s.rep_); } diff --git a/tests/SString_Test.cpp b/tests/SString_Test.cpp index ccc073085eb..1bac97aa2b7 100644 --- a/tests/SString_Test.cpp +++ b/tests/SString_Test.cpp @@ -44,6 +44,24 @@ main (int, ASYS_TCHAR *[]) } { + ACE_CString s1 ("hello", 0, 0); + ACE_CString s2 ("world", 0, 0); + ACE_CString s3 ("el", 0, 0); + ACE_CString s4 = s1 + " " + s2; + + ACE_ASSERT (s1 != s2); + ACE_ASSERT (s1.strstr (s2) == -1); + ACE_ASSERT (s1.strstr (s3)); + + ACE_ASSERT (s1.find (s3) == 1); + ACE_ASSERT (s1.find (s3, 2) == ACE_CString::npos); + ACE_ASSERT (s1.find (s2) == ACE_CString::npos); + ACE_ASSERT (s1.find ('o') == 4); + ACE_ASSERT (s1.rfind ('l') == 3); + ACE_ASSERT (s1.rfind ('l', 3) == 2); + } + + { ACE_WString s1 ("hello"); ACE_WString s2 ("world"); ACE_WString s3 ("el"); |