lib/ronin/extensions/file.rb in ronin-support-0.5.1 vs lib/ronin/extensions/file.rb in ronin-support-0.5.2
- old
+ new
@@ -1,22 +1,22 @@
#
-# Copyright (c) 2006-2012 Hal Brodigan (postmodern.mod3 at gmail.com)
+# Copyright (c) 2006-2021 Hal Brodigan (postmodern.mod3 at gmail.com)
#
-# This file is part of Ronin Support.
+# This file is part of ronin-support.
#
-# Ronin Support is free software: you can redistribute it and/or modify
+# ronin-support is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
-# Ronin Support is distributed in the hope that it will be useful,
+# ronin-support is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
-# along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
+# along with ronin-support. If not, see <https://www.gnu.org/licenses/>.
#
class File
#
@@ -41,16 +41,14 @@
#
# @since 0.3.0
#
# @api public
#
- def File.each_line(path)
+ def self.each_line(path)
return enum_for(__method__,path) unless block_given?
- File.open(path) do |file|
- file.each_line { |line| yield line.chomp }
- end
+ foreach(path) { |line| yield line.chomp }
end
#
# Reads each row from the file.
#
@@ -76,34 +74,42 @@
#
# @since 0.3.0
#
# @api public
#
- def File.each_row(path,separator=/\s+/)
+ def self.each_row(path,separator=/\s+/)
return enum_for(__method__,path,separator) unless block_given?
- File.each_line(path) { |line| yield line.split(separator) }
+ each_line(path) { |line| yield line.split(separator) }
end
- #
- # Writes the given data to a specified path.
- #
- # @param [String] path
- # The path of the file to write to.
- #
- # @param [String] data
- # The data to write to the file.
- #
- # @return [nil]
- #
- # @example
- # File.write('dump.txt',data)
- #
- # @api public
- #
- def File.write(path,data)
- File.open(path,'w') { |file| file.write(data) }
+ if RUBY_VERSION < '1.9.'
+ #
+ # Writes the given data to a specified path.
+ #
+ # @param [String] path
+ # The path of the file to write to.
+ #
+ # @param [String] data
+ # The data to write to the file.
+ #
+ # @param [Integer] offset
+ # Optional offset to write the data to.
+ #
+ # @return [nil]
+ #
+ # @example
+ # File.write('dump.txt',data)
+ #
+ # @api public
+ #
+ def self.write(path,data,offset=0)
+ open(path,'w') do |file|
+ file.seek(offset)
+ file.write(data)
+ end
+ end
end
#
# Escapes a path.
#
@@ -113,19 +119,19 @@
# @return [String]
# The escaped path.
#
# @api public
#
- def File.escape_path(path)
+ def self.escape_path(path)
path = path.to_s
# remove any \0 characters first
path.tr!("\0",'')
# remove any home-dir expansions
path.gsub!('~',"\\~")
- path = File.expand_path(File.join('/',path))
+ path = expand_path(File.join('/',path))
# remove the leading slash
return path[1..-1]
end