class SSCBot::SSCFile

@author Jonathan Bradley Whited @since 0.1.0

Constants

DEFAULT_BUFFER_LEN
DEFAULT_ENCODING
DEFAULT_MODE
DEFAULT_SEPARATOR

Public Class Methods

clear_content(filename,strip: true,textmode: true,**opt) click to toggle source

Clear (truncate) the contents of filename.

@param filename [String] the file to clear @param strip [Boolean] true to strip filename to help prevent fat-fingering, else false to not

# File lib/ssc.bot/ssc_file.rb, line 29
def self.clear_content(filename,strip: true,textmode: true,**opt)
  filename = Util.u_strip(filename) if strip

  return if filename.empty?
  return unless File.file?(filename) # Also checks if exists.

  # Clear the file.
  # - Do NOT call truncate() as it's not available on all platforms.
  self.open(filename,'w',textmode: textmode,**opt) do |file|
  end
end
new(filename,mode=DEFAULT_MODE,buffer_len: DEFAULT_BUFFER_LEN,encoding: DEFAULT_ENCODING, separator: DEFAULT_SEPARATOR,**opt) click to toggle source
Calls superclass method
# File lib/ssc.bot/ssc_file.rb, line 59
def initialize(filename,mode=DEFAULT_MODE,buffer_len: DEFAULT_BUFFER_LEN,encoding: DEFAULT_ENCODING,
               separator: DEFAULT_SEPARATOR,**opt)
  super(filename,mode,encoding: encoding,**opt)

  @sscbot_buffer = nil
  @sscbot_buffer_len = buffer_len
  @sscbot_separator = separator
end
soft_touch(filename,strip: true,textmode: true,**opt) click to toggle source

If filename exists, then it does nothing (does not update time), else, it creates the file.

I just prefer this over FileUtils.touch.

@param filename [String] the file to soft touch @param strip [Boolean] true to strip filename to help prevent fat-fingering, else false to not

# File lib/ssc.bot/ssc_file.rb, line 48
def self.soft_touch(filename,strip: true,textmode: true,**opt)
  filename = Util.u_strip(filename) if strip

  return if filename.empty?
  return if File.exist?(filename)

  # Create the file.
  self.open(filename,'a',textmode: textmode,**opt) do |file|
  end
end

Public Instance Methods

read_uline() click to toggle source

Read a universal line.

# File lib/ssc.bot/ssc_file.rb, line 69
def read_uline
  if @sscbot_buffer.nil?
    # See comment at loop below.
    # - Use gets() instead of eof?() because of this method's name.
    line = gets(nil,@sscbot_buffer_len)

    return nil if line.nil? # Still EOF?

    @sscbot_buffer = line
  end

  lines = @sscbot_buffer.split(@sscbot_separator,2)

  # Will only have 2 if there was a separator.
  if lines.length == 2
    @sscbot_buffer = lines[1]

    return lines[0]
  end

  # - Use a separator of nil to get all of the different types of newlines.
  # - Use gets() [instead of read(), etc.] to work probably with text (e.g., UTF-8)
  #   and to not throw an error at EOF (returns nil).
  while !(line = gets(nil,@sscbot_buffer_len)).nil?
    lines = line.split(@sscbot_separator,2)

    # Will only have 2 if there was a separator.
    if lines.length == 2
      line = "#{@sscbot_buffer}#{lines[0]}"
      @sscbot_buffer = lines[1]

      return line
    else
      @sscbot_buffer << line
    end
  end

  # EOF reached with text in the buffer.
  line = @sscbot_buffer
  @sscbot_buffer = nil

  return line
end
seek_to_end() click to toggle source
# File lib/ssc.bot/ssc_file.rb, line 113
def seek_to_end
  result = seek(0,:END)

  read_uline # Justin Case

  return result
end