Subversion Repositories Tewi

Rev

Rev 6 | Rev 20 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 nishi 1
/* $Id: string.c 16 2024-09-13 15:09:52Z nishi $ */
3 nishi 2
 
3
#include <string.h>
4
#include <stdlib.h>
5 nishi 5
#include <stdbool.h>
6
#include <ctype.h>
3 nishi 7
 
8
char* cm_strcat(const char* a, const char* b) {
9
	char* str = malloc(strlen(a) + strlen(b) + 1);
10
	memcpy(str, a, strlen(a));
11
	memcpy(str + strlen(a), b, strlen(b));
12
	str[strlen(a) + strlen(b)] = 0;
13
	return str;
14
}
15
 
16 nishi 16
char* cm_strcat3(const char* a, const char* b, const char* c) {
17
	char* tmp = cm_strcat(a, b);
18
	char* str = cm_strcat(tmp, c);
19
	free(tmp);
20
	return str;
21
}
22
 
3 nishi 23
char* cm_strdup(const char* str) { return cm_strcat(str, ""); }
4 nishi 24
 
6 nishi 25
char* cm_trimstart(const char* str) {
4 nishi 26
	int i;
6 nishi 27
	for(i = 0; str[i] != 0; i++) {
28
		if(str[i] != ' ' && str[i] != '\t') {
4 nishi 29
			return cm_strdup(str + i);
30
		}
31
	}
32
	return cm_strdup("");
33
}
34
 
6 nishi 35
char* cm_trimend(const char* str) {
4 nishi 36
	char* s = cm_strdup(str);
37
	int i;
6 nishi 38
	for(i = strlen(s) - 1; i >= 0; i--) {
39
		if(s[i] != '\t' && s[i] != ' ') {
4 nishi 40
			s[i + 1] = 0;
41
			break;
42
		}
43
	}
44
	return s;
45
}
46
 
6 nishi 47
char* cm_trim(const char* str) {
4 nishi 48
	char* tmp = cm_trimstart(str);
49
	char* s = cm_trimend(tmp);
50
	free(tmp);
51
	return s;
52
}
5 nishi 53
 
6 nishi 54
char** cm_split(const char* str, const char* by) {
5 nishi 55
	int i;
56
	char** r = malloc(sizeof(*r));
57
	r[0] = NULL;
58
	char* b = malloc(1);
59
	b[0] = 0;
60
	char cbuf[2];
61
	cbuf[1] = 0;
62
	bool dq = false;
63
	bool sq = false;
6 nishi 64
	for(i = 0;; i++) {
5 nishi 65
		int j;
66
		bool has = false;
6 nishi 67
		for(j = 0; by[j] != 0; j++) {
68
			if(by[j] == str[i]) {
5 nishi 69
				has = true;
70
				break;
71
			}
72
		}
6 nishi 73
		if(!(dq || sq) && (has || str[i] == 0)) {
74
			if(strlen(b) > 0) {
5 nishi 75
				char** old = r;
76
				int j;
6 nishi 77
				for(j = 0; old[j] != NULL; j++)
78
					;
5 nishi 79
				r = malloc(sizeof(*r) * (j + 2));
80
				for(j = 0; old[j] != NULL; j++) r[j] = old[j];
81
				r[j] = b;
82
				r[j + 1] = NULL;
83
				free(old);
84
			}
85
			b = malloc(1);
86
			b[0] = 0;
87
			if(str[i] == 0) break;
6 nishi 88
		} else {
89
			if(str[i] == '"' && !sq) {
5 nishi 90
				dq = !dq;
6 nishi 91
			} else if(str[i] == '\'' && !dq) {
5 nishi 92
				sq = !sq;
6 nishi 93
			} else {
5 nishi 94
				cbuf[0] = str[i];
95
				char* tmp = b;
96
				b = cm_strcat(tmp, cbuf);
97
				free(tmp);
98
			}
99
		}
100
	}
101
	free(b);
102
	return r;
103
}
104
 
6 nishi 105
bool cm_strcaseequ(const char* a, const char* b) {
5 nishi 106
	if(a == NULL) return false;
107
	if(b == NULL) return false;
108
	if(strlen(a) != strlen(b)) return false;
109
	int i;
6 nishi 110
	for(i = 0; a[i] != 0; i++) {
5 nishi 111
		if(tolower(a[i]) != tolower(b[i])) return false;
112
	}
113
	return true;
114
}