summaryrefslogtreecommitdiff
path: root/misc.c
blob: 771ca5655a59ad53b9c694810814e7076e2f256e (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
#include <ctype.h>
#include <dirent.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "misc.h"

char *read_file(const char *filename, long *size) {
	/* Open the file. */
	FILE *fp = fopen(filename, "r");
	/* Size of the file, in bytes. */
	long filesize = 0;
	/* Buffer of the file contents. */
	char *buf;

	/* Return NULL, if we couldn't open the file. */
	if (fp == NULL) {
		return NULL;
	}

	/* Return NULL, if we couldn't seek to the end of the file. */
	if (fseek(fp, 0L, SEEK_END)) {
		fclose(fp);
		return NULL;
	}

	/* Get the size of the file, in bytes. */
	filesize = ftell(fp);

	/* Return NULL, if the returned size is negative. */
	if (filesize < 0) {
		fclose(fp);
		return NULL;
	}

	/* Allocate enough space for the entire file, plus one. */
	buf = calloc(filesize+1, sizeof(char));

	/* Return NULL, if the buffer wasn't allocated. */
	if (buf == NULL) {
		fclose(fp);
		return NULL;
	}

	/* Seek back to the start of the file. */
	rewind(fp);
	/* Read the entire file contents into the buffer. */
	fread(buf, sizeof(char), filesize, fp);
	/* Close the file. */
	fclose(fp);

	/* Return the filesize, in bytes. */
	*size = filesize;
	/* Return the buffer. */
	return buf;
}

char *get_line(char **str) {
	if (str == NULL || is_empty(*str)) {
		return NULL;
	} else {
		size_t i = strcspn(*str, "\n");
		char *s = calloc(i+1, sizeof(char));

		memcpy(s, *str, i);

		*str += (i+1);
		return s;
	}
}

static int escape_span(char *str, char *cursor, const char *delm) {
	if (!is_empty(str) && !is_empty(cursor)) {
		char *s;
		if (*str == '\\') {
			const int span = strspn(str, "\\");
			if (&str[span] == cursor) {
				return span;
			}
		}

		for (s = cursor-1; s > str && *s == '\\'; --s);
		return (cursor-s)-1;
	} else {
		return -1;
	}
}

char *find_delm(char *str, const char *delm, int skip_delm) {
	if (!is_empty(str)) {
		while (*str != '\0') {
			char *s = str;
			str += strcspn(str, delm);

			if (str[-1] == '\\') {
				const int span = escape_span(s, str, delm);
				if (span > 0 && span % 2) {
					str += strspn(++str, delm);
					continue;
				}
			}
			break;
		}
		return (skip_delm) ? &str[strspn(str, delm)] : str;
	} else {
		return NULL;
	}
}

char *remove_trailing_whitespace(char *str) {
	const char *whitespace = " \t\v\r\n";
	char *s = str;

	for (char *tmp = s; !is_empty(tmp); s = tmp, tmp = find_delm(tmp, whitespace, 1));

	*find_delm(s, whitespace, 0) = '\0';
	return str;
}

char *get_str_delm_range(char *str, const char *start_delm, const char *end_delm, char **rhs) {
	if (!is_empty(str)) {
		char *start = find_delm(str, start_delm, 1);
		char *end = find_delm(start, end_delm, 0);
		char *dummy;
		rhs = (rhs != NULL) ? rhs : &dummy;

		*end++ = '\0';
		*rhs = &end[strspn(end, end_delm)];
		return start;
	} else {
		return NULL;
	}
}

char *make_str(const char *str) {
	const size_t length = strlen(str);
	char *s = calloc(length+1, sizeof(char));
	memcpy(s, str, length+1);
	return s;
}

char *dir_path_num(const char *root, int num) {
	/* Get the length of the path. */
	int len = snprintf(NULL, 0, "%s/%i", root, num);
	/* Create the directory path. */
	char *dir = calloc(len+1, sizeof(char));
	sprintf(dir, "%s/%i", root, num);
	return dir;
}

char *dir_path_name(const char *root, char *name) {
	if (!is_empty(root)) {
		if (!is_empty(name)) {
			/* Create the directory path. */
			char *dir = calloc(format_len("%s/%s", root, name)+1, sizeof(char));
			sprintf(dir, "%s/%s", root, name);
			return dir;
		} else {
			return make_str(root);
		}
	} else {
		return NULL;
	}
}

int delm_span(char *str, const char delm) {
	int i;
	for (i = 0; str[i] == delm; i++);
	return i;
}

int sanitize_strlen(char *str) {
	int len = 0;
	while (*str != '\0') {
		const int tok_span = strcspn(str, " .");
		const int tok_len = (tok_span) ? tok_span : 1;
		const char delm = (tok_span) ? str[tok_len] : '\0';
		const int span_len = (delm != '\0') ? delm_span(&str[tok_len+1], delm) : 0;
		str += (tok_len + span_len);
		len += tok_len;
	}
	return len;
}

char *sanitize_str(char *str) {
	const int len = sanitize_strlen(str);
	char *san_str = calloc(len+1, sizeof(char));
	char *tmp = san_str;
	while (*str != '\0' && tmp < &san_str[len]) {
		const int tok_span = strcspn(str, " .");
		const int tok_len = (tok_span) ? tok_span : 1;
		const char delm = (tok_span) ? str[tok_len] : '\0';
		const int span_len = (delm != '\0') ? delm_span(&str[tok_len+1], delm) : 0;
		memcpy(tmp, str, tok_len);
		tmp += tok_len;
		str += (tok_len + span_len);
		*tmp = (delm == ' ') ? '-' : delm;
	}
	return san_str;
}

char *sanitized_dir_path_name(const char *root, char *name) {
	char *san_name = sanitize_str(name);
	char *dir = dir_path_name(root, san_name);
	free(san_name);
	return dir;
}

char *find_alpha(const char *str) {
	for (; !isalpha(*str); str++);
	return (char *)str;
}

char *create_num_str(const char *str, int num) {
	char *name;
	int lead_num = strtol(str, &name, 10);
	const char *sep = (*name != '-') ? "-" : "";
	const char *s = find_alpha(name);

	name = calloc(format_len("%04d%s%s", num, sep, s), sizeof(char));
	sprintf(name, "%04d%s%s", num, sep, str);
	return name;
}

int is_empty(const char *str) {
	return (str == NULL) || (*str == '\0');
}

char *skip_whitespace(const char *str) {
	if (is_empty(str)) {
		return NULL;
	} else {
		const size_t span = strspn(str, " \t\v\r\n");
		return (char *)&str[span];
	}
}

int vformat_len_copy(const char *fmt, va_list args) {
	int len;
	va_list tmp_args;
	va_copy(tmp_args, args);
	len = vformat_len(fmt, tmp_args);
	va_end(tmp_args);
	return len;
}

int vformat_len(const char *fmt, va_list args) {
	return vsnprintf(NULL, 0, fmt, args);
}

int format_len(const char *fmt, ...) {
	int len;
	va_list args;
	va_start(args, fmt);
	len = vformat_len(fmt, args);
	va_end(args);
	return len;
}

int is_dir(const char *path) {
	DIR *dir;

	if (!access(path, F_OK)) {
		if ((dir = opendir(path)) != NULL) {
			closedir(dir);
			return 1;
		} else {
			return 0;
		}
	} else {
		return -1;
	}
}

void mkdirp(const char *path, int mode) {
	char *str = make_str(path);

	for (char *p = find_delm(str, "/", *str == '/'); !is_empty(p); p = find_delm(p, "/", *p == '/')) {
		const size_t span = strspn(p, "/");
		if (*p == '/') {
			*p = '\0';
		}
		if (is_dir(str) < 0 && mkdir(str, mode) < 0) {
			break;
		}
		if (*p == '\0') {
			*p = '/';
			if (*find_delm(&p[span], "/", 0) != '\0') {
				p += span;
			}
		}
	}

	free(str);
}