summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/copynpaste.cpp87
-rw-r--r--src/utils/copynpaste.h1
-rw-r--r--src/utils/sha256.cpp4
-rw-r--r--src/utils/stringutils.cpp33
-rw-r--r--src/utils/stringutils.h3
5 files changed, 119 insertions, 9 deletions
diff --git a/src/utils/copynpaste.cpp b/src/utils/copynpaste.cpp
index 5b1ccb5bc..6d1c675d3 100644
--- a/src/utils/copynpaste.cpp
+++ b/src/utils/copynpaste.cpp
@@ -1,6 +1,7 @@
/*
* Retrieve string pasted depending on OS mechanisms.
* Copyright (C) 2001-2010 Wormux Team
+ * Copyright (C) 2011 ManaPlus Developers
*
* This file is part of The ManaPlus Client.
*
@@ -90,6 +91,40 @@ bool retrieveBuffer(std::string& text, std::string::size_type& pos)
CloseClipboard();
return ret;
}
+
+bool sendBuffer(std::string& text)
+{
+ int wCharsLen = MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, NULL, 0);
+ if (!wCharsLen)
+ return false;
+
+ HANDLE h;
+ WCHAR *out;
+ h = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, wCharsLen * sizeof(WCHAR));
+ out = (WCHAR*)GlobalLock(h);
+
+ MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, out, wCharsLen);
+
+ if (!OpenClipboard(0))
+ {
+ GlobalUnlock(h);
+ GlobalFree(h);
+ return false;
+ }
+ GlobalUnlock(h);
+ EmptyClipboard();
+ if (!SetClipboardData(CF_UNICODETEXT, out))
+ {
+ GlobalFree(h);
+ CloseClipboard();
+ return false;
+ }
+ GlobalFree(h);
+ CloseClipboard();
+
+ return true;
+}
+
#elif defined(__APPLE__)
#ifdef Status
@@ -243,6 +278,11 @@ bool retrieveBuffer(std::string& text, std::string::size_type& pos)
}
}
+bool sendBuffer(std::string& text)
+{
+ return false;
+}
+
#elif USE_X11
static char* getSelection2(Display *dpy, Window us, Atom selection,
Atom request_target)
@@ -362,9 +402,56 @@ bool retrieveBuffer(std::string& text, std::string::size_type& pos)
}
return false;
}
+
+bool sendBuffer(std::string& text)
+{
+ pid_t pid;
+ int fd[2];
+
+ if (pipe(fd))
+ return false;
+
+ if ((pid = fork()) == -1)
+ { // fork error
+ return false;
+ }
+ else if (!pid)
+ { // child
+ close(fd[1]);
+
+ if (fd[0] != STDIN_FILENO)
+ {
+ if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO)
+ {
+ close(fd[0]);
+ exit(1);
+ }
+ close(fd[0]);
+ }
+ execl("/usr/bin/xsel", "xsel", "-i", (char *)0);
+ exit(1);
+ }
+
+ // parent
+ close(fd[0]);
+ const int len = strlen(text.c_str());
+ if (write(fd[1], text.c_str(), len) != len)
+ {
+ close(fd[1]);
+ return false;
+ }
+ close(fd[1]);
+ return true;
+}
+
#else
bool retrieveBuffer(std::string&, std::string::size_type&)
{
return false;
}
+
+bool sendBuffer(std::string& text)
+{
+ return false;
+}
#endif
diff --git a/src/utils/copynpaste.h b/src/utils/copynpaste.h
index d09105139..03596d5d1 100644
--- a/src/utils/copynpaste.h
+++ b/src/utils/copynpaste.h
@@ -31,3 +31,4 @@
*/
bool retrieveBuffer(std::string& text, std::string::size_type& pos);
+bool sendBuffer(std::string& text);
diff --git a/src/utils/sha256.cpp b/src/utils/sha256.cpp
index dab01a1ea..1e7143300 100644
--- a/src/utils/sha256.cpp
+++ b/src/utils/sha256.cpp
@@ -254,7 +254,7 @@ void SHA256Update(SHA256Context *ctx,
SHA256Transform(ctx, ctx->block, 1);
SHA256Transform(ctx, shifted_message, block_nb);
rem_len = new_len % SHA256_BLOCK_SIZE;
- memcpy(ctx->block, &shifted_message[block_nb << 6],rem_len);
+ memcpy(ctx->block, &shifted_message[block_nb << 6], rem_len);
ctx->len = rem_len;
ctx->tot_len += (block_nb + 1) << 6;
}
@@ -283,7 +283,7 @@ std::string SHA256Hash(const char *src, int len)
SHA256Final(&ctx, bytehash);
// Convert it to hex
const char* hxc = "0123456789abcdef";
- std::string hash = "";
+ std::string hash("");
for (int i = 0; i < SHA256_DIGEST_SIZE; i++)
{
hash += hxc[bytehash[i] / 16];
diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp
index 176c5499c..6c50d4019 100644
--- a/src/utils/stringutils.cpp
+++ b/src/utils/stringutils.cpp
@@ -174,6 +174,24 @@ const std::string findSameSubstring(const std::string &str1,
return str1.substr(0, minLength);
}
+const std::string findSameSubstringI(const std::string &s1,
+ const std::string &s2)
+{
+ std::string str1 = s1;
+ std::string str2 = s2;
+ toLower(str1);
+ toLower(str2);
+
+ int minLength = str1.length() > str2.length()
+ ? static_cast<int>(str2.length()) : static_cast<int>(str1.length());
+ for (int f = 0; f < minLength; f ++)
+ {
+ if (str1.at(f) != str2.at(f))
+ return s1.substr(0, f);
+ }
+ return s1.substr(0, minLength);
+}
+
unsigned long findI(std::string str, std::string subStr)
{
str = toLower(str);
@@ -185,10 +203,11 @@ unsigned long findI(std::string text, std::vector<std::string> &list)
{
std::string str = toLower(text);
unsigned long idx;
- for (std::vector<std::string>::iterator i = list.begin();
+ for (std::vector<std::string>::const_iterator i = list.begin();
i != list.end(); ++i)
{
- std::string subStr = toLower(*i);
+ std::string subStr = *i;
+ subStr = toLower(subStr);
idx = str.find(subStr);
if (idx != std::string::npos)
return idx;
@@ -446,12 +465,12 @@ std::string combineDye2(std::string file, std::string dye)
if (pos != std::string::npos)
{
std::string dye1 = file.substr(pos + 1);
- std::string str = "";
+ std::string str("");
file = file.substr(0, pos);
std::list<std::string> list1 = splitToStringList(dye1, ';');
std::list<std::string> list2 = splitToStringList(dye, ';');
- std::list<std::string>::iterator it1, it1_end = list1.end();
- std::list<std::string>::iterator it2, it2_end = list2.end();
+ std::list<std::string>::const_iterator it1, it1_end = list1.end();
+ std::list<std::string>::const_iterator it2, it2_end = list2.end();
for (it1 = list1.begin(), it2 = list2.begin();
it1 != it1_end && it2 != it2_end; ++it1, ++it2)
{
@@ -485,8 +504,8 @@ std::vector<std::string> getLang()
std::string packList(std::list<std::string> &list)
{
- std::list<std::string>::iterator i = list.begin();
- std::string str = "";
+ std::list<std::string>::const_iterator i = list.begin();
+ std::string str("");
while (i != list.end())
{
str = str + (*i) + "|";
diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h
index 25fe11e2d..273fa0c8c 100644
--- a/src/utils/stringutils.h
+++ b/src/utils/stringutils.h
@@ -116,6 +116,9 @@ std::string removeColors(std::string msg);
const std::string findSameSubstring(const std::string &str1,
const std::string &str2);
+const std::string findSameSubstringI(const std::string &str1,
+ const std::string &str2);
+
/**
* Compares the two strings case-insensitively.
*