~exprez135/mountaen

ref: fddb17f6172871079425de17b132b5d1d66fc253 mountaen/src/helpers.c -rw-r--r-- 3.9 KiB
fddb17f6Nate Ijams Discourse, mirrors, and updates. 1 year, 8 months ago
                                                                                
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
typedef enum { false,
	       true } bool;

bool
isalphachr(char ch)
{
	return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}

bool
isnumchr(char ch)
{
	return ch >= '0' && ch <= '9';
}

bool
isspacechr(char ch)
{
	return ch == ' ';
}

bool
isalphanumchr(char ch)
{
	return !isalphachr(ch) && !isnumchr(ch) && !isspacechr(ch) ? false : true;
}

bool
isalphanumstr(char* src)
{
	int i;
	for(i = 0; i < (int)strlen(src); i++) {
		if(!isalphanumchr(src[i])) {
			printf("%c\n", src[i]);
			return false;
		}
	}
	return true;
}

int
indexstr(char* a, char* b)
{
	int i, j, alen = strlen(a), blen = strlen(b);
	for(i = 0; i < alen; i++) {
		for(j = 0; j < blen; j++) {
			if(a[i + j] == '\0') {
				return -1;
			}
			if(a[i + j] != b[j]) {
				break;
			}
			if(j == blen - 1) {
				return i;
			}
		}
	}
	return -1;
}

int
indexchr(char* str, char target)
{
	int i;
	int len = strlen(str);
	for(i = 0; i < len; i++) {
		if(str[i] == target) {
			return i;
		}
	}
	return -1;
}

bool
isurlstr(char* str)
{
	return indexstr(str, "://") >= 0;
}

void
substr(char* src, char* dest, int from, int to)
{
	memcpy(dest, src + from, to);
	dest[to] = '\0';
}

void
swapstr(char* src, char* dest, char* a, char* b)
{
	char head[1024], tail[1024];
	int index = indexstr(src, a);
	if(index < 0) {
		return;
	}
	substr(src, head, 0, index);
	substr(src, tail, index + strlen(a), strlen(src) - index - strlen(a));
	dest[0] = '\0';
	strcat(dest, head);
	strcat(dest, b);
	strcat(dest, tail);
}

void
cpystr(char* src, char* dest)
{
	int i;
	int len = strlen(src);
	for(i = 0; i < len; i++) {
		dest[i] = src[i];
	}
	dest[len] = '\0';
}

void
ucstr(char* dest)
{
	int i, len = strlen(dest);
	for(i = 0; i < len; i++) {
		dest[i] = toupper(dest[i]);
	}
}

void
lcstr(char* dest)
{
	int i, len = strlen(dest);
	for(i = 0; i < len; i++) {
		dest[i] = tolower(dest[i]);
	}
}

char*
trimstr(char* str)
{
	char* end;
	while(isspace((unsigned char)*str))
		str++;
	if(*str == 0)
		return str;
	end = str + strlen(str) - 1;
	while(end > str && isspace((unsigned char)*end))
		end--;
	end[1] = '\0';
	return str;
}

void
alphanumstr(char* src, char* dest)
{
	int i;
	int len = strlen(src) + 1;
	for(i = 0; i < len; i++) {
		dest[i] = src[i];
		if(dest[i] == '\0') {
			break;
		}
		if(!isalphanumchr(dest[i])) {
			dest[i] = ' ';
		} else {
			dest[i] = tolower(dest[i]);
		}
	}
	dest[len - 1] = '\0';
}

void
filenamestr(char* str, char* mod)
{
	int i;
	int len = strlen(str) + 1;
	for(i = 0; i < len; i++) {
		mod[i] = str[i];
		if(mod[i] == '\0') {
			break;
		}
		if(!isalphachr(mod[i]) && !isnumchr(mod[i])) {
			mod[i] = '_';
		} else {
			mod[i] = tolower(mod[i]);
		}
	}
	mod[len - 1] = '\0';
}

void
firstword(char* src, char* dest)
{
	int until = indexchr(src, ' ');
	if(until > -1) {
		substr(src, dest, 0, until);
	} else {
		substr(src, dest, 0, strlen(src));
	}
}

int
count_leading_spaces(char* str)
{
	int i;
	int len = strlen(str) + 1;
	for(i = 0; i < len; i++) {
		if(str[i] != ' ') {
			return i;
		}
	}
	return -1;
}

int
index_of_string(char* a[], int num_elements, char* value)
{
	int i;
	for(i = 0; i < num_elements; i++) {
		if(strcmp(a[i], value) == 0) {
			return i;
		}
	}
	return -1;
}

float
clock_since(clock_t start)
{
	double cpu_time_used = ((double)(clock() - start)) / CLOCKS_PER_SEC;
	return cpu_time_used * 1000;
}

char*
nowstr(void)
{
	time_t now;
	time(&now);
	return ctime(&now);
}

void
fputs_lifeline(FILE* f, int limit_from, int limit_to, int range_from,
               int range_to, int len)
{
	int i;
	float f_len = len - 1;
	bool init = false;
	for(i = 0; i < len; i++) {
		float epoch = (i / f_len) * (limit_to - limit_from) + limit_from;
		if(epoch > range_from && !init) {
			fputs("+", f);
			init = true;
		} else if(epoch >= range_from && epoch <= range_to) {
			fputs("+", f);
		} else {
			fputs("-", f);
		}
	}
}

void
fputs_rfc2822(FILE* f, time_t t)
{
	char rfc_2822[40];
	strftime(rfc_2822, sizeof(rfc_2822), "%a, %d %b %Y 00:00:00 +0900", localtime(&t));
	fprintf(f, "%s", rfc_2822);
}