# module GxG::Support::Library::CommonIOPrepare

### Public Instance Methods

process_parameters(*args) click to toggle source

    # File lib/gxg/gxg_io.rb, line 769
    def process_parameters(*args)
      default_object = nil
      if args[0].is_a?(Hash)
        # GxG style parameters
        raw_params = args[0]
        unless raw_params[:object].is_any?(self.process_params_classes_valid())
          unless raw_params[:file_descriptor].is_any?(self.process_params_classes_valid())
            default_object = self.process_params_default_object((raw_params[:object] || raw_params[:file_descriptor]))
            if default_object
              raw_params[:object] = default_object
            else
              raise ArgumentError, (self.class::process_params_classes_error())
            end
          end
        end
        unless raw_params[:mode].is_a?(Array)
          raise ArgumentError, "you MUST specify an Array of :mode flags (as symbols)"
        end
        raw_params[:mode].to_enum.each do |the_mode_flag|
          unless [:read, :write, :readwrite, :create, :overwrite, :truncate, :append, :text, :binary].include?(the_mode_flag)
            raw_params[:mode].delete(the_mode_flag)
          end
        end
        unless (raw_params[:mode].include?(:binary) || raw_params[:mode].include?(:text))
          raw_params[:mode] << :text
        end
      else
        # standard MRI style parameters
        raw_params = {:mode => []}
        if args[0].is_any?(self.process_params_classes_valid())
          if args[0].is_a?(::Numeric)
            raw_params[:file_descriptor] = args[0].to_i
          else
            raw_params[:object] = args[0]
          end
        else
          default_object = self.process_params_default_object(args[0])
          if default_object
            raw_params[:object] = default_object
          else
            raise ArgumentError, (self.class::process_params_classes_error())
          end
        end
        if args[2].is_a?(Hash)
          if args[2][:mode]
            raw_params[:raw_mode] = args[2][:mode]
          end
          if args[2][:binmode]
            raw_params[:mode] << :binary
          else
            unless raw_params[:mode].index(:text)
              raw_params[:mode] << :text
            end
          end
          extern_encode = nil
          intern_encode = nil
          if args[2][:encoding]
            extern_encode = args[2][:encoding]
            intern_encode = args[2][:encoding]
          end
          if args[2][:external_encoding]
            unless extern_encode
              extern_encode = args[2][:external_encoding]
            end
          end
          if args[2][:internal_encoding]
            unless intern_encode
              intern_encode = args[2][:internal_encoding]
            end
          end
          if extern_encode
            raw_params[:external_encoding] = extern_encode
          end
          if intern_encode
            raw_params[:internal_encoding] = intern_encode
          end
          if args[2][:autoclose]
            raw_params[:autoclose] = args[2][:autoclose]
          end
        end
        if args[1].is_any?(String,Numeric)
          if raw_params[:raw_mode]
            raise ArgumentError, "you specify :mode as: 2nd parameter (String || Numeric) or an option in 3rd parameter Hash, but not in both"
          else
            raw_params[:raw_mode] = args[1]
          end
          if raw_params[:raw_mode].is_a?(String)
            mode_parse = raw_params[:raw_mode].split(":")
            mode_spec = mode_parse[0].numeric_values()
            if mode_spec.is_a?(Hash)
              raw_params[:raw_mode] = mode_spec[:integer]
            else
              raw_params[:raw_mode] = mode_parse[0]
            end
            #
            if mode_parse.size > 1
              extern_encode = mode_parse[1]
              if mode_parse.size > 2
                intern_encode = mode_parse[2]
              else
                intern_encode = nil
              end
            else
              extern_encode = nil
              intern_encode = nil
            end
            # if specified also in options, it is overwritten here by mode inclusion.
            if (extern_encode == intern_encode)
              if extern_encode
                # raw_params[:encoding] = extern_encode
                raw_params[:external_encoding] = extern_encode
                raw_params[:internal_encoding] = extern_encode
              end
            else
              if extern_encode
                raw_params[:external_encoding] = extern_encode
              end
              if intern_encode
                raw_params[:internal_encoding] = intern_encode
              end
            end
            #
          else
            # numeric mode arg
            raw_params[:raw_mode] = args[1].to_i
          end
        end
        #
        unless (raw_params[:object] || raw_params[:file_descriptor])
          raise ArgumentError, "you MUST specify an IO :object or a :file_descriptor Fixnum"
        end
        #
        if raw_params[:raw_mode]
          # decipher :raw_mode
          if raw_params[:raw_mode].is_a?(Numeric)
            # Numeric
            if raw_params[:file_descriptor]
              if (raw_params[:raw_mode] & ::File::RDWR) == ::File::RDWR
                raw_params[:mode] << :read
                raw_params[:mode] << :write
              else
                if (raw_params[:raw_mode] &  ::File::WRONLY) ==  ::File::WRONLY
                  raw_params[:mode] << :write
                else
                  if (raw_params[:raw_mode] & ::File::RDONLY) == ::File::RDONLY
                    raw_params[:mode] << :read
                  end
                end
              end
              if (raw_params[:raw_mode] & ::File::TRUNC) == ::File::TRUNC
                raw_params[:mode] << :overwrite
              end
              if (raw_params[:raw_mode] & ::File::APPEND) == ::File::APPEND
                raw_params[:mode] << :append
              end
              if (raw_params[:raw_mode] & ::File::CREAT) == ::File::CREAT
                raw_params[:mode] << :append
              end
            else
              if (raw_params[:raw_mode] & ::IO::RDWR) == ::IO::RDWR
                raw_params[:mode] << :read
                raw_params[:mode] << :write
              else
                if (raw_params[:raw_mode] &  ::IO::WRONLY) ==  ::IO::WRONLY
                  raw_params[:mode] << :write
                else
                  if (raw_params[:raw_mode] & ::IO::RDONLY) == ::IO::RDONLY
                    raw_params[:mode] << :read
                  end
                end
              end
              if (raw_params[:raw_mode] & ::IO::TRUNC) == ::IO::TRUNC
                raw_params[:mode] << :overwrite
              end
              if (raw_params[:raw_mode] & ::IO::APPEND) == ::IO::APPEND
                raw_params[:mode] << :append
              end
              if (raw_params[:raw_mode] & ::IO::CREAT) == ::IO::CREAT
                raw_params[:mode] << :create
              end
            end
          else
            # String
            unless (raw_params[:mode].include?(:binary) || raw_params[:mode].include?(:text))
              if raw_params[:raw_mode].to_s.include?("b")
                raw_params[:mode] << :binary
              else
                raw_params[:mode] << :text
              end
            end
            if raw_params[:raw_mode].to_s.include?("r")
              raw_params[:mode] << :read
              if raw_params[:raw_mode].to_s.include?("+")
                raw_params[:mode] << :write
              end
            else
              if raw_params[:raw_mode].to_s.include?("w")
                raw_params[:mode] << :write
                raw_params[:mode] << :overwrite
                if raw_params[:raw_mode].to_s.include?("+")
                  raw_params[:mode] << :read
                end
              else
                if raw_params[:raw_mode].to_s.include?("a")
                  raw_params[:mode] << :write
                  raw_params[:mode] << :append
                  if raw_params[:raw_mode].to_s.include?("+")
                    raw_params[:mode] << :read
                  end
                end
              end
            end
          end
          raw_params.delete(:raw_mode)
        end
        #
      end
      #
      if raw_params[:external_encoding]
        unless ::Encoding::constants.include?(raw_params[:external_encoding].to_s.to_sym)
          raise ArgumentError, ":external_encoding :#{raw_params[:external_encoding].to_s} unsupported"
        end
      end
      if raw_params[:internal_encoding]
        unless ::Encoding::constants.include?(raw_params[:internal_encoding].to_s.to_sym)
          raise ArgumentError, ":internal_encoding :#{raw_params[:internal_encoding].to_s} unsupported"
        end
      end
      #
      raw_params
    end