File: path.c

package info (click to toggle)
premake 3.7-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,148 kB
  • sloc: ansic: 20,490; cs: 15,185; makefile: 332
file content (308 lines) | stat: -rw-r--r-- 7,530 bytes parent folder | download
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**********************************************************************
 * Premake - path.c
 * Path handling routines.
 *
 * Copyright (c) 2002-2005 Jason Perkins and the Premake project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License in the file LICENSE.txt for details.
 **********************************************************************/

#include <stdlib.h>
#include <string.h>
#include "premake.h"
#include "os.h"
#include "platform.h"

static char working[8192];
static char forpart[8192];


/************************************************************************
 * Return the full path from a relative path. Contains some extra
 * logic to handle the case where the directory doesn't exist
 ***********************************************************************/

const char* path_absolute(const char* path)
{
	char  relative[8192];
	char* ptr;

	strcpy(relative, path ? path : ".");
	if (strlen(relative) == 0)
		strcpy(relative, ".");
	path_translateInPlace(relative, "posix");

	/* If the directory is already absolute I don't have to do anything */
	if (platform_isAbsolutePath(relative))
		return path;

	/* Figure out where I am currently */
	platform_getcwd(working, 8192);
	path_translateInPlace(working, "posix");

	/* Split the target path and add it in piece by piece */
	ptr = relative;
	while (ptr != NULL)
	{
		char* end = strchr(ptr, '/');
		if (end != NULL)
			*end = '\0';

		if (matches(ptr, ".."))
		{
			char* sep = strrchr(working, '/');
			if (sep != NULL)
				*sep = '\0';
		}
		else if (!matches(ptr, "."))
		{
			strcat(working, "/");
			strcat(working, ptr);
		}

		ptr = (end != NULL) ? end + 1 : NULL;
	}

	return working;
}


/************************************************************************
 * Build a path to get from `from` to `to`
 ***********************************************************************/

#include <stdio.h>

const char* path_build(const char* from, const char* to)
{
	char fromFull[8192];
	char toFull[8192];
	int start, i;

	/* Retrieve the full path to both locations */
	strcpy(fromFull, path_translate(path_absolute(from), "unix"));
	strcpy(toFull,   path_translate(path_absolute(to), "unix"));

	/* Append a separator to both */
	strcat(fromFull, "/");
	strcat(toFull,   "/");

	/* Trim off the common directories from the front */
	start = 0;
	i = 0;
	while (fromFull[i] != '\0' && toFull[i] != '\0' && fromFull[i] == toFull[i])
	{
		if (fromFull[i] == '/')
			start = i + 1;
		i++;
	}

	if (fromFull[i] == '\0' && toFull[i] == '\0')
		return ".";

	/* Build the connecting path */
	if (strlen(fromFull) - start > 0)
	{
		strcpy(working, "../");
		for (i = start; fromFull[i] != '\0'; ++i)
		{
			if (fromFull[i] == '/' && fromFull[i + 1] != '\0')
				strcat(working, "../");
		}
	}
	else
	{
		strcpy(working, "");
	}

	if (strlen(toFull) - start > 0)
	{
		strcat(working, toFull + start);
	}

	/* Remove the trailing slash */
	working[strlen(working) - 1] = '\0';

	/* Make sure I return something */
	if (strlen(working) == 0)
		strcpy(working, ".");

	return working;
}


/************************************************************************
 * Merges two paths
 ***********************************************************************/

const char* path_combine(const char* path0, const char* path1)
{
	strcpy(working, "");

	if (!matches(path0, ".") && !matches(path0, "./"))
		strcat(working, path0);

	path_translateInPlace(working, "posix");

	if (!matches(path1, "") && !matches(path1, ".") && !matches(path1, "./"))
	{
		if (strlen(working) > 0 && working[strlen(working) - 1] != '/')
			strcat(working, "/");
		strcat(working, path1);
	}

	path_translateInPlace(working, "posix");
	return working;
}


/************************************************************************
 * Compare two paths for equality, regardless of path structure.
 ***********************************************************************/

int path_compare(const char* path0, const char* path1)
{
	char abs0[8192];
	char abs1[8192];

	strcpy(abs0, path_absolute(path0));
	strcpy(abs1, path_absolute(path1));
	return matches(abs0, abs1);
}


/************************************************************************
 * Retrieve the portions of a path
 ***********************************************************************/

char path_getseparator(const char* type)
{
	if (type == NULL)
		type = os_get();

	if (matches(type, "windows"))
		return '\\';
	else
		return '/';
}

const char* path_getbasename(const char* path)
{
	const char* name = path_getname(path);
	char* ptr = strrchr(name, '.');
	if (ptr != NULL)
		*ptr = '\0';
	return name;
}

const char* path_getextension(const char* path)
{
	const char* ptr = strrchr(path, '.');
	return ptr;
}

const char* path_getdir(const char* path)
{
	char* ptr;

	if (path != NULL)
	{
		/* Convert path to neutral separators */
		strcpy(forpart, path);
		path_translateInPlace(forpart, "posix");

		/* Now split at last separator */
		ptr = strrchr(forpart, '/');
		if (ptr != NULL)
		{
			*ptr = '\0';
			return forpart;
		}
	}

	return "";
}

const char* path_getname(const char* path)
{
	char* ptr;

	if (path == NULL)
		return NULL;

	strcpy(forpart, path);
	path_translateInPlace(forpart, "posix");

	ptr = strrchr(forpart, '/');
	return (ptr) ? ++ptr : forpart;
}


/************************************************************************
 * Build a path from a directory, a file name, and a file extension
 ***********************************************************************/

const char* path_join(const char* dir, const char* name, const char* ext)
{
	if (dir != NULL)
		strcpy(working, dir);
	else
		strcpy(working, "");

	if (strlen(working) > 0)
		strcat(working, "/");

	strcat(working, name);

	if (ext != NULL && strlen(ext) > 0)
	{
		strcat(working, ".");
		strcat(working, ext);
	}

	return working;
}


/************************************************************************
 * Swap one file extension for another
 ***********************************************************************/

const char* path_swapextension(const char* path, const char* from, const char* to)
{
	strcpy(working, path);
	working[strlen(path) - strlen(from)] = '\0';
	strcat(working, to);
	return working;
}


/************************************************************************
 * Translate the separators used in a path
 ***********************************************************************/

const char* path_translate(const char* path, const char* type)
{
	if (working != path)
		strcpy(working, path);
	path_translateInPlace(working, type);
	return working;
}

void path_translateInPlace(char* buffer, const char* type)
{
	char* ptr;
	for (ptr = buffer; *ptr != '\0'; ++ptr)
	{
		if (*ptr == '\\' || *ptr == '/')
			*ptr = path_getseparator(type);
	}
}