1 // text.h
2
3
4 /**
5 * Copyright (C) 2018-present MongoDB, Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the Server Side Public License, version 1,
9 * as published by MongoDB, Inc.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * Server Side Public License for more details.
15 *
16 * You should have received a copy of the Server Side Public License
17 * along with this program. If not, see
18 * <http://www.mongodb.com/licensing/server-side-public-license>.
19 *
20 * As a special exception, the copyright holders give permission to link the
21 * code of portions of this program with the OpenSSL library under certain
22 * conditions as described in each individual source file and distribute
23 * linked combinations including the program with the OpenSSL library. You
24 * must comply with the Server Side Public License in all respects for
25 * all of the code used other than as permitted herein. If you modify file(s)
26 * with this exception, you may extend this exception to your version of the
27 * file(s), but you are not obligated to do so. If you do not wish to do so,
28 * delete this exception statement from your version. If you delete this
29 * exception statement from all source files in the program, then also delete
30 * it in the license file.
31 */
32
33 #pragma once
34
35 #include <string>
36 #include <vector>
37
38 #include "mongo/base/disallow_copying.h"
39 #include "mongo/config.h"
40
41 namespace mongo {
42
43 class StringSplitter {
44 public:
45 /** @param big the std::string to be split
46 @param splitter the delimiter
47 */
StringSplitter(const char * big,const char * splitter)48 StringSplitter(const char* big, const char* splitter) : _big(big), _splitter(splitter) {}
49
50 /** @return true if more to be taken via next() */
more()51 bool more() const {
52 return _big[0] != 0;
53 }
54
55 /** get next split std::string fragment */
56 std::string next();
57
58 void split(std::vector<std::string>& l);
59
60 std::vector<std::string> split();
61
62 static std::vector<std::string> split(const std::string& big, const std::string& splitter);
63
64 static std::string join(const std::vector<std::string>& l, const std::string& split);
65
66 private:
67 const char* _big;
68 const char* _splitter;
69 };
70
71 /* This doesn't defend against ALL bad UTF8, but it will guarantee that the
72 * std::string can be converted to sequence of codepoints. However, it doesn't
73 * guarantee that the codepoints are valid.
74 */
75 bool isValidUTF8(const char* s);
76 bool isValidUTF8(const std::string& s);
77
78 #if defined(_WIN32)
79
80 std::string toUtf8String(const std::wstring& wide);
81
82 std::wstring toWideString(const char* s);
83
84 bool writeUtf8ToWindowsConsole(const char* utf8String, unsigned int utf8StringSize);
85
86 /* like toWideString but UNICODE macro sensitive */
87 #if !defined(_UNICODE)
88 #error temp error
toNativeString(const char * s)89 inline std::string toNativeString(const char* s) {
90 return s;
91 }
92 #else
toNativeString(const char * s)93 inline std::wstring toNativeString(const char* s) {
94 return toWideString(s);
95 }
96 #endif
97
98 class WindowsCommandLine {
99 MONGO_DISALLOW_COPYING(WindowsCommandLine);
100 char** _argv;
101 char** _envp;
102
103 public:
104 WindowsCommandLine(int argc, wchar_t* argvW[], wchar_t* envpW[]);
105 ~WindowsCommandLine();
argv(void)106 char** argv(void) const {
107 return _argv;
108 };
envp(void)109 char** envp(void) const {
110 return _envp;
111 };
112 };
113
114 #endif // #if defined(_WIN32)
115
116 /**
117 * Construct a Windows command line string, UTF-8 encoded, from a vector of
118 * UTF-8 arguments, "argv".
119 *
120 * See "Parsing C++ Command-Line Arguments (C++)"
121 * http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx
122 */
123 std::string constructUtf8WindowsCommandLine(const std::vector<std::string>& argv);
124
125 } // namespace mongo
126