Subversion Repositories Tewi

Rev

Rev 16 | Rev 21 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 16 Rev 20
Line 1... Line 1...
1
/* $Id: string.c 16 2024-09-13 15:09:52Z nishi $ */
1
/* $Id: string.c 20 2024-09-14 09:59:15Z nishi $ */
2
 
2
 
3
#include <string.h>
3
#include <string.h>
4
#include <stdlib.h>
4
#include <stdlib.h>
5
#include <stdbool.h>
5
#include <stdbool.h>
-
 
6
#include <stdio.h>
6
#include <ctype.h>
7
#include <ctype.h>
7
 
8
 
8
char* cm_strcat(const char* a, const char* b) {
9
char* cm_strcat(const char* a, const char* b) {
9
	char* str = malloc(strlen(a) + strlen(b) + 1);
10
	char* str = malloc(strlen(a) + strlen(b) + 1);
10
	memcpy(str, a, strlen(a));
11
	memcpy(str, a, strlen(a));
Line 110... Line 111...
110
	for(i = 0; a[i] != 0; i++) {
111
	for(i = 0; a[i] != 0; i++) {
111
		if(tolower(a[i]) != tolower(b[i])) return false;
112
		if(tolower(a[i]) != tolower(b[i])) return false;
112
	}
113
	}
113
	return true;
114
	return true;
114
}
115
}
-
 
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
}