Warning, file /sdk/dferry/transport/stringtools.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002    Copyright (C) 2013 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #include "stringtools.h"
0025 
0026 #include <sstream>
0027 
0028 std::vector<std::string> split(const std::string &s, char delimiter, bool keepEmptyParts)
0029 {
0030     std::vector<std::string> ret;
0031     std::stringstream ss(s);
0032     std::string part;
0033     while (getline(ss, part, delimiter)) {
0034         if (keepEmptyParts || !part.empty()) {
0035             ret.push_back(part);
0036         }
0037     }
0038     return ret;
0039 }
0040 
0041 #ifndef DFERRY_SERDES_ONLY
0042 #include "sha1.c"
0043 
0044 std::string hexEncode(const std::string &s)
0045 {
0046     std::stringstream ss;
0047     for (size_t i = 0; i < s.length(); i++) {
0048         const byte b = static_cast<byte>(s[i]);
0049         ss << std::hex << uint(b >> 4) << uint(b & 0xf);
0050     }
0051     return ss.str();
0052 }
0053 
0054 std::string sha1Hex(const std::string &s)
0055 {
0056     sha1nfo sha;
0057     sha1_init(&sha);
0058     sha1_write(&sha, s.c_str(), s.length());
0059     // SHA-1 produces a 160 bits result, which is 20 bytes
0060     const std::string shaResult(reinterpret_cast<char *>(sha1_result(&sha)), 20);
0061     return hexEncode(shaResult);
0062 }
0063 #endif