From c3f277dbc6950154c92dcf47078b2fb41e4c8117 Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Tue, 19 Oct 2004 20:46:55 +0000 Subject: [PATCH] getName() and getHostName return string size --- src/ca/nciu.cpp | 36 ++++++++++++++++++++++++------------ src/ca/nciu.h | 13 +++++++------ src/ca/netiiu.cpp | 23 +++++++++++++++++------ src/ca/netiiu.h | 6 +++--- src/ca/noopiiu.cpp | 6 +++--- src/ca/noopiiu.h | 4 ++-- 6 files changed, 56 insertions(+), 32 deletions(-) diff --git a/src/ca/nciu.cpp b/src/ca/nciu.cpp index 9cf74fdb7..dda1d4201 100644 --- a/src/ca/nciu.cpp +++ b/src/ca/nciu.cpp @@ -236,12 +236,31 @@ bool nciu::searchMsg ( epicsGuard < epicsMutex > & guard ) } const char *nciu::pName ( - epicsGuard < epicsMutex > & guard ) const + epicsGuard < epicsMutex > & guard ) const throw () { guard.assertIdenticalMutex ( this->cacCtx.mutexRef () ); return this->pNameStr; } +unsigned nciu::getName ( + epicsGuard < epicsMutex > &, + char * pBuf, unsigned bufLen ) const throw () +{ + if ( bufLen == 0u ) { + return 0u; + } + if ( this->nameLength < bufLen ) { + strcpy ( pBuf, this->pNameStr ); + return this->nameLength; + } + else { + unsigned reducedSize = bufLen - 1u; + strncpy ( pBuf, this->pNameStr, bufLen ); + pBuf[reducedSize] = '\0'; + return reducedSize; + } +} + unsigned nciu::nameLen ( epicsGuard < epicsMutex > & guard ) const { @@ -386,21 +405,14 @@ void nciu::ioShow ( this->cacCtx.ioShow ( guard, idIn, level ); } -void nciu::hostName ( +unsigned nciu::getHostName ( epicsGuard < epicsMutex > & guard, - char *pBuf, unsigned bufLength ) const + char *pBuf, unsigned bufLength ) const throw () { - this->piiu->hostName ( + return this->piiu->getHostName ( guard, pBuf, bufLength ); } -// deprecated - please do not use, this is _not_ thread safe -const char * nciu::pHostName ( - epicsGuard < epicsMutex > & guard ) const -{ - return this->piiu->pHostName ( guard ); -} - bool nciu::ca_v42_ok ( epicsGuard < epicsMutex > & guard ) const { @@ -477,7 +489,7 @@ void nciu::show ( { if ( this->connected ( guard ) ) { char hostNameTmp [256]; - this->hostName ( guard, hostNameTmp, sizeof ( hostNameTmp ) ); + this->getHostName ( guard, hostNameTmp, sizeof ( hostNameTmp ) ); ::printf ( "Channel \"%s\", connected to server %s", this->pNameStr, hostNameTmp ); if ( level > 1u ) { diff --git a/src/ca/nciu.h b/src/ca/nciu.h index b7a5262ee..adba67457 100644 --- a/src/ca/nciu.h +++ b/src/ca/nciu.h @@ -178,12 +178,16 @@ public: void show ( epicsGuard < epicsMutex > &, unsigned level ) const; + unsigned getName ( + epicsGuard < epicsMutex > &, + char * pBuf, unsigned bufLen ) const throw (); const char * pName ( - epicsGuard < epicsMutex > & ) const; + epicsGuard < epicsMutex > & ) const throw (); unsigned nameLen ( epicsGuard < epicsMutex > & ) const; - const char * pHostName ( - epicsGuard < epicsMutex > & ) const; // deprecated - please do not use + unsigned getHostName ( + epicsGuard < epicsMutex > &, + char * pBuf, unsigned bufLen ) const throw (); void writeException ( epicsGuard < epicsMutex > &, epicsGuard < epicsMutex > &, int status, const char *pContext, unsigned type, arrayElementCount count ); @@ -255,9 +259,6 @@ private: epicsGuard < epicsMutex > & ) const; bool ca_v42_ok ( epicsGuard < epicsMutex > & ) const; - void hostName ( - epicsGuard < epicsMutex > &, - char *pBuf, unsigned bufLength ) const; arrayElementCount nativeElementCount ( epicsGuard < epicsMutex > & ) const; static void stringVerify ( const char *pStr, const unsigned count ); diff --git a/src/ca/netiiu.cpp b/src/ca/netiiu.cpp index d0030f5ce..f8e4883cd 100644 --- a/src/ca/netiiu.cpp +++ b/src/ca/netiiu.cpp @@ -88,20 +88,31 @@ void netiiu::subscriptionUpdateRequest ( { } -void netiiu::hostName ( +static const char * const pHostNameNetIIU = ""; + +unsigned netiiu::getHostName ( epicsGuard < epicsMutex > & guard, - char *pBuf, unsigned bufLength ) const + char * pBuf, unsigned bufLen ) const throw () { - if ( bufLength ) { - strncpy ( pBuf, this->pHostName ( guard ), bufLength ); - pBuf[bufLength - 1u] = '\0'; + if ( bufLen ) { + unsigned len = strlen ( pHostNameNetIIU ); + strncpy ( pBuf, pHostNameNetIIU, bufLen ); + if ( len < bufLen ) { + return len; + } + else { + unsigned reducedSize = bufLen - 1u; + pBuf[reducedSize] = '\0'; + return reducedSize; + } } + return 0u; } const char * netiiu::pHostName ( epicsGuard < epicsMutex > & ) const { - return ""; + return pHostNameNetIIU; } osiSockAddr netiiu::getNetworkAddress ( diff --git a/src/ca/netiiu.h b/src/ca/netiiu.h index 949c81948..286f06058 100644 --- a/src/ca/netiiu.h +++ b/src/ca/netiiu.h @@ -38,11 +38,11 @@ class nciu; class netiiu { public: virtual ~netiiu () = 0; - virtual void hostName ( + virtual unsigned getHostName ( epicsGuard < epicsMutex > &, char * pBuf, - unsigned bufLength ) const = 0; + unsigned bufLength ) const throw () = 0; virtual const char * pHostName ( - epicsGuard < epicsMutex > & ) const = 0; + epicsGuard < epicsMutex > & ) const = 0; virtual bool ca_v41_ok ( epicsGuard < epicsMutex > & ) const = 0; virtual bool ca_v42_ok ( diff --git a/src/ca/noopiiu.cpp b/src/ca/noopiiu.cpp index 133104285..0a7dcd485 100644 --- a/src/ca/noopiiu.cpp +++ b/src/ca/noopiiu.cpp @@ -33,11 +33,11 @@ noopiiu::~noopiiu () { } -void noopiiu::hostName ( +unsigned noopiiu::getHostName ( epicsGuard < epicsMutex > & cacGuard, - char *pBuf, unsigned bufLength ) const + char * pBuf, unsigned bufLength ) const throw () { - netiiu::hostName ( cacGuard, pBuf, bufLength ); + return netiiu::getHostName ( cacGuard, pBuf, bufLength ); } const char * noopiiu::pHostName ( diff --git a/src/ca/noopiiu.h b/src/ca/noopiiu.h index 0d1e616a8..58099b9f9 100644 --- a/src/ca/noopiiu.h +++ b/src/ca/noopiiu.h @@ -30,9 +30,9 @@ class noopiiu : public netiiu { public: ~noopiiu (); - void hostName ( + unsigned getHostName ( epicsGuard < epicsMutex > &, char * pBuf, - unsigned bufLength ) const; + unsigned bufLength ) const throw (); const char * pHostName ( epicsGuard < epicsMutex > & ) const; bool ca_v41_ok (