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

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