[2] | 1 | /* $Id: string.c 21 2024-09-14 12:39:39Z nishi $ */
|
---|
[3] | 2 |
|
---|
| 3 | #include <string.h>
|
---|
| 4 | #include <stdlib.h>
|
---|
[5] | 5 | #include <stdbool.h>
|
---|
[20] | 6 | #include <stdio.h>
|
---|
[5] | 7 | #include <ctype.h>
|
---|
[3] | 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] | 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] | 24 | char* cm_strdup(const char* str) { return cm_strcat(str, ""); }
|
---|
[4] | 25 |
|
---|
[6] | 26 | char* cm_trimstart(const char* str) {
|
---|
[4] | 27 | int i;
|
---|
[6] | 28 | for(i = 0; str[i] != 0; i++) {
|
---|
| 29 | if(str[i] != ' ' && str[i] != '\t') {
|
---|
[4] | 30 | return cm_strdup(str + i);
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 | return cm_strdup("");
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[6] | 36 | char* cm_trimend(const char* str) {
|
---|
[4] | 37 | char* s = cm_strdup(str);
|
---|
| 38 | int i;
|
---|
[6] | 39 | for(i = strlen(s) - 1; i >= 0; i--) {
|
---|
| 40 | if(s[i] != '\t' && s[i] != ' ') {
|
---|
[4] | 41 | s[i + 1] = 0;
|
---|
| 42 | break;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | return s;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[6] | 48 | char* cm_trim(const char* str) {
|
---|
[4] | 49 | char* tmp = cm_trimstart(str);
|
---|
| 50 | char* s = cm_trimend(tmp);
|
---|
| 51 | free(tmp);
|
---|
| 52 | return s;
|
---|
| 53 | }
|
---|
[5] | 54 |
|
---|
[6] | 55 | char** cm_split(const char* str, const char* by) {
|
---|
[5] | 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] | 65 | for(i = 0;; i++) {
|
---|
[5] | 66 | int j;
|
---|
| 67 | bool has = false;
|
---|
[6] | 68 | for(j = 0; by[j] != 0; j++) {
|
---|
| 69 | if(by[j] == str[i]) {
|
---|
[5] | 70 | has = true;
|
---|
| 71 | break;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
[6] | 74 | if(!(dq || sq) && (has || str[i] == 0)) {
|
---|
| 75 | if(strlen(b) > 0) {
|
---|
[5] | 76 | char** old = r;
|
---|
| 77 | int j;
|
---|
[6] | 78 | for(j = 0; old[j] != NULL; j++)
|
---|
| 79 | ;
|
---|
[5] | 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] | 89 | } else {
|
---|
| 90 | if(str[i] == '"' && !sq) {
|
---|
[5] | 91 | dq = !dq;
|
---|
[6] | 92 | } else if(str[i] == '\'' && !dq) {
|
---|
[5] | 93 | sq = !sq;
|
---|
[6] | 94 | } else {
|
---|
[5] | 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] | 106 | bool cm_strcaseequ(const char* a, const char* b) {
|
---|
[5] | 107 | if(a == NULL) return false;
|
---|
| 108 | if(b == NULL) return false;
|
---|
| 109 | if(strlen(a) != strlen(b)) return false;
|
---|
| 110 | int i;
|
---|
[6] | 111 | for(i = 0; a[i] != 0; i++) {
|
---|
[5] | 112 | if(tolower(a[i]) != tolower(b[i])) return false;
|
---|
| 113 | }
|
---|
| 114 | return true;
|
---|
| 115 | }
|
---|
[20] | 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, "&");
|
---|
| 145 | free(tmp);
|
---|
| 146 | } else if(str[i] == '<') {
|
---|
| 147 | char* tmp = result;
|
---|
| 148 | result = cm_strcat(tmp, "<");
|
---|
| 149 | free(tmp);
|
---|
| 150 | } else if(str[i] == '>') {
|
---|
| 151 | char* tmp = result;
|
---|
| 152 | result = cm_strcat(tmp, ">");
|
---|
| 153 | free(tmp);
|
---|
| 154 | } else {
|
---|
| 155 | char* tmp = result;
|
---|
| 156 | result = cm_strcat(tmp, cbuf);
|
---|
| 157 | free(tmp);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | return result;
|
---|
| 161 | }
|
---|
[21] | 162 |
|
---|
| 163 | char* cm_url_escape(const char* str) {
|
---|
| 164 | int i;
|
---|
| 165 | char* result = malloc(1);
|
---|
| 166 | result[0] = 0;
|
---|
| 167 | char cbuf[2];
|
---|
| 168 | cbuf[1] = 0;
|
---|
| 169 | for(i = 0; str[i] != 0; i++) {
|
---|
| 170 | cbuf[0] = str[i];
|
---|
| 171 | if('!' <= str[i] && str[i] <= '@' && str[i] != '.' && str[i] != '-' && str[i] != '/' && !('0' <= str[i] && str[i] <= '9')) {
|
---|
| 172 | char code[4];
|
---|
| 173 | sprintf(code, "%%%02X", str[i]);
|
---|
| 174 | char* tmp = result;
|
---|
| 175 | result = cm_strcat(tmp, code);
|
---|
| 176 | free(tmp);
|
---|
| 177 | } else {
|
---|
| 178 | char* tmp = result;
|
---|
| 179 | result = cm_strcat(tmp, cbuf);
|
---|
| 180 | free(tmp);
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | return result;
|
---|
| 184 | }
|
---|