Class: SSCBot::SSCFile

Inherits:
File
  • Object
show all
Defined in:
lib/ssc.bot/ssc_file.rb

Overview

Author:

  • Jonathan Bradley Whited

Since:

  • 0.1.0

Direct Known Subclasses

ChatLogFile

Constant Summary collapse

DEFAULT_BUFFER_LEN =

Since:

  • 0.1.0

520
DEFAULT_ENCODING =

Since:

  • 0.1.0

'Windows-1252:UTF-8'
DEFAULT_MODE =

Since:

  • 0.1.0

'rt'
DEFAULT_SEPARATOR =

Instead, could use /R/ for Ruby v2.0+.

Since:

  • 0.1.0

/\r?\n|\r/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, mode = DEFAULT_MODE, buffer_len: DEFAULT_BUFFER_LEN, encoding: DEFAULT_ENCODING, separator: DEFAULT_SEPARATOR, **opt) ⇒ SSCFile

Returns a new instance of SSCFile.

Since:

  • 0.1.0



59
60
61
62
63
64
65
66
# 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

Class Method Details

.clear_content(filename, strip: true, textmode: true, **opt) ⇒ Object

Clear (truncate) the contents of filename.

Parameters:

  • filename (String)

    the file to clear

  • strip (Boolean) (defaults to: true)

    true to strip filename to help prevent fat-fingering, else false to not

Since:

  • 0.1.0



29
30
31
32
33
34
35
36
37
38
39
# 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

.soft_touch(filename, strip: true, textmode: true, **opt) ⇒ Object

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

I just prefer this over FileUtils.touch.

Parameters:

  • filename (String)

    the file to soft touch

  • strip (Boolean) (defaults to: true)

    true to strip filename to help prevent fat-fingering, else false to not

Since:

  • 0.1.0



48
49
50
51
52
53
54
55
56
57
# 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

Instance Method Details

#read_ulineObject

Read a universal line.

Since:

  • 0.1.0



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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_endObject

Since:

  • 0.1.0



113
114
115
116
117
118
119
# File 'lib/ssc.bot/ssc_file.rb', line 113

def seek_to_end
  result = seek(0,:END)

  read_uline # Justin Case

  return result
end