Class: FortranSequential::Record
- Inherits:
-
Object
- Object
- FortranSequential::Record
- Defined in:
- ext/fortio/lib/fortio/fortran_sequential.rb
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(data = "", endian = "little", fmt = nil) ⇒ Record
constructor
A new instance of Record.
- #read(*fmts) ⇒ Object
- #rest? ⇒ Boolean
- #to_s ⇒ Object
- #write(*fmts) ⇒ Object
Constructor Details
#initialize(data = "", endian = "little", fmt = nil) ⇒ Record
Returns a new instance of Record.
93 94 95 96 97 |
# File 'ext/fortio/lib/fortio/fortran_sequential.rb', line 93 def initialize (data="", endian="little", fmt=nil) @io = StringIO.new(data) @fmt = fmt @endian = endian end |
Instance Method Details
#empty? ⇒ Boolean
103 104 105 |
# File 'ext/fortio/lib/fortio/fortran_sequential.rb', line 103 def empty? return @io.eof? end |
#read(*fmts) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'ext/fortio/lib/fortio/fortran_sequential.rb', line 111 def read (*fmts) out = [] while not fmts.empty? case fmt = fmts.shift when String list = [] specs = fmt.scan(/(?:\d+|)(?:a\[\d+\]|\w)/) while not specs.empty? case specs.shift when /(\d+)?a\[(\d+)\]/ ($1||1).to_i.times do list.push(*@io.read($2.to_i).unpack("a#{$2}")) end when /(\d+)?a/ ($1||1).to_i.times do list.push(*@io.read(1).unpack("a")) end when /(\d+)?([dDEG])/ ($1||1).to_i.times do list.push(*@io.read(8).unpack(@fmt[$2])) end when /(\d+)?([lNVefFg])/ ($1||1).to_i.times do list.push(*@io.read(4).unpack(@fmt[$2])) end when /(\d+)?([nsv])/ ($1||1).to_i.times do list.push(*@io.read(2).unpack(@fmt[$2])) end else raise "invalid format for FortranSequential::Record#read" end end if list.size == 1 out.push(list.first) else out.push(list) end when Integer n, tmpl = fmt, fmts.shift n.times do out.push(tmpl.template.load_binary(@io)) end when CArray tmpl = fmt if ENDIAN == @endian out.push(tmpl.template.load_binary(@io)) else out.push(tmpl.template.load_binary(@io).swap_bytes!) end when CA::Struct if ENDIAN == @endian out.push(CScalar.new(fmt.class).load_binary(@io)[0]) else out.push(CScalar.new(fmt.class).load_binary(@io).swap_bytes![0]) end when Class if fmt < CA::Struct if ENDIAN == @endian out.push(CScalar.new(fmt).load_binary(@io)[0]) else out.push(CScalar.new(fmt).load_binary(@io).swap_bytes![0]) end else raise "invlaid argument (format string or CArray)" end else raise "invlaid argument (format string or CArray)" end end if out.size == 1 return out.first else return out end end |
#rest? ⇒ Boolean
107 108 109 |
# File 'ext/fortio/lib/fortio/fortran_sequential.rb', line 107 def rest? return (not @io.eof?) end |
#to_s ⇒ Object
99 100 101 |
# File 'ext/fortio/lib/fortio/fortran_sequential.rb', line 99 def to_s return @io.string end |
#write(*fmts) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'ext/fortio/lib/fortio/fortran_sequential.rb', line 188 def write (*fmts) while fmt = fmts.shift if fmt.is_a?(String) argv = fmts.shift unless argv.is_a?(Array) raise "argv should be array" end argv = argv.clone end case fmt when String specs = fmt.scan(/(?:\d+|)(?:a\[\d+\]|\w)/) while not specs.empty? case specs.shift when /(\d+)?a\[(\d+)\]/ ($1||1).to_i.times do @io.write [argv.shift].pack("a#{$2}") end when /(\d+)?([adDEGefFglNVnsv])/ ($1||1).to_i.times do @io.write [argv.shift].pack(@fmt[$2]) end else raise "invalid format for FortranSequential::Record#write" end end when CArray if ENDIAN == @endian fmt.dump_binary(@io) else fmt.swap_bytes.dump_binary(@io) end when CA::Struct if RUBY_VERSION.to_f >= 1.9 if ENDIAN == @endian @io.write fmt.encode.force_encoding("ASCII-8BIT") else @io.write fmt.swap_bytes.encode.force_encoding("ASCII-8BIT") end else if ENDIAN == @endian @io.write fmt.encode else @io.write fmt.swap_bytes.encode end end else raise "invlaid argument (format string or CArray)" end end end |