Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/platform: Use std::string directly instead of String typedef #717

Merged
merged 2 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/net/IDataSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "net/ISocket.h"
#include "io/IStream.h"
#include "base/EventTypes.h"
#include <string>

//! Data stream socket interface
/*!
Expand Down
9 changes: 4 additions & 5 deletions src/lib/platform/MSWindowsClipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ MSWindowsClipboard::empty()
}

void
MSWindowsClipboard::add(EFormat format, const String& data)
MSWindowsClipboard::add(EFormat format, const std::string& data)
{
LOG((CLOG_DEBUG "add %d bytes to clipboard format: %d", data.size(), format));

Expand Down Expand Up @@ -165,8 +165,7 @@ MSWindowsClipboard::has(EFormat format) const
return false;
}

String
MSWindowsClipboard::get(EFormat format) const
std::string MSWindowsClipboard::get(EFormat format) const
{
// find the converter for the first clipboard format we can handle
IMSWindowsClipboardConverter* converter = NULL;
Expand All @@ -183,7 +182,7 @@ MSWindowsClipboard::get(EFormat format) const
// if no converter then we don't recognize any formats
if (converter == NULL) {
LOG((CLOG_WARN "no converter for format %d", format));
return String();
return {};
}

// get a handle to the clipboard data
Expand All @@ -192,7 +191,7 @@ MSWindowsClipboard::get(EFormat format) const
// nb: can't cause this using integ tests; this is only caused when
// the selected converter returns an invalid format -- which you
// cannot cause using public functions.
return String();
return {};
}

// convert
Expand Down
14 changes: 7 additions & 7 deletions src/lib/platform/MSWindowsClipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ class MSWindowsClipboard : public IClipboard {

// IClipboard overrides
virtual bool empty();
virtual void add(EFormat, const String& data);
virtual void add(EFormat, const std::string& data);
virtual bool open(Time) const;
virtual void close() const;
virtual Time getTime() const;
virtual bool has(EFormat) const;
virtual String get(EFormat) const;
virtual std::string get(EFormat) const;

void setFacade(IMSWindowsClipboardFacade& facade);

private:
void clearConverters();

UINT convertFormatToWin32(EFormat) const;
HANDLE convertTextToWin32(const String& data) const;
String convertTextFromWin32(HANDLE) const;
UINT convertFormatToWin32(EFormat) const;
HANDLE convertTextToWin32(const std::string& data) const;
std::string convertTextFromWin32(HANDLE) const;

static UINT getOwnershipFormat();

Expand Down Expand Up @@ -105,9 +105,9 @@ class IMSWindowsClipboardConverter : public IInterface {
// the input data must be in the IClipboard format returned by
// getFormat(). the return data will be in the win32 clipboard
// format returned by getWin32Format(), allocated by GlobalAlloc().
virtual HANDLE fromIClipboard(const String&) const = 0;
virtual HANDLE fromIClipboard(const std::string&) const = 0;

// convert from the win32 clipboard format to the IClipboard format
// (i.e., the reverse of fromIClipboard()).
virtual String toIClipboard(HANDLE data) const = 0;
virtual std::string toIClipboard(HANDLE data) const = 0;
};
24 changes: 9 additions & 15 deletions src/lib/platform/MSWindowsClipboardAnyTextConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ MSWindowsClipboardAnyTextConverter::getFormat() const
return IClipboard::kText;
}

HANDLE
MSWindowsClipboardAnyTextConverter::fromIClipboard(const String& data) const
HANDLE MSWindowsClipboardAnyTextConverter::fromIClipboard(const std::string& data) const
{
// convert linefeeds and then convert to desired encoding
String text = doFromIClipboard(convertLinefeedToWin32(data));
std::string text = doFromIClipboard(convertLinefeedToWin32(data));
UInt32 size = (UInt32)text.size();

// copy to memory handle
Expand All @@ -63,18 +62,17 @@ MSWindowsClipboardAnyTextConverter::fromIClipboard(const String& data) const
return gData;
}

String
MSWindowsClipboardAnyTextConverter::toIClipboard(HANDLE data) const
std::string MSWindowsClipboardAnyTextConverter::toIClipboard(HANDLE data) const
{
// get datator
const char* src = (const char*)GlobalLock(data);
UInt32 srcSize = (UInt32)GlobalSize(data);
if (src == NULL || srcSize <= 1) {
return String();
return {};
}

// convert text
String text = doToIClipboard(String(src, srcSize));
std::string text = doToIClipboard(std::string(src, srcSize));

// release handle
GlobalUnlock(data);
Expand All @@ -83,9 +81,7 @@ MSWindowsClipboardAnyTextConverter::toIClipboard(HANDLE data) const
return convertLinefeedToUnix(text);
}

String
MSWindowsClipboardAnyTextConverter::convertLinefeedToWin32(
const String& src) const
std::string MSWindowsClipboardAnyTextConverter::convertLinefeedToWin32(const std::string& src) const
{
// note -- we assume src is a valid UTF-8 string

Expand All @@ -102,7 +98,7 @@ MSWindowsClipboardAnyTextConverter::convertLinefeedToWin32(
}

// allocate new string
String dst;
std::string dst;
dst.reserve(src.size() + numNewlines);

// copy string, converting newlines
Expand All @@ -117,9 +113,7 @@ MSWindowsClipboardAnyTextConverter::convertLinefeedToWin32(
return dst;
}

String
MSWindowsClipboardAnyTextConverter::convertLinefeedToUnix(
const String& src) const
std::string MSWindowsClipboardAnyTextConverter::convertLinefeedToUnix(const std::string& src) const
{
// count newlines in string
UInt32 numNewlines = 0;
Expand All @@ -134,7 +128,7 @@ MSWindowsClipboardAnyTextConverter::convertLinefeedToUnix(
}

// allocate new string
String dst;
std::string dst;
dst.reserve(src.size());

// copy string, converting newlines
Expand Down
14 changes: 7 additions & 7 deletions src/lib/platform/MSWindowsClipboardAnyTextConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ class MSWindowsClipboardAnyTextConverter :
virtual IClipboard::EFormat
getFormat() const;
virtual UINT getWin32Format() const = 0;
virtual HANDLE fromIClipboard(const String&) const;
virtual String toIClipboard(HANDLE) const;
virtual HANDLE fromIClipboard(const std::string&) const;
virtual std::string toIClipboard(HANDLE) const;

protected:
//! Convert from IClipboard format
/*!
Do UTF-8 conversion only. Memory handle allocation and
linefeed conversion is done by this class. doFromIClipboard()
must include the nul terminator in the returned string (not
including the String's nul terminator).
including the std::string's nul terminator).
*/
virtual String doFromIClipboard(const String&) const = 0;
virtual std::string doFromIClipboard(const std::string&) const = 0;

//! Convert to IClipboard format
/*!
Do UTF-8 conversion only. Memory handle allocation and
linefeed conversion is done by this class.
*/
virtual String doToIClipboard(const String&) const = 0;
virtual std::string doToIClipboard(const std::string&) const = 0;

private:
String convertLinefeedToWin32(const String&) const;
String convertLinefeedToUnix(const String&) const;
std::string convertLinefeedToWin32(const std::string&) const;
std::string convertLinefeedToUnix(const std::string&) const;
};
12 changes: 5 additions & 7 deletions src/lib/platform/MSWindowsClipboardBitmapConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ MSWindowsClipboardBitmapConverter::getWin32Format() const
return CF_DIB;
}

HANDLE
MSWindowsClipboardBitmapConverter::fromIClipboard(const String& data) const
HANDLE MSWindowsClipboardBitmapConverter::fromIClipboard(const std::string& data) const
{
// copy to memory handle
HGLOBAL gData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, data.size());
Expand All @@ -67,13 +66,12 @@ MSWindowsClipboardBitmapConverter::fromIClipboard(const String& data) const
return gData;
}

String
MSWindowsClipboardBitmapConverter::toIClipboard(HANDLE data) const
std::string MSWindowsClipboardBitmapConverter::toIClipboard(HANDLE data) const
{
// get datator
LPVOID src = GlobalLock(data);
if (src == NULL) {
return String();
return {};
}
UInt32 srcSize = (UInt32)GlobalSize(data);

Expand All @@ -85,7 +83,7 @@ MSWindowsClipboardBitmapConverter::toIClipboard(HANDLE data) const
bitmap->bmiHeader.biBitCount == 32) &&
bitmap->bmiHeader.biCompression == BI_RGB) {
// already in canonical form
String image(static_cast<char const*>(src), srcSize);
std::string image(static_cast<char const*>(src), srcSize);
GlobalUnlock(data);
return image;
}
Expand Down Expand Up @@ -138,7 +136,7 @@ MSWindowsClipboardBitmapConverter::toIClipboard(HANDLE data) const
GdiFlush();

// extract data
String image((const char*)&info, info.biSize);
std::string image((const char*)&info, info.biSize);
image.append((const char*)raw, 4 * w * h);

// clean up GDI
Expand Down
4 changes: 2 additions & 2 deletions src/lib/platform/MSWindowsClipboardBitmapConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class MSWindowsClipboardBitmapConverter :
virtual IClipboard::EFormat
getFormat() const;
virtual UINT getWin32Format() const;
virtual HANDLE fromIClipboard(const String&) const;
virtual String toIClipboard(HANDLE) const;
virtual HANDLE fromIClipboard(const std::string&) const;
virtual std::string toIClipboard(HANDLE) const;
};
41 changes: 19 additions & 22 deletions src/lib/platform/MSWindowsClipboardHTMLConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ MSWindowsClipboardHTMLConverter::getWin32Format() const
return m_format;
}

String
MSWindowsClipboardHTMLConverter::doFromIClipboard(const String& data) const
std::string MSWindowsClipboardHTMLConverter::doFromIClipboard(const std::string& data) const
{
// prepare to CF_HTML format prefix and suffix
String prefix("Version:0.9\r\nStartHTML:0000000105\r\n"
std::string prefix("Version:0.9\r\nStartHTML:0000000105\r\n"
"EndHTML:ZZZZZZZZZZ\r\n"
"StartFragment:XXXXXXXXXX\r\nEndFragment:YYYYYYYYYY\r\n"
"<!DOCTYPE><HTML><BODY><!--StartFragment-->");
String suffix("<!--EndFragment--></BODY></HTML>\r\n");
std::string suffix("<!--EndFragment--></BODY></HTML>\r\n");

// Get byte offsets for header
UInt32 StartFragment = (UInt32)prefix.size();
Expand All @@ -75,45 +74,43 @@ MSWindowsClipboardHTMLConverter::doFromIClipboard(const String& data) const
return prefix;
}

String
MSWindowsClipboardHTMLConverter::doToIClipboard(const String& data) const
std::string MSWindowsClipboardHTMLConverter::doToIClipboard(const std::string& data) const
{
// get fragment start/end args
String startArg = findArg(data, "StartFragment");
String endArg = findArg(data, "EndFragment");
std::string startArg = findArg(data, "StartFragment");
std::string endArg = findArg(data, "EndFragment");
if (startArg.empty() || endArg.empty()) {
return String();
return std::string();
}

// convert args to integers
SInt32 start = (SInt32)atoi(startArg.c_str());
SInt32 end = (SInt32)atoi(endArg.c_str());
if (start <= 0 || end <= 0 || start >= end) {
return String();
return std::string();
}

// extract the fragment
return data.substr(start, end - start);
}

String
MSWindowsClipboardHTMLConverter::findArg(
const String& data, const String& name) const
std::string MSWindowsClipboardHTMLConverter::findArg(const std::string& data,
const std::string& name) const
{
String::size_type i = data.find(name);
if (i == String::npos) {
return String();
std::string::size_type i = data.find(name);
if (i == std::string::npos) {
return std::string();
}
i = data.find_first_of(":\r\n", i);
if (i == String::npos || data[i] != ':') {
return String();
if (i == std::string::npos || data[i] != ':') {
return std::string();
}
i = data.find_first_of("0123456789\r\n", i + 1);
if (i == String::npos || data[i] == '\r' || data[i] == '\n') {
return String();
if (i == std::string::npos || data[i] == '\r' || data[i] == '\n') {
return std::string();
}
String::size_type j = data.find_first_not_of("0123456789", i);
if (j == String::npos) {
std::string::size_type j = data.find_first_not_of("0123456789", i);
if (j == std::string::npos) {
j = data.size();
}
return data.substr(i, j - i);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/platform/MSWindowsClipboardHTMLConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class MSWindowsClipboardHTMLConverter :

protected:
// MSWindowsClipboardAnyTextConverter overrides
virtual String doFromIClipboard(const String&) const;
virtual String doToIClipboard(const String&) const;
virtual std::string doFromIClipboard(const std::string&) const;
virtual std::string doToIClipboard(const std::string&) const;

private:
String findArg(const String& data, const String& name) const;
std::string findArg(const std::string& data, const std::string& name) const;

private:
UINT m_format;
Expand Down
12 changes: 5 additions & 7 deletions src/lib/platform/MSWindowsClipboardTextConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,18 @@ MSWindowsClipboardTextConverter::getWin32Format() const
return CF_TEXT;
}

String
MSWindowsClipboardTextConverter::doFromIClipboard(const String& data) const
std::string MSWindowsClipboardTextConverter::doFromIClipboard(const std::string& data) const
{
// convert and add nul terminator
return Unicode::UTF8ToText(data) += '\0';
}

String
MSWindowsClipboardTextConverter::doToIClipboard(const String& data) const
std::string MSWindowsClipboardTextConverter::doToIClipboard(const std::string& data) const
{
// convert and truncate at first nul terminator
String dst = Unicode::textToUTF8(data);
String::size_type n = dst.find('\0');
if (n != String::npos) {
std::string dst = Unicode::textToUTF8(data);
std::string::size_type n = dst.find('\0');
if (n != std::string::npos) {
dst.erase(n);
}
return dst;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/platform/MSWindowsClipboardTextConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class MSWindowsClipboardTextConverter :

protected:
// MSWindowsClipboardAnyTextConverter overrides
virtual String doFromIClipboard(const String&) const;
virtual String doToIClipboard(const String&) const;
virtual std::string doFromIClipboard(const std::string&) const;
virtual std::string doToIClipboard(const std::string&) const;
};
Loading