[Spice-devel] [spice-xpi 2/4] Change SendStr to handle a std::string

Peter Hatina phatina at redhat.com
Wed Apr 4 07:02:38 PDT 2012


Hi,

On 04/03/2012 04:38 PM, Christophe Fergeau wrote:
> All callers were using std::string::c_str(), better to move this
> call to SendStr instead of doing it everywhere.
> ---
>  SpiceXPI/src/plugin/plugin.cpp |   28 ++++++++++++++--------------
>  SpiceXPI/src/plugin/plugin.h   |    2 +-
>  2 files changed, 15 insertions(+), 15 deletions(-)
> 
> diff --git a/SpiceXPI/src/plugin/plugin.cpp b/SpiceXPI/src/plugin/plugin.cpp
> index 04d272b..008688a 100644
> --- a/SpiceXPI/src/plugin/plugin.cpp
> +++ b/SpiceXPI/src/plugin/plugin.cpp
> @@ -570,16 +570,16 @@ void nsPluginInstance::SendBool(uint32_t id, bool value)
>      WriteToPipe(&msg, sizeof(msg));
>  }
>  
> -void nsPluginInstance::SendStr(uint32_t id, const char *str)
> +void nsPluginInstance::SendStr(uint32_t id, std::string str)

Maybe, you could have used:

void nsPluginInstance::SendStr(uint32_t id, std::string &str)

To avoid unnecessary copying. Just a footnote :)

>  {
> -    if (!strlen(str))
> +    if (str.empty())
>          return;
>  
> -    size_t size = sizeof(ControllerData) + strlen(str) + 1;
> +    size_t size = sizeof(ControllerData) + str.size() + 1;
>      ControllerData *msg = static_cast<ControllerData *>(malloc(size));
>      msg->base.id = id;
>      msg->base.size = size;
> -    strcpy(reinterpret_cast<char *>(msg->data), str);
> +    strcpy(reinterpret_cast<char *>(msg->data), str.c_str());
>      WriteToPipe(msg, size);
>      free(msg);
>  }
> @@ -695,26 +695,26 @@ void nsPluginInstance::Connect()
>          }
>  
>          SendInit();
> -        SendStr(CONTROLLER_HOST, m_host_ip.c_str());
> +        SendStr(CONTROLLER_HOST, m_host_ip);
>          SendValue(CONTROLLER_PORT, atoi(m_port.c_str()));
>          SendValue(CONTROLLER_SPORT, atoi(m_secure_port.c_str()));
>          SendValue(CONTROLLER_FULL_SCREEN,
>                     (m_fullscreen == PR_TRUE ? CONTROLLER_SET_FULL_SCREEN : 0) |
>                     (m_admin_console == PR_FALSE ? CONTROLLER_AUTO_DISPLAY_RES : 0));
>          SendBool(CONTROLLER_ENABLE_SMARTCARD, m_smartcard);
> -        SendStr(CONTROLLER_PASSWORD, m_password.c_str());
> -        SendStr(CONTROLLER_TLS_CIPHERS, m_cipher_suite.c_str());
> -        SendStr(CONTROLLER_SET_TITLE, m_title.c_str());
> +        SendStr(CONTROLLER_PASSWORD, m_password);
> +        SendStr(CONTROLLER_TLS_CIPHERS, m_cipher_suite);
> +        SendStr(CONTROLLER_SET_TITLE, m_title);
>          SendBool(CONTROLLER_SEND_CAD, m_send_ctrlaltdel);
>          SendBool(CONTROLLER_ENABLE_USB, m_usb_redir);
>          SendBool(CONTROLLER_ENABLE_USB_AUTOSHARE, m_usb_auto_share);
> -        SendStr(CONTROLLER_USB_FILTER, m_usb_filter.c_str());
> -        SendStr(CONTROLLER_SECURE_CHANNELS, m_ssl_channels.c_str());
> -        SendStr(CONTROLLER_CA_FILE, m_trust_store_file.c_str());
> -        SendStr(CONTROLLER_HOST_SUBJECT, m_host_subject.c_str());
> -        SendStr(CONTROLLER_HOTKEYS, m_hot_keys.c_str());
> +        SendStr(CONTROLLER_USB_FILTER, m_usb_filter);
> +        SendStr(CONTROLLER_SECURE_CHANNELS, m_ssl_channels);
> +        SendStr(CONTROLLER_CA_FILE, m_trust_store_file);
> +        SendStr(CONTROLLER_HOST_SUBJECT, m_host_subject);
> +        SendStr(CONTROLLER_HOTKEYS, m_hot_keys);
>          SendValue(CONTROLLER_COLOR_DEPTH, atoi(m_color_depth.c_str()));
> -        SendStr(CONTROLLER_DISABLE_EFFECTS, m_disable_effects.c_str());
> +        SendStr(CONTROLLER_DISABLE_EFFECTS, m_disable_effects);
>          SendMsg(CONTROLLER_CONNECT);
>          SendMsg(CONTROLLER_SHOW);
>  
> diff --git a/SpiceXPI/src/plugin/plugin.h b/SpiceXPI/src/plugin/plugin.h
> index e34574b..48e3554 100644
> --- a/SpiceXPI/src/plugin/plugin.h
> +++ b/SpiceXPI/src/plugin/plugin.h
> @@ -180,7 +180,7 @@ private:
>      void SendInit();
>      void SendMsg(uint32_t id);
>      void SendValue(uint32_t id, uint32_t value);
> -    void SendStr(uint32_t id, const char *str);
> +    void SendStr(uint32_t id, std::string str);
>      void SendWStr(uint32_t id, const wchar_t *str);
>      void SendBool(uint32_t id, bool value);
>      void CallOnDisconnected(int code);

The rest of the patches look good.

-- 
Peter Hatina
EMEA ENG-Desktop Development
Red Hat Czech, Brno


More information about the Spice-devel mailing list