summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/genie/src/host/path_helpers.c
blob: ee346b3bb39f0a44972d260bda03a40426766097 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
 * \file   path_helpers.c
 * \brief  Helper functions for path_getrelative.c and path_getabsolute.c
 */

#include "premake.h"
#include <string.h>

#define USE_DRIVE_LETTERS PLATFORM_WINDOWS

static int has_drive_letter(const char * path)
{
#if USE_DRIVE_LETTERS
	char ch = path[0];
	if (((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) && path[1] == ':')
		return 1;
#else
	(void)path;
#endif // USE_DRIVE_LETTERS

	return 0;
}

int is_absolute_path(const char * path)
{
	char ch = path[0];
	if (ch == '\0')
	{
		return 0;
	}

	if (ch == '/' || ch == '\\' || ch == '$')
	{
		return 1;
	}

	if (has_drive_letter(path))
	{
		return 1;
	}

	return 0;
}

static char *copy_current_directory(char * buffer, int bufsize)
{
#if PLATFORM_WINDOWS
	int len = GetCurrentDirectory(bufsize, buffer);
	int i;
	for (i = 0; i < len; i++)
		buffer[i] = (buffer[i] == '\\' ? '/' : buffer[i]);
	return buffer + len;
#else
	char *ptr = getcwd(buffer, bufsize);
	int len = ptr ? strlen(ptr) : 0;
	return buffer + len;
#endif
}

#if USE_DRIVE_LETTERS
static char *copy_current_drive_letter(char * buffer, int bufsize)
{
	int len = GetCurrentDirectory(bufsize, buffer);
	if (len >= 2 && buffer[1] == ':') {
		buffer[2] = '\0';
		return buffer + 2;
	}
	else {
		buffer[0] = '\0';
		return buffer;
	}
}
#endif // USE_DRIVE_LETTERS

static char *backtrack(char *path, char *start)
{
	for (; path > start; --path) {
		if (*path == '/') {
			*path = '\0';
			return path;
		}
#if USE_DRIVE_LETTERS
		else if (*path == ':' && (path == start + 1)) {
			path[1] = '/';
			path[2] = '\0';
			return path + 2;
		}
#endif
	}

	start[0] = '/';
	start[1] = '\0';
	return start + 1;
}

static char *concat(char *path, const char *pathTerm, const char *token, const char *tokenTerm)
{
	while (path < pathTerm && token < tokenTerm)
		*path++ = *token++;
	return path;
}

static char *copy(const char str[], char *buffer, int bufsize)
{
	int i = 0;
	for (; str[i] && i < bufsize - 1; i++)
		buffer[i] = str[i];
	buffer[i] = '\0';
	return buffer + i;
}

char *get_absolute_path(const char path[], char *buffer, int bufsize)
{
	// There must be room for at least a drive letter, colon and slash.
	if (bufsize < 4)
		return 0;

	char *dst  = buffer;
	char *term = buffer + bufsize - 1;

	// If the path started as relative, prepend the current working directory
	if (!is_absolute_path(path))
	{
		dst = copy_current_directory(buffer, bufsize);
	}
#if USE_DRIVE_LETTERS
	// Add drive letter to absolute paths without one
	else if (!has_drive_letter(path) && path[0] != '$')
	{
		dst = copy_current_drive_letter(buffer, bufsize);
	}
#endif

	// Process the path one segment at a time.
	int segment = 0;
	const char *src   = path;
	const char *token = src;
	for (; dst < term; ++src) {
		if (*src == '/' || *src == '\\' || *src == '\0') {
			int tokenlen = (int)(src - token);
			if (tokenlen == 0) {
				if (segment == 0)
					*dst++ = '/';
			}
			else if (tokenlen == 1 && token[0] == '.') {
				// do nothing
			}
			else if (tokenlen == 2 && token[0] == '.' && token[1] == '.') {
				dst = backtrack(dst - 1, buffer);
			}
			else {
				if (dst > buffer && dst[-1] != '/')
					*dst++ = '/';
				dst = concat(dst, term, token, src);
			}
			token = src + 1;
			++segment;
			if (*src == '\0')
				break;
		}
	}

#if USE_DRIVE_LETTERS
	// Make sure drive letter is lowercase
	buffer[0] = (char)tolower(buffer[0]);
#endif

	// Chop trailing slash
	if (dst > buffer && dst[-1] == '/')
		--dst;

	// Add a null terminator.
	if (dst < term)
		*dst = '\0';
	else
		*--dst = '\0';

	return dst;
}

char *get_relative_path(const char src[], const char dst[], char *buffer, int bufsize)
{
	// There must be room for at least a drive letter, colon and slash.
	if (bufsize < 4)
		return 0;

	// If the dst path starts with $, return it as an absolute path.
	if (dst[0] == '$')
		return copy(dst, buffer, bufsize);

	// Get the absolute source and destination paths.
	char srcBuf[PATH_BUFSIZE];
	char dstBuf[PATH_BUFSIZE];
	char *srcEnd = get_absolute_path(src, srcBuf, PATH_BUFSIZE - 1);
	char *dstEnd = get_absolute_path(dst, dstBuf, PATH_BUFSIZE - 1);

#if USE_DRIVE_LETTERS
	// If the paths have different drive letters, return the absolute path.
	if (srcBuf[0] != dstBuf[0])
		return copy(dstBuf, buffer, bufsize);
#endif

	// Append '/' to the end of each path.
	*srcEnd++ = '/'; *srcEnd = '\0';
	*dstEnd++ = '/'; *dstEnd = '\0';

	// Find the shared path prefix of the src and dst paths.
	int lastSlash = -1;
	int i;
	for (i = 0; srcBuf[i] == dstBuf[i]; i++) {
		if (srcBuf[i] == '/') {
			lastSlash = i;
		}
		else if (srcBuf[i] == '\0') {
			// Paths are identical, so return '.'
			buffer[0] = '.';
			buffer[1] = '\0';
			return buffer + 1;
		}
	}

	// For each remaining path segment in src after the last shared slash,
	// append '../' to the result.
	char *resultPtr  = buffer;
	char *resultTerm = buffer + bufsize;
	const char *ptr;
	for (ptr = srcBuf + lastSlash + 1; *ptr; ++ptr) {
		if (*ptr == '/' && resultPtr + 3 < resultTerm) {
			resultPtr[0] = '.';
			resultPtr[1] = '.';
			resultPtr[2] = '/';
			resultPtr += 3;
		}
	}

	// Append the portion of the destination path beyond the last shared
	// slash.
	char *dstPtr;
	for (dstPtr = dstBuf + lastSlash + 1; *dstPtr && resultPtr < resultTerm; )
		*resultPtr++ = *dstPtr++;

	// Remove the trailing slash.
	if (resultPtr[-1] == '/')
		--resultPtr;

	// Add a null terminator.
	if (resultPtr < resultTerm)
		*resultPtr = '\0';
	else
		*--resultPtr = '\0';

	return resultPtr;
}