Ruby 2.0.0p247(2013-06-27revision41674)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

generator.h
Go to the documentation of this file.
1 #ifndef _GENERATOR_H_
2 #define _GENERATOR_H_
3
4 #include <string.h>
5 #include <math.h>
6 #include <ctype.h>
7
8 #include "ruby.h"
9
10 #ifdef HAVE_RUBY_RE_H
11 #include "ruby/re.h"
12 #else
13 #include "re.h"
14 #endif
15
16 #ifndef rb_intern_str
17 #define rb_intern_str(string) SYM2ID(rb_str_intern(string))
18 #endif
19
20 #ifndef rb_obj_instance_variables
21 #define rb_obj_instance_variables(object) rb_funcall(object, rb_intern("instance_variables"), 0)
22 #endif
23
24 #define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
25
26 /* unicode defintions */
27
28 #define UNI_STRICT_CONVERSION 1
29
30 typedef unsigned long UTF32; /* at least 32 bits */
31 typedef unsigned short UTF16; /* at least 16 bits */
32 typedef unsigned char UTF8; /* typically 8 bits */
33
34 #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
35 #define UNI_MAX_BMP (UTF32)0x0000FFFF
36 #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
37 #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
38 #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
39
40 #define UNI_SUR_HIGH_START (UTF32)0xD800
41 #define UNI_SUR_HIGH_END (UTF32)0xDBFF
42 #define UNI_SUR_LOW_START (UTF32)0xDC00
43 #define UNI_SUR_LOW_END (UTF32)0xDFFF
44
45 static const int halfShift = 10; /* used for shifting by 10 bits */
46
47 static const UTF32 halfBase = 0x0010000UL;
48 static const UTF32 halfMask = 0x3FFUL;
49
50 static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length);
51 static void unicode_escape(char *buf, UTF16 character);
52 static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character);
53 static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string);
54 static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string);
55 static char *fstrndup(const char *ptr, unsigned long len);
56
57 /* ruby api and some helpers */
58
59 typedef struct JSON_Generator_StateStruct {
60 char *indent;
61 long indent_len;
62 char *space;
63 long space_len;
64 char *space_before;
65 long space_before_len;
66 char *object_nl;
67 long object_nl_len;
68 char *array_nl;
69 long array_nl_len;
70 FBuffer *array_delim;
71 FBuffer *object_delim;
72 FBuffer *object_delim2;
73 long max_nesting;
74 char allow_nan;
75 char ascii_only;
76 char quirks_mode;
77 long depth;
78 long buffer_initial_length;
79 } JSON_Generator_State;
80
81 #define GET_STATE(self) \
82 JSON_Generator_State *state; \
83 Data_Get_Struct(self, JSON_Generator_State, state)
84
85 #define GENERATE_JSON(type) \
86 FBuffer *buffer; \
87 VALUE Vstate; \
88 JSON_Generator_State *state; \
89 \
90 rb_scan_args(argc, argv, "01", &Vstate); \
91 Vstate = cState_from_state_s(cState, Vstate); \
92 Data_Get_Struct(Vstate, JSON_Generator_State, state); \
93 buffer = cState_prepare_buffer(Vstate); \
94 generate_json_##type(buffer, Vstate, state, self); \
95 return fbuffer_to_s(buffer)
96
97 static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self);
98 static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self);
99 static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self);
100 static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self);
101 static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self);
102 static VALUE mString_included_s(VALUE self, VALUE modul);
103 static VALUE mString_to_json(int argc, VALUE *argv, VALUE self);
104 static VALUE mString_to_json_raw_object(VALUE self);
105 static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self);
106 static VALUE mString_Extend_json_create(VALUE self, VALUE o);
107 static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self);
108 static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
109 static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
110 static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
111 static void State_free(JSON_Generator_State *state);
112 static JSON_Generator_State *State_allocate();
113 static VALUE cState_s_allocate(VALUE klass);
114 static VALUE cState_configure(VALUE self, VALUE opts);
115 static VALUE cState_to_h(VALUE self);
116 static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
117 static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
118 static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
119 static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
120 static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
121 static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
122 static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
123 static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
124 static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
125 static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
126 static VALUE cState_partial_generate(VALUE self, VALUE obj);
127 static VALUE cState_generate(VALUE self, VALUE obj);
128 static VALUE cState_initialize(int argc, VALUE *argv, VALUE self);
129 static VALUE cState_from_state_s(VALUE self, VALUE opts);
130 static VALUE cState_indent(VALUE self);
131 static VALUE cState_indent_set(VALUE self, VALUE indent);
132 static VALUE cState_space(VALUE self);
133 static VALUE cState_space_set(VALUE self, VALUE space);
134 static VALUE cState_space_before(VALUE self);
135 static VALUE cState_space_before_set(VALUE self, VALUE space_before);
136 static VALUE cState_object_nl(VALUE self);
137 static VALUE cState_object_nl_set(VALUE self, VALUE object_nl);
138 static VALUE cState_array_nl(VALUE self);
139 static VALUE cState_array_nl_set(VALUE self, VALUE array_nl);
140 static VALUE cState_max_nesting(VALUE self);
141 static VALUE cState_max_nesting_set(VALUE self, VALUE depth);
142 static VALUE cState_allow_nan_p(VALUE self);
143 static VALUE cState_ascii_only_p(VALUE self);
144 static VALUE cState_depth(VALUE self);
145 static VALUE cState_depth_set(VALUE self, VALUE depth);
146 static FBuffer *cState_prepare_buffer(VALUE self);
147
148 #endif
149