[Libreoffice-commits] online.git: common/Protocol.cpp common/Protocol.hpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Fri Feb 3 07:07:55 UTC 2017


 common/Protocol.cpp |  109 +++++++++++++++++++++++-----------------------------
 common/Protocol.hpp |    9 +++-
 2 files changed, 57 insertions(+), 61 deletions(-)

New commits:
commit cd85d8a7f13f21e69ca17f792079dd96fa3c8325
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Mon Jan 30 20:43:27 2017 -0500

    wsd: improved protocol helpers
    
    Change-Id: I434fd9d6bb3248a8b9c27bb6c8565369c95786b5
    Reviewed-on: https://gerrit.libreoffice.org/33860
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/common/Protocol.cpp b/common/Protocol.cpp
index a6a24b9..030517c 100644
--- a/common/Protocol.cpp
+++ b/common/Protocol.cpp
@@ -74,85 +74,67 @@ namespace LOOLProtocol
 
     bool getTokenInteger(const std::string& token, const std::string& name, int& value)
     {
-        size_t nextIdx;
-        try
-        {
-            if (token.size() < name.size() + 2 ||
-                token.substr(0, name.size()) != name ||
-                token[name.size()] != '=' ||
-                (value = std::stoi(token.substr(name.size() + 1), &nextIdx), false) ||
-                nextIdx != token.size() - name.size() - 1)
-            {
-                return false;
-            }
-        }
-        catch (std::invalid_argument&)
+        if (token.size() > (name.size() + 1) &&
+            token.compare(0, name.size(), name) == 0 &&
+            token[name.size()] == '=')
         {
-            return false;
+            const char* str = token.data() + name.size() + 1;
+            char* endptr = nullptr;
+            value = strtol(str, &endptr, 10);
+            return (endptr > str);
         }
 
-        return true;
+        return false;
     }
 
     bool getTokenUInt64(const std::string& token, const std::string& name, uint64_t& value)
     {
-        size_t nextIdx;
-        try
-        {
-            if (token.size() < name.size() + 2 ||
-                token.substr(0, name.size()) != name ||
-                token[name.size()] != '=' ||
-                (value = std::stoull(token.substr(name.size() + 1), &nextIdx), false) ||
-                nextIdx != token.size() - name.size() - 1)
-            {
-                return false;
-            }
-        }
-        catch (std::invalid_argument&)
+        if (token.size() > (name.size() + 1) &&
+            token.compare(0, name.size(), name) == 0 &&
+            token[name.size()] == '=')
         {
-            return false;
+            const char* str = token.data() + name.size() + 1;
+            char* endptr = nullptr;
+            value = strtoull(str, &endptr, 10);
+            return (endptr > str);
         }
 
-        return true;
+        return false;
     }
 
     bool getTokenString(const std::string& token, const std::string& name, std::string& value)
     {
-        try
+        if (token.size() > (name.size() + 1) &&
+            token.compare(0, name.size(), name) == 0 &&
+            token[name.size()] == '=')
         {
-            if (token.size() < name.size() + 2 ||
-                token.substr(0, name.size()) != name ||
-                token[name.size()] != '=')
-            {
-                return false;
-            }
-        }
-        catch (std::invalid_argument&)
-        {
-            return false;
+            value = token.substr(name.size() + 1);
+            return true;
         }
 
-        value = token.substr(name.size() + 1);
-        return true;
+        return false;
     }
 
-    bool getTokenKeyword(const std::string& token, const std::string& name, const std::map<std::string, int>& map, int& value)
+    bool getTokenKeyword(const std::string& token, const std::string& name,
+                         const std::map<std::string, int>& map, int& value)
     {
-        if (token.size() < name.size() + 2 ||
-            token.substr(0, name.size()) != name ||
-            token[name.size()] != '=')
-            return false;
-
-        std::string t = token.substr(name.size()+1);
-        if (t[0] == '\'' && t[t.size()-1] == '\'')
-            t = t.substr(1, t.size()-2);
+        std::string t;
+        if (getTokenString(token, name, t))
+        {
+            if (t[0] == '\'' && t[t.size() - 1] == '\'')
+            {
+                t = t.substr(1, t.size() - 2);
+            }
 
-        auto p = map.find(t);
-        if (p == map.cend())
-            return false;
+            const auto p = map.find(t);
+            if (p != map.cend())
+            {
+                value = p->second;
+                return true;
+            }
+        }
 
-        value = p->second;
-        return true;
+        return false;
     }
 
     bool getTokenInteger(const Poco::StringTokenizer& tokens, const std::string& name, int& value)
@@ -185,10 +167,17 @@ namespace LOOLProtocol
         return false;
     }
 
-    bool getTokenIntegerFromMessage(const std::string& message, const std::string& name, int& value)
+    bool getTokenInteger(const std::vector<std::string>& tokens, const std::string& name, int& value)
     {
-        Poco::StringTokenizer tokens(message, " \n", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
-        return getTokenInteger(tokens, name, value);
+        for (const auto& pair : tokens)
+        {
+            if (getTokenInteger(pair, name, value))
+            {
+                return true;
+            }
+        }
+
+        return false;
     }
 
     bool getTokenStringFromMessage(const std::string& message, const std::string& name, std::string& value)
diff --git a/common/Protocol.hpp b/common/Protocol.hpp
index 8188ca0..c404874 100644
--- a/common/Protocol.hpp
+++ b/common/Protocol.hpp
@@ -73,6 +73,9 @@ namespace LOOLProtocol
     bool getTokenInteger(const Poco::StringTokenizer& tokens, const std::string& name, int& value);
     bool getTokenString(const Poco::StringTokenizer& tokens, const std::string& name, std::string& value);
     bool getTokenKeyword(const Poco::StringTokenizer& tokens, const std::string& name, const std::map<std::string, int>& map, int& value);
+
+    bool getTokenInteger(const std::vector<std::string>& tokens, const std::string& name, int& value);
+
     inline bool getTokenString(const std::vector<std::string>& tokens,
                                const std::string& name,
                                std::string& value)
@@ -88,7 +91,6 @@ namespace LOOLProtocol
         return false;
     }
 
-    bool getTokenIntegerFromMessage(const std::string& message, const std::string& name, int& value);
     bool getTokenStringFromMessage(const std::string& message, const std::string& name, std::string& value);
     bool getTokenKeywordFromMessage(const std::string& message, const std::string& name, const std::map<std::string, int>& map, int& value);
 
@@ -135,6 +137,11 @@ namespace LOOLProtocol
         return tokenize(s.data(), s.size());
     }
 
+    inline bool getTokenIntegerFromMessage(const std::string& message, const std::string& name, int& value)
+    {
+        return getTokenInteger(tokenize(message), name, value);
+    }
+
     inline size_t getDelimiterPosition(const char* message, const int length, const char delim)
     {
         if (message && length > 0)


More information about the Libreoffice-commits mailing list