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

Last change on this file since 16 was 16, checked in by Nishi, on Sep 14, 2024 at 12:09:52 AM

can parse http now

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