Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2013-2017, Matt Godbolt |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // Redistributions of source code must retain the above copyright notice, this |
| 8 | // list of conditions and the following disclaimer. |
| 9 | // |
| 10 | // Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | // POSSIBILITY OF SUCH DAMAGE. |
| 25 | |
| 26 | #include "seasocks/StringUtil.h" |
| 27 | |
| 28 | #include <cctype> |
| 29 | #include <cerrno> |
| 30 | #include <cstddef> |
| 31 | #include <cstdio> |
| 32 | #include <cstring> |
| 33 | |
| 34 | namespace seasocks { |
| 35 | |
| 36 | char* skipWhitespace(char* str) { |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 37 | while (isspace(*str)) |
| 38 | ++str; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 39 | return str; |
| 40 | } |
| 41 | |
| 42 | char* skipNonWhitespace(char* str) { |
| 43 | while (*str && !isspace(*str)) { |
| 44 | ++str; |
| 45 | } |
| 46 | return str; |
| 47 | } |
| 48 | |
| 49 | char* shift(char*& str) { |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 50 | if (str == nullptr) { |
| 51 | return nullptr; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 52 | } |
| 53 | char* startOfWord = skipWhitespace(str); |
| 54 | if (*startOfWord == 0) { |
| 55 | str = startOfWord; |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 56 | return nullptr; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 57 | } |
| 58 | char* endOfWord = skipNonWhitespace(startOfWord); |
| 59 | if (*endOfWord != 0) { |
| 60 | *endOfWord++ = 0; |
| 61 | } |
| 62 | str = endOfWord; |
| 63 | return startOfWord; |
| 64 | } |
| 65 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 66 | std::string trimWhitespace(const std::string& str) { |
| 67 | auto* start = str.c_str(); |
| 68 | while (isspace(*start)) |
| 69 | ++start; |
| 70 | auto* end = &str.back(); |
| 71 | while (end >= start && isspace(*end)) |
| 72 | --end; |
| 73 | return std::string(start, end - start + 1); |
| 74 | } |
| 75 | |
| 76 | std::string getLastError() { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 77 | char errbuf[1024]; |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 78 | const auto ignore = strerror_r(errno, errbuf, sizeof(errbuf)); |
| 79 | static_cast<void>(ignore); |
| 80 | return errbuf; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | std::string formatAddress(const sockaddr_in& address) { |
| 84 | char ipBuffer[24]; |
| 85 | sprintf(ipBuffer, |
| 86 | "%d.%d.%d.%d:%d", |
| 87 | (address.sin_addr.s_addr >> 0) & 0xff, |
| 88 | (address.sin_addr.s_addr >> 8) & 0xff, |
| 89 | (address.sin_addr.s_addr >> 16) & 0xff, |
| 90 | (address.sin_addr.s_addr >> 24) & 0xff, |
| 91 | htons(address.sin_port)); |
| 92 | return ipBuffer; |
| 93 | } |
| 94 | |
| 95 | std::vector<std::string> split(const std::string& input, char splitChar) { |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 96 | if (input.empty()) |
| 97 | return std::vector<std::string>(); |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 98 | std::vector<std::string> result; |
| 99 | size_t pos = 0; |
| 100 | size_t newPos; |
| 101 | while ((newPos = input.find(splitChar, pos)) != std::string::npos) { |
| 102 | result.push_back(input.substr(pos, newPos - pos)); |
| 103 | pos = newPos + 1; |
| 104 | } |
| 105 | result.push_back(input.substr(pos)); |
| 106 | return result; |
| 107 | } |
| 108 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 109 | void replace(std::string& string, const std::string& find, |
| 110 | const std::string& replace) { |
| 111 | if (find.empty()) { |
| 112 | return; |
| 113 | } |
| 114 | |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 115 | size_t pos = 0; |
| 116 | const size_t findLen = find.length(); |
| 117 | const size_t replaceLen = replace.length(); |
| 118 | while ((pos = string.find(find, pos)) != std::string::npos) { |
| 119 | string = string.substr(0, pos) + replace + string.substr(pos + findLen); |
| 120 | pos += replaceLen; |
| 121 | } |
| 122 | } |
| 123 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 124 | bool caseInsensitiveSame(const std::string& lhs, const std::string& rhs) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 125 | return strcasecmp(lhs.c_str(), rhs.c_str()) == 0; |
| 126 | } |
| 127 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 128 | std::string webtime(time_t time) { |
| 129 | struct tm timeValue; |
| 130 | gmtime_r(&time, &timeValue); |
| 131 | char buf[1024]; |
| 132 | // Wed, 20 Apr 2011 17:31:28 GMT |
| 133 | strftime(buf, sizeof(buf) - 1, "%a, %d %b %Y %H:%M:%S %Z", &timeValue); |
| 134 | return buf; |
| 135 | } |
| 136 | |
| 137 | std::string now() { |
| 138 | return webtime(time(nullptr)); |
| 139 | } |
| 140 | |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 141 | } |