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

path_translate.c « host « src - github.com/windirstat/premake-4.x-stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bd0c2f18ebc3f163a830bf0ce49fd5de1f99e897 (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
/**
 * \file   path_translate.c
 * \brief  Translates between path separators.
 * \author Copyright (c) 2002-2013 Jason Perkins and the Premake project
 */

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


void do_translate(char* value, const char sep)
{
	char* ch;
	for (ch = value; *ch != '\0'; ++ch) {
		if (*ch == '/' || *ch == '\\') {
			*ch = sep;
		}
	}
}


static void translate(char* result, const char* value, const char sep)
{
	strcpy(result, value);
	do_translate(result, sep);
}


int path_translate(lua_State* L)
{
	const char* sep;
	char buffer[0x4000];

	if (lua_gettop(L) == 1) {
		sep = "\\";
	}
	else {
		sep = luaL_checkstring(L, 2);
	}

	if (lua_istable(L, 1)) {
		int i = 0;
		lua_newtable(L);
		lua_pushnil(L);
		while (lua_next(L, 1)) {
			const char* key;
			lua_pushvalue(L, 4); // copy the key
			key = luaL_checkstring(L, 5);
			translate(buffer, key, sep[0]);
			lua_pop(L, 2);

			lua_pushstring(L, buffer);
			lua_rawseti(L, -3, ++i);
		}
		return 1;
	}
	else {
		const char* value = luaL_checkstring(L, 1);
		translate(buffer, value, sep[0]);
		lua_pushstring(L, buffer);
		return 1;
	}
}