rake-builder 0.0.17 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (20) hide show
  1. data/examples/01_hello_world_cpp/vendor/bundle/gems/json-1.7.6/Rakefile +412 -0
  2. data/examples/01_hello_world_cpp/vendor/bundle/gems/json-1.7.6/ext/json/ext/fbuffer/fbuffer.h +185 -0
  3. data/examples/01_hello_world_cpp/vendor/bundle/gems/json-1.7.6/ext/json/ext/generator/generator.c +1427 -0
  4. data/examples/01_hello_world_cpp/vendor/bundle/gems/json-1.7.6/ext/json/ext/generator/generator.h +149 -0
  5. data/examples/01_hello_world_cpp/vendor/bundle/gems/json-1.7.6/ext/json/ext/parser/parser.c +2204 -0
  6. data/examples/01_hello_world_cpp/vendor/bundle/gems/json-1.7.6/ext/json/ext/parser/parser.h +77 -0
  7. data/examples/01_hello_world_cpp/vendor/bundle/gems/rake-10.0.3/Rakefile +374 -0
  8. data/examples/01_hello_world_cpp/vendor/bundle/gems/rake-10.0.3/doc/example/a.c +6 -0
  9. data/examples/01_hello_world_cpp/vendor/bundle/gems/rake-10.0.3/doc/example/b.c +6 -0
  10. data/examples/01_hello_world_cpp/vendor/bundle/gems/rake-10.0.3/doc/example/main.c +11 -0
  11. data/lib/rake/builder.rb +39 -70
  12. data/lib/rake/builder/presenters/makefile_am/builder_collection_presenter.rb +46 -0
  13. data/lib/rake/builder/presenters/makefile_am/builder_presenter.rb +40 -0
  14. data/lib/rake/builder/version.rb +1 -1
  15. data/spec/autoconf_spec.rb +1 -22
  16. data/spec/project_spec.rb +20 -0
  17. data/spec/unit/rake/builder/presenters/makefile_am/builder_collection_presenter_spec.rb +55 -0
  18. data/spec/unit/rake/builder/presenters/makefile_am/builder_presenter_spec.rb +71 -0
  19. data/spec/unit/rake/builder_spec.rb +46 -0
  20. metadata +24 -4
@@ -0,0 +1,412 @@
1
+ begin
2
+ require 'rubygems/package_task'
3
+ rescue LoadError
4
+ end
5
+
6
+ require 'rbconfig'
7
+ include\
8
+ begin
9
+ RbConfig
10
+ rescue NameError
11
+ Config
12
+ end
13
+
14
+ require 'rake/clean'
15
+ CLOBBER.include 'doc', 'Gemfile.lock'
16
+ CLEAN.include FileList['diagrams/*.*'], 'doc', 'coverage', 'tmp',
17
+ FileList["ext/**/{Makefile,mkmf.log}"], 'build', 'dist', FileList['**/*.rbc'],
18
+ FileList["{ext,lib}/**/*.{so,bundle,#{CONFIG['DLEXT']},o,obj,pdb,lib,manifest,exp,def,jar,class,dSYM}"],
19
+ FileList['java/src/**/*.class']
20
+
21
+ require 'rake/testtask'
22
+ class UndocumentedTestTask < Rake::TestTask
23
+ def desc(*) end
24
+ end
25
+
26
+ def skip_sdoc(src)
27
+ src.gsub(/^.*sdoc.*/) { |s| s + ' if RUBY_VERSION > "1.8.6"' }
28
+ end
29
+
30
+ MAKE = ENV['MAKE'] || %w[gmake make].find { |c| system(c, '-v') }
31
+ BUNDLE = ENV['BUNDLE'] || %w[bundle].find { |c| system(c, '-v') }
32
+ PKG_NAME = 'json'
33
+ PKG_TITLE = 'JSON Implementation for Ruby'
34
+ PKG_VERSION = File.read('VERSION').chomp
35
+ PKG_FILES = FileList[`git ls-files`.split(/\n/)]
36
+
37
+ EXT_ROOT_DIR = 'ext/json/ext'
38
+ EXT_PARSER_DIR = "#{EXT_ROOT_DIR}/parser"
39
+ EXT_PARSER_DL = "#{EXT_PARSER_DIR}/parser.#{CONFIG['DLEXT']}"
40
+ RAGEL_PATH = "#{EXT_PARSER_DIR}/parser.rl"
41
+ EXT_PARSER_SRC = "#{EXT_PARSER_DIR}/parser.c"
42
+ EXT_GENERATOR_DIR = "#{EXT_ROOT_DIR}/generator"
43
+ EXT_GENERATOR_DL = "#{EXT_GENERATOR_DIR}/generator.#{CONFIG['DLEXT']}"
44
+ EXT_GENERATOR_SRC = "#{EXT_GENERATOR_DIR}/generator.c"
45
+
46
+ JAVA_DIR = "java/src/json/ext"
47
+ JAVA_RAGEL_PATH = "#{JAVA_DIR}/Parser.rl"
48
+ JAVA_PARSER_SRC = "#{JAVA_DIR}/Parser.java"
49
+ JAVA_SOURCES = FileList["#{JAVA_DIR}/*.java"]
50
+ JAVA_CLASSES = []
51
+ JRUBY_PARSER_JAR = File.expand_path("lib/json/ext/parser.jar")
52
+ JRUBY_GENERATOR_JAR = File.expand_path("lib/json/ext/generator.jar")
53
+
54
+ RAGEL_CODEGEN = %w[rlcodegen rlgen-cd ragel].find { |c| system(c, '-v') }
55
+ RAGEL_DOTGEN = %w[rlgen-dot rlgen-cd ragel].find { |c| system(c, '-v') }
56
+
57
+ desc "Installing library (pure)"
58
+ task :install_pure => :version do
59
+ ruby 'install.rb'
60
+ end
61
+
62
+ task :install_ext_really do
63
+ sitearchdir = CONFIG["sitearchdir"]
64
+ cd 'ext' do
65
+ for file in Dir["json/ext/*.#{CONFIG['DLEXT']}"]
66
+ d = File.join(sitearchdir, file)
67
+ mkdir_p File.dirname(d)
68
+ install(file, d)
69
+ end
70
+ warn " *** Installed EXT ruby library."
71
+ end
72
+ end
73
+
74
+ desc "Installing library (extension)"
75
+ task :install_ext => [ :compile, :install_pure, :install_ext_really ]
76
+
77
+ desc "Installing library (extension)"
78
+ task :install => :install_ext
79
+
80
+ if defined?(Gem) and defined?(Gem::PackageTask)
81
+ spec_pure = Gem::Specification.new do |s|
82
+ s.name = 'json_pure'
83
+ s.version = PKG_VERSION
84
+ s.summary = PKG_TITLE
85
+ s.description = "This is a JSON implementation in pure Ruby."
86
+
87
+ s.files = PKG_FILES
88
+
89
+ s.require_path = 'lib'
90
+ s.add_development_dependency 'permutation'
91
+ s.add_development_dependency 'sdoc', '~> 0.3.16'
92
+ s.add_development_dependency 'rake', '~>0.9.2'
93
+
94
+ s.extra_rdoc_files << 'README.rdoc'
95
+ s.rdoc_options <<
96
+ '--title' << 'JSON implemention for ruby' << '--main' << 'README.rdoc'
97
+ s.test_files.concat Dir['./tests/test_*.rb']
98
+
99
+ s.author = "Florian Frank"
100
+ s.email = "flori@ping.de"
101
+ s.homepage = "http://flori.github.com/#{PKG_NAME}"
102
+ s.rubyforge_project = "json"
103
+ end
104
+
105
+ desc 'Creates a json_pure.gemspec file'
106
+ task :gemspec_pure => :version do
107
+ File.open('json_pure.gemspec', 'w') do |gemspec|
108
+ gemspec.write skip_sdoc(spec_pure.to_ruby)
109
+ end
110
+ end
111
+
112
+ Gem::PackageTask.new(spec_pure) do |pkg|
113
+ pkg.need_tar = true
114
+ pkg.package_files = PKG_FILES
115
+ end
116
+
117
+ spec_ext = Gem::Specification.new do |s|
118
+ s.name = 'json'
119
+ s.version = PKG_VERSION
120
+ s.summary = PKG_TITLE
121
+ s.description = "This is a JSON implementation as a Ruby extension in C."
122
+
123
+ s.files = PKG_FILES
124
+
125
+ s.extensions = FileList['ext/**/extconf.rb']
126
+
127
+ s.require_path = 'lib'
128
+ s.add_development_dependency 'permutation'
129
+ s.add_development_dependency 'sdoc', '~> 0.3.16'
130
+
131
+ s.extra_rdoc_files << 'README.rdoc'
132
+ s.rdoc_options <<
133
+ '--title' << 'JSON implemention for Ruby' << '--main' << 'README.rdoc'
134
+ s.test_files.concat Dir['./tests/test_*.rb']
135
+
136
+ s.author = "Florian Frank"
137
+ s.email = "flori@ping.de"
138
+ s.homepage = "http://flori.github.com/#{PKG_NAME}"
139
+ s.rubyforge_project = "json"
140
+ end
141
+
142
+ desc 'Creates a json.gemspec file'
143
+ task :gemspec_ext => :version do
144
+ File.open('json.gemspec', 'w') do |gemspec|
145
+ gemspec.write skip_sdoc(spec_ext.to_ruby)
146
+ end
147
+ end
148
+
149
+ Gem::PackageTask.new(spec_ext) do |pkg|
150
+ pkg.need_tar = true
151
+ pkg.package_files = PKG_FILES
152
+ end
153
+
154
+
155
+ desc 'Create all gemspec files'
156
+ task :gemspec => [ :gemspec_pure, :gemspec_ext ]
157
+ end
158
+
159
+ desc m = "Writing version information for #{PKG_VERSION}"
160
+ task :version do
161
+ puts m
162
+ File.open(File.join('lib', 'json', 'version.rb'), 'w') do |v|
163
+ v.puts <<EOT
164
+ module JSON
165
+ # JSON version
166
+ VERSION = '#{PKG_VERSION}'
167
+ VERSION_ARRAY = VERSION.split(/\\./).map { |x| x.to_i } # :nodoc:
168
+ VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
169
+ VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
170
+ VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
171
+ end
172
+ EOT
173
+ end
174
+ end
175
+
176
+ desc "Testing library (pure ruby)"
177
+ task :test_pure => [ :clean, :do_test_pure ]
178
+
179
+ UndocumentedTestTask.new do |t|
180
+ t.name = 'do_test_pure'
181
+ t.libs << 'lib'
182
+ t.test_files = FileList['tests/test_*.rb']
183
+ t.verbose = true
184
+ t.options = '-v'
185
+ end
186
+
187
+ desc "Testing library (pure ruby and extension)"
188
+ task :test do
189
+ sh "env JSON=pure #{BUNDLE} exec rake test_pure" or exit 1
190
+ sh "env JSON=ext #{BUNDLE} exec rake test_ext" or exit 1
191
+ end
192
+
193
+ namespace :gems do
194
+ desc 'Install all development gems'
195
+ task :install do
196
+ sh "#{BUNDLE}"
197
+ end
198
+ end
199
+
200
+ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby'
201
+ if ENV.key?('JAVA_HOME')
202
+ warn " *** JAVA_HOME was set to #{ENV['JAVA_HOME'].inspect}"
203
+ elsif File.directory?(local_java = '/usr/local/java/jdk') ||
204
+ File.directory?(local_java = '/usr/lib/jvm/java-6-openjdk')
205
+ then
206
+ ENV['JAVA_HOME'] = local_java
207
+ end
208
+ if ENV['JAVA_HOME']
209
+ warn " *** JAVA_HOME is set to #{ENV['JAVA_HOME'].inspect}"
210
+ ENV['PATH'] = ENV['PATH'].split(/:/).unshift(java_path = "#{ENV['JAVA_HOME']}/bin") * ':'
211
+ warn " *** java binaries are assumed to be in #{java_path.inspect}"
212
+ else
213
+ warn " *** JAVA_HOME was not set or could not be guessed!"
214
+ exit 1
215
+ end
216
+
217
+ file JAVA_PARSER_SRC => JAVA_RAGEL_PATH do
218
+ cd JAVA_DIR do
219
+ if RAGEL_CODEGEN == 'ragel'
220
+ sh "ragel Parser.rl -J -o Parser.java"
221
+ else
222
+ sh "ragel -x Parser.rl | #{RAGEL_CODEGEN} -J"
223
+ end
224
+ end
225
+ end
226
+
227
+ desc "Generate parser for java with ragel"
228
+ task :ragel => JAVA_PARSER_SRC
229
+
230
+ desc "Delete the ragel generated Java source"
231
+ task :ragel_clean do
232
+ rm_rf JAVA_PARSER_SRC
233
+ end
234
+
235
+ JRUBY_JAR = File.join(CONFIG["libdir"], "jruby.jar")
236
+ if File.exist?(JRUBY_JAR)
237
+ JAVA_SOURCES.each do |src|
238
+ classpath = (Dir['java/lib/*.jar'] << 'java/src' << JRUBY_JAR) * ':'
239
+ obj = src.sub(/\.java\Z/, '.class')
240
+ file obj => src do
241
+ sh 'javac', '-classpath', classpath, '-source', '1.5', '-target', '1.5', src
242
+ end
243
+ JAVA_CLASSES << obj
244
+ end
245
+ else
246
+ warn "WARNING: Cannot find jruby in path => Cannot build jruby extension!"
247
+ end
248
+
249
+ desc "Compiling jruby extension"
250
+ task :compile => JAVA_CLASSES
251
+
252
+ desc "Package the jruby gem"
253
+ task :jruby_gem => :create_jar do
254
+ sh 'gem build json-java.gemspec'
255
+ mkdir_p 'pkg'
256
+ mv "json-#{PKG_VERSION}-java.gem", 'pkg'
257
+ end
258
+
259
+ desc "Testing library (jruby)"
260
+ task :test_ext => [ :create_jar, :do_test_ext ]
261
+
262
+ UndocumentedTestTask.new do |t|
263
+ t.name = 'do_test_ext'
264
+ t.libs << 'lib'
265
+ t.test_files = FileList['tests/test_*.rb']
266
+ t.verbose = true
267
+ t.options = '-v'
268
+ end
269
+
270
+ file JRUBY_PARSER_JAR => :compile do
271
+ cd 'java/src' do
272
+ parser_classes = FileList[
273
+ "json/ext/ByteListTranscoder*.class",
274
+ "json/ext/OptionsReader*.class",
275
+ "json/ext/Parser*.class",
276
+ "json/ext/RuntimeInfo*.class",
277
+ "json/ext/StringDecoder*.class",
278
+ "json/ext/Utils*.class"
279
+ ]
280
+ sh 'jar', 'cf', File.basename(JRUBY_PARSER_JAR), *parser_classes
281
+ mv File.basename(JRUBY_PARSER_JAR), File.dirname(JRUBY_PARSER_JAR)
282
+ end
283
+ end
284
+
285
+ desc "Create parser jar"
286
+ task :create_parser_jar => JRUBY_PARSER_JAR
287
+
288
+ file JRUBY_GENERATOR_JAR => :compile do
289
+ cd 'java/src' do
290
+ generator_classes = FileList[
291
+ "json/ext/ByteListTranscoder*.class",
292
+ "json/ext/OptionsReader*.class",
293
+ "json/ext/Generator*.class",
294
+ "json/ext/RuntimeInfo*.class",
295
+ "json/ext/StringEncoder*.class",
296
+ "json/ext/Utils*.class"
297
+ ]
298
+ sh 'jar', 'cf', File.basename(JRUBY_GENERATOR_JAR), *generator_classes
299
+ mv File.basename(JRUBY_GENERATOR_JAR), File.dirname(JRUBY_GENERATOR_JAR)
300
+ end
301
+ end
302
+
303
+ desc "Create generator jar"
304
+ task :create_generator_jar => JRUBY_GENERATOR_JAR
305
+
306
+ desc "Create parser and generator jars"
307
+ task :create_jar => [ :create_parser_jar, :create_generator_jar ]
308
+
309
+ desc "Build all gems and archives for a new release of the jruby extension."
310
+ task :build => [ :clean, :version, :jruby_gem ]
311
+
312
+ task :release => :build
313
+ else
314
+ desc "Compiling extension"
315
+ task :compile => [ EXT_PARSER_DL, EXT_GENERATOR_DL ]
316
+
317
+ file EXT_PARSER_DL => EXT_PARSER_SRC do
318
+ cd EXT_PARSER_DIR do
319
+ ruby 'extconf.rb'
320
+ sh MAKE
321
+ end
322
+ cp "#{EXT_PARSER_DIR}/parser.#{CONFIG['DLEXT']}", EXT_ROOT_DIR
323
+ end
324
+
325
+ file EXT_GENERATOR_DL => EXT_GENERATOR_SRC do
326
+ cd EXT_GENERATOR_DIR do
327
+ ruby 'extconf.rb'
328
+ sh MAKE
329
+ end
330
+ cp "#{EXT_GENERATOR_DIR}/generator.#{CONFIG['DLEXT']}", EXT_ROOT_DIR
331
+ end
332
+
333
+ desc "Testing library (extension)"
334
+ task :test_ext => [ :compile, :do_test_ext ]
335
+
336
+ UndocumentedTestTask.new do |t|
337
+ t.name = 'do_test_ext'
338
+ t.libs << 'ext' << 'lib'
339
+ t.test_files = FileList['tests/test_*.rb']
340
+ t.verbose = true
341
+ t.options = '-v'
342
+ end
343
+
344
+ desc "Create RDOC documentation"
345
+ task :doc => [ :version, EXT_PARSER_SRC ] do
346
+ sh "sdoc -o doc -t '#{PKG_TITLE}' -m README.rdoc README.rdoc lib/json.rb #{FileList['lib/json/**/*.rb']} #{EXT_PARSER_SRC} #{EXT_GENERATOR_SRC}"
347
+ end
348
+
349
+ desc "Generate parser with ragel"
350
+ task :ragel => EXT_PARSER_SRC
351
+
352
+ desc "Delete the ragel generated C source"
353
+ task :ragel_clean do
354
+ rm_rf EXT_PARSER_SRC
355
+ end
356
+
357
+ desc "Update the tags file"
358
+ task :tags do
359
+ system 'ctags', *Dir['**/*.{rb,c,h,java}']
360
+ end
361
+
362
+ file EXT_PARSER_SRC => RAGEL_PATH do
363
+ cd EXT_PARSER_DIR do
364
+ if RAGEL_CODEGEN == 'ragel'
365
+ sh "ragel parser.rl -G2 -o parser.c"
366
+ else
367
+ sh "ragel -x parser.rl | #{RAGEL_CODEGEN} -G2"
368
+ end
369
+ src = File.read("parser.c").gsub(/[ \t]+$/, '')
370
+ File.open("parser.c", "w") {|f| f.print src}
371
+ end
372
+ end
373
+
374
+ desc "Generate diagrams of ragel parser (ps)"
375
+ task :ragel_dot_ps do
376
+ root = 'diagrams'
377
+ specs = []
378
+ File.new(RAGEL_PATH).grep(/^\s*machine\s*(\S+);\s*$/) { specs << $1 }
379
+ for s in specs
380
+ if RAGEL_DOTGEN == 'ragel'
381
+ sh "ragel #{RAGEL_PATH} -S#{s} -p -V | dot -Tps -o#{root}/#{s}.ps"
382
+ else
383
+ sh "ragel -x #{RAGEL_PATH} -S#{s} | #{RAGEL_DOTGEN} -p|dot -Tps -o#{root}/#{s}.ps"
384
+ end
385
+ end
386
+ end
387
+
388
+ desc "Generate diagrams of ragel parser (png)"
389
+ task :ragel_dot_png do
390
+ root = 'diagrams'
391
+ specs = []
392
+ File.new(RAGEL_PATH).grep(/^\s*machine\s*(\S+);\s*$/) { specs << $1 }
393
+ for s in specs
394
+ if RAGEL_DOTGEN == 'ragel'
395
+ sh "ragel #{RAGEL_PATH} -S#{s} -p -V | dot -Tpng -o#{root}/#{s}.png"
396
+ else
397
+ sh "ragel -x #{RAGEL_PATH} -S#{s} | #{RAGEL_DOTGEN} -p|dot -Tpng -o#{root}/#{s}.png"
398
+ end
399
+ end
400
+ end
401
+
402
+ desc "Generate diagrams of ragel parser"
403
+ task :ragel_dot => [ :ragel_dot_png, :ragel_dot_ps ]
404
+
405
+ desc "Build all gems and archives for a new release of json and json_pure."
406
+ task :build => [ :clean, :gemspec, :package ]
407
+
408
+ task :release => :build
409
+ end
410
+
411
+ desc "Compile in the the source directory"
412
+ task :default => [ :clean, :gemspec, :test ]
@@ -0,0 +1,185 @@
1
+
2
+ #ifndef _FBUFFER_H_
3
+ #define _FBUFFER_H_
4
+
5
+ #include "ruby.h"
6
+ #include <assert.h>
7
+
8
+ #ifndef RHASH_SIZE
9
+ #define RHASH_SIZE(hsh) (RHASH(hsh)->tbl->num_entries)
10
+ #endif
11
+
12
+ #ifndef RFLOAT_VALUE
13
+ #define RFLOAT_VALUE(val) (RFLOAT(val)->value)
14
+ #endif
15
+
16
+ #ifndef RARRAY_PTR
17
+ #define RARRAY_PTR(ARRAY) RARRAY(ARRAY)->ptr
18
+ #endif
19
+ #ifndef RARRAY_LEN
20
+ #define RARRAY_LEN(ARRAY) RARRAY(ARRAY)->len
21
+ #endif
22
+ #ifndef RSTRING_PTR
23
+ #define RSTRING_PTR(string) RSTRING(string)->ptr
24
+ #endif
25
+ #ifndef RSTRING_LEN
26
+ #define RSTRING_LEN(string) RSTRING(string)->len
27
+ #endif
28
+
29
+ #ifdef HAVE_RUBY_ENCODING_H
30
+ #include "ruby/encoding.h"
31
+ #define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
32
+ #else
33
+ #define FORCE_UTF8(obj)
34
+ #endif
35
+
36
+ /* We don't need to guard objects for rbx, so let's do nothing at all. */
37
+ #ifndef RB_GC_GUARD
38
+ #define RB_GC_GUARD(object)
39
+ #endif
40
+
41
+ typedef struct FBufferStruct {
42
+ unsigned long initial_length;
43
+ char *ptr;
44
+ unsigned long len;
45
+ unsigned long capa;
46
+ } FBuffer;
47
+
48
+ #define FBUFFER_INITIAL_LENGTH_DEFAULT 1024
49
+
50
+ #define FBUFFER_PTR(fb) (fb->ptr)
51
+ #define FBUFFER_LEN(fb) (fb->len)
52
+ #define FBUFFER_CAPA(fb) (fb->capa)
53
+ #define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
54
+
55
+ static FBuffer *fbuffer_alloc(unsigned long initial_length);
56
+ static void fbuffer_free(FBuffer *fb);
57
+ static void fbuffer_clear(FBuffer *fb);
58
+ static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
59
+ #ifdef JSON_GENERATOR
60
+ static void fbuffer_append_long(FBuffer *fb, long number);
61
+ #endif
62
+ static void fbuffer_append_char(FBuffer *fb, char newchr);
63
+ #ifdef JSON_GENERATOR
64
+ static FBuffer *fbuffer_dup(FBuffer *fb);
65
+ static VALUE fbuffer_to_s(FBuffer *fb);
66
+ #endif
67
+
68
+ static FBuffer *fbuffer_alloc(unsigned long initial_length)
69
+ {
70
+ FBuffer *fb;
71
+ if (initial_length <= 0) initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
72
+ fb = ALLOC(FBuffer);
73
+ memset((void *) fb, 0, sizeof(FBuffer));
74
+ fb->initial_length = initial_length;
75
+ return fb;
76
+ }
77
+
78
+ static void fbuffer_free(FBuffer *fb)
79
+ {
80
+ if (fb->ptr) ruby_xfree(fb->ptr);
81
+ ruby_xfree(fb);
82
+ }
83
+
84
+ static void fbuffer_clear(FBuffer *fb)
85
+ {
86
+ fb->len = 0;
87
+ }
88
+
89
+ static void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
90
+ {
91
+ unsigned long required;
92
+
93
+ if (!fb->ptr) {
94
+ fb->ptr = ALLOC_N(char, fb->initial_length);
95
+ fb->capa = fb->initial_length;
96
+ }
97
+
98
+ for (required = fb->capa; requested > required - fb->len; required <<= 1);
99
+
100
+ if (required > fb->capa) {
101
+ REALLOC_N(fb->ptr, char, required);
102
+ fb->capa = required;
103
+ }
104
+ }
105
+
106
+ static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
107
+ {
108
+ if (len > 0) {
109
+ fbuffer_inc_capa(fb, len);
110
+ MEMCPY(fb->ptr + fb->len, newstr, char, len);
111
+ fb->len += len;
112
+ }
113
+ }
114
+
115
+ #ifdef JSON_GENERATOR
116
+ static void fbuffer_append_str(FBuffer *fb, VALUE str)
117
+ {
118
+ const char *newstr = StringValuePtr(str);
119
+ unsigned long len = RSTRING_LEN(str);
120
+
121
+ RB_GC_GUARD(str);
122
+
123
+ fbuffer_append(fb, newstr, len);
124
+ }
125
+ #endif
126
+
127
+ static void fbuffer_append_char(FBuffer *fb, char newchr)
128
+ {
129
+ fbuffer_inc_capa(fb, 1);
130
+ *(fb->ptr + fb->len) = newchr;
131
+ fb->len++;
132
+ }
133
+
134
+ #ifdef JSON_GENERATOR
135
+ static void freverse(char *start, char *end)
136
+ {
137
+ char c;
138
+
139
+ while (end > start) {
140
+ c = *end, *end-- = *start, *start++ = c;
141
+ }
142
+ }
143
+
144
+ static long fltoa(long number, char *buf)
145
+ {
146
+ static char digits[] = "0123456789";
147
+ long sign = number;
148
+ char* tmp = buf;
149
+
150
+ if (sign < 0) number = -number;
151
+ do *tmp++ = digits[number % 10]; while (number /= 10);
152
+ if (sign < 0) *tmp++ = '-';
153
+ freverse(buf, tmp - 1);
154
+ return tmp - buf;
155
+ }
156
+
157
+ static void fbuffer_append_long(FBuffer *fb, long number)
158
+ {
159
+ char buf[20];
160
+ unsigned long len = fltoa(number, buf);
161
+ fbuffer_append(fb, buf, len);
162
+ }
163
+
164
+ static FBuffer *fbuffer_dup(FBuffer *fb)
165
+ {
166
+ unsigned long len = fb->len;
167
+ FBuffer *result;
168
+
169
+ assert(len > 0);
170
+ if (len > 0) {
171
+ result = fbuffer_alloc(len);
172
+ fbuffer_append(result, FBUFFER_PAIR(fb));
173
+ }
174
+ return result;
175
+ }
176
+
177
+ static VALUE fbuffer_to_s(FBuffer *fb)
178
+ {
179
+ VALUE result = rb_str_new(FBUFFER_PAIR(fb));
180
+ fbuffer_free(fb);
181
+ FORCE_UTF8(result);
182
+ return result;
183
+ }
184
+ #endif
185
+ #endif