summaryrefslogtreecommitdiff
path: root/src/utils/stringutils.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2016-01-16 22:21:30 +0300
committerAndrei Karas <akaras@inbox.ru>2016-01-16 22:34:33 +0300
commit0ad55dae62f1c8295517bf87844368575a813d57 (patch)
tree1611caa884e1b2fa5e0b4071feb436ec0e13d3d5 /src/utils/stringutils.cpp
parent71ef8f1355524d9af8a3c8231e128cbc71365a13 (diff)
downloadmanaplus-0ad55dae62f1c8295517bf87844368575a813d57.tar.gz
manaplus-0ad55dae62f1c8295517bf87844368575a813d57.tar.bz2
manaplus-0ad55dae62f1c8295517bf87844368575a813d57.tar.xz
manaplus-0ad55dae62f1c8295517bf87844368575a813d57.zip
Add function for replace item links in chat.
Diffstat (limited to 'src/utils/stringutils.cpp')
-rw-r--r--src/utils/stringutils.cpp54
1 files changed, 52 insertions, 2 deletions
diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp
index 8b616d2f7..e000b83c7 100644
--- a/src/utils/stringutils.cpp
+++ b/src/utils/stringutils.cpp
@@ -22,6 +22,12 @@
#include "utils/stringutils.h"
+#ifndef DYECMD
+#include "resources/iteminfo.h"
+
+#include "resources/db/itemdb.h"
+#endif
+
#include <algorithm>
#include <sstream>
@@ -204,8 +210,11 @@ size_t findI(std::string text, const StringVect &list)
return std::string::npos;
}
-unsigned int base = 94;
-unsigned int start = 33;
+namespace
+{
+ unsigned int base = 94;
+ unsigned int start = 33;
+} // namespace
const std::string encodeStr(unsigned int value, const unsigned int size)
{
@@ -960,3 +969,44 @@ std::string timeDiffToString(int timeDiff)
}
return str;
}
+
+void replaceItemLinks(std::string &msg)
+{
+#ifndef DYECMD
+ // Check for item link
+ size_t start2 = msg.find('[');
+ size_t sz = msg.size();
+ while (start2 + 1 < sz && start2 != std::string::npos
+ && msg[start2 + 1] != '@')
+ {
+ const size_t end = msg.find(']', start2);
+ if (start2 + 1 != end && end != std::string::npos)
+ {
+ // Catch multiple embeds and ignore them
+ // so it doesn't crash the client.
+ while ((msg.find('[', start2 + 1) != std::string::npos) &&
+ (msg.find('[', start2 + 1) < end))
+ {
+ start2 = msg.find('[', start2 + 1);
+ }
+
+ std::string temp;
+ if (start2 + 1 < sz && end < sz && end > start2 + 1)
+ {
+ temp = msg.substr(start2 + 1, end - start2 - 1);
+
+ const ItemInfo &itemInfo = ItemDB::get(temp);
+ if (itemInfo.getId() != 0)
+ {
+ msg.insert(end, "@@");
+ msg.insert(start2 + 1, "|");
+ msg.insert(start2 + 1, toString(itemInfo.getId()));
+ msg.insert(start2 + 1, "@@");
+ sz = msg.size();
+ }
+ }
+ }
+ start2 = msg.find('[', start2 + 1);
+ }
+#endif
+}