blob: 7d80104dd90706816b8c7fe8952b6ab83c167e71 (
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
|
/**
* \file path_join.c
* \brief Join two or more pieces of a file system path.
* \author Copyright (c) 2002-2013 Jason Perkins and the Premake project
*/
#include "premake.h"
#include <string.h>
int path_join(lua_State* L)
{
int i, len;
const char* part;
char buffer[0x4000];
char* ptr = buffer;
/* for each argument... */
int argc = lua_gettop(L);
for (i = 1; i <= argc; ++i) {
/* if next argument is nil, skip it */
if (lua_isnil(L, i)) {
continue;
}
/* grab the next argument */
part = luaL_checkstring(L, i);
len = strlen(part);
/* remove trailing slashes */
while (len > 1 && part[len - 1] == '/') {
--len;
}
/* ignore empty segments and "." */
if (len == 0 || (len == 1 && part[0] == '.')) {
continue;
}
/* if I encounter an absolute path, restart my result */
if (do_isabsolute(part)) {
ptr = buffer;
}
/* if the path is already started, split parts */
if (ptr != buffer && *(ptr - 1) != '/') {
*(ptr++) = '/';
}
/* append new part */
strcpy(ptr, part);
ptr += len;
}
*ptr = '\0';
lua_pushstring(L, buffer);
return 1;
}
|