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

cstr.c « base « src - github.com/windirstat/premake-4.x-stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4fa588db44db01ee3bdd2385545d3f6f57da3a1a (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
/**
 * \file   cstr.c
 * \brief  C string handling.
 * \author Copyright (c) 2002-2008 Jason Perkins and the Premake project
 */

#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "premake.h"
#include "base/buffers.h"
#include "base/cstr.h"
#include "base/luastate.h"


/**
 * Determines if the sequence appears anywhere in the target string.
 * \param   str      The string to test.
 * \param   expected The sequence to search for.
 * \returns True if the sequence is contained in the string.
 */
int cstr_contains(const char* str, const char* expected)
{
	assert(str);
	assert(expected);
	return (strstr(str, expected) != NULL);
}


/**
 * Determines if the string ends with a particular sequence.
 * \param str        The string to test.
 * \param expected   The sequence for which to look.
 * \returns True if the string ends with the sequence, false otherwise.
 */
int cstr_ends_with(const char* str, const char* expected)
{
	if (str != NULL && expected != NULL)
	{
		int str_len = strlen(str);
		int exp_len = strlen(expected);
		if (str_len >= exp_len)
		{
			const char* start = str + str_len - exp_len;
			return (strcmp(start, expected) == 0);
		}
	}
	return 0;
}


/**
 * Compares two C strings for equality.
 * \param   str        The string to compare.
 * \param   expected   The value to compare against.
 * \returns True if the strings match, zero otherwise.
 */
int cstr_eq(const char* str, const char* expected)
{
	if (str != NULL && expected != NULL)
	{
		return (strcmp(str, expected) == 0);
	}
	return 0;
}


/**
 * Performs a case-insensitive comparasion on two C strings for equality.
 * \param   str      The string to compare.
 * \param   expected The value to compare against.
 * \returns True if the strings match, zero otherwise.
 */
int cstr_eqi(const char* str, const char* expected)
{
	if (str != NULL && expected != NULL)
	{
		while (*str && *expected)
		{
			if (tolower(*str) != tolower(*expected))
			{
				return 0;
			}

			str++;
			expected++;
		}

		return (*str == *expected);
	}
	return 0;
}


/**
 * Builds a string using printf-style formatting codes.
 * \param   format   The format string, which may contain printf-style formatting codes.
 * \returns The formatted string.
 */
char* cstr_format(const char* format, ...)
{
	va_list args;
	char* buffer = buffers_next();

	va_start(args, format);
	vsprintf(buffer, format, args);
	va_end(args);

	return buffer;
}


/**
 * Compares a string value with a pattern, using the Lua pattern syntax, and
 * returns true if there is a match.
 */
int cstr_matches_pattern(const char* str, const char* pattern)
{
	lua_State* L;
	char* buffer;
	int i, escaped, top, result, is_my_state = 0;

	assert(str);
	assert(pattern);

	/* convert pattern to match any case, each non-escaped letter A becomes [Aa] */
	buffer = buffers_next();
	i = 0;
	escaped = 0;
	while (*pattern != '\0')
	{
		if (isalpha(*pattern) && !escaped)
		{
			buffer[i++] = '[';
			buffer[i++] = (char)toupper(*pattern);
			buffer[i++] = (char)tolower(*pattern);
			buffer[i++] = ']';
		}
		else
		{
			escaped = (*pattern == '%');
			buffer[i++] = *pattern;
		}

		pattern++;
	}
	buffer[i] = '\0';

	/* connect to Lua so I can call the match() function */
	L = luastate_get_current();
	is_my_state = (L == NULL);
	if (is_my_state)
	{
		L = luastate_create();
	}
	top = lua_gettop(L);

	/* retrieve the match() function and call it */
	lua_getglobal(L, "string");
	lua_getfield(L, -1, "match");	
	lua_pushstring(L, str);
	lua_pushstring(L, buffer);
	lua_call(L, 2, 1);
	result = cstr_eq(str, lua_tostring(L,-1));

	/* clean up and return the result */
	lua_settop(L, top);
	if (is_my_state)
	{
		luastate_destroy(L);
	}

	return result;
}


/**
 * Determines if the given C string starts with a particular sequence.
 * \param   str        The string to test.
 * \param   expected   The sequence for which to look.
 * \returns True if the string starts with the sequence, false otherwise.
 */
int cstr_starts_with(const char* str, const char* expected)
{
	if (str != NULL && expected != NULL)
	{
		return (strncmp(str, expected, strlen(expected)) == 0);
	}
	return 0;
}


/**
 * Removes spaces any other special characters from a string, converting it
 * into a C/C++/C# safe identifier.
 * \param   str       The string to process.
 * \returns An identifier-safe string.
 */
char* cstr_to_identifier(const char* str)
{
	char* buffer = buffers_next();
	char* dst = buffer;

	while (*str != '\0')
	{
		if (isalnum(*str) || *str == '_')
		{
			*(dst++) = *str;
		}
		str++;
	}

	*dst = '\0';
	return buffer;
}


/**
 * Remove a character from the end of a string, if present.
 * \param   str       The string to trim.
 * \param   ch        The character to trim.
 */
void cstr_trim(char* str, char ch)
{
	int i;
	assert(str);
	i = strlen(str) - 1;
	while (i >= 0 && str[i] == ch)
	{
		str[i] = '\0';
		i--;
	}
}