File: util.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 (269 lines) | stat: -rw-r--r-- 6,996 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
/**********************************************************************
 * Premake - util.c
 * Support functions.
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "premake.h"
#include "platform.h"

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

/* Buffer for generators */
char g_buffer[8192];

/* Buffer for internal use */
static char buffer[8192];


/************************************************************************
 * Checks a pattern against the start or end of a string
 ***********************************************************************/

int startsWith(const char* haystack, const char* needle)
{
	return (strncmp(haystack, needle, strlen(needle)) == 0);
}

int endsWith(const char* haystack, const char* needle)
{
	if (strlen(haystack) < strlen(needle))
		return 0;

	haystack = haystack + strlen(haystack) - strlen(needle);
	return (strcmp(haystack, needle) == 0);
}


/************************************************************************
 * Create a pseudo-UUID, good enough for Premake's purposes
 ***********************************************************************/

static void stringify(char* src, char* dst, int count)
{
	char buffer[4];
	int  i;

	for (i = 0; i < count; ++i)
	{
		unsigned value = (unsigned char)src[i];
		sprintf(buffer, "%X", value);

		if (value >= 0x10)
		{
			*(dst++) = buffer[0];
			*(dst++) = buffer[1];
		}
		else
		{
			*(dst++) = '0';
			*(dst++) = buffer[0];
		}
	}
}

void generateUUID(char* uuid)
{
	platform_getuuid(buffer);

	stringify(buffer, uuid, 4);
	uuid[8] = '-';
	stringify(buffer + 4, uuid + 9, 2);
	uuid[13] = '-';
	stringify(buffer + 6, uuid + 14, 2);
	uuid[18] = '-';
	stringify(buffer + 8, uuid + 19, 2);
	uuid[23] = '-';
	stringify(buffer + 10, uuid + 24, 6);
	uuid[36] = '\0';
}


/************************************************************************
 * Checks a file name against a list of known C/C++ file extensions
 ***********************************************************************/

int is_cpp(const char* name)
{
	int i;
	const char* ext = path_getextension(name);
	if (ext == NULL) 
		return 0;

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

	return 0;
}


/************************************************************************
 * Checks to see if two strings match. Just a more readable version
 * of the standard strcmp() function
 ***********************************************************************/

int matches(const char* str0, const char* str1)
{
	if (str0 == NULL || str1 == NULL)
		return (str0 == str1);

	return (strcmp(str0, str1) == 0);
}


/************************************************************************
 * Iterate through an array of string, calling a function for each
 ***********************************************************************/

void print_list(const char** list, const char* prefix, const char* postfix, const char* infix, const char* (*func)(const char*))
{
	int i = 0;
	while (*list)
	{
		const char* value = (func != NULL) ? func(*list) : *list;
		if (value != NULL)
		{
			if (i++ > 0) 
				io_print(infix);
			io_print("%s%s%s", prefix, value, postfix);
		}
		++list;
	}
}


/************************************************************************
 * Iterate through the list of files, build a tree structure
 ***********************************************************************/

void print_source_tree(const char* path, void (*cb)(const char*, int))
{
	const char** i;

	/* Open an enclosing group */
	strcpy(buffer, path);
	if (buffer[strlen(buffer) - 1] == '/')   /* Trim off trailing path separator */
		buffer[strlen(buffer) - 1] = '\0';
	cb(buffer, WST_OPENGROUP);

	for (i = prj_get_files(); *i != NULL; ++i)
	{
		const char* source = (*i);

		/* For each file in the target directory... */
		if (strlen(source) > strlen(path) && strncmp(source, path, strlen(path)) == 0)
		{
			/* Look for a subdirectory... */
			const char* ptr = strchr(source + strlen(path), '/');
			if (ptr != NULL)
			{
				const char** j;

				/* Pull out the subdirectory name */
				strncpy(buffer, source, ptr - source + 1);
				buffer[ptr - source + 1] = '\0';

				/* Have I processed this subdirectory already? Check to see if
				 * I encountered the same subdir name earlier in the list */
				for (j = prj_get_files(); *j != NULL; ++j)
				{
					if (strncmp(buffer, *j, strlen(buffer)) == 0)
						break;
				}

				if (i == j)
				{
					/* Not processed earlier, process it now. Make a local copy
					 * because 'buffer' will be reused in the next call */
					char* newpath = (char*)malloc(strlen(buffer) + 1);
					strcpy(newpath, buffer);
					print_source_tree(newpath, cb);
					free(newpath);
				}
			}
		}
	}

	/* Now send all files that live in 'path' */
	for (i = prj_get_files(); *i != NULL; ++i)
	{
		const char* source = (*i);
		const char* ptr = strrchr(source, '/');
		/* Make sure file is in path and not a subdir under path */
		if (strncmp(path, source, strlen(path)) == 0 && ptr <= source + strlen(path))
			cb(source, WST_SOURCEFILE);
	}

	/* Close the enclosing group */
	strcpy(buffer, path);
	if (buffer[strlen(buffer)-1] == '/')   /* Trim off trailing path separator */
		buffer[strlen(buffer)-1] = '\0';
	cb(buffer, WST_CLOSEGROUP);
}


/************************************************************************
 * Replace special XML characters.
 ***********************************************************************/

const char* xmlEscape(const char* value)
{
	const char* ptr;
	int len;

	len = 0;
	buffer[0] = '\0';
	for (ptr = value; *ptr != '\0'; ++ptr)
	{
		switch (*ptr)
		{
		case '\"':
			strcat(buffer, "&quot;");
			len += 6;
			break;

		case '&':
			strcat(buffer, "&amp;");
			len += 5;
			break;

		case '\'':
			strcat(buffer, "&apos;");
			len += 6;
			break;

		case '<':
			strcat(buffer, "&lt;");
			len += 4;
			break;

		case '>':
			strcat(buffer, "&gt;");
			len += 4;
			break;

		default:
			buffer[len++] = *ptr;
			buffer[len] = '\0';
		}
	}

	return buffer;
}