ox 2.14.14 → 2.14.17

Sign up to get free protection for your applications and to get access to all the features.
data/ext/ox/sax_buf.c CHANGED
@@ -3,96 +3,94 @@
3
3
  * All rights reserved.
4
4
  */
5
5
 
6
- #include <stdlib.h>
7
6
  #include <errno.h>
8
7
  #include <stdio.h>
8
+ #include <stdlib.h>
9
9
  #include <strings.h>
10
10
  #include <sys/types.h>
11
11
  #if HAVE_SYS_UIO_H
12
12
  #include <sys/uio.h>
13
13
  #endif
14
- #include <unistd.h>
15
14
  #include <time.h>
15
+ #include <unistd.h>
16
16
 
17
- #include "ruby.h"
18
17
  #include "ox.h"
18
+ #include "ruby.h"
19
19
  #include "sax.h"
20
20
 
21
- #define BUF_PAD 4
21
+ #define BUF_PAD 4
22
22
 
23
- static VALUE rescue_cb(VALUE rdr, VALUE err);
24
- static VALUE io_cb(VALUE rdr);
25
- static VALUE partial_io_cb(VALUE rdr);
26
- static int read_from_io(Buf buf);
27
- static int read_from_fd(Buf buf);
28
- static int read_from_io_partial(Buf buf);
29
- static int read_from_str(Buf buf);
23
+ static VALUE rescue_cb(VALUE rdr, VALUE err);
24
+ static VALUE io_cb(VALUE rdr);
25
+ static VALUE partial_io_cb(VALUE rdr);
26
+ static int read_from_io(Buf buf);
27
+ static int read_from_fd(Buf buf);
28
+ static int read_from_io_partial(Buf buf);
29
+ static int read_from_str(Buf buf);
30
30
 
31
- void
32
- ox_sax_buf_init(Buf buf, VALUE io) {
33
- volatile VALUE io_class = rb_obj_class(io);
34
- VALUE rfd;
31
+ void ox_sax_buf_init(Buf buf, VALUE io) {
32
+ volatile VALUE io_class = rb_obj_class(io);
33
+ VALUE rfd;
35
34
 
36
35
  if (rb_cString == io_class) {
37
- buf->read_func = read_from_str;
38
- buf->in.str = StringValuePtr(io);
36
+ buf->read_func = read_from_str;
37
+ buf->in.str = StringValuePtr(io);
39
38
  } else if (ox_stringio_class == io_class && 0 == FIX2INT(rb_funcall2(io, ox_pos_id, 0, 0))) {
40
- volatile VALUE s = rb_funcall2(io, ox_string_id, 0, 0);
39
+ volatile VALUE s = rb_funcall2(io, ox_string_id, 0, 0);
41
40
 
42
- buf->read_func = read_from_str;
43
- buf->in.str = StringValuePtr(s);
41
+ buf->read_func = read_from_str;
42
+ buf->in.str = StringValuePtr(s);
44
43
  } else if (rb_cFile == io_class && Qnil != (rfd = rb_funcall(io, ox_fileno_id, 0))) {
45
- buf->read_func = read_from_fd;
46
- buf->in.fd = FIX2INT(rfd);
44
+ buf->read_func = read_from_fd;
45
+ buf->in.fd = FIX2INT(rfd);
47
46
  } else if (rb_respond_to(io, ox_readpartial_id)) {
48
- buf->read_func = read_from_io_partial;
49
- buf->in.io = io;
47
+ buf->read_func = read_from_io_partial;
48
+ buf->in.io = io;
50
49
  } else if (rb_respond_to(io, ox_read_id)) {
51
- buf->read_func = read_from_io;
52
- buf->in.io = io;
50
+ buf->read_func = read_from_io;
51
+ buf->in.io = io;
53
52
  } else {
54
53
  rb_raise(ox_arg_error_class, "sax_parser io argument must respond to readpartial() or read().\n");
55
54
  }
56
- buf->head = buf->base;
57
- *buf->head = '\0';
58
- buf->end = buf->head + sizeof(buf->base) - BUF_PAD;
59
- buf->tail = buf->head;
55
+ buf->head = buf->base;
56
+ *buf->head = '\0';
57
+ buf->end = buf->head + sizeof(buf->base) - BUF_PAD;
58
+ buf->tail = buf->head;
60
59
  buf->read_end = buf->head;
61
- buf->pro = 0;
62
- buf->str = 0;
63
- buf->pos = 0;
64
- buf->line = 1;
65
- buf->col = 0;
66
- buf->pro_pos = 1;
60
+ buf->pro = 0;
61
+ buf->str = 0;
62
+ buf->pos = 0;
63
+ buf->line = 1;
64
+ buf->col = 0;
65
+ buf->pro_pos = 1;
67
66
  buf->pro_line = 1;
68
- buf->pro_col = 0;
69
- buf->dr = 0;
67
+ buf->pro_col = 0;
68
+ buf->dr = 0;
70
69
  }
71
70
 
72
- int
73
- ox_sax_buf_read(Buf buf) {
74
- int err;
75
- size_t shift = 0;
71
+ int ox_sax_buf_read(Buf buf) {
72
+ int err;
73
+ size_t shift = 0;
76
74
 
77
75
  // if there is not much room to read into, shift or realloc a larger buffer.
78
76
  if (buf->head < buf->tail && 4096 > buf->end - buf->tail) {
79
77
  if (0 == buf->pro) {
80
78
  shift = buf->tail - buf->head;
81
79
  } else {
82
- shift = buf->pro - buf->head - 1; // leave one character so we cab backup one
80
+ shift = buf->pro - buf->head - 1; // leave one character so we cab backup one
83
81
  }
84
- if (0 >= shift) { /* no space left so allocate more */
85
- char *old = buf->head;
86
- size_t size = buf->end - buf->head + BUF_PAD;
82
+ if (0 >= shift) { /* no space left so allocate more */
83
+ char *old = buf->head;
84
+ size_t size = buf->end - buf->head + BUF_PAD;
87
85
 
88
86
  if (buf->head == buf->base) {
89
87
  buf->head = ALLOC_N(char, size * 2);
90
88
  memcpy(buf->head, old, size);
91
89
  } else {
92
- REALLOC_N(buf->head, char, size * 2);
90
+ REALLOC_N(buf->head, char, size * 2);
93
91
  }
94
- buf->end = buf->head + size * 2 - BUF_PAD;
95
- buf->tail = buf->head + (buf->tail - old);
92
+ buf->end = buf->head + size * 2 - BUF_PAD;
93
+ buf->tail = buf->head + (buf->tail - old);
96
94
  buf->read_end = buf->head + (buf->read_end - old);
97
95
  if (0 != buf->pro) {
98
96
  buf->pro = buf->head + (buf->pro - old);
@@ -112,77 +110,71 @@ ox_sax_buf_read(Buf buf) {
112
110
  }
113
111
  }
114
112
  }
115
- err = buf->read_func(buf);
113
+ err = buf->read_func(buf);
116
114
  *buf->read_end = '\0';
117
115
 
118
116
  return err;
119
117
  }
120
118
 
121
- static VALUE
122
- rescue_cb(VALUE rbuf, VALUE err) {
123
- VALUE err_class = rb_obj_class(err);
119
+ static VALUE rescue_cb(VALUE rbuf, VALUE err) {
120
+ VALUE err_class = rb_obj_class(err);
124
121
 
125
122
  if (err_class != rb_eTypeError && err_class != rb_eEOFError) {
126
- Buf buf = (Buf)rbuf;
123
+ Buf buf = (Buf)rbuf;
127
124
 
128
- //ox_sax_drive_cleanup(buf->dr); called after exiting protect
125
+ // ox_sax_drive_cleanup(buf->dr); called after exiting protect
129
126
  rb_raise(err, "at line %ld, column %ld\n", (long)buf->line, (long)buf->col);
130
127
  }
131
128
  return Qfalse;
132
129
  }
133
130
 
134
- static VALUE
135
- partial_io_cb(VALUE rbuf) {
136
- Buf buf = (Buf)rbuf;
137
- VALUE args[1];
138
- VALUE rstr;
139
- char *str;
140
- size_t cnt;
131
+ static VALUE partial_io_cb(VALUE rbuf) {
132
+ Buf buf = (Buf)rbuf;
133
+ VALUE args[1];
134
+ VALUE rstr;
135
+ char *str;
136
+ size_t cnt;
141
137
 
142
138
  args[0] = ULONG2NUM(buf->end - buf->tail);
143
- rstr = rb_funcall2(buf->in.io, ox_readpartial_id, 1, args);
144
- str = StringValuePtr(rstr);
145
- cnt = strlen(str);
146
- //printf("*** read partial %lu bytes, str: '%s'\n", cnt, str);
139
+ rstr = rb_funcall2(buf->in.io, ox_readpartial_id, 1, args);
140
+ str = StringValuePtr(rstr);
141
+ cnt = strlen(str);
142
+ // printf("*** read partial %lu bytes, str: '%s'\n", cnt, str);
147
143
  strcpy(buf->tail, str);
148
144
  buf->read_end = buf->tail + cnt;
149
145
 
150
146
  return Qtrue;
151
147
  }
152
148
 
153
- static VALUE
154
- io_cb(VALUE rbuf) {
155
- Buf buf = (Buf)rbuf;
156
- VALUE args[1];
157
- VALUE rstr;
158
- char *str;
159
- size_t cnt;
149
+ static VALUE io_cb(VALUE rbuf) {
150
+ Buf buf = (Buf)rbuf;
151
+ VALUE args[1];
152
+ VALUE rstr;
153
+ char *str;
154
+ size_t cnt;
160
155
 
161
156
  args[0] = ULONG2NUM(buf->end - buf->tail);
162
- rstr = rb_funcall2(buf->in.io, ox_read_id, 1, args);
163
- str = StringValuePtr(rstr);
164
- cnt = strlen(str);
165
- //printf("*** read %lu bytes, str: '%s'\n", cnt, str);
157
+ rstr = rb_funcall2(buf->in.io, ox_read_id, 1, args);
158
+ str = StringValuePtr(rstr);
159
+ cnt = strlen(str);
160
+ // printf("*** read %lu bytes, str: '%s'\n", cnt, str);
166
161
  strcpy(buf->tail, str);
167
162
  buf->read_end = buf->tail + cnt;
168
163
 
169
164
  return Qtrue;
170
165
  }
171
166
 
172
- static int
173
- read_from_io_partial(Buf buf) {
167
+ static int read_from_io_partial(Buf buf) {
174
168
  return (Qfalse == rb_rescue(partial_io_cb, (VALUE)buf, rescue_cb, (VALUE)buf));
175
169
  }
176
170
 
177
- static int
178
- read_from_io(Buf buf) {
171
+ static int read_from_io(Buf buf) {
179
172
  return (Qfalse == rb_rescue(io_cb, (VALUE)buf, rescue_cb, (VALUE)buf));
180
173
  }
181
174
 
182
- static int
183
- read_from_fd(Buf buf) {
184
- ssize_t cnt;
185
- size_t max = buf->end - buf->tail;
175
+ static int read_from_fd(Buf buf) {
176
+ ssize_t cnt;
177
+ size_t max = buf->end - buf->tail;
186
178
 
187
179
  cnt = read(buf->in.fd, buf->tail, max);
188
180
  if (cnt < 0) {
@@ -194,22 +186,21 @@ read_from_fd(Buf buf) {
194
186
  return 0;
195
187
  }
196
188
 
197
- static int
198
- read_from_str(Buf buf) {
199
- size_t max = buf->end - buf->tail - 1;
200
- char *s;
201
- size_t cnt;
189
+ static int read_from_str(Buf buf) {
190
+ size_t max = buf->end - buf->tail - 1;
191
+ char *s;
192
+ size_t cnt;
202
193
 
203
194
  if ('\0' == *buf->in.str) {
204
- return -1;
195
+ return -1;
205
196
  }
206
197
  cnt = strlen(buf->in.str) + 1;
207
198
  if (max < cnt) {
208
- cnt = max;
199
+ cnt = max;
209
200
  }
210
201
  strncpy(buf->tail, buf->in.str, cnt);
211
- s = buf->tail + cnt - 1;
212
- *s = '\0';
202
+ s = buf->tail + cnt - 1;
203
+ *s = '\0';
213
204
  cnt = s - buf->tail;
214
205
  buf->in.str += cnt;
215
206
  buf->read_end = buf->tail + cnt;
data/ext/ox/sax_buf.h CHANGED
@@ -9,44 +9,45 @@
9
9
  #include <stdio.h>
10
10
 
11
11
  typedef struct _buf {
12
- char base[0x00001000];
13
- char *head;
14
- char *end;
15
- char *tail;
16
- char *read_end; /* one past last character read */
17
- char *pro; /* protection start, buffer can not slide past this point */
18
- char *str; /* start of current string being read */
19
- off_t pos;
20
- off_t line;
21
- off_t col;
22
- off_t pro_pos;
23
- off_t pro_line;
24
- off_t pro_col;
25
- int (*read_func)(struct _buf *buf);
12
+ char base[0x00001000];
13
+ char *head;
14
+ char *end;
15
+ char *tail;
16
+ char *read_end; /* one past last character read */
17
+ char *pro; /* protection start, buffer can not slide past this point */
18
+ char *str; /* start of current string being read */
19
+ off_t pos;
20
+ off_t line;
21
+ off_t col;
22
+ off_t pro_pos;
23
+ off_t pro_line;
24
+ off_t pro_col;
25
+ int (*read_func)(struct _buf *buf);
26
26
  union {
27
- int fd;
28
- VALUE io;
29
- const char *str;
27
+ int fd;
28
+ VALUE io;
29
+ const char *str;
30
30
  } in;
31
- struct _saxDrive *dr;
31
+ struct _saxDrive *dr;
32
32
  } *Buf;
33
33
 
34
34
  typedef struct _checkPt {
35
- off_t pro_dif;
36
- off_t pos;
37
- off_t line;
38
- off_t col;
39
- char c;
35
+ off_t pro_dif;
36
+ off_t pos;
37
+ off_t line;
38
+ off_t col;
39
+ char c;
40
40
  } *CheckPt;
41
41
 
42
- #define CHECK_PT_INIT { -1, 0, 0, 0, '\0' }
42
+ #define CHECK_PT_INIT \
43
+ { -1, 0, 0, 0, '\0' }
43
44
 
44
- extern void ox_sax_buf_init(Buf buf, VALUE io);
45
- extern int ox_sax_buf_read(Buf buf);
45
+ extern void ox_sax_buf_init(Buf buf, VALUE io);
46
+ extern int ox_sax_buf_read(Buf buf);
46
47
 
47
- static inline char
48
- buf_get(Buf buf) {
49
- //printf("*** drive get from '%s' from start: %ld buf: %p from read_end: %ld\n", buf->tail, buf->tail - buf->head, buf->head, buf->read_end - buf->tail);
48
+ static inline char buf_get(Buf buf) {
49
+ // printf("*** drive get from '%s' from start: %ld buf: %p from read_end: %ld\n", buf->tail, buf->tail -
50
+ // buf->head, buf->head, buf->read_end - buf->tail);
50
51
  if (buf->read_end <= buf->tail) {
51
52
  if (0 != ox_sax_buf_read(buf)) {
52
53
  return '\0';
@@ -56,59 +57,53 @@ buf_get(Buf buf) {
56
57
  buf->line++;
57
58
  buf->col = 0;
58
59
  } else {
59
- buf->col++;
60
+ buf->col++;
60
61
  }
61
62
  buf->pos++;
62
63
 
63
64
  return *buf->tail++;
64
65
  }
65
66
 
66
- static inline void
67
- buf_backup(Buf buf) {
67
+ static inline void buf_backup(Buf buf) {
68
68
  buf->tail--;
69
69
  buf->col--;
70
70
  buf->pos--;
71
71
  if (0 >= buf->col) {
72
- buf->line--;
73
- // allow col to be negative since we never backup twice in a row
72
+ buf->line--;
73
+ // allow col to be negative since we never backup twice in a row
74
74
  }
75
75
  }
76
76
 
77
- static inline void
78
- buf_protect(Buf buf) {
79
- buf->pro = buf->tail;
80
- buf->str = buf->tail; // can't have str before pro
81
- buf->pro_pos = buf->pos;
77
+ static inline void buf_protect(Buf buf) {
78
+ buf->pro = buf->tail;
79
+ buf->str = buf->tail; // can't have str before pro
80
+ buf->pro_pos = buf->pos;
82
81
  buf->pro_line = buf->line;
83
- buf->pro_col = buf->col;
82
+ buf->pro_col = buf->col;
84
83
  }
85
84
 
86
- static inline void
87
- buf_reset(Buf buf) {
85
+ static inline void buf_reset(Buf buf) {
88
86
  buf->tail = buf->pro;
89
- buf->pos = buf->pro_pos;
87
+ buf->pos = buf->pro_pos;
90
88
  buf->line = buf->pro_line;
91
- buf->col = buf->pro_col;
89
+ buf->col = buf->pro_col;
92
90
  }
93
91
 
94
92
  /* Starts by reading a character so it is safe to use with an empty or
95
93
  * compacted buffer.
96
94
  */
97
- static inline char
98
- buf_next_non_white(Buf buf) {
99
- char c;
95
+ static inline char buf_next_non_white(Buf buf) {
96
+ char c;
100
97
 
101
98
  while ('\0' != (c = buf_get(buf))) {
102
- switch(c) {
103
- case ' ':
104
- case '\t':
105
- case '\f':
106
- case '\n':
107
- case '\r':
108
- break;
109
- default:
110
- return c;
111
- }
99
+ switch (c) {
100
+ case ' ':
101
+ case '\t':
102
+ case '\f':
103
+ case '\n':
104
+ case '\r': break;
105
+ default: return c;
106
+ }
112
107
  }
113
108
  return '\0';
114
109
  }
@@ -116,107 +111,93 @@ buf_next_non_white(Buf buf) {
116
111
  /* Starts by reading a character so it is safe to use with an empty or
117
112
  * compacted buffer.
118
113
  */
119
- static inline char
120
- buf_next_white(Buf buf) {
121
- char c;
114
+ static inline char buf_next_white(Buf buf) {
115
+ char c;
122
116
 
123
117
  while ('\0' != (c = buf_get(buf))) {
124
- switch(c) {
125
- case ' ':
126
- case '\t':
127
- case '\f':
128
- case '\n':
129
- case '\r':
130
- case '\0':
131
- return c;
132
- default:
133
- break;
134
- }
118
+ switch (c) {
119
+ case ' ':
120
+ case '\t':
121
+ case '\f':
122
+ case '\n':
123
+ case '\r':
124
+ case '\0': return c;
125
+ default: break;
126
+ }
135
127
  }
136
128
  return '\0';
137
129
  }
138
130
 
139
- static inline void
140
- buf_cleanup(Buf buf) {
131
+ static inline void buf_cleanup(Buf buf) {
141
132
  if (buf->base != buf->head && 0 != buf->head) {
142
133
  xfree(buf->head);
143
- buf->head = 0;
134
+ buf->head = 0;
144
135
  }
145
136
  }
146
137
 
147
- static inline int
148
- is_white(char c) {
149
- switch(c) {
138
+ static inline int is_white(char c) {
139
+ switch (c) {
150
140
  case ' ':
151
141
  case '\t':
152
142
  case '\f':
153
143
  case '\n':
154
- case '\r':
155
- return 1;
156
- default:
157
- break;
144
+ case '\r': return 1;
145
+ default: break;
158
146
  }
159
147
  return 0;
160
148
  }
161
149
 
162
- static inline void
163
- buf_checkpoint(Buf buf, CheckPt cp) {
150
+ static inline void buf_checkpoint(Buf buf, CheckPt cp) {
164
151
  cp->pro_dif = (int)(buf->tail - buf->pro);
165
- cp->pos = buf->pos;
166
- cp->line = buf->line;
167
- cp->col = buf->col;
168
- cp->c = *(buf->tail - 1);
152
+ cp->pos = buf->pos;
153
+ cp->line = buf->line;
154
+ cp->col = buf->col;
155
+ cp->c = *(buf->tail - 1);
169
156
  }
170
157
 
171
- static inline int
172
- buf_checkset(CheckPt cp) {
158
+ static inline int buf_checkset(CheckPt cp) {
173
159
  return (0 <= cp->pro_dif);
174
160
  }
175
161
 
176
- static inline char
177
- buf_checkback(Buf buf, CheckPt cp) {
162
+ static inline char buf_checkback(Buf buf, CheckPt cp) {
178
163
  buf->tail = buf->pro + cp->pro_dif;
179
- buf->pos = cp->pos;
164
+ buf->pos = cp->pos;
180
165
  buf->line = cp->line;
181
- buf->col = cp->col;
166
+ buf->col = cp->col;
182
167
  return cp->c;
183
168
  }
184
169
 
185
- static inline void
186
- buf_collapse_return(char *str) {
187
- char *s = str;
188
- char *back = str;
170
+ static inline void buf_collapse_return(char *str) {
171
+ char *s = str;
172
+ char *back = str;
189
173
 
190
174
  for (; '\0' != *s; s++) {
191
- if (back != str && '\n' == *s && '\r' == *(back - 1)) {
192
- *(back - 1) = '\n';
193
- } else {
194
- *back++ = *s;
195
- }
175
+ if (back != str && '\n' == *s && '\r' == *(back - 1)) {
176
+ *(back - 1) = '\n';
177
+ } else {
178
+ *back++ = *s;
179
+ }
196
180
  }
197
181
  *back = '\0';
198
182
  }
199
183
 
200
- static inline void
201
- buf_collapse_white(char *str) {
202
- char *s = str;
203
- char *back = str;
184
+ static inline void buf_collapse_white(char *str) {
185
+ char *s = str;
186
+ char *back = str;
204
187
 
205
188
  for (; '\0' != *s; s++) {
206
- switch(*s) {
207
- case ' ':
208
- case '\t':
209
- case '\f':
210
- case '\n':
211
- case '\r':
212
- if (back == str || ' ' != *(back - 1)) {
213
- *back++ = ' ';
214
- }
215
- break;
216
- default:
217
- *back++ = *s;
218
- break;
219
- }
189
+ switch (*s) {
190
+ case ' ':
191
+ case '\t':
192
+ case '\f':
193
+ case '\n':
194
+ case '\r':
195
+ if (back == str || ' ' != *(back - 1)) {
196
+ *back++ = ' ';
197
+ }
198
+ break;
199
+ default: *back++ = *s; break;
200
+ }
220
201
  }
221
202
  *back = '\0';
222
203
  }