This repository has been archived by the owner on Dec 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ytrace_str.c
192 lines (163 loc) · 4.04 KB
/
ytrace_str.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* Copyright 2017 Rokety Yang <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "php.h"
#include "ext/standard/php_string.h"
#include "ytrace_str.h"
void ytrace_str_new(ytrace_str *ys)
{
ys->c = (char*)malloc(sizeof(char)*YTRACE_STR_START_SIZE);
ys->c[0] = '\0';
ys->len = 0;
ys->a = YTRACE_STR_START_SIZE;
}
void ytrace_str_append(ytrace_str *ys, char *str, int f)
{
if (!str) return;
int l = strlen(str);
if (ys->len + l > ys->a - 1) {
ys->c = (char*)realloc(ys->c, ys->a + l + YTRACE_STR_PREALLOC);
ys->a = ys->a + l + YTRACE_STR_PREALLOC;
}
memcpy(ys->c + ys->len, str, l);
ys->c[ys->len + l] = '\0';
ys->len = ys->len + l;
if (f) {
free(str);
}
}
void ytrace_str_appendl(ytrace_str *ys, char *str, int l, int f)
{
if (!str) return;
if (ys->len + l > ys->a - 1) {
ys->c = (char*)realloc(ys->c, ys->a + l + YTRACE_STR_PREALLOC);
ys->a = ys->a + l + YTRACE_STR_PREALLOC;
}
memcpy(ys->c + ys->len, str, l);
ys->c[ys->len + l] = '\0';
ys->len = ys->len + l;
if (f) {
free(str);
}
}
void ytrace_str_chop(ytrace_str *ys, int c)
{
if (c <= ys->len) {
ys->len -= c;
ys->c[ys->len] = '\0';
}
}
void ytrace_str_destroy(ytrace_str *ys)
{
if (ys->c) {
free(ys->c);
}
ys->len = 0;
ys->a = 0;
}
char* ytrace_str_addcslashes(char* str, int str_len, int *new_len)
{
#if PHP_VERSION_ID >= 70000
zend_string *i_string = zend_string_init(str, str_len, 0);
zend_string *tmp_zstr;
tmp_zstr = php_addcslashes(i_string, 0, "'\\\0..\37", 6);
char *tmp_str = estrndup(tmp_zstr->val, tmp_zstr->len);
*new_len = tmp_zstr->len;
zend_string_release(tmp_zstr);
zend_string_release(i_string);
return tmp_str;
#else
return php_addcslashes(str, str_len, new_len, 0, "'\\\0..\37", 6 TSRMLS_CC);
#endif
}
char* ytrace_str_tab_pad(int len)
{
int i;
char *str = (char*)malloc(len);
for (i = 0; i < len; i++) {
str[i] = '\t';
}
return str;
}
// from https://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c
char** ytrace_str_split(char* a_str, const char a_delim)
{
a_str = strdup(a_str);
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
free(a_str);
return result;
}
char *ytrace_sprintf(const char *fmt, ...)
{
int size = 0;
char *p = NULL;
va_list ap;
/* Determine required size */
va_start(ap, fmt);
size = vsnprintf(p, size, fmt, ap);
va_end(ap);
if (size < 0)
return NULL;
size++; /* For '\0' */
p = malloc(size);
if (p == NULL)
return NULL;
va_start(ap, fmt);
size = vsnprintf(p, size, fmt, ap);
if (size < 0) {
free(p);
return NULL;
}
va_end(ap);
return p;
}