Subversion Repositories Tewi

Rev

Rev 16 | Rev 21 | 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 20 2024-09-14 09:59:15Z nishi $ */
3 nishi 2
 
3
#include <string.h>
4
#include <stdlib.h>
5 nishi 5
#include <stdbool.h>
20 nishi 6
#include <stdio.h>
5 nishi 7
#include <ctype.h>
3 nishi 8
 
9
char* cm_strcat(const char* a, const char* b) {
10
	char* str = malloc(strlen(a) + strlen(b) + 1);
11
	memcpy(str, a, strlen(a));
12
	memcpy(str + strlen(a), b, strlen(b));
13
	str[strlen(a) + strlen(b)] = 0;
14
	return str;
15
}
16
 
16 nishi 17
char* cm_strcat3(const char* a, const char* b, const char* c) {
18
	char* tmp = cm_strcat(a, b);
19
	char* str = cm_strcat(tmp, c);
20
	free(tmp);
21
	return str;
22
}
23
 
3 nishi 24
char* cm_strdup(const char* str) { return cm_strcat(str, ""); }
4 nishi 25
 
6 nishi 26
char* cm_trimstart(const char* str) {
4 nishi 27
	int i;
6 nishi 28
	for(i = 0; str[i] != 0; i++) {
29
		if(str[i] != ' ' && str[i] != '\t') {
4 nishi 30
			return cm_strdup(str + i);
31
		}
32
	}
33
	return cm_strdup("");
34
}
35
 
6 nishi 36
char* cm_trimend(const char* str) {
4 nishi 37
	char* s = cm_strdup(str);
38
	int i;
6 nishi 39
	for(i = strlen(s) - 1; i >= 0; i--) {
40
		if(s[i] != '\t' && s[i] != ' ') {
4 nishi 41
			s[i + 1] = 0;
42
			break;
43
		}
44
	}
45
	return s;
46
}
47
 
6 nishi 48
char* cm_trim(const char* str) {
4 nishi 49
	char* tmp = cm_trimstart(str);
50
	char* s = cm_trimend(tmp);
51
	free(tmp);
52
	return s;
53
}
5 nishi 54
 
6 nishi 55
char** cm_split(const char* str, const char* by) {
5 nishi 56
	int i;
57
	char** r = malloc(sizeof(*r));
58
	r[0] = NULL;
59
	char* b = malloc(1);
60
	b[0] = 0;
61
	char cbuf[2];
62
	cbuf[1] = 0;
63
	bool dq = false;
64
	bool sq = false;
6 nishi 65
	for(i = 0;; i++) {
5 nishi 66
		int j;
67
		bool has = false;
6 nishi 68
		for(j = 0; by[j] != 0; j++) {
69
			if(by[j] == str[i]) {
5 nishi 70
				has = true;
71
				break;
72
			}
73
		}
6 nishi 74
		if(!(dq || sq) && (has || str[i] == 0)) {
75
			if(strlen(b) > 0) {
5 nishi 76
				char** old = r;
77
				int j;
6 nishi 78
				for(j = 0; old[j] != NULL; j++)
79
					;
5 nishi 80
				r = malloc(sizeof(*r) * (j + 2));
81
				for(j = 0; old[j] != NULL; j++) r[j] = old[j];
82
				r[j] = b;
83
				r[j + 1] = NULL;
84
				free(old);
85
			}
86
			b = malloc(1);
87
			b[0] = 0;
88
			if(str[i] == 0) break;
6 nishi 89
		} else {
90
			if(str[i] == '"' && !sq) {
5 nishi 91
				dq = !dq;
6 nishi 92
			} else if(str[i] == '\'' && !dq) {
5 nishi 93
				sq = !sq;
6 nishi 94
			} else {
5 nishi 95
				cbuf[0] = str[i];
96
				char* tmp = b;
97
				b = cm_strcat(tmp, cbuf);
98
				free(tmp);
99
			}
100
		}
101
	}
102
	free(b);
103
	return r;
104
}
105
 
6 nishi 106
bool cm_strcaseequ(const char* a, const char* b) {
5 nishi 107
	if(a == NULL) return false;
108
	if(b == NULL) return false;
109
	if(strlen(a) != strlen(b)) return false;
110
	int i;
6 nishi 111
	for(i = 0; a[i] != 0; i++) {
5 nishi 112
		if(tolower(a[i]) != tolower(b[i])) return false;
113
	}
114
	return true;
115
}
20 nishi 116
 
117
int cm_hex(const char* str, int len) {
118
	int n = 0;
119
	int i;
120
	for(i = 0; i < len; i++) {
121
		char c = str[i];
122
		n *= 16;
123
		if('0' <= c && c <= '9') {
124
			n += c - '0';
125
		} else if('a' <= c && c <= 'f') {
126
			n += c - 'a' + 10;
127
		} else if('A' <= c && c <= 'F') {
128
			n += c - 'A' + 10;
129
		}
130
	}
131
	return n;
132
}
133
 
134
char* cm_html_escape(const char* str) {
135
	int i;
136
	char* result = malloc(1);
137
	result[0] = 0;
138
	char cbuf[2];
139
	cbuf[1] = 0;
140
	for(i = 0; str[i] != 0; i++) {
141
		cbuf[0] = str[i];
142
		if(str[i] == '&') {
143
			char* tmp = result;
144
			result = cm_strcat(tmp, "&amp;");
145
			free(tmp);
146
		} else if(str[i] == '<') {
147
			char* tmp = result;
148
			result = cm_strcat(tmp, "&lt;");
149
			free(tmp);
150
		} else if(str[i] == '>') {
151
			char* tmp = result;
152
			result = cm_strcat(tmp, "&gt;");
153
			free(tmp);
154
		} else {
155
			char* tmp = result;
156
			result = cm_strcat(tmp, cbuf);
157
			free(tmp);
158
		}
159
	}
160
	return result;
161
}