Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EnvUtils.cpp « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1c3a6dd633ea198b76464ab7024d5732c3770bb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2017-2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "EnvUtils.h"

#include <QByteArray>

namespace EnvUtils {

QString getenv(QString name) {
#ifdef Q_OS_WIN
	QByteArray buf;
	size_t requiredSize = 0;

	static_assert(sizeof(wchar_t) == sizeof(ushort), "expected 2-byte wchar_t");

	const wchar_t *wname = reinterpret_cast< const wchar_t * >(name.utf16());

	// Query the required buffer size (in elements).
	if (_wgetenv_s(&requiredSize, 0, 0, wname) != 0) {
		return QString();
	}
	if (requiredSize == 0) {
		return QString();
	}

	// Resize buf to fit the value and put it there.
	buf.resize(static_cast< int >(requiredSize * sizeof(wchar_t)));
	if (_wgetenv_s(&requiredSize, reinterpret_cast< wchar_t * >(buf.data()), requiredSize, wname) != 0) {
		return QString();
	}

	// Convert the value to QString and return it.
	const wchar_t *wbuf = reinterpret_cast< const wchar_t * >(buf.constData());
	return QString::fromWCharArray(wbuf);
#else
	QByteArray name8bit = name.toLocal8Bit();
	char *val           = ::getenv(name8bit.constData());
	if (!val) {
		return QString();
	}
	return QString::fromLocal8Bit(val);
#endif
}

bool setenv(QString name, QString value) {
#ifdef Q_OS_WIN
	return _wputenv_s(reinterpret_cast< const wchar_t * >(name.utf16()),
					  reinterpret_cast< const wchar_t * >(value.utf16()))
		   == 0;
#else
	const int OVERWRITE = 1;
	return ::setenv(name.toLocal8Bit().constData(), value.toLocal8Bit().constData(), OVERWRITE) == 0;
#endif
}

bool waylandIsUsed() {
	// If wayland is used, this environment variable is expected to be set
	return getenv(QStringLiteral("WAYLAND_DISPLAY")) != "";
}

}; // namespace EnvUtils