# class GxG::IO::File

### Public Class Methods

new(*args) click to toggle source

As of Ruby 1.9.2: if a file is not set to :binary mode, it defaults to :text mode. See: www.tutorialspoint.com/ruby/ruby_input_output.htm <file>.stat table: (mode example : 0100664)

    0700       rwx mask for owner
    0400       r for owner
    0200       w for owner
    0100       x for owner
    0070       rwx mask for group
    0040       r for group
    0020       w for group
    0010       x for group
    0007       rwx mask for other
    0004       r for other
    0002       w for other
    0001       x for other
    4000       Set user ID on execution
    2000       Set group ID on execution
    1000       Save swapped text, even after use

Calls superclass method `GxG::IO::IO::new`

    # File lib/gxg/gxg_io.rb, line 2577
    def initialize(*args)
      if args[0].is_a?(String)
        the_path = args[0]
        the_mode = (args[1] || "r")
        the_permissions = args[2]
        the_options = args[3]
      else
        raise ArgumentError, "filename or path must be passed as a String"
      end
      #
      if the_options
        params = self.process_parameters(0,the_mode,the_options)
      else
        params = self.process_parameters(0,the_mode)
      end
      #
      mode_flags = {:binary => ::File::BINARY, :text => ::File::TEXT, :read => ::File::RDONLY, :write => ::File::WRONLY, :readwrite => ::File::RDWR, :create => ::File::CREAT, :overwrite => ::File::TRUNC, :append => ::File::APPEND}
      modes = GxG::IO::IO::valid_mode_set({:type => :file, :read => true, :write => true, :text => true, :binary => true})
      the_mode_used = nil
      mode_string = ""
      mode_numeric = 0
      #
      if params[:mode]
        if params[:mode].index(:binary)
          if params[:mode].index(:read)
            if params[:mode].index(:write)
              mode_string = "binary_readwrite"
              mode_numeric = (mode_numeric | mode_flags[:binary] | mode_flags[:readwrite])
            else
              mode_string = "binary_read"
              mode_numeric = (mode_numeric | mode_flags[:binary] | mode_flags[:read])
            end
          else
            if params[:mode].index(:write)
              mode_string = "binary_write"
              mode_numeric = (mode_numeric | mode_flags[:binary] | mode_flags[:write])
            end
          end
        else
          if params[:mode].index(:read)
            if params[:mode].index(:write)
              mode_string = "text_readwrite"
              mode_numeric = (mode_numeric | mode_flags[:text] | mode_flags[:readwrite])
            else
              mode_string = "text_read"
              mode_numeric = (mode_numeric | mode_flags[:text] | mode_flags[:read])
            end
          else
            if params[:mode].index(:write)
              mode_string = "text_write"
              mode_numeric = (mode_numeric | mode_flags[:text] | mode_flags[:write])
            end
          end
        end
        if (params[:mode].index(:overwrite) || params[:mode].index(:truncate) )
          mode_string << "_overwrite"
          mode_numeric = (mode_numeric | mode_flags[:overwrite])
        else
          if params[:mode].index(:append)
            mode_string << "_append"
            mode_numeric = (mode_numeric | mode_flags[:append])
          end
        end
        # [:read, :write, :readwrite, :overwrite, :truncate:, :append, :text, :binary]
        modes.to_enum.each do |mode_entry|
          if (mode_entry[:synonyms].include?(mode_string.to_sym) && mode_entry[:synonyms].include?(mode_numeric))
            the_mode_used = mode_entry
            break
          end
          #
        end
        if (the_mode_used)
          options = {}
          the_mode = nil
          if params[:external_encoding]
            options[:external_encoding] = params.delete(:external_encoding)
          end
          if params[:internal_encoding]
            #If the value is nil no conversion occurs.
            if params[:internal_encoding] == params[:external_encoding]
              options[:internal_encoding] = nil
            else
              options[:internal_encoding] = params.delete(:internal_encoding)
            end
          end
          if params[:autoclose]
            options[:autoclose] = params.delete(:autoclose)
          end
          if ::File::exist?(the_path)
            the_mode = the_mode_used[:if_exist].to_i
          else
            if the_mode_used[:if_create]
              the_mode = the_mode_used[:if_create].to_i
            else
              raise ArgumentError, "you cannot operate upon a non-existent file in read-only mode"
            end
          end
          if the_mode
            file_descriptor = GxG::IO::IO::sysopen(the_path,the_mode)
          else
            file_descriptor = nil
          end
          #
          if file_descriptor
            #
            if ::File::directory?(the_path)
              raise ArgumentError, "you cannot operate upon a directory as if it were a file"
            else
              if the_permissions
                super(file_descriptor, the_mode, the_permissions, options)
              else
                super(file_descriptor, the_mode, nil, options)
              end
            end
            #
          else
            raise Exception, "unable to create a valid file descriptor with: #{args.inspect}"
          end
          #
        else
          modes_list = []
          modes.to_enum.each do |item|
            modes_list << item[:synonyms]
          end
          raise ArgumentError, "#{params[:mode].inspect} is an invalid :mode, use one of the following: #{modes_list.inspect}"
        end
      end
      #
      @path = the_path
      self
    end

### Public Instance Methods

to_path() click to toggle source

    # File lib/gxg/gxg_io.rb, line 2709
    def to_path()
      @path.dup
    end