-
Notifications
You must be signed in to change notification settings - Fork 3
/
ooString.c
121 lines (103 loc) · 2.07 KB
/
ooString.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* oostring.c
*
* Created on: Aug 1, 2014
* Author: fcuri
*/
#include "ooString.h"
ooPropertyGet(ooString, int, Len) {
return this->len;
}
ooPropertyGet(ooString, ooBoolean, IsEmpty) {
return this->len==0? ooTrue: ooFalse;
}
ooString *ooMethod(ooString, Cat, char *st) {
if (st) {
int l = strlen(st);
if (l) {
void *new = realloc(this->st, this->len + l + 1);
if (!new) {
ooRaise(Malloc);
}
this->st = new;
strcpy(this->st + this->len, st);
this->len += l;
}
}
return(this);
}
ooString *ooMethod(ooString, CatBuffer, char *buf, int l) {
if (buf && l) {
void *new = realloc(this->st, this->len + l + 1);
if (new) {
this->st = new;
memcpy(this->st + this->len, buf, l);
this->len += l;
this->st[this->len]=0;
}
}
return(this);
}
ooString *ooMethod(ooString, Clean) {
if (this->st) {
this->st[0]=0;
this->len=0;
}
return(this);
}
ooString *ooMethod(ooString, Catf, const char *format, ...) {
char *buf = malloc(1024);
if (buf) {
va_list va;
va_start(va, format);
vsnprintf(buf, 1024, format, va);
buf[1023]=0;
va_end(va);
this->Cat(this, buf);
free(buf);
}
return(this);
}
ooBoolean ooMethod(ooString, Equal, char *st) {
return(strcmp(this->st, st) == 0 ? ooTrue: ooFalse);
}
ooString *ooMethod(ooString, Copy, char *st) {
if (st) {
int l = strlen(st);
if (l) {
char *new = (char*) malloc(l + 1);
if (new) {
if (this->st) {
free(this->st);
}
this->st = new;
strcpy(this->st, st);
this->len = l;
}
}
}
return this;
}
int ooMethod(ooString, compare, ooString *with) {
return(strcmp(this->st, with->st));
}
ooCtor(ooString, char *st) {
ooTypeable(ooString);
ooComparable(ooString, compare);
ooMethodInit(ooString, Copy);
ooMethodInit(ooString, Cat);
ooMethodInit(ooString, Catf);
ooMethodInit(ooString, CatBuffer);
ooMethodInit(ooString, Equal);
ooMethodInit(ooString, Clean);
ooPropertyGetInit(ooString, IsEmpty);
ooPropertyGetInit(ooString, Len);
this->Copy(this, st);
return;
}
ooDtor(ooString) {
if (this->st) {
free(this->st);
}
return;
}