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

path.c « base « src - github.com/windirstat/premake-4.x-stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1648aa71c5415de5fbd841c26cb0b1a798fe3357 (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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
 * \file   path.c
 * \brief  Path handling.
 * \author Copyright (c) 2002-2008 Jason Perkins and the Premake project
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "premake.h"
#include "base/path.h"
#include "platform/platform.h"
#include "base/buffers.h"
#include "base/cstr.h"
#include "base/dir.h"

static char* CppFileExtensions[] = { ".cc", ".cpp", ".cxx", ".c", ".s", NULL };


/**
 * Create an absolute path from a relative one.
 * \param   path      The relative path to convert.
 * \returns An absolute version of the relative path.
 */
char* path_absolute(const char* path)
{
	char* source;
	char* result;

	assert(path);

	/* normalize the target path */
	source = path_translate(path, "/");
	if (strlen(source) == 0)
		strcpy(source, ".");

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

	/* start from the current location */
	result = dir_get_current();

	/* split up the supplied relative path and tackle it bit by bit */
	while (source)
	{
		char* end = strchr(source, '/');
		if (end)
			*end = 0;

		if (cstr_eq(source, ".."))
		{
			char* up = strrchr(result, '/');
			if (up)
				*up = 0;
		}
		else if (!cstr_eq(source, "."))
		{
			strcat(result, "/");
			strcat(result, source);
		}

		source = end ? end + 1 : NULL;
	}

	return result;
}


/**
 * Assemble a complete file path from its component parts.
 * \param   dir       The directory portion of the path.
 * \param   filename  The file name portion of the path.
 * \param   ext       The extension portion of the path.
 * \returns The assembled file path.
 */
char* path_assemble(const char* dir, const char* filename, const char* ext)
{
	char* buffer;

	assert(dir);
	assert(filename);
	assert(ext);
	
	buffer = path_join(dir, filename);
	if (ext)
	{
		strcat(buffer, ext);
	}
	return buffer;
}


/**
 * Returns the base name in a path: the filename, without the directory or
 * file extension.
 * \param   path     The path to split.
 * \returns The base name part of the path.
 */
char* path_basename(const char* path)
{
	char* buffer = path_filename(path);
	char* ptr = strrchr(buffer, '.');
	if (ptr)
	{
		*ptr = '\0';
	}
	return buffer;
}


/**
 * Retrieve the directory portion of a path.
 * \param   path  The path to split.
 * \returns The directory portion of the path. Returns an empty string ("") if
 *          the path does not contain any directory information.
 */
char* path_directory(const char* path)
{
	char* ptr;
	char* buffer = buffers_next();
	
	assert(path);
	strcpy(buffer, path);
	
	ptr = strrchr(buffer, '/');
	if (ptr)
		*ptr = '\0';
	else
		*buffer = '\0';

	return buffer;
}


/**
 * Retrieve the file extension portion of a path. If the path has multiple
 * dots in the filename (ie. filename.my.ext) only the last bit (.ext) will
 * be returned. The dot is included in the extension.
 * \param   path    The path to split.
 * \returns The extension portion of the path, or an empty string if no extension is present.
 */
char* path_extension(const char* path)
{
	char* ptr;
	char* buffer = buffers_next();

	assert(path);
	strcpy(buffer, path);
	ptr = strrchr(buffer, '.');
	return (ptr) ? ptr : "";
}


/**
 * Retrieve the fileame (filename.ext) portion of a path.
 * \param   path      The path to split.
 * \returns The filename portion of the path. Returns an empty string ("") if
 *          the path is empty.
 */
char* path_filename(const char* path)
{
	char* ptr;
	char* buffer = buffers_next();

	assert(path);

	ptr = strrchr(path, '/');
	if (ptr)
	{
		strcpy(buffer, ptr + 1);
	}
	else
	{
		strcpy(buffer, path);
	}

	return buffer;
}


/**
 * Determine is a path is absolute (rooted at base of filesystem).
 * \param   path      The path to check.
 * \returns True if the path is absolute.
 */
int path_is_absolute(const char* path)
{
	assert(path);

	if (path[0] == '/' || path[0] == '\\')
		return 1;
	if (path[1] == ':')
		return 1;
	return 0;
}


/**
 * Returns true if the path represents a C++ source code file; by checking
 * the file extension.
 * \param   path      The path to check.
 * \returns True if the path uses a known C++ file extension.
 */
int path_is_cpp_source(const char* path)
{
	int i;

	char* ext = path_extension(path);
	if (cstr_eq(ext, ""))
	{
		return 0;
	}

	for (i = 0; CppFileExtensions[i] != NULL; ++i)
	{
		if (cstr_eqi(CppFileExtensions[i], ext))
			return 1;
	}

	return 0;
}


/**
 * Join two paths togethers.
 * \param   leading   The leading path.
 * \param   trailing  The trailing path.
 * \returns A unified path.
 * \note If the trailing path is absolute, that will be the return value.
 *       A join is only performed if the trailing path is relative.
 */
char* path_join(const char* leading, const char* trailing)
{
	char* buffer = buffers_next();
	
	/* treat nulls like empty paths */
	leading = (leading != NULL) ? leading : "";
	trailing = (trailing != NULL) ? trailing : "";

	if (!trailing)
	{
		strcpy(buffer, leading);
		return buffer;
	}

	if (!leading || path_is_absolute(trailing))
	{
		strcpy(buffer, trailing);
		return buffer;
	}

	if (leading)
	{
		strcat(buffer, leading);
	}

	if (strlen(buffer) > 0 && !cstr_ends_with(buffer, "/"))
	{
		strcat(buffer, "/");
	}
		
	strcat(buffer, trailing);
	return buffer;
}


/**
 * \brief   Compute the relative path between two locations.
 * \param   base    The base path.
 * \param   target  The target path.
 * \returns A relative path from the base to the target.
 */
char* path_relative(const char* base, const char* target)
{
	int start, i;
	char* result;

	/* normalize the two paths */
	char* full_base = path_absolute(base);
	char* full_targ = path_absolute(target);

	strcat(full_base, "/");
	strcat(full_targ, "/");

	/* trim off the common directories from the start */
	for (start = 0, i = 0; full_base[i] && full_targ[i] && full_base[i] == full_targ[i]; ++i)
	{
		if (full_base[i] == '/')
			start = i + 1;
	}

	/* same directory? */
	if (full_base[i] == 0 && full_targ[i] == 0)
		return ".";

	/* build a connecting path */
	result = buffers_next();
	if (strlen(full_base) - start > 0)
	{
		strcpy(result, "../");
		for (i = start; full_base[i]; ++i)
		{
			if (full_base[i] == '/' && full_base[i + 1])
				strcat(result, "../");
		}
	}

	if (strlen(full_targ) - start > 0)
	{
		strcat(result, full_targ + start);
	}

	/* remove the trailing slash */
	result[strlen(result) - 1] = 0;

	if (strlen(result) == 0)
		strcpy(result, ".");
	return result;
}


/**
 * Replace all path separator characters in a path.
 * \param   path    The path to translate.
 * \param   sep     The desired separator, or NULL for the platform's native separator.
 * \returns The translated path.
 */
char* path_translate(const char* path, const char* sep)
{
	char* ptr;
	char* buffer;

	assert(path);

	buffer = buffers_next();
	if (sep == NULL)
	{
#if defined(PLATFORM_WINDOWS)
		sep = "\\";
#else
		sep = "/";
#endif
	}

	strcpy(buffer, path);
	for (ptr = buffer; *ptr; ++ptr)
	{
		if (*ptr == '/' || *ptr == '\\')
			*ptr = *sep;
	}

	return buffer;
}