forked from aterrien/forp-PHP-profiler
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforp_annotation.c
More file actions
160 lines (146 loc) · 4.68 KB
/
forp_annotation.c
File metadata and controls
160 lines (146 loc) · 4.68 KB
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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2011 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Anthony Terrien <forp@anthonyterrien.com> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "forp_string.h"
#include "forp_annotation.h"
/* {{{ forp_annotation_args
*
* Parses args of an annotation
*
* @param char* str
* @param char*** args
* @param int* args_count
* @return void
*/
void forp_annotation_args(char *str, char ***args, int *args_count TSRMLS_DC) {
int esc = 0, buf = 0, i = 0, j = 0;
char *ex;
*args_count = 0;
if(strlen(str) > 0) {
ex = malloc(sizeof(char*));
while(str[i] != '\0') {
if(!esc) {
if(str[i] == '\\') {
esc = 1;
}
if((!esc) && str[i] == '"') {
if(buf && j > 0) {
ex = realloc(ex, sizeof(char*) * (j + 1));
ex[j] = '\0';
(*args)[(*args_count)] = strdup(ex);
(*args_count)++;
memset(ex, 0, sizeof(char*));
j = 0;
}
buf = !buf;
} else {
if(buf) {
ex = realloc(ex, sizeof(char*) * (j + 1));
ex[j] = str[i];
j++;
}
}
} else {
if(buf) {
ex = realloc(ex, sizeof(char*) * (j + 1));
ex[j] = str[i];
j++;
}
esc = 0;
}
i++;
}
//if(buf) {
//ex[j] = '\0';
//printf("NOT CLOSED \"!:|%s|\n", ex);
//}
free(ex);
}
}
/* }}} */
/* {{{ forp_annotation_tok
*
* Handles annotations in doc_comment : @<tag>(<params>)<\n>
*
* @param char* doc_comment
* @param char* tag
* @return char*
*/
char *forp_annotation_tok(const char *doc_comment, char *tag TSRMLS_DC) {
char *v = NULL, *v_search = NULL, *t_start = NULL, *tmp = NULL, *eot = NULL;
unsigned int v_start, v_end;
v_search = malloc(sizeof(char*) * (strlen(tag) + 3));
if(v_search) {
sprintf(v_search, "@%s(", tag);
t_start = strstr(doc_comment, v_search);
if (t_start) {
v_start = t_start - doc_comment + strlen(v_search);
tmp = strndup(doc_comment + v_start, strlen(doc_comment));
eot = strstr(tmp, ")");
v_end = eot - tmp;
v = strndup(doc_comment + v_start, v_end);
}
free(v_search);
}
return v;
}
/* }}} */
/* {{{ forp_annotation_string
*
* Retrieves string arg in an annotation
*
* @param char* doc_comment
* @param char* tag
* @return char*
*/
char *forp_annotation_string(const char *doc_comment, char *tag TSRMLS_DC) {
int args_count;
char *v = NULL;
char **args = malloc(sizeof(char*));
char *args_str = forp_annotation_tok(doc_comment, tag TSRMLS_CC);
if(args_str != NULL) {
forp_annotation_args(args_str, &args, &args_count TSRMLS_CC);
if(args_count > 0) {
v = strdup(args[0]);
}
}
free(args);
return v;
}
/* }}} */
/* {{{ forp_annotation_array
*
* Retrieves args array in an annotation
*
* @param char* doc_comment
* @param char* tag
* @param char*** args
* @param int* args_count
* @return void
*/
void forp_annotation_array(const char *doc_comment, char *tag, char ***args, int *args_count TSRMLS_DC) {
char *args_str = forp_annotation_tok(doc_comment, tag TSRMLS_CC);
if(args_str != NULL) {
forp_annotation_args(args_str, args, args_count TSRMLS_CC);
}
}
/* }}} */