psych 2.0.14-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +18 -0
  3. data/.gemtest +0 -0
  4. data/.travis.yml +16 -0
  5. data/CHANGELOG.rdoc +576 -0
  6. data/Manifest.txt +114 -0
  7. data/README.rdoc +71 -0
  8. data/Rakefile +123 -0
  9. data/ext/psych/depend +3 -0
  10. data/ext/psych/extconf.rb +38 -0
  11. data/ext/psych/psych.c +34 -0
  12. data/ext/psych/psych.h +20 -0
  13. data/ext/psych/psych_emitter.c +555 -0
  14. data/ext/psych/psych_emitter.h +8 -0
  15. data/ext/psych/psych_parser.c +597 -0
  16. data/ext/psych/psych_parser.h +6 -0
  17. data/ext/psych/psych_to_ruby.c +43 -0
  18. data/ext/psych/psych_to_ruby.h +8 -0
  19. data/ext/psych/psych_yaml_tree.c +24 -0
  20. data/ext/psych/psych_yaml_tree.h +8 -0
  21. data/ext/psych/yaml/LICENSE +19 -0
  22. data/ext/psych/yaml/api.c +1415 -0
  23. data/ext/psych/yaml/config.h +10 -0
  24. data/ext/psych/yaml/dumper.c +394 -0
  25. data/ext/psych/yaml/emitter.c +2329 -0
  26. data/ext/psych/yaml/loader.c +459 -0
  27. data/ext/psych/yaml/parser.c +1370 -0
  28. data/ext/psych/yaml/reader.c +469 -0
  29. data/ext/psych/yaml/scanner.c +3576 -0
  30. data/ext/psych/yaml/writer.c +141 -0
  31. data/ext/psych/yaml/yaml.h +1971 -0
  32. data/ext/psych/yaml/yaml_private.h +664 -0
  33. data/lib/psych.jar +0 -0
  34. data/lib/psych.rb +504 -0
  35. data/lib/psych/class_loader.rb +101 -0
  36. data/lib/psych/coder.rb +94 -0
  37. data/lib/psych/core_ext.rb +35 -0
  38. data/lib/psych/deprecated.rb +85 -0
  39. data/lib/psych/exception.rb +13 -0
  40. data/lib/psych/handler.rb +249 -0
  41. data/lib/psych/handlers/document_stream.rb +22 -0
  42. data/lib/psych/handlers/recorder.rb +39 -0
  43. data/lib/psych/json/ruby_events.rb +19 -0
  44. data/lib/psych/json/stream.rb +16 -0
  45. data/lib/psych/json/tree_builder.rb +12 -0
  46. data/lib/psych/json/yaml_events.rb +29 -0
  47. data/lib/psych/nodes.rb +77 -0
  48. data/lib/psych/nodes/alias.rb +18 -0
  49. data/lib/psych/nodes/document.rb +60 -0
  50. data/lib/psych/nodes/mapping.rb +56 -0
  51. data/lib/psych/nodes/node.rb +55 -0
  52. data/lib/psych/nodes/scalar.rb +67 -0
  53. data/lib/psych/nodes/sequence.rb +81 -0
  54. data/lib/psych/nodes/stream.rb +37 -0
  55. data/lib/psych/omap.rb +4 -0
  56. data/lib/psych/parser.rb +51 -0
  57. data/lib/psych/scalar_scanner.rb +149 -0
  58. data/lib/psych/set.rb +4 -0
  59. data/lib/psych/stream.rb +37 -0
  60. data/lib/psych/streaming.rb +27 -0
  61. data/lib/psych/syntax_error.rb +21 -0
  62. data/lib/psych/tree_builder.rb +96 -0
  63. data/lib/psych/versions.rb +3 -0
  64. data/lib/psych/visitors.rb +6 -0
  65. data/lib/psych/visitors/depth_first.rb +26 -0
  66. data/lib/psych/visitors/emitter.rb +51 -0
  67. data/lib/psych/visitors/json_tree.rb +24 -0
  68. data/lib/psych/visitors/to_ruby.rb +404 -0
  69. data/lib/psych/visitors/visitor.rb +19 -0
  70. data/lib/psych/visitors/yaml_tree.rb +605 -0
  71. data/lib/psych/y.rb +9 -0
  72. data/lib/psych_jars.rb +5 -0
  73. data/test/psych/handlers/test_recorder.rb +25 -0
  74. data/test/psych/helper.rb +121 -0
  75. data/test/psych/json/test_stream.rb +109 -0
  76. data/test/psych/nodes/test_enumerable.rb +43 -0
  77. data/test/psych/test_alias_and_anchor.rb +96 -0
  78. data/test/psych/test_array.rb +57 -0
  79. data/test/psych/test_boolean.rb +36 -0
  80. data/test/psych/test_class.rb +36 -0
  81. data/test/psych/test_coder.rb +206 -0
  82. data/test/psych/test_date_time.rb +38 -0
  83. data/test/psych/test_deprecated.rb +214 -0
  84. data/test/psych/test_document.rb +46 -0
  85. data/test/psych/test_emitter.rb +93 -0
  86. data/test/psych/test_encoding.rb +259 -0
  87. data/test/psych/test_exception.rb +157 -0
  88. data/test/psych/test_hash.rb +94 -0
  89. data/test/psych/test_json_tree.rb +65 -0
  90. data/test/psych/test_merge_keys.rb +180 -0
  91. data/test/psych/test_nil.rb +18 -0
  92. data/test/psych/test_null.rb +19 -0
  93. data/test/psych/test_numeric.rb +45 -0
  94. data/test/psych/test_object.rb +44 -0
  95. data/test/psych/test_object_references.rb +71 -0
  96. data/test/psych/test_omap.rb +75 -0
  97. data/test/psych/test_parser.rb +339 -0
  98. data/test/psych/test_psych.rb +168 -0
  99. data/test/psych/test_safe_load.rb +97 -0
  100. data/test/psych/test_scalar.rb +11 -0
  101. data/test/psych/test_scalar_scanner.rb +106 -0
  102. data/test/psych/test_serialize_subclasses.rb +38 -0
  103. data/test/psych/test_set.rb +49 -0
  104. data/test/psych/test_stream.rb +93 -0
  105. data/test/psych/test_string.rb +226 -0
  106. data/test/psych/test_struct.rb +49 -0
  107. data/test/psych/test_symbol.rb +25 -0
  108. data/test/psych/test_tainted.rb +130 -0
  109. data/test/psych/test_to_yaml_properties.rb +63 -0
  110. data/test/psych/test_tree_builder.rb +79 -0
  111. data/test/psych/test_yaml.rb +1292 -0
  112. data/test/psych/test_yamldbm.rb +193 -0
  113. data/test/psych/test_yamlstore.rb +85 -0
  114. data/test/psych/visitors/test_depth_first.rb +49 -0
  115. data/test/psych/visitors/test_emitter.rb +144 -0
  116. data/test/psych/visitors/test_to_ruby.rb +333 -0
  117. data/test/psych/visitors/test_yaml_tree.rb +173 -0
  118. metadata +240 -0
data/Manifest.txt ADDED
@@ -0,0 +1,114 @@
1
+ .autotest
2
+ .travis.yml
3
+ CHANGELOG.rdoc
4
+ Manifest.txt
5
+ README.rdoc
6
+ Rakefile
7
+ ext/psych/depend
8
+ ext/psych/extconf.rb
9
+ ext/psych/psych.c
10
+ ext/psych/psych.h
11
+ ext/psych/psych_emitter.c
12
+ ext/psych/psych_emitter.h
13
+ ext/psych/psych_parser.c
14
+ ext/psych/psych_parser.h
15
+ ext/psych/psych_to_ruby.c
16
+ ext/psych/psych_to_ruby.h
17
+ ext/psych/psych_yaml_tree.c
18
+ ext/psych/psych_yaml_tree.h
19
+ ext/psych/yaml/LICENSE
20
+ ext/psych/yaml/api.c
21
+ ext/psych/yaml/config.h
22
+ ext/psych/yaml/dumper.c
23
+ ext/psych/yaml/emitter.c
24
+ ext/psych/yaml/loader.c
25
+ ext/psych/yaml/parser.c
26
+ ext/psych/yaml/reader.c
27
+ ext/psych/yaml/scanner.c
28
+ ext/psych/yaml/writer.c
29
+ ext/psych/yaml/yaml.h
30
+ ext/psych/yaml/yaml_private.h
31
+ lib/psych.rb
32
+ lib/psych/class_loader.rb
33
+ lib/psych/coder.rb
34
+ lib/psych/core_ext.rb
35
+ lib/psych/deprecated.rb
36
+ lib/psych/exception.rb
37
+ lib/psych/handler.rb
38
+ lib/psych/handlers/document_stream.rb
39
+ lib/psych/handlers/recorder.rb
40
+ lib/psych/json/ruby_events.rb
41
+ lib/psych/json/stream.rb
42
+ lib/psych/json/tree_builder.rb
43
+ lib/psych/json/yaml_events.rb
44
+ lib/psych/nodes.rb
45
+ lib/psych/nodes/alias.rb
46
+ lib/psych/nodes/document.rb
47
+ lib/psych/nodes/mapping.rb
48
+ lib/psych/nodes/node.rb
49
+ lib/psych/nodes/scalar.rb
50
+ lib/psych/nodes/sequence.rb
51
+ lib/psych/nodes/stream.rb
52
+ lib/psych/omap.rb
53
+ lib/psych/parser.rb
54
+ lib/psych/scalar_scanner.rb
55
+ lib/psych/set.rb
56
+ lib/psych/stream.rb
57
+ lib/psych/streaming.rb
58
+ lib/psych/syntax_error.rb
59
+ lib/psych/tree_builder.rb
60
+ lib/psych/versions.rb
61
+ lib/psych/visitors.rb
62
+ lib/psych/visitors/depth_first.rb
63
+ lib/psych/visitors/emitter.rb
64
+ lib/psych/visitors/json_tree.rb
65
+ lib/psych/visitors/to_ruby.rb
66
+ lib/psych/visitors/visitor.rb
67
+ lib/psych/visitors/yaml_tree.rb
68
+ lib/psych/y.rb
69
+ lib/psych_jars.rb
70
+ test/psych/handlers/test_recorder.rb
71
+ test/psych/helper.rb
72
+ test/psych/json/test_stream.rb
73
+ test/psych/nodes/test_enumerable.rb
74
+ test/psych/test_alias_and_anchor.rb
75
+ test/psych/test_array.rb
76
+ test/psych/test_boolean.rb
77
+ test/psych/test_class.rb
78
+ test/psych/test_coder.rb
79
+ test/psych/test_date_time.rb
80
+ test/psych/test_deprecated.rb
81
+ test/psych/test_document.rb
82
+ test/psych/test_emitter.rb
83
+ test/psych/test_encoding.rb
84
+ test/psych/test_exception.rb
85
+ test/psych/test_hash.rb
86
+ test/psych/test_json_tree.rb
87
+ test/psych/test_merge_keys.rb
88
+ test/psych/test_nil.rb
89
+ test/psych/test_null.rb
90
+ test/psych/test_numeric.rb
91
+ test/psych/test_object.rb
92
+ test/psych/test_object_references.rb
93
+ test/psych/test_omap.rb
94
+ test/psych/test_parser.rb
95
+ test/psych/test_psych.rb
96
+ test/psych/test_safe_load.rb
97
+ test/psych/test_scalar.rb
98
+ test/psych/test_scalar_scanner.rb
99
+ test/psych/test_serialize_subclasses.rb
100
+ test/psych/test_set.rb
101
+ test/psych/test_stream.rb
102
+ test/psych/test_string.rb
103
+ test/psych/test_struct.rb
104
+ test/psych/test_symbol.rb
105
+ test/psych/test_tainted.rb
106
+ test/psych/test_to_yaml_properties.rb
107
+ test/psych/test_tree_builder.rb
108
+ test/psych/test_yaml.rb
109
+ test/psych/test_yamldbm.rb
110
+ test/psych/test_yamlstore.rb
111
+ test/psych/visitors/test_depth_first.rb
112
+ test/psych/visitors/test_emitter.rb
113
+ test/psych/visitors/test_to_ruby.rb
114
+ test/psych/visitors/test_yaml_tree.rb
data/README.rdoc ADDED
@@ -0,0 +1,71 @@
1
+ = Psych
2
+
3
+ * http://github.com/tenderlove/psych
4
+
5
+ == Description
6
+
7
+ Psych is a YAML parser and emitter. Psych leverages libyaml[http://pyyaml.org/wiki/LibYAML]
8
+ for its YAML parsing and emitting capabilities. In addition to wrapping
9
+ libyaml, Psych also knows how to serialize and de-serialize most Ruby objects
10
+ to and from the YAML format.
11
+
12
+ == Examples
13
+
14
+ # Load YAML in to a Ruby object
15
+ Psych.load('--- foo') # => 'foo'
16
+
17
+ # Emit YAML from a Ruby object
18
+ Psych.dump("foo") # => "--- foo\n...\n"
19
+
20
+ == Dependencies
21
+
22
+ * libyaml
23
+
24
+ == Installation
25
+
26
+ Psych has been included with MRI since 1.9.2,
27
+ and is the default YAML parser in 1.9.3.
28
+
29
+ If you want a newer gem release of Psych, you can use rubygems:
30
+
31
+ gem install psych
32
+
33
+ In order to use the gem release in your app,
34
+ and not the stdlib version, you'll need the following:
35
+
36
+ gem 'psych'
37
+ require 'psych'
38
+
39
+ Or if you use Bundler add this to your +Gemfile+:
40
+
41
+ gem 'psych'
42
+
43
+ JRuby ships with a pure Java implementation of Psych.
44
+
45
+ If you're on Rubinius, Psych is available in 1.9 mode, please refer to the
46
+ Language Modes section of the {Rubinius
47
+ README}[https://github.com/rubinius/rubinius#readme] for more information on
48
+ building and 1.9 mode.
49
+
50
+ == License
51
+
52
+ Copyright 2009 Aaron Patterson, et al.
53
+
54
+ Permission is hereby granted, free of charge, to any person obtaining
55
+ a copy of this software and associated documentation files (the
56
+ 'Software'), to deal in the Software without restriction, including
57
+ without limitation the rights to use, copy, modify, merge, publish,
58
+ distribute, sublicense, and/or sell copies of the Software, and to
59
+ permit persons to whom the Software is furnished to do so, subject to
60
+ the following conditions:
61
+
62
+ The above copyright notice and this permission notice shall be
63
+ included in all copies or substantial portions of the Software.
64
+
65
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
66
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
67
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
68
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
69
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
70
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
71
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,123 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'psych'
4
+ require 'rubygems'
5
+ require 'hoe'
6
+
7
+ def java?
8
+ RUBY_PLATFORM =~ /java/
9
+ end
10
+
11
+ class Hoe
12
+ remove_const :RUBY_FLAGS
13
+ flags = "-I#{%w(lib ext bin test).join(File::PATH_SEPARATOR)}"
14
+ flags = "--1.9 " + flags if java?
15
+ RUBY_FLAGS = flags
16
+ end
17
+
18
+ gem 'rake-compiler', '>= 0.4.1'
19
+ gem 'minitest', '~> 5.0'
20
+ require "rake/extensiontask"
21
+
22
+ Hoe.plugin :doofus, :git, :gemspec
23
+
24
+ $hoe = Hoe.spec 'psych' do
25
+ license 'MIT'
26
+ developer 'Aaron Patterson', 'aaron@tenderlovemaking.com'
27
+
28
+ self.extra_rdoc_files = Dir['*.rdoc']
29
+ self.history_file = 'CHANGELOG.rdoc'
30
+ self.readme_file = 'README.rdoc'
31
+ self.testlib = :minitest
32
+
33
+ extra_dev_deps << ['rake-compiler', '>= 0.4.1']
34
+ extra_dev_deps << ['minitest', '~> 5.0']
35
+
36
+ self.spec_extras = {
37
+ :required_ruby_version => '>= 1.9.2'
38
+ }
39
+
40
+ if java?
41
+ require './lib/psych/versions.rb'
42
+ extra_deps << ['jar-dependencies', '>= 0.1.7']
43
+
44
+ # the jar declaration for jar-dependencies
45
+ self.spec_extras[ 'requirements' ] = "jar org.yaml:snakeyaml, #{Psych::DEFAULT_SNAKEYAML_VERSION}"
46
+ self.spec_extras[ 'platform' ] = 'java'
47
+ # TODO: clean this section up.
48
+ require "rake/javaextensiontask"
49
+ Rake::JavaExtensionTask.new("psych", spec) do |ext|
50
+ require 'maven/ruby/maven'
51
+ # uses Mavenfile to write classpath into pkg/classpath
52
+ # and tell maven via system properties the snakeyaml version
53
+ # this is basically the same as running from the commandline:
54
+ # rmvn dependency:build-classpath -Dsnakeyaml.version='use version from Psych::DEFAULT_SNAKEYAML_VERSION here'
55
+ Maven::Ruby::Maven.new.exec( 'dependency:build-classpath', "-Dsnakeyaml.version=#{Psych::DEFAULT_SNAKEYAML_VERSION}", '-Dverbose=true')#, '--quiet' )
56
+ ext.source_version = '1.7'
57
+ ext.target_version = '1.7'
58
+ ext.classpath = File.read('pkg/classpath')
59
+ ext.ext_dir = 'ext/java'
60
+ end
61
+ else
62
+ self.spec_extras[:extensions] = ["ext/psych/extconf.rb"]
63
+ Rake::ExtensionTask.new "psych", spec do |ext|
64
+ ext.lib_dir = File.join(*['lib', ENV['FAT_DIR']].compact)
65
+ end
66
+ end
67
+ end
68
+
69
+ def gem_build_path
70
+ File.join 'pkg', $hoe.spec.full_name
71
+ end
72
+
73
+ def add_file_to_gem relative_path
74
+ target_path = File.join gem_build_path, relative_path
75
+ target_dir = File.dirname(target_path)
76
+ mkdir_p target_dir unless File.directory?(target_dir)
77
+ rm_f target_path
78
+ safe_ln relative_path, target_path
79
+ $hoe.spec.files.concat [relative_path]
80
+ end
81
+
82
+ if java?
83
+ task gem_build_path => [:compile] do
84
+ add_file_to_gem 'lib/psych.jar'
85
+ end
86
+ end
87
+
88
+ Hoe.add_include_dirs('.:lib/psych')
89
+
90
+ task :test => :compile
91
+
92
+ task :hack_spec do
93
+ $hoe.spec.extra_rdoc_files.clear
94
+ end
95
+ task 'core:spec' => [:hack_spec, 'gem:spec']
96
+
97
+ desc "merge psych in to ruby trunk"
98
+ namespace :merge do
99
+ basedir = File.expand_path File.dirname __FILE__
100
+ rubydir = File.join ENV['HOME'], 'git', 'ruby'
101
+ mergedirs = {
102
+ # From # To
103
+ [basedir, 'ext', 'psych/'] => [rubydir, 'ext', 'psych/'],
104
+ [basedir, 'lib/'] => [rubydir, 'ext', 'psych', 'lib/'],
105
+ [basedir, 'test', 'psych/'] => [rubydir, 'test', 'psych/'],
106
+ }
107
+
108
+ rsync = 'rsync -av --exclude lib --exclude ".*" --exclude "*.o" --exclude Makefile --exclude mkmf.log --delete'
109
+
110
+ task :to_ruby do
111
+ mergedirs.each do |from, to|
112
+ sh "#{rsync} #{File.join(*from)} #{File.join(*to)}"
113
+ end
114
+ end
115
+
116
+ task :from_ruby do
117
+ mergedirs.each do |from, to|
118
+ sh "#{rsync} #{File.join(*to)} #{File.join(*from)}"
119
+ end
120
+ end
121
+ end
122
+
123
+ # vim: syntax=ruby
data/ext/psych/depend ADDED
@@ -0,0 +1,3 @@
1
+ $(OBJS): $(HDRS) $(ruby_headers) \
2
+ $(hdrdir)/ruby/encoding.h \
3
+ $(hdrdir)/ruby/oniguruma.h
@@ -0,0 +1,38 @@
1
+ # -*- coding: us-ascii -*-
2
+ require 'mkmf'
3
+ require 'fileutils'
4
+
5
+ # :stopdoc:
6
+
7
+ dir_config 'libyaml'
8
+
9
+ if enable_config("bundled-libyaml", false) || !(find_header('yaml.h') && find_library('yaml', 'yaml_get_version'))
10
+ # Embed libyaml since we could not find it.
11
+
12
+ $VPATH << "$(srcdir)/yaml"
13
+ $INCFLAGS << " -I$(srcdir)/yaml"
14
+
15
+ $srcs = Dir.glob("#{$srcdir}/{,yaml/}*.c").map {|n| File.basename(n)}
16
+
17
+ if have_macro("_WIN32")
18
+ $CPPFLAGS << " -DYAML_DECLARE_STATIC -DHAVE_CONFIG_H"
19
+ end
20
+
21
+ have_header 'dlfcn.h'
22
+ have_header 'inttypes.h'
23
+ have_header 'memory.h'
24
+ have_header 'stdint.h'
25
+ have_header 'stdlib.h'
26
+ have_header 'strings.h'
27
+ have_header 'string.h'
28
+ have_header 'sys/stat.h'
29
+ have_header 'sys/types.h'
30
+ have_header 'unistd.h'
31
+
32
+ find_header 'yaml.h'
33
+ have_header 'config.h'
34
+ end
35
+
36
+ create_makefile 'psych'
37
+
38
+ # :startdoc:
data/ext/psych/psych.c ADDED
@@ -0,0 +1,34 @@
1
+ #include <psych.h>
2
+
3
+ /* call-seq: Psych.libyaml_version
4
+ *
5
+ * Returns the version of libyaml being used
6
+ */
7
+ static VALUE libyaml_version(VALUE module)
8
+ {
9
+ int major, minor, patch;
10
+ VALUE list[3];
11
+
12
+ yaml_get_version(&major, &minor, &patch);
13
+
14
+ list[0] = INT2NUM((long)major);
15
+ list[1] = INT2NUM((long)minor);
16
+ list[2] = INT2NUM((long)patch);
17
+
18
+ return rb_ary_new4((long)3, list);
19
+ }
20
+
21
+ VALUE mPsych;
22
+
23
+ void Init_psych(void)
24
+ {
25
+ mPsych = rb_define_module("Psych");
26
+
27
+ rb_define_singleton_method(mPsych, "libyaml_version", libyaml_version, 0);
28
+
29
+ Init_psych_parser();
30
+ Init_psych_emitter();
31
+ Init_psych_to_ruby();
32
+ Init_psych_yaml_tree();
33
+ }
34
+ /* vim: set noet sws=4 sw=4: */
data/ext/psych/psych.h ADDED
@@ -0,0 +1,20 @@
1
+ #ifndef PSYCH_H
2
+ #define PSYCH_H
3
+
4
+ #include <ruby.h>
5
+
6
+ #ifdef HAVE_RUBY_ENCODING_H
7
+ #include <ruby/encoding.h>
8
+ #endif
9
+
10
+ #include <yaml.h>
11
+
12
+ #include <psych_parser.h>
13
+ #include <psych_emitter.h>
14
+ #include <psych_to_ruby.h>
15
+ #include <psych_yaml_tree.h>
16
+
17
+ extern VALUE mPsych;
18
+
19
+
20
+ #endif
@@ -0,0 +1,555 @@
1
+ #include <psych.h>
2
+
3
+ VALUE cPsychEmitter;
4
+ static ID id_write;
5
+ static ID id_line_width;
6
+ static ID id_indentation;
7
+ static ID id_canonical;
8
+
9
+ static void emit(yaml_emitter_t * emitter, yaml_event_t * event)
10
+ {
11
+ if(!yaml_emitter_emit(emitter, event))
12
+ rb_raise(rb_eRuntimeError, "%s", emitter->problem);
13
+ }
14
+
15
+ static int writer(void *ctx, unsigned char *buffer, size_t size)
16
+ {
17
+ VALUE io = (VALUE)ctx;
18
+ VALUE str = rb_str_new((const char *)buffer, (long)size);
19
+ VALUE wrote = rb_funcall(io, id_write, 1, str);
20
+ return (int)NUM2INT(wrote);
21
+ }
22
+
23
+ static void dealloc(void * ptr)
24
+ {
25
+ yaml_emitter_t * emitter;
26
+
27
+ emitter = (yaml_emitter_t *)ptr;
28
+ yaml_emitter_delete(emitter);
29
+ xfree(emitter);
30
+ }
31
+
32
+ #if 0
33
+ static size_t memsize(const void *ptr)
34
+ {
35
+ const yaml_emitter_t *emitter = ptr;
36
+ /* TODO: calculate emitter's size */
37
+ return 0;
38
+ }
39
+ #endif
40
+
41
+ static const rb_data_type_t psych_emitter_type = {
42
+ "Psych/emitter",
43
+ {0, dealloc, 0,},
44
+ 0, 0,
45
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
46
+ RUBY_TYPED_FREE_IMMEDIATELY,
47
+ #endif
48
+ };
49
+
50
+ static VALUE allocate(VALUE klass)
51
+ {
52
+ yaml_emitter_t * emitter;
53
+ VALUE obj = TypedData_Make_Struct(klass, yaml_emitter_t, &psych_emitter_type, emitter);
54
+
55
+ yaml_emitter_initialize(emitter);
56
+ yaml_emitter_set_unicode(emitter, 1);
57
+ yaml_emitter_set_indent(emitter, 2);
58
+
59
+ return obj;
60
+ }
61
+
62
+ /* call-seq: Psych::Emitter.new(io, options = Psych::Emitter::OPTIONS)
63
+ *
64
+ * Create a new Psych::Emitter that writes to +io+.
65
+ */
66
+ static VALUE initialize(int argc, VALUE *argv, VALUE self)
67
+ {
68
+ yaml_emitter_t * emitter;
69
+ VALUE io, options;
70
+ VALUE line_width;
71
+ VALUE indent;
72
+ VALUE canonical;
73
+
74
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
75
+
76
+ if (rb_scan_args(argc, argv, "11", &io, &options) == 2) {
77
+ line_width = rb_funcall(options, id_line_width, 0);
78
+ indent = rb_funcall(options, id_indentation, 0);
79
+ canonical = rb_funcall(options, id_canonical, 0);
80
+
81
+ yaml_emitter_set_width(emitter, NUM2INT(line_width));
82
+ yaml_emitter_set_indent(emitter, NUM2INT(indent));
83
+ yaml_emitter_set_canonical(emitter, Qtrue == canonical ? 1 : 0);
84
+ }
85
+
86
+ yaml_emitter_set_output(emitter, writer, (void *)io);
87
+
88
+ return self;
89
+ }
90
+
91
+ /* call-seq: emitter.start_stream(encoding)
92
+ *
93
+ * Start a stream emission with +encoding+
94
+ *
95
+ * See Psych::Handler#start_stream
96
+ */
97
+ static VALUE start_stream(VALUE self, VALUE encoding)
98
+ {
99
+ yaml_emitter_t * emitter;
100
+ yaml_event_t event;
101
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
102
+ Check_Type(encoding, T_FIXNUM);
103
+
104
+ yaml_stream_start_event_initialize(&event, (yaml_encoding_t)NUM2INT(encoding));
105
+
106
+ emit(emitter, &event);
107
+
108
+ return self;
109
+ }
110
+
111
+ /* call-seq: emitter.end_stream
112
+ *
113
+ * End a stream emission
114
+ *
115
+ * See Psych::Handler#end_stream
116
+ */
117
+ static VALUE end_stream(VALUE self)
118
+ {
119
+ yaml_emitter_t * emitter;
120
+ yaml_event_t event;
121
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
122
+
123
+ yaml_stream_end_event_initialize(&event);
124
+
125
+ emit(emitter, &event);
126
+
127
+ return self;
128
+ }
129
+
130
+ /* call-seq: emitter.start_document(version, tags, implicit)
131
+ *
132
+ * Start a document emission with YAML +version+, +tags+, and an +implicit+
133
+ * start.
134
+ *
135
+ * See Psych::Handler#start_document
136
+ */
137
+ static VALUE start_document(VALUE self, VALUE version, VALUE tags, VALUE imp)
138
+ {
139
+ yaml_emitter_t * emitter;
140
+ yaml_tag_directive_t * head = NULL;
141
+ yaml_tag_directive_t * tail = NULL;
142
+ yaml_event_t event;
143
+ yaml_version_directive_t version_directive;
144
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
145
+
146
+
147
+ Check_Type(version, T_ARRAY);
148
+
149
+ if(RARRAY_LEN(version) > 0) {
150
+ VALUE major = rb_ary_entry(version, (long)0);
151
+ VALUE minor = rb_ary_entry(version, (long)1);
152
+
153
+ version_directive.major = NUM2INT(major);
154
+ version_directive.minor = NUM2INT(minor);
155
+ }
156
+
157
+ if(RTEST(tags)) {
158
+ int i = 0;
159
+ #ifdef HAVE_RUBY_ENCODING_H
160
+ rb_encoding * encoding = rb_utf8_encoding();
161
+ #endif
162
+
163
+ Check_Type(tags, T_ARRAY);
164
+
165
+ head = xcalloc((size_t)RARRAY_LEN(tags), sizeof(yaml_tag_directive_t));
166
+ tail = head;
167
+
168
+ for(i = 0; i < RARRAY_LEN(tags); i++) {
169
+ VALUE tuple = RARRAY_PTR(tags)[i];
170
+ VALUE name;
171
+ VALUE value;
172
+
173
+ Check_Type(tuple, T_ARRAY);
174
+
175
+ if(RARRAY_LEN(tuple) < 2) {
176
+ xfree(head);
177
+ rb_raise(rb_eRuntimeError, "tag tuple must be of length 2");
178
+ }
179
+ name = RARRAY_PTR(tuple)[0];
180
+ value = RARRAY_PTR(tuple)[1];
181
+ #ifdef HAVE_RUBY_ENCODING_H
182
+ name = rb_str_export_to_enc(name, encoding);
183
+ value = rb_str_export_to_enc(value, encoding);
184
+ #endif
185
+
186
+ tail->handle = (yaml_char_t *)StringValuePtr(name);
187
+ tail->prefix = (yaml_char_t *)StringValuePtr(value);
188
+
189
+ tail++;
190
+ }
191
+ }
192
+
193
+ yaml_document_start_event_initialize(
194
+ &event,
195
+ (RARRAY_LEN(version) > 0) ? &version_directive : NULL,
196
+ head,
197
+ tail,
198
+ imp ? 1 : 0
199
+ );
200
+
201
+ emit(emitter, &event);
202
+
203
+ if(head) xfree(head);
204
+
205
+ return self;
206
+ }
207
+
208
+ /* call-seq: emitter.end_document(implicit)
209
+ *
210
+ * End a document emission with an +implicit+ ending.
211
+ *
212
+ * See Psych::Handler#end_document
213
+ */
214
+ static VALUE end_document(VALUE self, VALUE imp)
215
+ {
216
+ yaml_emitter_t * emitter;
217
+ yaml_event_t event;
218
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
219
+
220
+ yaml_document_end_event_initialize(&event, imp ? 1 : 0);
221
+
222
+ emit(emitter, &event);
223
+
224
+ return self;
225
+ }
226
+
227
+ /* call-seq: emitter.scalar(value, anchor, tag, plain, quoted, style)
228
+ *
229
+ * Emit a scalar with +value+, +anchor+, +tag+, and a +plain+ or +quoted+
230
+ * string type with +style+.
231
+ *
232
+ * See Psych::Handler#scalar
233
+ */
234
+ static VALUE scalar(
235
+ VALUE self,
236
+ VALUE value,
237
+ VALUE anchor,
238
+ VALUE tag,
239
+ VALUE plain,
240
+ VALUE quoted,
241
+ VALUE style
242
+ ) {
243
+ yaml_emitter_t * emitter;
244
+ yaml_event_t event;
245
+ #ifdef HAVE_RUBY_ENCODING_H
246
+ rb_encoding *encoding;
247
+ #endif
248
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
249
+
250
+ Check_Type(value, T_STRING);
251
+
252
+ #ifdef HAVE_RUBY_ENCODING_H
253
+ encoding = rb_utf8_encoding();
254
+
255
+ value = rb_str_export_to_enc(value, encoding);
256
+
257
+ if(!NIL_P(anchor)) {
258
+ Check_Type(anchor, T_STRING);
259
+ anchor = rb_str_export_to_enc(anchor, encoding);
260
+ }
261
+
262
+ if(!NIL_P(tag)) {
263
+ Check_Type(tag, T_STRING);
264
+ tag = rb_str_export_to_enc(tag, encoding);
265
+ }
266
+ #endif
267
+
268
+ yaml_scalar_event_initialize(
269
+ &event,
270
+ (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
271
+ (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
272
+ (yaml_char_t*)StringValuePtr(value),
273
+ (int)RSTRING_LEN(value),
274
+ plain ? 1 : 0,
275
+ quoted ? 1 : 0,
276
+ (yaml_scalar_style_t)NUM2INT(style)
277
+ );
278
+
279
+ emit(emitter, &event);
280
+
281
+ return self;
282
+ }
283
+
284
+ /* call-seq: emitter.start_sequence(anchor, tag, implicit, style)
285
+ *
286
+ * Start emitting a sequence with +anchor+, a +tag+, +implicit+ sequence
287
+ * start and end, along with +style+.
288
+ *
289
+ * See Psych::Handler#start_sequence
290
+ */
291
+ static VALUE start_sequence(
292
+ VALUE self,
293
+ VALUE anchor,
294
+ VALUE tag,
295
+ VALUE implicit,
296
+ VALUE style
297
+ ) {
298
+ yaml_emitter_t * emitter;
299
+ yaml_event_t event;
300
+
301
+ #ifdef HAVE_RUBY_ENCODING_H
302
+ rb_encoding * encoding = rb_utf8_encoding();
303
+
304
+ if(!NIL_P(anchor)) {
305
+ Check_Type(anchor, T_STRING);
306
+ anchor = rb_str_export_to_enc(anchor, encoding);
307
+ }
308
+
309
+ if(!NIL_P(tag)) {
310
+ Check_Type(tag, T_STRING);
311
+ tag = rb_str_export_to_enc(tag, encoding);
312
+ }
313
+ #endif
314
+
315
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
316
+
317
+ yaml_sequence_start_event_initialize(
318
+ &event,
319
+ (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
320
+ (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
321
+ implicit ? 1 : 0,
322
+ (yaml_sequence_style_t)NUM2INT(style)
323
+ );
324
+
325
+ emit(emitter, &event);
326
+
327
+ return self;
328
+ }
329
+
330
+ /* call-seq: emitter.end_sequence
331
+ *
332
+ * End sequence emission.
333
+ *
334
+ * See Psych::Handler#end_sequence
335
+ */
336
+ static VALUE end_sequence(VALUE self)
337
+ {
338
+ yaml_emitter_t * emitter;
339
+ yaml_event_t event;
340
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
341
+
342
+ yaml_sequence_end_event_initialize(&event);
343
+
344
+ emit(emitter, &event);
345
+
346
+ return self;
347
+ }
348
+
349
+ /* call-seq: emitter.start_mapping(anchor, tag, implicit, style)
350
+ *
351
+ * Start emitting a YAML map with +anchor+, +tag+, an +implicit+ start
352
+ * and end, and +style+.
353
+ *
354
+ * See Psych::Handler#start_mapping
355
+ */
356
+ static VALUE start_mapping(
357
+ VALUE self,
358
+ VALUE anchor,
359
+ VALUE tag,
360
+ VALUE implicit,
361
+ VALUE style
362
+ ) {
363
+ yaml_emitter_t * emitter;
364
+ yaml_event_t event;
365
+ #ifdef HAVE_RUBY_ENCODING_H
366
+ rb_encoding *encoding;
367
+ #endif
368
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
369
+
370
+ #ifdef HAVE_RUBY_ENCODING_H
371
+ encoding = rb_utf8_encoding();
372
+
373
+ if(!NIL_P(anchor)) {
374
+ Check_Type(anchor, T_STRING);
375
+ anchor = rb_str_export_to_enc(anchor, encoding);
376
+ }
377
+
378
+ if(!NIL_P(tag)) {
379
+ Check_Type(tag, T_STRING);
380
+ tag = rb_str_export_to_enc(tag, encoding);
381
+ }
382
+ #endif
383
+
384
+ yaml_mapping_start_event_initialize(
385
+ &event,
386
+ (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
387
+ (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
388
+ implicit ? 1 : 0,
389
+ (yaml_mapping_style_t)NUM2INT(style)
390
+ );
391
+
392
+ emit(emitter, &event);
393
+
394
+ return self;
395
+ }
396
+
397
+ /* call-seq: emitter.end_mapping
398
+ *
399
+ * Emit the end of a mapping.
400
+ *
401
+ * See Psych::Handler#end_mapping
402
+ */
403
+ static VALUE end_mapping(VALUE self)
404
+ {
405
+ yaml_emitter_t * emitter;
406
+ yaml_event_t event;
407
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
408
+
409
+ yaml_mapping_end_event_initialize(&event);
410
+
411
+ emit(emitter, &event);
412
+
413
+ return self;
414
+ }
415
+
416
+ /* call-seq: emitter.alias(anchor)
417
+ *
418
+ * Emit an alias with +anchor+.
419
+ *
420
+ * See Psych::Handler#alias
421
+ */
422
+ static VALUE alias(VALUE self, VALUE anchor)
423
+ {
424
+ yaml_emitter_t * emitter;
425
+ yaml_event_t event;
426
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
427
+
428
+ #ifdef HAVE_RUBY_ENCODING_H
429
+ if(!NIL_P(anchor)) {
430
+ Check_Type(anchor, T_STRING);
431
+ anchor = rb_str_export_to_enc(anchor, rb_utf8_encoding());
432
+ }
433
+ #endif
434
+
435
+ yaml_alias_event_initialize(
436
+ &event,
437
+ (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor))
438
+ );
439
+
440
+ emit(emitter, &event);
441
+
442
+ return self;
443
+ }
444
+
445
+ /* call-seq: emitter.canonical = true
446
+ *
447
+ * Set the output style to canonical, or not.
448
+ */
449
+ static VALUE set_canonical(VALUE self, VALUE style)
450
+ {
451
+ yaml_emitter_t * emitter;
452
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
453
+
454
+ yaml_emitter_set_canonical(emitter, Qtrue == style ? 1 : 0);
455
+
456
+ return style;
457
+ }
458
+
459
+ /* call-seq: emitter.canonical
460
+ *
461
+ * Get the output style, canonical or not.
462
+ */
463
+ static VALUE canonical(VALUE self)
464
+ {
465
+ yaml_emitter_t * emitter;
466
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
467
+
468
+ return (emitter->canonical == 0) ? Qfalse : Qtrue;
469
+ }
470
+
471
+ /* call-seq: emitter.indentation = level
472
+ *
473
+ * Set the indentation level to +level+. The level must be less than 10 and
474
+ * greater than 1.
475
+ */
476
+ static VALUE set_indentation(VALUE self, VALUE level)
477
+ {
478
+ yaml_emitter_t * emitter;
479
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
480
+
481
+ yaml_emitter_set_indent(emitter, NUM2INT(level));
482
+
483
+ return level;
484
+ }
485
+
486
+ /* call-seq: emitter.indentation
487
+ *
488
+ * Get the indentation level.
489
+ */
490
+ static VALUE indentation(VALUE self)
491
+ {
492
+ yaml_emitter_t * emitter;
493
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
494
+
495
+ return INT2NUM(emitter->best_indent);
496
+ }
497
+
498
+ /* call-seq: emitter.line_width
499
+ *
500
+ * Get the preferred line width.
501
+ */
502
+ static VALUE line_width(VALUE self)
503
+ {
504
+ yaml_emitter_t * emitter;
505
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
506
+
507
+ return INT2NUM(emitter->best_width);
508
+ }
509
+
510
+ /* call-seq: emitter.line_width = width
511
+ *
512
+ * Set the preferred line with to +width+.
513
+ */
514
+ static VALUE set_line_width(VALUE self, VALUE width)
515
+ {
516
+ yaml_emitter_t * emitter;
517
+ TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
518
+
519
+ yaml_emitter_set_width(emitter, NUM2INT(width));
520
+
521
+ return width;
522
+ }
523
+
524
+ void Init_psych_emitter(void)
525
+ {
526
+ VALUE psych = rb_define_module("Psych");
527
+ VALUE handler = rb_define_class_under(psych, "Handler", rb_cObject);
528
+ cPsychEmitter = rb_define_class_under(psych, "Emitter", handler);
529
+
530
+ rb_define_alloc_func(cPsychEmitter, allocate);
531
+
532
+ rb_define_method(cPsychEmitter, "initialize", initialize, -1);
533
+ rb_define_method(cPsychEmitter, "start_stream", start_stream, 1);
534
+ rb_define_method(cPsychEmitter, "end_stream", end_stream, 0);
535
+ rb_define_method(cPsychEmitter, "start_document", start_document, 3);
536
+ rb_define_method(cPsychEmitter, "end_document", end_document, 1);
537
+ rb_define_method(cPsychEmitter, "scalar", scalar, 6);
538
+ rb_define_method(cPsychEmitter, "start_sequence", start_sequence, 4);
539
+ rb_define_method(cPsychEmitter, "end_sequence", end_sequence, 0);
540
+ rb_define_method(cPsychEmitter, "start_mapping", start_mapping, 4);
541
+ rb_define_method(cPsychEmitter, "end_mapping", end_mapping, 0);
542
+ rb_define_method(cPsychEmitter, "alias", alias, 1);
543
+ rb_define_method(cPsychEmitter, "canonical", canonical, 0);
544
+ rb_define_method(cPsychEmitter, "canonical=", set_canonical, 1);
545
+ rb_define_method(cPsychEmitter, "indentation", indentation, 0);
546
+ rb_define_method(cPsychEmitter, "indentation=", set_indentation, 1);
547
+ rb_define_method(cPsychEmitter, "line_width", line_width, 0);
548
+ rb_define_method(cPsychEmitter, "line_width=", set_line_width, 1);
549
+
550
+ id_write = rb_intern("write");
551
+ id_line_width = rb_intern("line_width");
552
+ id_indentation = rb_intern("indentation");
553
+ id_canonical = rb_intern("canonical");
554
+ }
555
+ /* vim: set noet sws=4 sw=4: */