Module: GPS_PVT::Util

Defined in:
lib/gps_pvt/util.rb

Defined Under Namespace

Modules: BitOp, CRC24Q

Class Method Summary collapse

Class Method Details

.get_txt(fname_or_uri) ⇒ Object



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
# File 'lib/gps_pvt/util.rb', line 78

def get_txt(fname_or_uri)
  is_uri = fname_or_uri.kind_of?(URI)
  open(fname_or_uri){|src|
    compressed = proc{
      case src.content_type
      when /gzip/; next :gz
      end if is_uri
      case fname_or_uri.to_s
      when /\.gz$/; next :gz
      when /\.Z$/; next :Z
      end
      nil
    }.call
    is_file = src.kind_of?(File) || src.kind_of?(Tempfile)

    return src.path if ((!compressed) and is_file)

    Tempfile::open(File::basename($0, '.*')){|dst|
      dst.binmode
      dst.write((compressed ? inflate(is_file ? src.path : src, compressed) : src).read)
      dst.rewind
      dst.path
    }
  }
end

.inflate(src, type = :gz) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gps_pvt/util.rb', line 64

def inflate(src, type = :gz)
  case type
  when :gz
    require 'zlib'
    Zlib::GzipReader.send(*(src.kind_of?(IO) ? [:new, src] : [:open, src]))
  when :Z
    res = IO::popen("uncompress -c #{src.kind_of?(IO) ? '-' : src}", 'r+')
    res.print(src.read) if src.kind_of?(IO)
    res.close_write
    res
  else
    raise "Unknown compression type: #{type} of #{src}"
  end
end

.open(*args, &b) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gps_pvt/util.rb', line 49

def open(*args, &b)
  return args[0].open(*args[1..-1], &b) if args[0].respond_to?(:open)
  case args[0].to_str
  when (Serial::SPEC rescue nil)
    return ((@serial_ports ||= {})[$1] ||= Serial::new($1, $2 ? $2.to_i : 115200))
  when '-'
    if (/^[wa]/ === args[1]) \
        || (args[1].kind_of?(Integer) && ((File::Constants::WRONLY & args[1]) > 0)) then
      return STDOUT
    else
      return STDIN
    end
  end rescue nil
  super
end

.special_stream?(spec) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/gps_pvt/util.rb', line 46

def special_stream?(spec)
  ['-', (Serial::SPEC rescue nil)].compact.any?{|v| v === spec}
end