Sha256: 67d1dbc495b62479db5e553d95f7be91273cd0dbfbec4faaed108e76c1389ac7

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

# file_modification.rb
# Copyright (C) RĂ©mi Even 2016
#
# This file is part of Xolti.
#
# Xolti is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Xolti 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Xolti. If not, see <http://www.gnu.org/licenses/>.
module FileModification
	def FileModification.insert_lines_with_offset(path, text, offset)
		file = Tempfile.new("xolti")
		begin
			File.open(path, "r") do |source_file|
				i = 0
				source_file.each_line do |line|
					file.write(text) if i == offset
					i += 1
					file.write(line)
				end
			end
			file.close()
			FileUtils.cp(file, path)
		ensure
			file.close()
			file.unlink()
		end
	end

	def FileModification.delete_lines(path, start, length)
		file = Tempfile.new("xolti")
		begin
			File.open(path, "r").each_with_index do |line, index|
				file.write(line) if index < start || index >= start + length
			end
			file.close()
			FileUtils.cp(file, path)
		ensure
			file.close()
			file.unlink()
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xolti-0.1.1 lib/file_modification.rb
xolti-0.1.0 lib/file_modification.rb