source: Main/trunk/Common/string.c@ 20

Last change on this file since 20 was 20, checked in by Nishi, on Sep 14, 2024 at 6:59:15 PM

can show index

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1/* $Id: string.c 20 2024-09-14 09:59:15Z nishi $ */
2
3#include <string.h>
4#include <stdlib.h>
5#include <stdbool.h>
6#include <stdio.h>
7#include <ctype.h>
8
9char* 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
17char* 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
24char* cm_strdup(const char* str) { return cm_strcat(str, ""); }
25
26char* cm_trimstart(const char* str) {
27 int i;
28 for(i = 0; str[i] != 0; i++) {
29 if(str[i] != ' ' && str[i] != '\t') {
30 return cm_strdup(str + i);
31 }
32 }
33 return cm_strdup("");
34}
35
36char* cm_trimend(const char* str) {
37 char* s = cm_strdup(str);
38 int i;
39 for(i = strlen(s) - 1; i >= 0; i--) {
40 if(s[i] != '\t' && s[i] != ' ') {
41 s[i + 1] = 0;
42 break;
43 }
44 }
45 return s;
46}
47
48char* cm_trim(const char* str) {
49 char* tmp = cm_trimstart(str);
50 char* s = cm_trimend(tmp);
51 free(tmp);
52 return s;
53}
54
55char** cm_split(const char* str, const char* by) {
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;
65 for(i = 0;; i++) {
66 int j;
67 bool has = false;
68 for(j = 0; by[j] != 0; j++) {
69 if(by[j] == str[i]) {
70 has = true;
71 break;
72 }
73 }
74 if(!(dq || sq) && (has || str[i] == 0)) {
75 if(strlen(b) > 0) {
76 char** old = r;
77 int j;
78 for(j = 0; old[j] != NULL; j++)
79 ;
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;
89 } else {
90 if(str[i] == '"' && !sq) {
91 dq = !dq;
92 } else if(str[i] == '\'' && !dq) {
93 sq = !sq;
94 } else {
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
106bool cm_strcaseequ(const char* a, const char* b) {
107 if(a == NULL) return false;
108 if(b == NULL) return false;
109 if(strlen(a) != strlen(b)) return false;
110 int i;
111 for(i = 0; a[i] != 0; i++) {
112 if(tolower(a[i]) != tolower(b[i])) return false;
113 }
114 return true;
115}
116
117int 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
134char* 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}
Note: See TracBrowser for help on using the repository browser.