Menu

[r691]: / branches / Release-2-0-6-branch / php-java-bridge / parser.c  Maximize  Restore  History

Download this file

156 lines (146 with data), 3.6 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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include "php_java.h"
#include "parser.h"
#define RECV_SIZE 8192 // initial size of the receive buffer
#define MAX_ARGS 100 // max # of method arguments
#define APPEND(c) { \
if(i>=(*env)->len-1) { \
unsigned char* s1=realloc(s, (*env)->len*=2); \
if(!s1) return 1; else s=(*env)->s=s1; \
} \
s[i++]=c; \
}
#define PUSH(t) { \
parser_string_t *str = tag[t].strings; \
short n = tag[t].n; \
assert((t!=0) || (t==0 && !tag[0].n)); \
s[i]=0; \
str[n].string=&((*env)->s); \
str[n].off=i0; \
str[n].length=i-i0; \
++tag[t].n; \
APPEND(0); \
i0=i; \
}
#define RESET() { \
type=VOJD;\
mask=~(unsigned char)0; \
level=0; \
eor=0; \
blen=0; \
in_dquote=0; \
i=0; \
i0=0; \
}
#define CALL_BEGIN() { \
(*cb->begin)(tag, cb); \
}
#define CALL_END() { \
if(cb->end) (*cb->end)(tag[0].strings, cb); \
}
short parse(proxyenv *env, parser_cb_t *cb) {
parser_string_t v1[1], v2[MAX_ARGS], v3[MAX_ARGS];
parser_tag_t tag[] = {{0, v1}, {0, v2}, {0, v3}};
unsigned char buf[RECV_SIZE];
unsigned char ch, mask=~(unsigned char)0;
// VOJD is VOID for f... windows (VOID is in winsock2.h)
enum {BEGIN, KEY, VAL, ENTITY, BLOB, VOJD, END} type = VOJD;
short level=0, in_dquote=0, eor=0, blen=0;
ssize_t pos=0, c=0; size_t i=0, i0=0, e;
unsigned char *s=(*env)->s;
assert(s); if(!s) return 1;
while(!eor) {
if(c==pos) {
res:
errno=0;
pos=recv((*env)->peer, buf, RECV_SIZE, 0);
if(!pos && errno==EINTR) goto res; // Solaris, see INN FAQ
if(pos<=0) break;
c=0;
}
switch((ch=buf[c])&mask)
{/* --- This block must be compilable with an ansi C compiler or javac --- */
case '<': if(in_dquote) {APPEND(ch); break;}
level++;
type=BEGIN;
break;
case '\t': case '\f': case '\n': case '\r': case ' ': if(in_dquote) {APPEND(ch); break;}
if(type==BEGIN) {
PUSH(type);
type = KEY;
}
break;
case '=': if(in_dquote) {APPEND(ch); break;}
PUSH(type);
type=VAL;
break;
case '/': if(in_dquote) {APPEND(ch); break;}
if(type==BEGIN) { type=END; level--; }
level--;
break;
case '>': if(in_dquote) {APPEND(ch); break;}
if(type==END){
PUSH(BEGIN);
CALL_END();
} else {
if(type==VAL) PUSH(type);
CALL_BEGIN();
}
tag[0].n=tag[1].n=tag[2].n=0; i0=i=0; /* RESET */
type=VOJD;
if(level==0) eor=1;
break;
case ';':
if(type==ENTITY) {
switch (s[e+1]) {
case 'l': s[e]='<'; i=e+1; break; /* lt */
case 'g': s[e]='>'; i=e+1; break; /* gt */
case 'a': s[e]= (s[e+2]=='m'?'&':'\''); i=e+1; break; /* amp, apos */
case 'q': s[e]='"'; i=e+1; break; /* quot */
default: APPEND(ch);
}
type=VAL; //& escapes may only appear in values
} else {
APPEND(ch);
}
break;
case '&':
type = ENTITY;
e=i;
APPEND(ch);
break;
case '"':
in_dquote = !in_dquote;
if(!in_dquote && type==VAL) {
PUSH(type);
type = KEY;
}
break;
default:
APPEND(ch);
}
c++;
}
RESET();
return 0;
}
#if 0
void begin(parser_tag_t tag[3], struct parser_cb *cb){
int i;
for(i=0; i<tag[2].n; i++)
printf("<begin: %s, %s (%d, %d)\n", tag[1].strings[i].string, tag[2].strings[i].string, tag[1].strings[i].length, tag[2].strings[i].length);
}
void end(parser_string_t str[1], struct parser_cb *cb){
printf("<end> %s (%d)\n", str[0].string, str[0].length);
}
main() {
parser_cb_t cb={begin, end, 0};
FILE *f=fopen("xmltest.xml", "r");
parse(f, &cb);
fclose(f);
}
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.