blob: b7177ce649f927812e03b65d3491a62c3b2344ca [file] [log] [blame]
Austin Schuh24adb6b2015-09-06 17:37:40 -07001// Copyright (c) 2013, Matt Godbolt
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
34namespace seasocks {
35
36char* skipWhitespace(char* str) {
37 while (isspace(*str)) ++str;
38 return str;
39}
40
41char* skipNonWhitespace(char* str) {
42 while (*str && !isspace(*str)) {
43 ++str;
44 }
45 return str;
46}
47
48char* shift(char*& str) {
49 if (str == NULL) {
50 return NULL;
51 }
52 char* startOfWord = skipWhitespace(str);
53 if (*startOfWord == 0) {
54 str = startOfWord;
55 return NULL;
56 }
57 char* endOfWord = skipNonWhitespace(startOfWord);
58 if (*endOfWord != 0) {
59 *endOfWord++ = 0;
60 }
61 str = endOfWord;
62 return startOfWord;
63}
64
65std::string getLastError(){
66 char errbuf[1024];
67 return strerror_r(errno, errbuf, sizeof(errbuf));
68}
69
70std::string formatAddress(const sockaddr_in& address) {
71 char ipBuffer[24];
72 sprintf(ipBuffer,
73 "%d.%d.%d.%d:%d",
74 (address.sin_addr.s_addr >> 0) & 0xff,
75 (address.sin_addr.s_addr >> 8) & 0xff,
76 (address.sin_addr.s_addr >> 16) & 0xff,
77 (address.sin_addr.s_addr >> 24) & 0xff,
78 htons(address.sin_port));
79 return ipBuffer;
80}
81
82std::vector<std::string> split(const std::string& input, char splitChar) {
83 if (input.empty()) return std::vector<std::string>();
84 std::vector<std::string> result;
85 size_t pos = 0;
86 size_t newPos;
87 while ((newPos = input.find(splitChar, pos)) != std::string::npos) {
88 result.push_back(input.substr(pos, newPos - pos));
89 pos = newPos + 1;
90 }
91 result.push_back(input.substr(pos));
92 return result;
93}
94
95void replace(std::string& string, const std::string& find, const std::string& replace) {
96 size_t pos = 0;
97 const size_t findLen = find.length();
98 const size_t replaceLen = replace.length();
99 while ((pos = string.find(find, pos)) != std::string::npos) {
100 string = string.substr(0, pos) + replace + string.substr(pos + findLen);
101 pos += replaceLen;
102 }
103}
104
105bool caseInsensitiveSame(const std::string &lhs, const std::string &rhs) {
106 return strcasecmp(lhs.c_str(), rhs.c_str()) == 0;
107}
108
109}