Sha256: 3a4690005ad2b882f68f8c7ce194f21ae912641579e956bb2898c5ad1084e043

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

require 'ftools'

module FileUtil
  
  def FileUtil.read_file(path)
    FileUtil.read_file_to_array(path).join('')
  end
  
  def FileUtil.read_file_to_array(path)
    contents = []
    return contents unless File.exists?(path)
    
    open(path, 'r') do |l|
      contents = l.readlines
    end

    contents
  end
  def FileUtil.read_binary_file(path)
    contents = ''
    return contents unless File.exists?(path)
    
    open(path, 'rb') do |l|
      while (!l.eof?)
        contents << l.read(4096)
      end
    end

    contents
  end
  
  
  
  def FileUtil.write_file(path, content, flush=true)
    FileUtil.write_or_append_file('w', path, content, flush)
  end
  
  def FileUtil.append_file(path, content, flush=true)
    FileUtil.write_or_append_file('a', path, content, flush)
  end
  
  def FileUtil.write_or_append_file(write_or_append, path, content = '', flush = true)
    #STDERR.puts "Writing to #{ path }..." 
    FileUtil.create_dir(File.dirname(path))
    
    open(path, write_or_append) do |f| 
      f.puts content
      f.flush if flush;
    end
    File.chmod(0600, path)
  end
  
  def FileUtil.create_dir(dirpath)
    return if File.directory?(dirpath)
    
    #STDERR.puts "Creating #{ dirpath }"
    File.makedirs(dirpath)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stella-0.3.2 lib/utils/fileutil.rb
stella-0.5.1 lib/utils/fileutil.rb
stella-0.5.3 lib/utils/fileutil.rb