blob: 958caef3bc066b87909bad79fd2a4608993bb488 [file] [log] [blame]
Austin Schuh9d823002019-04-14 12:53:17 -07001// Copyright (c) 2013-2017, Matt Godbolt
Austin Schuh24adb6b2015-09-06 17:37:40 -07002// 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
34namespace seasocks {
35
36char* skipWhitespace(char* str) {
Austin Schuh9d823002019-04-14 12:53:17 -070037 while (isspace(*str))
38 ++str;
Austin Schuh24adb6b2015-09-06 17:37:40 -070039 return str;
40}
41
42char* skipNonWhitespace(char* str) {
43 while (*str && !isspace(*str)) {
44 ++str;
45 }
46 return str;
47}
48
49char* shift(char*& str) {
Austin Schuh9d823002019-04-14 12:53:17 -070050 if (str == nullptr) {
51 return nullptr;
Austin Schuh24adb6b2015-09-06 17:37:40 -070052 }
53 char* startOfWord = skipWhitespace(str);
54 if (*startOfWord == 0) {
55 str = startOfWord;
Austin Schuh9d823002019-04-14 12:53:17 -070056 return nullptr;
Austin Schuh24adb6b2015-09-06 17:37:40 -070057 }
58 char* endOfWord = skipNonWhitespace(startOfWord);
59 if (*endOfWord != 0) {
60 *endOfWord++ = 0;
61 }
62 str = endOfWord;
63 return startOfWord;
64}
65
Austin Schuh9d823002019-04-14 12:53:17 -070066std::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
76std::string getLastError() {
Austin Schuh24adb6b2015-09-06 17:37:40 -070077 char errbuf[1024];
Austin Schuh9d823002019-04-14 12:53:17 -070078 const auto ignore = strerror_r(errno, errbuf, sizeof(errbuf));
79 static_cast<void>(ignore);
80 return errbuf;
Austin Schuh24adb6b2015-09-06 17:37:40 -070081}
82
83std::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
95std::vector<std::string> split(const std::string& input, char splitChar) {
Austin Schuh9d823002019-04-14 12:53:17 -070096 if (input.empty())
97 return std::vector<std::string>();
Austin Schuh24adb6b2015-09-06 17:37:40 -070098 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 Schuh9d823002019-04-14 12:53:17 -0700109void replace(std::string& string, const std::string& find,
110 const std::string& replace) {
111 if (find.empty()) {
112 return;
113 }
114
Austin Schuh24adb6b2015-09-06 17:37:40 -0700115 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 Schuh9d823002019-04-14 12:53:17 -0700124bool caseInsensitiveSame(const std::string& lhs, const std::string& rhs) {
Austin Schuh24adb6b2015-09-06 17:37:40 -0700125 return strcasecmp(lhs.c_str(), rhs.c_str()) == 0;
126}
127
Austin Schuh9d823002019-04-14 12:53:17 -0700128std::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
137std::string now() {
138 return webtime(time(nullptr));
139}
140
Austin Schuh24adb6b2015-09-06 17:37:40 -0700141}