summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/path.h
blob: 1291d7b17d318a8828e0e533260c013f69896ae0 (plain) (blame)
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
65
// license:BSD-3-Clause
// copyright-holders:Vas Crabb
/***************************************************************************

    path.h

    Filesystem path utilties

***************************************************************************/
#ifndef MAME_LIB_UTIL_PATH_H
#define MAME_LIB_UTIL_PATH_H

#include "osdfile.h" // for PATH_SEPARATOR

#include <string>
#include <utility>


namespace util {

/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

// is a given character a directory separator?

constexpr bool is_directory_separator(char c)
{
#if defined(WIN32)
	return ('\\' == c) || ('/' == c) || (':' == c);
#else
	return '/' == c;
#endif
}


// append to a path

template <typename T, typename... U>
inline std::string &path_append(std::string &path, T &&next, U &&... more)
{
	if (!path.empty() && !is_directory_separator(path.back()))
		path.append(PATH_SEPARATOR);
	path.append(std::forward<T>(next));
	if constexpr (sizeof...(U))
		return path_append(std::forward<U>(more)...);
	else
		return path;
}


// concatenate paths

template <typename T, typename... U>
inline std::string path_concat(T &&first, U &&... more)
{
	std::string result(std::forward<T>(first));
	if constexpr (sizeof...(U))
		path_append(result, std::forward<U>(more)...);
	return result;
}

} // namespace util

#endif // MAME_LIB_UTIL_PATH_H