summaryrefslogtreecommitdiff
path: root/ext/syck/lib/syck/stringio.rb
diff options
context:
space:
mode:
authortenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-04-03 21:50:47 +0000
committertenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-04-03 21:50:47 +0000
commiteb71e5cd67f4ce4da5ba331f93a8e36c65457b94 (patch)
tree1df66113ae7340e2b928839b3f09dbf8dcfb3d73 /ext/syck/lib/syck/stringio.rb
parentca926ad017a804817b9a2e5389196f9acf9d9b68 (diff)
* lib/yaml: Moved to ext/syck/lib, Syck only uses Syck constant.
* lib/yaml.rb: Added an engine manager for choosing YAML engine. * ext/syck/lib/syck/rubytypes.rb: squashed warnings when using Psych git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27212 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/syck/lib/syck/stringio.rb')
-rw-r--r--ext/syck/lib/syck/stringio.rb85
1 files changed, 85 insertions, 0 deletions
diff --git a/ext/syck/lib/syck/stringio.rb b/ext/syck/lib/syck/stringio.rb
new file mode 100644
index 0000000000..77a2b827e5
--- /dev/null
+++ b/ext/syck/lib/syck/stringio.rb
@@ -0,0 +1,85 @@
+warn "#{caller[0]}: yaml/stringio is deprecated" if $VERBOSE
+
+#
+# Limited StringIO if no core lib is available
+#
+begin
+require 'stringio'
+rescue LoadError
+ # StringIO based on code by MoonWolf
+ class StringIO
+ def initialize(string="")
+ @string=string
+ @pos=0
+ @eof=(string.size==0)
+ end
+ def pos
+ @pos
+ end
+ def eof
+ @eof
+ end
+ alias eof? eof
+ def readline(rs=$/)
+ if @eof
+ raise EOFError
+ else
+ if p = @string[@pos..-1]=~rs
+ line = @string[@pos,p+1]
+ else
+ line = @string[@pos..-1]
+ end
+ @pos+=line.size
+ @eof =true if @pos==@string.size
+ $_ = line
+ end
+ end
+ def rewind
+ seek(0,0)
+ end
+ def seek(offset,whence)
+ case whence
+ when 0
+ @pos=offset
+ when 1
+ @pos+=offset
+ when 2
+ @pos=@string.size+offset
+ end
+ @eof=(@pos>=@string.size)
+ 0
+ end
+ end
+
+ #
+ # Class method for creating streams
+ #
+ def Syck.make_stream( io )
+ if String === io
+ io = StringIO.new( io )
+ elsif not IO === io
+ raise Syck::Error, "YAML stream must be an IO or String object."
+ end
+ if Syck::unicode
+ def io.readline
+ Syck.utf_to_internal( readline( @ln_sep ), @utf_encoding )
+ end
+ def io.check_unicode
+ @utf_encoding = Syck.sniff_encoding( read( 4 ) )
+ @ln_sep = Syck.enc_separator( @utf_encoding )
+ seek( -4, IO::SEEK_CUR )
+ end
+ def io.utf_encoding
+ @utf_encoding
+ end
+ io.check_unicode
+ else
+ def io.utf_encoding
+ :None
+ end
+ end
+ io
+ end
+
+end
+